]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
re PR c/60351 (Incorrect column number for warning on "right shift count is negative")
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
23a5b65a 2 Copyright (C) 1987-2014 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
ZW
28#include "coretypes.h"
29#include "tm.h"
400fbf9f 30#include "tree.h"
d8a2d370
DN
31#include "stor-layout.h"
32#include "trans-mem.h"
33#include "varasm.h"
34#include "stmt.h"
e57e265b 35#include "langhooks.h"
400fbf9f 36#include "c-tree.h"
29cc57cf 37#include "c-lang.h"
400fbf9f 38#include "flags.h"
ab87f8c8 39#include "intl.h"
672a6f42 40#include "target.h"
325c3691 41#include "tree-iterator.h"
6662d794 42#include "bitmap.h"
2fb9a547
AM
43#include "pointer-set.h"
44#include "basic-block.h"
45#include "gimple-expr.h"
45b0be94 46#include "gimplify.h"
acf0174b 47#include "tree-inline.h"
0645c1a2 48#include "omp-low.h"
61d3ce20 49#include "c-family/c-objc.h"
a212e43f 50#include "c-family/c-common.h"
de5a5fa1 51#include "c-family/c-ubsan.h"
12893402 52#include "cilk.h"
325c3691 53
2ac2f164
JM
54/* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
56enum impl_conv {
57 ic_argpass,
58 ic_assign,
59 ic_init,
60 ic_return
61};
62
bc4b653b
JM
63/* The level of nesting inside "__alignof__". */
64int in_alignof;
65
66/* The level of nesting inside "sizeof". */
67int in_sizeof;
68
69/* The level of nesting inside "typeof". */
70int in_typeof;
400fbf9f 71
1a4049e7
JJ
72/* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74tree c_last_sizeof_arg;
75
b71c7f8a 76/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 77 message within this initializer. */
b71c7f8a 78static int missing_braces_mentioned;
103b7b17 79
bf730f15
RS
80static int require_constant_value;
81static int require_constant_elements;
82
58f9752a 83static bool null_pointer_constant_p (const_tree);
f55ade6e 84static tree qualify_type (tree, tree);
dc5027f4
JM
85static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
86 bool *);
744aa42f 87static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
88static int function_types_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 91static tree lookup_field (tree, tree);
81e5eca8
MP
92static int convert_arguments (location_t, vec<location_t>, tree,
93 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
94 tree);
db3927fb 95static tree pointer_diff (location_t, tree, tree);
68fca595 96static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 97 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
98static tree valid_compound_expr_initializer (tree, tree);
99static void push_string (const char *);
100static void push_member_name (tree);
f55ade6e
AJ
101static int spelling_length (void);
102static char *print_spelling (char *);
683d6ff9 103static void warning_init (int, const char *);
c2255bc4 104static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
105static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
106 bool, struct obstack *);
a1e3b3d9
LB
107static void output_pending_init_elements (int, struct obstack *);
108static int set_designator (int, struct obstack *);
109static void push_range_stack (tree, struct obstack *);
110static void add_pending_init (tree, tree, tree, bool, struct obstack *);
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
c3d5c3fa 194 if (value == error_mark_node || type == error_mark_node)
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{
5d5993dd 212 const char *type_code_string;
400fbf9f
JW
213
214 /* Avoid duplicate error message. */
215 if (TREE_CODE (type) == ERROR_MARK)
216 return;
217
218 if (value != 0 && (TREE_CODE (value) == VAR_DECL
219 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 220 error ("%qD has an incomplete type", value);
400fbf9f
JW
221 else
222 {
223 retry:
224 /* We must print an error message. Be clever about what it says. */
225
226 switch (TREE_CODE (type))
227 {
228 case RECORD_TYPE:
ab87f8c8 229 type_code_string = "struct";
400fbf9f
JW
230 break;
231
232 case UNION_TYPE:
ab87f8c8 233 type_code_string = "union";
400fbf9f
JW
234 break;
235
236 case ENUMERAL_TYPE:
ab87f8c8 237 type_code_string = "enum";
400fbf9f
JW
238 break;
239
240 case VOID_TYPE:
241 error ("invalid use of void expression");
242 return;
243
244 case ARRAY_TYPE:
245 if (TYPE_DOMAIN (type))
246 {
fba78abb
RH
247 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
248 {
249 error ("invalid use of flexible array member");
250 return;
251 }
400fbf9f
JW
252 type = TREE_TYPE (type);
253 goto retry;
254 }
255 error ("invalid use of array with unspecified bounds");
256 return;
257
258 default:
366de0ce 259 gcc_unreachable ();
400fbf9f
JW
260 }
261
262 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
263 error ("invalid use of undefined type %<%s %E%>",
264 type_code_string, TYPE_NAME (type));
400fbf9f
JW
265 else
266 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 267 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
268 }
269}
270
ab393bf1
NB
271/* Given a type, apply default promotions wrt unnamed function
272 arguments and return the new type. */
273
274tree
2f6e4e97 275c_type_promotes_to (tree type)
ab393bf1 276{
267bac10 277 tree ret = NULL_TREE;
ab393bf1 278
267bac10
JM
279 if (TYPE_MAIN_VARIANT (type) == float_type_node)
280 ret = double_type_node;
281 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
282 {
283 /* Preserve unsignedness if not really getting any wider. */
8df83eae 284 if (TYPE_UNSIGNED (type)
c22cacf3 285 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
286 ret = unsigned_type_node;
287 else
288 ret = integer_type_node;
ab393bf1
NB
289 }
290
267bac10
JM
291 if (ret != NULL_TREE)
292 return (TYPE_ATOMIC (type)
293 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
294 : ret);
295
ab393bf1
NB
296 return type;
297}
298
36c5e70a
BE
299/* Return true if between two named address spaces, whether there is a superset
300 named address space that encompasses both address spaces. If there is a
301 superset, return which address space is the superset. */
302
303static bool
304addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
305{
306 if (as1 == as2)
307 {
308 *common = as1;
309 return true;
310 }
311 else if (targetm.addr_space.subset_p (as1, as2))
312 {
313 *common = as2;
314 return true;
315 }
316 else if (targetm.addr_space.subset_p (as2, as1))
317 {
318 *common = as1;
319 return true;
320 }
321 else
322 return false;
323}
324
400fbf9f
JW
325/* Return a variant of TYPE which has all the type qualifiers of LIKE
326 as well as those of TYPE. */
327
328static tree
2f6e4e97 329qualify_type (tree type, tree like)
400fbf9f 330{
36c5e70a
BE
331 addr_space_t as_type = TYPE_ADDR_SPACE (type);
332 addr_space_t as_like = TYPE_ADDR_SPACE (like);
333 addr_space_t as_common;
334
335 /* If the two named address spaces are different, determine the common
336 superset address space. If there isn't one, raise an error. */
337 if (!addr_space_superset (as_type, as_like, &as_common))
338 {
339 as_common = as_type;
340 error ("%qT and %qT are in disjoint named address spaces",
341 type, like);
342 }
343
2f6e4e97 344 return c_build_qualified_type (type,
36c5e70a 345 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 346 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 347 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 348}
52ffd86e
MS
349
350/* Return true iff the given tree T is a variable length array. */
351
352bool
ac7d7749 353c_vla_type_p (const_tree t)
52ffd86e
MS
354{
355 if (TREE_CODE (t) == ARRAY_TYPE
356 && C_TYPE_VARIABLE_SIZE (t))
357 return true;
358 return false;
359}
400fbf9f 360\f
10bc1b1b 361/* Return the composite type of two compatible types.
5305f6d7 362
10bc1b1b
JM
363 We assume that comptypes has already been done and returned
364 nonzero; if that isn't so, this may crash. In particular, we
365 assume that qualifiers match. */
400fbf9f
JW
366
367tree
10bc1b1b 368composite_type (tree t1, tree t2)
400fbf9f 369{
b3694847
SS
370 enum tree_code code1;
371 enum tree_code code2;
4b027d16 372 tree attributes;
400fbf9f
JW
373
374 /* Save time if the two types are the same. */
375
376 if (t1 == t2) return t1;
377
378 /* If one type is nonsense, use the other. */
379 if (t1 == error_mark_node)
380 return t2;
381 if (t2 == error_mark_node)
382 return t1;
383
10bc1b1b
JM
384 code1 = TREE_CODE (t1);
385 code2 = TREE_CODE (t2);
386
d9525bec 387 /* Merge the attributes. */
5fd9b178 388 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 389
10bc1b1b
JM
390 /* If one is an enumerated type and the other is the compatible
391 integer type, the composite type might be either of the two
392 (DR#013 question 3). For consistency, use the enumerated type as
393 the composite type. */
400fbf9f 394
10bc1b1b
JM
395 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
396 return t1;
397 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
398 return t2;
75326e8c 399
366de0ce 400 gcc_assert (code1 == code2);
b6a10c9f 401
400fbf9f
JW
402 switch (code1)
403 {
400fbf9f 404 case POINTER_TYPE:
10bc1b1b 405 /* For two pointers, do this recursively on the target type. */
400fbf9f 406 {
3932261a
MM
407 tree pointed_to_1 = TREE_TYPE (t1);
408 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 409 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 410 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
411 t1 = build_type_attribute_variant (t1, attributes);
412 return qualify_type (t1, t2);
400fbf9f 413 }
400fbf9f
JW
414
415 case ARRAY_TYPE:
416 {
10bc1b1b 417 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
418 int quals;
419 tree unqual_elt;
ca8bdb78
JM
420 tree d1 = TYPE_DOMAIN (t1);
421 tree d2 = TYPE_DOMAIN (t2);
422 bool d1_variable, d2_variable;
423 bool d1_zero, d2_zero;
f6294de7 424 bool t1_complete, t2_complete;
46df2823 425
de46b2fe 426 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
427 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
428 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 429
f6294de7
JM
430 t1_complete = COMPLETE_TYPE_P (t1);
431 t2_complete = COMPLETE_TYPE_P (t2);
432
ca8bdb78
JM
433 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
434 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
435
436 d1_variable = (!d1_zero
437 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
438 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
439 d2_variable = (!d2_zero
440 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
441 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
442 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
443 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 444
400fbf9f 445 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
446 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
447 && (d2_variable || d2_zero || !d1_variable))
4b027d16 448 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
449 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
450 && (d1_variable || d1_zero || !d2_variable))
4b027d16 451 return build_type_attribute_variant (t2, attributes);
c22cacf3 452
de46b2fe
AP
453 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
454 return build_type_attribute_variant (t1, attributes);
455 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
456 return build_type_attribute_variant (t2, attributes);
c22cacf3 457
46df2823
JM
458 /* Merge the element types, and have a size if either arg has
459 one. We may have qualifiers on the element types. To set
460 up TYPE_MAIN_VARIANT correctly, we need to form the
461 composite of the unqualified types and add the qualifiers
462 back at the end. */
463 quals = TYPE_QUALS (strip_array_types (elt));
464 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
465 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
466 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
467 && (d2_variable
468 || d2_zero
469 || !d1_variable))
470 ? t1
471 : t2));
f6294de7
JM
472 /* Ensure a composite type involving a zero-length array type
473 is a zero-length type not an incomplete type. */
474 if (d1_zero && d2_zero
475 && (t1_complete || t2_complete)
476 && !COMPLETE_TYPE_P (t1))
477 {
478 TYPE_SIZE (t1) = bitsize_zero_node;
479 TYPE_SIZE_UNIT (t1) = size_zero_node;
480 }
46df2823 481 t1 = c_build_qualified_type (t1, quals);
de46b2fe 482 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
483 }
484
fcb99e7b
JJ
485 case ENUMERAL_TYPE:
486 case RECORD_TYPE:
487 case UNION_TYPE:
488 if (attributes != NULL)
489 {
490 /* Try harder not to create a new aggregate type. */
491 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
492 return t1;
493 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
494 return t2;
495 }
496 return build_type_attribute_variant (t1, attributes);
497
400fbf9f
JW
498 case FUNCTION_TYPE:
499 /* Function types: prefer the one that specified arg types.
500 If both do, merge the arg types. Also merge the return types. */
501 {
10bc1b1b 502 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
503 tree p1 = TYPE_ARG_TYPES (t1);
504 tree p2 = TYPE_ARG_TYPES (t2);
505 int len;
506 tree newargs, n;
507 int i;
508
509 /* Save space: see if the result is identical to one of the args. */
3f75a254 510 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 511 return build_type_attribute_variant (t1, attributes);
3f75a254 512 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 513 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
514
515 /* Simple way if one arg fails to specify argument types. */
516 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 517 {
fe7080d2
AP
518 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
519 t1 = build_type_attribute_variant (t1, attributes);
520 return qualify_type (t1, t2);
4b027d16 521 }
400fbf9f 522 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
523 {
524 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
525 t1 = build_type_attribute_variant (t1, attributes);
526 return qualify_type (t1, t2);
4b027d16 527 }
400fbf9f
JW
528
529 /* If both args specify argument types, we must merge the two
530 lists, argument by argument. */
2f4e8f2b 531
400fbf9f
JW
532 len = list_length (p1);
533 newargs = 0;
534
535 for (i = 0; i < len; i++)
8d9bfdc5 536 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
537
538 n = newargs;
539
540 for (; p1;
541 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
542 {
543 /* A null type means arg type is not specified.
544 Take whatever the other function type has. */
545 if (TREE_VALUE (p1) == 0)
546 {
547 TREE_VALUE (n) = TREE_VALUE (p2);
548 goto parm_done;
549 }
550 if (TREE_VALUE (p2) == 0)
551 {
552 TREE_VALUE (n) = TREE_VALUE (p1);
553 goto parm_done;
554 }
2f6e4e97 555
400fbf9f
JW
556 /* Given wait (union {union wait *u; int *i} *)
557 and wait (union wait *),
558 prefer union wait * as type of parm. */
559 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
560 && TREE_VALUE (p1) != TREE_VALUE (p2))
561 {
562 tree memb;
58cb41e6
JJ
563 tree mv2 = TREE_VALUE (p2);
564 if (mv2 && mv2 != error_mark_node
565 && TREE_CODE (mv2) != ARRAY_TYPE)
566 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 567 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 568 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
569 {
570 tree mv3 = TREE_TYPE (memb);
571 if (mv3 && mv3 != error_mark_node
572 && TREE_CODE (mv3) != ARRAY_TYPE)
573 mv3 = TYPE_MAIN_VARIANT (mv3);
574 if (comptypes (mv3, mv2))
575 {
576 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
577 TREE_VALUE (p2));
c1771a20 578 pedwarn (input_location, OPT_Wpedantic,
fcf73884 579 "function types not truly compatible in ISO C");
58cb41e6
JJ
580 goto parm_done;
581 }
582 }
400fbf9f
JW
583 }
584 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
585 && TREE_VALUE (p2) != TREE_VALUE (p1))
586 {
587 tree memb;
58cb41e6
JJ
588 tree mv1 = TREE_VALUE (p1);
589 if (mv1 && mv1 != error_mark_node
590 && TREE_CODE (mv1) != ARRAY_TYPE)
591 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 592 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 593 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
594 {
595 tree mv3 = TREE_TYPE (memb);
596 if (mv3 && mv3 != error_mark_node
597 && TREE_CODE (mv3) != ARRAY_TYPE)
598 mv3 = TYPE_MAIN_VARIANT (mv3);
599 if (comptypes (mv3, mv1))
600 {
601 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
602 TREE_VALUE (p1));
c1771a20 603 pedwarn (input_location, OPT_Wpedantic,
fcf73884 604 "function types not truly compatible in ISO C");
58cb41e6
JJ
605 goto parm_done;
606 }
607 }
400fbf9f 608 }
10bc1b1b 609 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
610 parm_done: ;
611 }
612
4b027d16 613 t1 = build_function_type (valtype, newargs);
fe7080d2 614 t1 = qualify_type (t1, t2);
0f41302f 615 /* ... falls through ... */
400fbf9f
JW
616 }
617
618 default:
4b027d16 619 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
620 }
621
622}
10bc1b1b
JM
623
624/* Return the type of a conditional expression between pointers to
625 possibly differently qualified versions of compatible types.
626
627 We assume that comp_target_types has already been done and returned
628 nonzero; if that isn't so, this may crash. */
629
630static tree
631common_pointer_type (tree t1, tree t2)
632{
633 tree attributes;
46df2823
JM
634 tree pointed_to_1, mv1;
635 tree pointed_to_2, mv2;
10bc1b1b 636 tree target;
eb1387a0 637 unsigned target_quals;
36c5e70a
BE
638 addr_space_t as1, as2, as_common;
639 int quals1, quals2;
10bc1b1b
JM
640
641 /* Save time if the two types are the same. */
642
643 if (t1 == t2) return t1;
644
645 /* If one type is nonsense, use the other. */
646 if (t1 == error_mark_node)
647 return t2;
648 if (t2 == error_mark_node)
649 return t1;
650
366de0ce 651 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 652 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
653
654 /* Merge the attributes. */
655 attributes = targetm.merge_type_attributes (t1, t2);
656
657 /* Find the composite type of the target types, and combine the
46df2823
JM
658 qualifiers of the two types' targets. Do not lose qualifiers on
659 array element types by taking the TYPE_MAIN_VARIANT. */
660 mv1 = pointed_to_1 = TREE_TYPE (t1);
661 mv2 = pointed_to_2 = TREE_TYPE (t2);
662 if (TREE_CODE (mv1) != ARRAY_TYPE)
663 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
664 if (TREE_CODE (mv2) != ARRAY_TYPE)
665 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
666 target = composite_type (mv1, mv2);
eb1387a0
RG
667
668 /* For function types do not merge const qualifiers, but drop them
669 if used inconsistently. The middle-end uses these to mark const
670 and noreturn functions. */
36c5e70a
BE
671 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
672 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
673
eb1387a0 674 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 675 target_quals = (quals1 & quals2);
eb1387a0 676 else
36c5e70a
BE
677 target_quals = (quals1 | quals2);
678
679 /* If the two named address spaces are different, determine the common
680 superset address space. This is guaranteed to exist due to the
681 assumption that comp_target_type returned non-zero. */
682 as1 = TYPE_ADDR_SPACE (pointed_to_1);
683 as2 = TYPE_ADDR_SPACE (pointed_to_2);
684 if (!addr_space_superset (as1, as2, &as_common))
685 gcc_unreachable ();
686
687 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
688
eb1387a0 689 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
690 return build_type_attribute_variant (t1, attributes);
691}
692
693/* Return the common type for two arithmetic types under the usual
694 arithmetic conversions. The default conversions have already been
695 applied, and enumerated types converted to their compatible integer
696 types. The resulting type is unqualified and has no attributes.
697
698 This is the type for the result of most arithmetic operations
699 if the operands have the given two types. */
700
ccf7f880
JJ
701static tree
702c_common_type (tree t1, tree t2)
10bc1b1b
JM
703{
704 enum tree_code code1;
705 enum tree_code code2;
706
707 /* If one type is nonsense, use the other. */
708 if (t1 == error_mark_node)
709 return t2;
710 if (t2 == error_mark_node)
711 return t1;
712
713 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
714 t1 = TYPE_MAIN_VARIANT (t1);
715
716 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
717 t2 = TYPE_MAIN_VARIANT (t2);
718
719 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
720 t1 = build_type_attribute_variant (t1, NULL_TREE);
721
722 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
723 t2 = build_type_attribute_variant (t2, NULL_TREE);
724
725 /* Save time if the two types are the same. */
726
727 if (t1 == t2) return t1;
728
729 code1 = TREE_CODE (t1);
730 code2 = TREE_CODE (t2);
731
366de0ce 732 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
733 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
734 || code1 == INTEGER_TYPE);
366de0ce 735 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
736 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
737 || code2 == INTEGER_TYPE);
10bc1b1b 738
5fc89bfd
JJ
739 /* When one operand is a decimal float type, the other operand cannot be
740 a generic float type or a complex type. We also disallow vector types
741 here. */
742 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
743 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
744 {
745 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
746 {
747 error ("can%'t mix operands of decimal float and vector types");
748 return error_mark_node;
749 }
750 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
751 {
752 error ("can%'t mix operands of decimal float and complex types");
753 return error_mark_node;
754 }
755 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
756 {
757 error ("can%'t mix operands of decimal float and other float types");
758 return error_mark_node;
759 }
760 }
761
10bc1b1b
JM
762 /* If one type is a vector type, return that type. (How the usual
763 arithmetic conversions apply to the vector types extension is not
764 precisely specified.) */
765 if (code1 == VECTOR_TYPE)
766 return t1;
767
768 if (code2 == VECTOR_TYPE)
769 return t2;
770
771 /* If one type is complex, form the common type of the non-complex
772 components, then make that complex. Use T1 or T2 if it is the
773 required type. */
774 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
775 {
776 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
777 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 778 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
779
780 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
781 return t1;
782 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
783 return t2;
784 else
785 return build_complex_type (subtype);
786 }
787
788 /* If only one is real, use it as the result. */
789
790 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
791 return t1;
792
793 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
794 return t2;
795
9a8ce21f
JG
796 /* If both are real and either are decimal floating point types, use
797 the decimal floating point type with the greater precision. */
798
799 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
800 {
801 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
802 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
803 return dfloat128_type_node;
804 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
805 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
806 return dfloat64_type_node;
807 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
808 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
809 return dfloat32_type_node;
810 }
811
ab22c1fa
CF
812 /* Deal with fixed-point types. */
813 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
814 {
815 unsigned int unsignedp = 0, satp = 0;
816 enum machine_mode m1, m2;
817 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
818
819 m1 = TYPE_MODE (t1);
820 m2 = TYPE_MODE (t2);
821
822 /* If one input type is saturating, the result type is saturating. */
823 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
824 satp = 1;
825
826 /* If both fixed-point types are unsigned, the result type is unsigned.
827 When mixing fixed-point and integer types, follow the sign of the
828 fixed-point type.
829 Otherwise, the result type is signed. */
830 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
831 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
832 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t1))
834 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
835 && TYPE_UNSIGNED (t2)))
836 unsignedp = 1;
837
838 /* The result type is signed. */
839 if (unsignedp == 0)
840 {
841 /* If the input type is unsigned, we need to convert to the
842 signed type. */
843 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
844 {
d75d71e0 845 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
846 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
847 mclass = MODE_FRACT;
848 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
849 mclass = MODE_ACCUM;
850 else
851 gcc_unreachable ();
852 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
853 }
854 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
855 {
d75d71e0 856 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
857 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
858 mclass = MODE_FRACT;
859 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
860 mclass = MODE_ACCUM;
861 else
862 gcc_unreachable ();
863 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
864 }
865 }
866
867 if (code1 == FIXED_POINT_TYPE)
868 {
869 fbit1 = GET_MODE_FBIT (m1);
870 ibit1 = GET_MODE_IBIT (m1);
871 }
872 else
873 {
874 fbit1 = 0;
875 /* Signed integers need to subtract one sign bit. */
876 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
877 }
878
879 if (code2 == FIXED_POINT_TYPE)
880 {
881 fbit2 = GET_MODE_FBIT (m2);
882 ibit2 = GET_MODE_IBIT (m2);
883 }
884 else
885 {
886 fbit2 = 0;
887 /* Signed integers need to subtract one sign bit. */
888 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
889 }
890
891 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
892 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
893 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
894 satp);
895 }
896
10bc1b1b
JM
897 /* Both real or both integers; use the one with greater precision. */
898
899 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
900 return t1;
901 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
902 return t2;
903
904 /* Same precision. Prefer long longs to longs to ints when the
905 same precision, following the C99 rules on integer type rank
906 (which are equivalent to the C90 rules for C90 types). */
907
908 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
910 return long_long_unsigned_type_node;
911
912 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
914 {
915 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
916 return long_long_unsigned_type_node;
917 else
c22cacf3 918 return long_long_integer_type_node;
10bc1b1b
JM
919 }
920
921 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
923 return long_unsigned_type_node;
924
925 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
926 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
927 {
928 /* But preserve unsignedness from the other type,
929 since long cannot hold all the values of an unsigned int. */
930 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
931 return long_unsigned_type_node;
932 else
933 return long_integer_type_node;
934 }
935
936 /* Likewise, prefer long double to double even if same size. */
937 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
938 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
939 return long_double_type_node;
940
2531a1d9
JR
941 /* Likewise, prefer double to float even if same size.
942 We got a couple of embedded targets with 32 bit doubles, and the
943 pdp11 might have 64 bit floats. */
944 if (TYPE_MAIN_VARIANT (t1) == double_type_node
945 || TYPE_MAIN_VARIANT (t2) == double_type_node)
946 return double_type_node;
947
10bc1b1b
JM
948 /* Otherwise prefer the unsigned one. */
949
950 if (TYPE_UNSIGNED (t1))
951 return t1;
952 else
953 return t2;
954}
400fbf9f 955\f
5922c215
JM
956/* Wrapper around c_common_type that is used by c-common.c and other
957 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
958 are allowed here and are converted to their compatible integer types.
959 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
960 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
961tree
962common_type (tree t1, tree t2)
963{
964 if (TREE_CODE (t1) == ENUMERAL_TYPE)
965 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
966 if (TREE_CODE (t2) == ENUMERAL_TYPE)
967 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
968
969 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
970 if (TREE_CODE (t1) == BOOLEAN_TYPE
971 && TREE_CODE (t2) == BOOLEAN_TYPE)
972 return boolean_type_node;
973
974 /* If either type is BOOLEAN_TYPE, then return the other. */
975 if (TREE_CODE (t1) == BOOLEAN_TYPE)
976 return t2;
977 if (TREE_CODE (t2) == BOOLEAN_TYPE)
978 return t1;
979
ccf7f880
JJ
980 return c_common_type (t1, t2);
981}
f13c9b2c 982
400fbf9f
JW
983/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
984 or various other operations. Return 2 if they are compatible
985 but a warning may be needed if you use them together. */
986
987int
132da1a5 988comptypes (tree type1, tree type2)
f13c9b2c
AP
989{
990 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
991 int val;
992
dc5027f4 993 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
994 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
995
996 return val;
997}
998
999/* Like comptypes, but if it returns non-zero because enum and int are
1000 compatible, it sets *ENUM_AND_INT_P to true. */
1001
1002static int
1003comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1004{
1005 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1006 int val;
1007
dc5027f4
JM
1008 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1009 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1010
1011 return val;
1012}
1013
1014/* Like comptypes, but if it returns nonzero for different types, it
1015 sets *DIFFERENT_TYPES_P to true. */
1016
1017int
1018comptypes_check_different_types (tree type1, tree type2,
1019 bool *different_types_p)
1020{
1021 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1022 int val;
1023
1024 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1025 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1026
f13c9b2c 1027 return val;
c22cacf3
MS
1028}
1029\f
f13c9b2c
AP
1030/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1031 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1032 but a warning may be needed if you use them together. If
1033 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1034 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1035 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1036 NULL, and the types are compatible but different enough not to be
48b0b196 1037 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1038 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1039 false, but may or may not be set if the types are incompatible.
1040 This differs from comptypes, in that we don't free the seen
1041 types. */
f13c9b2c
AP
1042
1043static int
dc5027f4
JM
1044comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1045 bool *different_types_p)
400fbf9f 1046{
58f9752a
KG
1047 const_tree t1 = type1;
1048 const_tree t2 = type2;
4b027d16 1049 int attrval, val;
400fbf9f
JW
1050
1051 /* Suppress errors caused by previously reported errors. */
1052
8d47dfc5
RH
1053 if (t1 == t2 || !t1 || !t2
1054 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1055 return 1;
1056
bca63328
JM
1057 /* Enumerated types are compatible with integer types, but this is
1058 not transitive: two enumerated types in the same translation unit
1059 are compatible with each other only if they are the same type. */
400fbf9f 1060
bca63328 1061 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1062 {
1063 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1064 if (TREE_CODE (t2) != VOID_TYPE)
1065 {
1066 if (enum_and_int_p != NULL)
1067 *enum_and_int_p = true;
1068 if (different_types_p != NULL)
1069 *different_types_p = true;
1070 }
744aa42f 1071 }
bca63328 1072 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1073 {
1074 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1075 if (TREE_CODE (t1) != VOID_TYPE)
1076 {
1077 if (enum_and_int_p != NULL)
1078 *enum_and_int_p = true;
1079 if (different_types_p != NULL)
1080 *different_types_p = true;
1081 }
744aa42f 1082 }
400fbf9f
JW
1083
1084 if (t1 == t2)
1085 return 1;
1086
1087 /* Different classes of types can't be compatible. */
1088
3aeb3655
EC
1089 if (TREE_CODE (t1) != TREE_CODE (t2))
1090 return 0;
400fbf9f 1091
118a3a8b 1092 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1093
3932261a 1094 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1095 return 0;
1096
08632da2
RS
1097 /* Allow for two different type nodes which have essentially the same
1098 definition. Note that we already checked for equality of the type
38e01259 1099 qualifiers (just above). */
400fbf9f 1100
46df2823
JM
1101 if (TREE_CODE (t1) != ARRAY_TYPE
1102 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1103 return 1;
1104
4b027d16 1105 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1106 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1107 return 0;
1108
1109 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1110 val = 0;
1111
400fbf9f
JW
1112 switch (TREE_CODE (t1))
1113 {
1114 case POINTER_TYPE:
106f5de5
UW
1115 /* Do not remove mode or aliasing information. */
1116 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1117 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1118 break;
4b027d16 1119 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1120 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1121 enum_and_int_p, different_types_p));
4b027d16 1122 break;
400fbf9f
JW
1123
1124 case FUNCTION_TYPE:
dc5027f4
JM
1125 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1126 different_types_p);
4b027d16 1127 break;
400fbf9f
JW
1128
1129 case ARRAY_TYPE:
1130 {
400fbf9f
JW
1131 tree d1 = TYPE_DOMAIN (t1);
1132 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1133 bool d1_variable, d2_variable;
1134 bool d1_zero, d2_zero;
4b027d16 1135 val = 1;
400fbf9f
JW
1136
1137 /* Target types must match incl. qualifiers. */
1138 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1139 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1140 enum_and_int_p,
1141 different_types_p)))
400fbf9f
JW
1142 return 0;
1143
dc5027f4
JM
1144 if (different_types_p != NULL
1145 && (d1 == 0) != (d2 == 0))
1146 *different_types_p = true;
400fbf9f 1147 /* Sizes must match unless one is missing or variable. */
3f85558f 1148 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1149 break;
400fbf9f 1150
3f75a254
JM
1151 d1_zero = !TYPE_MAX_VALUE (d1);
1152 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1153
3f75a254 1154 d1_variable = (!d1_zero
3f85558f
RH
1155 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1156 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1157 d2_variable = (!d2_zero
3f85558f
RH
1158 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1159 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1160 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1161 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1162
dc5027f4
JM
1163 if (different_types_p != NULL
1164 && d1_variable != d2_variable)
1165 *different_types_p = true;
3f85558f
RH
1166 if (d1_variable || d2_variable)
1167 break;
1168 if (d1_zero && d2_zero)
1169 break;
1170 if (d1_zero || d2_zero
3f75a254
JM
1171 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1172 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1173 val = 0;
1174
c22cacf3 1175 break;
400fbf9f
JW
1176 }
1177
d1bd0ded 1178 case ENUMERAL_TYPE:
58393038 1179 case RECORD_TYPE:
d1bd0ded 1180 case UNION_TYPE:
766beae1 1181 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1182 {
fcb99e7b
JJ
1183 tree a1 = TYPE_ATTRIBUTES (t1);
1184 tree a2 = TYPE_ATTRIBUTES (t2);
1185
1186 if (! attribute_list_contained (a1, a2)
1187 && ! attribute_list_contained (a2, a1))
1188 break;
1189
f13c9b2c 1190 if (attrval != 2)
dc5027f4
JM
1191 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1192 different_types_p);
1193 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1194 different_types_p);
f13c9b2c 1195 }
4b027d16 1196 break;
e9a25f70 1197
62e1dfcf 1198 case VECTOR_TYPE:
744aa42f
ILT
1199 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1200 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1201 enum_and_int_p, different_types_p));
62e1dfcf
NC
1202 break;
1203
e9a25f70
JL
1204 default:
1205 break;
400fbf9f 1206 }
4b027d16 1207 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1208}
1209
36c5e70a
BE
1210/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1211 their qualifiers, except for named address spaces. If the pointers point to
1212 different named addresses, then we must determine if one address space is a
1213 subset of the other. */
400fbf9f
JW
1214
1215static int
744aa42f 1216comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1217{
392202b0 1218 int val;
36c5e70a
BE
1219 tree mvl = TREE_TYPE (ttl);
1220 tree mvr = TREE_TYPE (ttr);
1221 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1222 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1223 addr_space_t as_common;
744aa42f 1224 bool enum_and_int_p;
8b40563c 1225
36c5e70a
BE
1226 /* Fail if pointers point to incompatible address spaces. */
1227 if (!addr_space_superset (asl, asr, &as_common))
1228 return 0;
1229
46df2823
JM
1230 /* Do not lose qualifiers on element types of array types that are
1231 pointer targets by taking their TYPE_MAIN_VARIANT. */
46df2823 1232 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
1233 mvl = (TYPE_ATOMIC (mvl)
1234 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1235 : TYPE_MAIN_VARIANT (mvl));
46df2823 1236 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
1237 mvr = (TYPE_ATOMIC (mvr)
1238 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1239 : TYPE_MAIN_VARIANT (mvr));
744aa42f
ILT
1240 enum_and_int_p = false;
1241 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1242
fcf73884 1243 if (val == 2)
c1771a20 1244 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1245
1246 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1247 warning_at (location, OPT_Wc___compat,
1248 "pointer target types incompatible in C++");
1249
400fbf9f
JW
1250 return val;
1251}
1252\f
1253/* Subroutines of `comptypes'. */
1254
f75fbaf7
ZW
1255/* Determine whether two trees derive from the same translation unit.
1256 If the CONTEXT chain ends in a null, that tree's context is still
1257 being parsed, so if two trees have context chains ending in null,
766beae1 1258 they're in the same translation unit. */
f75fbaf7 1259int
58f9752a 1260same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1261{
1262 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1263 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1264 {
6615c446
JO
1265 case tcc_declaration:
1266 t1 = DECL_CONTEXT (t1); break;
1267 case tcc_type:
1268 t1 = TYPE_CONTEXT (t1); break;
1269 case tcc_exceptional:
1270 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1271 default: gcc_unreachable ();
766beae1
ZW
1272 }
1273
1274 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1275 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1276 {
6615c446
JO
1277 case tcc_declaration:
1278 t2 = DECL_CONTEXT (t2); break;
1279 case tcc_type:
1280 t2 = TYPE_CONTEXT (t2); break;
1281 case tcc_exceptional:
1282 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1283 default: gcc_unreachable ();
766beae1
ZW
1284 }
1285
1286 return t1 == t2;
1287}
1288
f13c9b2c 1289/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1290
f13c9b2c 1291static struct tagged_tu_seen_cache *
58f9752a 1292alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1293{
cceb1885 1294 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1295 tu->next = tagged_tu_seen_base;
1296 tu->t1 = t1;
1297 tu->t2 = t2;
c22cacf3 1298
f13c9b2c 1299 tagged_tu_seen_base = tu;
c22cacf3 1300
f13c9b2c
AP
1301 /* The C standard says that two structures in different translation
1302 units are compatible with each other only if the types of their
1303 fields are compatible (among other things). We assume that they
1304 are compatible until proven otherwise when building the cache.
1305 An example where this can occur is:
1306 struct a
1307 {
1308 struct a *next;
1309 };
1310 If we are comparing this against a similar struct in another TU,
c83eecad 1311 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1312 loop. */
1313 tu->val = 1;
1314 return tu;
1315}
d1bd0ded 1316
f13c9b2c 1317/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1318
f13c9b2c
AP
1319static void
1320free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1321{
1322 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1323 while (tu != tu_til)
1324 {
741ac903
KG
1325 const struct tagged_tu_seen_cache *const tu1
1326 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1327 tu = tu1->next;
b1d5455a 1328 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1329 }
1330 tagged_tu_seen_base = tu_til;
1331}
d1bd0ded
GK
1332
1333/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1334 compatible. If the two types are not the same (which has been
1335 checked earlier), this can only happen when multiple translation
1336 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1337 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1338 comptypes_internal. */
d1bd0ded
GK
1339
1340static int
744aa42f 1341tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1342 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1343{
1344 tree s1, s2;
1345 bool needs_warning = false;
3aeb3655 1346
d1bd0ded
GK
1347 /* We have to verify that the tags of the types are the same. This
1348 is harder than it looks because this may be a typedef, so we have
1349 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1350 typedef...
1351 In the case of compiler-created builtin structs the TYPE_DECL
1352 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1353 while (TYPE_NAME (t1)
1354 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1355 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1356 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1357
dea984dc
ILT
1358 while (TYPE_NAME (t2)
1359 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1360 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1361 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1362
1363 /* C90 didn't have the requirement that the two tags be the same. */
1364 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1365 return 0;
3aeb3655 1366
d1bd0ded
GK
1367 /* C90 didn't say what happened if one or both of the types were
1368 incomplete; we choose to follow C99 rules here, which is that they
1369 are compatible. */
1370 if (TYPE_SIZE (t1) == NULL
1371 || TYPE_SIZE (t2) == NULL)
1372 return 1;
3aeb3655 1373
d1bd0ded 1374 {
f13c9b2c 1375 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1376 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1377 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1378 return tts_i->val;
d1bd0ded 1379 }
3aeb3655 1380
d1bd0ded
GK
1381 switch (TREE_CODE (t1))
1382 {
1383 case ENUMERAL_TYPE:
1384 {
f13c9b2c 1385 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1386 /* Speed up the case where the type values are in the same order. */
1387 tree tv1 = TYPE_VALUES (t1);
1388 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1389
c22cacf3 1390 if (tv1 == tv2)
f13c9b2c
AP
1391 {
1392 return 1;
1393 }
3aeb3655 1394
c22cacf3
MS
1395 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1396 {
1397 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1398 break;
1399 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1400 {
c22cacf3 1401 tu->val = 0;
f13c9b2c
AP
1402 return 0;
1403 }
c22cacf3 1404 }
3aeb3655 1405
c22cacf3 1406 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1407 {
1408 return 1;
1409 }
c22cacf3 1410 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1411 {
1412 tu->val = 0;
1413 return 0;
1414 }
3aeb3655 1415
d1bd0ded 1416 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1417 {
1418 tu->val = 0;
1419 return 0;
1420 }
3aeb3655 1421
d1bd0ded
GK
1422 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1423 {
1424 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1425 if (s2 == NULL
1426 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1427 {
1428 tu->val = 0;
1429 return 0;
1430 }
d1bd0ded
GK
1431 }
1432 return 1;
1433 }
1434
1435 case UNION_TYPE:
1436 {
f13c9b2c 1437 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1438 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1439 {
1440 tu->val = 0;
1441 return 0;
1442 }
c22cacf3 1443
f13c9b2c
AP
1444 /* Speed up the common case where the fields are in the same order. */
1445 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1446 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1447 {
1448 int result;
c22cacf3 1449
3ae4d3cc 1450 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1451 break;
744aa42f 1452 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1453 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1454
1455 if (result != 1 && !DECL_NAME (s1))
1456 break;
f13c9b2c
AP
1457 if (result == 0)
1458 {
1459 tu->val = 0;
1460 return 0;
1461 }
1462 if (result == 2)
1463 needs_warning = true;
1464
1465 if (TREE_CODE (s1) == FIELD_DECL
1466 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1467 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1468 {
1469 tu->val = 0;
1470 return 0;
1471 }
1472 }
1473 if (!s1 && !s2)
1474 {
1475 tu->val = needs_warning ? 2 : 1;
1476 return tu->val;
1477 }
d1bd0ded 1478
910ad8de 1479 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1480 {
1481 bool ok = false;
3aeb3655 1482
910ad8de 1483 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1484 if (DECL_NAME (s1) == DECL_NAME (s2))
1485 {
1486 int result;
1487
744aa42f 1488 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1489 enum_and_int_p,
1490 different_types_p);
3ae4d3cc
AO
1491
1492 if (result != 1 && !DECL_NAME (s1))
1493 continue;
1494 if (result == 0)
1495 {
1496 tu->val = 0;
1497 return 0;
1498 }
1499 if (result == 2)
1500 needs_warning = true;
1501
1502 if (TREE_CODE (s1) == FIELD_DECL
1503 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1504 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1505 break;
3ae4d3cc
AO
1506
1507 ok = true;
1508 break;
1509 }
3f75a254 1510 if (!ok)
f13c9b2c
AP
1511 {
1512 tu->val = 0;
1513 return 0;
1514 }
d1bd0ded 1515 }
f13c9b2c
AP
1516 tu->val = needs_warning ? 2 : 10;
1517 return tu->val;
d1bd0ded
GK
1518 }
1519
1520 case RECORD_TYPE:
1521 {
c22cacf3 1522 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1523
1524 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1525 s1 && s2;
910ad8de 1526 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1527 {
1528 int result;
1529 if (TREE_CODE (s1) != TREE_CODE (s2)
1530 || DECL_NAME (s1) != DECL_NAME (s2))
1531 break;
744aa42f 1532 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1533 enum_and_int_p, different_types_p);
d1bd0ded
GK
1534 if (result == 0)
1535 break;
1536 if (result == 2)
1537 needs_warning = true;
3aeb3655 1538
d1bd0ded
GK
1539 if (TREE_CODE (s1) == FIELD_DECL
1540 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1541 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1542 break;
1543 }
d1bd0ded 1544 if (s1 && s2)
f13c9b2c
AP
1545 tu->val = 0;
1546 else
1547 tu->val = needs_warning ? 2 : 1;
1548 return tu->val;
d1bd0ded
GK
1549 }
1550
1551 default:
366de0ce 1552 gcc_unreachable ();
d1bd0ded
GK
1553 }
1554}
1555
400fbf9f
JW
1556/* Return 1 if two function types F1 and F2 are compatible.
1557 If either type specifies no argument types,
1558 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1559 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1560 the other must specify that number of self-promoting arg types.
744aa42f 1561 Otherwise, the argument types must match.
dc5027f4 1562 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1563
1564static int
744aa42f 1565function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1566 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1567{
1568 tree args1, args2;
1569 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1570 int val = 1;
1571 int val1;
a6fdc086
GK
1572 tree ret1, ret2;
1573
1574 ret1 = TREE_TYPE (f1);
1575 ret2 = TREE_TYPE (f2);
1576
e508a019
JM
1577 /* 'volatile' qualifiers on a function's return type used to mean
1578 the function is noreturn. */
1579 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1580 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1581 if (TYPE_VOLATILE (ret1))
1582 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1583 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1584 if (TYPE_VOLATILE (ret2))
1585 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1586 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1587 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1588 if (val == 0)
400fbf9f
JW
1589 return 0;
1590
1591 args1 = TYPE_ARG_TYPES (f1);
1592 args2 = TYPE_ARG_TYPES (f2);
1593
dc5027f4
JM
1594 if (different_types_p != NULL
1595 && (args1 == 0) != (args2 == 0))
1596 *different_types_p = true;
1597
400fbf9f
JW
1598 /* An unspecified parmlist matches any specified parmlist
1599 whose argument types don't need default promotions. */
1600
1601 if (args1 == 0)
1602 {
1603 if (!self_promoting_args_p (args2))
1604 return 0;
1605 /* If one of these types comes from a non-prototype fn definition,
1606 compare that with the other type's arglist.
3176a0c2 1607 If they don't match, ask for a warning (but no error). */
400fbf9f 1608 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1609 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1610 enum_and_int_p, different_types_p))
400fbf9f
JW
1611 val = 2;
1612 return val;
1613 }
1614 if (args2 == 0)
1615 {
1616 if (!self_promoting_args_p (args1))
1617 return 0;
1618 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1619 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1620 enum_and_int_p, different_types_p))
400fbf9f
JW
1621 val = 2;
1622 return val;
1623 }
1624
1625 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1626 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1627 different_types_p);
400fbf9f
JW
1628 return val1 != 1 ? val1 : val;
1629}
1630
744aa42f
ILT
1631/* Check two lists of types for compatibility, returning 0 for
1632 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1633 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1634 comptypes_internal. */
400fbf9f
JW
1635
1636static int
744aa42f 1637type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1638 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1639{
1640 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1641 int val = 1;
9d5f3e49 1642 int newval = 0;
400fbf9f
JW
1643
1644 while (1)
1645 {
46df2823 1646 tree a1, mv1, a2, mv2;
400fbf9f
JW
1647 if (args1 == 0 && args2 == 0)
1648 return val;
1649 /* If one list is shorter than the other,
1650 they fail to match. */
1651 if (args1 == 0 || args2 == 0)
1652 return 0;
46df2823
JM
1653 mv1 = a1 = TREE_VALUE (args1);
1654 mv2 = a2 = TREE_VALUE (args2);
1655 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1656 mv1 = (TYPE_ATOMIC (mv1)
1657 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1658 TYPE_QUAL_ATOMIC)
1659 : TYPE_MAIN_VARIANT (mv1));
46df2823 1660 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1661 mv2 = (TYPE_ATOMIC (mv2)
1662 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1663 TYPE_QUAL_ATOMIC)
1664 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1665 /* A null pointer instead of a type
1666 means there is supposed to be an argument
1667 but nothing is specified about what type it has.
1668 So match anything that self-promotes. */
dc5027f4
JM
1669 if (different_types_p != NULL
1670 && (a1 == 0) != (a2 == 0))
1671 *different_types_p = true;
46df2823 1672 if (a1 == 0)
400fbf9f 1673 {
46df2823 1674 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1675 return 0;
1676 }
46df2823 1677 else if (a2 == 0)
400fbf9f 1678 {
46df2823 1679 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1680 return 0;
1681 }
8f5b6d29 1682 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1683 else if (TREE_CODE (a1) == ERROR_MARK
1684 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1685 ;
dc5027f4
JM
1686 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1687 different_types_p)))
400fbf9f 1688 {
dc5027f4
JM
1689 if (different_types_p != NULL)
1690 *different_types_p = true;
400fbf9f
JW
1691 /* Allow wait (union {union wait *u; int *i} *)
1692 and wait (union wait *) to be compatible. */
46df2823
JM
1693 if (TREE_CODE (a1) == UNION_TYPE
1694 && (TYPE_NAME (a1) == 0
ebf0bf7f 1695 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1696 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1697 && tree_int_cst_equal (TYPE_SIZE (a1),
1698 TYPE_SIZE (a2)))
400fbf9f
JW
1699 {
1700 tree memb;
46df2823 1701 for (memb = TYPE_FIELDS (a1);
910ad8de 1702 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1703 {
1704 tree mv3 = TREE_TYPE (memb);
1705 if (mv3 && mv3 != error_mark_node
1706 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1707 mv3 = (TYPE_ATOMIC (mv3)
1708 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1709 TYPE_QUAL_ATOMIC)
1710 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1711 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1712 different_types_p))
58cb41e6
JJ
1713 break;
1714 }
400fbf9f
JW
1715 if (memb == 0)
1716 return 0;
1717 }
46df2823
JM
1718 else if (TREE_CODE (a2) == UNION_TYPE
1719 && (TYPE_NAME (a2) == 0
ebf0bf7f 1720 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1721 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1722 && tree_int_cst_equal (TYPE_SIZE (a2),
1723 TYPE_SIZE (a1)))
400fbf9f
JW
1724 {
1725 tree memb;
46df2823 1726 for (memb = TYPE_FIELDS (a2);
910ad8de 1727 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1728 {
1729 tree mv3 = TREE_TYPE (memb);
1730 if (mv3 && mv3 != error_mark_node
1731 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1732 mv3 = (TYPE_ATOMIC (mv3)
1733 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1734 TYPE_QUAL_ATOMIC)
1735 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1736 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1737 different_types_p))
58cb41e6
JJ
1738 break;
1739 }
400fbf9f
JW
1740 if (memb == 0)
1741 return 0;
1742 }
1743 else
1744 return 0;
1745 }
1746
1747 /* comptypes said ok, but record if it said to warn. */
1748 if (newval > val)
1749 val = newval;
1750
1751 args1 = TREE_CHAIN (args1);
1752 args2 = TREE_CHAIN (args2);
1753 }
1754}
400fbf9f 1755\f
400fbf9f
JW
1756/* Compute the size to increment a pointer by. */
1757
4e2fb7de 1758static tree
58f9752a 1759c_size_in_bytes (const_tree type)
400fbf9f
JW
1760{
1761 enum tree_code code = TREE_CODE (type);
1762
fed3cef0
RK
1763 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1764 return size_one_node;
1765
d0f062fb 1766 if (!COMPLETE_OR_VOID_TYPE_P (type))
400fbf9f
JW
1767 {
1768 error ("arithmetic on pointer to an incomplete type");
fed3cef0 1769 return size_one_node;
400fbf9f
JW
1770 }
1771
1772 /* Convert in case a char is more than one unit. */
db3927fb
AH
1773 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1774 size_int (TYPE_PRECISION (char_type_node)
1775 / BITS_PER_UNIT));
400fbf9f 1776}
400fbf9f 1777\f
400fbf9f
JW
1778/* Return either DECL or its known constant value (if it has one). */
1779
56cb9733 1780tree
2f6e4e97 1781decl_constant_value (tree decl)
400fbf9f 1782{
a7c1916a 1783 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1784 in a place where a variable is invalid. Note that DECL_INITIAL
1785 isn't valid for a PARM_DECL. */
a7c1916a 1786 current_function_decl != 0
4f976745 1787 && TREE_CODE (decl) != PARM_DECL
3f75a254 1788 && !TREE_THIS_VOLATILE (decl)
83bab8db 1789 && TREE_READONLY (decl)
400fbf9f
JW
1790 && DECL_INITIAL (decl) != 0
1791 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1792 /* This is invalid if initial value is not constant.
1793 If it has either a function call, a memory reference,
1794 or a variable, then re-evaluating it could give different results. */
1795 && TREE_CONSTANT (DECL_INITIAL (decl))
1796 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1797 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1798 return DECL_INITIAL (decl);
1799 return decl;
1800}
1801
f2a71bbc
JM
1802/* Convert the array expression EXP to a pointer. */
1803static tree
c2255bc4 1804array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1805{
f2a71bbc 1806 tree orig_exp = exp;
207bf485 1807 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1808 tree adr;
1809 tree restype = TREE_TYPE (type);
1810 tree ptrtype;
207bf485 1811
f2a71bbc 1812 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1813
f2a71bbc 1814 STRIP_TYPE_NOPS (exp);
207bf485 1815
487a92fe
JM
1816 if (TREE_NO_WARNING (orig_exp))
1817 TREE_NO_WARNING (exp) = 1;
207bf485 1818
f2a71bbc
JM
1819 ptrtype = build_pointer_type (restype);
1820
1821 if (TREE_CODE (exp) == INDIRECT_REF)
1822 return convert (ptrtype, TREE_OPERAND (exp, 0));
1823
1f37c583
JM
1824 /* In C++ array compound literals are temporary objects unless they are
1825 const or appear in namespace scope, so they are destroyed too soon
1826 to use them for much of anything (c++/53220). */
1827 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1828 {
1829 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1830 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1831 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1832 "converting an array compound literal to a pointer "
1833 "is ill-formed in C++");
1834 }
1835
c2255bc4 1836 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1837 return convert (ptrtype, adr);
1838}
207bf485 1839
f2a71bbc
JM
1840/* Convert the function expression EXP to a pointer. */
1841static tree
c2255bc4 1842function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1843{
1844 tree orig_exp = exp;
207bf485 1845
f2a71bbc 1846 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1847
f2a71bbc 1848 STRIP_TYPE_NOPS (exp);
207bf485 1849
f2a71bbc
JM
1850 if (TREE_NO_WARNING (orig_exp))
1851 TREE_NO_WARNING (exp) = 1;
207bf485 1852
c2255bc4 1853 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1854}
207bf485 1855
ebfbbdc5
JJ
1856/* Mark EXP as read, not just set, for set but not used -Wunused
1857 warning purposes. */
1858
1859void
1860mark_exp_read (tree exp)
1861{
1862 switch (TREE_CODE (exp))
1863 {
1864 case VAR_DECL:
1865 case PARM_DECL:
1866 DECL_READ_P (exp) = 1;
1867 break;
1868 case ARRAY_REF:
1869 case COMPONENT_REF:
1870 case MODIFY_EXPR:
1871 case REALPART_EXPR:
1872 case IMAGPART_EXPR:
1873 CASE_CONVERT:
1874 case ADDR_EXPR:
1875 mark_exp_read (TREE_OPERAND (exp, 0));
1876 break;
1877 case COMPOUND_EXPR:
82c3c067 1878 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1879 mark_exp_read (TREE_OPERAND (exp, 1));
1880 break;
1881 default:
1882 break;
1883 }
1884}
1885
f2a71bbc
JM
1886/* Perform the default conversion of arrays and functions to pointers.
1887 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1888 return EXP.
1889
1890 LOC is the location of the expression. */
f2a71bbc
JM
1891
1892struct c_expr
c2255bc4 1893default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1894{
1895 tree orig_exp = exp.value;
1896 tree type = TREE_TYPE (exp.value);
1897 enum tree_code code = TREE_CODE (type);
1898
1899 switch (code)
1900 {
1901 case ARRAY_TYPE:
1902 {
1903 bool not_lvalue = false;
1904 bool lvalue_array_p;
1905
1906 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1907 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1908 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1909 {
1910 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1911 not_lvalue = true;
1912 exp.value = TREE_OPERAND (exp.value, 0);
1913 }
1914
1915 if (TREE_NO_WARNING (orig_exp))
1916 TREE_NO_WARNING (exp.value) = 1;
1917
1918 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1919 if (!flag_isoc99 && !lvalue_array_p)
1920 {
1921 /* Before C99, non-lvalue arrays do not decay to pointers.
1922 Normally, using such an array would be invalid; but it can
1923 be used correctly inside sizeof or as a statement expression.
1924 Thus, do not give an error here; an error will result later. */
1925 return exp;
1926 }
1927
c2255bc4 1928 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1929 }
1930 break;
1931 case FUNCTION_TYPE:
c2255bc4 1932 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1933 break;
1934 default:
f2a71bbc 1935 break;
207bf485 1936 }
f2a71bbc 1937
207bf485
JM
1938 return exp;
1939}
1940
ebfbbdc5
JJ
1941struct c_expr
1942default_function_array_read_conversion (location_t loc, struct c_expr exp)
1943{
1944 mark_exp_read (exp.value);
1945 return default_function_array_conversion (loc, exp);
1946}
522ddfa2 1947
267bac10
JM
1948/* Return whether EXPR should be treated as an atomic lvalue for the
1949 purposes of load and store handling. */
1950
1951static bool
1952really_atomic_lvalue (tree expr)
1953{
1954 if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node)
1955 return false;
1956 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1957 return false;
1958 if (!lvalue_p (expr))
1959 return false;
1960
1961 /* Ignore _Atomic on register variables, since their addresses can't
1962 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1963 sequences wouldn't work. Ignore _Atomic on structures containing
1964 bit-fields, since accessing elements of atomic structures or
1965 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1966 it's undefined at translation time or execution time, and the
1967 normal atomic sequences again wouldn't work. */
1968 while (handled_component_p (expr))
1969 {
1970 if (TREE_CODE (expr) == COMPONENT_REF
1971 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1972 return false;
1973 expr = TREE_OPERAND (expr, 0);
1974 }
1975 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1976 return false;
1977 return true;
1978}
1979
1980/* Convert expression EXP (location LOC) from lvalue to rvalue,
1981 including converting functions and arrays to pointers if CONVERT_P.
1982 If READ_P, also mark the expression as having been read. */
1983
1984struct c_expr
1985convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1986 bool convert_p, bool read_p)
1987{
1988 if (read_p)
1989 mark_exp_read (exp.value);
1990 if (convert_p)
1991 exp = default_function_array_conversion (loc, exp);
1992 if (really_atomic_lvalue (exp.value))
1993 {
1994 vec<tree, va_gc> *params;
1995 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1996 tree expr_type = TREE_TYPE (exp.value);
1997 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1998 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
1999
2000 gcc_assert (TYPE_ATOMIC (expr_type));
2001
2002 /* Expansion of a generic atomic load may require an addition
2003 element, so allocate enough to prevent a resize. */
2004 vec_alloc (params, 4);
2005
2006 /* Remove the qualifiers for the rest of the expressions and
2007 create the VAL temp variable to hold the RHS. */
2008 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2009 tmp = create_tmp_var (nonatomic_type, NULL);
2010 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2011 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2012 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2013
2014 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2015 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2016 params->quick_push (expr_addr);
2017 params->quick_push (tmp_addr);
2018 params->quick_push (seq_cst);
8edbfaa6 2019 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2020
cc28fc7f
MP
2021 /* EXPR is always read. */
2022 mark_exp_read (exp.value);
2023
267bac10
JM
2024 /* Return tmp which contains the value loaded. */
2025 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2026 }
2027 return exp;
2028}
2029
522ddfa2
JM
2030/* EXP is an expression of integer type. Apply the integer promotions
2031 to it and return the promoted value. */
400fbf9f
JW
2032
2033tree
522ddfa2 2034perform_integral_promotions (tree exp)
400fbf9f 2035{
b3694847
SS
2036 tree type = TREE_TYPE (exp);
2037 enum tree_code code = TREE_CODE (type);
400fbf9f 2038
522ddfa2 2039 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2040
400fbf9f
JW
2041 /* Normally convert enums to int,
2042 but convert wide enums to something wider. */
2043 if (code == ENUMERAL_TYPE)
2044 {
b0c48229
NB
2045 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2046 TYPE_PRECISION (integer_type_node)),
2047 ((TYPE_PRECISION (type)
2048 >= TYPE_PRECISION (integer_type_node))
8df83eae 2049 && TYPE_UNSIGNED (type)));
05bccae2 2050
400fbf9f
JW
2051 return convert (type, exp);
2052 }
2053
522ddfa2
JM
2054 /* ??? This should no longer be needed now bit-fields have their
2055 proper types. */
9753f113 2056 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2057 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2058 /* If it's thinner than an int, promote it like a
d72040f5 2059 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2060 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2061 TYPE_PRECISION (integer_type_node)))
f458d1d5 2062 return convert (integer_type_node, exp);
9753f113 2063
d72040f5 2064 if (c_promoting_integer_type_p (type))
400fbf9f 2065 {
f458d1d5 2066 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2067 if (TYPE_UNSIGNED (type)
f458d1d5 2068 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2069 return convert (unsigned_type_node, exp);
05bccae2 2070
400fbf9f
JW
2071 return convert (integer_type_node, exp);
2072 }
05bccae2 2073
522ddfa2
JM
2074 return exp;
2075}
2076
2077
2078/* Perform default promotions for C data used in expressions.
46bdb9cf 2079 Enumeral types or short or char are converted to int.
522ddfa2
JM
2080 In addition, manifest constants symbols are replaced by their values. */
2081
2082tree
2083default_conversion (tree exp)
2084{
2085 tree orig_exp;
2086 tree type = TREE_TYPE (exp);
2087 enum tree_code code = TREE_CODE (type);
40449a90 2088 tree promoted_type;
522ddfa2 2089
ebfbbdc5
JJ
2090 mark_exp_read (exp);
2091
46bdb9cf
JM
2092 /* Functions and arrays have been converted during parsing. */
2093 gcc_assert (code != FUNCTION_TYPE);
2094 if (code == ARRAY_TYPE)
2095 return exp;
522ddfa2
JM
2096
2097 /* Constants can be used directly unless they're not loadable. */
2098 if (TREE_CODE (exp) == CONST_DECL)
2099 exp = DECL_INITIAL (exp);
2100
522ddfa2
JM
2101 /* Strip no-op conversions. */
2102 orig_exp = exp;
2103 STRIP_TYPE_NOPS (exp);
2104
2105 if (TREE_NO_WARNING (orig_exp))
2106 TREE_NO_WARNING (exp) = 1;
2107
400fbf9f
JW
2108 if (code == VOID_TYPE)
2109 {
2110 error ("void value not ignored as it ought to be");
2111 return error_mark_node;
2112 }
808d6eaa
JM
2113
2114 exp = require_complete_type (exp);
2115 if (exp == error_mark_node)
2116 return error_mark_node;
2117
40449a90
SL
2118 promoted_type = targetm.promoted_type (type);
2119 if (promoted_type)
2120 return convert (promoted_type, exp);
2121
808d6eaa
JM
2122 if (INTEGRAL_TYPE_P (type))
2123 return perform_integral_promotions (exp);
2124
400fbf9f
JW
2125 return exp;
2126}
2127\f
0fb96aa4 2128/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2129
2130 If the component name is not found, returns NULL_TREE. Otherwise,
2131 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2132 stepping down the chain to the component, which is in the last
2133 TREE_VALUE of the list. Normally the list is of length one, but if
2134 the component is embedded within (nested) anonymous structures or
2135 unions, the list steps down the chain to the component. */
2f6e4e97 2136
2f2d13da 2137static tree
0fb96aa4 2138lookup_field (tree type, tree component)
2f2d13da
DE
2139{
2140 tree field;
2141
2142 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2143 to the field elements. Use a binary search on this array to quickly
2144 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2145 will always be set for structures which have many elements. */
2146
22a0b85f 2147 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2148 {
2149 int bot, top, half;
d07605f5 2150 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2151
2152 field = TYPE_FIELDS (type);
2153 bot = 0;
d07605f5 2154 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2155 while (top - bot > 1)
2156 {
2f2d13da
DE
2157 half = (top - bot + 1) >> 1;
2158 field = field_array[bot+half];
2159
2160 if (DECL_NAME (field) == NULL_TREE)
2161 {
2162 /* Step through all anon unions in linear fashion. */
2163 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2164 {
2f2d13da 2165 field = field_array[bot++];
a68b98cf
RK
2166 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2167 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2168 {
0fb96aa4 2169 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2170
2171 if (anon)
2172 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2173
2174 /* The Plan 9 compiler permits referring
2175 directly to an anonymous struct/union field
2176 using a typedef name. */
2177 if (flag_plan9_extensions
2178 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2179 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2180 == TYPE_DECL)
2181 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2182 == component))
2183 break;
2f6e4e97 2184 }
2f2d13da
DE
2185 }
2186
2187 /* Entire record is only anon unions. */
2188 if (bot > top)
2189 return NULL_TREE;
2190
2191 /* Restart the binary search, with new lower bound. */
2192 continue;
2193 }
2194
e8b87aac 2195 if (DECL_NAME (field) == component)
2f2d13da 2196 break;
e8b87aac 2197 if (DECL_NAME (field) < component)
2f2d13da
DE
2198 bot += half;
2199 else
2200 top = bot + half;
2201 }
2202
2203 if (DECL_NAME (field_array[bot]) == component)
2204 field = field_array[bot];
2205 else if (DECL_NAME (field) != component)
e9b2c823 2206 return NULL_TREE;
2f2d13da
DE
2207 }
2208 else
2209 {
910ad8de 2210 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2211 {
e9b2c823
NB
2212 if (DECL_NAME (field) == NULL_TREE
2213 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2214 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2215 {
0fb96aa4 2216 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2217
e9b2c823
NB
2218 if (anon)
2219 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2220
2221 /* The Plan 9 compiler permits referring directly to an
2222 anonymous struct/union field using a typedef
2223 name. */
2224 if (flag_plan9_extensions
2225 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2226 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2227 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2228 == component))
2229 break;
2f2d13da
DE
2230 }
2231
2232 if (DECL_NAME (field) == component)
2233 break;
2234 }
e9b2c823
NB
2235
2236 if (field == NULL_TREE)
2237 return NULL_TREE;
2f2d13da
DE
2238 }
2239
e9b2c823 2240 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2241}
2242
c2255bc4
AH
2243/* Make an expression to refer to the COMPONENT field of structure or
2244 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2245 location of the COMPONENT_REF. */
400fbf9f
JW
2246
2247tree
c2255bc4 2248build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2249{
b3694847
SS
2250 tree type = TREE_TYPE (datum);
2251 enum tree_code code = TREE_CODE (type);
2252 tree field = NULL;
2253 tree ref;
1e57bf47 2254 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2255
7a3ea201
RH
2256 if (!objc_is_public (datum, component))
2257 return error_mark_node;
2258
46a88c12 2259 /* Detect Objective-C property syntax object.property. */
668ea4b1 2260 if (c_dialect_objc ()
46a88c12 2261 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2262 return ref;
2263
400fbf9f
JW
2264 /* See if there is a field or component with name COMPONENT. */
2265
2266 if (code == RECORD_TYPE || code == UNION_TYPE)
2267 {
d0f062fb 2268 if (!COMPLETE_TYPE_P (type))
400fbf9f 2269 {
7a228918 2270 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2271 return error_mark_node;
2272 }
2273
0fb96aa4 2274 field = lookup_field (type, component);
400fbf9f
JW
2275
2276 if (!field)
2277 {
c2255bc4 2278 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2279 return error_mark_node;
2280 }
400fbf9f 2281
e9b2c823
NB
2282 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2283 This might be better solved in future the way the C++ front
2284 end does it - by giving the anonymous entities each a
2285 separate name and type, and then have build_component_ref
2286 recursively call itself. We can't do that here. */
46ea50cb 2287 do
19d76e60 2288 {
e9b2c823 2289 tree subdatum = TREE_VALUE (field);
efed193e
JM
2290 int quals;
2291 tree subtype;
1e57bf47 2292 bool use_datum_quals;
e9b2c823
NB
2293
2294 if (TREE_TYPE (subdatum) == error_mark_node)
2295 return error_mark_node;
2296
1e57bf47
JM
2297 /* If this is an rvalue, it does not have qualifiers in C
2298 standard terms and we must avoid propagating such
2299 qualifiers down to a non-lvalue array that is then
2300 converted to a pointer. */
2301 use_datum_quals = (datum_lvalue
2302 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2303
efed193e 2304 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2305 if (use_datum_quals)
2306 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2307 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2308
2309 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2310 NULL_TREE);
c2255bc4 2311 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2312 if (TREE_READONLY (subdatum)
2313 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2314 TREE_READONLY (ref) = 1;
1e57bf47
JM
2315 if (TREE_THIS_VOLATILE (subdatum)
2316 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2317 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2318
2319 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2320 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2321
19d76e60 2322 datum = ref;
46ea50cb
RS
2323
2324 field = TREE_CHAIN (field);
19d76e60 2325 }
46ea50cb 2326 while (field);
19d76e60 2327
400fbf9f
JW
2328 return ref;
2329 }
2330 else if (code != ERROR_MARK)
c2255bc4
AH
2331 error_at (loc,
2332 "request for member %qE in something not a structure or union",
2333 component);
400fbf9f
JW
2334
2335 return error_mark_node;
2336}
2337\f
2338/* Given an expression PTR for a pointer, return an expression
2339 for the value pointed to.
6a3799eb
AH
2340 ERRORSTRING is the name of the operator to appear in error messages.
2341
2342 LOC is the location to use for the generated tree. */
400fbf9f
JW
2343
2344tree
dd865ef6 2345build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2346{
b3694847
SS
2347 tree pointer = default_conversion (ptr);
2348 tree type = TREE_TYPE (pointer);
6a3799eb 2349 tree ref;
400fbf9f
JW
2350
2351 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2352 {
1043771b 2353 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2354 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2355 {
2356 /* If a warning is issued, mark it to avoid duplicates from
2357 the backend. This only needs to be done at
2358 warn_strict_aliasing > 2. */
2359 if (warn_strict_aliasing > 2)
2360 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2361 type, TREE_OPERAND (pointer, 0)))
2362 TREE_NO_WARNING (pointer) = 1;
2363 }
2364
870cc33b 2365 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2366 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2367 == TREE_TYPE (type)))
6a3799eb
AH
2368 {
2369 ref = TREE_OPERAND (pointer, 0);
2370 protected_set_expr_location (ref, loc);
2371 return ref;
2372 }
870cc33b
RS
2373 else
2374 {
2375 tree t = TREE_TYPE (type);
46df2823 2376
984dfd8c 2377 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2378
baae9b65 2379 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2380 {
c9f9eb5d 2381 error_at (loc, "dereferencing pointer to incomplete type");
870cc33b
RS
2382 return error_mark_node;
2383 }
7d882b83 2384 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2385 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2386
2387 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2388 so that we get the proper error message if the result is used
2389 to assign to. Also, &* is supposed to be a no-op.
2390 And ANSI C seems to specify that the type of the result
2391 should be the const type. */
2392 /* A de-reference of a pointer to const is not a const. It is valid
2393 to change it via some other pointer. */
2394 TREE_READONLY (ref) = TYPE_READONLY (t);
2395 TREE_SIDE_EFFECTS (ref)
271bd540 2396 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2397 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2398 protected_set_expr_location (ref, loc);
870cc33b
RS
2399 return ref;
2400 }
2401 }
400fbf9f 2402 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2403 invalid_indirection_error (loc, type, errstring);
2404
400fbf9f
JW
2405 return error_mark_node;
2406}
2407
2408/* This handles expressions of the form "a[i]", which denotes
2409 an array reference.
2410
2411 This is logically equivalent in C to *(a+i), but we may do it differently.
2412 If A is a variable or a member, we generate a primitive ARRAY_REF.
2413 This avoids forcing the array out of registers, and can work on
2414 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2415 by functions).
2416
30cd1c5d
AS
2417 For vector types, allow vector[i] but not i[vector], and create
2418 *(((type*)&vectortype) + i) for the expression.
2419
6a3799eb 2420 LOC is the location to use for the returned expression. */
400fbf9f
JW
2421
2422tree
c2255bc4 2423build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2424{
6a3799eb 2425 tree ret;
a4ab7973 2426 bool swapped = false;
400fbf9f
JW
2427 if (TREE_TYPE (array) == error_mark_node
2428 || TREE_TYPE (index) == error_mark_node)
2429 return error_mark_node;
2430
b72271b9 2431 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2432 {
2433 size_t rank = 0;
2434 if (!find_rank (loc, index, index, true, &rank))
2435 return error_mark_node;
2436 if (rank > 1)
2437 {
2438 error_at (loc, "rank of the array's index is greater than 1");
2439 return error_mark_node;
2440 }
2441 }
a4ab7973 2442 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2443 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2444 /* Allow vector[index] but not index[vector]. */
2445 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2446 {
a4ab7973
JM
2447 tree temp;
2448 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2449 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2450 {
f90e8e2e 2451 error_at (loc,
30cd1c5d
AS
2452 "subscripted value is neither array nor pointer nor vector");
2453
fdeefd49
RS
2454 return error_mark_node;
2455 }
a4ab7973
JM
2456 temp = array;
2457 array = index;
2458 index = temp;
2459 swapped = true;
2460 }
2461
2462 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2463 {
a63068b6 2464 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2465 return error_mark_node;
2466 }
2467
2468 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2469 {
a63068b6 2470 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2471 return error_mark_node;
2472 }
2473
ff6b6641
GDR
2474 /* ??? Existing practice has been to warn only when the char
2475 index is syntactically the index, not for char[array]. */
2476 if (!swapped)
2477 warn_array_subscript_with_type_char (index);
a4ab7973
JM
2478
2479 /* Apply default promotions *after* noticing character types. */
2480 index = default_conversion (index);
2481
2482 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2483
7edaa4d2 2484 convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2485
2486 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2487 {
e4d35515 2488 tree rval, type;
fdeefd49 2489
400fbf9f
JW
2490 /* An array that is indexed by a non-constant
2491 cannot be stored in a register; we must be able to do
2492 address arithmetic on its address.
2493 Likewise an array of elements of variable size. */
2494 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2495 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2496 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2497 {
dffd7eb6 2498 if (!c_mark_addressable (array))
400fbf9f
JW
2499 return error_mark_node;
2500 }
e6d52559
JW
2501 /* An array that is indexed by a constant value which is not within
2502 the array bounds cannot be stored in a register either; because we
2503 would get a crash in store_bit_field/extract_bit_field when trying
2504 to access a non-existent part of the register. */
2505 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2506 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2507 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2508 {
dffd7eb6 2509 if (!c_mark_addressable (array))
e6d52559
JW
2510 return error_mark_node;
2511 }
400fbf9f 2512
400fbf9f
JW
2513 if (pedantic)
2514 {
2515 tree foo = array;
2516 while (TREE_CODE (foo) == COMPONENT_REF)
2517 foo = TREE_OPERAND (foo, 0);
5baeaac0 2518 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2519 pedwarn (loc, OPT_Wpedantic,
fcf73884 2520 "ISO C forbids subscripting %<register%> array");
3f75a254 2521 else if (!flag_isoc99 && !lvalue_p (foo))
c1771a20 2522 pedwarn (loc, OPT_Wpedantic,
fcf73884 2523 "ISO C90 forbids subscripting non-lvalue array");
400fbf9f
JW
2524 }
2525
46df2823 2526 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2527 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2528 /* Array ref is const/volatile if the array elements are
c22cacf3 2529 or if the array is. */
400fbf9f
JW
2530 TREE_READONLY (rval)
2531 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2532 | TREE_READONLY (array));
2533 TREE_SIDE_EFFECTS (rval)
2534 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2535 | TREE_SIDE_EFFECTS (array));
2536 TREE_THIS_VOLATILE (rval)
2537 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2538 /* This was added by rms on 16 Nov 91.
2f6e4e97 2539 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2540 in an inline function.
2541 Hope it doesn't break something else. */
2542 | TREE_THIS_VOLATILE (array));
928c19bb 2543 ret = require_complete_type (rval);
6a3799eb
AH
2544 protected_set_expr_location (ret, loc);
2545 return ret;
400fbf9f 2546 }
a4ab7973
JM
2547 else
2548 {
2549 tree ar = default_conversion (array);
400fbf9f 2550
a4ab7973
JM
2551 if (ar == error_mark_node)
2552 return ar;
400fbf9f 2553
a4ab7973
JM
2554 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2555 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2556
ba47d38d 2557 return build_indirect_ref
c9f9eb5d 2558 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
dd865ef6 2559 RO_ARRAY_INDEXING);
a4ab7973 2560 }
400fbf9f
JW
2561}
2562\f
7e585d16 2563/* Build an external reference to identifier ID. FUN indicates
766beb40 2564 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2565 location of the identifier. This sets *TYPE to the type of the
2566 identifier, which is not the same as the type of the returned value
2567 for CONST_DECLs defined as enum constants. If the type of the
2568 identifier is not available, *TYPE is set to NULL. */
7e585d16 2569tree
c2255bc4 2570build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2571{
2572 tree ref;
2573 tree decl = lookup_name (id);
16b34ad6
ZL
2574
2575 /* In Objective-C, an instance variable (ivar) may be preferred to
2576 whatever lookup_name() found. */
2577 decl = objc_lookup_ivar (decl, id);
7e585d16 2578
6866c6e8 2579 *type = NULL;
339a28b9 2580 if (decl && decl != error_mark_node)
6866c6e8
ILT
2581 {
2582 ref = decl;
2583 *type = TREE_TYPE (ref);
2584 }
339a28b9
ZW
2585 else if (fun)
2586 /* Implicit function declaration. */
c2255bc4 2587 ref = implicitly_declare (loc, id);
339a28b9
ZW
2588 else if (decl == error_mark_node)
2589 /* Don't complain about something that's already been
2590 complained about. */
2591 return error_mark_node;
2592 else
2593 {
c2255bc4 2594 undeclared_variable (loc, id);
339a28b9
ZW
2595 return error_mark_node;
2596 }
7e585d16
ZW
2597
2598 if (TREE_TYPE (ref) == error_mark_node)
2599 return error_mark_node;
2600
339a28b9 2601 if (TREE_DEPRECATED (ref))
9b86d6bb 2602 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2603
ad960f56 2604 /* Recursive call does not count as usage. */
b8698a0f 2605 if (ref != current_function_decl)
ad960f56 2606 {
ad960f56
MLI
2607 TREE_USED (ref) = 1;
2608 }
7e585d16 2609
bc4b653b
JM
2610 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2611 {
2612 if (!in_sizeof && !in_typeof)
2613 C_DECL_USED (ref) = 1;
2614 else if (DECL_INITIAL (ref) == 0
2615 && DECL_EXTERNAL (ref)
2616 && !TREE_PUBLIC (ref))
2617 record_maybe_used_decl (ref);
2618 }
2619
7e585d16
ZW
2620 if (TREE_CODE (ref) == CONST_DECL)
2621 {
6193b8b7 2622 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2623
2624 if (warn_cxx_compat
2625 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2626 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2627 {
2628 warning_at (loc, OPT_Wc___compat,
2629 ("enum constant defined in struct or union "
2630 "is not visible in C++"));
2631 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2632 }
2633
7e585d16
ZW
2634 ref = DECL_INITIAL (ref);
2635 TREE_CONSTANT (ref) = 1;
2636 }
6a29edea 2637 else if (current_function_decl != 0
4b1e44be 2638 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2639 && (TREE_CODE (ref) == VAR_DECL
2640 || TREE_CODE (ref) == PARM_DECL
2641 || TREE_CODE (ref) == FUNCTION_DECL))
2642 {
2643 tree context = decl_function_context (ref);
2f6e4e97 2644
6a29edea
EB
2645 if (context != 0 && context != current_function_decl)
2646 DECL_NONLOCAL (ref) = 1;
2647 }
71113fcd
GK
2648 /* C99 6.7.4p3: An inline definition of a function with external
2649 linkage ... shall not contain a reference to an identifier with
2650 internal linkage. */
2651 else if (current_function_decl != 0
2652 && DECL_DECLARED_INLINE_P (current_function_decl)
2653 && DECL_EXTERNAL (current_function_decl)
2654 && VAR_OR_FUNCTION_DECL_P (ref)
2655 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2656 && ! TREE_PUBLIC (ref)
2657 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2658 record_inline_static (loc, current_function_decl, ref,
2659 csi_internal);
7e585d16
ZW
2660
2661 return ref;
2662}
2663
bc4b653b
JM
2664/* Record details of decls possibly used inside sizeof or typeof. */
2665struct maybe_used_decl
2666{
2667 /* The decl. */
2668 tree decl;
2669 /* The level seen at (in_sizeof + in_typeof). */
2670 int level;
2671 /* The next one at this level or above, or NULL. */
2672 struct maybe_used_decl *next;
2673};
2674
2675static struct maybe_used_decl *maybe_used_decls;
2676
2677/* Record that DECL, an undefined static function reference seen
2678 inside sizeof or typeof, might be used if the operand of sizeof is
2679 a VLA type or the operand of typeof is a variably modified
2680 type. */
2681
4e2fb7de 2682static void
bc4b653b
JM
2683record_maybe_used_decl (tree decl)
2684{
2685 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2686 t->decl = decl;
2687 t->level = in_sizeof + in_typeof;
2688 t->next = maybe_used_decls;
2689 maybe_used_decls = t;
2690}
2691
2692/* Pop the stack of decls possibly used inside sizeof or typeof. If
2693 USED is false, just discard them. If it is true, mark them used
2694 (if no longer inside sizeof or typeof) or move them to the next
2695 level up (if still inside sizeof or typeof). */
2696
2697void
2698pop_maybe_used (bool used)
2699{
2700 struct maybe_used_decl *p = maybe_used_decls;
2701 int cur_level = in_sizeof + in_typeof;
2702 while (p && p->level > cur_level)
2703 {
2704 if (used)
2705 {
2706 if (cur_level == 0)
2707 C_DECL_USED (p->decl) = 1;
2708 else
2709 p->level = cur_level;
2710 }
2711 p = p->next;
2712 }
2713 if (!used || cur_level == 0)
2714 maybe_used_decls = p;
2715}
2716
2717/* Return the result of sizeof applied to EXPR. */
2718
2719struct c_expr
c2255bc4 2720c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2721{
2722 struct c_expr ret;
ad97f4be
JM
2723 if (expr.value == error_mark_node)
2724 {
2725 ret.value = error_mark_node;
2726 ret.original_code = ERROR_MARK;
6866c6e8 2727 ret.original_type = NULL;
ad97f4be
JM
2728 pop_maybe_used (false);
2729 }
2730 else
2731 {
928c19bb
JM
2732 bool expr_const_operands = true;
2733 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2734 &expr_const_operands);
c2255bc4 2735 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2736 c_last_sizeof_arg = expr.value;
2737 ret.original_code = SIZEOF_EXPR;
6866c6e8 2738 ret.original_type = NULL;
928c19bb 2739 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2740 {
2741 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2742 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2743 folded_expr, ret.value);
2744 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2745 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2746 }
928c19bb 2747 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2748 }
bc4b653b
JM
2749 return ret;
2750}
2751
2752/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2753 name passed to sizeof (rather than the type itself). LOC is the
2754 location of the original expression. */
bc4b653b
JM
2755
2756struct c_expr
c2255bc4 2757c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2758{
2759 tree type;
2760 struct c_expr ret;
928c19bb
JM
2761 tree type_expr = NULL_TREE;
2762 bool type_expr_const = true;
2763 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2764 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2765 c_last_sizeof_arg = type;
2766 ret.original_code = SIZEOF_EXPR;
6866c6e8 2767 ret.original_type = NULL;
24070fcb
JM
2768 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2769 && c_vla_type_p (type))
928c19bb 2770 {
24070fcb
JM
2771 /* If the type is a [*] array, it is a VLA but is represented as
2772 having a size of zero. In such a case we must ensure that
2773 the result of sizeof does not get folded to a constant by
2774 c_fully_fold, because if the size is evaluated the result is
2775 not constant and so constraints on zero or negative size
2776 arrays must not be applied when this sizeof call is inside
2777 another array declarator. */
2778 if (!type_expr)
2779 type_expr = integer_zero_node;
928c19bb
JM
2780 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2781 type_expr, ret.value);
2782 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2783 }
16464cc1
VR
2784 pop_maybe_used (type != error_mark_node
2785 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2786 return ret;
2787}
2788
400fbf9f 2789/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2790 The function call is at LOC.
400fbf9f
JW
2791 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2792 TREE_VALUE of each node is a parameter-expression.
2793 FUNCTION's data type may be a function type or a pointer-to-function. */
2794
2795tree
c2255bc4 2796build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2797{
9771b263 2798 vec<tree, va_gc> *v;
bbbbb16a
ILT
2799 tree ret;
2800
9771b263 2801 vec_alloc (v, list_length (params));
bbbbb16a 2802 for (; params; params = TREE_CHAIN (params))
9771b263 2803 v->quick_push (TREE_VALUE (params));
8edbfaa6 2804 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2805 vec_free (v);
bbbbb16a
ILT
2806 return ret;
2807}
2808
ae52741c
MLI
2809/* Give a note about the location of the declaration of DECL. */
2810
2811static void inform_declaration (tree decl)
2812{
2813 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2814 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2815}
2816
bbbbb16a
ILT
2817/* Build a function call to function FUNCTION with parameters PARAMS.
2818 ORIGTYPES, if not NULL, is a vector of types; each element is
2819 either NULL or the original type of the corresponding element in
2820 PARAMS. The original type may differ from TREE_TYPE of the
2821 parameter for enums. FUNCTION's data type may be a function type
2822 or pointer-to-function. This function changes the elements of
2823 PARAMS. */
2824
2825tree
81e5eca8
MP
2826build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2827 tree function, vec<tree, va_gc> *params,
9771b263 2828 vec<tree, va_gc> *origtypes)
400fbf9f 2829{
b3694847 2830 tree fntype, fundecl = 0;
4977bab6 2831 tree name = NULL_TREE, result;
c96f4f73 2832 tree tem;
94a0dd7b
SL
2833 int nargs;
2834 tree *argarray;
b8698a0f 2835
400fbf9f 2836
fc76e425 2837 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2838 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2839
2840 /* Convert anything with function type to a pointer-to-function. */
2841 if (TREE_CODE (function) == FUNCTION_DECL)
2842 {
2843 name = DECL_NAME (function);
0a35513e
AH
2844
2845 if (flag_tm)
2846 tm_malloc_replacement (function);
a5eadacc 2847 fundecl = function;
86951993
AM
2848 /* Atomic functions have type checking/casting already done. They are
2849 often rewritten and don't match the original parameter list. */
2850 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2851 origtypes = NULL;
36536d79 2852
b72271b9 2853 if (flag_cilkplus
36536d79
BI
2854 && is_cilkplus_reduce_builtin (function))
2855 origtypes = NULL;
400fbf9f 2856 }
f2a71bbc 2857 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2858 function = function_to_pointer_conversion (loc, function);
400fbf9f 2859
6e955430
ZL
2860 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2861 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2862 if (params && !params->is_empty ())
2863 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2864
928c19bb
JM
2865 function = c_fully_fold (function, false, NULL);
2866
400fbf9f
JW
2867 fntype = TREE_TYPE (function);
2868
2869 if (TREE_CODE (fntype) == ERROR_MARK)
2870 return error_mark_node;
2871
2872 if (!(TREE_CODE (fntype) == POINTER_TYPE
2873 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2874 {
ae52741c
MLI
2875 if (!flag_diagnostics_show_caret)
2876 error_at (loc,
2877 "called object %qE is not a function or function pointer",
2878 function);
2879 else if (DECL_P (function))
2880 {
2881 error_at (loc,
2882 "called object %qD is not a function or function pointer",
2883 function);
2884 inform_declaration (function);
2885 }
2886 else
2887 error_at (loc,
2888 "called object is not a function or function pointer");
400fbf9f
JW
2889 return error_mark_node;
2890 }
2891
5ce89b2e
JM
2892 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2893 current_function_returns_abnormally = 1;
2894
400fbf9f
JW
2895 /* fntype now gets the type of function pointed to. */
2896 fntype = TREE_TYPE (fntype);
2897
ab4194da
JM
2898 /* Convert the parameters to the types declared in the
2899 function prototype, or apply default promotions. */
2900
81e5eca8
MP
2901 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2902 origtypes, function, fundecl);
ab4194da
JM
2903 if (nargs < 0)
2904 return error_mark_node;
2905
c96f4f73 2906 /* Check that the function is called through a compatible prototype.
fa337f3a 2907 If it is not, warn. */
1043771b 2908 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2909 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2910 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2911 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2912 {
2913 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
2914
2915 /* This situation leads to run-time undefined behavior. We can't,
2916 therefore, simply error unless we can prove that all possible
2917 executions of the program must execute the code. */
fa337f3a 2918 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 2919
fa337f3a
RB
2920 if (VOID_TYPE_P (return_type)
2921 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2922 pedwarn (loc, 0,
2923 "function with qualified void return type called");
2924 }
c96f4f73 2925
9771b263 2926 argarray = vec_safe_address (params);
bbbbb16a 2927
83322951
RG
2928 /* Check that arguments to builtin functions match the expectations. */
2929 if (fundecl
2930 && DECL_BUILT_IN (fundecl)
2931 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2932 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2933 return error_mark_node;
400fbf9f 2934
83322951 2935 /* Check that the arguments to the function are valid. */
dde05067 2936 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2937
928c19bb
JM
2938 if (name != NULL_TREE
2939 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2940 {
928c19bb 2941 if (require_constant_value)
b8698a0f 2942 result =
db3927fb
AH
2943 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2944 function, nargs, argarray);
928c19bb 2945 else
db3927fb
AH
2946 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2947 function, nargs, argarray);
928c19bb
JM
2948 if (TREE_CODE (result) == NOP_EXPR
2949 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2950 STRIP_TYPE_NOPS (result);
bf730f15
RS
2951 }
2952 else
db3927fb
AH
2953 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2954 function, nargs, argarray);
b0b3afb2 2955
71653180 2956 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2957 {
2958 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2959 pedwarn (loc, 0,
3ce62965
JM
2960 "function with qualified void return type called");
2961 return result;
2962 }
1eb8759b 2963 return require_complete_type (result);
400fbf9f 2964}
8edbfaa6
JJ
2965
2966/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2967
2968tree
2969c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2970 tree function, vec<tree, va_gc> *params,
2971 vec<tree, va_gc> *origtypes)
2972{
2973 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2974 STRIP_TYPE_NOPS (function);
2975
2976 /* Convert anything with function type to a pointer-to-function. */
2977 if (TREE_CODE (function) == FUNCTION_DECL)
2978 {
2979 /* Implement type-directed function overloading for builtins.
2980 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2981 handle all the type checking. The result is a complete expression
2982 that implements this function call. */
2983 tree tem = resolve_overloaded_builtin (loc, function, params);
2984 if (tem)
2985 return tem;
2986 }
2987 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
2988}
400fbf9f 2989\f
bbbbb16a
ILT
2990/* Convert the argument expressions in the vector VALUES
2991 to the types in the list TYPELIST.
400fbf9f
JW
2992
2993 If TYPELIST is exhausted, or when an element has NULL as its type,
2994 perform the default conversions.
2995
bbbbb16a
ILT
2996 ORIGTYPES is the original types of the expressions in VALUES. This
2997 holds the type of enum values which have been converted to integral
2998 types. It may be NULL.
400fbf9f 2999
03dafa61
JM
3000 FUNCTION is a tree for the called function. It is used only for
3001 error messages, where it is formatted with %qE.
400fbf9f
JW
3002
3003 This is also where warnings about wrong number of args are generated.
3004
81e5eca8
MP
3005 ARG_LOC are locations of function arguments (if any).
3006
94a0dd7b 3007 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3008 than the length of VALUES in some error situations), or -1 on
3009 failure. */
94a0dd7b
SL
3010
3011static int
81e5eca8
MP
3012convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3013 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3014 tree function, tree fundecl)
400fbf9f 3015{
bbbbb16a
ILT
3016 tree typetail, val;
3017 unsigned int parmnum;
06302a02 3018 bool error_args = false;
b5d32c25 3019 const bool type_generic = fundecl
81e5eca8 3020 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3021 bool type_generic_remove_excess_precision = false;
03dafa61 3022 tree selector;
03dafa61 3023
2ac2f164
JM
3024 /* Change pointer to function to the function itself for
3025 diagnostics. */
03dafa61
JM
3026 if (TREE_CODE (function) == ADDR_EXPR
3027 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3028 function = TREE_OPERAND (function, 0);
03dafa61
JM
3029
3030 /* Handle an ObjC selector specially for diagnostics. */
3031 selector = objc_message_selector ();
400fbf9f 3032
8ce94e44
JM
3033 /* For type-generic built-in functions, determine whether excess
3034 precision should be removed (classification) or not
3035 (comparison). */
3036 if (type_generic
3037 && DECL_BUILT_IN (fundecl)
3038 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3039 {
3040 switch (DECL_FUNCTION_CODE (fundecl))
3041 {
3042 case BUILT_IN_ISFINITE:
3043 case BUILT_IN_ISINF:
3044 case BUILT_IN_ISINF_SIGN:
3045 case BUILT_IN_ISNAN:
3046 case BUILT_IN_ISNORMAL:
3047 case BUILT_IN_FPCLASSIFY:
3048 type_generic_remove_excess_precision = true;
3049 break;
3050
3051 default:
3052 type_generic_remove_excess_precision = false;
3053 break;
3054 }
3055 }
b72271b9 3056 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3057 return vec_safe_length (values);
8ce94e44 3058
400fbf9f 3059 /* Scan the given expressions and types, producing individual
bbbbb16a 3060 converted arguments. */
400fbf9f 3061
bbbbb16a 3062 for (typetail = typelist, parmnum = 0;
9771b263 3063 values && values->iterate (parmnum, &val);
bbbbb16a 3064 ++parmnum)
400fbf9f 3065 {
b3694847 3066 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3067 tree valtype = TREE_TYPE (val);
03dafa61
JM
3068 tree rname = function;
3069 int argnum = parmnum + 1;
4d3e6fae 3070 const char *invalid_func_diag;
8ce94e44 3071 bool excess_precision = false;
928c19bb 3072 bool npc;
bbbbb16a 3073 tree parmval;
400fbf9f
JW
3074
3075 if (type == void_type_node)
3076 {
19dc6d01 3077 if (selector)
68fca595 3078 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3079 else
68fca595 3080 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3081 inform_declaration (fundecl);
94a0dd7b 3082 return parmnum;
400fbf9f
JW
3083 }
3084
03dafa61
JM
3085 if (selector && argnum > 2)
3086 {
3087 rname = selector;
3088 argnum -= 2;
3089 }
3090
928c19bb 3091 npc = null_pointer_constant_p (val);
8ce94e44
JM
3092
3093 /* If there is excess precision and a prototype, convert once to
3094 the required type rather than converting via the semantic
3095 type. Likewise without a prototype a float value represented
3096 as long double should be converted once to double. But for
3097 type-generic classification functions excess precision must
3098 be removed here. */
3099 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3100 && (type || !type_generic || !type_generic_remove_excess_precision))
3101 {
3102 val = TREE_OPERAND (val, 0);
3103 excess_precision = true;
3104 }
928c19bb 3105 val = c_fully_fold (val, false, NULL);
ed248cf7 3106 STRIP_TYPE_NOPS (val);
400fbf9f 3107
400fbf9f
JW
3108 val = require_complete_type (val);
3109
3110 if (type != 0)
3111 {
3112 /* Formal parm type is specified by a function prototype. */
400fbf9f 3113
20913689 3114 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f
JW
3115 {
3116 error ("type of formal parameter %d is incomplete", parmnum + 1);
3117 parmval = val;
3118 }
3119 else
3120 {
bbbbb16a
ILT
3121 tree origtype;
3122
d45cf215
RS
3123 /* Optionally warn about conversions that
3124 differ from the default conversions. */
05170031 3125 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3126 {
e3a64162 3127 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3128
aae43c5f 3129 if (INTEGRAL_TYPE_P (type)
8ce94e44 3130 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3131 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3132 "rather than floating due to prototype",
3133 argnum, rname);
03829ad2 3134 if (INTEGRAL_TYPE_P (type)
8ce94e44 3135 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3136 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3137 "rather than complex due to prototype",
3138 argnum, rname);
aae43c5f 3139 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3140 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3141 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3142 "rather than floating due to prototype",
3143 argnum, rname);
400fbf9f 3144 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3145 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3146 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3147 "rather than integer due to prototype",
3148 argnum, rname);
03829ad2 3149 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3150 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3151 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3152 "rather than integer due to prototype",
3153 argnum, rname);
aae43c5f 3154 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3155 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3156 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3157 "rather than complex due to prototype",
3158 argnum, rname);
aae43c5f
RK
3159 /* ??? At some point, messages should be written about
3160 conversions between complex types, but that's too messy
3161 to do now. */
d45cf215 3162 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3163 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3164 {
3165 /* Warn if any argument is passed as `float',
047de90b 3166 since without a prototype it would be `double'. */
9a8ce21f
JG
3167 if (formal_prec == TYPE_PRECISION (float_type_node)
3168 && type != dfloat32_type_node)
d4ee4d25 3169 warning (0, "passing argument %d of %qE as %<float%> "
03dafa61
JM
3170 "rather than %<double%> due to prototype",
3171 argnum, rname);
9a8ce21f
JG
3172
3173 /* Warn if mismatch between argument and prototype
3174 for decimal float types. Warn of conversions with
3175 binary float types and of precision narrowing due to
3176 prototype. */
8ce94e44 3177 else if (type != valtype
9a8ce21f
JG
3178 && (type == dfloat32_type_node
3179 || type == dfloat64_type_node
c22cacf3 3180 || type == dfloat128_type_node
8ce94e44
JM
3181 || valtype == dfloat32_type_node
3182 || valtype == dfloat64_type_node
3183 || valtype == dfloat128_type_node)
c22cacf3 3184 && (formal_prec
8ce94e44 3185 <= TYPE_PRECISION (valtype)
9a8ce21f 3186 || (type == dfloat128_type_node
8ce94e44 3187 && (valtype
c22cacf3 3188 != dfloat64_type_node
8ce94e44 3189 && (valtype
9a8ce21f
JG
3190 != dfloat32_type_node)))
3191 || (type == dfloat64_type_node
8ce94e44 3192 && (valtype
9a8ce21f
JG
3193 != dfloat32_type_node))))
3194 warning (0, "passing argument %d of %qE as %qT "
3195 "rather than %qT due to prototype",
8ce94e44 3196 argnum, rname, type, valtype);
9a8ce21f 3197
d45cf215 3198 }
3ed56f8a
KG
3199 /* Detect integer changing in width or signedness.
3200 These warnings are only activated with
05170031
MLI
3201 -Wtraditional-conversion, not with -Wtraditional. */
3202 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3203 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3204 {
d45cf215
RS
3205 tree would_have_been = default_conversion (val);
3206 tree type1 = TREE_TYPE (would_have_been);
3207
754a4d82 3208 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3209 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3210 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3211 /* No warning if function asks for enum
3212 and the actual arg is that enum type. */
3213 ;
3214 else if (formal_prec != TYPE_PRECISION (type1))
c2255bc4
AH
3215 warning (OPT_Wtraditional_conversion,
3216 "passing argument %d of %qE "
3176a0c2
DD
3217 "with different width due to prototype",
3218 argnum, rname);
8df83eae 3219 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3220 ;
800cd3b9
RS
3221 /* Don't complain if the formal parameter type
3222 is an enum, because we can't tell now whether
3223 the value was an enum--even the same enum. */
3224 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3225 ;
400fbf9f
JW
3226 else if (TREE_CODE (val) == INTEGER_CST
3227 && int_fits_type_p (val, type))
3228 /* Change in signedness doesn't matter
3229 if a constant value is unaffected. */
3230 ;
ce9895ae
RS
3231 /* If the value is extended from a narrower
3232 unsigned type, it doesn't matter whether we
3233 pass it as signed or unsigned; the value
3234 certainly is the same either way. */
8ce94e44
JM
3235 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3236 && TYPE_UNSIGNED (valtype))
ce9895ae 3237 ;
8df83eae 3238 else if (TYPE_UNSIGNED (type))
c2255bc4
AH
3239 warning (OPT_Wtraditional_conversion,
3240 "passing argument %d of %qE "
3176a0c2
DD
3241 "as unsigned due to prototype",
3242 argnum, rname);
3ed56f8a 3243 else
c2255bc4
AH
3244 warning (OPT_Wtraditional_conversion,
3245 "passing argument %d of %qE "
3176a0c2 3246 "as signed due to prototype", argnum, rname);
400fbf9f
JW
3247 }
3248 }
3249
8ce94e44
JM
3250 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3251 sake of better warnings from convert_and_check. */
3252 if (excess_precision)
3253 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3254 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
81e5eca8
MP
3255 bool arg_loc_ok = !arg_loc.is_empty ()
3256 /* Some __atomic_* builtins have additional
3257 hidden argument at position 0. */
3258 && values->length () == arg_loc.length ();
3259 parmval = convert_for_assignment (loc,
3260 arg_loc_ok ? arg_loc[parmnum]
3261 : UNKNOWN_LOCATION, type,
68fca595
MP
3262 val, origtype, ic_argpass,
3263 npc, fundecl, function,
2ac2f164 3264 parmnum + 1);
2f6e4e97 3265
61f71b34 3266 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3267 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3268 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3269 parmval = default_conversion (parmval);
400fbf9f 3270 }
400fbf9f 3271 }
8ce94e44
JM
3272 else if (TREE_CODE (valtype) == REAL_TYPE
3273 && (TYPE_PRECISION (valtype)
2531a1d9 3274 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3275 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3276 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3277 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3278 {
3279 if (type_generic)
bbbbb16a 3280 parmval = val;
b5d32c25 3281 else
0a0b3574
MM
3282 {
3283 /* Convert `float' to `double'. */
3284 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
81e5eca8 3285 warning_at (arg_loc[parmnum], OPT_Wdouble_promotion,
0a0b3574
MM
3286 "implicit conversion from %qT to %qT when passing "
3287 "argument to function",
3288 valtype, double_type_node);
3289 parmval = convert (double_type_node, val);
3290 }
b5d32c25 3291 }
8ce94e44
JM
3292 else if (excess_precision && !type_generic)
3293 /* A "double" argument with excess precision being passed
3294 without a prototype or in variable arguments. */
bbbbb16a 3295 parmval = convert (valtype, val);
c22cacf3
MS
3296 else if ((invalid_func_diag =
3297 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3298 {
3299 error (invalid_func_diag);
94a0dd7b 3300 return -1;
4d3e6fae 3301 }
400fbf9f
JW
3302 else
3303 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3304 parmval = default_conversion (val);
3305
9771b263 3306 (*values)[parmnum] = parmval;
06302a02
JJ
3307 if (parmval == error_mark_node)
3308 error_args = true;
400fbf9f
JW
3309
3310 if (typetail)
3311 typetail = TREE_CHAIN (typetail);
3312 }
3313
9771b263 3314 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3315
400fbf9f 3316 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3317 {
68fca595 3318 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3319 inform_declaration (fundecl);
3320 return -1;
3789b316 3321 }
400fbf9f 3322
06302a02 3323 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3324}
3325\f
43f6dfd3
RS
3326/* This is the entry point used by the parser to build unary operators
3327 in the input. CODE, a tree_code, specifies the unary operator, and
3328 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3329 CONVERT_EXPR for code.
3330
3331 LOC is the location to use for the tree generated.
3332*/
43f6dfd3
RS
3333
3334struct c_expr
c2255bc4 3335parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3336{
3337 struct c_expr result;
3338
c9f9eb5d 3339 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3340 result.original_code = code;
6866c6e8
ILT
3341 result.original_type = NULL;
3342
59c0753d 3343 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3344 overflow_warning (loc, result.value);
59c0753d 3345
43f6dfd3
RS
3346 return result;
3347}
3348
3349/* This is the entry point used by the parser to build binary operators
3350 in the input. CODE, a tree_code, specifies the binary operator, and
3351 ARG1 and ARG2 are the operands. In addition to constructing the
3352 expression, we check for operands that were written with other binary
ba47d38d
AH
3353 operators in a way that is likely to confuse the user.
3354
3355 LOCATION is the location of the binary operator. */
edc7c4ec 3356
487a92fe 3357struct c_expr
ba47d38d
AH
3358parser_build_binary_op (location_t location, enum tree_code code,
3359 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3360{
487a92fe 3361 struct c_expr result;
400fbf9f 3362
487a92fe
JM
3363 enum tree_code code1 = arg1.original_code;
3364 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3365 tree type1 = (arg1.original_type
3366 ? arg1.original_type
3367 : TREE_TYPE (arg1.value));
3368 tree type2 = (arg2.original_type
3369 ? arg2.original_type
3370 : TREE_TYPE (arg2.value));
400fbf9f 3371
ba47d38d
AH
3372 result.value = build_binary_op (location, code,
3373 arg1.value, arg2.value, 1);
487a92fe 3374 result.original_code = code;
6866c6e8 3375 result.original_type = NULL;
58bf601b 3376
487a92fe
JM
3377 if (TREE_CODE (result.value) == ERROR_MARK)
3378 return result;
400fbf9f 3379
ba47d38d
AH
3380 if (location != UNKNOWN_LOCATION)
3381 protected_set_expr_location (result.value, location);
3382
400fbf9f 3383 /* Check for cases such as x+y<<z which users are likely
487a92fe 3384 to misinterpret. */
400fbf9f 3385 if (warn_parentheses)
393e8e8b
MP
3386 warn_about_parentheses (location, code, code1, arg1.value, code2,
3387 arg2.value);
001af587 3388
ca409efd 3389 if (warn_logical_op)
393e8e8b 3390 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3391 code1, arg1.value, code2, arg2.value);
63a08740 3392
e994a705
RS
3393 /* Warn about comparisons against string literals, with the exception
3394 of testing for equality or inequality of a string literal with NULL. */
3395 if (code == EQ_EXPR || code == NE_EXPR)
3396 {
3397 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3398 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3399 warning_at (location, OPT_Waddress,
3400 "comparison with string literal results in unspecified behavior");
e994a705
RS
3401 }
3402 else if (TREE_CODE_CLASS (code) == tcc_comparison
3403 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3404 warning_at (location, OPT_Waddress,
3405 "comparison with string literal results in unspecified behavior");
e994a705 3406
b8698a0f
L
3407 if (TREE_OVERFLOW_P (result.value)
3408 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3409 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3410 overflow_warning (location, result.value);
400fbf9f 3411
6866c6e8
ILT
3412 /* Warn about comparisons of different enum types. */
3413 if (warn_enum_compare
3414 && TREE_CODE_CLASS (code) == tcc_comparison
3415 && TREE_CODE (type1) == ENUMERAL_TYPE
3416 && TREE_CODE (type2) == ENUMERAL_TYPE
3417 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3418 warning_at (location, OPT_Wenum_compare,
3419 "comparison between %qT and %qT",
3420 type1, type2);
3421
400fbf9f
JW
3422 return result;
3423}
3e4093b6 3424\f
3e4093b6
RS
3425/* Return a tree for the difference of pointers OP0 and OP1.
3426 The resulting tree has type int. */
293c9fdd 3427
3e4093b6 3428static tree
db3927fb 3429pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3430{
3e4093b6 3431 tree restype = ptrdiff_type_node;
36c5e70a 3432 tree result, inttype;
400fbf9f 3433
36c5e70a
BE
3434 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3435 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6
RS
3436 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3437 tree con0, con1, lit0, lit1;
3438 tree orig_op1 = op1;
400fbf9f 3439
36c5e70a
BE
3440 /* If the operands point into different address spaces, we need to
3441 explicitly convert them to pointers into the common address space
3442 before we can subtract the numerical address values. */
3443 if (as0 != as1)
3444 {
3445 addr_space_t as_common;
3446 tree common_type;
3447
3448 /* Determine the common superset address space. This is guaranteed
3449 to exist because the caller verified that comp_target_types
3450 returned non-zero. */
3451 if (!addr_space_superset (as0, as1, &as_common))
3452 gcc_unreachable ();
3453
3454 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3455 op0 = convert (common_type, op0);
3456 op1 = convert (common_type, op1);
3457 }
3458
3459 /* Determine integer type to perform computations in. This will usually
3460 be the same as the result type (ptrdiff_t), but may need to be a wider
3461 type if pointers for the address space are wider than ptrdiff_t. */
3462 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3463 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3464 else
3465 inttype = restype;
3466
3467
fcf73884 3468 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3469 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3470 "pointer of type %<void *%> used in subtraction");
3471 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3472 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3473 "pointer to a function used in subtraction");
400fbf9f 3474
3e4093b6
RS
3475 /* If the conversion to ptrdiff_type does anything like widening or
3476 converting a partial to an integral mode, we get a convert_expression
3477 that is in the way to do any simplifications.
3478 (fold-const.c doesn't know that the extra bits won't be needed.
3479 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3480 different mode in place.)
3481 So first try to find a common term here 'by hand'; we want to cover
3482 at least the cases that occur in legal static initializers. */
1043771b 3483 if (CONVERT_EXPR_P (op0)
1344f9a3
JM
3484 && (TYPE_PRECISION (TREE_TYPE (op0))
3485 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3486 con0 = TREE_OPERAND (op0, 0);
3487 else
3488 con0 = op0;
1043771b 3489 if (CONVERT_EXPR_P (op1)
1344f9a3
JM
3490 && (TYPE_PRECISION (TREE_TYPE (op1))
3491 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3492 con1 = TREE_OPERAND (op1, 0);
3493 else
3494 con1 = op1;
400fbf9f 3495
5de72424 3496 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3e4093b6
RS
3497 {
3498 lit0 = TREE_OPERAND (con0, 1);
3499 con0 = TREE_OPERAND (con0, 0);
3500 }
3501 else
3502 lit0 = integer_zero_node;
400fbf9f 3503
5de72424 3504 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
400fbf9f 3505 {
3e4093b6
RS
3506 lit1 = TREE_OPERAND (con1, 1);
3507 con1 = TREE_OPERAND (con1, 0);
400fbf9f
JW
3508 }
3509 else
3e4093b6
RS
3510 lit1 = integer_zero_node;
3511
3512 if (operand_equal_p (con0, con1, 0))
400fbf9f 3513 {
3e4093b6
RS
3514 op0 = lit0;
3515 op1 = lit1;
400fbf9f
JW
3516 }
3517
400fbf9f 3518
3e4093b6
RS
3519 /* First do the subtraction as integers;
3520 then drop through to build the divide operator.
3521 Do not do default conversions on the minus operator
3522 in case restype is a short type. */
400fbf9f 3523
db3927fb 3524 op0 = build_binary_op (loc,
36c5e70a
BE
3525 MINUS_EXPR, convert (inttype, op0),
3526 convert (inttype, op1), 0);
3e4093b6
RS
3527 /* This generates an error if op1 is pointer to incomplete type. */
3528 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3529 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3530
3e4093b6
RS
3531 /* This generates an error if op0 is pointer to incomplete type. */
3532 op1 = c_size_in_bytes (target_type);
400fbf9f 3533
f04dda30
MP
3534 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3535 error_at (loc, "arithmetic on pointer to an empty aggregate");
3536
3e4093b6 3537 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3538 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3539 op0, convert (inttype, op1));
3540
3541 /* Convert to final result type if necessary. */
3542 return convert (restype, result);
3e4093b6
RS
3543}
3544\f
267bac10
JM
3545/* Expand atomic compound assignments into an approriate sequence as
3546 specified by the C11 standard section 6.5.16.2.
3547 given
3548 _Atomic T1 E1
3549 T2 E2
3550 E1 op= E2
3551
3552 This sequence is used for all types for which these operations are
3553 supported.
3554
3555 In addition, built-in versions of the 'fe' prefixed routines may
3556 need to be invoked for floating point (real, complex or vector) when
3557 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3558
3559 T1 newval;
3560 T1 old;
3561 T1 *addr
3562 T2 val
3563 fenv_t fenv
3564
3565 addr = &E1;
3566 val = (E2);
3567 __atomic_load (addr, &old, SEQ_CST);
3568 feholdexcept (&fenv);
3569loop:
3570 newval = old op val;
3571 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3572 SEQ_CST))
3573 goto done;
3574 feclearexcept (FE_ALL_EXCEPT);
3575 goto loop:
3576done:
3577 feupdateenv (&fenv);
3578
3579 Also note that the compiler is simply issuing the generic form of
3580 the atomic operations. This requires temp(s) and has their address
3581 taken. The atomic processing is smart enough to figure out when the
3582 size of an object can utilize a lock-free version, and convert the
3583 built-in call to the appropriate lock-free routine. The optimizers
3584 will then dispose of any temps that are no longer required, and
3585 lock-free implementations are utilized as long as there is target
3586 support for the required size.
3587
3588 If the operator is NOP_EXPR, then this is a simple assignment, and
3589 an __atomic_store is issued to perform the assignment rather than
3590 the above loop.
3591
3592*/
3593
3594/* Build an atomic assignment at LOC, expanding into the proper
3595 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3596 the result of the operation, unless RETURN_OLD_P in which case
3597 return the old value of LHS (this is only for postincrement and
3598 postdecrement). */
3599static tree
3600build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3601 tree rhs, bool return_old_p)
3602{
3603 tree fndecl, func_call;
3604 vec<tree, va_gc> *params;
3605 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3606 tree old, old_addr;
3607 tree compound_stmt;
3608 tree stmt, goto_stmt;
3609 tree loop_label, loop_decl, done_label, done_decl;
3610
3611 tree lhs_type = TREE_TYPE (lhs);
3612 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3613 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3614 tree rhs_type = TREE_TYPE (rhs);
3615
3616 gcc_assert (TYPE_ATOMIC (lhs_type));
3617
3618 if (return_old_p)
3619 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3620
3621 /* Allocate enough vector items for a compare_exchange. */
3622 vec_alloc (params, 6);
3623
3624 /* Create a compound statement to hold the sequence of statements
3625 with a loop. */
3626 compound_stmt = c_begin_compound_stmt (false);
3627
3628 /* Fold the RHS if it hasn't already been folded. */
3629 if (modifycode != NOP_EXPR)
3630 rhs = c_fully_fold (rhs, false, NULL);
3631
3632 /* Remove the qualifiers for the rest of the expressions and create
3633 the VAL temp variable to hold the RHS. */
3634 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3635 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3636 val = create_tmp_var (nonatomic_rhs_type, NULL);
3637 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3638 TREE_NO_WARNING (val) = 1;
267bac10
JM
3639 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3640 SET_EXPR_LOCATION (rhs, loc);
3641 add_stmt (rhs);
3642
3643 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3644 an atomic_store. */
3645 if (modifycode == NOP_EXPR)
3646 {
3647 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3648 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3649 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3650 params->quick_push (lhs_addr);
3651 params->quick_push (rhs);
3652 params->quick_push (seq_cst);
8edbfaa6 3653 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3654 add_stmt (func_call);
3655
3656 /* Finish the compound statement. */
3657 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3658
3659 /* VAL is the value which was stored, return a COMPOUND_STMT of
3660 the statement and that value. */
3661 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3662 }
3663
3664 /* Create the variables and labels required for the op= form. */
3665 old = create_tmp_var (nonatomic_lhs_type, NULL);
3666 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3667 TREE_ADDRESSABLE (old) = 1;
3668 TREE_NO_WARNING (old) = 1;
267bac10
JM
3669
3670 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3671 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3672 TREE_ADDRESSABLE (newval) = 1;
3673
3674 loop_decl = create_artificial_label (loc);
3675 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3676
3677 done_decl = create_artificial_label (loc);
3678 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3679
3680 /* __atomic_load (addr, &old, SEQ_CST). */
3681 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3682 params->quick_push (lhs_addr);
3683 params->quick_push (old_addr);
3684 params->quick_push (seq_cst);
8edbfaa6 3685 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3686 add_stmt (func_call);
3687 params->truncate (0);
3688
3689 /* Create the expressions for floating-point environment
3690 manipulation, if required. */
3691 bool need_fenv = (flag_trapping_math
3692 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3693 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3694 if (need_fenv)
3695 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3696
3697 if (hold_call)
3698 add_stmt (hold_call);
3699
3700 /* loop: */
3701 add_stmt (loop_label);
3702
3703 /* newval = old + val; */
3704 rhs = build_binary_op (loc, modifycode, old, val, 1);
68fca595
MP
3705 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3706 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3707 NULL_TREE, 0);
3708 if (rhs != error_mark_node)
3709 {
3710 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3711 SET_EXPR_LOCATION (rhs, loc);
3712 add_stmt (rhs);
3713 }
3714
3715 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3716 goto done; */
3717 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3718 params->quick_push (lhs_addr);
3719 params->quick_push (old_addr);
3720 params->quick_push (newval_addr);
3721 params->quick_push (integer_zero_node);
3722 params->quick_push (seq_cst);
3723 params->quick_push (seq_cst);
8edbfaa6 3724 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3725
3726 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3727 SET_EXPR_LOCATION (goto_stmt, loc);
3728
3729 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3730 SET_EXPR_LOCATION (stmt, loc);
3731 add_stmt (stmt);
3732
3733 if (clear_call)
3734 add_stmt (clear_call);
3735
3736 /* goto loop; */
3737 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3738 SET_EXPR_LOCATION (goto_stmt, loc);
3739 add_stmt (goto_stmt);
3740
3741 /* done: */
3742 add_stmt (done_label);
3743
3744 if (update_call)
3745 add_stmt (update_call);
3746
3747 /* Finish the compound statement. */
3748 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3749
3750 /* NEWVAL is the value that was successfully stored, return a
3751 COMPOUND_EXPR of the statement and the appropriate value. */
3752 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3753 return_old_p ? old : newval);
3754}
3755
3e4093b6
RS
3756/* Construct and perhaps optimize a tree representation
3757 for a unary operation. CODE, a tree_code, specifies the operation
3758 and XARG is the operand.
3759 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3760 the default promotions (such as from short to int).
3761 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3762 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3763 arrays to pointers in C99.
3764
3765 LOCATION is the location of the operator. */
400fbf9f 3766
3e4093b6 3767tree
c9f9eb5d
AH
3768build_unary_op (location_t location,
3769 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3770{
3771 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3772 tree arg = xarg;
3773 tree argtype = 0;
808d6eaa 3774 enum tree_code typecode;
3e4093b6 3775 tree val;
c9f9eb5d 3776 tree ret = error_mark_node;
8ce94e44 3777 tree eptype = NULL_TREE;
3e4093b6 3778 int noconvert = flag;
4de67c26 3779 const char *invalid_op_diag;
928c19bb
JM
3780 bool int_operands;
3781
3782 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3783 if (int_operands)
3784 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3785
808d6eaa
JM
3786 if (code != ADDR_EXPR)
3787 arg = require_complete_type (arg);
3788
3789 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3790 if (typecode == ERROR_MARK)
3791 return error_mark_node;
3792 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3793 typecode = INTEGER_TYPE;
6c36d76b 3794
4de67c26
JM
3795 if ((invalid_op_diag
3796 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3797 {
c9f9eb5d 3798 error_at (location, invalid_op_diag);
4de67c26
JM
3799 return error_mark_node;
3800 }
3801
8ce94e44
JM
3802 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3803 {
3804 eptype = TREE_TYPE (arg);
3805 arg = TREE_OPERAND (arg, 0);
3806 }
3807
3e4093b6
RS
3808 switch (code)
3809 {
3810 case CONVERT_EXPR:
3811 /* This is used for unary plus, because a CONVERT_EXPR
3812 is enough to prevent anybody from looking inside for
3813 associativity, but won't generate any code. */
3814 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3815 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3816 || typecode == VECTOR_TYPE))
400fbf9f 3817 {
c9f9eb5d 3818 error_at (location, "wrong type argument to unary plus");
3e4093b6 3819 return error_mark_node;
400fbf9f 3820 }
3e4093b6
RS
3821 else if (!noconvert)
3822 arg = default_conversion (arg);
db3927fb 3823 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3824 break;
3825
3e4093b6
RS
3826 case NEGATE_EXPR:
3827 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3828 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3829 || typecode == VECTOR_TYPE))
3830 {
c9f9eb5d 3831 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3832 return error_mark_node;
3833 }
3834 else if (!noconvert)
3835 arg = default_conversion (arg);
400fbf9f
JW
3836 break;
3837
3e4093b6 3838 case BIT_NOT_EXPR:
462643f0
AP
3839 /* ~ works on integer types and non float vectors. */
3840 if (typecode == INTEGER_TYPE
3841 || (typecode == VECTOR_TYPE
3842 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3843 {
3e4093b6
RS
3844 if (!noconvert)
3845 arg = default_conversion (arg);
03d5b1f5 3846 }
3e4093b6 3847 else if (typecode == COMPLEX_TYPE)
400fbf9f 3848 {
3e4093b6 3849 code = CONJ_EXPR;
c1771a20 3850 pedwarn (location, OPT_Wpedantic,
fcf73884 3851 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3852 if (!noconvert)
3853 arg = default_conversion (arg);
3854 }
3855 else
3856 {
c9f9eb5d 3857 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3858 return error_mark_node;
400fbf9f
JW
3859 }
3860 break;
3861
3e4093b6 3862 case ABS_EXPR:
11017cc7 3863 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3864 {
c9f9eb5d 3865 error_at (location, "wrong type argument to abs");
3e4093b6 3866 return error_mark_node;
400fbf9f 3867 }
3e4093b6
RS
3868 else if (!noconvert)
3869 arg = default_conversion (arg);
400fbf9f
JW
3870 break;
3871
3e4093b6
RS
3872 case CONJ_EXPR:
3873 /* Conjugating a real value is a no-op, but allow it anyway. */
3874 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3875 || typecode == COMPLEX_TYPE))
400fbf9f 3876 {
c9f9eb5d 3877 error_at (location, "wrong type argument to conjugation");
3e4093b6 3878 return error_mark_node;
400fbf9f 3879 }
3e4093b6
RS
3880 else if (!noconvert)
3881 arg = default_conversion (arg);
400fbf9f
JW
3882 break;
3883
3e4093b6 3884 case TRUTH_NOT_EXPR:
ab22c1fa 3885 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3886 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3887 && typecode != COMPLEX_TYPE)
400fbf9f 3888 {
c9f9eb5d
AH
3889 error_at (location,
3890 "wrong type argument to unary exclamation mark");
3e4093b6 3891 return error_mark_node;
400fbf9f 3892 }
a27d595d
JM
3893 if (int_operands)
3894 {
3895 arg = c_objc_common_truthvalue_conversion (location, xarg);
3896 arg = remove_c_maybe_const_expr (arg);
3897 }
3898 else
3899 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3900 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3901 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3902 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3903 location = EXPR_LOCATION (ret);
c9f9eb5d 3904 goto return_build_unary_op;
3e4093b6 3905
3e4093b6 3906 case REALPART_EXPR:
3e4093b6 3907 case IMAGPART_EXPR:
fb52b50a
NF
3908 ret = build_real_imag_expr (location, code, arg);
3909 if (ret == error_mark_node)
3910 return error_mark_node;
8ce94e44
JM
3911 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3912 eptype = TREE_TYPE (eptype);
c9f9eb5d 3913 goto return_build_unary_op;
3e4093b6
RS
3914
3915 case PREINCREMENT_EXPR:
3916 case POSTINCREMENT_EXPR:
3917 case PREDECREMENT_EXPR:
3918 case POSTDECREMENT_EXPR:
3e4093b6 3919
928c19bb
JM
3920 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3921 {
3922 tree inner = build_unary_op (location, code,
3923 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3924 if (inner == error_mark_node)
3925 return error_mark_node;
3926 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3927 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3928 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3929 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3930 goto return_build_unary_op;
3931 }
3932
925e8657
NP
3933 /* Complain about anything that is not a true lvalue. In
3934 Objective-C, skip this check for property_refs. */
f90e8e2e 3935 if (!objc_is_property_ref (arg)
7bd11157
TT
3936 && !lvalue_or_else (location,
3937 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3938 || code == POSTINCREMENT_EXPR)
3939 ? lv_increment
3940 : lv_decrement)))
928c19bb
JM
3941 return error_mark_node;
3942
09639a83
ILT
3943 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3944 {
3945 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3946 warning_at (location, OPT_Wc___compat,
3947 "increment of enumeration value is invalid in C++");
3948 else
3949 warning_at (location, OPT_Wc___compat,
3950 "decrement of enumeration value is invalid in C++");
3951 }
3952
928c19bb
JM
3953 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3954 arg = c_fully_fold (arg, false, NULL);
3955
267bac10
JM
3956 bool atomic_op;
3957 atomic_op = really_atomic_lvalue (arg);
3958
3e4093b6
RS
3959 /* Increment or decrement the real part of the value,
3960 and don't change the imaginary part. */
3961 if (typecode == COMPLEX_TYPE)
400fbf9f 3962 {
3e4093b6
RS
3963 tree real, imag;
3964
c1771a20 3965 pedwarn (location, OPT_Wpedantic,
509c9d60 3966 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3967
267bac10
JM
3968 if (!atomic_op)
3969 {
3970 arg = stabilize_reference (arg);
3971 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3972 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3973 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3974 if (real == error_mark_node || imag == error_mark_node)
3975 return error_mark_node;
3976 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3977 real, imag);
3978 goto return_build_unary_op;
3979 }
400fbf9f 3980 }
3e4093b6
RS
3981
3982 /* Report invalid types. */
3983
ab22c1fa 3984 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 3985 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 3986 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 3987 {
3e4093b6 3988 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 3989 error_at (location, "wrong type argument to increment");
c22cacf3 3990 else
c9f9eb5d 3991 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
3992
3993 return error_mark_node;
400fbf9f 3994 }
400fbf9f 3995
3e4093b6
RS
3996 {
3997 tree inc;
400fbf9f 3998
3e4093b6
RS
3999 argtype = TREE_TYPE (arg);
4000
4001 /* Compute the increment. */
4002
4003 if (typecode == POINTER_TYPE)
4004 {
4005 /* If pointer target is an undefined struct,
4006 we just cannot know how to do the arithmetic. */
b70cef5d 4007 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4008 {
4009 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d
AH
4010 error_at (location,
4011 "increment of pointer to unknown structure");
3e4093b6 4012 else
c9f9eb5d
AH
4013 error_at (location,
4014 "decrement of pointer to unknown structure");
3e4093b6 4015 }
b70cef5d
JJ
4016 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4017 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4018 {
3e4093b6 4019 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4020 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4021 "wrong type argument to increment");
3e4093b6 4022 else
44d90fe1 4023 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4024 "wrong type argument to decrement");
3e4093b6
RS
4025 }
4026
b70cef5d 4027 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4028 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4029 }
b70cef5d 4030 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4031 {
4032 /* For signed fract types, we invert ++ to -- or
4033 -- to ++, and change inc from 1 to -1, because
4034 it is not possible to represent 1 in signed fract constants.
4035 For unsigned fract types, the result always overflows and
4036 we get an undefined (original) or the maximum value. */
4037 if (code == PREINCREMENT_EXPR)
4038 code = PREDECREMENT_EXPR;
4039 else if (code == PREDECREMENT_EXPR)
4040 code = PREINCREMENT_EXPR;
4041 else if (code == POSTINCREMENT_EXPR)
4042 code = POSTDECREMENT_EXPR;
4043 else /* code == POSTDECREMENT_EXPR */
4044 code = POSTINCREMENT_EXPR;
4045
4046 inc = integer_minus_one_node;
4047 inc = convert (argtype, inc);
4048 }
3e4093b6 4049 else
5be014d5 4050 {
241b71bb
TV
4051 inc = VECTOR_TYPE_P (argtype)
4052 ? build_one_cst (argtype)
4053 : integer_one_node;
5be014d5
AP
4054 inc = convert (argtype, inc);
4055 }
3e4093b6 4056
925e8657
NP
4057 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4058 need to ask Objective-C to build the increment or decrement
4059 expression for it. */
4060 if (objc_is_property_ref (arg))
f90e8e2e 4061 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4062 arg, inc);
4063
3e4093b6 4064 /* Report a read-only lvalue. */
f37acdf9 4065 if (TYPE_READONLY (argtype))
953ff289 4066 {
c02065fc 4067 readonly_error (location, arg,
953ff289
DN
4068 ((code == PREINCREMENT_EXPR
4069 || code == POSTINCREMENT_EXPR)
4070 ? lv_increment : lv_decrement));
4071 return error_mark_node;
4072 }
f37acdf9
JM
4073 else if (TREE_READONLY (arg))
4074 readonly_warning (arg,
4075 ((code == PREINCREMENT_EXPR
4076 || code == POSTINCREMENT_EXPR)
4077 ? lv_increment : lv_decrement));
3e4093b6 4078
267bac10
JM
4079 /* If the argument is atomic, use the special code sequences for
4080 atomic compound assignment. */
4081 if (atomic_op)
4082 {
4083 arg = stabilize_reference (arg);
4084 ret = build_atomic_assign (location, arg,
4085 ((code == PREINCREMENT_EXPR
4086 || code == POSTINCREMENT_EXPR)
4087 ? PLUS_EXPR
4088 : MINUS_EXPR),
4089 (FRACT_MODE_P (TYPE_MODE (argtype))
4090 ? inc
4091 : integer_one_node),
4092 (code == POSTINCREMENT_EXPR
4093 || code == POSTDECREMENT_EXPR));
4094 goto return_build_unary_op;
4095 }
4096
3e4093b6
RS
4097 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4098 val = boolean_increment (code, arg);
4099 else
53fb4de3 4100 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4101 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4102 if (TREE_CODE (val) != code)
6de9cd9a 4103 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4104 ret = val;
4105 goto return_build_unary_op;
3e4093b6
RS
4106 }
4107
4108 case ADDR_EXPR:
4109 /* Note that this operation never does default_conversion. */
4110
2b4b7036
JM
4111 /* The operand of unary '&' must be an lvalue (which excludes
4112 expressions of type void), or, in C99, the result of a [] or
4113 unary '*' operator. */
4114 if (VOID_TYPE_P (TREE_TYPE (arg))
4115 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4116 && (TREE_CODE (arg) != INDIRECT_REF
4117 || !flag_isoc99))
4118 pedwarn (location, 0, "taking address of expression of type %<void%>");
4119
3e4093b6
RS
4120 /* Let &* cancel out to simplify resulting code. */
4121 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 4122 {
3e4093b6
RS
4123 /* Don't let this be an lvalue. */
4124 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4125 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4126 ret = TREE_OPERAND (arg, 0);
4127 goto return_build_unary_op;
400fbf9f 4128 }
1eb8759b 4129
7c672dfc 4130 /* For &x[y], return x+y */
3e4093b6 4131 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4132 {
f2a71bbc
JM
4133 tree op0 = TREE_OPERAND (arg, 0);
4134 if (!c_mark_addressable (op0))
3e4093b6 4135 return error_mark_node;
1eb8759b 4136 }
1eb8759b 4137
3e4093b6
RS
4138 /* Anything not already handled and not a true memory reference
4139 or a non-lvalue array is an error. */
4140 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4141 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4142 return error_mark_node;
b6a10c9f 4143
928c19bb
JM
4144 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4145 folding later. */
4146 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4147 {
4148 tree inner = build_unary_op (location, code,
4149 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4150 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4151 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4152 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4153 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4154 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4155 goto return_build_unary_op;
4156 }
4157
3e4093b6
RS
4158 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4159 argtype = TREE_TYPE (arg);
400fbf9f 4160
3e4093b6 4161 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4162 to which the address will point. This is only needed
f2c1da78 4163 for function types. */
6615c446 4164 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4165 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4166 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4167 {
4168 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4169 int quals = orig_quals;
4170
4171 if (TREE_READONLY (arg))
4172 quals |= TYPE_QUAL_CONST;
4173 if (TREE_THIS_VOLATILE (arg))
4174 quals |= TYPE_QUAL_VOLATILE;
4175
f2c1da78
JM
4176 argtype = c_build_qualified_type (argtype, quals);
4177 }
400fbf9f 4178
3e4093b6
RS
4179 if (!c_mark_addressable (arg))
4180 return error_mark_node;
400fbf9f 4181
abb54d14
JM
4182 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4183 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4184
5cc200fc 4185 argtype = build_pointer_type (argtype);
5e55f99d
RH
4186
4187 /* ??? Cope with user tricks that amount to offsetof. Delete this
4188 when we have proper support for integer constant expressions. */
4189 val = get_base_address (arg);
4190 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
4191 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4192 {
cf9e9959 4193 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4194 goto return_build_unary_op;
3aa2ddb8 4195 }
5e55f99d 4196
5cc200fc 4197 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4198
c9f9eb5d
AH
4199 ret = val;
4200 goto return_build_unary_op;
400fbf9f 4201
3e4093b6 4202 default:
1344f9a3 4203 gcc_unreachable ();
3e4093b6 4204 }
400fbf9f 4205
3e4093b6
RS
4206 if (argtype == 0)
4207 argtype = TREE_TYPE (arg);
928c19bb
JM
4208 if (TREE_CODE (arg) == INTEGER_CST)
4209 ret = (require_constant_value
db3927fb
AH
4210 ? fold_build1_initializer_loc (location, code, argtype, arg)
4211 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4212 else
4213 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4214 return_build_unary_op:
4215 gcc_assert (ret != error_mark_node);
928c19bb
JM
4216 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4217 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4218 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4219 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4220 ret = note_integer_operands (ret);
8ce94e44
JM
4221 if (eptype)
4222 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4223 protected_set_expr_location (ret, location);
4224 return ret;
3e4093b6 4225}
400fbf9f 4226
3e4093b6
RS
4227/* Return nonzero if REF is an lvalue valid for this language.
4228 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4229 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4230
1e4ae551 4231bool
58f9752a 4232lvalue_p (const_tree ref)
3e4093b6 4233{
58f9752a 4234 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4235
3e4093b6
RS
4236 switch (code)
4237 {
4238 case REALPART_EXPR:
4239 case IMAGPART_EXPR:
4240 case COMPONENT_REF:
4241 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4242
928c19bb
JM
4243 case C_MAYBE_CONST_EXPR:
4244 return lvalue_p (TREE_OPERAND (ref, 1));
4245
3e4093b6
RS
4246 case COMPOUND_LITERAL_EXPR:
4247 case STRING_CST:
4248 return 1;
400fbf9f 4249
3e4093b6
RS
4250 case INDIRECT_REF:
4251 case ARRAY_REF:
36536d79 4252 case ARRAY_NOTATION_REF:
3e4093b6
RS
4253 case VAR_DECL:
4254 case PARM_DECL:
4255 case RESULT_DECL:
4256 case ERROR_MARK:
4257 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4258 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4259
3e4093b6 4260 case BIND_EXPR:
3e4093b6 4261 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4262
3e4093b6
RS
4263 default:
4264 return 0;
4265 }
4266}
400fbf9f 4267\f
f37acdf9
JM
4268/* Give a warning for storing in something that is read-only in GCC
4269 terms but not const in ISO C terms. */
4270
4271static void
4272readonly_warning (tree arg, enum lvalue_use use)
4273{
4274 switch (use)
4275 {
4276 case lv_assign:
4277 warning (0, "assignment of read-only location %qE", arg);
4278 break;
4279 case lv_increment:
4280 warning (0, "increment of read-only location %qE", arg);
4281 break;
4282 case lv_decrement:
4283 warning (0, "decrement of read-only location %qE", arg);
4284 break;
4285 default:
4286 gcc_unreachable ();
4287 }
4288 return;
4289}
4290
37dc0d8d
JM
4291
4292/* Return nonzero if REF is an lvalue valid for this language;
4293 otherwise, print an error message and return zero. USE says
7bd11157
TT
4294 how the lvalue is being used and so selects the error message.
4295 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4296
4297static int
7bd11157 4298lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4299{
4300 int win = lvalue_p (ref);
4301
4302 if (!win)
7bd11157 4303 lvalue_error (loc, use);
37dc0d8d
JM
4304
4305 return win;
4306}
3e4093b6
RS
4307\f
4308/* Mark EXP saying that we need to be able to take the
4309 address of it; it should not be allocated in a register.
4310 Returns true if successful. */
54c93c30 4311
3e4093b6
RS
4312bool
4313c_mark_addressable (tree exp)
400fbf9f 4314{
3e4093b6 4315 tree x = exp;
95602da1 4316
3e4093b6
RS
4317 while (1)
4318 switch (TREE_CODE (x))
4319 {
4320 case COMPONENT_REF:
4321 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4322 {
0039fa55
AN
4323 error
4324 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4325 return false;
4326 }
95602da1 4327
3e4093b6 4328 /* ... fall through ... */
95602da1 4329
3e4093b6
RS
4330 case ADDR_EXPR:
4331 case ARRAY_REF:
4332 case REALPART_EXPR:
4333 case IMAGPART_EXPR:
4334 x = TREE_OPERAND (x, 0);
4335 break;
95602da1 4336
3e4093b6
RS
4337 case COMPOUND_LITERAL_EXPR:
4338 case CONSTRUCTOR:
4339 TREE_ADDRESSABLE (x) = 1;
4340 return true;
95602da1 4341
3e4093b6
RS
4342 case VAR_DECL:
4343 case CONST_DECL:
4344 case PARM_DECL:
4345 case RESULT_DECL:
5baeaac0 4346 if (C_DECL_REGISTER (x)
3e4093b6
RS
4347 && DECL_NONLOCAL (x))
4348 {
e697b20f 4349 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4350 {
0039fa55
AN
4351 error
4352 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4353 return false;
4354 }
509c9d60 4355 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4356 }
5baeaac0 4357 else if (C_DECL_REGISTER (x))
3e4093b6 4358 {
e697b20f 4359 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4360 error ("address of global register variable %qD requested", x);
4361 else
4362 error ("address of register variable %qD requested", x);
4363 return false;
3e4093b6 4364 }
400fbf9f 4365
3e4093b6
RS
4366 /* drops in */
4367 case FUNCTION_DECL:
4368 TREE_ADDRESSABLE (x) = 1;
4369 /* drops out */
4370 default:
4371 return true;
4372 }
4373}
4374\f
2d2e923f
MLI
4375/* Convert EXPR to TYPE, warning about conversion problems with
4376 constants. SEMANTIC_TYPE is the type this conversion would use
4377 without excess precision. If SEMANTIC_TYPE is NULL, this function
4378 is equivalent to convert_and_check. This function is a wrapper that
4379 handles conversions that may be different than
4380 the usual ones because of excess precision. */
4381
4382static tree
68fca595
MP
4383ep_convert_and_check (location_t loc, tree type, tree expr,
4384 tree semantic_type)
2d2e923f
MLI
4385{
4386 if (TREE_TYPE (expr) == type)
4387 return expr;
4388
4389 if (!semantic_type)
68fca595 4390 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4391
4392 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4393 && TREE_TYPE (expr) != semantic_type)
4394 {
4395 /* For integers, we need to check the real conversion, not
4396 the conversion to the excess precision type. */
68fca595 4397 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4398 }
4399 /* Result type is the excess precision type, which should be
4400 large enough, so do not check. */
4401 return convert (type, expr);
4402}
4403
928c19bb
JM
4404/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4405 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4406 if folded to an integer constant then the unselected half may
4407 contain arbitrary operations not normally permitted in constant
c2255bc4 4408 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4409
4410tree
744aa42f 4411build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4412 tree op1, tree op1_original_type, tree op2,
4413 tree op2_original_type)
400fbf9f 4414{
3e4093b6
RS
4415 tree type1;
4416 tree type2;
4417 enum tree_code code1;
4418 enum tree_code code2;
4419 tree result_type = NULL;
2d2e923f 4420 tree semantic_result_type = NULL;
3e4093b6 4421 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4422 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4423 bool ifexp_int_operands;
928c19bb 4424 tree ret;
400fbf9f 4425
4d84fe7c
JM
4426 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4427 if (op1_int_operands)
4428 op1 = remove_c_maybe_const_expr (op1);
4429 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4430 if (op2_int_operands)
4431 op2 = remove_c_maybe_const_expr (op2);
4432 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4433 if (ifexp_int_operands)
4434 ifexp = remove_c_maybe_const_expr (ifexp);
4435
3e4093b6
RS
4436 /* Promote both alternatives. */
4437
4438 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4439 op1 = default_conversion (op1);
4440 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4441 op2 = default_conversion (op2);
4442
4443 if (TREE_CODE (ifexp) == ERROR_MARK
4444 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4445 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4446 return error_mark_node;
400fbf9f 4447
3e4093b6
RS
4448 type1 = TREE_TYPE (op1);
4449 code1 = TREE_CODE (type1);
4450 type2 = TREE_TYPE (op2);
4451 code2 = TREE_CODE (type2);
4452
b1adf557
JM
4453 /* C90 does not permit non-lvalue arrays in conditional expressions.
4454 In C99 they will be pointers by now. */
4455 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4456 {
744aa42f 4457 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4458 return error_mark_node;
4459 }
4460
8ce94e44
JM
4461 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4462 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4463 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4464 || code1 == COMPLEX_TYPE)
4465 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4466 || code2 == COMPLEX_TYPE))
4467 {
2d2e923f 4468 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4469 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4470 {
4471 op1 = TREE_OPERAND (op1, 0);
4472 type1 = TREE_TYPE (op1);
4473 gcc_assert (TREE_CODE (type1) == code1);
4474 }
4475 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4476 {
4477 op2 = TREE_OPERAND (op2, 0);
4478 type2 = TREE_TYPE (op2);
4479 gcc_assert (TREE_CODE (type2) == code2);
4480 }
4481 }
4482
d130ae11
ILT
4483 if (warn_cxx_compat)
4484 {
4485 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4486 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4487
4488 if (TREE_CODE (t1) == ENUMERAL_TYPE
4489 && TREE_CODE (t2) == ENUMERAL_TYPE
4490 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4491 warning_at (colon_loc, OPT_Wc___compat,
4492 ("different enum types in conditional is "
4493 "invalid in C++: %qT vs %qT"),
4494 t1, t2);
4495 }
4496
3e4093b6
RS
4497 /* Quickly detect the usual case where op1 and op2 have the same type
4498 after promotion. */
4499 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4500 {
3e4093b6
RS
4501 if (type1 == type2)
4502 result_type = type1;
4503 else
4504 result_type = TYPE_MAIN_VARIANT (type1);
4505 }
4506 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4507 || code1 == COMPLEX_TYPE)
4508 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4509 || code2 == COMPLEX_TYPE))
3e4093b6 4510 {
ccf7f880 4511 result_type = c_common_type (type1, type2);
0a0b3574
MM
4512 do_warn_double_promotion (result_type, type1, type2,
4513 "implicit conversion from %qT to %qT to "
4514 "match other result of conditional",
4515 colon_loc);
400fbf9f 4516
3e4093b6
RS
4517 /* If -Wsign-compare, warn here if type1 and type2 have
4518 different signedness. We'll promote the signed to unsigned
4519 and later code won't know it used to be different.
4520 Do this check on the original types, so that explicit casts
4521 will be considered, but default promotions won't. */
7d882b83 4522 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4523 {
8df83eae
RK
4524 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4525 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4526
3e4093b6
RS
4527 if (unsigned_op1 ^ unsigned_op2)
4528 {
6ac01510
ILT
4529 bool ovf;
4530
3e4093b6
RS
4531 /* Do not warn if the result type is signed, since the
4532 signed type will only be chosen if it can represent
4533 all the values of the unsigned type. */
3f75a254 4534 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4535 /* OK */;
3e4093b6 4536 else
928c19bb
JM
4537 {
4538 bool op1_maybe_const = true;
4539 bool op2_maybe_const = true;
4540
4541 /* Do not warn if the signed quantity is an
4542 unsuffixed integer literal (or some static
4543 constant expression involving such literals) and
4544 it is non-negative. This warning requires the
4545 operands to be folded for best results, so do
4546 that folding in this case even without
4547 warn_sign_compare to avoid warning options
4548 possibly affecting code generation. */
f5178456
RS
4549 c_inhibit_evaluation_warnings
4550 += (ifexp == truthvalue_false_node);
928c19bb
JM
4551 op1 = c_fully_fold (op1, require_constant_value,
4552 &op1_maybe_const);
f5178456
RS
4553 c_inhibit_evaluation_warnings
4554 -= (ifexp == truthvalue_false_node);
4555
4556 c_inhibit_evaluation_warnings
4557 += (ifexp == truthvalue_true_node);
928c19bb
JM
4558 op2 = c_fully_fold (op2, require_constant_value,
4559 &op2_maybe_const);
f5178456
RS
4560 c_inhibit_evaluation_warnings
4561 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4562
4563 if (warn_sign_compare)
4564 {
4565 if ((unsigned_op2
4566 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4567 || (unsigned_op1
4568 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4569 /* OK */;
4570 else
744aa42f
ILT
4571 warning_at (colon_loc, OPT_Wsign_compare,
4572 ("signed and unsigned type in "
4573 "conditional expression"));
928c19bb
JM
4574 }
4575 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4576 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4577 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4578 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4579 }
3e4093b6
RS
4580 }
4581 }
4582 }
4583 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4584 {
fcf73884 4585 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4586 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4587 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4588 result_type = void_type_node;
4589 }
4590 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4591 {
36c5e70a
BE
4592 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4593 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4594 addr_space_t as_common;
4595
744aa42f 4596 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4597 result_type = common_pointer_type (type1, type2);
6aa3c60d 4598 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4599 result_type = type2;
6aa3c60d 4600 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4601 result_type = type1;
4602 else if (!addr_space_superset (as1, as2, &as_common))
4603 {
4604 error_at (colon_loc, "pointers to disjoint address spaces "
4605 "used in conditional expression");
4606 return error_mark_node;
4607 }
267bac10
JM
4608 else if (VOID_TYPE_P (TREE_TYPE (type1))
4609 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4610 {
fcf73884 4611 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4612 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4613 "ISO C forbids conditional expr between "
bda67431 4614 "%<void *%> and function pointer");
3e4093b6
RS
4615 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4616 TREE_TYPE (type2)));
34a80643 4617 }
267bac10
JM
4618 else if (VOID_TYPE_P (TREE_TYPE (type2))
4619 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4620 {
fcf73884 4621 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4622 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4623 "ISO C forbids conditional expr between "
bda67431 4624 "%<void *%> and function pointer");
3e4093b6
RS
4625 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4626 TREE_TYPE (type1)));
1c2a9b35 4627 }
b581b85b
NP
4628 /* Objective-C pointer comparisons are a bit more lenient. */
4629 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4630 result_type = objc_common_type (type1, type2);
34a80643 4631 else
ab87f8c8 4632 {
36c5e70a
BE
4633 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4634
b581b85b
NP
4635 pedwarn (colon_loc, 0,
4636 "pointer type mismatch in conditional expression");
36c5e70a
BE
4637 result_type = build_pointer_type
4638 (build_qualified_type (void_type_node, qual));
ab87f8c8 4639 }
3e4093b6
RS
4640 }
4641 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4642 {
6aa3c60d 4643 if (!null_pointer_constant_p (orig_op2))
744aa42f 4644 pedwarn (colon_loc, 0,
509c9d60 4645 "pointer/integer type mismatch in conditional expression");
3e4093b6 4646 else
ab87f8c8 4647 {
3e4093b6 4648 op2 = null_pointer_node;
ab87f8c8 4649 }
3e4093b6
RS
4650 result_type = type1;
4651 }
4652 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4653 {
6aa3c60d 4654 if (!null_pointer_constant_p (orig_op1))
744aa42f 4655 pedwarn (colon_loc, 0,
509c9d60 4656 "pointer/integer type mismatch in conditional expression");
3e4093b6 4657 else
ab87f8c8 4658 {
3e4093b6 4659 op1 = null_pointer_node;
ab87f8c8 4660 }
3e4093b6
RS
4661 result_type = type2;
4662 }
1c2a9b35 4663
3e4093b6
RS
4664 if (!result_type)
4665 {
4666 if (flag_cond_mismatch)
4667 result_type = void_type_node;
4668 else
400fbf9f 4669 {
c2255bc4 4670 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4671 return error_mark_node;
400fbf9f 4672 }
3e4093b6 4673 }
400fbf9f 4674
3e4093b6
RS
4675 /* Merge const and volatile flags of the incoming types. */
4676 result_type
4677 = build_type_variant (result_type,
afbd0665
AS
4678 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4679 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4680
68fca595
MP
4681 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4682 semantic_result_type);
4683 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4684 semantic_result_type);
b6a10c9f 4685
928c19bb
JM
4686 if (ifexp_bcp && ifexp == truthvalue_true_node)
4687 {
4688 op2_int_operands = true;
4689 op1 = c_fully_fold (op1, require_constant_value, NULL);
4690 }
4691 if (ifexp_bcp && ifexp == truthvalue_false_node)
4692 {
4693 op1_int_operands = true;
4694 op2 = c_fully_fold (op2, require_constant_value, NULL);
4695 }
4d84fe7c 4696 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4697 && op1_int_operands
4698 && op2_int_operands);
4699 if (int_operands)
4700 {
4701 int_const = ((ifexp == truthvalue_true_node
4702 && TREE_CODE (orig_op1) == INTEGER_CST
4703 && !TREE_OVERFLOW (orig_op1))
4704 || (ifexp == truthvalue_false_node
4705 && TREE_CODE (orig_op2) == INTEGER_CST
4706 && !TREE_OVERFLOW (orig_op2)));
4707 }
4708 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4709 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4710 else
4711 {
01c7ccbb
JM
4712 if (int_operands)
4713 {
f34f1c87
MP
4714 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4715 nested inside of the expression. */
4716 op1 = c_fully_fold (op1, false, NULL);
4717 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4718 }
928c19bb
JM
4719 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4720 if (int_operands)
4721 ret = note_integer_operands (ret);
4722 }
2d2e923f
MLI
4723 if (semantic_result_type)
4724 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4725
c2255bc4 4726 protected_set_expr_location (ret, colon_loc);
928c19bb 4727 return ret;
3e4093b6
RS
4728}
4729\f
487a92fe 4730/* Return a compound expression that performs two expressions and
c2255bc4
AH
4731 returns the value of the second of them.
4732
4733 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4734
3e4093b6 4735tree
c2255bc4 4736build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4737{
4d84fe7c 4738 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4739 tree eptype = NULL_TREE;
928c19bb
JM
4740 tree ret;
4741
b72271b9 4742 if (flag_cilkplus
939b37da
BI
4743 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4744 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4745 {
4746 error_at (loc,
4747 "spawned function call cannot be part of a comma expression");
4748 return error_mark_node;
4749 }
4d84fe7c
JM
4750 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4751 if (expr1_int_operands)
4752 expr1 = remove_c_maybe_const_expr (expr1);
4753 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4754 if (expr2_int_operands)
4755 expr2 = remove_c_maybe_const_expr (expr2);
4756
8ce94e44
JM
4757 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4758 expr1 = TREE_OPERAND (expr1, 0);
4759 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4760 {
4761 eptype = TREE_TYPE (expr2);
4762 expr2 = TREE_OPERAND (expr2, 0);
4763 }
4764
3f75a254 4765 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4766 {
4767 /* The left-hand operand of a comma expression is like an expression
c5409249 4768 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4769 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4770 if (warn_unused_value)
47aecf47 4771 {
e14a6540 4772 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4773 && CONVERT_EXPR_P (expr1))
47aecf47 4774 ; /* (void) a, b */
e14a6540
JM
4775 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4776 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4777 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4778 ; /* (void) a, (void) b, c */
4779 else
b8698a0f 4780 warning_at (loc, OPT_Wunused_value,
c2255bc4 4781 "left-hand operand of comma expression has no effect");
47aecf47 4782 }
3e4093b6 4783 }
789eadcd
MP
4784 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4785 && warn_unused_value)
4786 {
4787 tree r = expr1;
4788 location_t cloc = loc;
4789 while (TREE_CODE (r) == COMPOUND_EXPR)
4790 {
4791 if (EXPR_HAS_LOCATION (r))
4792 cloc = EXPR_LOCATION (r);
4793 r = TREE_OPERAND (r, 1);
4794 }
4795 if (!TREE_SIDE_EFFECTS (r)
4796 && !VOID_TYPE_P (TREE_TYPE (r))
4797 && !CONVERT_EXPR_P (r))
4798 warning_at (cloc, OPT_Wunused_value,
4799 "right-hand operand of comma expression has no effect");
4800 }
400fbf9f 4801
3e4093b6
RS
4802 /* With -Wunused, we should also warn if the left-hand operand does have
4803 side-effects, but computes a value which is not used. For example, in
4804 `foo() + bar(), baz()' the result of the `+' operator is not used,
4805 so we should issue a warning. */
4806 else if (warn_unused_value)
c2255bc4 4807 warn_if_unused_value (expr1, loc);
400fbf9f 4808
e63d6886
AP
4809 if (expr2 == error_mark_node)
4810 return error_mark_node;
4811
928c19bb
JM
4812 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4813
4814 if (flag_isoc99
4d84fe7c
JM
4815 && expr1_int_operands
4816 && expr2_int_operands)
928c19bb
JM
4817 ret = note_integer_operands (ret);
4818
8ce94e44
JM
4819 if (eptype)
4820 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4821
c2255bc4 4822 protected_set_expr_location (ret, loc);
928c19bb 4823 return ret;
3e4093b6 4824}
400fbf9f 4825
67165eb3
ILT
4826/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4827 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4828 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4829 of the cast. -Wcast-qual appeared on the command line. Named
4830 address space qualifiers are not handled here, because they result
4831 in different warnings. */
67165eb3
ILT
4832
4833static void
2ee3cb35 4834handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4835{
4836 tree in_type = type;
4837 tree in_otype = otype;
4838 int added = 0;
4839 int discarded = 0;
4840 bool is_const;
4841
4842 /* Check that the qualifiers on IN_TYPE are a superset of the
4843 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4844 nodes is uninteresting and we stop as soon as we hit a
4845 non-POINTER_TYPE node on either type. */
4846 do
4847 {
4848 in_otype = TREE_TYPE (in_otype);
4849 in_type = TREE_TYPE (in_type);
4850
4851 /* GNU C allows cv-qualified function types. 'const' means the
4852 function is very pure, 'volatile' means it can't return. We
4853 need to warn when such qualifiers are added, not when they're
4854 taken away. */
4855 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4856 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4857 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4858 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4859 else
36c5e70a
BE
4860 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4861 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4862 }
4863 while (TREE_CODE (in_type) == POINTER_TYPE
4864 && TREE_CODE (in_otype) == POINTER_TYPE);
4865
4866 if (added)
2ee3cb35
MLI
4867 warning_at (loc, OPT_Wcast_qual,
4868 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4869
4870 if (discarded)
4871 /* There are qualifiers present in IN_OTYPE that are not present
4872 in IN_TYPE. */
2ee3cb35 4873 warning_at (loc, OPT_Wcast_qual,
7485aeea 4874 "cast discards %qv qualifier from pointer target type",
2ee3cb35 4875 discarded);
67165eb3
ILT
4876
4877 if (added || discarded)
4878 return;
4879
4880 /* A cast from **T to const **T is unsafe, because it can cause a
4881 const value to be changed with no additional warning. We only
4882 issue this warning if T is the same on both sides, and we only
4883 issue the warning if there are the same number of pointers on
4884 both sides, as otherwise the cast is clearly unsafe anyhow. A
4885 cast is unsafe when a qualifier is added at one level and const
4886 is not present at all outer levels.
4887
4888 To issue this warning, we check at each level whether the cast
4889 adds new qualifiers not already seen. We don't need to special
4890 case function types, as they won't have the same
4891 TYPE_MAIN_VARIANT. */
4892
4893 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4894 return;
4895 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4896 return;
4897
4898 in_type = type;
4899 in_otype = otype;
4900 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4901 do
4902 {
4903 in_type = TREE_TYPE (in_type);
4904 in_otype = TREE_TYPE (in_otype);
4905 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4906 && !is_const)
4907 {
2ee3cb35
MLI
4908 warning_at (loc, OPT_Wcast_qual,
4909 "to be safe all intermediate pointers in cast from "
4910 "%qT to %qT must be %<const%> qualified",
4911 otype, type);
67165eb3
ILT
4912 break;
4913 }
4914 if (is_const)
4915 is_const = TYPE_READONLY (in_type);
4916 }
4917 while (TREE_CODE (in_type) == POINTER_TYPE);
4918}
4919
b8698a0f 4920/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4921 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4922
3e4093b6 4923tree
c2255bc4 4924build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4925{
8ce94e44
JM
4926 tree value;
4927
4928 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4929 expr = TREE_OPERAND (expr, 0);
4930
4931 value = expr;
400fbf9f 4932
3e4093b6
RS
4933 if (type == error_mark_node || expr == error_mark_node)
4934 return error_mark_node;
400fbf9f 4935
3e4093b6
RS
4936 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4937 only in <protocol> qualifications. But when constructing cast expressions,
4938 the protocols do matter and must be kept around. */
700686fa
ZL
4939 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4940 return build1 (NOP_EXPR, type, expr);
4941
4942 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4943
3e4093b6
RS
4944 if (TREE_CODE (type) == ARRAY_TYPE)
4945 {
c2255bc4 4946 error_at (loc, "cast specifies array type");
3e4093b6
RS
4947 return error_mark_node;
4948 }
400fbf9f 4949
3e4093b6
RS
4950 if (TREE_CODE (type) == FUNCTION_TYPE)
4951 {
c2255bc4 4952 error_at (loc, "cast specifies function type");
3e4093b6
RS
4953 return error_mark_node;
4954 }
400fbf9f 4955
808d6eaa
JM
4956 if (!VOID_TYPE_P (type))
4957 {
4958 value = require_complete_type (value);
4959 if (value == error_mark_node)
4960 return error_mark_node;
4961 }
4962
3e4093b6
RS
4963 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4964 {
fcf73884
MLI
4965 if (TREE_CODE (type) == RECORD_TYPE
4966 || TREE_CODE (type) == UNION_TYPE)
c1771a20 4967 pedwarn (loc, OPT_Wpedantic,
fcf73884 4968 "ISO C forbids casting nonscalar to the same type");
3e4093b6
RS
4969 }
4970 else if (TREE_CODE (type) == UNION_TYPE)
4971 {
4972 tree field;
400fbf9f 4973
910ad8de 4974 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
4975 if (TREE_TYPE (field) != error_mark_node
4976 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4977 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
4978 break;
4979
4980 if (field)
400fbf9f 4981 {
3e4093b6 4982 tree t;
e616f54d 4983 bool maybe_const = true;
3e4093b6 4984
c1771a20 4985 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
4986 t = c_fully_fold (value, false, &maybe_const);
4987 t = build_constructor_single (type, field, t);
4988 if (!maybe_const)
4989 t = c_wrap_maybe_const (t, true);
4990 t = digest_init (loc, type, t,
bbbbb16a 4991 NULL_TREE, false, true, 0);
3e4093b6
RS
4992 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4993 return t;
400fbf9f 4994 }
c2255bc4 4995 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
4996 return error_mark_node;
4997 }
4998 else
4999 {
5000 tree otype, ovalue;
400fbf9f 5001
3e4093b6 5002 if (type == void_type_node)
c2255bc4
AH
5003 {
5004 tree t = build1 (CONVERT_EXPR, type, value);
5005 SET_EXPR_LOCATION (t, loc);
5006 return t;
5007 }
400fbf9f 5008
3e4093b6 5009 otype = TREE_TYPE (value);
400fbf9f 5010
3e4093b6 5011 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5012 if (warn_cast_qual
5013 && TREE_CODE (type) == POINTER_TYPE
5014 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5015 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5016
36c5e70a
BE
5017 /* Warn about conversions between pointers to disjoint
5018 address spaces. */
5019 if (TREE_CODE (type) == POINTER_TYPE
5020 && TREE_CODE (otype) == POINTER_TYPE
5021 && !null_pointer_constant_p (value))
5022 {
5023 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5024 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5025 addr_space_t as_common;
5026
5027 if (!addr_space_superset (as_to, as_from, &as_common))
5028 {
5029 if (ADDR_SPACE_GENERIC_P (as_from))
5030 warning_at (loc, 0, "cast to %s address space pointer "
5031 "from disjoint generic address space pointer",
5032 c_addr_space_name (as_to));
5033
5034 else if (ADDR_SPACE_GENERIC_P (as_to))
5035 warning_at (loc, 0, "cast to generic address space pointer "
5036 "from disjoint %s address space pointer",
5037 c_addr_space_name (as_from));
5038
5039 else
5040 warning_at (loc, 0, "cast to %s address space pointer "
5041 "from disjoint %s address space pointer",
5042 c_addr_space_name (as_to),
5043 c_addr_space_name (as_from));
5044 }
5045 }
5046
3e4093b6 5047 /* Warn about possible alignment problems. */
3176a0c2 5048 if (STRICT_ALIGNMENT
3e4093b6
RS
5049 && TREE_CODE (type) == POINTER_TYPE
5050 && TREE_CODE (otype) == POINTER_TYPE
5051 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5052 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5053 /* Don't warn about opaque types, where the actual alignment
5054 restriction is unknown. */
5055 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5056 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5057 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5058 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5059 warning_at (loc, OPT_Wcast_align,
5060 "cast increases required alignment of target type");
e9a25f70 5061
3176a0c2 5062 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5063 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5064 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5065 /* Unlike conversion of integers to pointers, where the
5066 warning is disabled for converting constants because
5067 of cases such as SIG_*, warn about converting constant
5068 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5069 sign extension, and a warning is appropriate. */
c2255bc4
AH
5070 warning_at (loc, OPT_Wpointer_to_int_cast,
5071 "cast from pointer to integer of different size");
400fbf9f 5072
3176a0c2 5073 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5074 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5075 warning_at (loc, OPT_Wbad_function_cast,
5076 "cast from function call of type %qT "
5077 "to non-matching type %qT", otype, type);
400fbf9f 5078
3176a0c2 5079 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5080 && TREE_CODE (otype) == INTEGER_TYPE
5081 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5082 /* Don't warn about converting any constant. */
5083 && !TREE_CONSTANT (value))
c2255bc4
AH
5084 warning_at (loc,
5085 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5086 "of different size");
400fbf9f 5087
79bedddc
SR
5088 if (warn_strict_aliasing <= 2)
5089 strict_aliasing_warning (otype, type, expr);
400fbf9f 5090
3897f229
JM
5091 /* If pedantic, warn for conversions between function and object
5092 pointer types, except for converting a null pointer constant
5093 to function pointer type. */
5094 if (pedantic
5095 && TREE_CODE (type) == POINTER_TYPE
5096 && TREE_CODE (otype) == POINTER_TYPE
5097 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5098 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5099 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5100 "conversion of function pointer to object pointer type");
3897f229
JM
5101
5102 if (pedantic
5103 && TREE_CODE (type) == POINTER_TYPE
5104 && TREE_CODE (otype) == POINTER_TYPE
5105 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5106 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5107 && !null_pointer_constant_p (value))
c1771a20 5108 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5109 "conversion of object pointer to function pointer type");
3897f229 5110
3e4093b6 5111 ovalue = value;
3e4093b6 5112 value = convert (type, value);
400fbf9f 5113
3e4093b6 5114 /* Ignore any integer overflow caused by the cast. */
928c19bb 5115 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5116 {
8bcd6380 5117 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5118 {
8bcd6380
RS
5119 if (!TREE_OVERFLOW (value))
5120 {
5121 /* Avoid clobbering a shared constant. */
5122 value = copy_node (value);
5123 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5124 }
6414bad6 5125 }
8bcd6380 5126 else if (TREE_OVERFLOW (value))
d8e1f97b
PB
5127 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5128 value = build_int_cst_wide (TREE_TYPE (value),
5129 TREE_INT_CST_LOW (value),
5130 TREE_INT_CST_HIGH (value));
3e4093b6
RS
5131 }
5132 }
400fbf9f 5133
53cd18ec
JM
5134 /* Don't let a cast be an lvalue. */
5135 if (value == expr)
db3927fb 5136 value = non_lvalue_loc (loc, value);
e9a25f70 5137
928c19bb
JM
5138 /* Don't allow the results of casting to floating-point or complex
5139 types be confused with actual constants, or casts involving
5140 integer and pointer types other than direct integer-to-integer
5141 and integer-to-pointer be confused with integer constant
5142 expressions and null pointer constants. */
5143 if (TREE_CODE (value) == REAL_CST
5144 || TREE_CODE (value) == COMPLEX_CST
5145 || (TREE_CODE (value) == INTEGER_CST
5146 && !((TREE_CODE (expr) == INTEGER_CST
5147 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5148 || TREE_CODE (expr) == REAL_CST
5149 || TREE_CODE (expr) == COMPLEX_CST)))
5150 value = build1 (NOP_EXPR, type, value);
5151
c2255bc4
AH
5152 if (CAN_HAVE_LOCATION_P (value))
5153 SET_EXPR_LOCATION (value, loc);
3e4093b6 5154 return value;
400fbf9f
JW
5155}
5156
c2255bc4
AH
5157/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5158 location of the open paren of the cast, or the position of the cast
5159 expr. */
3e4093b6 5160tree
c2255bc4 5161c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5162{
f8893e47 5163 tree type;
928c19bb
JM
5164 tree type_expr = NULL_TREE;
5165 bool type_expr_const = true;
5166 tree ret;
3e4093b6 5167 int saved_wsp = warn_strict_prototypes;
c5c76735 5168
3e4093b6
RS
5169 /* This avoids warnings about unprototyped casts on
5170 integers. E.g. "#define SIG_DFL (void(*)())0". */
5171 if (TREE_CODE (expr) == INTEGER_CST)
5172 warn_strict_prototypes = 0;
928c19bb 5173 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5174 warn_strict_prototypes = saved_wsp;
c5c76735 5175
c2255bc4 5176 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5177 if (type_expr)
5178 {
9f33203d
JM
5179 bool inner_expr_const = true;
5180 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5181 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5182 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5183 && inner_expr_const);
c2255bc4 5184 SET_EXPR_LOCATION (ret, loc);
928c19bb 5185 }
24b97832
ILT
5186
5187 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5188 SET_EXPR_LOCATION (ret, loc);
5189
9e5b2115
PB
5190 /* C++ does not permits types to be defined in a cast, but it
5191 allows references to incomplete types. */
5192 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5193 warning_at (loc, OPT_Wc___compat,
5194 "defining a type in a cast is invalid in C++");
5195
928c19bb 5196 return ret;
400fbf9f 5197}
3e4093b6
RS
5198\f
5199/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5200 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5201 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5202 MODIFYCODE is the code for a binary operator that we use
5203 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5204 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5205 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5206 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5207
c2255bc4
AH
5208 LOCATION is the location of the MODIFYCODE operator.
5209 RHS_LOC is the location of the RHS. */
2f6e4e97 5210
3e4093b6 5211tree
32e8bb8e 5212build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5213 enum tree_code modifycode,
c2255bc4 5214 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5215{
3e4093b6
RS
5216 tree result;
5217 tree newrhs;
241f845a 5218 tree rhseval = NULL_TREE;
8ce94e44 5219 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5220 tree lhstype = TREE_TYPE (lhs);
5221 tree olhstype = lhstype;
928c19bb 5222 bool npc;
267bac10 5223 bool is_atomic_op;
e9a25f70 5224
3e4093b6
RS
5225 /* Types that aren't fully specified cannot be used in assignments. */
5226 lhs = require_complete_type (lhs);
e9a25f70 5227
3e4093b6
RS
5228 /* Avoid duplicate error messages from operands that had errors. */
5229 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5230 return error_mark_node;
400fbf9f 5231
4c2ecab0
JM
5232 /* Ensure an error for assigning a non-lvalue array to an array in
5233 C90. */
5234 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5235 {
5236 error_at (location, "assignment to expression with array type");
5237 return error_mark_node;
5238 }
5239
46a88c12 5240 /* For ObjC properties, defer this check. */
7bd11157 5241 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5242 return error_mark_node;
5243
267bac10
JM
5244 is_atomic_op = really_atomic_lvalue (lhs);
5245
8ce94e44
JM
5246 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5247 {
5248 rhs_semantic_type = TREE_TYPE (rhs);
5249 rhs = TREE_OPERAND (rhs, 0);
5250 }
5251
3e4093b6 5252 newrhs = rhs;
400fbf9f 5253
928c19bb
JM
5254 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5255 {
5256 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5257 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5258 rhs_origtype);
928c19bb
JM
5259 if (inner == error_mark_node)
5260 return error_mark_node;
5261 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5262 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5263 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5264 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5265 protected_set_expr_location (result, location);
5266 return result;
5267 }
5268
3e4093b6
RS
5269 /* If a binary op has been requested, combine the old LHS value with the RHS
5270 producing the value we should actually store into the LHS. */
5271
5272 if (modifycode != NOP_EXPR)
400fbf9f 5273 {
928c19bb 5274 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5275 lhs = stabilize_reference (lhs);
bbbbb16a 5276
267bac10
JM
5277 /* Construct the RHS for any non-atomic compound assignemnt. */
5278 if (!is_atomic_op)
5279 {
241f845a
JJ
5280 /* If in LHS op= RHS the RHS has side-effects, ensure they
5281 are preevaluated before the rest of the assignment expression's
5282 side-effects, because RHS could contain e.g. function calls
5283 that modify LHS. */
5284 if (TREE_SIDE_EFFECTS (rhs))
5285 {
5286 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5287 rhseval = newrhs;
5288 }
267bac10 5289 newrhs = build_binary_op (location,
241f845a 5290 modifycode, lhs, newrhs, 1);
267bac10
JM
5291
5292 /* The original type of the right hand side is no longer
5293 meaningful. */
5294 rhs_origtype = NULL_TREE;
5295 }
400fbf9f 5296 }
400fbf9f 5297
668ea4b1
IS
5298 if (c_dialect_objc ())
5299 {
46a88c12
NP
5300 /* Check if we are modifying an Objective-C property reference;
5301 if so, we need to generate setter calls. */
5302 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5303 if (result)
241f845a 5304 goto return_result;
46a88c12
NP
5305
5306 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5307 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5308 return error_mark_node;
5309 }
5310
9bf24266 5311 /* Give an error for storing in something that is 'const'. */
bbbd6700 5312
f37acdf9 5313 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5314 || ((TREE_CODE (lhstype) == RECORD_TYPE
5315 || TREE_CODE (lhstype) == UNION_TYPE)
5316 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5317 {
c02065fc 5318 readonly_error (location, lhs, lv_assign);
953ff289
DN
5319 return error_mark_node;
5320 }
f37acdf9
JM
5321 else if (TREE_READONLY (lhs))
5322 readonly_warning (lhs, lv_assign);
bbbd6700 5323
3e4093b6
RS
5324 /* If storing into a structure or union member,
5325 it has probably been given type `int'.
5326 Compute the type that would go with
5327 the actual amount of storage the member occupies. */
bbbd6700 5328
3e4093b6
RS
5329 if (TREE_CODE (lhs) == COMPONENT_REF
5330 && (TREE_CODE (lhstype) == INTEGER_TYPE
5331 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5332 || TREE_CODE (lhstype) == REAL_TYPE
5333 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5334 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5335
3e4093b6
RS
5336 /* If storing in a field that is in actuality a short or narrower than one,
5337 we must store in the field in its actual type. */
5338
5339 if (lhstype != TREE_TYPE (lhs))
5340 {
5341 lhs = copy_node (lhs);
5342 TREE_TYPE (lhs) = lhstype;
400fbf9f 5343 }
400fbf9f 5344
32e8bb8e
ILT
5345 /* Issue -Wc++-compat warnings about an assignment to an enum type
5346 when LHS does not have its original type. This happens for,
5347 e.g., an enum bitfield in a struct. */
5348 if (warn_cxx_compat
5349 && lhs_origtype != NULL_TREE
5350 && lhs_origtype != lhstype
5351 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5352 {
5353 tree checktype = (rhs_origtype != NULL_TREE
5354 ? rhs_origtype
5355 : TREE_TYPE (rhs));
5356 if (checktype != error_mark_node
267bac10
JM
5357 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5358 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5359 warning_at (location, OPT_Wc___compat,
5360 "enum conversion in assignment is invalid in C++");
5361 }
5362
267bac10
JM
5363 /* If the lhs is atomic, remove that qualifier. */
5364 if (is_atomic_op)
5365 {
5366 lhstype = build_qualified_type (lhstype,
5367 (TYPE_QUALS (lhstype)
5368 & ~TYPE_QUAL_ATOMIC));
5369 olhstype = build_qualified_type (olhstype,
5370 (TYPE_QUALS (lhstype)
5371 & ~TYPE_QUAL_ATOMIC));
5372 }
5373
8ce94e44
JM
5374 /* Convert new value to destination type. Fold it first, then
5375 restore any excess precision information, for the sake of
5376 conversion warnings. */
400fbf9f 5377
267bac10
JM
5378 if (!(is_atomic_op && modifycode != NOP_EXPR))
5379 {
5380 npc = null_pointer_constant_p (newrhs);
5381 newrhs = c_fully_fold (newrhs, false, NULL);
5382 if (rhs_semantic_type)
5383 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5384 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5385 rhs_origtype, ic_assign, npc,
5386 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5387 if (TREE_CODE (newrhs) == ERROR_MARK)
5388 return error_mark_node;
5389 }
400fbf9f 5390
6e955430
ZL
5391 /* Emit ObjC write barrier, if necessary. */
5392 if (c_dialect_objc () && flag_objc_gc)
5393 {
5394 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5395 if (result)
c9f9eb5d
AH
5396 {
5397 protected_set_expr_location (result, location);
241f845a 5398 goto return_result;
c9f9eb5d 5399 }
6e955430
ZL
5400 }
5401
ea4b7848 5402 /* Scan operands. */
400fbf9f 5403
267bac10
JM
5404 if (is_atomic_op)
5405 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5406 else
5407 {
5408 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5409 TREE_SIDE_EFFECTS (result) = 1;
5410 protected_set_expr_location (result, location);
5411 }
400fbf9f 5412
3e4093b6
RS
5413 /* If we got the LHS in a different type for storing in,
5414 convert the result back to the nominal type of LHS
5415 so that the value we return always has the same type
5416 as the LHS argument. */
e855c5ce 5417
3e4093b6 5418 if (olhstype == TREE_TYPE (result))
241f845a 5419 goto return_result;
c9f9eb5d 5420
68fca595
MP
5421 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5422 rhs_origtype, ic_assign, false, NULL_TREE,
5423 NULL_TREE, 0);
c9f9eb5d 5424 protected_set_expr_location (result, location);
241f845a
JJ
5425
5426return_result:
5427 if (rhseval)
5428 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5429 return result;
3e4093b6
RS
5430}
5431\f
478a1c5b
ILT
5432/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5433 This is used to implement -fplan9-extensions. */
5434
5435static bool
5436find_anonymous_field_with_type (tree struct_type, tree type)
5437{
5438 tree field;
5439 bool found;
5440
5441 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5442 || TREE_CODE (struct_type) == UNION_TYPE);
5443 found = false;
5444 for (field = TYPE_FIELDS (struct_type);
5445 field != NULL_TREE;
5446 field = TREE_CHAIN (field))
5447 {
267bac10
JM
5448 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5449 ? c_build_qualified_type (TREE_TYPE (field),
5450 TYPE_QUAL_ATOMIC)
5451 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5452 if (DECL_NAME (field) == NULL
267bac10 5453 && comptypes (type, fieldtype))
478a1c5b
ILT
5454 {
5455 if (found)
5456 return false;
5457 found = true;
5458 }
5459 else if (DECL_NAME (field) == NULL
5460 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5461 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5462 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5463 {
5464 if (found)
5465 return false;
5466 found = true;
5467 }
5468 }
5469 return found;
5470}
5471
5472/* RHS is an expression whose type is pointer to struct. If there is
5473 an anonymous field in RHS with type TYPE, then return a pointer to
5474 that field in RHS. This is used with -fplan9-extensions. This
5475 returns NULL if no conversion could be found. */
5476
5477static tree
5478convert_to_anonymous_field (location_t location, tree type, tree rhs)
5479{
5480 tree rhs_struct_type, lhs_main_type;
5481 tree field, found_field;
5482 bool found_sub_field;
5483 tree ret;
5484
5485 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5486 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5487 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5488 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5489
5490 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5491 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5492 ? c_build_qualified_type (TREE_TYPE (type),
5493 TYPE_QUAL_ATOMIC)
5494 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5495
5496 found_field = NULL_TREE;
5497 found_sub_field = false;
5498 for (field = TYPE_FIELDS (rhs_struct_type);
5499 field != NULL_TREE;
5500 field = TREE_CHAIN (field))
5501 {
5502 if (DECL_NAME (field) != NULL_TREE
5503 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5504 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5505 continue;
267bac10
JM
5506 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5507 ? c_build_qualified_type (TREE_TYPE (field),
5508 TYPE_QUAL_ATOMIC)
5509 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5510 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5511 {
5512 if (found_field != NULL_TREE)
5513 return NULL_TREE;
5514 found_field = field;
5515 }
5516 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5517 lhs_main_type))
5518 {
5519 if (found_field != NULL_TREE)
5520 return NULL_TREE;
5521 found_field = field;
5522 found_sub_field = true;
5523 }
5524 }
5525
5526 if (found_field == NULL_TREE)
5527 return NULL_TREE;
5528
5529 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5530 build_fold_indirect_ref (rhs), found_field,
5531 NULL_TREE);
5532 ret = build_fold_addr_expr_loc (location, ret);
5533
5534 if (found_sub_field)
5535 {
5536 ret = convert_to_anonymous_field (location, type, ret);
5537 gcc_assert (ret != NULL_TREE);
5538 }
5539
5540 return ret;
5541}
5542
bbbbb16a
ILT
5543/* Convert value RHS to type TYPE as preparation for an assignment to
5544 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5545 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5546 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5547 constant before any folding.
3e4093b6
RS
5548 The real work of conversion is done by `convert'.
5549 The purpose of this function is to generate error messages
5550 for assignments that are not allowed in C.
2ac2f164
JM
5551 ERRTYPE says whether it is argument passing, assignment,
5552 initialization or return.
2f6e4e97 5553
81e5eca8
MP
5554 LOCATION is the location of the assignment, EXPR_LOC is the location of
5555 the RHS or, for a function, location of an argument.
2ac2f164 5556 FUNCTION is a tree for the function being called.
3e4093b6 5557 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5558
3e4093b6 5559static tree
81e5eca8 5560convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5561 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5562 bool null_pointer_constant, tree fundecl,
5563 tree function, int parmnum)
3e4093b6
RS
5564{
5565 enum tree_code codel = TREE_CODE (type);
8ce94e44 5566 tree orig_rhs = rhs;
3e4093b6
RS
5567 tree rhstype;
5568 enum tree_code coder;
2ac2f164 5569 tree rname = NULL_TREE;
58393038 5570 bool objc_ok = false;
2ac2f164 5571
6b4ef5c1 5572 if (errtype == ic_argpass)
2ac2f164
JM
5573 {
5574 tree selector;
5575 /* Change pointer to function to the function itself for
5576 diagnostics. */
5577 if (TREE_CODE (function) == ADDR_EXPR
5578 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5579 function = TREE_OPERAND (function, 0);
5580
5581 /* Handle an ObjC selector specially for diagnostics. */
5582 selector = objc_message_selector ();
5583 rname = function;
5584 if (selector && parmnum > 2)
5585 {
5586 rname = selector;
5587 parmnum -= 2;
5588 }
5589 }
5590
5591 /* This macro is used to emit diagnostics to ensure that all format
5592 strings are complete sentences, visible to gettext and checked at
5593 compile time. */
c2255bc4 5594#define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5595 do { \
5596 switch (errtype) \
5597 { \
5598 case ic_argpass: \
5599 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
c2255bc4
AH
5600 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5601 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
1e053dfe
MLI
5602 "expected %qT but argument is of type %qT", \
5603 type, rhstype); \
5604 break; \
1e053dfe
MLI
5605 case ic_assign: \
5606 pedwarn (LOCATION, OPT, AS); \
5607 break; \
5608 case ic_init: \
6a8f4e12 5609 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5610 break; \
5611 case ic_return: \
c2255bc4 5612 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5613 break; \
5614 default: \
5615 gcc_unreachable (); \
5616 } \
2ac2f164 5617 } while (0)
cb3ca04e 5618
49706e39
MLI
5619 /* This macro is used to emit diagnostics to ensure that all format
5620 strings are complete sentences, visible to gettext and checked at
5621 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5622 extra parameter to enumerate qualifiers. */
5623
5624#define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5625 do { \
5626 switch (errtype) \
5627 { \
5628 case ic_argpass: \
5629 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5630 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5631 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5632 "expected %qT but argument is of type %qT", \
5633 type, rhstype); \
5634 break; \
5635 case ic_assign: \
5636 pedwarn (LOCATION, OPT, AS, QUALS); \
5637 break; \
5638 case ic_init: \
5639 pedwarn (LOCATION, OPT, IN, QUALS); \
5640 break; \
5641 case ic_return: \
5642 pedwarn (LOCATION, OPT, RE, QUALS); \
5643 break; \
5644 default: \
5645 gcc_unreachable (); \
5646 } \
5647 } while (0)
5648
8ce94e44
JM
5649 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5650 rhs = TREE_OPERAND (rhs, 0);
5651
3e4093b6
RS
5652 rhstype = TREE_TYPE (rhs);
5653 coder = TREE_CODE (rhstype);
5654
5655 if (coder == ERROR_MARK)
5656 return error_mark_node;
5657
58393038
ZL
5658 if (c_dialect_objc ())
5659 {
5660 int parmno;
5661
5662 switch (errtype)
5663 {
5664 case ic_return:
5665 parmno = 0;
5666 break;
5667
5668 case ic_assign:
5669 parmno = -1;
5670 break;
5671
5672 case ic_init:
5673 parmno = -2;
5674 break;
5675
5676 default:
5677 parmno = parmnum;
5678 break;
5679 }
5680
5681 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5682 }
5683
bbbbb16a
ILT
5684 if (warn_cxx_compat)
5685 {
5686 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5687 if (checktype != error_mark_node
5688 && TREE_CODE (type) == ENUMERAL_TYPE
5689 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5690 {
81f40b79
ILT
5691 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5692 G_("enum conversion when passing argument "
5693 "%d of %qE is invalid in C++"),
5694 G_("enum conversion in assignment is "
5695 "invalid in C++"),
5696 G_("enum conversion in initialization is "
5697 "invalid in C++"),
5698 G_("enum conversion in return is "
5699 "invalid in C++"));
bbbbb16a
ILT
5700 }
5701 }
5702
3e4093b6 5703 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5704 return rhs;
3e4093b6
RS
5705
5706 if (coder == VOID_TYPE)
400fbf9f 5707 {
6dcc04b0
JM
5708 /* Except for passing an argument to an unprototyped function,
5709 this is a constraint violation. When passing an argument to
5710 an unprototyped function, it is compile-time undefined;
5711 making it a constraint in that case was rejected in
5712 DR#252. */
c2255bc4 5713 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5714 return error_mark_node;
400fbf9f 5715 }
808d6eaa
JM
5716 rhs = require_complete_type (rhs);
5717 if (rhs == error_mark_node)
5718 return error_mark_node;
cd192ccc
MS
5719 /* A non-reference type can convert to a reference. This handles
5720 va_start, va_copy and possibly port built-ins. */
5721 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5722 {
3e4093b6 5723 if (!lvalue_p (rhs))
400fbf9f 5724 {
c2255bc4 5725 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5726 return error_mark_node;
400fbf9f 5727 }
3e4093b6
RS
5728 if (!c_mark_addressable (rhs))
5729 return error_mark_node;
5730 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5731 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5732
81e5eca8 5733 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
5734 build_pointer_type (TREE_TYPE (type)),
5735 rhs, origtype, errtype,
5736 null_pointer_constant, fundecl, function,
5737 parmnum);
cd192ccc
MS
5738 if (rhs == error_mark_node)
5739 return error_mark_node;
3e4093b6
RS
5740
5741 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5742 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5743 return rhs;
400fbf9f 5744 }
3e4093b6 5745 /* Some types can interconvert without explicit casts. */
3274deff 5746 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5747 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5748 return convert (type, rhs);
5749 /* Arithmetic types all interconvert, and enum is treated like int. */
5750 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5751 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5752 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5753 || codel == BOOLEAN_TYPE)
5754 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5755 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5756 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5757 || coder == BOOLEAN_TYPE))
928c19bb
JM
5758 {
5759 tree ret;
5760 bool save = in_late_binary_op;
dfe776dd 5761 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb 5762 in_late_binary_op = true;
81e5eca8
MP
5763 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5764 ? expr_loc : location, type, orig_rhs);
dfe776dd 5765 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb
JM
5766 in_late_binary_op = save;
5767 return ret;
5768 }
400fbf9f 5769
79077aea
JJ
5770 /* Aggregates in different TUs might need conversion. */
5771 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5772 && codel == coder
5773 && comptypes (type, rhstype))
81e5eca8
MP
5774 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5775 ? expr_loc : location, type, rhs);
79077aea 5776
ebf0bf7f 5777 /* Conversion to a transparent union or record from its member types.
3e4093b6 5778 This applies only to function arguments. */
ebf0bf7f
JJ
5779 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5780 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5781 && errtype == ic_argpass)
400fbf9f 5782 {
0257e383 5783 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5784
910ad8de 5785 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5786 {
0257e383 5787 tree memb_type = TREE_TYPE (memb);
400fbf9f 5788
3e4093b6 5789 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5790 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5791 break;
e58cd767 5792
3e4093b6
RS
5793 if (TREE_CODE (memb_type) != POINTER_TYPE)
5794 continue;
2f6e4e97 5795
3e4093b6
RS
5796 if (coder == POINTER_TYPE)
5797 {
5798 tree ttl = TREE_TYPE (memb_type);
5799 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5800
3e4093b6
RS
5801 /* Any non-function converts to a [const][volatile] void *
5802 and vice versa; otherwise, targets must be the same.
5803 Meanwhile, the lhs target must have all the qualifiers of
5804 the rhs. */
267bac10
JM
5805 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5806 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5807 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5808 {
267bac10
JM
5809 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5810 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5811 /* If this type won't generate any warnings, use it. */
267bac10 5812 if (lquals == rquals
3e4093b6
RS
5813 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5814 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5815 ? ((lquals | rquals) == rquals)
5816 : ((lquals | rquals) == lquals)))
3e4093b6 5817 break;
400fbf9f 5818
3e4093b6 5819 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5820 if (!marginal_memb)
5821 marginal_memb = memb;
3e4093b6
RS
5822 }
5823 }
82bde854 5824
3e4093b6 5825 /* Can convert integer zero to any pointer type. */
928c19bb 5826 if (null_pointer_constant)
3e4093b6
RS
5827 {
5828 rhs = null_pointer_node;
5829 break;
5830 }
5831 }
400fbf9f 5832
0257e383 5833 if (memb || marginal_memb)
3e4093b6 5834 {
0257e383 5835 if (!memb)
3e4093b6
RS
5836 {
5837 /* We have only a marginally acceptable member type;
5838 it needs a warning. */
0257e383 5839 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5840 tree ttr = TREE_TYPE (rhstype);
714a0864 5841
3e4093b6
RS
5842 /* Const and volatile mean something different for function
5843 types, so the usual warnings are not appropriate. */
5844 if (TREE_CODE (ttr) == FUNCTION_TYPE
5845 && TREE_CODE (ttl) == FUNCTION_TYPE)
5846 {
5847 /* Because const and volatile on functions are
5848 restrictions that say the function will not do
5849 certain things, it is okay to use a const or volatile
5850 function where an ordinary one is wanted, but not
5851 vice-versa. */
36c5e70a
BE
5852 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5853 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 5854 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5855 G_("passing argument %d of %qE "
49706e39 5856 "makes %q#v qualified function "
2ac2f164 5857 "pointer from unqualified"),
49706e39 5858 G_("assignment makes %q#v qualified "
2ac2f164
JM
5859 "function pointer from "
5860 "unqualified"),
49706e39 5861 G_("initialization makes %q#v qualified "
2ac2f164
JM
5862 "function pointer from "
5863 "unqualified"),
49706e39
MLI
5864 G_("return makes %q#v qualified function "
5865 "pointer from unqualified"),
5866 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 5867 }
36c5e70a
BE
5868 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5869 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
49706e39 5870 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5871 G_("passing argument %d of %qE discards "
49706e39
MLI
5872 "%qv qualifier from pointer target type"),
5873 G_("assignment discards %qv qualifier "
2ac2f164 5874 "from pointer target type"),
49706e39 5875 G_("initialization discards %qv qualifier "
2ac2f164 5876 "from pointer target type"),
49706e39
MLI
5877 G_("return discards %qv qualifier from "
5878 "pointer target type"),
5879 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
5880
5881 memb = marginal_memb;
3e4093b6 5882 }
400fbf9f 5883
fcf73884 5884 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 5885 pedwarn (location, OPT_Wpedantic,
fcf73884 5886 "ISO C prohibits argument conversion to union type");
0e7c47fa 5887
db3927fb 5888 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 5889 return build_constructor_single (type, memb, rhs);
3e4093b6 5890 }
0e7c47fa
RK
5891 }
5892
3e4093b6
RS
5893 /* Conversions among pointers */
5894 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5895 && (coder == codel))
400fbf9f 5896 {
3e4093b6
RS
5897 tree ttl = TREE_TYPE (type);
5898 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
5899 tree mvl = ttl;
5900 tree mvr = ttr;
3e4093b6 5901 bool is_opaque_pointer;
264fa2db 5902 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
5903 addr_space_t asl;
5904 addr_space_t asr;
400fbf9f 5905
46df2823 5906 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
5907 mvl = (TYPE_ATOMIC (mvl)
5908 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5909 TYPE_QUAL_ATOMIC)
5910 : TYPE_MAIN_VARIANT (mvl));
46df2823 5911 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
5912 mvr = (TYPE_ATOMIC (mvr)
5913 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5914 TYPE_QUAL_ATOMIC)
5915 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 5916 /* Opaque pointers are treated like void pointers. */
f83c7f63 5917 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 5918
478a1c5b
ILT
5919 /* The Plan 9 compiler permits a pointer to a struct to be
5920 automatically converted into a pointer to an anonymous field
5921 within the struct. */
5922 if (flag_plan9_extensions
5923 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5924 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5925 && mvl != mvr)
5926 {
5927 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5928 if (new_rhs != NULL_TREE)
5929 {
5930 rhs = new_rhs;
5931 rhstype = TREE_TYPE (rhs);
5932 coder = TREE_CODE (rhstype);
5933 ttr = TREE_TYPE (rhstype);
5934 mvr = TYPE_MAIN_VARIANT (ttr);
5935 }
5936 }
5937
b7e20b53 5938 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
5939 for the purpose of reducing the number of false positives, we
5940 tolerate the special case of
b7e20b53 5941
c22cacf3 5942 int *p = NULL;
b7e20b53 5943
c22cacf3 5944 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 5945 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
c2255bc4
AH
5946 warning_at (location, OPT_Wc___compat,
5947 "request for implicit conversion "
5948 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 5949
36c5e70a
BE
5950 /* See if the pointers point to incompatible address spaces. */
5951 asl = TYPE_ADDR_SPACE (ttl);
5952 asr = TYPE_ADDR_SPACE (ttr);
5953 if (!null_pointer_constant_p (rhs)
5954 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5955 {
5956 switch (errtype)
5957 {
5958 case ic_argpass:
5959 error_at (location, "passing argument %d of %qE from pointer to "
5960 "non-enclosed address space", parmnum, rname);
5961 break;
5962 case ic_assign:
5963 error_at (location, "assignment from pointer to "
5964 "non-enclosed address space");
5965 break;
5966 case ic_init:
5967 error_at (location, "initialization from pointer to "
5968 "non-enclosed address space");
5969 break;
5970 case ic_return:
5971 error_at (location, "return from pointer to "
5972 "non-enclosed address space");
5973 break;
5974 default:
5975 gcc_unreachable ();
5976 }
5977 return error_mark_node;
5978 }
5979
7876a414
KG
5980 /* Check if the right-hand side has a format attribute but the
5981 left-hand side doesn't. */
90137d8f 5982 if (warn_suggest_attribute_format
104f8784 5983 && check_missing_format_attribute (type, rhstype))
c22cacf3 5984 {
104f8784
KG
5985 switch (errtype)
5986 {
5987 case ic_argpass:
90137d8f 5988 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5989 "argument %d of %qE might be "
5990 "a candidate for a format attribute",
5991 parmnum, rname);
104f8784
KG
5992 break;
5993 case ic_assign:
90137d8f 5994 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5995 "assignment left-hand side might be "
5996 "a candidate for a format attribute");
104f8784
KG
5997 break;
5998 case ic_init:
90137d8f 5999 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6000 "initialization left-hand side might be "
6001 "a candidate for a format attribute");
104f8784
KG
6002 break;
6003 case ic_return:
90137d8f 6004 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6005 "return type might be "
6006 "a candidate for a format attribute");
104f8784
KG
6007 break;
6008 default:
6009 gcc_unreachable ();
6010 }
7876a414 6011 }
c22cacf3 6012
3e4093b6
RS
6013 /* Any non-function converts to a [const][volatile] void *
6014 and vice versa; otherwise, targets must be the same.
6015 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6016 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6017 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6018 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6019 || is_opaque_pointer
f8a93a2e
JJ
6020 || ((c_common_unsigned_type (mvl)
6021 == c_common_unsigned_type (mvr))
267bac10
JM
6022 && (c_common_signed_type (mvl)
6023 == c_common_signed_type (mvr))
6024 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6
RS
6025 {
6026 if (pedantic
6027 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6028 ||
6029 (VOID_TYPE_P (ttr)
928c19bb 6030 && !null_pointer_constant
3e4093b6 6031 && TREE_CODE (ttl) == FUNCTION_TYPE)))
c1771a20 6032 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
509c9d60 6033 G_("ISO C forbids passing argument %d of "
2ac2f164
JM
6034 "%qE between function pointer "
6035 "and %<void *%>"),
4b794eaf 6036 G_("ISO C forbids assignment between "
2ac2f164 6037 "function pointer and %<void *%>"),
4b794eaf 6038 G_("ISO C forbids initialization between "
2ac2f164 6039 "function pointer and %<void *%>"),
4b794eaf 6040 G_("ISO C forbids return between function "
2ac2f164 6041 "pointer and %<void *%>"));
3e4093b6
RS
6042 /* Const and volatile mean something different for function types,
6043 so the usual warnings are not appropriate. */
6044 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6045 && TREE_CODE (ttl) != FUNCTION_TYPE)
6046 {
267bac10
JM
6047 /* Assignments between atomic and non-atomic objects are OK. */
6048 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6049 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6050 {
6c39e757
NP
6051 WARN_FOR_QUALIFIERS (location, 0,
6052 G_("passing argument %d of %qE discards "
6053 "%qv qualifier from pointer target type"),
6054 G_("assignment discards %qv qualifier "
6055 "from pointer target type"),
6056 G_("initialization discards %qv qualifier "
6057 "from pointer target type"),
6058 G_("return discards %qv qualifier from "
6059 "pointer target type"),
6060 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6061 }
3e4093b6
RS
6062 /* If this is not a case of ignoring a mismatch in signedness,
6063 no warning. */
6064 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6065 || target_cmp)
3e4093b6
RS
6066 ;
6067 /* If there is a mismatch, do warn. */
f2fd3821 6068 else if (warn_pointer_sign)
c2255bc4 6069 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
509c9d60 6070 G_("pointer targets in passing argument "
2ac2f164 6071 "%d of %qE differ in signedness"),
4b794eaf 6072 G_("pointer targets in assignment "
2ac2f164 6073 "differ in signedness"),
4b794eaf 6074 G_("pointer targets in initialization "
2ac2f164 6075 "differ in signedness"),
4b794eaf 6076 G_("pointer targets in return differ "
2ac2f164 6077 "in signedness"));
3e4093b6
RS
6078 }
6079 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6080 && TREE_CODE (ttr) == FUNCTION_TYPE)
6081 {
6082 /* Because const and volatile on functions are restrictions
6083 that say the function will not do certain things,
6084 it is okay to use a const or volatile function
6085 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6086 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6087 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 6088 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 6089 G_("passing argument %d of %qE makes "
49706e39 6090 "%q#v qualified function pointer "
2ac2f164 6091 "from unqualified"),
49706e39 6092 G_("assignment makes %q#v qualified function "
2ac2f164 6093 "pointer from unqualified"),
49706e39 6094 G_("initialization makes %q#v qualified "
2ac2f164 6095 "function pointer from unqualified"),
49706e39
MLI
6096 G_("return makes %q#v qualified function "
6097 "pointer from unqualified"),
6098 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6099 }
6100 }
6101 else
58393038
ZL
6102 /* Avoid warning about the volatile ObjC EH puts on decls. */
6103 if (!objc_ok)
c2255bc4 6104 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6105 G_("passing argument %d of %qE from "
58393038 6106 "incompatible pointer type"),
4b794eaf
JJ
6107 G_("assignment from incompatible pointer type"),
6108 G_("initialization from incompatible "
58393038 6109 "pointer type"),
4b794eaf 6110 G_("return from incompatible pointer type"));
58393038 6111
3e4093b6
RS
6112 return convert (type, rhs);
6113 }
b494fd98
EB
6114 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6115 {
6dcc04b0
JM
6116 /* ??? This should not be an error when inlining calls to
6117 unprototyped functions. */
c2255bc4 6118 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6119 return error_mark_node;
6120 }
3e4093b6 6121 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6122 {
3e4093b6
RS
6123 /* An explicit constant 0 can convert to a pointer,
6124 or one that results from arithmetic, even including
6125 a cast to integer type. */
928c19bb 6126 if (!null_pointer_constant)
c2255bc4 6127 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6128 G_("passing argument %d of %qE makes "
2ac2f164 6129 "pointer from integer without a cast"),
4b794eaf 6130 G_("assignment makes pointer from integer "
2ac2f164 6131 "without a cast"),
4b794eaf 6132 G_("initialization makes pointer from "
2ac2f164 6133 "integer without a cast"),
4b794eaf 6134 G_("return makes pointer from integer "
2ac2f164 6135 "without a cast"));
b3006337
EB
6136
6137 return convert (type, rhs);
400fbf9f 6138 }
3e4093b6 6139 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6140 {
c2255bc4 6141 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6142 G_("passing argument %d of %qE makes integer "
2ac2f164 6143 "from pointer without a cast"),
4b794eaf 6144 G_("assignment makes integer from pointer "
2ac2f164 6145 "without a cast"),
4b794eaf 6146 G_("initialization makes integer from pointer "
2ac2f164 6147 "without a cast"),
4b794eaf 6148 G_("return makes integer from pointer "
2ac2f164 6149 "without a cast"));
3e4093b6
RS
6150 return convert (type, rhs);
6151 }
6152 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6153 {
6154 tree ret;
6155 bool save = in_late_binary_op;
6156 in_late_binary_op = true;
6157 ret = convert (type, rhs);
6158 in_late_binary_op = save;
6159 return ret;
6160 }
400fbf9f 6161
2ac2f164 6162 switch (errtype)
3e4093b6 6163 {
2ac2f164 6164 case ic_argpass:
c2255bc4 6165 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
a7da8b42
MLI
6166 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6167 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
6168 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6169 break;
6170 case ic_assign:
c2255bc4
AH
6171 error_at (location, "incompatible types when assigning to type %qT from "
6172 "type %qT", type, rhstype);
2ac2f164
JM
6173 break;
6174 case ic_init:
c2255bc4
AH
6175 error_at (location,
6176 "incompatible types when initializing type %qT using type %qT",
6177 type, rhstype);
2ac2f164
JM
6178 break;
6179 case ic_return:
c2255bc4
AH
6180 error_at (location,
6181 "incompatible types when returning type %qT but %qT was "
6182 "expected", rhstype, type);
2ac2f164
JM
6183 break;
6184 default:
6185 gcc_unreachable ();
400fbf9f 6186 }
53b01f59 6187
3e4093b6
RS
6188 return error_mark_node;
6189}
3e4093b6
RS
6190\f
6191/* If VALUE is a compound expr all of whose expressions are constant, then
6192 return its value. Otherwise, return error_mark_node.
15b732b2 6193
3e4093b6
RS
6194 This is for handling COMPOUND_EXPRs as initializer elements
6195 which is allowed with a warning when -pedantic is specified. */
15b732b2 6196
3e4093b6
RS
6197static tree
6198valid_compound_expr_initializer (tree value, tree endtype)
6199{
6200 if (TREE_CODE (value) == COMPOUND_EXPR)
6201 {
6202 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6203 == error_mark_node)
6204 return error_mark_node;
6205 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6206 endtype);
6207 }
116df786 6208 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6209 return error_mark_node;
6210 else
6211 return value;
15b732b2 6212}
400fbf9f 6213\f
3e4093b6
RS
6214/* Perform appropriate conversions on the initial value of a variable,
6215 store it in the declaration DECL,
6216 and print any error messages that are appropriate.
bbbbb16a 6217 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6218 If the init is invalid, store an ERROR_MARK.
6219
6220 INIT_LOC is the location of the initial value. */
400fbf9f 6221
3e4093b6 6222void
c2255bc4 6223store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6224{
3e4093b6 6225 tree value, type;
928c19bb 6226 bool npc = false;
400fbf9f 6227
3e4093b6 6228 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6229
3e4093b6
RS
6230 type = TREE_TYPE (decl);
6231 if (TREE_CODE (type) == ERROR_MARK)
6232 return;
400fbf9f 6233
3e4093b6 6234 /* Digest the specified initializer into an expression. */
400fbf9f 6235
928c19bb
JM
6236 if (init)
6237 npc = null_pointer_constant_p (init);
c2255bc4
AH
6238 value = digest_init (init_loc, type, init, origtype, npc,
6239 true, TREE_STATIC (decl));
400fbf9f 6240
3e4093b6 6241 /* Store the expression if valid; else report error. */
400fbf9f 6242
8400e75e 6243 if (!in_system_header_at (input_location)
3f75a254 6244 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6245 warning (OPT_Wtraditional, "traditional C rejects automatic "
6246 "aggregate initialization");
2f6e4e97 6247
3e4093b6 6248 DECL_INITIAL (decl) = value;
400fbf9f 6249
3e4093b6
RS
6250 /* ANSI wants warnings about out-of-range constant initializers. */
6251 STRIP_TYPE_NOPS (value);
b8698a0f 6252 if (TREE_STATIC (decl))
c2658540 6253 constant_expression_warning (value);
400fbf9f 6254
3e4093b6
RS
6255 /* Check if we need to set array size from compound literal size. */
6256 if (TREE_CODE (type) == ARRAY_TYPE
6257 && TYPE_DOMAIN (type) == 0
6258 && value != error_mark_node)
400fbf9f 6259 {
3e4093b6
RS
6260 tree inside_init = init;
6261
ed248cf7 6262 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6263 inside_init = fold (inside_init);
6264
6265 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6266 {
8d9f82d5 6267 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6268
8d9f82d5 6269 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6270 {
6271 /* For int foo[] = (int [3]){1}; we need to set array size
6272 now since later on array initializer will be just the
6273 brace enclosed list of the compound literal. */
e30ecc5d 6274 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6275 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6276 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6277 layout_type (type);
8d9f82d5 6278 layout_decl (cldecl, 0);
e30ecc5d
JJ
6279 TREE_TYPE (decl)
6280 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6281 }
6282 }
400fbf9f 6283 }
3e4093b6
RS
6284}
6285\f
6286/* Methods for storing and printing names for error messages. */
400fbf9f 6287
3e4093b6
RS
6288/* Implement a spelling stack that allows components of a name to be pushed
6289 and popped. Each element on the stack is this structure. */
400fbf9f 6290
3e4093b6
RS
6291struct spelling
6292{
6293 int kind;
6294 union
400fbf9f 6295 {
a0f0ab9f 6296 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6297 const char *s;
6298 } u;
6299};
2f6e4e97 6300
3e4093b6
RS
6301#define SPELLING_STRING 1
6302#define SPELLING_MEMBER 2
6303#define SPELLING_BOUNDS 3
400fbf9f 6304
3e4093b6
RS
6305static struct spelling *spelling; /* Next stack element (unused). */
6306static struct spelling *spelling_base; /* Spelling stack base. */
6307static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6308
3e4093b6
RS
6309/* Macros to save and restore the spelling stack around push_... functions.
6310 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6311
3e4093b6
RS
6312#define SPELLING_DEPTH() (spelling - spelling_base)
6313#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6314
3e4093b6
RS
6315/* Push an element on the spelling stack with type KIND and assign VALUE
6316 to MEMBER. */
400fbf9f 6317
3e4093b6
RS
6318#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6319{ \
6320 int depth = SPELLING_DEPTH (); \
6321 \
6322 if (depth >= spelling_size) \
6323 { \
6324 spelling_size += 10; \
cca8ead2
BI
6325 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6326 spelling_size); \
3e4093b6
RS
6327 RESTORE_SPELLING_DEPTH (depth); \
6328 } \
6329 \
6330 spelling->kind = (KIND); \
6331 spelling->MEMBER = (VALUE); \
6332 spelling++; \
6333}
400fbf9f 6334
3e4093b6 6335/* Push STRING on the stack. Printed literally. */
400fbf9f 6336
3e4093b6
RS
6337static void
6338push_string (const char *string)
6339{
6340 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6341}
400fbf9f 6342
3e4093b6 6343/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6344
3e4093b6
RS
6345static void
6346push_member_name (tree decl)
6347{
6348 const char *const string
88388a52
JM
6349 = (DECL_NAME (decl)
6350 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6351 : _("<anonymous>"));
3e4093b6
RS
6352 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6353}
400fbf9f 6354
3e4093b6 6355/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6356
3e4093b6 6357static void
a0f0ab9f 6358push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6359{
6360 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6361}
bb58bec5 6362
3e4093b6 6363/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6364
3e4093b6
RS
6365static int
6366spelling_length (void)
6367{
6368 int size = 0;
6369 struct spelling *p;
400fbf9f 6370
3e4093b6
RS
6371 for (p = spelling_base; p < spelling; p++)
6372 {
6373 if (p->kind == SPELLING_BOUNDS)
6374 size += 25;
6375 else
6376 size += strlen (p->u.s) + 1;
6377 }
6378
6379 return size;
400fbf9f 6380}
400fbf9f 6381
3e4093b6 6382/* Print the spelling to BUFFER and return it. */
400fbf9f 6383
3e4093b6
RS
6384static char *
6385print_spelling (char *buffer)
400fbf9f 6386{
3e4093b6
RS
6387 char *d = buffer;
6388 struct spelling *p;
400fbf9f 6389
3e4093b6
RS
6390 for (p = spelling_base; p < spelling; p++)
6391 if (p->kind == SPELLING_BOUNDS)
6392 {
a0f0ab9f 6393 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6394 d += strlen (d);
6395 }
6396 else
6397 {
6398 const char *s;
6399 if (p->kind == SPELLING_MEMBER)
6400 *d++ = '.';
6401 for (s = p->u.s; (*d = *s++); d++)
6402 ;
6403 }
6404 *d++ = '\0';
6405 return buffer;
6406}
400fbf9f 6407
3e4093b6 6408/* Issue an error message for a bad initializer component.
6a8f4e12 6409 GMSGID identifies the message.
3e4093b6 6410 The component name is taken from the spelling stack. */
400fbf9f 6411
3e4093b6 6412void
6a8f4e12 6413error_init (const char *gmsgid)
3e4093b6
RS
6414{
6415 char *ofwhat;
400fbf9f 6416
6a8f4e12
AP
6417 /* The gmsgid may be a format string with %< and %>. */
6418 error (gmsgid);
28dab132 6419 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6420 if (*ofwhat)
bda67431 6421 error ("(near initialization for %qs)", ofwhat);
3e4093b6 6422}
400fbf9f 6423
fcf73884
MLI
6424/* Issue a pedantic warning for a bad initializer component. OPT is
6425 the option OPT_* (from options.h) controlling this warning or 0 if
6a8f4e12 6426 it is unconditionally given. GMSGID identifies the message. The
fcf73884 6427 component name is taken from the spelling stack. */
400fbf9f 6428
3e4093b6 6429void
6a8f4e12 6430pedwarn_init (location_t location, int opt, const char *gmsgid)
3e4093b6
RS
6431{
6432 char *ofwhat;
f90e8e2e 6433
6a8f4e12
AP
6434 /* The gmsgid may be a format string with %< and %>. */
6435 pedwarn (location, opt, gmsgid);
28dab132 6436 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6437 if (*ofwhat)
509c9d60 6438 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
3e4093b6 6439}
9f720c3e 6440
b8698a0f 6441/* Issue a warning for a bad initializer component.
683d6ff9
MLI
6442
6443 OPT is the OPT_W* value corresponding to the warning option that
6a8f4e12 6444 controls this warning. GMSGID identifies the message. The
683d6ff9 6445 component name is taken from the spelling stack. */
61179109 6446
3e4093b6 6447static void
6a8f4e12 6448warning_init (int opt, const char *gmsgid)
3e4093b6
RS
6449{
6450 char *ofwhat;
7e842ef8 6451
6a8f4e12
AP
6452 /* The gmsgid may be a format string with %< and %>. */
6453 warning (opt, gmsgid);
28dab132 6454 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6455 if (*ofwhat)
683d6ff9 6456 warning (opt, "(near initialization for %qs)", ofwhat);
3e4093b6
RS
6457}
6458\f
916c5919
JM
6459/* If TYPE is an array type and EXPR is a parenthesized string
6460 constant, warn if pedantic that EXPR is being used to initialize an
6461 object of type TYPE. */
6462
6463void
6464maybe_warn_string_init (tree type, struct c_expr expr)
6465{
6466 if (pedantic
6467 && TREE_CODE (type) == ARRAY_TYPE
6468 && TREE_CODE (expr.value) == STRING_CST
6469 && expr.original_code != STRING_CST)
c1771a20 6470 pedwarn_init (input_location, OPT_Wpedantic,
fcf73884 6471 "array initialized from parenthesized string constant");
916c5919
JM
6472}
6473
3e4093b6
RS
6474/* Digest the parser output INIT as an initializer for type TYPE.
6475 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6476
bbbbb16a
ILT
6477 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6478
928c19bb
JM
6479 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6480
916c5919
JM
6481 If INIT is a string constant, STRICT_STRING is true if it is
6482 unparenthesized or we should not warn here for it being parenthesized.
6483 For other types of INIT, STRICT_STRING is not used.
6484
c2255bc4
AH
6485 INIT_LOC is the location of the INIT.
6486
3e4093b6
RS
6487 REQUIRE_CONSTANT requests an error if non-constant initializers or
6488 elements are seen. */
7e842ef8 6489
3e4093b6 6490static tree
c2255bc4
AH
6491digest_init (location_t init_loc, tree type, tree init, tree origtype,
6492 bool null_pointer_constant, bool strict_string,
6493 int require_constant)
3e4093b6
RS
6494{
6495 enum tree_code code = TREE_CODE (type);
6496 tree inside_init = init;
8ce94e44 6497 tree semantic_type = NULL_TREE;
928c19bb 6498 bool maybe_const = true;
7e842ef8 6499
3e4093b6 6500 if (type == error_mark_node
f01da1a5 6501 || !init
3e4093b6
RS
6502 || init == error_mark_node
6503 || TREE_TYPE (init) == error_mark_node)
6504 return error_mark_node;
7e842ef8 6505
ed248cf7 6506 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6507
8ce94e44
JM
6508 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6509 {
6510 semantic_type = TREE_TYPE (inside_init);
6511 inside_init = TREE_OPERAND (inside_init, 0);
6512 }
928c19bb
JM
6513 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6514 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6515
3e4093b6
RS
6516 /* Initialization of an array of chars from a string constant
6517 optionally enclosed in braces. */
7e842ef8 6518
197463ae
JM
6519 if (code == ARRAY_TYPE && inside_init
6520 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6521 {
267bac10
JM
6522 tree typ1
6523 = (TYPE_ATOMIC (TREE_TYPE (type))
6524 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6525 TYPE_QUAL_ATOMIC)
6526 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6527 /* Note that an array could be both an array of character type
6528 and an array of wchar_t if wchar_t is signed char or unsigned
6529 char. */
6530 bool char_array = (typ1 == char_type_node
6531 || typ1 == signed_char_type_node
6532 || typ1 == unsigned_char_type_node);
6533 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6534 bool char16_array = !!comptypes (typ1, char16_type_node);
6535 bool char32_array = !!comptypes (typ1, char32_type_node);
6536
6537 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6538 {
916c5919 6539 struct c_expr expr;
c466b2cd 6540 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6541 expr.value = inside_init;
6542 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6543 expr.original_type = NULL;
916c5919
JM
6544 maybe_warn_string_init (type, expr);
6545
a45e580b 6546 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6547 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6548 "initialization of a flexible array member");
6549
3e4093b6 6550 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6551 TYPE_MAIN_VARIANT (type)))
3e4093b6 6552 return inside_init;
7e842ef8 6553
c466b2cd 6554 if (char_array)
3e4093b6 6555 {
c466b2cd
KVH
6556 if (typ2 != char_type_node)
6557 {
6558 error_init ("char-array initialized from wide string");
6559 return error_mark_node;
6560 }
3e4093b6 6561 }
c466b2cd 6562 else
3e4093b6 6563 {
c466b2cd
KVH
6564 if (typ2 == char_type_node)
6565 {
6566 error_init ("wide character array initialized from non-wide "
6567 "string");
6568 return error_mark_node;
6569 }
6570 else if (!comptypes(typ1, typ2))
6571 {
6572 error_init ("wide character array initialized from "
6573 "incompatible wide string");
6574 return error_mark_node;
6575 }
7e842ef8 6576 }
2f6e4e97 6577
3e4093b6
RS
6578 TREE_TYPE (inside_init) = type;
6579 if (TYPE_DOMAIN (type) != 0
6580 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6581 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6582 {
6583 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6584
c466b2cd 6585 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6586 because it's ok to ignore the terminating null char
6587 that is counted in the length of the constant. */
5eb4df45
ILT
6588 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6589 (len
6590 - (TYPE_PRECISION (typ1)
6591 / BITS_PER_UNIT))))
6592 pedwarn_init (init_loc, 0,
6593 ("initializer-string for array of chars "
6594 "is too long"));
6595 else if (warn_cxx_compat
6596 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6597 warning_at (init_loc, OPT_Wc___compat,
6598 ("initializer-string for array chars "
6599 "is too long for C++"));
6600 }
7e842ef8 6601
3e4093b6 6602 return inside_init;
7e842ef8 6603 }
197463ae
JM
6604 else if (INTEGRAL_TYPE_P (typ1))
6605 {
6606 error_init ("array of inappropriate type initialized "
6607 "from string constant");
6608 return error_mark_node;
6609 }
7e842ef8
PE
6610 }
6611
3e4093b6
RS
6612 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6613 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6614 below and handle as a constructor. */
e89be13b
JJ
6615 if (code == VECTOR_TYPE
6616 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6617 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6618 && TREE_CONSTANT (inside_init))
6619 {
6620 if (TREE_CODE (inside_init) == VECTOR_CST
6621 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6622 TYPE_MAIN_VARIANT (type)))
6623 return inside_init;
6624
6625 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6626 {
4038c495
GB
6627 unsigned HOST_WIDE_INT ix;
6628 tree value;
6629 bool constant_p = true;
e89be13b
JJ
6630
6631 /* Iterate through elements and check if all constructor
6632 elements are *_CSTs. */
4038c495
GB
6633 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6634 if (!CONSTANT_CLASS_P (value))
6635 {
6636 constant_p = false;
6637 break;
6638 }
e89be13b 6639
4038c495
GB
6640 if (constant_p)
6641 return build_vector_from_ctor (type,
6642 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6643 }
6644 }
6035d635 6645
ca085fd7
MLI
6646 if (warn_sequence_point)
6647 verify_sequence_points (inside_init);
6648
3e4093b6
RS
6649 /* Any type can be initialized
6650 from an expression of the same type, optionally with braces. */
400fbf9f 6651
3e4093b6
RS
6652 if (inside_init && TREE_TYPE (inside_init) != 0
6653 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6654 TYPE_MAIN_VARIANT (type))
3e4093b6 6655 || (code == ARRAY_TYPE
132da1a5 6656 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6657 || (code == VECTOR_TYPE
132da1a5 6658 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6659 || (code == POINTER_TYPE
3897f229 6660 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6661 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6662 TREE_TYPE (type)))))
3e4093b6
RS
6663 {
6664 if (code == POINTER_TYPE)
b494fd98 6665 {
b494fd98
EB
6666 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6667 {
f2a71bbc
JM
6668 if (TREE_CODE (inside_init) == STRING_CST
6669 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6670 inside_init = array_to_pointer_conversion
6671 (init_loc, inside_init);
f2a71bbc
JM
6672 else
6673 {
6674 error_init ("invalid use of non-lvalue array");
6675 return error_mark_node;
6676 }
b494fd98 6677 }
f2a71bbc 6678 }
b494fd98 6679
bae39a73
NS
6680 if (code == VECTOR_TYPE)
6681 /* Although the types are compatible, we may require a
6682 conversion. */
6683 inside_init = convert (type, inside_init);
3e4093b6 6684
ca58211b
PB
6685 if (require_constant
6686 && (code == VECTOR_TYPE || !flag_isoc99)
3e4093b6 6687 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6688 {
3e4093b6
RS
6689 /* As an extension, allow initializing objects with static storage
6690 duration with compound literals (which are then treated just as
ca58211b
PB
6691 the brace enclosed list they contain). Also allow this for
6692 vectors, as we can only assign them with compound literals. */
3e4093b6
RS
6693 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6694 inside_init = DECL_INITIAL (decl);
400fbf9f 6695 }
3e4093b6
RS
6696
6697 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6698 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6699 {
3e4093b6
RS
6700 error_init ("array initialized from non-constant array expression");
6701 return error_mark_node;
400fbf9f 6702 }
400fbf9f 6703
c1771a20 6704 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6705 -pedantic-errors is specified. In the later case, we always want
6706 an error. In the former case, we simply want a warning. */
6707 if (require_constant && pedantic
6708 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6709 {
6710 inside_init
6711 = valid_compound_expr_initializer (inside_init,
6712 TREE_TYPE (inside_init));
6713 if (inside_init == error_mark_node)
6714 error_init ("initializer element is not constant");
2f6e4e97 6715 else
c1771a20 6716 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6717 "initializer element is not constant");
3e4093b6
RS
6718 if (flag_pedantic_errors)
6719 inside_init = error_mark_node;
6720 }
6721 else if (require_constant
116df786
RH
6722 && !initializer_constant_valid_p (inside_init,
6723 TREE_TYPE (inside_init)))
3e4093b6
RS
6724 {
6725 error_init ("initializer element is not constant");
6726 inside_init = error_mark_node;
8b40563c 6727 }
928c19bb 6728 else if (require_constant && !maybe_const)
c2255bc4 6729 pedwarn_init (init_loc, 0,
928c19bb 6730 "initializer element is not a constant expression");
f735a153 6731
90137d8f 6732 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6733 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
6734 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6735 type, inside_init, origtype,
bbbbb16a 6736 ic_init, null_pointer_constant,
928c19bb 6737 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6738 return inside_init;
6739 }
f735a153 6740
3e4093b6 6741 /* Handle scalar types, including conversions. */
400fbf9f 6742
ab22c1fa
CF
6743 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6744 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6745 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6746 {
f2a71bbc
JM
6747 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6748 && (TREE_CODE (init) == STRING_CST
6749 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6750 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6751 if (semantic_type)
6752 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6753 inside_init);
3e4093b6 6754 inside_init
68fca595
MP
6755 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6756 inside_init, origtype, ic_init,
6757 null_pointer_constant, NULL_TREE, NULL_TREE,
6758 0);
2f6e4e97 6759
3274deff
JW
6760 /* Check to see if we have already given an error message. */
6761 if (inside_init == error_mark_node)
6762 ;
3f75a254 6763 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6764 {
3e4093b6
RS
6765 error_init ("initializer element is not constant");
6766 inside_init = error_mark_node;
400fbf9f 6767 }
3e4093b6 6768 else if (require_constant
116df786
RH
6769 && !initializer_constant_valid_p (inside_init,
6770 TREE_TYPE (inside_init)))
400fbf9f 6771 {
3e4093b6
RS
6772 error_init ("initializer element is not computable at load time");
6773 inside_init = error_mark_node;
400fbf9f 6774 }
928c19bb 6775 else if (require_constant && !maybe_const)
c2255bc4 6776 pedwarn_init (init_loc, 0,
928c19bb 6777 "initializer element is not a constant expression");
3e4093b6
RS
6778
6779 return inside_init;
400fbf9f 6780 }
d9fc6069 6781
3e4093b6 6782 /* Come here only for records and arrays. */
d9fc6069 6783
3e4093b6 6784 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6785 {
3e4093b6
RS
6786 error_init ("variable-sized object may not be initialized");
6787 return error_mark_node;
d9fc6069 6788 }
3e4093b6
RS
6789
6790 error_init ("invalid initializer");
6791 return error_mark_node;
d9fc6069 6792}
400fbf9f 6793\f
3e4093b6 6794/* Handle initializers that use braces. */
400fbf9f 6795
3e4093b6
RS
6796/* Type of object we are accumulating a constructor for.
6797 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6798static tree constructor_type;
400fbf9f 6799
3e4093b6
RS
6800/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6801 left to fill. */
6802static tree constructor_fields;
400fbf9f 6803
3e4093b6
RS
6804/* For an ARRAY_TYPE, this is the specified index
6805 at which to store the next element we get. */
6806static tree constructor_index;
400fbf9f 6807
3e4093b6
RS
6808/* For an ARRAY_TYPE, this is the maximum index. */
6809static tree constructor_max_index;
400fbf9f 6810
3e4093b6
RS
6811/* For a RECORD_TYPE, this is the first field not yet written out. */
6812static tree constructor_unfilled_fields;
400fbf9f 6813
3e4093b6
RS
6814/* For an ARRAY_TYPE, this is the index of the first element
6815 not yet written out. */
6816static tree constructor_unfilled_index;
895ea614 6817
3e4093b6
RS
6818/* In a RECORD_TYPE, the byte index of the next consecutive field.
6819 This is so we can generate gaps between fields, when appropriate. */
6820static tree constructor_bit_index;
10d5caec 6821
3e4093b6
RS
6822/* If we are saving up the elements rather than allocating them,
6823 this is the list of elements so far (in reverse order,
6824 most recent first). */
9771b263 6825static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6826
3e4093b6
RS
6827/* 1 if constructor should be incrementally stored into a constructor chain,
6828 0 if all the elements should be kept in AVL tree. */
6829static int constructor_incremental;
ad47f1e5 6830
3e4093b6
RS
6831/* 1 if so far this constructor's elements are all compile-time constants. */
6832static int constructor_constant;
ad47f1e5 6833
3e4093b6
RS
6834/* 1 if so far this constructor's elements are all valid address constants. */
6835static int constructor_simple;
ad47f1e5 6836
928c19bb
JM
6837/* 1 if this constructor has an element that cannot be part of a
6838 constant expression. */
6839static int constructor_nonconst;
6840
3e4093b6
RS
6841/* 1 if this constructor is erroneous so far. */
6842static int constructor_erroneous;
d45cf215 6843
3e4093b6
RS
6844/* Structure for managing pending initializer elements, organized as an
6845 AVL tree. */
d45cf215 6846
3e4093b6 6847struct init_node
d45cf215 6848{
3e4093b6
RS
6849 struct init_node *left, *right;
6850 struct init_node *parent;
6851 int balance;
6852 tree purpose;
6853 tree value;
bbbbb16a 6854 tree origtype;
d45cf215
RS
6855};
6856
3e4093b6
RS
6857/* Tree of pending elements at this constructor level.
6858 These are elements encountered out of order
6859 which belong at places we haven't reached yet in actually
6860 writing the output.
6861 Will never hold tree nodes across GC runs. */
6862static struct init_node *constructor_pending_elts;
d45cf215 6863
3e4093b6
RS
6864/* The SPELLING_DEPTH of this constructor. */
6865static int constructor_depth;
d45cf215 6866
3e4093b6
RS
6867/* DECL node for which an initializer is being read.
6868 0 means we are reading a constructor expression
6869 such as (struct foo) {...}. */
6870static tree constructor_decl;
d45cf215 6871
3e4093b6
RS
6872/* Nonzero if this is an initializer for a top-level decl. */
6873static int constructor_top_level;
d45cf215 6874
3e4093b6
RS
6875/* Nonzero if there were any member designators in this initializer. */
6876static int constructor_designated;
d45cf215 6877
3e4093b6
RS
6878/* Nesting depth of designator list. */
6879static int designator_depth;
d45cf215 6880
3e4093b6 6881/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 6882static int designator_erroneous;
d45cf215 6883
3e4093b6
RS
6884\f
6885/* This stack has a level for each implicit or explicit level of
6886 structuring in the initializer, including the outermost one. It
6887 saves the values of most of the variables above. */
d45cf215 6888
3e4093b6
RS
6889struct constructor_range_stack;
6890
6891struct constructor_stack
d45cf215 6892{
3e4093b6
RS
6893 struct constructor_stack *next;
6894 tree type;
6895 tree fields;
6896 tree index;
6897 tree max_index;
6898 tree unfilled_index;
6899 tree unfilled_fields;
6900 tree bit_index;
9771b263 6901 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6902 struct init_node *pending_elts;
6903 int offset;
6904 int depth;
916c5919 6905 /* If value nonzero, this value should replace the entire
3e4093b6 6906 constructor at this level. */
916c5919 6907 struct c_expr replacement_value;
3e4093b6
RS
6908 struct constructor_range_stack *range_stack;
6909 char constant;
6910 char simple;
928c19bb 6911 char nonconst;
3e4093b6
RS
6912 char implicit;
6913 char erroneous;
6914 char outer;
6915 char incremental;
6916 char designated;
6917};
d45cf215 6918
802415d1 6919static struct constructor_stack *constructor_stack;
d45cf215 6920
3e4093b6
RS
6921/* This stack represents designators from some range designator up to
6922 the last designator in the list. */
d45cf215 6923
3e4093b6
RS
6924struct constructor_range_stack
6925{
6926 struct constructor_range_stack *next, *prev;
6927 struct constructor_stack *stack;
6928 tree range_start;
6929 tree index;
6930 tree range_end;
6931 tree fields;
6932};
d45cf215 6933
802415d1 6934static struct constructor_range_stack *constructor_range_stack;
d45cf215 6935
3e4093b6
RS
6936/* This stack records separate initializers that are nested.
6937 Nested initializers can't happen in ANSI C, but GNU C allows them
6938 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 6939
3e4093b6 6940struct initializer_stack
d45cf215 6941{
3e4093b6
RS
6942 struct initializer_stack *next;
6943 tree decl;
3e4093b6
RS
6944 struct constructor_stack *constructor_stack;
6945 struct constructor_range_stack *constructor_range_stack;
9771b263 6946 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6947 struct spelling *spelling;
6948 struct spelling *spelling_base;
6949 int spelling_size;
6950 char top_level;
6951 char require_constant_value;
6952 char require_constant_elements;
6953};
d45cf215 6954
802415d1 6955static struct initializer_stack *initializer_stack;
3e4093b6
RS
6956\f
6957/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
6958
6959void
a396f8ae 6960start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 6961{
3e4093b6 6962 const char *locus;
cceb1885 6963 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 6964
3e4093b6 6965 p->decl = constructor_decl;
3e4093b6
RS
6966 p->require_constant_value = require_constant_value;
6967 p->require_constant_elements = require_constant_elements;
6968 p->constructor_stack = constructor_stack;
6969 p->constructor_range_stack = constructor_range_stack;
6970 p->elements = constructor_elements;
6971 p->spelling = spelling;
6972 p->spelling_base = spelling_base;
6973 p->spelling_size = spelling_size;
6974 p->top_level = constructor_top_level;
6975 p->next = initializer_stack;
6976 initializer_stack = p;
400fbf9f 6977
3e4093b6 6978 constructor_decl = decl;
3e4093b6
RS
6979 constructor_designated = 0;
6980 constructor_top_level = top_level;
400fbf9f 6981
6f17bbcf 6982 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
6983 {
6984 require_constant_value = TREE_STATIC (decl);
6985 require_constant_elements
6986 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6987 /* For a scalar, you can always use any value to initialize,
6988 even within braces. */
6989 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6990 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6991 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6992 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 6993 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
6994 }
6995 else
6996 {
6997 require_constant_value = 0;
6998 require_constant_elements = 0;
88388a52 6999 locus = _("(anonymous)");
3e4093b6 7000 }
b71c7f8a 7001
3e4093b6
RS
7002 constructor_stack = 0;
7003 constructor_range_stack = 0;
b71c7f8a 7004
3e4093b6
RS
7005 missing_braces_mentioned = 0;
7006
7007 spelling_base = 0;
7008 spelling_size = 0;
7009 RESTORE_SPELLING_DEPTH (0);
7010
7011 if (locus)
7012 push_string (locus);
7013}
7014
7015void
7016finish_init (void)
b71c7f8a 7017{
3e4093b6 7018 struct initializer_stack *p = initializer_stack;
b71c7f8a 7019
3e4093b6
RS
7020 /* Free the whole constructor stack of this initializer. */
7021 while (constructor_stack)
7022 {
7023 struct constructor_stack *q = constructor_stack;
7024 constructor_stack = q->next;
7025 free (q);
7026 }
7027
366de0ce 7028 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7029
7030 /* Pop back to the data of the outer initializer (if any). */
36579663 7031 free (spelling_base);
3aeb3655 7032
3e4093b6 7033 constructor_decl = p->decl;
3e4093b6
RS
7034 require_constant_value = p->require_constant_value;
7035 require_constant_elements = p->require_constant_elements;
7036 constructor_stack = p->constructor_stack;
7037 constructor_range_stack = p->constructor_range_stack;
7038 constructor_elements = p->elements;
7039 spelling = p->spelling;
7040 spelling_base = p->spelling_base;
7041 spelling_size = p->spelling_size;
7042 constructor_top_level = p->top_level;
7043 initializer_stack = p->next;
7044 free (p);
b71c7f8a 7045}
400fbf9f 7046\f
3e4093b6
RS
7047/* Call here when we see the initializer is surrounded by braces.
7048 This is instead of a call to push_init_level;
7049 it is matched by a call to pop_init_level.
400fbf9f 7050
3e4093b6
RS
7051 TYPE is the type to initialize, for a constructor expression.
7052 For an initializer for a decl, TYPE is zero. */
400fbf9f 7053
3e4093b6
RS
7054void
7055really_start_incremental_init (tree type)
400fbf9f 7056{
5d038c4c 7057 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7058
3e4093b6
RS
7059 if (type == 0)
7060 type = TREE_TYPE (constructor_decl);
400fbf9f 7061
b6fc2cdb
PB
7062 if (TREE_CODE (type) == VECTOR_TYPE
7063 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7064 error ("opaque vector types cannot be initialized");
400fbf9f 7065
3e4093b6
RS
7066 p->type = constructor_type;
7067 p->fields = constructor_fields;
7068 p->index = constructor_index;
7069 p->max_index = constructor_max_index;
7070 p->unfilled_index = constructor_unfilled_index;
7071 p->unfilled_fields = constructor_unfilled_fields;
7072 p->bit_index = constructor_bit_index;
7073 p->elements = constructor_elements;
7074 p->constant = constructor_constant;
7075 p->simple = constructor_simple;
928c19bb 7076 p->nonconst = constructor_nonconst;
3e4093b6
RS
7077 p->erroneous = constructor_erroneous;
7078 p->pending_elts = constructor_pending_elts;
7079 p->depth = constructor_depth;
916c5919
JM
7080 p->replacement_value.value = 0;
7081 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7082 p->replacement_value.original_type = NULL;
3e4093b6
RS
7083 p->implicit = 0;
7084 p->range_stack = 0;
7085 p->outer = 0;
7086 p->incremental = constructor_incremental;
7087 p->designated = constructor_designated;
7088 p->next = 0;
7089 constructor_stack = p;
b13aca19 7090
3e4093b6
RS
7091 constructor_constant = 1;
7092 constructor_simple = 1;
928c19bb 7093 constructor_nonconst = 0;
3e4093b6 7094 constructor_depth = SPELLING_DEPTH ();
9771b263 7095 constructor_elements = NULL;
3e4093b6
RS
7096 constructor_pending_elts = 0;
7097 constructor_type = type;
7098 constructor_incremental = 1;
7099 constructor_designated = 0;
7100 designator_depth = 0;
b06df647 7101 designator_erroneous = 0;
400fbf9f 7102
3e4093b6
RS
7103 if (TREE_CODE (constructor_type) == RECORD_TYPE
7104 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7105 {
3e4093b6
RS
7106 constructor_fields = TYPE_FIELDS (constructor_type);
7107 /* Skip any nameless bit fields at the beginning. */
7108 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7109 && DECL_NAME (constructor_fields) == 0)
910ad8de 7110 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7111
3e4093b6
RS
7112 constructor_unfilled_fields = constructor_fields;
7113 constructor_bit_index = bitsize_zero_node;
400fbf9f 7114 }
3e4093b6
RS
7115 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7116 {
7117 if (TYPE_DOMAIN (constructor_type))
7118 {
7119 constructor_max_index
7120 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7121
3e4093b6
RS
7122 /* Detect non-empty initializations of zero-length arrays. */
7123 if (constructor_max_index == NULL_TREE
7124 && TYPE_SIZE (constructor_type))
9a9d280e 7125 constructor_max_index = integer_minus_one_node;
400fbf9f 7126
3e4093b6
RS
7127 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7128 to initialize VLAs will cause a proper error; avoid tree
7129 checking errors as well by setting a safe value. */
7130 if (constructor_max_index
7131 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7132 constructor_max_index = integer_minus_one_node;
59c83dbf 7133
3e4093b6
RS
7134 constructor_index
7135 = convert (bitsizetype,
7136 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7137 }
3e4093b6 7138 else
493179da
JM
7139 {
7140 constructor_index = bitsize_zero_node;
7141 constructor_max_index = NULL_TREE;
7142 }
59c83dbf 7143
3e4093b6
RS
7144 constructor_unfilled_index = constructor_index;
7145 }
7146 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7147 {
7148 /* Vectors are like simple fixed-size arrays. */
7149 constructor_max_index =
c62c040f 7150 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7151 constructor_index = bitsize_zero_node;
3e4093b6
RS
7152 constructor_unfilled_index = constructor_index;
7153 }
7154 else
7155 {
7156 /* Handle the case of int x = {5}; */
7157 constructor_fields = constructor_type;
7158 constructor_unfilled_fields = constructor_type;
7159 }
7160}
7161\f
7162/* Push down into a subobject, for initialization.
7163 If this is for an explicit set of braces, IMPLICIT is 0.
7164 If it is because the next element belongs at a lower level,
7165 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7166
3e4093b6 7167void
a1e3b3d9 7168push_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
7169{
7170 struct constructor_stack *p;
7171 tree value = NULL_TREE;
400fbf9f 7172
3e4093b6 7173 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7174 pop them now. If implicit == 1, this will have been done in
7175 process_init_element; do not repeat it here because in the case
7176 of excess initializers for an empty aggregate this leads to an
7177 infinite cycle of popping a level and immediately recreating
7178 it. */
7179 if (implicit != 1)
3e4093b6 7180 {
472d98b4
JM
7181 while (constructor_stack->implicit)
7182 {
7183 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7184 || TREE_CODE (constructor_type) == UNION_TYPE)
7185 && constructor_fields == 0)
34cf811f
MP
7186 process_init_element (input_location,
7187 pop_init_level (1, braced_init_obstack),
a1e3b3d9 7188 true, braced_init_obstack);
472d98b4
JM
7189 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7190 && constructor_max_index
7191 && tree_int_cst_lt (constructor_max_index,
7192 constructor_index))
34cf811f
MP
7193 process_init_element (input_location,
7194 pop_init_level (1, braced_init_obstack),
a1e3b3d9 7195 true, braced_init_obstack);
472d98b4
JM
7196 else
7197 break;
7198 }
3e4093b6 7199 }
400fbf9f 7200
3e4093b6
RS
7201 /* Unless this is an explicit brace, we need to preserve previous
7202 content if any. */
7203 if (implicit)
7204 {
7205 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7206 || TREE_CODE (constructor_type) == UNION_TYPE)
7207 && constructor_fields)
a1e3b3d9 7208 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7209 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7210 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7211 }
7212
5d038c4c 7213 p = XNEW (struct constructor_stack);
3e4093b6
RS
7214 p->type = constructor_type;
7215 p->fields = constructor_fields;
7216 p->index = constructor_index;
7217 p->max_index = constructor_max_index;
7218 p->unfilled_index = constructor_unfilled_index;
7219 p->unfilled_fields = constructor_unfilled_fields;
7220 p->bit_index = constructor_bit_index;
7221 p->elements = constructor_elements;
7222 p->constant = constructor_constant;
7223 p->simple = constructor_simple;
928c19bb 7224 p->nonconst = constructor_nonconst;
3e4093b6
RS
7225 p->erroneous = constructor_erroneous;
7226 p->pending_elts = constructor_pending_elts;
7227 p->depth = constructor_depth;
916c5919
JM
7228 p->replacement_value.value = 0;
7229 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7230 p->replacement_value.original_type = NULL;
3e4093b6
RS
7231 p->implicit = implicit;
7232 p->outer = 0;
7233 p->incremental = constructor_incremental;
7234 p->designated = constructor_designated;
7235 p->next = constructor_stack;
7236 p->range_stack = 0;
7237 constructor_stack = p;
400fbf9f 7238
3e4093b6
RS
7239 constructor_constant = 1;
7240 constructor_simple = 1;
928c19bb 7241 constructor_nonconst = 0;
3e4093b6 7242 constructor_depth = SPELLING_DEPTH ();
9771b263 7243 constructor_elements = NULL;
3e4093b6
RS
7244 constructor_incremental = 1;
7245 constructor_designated = 0;
7246 constructor_pending_elts = 0;
7247 if (!implicit)
400fbf9f 7248 {
3e4093b6
RS
7249 p->range_stack = constructor_range_stack;
7250 constructor_range_stack = 0;
7251 designator_depth = 0;
b06df647 7252 designator_erroneous = 0;
3e4093b6 7253 }
400fbf9f 7254
3e4093b6
RS
7255 /* Don't die if an entire brace-pair level is superfluous
7256 in the containing level. */
7257 if (constructor_type == 0)
7258 ;
7259 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7260 || TREE_CODE (constructor_type) == UNION_TYPE)
7261 {
7262 /* Don't die if there are extra init elts at the end. */
7263 if (constructor_fields == 0)
7264 constructor_type = 0;
7265 else
400fbf9f 7266 {
3e4093b6
RS
7267 constructor_type = TREE_TYPE (constructor_fields);
7268 push_member_name (constructor_fields);
7269 constructor_depth++;
400fbf9f 7270 }
3e4093b6
RS
7271 }
7272 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7273 {
7274 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7275 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7276 constructor_depth++;
400fbf9f
JW
7277 }
7278
3e4093b6 7279 if (constructor_type == 0)
400fbf9f 7280 {
3e4093b6
RS
7281 error_init ("extra brace group at end of initializer");
7282 constructor_fields = 0;
7283 constructor_unfilled_fields = 0;
7284 return;
400fbf9f
JW
7285 }
7286
3e4093b6
RS
7287 if (value && TREE_CODE (value) == CONSTRUCTOR)
7288 {
7289 constructor_constant = TREE_CONSTANT (value);
7290 constructor_simple = TREE_STATIC (value);
928c19bb 7291 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7292 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7293 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7294 && (TREE_CODE (constructor_type) == RECORD_TYPE
7295 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7296 set_nonincremental_init (braced_init_obstack);
3e4093b6 7297 }
400fbf9f 7298
3e4093b6
RS
7299 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
7300 {
7301 missing_braces_mentioned = 1;
683d6ff9 7302 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
3e4093b6 7303 }
400fbf9f 7304
3e4093b6
RS
7305 if (TREE_CODE (constructor_type) == RECORD_TYPE
7306 || TREE_CODE (constructor_type) == UNION_TYPE)
7307 {
7308 constructor_fields = TYPE_FIELDS (constructor_type);
7309 /* Skip any nameless bit fields at the beginning. */
7310 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7311 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7312 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7313
3e4093b6
RS
7314 constructor_unfilled_fields = constructor_fields;
7315 constructor_bit_index = bitsize_zero_node;
7316 }
7317 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7318 {
7319 /* Vectors are like simple fixed-size arrays. */
7320 constructor_max_index =
c62c040f
RG
7321 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7322 constructor_index = bitsize_int (0);
3e4093b6
RS
7323 constructor_unfilled_index = constructor_index;
7324 }
7325 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7326 {
7327 if (TYPE_DOMAIN (constructor_type))
7328 {
7329 constructor_max_index
7330 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7331
3e4093b6
RS
7332 /* Detect non-empty initializations of zero-length arrays. */
7333 if (constructor_max_index == NULL_TREE
7334 && TYPE_SIZE (constructor_type))
9a9d280e 7335 constructor_max_index = integer_minus_one_node;
de520661 7336
3e4093b6
RS
7337 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7338 to initialize VLAs will cause a proper error; avoid tree
7339 checking errors as well by setting a safe value. */
7340 if (constructor_max_index
7341 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7342 constructor_max_index = integer_minus_one_node;
b62acd60 7343
3e4093b6
RS
7344 constructor_index
7345 = convert (bitsizetype,
7346 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7347 }
7348 else
7349 constructor_index = bitsize_zero_node;
de520661 7350
3e4093b6
RS
7351 constructor_unfilled_index = constructor_index;
7352 if (value && TREE_CODE (value) == STRING_CST)
7353 {
7354 /* We need to split the char/wchar array into individual
7355 characters, so that we don't have to special case it
7356 everywhere. */
a1e3b3d9 7357 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7358 }
7359 }
7360 else
7361 {
b4519d39 7362 if (constructor_type != error_mark_node)
683d6ff9 7363 warning_init (0, "braces around scalar initializer");
3e4093b6
RS
7364 constructor_fields = constructor_type;
7365 constructor_unfilled_fields = constructor_type;
7366 }
7367}
8b6a5902 7368
3e4093b6 7369/* At the end of an implicit or explicit brace level,
916c5919
JM
7370 finish up that level of constructor. If a single expression
7371 with redundant braces initialized that level, return the
7372 c_expr structure for that expression. Otherwise, the original_code
7373 element is set to ERROR_MARK.
7374 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7375 from inner levels (process_init_element ignores that),
916c5919 7376 but return error_mark_node as the value from the outermost level
3e4093b6 7377 (that's what we want to put in DECL_INITIAL).
916c5919 7378 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7379
916c5919 7380struct c_expr
a1e3b3d9 7381pop_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
7382{
7383 struct constructor_stack *p;
916c5919
JM
7384 struct c_expr ret;
7385 ret.value = 0;
7386 ret.original_code = ERROR_MARK;
6866c6e8 7387 ret.original_type = NULL;
de520661 7388
3e4093b6
RS
7389 if (implicit == 0)
7390 {
7391 /* When we come to an explicit close brace,
7392 pop any inner levels that didn't have explicit braces. */
7393 while (constructor_stack->implicit)
34cf811f
MP
7394 process_init_element (input_location,
7395 pop_init_level (1, braced_init_obstack),
7396 true, braced_init_obstack);
366de0ce 7397 gcc_assert (!constructor_range_stack);
3e4093b6 7398 }
e5e809f4 7399
0066ef9c
RH
7400 /* Now output all pending elements. */
7401 constructor_incremental = 1;
a1e3b3d9 7402 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7403
3e4093b6 7404 p = constructor_stack;
e5e809f4 7405
3e4093b6
RS
7406 /* Error for initializing a flexible array member, or a zero-length
7407 array member in an inappropriate context. */
7408 if (constructor_type && constructor_fields
7409 && TREE_CODE (constructor_type) == ARRAY_TYPE
7410 && TYPE_DOMAIN (constructor_type)
3f75a254 7411 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7412 {
7413 /* Silently discard empty initializations. The parser will
7414 already have pedwarned for empty brackets. */
7415 if (integer_zerop (constructor_unfilled_index))
7416 constructor_type = NULL_TREE;
366de0ce 7417 else
3e4093b6 7418 {
366de0ce 7419 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7420
3e4093b6
RS
7421 if (constructor_depth > 2)
7422 error_init ("initialization of flexible array member in a nested context");
fcf73884 7423 else
c1771a20 7424 pedwarn_init (input_location, OPT_Wpedantic,
509c9d60 7425 "initialization of a flexible array member");
de520661 7426
3e4093b6
RS
7427 /* We have already issued an error message for the existence
7428 of a flexible array member not at the end of the structure.
535a42b1 7429 Discard the initializer so that we do not die later. */
910ad8de 7430 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7431 constructor_type = NULL_TREE;
7432 }
3e4093b6 7433 }
de520661 7434
3e4093b6 7435 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7436 if (warn_missing_field_initializers
3e4093b6
RS
7437 && constructor_type
7438 && TREE_CODE (constructor_type) == RECORD_TYPE
7439 && constructor_unfilled_fields)
7440 {
49819fef 7441 bool constructor_zeroinit =
9771b263
DN
7442 (vec_safe_length (constructor_elements) == 1
7443 && integer_zerop ((*constructor_elements)[0].value));
49819fef 7444
3e4093b6
RS
7445 /* Do not warn for flexible array members or zero-length arrays. */
7446 while (constructor_unfilled_fields
3f75a254 7447 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7448 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7449 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7450
49819fef
AM
7451 if (constructor_unfilled_fields
7452 /* Do not warn if this level of the initializer uses member
7453 designators; it is likely to be deliberate. */
7454 && !constructor_designated
7455 /* Do not warn about initializing with ` = {0}'. */
7456 && !constructor_zeroinit)
3e4093b6 7457 {
32397f22
MLI
7458 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7459 "missing initializer for field %qD of %qT",
7460 constructor_unfilled_fields,
7461 constructor_type))
7462 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7463 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7464 }
7465 }
de520661 7466
3e4093b6 7467 /* Pad out the end of the structure. */
916c5919 7468 if (p->replacement_value.value)
3e4093b6
RS
7469 /* If this closes a superfluous brace pair,
7470 just pass out the element between them. */
916c5919 7471 ret = p->replacement_value;
3e4093b6
RS
7472 else if (constructor_type == 0)
7473 ;
7474 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7475 && TREE_CODE (constructor_type) != UNION_TYPE
7476 && TREE_CODE (constructor_type) != ARRAY_TYPE
7477 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7478 {
7479 /* A nonincremental scalar initializer--just return
7480 the element, after verifying there is just one. */
9771b263 7481 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7482 {
7483 if (!constructor_erroneous)
7484 error_init ("empty scalar initializer");
916c5919 7485 ret.value = error_mark_node;
3e4093b6 7486 }
9771b263 7487 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6
RS
7488 {
7489 error_init ("extra elements in scalar initializer");
9771b263 7490 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7491 }
7492 else
9771b263 7493 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7494 }
7495 else
7496 {
7497 if (constructor_erroneous)
916c5919 7498 ret.value = error_mark_node;
3e4093b6
RS
7499 else
7500 {
916c5919 7501 ret.value = build_constructor (constructor_type,
4038c495 7502 constructor_elements);
3e4093b6 7503 if (constructor_constant)
51eed280 7504 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7505 if (constructor_constant && constructor_simple)
916c5919 7506 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7507 if (constructor_nonconst)
7508 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7509 }
7510 }
de520661 7511
928c19bb
JM
7512 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7513 {
7514 if (constructor_nonconst)
7515 ret.original_code = C_MAYBE_CONST_EXPR;
7516 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7517 ret.original_code = ERROR_MARK;
7518 }
7519
3e4093b6
RS
7520 constructor_type = p->type;
7521 constructor_fields = p->fields;
7522 constructor_index = p->index;
7523 constructor_max_index = p->max_index;
7524 constructor_unfilled_index = p->unfilled_index;
7525 constructor_unfilled_fields = p->unfilled_fields;
7526 constructor_bit_index = p->bit_index;
7527 constructor_elements = p->elements;
7528 constructor_constant = p->constant;
7529 constructor_simple = p->simple;
928c19bb 7530 constructor_nonconst = p->nonconst;
3e4093b6
RS
7531 constructor_erroneous = p->erroneous;
7532 constructor_incremental = p->incremental;
7533 constructor_designated = p->designated;
7534 constructor_pending_elts = p->pending_elts;
7535 constructor_depth = p->depth;
7536 if (!p->implicit)
7537 constructor_range_stack = p->range_stack;
7538 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7539
3e4093b6
RS
7540 constructor_stack = p->next;
7541 free (p);
b621a4dd 7542
5d5e98dc
VR
7543 if (ret.value == 0 && constructor_stack == 0)
7544 ret.value = error_mark_node;
916c5919 7545 return ret;
3e4093b6 7546}
8b6a5902 7547
3e4093b6
RS
7548/* Common handling for both array range and field name designators.
7549 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7550
3e4093b6 7551static int
a1e3b3d9 7552set_designator (int array, struct obstack * braced_init_obstack)
de520661 7553{
3e4093b6
RS
7554 tree subtype;
7555 enum tree_code subcode;
de520661 7556
3e4093b6
RS
7557 /* Don't die if an entire brace-pair level is superfluous
7558 in the containing level. */
7559 if (constructor_type == 0)
7560 return 1;
de520661 7561
366de0ce
NS
7562 /* If there were errors in this designator list already, bail out
7563 silently. */
b06df647 7564 if (designator_erroneous)
3e4093b6 7565 return 1;
e28cae4f 7566
3e4093b6
RS
7567 if (!designator_depth)
7568 {
366de0ce 7569 gcc_assert (!constructor_range_stack);
de520661 7570
3e4093b6
RS
7571 /* Designator list starts at the level of closest explicit
7572 braces. */
7573 while (constructor_stack->implicit)
34cf811f
MP
7574 process_init_element (input_location,
7575 pop_init_level (1, braced_init_obstack),
7576 true, braced_init_obstack);
3e4093b6
RS
7577 constructor_designated = 1;
7578 return 0;
7579 }
de520661 7580
366de0ce 7581 switch (TREE_CODE (constructor_type))
3c3fa147 7582 {
366de0ce
NS
7583 case RECORD_TYPE:
7584 case UNION_TYPE:
3e4093b6
RS
7585 subtype = TREE_TYPE (constructor_fields);
7586 if (subtype != error_mark_node)
7587 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7588 break;
7589 case ARRAY_TYPE:
3e4093b6 7590 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7591 break;
7592 default:
7593 gcc_unreachable ();
de520661 7594 }
400fbf9f 7595
3e4093b6
RS
7596 subcode = TREE_CODE (subtype);
7597 if (array && subcode != ARRAY_TYPE)
7598 {
7599 error_init ("array index in non-array initializer");
7600 return 1;
7601 }
7602 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7603 {
7604 error_init ("field name not in record or union initializer");
7605 return 1;
7606 }
d45cf215 7607
3e4093b6 7608 constructor_designated = 1;
a1e3b3d9 7609 push_init_level (2, braced_init_obstack);
3e4093b6 7610 return 0;
de520661 7611}
400fbf9f 7612
3e4093b6
RS
7613/* If there are range designators in designator list, push a new designator
7614 to constructor_range_stack. RANGE_END is end of such stack range or
7615 NULL_TREE if there is no range designator at this level. */
400fbf9f 7616
3e4093b6 7617static void
a1e3b3d9 7618push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7619{
7620 struct constructor_range_stack *p;
400fbf9f 7621
a1e3b3d9
LB
7622 p = (struct constructor_range_stack *)
7623 obstack_alloc (braced_init_obstack,
7624 sizeof (struct constructor_range_stack));
3e4093b6
RS
7625 p->prev = constructor_range_stack;
7626 p->next = 0;
7627 p->fields = constructor_fields;
7628 p->range_start = constructor_index;
7629 p->index = constructor_index;
7630 p->stack = constructor_stack;
7631 p->range_end = range_end;
8b6a5902 7632 if (constructor_range_stack)
3e4093b6
RS
7633 constructor_range_stack->next = p;
7634 constructor_range_stack = p;
de520661 7635}
400fbf9f 7636
3e4093b6
RS
7637/* Within an array initializer, specify the next index to be initialized.
7638 FIRST is that index. If LAST is nonzero, then initialize a range
7639 of indices, running from FIRST through LAST. */
5a7ec9d9 7640
de520661 7641void
a1e3b3d9
LB
7642set_init_index (tree first, tree last,
7643 struct obstack * braced_init_obstack)
de520661 7644{
a1e3b3d9 7645 if (set_designator (1, braced_init_obstack))
3e4093b6 7646 return;
de520661 7647
b06df647 7648 designator_erroneous = 1;
de520661 7649
3ea8cd06
JM
7650 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7651 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7652 {
7653 error_init ("array index in initializer not of integer type");
7654 return;
7655 }
7656
2b6da65c
JM
7657 if (TREE_CODE (first) != INTEGER_CST)
7658 {
7659 first = c_fully_fold (first, false, NULL);
7660 if (TREE_CODE (first) == INTEGER_CST)
c1771a20 7661 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7662 "array index in initializer is not "
7663 "an integer constant expression");
7664 }
7665
7666 if (last && TREE_CODE (last) != INTEGER_CST)
7667 {
7668 last = c_fully_fold (last, false, NULL);
7669 if (TREE_CODE (last) == INTEGER_CST)
c1771a20 7670 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7671 "array index in initializer is not "
7672 "an integer constant expression");
7673 }
7674
3e4093b6
RS
7675 if (TREE_CODE (first) != INTEGER_CST)
7676 error_init ("nonconstant array index in initializer");
7677 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7678 error_init ("nonconstant array index in initializer");
7679 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7680 error_init ("array index in non-array initializer");
622adc7e
MK
7681 else if (tree_int_cst_sgn (first) == -1)
7682 error_init ("array index in initializer exceeds array bounds");
3e4093b6
RS
7683 else if (constructor_max_index
7684 && tree_int_cst_lt (constructor_max_index, first))
7685 error_init ("array index in initializer exceeds array bounds");
7686 else
de520661 7687 {
928c19bb
JM
7688 constant_expression_warning (first);
7689 if (last)
7690 constant_expression_warning (last);
3e4093b6 7691 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7692 if (tree_int_cst_lt (constructor_index, first))
7693 {
7694 constructor_index = copy_node (constructor_index);
7695 TREE_OVERFLOW (constructor_index) = 1;
7696 }
665f2503 7697
3e4093b6 7698 if (last)
2bede729 7699 {
3e4093b6
RS
7700 if (tree_int_cst_equal (first, last))
7701 last = 0;
7702 else if (tree_int_cst_lt (last, first))
7703 {
7704 error_init ("empty index range in initializer");
7705 last = 0;
7706 }
7707 else
7708 {
7709 last = convert (bitsizetype, last);
7710 if (constructor_max_index != 0
7711 && tree_int_cst_lt (constructor_max_index, last))
7712 {
7713 error_init ("array index range in initializer exceeds array bounds");
7714 last = 0;
7715 }
7716 }
2bede729 7717 }
fed3cef0 7718
3e4093b6 7719 designator_depth++;
b06df647 7720 designator_erroneous = 0;
3e4093b6 7721 if (constructor_range_stack || last)
a1e3b3d9 7722 push_range_stack (last, braced_init_obstack);
de520661 7723 }
de520661 7724}
3e4093b6
RS
7725
7726/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7727
de520661 7728void
a1e3b3d9 7729set_init_label (tree fieldname, struct obstack * braced_init_obstack)
de520661 7730{
0fb96aa4 7731 tree field;
94ba5069 7732
a1e3b3d9 7733 if (set_designator (0, braced_init_obstack))
3e4093b6
RS
7734 return;
7735
b06df647 7736 designator_erroneous = 1;
3e4093b6
RS
7737
7738 if (TREE_CODE (constructor_type) != RECORD_TYPE
7739 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7740 {
3e4093b6
RS
7741 error_init ("field name not in record or union initializer");
7742 return;
94ba5069
RS
7743 }
7744
0fb96aa4 7745 field = lookup_field (constructor_type, fieldname);
8b6a5902 7746
0fb96aa4 7747 if (field == 0)
c51a1ba9 7748 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 7749 else
0fb96aa4
JM
7750 do
7751 {
7752 constructor_fields = TREE_VALUE (field);
7753 designator_depth++;
7754 designator_erroneous = 0;
7755 if (constructor_range_stack)
7756 push_range_stack (NULL_TREE, braced_init_obstack);
7757 field = TREE_CHAIN (field);
7758 if (field)
7759 {
7760 if (set_designator (0, braced_init_obstack))
7761 return;
7762 }
7763 }
7764 while (field != NULL_TREE);
3e4093b6
RS
7765}
7766\f
7767/* Add a new initializer to the tree of pending initializers. PURPOSE
7768 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7769 VALUE is the value of that index or field. If ORIGTYPE is not
7770 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7771
7772 IMPLICIT is true if value comes from pop_init_level (1),
7773 the new initializer has been merged with the existing one
7774 and thus no warnings should be emitted about overriding an
7775 existing initializer. */
de520661 7776
3e4093b6 7777static void
a1e3b3d9
LB
7778add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7779 struct obstack * braced_init_obstack)
3e4093b6
RS
7780{
7781 struct init_node *p, **q, *r;
7782
7783 q = &constructor_pending_elts;
7784 p = 0;
7785
7786 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7787 {
3e4093b6 7788 while (*q != 0)
91fa3c30 7789 {
3e4093b6
RS
7790 p = *q;
7791 if (tree_int_cst_lt (purpose, p->purpose))
7792 q = &p->left;
7793 else if (tree_int_cst_lt (p->purpose, purpose))
7794 q = &p->right;
7795 else
7796 {
b295aee2
JJ
7797 if (!implicit)
7798 {
7799 if (TREE_SIDE_EFFECTS (p->value))
7800 warning_init (0, "initialized field with side-effects overwritten");
7801 else if (warn_override_init)
7802 warning_init (OPT_Woverride_init, "initialized field overwritten");
7803 }
3e4093b6 7804 p->value = value;
bbbbb16a 7805 p->origtype = origtype;
3e4093b6
RS
7806 return;
7807 }
91fa3c30 7808 }
de520661 7809 }
3e4093b6 7810 else
de520661 7811 {
3e4093b6 7812 tree bitpos;
400fbf9f 7813
3e4093b6
RS
7814 bitpos = bit_position (purpose);
7815 while (*q != NULL)
7816 {
7817 p = *q;
7818 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7819 q = &p->left;
7820 else if (p->purpose != purpose)
7821 q = &p->right;
7822 else
7823 {
b295aee2
JJ
7824 if (!implicit)
7825 {
7826 if (TREE_SIDE_EFFECTS (p->value))
7827 warning_init (0, "initialized field with side-effects overwritten");
7828 else if (warn_override_init)
7829 warning_init (OPT_Woverride_init, "initialized field overwritten");
7830 }
3e4093b6 7831 p->value = value;
bbbbb16a 7832 p->origtype = origtype;
3e4093b6
RS
7833 return;
7834 }
7835 }
91fa3c30 7836 }
b71c7f8a 7837
a1e3b3d9
LB
7838 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7839 sizeof (struct init_node));
3e4093b6
RS
7840 r->purpose = purpose;
7841 r->value = value;
bbbbb16a 7842 r->origtype = origtype;
8b6a5902 7843
3e4093b6
RS
7844 *q = r;
7845 r->parent = p;
7846 r->left = 0;
7847 r->right = 0;
7848 r->balance = 0;
b71c7f8a 7849
3e4093b6 7850 while (p)
de520661 7851 {
3e4093b6 7852 struct init_node *s;
665f2503 7853
3e4093b6 7854 if (r == p->left)
2bede729 7855 {
3e4093b6
RS
7856 if (p->balance == 0)
7857 p->balance = -1;
7858 else if (p->balance < 0)
7859 {
7860 if (r->balance < 0)
7861 {
7862 /* L rotation. */
7863 p->left = r->right;
7864 if (p->left)
7865 p->left->parent = p;
7866 r->right = p;
e7b6a0ee 7867
3e4093b6
RS
7868 p->balance = 0;
7869 r->balance = 0;
39bc99c2 7870
3e4093b6
RS
7871 s = p->parent;
7872 p->parent = r;
7873 r->parent = s;
7874 if (s)
7875 {
7876 if (s->left == p)
7877 s->left = r;
7878 else
7879 s->right = r;
7880 }
7881 else
7882 constructor_pending_elts = r;
7883 }
7884 else
7885 {
7886 /* LR rotation. */
7887 struct init_node *t = r->right;
e7b6a0ee 7888
3e4093b6
RS
7889 r->right = t->left;
7890 if (r->right)
7891 r->right->parent = r;
7892 t->left = r;
7893
7894 p->left = t->right;
7895 if (p->left)
7896 p->left->parent = p;
7897 t->right = p;
7898
7899 p->balance = t->balance < 0;
7900 r->balance = -(t->balance > 0);
7901 t->balance = 0;
7902
7903 s = p->parent;
7904 p->parent = t;
7905 r->parent = t;
7906 t->parent = s;
7907 if (s)
7908 {
7909 if (s->left == p)
7910 s->left = t;
7911 else
7912 s->right = t;
7913 }
7914 else
7915 constructor_pending_elts = t;
7916 }
7917 break;
7918 }
7919 else
7920 {
7921 /* p->balance == +1; growth of left side balances the node. */
7922 p->balance = 0;
7923 break;
7924 }
2bede729 7925 }
3e4093b6
RS
7926 else /* r == p->right */
7927 {
7928 if (p->balance == 0)
7929 /* Growth propagation from right side. */
7930 p->balance++;
7931 else if (p->balance > 0)
7932 {
7933 if (r->balance > 0)
7934 {
7935 /* R rotation. */
7936 p->right = r->left;
7937 if (p->right)
7938 p->right->parent = p;
7939 r->left = p;
7940
7941 p->balance = 0;
7942 r->balance = 0;
7943
7944 s = p->parent;
7945 p->parent = r;
7946 r->parent = s;
7947 if (s)
7948 {
7949 if (s->left == p)
7950 s->left = r;
7951 else
7952 s->right = r;
7953 }
7954 else
7955 constructor_pending_elts = r;
7956 }
7957 else /* r->balance == -1 */
7958 {
7959 /* RL rotation */
7960 struct init_node *t = r->left;
7961
7962 r->left = t->right;
7963 if (r->left)
7964 r->left->parent = r;
7965 t->right = r;
7966
7967 p->right = t->left;
7968 if (p->right)
7969 p->right->parent = p;
7970 t->left = p;
7971
7972 r->balance = (t->balance < 0);
7973 p->balance = -(t->balance > 0);
7974 t->balance = 0;
7975
7976 s = p->parent;
7977 p->parent = t;
7978 r->parent = t;
7979 t->parent = s;
7980 if (s)
7981 {
7982 if (s->left == p)
7983 s->left = t;
7984 else
7985 s->right = t;
7986 }
7987 else
7988 constructor_pending_elts = t;
7989 }
7990 break;
7991 }
7992 else
7993 {
7994 /* p->balance == -1; growth of right side balances the node. */
7995 p->balance = 0;
7996 break;
7997 }
7998 }
7999
8000 r = p;
8001 p = p->parent;
8002 }
8003}
8004
8005/* Build AVL tree from a sorted chain. */
8006
8007static void
a1e3b3d9 8008set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8009{
4038c495
GB
8010 unsigned HOST_WIDE_INT ix;
8011 tree index, value;
3e4093b6
RS
8012
8013 if (TREE_CODE (constructor_type) != RECORD_TYPE
8014 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8015 return;
8016
4038c495 8017 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
a1e3b3d9 8018 {
fdce1719 8019 add_pending_init (index, value, NULL_TREE, true,
a1e3b3d9
LB
8020 braced_init_obstack);
8021 }
9771b263 8022 constructor_elements = NULL;
3e4093b6
RS
8023 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8024 {
8025 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8026 /* Skip any nameless bit fields at the beginning. */
8027 while (constructor_unfilled_fields != 0
8028 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8029 && DECL_NAME (constructor_unfilled_fields) == 0)
8030 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8031
de520661 8032 }
3e4093b6 8033 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8034 {
3e4093b6
RS
8035 if (TYPE_DOMAIN (constructor_type))
8036 constructor_unfilled_index
8037 = convert (bitsizetype,
8038 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8039 else
8040 constructor_unfilled_index = bitsize_zero_node;
de520661 8041 }
3e4093b6 8042 constructor_incremental = 0;
de520661 8043}
400fbf9f 8044
3e4093b6 8045/* Build AVL tree from a string constant. */
de520661 8046
3e4093b6 8047static void
a1e3b3d9
LB
8048set_nonincremental_init_from_string (tree str,
8049 struct obstack * braced_init_obstack)
de520661 8050{
3e4093b6
RS
8051 tree value, purpose, type;
8052 HOST_WIDE_INT val[2];
8053 const char *p, *end;
8054 int byte, wchar_bytes, charwidth, bitpos;
de520661 8055
366de0ce 8056 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8057
c466b2cd 8058 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8059 charwidth = TYPE_PRECISION (char_type_node);
8060 type = TREE_TYPE (constructor_type);
8061 p = TREE_STRING_POINTER (str);
8062 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8063
3e4093b6 8064 for (purpose = bitsize_zero_node;
8824edff
JJ
8065 p < end
8066 && !(constructor_max_index
8067 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8068 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8069 {
3e4093b6 8070 if (wchar_bytes == 1)
ffc5c6a9 8071 {
3e4093b6
RS
8072 val[1] = (unsigned char) *p++;
8073 val[0] = 0;
ffc5c6a9
RH
8074 }
8075 else
3e4093b6
RS
8076 {
8077 val[0] = 0;
8078 val[1] = 0;
8079 for (byte = 0; byte < wchar_bytes; byte++)
8080 {
8081 if (BYTES_BIG_ENDIAN)
8082 bitpos = (wchar_bytes - byte - 1) * charwidth;
8083 else
8084 bitpos = byte * charwidth;
8085 val[bitpos < HOST_BITS_PER_WIDE_INT]
8086 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8087 << (bitpos % HOST_BITS_PER_WIDE_INT);
8088 }
8089 }
584ef5fe 8090
8df83eae 8091 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8092 {
8093 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8094 if (bitpos < HOST_BITS_PER_WIDE_INT)
8095 {
8096 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8097 {
8098 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
8099 val[0] = -1;
8100 }
8101 }
8102 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8103 {
8104 if (val[1] < 0)
c22cacf3 8105 val[0] = -1;
3e4093b6
RS
8106 }
8107 else if (val[0] & (((HOST_WIDE_INT) 1)
8108 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8109 val[0] |= ((HOST_WIDE_INT) -1)
8110 << (bitpos - HOST_BITS_PER_WIDE_INT);
8111 }
ffc5c6a9 8112
7d60be94 8113 value = build_int_cst_wide (type, val[1], val[0]);
fdce1719 8114 add_pending_init (purpose, value, NULL_TREE, true,
a1e3b3d9 8115 braced_init_obstack);
9dfcc8db
BH
8116 }
8117
3e4093b6
RS
8118 constructor_incremental = 0;
8119}
de520661 8120
3e4093b6
RS
8121/* Return value of FIELD in pending initializer or zero if the field was
8122 not initialized yet. */
8123
8124static tree
a1e3b3d9 8125find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8126{
8127 struct init_node *p;
8128
8129 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8130 {
3e4093b6
RS
8131 if (constructor_incremental
8132 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8133 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8134
8135 p = constructor_pending_elts;
8136 while (p)
19d76e60 8137 {
3e4093b6
RS
8138 if (tree_int_cst_lt (field, p->purpose))
8139 p = p->left;
8140 else if (tree_int_cst_lt (p->purpose, field))
8141 p = p->right;
8142 else
8143 return p->value;
19d76e60 8144 }
19d76e60 8145 }
3e4093b6 8146 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8147 {
3e4093b6 8148 tree bitpos = bit_position (field);
de520661 8149
3e4093b6
RS
8150 if (constructor_incremental
8151 && (!constructor_unfilled_fields
8152 || tree_int_cst_lt (bitpos,
8153 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8154 set_nonincremental_init (braced_init_obstack);
de520661 8155
3e4093b6
RS
8156 p = constructor_pending_elts;
8157 while (p)
8158 {
8159 if (field == p->purpose)
8160 return p->value;
8161 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8162 p = p->left;
8163 else
8164 p = p->right;
8165 }
8166 }
8167 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8168 {
9771b263
DN
8169 if (!vec_safe_is_empty (constructor_elements)
8170 && (constructor_elements->last ().index == field))
8171 return constructor_elements->last ().value;
de520661 8172 }
3e4093b6 8173 return 0;
de520661
RS
8174}
8175
3e4093b6
RS
8176/* "Output" the next constructor element.
8177 At top level, really output it to assembler code now.
8178 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8179 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8180 TYPE is the data type that the containing data type wants here.
8181 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8182 If VALUE is a string constant, STRICT_STRING is true if it is
8183 unparenthesized or we should not warn here for it being parenthesized.
8184 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8185
3e4093b6
RS
8186 PENDING if non-nil means output pending elements that belong
8187 right after this element. (PENDING is normally 1;
b295aee2
JJ
8188 it is 0 while outputting pending elements, to avoid recursion.)
8189
8190 IMPLICIT is true if value comes from pop_init_level (1),
8191 the new initializer has been merged with the existing one
8192 and thus no warnings should be emitted about overriding an
8193 existing initializer. */
8b6a5902 8194
3e4093b6 8195static void
34cf811f
MP
8196output_init_element (location_t loc, tree value, tree origtype,
8197 bool strict_string, tree type, tree field, int pending,
8198 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8199{
8ce94e44 8200 tree semantic_type = NULL_TREE;
928c19bb
JM
8201 bool maybe_const = true;
8202 bool npc;
4038c495 8203
0a880880 8204 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8205 {
3e4093b6
RS
8206 constructor_erroneous = 1;
8207 return;
8b6a5902 8208 }
46bdb9cf
JM
8209 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8210 && (TREE_CODE (value) == STRING_CST
8211 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8212 && !(TREE_CODE (value) == STRING_CST
8213 && TREE_CODE (type) == ARRAY_TYPE
8214 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8215 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8216 TYPE_MAIN_VARIANT (type)))
c2255bc4 8217 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8218
3e4093b6
RS
8219 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8220 && require_constant_value && !flag_isoc99 && pending)
8b6a5902 8221 {
3e4093b6
RS
8222 /* As an extension, allow initializing objects with static storage
8223 duration with compound literals (which are then treated just as
8224 the brace enclosed list they contain). */
8225 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8226 value = DECL_INITIAL (decl);
8b6a5902
JJ
8227 }
8228
928c19bb 8229 npc = null_pointer_constant_p (value);
8ce94e44
JM
8230 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8231 {
8232 semantic_type = TREE_TYPE (value);
8233 value = TREE_OPERAND (value, 0);
8234 }
928c19bb
JM
8235 value = c_fully_fold (value, require_constant_value, &maybe_const);
8236
3e4093b6
RS
8237 if (value == error_mark_node)
8238 constructor_erroneous = 1;
8239 else if (!TREE_CONSTANT (value))
8240 constructor_constant = 0;
116df786 8241 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8242 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8243 || TREE_CODE (constructor_type) == UNION_TYPE)
8244 && DECL_C_BIT_FIELD (field)
8245 && TREE_CODE (value) != INTEGER_CST))
8246 constructor_simple = 0;
928c19bb
JM
8247 if (!maybe_const)
8248 constructor_nonconst = 1;
3e4093b6 8249
116df786 8250 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8251 {
116df786
RH
8252 if (require_constant_value)
8253 {
8254 error_init ("initializer element is not constant");
8255 value = error_mark_node;
8256 }
8257 else if (require_constant_elements)
509c9d60
MLI
8258 pedwarn (input_location, 0,
8259 "initializer element is not computable at load time");
8b6a5902 8260 }
928c19bb
JM
8261 else if (!maybe_const
8262 && (require_constant_value || require_constant_elements))
8263 pedwarn_init (input_location, 0,
8264 "initializer element is not a constant expression");
3e4093b6 8265
81f40b79
ILT
8266 /* Issue -Wc++-compat warnings about initializing a bitfield with
8267 enum type. */
8268 if (warn_cxx_compat
8269 && field != NULL_TREE
8270 && TREE_CODE (field) == FIELD_DECL
8271 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8272 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8273 != TYPE_MAIN_VARIANT (type))
8274 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8275 {
8276 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8277 if (checktype != error_mark_node
8278 && (TYPE_MAIN_VARIANT (checktype)
8279 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8280 warning_init (OPT_Wc___compat,
8281 "enum conversion in initialization is invalid in C++");
8282 }
8283
3e4093b6
RS
8284 /* If this field is empty (and not at the end of structure),
8285 don't do anything other than checking the initializer. */
8286 if (field
8287 && (TREE_TYPE (field) == error_mark_node
8288 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8289 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8290 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8291 || DECL_CHAIN (field)))))
3e4093b6
RS
8292 return;
8293
8ce94e44
JM
8294 if (semantic_type)
8295 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8296 value = digest_init (loc, type, value, origtype, npc, strict_string,
8297 require_constant_value);
3e4093b6 8298 if (value == error_mark_node)
8b6a5902 8299 {
3e4093b6
RS
8300 constructor_erroneous = 1;
8301 return;
8b6a5902 8302 }
928c19bb
JM
8303 if (require_constant_value || require_constant_elements)
8304 constant_expression_warning (value);
8b6a5902 8305
3e4093b6
RS
8306 /* If this element doesn't come next in sequence,
8307 put it on constructor_pending_elts. */
8308 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8309 && (!constructor_incremental
8310 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8311 {
3e4093b6
RS
8312 if (constructor_incremental
8313 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8314 set_nonincremental_init (braced_init_obstack);
3e4093b6 8315
a1e3b3d9
LB
8316 add_pending_init (field, value, origtype, implicit,
8317 braced_init_obstack);
3e4093b6 8318 return;
8b6a5902 8319 }
3e4093b6
RS
8320 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8321 && (!constructor_incremental
8322 || field != constructor_unfilled_fields))
8b6a5902 8323 {
3e4093b6
RS
8324 /* We do this for records but not for unions. In a union,
8325 no matter which field is specified, it can be initialized
8326 right away since it starts at the beginning of the union. */
8327 if (constructor_incremental)
8328 {
8329 if (!constructor_unfilled_fields)
a1e3b3d9 8330 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8331 else
8332 {
8333 tree bitpos, unfillpos;
8334
8335 bitpos = bit_position (field);
8336 unfillpos = bit_position (constructor_unfilled_fields);
8337
8338 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8339 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8340 }
8341 }
8342
a1e3b3d9
LB
8343 add_pending_init (field, value, origtype, implicit,
8344 braced_init_obstack);
3e4093b6 8345 return;
8b6a5902 8346 }
3e4093b6 8347 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8348 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8349 {
b295aee2
JJ
8350 if (!implicit)
8351 {
9771b263 8352 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
b295aee2
JJ
8353 warning_init (0,
8354 "initialized field with side-effects overwritten");
8355 else if (warn_override_init)
8356 warning_init (OPT_Woverride_init, "initialized field overwritten");
8357 }
8b6a5902 8358
3e4093b6 8359 /* We can have just one union field set. */
9771b263 8360 constructor_elements = NULL;
3e4093b6 8361 }
8b6a5902 8362
3e4093b6
RS
8363 /* Otherwise, output this element either to
8364 constructor_elements or to the assembler file. */
8b6a5902 8365
f32682ca 8366 constructor_elt celt = {field, value};
9771b263 8367 vec_safe_push (constructor_elements, celt);
8b6a5902 8368
3e4093b6
RS
8369 /* Advance the variable that indicates sequential elements output. */
8370 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8371 constructor_unfilled_index
db3927fb
AH
8372 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8373 bitsize_one_node);
3e4093b6
RS
8374 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8375 {
8376 constructor_unfilled_fields
910ad8de 8377 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8378
3e4093b6
RS
8379 /* Skip any nameless bit fields. */
8380 while (constructor_unfilled_fields != 0
8381 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8382 && DECL_NAME (constructor_unfilled_fields) == 0)
8383 constructor_unfilled_fields =
910ad8de 8384 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8385 }
8386 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8387 constructor_unfilled_fields = 0;
de520661 8388
3e4093b6
RS
8389 /* Now output any pending elements which have become next. */
8390 if (pending)
a1e3b3d9 8391 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8392}
8b6a5902 8393
3e4093b6
RS
8394/* Output any pending elements which have become next.
8395 As we output elements, constructor_unfilled_{fields,index}
8396 advances, which may cause other elements to become next;
8397 if so, they too are output.
8b6a5902 8398
3e4093b6
RS
8399 If ALL is 0, we return when there are
8400 no more pending elements to output now.
665f2503 8401
3e4093b6
RS
8402 If ALL is 1, we output space as necessary so that
8403 we can output all the pending elements. */
3e4093b6 8404static void
a1e3b3d9 8405output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8406{
8407 struct init_node *elt = constructor_pending_elts;
8408 tree next;
de520661 8409
3e4093b6
RS
8410 retry:
8411
ba228239 8412 /* Look through the whole pending tree.
3e4093b6
RS
8413 If we find an element that should be output now,
8414 output it. Otherwise, set NEXT to the element
8415 that comes first among those still pending. */
8416
8417 next = 0;
8418 while (elt)
8419 {
8420 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8421 {
3e4093b6
RS
8422 if (tree_int_cst_equal (elt->purpose,
8423 constructor_unfilled_index))
34cf811f
MP
8424 output_init_element (input_location, elt->value, elt->origtype,
8425 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8426 constructor_unfilled_index, 0, false,
8427 braced_init_obstack);
3e4093b6
RS
8428 else if (tree_int_cst_lt (constructor_unfilled_index,
8429 elt->purpose))
8b6a5902 8430 {
3e4093b6
RS
8431 /* Advance to the next smaller node. */
8432 if (elt->left)
8433 elt = elt->left;
8434 else
8435 {
8436 /* We have reached the smallest node bigger than the
8437 current unfilled index. Fill the space first. */
8438 next = elt->purpose;
8439 break;
8440 }
8b6a5902 8441 }
ce662d4c
JJ
8442 else
8443 {
3e4093b6
RS
8444 /* Advance to the next bigger node. */
8445 if (elt->right)
8446 elt = elt->right;
8447 else
ce662d4c 8448 {
3e4093b6
RS
8449 /* We have reached the biggest node in a subtree. Find
8450 the parent of it, which is the next bigger node. */
8451 while (elt->parent && elt->parent->right == elt)
8452 elt = elt->parent;
8453 elt = elt->parent;
8454 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8455 elt->purpose))
8456 {
8457 next = elt->purpose;
8458 break;
8459 }
ce662d4c
JJ
8460 }
8461 }
8b6a5902 8462 }
3e4093b6
RS
8463 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8464 || TREE_CODE (constructor_type) == UNION_TYPE)
8465 {
8466 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8467
3e4093b6
RS
8468 /* If the current record is complete we are done. */
8469 if (constructor_unfilled_fields == 0)
8470 break;
de520661 8471
3e4093b6
RS
8472 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8473 elt_bitpos = bit_position (elt->purpose);
8474 /* We can't compare fields here because there might be empty
8475 fields in between. */
8476 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8477 {
8478 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8479 output_init_element (input_location, elt->value, elt->origtype,
8480 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8481 elt->purpose, 0, false,
8482 braced_init_obstack);
3e4093b6
RS
8483 }
8484 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8485 {
8486 /* Advance to the next smaller node. */
8487 if (elt->left)
8488 elt = elt->left;
8489 else
8490 {
8491 /* We have reached the smallest node bigger than the
8492 current unfilled field. Fill the space first. */
8493 next = elt->purpose;
8494 break;
8495 }
8496 }
8497 else
8498 {
8499 /* Advance to the next bigger node. */
8500 if (elt->right)
8501 elt = elt->right;
8502 else
8503 {
8504 /* We have reached the biggest node in a subtree. Find
8505 the parent of it, which is the next bigger node. */
8506 while (elt->parent && elt->parent->right == elt)
8507 elt = elt->parent;
8508 elt = elt->parent;
8509 if (elt
8510 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8511 bit_position (elt->purpose))))
8512 {
8513 next = elt->purpose;
8514 break;
8515 }
8516 }
8517 }
8518 }
8519 }
de520661 8520
3e4093b6
RS
8521 /* Ordinarily return, but not if we want to output all
8522 and there are elements left. */
3f75a254 8523 if (!(all && next != 0))
e5cfb88f
RK
8524 return;
8525
3e4093b6
RS
8526 /* If it's not incremental, just skip over the gap, so that after
8527 jumping to retry we will output the next successive element. */
8528 if (TREE_CODE (constructor_type) == RECORD_TYPE
8529 || TREE_CODE (constructor_type) == UNION_TYPE)
8530 constructor_unfilled_fields = next;
8531 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8532 constructor_unfilled_index = next;
de520661 8533
3e4093b6
RS
8534 /* ELT now points to the node in the pending tree with the next
8535 initializer to output. */
8536 goto retry;
de520661
RS
8537}
8538\f
3e4093b6
RS
8539/* Add one non-braced element to the current constructor level.
8540 This adjusts the current position within the constructor's type.
8541 This may also start or terminate implicit levels
8542 to handle a partly-braced initializer.
e5e809f4 8543
3e4093b6 8544 Once this has found the correct level for the new element,
b295aee2
JJ
8545 it calls output_init_element.
8546
8547 IMPLICIT is true if value comes from pop_init_level (1),
8548 the new initializer has been merged with the existing one
8549 and thus no warnings should be emitted about overriding an
8550 existing initializer. */
3e4093b6
RS
8551
8552void
34cf811f 8553process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8554 struct obstack * braced_init_obstack)
e5e809f4 8555{
916c5919
JM
8556 tree orig_value = value.value;
8557 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8558 bool strict_string = value.original_code == STRING_CST;
340baef7 8559 bool was_designated = designator_depth != 0;
e5e809f4 8560
3e4093b6 8561 designator_depth = 0;
b06df647 8562 designator_erroneous = 0;
e5e809f4 8563
3e4093b6
RS
8564 /* Handle superfluous braces around string cst as in
8565 char x[] = {"foo"}; */
8566 if (string_flag
8567 && constructor_type
340baef7 8568 && !was_designated
3e4093b6 8569 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8570 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8571 && integer_zerop (constructor_unfilled_index))
e5e809f4 8572 {
916c5919 8573 if (constructor_stack->replacement_value.value)
c22cacf3 8574 error_init ("excess elements in char array initializer");
3e4093b6
RS
8575 constructor_stack->replacement_value = value;
8576 return;
e5e809f4 8577 }
8b6a5902 8578
916c5919 8579 if (constructor_stack->replacement_value.value != 0)
3e4093b6
RS
8580 {
8581 error_init ("excess elements in struct initializer");
8582 return;
e5e809f4
JL
8583 }
8584
3e4093b6
RS
8585 /* Ignore elements of a brace group if it is entirely superfluous
8586 and has already been diagnosed. */
8587 if (constructor_type == 0)
8588 return;
e5e809f4 8589
3e4093b6
RS
8590 /* If we've exhausted any levels that didn't have braces,
8591 pop them now. */
8592 while (constructor_stack->implicit)
8593 {
8594 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8595 || TREE_CODE (constructor_type) == UNION_TYPE)
8596 && constructor_fields == 0)
34cf811f 8597 process_init_element (loc, pop_init_level (1, braced_init_obstack),
a1e3b3d9 8598 true, braced_init_obstack);
53650abe
AP
8599 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8600 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8601 && constructor_max_index
8602 && tree_int_cst_lt (constructor_max_index,
8603 constructor_index))
34cf811f 8604 process_init_element (loc, pop_init_level (1, braced_init_obstack),
a1e3b3d9 8605 true, braced_init_obstack);
3e4093b6
RS
8606 else
8607 break;
8608 }
e5e809f4 8609
3e4093b6
RS
8610 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8611 if (constructor_range_stack)
e5e809f4 8612 {
3e4093b6
RS
8613 /* If value is a compound literal and we'll be just using its
8614 content, don't put it into a SAVE_EXPR. */
916c5919 8615 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
8616 || !require_constant_value
8617 || flag_isoc99)
8ce94e44
JM
8618 {
8619 tree semantic_type = NULL_TREE;
8620 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8621 {
8622 semantic_type = TREE_TYPE (value.value);
8623 value.value = TREE_OPERAND (value.value, 0);
8624 }
8625 value.value = c_save_expr (value.value);
8626 if (semantic_type)
8627 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8628 value.value);
8629 }
3e4093b6 8630 }
e5e809f4 8631
3e4093b6
RS
8632 while (1)
8633 {
8634 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8635 {
3e4093b6
RS
8636 tree fieldtype;
8637 enum tree_code fieldcode;
e5e809f4 8638
3e4093b6
RS
8639 if (constructor_fields == 0)
8640 {
509c9d60
MLI
8641 pedwarn_init (input_location, 0,
8642 "excess elements in struct initializer");
3e4093b6
RS
8643 break;
8644 }
e5e809f4 8645
3e4093b6
RS
8646 fieldtype = TREE_TYPE (constructor_fields);
8647 if (fieldtype != error_mark_node)
8648 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8649 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8650
3e4093b6
RS
8651 /* Error for non-static initialization of a flexible array member. */
8652 if (fieldcode == ARRAY_TYPE
8653 && !require_constant_value
8654 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8655 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6
RS
8656 {
8657 error_init ("non-static initialization of a flexible array member");
8658 break;
8659 }
e5e809f4 8660
3e4093b6 8661 /* Accept a string constant to initialize a subarray. */
916c5919 8662 if (value.value != 0
3e4093b6 8663 && fieldcode == ARRAY_TYPE
197463ae 8664 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8665 && string_flag)
916c5919 8666 value.value = orig_value;
3e4093b6
RS
8667 /* Otherwise, if we have come to a subaggregate,
8668 and we don't have an element of its type, push into it. */
0953878d 8669 else if (value.value != 0
916c5919
JM
8670 && value.value != error_mark_node
8671 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8672 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8673 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8674 {
a1e3b3d9 8675 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8676 continue;
8677 }
e5e809f4 8678
916c5919 8679 if (value.value)
3e4093b6
RS
8680 {
8681 push_member_name (constructor_fields);
34cf811f 8682 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8683 strict_string, fieldtype,
a1e3b3d9
LB
8684 constructor_fields, 1, implicit,
8685 braced_init_obstack);
3e4093b6 8686 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8687 }
8688 else
3e4093b6
RS
8689 /* Do the bookkeeping for an element that was
8690 directly output as a constructor. */
e5e809f4 8691 {
3e4093b6
RS
8692 /* For a record, keep track of end position of last field. */
8693 if (DECL_SIZE (constructor_fields))
c22cacf3 8694 constructor_bit_index
db3927fb
AH
8695 = size_binop_loc (input_location, PLUS_EXPR,
8696 bit_position (constructor_fields),
8697 DECL_SIZE (constructor_fields));
3e4093b6
RS
8698
8699 /* If the current field was the first one not yet written out,
8700 it isn't now, so update. */
8701 if (constructor_unfilled_fields == constructor_fields)
8702 {
910ad8de 8703 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8704 /* Skip any nameless bit fields. */
8705 while (constructor_unfilled_fields != 0
8706 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8707 && DECL_NAME (constructor_unfilled_fields) == 0)
8708 constructor_unfilled_fields =
910ad8de 8709 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8710 }
e5e809f4 8711 }
3e4093b6 8712
910ad8de 8713 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8714 /* Skip any nameless bit fields at the beginning. */
8715 while (constructor_fields != 0
8716 && DECL_C_BIT_FIELD (constructor_fields)
8717 && DECL_NAME (constructor_fields) == 0)
910ad8de 8718 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8719 }
3e4093b6 8720 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8721 {
3e4093b6
RS
8722 tree fieldtype;
8723 enum tree_code fieldcode;
e5e809f4 8724
3e4093b6
RS
8725 if (constructor_fields == 0)
8726 {
509c9d60
MLI
8727 pedwarn_init (input_location, 0,
8728 "excess elements in union initializer");
3e4093b6
RS
8729 break;
8730 }
e5e809f4 8731
3e4093b6
RS
8732 fieldtype = TREE_TYPE (constructor_fields);
8733 if (fieldtype != error_mark_node)
8734 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8735 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8736
3e4093b6
RS
8737 /* Warn that traditional C rejects initialization of unions.
8738 We skip the warning if the value is zero. This is done
8739 under the assumption that the zero initializer in user
8740 code appears conditioned on e.g. __STDC__ to avoid
8741 "missing initializer" warnings and relies on default
8742 initialization to zero in the traditional C case.
8743 We also skip the warning if the initializer is designated,
8744 again on the assumption that this must be conditional on
8745 __STDC__ anyway (and we've already complained about the
8746 member-designator already). */
8400e75e 8747 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8748 && !(value.value && (integer_zerop (value.value)
8749 || real_zerop (value.value))))
3176a0c2
DD
8750 warning (OPT_Wtraditional, "traditional C rejects initialization "
8751 "of unions");
e5e809f4 8752
3e4093b6 8753 /* Accept a string constant to initialize a subarray. */
916c5919 8754 if (value.value != 0
3e4093b6 8755 && fieldcode == ARRAY_TYPE
197463ae 8756 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8757 && string_flag)
916c5919 8758 value.value = orig_value;
3e4093b6
RS
8759 /* Otherwise, if we have come to a subaggregate,
8760 and we don't have an element of its type, push into it. */
0953878d 8761 else if (value.value != 0
916c5919
JM
8762 && value.value != error_mark_node
8763 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8764 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8765 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8766 {
a1e3b3d9 8767 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8768 continue;
8769 }
e5e809f4 8770
916c5919 8771 if (value.value)
3e4093b6
RS
8772 {
8773 push_member_name (constructor_fields);
34cf811f 8774 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8775 strict_string, fieldtype,
a1e3b3d9
LB
8776 constructor_fields, 1, implicit,
8777 braced_init_obstack);
3e4093b6 8778 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8779 }
8780 else
3e4093b6
RS
8781 /* Do the bookkeeping for an element that was
8782 directly output as a constructor. */
e5e809f4 8783 {
3e4093b6 8784 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8785 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8786 }
e5e809f4 8787
3e4093b6
RS
8788 constructor_fields = 0;
8789 }
8790 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8791 {
8792 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8793 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8794
3e4093b6 8795 /* Accept a string constant to initialize a subarray. */
916c5919 8796 if (value.value != 0
3e4093b6 8797 && eltcode == ARRAY_TYPE
197463ae 8798 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8799 && string_flag)
916c5919 8800 value.value = orig_value;
3e4093b6
RS
8801 /* Otherwise, if we have come to a subaggregate,
8802 and we don't have an element of its type, push into it. */
0953878d 8803 else if (value.value != 0
916c5919
JM
8804 && value.value != error_mark_node
8805 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 8806 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 8807 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 8808 {
a1e3b3d9 8809 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8810 continue;
8811 }
8b6a5902 8812
3e4093b6
RS
8813 if (constructor_max_index != 0
8814 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8815 || integer_all_onesp (constructor_max_index)))
8816 {
509c9d60
MLI
8817 pedwarn_init (input_location, 0,
8818 "excess elements in array initializer");
3e4093b6
RS
8819 break;
8820 }
8b6a5902 8821
3e4093b6 8822 /* Now output the actual element. */
916c5919 8823 if (value.value)
3e4093b6 8824 {
ae7e9ddd 8825 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 8826 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8827 strict_string, elttype,
a1e3b3d9
LB
8828 constructor_index, 1, implicit,
8829 braced_init_obstack);
3e4093b6
RS
8830 RESTORE_SPELLING_DEPTH (constructor_depth);
8831 }
2f6e4e97 8832
3e4093b6 8833 constructor_index
db3927fb
AH
8834 = size_binop_loc (input_location, PLUS_EXPR,
8835 constructor_index, bitsize_one_node);
8b6a5902 8836
916c5919 8837 if (!value.value)
3e4093b6
RS
8838 /* If we are doing the bookkeeping for an element that was
8839 directly output as a constructor, we must update
8840 constructor_unfilled_index. */
8841 constructor_unfilled_index = constructor_index;
8842 }
8843 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8844 {
8845 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 8846
c22cacf3
MS
8847 /* Do a basic check of initializer size. Note that vectors
8848 always have a fixed size derived from their type. */
3e4093b6
RS
8849 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8850 {
509c9d60
MLI
8851 pedwarn_init (input_location, 0,
8852 "excess elements in vector initializer");
3e4093b6
RS
8853 break;
8854 }
8b6a5902 8855
3e4093b6 8856 /* Now output the actual element. */
916c5919 8857 if (value.value)
53650abe
AP
8858 {
8859 if (TREE_CODE (value.value) == VECTOR_CST)
8860 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 8861 output_init_element (loc, value.value, value.original_type,
53650abe 8862 strict_string, elttype,
a1e3b3d9
LB
8863 constructor_index, 1, implicit,
8864 braced_init_obstack);
53650abe 8865 }
8b6a5902 8866
3e4093b6 8867 constructor_index
db3927fb
AH
8868 = size_binop_loc (input_location,
8869 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 8870
916c5919 8871 if (!value.value)
3e4093b6
RS
8872 /* If we are doing the bookkeeping for an element that was
8873 directly output as a constructor, we must update
8874 constructor_unfilled_index. */
8875 constructor_unfilled_index = constructor_index;
8876 }
8b6a5902 8877
3e4093b6
RS
8878 /* Handle the sole element allowed in a braced initializer
8879 for a scalar variable. */
b4519d39
SB
8880 else if (constructor_type != error_mark_node
8881 && constructor_fields == 0)
8b6a5902 8882 {
509c9d60
MLI
8883 pedwarn_init (input_location, 0,
8884 "excess elements in scalar initializer");
3e4093b6 8885 break;
8b6a5902
JJ
8886 }
8887 else
8888 {
916c5919 8889 if (value.value)
34cf811f 8890 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8891 strict_string, constructor_type,
a1e3b3d9
LB
8892 NULL_TREE, 1, implicit,
8893 braced_init_obstack);
3e4093b6 8894 constructor_fields = 0;
8b6a5902
JJ
8895 }
8896
3e4093b6
RS
8897 /* Handle range initializers either at this level or anywhere higher
8898 in the designator stack. */
8899 if (constructor_range_stack)
8b6a5902 8900 {
3e4093b6
RS
8901 struct constructor_range_stack *p, *range_stack;
8902 int finish = 0;
8903
8904 range_stack = constructor_range_stack;
8905 constructor_range_stack = 0;
8906 while (constructor_stack != range_stack->stack)
8b6a5902 8907 {
366de0ce 8908 gcc_assert (constructor_stack->implicit);
34cf811f
MP
8909 process_init_element (loc,
8910 pop_init_level (1, braced_init_obstack),
a1e3b3d9 8911 true, braced_init_obstack);
8b6a5902 8912 }
3e4093b6
RS
8913 for (p = range_stack;
8914 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8915 p = p->prev)
8b6a5902 8916 {
366de0ce 8917 gcc_assert (constructor_stack->implicit);
34cf811f
MP
8918 process_init_element (loc,
8919 pop_init_level (1, braced_init_obstack),
a1e3b3d9 8920 true, braced_init_obstack);
8b6a5902 8921 }
3e4093b6 8922
db3927fb
AH
8923 p->index = size_binop_loc (input_location,
8924 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
8925 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8926 finish = 1;
8927
8928 while (1)
8929 {
8930 constructor_index = p->index;
8931 constructor_fields = p->fields;
8932 if (finish && p->range_end && p->index == p->range_start)
8933 {
8934 finish = 0;
8935 p->prev = 0;
8936 }
8937 p = p->next;
8938 if (!p)
8939 break;
a1e3b3d9 8940 push_init_level (2, braced_init_obstack);
3e4093b6
RS
8941 p->stack = constructor_stack;
8942 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8943 p->index = p->range_start;
8944 }
8945
8946 if (!finish)
8947 constructor_range_stack = range_stack;
8948 continue;
8b6a5902
JJ
8949 }
8950
3e4093b6 8951 break;
8b6a5902
JJ
8952 }
8953
3e4093b6
RS
8954 constructor_range_stack = 0;
8955}
8956\f
9f0e2d86
ZW
8957/* Build a complete asm-statement, whose components are a CV_QUALIFIER
8958 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 8959 an ASM_EXPR node). */
3e4093b6 8960tree
9f0e2d86 8961build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 8962{
6de9cd9a
DN
8963 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8964 ASM_VOLATILE_P (args) = 1;
9f0e2d86 8965 return add_stmt (args);
8b6a5902
JJ
8966}
8967
9f0e2d86
ZW
8968/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8969 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8970 SIMPLE indicates whether there was anything at all after the
8971 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 8972 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 8973tree
c2255bc4 8974build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 8975 tree clobbers, tree labels, bool simple)
e5e809f4 8976{
3e4093b6 8977 tree tail;
9f0e2d86 8978 tree args;
6de9cd9a
DN
8979 int i;
8980 const char *constraint;
74f0c611 8981 const char **oconstraints;
6de9cd9a 8982 bool allows_mem, allows_reg, is_inout;
74f0c611 8983 int ninputs, noutputs;
6de9cd9a
DN
8984
8985 ninputs = list_length (inputs);
8986 noutputs = list_length (outputs);
74f0c611
RH
8987 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8988
1c384bf1 8989 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 8990
6de9cd9a
DN
8991 /* Remove output conversions that change the type but not the mode. */
8992 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 8993 {
3e4093b6 8994 tree output = TREE_VALUE (tail);
74f0c611 8995
eadd3d0d
JJ
8996 output = c_fully_fold (output, false, NULL);
8997
74f0c611
RH
8998 /* ??? Really, this should not be here. Users should be using a
8999 proper lvalue, dammit. But there's a long history of using casts
9000 in the output operands. In cases like longlong.h, this becomes a
9001 primitive form of typechecking -- if the cast can be removed, then
9002 the output operand had a type of the proper width; otherwise we'll
9003 get an error. Gross, but ... */
3e4093b6 9004 STRIP_NOPS (output);
74f0c611 9005
7bd11157 9006 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9007 output = error_mark_node;
8b6a5902 9008
5544530a
PB
9009 if (output != error_mark_node
9010 && (TREE_READONLY (output)
9011 || TYPE_READONLY (TREE_TYPE (output))
9012 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9013 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9014 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9015 readonly_error (loc, output, lv_asm);
5544530a 9016
6de9cd9a 9017 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9018 oconstraints[i] = constraint;
9019
9020 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9021 &allows_mem, &allows_reg, &is_inout))
9022 {
9023 /* If the operand is going to end up in memory,
9024 mark it addressable. */
9025 if (!allows_reg && !c_mark_addressable (output))
9026 output = error_mark_node;
bae5cddf
JJ
9027 if (!(!allows_reg && allows_mem)
9028 && output != error_mark_node
9029 && VOID_TYPE_P (TREE_TYPE (output)))
9030 {
9031 error_at (loc, "invalid use of void expression");
9032 output = error_mark_node;
9033 }
74f0c611
RH
9034 }
9035 else
c22cacf3 9036 output = error_mark_node;
3e4093b6 9037
74f0c611 9038 TREE_VALUE (tail) = output;
8b6a5902 9039 }
3e4093b6 9040
74f0c611
RH
9041 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9042 {
9043 tree input;
9044
9045 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9046 input = TREE_VALUE (tail);
9047
74f0c611
RH
9048 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9049 oconstraints, &allows_mem, &allows_reg))
9050 {
9051 /* If the operand is going to end up in memory,
9052 mark it addressable. */
b4c33883
AP
9053 if (!allows_reg && allows_mem)
9054 {
eadd3d0d
JJ
9055 input = c_fully_fold (input, false, NULL);
9056
b4c33883
AP
9057 /* Strip the nops as we allow this case. FIXME, this really
9058 should be rejected or made deprecated. */
9059 STRIP_NOPS (input);
9060 if (!c_mark_addressable (input))
9061 input = error_mark_node;
bae5cddf 9062 }
eadd3d0d 9063 else
bae5cddf 9064 {
eadd3d0d
JJ
9065 struct c_expr expr;
9066 memset (&expr, 0, sizeof (expr));
9067 expr.value = input;
267bac10 9068 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9069 input = c_fully_fold (expr.value, false, NULL);
9070
9071 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9072 {
9073 error_at (loc, "invalid use of void expression");
9074 input = error_mark_node;
9075 }
bae5cddf 9076 }
74f0c611
RH
9077 }
9078 else
9079 input = error_mark_node;
9080
9081 TREE_VALUE (tail) = input;
9082 }
3e4093b6 9083
1c384bf1
RH
9084 /* ASMs with labels cannot have outputs. This should have been
9085 enforced by the parser. */
9086 gcc_assert (outputs == NULL || labels == NULL);
9087
9088 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9089
5544530a
PB
9090 /* asm statements without outputs, including simple ones, are treated
9091 as volatile. */
9092 ASM_INPUT_P (args) = simple;
9093 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9094
9f0e2d86 9095 return args;
e5e809f4 9096}
3e4093b6 9097\f
c2255bc4
AH
9098/* Generate a goto statement to LABEL. LOC is the location of the
9099 GOTO. */
506e2710
RH
9100
9101tree
c2255bc4 9102c_finish_goto_label (location_t loc, tree label)
506e2710 9103{
e1b7793c 9104 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9105 if (!decl)
9106 return NULL_TREE;
506e2710 9107 TREE_USED (decl) = 1;
c2255bc4
AH
9108 {
9109 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9110 SET_EXPR_LOCATION (t, loc);
9111 return add_stmt (t);
9112 }
506e2710
RH
9113}
9114
c2255bc4
AH
9115/* Generate a computed goto statement to EXPR. LOC is the location of
9116 the GOTO. */
506e2710
RH
9117
9118tree
c2255bc4 9119c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9120{
c2255bc4 9121 tree t;
c1771a20 9122 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9123 expr = c_fully_fold (expr, false, NULL);
506e2710 9124 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9125 t = build1 (GOTO_EXPR, void_type_node, expr);
9126 SET_EXPR_LOCATION (t, loc);
9127 return add_stmt (t);
506e2710
RH
9128}
9129
5088b058 9130/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4
AH
9131 to return, or a null pointer for `return;' with no value. LOC is
9132 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
9133 is the original type of RETVAL. */
de520661 9134
506e2710 9135tree
c2255bc4 9136c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9137{
0c9b182b
JJ
9138 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9139 bool no_warning = false;
928c19bb 9140 bool npc = false;
36536d79 9141 size_t rank = 0;
3e4093b6
RS
9142
9143 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9144 warning_at (loc, 0,
9145 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9146
b72271b9 9147 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9148 {
9149 /* Array notations are allowed in a return statement if it is inside a
9150 built-in array notation reduction function. */
9151 if (!find_rank (loc, retval, retval, false, &rank))
9152 return error_mark_node;
9153 if (rank >= 1)
9154 {
9155 error_at (loc, "array notation expression cannot be used as a "
9156 "return value");
9157 return error_mark_node;
9158 }
9159 }
3af9c5e9 9160 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9161 {
9162 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9163 "allowed");
9164 return error_mark_node;
9165 }
928c19bb
JM
9166 if (retval)
9167 {
8ce94e44 9168 tree semantic_type = NULL_TREE;
928c19bb 9169 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9170 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9171 {
9172 semantic_type = TREE_TYPE (retval);
9173 retval = TREE_OPERAND (retval, 0);
9174 }
928c19bb 9175 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9176 if (semantic_type)
9177 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9178 }
9179
3e4093b6 9180 if (!retval)
de520661 9181 {
3e4093b6
RS
9182 current_function_returns_null = 1;
9183 if ((warn_return_type || flag_isoc99)
9184 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9185 {
b8698a0f 9186 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
fcf73884 9187 "%<return%> with no value, in "
0c9b182b
JJ
9188 "function returning non-void");
9189 no_warning = true;
9190 }
400fbf9f 9191 }
3e4093b6 9192 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9193 {
3e4093b6 9194 current_function_returns_null = 1;
2397c575 9195 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9196 pedwarn (loc, 0,
509c9d60 9197 "%<return%> with a value, in function returning void");
b8698a0f 9198 else
c1771a20 9199 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9200 "%<return%> with expression, in function returning void");
de520661 9201 }
3e4093b6 9202 else
de520661 9203 {
68fca595
MP
9204 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9205 retval, origtype, ic_return,
c2255bc4 9206 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9207 tree res = DECL_RESULT (current_function_decl);
9208 tree inner;
9feb29df 9209 bool save;
3e4093b6
RS
9210
9211 current_function_returns_value = 1;
9212 if (t == error_mark_node)
506e2710 9213 return NULL_TREE;
3e4093b6 9214
9feb29df
JJ
9215 save = in_late_binary_op;
9216 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9217 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9218 in_late_binary_op = true;
3e4093b6 9219 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9220 in_late_binary_op = save;
3e4093b6
RS
9221
9222 /* Strip any conversions, additions, and subtractions, and see if
9223 we are returning the address of a local variable. Warn if so. */
9224 while (1)
8b6a5902 9225 {
3e4093b6 9226 switch (TREE_CODE (inner))
8b6a5902 9227 {
849421a3
JJ
9228 CASE_CONVERT:
9229 case NON_LVALUE_EXPR:
3e4093b6 9230 case PLUS_EXPR:
849421a3 9231 case POINTER_PLUS_EXPR:
3e4093b6
RS
9232 inner = TREE_OPERAND (inner, 0);
9233 continue;
9234
9235 case MINUS_EXPR:
9236 /* If the second operand of the MINUS_EXPR has a pointer
9237 type (or is converted from it), this may be valid, so
9238 don't give a warning. */
9239 {
9240 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9241
3f75a254 9242 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9243 && (CONVERT_EXPR_P (op1)
9244 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9245 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9246
3e4093b6
RS
9247 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9248 break;
8b6a5902 9249
3e4093b6
RS
9250 inner = TREE_OPERAND (inner, 0);
9251 continue;
9252 }
400fbf9f 9253
3e4093b6
RS
9254 case ADDR_EXPR:
9255 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9256
6615c446 9257 while (REFERENCE_CLASS_P (inner)
c22cacf3 9258 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 9259 inner = TREE_OPERAND (inner, 0);
8b6a5902 9260
a2f1f4c3 9261 if (DECL_P (inner)
3f75a254
JM
9262 && !DECL_EXTERNAL (inner)
9263 && !TREE_STATIC (inner)
3e4093b6 9264 && DECL_CONTEXT (inner) == current_function_decl)
c2255bc4 9265 warning_at (loc,
880661a4
JW
9266 OPT_Wreturn_local_addr, "function returns address "
9267 "of local variable");
3e4093b6 9268 break;
8b6a5902 9269
3e4093b6
RS
9270 default:
9271 break;
9272 }
de520661 9273
3e4093b6
RS
9274 break;
9275 }
9276
53fb4de3 9277 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9278 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9279
9280 if (warn_sequence_point)
9281 verify_sequence_points (retval);
de520661 9282 }
8b6a5902 9283
c2255bc4 9284 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9285 TREE_NO_WARNING (ret_stmt) |= no_warning;
9286 return add_stmt (ret_stmt);
de520661 9287}
3e4093b6
RS
9288\f
9289struct c_switch {
604f5adf
ILT
9290 /* The SWITCH_EXPR being built. */
9291 tree switch_expr;
a6c0a76c 9292
89dbed81 9293 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9294 default conversion is applied. */
9295 tree orig_type;
9296
3e4093b6
RS
9297 /* A splay-tree mapping the low element of a case range to the high
9298 element, or NULL_TREE if there is no high element. Used to
9299 determine whether or not a new case label duplicates an old case
9300 label. We need a tree, rather than simply a hash table, because
9301 of the GNU case range extension. */
9302 splay_tree cases;
a6c0a76c 9303
e1b7793c
ILT
9304 /* The bindings at the point of the switch. This is used for
9305 warnings crossing decls when branching to a case label. */
9306 struct c_spot_bindings *bindings;
187230a7 9307
3e4093b6
RS
9308 /* The next node on the stack. */
9309 struct c_switch *next;
9310};
400fbf9f 9311
3e4093b6
RS
9312/* A stack of the currently active switch statements. The innermost
9313 switch statement is on the top of the stack. There is no need to
9314 mark the stack for garbage collection because it is only active
9315 during the processing of the body of a function, and we never
9316 collect at that point. */
de520661 9317
506e2710 9318struct c_switch *c_switch_stack;
de520661 9319
3e4093b6 9320/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4
AH
9321 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9322 SWITCH_COND_LOC is the location of the switch's condition. */
de520661 9323
3e4093b6 9324tree
c2255bc4
AH
9325c_start_case (location_t switch_loc,
9326 location_t switch_cond_loc,
9327 tree exp)
de520661 9328{
c58e8676 9329 tree orig_type = error_mark_node;
3e4093b6 9330 struct c_switch *cs;
2f6e4e97 9331
3e4093b6 9332 if (exp != error_mark_node)
de520661 9333 {
3e4093b6
RS
9334 orig_type = TREE_TYPE (exp);
9335
c58e8676 9336 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9337 {
c58e8676
VR
9338 if (orig_type != error_mark_node)
9339 {
c2255bc4 9340 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9341 orig_type = error_mark_node;
9342 }
3e4093b6 9343 exp = integer_zero_node;
de520661 9344 }
3e4093b6 9345 else
de520661 9346 {
c58e8676 9347 tree type = TYPE_MAIN_VARIANT (orig_type);
8b6a5902 9348
8400e75e 9349 if (!in_system_header_at (input_location)
3e4093b6
RS
9350 && (type == long_integer_type_node
9351 || type == long_unsigned_type_node))
c2255bc4
AH
9352 warning_at (switch_cond_loc,
9353 OPT_Wtraditional, "%<long%> switch expression not "
9354 "converted to %<int%> in ISO C");
8b6a5902 9355
928c19bb 9356 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9357 exp = default_conversion (exp);
ca085fd7
MLI
9358
9359 if (warn_sequence_point)
9360 verify_sequence_points (exp);
3e4093b6
RS
9361 }
9362 }
9363
604f5adf 9364 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9365 cs = XNEW (struct c_switch);
604f5adf 9366 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9367 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9368 cs->orig_type = orig_type;
3e4093b6 9369 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9370 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9371 cs->next = c_switch_stack;
9372 c_switch_stack = cs;
3e4093b6 9373
604f5adf 9374 return add_stmt (cs->switch_expr);
3e4093b6
RS
9375}
9376
c2255bc4 9377/* Process a case label at location LOC. */
3e4093b6
RS
9378
9379tree
c2255bc4 9380do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9381{
9382 tree label = NULL_TREE;
9383
17cede2e
JM
9384 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9385 {
9386 low_value = c_fully_fold (low_value, false, NULL);
9387 if (TREE_CODE (low_value) == INTEGER_CST)
c1771a20 9388 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9389 "case label is not an integer constant expression");
9390 }
9391
9392 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9393 {
9394 high_value = c_fully_fold (high_value, false, NULL);
9395 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9396 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9397 "case label is not an integer constant expression");
9398 }
9399
e1b7793c 9400 if (c_switch_stack == NULL)
187230a7
JM
9401 {
9402 if (low_value)
e1b7793c 9403 error_at (loc, "case label not within a switch statement");
187230a7 9404 else
e1b7793c
ILT
9405 error_at (loc, "%<default%> label not within a switch statement");
9406 return NULL_TREE;
187230a7 9407 }
de520661 9408
e1b7793c
ILT
9409 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9410 EXPR_LOCATION (c_switch_stack->switch_expr),
9411 loc))
9412 return NULL_TREE;
9413
9414 label = c_add_case_label (loc, c_switch_stack->cases,
9415 SWITCH_COND (c_switch_stack->switch_expr),
9416 c_switch_stack->orig_type,
9417 low_value, high_value);
9418 if (label == error_mark_node)
9419 label = NULL_TREE;
3e4093b6
RS
9420 return label;
9421}
de520661 9422
3e4093b6 9423/* Finish the switch statement. */
de520661 9424
3e4093b6 9425void
325c3691 9426c_finish_case (tree body)
3e4093b6 9427{
506e2710 9428 struct c_switch *cs = c_switch_stack;
fbc315db 9429 location_t switch_location;
3e4093b6 9430
604f5adf 9431 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9432
6de9cd9a 9433 /* Emit warnings as needed. */
c2255bc4 9434 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db
ILT
9435 c_do_switch_warnings (cs->cases, switch_location,
9436 TREE_TYPE (cs->switch_expr),
9437 SWITCH_COND (cs->switch_expr));
6de9cd9a 9438
3e4093b6 9439 /* Pop the stack. */
506e2710 9440 c_switch_stack = cs->next;
3e4093b6 9441 splay_tree_delete (cs->cases);
e1b7793c 9442 c_release_switch_bindings (cs->bindings);
5d038c4c 9443 XDELETE (cs);
de520661 9444}
325c3691 9445\f
506e2710
RH
9446/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9447 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9448 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9449 statement, and was not surrounded with parenthesis. */
325c3691 9450
9e51cf9d 9451void
506e2710
RH
9452c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9453 tree else_block, bool nested_if)
325c3691 9454{
506e2710 9455 tree stmt;
325c3691 9456
25c22937
BI
9457 /* If the condition has array notations, then the rank of the then_block and
9458 else_block must be either 0 or be equal to the rank of the condition. If
9459 the condition does not have array notations then break them up as it is
9460 broken up in a normal expression. */
b72271b9 9461 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9462 {
9463 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9464 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9465 return;
9466 if (then_block
9467 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9468 return;
9469 if (else_block
9470 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9471 return;
9472 if (cond_rank != then_rank && then_rank != 0)
9473 {
9474 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9475 " and the then-block");
9476 return;
9477 }
9478 else if (cond_rank != else_rank && else_rank != 0)
9479 {
9480 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9481 " and the else-block");
9482 return;
9483 }
9484 }
506e2710
RH
9485 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9486 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9487 {
506e2710 9488 tree inner_if = then_block;
16865eaa 9489
61ada8ae 9490 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9491 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9492 it might not be exactly THEN_BLOCK, but should be the last
9493 non-container statement within. */
9494 while (1)
9495 switch (TREE_CODE (inner_if))
9496 {
9497 case COND_EXPR:
9498 goto found;
9499 case BIND_EXPR:
9500 inner_if = BIND_EXPR_BODY (inner_if);
9501 break;
9502 case STATEMENT_LIST:
9503 inner_if = expr_last (then_block);
9504 break;
9505 case TRY_FINALLY_EXPR:
9506 case TRY_CATCH_EXPR:
9507 inner_if = TREE_OPERAND (inner_if, 0);
9508 break;
9509 default:
366de0ce 9510 gcc_unreachable ();
506e2710
RH
9511 }
9512 found:
16865eaa 9513
506e2710 9514 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9515 warning_at (if_locus, OPT_Wparentheses,
9516 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9517 }
16865eaa 9518
2214de30 9519 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9520 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9521 add_stmt (stmt);
325c3691
RH
9522}
9523
506e2710
RH
9524/* Emit a general-purpose loop construct. START_LOCUS is the location of
9525 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9526 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9527 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9528 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9529
9530void
506e2710
RH
9531c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9532 tree blab, tree clab, bool cond_is_first)
325c3691 9533{
506e2710
RH
9534 tree entry = NULL, exit = NULL, t;
9535
b72271b9 9536 if (flag_cilkplus && contains_array_notation_expr (cond))
36536d79
BI
9537 {
9538 error_at (start_locus, "array notation expression cannot be used in a "
9539 "loop%'s condition");
9540 return;
9541 }
9542
28af952a
RS
9543 /* If the condition is zero don't generate a loop construct. */
9544 if (cond && integer_zerop (cond))
9545 {
9546 if (cond_is_first)
9547 {
9548 t = build_and_jump (&blab);
9549 SET_EXPR_LOCATION (t, start_locus);
9550 add_stmt (t);
9551 }
9552 }
9553 else
506e2710
RH
9554 {
9555 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9556
506e2710 9557 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9558 out of the loop, or to the top of it. If there's no exit condition,
9559 then we just build a jump back to the top. */
506e2710 9560 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9561
28af952a 9562 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9563 {
9564 /* Canonicalize the loop condition to the end. This means
9565 generating a branch to the loop condition. Reuse the
9566 continue label, if possible. */
9567 if (cond_is_first)
9568 {
9569 if (incr || !clab)
9570 {
9571 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9572 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9573 }
9574 else
9575 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9576 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9577 add_stmt (t);
9578 }
9579
506e2710 9580 t = build_and_jump (&blab);
506e2710 9581 if (cond_is_first)
db3927fb
AH
9582 exit = fold_build3_loc (start_locus,
9583 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9584 else
db3927fb
AH
9585 exit = fold_build3_loc (input_location,
9586 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9587 }
9588
506e2710
RH
9589 add_stmt (top);
9590 }
c22cacf3 9591
506e2710
RH
9592 if (body)
9593 add_stmt (body);
9594 if (clab)
9595 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9596 if (incr)
9597 add_stmt (incr);
9598 if (entry)
9599 add_stmt (entry);
9600 if (exit)
9601 add_stmt (exit);
9602 if (blab)
9603 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9604}
325c3691
RH
9605
9606tree
c2255bc4 9607c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9608{
089efaa4 9609 bool skip;
506e2710 9610 tree label = *label_p;
325c3691 9611
089efaa4
ILT
9612 /* In switch statements break is sometimes stylistically used after
9613 a return statement. This can lead to spurious warnings about
9614 control reaching the end of a non-void function when it is
9615 inlined. Note that we are calling block_may_fallthru with
9616 language specific tree nodes; this works because
9617 block_may_fallthru returns true when given something it does not
9618 understand. */
9619 skip = !block_may_fallthru (cur_stmt_list);
9620
506e2710 9621 if (!label)
089efaa4
ILT
9622 {
9623 if (!skip)
c2255bc4 9624 *label_p = label = create_artificial_label (loc);
089efaa4 9625 }
953ff289
DN
9626 else if (TREE_CODE (label) == LABEL_DECL)
9627 ;
9628 else switch (TREE_INT_CST_LOW (label))
506e2710 9629 {
953ff289 9630 case 0:
506e2710 9631 if (is_break)
c2255bc4 9632 error_at (loc, "break statement not within loop or switch");
506e2710 9633 else
c2255bc4 9634 error_at (loc, "continue statement not within a loop");
506e2710 9635 return NULL_TREE;
953ff289
DN
9636
9637 case 1:
9638 gcc_assert (is_break);
c2255bc4 9639 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9640 return NULL_TREE;
9641
c02065fc
AH
9642 case 2:
9643 if (is_break)
9644 error ("break statement within %<#pragma simd%> loop body");
9645 else
9646 error ("continue statement within %<#pragma simd%> loop body");
9647 return NULL_TREE;
9648
953ff289
DN
9649 default:
9650 gcc_unreachable ();
506e2710 9651 }
325c3691 9652
089efaa4
ILT
9653 if (skip)
9654 return NULL_TREE;
9655
2e28e797
JH
9656 if (!is_break)
9657 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9658
53fb4de3 9659 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9660}
9661
506e2710 9662/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9663
9664static void
c2255bc4 9665emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9666{
e6b5a630
RH
9667 if (expr == error_mark_node)
9668 ;
9669 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9670 {
9671 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9672 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9673 }
789eadcd
MP
9674 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9675 {
9676 tree r = expr;
9677 location_t cloc = loc;
9678 while (TREE_CODE (r) == COMPOUND_EXPR)
9679 {
9680 if (EXPR_HAS_LOCATION (r))
9681 cloc = EXPR_LOCATION (r);
9682 r = TREE_OPERAND (r, 1);
9683 }
9684 if (!TREE_SIDE_EFFECTS (r)
9685 && !VOID_TYPE_P (TREE_TYPE (r))
9686 && !CONVERT_EXPR_P (r)
cc28fc7f 9687 && !TREE_NO_WARNING (r)
789eadcd
MP
9688 && !TREE_NO_WARNING (expr))
9689 warning_at (cloc, OPT_Wunused_value,
9690 "right-hand operand of comma expression has no effect");
9691 }
27f33b15 9692 else
c2255bc4 9693 warn_if_unused_value (expr, loc);
3a5b9284
RH
9694}
9695
506e2710 9696/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9697 diagnostics, but do not call ADD_STMT. LOC is the location of the
9698 statement. */
3a5b9284 9699
506e2710 9700tree
c2255bc4 9701c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9702{
056928b2
JJ
9703 tree exprv;
9704
3a5b9284 9705 if (!expr)
506e2710 9706 return NULL_TREE;
3a5b9284 9707
928c19bb
JM
9708 expr = c_fully_fold (expr, false, NULL);
9709
3a5b9284
RH
9710 if (warn_sequence_point)
9711 verify_sequence_points (expr);
9712
9713 if (TREE_TYPE (expr) != error_mark_node
9714 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9715 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9716 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9717
9718 /* If we're not processing a statement expression, warn about unused values.
9719 Warnings for statement expressions will be emitted later, once we figure
9720 out which is the result. */
9721 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9722 && warn_unused_value)
c2255bc4 9723 emit_side_effect_warnings (loc, expr);
3a5b9284 9724
056928b2
JJ
9725 exprv = expr;
9726 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9727 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9728 while (CONVERT_EXPR_P (exprv))
9729 exprv = TREE_OPERAND (exprv, 0);
9730 if (DECL_P (exprv)
9731 || handled_component_p (exprv)
9732 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9733 mark_exp_read (exprv);
fa8351f8 9734
3a5b9284
RH
9735 /* If the expression is not of a type to which we cannot assign a line
9736 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9737 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9738 {
9739 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9740 SET_EXPR_LOCATION (expr, loc);
9741 }
506e2710
RH
9742
9743 return expr;
9744}
9745
c2255bc4
AH
9746/* Emit an expression as a statement. LOC is the location of the
9747 expression. */
506e2710
RH
9748
9749tree
c2255bc4 9750c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9751{
9752 if (expr)
c2255bc4 9753 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9754 else
9755 return NULL;
3a5b9284
RH
9756}
9757
9758/* Do the opposite and emit a statement as an expression. To begin,
9759 create a new binding level and return it. */
325c3691
RH
9760
9761tree
9762c_begin_stmt_expr (void)
9763{
9764 tree ret;
9765
9766 /* We must force a BLOCK for this level so that, if it is not expanded
9767 later, there is a way to turn off the entire subtree of blocks that
9768 are contained in it. */
9769 keep_next_level ();
9770 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
9771
9772 c_bindings_start_stmt_expr (c_switch_stack == NULL
9773 ? NULL
9774 : c_switch_stack->bindings);
325c3691
RH
9775
9776 /* Mark the current statement list as belonging to a statement list. */
9777 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9778
9779 return ret;
9780}
9781
c2255bc4
AH
9782/* LOC is the location of the compound statement to which this body
9783 belongs. */
9784
325c3691 9785tree
c2255bc4 9786c_finish_stmt_expr (location_t loc, tree body)
325c3691 9787{
3a5b9284 9788 tree last, type, tmp, val;
325c3691
RH
9789 tree *last_p;
9790
c2255bc4 9791 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
9792
9793 c_bindings_end_stmt_expr (c_switch_stack == NULL
9794 ? NULL
9795 : c_switch_stack->bindings);
325c3691 9796
3a5b9284
RH
9797 /* Locate the last statement in BODY. See c_end_compound_stmt
9798 about always returning a BIND_EXPR. */
9799 last_p = &BIND_EXPR_BODY (body);
9800 last = BIND_EXPR_BODY (body);
9801
9802 continue_searching:
325c3691
RH
9803 if (TREE_CODE (last) == STATEMENT_LIST)
9804 {
3a5b9284
RH
9805 tree_stmt_iterator i;
9806
9807 /* This can happen with degenerate cases like ({ }). No value. */
9808 if (!TREE_SIDE_EFFECTS (last))
9809 return body;
9810
9811 /* If we're supposed to generate side effects warnings, process
9812 all of the statements except the last. */
27f33b15 9813 if (warn_unused_value)
325c3691 9814 {
3a5b9284 9815 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
9816 {
9817 location_t tloc;
9818 tree t = tsi_stmt (i);
9819
9820 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9821 emit_side_effect_warnings (tloc, t);
9822 }
325c3691
RH
9823 }
9824 else
3a5b9284
RH
9825 i = tsi_last (last);
9826 last_p = tsi_stmt_ptr (i);
9827 last = *last_p;
325c3691
RH
9828 }
9829
3a5b9284
RH
9830 /* If the end of the list is exception related, then the list was split
9831 by a call to push_cleanup. Continue searching. */
9832 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9833 || TREE_CODE (last) == TRY_CATCH_EXPR)
9834 {
9835 last_p = &TREE_OPERAND (last, 0);
9836 last = *last_p;
9837 goto continue_searching;
9838 }
9839
26d8af35
JM
9840 if (last == error_mark_node)
9841 return last;
9842
3a5b9284
RH
9843 /* In the case that the BIND_EXPR is not necessary, return the
9844 expression out from inside it. */
26d8af35
JM
9845 if (last == BIND_EXPR_BODY (body)
9846 && BIND_EXPR_VARS (body) == NULL)
591baeb0 9847 {
928c19bb
JM
9848 /* Even if this looks constant, do not allow it in a constant
9849 expression. */
e5a94231 9850 last = c_wrap_maybe_const (last, true);
591baeb0
JM
9851 /* Do not warn if the return value of a statement expression is
9852 unused. */
928c19bb 9853 TREE_NO_WARNING (last) = 1;
591baeb0
JM
9854 return last;
9855 }
325c3691
RH
9856
9857 /* Extract the type of said expression. */
9858 type = TREE_TYPE (last);
325c3691 9859
3a5b9284
RH
9860 /* If we're not returning a value at all, then the BIND_EXPR that
9861 we already have is a fine expression to return. */
9862 if (!type || VOID_TYPE_P (type))
9863 return body;
9864
9865 /* Now that we've located the expression containing the value, it seems
9866 silly to make voidify_wrapper_expr repeat the process. Create a
9867 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9868 tmp = create_tmp_var_raw (type, NULL);
9869
9870 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9871 tree_expr_nonnegative_p giving up immediately. */
9872 val = last;
9873 if (TREE_CODE (val) == NOP_EXPR
9874 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9875 val = TREE_OPERAND (val, 0);
9876
53fb4de3 9877 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 9878 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 9879
c2255bc4
AH
9880 {
9881 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9882 SET_EXPR_LOCATION (t, loc);
9883 return t;
9884 }
325c3691
RH
9885}
9886\f
9887/* Begin and end compound statements. This is as simple as pushing
9888 and popping new statement lists from the tree. */
9889
9890tree
9891c_begin_compound_stmt (bool do_scope)
9892{
9893 tree stmt = push_stmt_list ();
9894 if (do_scope)
4dfa0342 9895 push_scope ();
325c3691
RH
9896 return stmt;
9897}
9898
c2255bc4
AH
9899/* End a compound statement. STMT is the statement. LOC is the
9900 location of the compound statement-- this is usually the location
9901 of the opening brace. */
9902
325c3691 9903tree
c2255bc4 9904c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
9905{
9906 tree block = NULL;
9907
9908 if (do_scope)
9909 {
9910 if (c_dialect_objc ())
9911 objc_clear_super_receiver ();
9912 block = pop_scope ();
9913 }
9914
9915 stmt = pop_stmt_list (stmt);
c2255bc4 9916 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
9917
9918 /* If this compound statement is nested immediately inside a statement
9919 expression, then force a BIND_EXPR to be created. Otherwise we'll
9920 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9921 STATEMENT_LISTs merge, and thus we can lose track of what statement
9922 was really last. */
38e01f9e 9923 if (building_stmt_list_p ()
325c3691
RH
9924 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9925 && TREE_CODE (stmt) != BIND_EXPR)
9926 {
53fb4de3 9927 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 9928 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 9929 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
9930 }
9931
9932 return stmt;
9933}
5a508662
RH
9934
9935/* Queue a cleanup. CLEANUP is an expression/statement to be executed
9936 when the current scope is exited. EH_ONLY is true when this is not
9937 meant to apply to normal control flow transfer. */
9938
9939void
c2255bc4 9940push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 9941{
3a5b9284
RH
9942 enum tree_code code;
9943 tree stmt, list;
9944 bool stmt_expr;
9945
9946 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 9947 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 9948 add_stmt (stmt);
3a5b9284
RH
9949 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9950 list = push_stmt_list ();
9951 TREE_OPERAND (stmt, 0) = list;
9952 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 9953}
325c3691 9954\f
3e4093b6
RS
9955/* Build a binary-operation expression without default conversions.
9956 CODE is the kind of expression to build.
ba47d38d 9957 LOCATION is the operator's location.
3e4093b6
RS
9958 This function differs from `build' in several ways:
9959 the data type of the result is computed and recorded in it,
9960 warnings are generated if arg data types are invalid,
9961 special handling for addition and subtraction of pointers is known,
9962 and some optimization is done (operations on narrow ints
9963 are done in the narrower type when that gives the same result).
9964 Constant folding is also done before the result is returned.
de520661 9965
3e4093b6
RS
9966 Note that the operands will never have enumeral types, or function
9967 or array types, because either they will have the default conversions
9968 performed or they have both just been converted to some other type in which
9969 the arithmetic is to be done. */
9970
9971tree
ba47d38d
AH
9972build_binary_op (location_t location, enum tree_code code,
9973 tree orig_op0, tree orig_op1, int convert_p)
de520661 9974{
8ce94e44
JM
9975 tree type0, type1, orig_type0, orig_type1;
9976 tree eptype;
3e4093b6
RS
9977 enum tree_code code0, code1;
9978 tree op0, op1;
c9f9eb5d 9979 tree ret = error_mark_node;
4de67c26 9980 const char *invalid_op_diag;
4d84fe7c 9981 bool op0_int_operands, op1_int_operands;
928c19bb 9982 bool int_const, int_const_or_overflow, int_operands;
b62acd60 9983
3e4093b6
RS
9984 /* Expression code to give to the expression when it is built.
9985 Normally this is CODE, which is what the caller asked for,
9986 but in some special cases we change it. */
9987 enum tree_code resultcode = code;
8b6a5902 9988
3e4093b6
RS
9989 /* Data type in which the computation is to be performed.
9990 In the simplest cases this is the common type of the arguments. */
9991 tree result_type = NULL;
9992
8ce94e44
JM
9993 /* When the computation is in excess precision, the type of the
9994 final EXCESS_PRECISION_EXPR. */
2d2e923f 9995 tree semantic_result_type = NULL;
8ce94e44 9996
3e4093b6
RS
9997 /* Nonzero means operands have already been type-converted
9998 in whatever way is necessary.
9999 Zero means they need to be converted to RESULT_TYPE. */
10000 int converted = 0;
10001
10002 /* Nonzero means create the expression with this type, rather than
10003 RESULT_TYPE. */
10004 tree build_type = 0;
10005
10006 /* Nonzero means after finally constructing the expression
10007 convert it to this type. */
10008 tree final_type = 0;
10009
10010 /* Nonzero if this is an operation like MIN or MAX which can
10011 safely be computed in short if both args are promoted shorts.
10012 Also implies COMMON.
10013 -1 indicates a bitwise operation; this makes a difference
10014 in the exact conditions for when it is safe to do the operation
10015 in a narrower mode. */
10016 int shorten = 0;
10017
10018 /* Nonzero if this is a comparison operation;
10019 if both args are promoted shorts, compare the original shorts.
10020 Also implies COMMON. */
10021 int short_compare = 0;
10022
10023 /* Nonzero if this is a right-shift operation, which can be computed on the
10024 original short and then promoted if the operand is a promoted short. */
10025 int short_shift = 0;
10026
10027 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10028 int common = 0;
10029
58393038
ZL
10030 /* True means types are compatible as far as ObjC is concerned. */
10031 bool objc_ok;
10032
8ce94e44
JM
10033 /* True means this is an arithmetic operation that may need excess
10034 precision. */
10035 bool may_need_excess_precision;
10036
180f8dbb
JM
10037 /* True means this is a boolean operation that converts both its
10038 operands to truth-values. */
10039 bool boolean_op = false;
10040
de5a5fa1
MP
10041 /* Remember whether we're doing / or %. */
10042 bool doing_div_or_mod = false;
10043
10044 /* Remember whether we're doing << or >>. */
10045 bool doing_shift = false;
10046
10047 /* Tree holding instrumentation expression. */
10048 tree instrument_expr = NULL;
10049
ba47d38d
AH
10050 if (location == UNKNOWN_LOCATION)
10051 location = input_location;
10052
4d84fe7c
JM
10053 op0 = orig_op0;
10054 op1 = orig_op1;
10055
10056 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10057 if (op0_int_operands)
10058 op0 = remove_c_maybe_const_expr (op0);
10059 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10060 if (op1_int_operands)
10061 op1 = remove_c_maybe_const_expr (op1);
10062 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10063 if (int_operands)
10064 {
10065 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10066 && TREE_CODE (orig_op1) == INTEGER_CST);
10067 int_const = (int_const_or_overflow
10068 && !TREE_OVERFLOW (orig_op0)
10069 && !TREE_OVERFLOW (orig_op1));
10070 }
10071 else
10072 int_const = int_const_or_overflow = false;
10073
0e3a99ae 10074 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
10075 if (convert_p
10076 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 10077 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 10078 {
4d84fe7c
JM
10079 op0 = default_conversion (op0);
10080 op1 = default_conversion (op1);
790e9490
RS
10081 }
10082
36536d79
BI
10083 /* When Cilk Plus is enabled and there are array notations inside op0, then
10084 we check to see if there are builtin array notation functions. If
10085 so, then we take on the type of the array notation inside it. */
b72271b9 10086 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10087 orig_type0 = type0 = find_correct_array_notation_type (op0);
10088 else
10089 orig_type0 = type0 = TREE_TYPE (op0);
10090
b72271b9 10091 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10092 orig_type1 = type1 = find_correct_array_notation_type (op1);
10093 else
10094 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10095
3e4093b6
RS
10096 /* The expression codes of the data types of the arguments tell us
10097 whether the arguments are integers, floating, pointers, etc. */
10098 code0 = TREE_CODE (type0);
10099 code1 = TREE_CODE (type1);
10100
10101 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10102 STRIP_TYPE_NOPS (op0);
10103 STRIP_TYPE_NOPS (op1);
10104
10105 /* If an error was already reported for one of the arguments,
10106 avoid reporting another error. */
10107
10108 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10109 return error_mark_node;
10110
4de67c26
JM
10111 if ((invalid_op_diag
10112 = targetm.invalid_binary_op (code, type0, type1)))
10113 {
ba47d38d 10114 error_at (location, invalid_op_diag);
4de67c26
JM
10115 return error_mark_node;
10116 }
10117
8ce94e44
JM
10118 switch (code)
10119 {
10120 case PLUS_EXPR:
10121 case MINUS_EXPR:
10122 case MULT_EXPR:
10123 case TRUNC_DIV_EXPR:
10124 case CEIL_DIV_EXPR:
10125 case FLOOR_DIV_EXPR:
10126 case ROUND_DIV_EXPR:
10127 case EXACT_DIV_EXPR:
10128 may_need_excess_precision = true;
10129 break;
10130 default:
10131 may_need_excess_precision = false;
10132 break;
10133 }
10134 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10135 {
10136 op0 = TREE_OPERAND (op0, 0);
10137 type0 = TREE_TYPE (op0);
10138 }
10139 else if (may_need_excess_precision
10140 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10141 {
10142 type0 = eptype;
10143 op0 = convert (eptype, op0);
10144 }
10145 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10146 {
10147 op1 = TREE_OPERAND (op1, 0);
10148 type1 = TREE_TYPE (op1);
10149 }
10150 else if (may_need_excess_precision
10151 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10152 {
10153 type1 = eptype;
10154 op1 = convert (eptype, op1);
10155 }
10156
58393038
ZL
10157 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10158
0e3a99ae
AS
10159 /* In case when one of the operands of the binary operation is
10160 a vector and another is a scalar -- convert scalar to vector. */
10161 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10162 {
a212e43f
MG
10163 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10164 true);
f90e8e2e 10165
0e3a99ae
AS
10166 switch (convert_flag)
10167 {
10168 case stv_error:
10169 return error_mark_node;
10170 case stv_firstarg:
10171 {
10172 bool maybe_const = true;
10173 tree sc;
10174 sc = c_fully_fold (op0, false, &maybe_const);
10175 sc = save_expr (sc);
10176 sc = convert (TREE_TYPE (type1), sc);
10177 op0 = build_vector_from_val (type1, sc);
10178 if (!maybe_const)
10179 op0 = c_wrap_maybe_const (op0, true);
10180 orig_type0 = type0 = TREE_TYPE (op0);
10181 code0 = TREE_CODE (type0);
10182 converted = 1;
10183 break;
10184 }
10185 case stv_secondarg:
10186 {
10187 bool maybe_const = true;
10188 tree sc;
10189 sc = c_fully_fold (op1, false, &maybe_const);
10190 sc = save_expr (sc);
10191 sc = convert (TREE_TYPE (type0), sc);
10192 op1 = build_vector_from_val (type0, sc);
10193 if (!maybe_const)
54b9f838 10194 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10195 orig_type1 = type1 = TREE_TYPE (op1);
10196 code1 = TREE_CODE (type1);
10197 converted = 1;
10198 break;
10199 }
10200 default:
10201 break;
10202 }
10203 }
10204
3e4093b6 10205 switch (code)
de520661 10206 {
3e4093b6
RS
10207 case PLUS_EXPR:
10208 /* Handle the pointer + int case. */
10209 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10210 {
db3927fb 10211 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10212 goto return_build_binary_op;
10213 }
3e4093b6 10214 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10215 {
db3927fb 10216 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10217 goto return_build_binary_op;
10218 }
fe67cf58 10219 else
3e4093b6
RS
10220 common = 1;
10221 break;
400fbf9f 10222
3e4093b6
RS
10223 case MINUS_EXPR:
10224 /* Subtraction of two similar pointers.
10225 We must subtract them as integers, then divide by object size. */
10226 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10227 && comp_target_types (location, type0, type1))
c9f9eb5d 10228 {
db3927fb 10229 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10230 goto return_build_binary_op;
10231 }
3e4093b6
RS
10232 /* Handle pointer minus int. Just like pointer plus int. */
10233 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10234 {
db3927fb 10235 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10236 goto return_build_binary_op;
10237 }
3e4093b6
RS
10238 else
10239 common = 1;
10240 break;
8b6a5902 10241
3e4093b6
RS
10242 case MULT_EXPR:
10243 common = 1;
10244 break;
10245
10246 case TRUNC_DIV_EXPR:
10247 case CEIL_DIV_EXPR:
10248 case FLOOR_DIV_EXPR:
10249 case ROUND_DIV_EXPR:
10250 case EXACT_DIV_EXPR:
de5a5fa1 10251 doing_div_or_mod = true;
c9f9eb5d 10252 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10253
10254 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10255 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10256 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10257 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10258 || code1 == FIXED_POINT_TYPE
3e4093b6 10259 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10260 {
5bed876a
AH
10261 enum tree_code tcode0 = code0, tcode1 = code1;
10262
3a021db2 10263 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10264 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10265 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10266 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10267
ab22c1fa
CF
10268 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10269 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10270 resultcode = RDIV_EXPR;
10271 else
10272 /* Although it would be tempting to shorten always here, that
10273 loses on some targets, since the modulo instruction is
10274 undefined if the quotient can't be represented in the
10275 computation mode. We shorten only if unsigned or if
10276 dividing by something we know != -1. */
8df83eae 10277 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10278 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10279 && !integer_all_onesp (op1)));
3e4093b6
RS
10280 common = 1;
10281 }
10282 break;
de520661 10283
3e4093b6 10284 case BIT_AND_EXPR:
3e4093b6
RS
10285 case BIT_IOR_EXPR:
10286 case BIT_XOR_EXPR:
10287 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10288 shorten = -1;
9ef0c8d9
AP
10289 /* Allow vector types which are not floating point types. */
10290 else if (code0 == VECTOR_TYPE
10291 && code1 == VECTOR_TYPE
10292 && !VECTOR_FLOAT_TYPE_P (type0)
10293 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10294 common = 1;
10295 break;
10296
10297 case TRUNC_MOD_EXPR:
10298 case FLOOR_MOD_EXPR:
de5a5fa1 10299 doing_div_or_mod = true;
c9f9eb5d 10300 warn_for_div_by_zero (location, op1);
de520661 10301
5cfd5d9b
AP
10302 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10303 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10304 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10305 common = 1;
10306 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10307 {
10308 /* Although it would be tempting to shorten always here, that loses
10309 on some targets, since the modulo instruction is undefined if the
10310 quotient can't be represented in the computation mode. We shorten
10311 only if unsigned or if dividing by something we know != -1. */
8df83eae 10312 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10313 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10314 && !integer_all_onesp (op1)));
3e4093b6
RS
10315 common = 1;
10316 }
10317 break;
de520661 10318
3e4093b6
RS
10319 case TRUTH_ANDIF_EXPR:
10320 case TRUTH_ORIF_EXPR:
10321 case TRUTH_AND_EXPR:
10322 case TRUTH_OR_EXPR:
10323 case TRUTH_XOR_EXPR:
10324 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10325 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10326 || code0 == FIXED_POINT_TYPE)
3e4093b6 10327 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10328 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10329 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10330 {
10331 /* Result of these operations is always an int,
10332 but that does not mean the operands should be
10333 converted to ints! */
10334 result_type = integer_type_node;
a27d595d
JM
10335 if (op0_int_operands)
10336 {
10337 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10338 op0 = remove_c_maybe_const_expr (op0);
10339 }
10340 else
10341 op0 = c_objc_common_truthvalue_conversion (location, op0);
10342 if (op1_int_operands)
10343 {
10344 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10345 op1 = remove_c_maybe_const_expr (op1);
10346 }
10347 else
10348 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10349 converted = 1;
180f8dbb 10350 boolean_op = true;
3e4093b6 10351 }
928c19bb
JM
10352 if (code == TRUTH_ANDIF_EXPR)
10353 {
10354 int_const_or_overflow = (int_operands
10355 && TREE_CODE (orig_op0) == INTEGER_CST
10356 && (op0 == truthvalue_false_node
10357 || TREE_CODE (orig_op1) == INTEGER_CST));
10358 int_const = (int_const_or_overflow
10359 && !TREE_OVERFLOW (orig_op0)
10360 && (op0 == truthvalue_false_node
10361 || !TREE_OVERFLOW (orig_op1)));
10362 }
10363 else if (code == TRUTH_ORIF_EXPR)
10364 {
10365 int_const_or_overflow = (int_operands
10366 && TREE_CODE (orig_op0) == INTEGER_CST
10367 && (op0 == truthvalue_true_node
10368 || TREE_CODE (orig_op1) == INTEGER_CST));
10369 int_const = (int_const_or_overflow
10370 && !TREE_OVERFLOW (orig_op0)
10371 && (op0 == truthvalue_true_node
10372 || !TREE_OVERFLOW (orig_op1)));
10373 }
3e4093b6 10374 break;
eba80994 10375
3e4093b6
RS
10376 /* Shift operations: result has same type as first operand;
10377 always convert second operand to int.
10378 Also set SHORT_SHIFT if shifting rightward. */
de520661 10379
3e4093b6 10380 case RSHIFT_EXPR:
f87bd04b
AS
10381 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10382 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10383 {
10384 result_type = type0;
10385 converted = 1;
10386 }
10387 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10388 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10389 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10390 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10391 {
10392 result_type = type0;
10393 converted = 1;
10394 }
10395 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10396 && code1 == INTEGER_TYPE)
3e4093b6 10397 {
de5a5fa1 10398 doing_shift = true;
928c19bb 10399 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10400 {
3e4093b6 10401 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10402 {
10403 int_const = false;
7d882b83 10404 if (c_inhibit_evaluation_warnings == 0)
c4bdc42f 10405 warning_at (location, 0, "right shift count is negative");
928c19bb 10406 }
3e4093b6 10407 else
bbb818c6 10408 {
3f75a254 10409 if (!integer_zerop (op1))
3e4093b6
RS
10410 short_shift = 1;
10411
10412 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10413 {
10414 int_const = false;
7d882b83 10415 if (c_inhibit_evaluation_warnings == 0)
c4bdc42f
MP
10416 warning_at (location, 0, "right shift count >= width "
10417 "of type");
928c19bb 10418 }
bbb818c6 10419 }
b62acd60 10420 }
de520661 10421
3e4093b6
RS
10422 /* Use the type of the value to be shifted. */
10423 result_type = type0;
f87bd04b
AS
10424 /* Convert the non vector shift-count to an integer, regardless
10425 of size of value being shifted. */
10426 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10427 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10428 op1 = convert (integer_type_node, op1);
10429 /* Avoid converting op1 to result_type later. */
10430 converted = 1;
400fbf9f 10431 }
3e4093b6 10432 break;
253b6b82 10433
3e4093b6 10434 case LSHIFT_EXPR:
f87bd04b
AS
10435 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10436 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10437 {
10438 result_type = type0;
10439 converted = 1;
10440 }
10441 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10442 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10443 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10444 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10445 {
10446 result_type = type0;
10447 converted = 1;
10448 }
10449 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10450 && code1 == INTEGER_TYPE)
3e4093b6 10451 {
de5a5fa1 10452 doing_shift = true;
928c19bb 10453 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10454 {
3e4093b6 10455 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10456 {
10457 int_const = false;
7d882b83 10458 if (c_inhibit_evaluation_warnings == 0)
c4bdc42f 10459 warning_at (location, 0, "left shift count is negative");
928c19bb 10460 }
de520661 10461
3e4093b6 10462 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10463 {
10464 int_const = false;
7d882b83 10465 if (c_inhibit_evaluation_warnings == 0)
c4bdc42f
MP
10466 warning_at (location, 0, "left shift count >= width of "
10467 "type");
928c19bb 10468 }
94ba5069 10469 }
de520661 10470
3e4093b6
RS
10471 /* Use the type of the value to be shifted. */
10472 result_type = type0;
f87bd04b
AS
10473 /* Convert the non vector shift-count to an integer, regardless
10474 of size of value being shifted. */
10475 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10476 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10477 op1 = convert (integer_type_node, op1);
10478 /* Avoid converting op1 to result_type later. */
10479 converted = 1;
400fbf9f 10480 }
3e4093b6 10481 break;
de520661 10482
3e4093b6
RS
10483 case EQ_EXPR:
10484 case NE_EXPR:
d246ab4f
AS
10485 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10486 {
10487 tree intt;
0af94e6f 10488 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10489 {
10490 error_at (location, "comparing vectors with different "
10491 "element types");
10492 return error_mark_node;
10493 }
10494
10495 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10496 {
10497 error_at (location, "comparing vectors with different "
10498 "number of elements");
10499 return error_mark_node;
10500 }
10501
10502 /* Always construct signed integer vector type. */
10503 intt = c_common_type_for_size (GET_MODE_BITSIZE
10504 (TYPE_MODE (TREE_TYPE (type0))), 0);
10505 result_type = build_opaque_vector_type (intt,
10506 TYPE_VECTOR_SUBPARTS (type0));
10507 converted = 1;
10508 break;
10509 }
ae311566 10510 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10511 warning_at (location,
10512 OPT_Wfloat_equal,
10513 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10514 /* Result of comparison is always int,
10515 but don't convert the args to int! */
10516 build_type = integer_type_node;
10517 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10518 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10519 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10520 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10521 short_compare = 1;
637f1455
SZ
10522 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10523 {
10524 if (TREE_CODE (op0) == ADDR_EXPR
10525 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10526 {
10527 if (code == EQ_EXPR)
10528 warning_at (location,
10529 OPT_Waddress,
10530 "the comparison will always evaluate as %<false%> "
10531 "for the address of %qD will never be NULL",
10532 TREE_OPERAND (op0, 0));
10533 else
10534 warning_at (location,
10535 OPT_Waddress,
10536 "the comparison will always evaluate as %<true%> "
10537 "for the address of %qD will never be NULL",
10538 TREE_OPERAND (op0, 0));
10539 }
10540 result_type = type0;
10541 }
10542 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10543 {
10544 if (TREE_CODE (op1) == ADDR_EXPR
10545 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10546 {
10547 if (code == EQ_EXPR)
10548 warning_at (location,
f90e8e2e 10549 OPT_Waddress,
637f1455
SZ
10550 "the comparison will always evaluate as %<false%> "
10551 "for the address of %qD will never be NULL",
10552 TREE_OPERAND (op1, 0));
10553 else
10554 warning_at (location,
10555 OPT_Waddress,
10556 "the comparison will always evaluate as %<true%> "
10557 "for the address of %qD will never be NULL",
10558 TREE_OPERAND (op1, 0));
10559 }
10560 result_type = type1;
10561 }
3e4093b6
RS
10562 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10563 {
10564 tree tt0 = TREE_TYPE (type0);
10565 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10566 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10567 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10568 addr_space_t as_common = ADDR_SPACE_GENERIC;
10569
3e4093b6
RS
10570 /* Anything compares with void *. void * compares with anything.
10571 Otherwise, the targets must be compatible
10572 and both must be object or both incomplete. */
744aa42f 10573 if (comp_target_types (location, type0, type1))
10bc1b1b 10574 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10575 else if (!addr_space_superset (as0, as1, &as_common))
10576 {
10577 error_at (location, "comparison of pointers to "
10578 "disjoint address spaces");
10579 return error_mark_node;
10580 }
267bac10 10581 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10582 {
36c5e70a 10583 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10584 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10585 "comparison of %<void *%> with function pointer");
ee2990e7 10586 }
267bac10 10587 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10588 {
36c5e70a 10589 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10590 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10591 "comparison of %<void *%> with function pointer");
e6834654 10592 }
3e4093b6 10593 else
58393038
ZL
10594 /* Avoid warning about the volatile ObjC EH puts on decls. */
10595 if (!objc_ok)
ba47d38d 10596 pedwarn (location, 0,
509c9d60 10597 "comparison of distinct pointer types lacks a cast");
e6834654 10598
3e4093b6 10599 if (result_type == NULL_TREE)
36c5e70a
BE
10600 {
10601 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10602 result_type = build_pointer_type
10603 (build_qualified_type (void_type_node, qual));
10604 }
e6834654 10605 }
3e4093b6 10606 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10607 {
3e4093b6 10608 result_type = type0;
ba47d38d 10609 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10610 }
3e4093b6 10611 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10612 {
3e4093b6 10613 result_type = type1;
ba47d38d 10614 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10615 }
3e4093b6 10616 break;
8b6a5902 10617
3e4093b6
RS
10618 case LE_EXPR:
10619 case GE_EXPR:
10620 case LT_EXPR:
10621 case GT_EXPR:
d246ab4f
AS
10622 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10623 {
10624 tree intt;
0af94e6f 10625 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10626 {
10627 error_at (location, "comparing vectors with different "
10628 "element types");
10629 return error_mark_node;
10630 }
10631
10632 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10633 {
10634 error_at (location, "comparing vectors with different "
10635 "number of elements");
10636 return error_mark_node;
10637 }
10638
10639 /* Always construct signed integer vector type. */
10640 intt = c_common_type_for_size (GET_MODE_BITSIZE
10641 (TYPE_MODE (TREE_TYPE (type0))), 0);
10642 result_type = build_opaque_vector_type (intt,
10643 TYPE_VECTOR_SUBPARTS (type0));
10644 converted = 1;
10645 break;
10646 }
3e4093b6 10647 build_type = integer_type_node;
ab22c1fa
CF
10648 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10649 || code0 == FIXED_POINT_TYPE)
10650 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10651 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10652 short_compare = 1;
10653 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10654 {
36c5e70a
BE
10655 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10656 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10657 addr_space_t as_common;
10658
744aa42f 10659 if (comp_target_types (location, type0, type1))
3e4093b6 10660 {
10bc1b1b 10661 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10662 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10663 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10664 pedwarn (location, 0,
509c9d60 10665 "comparison of complete and incomplete pointers");
fcf73884 10666 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10667 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10668 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10669 else if (null_pointer_constant_p (orig_op0)
10670 || null_pointer_constant_p (orig_op1))
10671 warning_at (location, OPT_Wextra,
10672 "ordered comparison of pointer with null pointer");
10673
3e4093b6 10674 }
36c5e70a
BE
10675 else if (!addr_space_superset (as0, as1, &as_common))
10676 {
10677 error_at (location, "comparison of pointers to "
10678 "disjoint address spaces");
10679 return error_mark_node;
10680 }
3e4093b6
RS
10681 else
10682 {
36c5e70a
BE
10683 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10684 result_type = build_pointer_type
10685 (build_qualified_type (void_type_node, qual));
ba47d38d 10686 pedwarn (location, 0,
509c9d60 10687 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10688 }
10689 }
6aa3c60d 10690 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10691 {
10692 result_type = type0;
fcf73884 10693 if (pedantic)
c1771a20 10694 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10695 "ordered comparison of pointer with integer zero");
10696 else if (extra_warnings)
ba47d38d 10697 warning_at (location, OPT_Wextra,
d42ba3b8 10698 "ordered comparison of pointer with integer zero");
3e4093b6 10699 }
6aa3c60d 10700 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10701 {
10702 result_type = type1;
d42ba3b8 10703 if (pedantic)
c1771a20 10704 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10705 "ordered comparison of pointer with integer zero");
10706 else if (extra_warnings)
10707 warning_at (location, OPT_Wextra,
10708 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10709 }
10710 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10711 {
10712 result_type = type0;
ba47d38d 10713 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10714 }
10715 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10716 {
10717 result_type = type1;
ba47d38d 10718 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10719 }
10720 break;
64094f6a 10721
3e4093b6 10722 default:
37b2f290 10723 gcc_unreachable ();
c9fe6f9f 10724 }
8f17b5c5 10725
e57e265b
PB
10726 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10727 return error_mark_node;
10728
5bed876a
AH
10729 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10730 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10731 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10732 {
ba47d38d 10733 binary_op_error (location, code, type0, type1);
5bed876a
AH
10734 return error_mark_node;
10735 }
10736
3e4093b6 10737 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10738 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10739 &&
10740 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10741 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10742 {
2ca862e9
JM
10743 bool first_complex = (code0 == COMPLEX_TYPE);
10744 bool second_complex = (code1 == COMPLEX_TYPE);
10745 int none_complex = (!first_complex && !second_complex);
39b726dd 10746
3e4093b6 10747 if (shorten || common || short_compare)
3bf6bfcc
JJ
10748 {
10749 result_type = c_common_type (type0, type1);
0a0b3574
MM
10750 do_warn_double_promotion (result_type, type0, type1,
10751 "implicit conversion from %qT to %qT "
10752 "to match other operand of binary "
10753 "expression",
10754 location);
3bf6bfcc
JJ
10755 if (result_type == error_mark_node)
10756 return error_mark_node;
10757 }
400fbf9f 10758
2ca862e9
JM
10759 if (first_complex != second_complex
10760 && (code == PLUS_EXPR
10761 || code == MINUS_EXPR
10762 || code == MULT_EXPR
10763 || (code == TRUNC_DIV_EXPR && first_complex))
10764 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10765 && flag_signed_zeros)
10766 {
10767 /* An operation on mixed real/complex operands must be
10768 handled specially, but the language-independent code can
10769 more easily optimize the plain complex arithmetic if
10770 -fno-signed-zeros. */
10771 tree real_type = TREE_TYPE (result_type);
10772 tree real, imag;
10773 if (type0 != orig_type0 || type1 != orig_type1)
10774 {
10775 gcc_assert (may_need_excess_precision && common);
2d2e923f 10776 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
10777 }
10778 if (first_complex)
10779 {
10780 if (TREE_TYPE (op0) != result_type)
68fca595 10781 op0 = convert_and_check (location, result_type, op0);
2ca862e9 10782 if (TREE_TYPE (op1) != real_type)
68fca595 10783 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
10784 }
10785 else
10786 {
10787 if (TREE_TYPE (op0) != real_type)
68fca595 10788 op0 = convert_and_check (location, real_type, op0);
2ca862e9 10789 if (TREE_TYPE (op1) != result_type)
68fca595 10790 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
10791 }
10792 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10793 return error_mark_node;
10794 if (first_complex)
10795 {
10796 op0 = c_save_expr (op0);
10797 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10798 op0, 1);
10799 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10800 op0, 1);
10801 switch (code)
10802 {
10803 case MULT_EXPR:
10804 case TRUNC_DIV_EXPR:
c4603e7c 10805 op1 = c_save_expr (op1);
2ca862e9
JM
10806 imag = build2 (resultcode, real_type, imag, op1);
10807 /* Fall through. */
10808 case PLUS_EXPR:
10809 case MINUS_EXPR:
10810 real = build2 (resultcode, real_type, real, op1);
10811 break;
10812 default:
10813 gcc_unreachable();
10814 }
10815 }
10816 else
10817 {
10818 op1 = c_save_expr (op1);
10819 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10820 op1, 1);
10821 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10822 op1, 1);
10823 switch (code)
10824 {
10825 case MULT_EXPR:
c4603e7c 10826 op0 = c_save_expr (op0);
2ca862e9
JM
10827 imag = build2 (resultcode, real_type, op0, imag);
10828 /* Fall through. */
10829 case PLUS_EXPR:
10830 real = build2 (resultcode, real_type, op0, real);
10831 break;
10832 case MINUS_EXPR:
10833 real = build2 (resultcode, real_type, op0, real);
10834 imag = build1 (NEGATE_EXPR, real_type, imag);
10835 break;
10836 default:
10837 gcc_unreachable();
10838 }
10839 }
10840 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10841 goto return_build_binary_op;
10842 }
10843
3e4093b6
RS
10844 /* For certain operations (which identify themselves by shorten != 0)
10845 if both args were extended from the same smaller type,
10846 do the arithmetic in that type and then extend.
400fbf9f 10847
3e4093b6
RS
10848 shorten !=0 and !=1 indicates a bitwise operation.
10849 For them, this optimization is safe only if
10850 both args are zero-extended or both are sign-extended.
10851 Otherwise, we might change the result.
10852 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10853 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 10854
3e4093b6
RS
10855 if (shorten && none_complex)
10856 {
3e4093b6 10857 final_type = result_type;
b8698a0f 10858 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 10859 shorten == -1);
3e4093b6 10860 }
88a3dbc1 10861
3e4093b6 10862 /* Shifts can be shortened if shifting right. */
2f6e4e97 10863
3e4093b6
RS
10864 if (short_shift)
10865 {
10866 int unsigned_arg;
10867 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 10868
3e4093b6 10869 final_type = result_type;
abe80e6d 10870
3e4093b6 10871 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 10872 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 10873
3e4093b6 10874 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 10875 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
10876 /* We can shorten only if the shift count is less than the
10877 number of bits in the smaller type size. */
10878 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10879 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 10880 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
10881 {
10882 /* Do an unsigned shift if the operand was zero-extended. */
10883 result_type
10884 = c_common_signed_or_unsigned_type (unsigned_arg,
10885 TREE_TYPE (arg0));
10886 /* Convert value-to-be-shifted to that type. */
10887 if (TREE_TYPE (op0) != result_type)
10888 op0 = convert (result_type, op0);
10889 converted = 1;
abe80e6d 10890 }
88a3dbc1
RK
10891 }
10892
3e4093b6
RS
10893 /* Comparison operations are shortened too but differently.
10894 They identify themselves by setting short_compare = 1. */
56cb9733 10895
3e4093b6
RS
10896 if (short_compare)
10897 {
10898 /* Don't write &op0, etc., because that would prevent op0
10899 from being kept in a register.
10900 Instead, make copies of the our local variables and
10901 pass the copies by reference, then copy them back afterward. */
10902 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10903 enum tree_code xresultcode = resultcode;
10904 tree val
393e8e8b
MP
10905 = shorten_compare (location, &xop0, &xop1, &xresult_type,
10906 &xresultcode);
8f17b5c5 10907
3e4093b6 10908 if (val != 0)
c9f9eb5d
AH
10909 {
10910 ret = val;
10911 goto return_build_binary_op;
10912 }
8f17b5c5 10913
3e4093b6
RS
10914 op0 = xop0, op1 = xop1;
10915 converted = 1;
10916 resultcode = xresultcode;
8f17b5c5 10917
7d882b83 10918 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10919 {
10920 bool op0_maybe_const = true;
10921 bool op1_maybe_const = true;
10922 tree orig_op0_folded, orig_op1_folded;
10923
10924 if (in_late_binary_op)
10925 {
10926 orig_op0_folded = orig_op0;
10927 orig_op1_folded = orig_op1;
10928 }
10929 else
10930 {
10931 /* Fold for the sake of possible warnings, as in
10932 build_conditional_expr. This requires the
10933 "original" values to be folded, not just op0 and
10934 op1. */
f5178456 10935 c_inhibit_evaluation_warnings++;
928c19bb
JM
10936 op0 = c_fully_fold (op0, require_constant_value,
10937 &op0_maybe_const);
10938 op1 = c_fully_fold (op1, require_constant_value,
10939 &op1_maybe_const);
f5178456 10940 c_inhibit_evaluation_warnings--;
928c19bb
JM
10941 orig_op0_folded = c_fully_fold (orig_op0,
10942 require_constant_value,
10943 NULL);
10944 orig_op1_folded = c_fully_fold (orig_op1,
10945 require_constant_value,
10946 NULL);
10947 }
10948
10949 if (warn_sign_compare)
10950 warn_for_sign_compare (location, orig_op0_folded,
10951 orig_op1_folded, op0, op1,
10952 result_type, resultcode);
5c2f94b4 10953 if (!in_late_binary_op && !int_operands)
928c19bb
JM
10954 {
10955 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 10956 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 10957 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 10958 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 10959 }
3e4093b6 10960 }
2ad1815d 10961 }
64094f6a 10962 }
64094f6a 10963
3e4093b6
RS
10964 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10965 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10966 Then the expression will be built.
10967 It will be given type FINAL_TYPE if that is nonzero;
10968 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 10969
3e4093b6
RS
10970 if (!result_type)
10971 {
ba47d38d 10972 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
10973 return error_mark_node;
10974 }
400fbf9f 10975
3e4093b6 10976 if (build_type == NULL_TREE)
8ce94e44
JM
10977 {
10978 build_type = result_type;
180f8dbb
JM
10979 if ((type0 != orig_type0 || type1 != orig_type1)
10980 && !boolean_op)
8ce94e44
JM
10981 {
10982 gcc_assert (may_need_excess_precision && common);
2d2e923f 10983 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
10984 }
10985 }
400fbf9f 10986
2d2e923f
MLI
10987 if (!converted)
10988 {
68fca595
MP
10989 op0 = ep_convert_and_check (location, result_type, op0,
10990 semantic_result_type);
10991 op1 = ep_convert_and_check (location, result_type, op1,
10992 semantic_result_type);
2d2e923f
MLI
10993
10994 /* This can happen if one operand has a vector type, and the other
10995 has a different type. */
10996 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10997 return error_mark_node;
10998 }
10999
a24d975c 11000 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
de5a5fa1 11001 && current_function_decl != 0
ce6923c5
MP
11002 && !lookup_attribute ("no_sanitize_undefined",
11003 DECL_ATTRIBUTES (current_function_decl))
de5a5fa1
MP
11004 && (doing_div_or_mod || doing_shift))
11005 {
11006 /* OP0 and/or OP1 might have side-effects. */
11007 op0 = c_save_expr (op0);
11008 op1 = c_save_expr (op1);
11009 op0 = c_fully_fold (op0, false, NULL);
11010 op1 = c_fully_fold (op1, false, NULL);
a24d975c 11011 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
de5a5fa1 11012 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11013 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11014 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11015 }
11016
c9f9eb5d 11017 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11018 if (int_const_or_overflow)
11019 ret = (require_constant_value
db3927fb
AH
11020 ? fold_build2_initializer_loc (location, resultcode, build_type,
11021 op0, op1)
11022 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11023 else
11024 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11025 if (final_type != 0)
11026 ret = convert (final_type, ret);
11027
11028 return_build_binary_op:
11029 gcc_assert (ret != error_mark_node);
928c19bb
JM
11030 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11031 ret = (int_operands
11032 ? note_integer_operands (ret)
11033 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11034 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11035 && !in_late_binary_op)
11036 ret = note_integer_operands (ret);
2d2e923f
MLI
11037 if (semantic_result_type)
11038 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11039 protected_set_expr_location (ret, location);
de5a5fa1 11040
a24d975c 11041 if (instrument_expr != NULL)
de5a5fa1
MP
11042 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11043 instrument_expr, ret);
11044
c9f9eb5d 11045 return ret;
400fbf9f 11046}
85498824
JM
11047
11048
11049/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11050 purpose. LOCATION is the source location for the expression. */
85498824
JM
11051
11052tree
ba47d38d 11053c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11054{
928c19bb
JM
11055 bool int_const, int_operands;
11056
85498824
JM
11057 switch (TREE_CODE (TREE_TYPE (expr)))
11058 {
11059 case ARRAY_TYPE:
ba47d38d 11060 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11061 return error_mark_node;
11062
11063 case RECORD_TYPE:
ba47d38d 11064 error_at (location, "used struct type value where scalar is required");
85498824
JM
11065 return error_mark_node;
11066
11067 case UNION_TYPE:
ba47d38d 11068 error_at (location, "used union type value where scalar is required");
85498824
JM
11069 return error_mark_node;
11070
04af8788
NP
11071 case VOID_TYPE:
11072 error_at (location, "void value not ignored as it ought to be");
11073 return error_mark_node;
11074
46bdb9cf
JM
11075 case FUNCTION_TYPE:
11076 gcc_unreachable ();
11077
d246ab4f
AS
11078 case VECTOR_TYPE:
11079 error_at (location, "used vector type where scalar is required");
11080 return error_mark_node;
11081
85498824
JM
11082 default:
11083 break;
11084 }
11085
928c19bb
JM
11086 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11087 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11088 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11089 {
11090 expr = remove_c_maybe_const_expr (expr);
11091 expr = build2 (NE_EXPR, integer_type_node, expr,
11092 convert (TREE_TYPE (expr), integer_zero_node));
11093 expr = note_integer_operands (expr);
11094 }
11095 else
11096 /* ??? Should we also give an error for vectors rather than leaving
11097 those to give errors later? */
11098 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11099
11100 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11101 {
11102 if (TREE_OVERFLOW (expr))
11103 return expr;
11104 else
11105 return note_integer_operands (expr);
11106 }
11107 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11108 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11109 return expr;
85498824 11110}
73f397d4
JM
11111\f
11112
11113/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11114 required. */
11115
11116tree
51eed280 11117c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11118{
11119 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11120 {
11121 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11122 /* Executing a compound literal inside a function reinitializes
11123 it. */
11124 if (!TREE_STATIC (decl))
11125 *se = true;
11126 return decl;
11127 }
11128 else
11129 return expr;
11130}
953ff289 11131\f
c0220ea4 11132/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11133
11134tree
11135c_begin_omp_parallel (void)
11136{
11137 tree block;
11138
11139 keep_next_level ();
11140 block = c_begin_compound_stmt (true);
11141
11142 return block;
11143}
11144
c2255bc4
AH
11145/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11146 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11147
953ff289 11148tree
c2255bc4 11149c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11150{
11151 tree stmt;
11152
c2255bc4 11153 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11154
11155 stmt = make_node (OMP_PARALLEL);
11156 TREE_TYPE (stmt) = void_type_node;
11157 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11158 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11159 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11160
11161 return add_stmt (stmt);
11162}
11163
a68ab351
JJ
11164/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11165
11166tree
11167c_begin_omp_task (void)
11168{
11169 tree block;
11170
11171 keep_next_level ();
11172 block = c_begin_compound_stmt (true);
11173
11174 return block;
11175}
11176
c2255bc4
AH
11177/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11178 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11179
11180tree
c2255bc4 11181c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11182{
11183 tree stmt;
11184
c2255bc4 11185 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11186
11187 stmt = make_node (OMP_TASK);
11188 TREE_TYPE (stmt) = void_type_node;
11189 OMP_TASK_CLAUSES (stmt) = clauses;
11190 OMP_TASK_BODY (stmt) = block;
c2255bc4 11191 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11192
11193 return add_stmt (stmt);
11194}
11195
acf0174b
JJ
11196/* Generate GOMP_cancel call for #pragma omp cancel. */
11197
11198void
11199c_finish_omp_cancel (location_t loc, tree clauses)
11200{
11201 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11202 int mask = 0;
11203 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11204 mask = 1;
11205 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11206 mask = 2;
11207 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11208 mask = 4;
11209 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11210 mask = 8;
11211 else
11212 {
11213 error_at (loc, "%<#pragma omp cancel must specify one of "
11214 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11215 "clauses");
11216 return;
11217 }
11218 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11219 if (ifc != NULL_TREE)
11220 {
11221 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11222 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11223 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11224 build_zero_cst (type));
11225 }
11226 else
11227 ifc = boolean_true_node;
11228 tree stmt = build_call_expr_loc (loc, fn, 2,
11229 build_int_cst (integer_type_node, mask),
11230 ifc);
11231 add_stmt (stmt);
11232}
11233
11234/* Generate GOMP_cancellation_point call for
11235 #pragma omp cancellation point. */
11236
11237void
11238c_finish_omp_cancellation_point (location_t loc, tree clauses)
11239{
11240 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11241 int mask = 0;
11242 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11243 mask = 1;
11244 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11245 mask = 2;
11246 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11247 mask = 4;
11248 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11249 mask = 8;
11250 else
11251 {
11252 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11253 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11254 "clauses");
11255 return;
11256 }
11257 tree stmt = build_call_expr_loc (loc, fn, 1,
11258 build_int_cst (integer_type_node, mask));
11259 add_stmt (stmt);
11260}
11261
11262/* Helper function for handle_omp_array_sections. Called recursively
11263 to handle multiple array-section-subscripts. C is the clause,
11264 T current expression (initially OMP_CLAUSE_DECL), which is either
11265 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11266 expression if specified, TREE_VALUE length expression if specified,
11267 TREE_CHAIN is what it has been specified after, or some decl.
11268 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11269 set to true if any of the array-section-subscript could have length
11270 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11271 first array-section-subscript which is known not to have length
11272 of one. Given say:
11273 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11274 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11275 all are or may have length of 1, array-section-subscript [:2] is the
11276 first one knonwn not to have length 1. For array-section-subscript
11277 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11278 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11279 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11280 case though, as some lengths could be zero. */
11281
11282static tree
11283handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11284 bool &maybe_zero_len, unsigned int &first_non_one)
11285{
11286 tree ret, low_bound, length, type;
11287 if (TREE_CODE (t) != TREE_LIST)
11288 {
11289 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11290 return error_mark_node;
11291 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11292 {
11293 if (DECL_P (t))
11294 error_at (OMP_CLAUSE_LOCATION (c),
11295 "%qD is not a variable in %qs clause", t,
11296 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11297 else
11298 error_at (OMP_CLAUSE_LOCATION (c),
11299 "%qE is not a variable in %qs clause", t,
11300 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11301 return error_mark_node;
11302 }
11303 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11304 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11305 {
11306 error_at (OMP_CLAUSE_LOCATION (c),
11307 "%qD is threadprivate variable in %qs clause", t,
11308 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11309 return error_mark_node;
11310 }
11311 return t;
11312 }
11313
11314 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11315 maybe_zero_len, first_non_one);
11316 if (ret == error_mark_node || ret == NULL_TREE)
11317 return ret;
11318
11319 type = TREE_TYPE (ret);
11320 low_bound = TREE_PURPOSE (t);
11321 length = TREE_VALUE (t);
11322
11323 if (low_bound == error_mark_node || length == error_mark_node)
11324 return error_mark_node;
11325
11326 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11327 {
11328 error_at (OMP_CLAUSE_LOCATION (c),
11329 "low bound %qE of array section does not have integral type",
11330 low_bound);
11331 return error_mark_node;
11332 }
11333 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11334 {
11335 error_at (OMP_CLAUSE_LOCATION (c),
11336 "length %qE of array section does not have integral type",
11337 length);
11338 return error_mark_node;
11339 }
11340 if (low_bound
11341 && TREE_CODE (low_bound) == INTEGER_CST
11342 && TYPE_PRECISION (TREE_TYPE (low_bound))
11343 > TYPE_PRECISION (sizetype))
11344 low_bound = fold_convert (sizetype, low_bound);
11345 if (length
11346 && TREE_CODE (length) == INTEGER_CST
11347 && TYPE_PRECISION (TREE_TYPE (length))
11348 > TYPE_PRECISION (sizetype))
11349 length = fold_convert (sizetype, length);
11350 if (low_bound == NULL_TREE)
11351 low_bound = integer_zero_node;
11352
11353 if (length != NULL_TREE)
11354 {
11355 if (!integer_nonzerop (length))
11356 maybe_zero_len = true;
11357 if (first_non_one == types.length ()
11358 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11359 first_non_one++;
11360 }
11361 if (TREE_CODE (type) == ARRAY_TYPE)
11362 {
11363 if (length == NULL_TREE
11364 && (TYPE_DOMAIN (type) == NULL_TREE
11365 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11366 {
11367 error_at (OMP_CLAUSE_LOCATION (c),
11368 "for unknown bound array type length expression must "
11369 "be specified");
11370 return error_mark_node;
11371 }
11372 if (TREE_CODE (low_bound) == INTEGER_CST
11373 && tree_int_cst_sgn (low_bound) == -1)
11374 {
11375 error_at (OMP_CLAUSE_LOCATION (c),
11376 "negative low bound in array section in %qs clause",
11377 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11378 return error_mark_node;
11379 }
11380 if (length != NULL_TREE
11381 && TREE_CODE (length) == INTEGER_CST
11382 && tree_int_cst_sgn (length) == -1)
11383 {
11384 error_at (OMP_CLAUSE_LOCATION (c),
11385 "negative length in array section in %qs clause",
11386 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11387 return error_mark_node;
11388 }
11389 if (TYPE_DOMAIN (type)
11390 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11391 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11392 == INTEGER_CST)
11393 {
11394 tree size = size_binop (PLUS_EXPR,
11395 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11396 size_one_node);
11397 if (TREE_CODE (low_bound) == INTEGER_CST)
11398 {
11399 if (tree_int_cst_lt (size, low_bound))
11400 {
11401 error_at (OMP_CLAUSE_LOCATION (c),
11402 "low bound %qE above array section size "
11403 "in %qs clause", low_bound,
11404 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11405 return error_mark_node;
11406 }
11407 if (tree_int_cst_equal (size, low_bound))
11408 maybe_zero_len = true;
11409 else if (length == NULL_TREE
11410 && first_non_one == types.length ()
11411 && tree_int_cst_equal
11412 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11413 low_bound))
11414 first_non_one++;
11415 }
11416 else if (length == NULL_TREE)
11417 {
11418 maybe_zero_len = true;
11419 if (first_non_one == types.length ())
11420 first_non_one++;
11421 }
11422 if (length && TREE_CODE (length) == INTEGER_CST)
11423 {
11424 if (tree_int_cst_lt (size, length))
11425 {
11426 error_at (OMP_CLAUSE_LOCATION (c),
11427 "length %qE above array section size "
11428 "in %qs clause", length,
11429 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11430 return error_mark_node;
11431 }
11432 if (TREE_CODE (low_bound) == INTEGER_CST)
11433 {
11434 tree lbpluslen
11435 = size_binop (PLUS_EXPR,
11436 fold_convert (sizetype, low_bound),
11437 fold_convert (sizetype, length));
11438 if (TREE_CODE (lbpluslen) == INTEGER_CST
11439 && tree_int_cst_lt (size, lbpluslen))
11440 {
11441 error_at (OMP_CLAUSE_LOCATION (c),
11442 "high bound %qE above array section size "
11443 "in %qs clause", lbpluslen,
11444 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11445 return error_mark_node;
11446 }
11447 }
11448 }
11449 }
11450 else if (length == NULL_TREE)
11451 {
11452 maybe_zero_len = true;
11453 if (first_non_one == types.length ())
11454 first_non_one++;
11455 }
11456
11457 /* For [lb:] we will need to evaluate lb more than once. */
11458 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11459 {
11460 tree lb = c_save_expr (low_bound);
11461 if (lb != low_bound)
11462 {
11463 TREE_PURPOSE (t) = lb;
11464 low_bound = lb;
11465 }
11466 }
11467 }
11468 else if (TREE_CODE (type) == POINTER_TYPE)
11469 {
11470 if (length == NULL_TREE)
11471 {
11472 error_at (OMP_CLAUSE_LOCATION (c),
11473 "for pointer type length expression must be specified");
11474 return error_mark_node;
11475 }
11476 /* If there is a pointer type anywhere but in the very first
11477 array-section-subscript, the array section can't be contiguous. */
11478 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11479 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11480 {
11481 error_at (OMP_CLAUSE_LOCATION (c),
11482 "array section is not contiguous in %qs clause",
11483 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11484 return error_mark_node;
11485 }
11486 }
11487 else
11488 {
11489 error_at (OMP_CLAUSE_LOCATION (c),
11490 "%qE does not have pointer or array type", ret);
11491 return error_mark_node;
11492 }
11493 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11494 types.safe_push (TREE_TYPE (ret));
11495 /* We will need to evaluate lb more than once. */
11496 tree lb = c_save_expr (low_bound);
11497 if (lb != low_bound)
11498 {
11499 TREE_PURPOSE (t) = lb;
11500 low_bound = lb;
11501 }
11502 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11503 return ret;
11504}
11505
11506/* Handle array sections for clause C. */
11507
11508static bool
11509handle_omp_array_sections (tree c)
11510{
11511 bool maybe_zero_len = false;
11512 unsigned int first_non_one = 0;
11513 vec<tree> types = vNULL;
11514 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11515 maybe_zero_len, first_non_one);
11516 if (first == error_mark_node)
11517 {
11518 types.release ();
11519 return true;
11520 }
11521 if (first == NULL_TREE)
11522 {
11523 types.release ();
11524 return false;
11525 }
11526 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11527 {
11528 tree t = OMP_CLAUSE_DECL (c);
11529 tree tem = NULL_TREE;
11530 types.release ();
11531 /* Need to evaluate side effects in the length expressions
11532 if any. */
11533 while (TREE_CODE (t) == TREE_LIST)
11534 {
11535 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11536 {
11537 if (tem == NULL_TREE)
11538 tem = TREE_VALUE (t);
11539 else
11540 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11541 TREE_VALUE (t), tem);
11542 }
11543 t = TREE_CHAIN (t);
11544 }
11545 if (tem)
11546 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11547 first = c_fully_fold (first, false, NULL);
11548 OMP_CLAUSE_DECL (c) = first;
11549 }
11550 else
11551 {
11552 unsigned int num = types.length (), i;
11553 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11554 tree condition = NULL_TREE;
11555
11556 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11557 maybe_zero_len = true;
11558
11559 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11560 t = TREE_CHAIN (t))
11561 {
11562 tree low_bound = TREE_PURPOSE (t);
11563 tree length = TREE_VALUE (t);
11564
11565 i--;
11566 if (low_bound
11567 && TREE_CODE (low_bound) == INTEGER_CST
11568 && TYPE_PRECISION (TREE_TYPE (low_bound))
11569 > TYPE_PRECISION (sizetype))
11570 low_bound = fold_convert (sizetype, low_bound);
11571 if (length
11572 && TREE_CODE (length) == INTEGER_CST
11573 && TYPE_PRECISION (TREE_TYPE (length))
11574 > TYPE_PRECISION (sizetype))
11575 length = fold_convert (sizetype, length);
11576 if (low_bound == NULL_TREE)
11577 low_bound = integer_zero_node;
11578 if (!maybe_zero_len && i > first_non_one)
11579 {
11580 if (integer_nonzerop (low_bound))
11581 goto do_warn_noncontiguous;
11582 if (length != NULL_TREE
11583 && TREE_CODE (length) == INTEGER_CST
11584 && TYPE_DOMAIN (types[i])
11585 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11586 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11587 == INTEGER_CST)
11588 {
11589 tree size;
11590 size = size_binop (PLUS_EXPR,
11591 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11592 size_one_node);
11593 if (!tree_int_cst_equal (length, size))
11594 {
11595 do_warn_noncontiguous:
11596 error_at (OMP_CLAUSE_LOCATION (c),
11597 "array section is not contiguous in %qs "
11598 "clause",
11599 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11600 types.release ();
11601 return true;
11602 }
11603 }
11604 if (length != NULL_TREE
11605 && TREE_SIDE_EFFECTS (length))
11606 {
11607 if (side_effects == NULL_TREE)
11608 side_effects = length;
11609 else
11610 side_effects = build2 (COMPOUND_EXPR,
11611 TREE_TYPE (side_effects),
11612 length, side_effects);
11613 }
11614 }
11615 else
11616 {
11617 tree l;
11618
11619 if (i > first_non_one && length && integer_nonzerop (length))
11620 continue;
11621 if (length)
11622 l = fold_convert (sizetype, length);
11623 else
11624 {
11625 l = size_binop (PLUS_EXPR,
11626 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11627 size_one_node);
11628 l = size_binop (MINUS_EXPR, l,
11629 fold_convert (sizetype, low_bound));
11630 }
11631 if (i > first_non_one)
11632 {
11633 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11634 size_zero_node);
11635 if (condition == NULL_TREE)
11636 condition = l;
11637 else
11638 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11639 l, condition);
11640 }
11641 else if (size == NULL_TREE)
11642 {
11643 size = size_in_bytes (TREE_TYPE (types[i]));
11644 size = size_binop (MULT_EXPR, size, l);
11645 if (condition)
11646 size = fold_build3 (COND_EXPR, sizetype, condition,
11647 size, size_zero_node);
11648 }
11649 else
11650 size = size_binop (MULT_EXPR, size, l);
11651 }
11652 }
11653 types.release ();
11654 if (side_effects)
11655 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11656 first = c_fully_fold (first, false, NULL);
11657 OMP_CLAUSE_DECL (c) = first;
11658 if (size)
11659 size = c_fully_fold (size, false, NULL);
11660 OMP_CLAUSE_SIZE (c) = size;
11661 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11662 return false;
11663 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11664 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11665 if (!c_mark_addressable (t))
11666 return false;
11667 OMP_CLAUSE_DECL (c2) = t;
11668 t = build_fold_addr_expr (first);
11669 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11670 tree ptr = OMP_CLAUSE_DECL (c2);
11671 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11672 ptr = build_fold_addr_expr (ptr);
11673 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11674 ptrdiff_type_node, t,
11675 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11676 ptrdiff_type_node, ptr));
11677 t = c_fully_fold (t, false, NULL);
11678 OMP_CLAUSE_SIZE (c2) = t;
11679 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11680 OMP_CLAUSE_CHAIN (c) = c2;
11681 }
11682 return false;
11683}
11684
11685/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11686 an inline call. But, remap
11687 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11688 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11689
11690static tree
11691c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11692 tree decl, tree placeholder)
11693{
11694 copy_body_data id;
11695 struct pointer_map_t *decl_map = pointer_map_create ();
11696
11697 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
11698 *pointer_map_insert (decl_map, omp_decl2) = decl;
11699 memset (&id, 0, sizeof (id));
11700 id.src_fn = DECL_CONTEXT (omp_decl1);
11701 id.dst_fn = current_function_decl;
11702 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11703 id.decl_map = decl_map;
11704
11705 id.copy_decl = copy_decl_no_change;
11706 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11707 id.transform_new_cfg = true;
11708 id.transform_return_to_modify = false;
11709 id.transform_lang_insert_block = NULL;
11710 id.eh_lp_nr = 0;
11711 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11712 pointer_map_destroy (decl_map);
11713 return stmt;
11714}
11715
11716/* Helper function of c_finish_omp_clauses, called via walk_tree.
11717 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11718
11719static tree
11720c_find_omp_placeholder_r (tree *tp, int *, void *data)
11721{
11722 if (*tp == (tree) data)
11723 return *tp;
11724 return NULL_TREE;
11725}
11726
953ff289
DN
11727/* For all elements of CLAUSES, validate them vs OpenMP constraints.
11728 Remove any elements from the list that are invalid. */
11729
11730tree
11731c_finish_omp_clauses (tree clauses)
11732{
11733 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 11734 bitmap_head aligned_head;
953ff289 11735 tree c, t, *pc = &clauses;
acf0174b
JJ
11736 bool branch_seen = false;
11737 bool copyprivate_seen = false;
11738 tree *nowait_clause = NULL;
953ff289
DN
11739
11740 bitmap_obstack_initialize (NULL);
11741 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11742 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11743 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 11744 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
11745
11746 for (pc = &clauses, c = clauses; c ; c = *pc)
11747 {
11748 bool remove = false;
11749 bool need_complete = false;
11750 bool need_implicitly_determined = false;
11751
aaf46ef9 11752 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
11753 {
11754 case OMP_CLAUSE_SHARED:
953ff289
DN
11755 need_implicitly_determined = true;
11756 goto check_dup_generic;
11757
11758 case OMP_CLAUSE_PRIVATE:
953ff289
DN
11759 need_complete = true;
11760 need_implicitly_determined = true;
11761 goto check_dup_generic;
11762
11763 case OMP_CLAUSE_REDUCTION:
953ff289
DN
11764 need_implicitly_determined = true;
11765 t = OMP_CLAUSE_DECL (c);
acf0174b 11766 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
652fea39
JJ
11767 && (FLOAT_TYPE_P (TREE_TYPE (t))
11768 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
953ff289
DN
11769 {
11770 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11771 const char *r_name = NULL;
11772
11773 switch (r_code)
11774 {
11775 case PLUS_EXPR:
11776 case MULT_EXPR:
11777 case MINUS_EXPR:
652fea39 11778 break;
20906c66 11779 case MIN_EXPR:
652fea39
JJ
11780 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11781 r_name = "min";
11782 break;
20906c66 11783 case MAX_EXPR:
652fea39
JJ
11784 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11785 r_name = "max";
953ff289
DN
11786 break;
11787 case BIT_AND_EXPR:
11788 r_name = "&";
11789 break;
11790 case BIT_XOR_EXPR:
11791 r_name = "^";
11792 break;
11793 case BIT_IOR_EXPR:
11794 r_name = "|";
11795 break;
11796 case TRUTH_ANDIF_EXPR:
652fea39
JJ
11797 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11798 r_name = "&&";
953ff289
DN
11799 break;
11800 case TRUTH_ORIF_EXPR:
652fea39
JJ
11801 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11802 r_name = "||";
953ff289
DN
11803 break;
11804 default:
11805 gcc_unreachable ();
11806 }
11807 if (r_name)
11808 {
c2255bc4
AH
11809 error_at (OMP_CLAUSE_LOCATION (c),
11810 "%qE has invalid type for %<reduction(%s)%>",
11811 t, r_name);
953ff289 11812 remove = true;
ee1d5a02 11813 break;
953ff289
DN
11814 }
11815 }
acf0174b
JJ
11816 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11817 {
11818 error_at (OMP_CLAUSE_LOCATION (c),
11819 "user defined reduction not found for %qD", t);
11820 remove = true;
ee1d5a02 11821 break;
acf0174b
JJ
11822 }
11823 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11824 {
11825 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11826 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11827 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11828 VAR_DECL, NULL_TREE, type);
11829 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11830 DECL_ARTIFICIAL (placeholder) = 1;
11831 DECL_IGNORED_P (placeholder) = 1;
11832 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11833 c_mark_addressable (placeholder);
11834 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11835 c_mark_addressable (OMP_CLAUSE_DECL (c));
11836 OMP_CLAUSE_REDUCTION_MERGE (c)
11837 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11838 TREE_VEC_ELT (list, 0),
11839 TREE_VEC_ELT (list, 1),
11840 OMP_CLAUSE_DECL (c), placeholder);
11841 OMP_CLAUSE_REDUCTION_MERGE (c)
11842 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11843 void_type_node, NULL_TREE,
11844 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11845 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11846 if (TREE_VEC_LENGTH (list) == 6)
11847 {
11848 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11849 c_mark_addressable (OMP_CLAUSE_DECL (c));
11850 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11851 c_mark_addressable (placeholder);
11852 tree init = TREE_VEC_ELT (list, 5);
11853 if (init == error_mark_node)
11854 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11855 OMP_CLAUSE_REDUCTION_INIT (c)
11856 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11857 TREE_VEC_ELT (list, 3),
11858 OMP_CLAUSE_DECL (c), placeholder);
11859 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11860 OMP_CLAUSE_REDUCTION_INIT (c)
11861 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11862 OMP_CLAUSE_REDUCTION_INIT (c));
11863 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11864 c_find_omp_placeholder_r,
11865 placeholder, NULL))
11866 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11867 }
11868 else
11869 {
11870 tree init;
11871 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11872 init = build_constructor (TREE_TYPE (t), NULL);
11873 else
11874 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11875 OMP_CLAUSE_REDUCTION_INIT (c)
11876 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11877 }
11878 OMP_CLAUSE_REDUCTION_INIT (c)
11879 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11880 void_type_node, NULL_TREE,
11881 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11882 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11883 }
953ff289
DN
11884 goto check_dup_generic;
11885
11886 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
11887 copyprivate_seen = true;
11888 if (nowait_clause)
11889 {
11890 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11891 "%<nowait%> clause must not be used together "
11892 "with %<copyprivate%>");
11893 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11894 nowait_clause = NULL;
11895 }
953ff289
DN
11896 goto check_dup_generic;
11897
11898 case OMP_CLAUSE_COPYIN:
953ff289
DN
11899 t = OMP_CLAUSE_DECL (c);
11900 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11901 {
c2255bc4
AH
11902 error_at (OMP_CLAUSE_LOCATION (c),
11903 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 11904 remove = true;
ee1d5a02 11905 break;
953ff289
DN
11906 }
11907 goto check_dup_generic;
11908
acf0174b
JJ
11909 case OMP_CLAUSE_LINEAR:
11910 t = OMP_CLAUSE_DECL (c);
11911 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11912 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11913 {
11914 error_at (OMP_CLAUSE_LOCATION (c),
11915 "linear clause applied to non-integral non-pointer "
11916 "variable with type %qT", TREE_TYPE (t));
11917 remove = true;
11918 break;
11919 }
11920 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11921 {
11922 tree s = OMP_CLAUSE_LINEAR_STEP (c);
11923 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
11924 OMP_CLAUSE_DECL (c), s);
11925 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11926 sizetype, s, OMP_CLAUSE_DECL (c));
11927 if (s == error_mark_node)
11928 s = size_one_node;
11929 OMP_CLAUSE_LINEAR_STEP (c) = s;
11930 }
11931 goto check_dup_generic;
11932
953ff289
DN
11933 check_dup_generic:
11934 t = OMP_CLAUSE_DECL (c);
11935 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11936 {
c2255bc4 11937 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
11938 "%qE is not a variable in clause %qs", t,
11939 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
11940 remove = true;
11941 }
11942 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11943 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
11944 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11945 {
c2255bc4
AH
11946 error_at (OMP_CLAUSE_LOCATION (c),
11947 "%qE appears more than once in data clauses", t);
953ff289
DN
11948 remove = true;
11949 }
11950 else
11951 bitmap_set_bit (&generic_head, DECL_UID (t));
11952 break;
11953
11954 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
11955 t = OMP_CLAUSE_DECL (c);
11956 need_complete = true;
11957 need_implicitly_determined = true;
11958 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11959 {
c2255bc4
AH
11960 error_at (OMP_CLAUSE_LOCATION (c),
11961 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
11962 remove = true;
11963 }
11964 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11965 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
11966 {
c2255bc4
AH
11967 error_at (OMP_CLAUSE_LOCATION (c),
11968 "%qE appears more than once in data clauses", t);
953ff289
DN
11969 remove = true;
11970 }
11971 else
11972 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
11973 break;
11974
11975 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
11976 t = OMP_CLAUSE_DECL (c);
11977 need_complete = true;
11978 need_implicitly_determined = true;
11979 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11980 {
c2255bc4
AH
11981 error_at (OMP_CLAUSE_LOCATION (c),
11982 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
11983 remove = true;
11984 }
11985 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11986 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11987 {
c2255bc4
AH
11988 error_at (OMP_CLAUSE_LOCATION (c),
11989 "%qE appears more than once in data clauses", t);
953ff289
DN
11990 remove = true;
11991 }
11992 else
11993 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
11994 break;
11995
acf0174b
JJ
11996 case OMP_CLAUSE_ALIGNED:
11997 t = OMP_CLAUSE_DECL (c);
11998 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11999 {
12000 error_at (OMP_CLAUSE_LOCATION (c),
12001 "%qE is not a variable in %<aligned%> clause", t);
12002 remove = true;
12003 }
5a9785fb
JJ
12004 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12005 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12006 {
12007 error_at (OMP_CLAUSE_LOCATION (c),
12008 "%qE in %<aligned%> clause is neither a pointer nor "
12009 "an array", t);
12010 remove = true;
12011 }
acf0174b
JJ
12012 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12013 {
12014 error_at (OMP_CLAUSE_LOCATION (c),
12015 "%qE appears more than once in %<aligned%> clauses",
12016 t);
12017 remove = true;
12018 }
12019 else
12020 bitmap_set_bit (&aligned_head, DECL_UID (t));
12021 break;
12022
12023 case OMP_CLAUSE_DEPEND:
12024 t = OMP_CLAUSE_DECL (c);
12025 if (TREE_CODE (t) == TREE_LIST)
12026 {
12027 if (handle_omp_array_sections (c))
12028 remove = true;
12029 break;
12030 }
12031 if (t == error_mark_node)
12032 remove = true;
12033 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12034 {
12035 error_at (OMP_CLAUSE_LOCATION (c),
12036 "%qE is not a variable in %<depend%> clause", t);
12037 remove = true;
12038 }
12039 else if (!c_mark_addressable (t))
12040 remove = true;
12041 break;
12042
12043 case OMP_CLAUSE_MAP:
12044 case OMP_CLAUSE_TO:
12045 case OMP_CLAUSE_FROM:
12046 t = OMP_CLAUSE_DECL (c);
12047 if (TREE_CODE (t) == TREE_LIST)
12048 {
12049 if (handle_omp_array_sections (c))
12050 remove = true;
12051 else
12052 {
12053 t = OMP_CLAUSE_DECL (c);
b17a8b07 12054 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12055 {
12056 error_at (OMP_CLAUSE_LOCATION (c),
12057 "array section does not have mappable type "
12058 "in %qs clause",
12059 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12060 remove = true;
12061 }
12062 }
12063 break;
12064 }
12065 if (t == error_mark_node)
12066 remove = true;
12067 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12068 {
12069 error_at (OMP_CLAUSE_LOCATION (c),
12070 "%qE is not a variable in %qs clause", t,
12071 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12072 remove = true;
12073 }
12074 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12075 {
12076 error_at (OMP_CLAUSE_LOCATION (c),
12077 "%qD is threadprivate variable in %qs clause", t,
12078 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12079 remove = true;
12080 }
12081 else if (!c_mark_addressable (t))
12082 remove = true;
b17a8b07
TS
12083 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12084 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12085 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12086 {
12087 error_at (OMP_CLAUSE_LOCATION (c),
12088 "%qD does not have a mappable type in %qs clause", t,
12089 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12090 remove = true;
12091 }
12092 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12093 {
12094 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12095 error ("%qD appears more than once in motion clauses", t);
12096 else
12097 error ("%qD appears more than once in map clauses", t);
12098 remove = true;
12099 }
12100 else
12101 bitmap_set_bit (&generic_head, DECL_UID (t));
12102 break;
12103
12104 case OMP_CLAUSE_UNIFORM:
12105 t = OMP_CLAUSE_DECL (c);
12106 if (TREE_CODE (t) != PARM_DECL)
12107 {
12108 if (DECL_P (t))
12109 error_at (OMP_CLAUSE_LOCATION (c),
12110 "%qD is not an argument in %<uniform%> clause", t);
12111 else
12112 error_at (OMP_CLAUSE_LOCATION (c),
12113 "%qE is not an argument in %<uniform%> clause", t);
12114 remove = true;
ee1d5a02 12115 break;
acf0174b 12116 }
ee1d5a02 12117 goto check_dup_generic;
acf0174b
JJ
12118
12119 case OMP_CLAUSE_NOWAIT:
12120 if (copyprivate_seen)
12121 {
12122 error_at (OMP_CLAUSE_LOCATION (c),
12123 "%<nowait%> clause must not be used together "
12124 "with %<copyprivate%>");
12125 remove = true;
12126 break;
12127 }
12128 nowait_clause = pc;
12129 pc = &OMP_CLAUSE_CHAIN (c);
12130 continue;
12131
953ff289
DN
12132 case OMP_CLAUSE_IF:
12133 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12134 case OMP_CLAUSE_NUM_TEAMS:
12135 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12136 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12137 case OMP_CLAUSE_ORDERED:
12138 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12139 case OMP_CLAUSE_UNTIED:
12140 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12141 case OMP_CLAUSE_FINAL:
12142 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12143 case OMP_CLAUSE_SAFELEN:
12144 case OMP_CLAUSE_SIMDLEN:
12145 case OMP_CLAUSE_DEVICE:
12146 case OMP_CLAUSE_DIST_SCHEDULE:
12147 case OMP_CLAUSE_PARALLEL:
12148 case OMP_CLAUSE_FOR:
12149 case OMP_CLAUSE_SECTIONS:
12150 case OMP_CLAUSE_TASKGROUP:
12151 case OMP_CLAUSE_PROC_BIND:
12152 pc = &OMP_CLAUSE_CHAIN (c);
12153 continue;
12154
12155 case OMP_CLAUSE_INBRANCH:
12156 case OMP_CLAUSE_NOTINBRANCH:
12157 if (branch_seen)
12158 {
12159 error_at (OMP_CLAUSE_LOCATION (c),
12160 "%<inbranch%> clause is incompatible with "
12161 "%<notinbranch%>");
12162 remove = true;
12163 break;
12164 }
12165 branch_seen = true;
953ff289
DN
12166 pc = &OMP_CLAUSE_CHAIN (c);
12167 continue;
12168
12169 default:
12170 gcc_unreachable ();
12171 }
12172
12173 if (!remove)
12174 {
12175 t = OMP_CLAUSE_DECL (c);
12176
12177 if (need_complete)
12178 {
12179 t = require_complete_type (t);
12180 if (t == error_mark_node)
12181 remove = true;
12182 }
12183
12184 if (need_implicitly_determined)
12185 {
12186 const char *share_name = NULL;
12187
12188 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12189 share_name = "threadprivate";
12190 else switch (c_omp_predetermined_sharing (t))
12191 {
12192 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12193 break;
12194 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12195 /* const vars may be specified in firstprivate clause. */
12196 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12197 && TREE_READONLY (t))
12198 break;
953ff289
DN
12199 share_name = "shared";
12200 break;
12201 case OMP_CLAUSE_DEFAULT_PRIVATE:
12202 share_name = "private";
12203 break;
12204 default:
12205 gcc_unreachable ();
12206 }
12207 if (share_name)
12208 {
c2255bc4
AH
12209 error_at (OMP_CLAUSE_LOCATION (c),
12210 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12211 t, share_name,
12212 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12213 remove = true;
12214 }
12215 }
12216 }
12217
12218 if (remove)
12219 *pc = OMP_CLAUSE_CHAIN (c);
12220 else
12221 pc = &OMP_CLAUSE_CHAIN (c);
12222 }
12223
12224 bitmap_obstack_release (NULL);
12225 return clauses;
12226}
9ae165a0 12227
0a35513e
AH
12228/* Create a transaction node. */
12229
12230tree
12231c_finish_transaction (location_t loc, tree block, int flags)
12232{
12233 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12234 if (flags & TM_STMT_ATTR_OUTER)
12235 TRANSACTION_EXPR_OUTER (stmt) = 1;
12236 if (flags & TM_STMT_ATTR_RELAXED)
12237 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12238 return add_stmt (stmt);
12239}
12240
9ae165a0
DG
12241/* Make a variant type in the proper way for C/C++, propagating qualifiers
12242 down to the element type of an array. */
12243
12244tree
12245c_build_qualified_type (tree type, int type_quals)
12246{
12247 if (type == error_mark_node)
12248 return type;
12249
12250 if (TREE_CODE (type) == ARRAY_TYPE)
12251 {
12252 tree t;
12253 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12254 type_quals);
12255
12256 /* See if we already have an identically qualified type. */
12257 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12258 {
12259 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12260 && TYPE_NAME (t) == TYPE_NAME (type)
12261 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12262 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12263 TYPE_ATTRIBUTES (type)))
12264 break;
12265 }
12266 if (!t)
12267 {
12268 tree domain = TYPE_DOMAIN (type);
12269
12270 t = build_variant_type_copy (type);
12271 TREE_TYPE (t) = element_type;
12272
12273 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12274 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12275 SET_TYPE_STRUCTURAL_EQUALITY (t);
12276 else if (TYPE_CANONICAL (element_type) != element_type
12277 || (domain && TYPE_CANONICAL (domain) != domain))
12278 {
b8698a0f 12279 tree unqualified_canon
9ae165a0 12280 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12281 domain? TYPE_CANONICAL (domain)
9ae165a0 12282 : NULL_TREE);
b8698a0f 12283 TYPE_CANONICAL (t)
9ae165a0
DG
12284 = c_build_qualified_type (unqualified_canon, type_quals);
12285 }
12286 else
12287 TYPE_CANONICAL (t) = t;
12288 }
12289 return t;
12290 }
12291
12292 /* A restrict-qualified pointer type must be a pointer to object or
12293 incomplete type. Note that the use of POINTER_TYPE_P also allows
12294 REFERENCE_TYPEs, which is appropriate for C++. */
12295 if ((type_quals & TYPE_QUAL_RESTRICT)
12296 && (!POINTER_TYPE_P (type)
12297 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12298 {
12299 error ("invalid use of %<restrict%>");
12300 type_quals &= ~TYPE_QUAL_RESTRICT;
12301 }
12302
12303 return build_qualified_type (type, type_quals);
12304}
72b5577d
ILT
12305
12306/* Build a VA_ARG_EXPR for the C parser. */
12307
12308tree
c2255bc4 12309c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d
ILT
12310{
12311 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12312 warning_at (loc, OPT_Wc___compat,
12313 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12314 return build_va_arg (loc, expr, type);
72b5577d 12315}
acf0174b
JJ
12316
12317/* Return truthvalue of whether T1 is the same tree structure as T2.
12318 Return 1 if they are the same. Return 0 if they are different. */
12319
12320bool
12321c_tree_equal (tree t1, tree t2)
12322{
12323 enum tree_code code1, code2;
12324
12325 if (t1 == t2)
12326 return true;
12327 if (!t1 || !t2)
12328 return false;
12329
12330 for (code1 = TREE_CODE (t1);
12331 CONVERT_EXPR_CODE_P (code1)
12332 || code1 == NON_LVALUE_EXPR;
12333 code1 = TREE_CODE (t1))
12334 t1 = TREE_OPERAND (t1, 0);
12335 for (code2 = TREE_CODE (t2);
12336 CONVERT_EXPR_CODE_P (code2)
12337 || code2 == NON_LVALUE_EXPR;
12338 code2 = TREE_CODE (t2))
12339 t2 = TREE_OPERAND (t2, 0);
12340
12341 /* They might have become equal now. */
12342 if (t1 == t2)
12343 return true;
12344
12345 if (code1 != code2)
12346 return false;
12347
12348 switch (code1)
12349 {
12350 case INTEGER_CST:
12351 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
12352 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
12353
12354 case REAL_CST:
12355 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12356
12357 case STRING_CST:
12358 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12359 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12360 TREE_STRING_LENGTH (t1));
12361
12362 case FIXED_CST:
12363 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12364 TREE_FIXED_CST (t2));
12365
12366 case COMPLEX_CST:
12367 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12368 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12369
12370 case VECTOR_CST:
12371 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12372
12373 case CONSTRUCTOR:
12374 /* We need to do this when determining whether or not two
12375 non-type pointer to member function template arguments
12376 are the same. */
12377 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12378 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12379 return false;
12380 {
12381 tree field, value;
12382 unsigned int i;
12383 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12384 {
12385 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12386 if (!c_tree_equal (field, elt2->index)
12387 || !c_tree_equal (value, elt2->value))
12388 return false;
12389 }
12390 }
12391 return true;
12392
12393 case TREE_LIST:
12394 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12395 return false;
12396 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12397 return false;
12398 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12399
12400 case SAVE_EXPR:
12401 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12402
12403 case CALL_EXPR:
12404 {
12405 tree arg1, arg2;
12406 call_expr_arg_iterator iter1, iter2;
12407 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12408 return false;
12409 for (arg1 = first_call_expr_arg (t1, &iter1),
12410 arg2 = first_call_expr_arg (t2, &iter2);
12411 arg1 && arg2;
12412 arg1 = next_call_expr_arg (&iter1),
12413 arg2 = next_call_expr_arg (&iter2))
12414 if (!c_tree_equal (arg1, arg2))
12415 return false;
12416 if (arg1 || arg2)
12417 return false;
12418 return true;
12419 }
12420
12421 case TARGET_EXPR:
12422 {
12423 tree o1 = TREE_OPERAND (t1, 0);
12424 tree o2 = TREE_OPERAND (t2, 0);
12425
12426 /* Special case: if either target is an unallocated VAR_DECL,
12427 it means that it's going to be unified with whatever the
12428 TARGET_EXPR is really supposed to initialize, so treat it
12429 as being equivalent to anything. */
12430 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12431 && !DECL_RTL_SET_P (o1))
12432 /*Nop*/;
12433 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12434 && !DECL_RTL_SET_P (o2))
12435 /*Nop*/;
12436 else if (!c_tree_equal (o1, o2))
12437 return false;
12438
12439 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12440 }
12441
12442 case COMPONENT_REF:
12443 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12444 return false;
12445 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12446
12447 case PARM_DECL:
12448 case VAR_DECL:
12449 case CONST_DECL:
12450 case FIELD_DECL:
12451 case FUNCTION_DECL:
12452 case IDENTIFIER_NODE:
12453 case SSA_NAME:
12454 return false;
12455
12456 case TREE_VEC:
12457 {
12458 unsigned ix;
12459 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12460 return false;
12461 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12462 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12463 TREE_VEC_ELT (t2, ix)))
12464 return false;
12465 return true;
12466 }
12467
12468 default:
12469 break;
12470 }
12471
12472 switch (TREE_CODE_CLASS (code1))
12473 {
12474 case tcc_unary:
12475 case tcc_binary:
12476 case tcc_comparison:
12477 case tcc_expression:
12478 case tcc_vl_exp:
12479 case tcc_reference:
12480 case tcc_statement:
12481 {
12482 int i, n = TREE_OPERAND_LENGTH (t1);
12483
12484 switch (code1)
12485 {
12486 case PREINCREMENT_EXPR:
12487 case PREDECREMENT_EXPR:
12488 case POSTINCREMENT_EXPR:
12489 case POSTDECREMENT_EXPR:
12490 n = 1;
12491 break;
12492 case ARRAY_REF:
12493 n = 2;
12494 break;
12495 default:
12496 break;
12497 }
12498
12499 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12500 && n != TREE_OPERAND_LENGTH (t2))
12501 return false;
12502
12503 for (i = 0; i < n; ++i)
12504 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12505 return false;
12506
12507 return true;
12508 }
12509
12510 case tcc_type:
12511 return comptypes (t1, t2);
12512 default:
12513 gcc_unreachable ();
12514 }
12515 /* We can get here with --disable-checking. */
12516 return false;
12517}
12893402
BI
12518
12519/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12520 spawn-helper and BODY is the newly created body for FNDECL. */
12521
12522void
12523cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12524{
12525 tree list = alloc_stmt_list ();
12526 tree frame = make_cilk_frame (fndecl);
12527 tree dtor = create_cilk_function_exit (frame, false, true);
12528 add_local_decl (cfun, frame);
12529
12530 DECL_SAVED_TREE (fndecl) = list;
12531 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12532 frame);
12533 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12534 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12535
12536 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12537 append_to_statement_list (detach_expr, &body_list);
12538
12539 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12540 body = fold_build_cleanup_point_expr (void_type_node, body);
12541
12542 append_to_statement_list (body, &body_list);
12543 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12544 body_list, dtor), &list);
12545}