]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
re PR ipa/61548 (FAIL: gcc.dg/tls/alias-1.c)
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
5624e564 2 Copyright (C) 1987-2015 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"
40e23961
MC
30#include "hash-set.h"
31#include "vec.h"
32#include "symtab.h"
33#include "input.h"
34#include "alias.h"
35#include "double-int.h"
36#include "machmode.h"
37#include "inchash.h"
38#include "real.h"
39#include "fixed-value.h"
400fbf9f 40#include "tree.h"
40e23961 41#include "fold-const.h"
d8a2d370
DN
42#include "stor-layout.h"
43#include "trans-mem.h"
44#include "varasm.h"
45#include "stmt.h"
e57e265b 46#include "langhooks.h"
400fbf9f 47#include "c-tree.h"
29cc57cf 48#include "c-lang.h"
400fbf9f 49#include "flags.h"
ab87f8c8 50#include "intl.h"
672a6f42 51#include "target.h"
325c3691 52#include "tree-iterator.h"
6662d794 53#include "bitmap.h"
60393bbc
AM
54#include "predict.h"
55#include "vec.h"
56#include "hashtab.h"
57#include "hash-set.h"
58#include "machmode.h"
59#include "hard-reg-set.h"
60#include "input.h"
61#include "function.h"
2fb9a547 62#include "gimple-expr.h"
45b0be94 63#include "gimplify.h"
acf0174b 64#include "tree-inline.h"
0645c1a2 65#include "omp-low.h"
61d3ce20 66#include "c-family/c-objc.h"
a212e43f 67#include "c-family/c-common.h"
de5a5fa1 68#include "c-family/c-ubsan.h"
12893402 69#include "cilk.h"
807e902e 70#include "wide-int.h"
41dbbb37 71#include "gomp-constants.h"
325c3691 72
2ac2f164
JM
73/* Possible cases of implicit bad conversions. Used to select
74 diagnostic messages in convert_for_assignment. */
75enum impl_conv {
76 ic_argpass,
77 ic_assign,
78 ic_init,
79 ic_return
80};
81
bc4b653b
JM
82/* The level of nesting inside "__alignof__". */
83int in_alignof;
84
85/* The level of nesting inside "sizeof". */
86int in_sizeof;
87
88/* The level of nesting inside "typeof". */
89int in_typeof;
400fbf9f 90
1a4049e7
JJ
91/* The argument of last parsed sizeof expression, only to be tested
92 if expr.original_code == SIZEOF_EXPR. */
93tree c_last_sizeof_arg;
94
9bac5cbb
G
95/* Nonzero if we might need to print a "missing braces around
96 initializer" message within this initializer. */
97static int found_missing_braces;
103b7b17 98
bf730f15
RS
99static int require_constant_value;
100static int require_constant_elements;
101
58f9752a 102static bool null_pointer_constant_p (const_tree);
f55ade6e 103static tree qualify_type (tree, tree);
dc5027f4
JM
104static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
105 bool *);
744aa42f 106static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
107static int function_types_compatible_p (const_tree, const_tree, bool *,
108 bool *);
109static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 110static tree lookup_field (tree, tree);
81e5eca8
MP
111static int convert_arguments (location_t, vec<location_t>, tree,
112 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
113 tree);
db3927fb 114static tree pointer_diff (location_t, tree, tree);
68fca595 115static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 116 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
117static tree valid_compound_expr_initializer (tree, tree);
118static void push_string (const char *);
119static void push_member_name (tree);
f55ade6e
AJ
120static int spelling_length (void);
121static char *print_spelling (char *);
96b40f8d 122static void warning_init (location_t, int, const char *);
c2255bc4 123static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
124static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
125 bool, struct obstack *);
a1e3b3d9 126static void output_pending_init_elements (int, struct obstack *);
ea58ef42 127static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 128static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
129static void add_pending_init (location_t, tree, tree, tree, bool,
130 struct obstack *);
a1e3b3d9
LB
131static void set_nonincremental_init (struct obstack *);
132static void set_nonincremental_init_from_string (tree, struct obstack *);
133static tree find_init_member (tree, struct obstack *);
f37acdf9 134static void readonly_warning (tree, enum lvalue_use);
7bd11157 135static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 136static void record_maybe_used_decl (tree);
dc5027f4 137static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
138\f
139/* Return true if EXP is a null pointer constant, false otherwise. */
140
141static bool
58f9752a 142null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
143{
144 /* This should really operate on c_expr structures, but they aren't
145 yet available everywhere required. */
146 tree type = TREE_TYPE (expr);
147 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 148 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
149 && integer_zerop (expr)
150 && (INTEGRAL_TYPE_P (type)
151 || (TREE_CODE (type) == POINTER_TYPE
152 && VOID_TYPE_P (TREE_TYPE (type))
153 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
154}
928c19bb
JM
155
156/* EXPR may appear in an unevaluated part of an integer constant
157 expression, but not in an evaluated part. Wrap it in a
158 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
159 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
160
161static tree
162note_integer_operands (tree expr)
163{
164 tree ret;
165 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
166 {
167 ret = copy_node (expr);
168 TREE_OVERFLOW (ret) = 1;
169 }
170 else
171 {
172 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
173 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
174 }
175 return ret;
176}
177
4d84fe7c
JM
178/* Having checked whether EXPR may appear in an unevaluated part of an
179 integer constant expression and found that it may, remove any
180 C_MAYBE_CONST_EXPR noting this fact and return the resulting
181 expression. */
182
183static inline tree
184remove_c_maybe_const_expr (tree expr)
185{
186 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
187 return C_MAYBE_CONST_EXPR_EXPR (expr);
188 else
189 return expr;
190}
191
f13c9b2c
AP
192\f/* This is a cache to hold if two types are compatible or not. */
193
194struct tagged_tu_seen_cache {
195 const struct tagged_tu_seen_cache * next;
58f9752a
KG
196 const_tree t1;
197 const_tree t2;
f13c9b2c
AP
198 /* The return value of tagged_types_tu_compatible_p if we had seen
199 these two types already. */
200 int val;
201};
202
203static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
204static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
205
400fbf9f
JW
206/* Do `exp = require_complete_type (exp);' to make sure exp
207 does not have an incomplete type. (That includes void types.) */
208
209tree
2f6e4e97 210require_complete_type (tree value)
400fbf9f
JW
211{
212 tree type = TREE_TYPE (value);
213
7a0ca710 214 if (error_operand_p (value))
ea0f786b
CB
215 return error_mark_node;
216
400fbf9f 217 /* First, detect a valid value with a complete type. */
d0f062fb 218 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
219 return value;
220
7a228918 221 c_incomplete_type_error (value, type);
400fbf9f
JW
222 return error_mark_node;
223}
224
225/* Print an error message for invalid use of an incomplete type.
226 VALUE is the expression that was used (or 0 if that isn't known)
227 and TYPE is the type that was invalid. */
228
229void
ac7d7749 230c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 231{
5d5993dd 232 const char *type_code_string;
400fbf9f
JW
233
234 /* Avoid duplicate error message. */
235 if (TREE_CODE (type) == ERROR_MARK)
236 return;
237
238 if (value != 0 && (TREE_CODE (value) == VAR_DECL
239 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 240 error ("%qD has an incomplete type", value);
400fbf9f
JW
241 else
242 {
243 retry:
244 /* We must print an error message. Be clever about what it says. */
245
246 switch (TREE_CODE (type))
247 {
248 case RECORD_TYPE:
ab87f8c8 249 type_code_string = "struct";
400fbf9f
JW
250 break;
251
252 case UNION_TYPE:
ab87f8c8 253 type_code_string = "union";
400fbf9f
JW
254 break;
255
256 case ENUMERAL_TYPE:
ab87f8c8 257 type_code_string = "enum";
400fbf9f
JW
258 break;
259
260 case VOID_TYPE:
261 error ("invalid use of void expression");
262 return;
263
264 case ARRAY_TYPE:
265 if (TYPE_DOMAIN (type))
266 {
fba78abb
RH
267 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
268 {
269 error ("invalid use of flexible array member");
270 return;
271 }
400fbf9f
JW
272 type = TREE_TYPE (type);
273 goto retry;
274 }
275 error ("invalid use of array with unspecified bounds");
276 return;
277
278 default:
366de0ce 279 gcc_unreachable ();
400fbf9f
JW
280 }
281
282 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
283 error ("invalid use of undefined type %<%s %E%>",
284 type_code_string, TYPE_NAME (type));
400fbf9f
JW
285 else
286 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 287 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
288 }
289}
290
ab393bf1
NB
291/* Given a type, apply default promotions wrt unnamed function
292 arguments and return the new type. */
293
294tree
2f6e4e97 295c_type_promotes_to (tree type)
ab393bf1 296{
267bac10 297 tree ret = NULL_TREE;
ab393bf1 298
267bac10
JM
299 if (TYPE_MAIN_VARIANT (type) == float_type_node)
300 ret = double_type_node;
301 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
302 {
303 /* Preserve unsignedness if not really getting any wider. */
8df83eae 304 if (TYPE_UNSIGNED (type)
c22cacf3 305 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
306 ret = unsigned_type_node;
307 else
308 ret = integer_type_node;
ab393bf1
NB
309 }
310
267bac10
JM
311 if (ret != NULL_TREE)
312 return (TYPE_ATOMIC (type)
313 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
314 : ret);
315
ab393bf1
NB
316 return type;
317}
318
36c5e70a
BE
319/* Return true if between two named address spaces, whether there is a superset
320 named address space that encompasses both address spaces. If there is a
321 superset, return which address space is the superset. */
322
323static bool
324addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
325{
326 if (as1 == as2)
327 {
328 *common = as1;
329 return true;
330 }
331 else if (targetm.addr_space.subset_p (as1, as2))
332 {
333 *common = as2;
334 return true;
335 }
336 else if (targetm.addr_space.subset_p (as2, as1))
337 {
338 *common = as1;
339 return true;
340 }
341 else
342 return false;
343}
344
400fbf9f
JW
345/* Return a variant of TYPE which has all the type qualifiers of LIKE
346 as well as those of TYPE. */
347
348static tree
2f6e4e97 349qualify_type (tree type, tree like)
400fbf9f 350{
36c5e70a
BE
351 addr_space_t as_type = TYPE_ADDR_SPACE (type);
352 addr_space_t as_like = TYPE_ADDR_SPACE (like);
353 addr_space_t as_common;
354
355 /* If the two named address spaces are different, determine the common
356 superset address space. If there isn't one, raise an error. */
357 if (!addr_space_superset (as_type, as_like, &as_common))
358 {
359 as_common = as_type;
360 error ("%qT and %qT are in disjoint named address spaces",
361 type, like);
362 }
363
2f6e4e97 364 return c_build_qualified_type (type,
36c5e70a 365 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 366 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 367 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 368}
52ffd86e
MS
369
370/* Return true iff the given tree T is a variable length array. */
371
372bool
ac7d7749 373c_vla_type_p (const_tree t)
52ffd86e
MS
374{
375 if (TREE_CODE (t) == ARRAY_TYPE
376 && C_TYPE_VARIABLE_SIZE (t))
377 return true;
378 return false;
379}
400fbf9f 380\f
10bc1b1b 381/* Return the composite type of two compatible types.
5305f6d7 382
10bc1b1b
JM
383 We assume that comptypes has already been done and returned
384 nonzero; if that isn't so, this may crash. In particular, we
385 assume that qualifiers match. */
400fbf9f
JW
386
387tree
10bc1b1b 388composite_type (tree t1, tree t2)
400fbf9f 389{
b3694847
SS
390 enum tree_code code1;
391 enum tree_code code2;
4b027d16 392 tree attributes;
400fbf9f
JW
393
394 /* Save time if the two types are the same. */
395
396 if (t1 == t2) return t1;
397
398 /* If one type is nonsense, use the other. */
399 if (t1 == error_mark_node)
400 return t2;
401 if (t2 == error_mark_node)
402 return t1;
403
10bc1b1b
JM
404 code1 = TREE_CODE (t1);
405 code2 = TREE_CODE (t2);
406
d9525bec 407 /* Merge the attributes. */
5fd9b178 408 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 409
10bc1b1b
JM
410 /* If one is an enumerated type and the other is the compatible
411 integer type, the composite type might be either of the two
412 (DR#013 question 3). For consistency, use the enumerated type as
413 the composite type. */
400fbf9f 414
10bc1b1b
JM
415 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
416 return t1;
417 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
418 return t2;
75326e8c 419
366de0ce 420 gcc_assert (code1 == code2);
b6a10c9f 421
400fbf9f
JW
422 switch (code1)
423 {
400fbf9f 424 case POINTER_TYPE:
10bc1b1b 425 /* For two pointers, do this recursively on the target type. */
400fbf9f 426 {
3932261a
MM
427 tree pointed_to_1 = TREE_TYPE (t1);
428 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 429 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 430 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
431 t1 = build_type_attribute_variant (t1, attributes);
432 return qualify_type (t1, t2);
400fbf9f 433 }
400fbf9f
JW
434
435 case ARRAY_TYPE:
436 {
10bc1b1b 437 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
438 int quals;
439 tree unqual_elt;
ca8bdb78
JM
440 tree d1 = TYPE_DOMAIN (t1);
441 tree d2 = TYPE_DOMAIN (t2);
442 bool d1_variable, d2_variable;
443 bool d1_zero, d2_zero;
f6294de7 444 bool t1_complete, t2_complete;
46df2823 445
de46b2fe 446 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
447 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
448 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 449
f6294de7
JM
450 t1_complete = COMPLETE_TYPE_P (t1);
451 t2_complete = COMPLETE_TYPE_P (t2);
452
ca8bdb78
JM
453 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
454 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
455
456 d1_variable = (!d1_zero
457 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
458 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
459 d2_variable = (!d2_zero
460 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
461 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
462 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
463 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 464
400fbf9f 465 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
466 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
467 && (d2_variable || d2_zero || !d1_variable))
4b027d16 468 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
469 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
470 && (d1_variable || d1_zero || !d2_variable))
4b027d16 471 return build_type_attribute_variant (t2, attributes);
c22cacf3 472
de46b2fe
AP
473 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
474 return build_type_attribute_variant (t1, attributes);
475 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
476 return build_type_attribute_variant (t2, attributes);
c22cacf3 477
46df2823
JM
478 /* Merge the element types, and have a size if either arg has
479 one. We may have qualifiers on the element types. To set
480 up TYPE_MAIN_VARIANT correctly, we need to form the
481 composite of the unqualified types and add the qualifiers
482 back at the end. */
483 quals = TYPE_QUALS (strip_array_types (elt));
484 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
485 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
486 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
487 && (d2_variable
488 || d2_zero
489 || !d1_variable))
490 ? t1
491 : t2));
f6294de7
JM
492 /* Ensure a composite type involving a zero-length array type
493 is a zero-length type not an incomplete type. */
494 if (d1_zero && d2_zero
495 && (t1_complete || t2_complete)
496 && !COMPLETE_TYPE_P (t1))
497 {
498 TYPE_SIZE (t1) = bitsize_zero_node;
499 TYPE_SIZE_UNIT (t1) = size_zero_node;
500 }
46df2823 501 t1 = c_build_qualified_type (t1, quals);
de46b2fe 502 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
503 }
504
fcb99e7b
JJ
505 case ENUMERAL_TYPE:
506 case RECORD_TYPE:
507 case UNION_TYPE:
508 if (attributes != NULL)
509 {
510 /* Try harder not to create a new aggregate type. */
511 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
512 return t1;
513 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
514 return t2;
515 }
516 return build_type_attribute_variant (t1, attributes);
517
400fbf9f
JW
518 case FUNCTION_TYPE:
519 /* Function types: prefer the one that specified arg types.
520 If both do, merge the arg types. Also merge the return types. */
521 {
10bc1b1b 522 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
523 tree p1 = TYPE_ARG_TYPES (t1);
524 tree p2 = TYPE_ARG_TYPES (t2);
525 int len;
526 tree newargs, n;
527 int i;
528
529 /* Save space: see if the result is identical to one of the args. */
3f75a254 530 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 531 return build_type_attribute_variant (t1, attributes);
3f75a254 532 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 533 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
534
535 /* Simple way if one arg fails to specify argument types. */
536 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 537 {
fe7080d2
AP
538 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
539 t1 = build_type_attribute_variant (t1, attributes);
540 return qualify_type (t1, t2);
4b027d16 541 }
400fbf9f 542 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
543 {
544 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
545 t1 = build_type_attribute_variant (t1, attributes);
546 return qualify_type (t1, t2);
4b027d16 547 }
400fbf9f
JW
548
549 /* If both args specify argument types, we must merge the two
550 lists, argument by argument. */
2f4e8f2b 551
400fbf9f
JW
552 len = list_length (p1);
553 newargs = 0;
554
555 for (i = 0; i < len; i++)
8d9bfdc5 556 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
557
558 n = newargs;
559
560 for (; p1;
561 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
562 {
563 /* A null type means arg type is not specified.
564 Take whatever the other function type has. */
565 if (TREE_VALUE (p1) == 0)
566 {
567 TREE_VALUE (n) = TREE_VALUE (p2);
568 goto parm_done;
569 }
570 if (TREE_VALUE (p2) == 0)
571 {
572 TREE_VALUE (n) = TREE_VALUE (p1);
573 goto parm_done;
574 }
2f6e4e97 575
400fbf9f
JW
576 /* Given wait (union {union wait *u; int *i} *)
577 and wait (union wait *),
578 prefer union wait * as type of parm. */
579 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
580 && TREE_VALUE (p1) != TREE_VALUE (p2))
581 {
582 tree memb;
58cb41e6
JJ
583 tree mv2 = TREE_VALUE (p2);
584 if (mv2 && mv2 != error_mark_node
585 && TREE_CODE (mv2) != ARRAY_TYPE)
586 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 587 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 588 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
589 {
590 tree mv3 = TREE_TYPE (memb);
591 if (mv3 && mv3 != error_mark_node
592 && TREE_CODE (mv3) != ARRAY_TYPE)
593 mv3 = TYPE_MAIN_VARIANT (mv3);
594 if (comptypes (mv3, mv2))
595 {
596 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
597 TREE_VALUE (p2));
c1771a20 598 pedwarn (input_location, OPT_Wpedantic,
fcf73884 599 "function types not truly compatible in ISO C");
58cb41e6
JJ
600 goto parm_done;
601 }
602 }
400fbf9f
JW
603 }
604 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
605 && TREE_VALUE (p2) != TREE_VALUE (p1))
606 {
607 tree memb;
58cb41e6
JJ
608 tree mv1 = TREE_VALUE (p1);
609 if (mv1 && mv1 != error_mark_node
610 && TREE_CODE (mv1) != ARRAY_TYPE)
611 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 612 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 613 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
614 {
615 tree mv3 = TREE_TYPE (memb);
616 if (mv3 && mv3 != error_mark_node
617 && TREE_CODE (mv3) != ARRAY_TYPE)
618 mv3 = TYPE_MAIN_VARIANT (mv3);
619 if (comptypes (mv3, mv1))
620 {
621 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
622 TREE_VALUE (p1));
c1771a20 623 pedwarn (input_location, OPT_Wpedantic,
fcf73884 624 "function types not truly compatible in ISO C");
58cb41e6
JJ
625 goto parm_done;
626 }
627 }
400fbf9f 628 }
10bc1b1b 629 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
630 parm_done: ;
631 }
632
4b027d16 633 t1 = build_function_type (valtype, newargs);
fe7080d2 634 t1 = qualify_type (t1, t2);
0f41302f 635 /* ... falls through ... */
400fbf9f
JW
636 }
637
638 default:
4b027d16 639 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
640 }
641
642}
10bc1b1b
JM
643
644/* Return the type of a conditional expression between pointers to
645 possibly differently qualified versions of compatible types.
646
647 We assume that comp_target_types has already been done and returned
648 nonzero; if that isn't so, this may crash. */
649
650static tree
651common_pointer_type (tree t1, tree t2)
652{
653 tree attributes;
46df2823
JM
654 tree pointed_to_1, mv1;
655 tree pointed_to_2, mv2;
10bc1b1b 656 tree target;
eb1387a0 657 unsigned target_quals;
36c5e70a
BE
658 addr_space_t as1, as2, as_common;
659 int quals1, quals2;
10bc1b1b
JM
660
661 /* Save time if the two types are the same. */
662
663 if (t1 == t2) return t1;
664
665 /* If one type is nonsense, use the other. */
666 if (t1 == error_mark_node)
667 return t2;
668 if (t2 == error_mark_node)
669 return t1;
670
366de0ce 671 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 672 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
673
674 /* Merge the attributes. */
675 attributes = targetm.merge_type_attributes (t1, t2);
676
677 /* Find the composite type of the target types, and combine the
46df2823
JM
678 qualifiers of the two types' targets. Do not lose qualifiers on
679 array element types by taking the TYPE_MAIN_VARIANT. */
680 mv1 = pointed_to_1 = TREE_TYPE (t1);
681 mv2 = pointed_to_2 = TREE_TYPE (t2);
682 if (TREE_CODE (mv1) != ARRAY_TYPE)
683 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
684 if (TREE_CODE (mv2) != ARRAY_TYPE)
685 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
686 target = composite_type (mv1, mv2);
eb1387a0 687
768952be
MU
688 /* Strip array types to get correct qualifier for pointers to arrays */
689 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
690 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
691
eb1387a0
RG
692 /* For function types do not merge const qualifiers, but drop them
693 if used inconsistently. The middle-end uses these to mark const
694 and noreturn functions. */
695 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 696 target_quals = (quals1 & quals2);
eb1387a0 697 else
36c5e70a
BE
698 target_quals = (quals1 | quals2);
699
700 /* If the two named address spaces are different, determine the common
701 superset address space. This is guaranteed to exist due to the
702 assumption that comp_target_type returned non-zero. */
703 as1 = TYPE_ADDR_SPACE (pointed_to_1);
704 as2 = TYPE_ADDR_SPACE (pointed_to_2);
705 if (!addr_space_superset (as1, as2, &as_common))
706 gcc_unreachable ();
707
708 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
709
eb1387a0 710 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
711 return build_type_attribute_variant (t1, attributes);
712}
713
714/* Return the common type for two arithmetic types under the usual
715 arithmetic conversions. The default conversions have already been
716 applied, and enumerated types converted to their compatible integer
717 types. The resulting type is unqualified and has no attributes.
718
719 This is the type for the result of most arithmetic operations
720 if the operands have the given two types. */
721
ccf7f880
JJ
722static tree
723c_common_type (tree t1, tree t2)
10bc1b1b
JM
724{
725 enum tree_code code1;
726 enum tree_code code2;
727
728 /* If one type is nonsense, use the other. */
729 if (t1 == error_mark_node)
730 return t2;
731 if (t2 == error_mark_node)
732 return t1;
733
734 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
735 t1 = TYPE_MAIN_VARIANT (t1);
736
737 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
738 t2 = TYPE_MAIN_VARIANT (t2);
739
740 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
741 t1 = build_type_attribute_variant (t1, NULL_TREE);
742
743 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
744 t2 = build_type_attribute_variant (t2, NULL_TREE);
745
746 /* Save time if the two types are the same. */
747
748 if (t1 == t2) return t1;
749
750 code1 = TREE_CODE (t1);
751 code2 = TREE_CODE (t2);
752
366de0ce 753 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
754 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
755 || code1 == INTEGER_TYPE);
366de0ce 756 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
757 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
758 || code2 == INTEGER_TYPE);
10bc1b1b 759
5fc89bfd
JJ
760 /* When one operand is a decimal float type, the other operand cannot be
761 a generic float type or a complex type. We also disallow vector types
762 here. */
763 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
764 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
765 {
766 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
767 {
768 error ("can%'t mix operands of decimal float and vector types");
769 return error_mark_node;
770 }
771 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
772 {
773 error ("can%'t mix operands of decimal float and complex types");
774 return error_mark_node;
775 }
776 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
777 {
778 error ("can%'t mix operands of decimal float and other float types");
779 return error_mark_node;
780 }
781 }
782
10bc1b1b
JM
783 /* If one type is a vector type, return that type. (How the usual
784 arithmetic conversions apply to the vector types extension is not
785 precisely specified.) */
786 if (code1 == VECTOR_TYPE)
787 return t1;
788
789 if (code2 == VECTOR_TYPE)
790 return t2;
791
792 /* If one type is complex, form the common type of the non-complex
793 components, then make that complex. Use T1 or T2 if it is the
794 required type. */
795 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
796 {
797 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
798 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 799 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
800
801 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
802 return t1;
803 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
804 return t2;
805 else
806 return build_complex_type (subtype);
807 }
808
809 /* If only one is real, use it as the result. */
810
811 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
812 return t1;
813
814 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
815 return t2;
816
9a8ce21f
JG
817 /* If both are real and either are decimal floating point types, use
818 the decimal floating point type with the greater precision. */
819
820 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
821 {
822 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
823 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
824 return dfloat128_type_node;
825 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
826 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
827 return dfloat64_type_node;
828 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
829 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
830 return dfloat32_type_node;
831 }
832
ab22c1fa
CF
833 /* Deal with fixed-point types. */
834 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
835 {
836 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 837 machine_mode m1, m2;
ab22c1fa
CF
838 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
839
840 m1 = TYPE_MODE (t1);
841 m2 = TYPE_MODE (t2);
842
843 /* If one input type is saturating, the result type is saturating. */
844 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
845 satp = 1;
846
847 /* If both fixed-point types are unsigned, the result type is unsigned.
848 When mixing fixed-point and integer types, follow the sign of the
849 fixed-point type.
850 Otherwise, the result type is signed. */
851 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
852 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
853 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
854 && TYPE_UNSIGNED (t1))
855 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
856 && TYPE_UNSIGNED (t2)))
857 unsignedp = 1;
858
859 /* The result type is signed. */
860 if (unsignedp == 0)
861 {
862 /* If the input type is unsigned, we need to convert to the
863 signed type. */
864 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
865 {
d75d71e0 866 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
867 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
868 mclass = MODE_FRACT;
869 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
870 mclass = MODE_ACCUM;
871 else
872 gcc_unreachable ();
873 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
874 }
875 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
876 {
d75d71e0 877 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
878 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
879 mclass = MODE_FRACT;
880 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
881 mclass = MODE_ACCUM;
882 else
883 gcc_unreachable ();
884 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
885 }
886 }
887
888 if (code1 == FIXED_POINT_TYPE)
889 {
890 fbit1 = GET_MODE_FBIT (m1);
891 ibit1 = GET_MODE_IBIT (m1);
892 }
893 else
894 {
895 fbit1 = 0;
896 /* Signed integers need to subtract one sign bit. */
897 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
898 }
899
900 if (code2 == FIXED_POINT_TYPE)
901 {
902 fbit2 = GET_MODE_FBIT (m2);
903 ibit2 = GET_MODE_IBIT (m2);
904 }
905 else
906 {
907 fbit2 = 0;
908 /* Signed integers need to subtract one sign bit. */
909 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
910 }
911
912 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
913 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
914 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
915 satp);
916 }
917
10bc1b1b
JM
918 /* Both real or both integers; use the one with greater precision. */
919
920 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
921 return t1;
922 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
923 return t2;
924
925 /* Same precision. Prefer long longs to longs to ints when the
926 same precision, following the C99 rules on integer type rank
927 (which are equivalent to the C90 rules for C90 types). */
928
929 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
930 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
931 return long_long_unsigned_type_node;
932
933 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
934 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
935 {
936 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
937 return long_long_unsigned_type_node;
938 else
c22cacf3 939 return long_long_integer_type_node;
10bc1b1b
JM
940 }
941
942 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
943 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
944 return long_unsigned_type_node;
945
946 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
947 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
948 {
949 /* But preserve unsignedness from the other type,
950 since long cannot hold all the values of an unsigned int. */
951 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
952 return long_unsigned_type_node;
953 else
954 return long_integer_type_node;
955 }
956
957 /* Likewise, prefer long double to double even if same size. */
958 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
959 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
960 return long_double_type_node;
961
2531a1d9
JR
962 /* Likewise, prefer double to float even if same size.
963 We got a couple of embedded targets with 32 bit doubles, and the
964 pdp11 might have 64 bit floats. */
965 if (TYPE_MAIN_VARIANT (t1) == double_type_node
966 || TYPE_MAIN_VARIANT (t2) == double_type_node)
967 return double_type_node;
968
10bc1b1b
JM
969 /* Otherwise prefer the unsigned one. */
970
971 if (TYPE_UNSIGNED (t1))
972 return t1;
973 else
974 return t2;
975}
400fbf9f 976\f
5922c215
JM
977/* Wrapper around c_common_type that is used by c-common.c and other
978 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
979 are allowed here and are converted to their compatible integer types.
980 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
981 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
982tree
983common_type (tree t1, tree t2)
984{
985 if (TREE_CODE (t1) == ENUMERAL_TYPE)
986 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
987 if (TREE_CODE (t2) == ENUMERAL_TYPE)
988 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
989
990 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
991 if (TREE_CODE (t1) == BOOLEAN_TYPE
992 && TREE_CODE (t2) == BOOLEAN_TYPE)
993 return boolean_type_node;
994
995 /* If either type is BOOLEAN_TYPE, then return the other. */
996 if (TREE_CODE (t1) == BOOLEAN_TYPE)
997 return t2;
998 if (TREE_CODE (t2) == BOOLEAN_TYPE)
999 return t1;
1000
ccf7f880
JJ
1001 return c_common_type (t1, t2);
1002}
f13c9b2c 1003
400fbf9f
JW
1004/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1005 or various other operations. Return 2 if they are compatible
1006 but a warning may be needed if you use them together. */
1007
1008int
132da1a5 1009comptypes (tree type1, tree type2)
f13c9b2c
AP
1010{
1011 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1012 int val;
1013
dc5027f4 1014 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
1015 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1016
1017 return val;
1018}
1019
1020/* Like comptypes, but if it returns non-zero because enum and int are
1021 compatible, it sets *ENUM_AND_INT_P to true. */
1022
1023static int
1024comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1025{
1026 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1027 int val;
1028
dc5027f4
JM
1029 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1030 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1031
1032 return val;
1033}
1034
1035/* Like comptypes, but if it returns nonzero for different types, it
1036 sets *DIFFERENT_TYPES_P to true. */
1037
1038int
1039comptypes_check_different_types (tree type1, tree type2,
1040 bool *different_types_p)
1041{
1042 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1043 int val;
1044
1045 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1046 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1047
f13c9b2c 1048 return val;
c22cacf3
MS
1049}
1050\f
f13c9b2c
AP
1051/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1052 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1053 but a warning may be needed if you use them together. If
1054 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1055 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1056 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1057 NULL, and the types are compatible but different enough not to be
48b0b196 1058 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1059 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1060 false, but may or may not be set if the types are incompatible.
1061 This differs from comptypes, in that we don't free the seen
1062 types. */
f13c9b2c
AP
1063
1064static int
dc5027f4
JM
1065comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1066 bool *different_types_p)
400fbf9f 1067{
58f9752a
KG
1068 const_tree t1 = type1;
1069 const_tree t2 = type2;
4b027d16 1070 int attrval, val;
400fbf9f
JW
1071
1072 /* Suppress errors caused by previously reported errors. */
1073
8d47dfc5
RH
1074 if (t1 == t2 || !t1 || !t2
1075 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1076 return 1;
1077
bca63328
JM
1078 /* Enumerated types are compatible with integer types, but this is
1079 not transitive: two enumerated types in the same translation unit
1080 are compatible with each other only if they are the same type. */
400fbf9f 1081
bca63328 1082 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1083 {
1084 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1085 if (TREE_CODE (t2) != VOID_TYPE)
1086 {
1087 if (enum_and_int_p != NULL)
1088 *enum_and_int_p = true;
1089 if (different_types_p != NULL)
1090 *different_types_p = true;
1091 }
744aa42f 1092 }
bca63328 1093 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1094 {
1095 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1096 if (TREE_CODE (t1) != VOID_TYPE)
1097 {
1098 if (enum_and_int_p != NULL)
1099 *enum_and_int_p = true;
1100 if (different_types_p != NULL)
1101 *different_types_p = true;
1102 }
744aa42f 1103 }
400fbf9f
JW
1104
1105 if (t1 == t2)
1106 return 1;
1107
1108 /* Different classes of types can't be compatible. */
1109
3aeb3655
EC
1110 if (TREE_CODE (t1) != TREE_CODE (t2))
1111 return 0;
400fbf9f 1112
118a3a8b 1113 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1114
3932261a 1115 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1116 return 0;
1117
08632da2
RS
1118 /* Allow for two different type nodes which have essentially the same
1119 definition. Note that we already checked for equality of the type
38e01259 1120 qualifiers (just above). */
400fbf9f 1121
46df2823
JM
1122 if (TREE_CODE (t1) != ARRAY_TYPE
1123 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1124 return 1;
1125
4b027d16 1126 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1127 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1128 return 0;
1129
1130 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1131 val = 0;
1132
400fbf9f
JW
1133 switch (TREE_CODE (t1))
1134 {
1135 case POINTER_TYPE:
106f5de5
UW
1136 /* Do not remove mode or aliasing information. */
1137 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1138 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1139 break;
4b027d16 1140 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1141 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1142 enum_and_int_p, different_types_p));
4b027d16 1143 break;
400fbf9f
JW
1144
1145 case FUNCTION_TYPE:
dc5027f4
JM
1146 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1147 different_types_p);
4b027d16 1148 break;
400fbf9f
JW
1149
1150 case ARRAY_TYPE:
1151 {
400fbf9f
JW
1152 tree d1 = TYPE_DOMAIN (t1);
1153 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1154 bool d1_variable, d2_variable;
1155 bool d1_zero, d2_zero;
4b027d16 1156 val = 1;
400fbf9f
JW
1157
1158 /* Target types must match incl. qualifiers. */
1159 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1160 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1161 enum_and_int_p,
1162 different_types_p)))
400fbf9f
JW
1163 return 0;
1164
dc5027f4
JM
1165 if (different_types_p != NULL
1166 && (d1 == 0) != (d2 == 0))
1167 *different_types_p = true;
400fbf9f 1168 /* Sizes must match unless one is missing or variable. */
3f85558f 1169 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1170 break;
400fbf9f 1171
3f75a254
JM
1172 d1_zero = !TYPE_MAX_VALUE (d1);
1173 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1174
3f75a254 1175 d1_variable = (!d1_zero
3f85558f
RH
1176 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1177 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1178 d2_variable = (!d2_zero
3f85558f
RH
1179 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1180 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1181 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1182 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1183
dc5027f4
JM
1184 if (different_types_p != NULL
1185 && d1_variable != d2_variable)
1186 *different_types_p = true;
3f85558f
RH
1187 if (d1_variable || d2_variable)
1188 break;
1189 if (d1_zero && d2_zero)
1190 break;
1191 if (d1_zero || d2_zero
3f75a254
JM
1192 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1193 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1194 val = 0;
1195
c22cacf3 1196 break;
400fbf9f
JW
1197 }
1198
d1bd0ded 1199 case ENUMERAL_TYPE:
58393038 1200 case RECORD_TYPE:
d1bd0ded 1201 case UNION_TYPE:
766beae1 1202 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1203 {
fcb99e7b
JJ
1204 tree a1 = TYPE_ATTRIBUTES (t1);
1205 tree a2 = TYPE_ATTRIBUTES (t2);
1206
1207 if (! attribute_list_contained (a1, a2)
1208 && ! attribute_list_contained (a2, a1))
1209 break;
1210
f13c9b2c 1211 if (attrval != 2)
dc5027f4
JM
1212 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1213 different_types_p);
1214 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1215 different_types_p);
f13c9b2c 1216 }
4b027d16 1217 break;
e9a25f70 1218
62e1dfcf 1219 case VECTOR_TYPE:
744aa42f
ILT
1220 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1221 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1222 enum_and_int_p, different_types_p));
62e1dfcf
NC
1223 break;
1224
e9a25f70
JL
1225 default:
1226 break;
400fbf9f 1227 }
4b027d16 1228 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1229}
1230
36c5e70a
BE
1231/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1232 their qualifiers, except for named address spaces. If the pointers point to
1233 different named addresses, then we must determine if one address space is a
1234 subset of the other. */
400fbf9f
JW
1235
1236static int
744aa42f 1237comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1238{
392202b0 1239 int val;
768952be 1240 int val_ped;
36c5e70a
BE
1241 tree mvl = TREE_TYPE (ttl);
1242 tree mvr = TREE_TYPE (ttr);
1243 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1244 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1245 addr_space_t as_common;
744aa42f 1246 bool enum_and_int_p;
8b40563c 1247
36c5e70a
BE
1248 /* Fail if pointers point to incompatible address spaces. */
1249 if (!addr_space_superset (asl, asr, &as_common))
1250 return 0;
1251
768952be
MU
1252 /* For pedantic record result of comptypes on arrays before losing
1253 qualifiers on the element type below. */
1254 val_ped = 1;
1255
1256 if (TREE_CODE (mvl) == ARRAY_TYPE
1257 && TREE_CODE (mvr) == ARRAY_TYPE)
1258 val_ped = comptypes (mvl, mvr);
1259
1260 /* Qualifiers on element types of array types that are
1261 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1262
1263 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1264 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1265 : TYPE_MAIN_VARIANT (mvl));
1266
1267 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1268 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1269 : TYPE_MAIN_VARIANT (mvr));
1270
744aa42f
ILT
1271 enum_and_int_p = false;
1272 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1273
768952be
MU
1274 if (val == 1 && val_ped != 1)
1275 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1276 "are incompatible in ISO C");
1277
fcf73884 1278 if (val == 2)
c1771a20 1279 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1280
1281 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1282 warning_at (location, OPT_Wc___compat,
1283 "pointer target types incompatible in C++");
1284
400fbf9f
JW
1285 return val;
1286}
1287\f
1288/* Subroutines of `comptypes'. */
1289
f75fbaf7
ZW
1290/* Determine whether two trees derive from the same translation unit.
1291 If the CONTEXT chain ends in a null, that tree's context is still
1292 being parsed, so if two trees have context chains ending in null,
766beae1 1293 they're in the same translation unit. */
f75fbaf7 1294int
58f9752a 1295same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1296{
1297 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1298 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1299 {
6615c446
JO
1300 case tcc_declaration:
1301 t1 = DECL_CONTEXT (t1); break;
1302 case tcc_type:
1303 t1 = TYPE_CONTEXT (t1); break;
1304 case tcc_exceptional:
1305 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1306 default: gcc_unreachable ();
766beae1
ZW
1307 }
1308
1309 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1310 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1311 {
6615c446
JO
1312 case tcc_declaration:
1313 t2 = DECL_CONTEXT (t2); break;
1314 case tcc_type:
1315 t2 = TYPE_CONTEXT (t2); break;
1316 case tcc_exceptional:
1317 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1318 default: gcc_unreachable ();
766beae1
ZW
1319 }
1320
1321 return t1 == t2;
1322}
1323
f13c9b2c 1324/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1325
f13c9b2c 1326static struct tagged_tu_seen_cache *
58f9752a 1327alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1328{
cceb1885 1329 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1330 tu->next = tagged_tu_seen_base;
1331 tu->t1 = t1;
1332 tu->t2 = t2;
c22cacf3 1333
f13c9b2c 1334 tagged_tu_seen_base = tu;
c22cacf3 1335
f13c9b2c
AP
1336 /* The C standard says that two structures in different translation
1337 units are compatible with each other only if the types of their
1338 fields are compatible (among other things). We assume that they
1339 are compatible until proven otherwise when building the cache.
1340 An example where this can occur is:
1341 struct a
1342 {
1343 struct a *next;
1344 };
1345 If we are comparing this against a similar struct in another TU,
c83eecad 1346 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1347 loop. */
1348 tu->val = 1;
1349 return tu;
1350}
d1bd0ded 1351
f13c9b2c 1352/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1353
f13c9b2c
AP
1354static void
1355free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1356{
1357 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1358 while (tu != tu_til)
1359 {
741ac903
KG
1360 const struct tagged_tu_seen_cache *const tu1
1361 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1362 tu = tu1->next;
b1d5455a 1363 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1364 }
1365 tagged_tu_seen_base = tu_til;
1366}
d1bd0ded
GK
1367
1368/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1369 compatible. If the two types are not the same (which has been
1370 checked earlier), this can only happen when multiple translation
1371 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1372 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1373 comptypes_internal. */
d1bd0ded
GK
1374
1375static int
744aa42f 1376tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1377 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1378{
1379 tree s1, s2;
1380 bool needs_warning = false;
3aeb3655 1381
d1bd0ded
GK
1382 /* We have to verify that the tags of the types are the same. This
1383 is harder than it looks because this may be a typedef, so we have
1384 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1385 typedef...
1386 In the case of compiler-created builtin structs the TYPE_DECL
1387 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1388 while (TYPE_NAME (t1)
1389 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1390 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1391 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1392
dea984dc
ILT
1393 while (TYPE_NAME (t2)
1394 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1395 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1396 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1397
1398 /* C90 didn't have the requirement that the two tags be the same. */
1399 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1400 return 0;
3aeb3655 1401
d1bd0ded
GK
1402 /* C90 didn't say what happened if one or both of the types were
1403 incomplete; we choose to follow C99 rules here, which is that they
1404 are compatible. */
1405 if (TYPE_SIZE (t1) == NULL
1406 || TYPE_SIZE (t2) == NULL)
1407 return 1;
3aeb3655 1408
d1bd0ded 1409 {
f13c9b2c 1410 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1411 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1412 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1413 return tts_i->val;
d1bd0ded 1414 }
3aeb3655 1415
d1bd0ded
GK
1416 switch (TREE_CODE (t1))
1417 {
1418 case ENUMERAL_TYPE:
1419 {
f13c9b2c 1420 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1421 /* Speed up the case where the type values are in the same order. */
1422 tree tv1 = TYPE_VALUES (t1);
1423 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1424
c22cacf3 1425 if (tv1 == tv2)
f13c9b2c
AP
1426 {
1427 return 1;
1428 }
3aeb3655 1429
c22cacf3
MS
1430 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1431 {
1432 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1433 break;
1434 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1435 {
c22cacf3 1436 tu->val = 0;
f13c9b2c
AP
1437 return 0;
1438 }
c22cacf3 1439 }
3aeb3655 1440
c22cacf3 1441 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1442 {
1443 return 1;
1444 }
c22cacf3 1445 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1446 {
1447 tu->val = 0;
1448 return 0;
1449 }
3aeb3655 1450
d1bd0ded 1451 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1452 {
1453 tu->val = 0;
1454 return 0;
1455 }
3aeb3655 1456
d1bd0ded
GK
1457 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1458 {
1459 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1460 if (s2 == NULL
1461 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1462 {
1463 tu->val = 0;
1464 return 0;
1465 }
d1bd0ded
GK
1466 }
1467 return 1;
1468 }
1469
1470 case UNION_TYPE:
1471 {
f13c9b2c 1472 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1473 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1474 {
1475 tu->val = 0;
1476 return 0;
1477 }
c22cacf3 1478
f13c9b2c
AP
1479 /* Speed up the common case where the fields are in the same order. */
1480 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1481 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1482 {
1483 int result;
c22cacf3 1484
3ae4d3cc 1485 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1486 break;
744aa42f 1487 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1488 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1489
1490 if (result != 1 && !DECL_NAME (s1))
1491 break;
f13c9b2c
AP
1492 if (result == 0)
1493 {
1494 tu->val = 0;
1495 return 0;
1496 }
1497 if (result == 2)
1498 needs_warning = true;
1499
1500 if (TREE_CODE (s1) == FIELD_DECL
1501 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1502 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1503 {
1504 tu->val = 0;
1505 return 0;
1506 }
1507 }
1508 if (!s1 && !s2)
1509 {
1510 tu->val = needs_warning ? 2 : 1;
1511 return tu->val;
1512 }
d1bd0ded 1513
910ad8de 1514 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1515 {
1516 bool ok = false;
3aeb3655 1517
910ad8de 1518 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1519 if (DECL_NAME (s1) == DECL_NAME (s2))
1520 {
1521 int result;
1522
744aa42f 1523 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1524 enum_and_int_p,
1525 different_types_p);
3ae4d3cc
AO
1526
1527 if (result != 1 && !DECL_NAME (s1))
1528 continue;
1529 if (result == 0)
1530 {
1531 tu->val = 0;
1532 return 0;
1533 }
1534 if (result == 2)
1535 needs_warning = true;
1536
1537 if (TREE_CODE (s1) == FIELD_DECL
1538 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1539 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1540 break;
3ae4d3cc
AO
1541
1542 ok = true;
1543 break;
1544 }
3f75a254 1545 if (!ok)
f13c9b2c
AP
1546 {
1547 tu->val = 0;
1548 return 0;
1549 }
d1bd0ded 1550 }
f13c9b2c
AP
1551 tu->val = needs_warning ? 2 : 10;
1552 return tu->val;
d1bd0ded
GK
1553 }
1554
1555 case RECORD_TYPE:
1556 {
c22cacf3 1557 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1558
1559 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1560 s1 && s2;
910ad8de 1561 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1562 {
1563 int result;
1564 if (TREE_CODE (s1) != TREE_CODE (s2)
1565 || DECL_NAME (s1) != DECL_NAME (s2))
1566 break;
744aa42f 1567 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1568 enum_and_int_p, different_types_p);
d1bd0ded
GK
1569 if (result == 0)
1570 break;
1571 if (result == 2)
1572 needs_warning = true;
3aeb3655 1573
d1bd0ded
GK
1574 if (TREE_CODE (s1) == FIELD_DECL
1575 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1576 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1577 break;
1578 }
d1bd0ded 1579 if (s1 && s2)
f13c9b2c
AP
1580 tu->val = 0;
1581 else
1582 tu->val = needs_warning ? 2 : 1;
1583 return tu->val;
d1bd0ded
GK
1584 }
1585
1586 default:
366de0ce 1587 gcc_unreachable ();
d1bd0ded
GK
1588 }
1589}
1590
400fbf9f
JW
1591/* Return 1 if two function types F1 and F2 are compatible.
1592 If either type specifies no argument types,
1593 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1594 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1595 the other must specify that number of self-promoting arg types.
744aa42f 1596 Otherwise, the argument types must match.
dc5027f4 1597 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1598
1599static int
744aa42f 1600function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1601 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1602{
1603 tree args1, args2;
1604 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1605 int val = 1;
1606 int val1;
a6fdc086
GK
1607 tree ret1, ret2;
1608
1609 ret1 = TREE_TYPE (f1);
1610 ret2 = TREE_TYPE (f2);
1611
e508a019
JM
1612 /* 'volatile' qualifiers on a function's return type used to mean
1613 the function is noreturn. */
1614 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1615 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1616 if (TYPE_VOLATILE (ret1))
1617 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1618 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1619 if (TYPE_VOLATILE (ret2))
1620 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1621 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1622 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1623 if (val == 0)
400fbf9f
JW
1624 return 0;
1625
1626 args1 = TYPE_ARG_TYPES (f1);
1627 args2 = TYPE_ARG_TYPES (f2);
1628
dc5027f4
JM
1629 if (different_types_p != NULL
1630 && (args1 == 0) != (args2 == 0))
1631 *different_types_p = true;
1632
400fbf9f
JW
1633 /* An unspecified parmlist matches any specified parmlist
1634 whose argument types don't need default promotions. */
1635
1636 if (args1 == 0)
1637 {
1638 if (!self_promoting_args_p (args2))
1639 return 0;
1640 /* If one of these types comes from a non-prototype fn definition,
1641 compare that with the other type's arglist.
3176a0c2 1642 If they don't match, ask for a warning (but no error). */
400fbf9f 1643 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1644 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1645 enum_and_int_p, different_types_p))
400fbf9f
JW
1646 val = 2;
1647 return val;
1648 }
1649 if (args2 == 0)
1650 {
1651 if (!self_promoting_args_p (args1))
1652 return 0;
1653 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1654 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1655 enum_and_int_p, different_types_p))
400fbf9f
JW
1656 val = 2;
1657 return val;
1658 }
1659
1660 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1661 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1662 different_types_p);
400fbf9f
JW
1663 return val1 != 1 ? val1 : val;
1664}
1665
744aa42f
ILT
1666/* Check two lists of types for compatibility, returning 0 for
1667 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1668 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1669 comptypes_internal. */
400fbf9f
JW
1670
1671static int
744aa42f 1672type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1673 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1674{
1675 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1676 int val = 1;
9d5f3e49 1677 int newval = 0;
400fbf9f
JW
1678
1679 while (1)
1680 {
46df2823 1681 tree a1, mv1, a2, mv2;
400fbf9f
JW
1682 if (args1 == 0 && args2 == 0)
1683 return val;
1684 /* If one list is shorter than the other,
1685 they fail to match. */
1686 if (args1 == 0 || args2 == 0)
1687 return 0;
46df2823
JM
1688 mv1 = a1 = TREE_VALUE (args1);
1689 mv2 = a2 = TREE_VALUE (args2);
1690 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1691 mv1 = (TYPE_ATOMIC (mv1)
1692 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1693 TYPE_QUAL_ATOMIC)
1694 : TYPE_MAIN_VARIANT (mv1));
46df2823 1695 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1696 mv2 = (TYPE_ATOMIC (mv2)
1697 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1698 TYPE_QUAL_ATOMIC)
1699 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1700 /* A null pointer instead of a type
1701 means there is supposed to be an argument
1702 but nothing is specified about what type it has.
1703 So match anything that self-promotes. */
dc5027f4
JM
1704 if (different_types_p != NULL
1705 && (a1 == 0) != (a2 == 0))
1706 *different_types_p = true;
46df2823 1707 if (a1 == 0)
400fbf9f 1708 {
46df2823 1709 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1710 return 0;
1711 }
46df2823 1712 else if (a2 == 0)
400fbf9f 1713 {
46df2823 1714 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1715 return 0;
1716 }
8f5b6d29 1717 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1718 else if (TREE_CODE (a1) == ERROR_MARK
1719 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1720 ;
dc5027f4
JM
1721 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1722 different_types_p)))
400fbf9f 1723 {
dc5027f4
JM
1724 if (different_types_p != NULL)
1725 *different_types_p = true;
400fbf9f
JW
1726 /* Allow wait (union {union wait *u; int *i} *)
1727 and wait (union wait *) to be compatible. */
46df2823
JM
1728 if (TREE_CODE (a1) == UNION_TYPE
1729 && (TYPE_NAME (a1) == 0
ebf0bf7f 1730 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1731 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1732 && tree_int_cst_equal (TYPE_SIZE (a1),
1733 TYPE_SIZE (a2)))
400fbf9f
JW
1734 {
1735 tree memb;
46df2823 1736 for (memb = TYPE_FIELDS (a1);
910ad8de 1737 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1738 {
1739 tree mv3 = TREE_TYPE (memb);
1740 if (mv3 && mv3 != error_mark_node
1741 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1742 mv3 = (TYPE_ATOMIC (mv3)
1743 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1744 TYPE_QUAL_ATOMIC)
1745 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1746 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1747 different_types_p))
58cb41e6
JJ
1748 break;
1749 }
400fbf9f
JW
1750 if (memb == 0)
1751 return 0;
1752 }
46df2823
JM
1753 else if (TREE_CODE (a2) == UNION_TYPE
1754 && (TYPE_NAME (a2) == 0
ebf0bf7f 1755 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1756 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1757 && tree_int_cst_equal (TYPE_SIZE (a2),
1758 TYPE_SIZE (a1)))
400fbf9f
JW
1759 {
1760 tree memb;
46df2823 1761 for (memb = TYPE_FIELDS (a2);
910ad8de 1762 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1763 {
1764 tree mv3 = TREE_TYPE (memb);
1765 if (mv3 && mv3 != error_mark_node
1766 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1767 mv3 = (TYPE_ATOMIC (mv3)
1768 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1769 TYPE_QUAL_ATOMIC)
1770 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1771 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1772 different_types_p))
58cb41e6
JJ
1773 break;
1774 }
400fbf9f
JW
1775 if (memb == 0)
1776 return 0;
1777 }
1778 else
1779 return 0;
1780 }
1781
1782 /* comptypes said ok, but record if it said to warn. */
1783 if (newval > val)
1784 val = newval;
1785
1786 args1 = TREE_CHAIN (args1);
1787 args2 = TREE_CHAIN (args2);
1788 }
1789}
400fbf9f 1790\f
a0e24419
MP
1791/* Compute the size to increment a pointer by. When a function type or void
1792 type or incomplete type is passed, size_one_node is returned.
1793 This function does not emit any diagnostics; the caller is responsible
1794 for that. */
400fbf9f 1795
4e2fb7de 1796static tree
58f9752a 1797c_size_in_bytes (const_tree type)
400fbf9f
JW
1798{
1799 enum tree_code code = TREE_CODE (type);
1800
a0e24419
MP
1801 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1802 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1803 return size_one_node;
1804
400fbf9f 1805 /* Convert in case a char is more than one unit. */
db3927fb
AH
1806 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1807 size_int (TYPE_PRECISION (char_type_node)
1808 / BITS_PER_UNIT));
400fbf9f 1809}
400fbf9f 1810\f
400fbf9f
JW
1811/* Return either DECL or its known constant value (if it has one). */
1812
56cb9733 1813tree
2f6e4e97 1814decl_constant_value (tree decl)
400fbf9f 1815{
a7c1916a 1816 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1817 in a place where a variable is invalid. Note that DECL_INITIAL
1818 isn't valid for a PARM_DECL. */
a7c1916a 1819 current_function_decl != 0
4f976745 1820 && TREE_CODE (decl) != PARM_DECL
3f75a254 1821 && !TREE_THIS_VOLATILE (decl)
83bab8db 1822 && TREE_READONLY (decl)
400fbf9f
JW
1823 && DECL_INITIAL (decl) != 0
1824 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1825 /* This is invalid if initial value is not constant.
1826 If it has either a function call, a memory reference,
1827 or a variable, then re-evaluating it could give different results. */
1828 && TREE_CONSTANT (DECL_INITIAL (decl))
1829 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1830 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1831 return DECL_INITIAL (decl);
1832 return decl;
1833}
1834
f2a71bbc
JM
1835/* Convert the array expression EXP to a pointer. */
1836static tree
c2255bc4 1837array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1838{
f2a71bbc 1839 tree orig_exp = exp;
207bf485 1840 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1841 tree adr;
1842 tree restype = TREE_TYPE (type);
1843 tree ptrtype;
207bf485 1844
f2a71bbc 1845 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1846
f2a71bbc 1847 STRIP_TYPE_NOPS (exp);
207bf485 1848
487a92fe
JM
1849 if (TREE_NO_WARNING (orig_exp))
1850 TREE_NO_WARNING (exp) = 1;
207bf485 1851
f2a71bbc
JM
1852 ptrtype = build_pointer_type (restype);
1853
1854 if (TREE_CODE (exp) == INDIRECT_REF)
1855 return convert (ptrtype, TREE_OPERAND (exp, 0));
1856
1f37c583
JM
1857 /* In C++ array compound literals are temporary objects unless they are
1858 const or appear in namespace scope, so they are destroyed too soon
1859 to use them for much of anything (c++/53220). */
1860 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1861 {
1862 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1863 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1864 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1865 "converting an array compound literal to a pointer "
1866 "is ill-formed in C++");
1867 }
1868
c2255bc4 1869 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1870 return convert (ptrtype, adr);
1871}
207bf485 1872
f2a71bbc
JM
1873/* Convert the function expression EXP to a pointer. */
1874static tree
c2255bc4 1875function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1876{
1877 tree orig_exp = exp;
207bf485 1878
f2a71bbc 1879 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1880
f2a71bbc 1881 STRIP_TYPE_NOPS (exp);
207bf485 1882
f2a71bbc
JM
1883 if (TREE_NO_WARNING (orig_exp))
1884 TREE_NO_WARNING (exp) = 1;
207bf485 1885
c2255bc4 1886 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1887}
207bf485 1888
ebfbbdc5
JJ
1889/* Mark EXP as read, not just set, for set but not used -Wunused
1890 warning purposes. */
1891
1892void
1893mark_exp_read (tree exp)
1894{
1895 switch (TREE_CODE (exp))
1896 {
1897 case VAR_DECL:
1898 case PARM_DECL:
1899 DECL_READ_P (exp) = 1;
1900 break;
1901 case ARRAY_REF:
1902 case COMPONENT_REF:
1903 case MODIFY_EXPR:
1904 case REALPART_EXPR:
1905 case IMAGPART_EXPR:
1906 CASE_CONVERT:
1907 case ADDR_EXPR:
1908 mark_exp_read (TREE_OPERAND (exp, 0));
1909 break;
1910 case COMPOUND_EXPR:
82c3c067 1911 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1912 mark_exp_read (TREE_OPERAND (exp, 1));
1913 break;
1914 default:
1915 break;
1916 }
1917}
1918
f2a71bbc
JM
1919/* Perform the default conversion of arrays and functions to pointers.
1920 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1921 return EXP.
1922
1923 LOC is the location of the expression. */
f2a71bbc
JM
1924
1925struct c_expr
c2255bc4 1926default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1927{
1928 tree orig_exp = exp.value;
1929 tree type = TREE_TYPE (exp.value);
1930 enum tree_code code = TREE_CODE (type);
1931
1932 switch (code)
1933 {
1934 case ARRAY_TYPE:
1935 {
1936 bool not_lvalue = false;
1937 bool lvalue_array_p;
1938
1939 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1940 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1941 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1942 {
1943 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1944 not_lvalue = true;
1945 exp.value = TREE_OPERAND (exp.value, 0);
1946 }
1947
1948 if (TREE_NO_WARNING (orig_exp))
1949 TREE_NO_WARNING (exp.value) = 1;
1950
1951 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1952 if (!flag_isoc99 && !lvalue_array_p)
1953 {
1954 /* Before C99, non-lvalue arrays do not decay to pointers.
1955 Normally, using such an array would be invalid; but it can
1956 be used correctly inside sizeof or as a statement expression.
1957 Thus, do not give an error here; an error will result later. */
1958 return exp;
1959 }
1960
c2255bc4 1961 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1962 }
1963 break;
1964 case FUNCTION_TYPE:
c2255bc4 1965 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1966 break;
1967 default:
f2a71bbc 1968 break;
207bf485 1969 }
f2a71bbc 1970
207bf485
JM
1971 return exp;
1972}
1973
ebfbbdc5
JJ
1974struct c_expr
1975default_function_array_read_conversion (location_t loc, struct c_expr exp)
1976{
1977 mark_exp_read (exp.value);
1978 return default_function_array_conversion (loc, exp);
1979}
522ddfa2 1980
267bac10
JM
1981/* Return whether EXPR should be treated as an atomic lvalue for the
1982 purposes of load and store handling. */
1983
1984static bool
1985really_atomic_lvalue (tree expr)
1986{
7a0ca710 1987 if (error_operand_p (expr))
267bac10
JM
1988 return false;
1989 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1990 return false;
1991 if (!lvalue_p (expr))
1992 return false;
1993
1994 /* Ignore _Atomic on register variables, since their addresses can't
1995 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1996 sequences wouldn't work. Ignore _Atomic on structures containing
1997 bit-fields, since accessing elements of atomic structures or
1998 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1999 it's undefined at translation time or execution time, and the
2000 normal atomic sequences again wouldn't work. */
2001 while (handled_component_p (expr))
2002 {
2003 if (TREE_CODE (expr) == COMPONENT_REF
2004 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2005 return false;
2006 expr = TREE_OPERAND (expr, 0);
2007 }
2008 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2009 return false;
2010 return true;
2011}
2012
2013/* Convert expression EXP (location LOC) from lvalue to rvalue,
2014 including converting functions and arrays to pointers if CONVERT_P.
2015 If READ_P, also mark the expression as having been read. */
2016
2017struct c_expr
2018convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2019 bool convert_p, bool read_p)
2020{
2021 if (read_p)
2022 mark_exp_read (exp.value);
2023 if (convert_p)
2024 exp = default_function_array_conversion (loc, exp);
2025 if (really_atomic_lvalue (exp.value))
2026 {
2027 vec<tree, va_gc> *params;
2028 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2029 tree expr_type = TREE_TYPE (exp.value);
2030 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2031 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2032
2033 gcc_assert (TYPE_ATOMIC (expr_type));
2034
2035 /* Expansion of a generic atomic load may require an addition
2036 element, so allocate enough to prevent a resize. */
2037 vec_alloc (params, 4);
2038
2039 /* Remove the qualifiers for the rest of the expressions and
2040 create the VAL temp variable to hold the RHS. */
2041 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
b731b390 2042 tmp = create_tmp_var (nonatomic_type);
267bac10
JM
2043 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2044 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2045 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2046
2047 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2048 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2049 params->quick_push (expr_addr);
2050 params->quick_push (tmp_addr);
2051 params->quick_push (seq_cst);
8edbfaa6 2052 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2053
cc28fc7f
MP
2054 /* EXPR is always read. */
2055 mark_exp_read (exp.value);
2056
267bac10
JM
2057 /* Return tmp which contains the value loaded. */
2058 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2059 }
2060 return exp;
2061}
2062
522ddfa2
JM
2063/* EXP is an expression of integer type. Apply the integer promotions
2064 to it and return the promoted value. */
400fbf9f
JW
2065
2066tree
522ddfa2 2067perform_integral_promotions (tree exp)
400fbf9f 2068{
b3694847
SS
2069 tree type = TREE_TYPE (exp);
2070 enum tree_code code = TREE_CODE (type);
400fbf9f 2071
522ddfa2 2072 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2073
400fbf9f
JW
2074 /* Normally convert enums to int,
2075 but convert wide enums to something wider. */
2076 if (code == ENUMERAL_TYPE)
2077 {
b0c48229
NB
2078 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2079 TYPE_PRECISION (integer_type_node)),
2080 ((TYPE_PRECISION (type)
2081 >= TYPE_PRECISION (integer_type_node))
8df83eae 2082 && TYPE_UNSIGNED (type)));
05bccae2 2083
400fbf9f
JW
2084 return convert (type, exp);
2085 }
2086
522ddfa2
JM
2087 /* ??? This should no longer be needed now bit-fields have their
2088 proper types. */
9753f113 2089 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2090 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2091 /* If it's thinner than an int, promote it like a
d72040f5 2092 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2093 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2094 TYPE_PRECISION (integer_type_node)))
f458d1d5 2095 return convert (integer_type_node, exp);
9753f113 2096
d72040f5 2097 if (c_promoting_integer_type_p (type))
400fbf9f 2098 {
f458d1d5 2099 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2100 if (TYPE_UNSIGNED (type)
f458d1d5 2101 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2102 return convert (unsigned_type_node, exp);
05bccae2 2103
400fbf9f
JW
2104 return convert (integer_type_node, exp);
2105 }
05bccae2 2106
522ddfa2
JM
2107 return exp;
2108}
2109
2110
2111/* Perform default promotions for C data used in expressions.
46bdb9cf 2112 Enumeral types or short or char are converted to int.
522ddfa2
JM
2113 In addition, manifest constants symbols are replaced by their values. */
2114
2115tree
2116default_conversion (tree exp)
2117{
2118 tree orig_exp;
2119 tree type = TREE_TYPE (exp);
2120 enum tree_code code = TREE_CODE (type);
40449a90 2121 tree promoted_type;
522ddfa2 2122
ebfbbdc5
JJ
2123 mark_exp_read (exp);
2124
46bdb9cf
JM
2125 /* Functions and arrays have been converted during parsing. */
2126 gcc_assert (code != FUNCTION_TYPE);
2127 if (code == ARRAY_TYPE)
2128 return exp;
522ddfa2
JM
2129
2130 /* Constants can be used directly unless they're not loadable. */
2131 if (TREE_CODE (exp) == CONST_DECL)
2132 exp = DECL_INITIAL (exp);
2133
522ddfa2
JM
2134 /* Strip no-op conversions. */
2135 orig_exp = exp;
2136 STRIP_TYPE_NOPS (exp);
2137
2138 if (TREE_NO_WARNING (orig_exp))
2139 TREE_NO_WARNING (exp) = 1;
2140
400fbf9f
JW
2141 if (code == VOID_TYPE)
2142 {
5436fa2e
MP
2143 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2144 "void value not ignored as it ought to be");
400fbf9f
JW
2145 return error_mark_node;
2146 }
808d6eaa
JM
2147
2148 exp = require_complete_type (exp);
2149 if (exp == error_mark_node)
2150 return error_mark_node;
2151
40449a90
SL
2152 promoted_type = targetm.promoted_type (type);
2153 if (promoted_type)
2154 return convert (promoted_type, exp);
2155
808d6eaa
JM
2156 if (INTEGRAL_TYPE_P (type))
2157 return perform_integral_promotions (exp);
2158
400fbf9f
JW
2159 return exp;
2160}
2161\f
0fb96aa4 2162/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2163
2164 If the component name is not found, returns NULL_TREE. Otherwise,
2165 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2166 stepping down the chain to the component, which is in the last
2167 TREE_VALUE of the list. Normally the list is of length one, but if
2168 the component is embedded within (nested) anonymous structures or
2169 unions, the list steps down the chain to the component. */
2f6e4e97 2170
2f2d13da 2171static tree
0fb96aa4 2172lookup_field (tree type, tree component)
2f2d13da
DE
2173{
2174 tree field;
2175
2176 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2177 to the field elements. Use a binary search on this array to quickly
2178 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2179 will always be set for structures which have many elements. */
2180
22a0b85f 2181 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2182 {
2183 int bot, top, half;
d07605f5 2184 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2185
2186 field = TYPE_FIELDS (type);
2187 bot = 0;
d07605f5 2188 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2189 while (top - bot > 1)
2190 {
2f2d13da
DE
2191 half = (top - bot + 1) >> 1;
2192 field = field_array[bot+half];
2193
2194 if (DECL_NAME (field) == NULL_TREE)
2195 {
2196 /* Step through all anon unions in linear fashion. */
2197 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2198 {
2f2d13da 2199 field = field_array[bot++];
a68b98cf
RK
2200 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2201 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2202 {
0fb96aa4 2203 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2204
2205 if (anon)
2206 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2207
2208 /* The Plan 9 compiler permits referring
2209 directly to an anonymous struct/union field
2210 using a typedef name. */
2211 if (flag_plan9_extensions
2212 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2213 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2214 == TYPE_DECL)
2215 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2216 == component))
2217 break;
2f6e4e97 2218 }
2f2d13da
DE
2219 }
2220
2221 /* Entire record is only anon unions. */
2222 if (bot > top)
2223 return NULL_TREE;
2224
2225 /* Restart the binary search, with new lower bound. */
2226 continue;
2227 }
2228
e8b87aac 2229 if (DECL_NAME (field) == component)
2f2d13da 2230 break;
e8b87aac 2231 if (DECL_NAME (field) < component)
2f2d13da
DE
2232 bot += half;
2233 else
2234 top = bot + half;
2235 }
2236
2237 if (DECL_NAME (field_array[bot]) == component)
2238 field = field_array[bot];
2239 else if (DECL_NAME (field) != component)
e9b2c823 2240 return NULL_TREE;
2f2d13da
DE
2241 }
2242 else
2243 {
910ad8de 2244 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2245 {
e9b2c823
NB
2246 if (DECL_NAME (field) == NULL_TREE
2247 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2248 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2249 {
0fb96aa4 2250 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2251
e9b2c823
NB
2252 if (anon)
2253 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2254
2255 /* The Plan 9 compiler permits referring directly to an
2256 anonymous struct/union field using a typedef
2257 name. */
2258 if (flag_plan9_extensions
2259 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2260 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2261 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2262 == component))
2263 break;
2f2d13da
DE
2264 }
2265
2266 if (DECL_NAME (field) == component)
2267 break;
2268 }
e9b2c823
NB
2269
2270 if (field == NULL_TREE)
2271 return NULL_TREE;
2f2d13da
DE
2272 }
2273
e9b2c823 2274 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2275}
2276
c2255bc4
AH
2277/* Make an expression to refer to the COMPONENT field of structure or
2278 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2279 location of the COMPONENT_REF. */
400fbf9f
JW
2280
2281tree
c2255bc4 2282build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2283{
b3694847
SS
2284 tree type = TREE_TYPE (datum);
2285 enum tree_code code = TREE_CODE (type);
2286 tree field = NULL;
2287 tree ref;
1e57bf47 2288 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2289
7a3ea201
RH
2290 if (!objc_is_public (datum, component))
2291 return error_mark_node;
2292
46a88c12 2293 /* Detect Objective-C property syntax object.property. */
668ea4b1 2294 if (c_dialect_objc ()
46a88c12 2295 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2296 return ref;
2297
400fbf9f
JW
2298 /* See if there is a field or component with name COMPONENT. */
2299
2300 if (code == RECORD_TYPE || code == UNION_TYPE)
2301 {
d0f062fb 2302 if (!COMPLETE_TYPE_P (type))
400fbf9f 2303 {
7a228918 2304 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2305 return error_mark_node;
2306 }
2307
0fb96aa4 2308 field = lookup_field (type, component);
400fbf9f
JW
2309
2310 if (!field)
2311 {
c2255bc4 2312 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2313 return error_mark_node;
2314 }
400fbf9f 2315
e9b2c823
NB
2316 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2317 This might be better solved in future the way the C++ front
2318 end does it - by giving the anonymous entities each a
2319 separate name and type, and then have build_component_ref
2320 recursively call itself. We can't do that here. */
46ea50cb 2321 do
19d76e60 2322 {
e9b2c823 2323 tree subdatum = TREE_VALUE (field);
efed193e
JM
2324 int quals;
2325 tree subtype;
1e57bf47 2326 bool use_datum_quals;
e9b2c823
NB
2327
2328 if (TREE_TYPE (subdatum) == error_mark_node)
2329 return error_mark_node;
2330
1e57bf47
JM
2331 /* If this is an rvalue, it does not have qualifiers in C
2332 standard terms and we must avoid propagating such
2333 qualifiers down to a non-lvalue array that is then
2334 converted to a pointer. */
2335 use_datum_quals = (datum_lvalue
2336 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2337
efed193e 2338 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2339 if (use_datum_quals)
2340 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2341 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2342
2343 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2344 NULL_TREE);
c2255bc4 2345 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2346 if (TREE_READONLY (subdatum)
2347 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2348 TREE_READONLY (ref) = 1;
1e57bf47
JM
2349 if (TREE_THIS_VOLATILE (subdatum)
2350 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2351 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2352
2353 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2354 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2355
19d76e60 2356 datum = ref;
46ea50cb
RS
2357
2358 field = TREE_CHAIN (field);
19d76e60 2359 }
46ea50cb 2360 while (field);
19d76e60 2361
400fbf9f
JW
2362 return ref;
2363 }
2364 else if (code != ERROR_MARK)
c2255bc4
AH
2365 error_at (loc,
2366 "request for member %qE in something not a structure or union",
2367 component);
400fbf9f
JW
2368
2369 return error_mark_node;
2370}
2371\f
2372/* Given an expression PTR for a pointer, return an expression
2373 for the value pointed to.
6a3799eb
AH
2374 ERRORSTRING is the name of the operator to appear in error messages.
2375
2376 LOC is the location to use for the generated tree. */
400fbf9f
JW
2377
2378tree
dd865ef6 2379build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2380{
b3694847
SS
2381 tree pointer = default_conversion (ptr);
2382 tree type = TREE_TYPE (pointer);
6a3799eb 2383 tree ref;
400fbf9f
JW
2384
2385 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2386 {
1043771b 2387 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2388 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2389 {
2390 /* If a warning is issued, mark it to avoid duplicates from
2391 the backend. This only needs to be done at
2392 warn_strict_aliasing > 2. */
2393 if (warn_strict_aliasing > 2)
2394 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2395 type, TREE_OPERAND (pointer, 0)))
2396 TREE_NO_WARNING (pointer) = 1;
2397 }
2398
870cc33b 2399 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2400 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2401 == TREE_TYPE (type)))
6a3799eb
AH
2402 {
2403 ref = TREE_OPERAND (pointer, 0);
2404 protected_set_expr_location (ref, loc);
2405 return ref;
2406 }
870cc33b
RS
2407 else
2408 {
2409 tree t = TREE_TYPE (type);
46df2823 2410
984dfd8c 2411 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2412
baae9b65 2413 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2414 {
d9b7be2e
MP
2415 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2416 {
2417 error_at (loc, "dereferencing pointer to incomplete type "
2418 "%qT", t);
2419 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2420 }
870cc33b
RS
2421 return error_mark_node;
2422 }
7d882b83 2423 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2424 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2425
2426 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2427 so that we get the proper error message if the result is used
2428 to assign to. Also, &* is supposed to be a no-op.
2429 And ANSI C seems to specify that the type of the result
2430 should be the const type. */
2431 /* A de-reference of a pointer to const is not a const. It is valid
2432 to change it via some other pointer. */
2433 TREE_READONLY (ref) = TYPE_READONLY (t);
2434 TREE_SIDE_EFFECTS (ref)
271bd540 2435 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2436 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2437 protected_set_expr_location (ref, loc);
870cc33b
RS
2438 return ref;
2439 }
2440 }
400fbf9f 2441 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2442 invalid_indirection_error (loc, type, errstring);
2443
400fbf9f
JW
2444 return error_mark_node;
2445}
2446
2447/* This handles expressions of the form "a[i]", which denotes
2448 an array reference.
2449
2450 This is logically equivalent in C to *(a+i), but we may do it differently.
2451 If A is a variable or a member, we generate a primitive ARRAY_REF.
2452 This avoids forcing the array out of registers, and can work on
2453 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2454 by functions).
2455
30cd1c5d
AS
2456 For vector types, allow vector[i] but not i[vector], and create
2457 *(((type*)&vectortype) + i) for the expression.
2458
6a3799eb 2459 LOC is the location to use for the returned expression. */
400fbf9f
JW
2460
2461tree
c2255bc4 2462build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2463{
6a3799eb 2464 tree ret;
a4ab7973 2465 bool swapped = false;
400fbf9f
JW
2466 if (TREE_TYPE (array) == error_mark_node
2467 || TREE_TYPE (index) == error_mark_node)
2468 return error_mark_node;
2469
b72271b9 2470 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2471 {
2472 size_t rank = 0;
2473 if (!find_rank (loc, index, index, true, &rank))
2474 return error_mark_node;
2475 if (rank > 1)
2476 {
2477 error_at (loc, "rank of the array's index is greater than 1");
2478 return error_mark_node;
2479 }
2480 }
a4ab7973 2481 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2482 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2483 /* Allow vector[index] but not index[vector]. */
2484 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2485 {
a4ab7973
JM
2486 tree temp;
2487 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2488 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2489 {
f90e8e2e 2490 error_at (loc,
30cd1c5d
AS
2491 "subscripted value is neither array nor pointer nor vector");
2492
fdeefd49
RS
2493 return error_mark_node;
2494 }
a4ab7973
JM
2495 temp = array;
2496 array = index;
2497 index = temp;
2498 swapped = true;
2499 }
2500
2501 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2502 {
a63068b6 2503 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2504 return error_mark_node;
2505 }
2506
2507 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2508 {
a63068b6 2509 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2510 return error_mark_node;
2511 }
2512
ff6b6641
GDR
2513 /* ??? Existing practice has been to warn only when the char
2514 index is syntactically the index, not for char[array]. */
2515 if (!swapped)
5bd012f8 2516 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2517
2518 /* Apply default promotions *after* noticing character types. */
2519 index = default_conversion (index);
f406ae1f
MP
2520 if (index == error_mark_node)
2521 return error_mark_node;
a4ab7973
JM
2522
2523 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2524
aa7da51a
JJ
2525 bool non_lvalue
2526 = convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2527
2528 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2529 {
e4d35515 2530 tree rval, type;
fdeefd49 2531
400fbf9f
JW
2532 /* An array that is indexed by a non-constant
2533 cannot be stored in a register; we must be able to do
2534 address arithmetic on its address.
2535 Likewise an array of elements of variable size. */
2536 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2537 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2538 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2539 {
dffd7eb6 2540 if (!c_mark_addressable (array))
400fbf9f
JW
2541 return error_mark_node;
2542 }
e6d52559
JW
2543 /* An array that is indexed by a constant value which is not within
2544 the array bounds cannot be stored in a register either; because we
2545 would get a crash in store_bit_field/extract_bit_field when trying
2546 to access a non-existent part of the register. */
2547 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2548 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2549 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2550 {
dffd7eb6 2551 if (!c_mark_addressable (array))
e6d52559
JW
2552 return error_mark_node;
2553 }
400fbf9f 2554
f3bede71 2555 if (pedantic || warn_c90_c99_compat)
400fbf9f
JW
2556 {
2557 tree foo = array;
2558 while (TREE_CODE (foo) == COMPONENT_REF)
2559 foo = TREE_OPERAND (foo, 0);
5baeaac0 2560 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2561 pedwarn (loc, OPT_Wpedantic,
fcf73884 2562 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2563 else if (!lvalue_p (foo))
2564 pedwarn_c90 (loc, OPT_Wpedantic,
2565 "ISO C90 forbids subscripting non-lvalue "
2566 "array");
400fbf9f
JW
2567 }
2568
46df2823 2569 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2570 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2571 /* Array ref is const/volatile if the array elements are
c22cacf3 2572 or if the array is. */
400fbf9f
JW
2573 TREE_READONLY (rval)
2574 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2575 | TREE_READONLY (array));
2576 TREE_SIDE_EFFECTS (rval)
2577 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2578 | TREE_SIDE_EFFECTS (array));
2579 TREE_THIS_VOLATILE (rval)
2580 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2581 /* This was added by rms on 16 Nov 91.
2f6e4e97 2582 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2583 in an inline function.
2584 Hope it doesn't break something else. */
2585 | TREE_THIS_VOLATILE (array));
928c19bb 2586 ret = require_complete_type (rval);
6a3799eb 2587 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2588 if (non_lvalue)
2589 ret = non_lvalue_loc (loc, ret);
6a3799eb 2590 return ret;
400fbf9f 2591 }
a4ab7973
JM
2592 else
2593 {
2594 tree ar = default_conversion (array);
400fbf9f 2595
a4ab7973
JM
2596 if (ar == error_mark_node)
2597 return ar;
400fbf9f 2598
a4ab7973
JM
2599 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2600 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2601
aa7da51a
JJ
2602 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2603 index, 0),
2604 RO_ARRAY_INDEXING);
2605 if (non_lvalue)
2606 ret = non_lvalue_loc (loc, ret);
2607 return ret;
a4ab7973 2608 }
400fbf9f
JW
2609}
2610\f
7e585d16 2611/* Build an external reference to identifier ID. FUN indicates
766beb40 2612 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2613 location of the identifier. This sets *TYPE to the type of the
2614 identifier, which is not the same as the type of the returned value
2615 for CONST_DECLs defined as enum constants. If the type of the
2616 identifier is not available, *TYPE is set to NULL. */
7e585d16 2617tree
c2255bc4 2618build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2619{
2620 tree ref;
2621 tree decl = lookup_name (id);
16b34ad6
ZL
2622
2623 /* In Objective-C, an instance variable (ivar) may be preferred to
2624 whatever lookup_name() found. */
2625 decl = objc_lookup_ivar (decl, id);
7e585d16 2626
6866c6e8 2627 *type = NULL;
339a28b9 2628 if (decl && decl != error_mark_node)
6866c6e8
ILT
2629 {
2630 ref = decl;
2631 *type = TREE_TYPE (ref);
2632 }
339a28b9
ZW
2633 else if (fun)
2634 /* Implicit function declaration. */
c2255bc4 2635 ref = implicitly_declare (loc, id);
339a28b9
ZW
2636 else if (decl == error_mark_node)
2637 /* Don't complain about something that's already been
2638 complained about. */
2639 return error_mark_node;
2640 else
2641 {
c2255bc4 2642 undeclared_variable (loc, id);
339a28b9
ZW
2643 return error_mark_node;
2644 }
7e585d16
ZW
2645
2646 if (TREE_TYPE (ref) == error_mark_node)
2647 return error_mark_node;
2648
339a28b9 2649 if (TREE_DEPRECATED (ref))
9b86d6bb 2650 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2651
ad960f56 2652 /* Recursive call does not count as usage. */
b8698a0f 2653 if (ref != current_function_decl)
ad960f56 2654 {
ad960f56
MLI
2655 TREE_USED (ref) = 1;
2656 }
7e585d16 2657
bc4b653b
JM
2658 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2659 {
2660 if (!in_sizeof && !in_typeof)
2661 C_DECL_USED (ref) = 1;
2662 else if (DECL_INITIAL (ref) == 0
2663 && DECL_EXTERNAL (ref)
2664 && !TREE_PUBLIC (ref))
2665 record_maybe_used_decl (ref);
2666 }
2667
7e585d16
ZW
2668 if (TREE_CODE (ref) == CONST_DECL)
2669 {
6193b8b7 2670 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2671
2672 if (warn_cxx_compat
2673 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2674 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2675 {
2676 warning_at (loc, OPT_Wc___compat,
2677 ("enum constant defined in struct or union "
2678 "is not visible in C++"));
2679 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2680 }
2681
7e585d16
ZW
2682 ref = DECL_INITIAL (ref);
2683 TREE_CONSTANT (ref) = 1;
2684 }
6a29edea 2685 else if (current_function_decl != 0
4b1e44be 2686 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2687 && (TREE_CODE (ref) == VAR_DECL
2688 || TREE_CODE (ref) == PARM_DECL
2689 || TREE_CODE (ref) == FUNCTION_DECL))
2690 {
2691 tree context = decl_function_context (ref);
2f6e4e97 2692
6a29edea
EB
2693 if (context != 0 && context != current_function_decl)
2694 DECL_NONLOCAL (ref) = 1;
2695 }
71113fcd
GK
2696 /* C99 6.7.4p3: An inline definition of a function with external
2697 linkage ... shall not contain a reference to an identifier with
2698 internal linkage. */
2699 else if (current_function_decl != 0
2700 && DECL_DECLARED_INLINE_P (current_function_decl)
2701 && DECL_EXTERNAL (current_function_decl)
2702 && VAR_OR_FUNCTION_DECL_P (ref)
2703 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2704 && ! TREE_PUBLIC (ref)
2705 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2706 record_inline_static (loc, current_function_decl, ref,
2707 csi_internal);
7e585d16
ZW
2708
2709 return ref;
2710}
2711
bc4b653b
JM
2712/* Record details of decls possibly used inside sizeof or typeof. */
2713struct maybe_used_decl
2714{
2715 /* The decl. */
2716 tree decl;
2717 /* The level seen at (in_sizeof + in_typeof). */
2718 int level;
2719 /* The next one at this level or above, or NULL. */
2720 struct maybe_used_decl *next;
2721};
2722
2723static struct maybe_used_decl *maybe_used_decls;
2724
2725/* Record that DECL, an undefined static function reference seen
2726 inside sizeof or typeof, might be used if the operand of sizeof is
2727 a VLA type or the operand of typeof is a variably modified
2728 type. */
2729
4e2fb7de 2730static void
bc4b653b
JM
2731record_maybe_used_decl (tree decl)
2732{
2733 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2734 t->decl = decl;
2735 t->level = in_sizeof + in_typeof;
2736 t->next = maybe_used_decls;
2737 maybe_used_decls = t;
2738}
2739
2740/* Pop the stack of decls possibly used inside sizeof or typeof. If
2741 USED is false, just discard them. If it is true, mark them used
2742 (if no longer inside sizeof or typeof) or move them to the next
2743 level up (if still inside sizeof or typeof). */
2744
2745void
2746pop_maybe_used (bool used)
2747{
2748 struct maybe_used_decl *p = maybe_used_decls;
2749 int cur_level = in_sizeof + in_typeof;
2750 while (p && p->level > cur_level)
2751 {
2752 if (used)
2753 {
2754 if (cur_level == 0)
2755 C_DECL_USED (p->decl) = 1;
2756 else
2757 p->level = cur_level;
2758 }
2759 p = p->next;
2760 }
2761 if (!used || cur_level == 0)
2762 maybe_used_decls = p;
2763}
2764
2765/* Return the result of sizeof applied to EXPR. */
2766
2767struct c_expr
c2255bc4 2768c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2769{
2770 struct c_expr ret;
ad97f4be
JM
2771 if (expr.value == error_mark_node)
2772 {
2773 ret.value = error_mark_node;
2774 ret.original_code = ERROR_MARK;
6866c6e8 2775 ret.original_type = NULL;
ad97f4be
JM
2776 pop_maybe_used (false);
2777 }
2778 else
2779 {
928c19bb 2780 bool expr_const_operands = true;
773ec47f
MP
2781
2782 if (TREE_CODE (expr.value) == PARM_DECL
2783 && C_ARRAY_PARAMETER (expr.value))
2784 {
2785 if (warning_at (loc, OPT_Wsizeof_array_argument,
2786 "%<sizeof%> on array function parameter %qE will "
2787 "return size of %qT", expr.value,
2788 expr.original_type))
2789 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2790 }
928c19bb
JM
2791 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2792 &expr_const_operands);
c2255bc4 2793 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2794 c_last_sizeof_arg = expr.value;
2795 ret.original_code = SIZEOF_EXPR;
6866c6e8 2796 ret.original_type = NULL;
928c19bb 2797 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2798 {
2799 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2800 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2801 folded_expr, ret.value);
2802 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2803 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2804 }
928c19bb 2805 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2806 }
bc4b653b
JM
2807 return ret;
2808}
2809
2810/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2811 name passed to sizeof (rather than the type itself). LOC is the
2812 location of the original expression. */
bc4b653b
JM
2813
2814struct c_expr
c2255bc4 2815c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2816{
2817 tree type;
2818 struct c_expr ret;
928c19bb
JM
2819 tree type_expr = NULL_TREE;
2820 bool type_expr_const = true;
2821 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2822 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2823 c_last_sizeof_arg = type;
2824 ret.original_code = SIZEOF_EXPR;
6866c6e8 2825 ret.original_type = NULL;
24070fcb
JM
2826 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2827 && c_vla_type_p (type))
928c19bb 2828 {
24070fcb
JM
2829 /* If the type is a [*] array, it is a VLA but is represented as
2830 having a size of zero. In such a case we must ensure that
2831 the result of sizeof does not get folded to a constant by
2832 c_fully_fold, because if the size is evaluated the result is
2833 not constant and so constraints on zero or negative size
2834 arrays must not be applied when this sizeof call is inside
2835 another array declarator. */
2836 if (!type_expr)
2837 type_expr = integer_zero_node;
928c19bb
JM
2838 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2839 type_expr, ret.value);
2840 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2841 }
16464cc1
VR
2842 pop_maybe_used (type != error_mark_node
2843 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2844 return ret;
2845}
2846
400fbf9f 2847/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2848 The function call is at LOC.
400fbf9f
JW
2849 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2850 TREE_VALUE of each node is a parameter-expression.
2851 FUNCTION's data type may be a function type or a pointer-to-function. */
2852
2853tree
c2255bc4 2854build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2855{
9771b263 2856 vec<tree, va_gc> *v;
bbbbb16a
ILT
2857 tree ret;
2858
9771b263 2859 vec_alloc (v, list_length (params));
bbbbb16a 2860 for (; params; params = TREE_CHAIN (params))
9771b263 2861 v->quick_push (TREE_VALUE (params));
8edbfaa6 2862 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2863 vec_free (v);
bbbbb16a
ILT
2864 return ret;
2865}
2866
ae52741c
MLI
2867/* Give a note about the location of the declaration of DECL. */
2868
2869static void inform_declaration (tree decl)
2870{
2871 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2872 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2873}
2874
bbbbb16a
ILT
2875/* Build a function call to function FUNCTION with parameters PARAMS.
2876 ORIGTYPES, if not NULL, is a vector of types; each element is
2877 either NULL or the original type of the corresponding element in
2878 PARAMS. The original type may differ from TREE_TYPE of the
2879 parameter for enums. FUNCTION's data type may be a function type
2880 or pointer-to-function. This function changes the elements of
2881 PARAMS. */
2882
2883tree
81e5eca8
MP
2884build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2885 tree function, vec<tree, va_gc> *params,
9771b263 2886 vec<tree, va_gc> *origtypes)
400fbf9f 2887{
b3694847 2888 tree fntype, fundecl = 0;
4977bab6 2889 tree name = NULL_TREE, result;
c96f4f73 2890 tree tem;
94a0dd7b
SL
2891 int nargs;
2892 tree *argarray;
b8698a0f 2893
400fbf9f 2894
fc76e425 2895 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2896 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2897
2898 /* Convert anything with function type to a pointer-to-function. */
2899 if (TREE_CODE (function) == FUNCTION_DECL)
2900 {
2901 name = DECL_NAME (function);
0a35513e
AH
2902
2903 if (flag_tm)
2904 tm_malloc_replacement (function);
a5eadacc 2905 fundecl = function;
86951993
AM
2906 /* Atomic functions have type checking/casting already done. They are
2907 often rewritten and don't match the original parameter list. */
2908 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2909 origtypes = NULL;
36536d79 2910
b72271b9 2911 if (flag_cilkplus
36536d79
BI
2912 && is_cilkplus_reduce_builtin (function))
2913 origtypes = NULL;
400fbf9f 2914 }
f2a71bbc 2915 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2916 function = function_to_pointer_conversion (loc, function);
400fbf9f 2917
6e955430
ZL
2918 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2919 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2920 if (params && !params->is_empty ())
2921 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2922
928c19bb
JM
2923 function = c_fully_fold (function, false, NULL);
2924
400fbf9f
JW
2925 fntype = TREE_TYPE (function);
2926
2927 if (TREE_CODE (fntype) == ERROR_MARK)
2928 return error_mark_node;
2929
2930 if (!(TREE_CODE (fntype) == POINTER_TYPE
2931 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2932 {
ae52741c
MLI
2933 if (!flag_diagnostics_show_caret)
2934 error_at (loc,
2935 "called object %qE is not a function or function pointer",
2936 function);
2937 else if (DECL_P (function))
2938 {
2939 error_at (loc,
2940 "called object %qD is not a function or function pointer",
2941 function);
2942 inform_declaration (function);
2943 }
2944 else
2945 error_at (loc,
2946 "called object is not a function or function pointer");
400fbf9f
JW
2947 return error_mark_node;
2948 }
2949
5ce89b2e
JM
2950 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2951 current_function_returns_abnormally = 1;
2952
400fbf9f
JW
2953 /* fntype now gets the type of function pointed to. */
2954 fntype = TREE_TYPE (fntype);
2955
ab4194da
JM
2956 /* Convert the parameters to the types declared in the
2957 function prototype, or apply default promotions. */
2958
81e5eca8
MP
2959 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2960 origtypes, function, fundecl);
ab4194da
JM
2961 if (nargs < 0)
2962 return error_mark_node;
2963
c96f4f73 2964 /* Check that the function is called through a compatible prototype.
fa337f3a 2965 If it is not, warn. */
1043771b 2966 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2967 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2968 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2969 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2970 {
2971 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
2972
2973 /* This situation leads to run-time undefined behavior. We can't,
2974 therefore, simply error unless we can prove that all possible
2975 executions of the program must execute the code. */
fa337f3a 2976 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 2977
fa337f3a
RB
2978 if (VOID_TYPE_P (return_type)
2979 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2980 pedwarn (loc, 0,
2981 "function with qualified void return type called");
2982 }
c96f4f73 2983
9771b263 2984 argarray = vec_safe_address (params);
bbbbb16a 2985
83322951
RG
2986 /* Check that arguments to builtin functions match the expectations. */
2987 if (fundecl
2988 && DECL_BUILT_IN (fundecl)
2989 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2990 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2991 return error_mark_node;
400fbf9f 2992
83322951 2993 /* Check that the arguments to the function are valid. */
dde05067 2994 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2995
928c19bb
JM
2996 if (name != NULL_TREE
2997 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2998 {
928c19bb 2999 if (require_constant_value)
b8698a0f 3000 result =
db3927fb
AH
3001 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3002 function, nargs, argarray);
928c19bb 3003 else
db3927fb
AH
3004 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3005 function, nargs, argarray);
928c19bb
JM
3006 if (TREE_CODE (result) == NOP_EXPR
3007 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3008 STRIP_TYPE_NOPS (result);
bf730f15
RS
3009 }
3010 else
db3927fb
AH
3011 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3012 function, nargs, argarray);
b0b3afb2 3013
71653180 3014 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
3015 {
3016 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 3017 pedwarn (loc, 0,
3ce62965
JM
3018 "function with qualified void return type called");
3019 return result;
3020 }
1eb8759b 3021 return require_complete_type (result);
400fbf9f 3022}
8edbfaa6
JJ
3023
3024/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3025
3026tree
3027c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3028 tree function, vec<tree, va_gc> *params,
3029 vec<tree, va_gc> *origtypes)
3030{
3031 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3032 STRIP_TYPE_NOPS (function);
3033
3034 /* Convert anything with function type to a pointer-to-function. */
3035 if (TREE_CODE (function) == FUNCTION_DECL)
3036 {
3037 /* Implement type-directed function overloading for builtins.
3038 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3039 handle all the type checking. The result is a complete expression
3040 that implements this function call. */
3041 tree tem = resolve_overloaded_builtin (loc, function, params);
3042 if (tem)
3043 return tem;
3044 }
3045 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3046}
400fbf9f 3047\f
bbbbb16a
ILT
3048/* Convert the argument expressions in the vector VALUES
3049 to the types in the list TYPELIST.
400fbf9f
JW
3050
3051 If TYPELIST is exhausted, or when an element has NULL as its type,
3052 perform the default conversions.
3053
bbbbb16a
ILT
3054 ORIGTYPES is the original types of the expressions in VALUES. This
3055 holds the type of enum values which have been converted to integral
3056 types. It may be NULL.
400fbf9f 3057
03dafa61
JM
3058 FUNCTION is a tree for the called function. It is used only for
3059 error messages, where it is formatted with %qE.
400fbf9f
JW
3060
3061 This is also where warnings about wrong number of args are generated.
3062
81e5eca8
MP
3063 ARG_LOC are locations of function arguments (if any).
3064
94a0dd7b 3065 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3066 than the length of VALUES in some error situations), or -1 on
3067 failure. */
94a0dd7b
SL
3068
3069static int
81e5eca8
MP
3070convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3071 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3072 tree function, tree fundecl)
400fbf9f 3073{
bbbbb16a
ILT
3074 tree typetail, val;
3075 unsigned int parmnum;
06302a02 3076 bool error_args = false;
b5d32c25 3077 const bool type_generic = fundecl
81e5eca8 3078 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3079 bool type_generic_remove_excess_precision = false;
03dafa61 3080 tree selector;
03dafa61 3081
2ac2f164
JM
3082 /* Change pointer to function to the function itself for
3083 diagnostics. */
03dafa61
JM
3084 if (TREE_CODE (function) == ADDR_EXPR
3085 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3086 function = TREE_OPERAND (function, 0);
03dafa61
JM
3087
3088 /* Handle an ObjC selector specially for diagnostics. */
3089 selector = objc_message_selector ();
400fbf9f 3090
8ce94e44
JM
3091 /* For type-generic built-in functions, determine whether excess
3092 precision should be removed (classification) or not
3093 (comparison). */
3094 if (type_generic
3095 && DECL_BUILT_IN (fundecl)
3096 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3097 {
3098 switch (DECL_FUNCTION_CODE (fundecl))
3099 {
3100 case BUILT_IN_ISFINITE:
3101 case BUILT_IN_ISINF:
3102 case BUILT_IN_ISINF_SIGN:
3103 case BUILT_IN_ISNAN:
3104 case BUILT_IN_ISNORMAL:
3105 case BUILT_IN_FPCLASSIFY:
3106 type_generic_remove_excess_precision = true;
3107 break;
3108
3109 default:
3110 type_generic_remove_excess_precision = false;
3111 break;
3112 }
3113 }
b72271b9 3114 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3115 return vec_safe_length (values);
8ce94e44 3116
400fbf9f 3117 /* Scan the given expressions and types, producing individual
bbbbb16a 3118 converted arguments. */
400fbf9f 3119
bbbbb16a 3120 for (typetail = typelist, parmnum = 0;
9771b263 3121 values && values->iterate (parmnum, &val);
bbbbb16a 3122 ++parmnum)
400fbf9f 3123 {
b3694847 3124 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3125 tree valtype = TREE_TYPE (val);
03dafa61
JM
3126 tree rname = function;
3127 int argnum = parmnum + 1;
4d3e6fae 3128 const char *invalid_func_diag;
8ce94e44 3129 bool excess_precision = false;
928c19bb 3130 bool npc;
bbbbb16a 3131 tree parmval;
5c1bc275
MP
3132 /* Some __atomic_* builtins have additional hidden argument at
3133 position 0. */
3134 location_t ploc
3135 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3136 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3137 : input_location;
400fbf9f
JW
3138
3139 if (type == void_type_node)
3140 {
19dc6d01 3141 if (selector)
68fca595 3142 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3143 else
68fca595 3144 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3145 inform_declaration (fundecl);
d38f7dce 3146 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3147 }
3148
03dafa61
JM
3149 if (selector && argnum > 2)
3150 {
3151 rname = selector;
3152 argnum -= 2;
3153 }
3154
928c19bb 3155 npc = null_pointer_constant_p (val);
8ce94e44
JM
3156
3157 /* If there is excess precision and a prototype, convert once to
3158 the required type rather than converting via the semantic
3159 type. Likewise without a prototype a float value represented
3160 as long double should be converted once to double. But for
3161 type-generic classification functions excess precision must
3162 be removed here. */
3163 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3164 && (type || !type_generic || !type_generic_remove_excess_precision))
3165 {
3166 val = TREE_OPERAND (val, 0);
3167 excess_precision = true;
3168 }
928c19bb 3169 val = c_fully_fold (val, false, NULL);
ed248cf7 3170 STRIP_TYPE_NOPS (val);
400fbf9f 3171
400fbf9f
JW
3172 val = require_complete_type (val);
3173
3174 if (type != 0)
3175 {
3176 /* Formal parm type is specified by a function prototype. */
400fbf9f 3177
20913689 3178 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3179 {
5c1bc275
MP
3180 error_at (ploc, "type of formal parameter %d is incomplete",
3181 parmnum + 1);
400fbf9f
JW
3182 parmval = val;
3183 }
3184 else
3185 {
bbbbb16a
ILT
3186 tree origtype;
3187
d45cf215
RS
3188 /* Optionally warn about conversions that
3189 differ from the default conversions. */
05170031 3190 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3191 {
e3a64162 3192 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3193
aae43c5f 3194 if (INTEGRAL_TYPE_P (type)
8ce94e44 3195 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3196 warning_at (ploc, OPT_Wtraditional_conversion,
3197 "passing argument %d of %qE as integer rather "
3198 "than floating due to prototype",
3199 argnum, rname);
03829ad2 3200 if (INTEGRAL_TYPE_P (type)
8ce94e44 3201 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3202 warning_at (ploc, OPT_Wtraditional_conversion,
3203 "passing argument %d of %qE as integer rather "
3204 "than complex due to prototype",
3205 argnum, rname);
aae43c5f 3206 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3207 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3208 warning_at (ploc, OPT_Wtraditional_conversion,
3209 "passing argument %d of %qE as complex rather "
3210 "than floating due to prototype",
3211 argnum, rname);
400fbf9f 3212 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3213 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3214 warning_at (ploc, OPT_Wtraditional_conversion,
3215 "passing argument %d of %qE as floating rather "
3216 "than integer due to prototype",
3217 argnum, rname);
03829ad2 3218 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3219 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3220 warning_at (ploc, OPT_Wtraditional_conversion,
3221 "passing argument %d of %qE as complex rather "
3222 "than integer due to prototype",
3223 argnum, rname);
aae43c5f 3224 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3225 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3226 warning_at (ploc, OPT_Wtraditional_conversion,
3227 "passing argument %d of %qE as floating rather "
3228 "than complex due to prototype",
3229 argnum, rname);
aae43c5f
RK
3230 /* ??? At some point, messages should be written about
3231 conversions between complex types, but that's too messy
3232 to do now. */
d45cf215 3233 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3234 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3235 {
3236 /* Warn if any argument is passed as `float',
047de90b 3237 since without a prototype it would be `double'. */
9a8ce21f
JG
3238 if (formal_prec == TYPE_PRECISION (float_type_node)
3239 && type != dfloat32_type_node)
5c1bc275
MP
3240 warning_at (ploc, 0,
3241 "passing argument %d of %qE as %<float%> "
3242 "rather than %<double%> due to prototype",
3243 argnum, rname);
9a8ce21f
JG
3244
3245 /* Warn if mismatch between argument and prototype
3246 for decimal float types. Warn of conversions with
3247 binary float types and of precision narrowing due to
3248 prototype. */
8ce94e44 3249 else if (type != valtype
9a8ce21f
JG
3250 && (type == dfloat32_type_node
3251 || type == dfloat64_type_node
c22cacf3 3252 || type == dfloat128_type_node
8ce94e44
JM
3253 || valtype == dfloat32_type_node
3254 || valtype == dfloat64_type_node
3255 || valtype == dfloat128_type_node)
c22cacf3 3256 && (formal_prec
8ce94e44 3257 <= TYPE_PRECISION (valtype)
9a8ce21f 3258 || (type == dfloat128_type_node
8ce94e44 3259 && (valtype
c22cacf3 3260 != dfloat64_type_node
8ce94e44 3261 && (valtype
9a8ce21f
JG
3262 != dfloat32_type_node)))
3263 || (type == dfloat64_type_node
8ce94e44 3264 && (valtype
9a8ce21f 3265 != dfloat32_type_node))))
5c1bc275
MP
3266 warning_at (ploc, 0,
3267 "passing argument %d of %qE as %qT "
3268 "rather than %qT due to prototype",
3269 argnum, rname, type, valtype);
9a8ce21f 3270
d45cf215 3271 }
3ed56f8a
KG
3272 /* Detect integer changing in width or signedness.
3273 These warnings are only activated with
05170031
MLI
3274 -Wtraditional-conversion, not with -Wtraditional. */
3275 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3276 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3277 {
d45cf215
RS
3278 tree would_have_been = default_conversion (val);
3279 tree type1 = TREE_TYPE (would_have_been);
3280
754a4d82 3281 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3282 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3283 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3284 /* No warning if function asks for enum
3285 and the actual arg is that enum type. */
3286 ;
3287 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3288 warning_at (ploc, OPT_Wtraditional_conversion,
3289 "passing argument %d of %qE "
3290 "with different width due to prototype",
3291 argnum, rname);
8df83eae 3292 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3293 ;
800cd3b9
RS
3294 /* Don't complain if the formal parameter type
3295 is an enum, because we can't tell now whether
3296 the value was an enum--even the same enum. */
3297 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3298 ;
400fbf9f
JW
3299 else if (TREE_CODE (val) == INTEGER_CST
3300 && int_fits_type_p (val, type))
3301 /* Change in signedness doesn't matter
3302 if a constant value is unaffected. */
3303 ;
ce9895ae
RS
3304 /* If the value is extended from a narrower
3305 unsigned type, it doesn't matter whether we
3306 pass it as signed or unsigned; the value
3307 certainly is the same either way. */
8ce94e44
JM
3308 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3309 && TYPE_UNSIGNED (valtype))
ce9895ae 3310 ;
8df83eae 3311 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3312 warning_at (ploc, OPT_Wtraditional_conversion,
3313 "passing argument %d of %qE "
3314 "as unsigned due to prototype",
3315 argnum, rname);
3ed56f8a 3316 else
5c1bc275
MP
3317 warning_at (ploc, OPT_Wtraditional_conversion,
3318 "passing argument %d of %qE "
3319 "as signed due to prototype",
3320 argnum, rname);
400fbf9f
JW
3321 }
3322 }
3323
8ce94e44
JM
3324 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3325 sake of better warnings from convert_and_check. */
3326 if (excess_precision)
3327 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3328 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3329 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3330 val, origtype, ic_argpass,
3331 npc, fundecl, function,
2ac2f164 3332 parmnum + 1);
2f6e4e97 3333
61f71b34 3334 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3335 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3336 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3337 parmval = default_conversion (parmval);
400fbf9f 3338 }
400fbf9f 3339 }
8ce94e44
JM
3340 else if (TREE_CODE (valtype) == REAL_TYPE
3341 && (TYPE_PRECISION (valtype)
2531a1d9 3342 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3343 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3344 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3345 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3346 {
3347 if (type_generic)
bbbbb16a 3348 parmval = val;
b5d32c25 3349 else
0a0b3574
MM
3350 {
3351 /* Convert `float' to `double'. */
3352 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3353 warning_at (ploc, OPT_Wdouble_promotion,
3354 "implicit conversion from %qT to %qT when passing "
3355 "argument to function",
3356 valtype, double_type_node);
0a0b3574
MM
3357 parmval = convert (double_type_node, val);
3358 }
b5d32c25 3359 }
8ce94e44
JM
3360 else if (excess_precision && !type_generic)
3361 /* A "double" argument with excess precision being passed
3362 without a prototype or in variable arguments. */
bbbbb16a 3363 parmval = convert (valtype, val);
c22cacf3
MS
3364 else if ((invalid_func_diag =
3365 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3366 {
3367 error (invalid_func_diag);
94a0dd7b 3368 return -1;
4d3e6fae 3369 }
400fbf9f
JW
3370 else
3371 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3372 parmval = default_conversion (val);
3373
9771b263 3374 (*values)[parmnum] = parmval;
06302a02
JJ
3375 if (parmval == error_mark_node)
3376 error_args = true;
400fbf9f
JW
3377
3378 if (typetail)
3379 typetail = TREE_CHAIN (typetail);
3380 }
3381
9771b263 3382 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3383
400fbf9f 3384 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3385 {
68fca595 3386 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3387 inform_declaration (fundecl);
3388 return -1;
3789b316 3389 }
400fbf9f 3390
06302a02 3391 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3392}
3393\f
43f6dfd3
RS
3394/* This is the entry point used by the parser to build unary operators
3395 in the input. CODE, a tree_code, specifies the unary operator, and
3396 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3397 CONVERT_EXPR for code.
3398
3399 LOC is the location to use for the tree generated.
3400*/
43f6dfd3
RS
3401
3402struct c_expr
c2255bc4 3403parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3404{
3405 struct c_expr result;
3406
c9f9eb5d 3407 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3408 result.original_code = code;
6866c6e8
ILT
3409 result.original_type = NULL;
3410
59c0753d 3411 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3412 overflow_warning (loc, result.value);
59c0753d 3413
43f6dfd3
RS
3414 return result;
3415}
3416
3417/* This is the entry point used by the parser to build binary operators
3418 in the input. CODE, a tree_code, specifies the binary operator, and
3419 ARG1 and ARG2 are the operands. In addition to constructing the
3420 expression, we check for operands that were written with other binary
ba47d38d
AH
3421 operators in a way that is likely to confuse the user.
3422
3423 LOCATION is the location of the binary operator. */
edc7c4ec 3424
487a92fe 3425struct c_expr
ba47d38d
AH
3426parser_build_binary_op (location_t location, enum tree_code code,
3427 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3428{
487a92fe 3429 struct c_expr result;
400fbf9f 3430
487a92fe
JM
3431 enum tree_code code1 = arg1.original_code;
3432 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3433 tree type1 = (arg1.original_type
3434 ? arg1.original_type
3435 : TREE_TYPE (arg1.value));
3436 tree type2 = (arg2.original_type
3437 ? arg2.original_type
3438 : TREE_TYPE (arg2.value));
400fbf9f 3439
ba47d38d
AH
3440 result.value = build_binary_op (location, code,
3441 arg1.value, arg2.value, 1);
487a92fe 3442 result.original_code = code;
6866c6e8 3443 result.original_type = NULL;
58bf601b 3444
487a92fe
JM
3445 if (TREE_CODE (result.value) == ERROR_MARK)
3446 return result;
400fbf9f 3447
ba47d38d
AH
3448 if (location != UNKNOWN_LOCATION)
3449 protected_set_expr_location (result.value, location);
3450
400fbf9f 3451 /* Check for cases such as x+y<<z which users are likely
487a92fe 3452 to misinterpret. */
400fbf9f 3453 if (warn_parentheses)
393e8e8b
MP
3454 warn_about_parentheses (location, code, code1, arg1.value, code2,
3455 arg2.value);
001af587 3456
ca409efd 3457 if (warn_logical_op)
393e8e8b 3458 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3459 code1, arg1.value, code2, arg2.value);
63a08740 3460
742938c9 3461 if (warn_logical_not_paren
47c2554f
MP
3462 && code1 == TRUTH_NOT_EXPR
3463 && code2 != TRUTH_NOT_EXPR)
59ea0364 3464 warn_logical_not_parentheses (location, code, arg2.value);
742938c9 3465
e994a705
RS
3466 /* Warn about comparisons against string literals, with the exception
3467 of testing for equality or inequality of a string literal with NULL. */
3468 if (code == EQ_EXPR || code == NE_EXPR)
3469 {
3470 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3471 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3472 warning_at (location, OPT_Waddress,
3473 "comparison with string literal results in unspecified behavior");
e994a705
RS
3474 }
3475 else if (TREE_CODE_CLASS (code) == tcc_comparison
3476 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3477 warning_at (location, OPT_Waddress,
3478 "comparison with string literal results in unspecified behavior");
e994a705 3479
b8698a0f
L
3480 if (TREE_OVERFLOW_P (result.value)
3481 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3482 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3483 overflow_warning (location, result.value);
400fbf9f 3484
6866c6e8
ILT
3485 /* Warn about comparisons of different enum types. */
3486 if (warn_enum_compare
3487 && TREE_CODE_CLASS (code) == tcc_comparison
3488 && TREE_CODE (type1) == ENUMERAL_TYPE
3489 && TREE_CODE (type2) == ENUMERAL_TYPE
3490 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3491 warning_at (location, OPT_Wenum_compare,
3492 "comparison between %qT and %qT",
3493 type1, type2);
3494
400fbf9f
JW
3495 return result;
3496}
3e4093b6 3497\f
3e4093b6
RS
3498/* Return a tree for the difference of pointers OP0 and OP1.
3499 The resulting tree has type int. */
293c9fdd 3500
3e4093b6 3501static tree
db3927fb 3502pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3503{
3e4093b6 3504 tree restype = ptrdiff_type_node;
36c5e70a 3505 tree result, inttype;
400fbf9f 3506
36c5e70a
BE
3507 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3508 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3509 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3510 tree orig_op1 = op1;
400fbf9f 3511
36c5e70a
BE
3512 /* If the operands point into different address spaces, we need to
3513 explicitly convert them to pointers into the common address space
3514 before we can subtract the numerical address values. */
3515 if (as0 != as1)
3516 {
3517 addr_space_t as_common;
3518 tree common_type;
3519
3520 /* Determine the common superset address space. This is guaranteed
3521 to exist because the caller verified that comp_target_types
3522 returned non-zero. */
3523 if (!addr_space_superset (as0, as1, &as_common))
3524 gcc_unreachable ();
3525
3526 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3527 op0 = convert (common_type, op0);
3528 op1 = convert (common_type, op1);
3529 }
3530
3531 /* Determine integer type to perform computations in. This will usually
3532 be the same as the result type (ptrdiff_t), but may need to be a wider
3533 type if pointers for the address space are wider than ptrdiff_t. */
3534 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3535 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3536 else
3537 inttype = restype;
3538
fcf73884 3539 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3540 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3541 "pointer of type %<void *%> used in subtraction");
3542 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3543 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3544 "pointer to a function used in subtraction");
400fbf9f 3545
3e4093b6
RS
3546 /* First do the subtraction as integers;
3547 then drop through to build the divide operator.
3548 Do not do default conversions on the minus operator
3549 in case restype is a short type. */
400fbf9f 3550
db3927fb 3551 op0 = build_binary_op (loc,
36c5e70a
BE
3552 MINUS_EXPR, convert (inttype, op0),
3553 convert (inttype, op1), 0);
3e4093b6
RS
3554 /* This generates an error if op1 is pointer to incomplete type. */
3555 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3556 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3557
3e4093b6 3558 op1 = c_size_in_bytes (target_type);
400fbf9f 3559
f04dda30
MP
3560 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3561 error_at (loc, "arithmetic on pointer to an empty aggregate");
3562
3e4093b6 3563 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3564 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3565 op0, convert (inttype, op1));
3566
3567 /* Convert to final result type if necessary. */
3568 return convert (restype, result);
3e4093b6
RS
3569}
3570\f
267bac10
JM
3571/* Expand atomic compound assignments into an approriate sequence as
3572 specified by the C11 standard section 6.5.16.2.
3573 given
3574 _Atomic T1 E1
3575 T2 E2
3576 E1 op= E2
3577
3578 This sequence is used for all types for which these operations are
3579 supported.
3580
3581 In addition, built-in versions of the 'fe' prefixed routines may
3582 need to be invoked for floating point (real, complex or vector) when
3583 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3584
3585 T1 newval;
3586 T1 old;
3587 T1 *addr
3588 T2 val
3589 fenv_t fenv
3590
3591 addr = &E1;
3592 val = (E2);
3593 __atomic_load (addr, &old, SEQ_CST);
3594 feholdexcept (&fenv);
3595loop:
3596 newval = old op val;
3597 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3598 SEQ_CST))
3599 goto done;
3600 feclearexcept (FE_ALL_EXCEPT);
3601 goto loop:
3602done:
3603 feupdateenv (&fenv);
3604
3605 Also note that the compiler is simply issuing the generic form of
3606 the atomic operations. This requires temp(s) and has their address
3607 taken. The atomic processing is smart enough to figure out when the
3608 size of an object can utilize a lock-free version, and convert the
3609 built-in call to the appropriate lock-free routine. The optimizers
3610 will then dispose of any temps that are no longer required, and
3611 lock-free implementations are utilized as long as there is target
3612 support for the required size.
3613
3614 If the operator is NOP_EXPR, then this is a simple assignment, and
3615 an __atomic_store is issued to perform the assignment rather than
3616 the above loop.
3617
3618*/
3619
3620/* Build an atomic assignment at LOC, expanding into the proper
3621 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3622 the result of the operation, unless RETURN_OLD_P in which case
3623 return the old value of LHS (this is only for postincrement and
3624 postdecrement). */
3625static tree
3626build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3627 tree rhs, bool return_old_p)
3628{
3629 tree fndecl, func_call;
3630 vec<tree, va_gc> *params;
3631 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3632 tree old, old_addr;
3633 tree compound_stmt;
3634 tree stmt, goto_stmt;
3635 tree loop_label, loop_decl, done_label, done_decl;
3636
3637 tree lhs_type = TREE_TYPE (lhs);
3638 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3639 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3640 tree rhs_type = TREE_TYPE (rhs);
3641
3642 gcc_assert (TYPE_ATOMIC (lhs_type));
3643
3644 if (return_old_p)
3645 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3646
3647 /* Allocate enough vector items for a compare_exchange. */
3648 vec_alloc (params, 6);
3649
3650 /* Create a compound statement to hold the sequence of statements
3651 with a loop. */
3652 compound_stmt = c_begin_compound_stmt (false);
3653
3654 /* Fold the RHS if it hasn't already been folded. */
3655 if (modifycode != NOP_EXPR)
3656 rhs = c_fully_fold (rhs, false, NULL);
3657
3658 /* Remove the qualifiers for the rest of the expressions and create
3659 the VAL temp variable to hold the RHS. */
3660 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3661 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
b731b390 3662 val = create_tmp_var (nonatomic_rhs_type);
267bac10 3663 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3664 TREE_NO_WARNING (val) = 1;
267bac10
JM
3665 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3666 SET_EXPR_LOCATION (rhs, loc);
3667 add_stmt (rhs);
3668
3669 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3670 an atomic_store. */
3671 if (modifycode == NOP_EXPR)
3672 {
3673 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3674 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3675 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3676 params->quick_push (lhs_addr);
3677 params->quick_push (rhs);
3678 params->quick_push (seq_cst);
8edbfaa6 3679 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3680 add_stmt (func_call);
3681
3682 /* Finish the compound statement. */
3683 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3684
3685 /* VAL is the value which was stored, return a COMPOUND_STMT of
3686 the statement and that value. */
3687 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3688 }
3689
3690 /* Create the variables and labels required for the op= form. */
b731b390 3691 old = create_tmp_var (nonatomic_lhs_type);
267bac10 3692 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3693 TREE_ADDRESSABLE (old) = 1;
3694 TREE_NO_WARNING (old) = 1;
267bac10 3695
b731b390 3696 newval = create_tmp_var (nonatomic_lhs_type);
267bac10
JM
3697 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3698 TREE_ADDRESSABLE (newval) = 1;
3699
3700 loop_decl = create_artificial_label (loc);
3701 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3702
3703 done_decl = create_artificial_label (loc);
3704 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3705
3706 /* __atomic_load (addr, &old, SEQ_CST). */
3707 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3708 params->quick_push (lhs_addr);
3709 params->quick_push (old_addr);
3710 params->quick_push (seq_cst);
8edbfaa6 3711 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3712 add_stmt (func_call);
3713 params->truncate (0);
3714
3715 /* Create the expressions for floating-point environment
3716 manipulation, if required. */
3717 bool need_fenv = (flag_trapping_math
3718 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3719 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3720 if (need_fenv)
3721 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3722
3723 if (hold_call)
3724 add_stmt (hold_call);
3725
3726 /* loop: */
3727 add_stmt (loop_label);
3728
3729 /* newval = old + val; */
3730 rhs = build_binary_op (loc, modifycode, old, val, 1);
68fca595
MP
3731 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3732 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3733 NULL_TREE, 0);
3734 if (rhs != error_mark_node)
3735 {
3736 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3737 SET_EXPR_LOCATION (rhs, loc);
3738 add_stmt (rhs);
3739 }
3740
3741 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3742 goto done; */
3743 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3744 params->quick_push (lhs_addr);
3745 params->quick_push (old_addr);
3746 params->quick_push (newval_addr);
3747 params->quick_push (integer_zero_node);
3748 params->quick_push (seq_cst);
3749 params->quick_push (seq_cst);
8edbfaa6 3750 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3751
3752 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3753 SET_EXPR_LOCATION (goto_stmt, loc);
3754
3755 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3756 SET_EXPR_LOCATION (stmt, loc);
3757 add_stmt (stmt);
3758
3759 if (clear_call)
3760 add_stmt (clear_call);
3761
3762 /* goto loop; */
3763 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3764 SET_EXPR_LOCATION (goto_stmt, loc);
3765 add_stmt (goto_stmt);
3766
3767 /* done: */
3768 add_stmt (done_label);
3769
3770 if (update_call)
3771 add_stmt (update_call);
3772
3773 /* Finish the compound statement. */
3774 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3775
3776 /* NEWVAL is the value that was successfully stored, return a
3777 COMPOUND_EXPR of the statement and the appropriate value. */
3778 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3779 return_old_p ? old : newval);
3780}
3781
3e4093b6
RS
3782/* Construct and perhaps optimize a tree representation
3783 for a unary operation. CODE, a tree_code, specifies the operation
3784 and XARG is the operand.
3785 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3786 the default promotions (such as from short to int).
3787 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3788 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3789 arrays to pointers in C99.
3790
3791 LOCATION is the location of the operator. */
400fbf9f 3792
3e4093b6 3793tree
c9f9eb5d
AH
3794build_unary_op (location_t location,
3795 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3796{
3797 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3798 tree arg = xarg;
3799 tree argtype = 0;
808d6eaa 3800 enum tree_code typecode;
3e4093b6 3801 tree val;
c9f9eb5d 3802 tree ret = error_mark_node;
8ce94e44 3803 tree eptype = NULL_TREE;
3e4093b6 3804 int noconvert = flag;
4de67c26 3805 const char *invalid_op_diag;
928c19bb
JM
3806 bool int_operands;
3807
3808 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3809 if (int_operands)
3810 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3811
808d6eaa
JM
3812 if (code != ADDR_EXPR)
3813 arg = require_complete_type (arg);
3814
3815 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3816 if (typecode == ERROR_MARK)
3817 return error_mark_node;
3818 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3819 typecode = INTEGER_TYPE;
6c36d76b 3820
4de67c26
JM
3821 if ((invalid_op_diag
3822 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3823 {
c9f9eb5d 3824 error_at (location, invalid_op_diag);
4de67c26
JM
3825 return error_mark_node;
3826 }
3827
8ce94e44
JM
3828 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3829 {
3830 eptype = TREE_TYPE (arg);
3831 arg = TREE_OPERAND (arg, 0);
3832 }
3833
3e4093b6
RS
3834 switch (code)
3835 {
3836 case CONVERT_EXPR:
3837 /* This is used for unary plus, because a CONVERT_EXPR
3838 is enough to prevent anybody from looking inside for
3839 associativity, but won't generate any code. */
3840 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3841 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3842 || typecode == VECTOR_TYPE))
400fbf9f 3843 {
c9f9eb5d 3844 error_at (location, "wrong type argument to unary plus");
3e4093b6 3845 return error_mark_node;
400fbf9f 3846 }
3e4093b6
RS
3847 else if (!noconvert)
3848 arg = default_conversion (arg);
db3927fb 3849 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3850 break;
3851
3e4093b6
RS
3852 case NEGATE_EXPR:
3853 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3854 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3855 || typecode == VECTOR_TYPE))
3856 {
c9f9eb5d 3857 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3858 return error_mark_node;
3859 }
3860 else if (!noconvert)
3861 arg = default_conversion (arg);
400fbf9f
JW
3862 break;
3863
3e4093b6 3864 case BIT_NOT_EXPR:
462643f0
AP
3865 /* ~ works on integer types and non float vectors. */
3866 if (typecode == INTEGER_TYPE
3867 || (typecode == VECTOR_TYPE
3868 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3869 {
3e4093b6
RS
3870 if (!noconvert)
3871 arg = default_conversion (arg);
03d5b1f5 3872 }
3e4093b6 3873 else if (typecode == COMPLEX_TYPE)
400fbf9f 3874 {
3e4093b6 3875 code = CONJ_EXPR;
c1771a20 3876 pedwarn (location, OPT_Wpedantic,
fcf73884 3877 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3878 if (!noconvert)
3879 arg = default_conversion (arg);
3880 }
3881 else
3882 {
c9f9eb5d 3883 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3884 return error_mark_node;
400fbf9f
JW
3885 }
3886 break;
3887
3e4093b6 3888 case ABS_EXPR:
11017cc7 3889 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3890 {
c9f9eb5d 3891 error_at (location, "wrong type argument to abs");
3e4093b6 3892 return error_mark_node;
400fbf9f 3893 }
3e4093b6
RS
3894 else if (!noconvert)
3895 arg = default_conversion (arg);
400fbf9f
JW
3896 break;
3897
3e4093b6
RS
3898 case CONJ_EXPR:
3899 /* Conjugating a real value is a no-op, but allow it anyway. */
3900 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3901 || typecode == COMPLEX_TYPE))
400fbf9f 3902 {
c9f9eb5d 3903 error_at (location, "wrong type argument to conjugation");
3e4093b6 3904 return error_mark_node;
400fbf9f 3905 }
3e4093b6
RS
3906 else if (!noconvert)
3907 arg = default_conversion (arg);
400fbf9f
JW
3908 break;
3909
3e4093b6 3910 case TRUTH_NOT_EXPR:
ab22c1fa 3911 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3912 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3913 && typecode != COMPLEX_TYPE)
400fbf9f 3914 {
c9f9eb5d
AH
3915 error_at (location,
3916 "wrong type argument to unary exclamation mark");
3e4093b6 3917 return error_mark_node;
400fbf9f 3918 }
a27d595d
JM
3919 if (int_operands)
3920 {
3921 arg = c_objc_common_truthvalue_conversion (location, xarg);
3922 arg = remove_c_maybe_const_expr (arg);
3923 }
3924 else
3925 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3926 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3927 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3928 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3929 location = EXPR_LOCATION (ret);
c9f9eb5d 3930 goto return_build_unary_op;
3e4093b6 3931
3e4093b6 3932 case REALPART_EXPR:
3e4093b6 3933 case IMAGPART_EXPR:
fb52b50a
NF
3934 ret = build_real_imag_expr (location, code, arg);
3935 if (ret == error_mark_node)
3936 return error_mark_node;
8ce94e44
JM
3937 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3938 eptype = TREE_TYPE (eptype);
c9f9eb5d 3939 goto return_build_unary_op;
3e4093b6
RS
3940
3941 case PREINCREMENT_EXPR:
3942 case POSTINCREMENT_EXPR:
3943 case PREDECREMENT_EXPR:
3944 case POSTDECREMENT_EXPR:
3e4093b6 3945
928c19bb
JM
3946 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3947 {
3948 tree inner = build_unary_op (location, code,
3949 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3950 if (inner == error_mark_node)
3951 return error_mark_node;
3952 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3953 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3954 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3955 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3956 goto return_build_unary_op;
3957 }
3958
925e8657
NP
3959 /* Complain about anything that is not a true lvalue. In
3960 Objective-C, skip this check for property_refs. */
f90e8e2e 3961 if (!objc_is_property_ref (arg)
7bd11157
TT
3962 && !lvalue_or_else (location,
3963 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3964 || code == POSTINCREMENT_EXPR)
3965 ? lv_increment
3966 : lv_decrement)))
928c19bb
JM
3967 return error_mark_node;
3968
09639a83
ILT
3969 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3970 {
3971 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3972 warning_at (location, OPT_Wc___compat,
3973 "increment of enumeration value is invalid in C++");
3974 else
3975 warning_at (location, OPT_Wc___compat,
3976 "decrement of enumeration value is invalid in C++");
3977 }
3978
928c19bb
JM
3979 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3980 arg = c_fully_fold (arg, false, NULL);
3981
267bac10
JM
3982 bool atomic_op;
3983 atomic_op = really_atomic_lvalue (arg);
3984
3e4093b6
RS
3985 /* Increment or decrement the real part of the value,
3986 and don't change the imaginary part. */
3987 if (typecode == COMPLEX_TYPE)
400fbf9f 3988 {
3e4093b6
RS
3989 tree real, imag;
3990
c1771a20 3991 pedwarn (location, OPT_Wpedantic,
509c9d60 3992 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3993
267bac10
JM
3994 if (!atomic_op)
3995 {
3996 arg = stabilize_reference (arg);
3997 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3998 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3999 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4000 if (real == error_mark_node || imag == error_mark_node)
4001 return error_mark_node;
4002 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4003 real, imag);
4004 goto return_build_unary_op;
4005 }
400fbf9f 4006 }
3e4093b6
RS
4007
4008 /* Report invalid types. */
4009
ab22c1fa 4010 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4011 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4012 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4013 {
3e4093b6 4014 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4015 error_at (location, "wrong type argument to increment");
c22cacf3 4016 else
c9f9eb5d 4017 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4018
4019 return error_mark_node;
400fbf9f 4020 }
400fbf9f 4021
3e4093b6
RS
4022 {
4023 tree inc;
400fbf9f 4024
3e4093b6
RS
4025 argtype = TREE_TYPE (arg);
4026
4027 /* Compute the increment. */
4028
4029 if (typecode == POINTER_TYPE)
4030 {
a0e24419 4031 /* If pointer target is an incomplete type,
3e4093b6 4032 we just cannot know how to do the arithmetic. */
b70cef5d 4033 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4034 {
4035 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4036 error_at (location,
a0e24419
MP
4037 "increment of pointer to an incomplete type %qT",
4038 TREE_TYPE (argtype));
3e4093b6 4039 else
c9f9eb5d 4040 error_at (location,
a0e24419
MP
4041 "decrement of pointer to an incomplete type %qT",
4042 TREE_TYPE (argtype));
3e4093b6 4043 }
b70cef5d
JJ
4044 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4045 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4046 {
3e4093b6 4047 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4048 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4049 "wrong type argument to increment");
3e4093b6 4050 else
44d90fe1 4051 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4052 "wrong type argument to decrement");
3e4093b6
RS
4053 }
4054
b70cef5d 4055 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4056 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4057 }
b70cef5d 4058 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4059 {
4060 /* For signed fract types, we invert ++ to -- or
4061 -- to ++, and change inc from 1 to -1, because
4062 it is not possible to represent 1 in signed fract constants.
4063 For unsigned fract types, the result always overflows and
4064 we get an undefined (original) or the maximum value. */
4065 if (code == PREINCREMENT_EXPR)
4066 code = PREDECREMENT_EXPR;
4067 else if (code == PREDECREMENT_EXPR)
4068 code = PREINCREMENT_EXPR;
4069 else if (code == POSTINCREMENT_EXPR)
4070 code = POSTDECREMENT_EXPR;
4071 else /* code == POSTDECREMENT_EXPR */
4072 code = POSTINCREMENT_EXPR;
4073
4074 inc = integer_minus_one_node;
4075 inc = convert (argtype, inc);
4076 }
3e4093b6 4077 else
5be014d5 4078 {
241b71bb
TV
4079 inc = VECTOR_TYPE_P (argtype)
4080 ? build_one_cst (argtype)
4081 : integer_one_node;
5be014d5
AP
4082 inc = convert (argtype, inc);
4083 }
3e4093b6 4084
925e8657
NP
4085 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4086 need to ask Objective-C to build the increment or decrement
4087 expression for it. */
4088 if (objc_is_property_ref (arg))
f90e8e2e 4089 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4090 arg, inc);
4091
3e4093b6 4092 /* Report a read-only lvalue. */
f37acdf9 4093 if (TYPE_READONLY (argtype))
953ff289 4094 {
c02065fc 4095 readonly_error (location, arg,
953ff289
DN
4096 ((code == PREINCREMENT_EXPR
4097 || code == POSTINCREMENT_EXPR)
4098 ? lv_increment : lv_decrement));
4099 return error_mark_node;
4100 }
f37acdf9
JM
4101 else if (TREE_READONLY (arg))
4102 readonly_warning (arg,
4103 ((code == PREINCREMENT_EXPR
4104 || code == POSTINCREMENT_EXPR)
4105 ? lv_increment : lv_decrement));
3e4093b6 4106
267bac10
JM
4107 /* If the argument is atomic, use the special code sequences for
4108 atomic compound assignment. */
4109 if (atomic_op)
4110 {
4111 arg = stabilize_reference (arg);
4112 ret = build_atomic_assign (location, arg,
4113 ((code == PREINCREMENT_EXPR
4114 || code == POSTINCREMENT_EXPR)
4115 ? PLUS_EXPR
4116 : MINUS_EXPR),
4117 (FRACT_MODE_P (TYPE_MODE (argtype))
4118 ? inc
4119 : integer_one_node),
4120 (code == POSTINCREMENT_EXPR
4121 || code == POSTDECREMENT_EXPR));
4122 goto return_build_unary_op;
4123 }
4124
3e4093b6
RS
4125 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4126 val = boolean_increment (code, arg);
4127 else
53fb4de3 4128 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4129 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4130 if (TREE_CODE (val) != code)
6de9cd9a 4131 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4132 ret = val;
4133 goto return_build_unary_op;
3e4093b6
RS
4134 }
4135
4136 case ADDR_EXPR:
4137 /* Note that this operation never does default_conversion. */
4138
2b4b7036
JM
4139 /* The operand of unary '&' must be an lvalue (which excludes
4140 expressions of type void), or, in C99, the result of a [] or
4141 unary '*' operator. */
4142 if (VOID_TYPE_P (TREE_TYPE (arg))
4143 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4144 && (TREE_CODE (arg) != INDIRECT_REF
4145 || !flag_isoc99))
4146 pedwarn (location, 0, "taking address of expression of type %<void%>");
4147
3e4093b6
RS
4148 /* Let &* cancel out to simplify resulting code. */
4149 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 4150 {
3e4093b6
RS
4151 /* Don't let this be an lvalue. */
4152 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4153 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4154 ret = TREE_OPERAND (arg, 0);
4155 goto return_build_unary_op;
400fbf9f 4156 }
1eb8759b 4157
7c672dfc 4158 /* For &x[y], return x+y */
3e4093b6 4159 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4160 {
f2a71bbc
JM
4161 tree op0 = TREE_OPERAND (arg, 0);
4162 if (!c_mark_addressable (op0))
3e4093b6 4163 return error_mark_node;
1eb8759b 4164 }
1eb8759b 4165
3e4093b6
RS
4166 /* Anything not already handled and not a true memory reference
4167 or a non-lvalue array is an error. */
4168 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4169 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4170 return error_mark_node;
b6a10c9f 4171
928c19bb
JM
4172 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4173 folding later. */
4174 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4175 {
4176 tree inner = build_unary_op (location, code,
4177 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4178 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4179 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4180 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4181 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4182 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4183 goto return_build_unary_op;
4184 }
4185
3e4093b6
RS
4186 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4187 argtype = TREE_TYPE (arg);
400fbf9f 4188
3e4093b6 4189 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4190 to which the address will point. This is only needed
f2c1da78 4191 for function types. */
6615c446 4192 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4193 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4194 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4195 {
4196 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4197 int quals = orig_quals;
4198
4199 if (TREE_READONLY (arg))
4200 quals |= TYPE_QUAL_CONST;
4201 if (TREE_THIS_VOLATILE (arg))
4202 quals |= TYPE_QUAL_VOLATILE;
4203
f2c1da78
JM
4204 argtype = c_build_qualified_type (argtype, quals);
4205 }
400fbf9f 4206
3e4093b6
RS
4207 if (!c_mark_addressable (arg))
4208 return error_mark_node;
400fbf9f 4209
abb54d14
JM
4210 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4211 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4212
5cc200fc 4213 argtype = build_pointer_type (argtype);
5e55f99d
RH
4214
4215 /* ??? Cope with user tricks that amount to offsetof. Delete this
4216 when we have proper support for integer constant expressions. */
4217 val = get_base_address (arg);
4218 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
4219 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4220 {
cf9e9959 4221 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4222 goto return_build_unary_op;
3aa2ddb8 4223 }
5e55f99d 4224
5cc200fc 4225 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4226
c9f9eb5d
AH
4227 ret = val;
4228 goto return_build_unary_op;
400fbf9f 4229
3e4093b6 4230 default:
1344f9a3 4231 gcc_unreachable ();
3e4093b6 4232 }
400fbf9f 4233
3e4093b6
RS
4234 if (argtype == 0)
4235 argtype = TREE_TYPE (arg);
928c19bb
JM
4236 if (TREE_CODE (arg) == INTEGER_CST)
4237 ret = (require_constant_value
db3927fb
AH
4238 ? fold_build1_initializer_loc (location, code, argtype, arg)
4239 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4240 else
4241 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4242 return_build_unary_op:
4243 gcc_assert (ret != error_mark_node);
928c19bb
JM
4244 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4245 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4246 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4247 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4248 ret = note_integer_operands (ret);
8ce94e44
JM
4249 if (eptype)
4250 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4251 protected_set_expr_location (ret, location);
4252 return ret;
3e4093b6 4253}
400fbf9f 4254
3e4093b6
RS
4255/* Return nonzero if REF is an lvalue valid for this language.
4256 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4257 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4258
1e4ae551 4259bool
58f9752a 4260lvalue_p (const_tree ref)
3e4093b6 4261{
58f9752a 4262 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4263
3e4093b6
RS
4264 switch (code)
4265 {
4266 case REALPART_EXPR:
4267 case IMAGPART_EXPR:
4268 case COMPONENT_REF:
4269 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4270
928c19bb
JM
4271 case C_MAYBE_CONST_EXPR:
4272 return lvalue_p (TREE_OPERAND (ref, 1));
4273
3e4093b6
RS
4274 case COMPOUND_LITERAL_EXPR:
4275 case STRING_CST:
4276 return 1;
400fbf9f 4277
3e4093b6
RS
4278 case INDIRECT_REF:
4279 case ARRAY_REF:
36536d79 4280 case ARRAY_NOTATION_REF:
3e4093b6
RS
4281 case VAR_DECL:
4282 case PARM_DECL:
4283 case RESULT_DECL:
4284 case ERROR_MARK:
4285 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4286 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4287
3e4093b6 4288 case BIND_EXPR:
3e4093b6 4289 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4290
3e4093b6
RS
4291 default:
4292 return 0;
4293 }
4294}
400fbf9f 4295\f
f37acdf9
JM
4296/* Give a warning for storing in something that is read-only in GCC
4297 terms but not const in ISO C terms. */
4298
4299static void
4300readonly_warning (tree arg, enum lvalue_use use)
4301{
4302 switch (use)
4303 {
4304 case lv_assign:
4305 warning (0, "assignment of read-only location %qE", arg);
4306 break;
4307 case lv_increment:
4308 warning (0, "increment of read-only location %qE", arg);
4309 break;
4310 case lv_decrement:
4311 warning (0, "decrement of read-only location %qE", arg);
4312 break;
4313 default:
4314 gcc_unreachable ();
4315 }
4316 return;
4317}
4318
37dc0d8d
JM
4319
4320/* Return nonzero if REF is an lvalue valid for this language;
4321 otherwise, print an error message and return zero. USE says
7bd11157
TT
4322 how the lvalue is being used and so selects the error message.
4323 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4324
4325static int
7bd11157 4326lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4327{
4328 int win = lvalue_p (ref);
4329
4330 if (!win)
7bd11157 4331 lvalue_error (loc, use);
37dc0d8d
JM
4332
4333 return win;
4334}
3e4093b6
RS
4335\f
4336/* Mark EXP saying that we need to be able to take the
4337 address of it; it should not be allocated in a register.
4338 Returns true if successful. */
54c93c30 4339
3e4093b6
RS
4340bool
4341c_mark_addressable (tree exp)
400fbf9f 4342{
3e4093b6 4343 tree x = exp;
95602da1 4344
3e4093b6
RS
4345 while (1)
4346 switch (TREE_CODE (x))
4347 {
4348 case COMPONENT_REF:
4349 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4350 {
0039fa55
AN
4351 error
4352 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4353 return false;
4354 }
95602da1 4355
3e4093b6 4356 /* ... fall through ... */
95602da1 4357
3e4093b6
RS
4358 case ADDR_EXPR:
4359 case ARRAY_REF:
4360 case REALPART_EXPR:
4361 case IMAGPART_EXPR:
4362 x = TREE_OPERAND (x, 0);
4363 break;
95602da1 4364
3e4093b6
RS
4365 case COMPOUND_LITERAL_EXPR:
4366 case CONSTRUCTOR:
4367 TREE_ADDRESSABLE (x) = 1;
4368 return true;
95602da1 4369
3e4093b6
RS
4370 case VAR_DECL:
4371 case CONST_DECL:
4372 case PARM_DECL:
4373 case RESULT_DECL:
5baeaac0 4374 if (C_DECL_REGISTER (x)
3e4093b6
RS
4375 && DECL_NONLOCAL (x))
4376 {
e697b20f 4377 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4378 {
0039fa55
AN
4379 error
4380 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4381 return false;
4382 }
509c9d60 4383 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4384 }
5baeaac0 4385 else if (C_DECL_REGISTER (x))
3e4093b6 4386 {
e697b20f 4387 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4388 error ("address of global register variable %qD requested", x);
4389 else
4390 error ("address of register variable %qD requested", x);
4391 return false;
3e4093b6 4392 }
400fbf9f 4393
3e4093b6
RS
4394 /* drops in */
4395 case FUNCTION_DECL:
4396 TREE_ADDRESSABLE (x) = 1;
4397 /* drops out */
4398 default:
4399 return true;
4400 }
4401}
4402\f
2d2e923f
MLI
4403/* Convert EXPR to TYPE, warning about conversion problems with
4404 constants. SEMANTIC_TYPE is the type this conversion would use
4405 without excess precision. If SEMANTIC_TYPE is NULL, this function
4406 is equivalent to convert_and_check. This function is a wrapper that
4407 handles conversions that may be different than
4408 the usual ones because of excess precision. */
4409
4410static tree
68fca595
MP
4411ep_convert_and_check (location_t loc, tree type, tree expr,
4412 tree semantic_type)
2d2e923f
MLI
4413{
4414 if (TREE_TYPE (expr) == type)
4415 return expr;
4416
4417 if (!semantic_type)
68fca595 4418 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4419
4420 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4421 && TREE_TYPE (expr) != semantic_type)
4422 {
4423 /* For integers, we need to check the real conversion, not
4424 the conversion to the excess precision type. */
68fca595 4425 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4426 }
4427 /* Result type is the excess precision type, which should be
4428 large enough, so do not check. */
4429 return convert (type, expr);
4430}
4431
928c19bb
JM
4432/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4433 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4434 if folded to an integer constant then the unselected half may
4435 contain arbitrary operations not normally permitted in constant
c2255bc4 4436 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4437
4438tree
744aa42f 4439build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4440 tree op1, tree op1_original_type, tree op2,
4441 tree op2_original_type)
400fbf9f 4442{
3e4093b6
RS
4443 tree type1;
4444 tree type2;
4445 enum tree_code code1;
4446 enum tree_code code2;
4447 tree result_type = NULL;
2d2e923f 4448 tree semantic_result_type = NULL;
3e4093b6 4449 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4450 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4451 bool ifexp_int_operands;
928c19bb 4452 tree ret;
400fbf9f 4453
4d84fe7c
JM
4454 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4455 if (op1_int_operands)
4456 op1 = remove_c_maybe_const_expr (op1);
4457 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4458 if (op2_int_operands)
4459 op2 = remove_c_maybe_const_expr (op2);
4460 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4461 if (ifexp_int_operands)
4462 ifexp = remove_c_maybe_const_expr (ifexp);
4463
3e4093b6
RS
4464 /* Promote both alternatives. */
4465
4466 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4467 op1 = default_conversion (op1);
4468 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4469 op2 = default_conversion (op2);
4470
4471 if (TREE_CODE (ifexp) == ERROR_MARK
4472 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4473 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4474 return error_mark_node;
400fbf9f 4475
3e4093b6
RS
4476 type1 = TREE_TYPE (op1);
4477 code1 = TREE_CODE (type1);
4478 type2 = TREE_TYPE (op2);
4479 code2 = TREE_CODE (type2);
4480
b1adf557
JM
4481 /* C90 does not permit non-lvalue arrays in conditional expressions.
4482 In C99 they will be pointers by now. */
4483 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4484 {
744aa42f 4485 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4486 return error_mark_node;
4487 }
4488
8ce94e44
JM
4489 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4490 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4491 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4492 || code1 == COMPLEX_TYPE)
4493 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4494 || code2 == COMPLEX_TYPE))
4495 {
2d2e923f 4496 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4497 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4498 {
4499 op1 = TREE_OPERAND (op1, 0);
4500 type1 = TREE_TYPE (op1);
4501 gcc_assert (TREE_CODE (type1) == code1);
4502 }
4503 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4504 {
4505 op2 = TREE_OPERAND (op2, 0);
4506 type2 = TREE_TYPE (op2);
4507 gcc_assert (TREE_CODE (type2) == code2);
4508 }
4509 }
4510
d130ae11
ILT
4511 if (warn_cxx_compat)
4512 {
4513 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4514 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4515
4516 if (TREE_CODE (t1) == ENUMERAL_TYPE
4517 && TREE_CODE (t2) == ENUMERAL_TYPE
4518 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4519 warning_at (colon_loc, OPT_Wc___compat,
4520 ("different enum types in conditional is "
4521 "invalid in C++: %qT vs %qT"),
4522 t1, t2);
4523 }
4524
3e4093b6
RS
4525 /* Quickly detect the usual case where op1 and op2 have the same type
4526 after promotion. */
4527 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4528 {
3e4093b6
RS
4529 if (type1 == type2)
4530 result_type = type1;
4531 else
4532 result_type = TYPE_MAIN_VARIANT (type1);
4533 }
4534 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4535 || code1 == COMPLEX_TYPE)
4536 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4537 || code2 == COMPLEX_TYPE))
3e4093b6 4538 {
ccf7f880 4539 result_type = c_common_type (type1, type2);
0a0b3574
MM
4540 do_warn_double_promotion (result_type, type1, type2,
4541 "implicit conversion from %qT to %qT to "
4542 "match other result of conditional",
4543 colon_loc);
400fbf9f 4544
3e4093b6
RS
4545 /* If -Wsign-compare, warn here if type1 and type2 have
4546 different signedness. We'll promote the signed to unsigned
4547 and later code won't know it used to be different.
4548 Do this check on the original types, so that explicit casts
4549 will be considered, but default promotions won't. */
7d882b83 4550 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4551 {
8df83eae
RK
4552 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4553 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4554
3e4093b6
RS
4555 if (unsigned_op1 ^ unsigned_op2)
4556 {
6ac01510
ILT
4557 bool ovf;
4558
3e4093b6
RS
4559 /* Do not warn if the result type is signed, since the
4560 signed type will only be chosen if it can represent
4561 all the values of the unsigned type. */
3f75a254 4562 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4563 /* OK */;
3e4093b6 4564 else
928c19bb
JM
4565 {
4566 bool op1_maybe_const = true;
4567 bool op2_maybe_const = true;
4568
4569 /* Do not warn if the signed quantity is an
4570 unsuffixed integer literal (or some static
4571 constant expression involving such literals) and
4572 it is non-negative. This warning requires the
4573 operands to be folded for best results, so do
4574 that folding in this case even without
4575 warn_sign_compare to avoid warning options
4576 possibly affecting code generation. */
f5178456
RS
4577 c_inhibit_evaluation_warnings
4578 += (ifexp == truthvalue_false_node);
928c19bb
JM
4579 op1 = c_fully_fold (op1, require_constant_value,
4580 &op1_maybe_const);
f5178456
RS
4581 c_inhibit_evaluation_warnings
4582 -= (ifexp == truthvalue_false_node);
4583
4584 c_inhibit_evaluation_warnings
4585 += (ifexp == truthvalue_true_node);
928c19bb
JM
4586 op2 = c_fully_fold (op2, require_constant_value,
4587 &op2_maybe_const);
f5178456
RS
4588 c_inhibit_evaluation_warnings
4589 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4590
4591 if (warn_sign_compare)
4592 {
4593 if ((unsigned_op2
4594 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4595 || (unsigned_op1
4596 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4597 /* OK */;
4598 else
744aa42f
ILT
4599 warning_at (colon_loc, OPT_Wsign_compare,
4600 ("signed and unsigned type in "
4601 "conditional expression"));
928c19bb
JM
4602 }
4603 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4604 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4605 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4606 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4607 }
3e4093b6
RS
4608 }
4609 }
4610 }
4611 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4612 {
fcf73884 4613 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4614 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4615 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4616 result_type = void_type_node;
4617 }
4618 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4619 {
36c5e70a
BE
4620 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4621 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4622 addr_space_t as_common;
4623
744aa42f 4624 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4625 result_type = common_pointer_type (type1, type2);
6aa3c60d 4626 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4627 result_type = type2;
6aa3c60d 4628 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4629 result_type = type1;
4630 else if (!addr_space_superset (as1, as2, &as_common))
4631 {
4632 error_at (colon_loc, "pointers to disjoint address spaces "
4633 "used in conditional expression");
4634 return error_mark_node;
4635 }
267bac10
JM
4636 else if (VOID_TYPE_P (TREE_TYPE (type1))
4637 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4638 {
768952be
MU
4639 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4640 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4641 & ~TYPE_QUALS (TREE_TYPE (type1))))
4642 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4643 "pointer to array loses qualifier "
4644 "in conditional expression");
4645
fcf73884 4646 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4647 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4648 "ISO C forbids conditional expr between "
bda67431 4649 "%<void *%> and function pointer");
3e4093b6
RS
4650 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4651 TREE_TYPE (type2)));
34a80643 4652 }
267bac10
JM
4653 else if (VOID_TYPE_P (TREE_TYPE (type2))
4654 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4655 {
768952be
MU
4656 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4657 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4658 & ~TYPE_QUALS (TREE_TYPE (type2))))
4659 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4660 "pointer to array loses qualifier "
4661 "in conditional expression");
4662
fcf73884 4663 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4664 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4665 "ISO C forbids conditional expr between "
bda67431 4666 "%<void *%> and function pointer");
3e4093b6
RS
4667 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4668 TREE_TYPE (type1)));
1c2a9b35 4669 }
b581b85b
NP
4670 /* Objective-C pointer comparisons are a bit more lenient. */
4671 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4672 result_type = objc_common_type (type1, type2);
34a80643 4673 else
ab87f8c8 4674 {
36c5e70a
BE
4675 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4676
b581b85b
NP
4677 pedwarn (colon_loc, 0,
4678 "pointer type mismatch in conditional expression");
36c5e70a
BE
4679 result_type = build_pointer_type
4680 (build_qualified_type (void_type_node, qual));
ab87f8c8 4681 }
3e4093b6
RS
4682 }
4683 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4684 {
6aa3c60d 4685 if (!null_pointer_constant_p (orig_op2))
744aa42f 4686 pedwarn (colon_loc, 0,
509c9d60 4687 "pointer/integer type mismatch in conditional expression");
3e4093b6 4688 else
ab87f8c8 4689 {
3e4093b6 4690 op2 = null_pointer_node;
ab87f8c8 4691 }
3e4093b6
RS
4692 result_type = type1;
4693 }
4694 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4695 {
6aa3c60d 4696 if (!null_pointer_constant_p (orig_op1))
744aa42f 4697 pedwarn (colon_loc, 0,
509c9d60 4698 "pointer/integer type mismatch in conditional expression");
3e4093b6 4699 else
ab87f8c8 4700 {
3e4093b6 4701 op1 = null_pointer_node;
ab87f8c8 4702 }
3e4093b6
RS
4703 result_type = type2;
4704 }
1c2a9b35 4705
3e4093b6
RS
4706 if (!result_type)
4707 {
4708 if (flag_cond_mismatch)
4709 result_type = void_type_node;
4710 else
400fbf9f 4711 {
c2255bc4 4712 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4713 return error_mark_node;
400fbf9f 4714 }
3e4093b6 4715 }
400fbf9f 4716
3e4093b6
RS
4717 /* Merge const and volatile flags of the incoming types. */
4718 result_type
4719 = build_type_variant (result_type,
afbd0665
AS
4720 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4721 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4722
68fca595
MP
4723 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4724 semantic_result_type);
4725 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4726 semantic_result_type);
b6a10c9f 4727
928c19bb
JM
4728 if (ifexp_bcp && ifexp == truthvalue_true_node)
4729 {
4730 op2_int_operands = true;
4731 op1 = c_fully_fold (op1, require_constant_value, NULL);
4732 }
4733 if (ifexp_bcp && ifexp == truthvalue_false_node)
4734 {
4735 op1_int_operands = true;
4736 op2 = c_fully_fold (op2, require_constant_value, NULL);
4737 }
4d84fe7c 4738 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4739 && op1_int_operands
4740 && op2_int_operands);
4741 if (int_operands)
4742 {
4743 int_const = ((ifexp == truthvalue_true_node
4744 && TREE_CODE (orig_op1) == INTEGER_CST
4745 && !TREE_OVERFLOW (orig_op1))
4746 || (ifexp == truthvalue_false_node
4747 && TREE_CODE (orig_op2) == INTEGER_CST
4748 && !TREE_OVERFLOW (orig_op2)));
4749 }
4750 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4751 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4752 else
4753 {
01c7ccbb
JM
4754 if (int_operands)
4755 {
f34f1c87
MP
4756 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4757 nested inside of the expression. */
4758 op1 = c_fully_fold (op1, false, NULL);
4759 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4760 }
928c19bb
JM
4761 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4762 if (int_operands)
4763 ret = note_integer_operands (ret);
4764 }
2d2e923f
MLI
4765 if (semantic_result_type)
4766 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4767
c2255bc4 4768 protected_set_expr_location (ret, colon_loc);
928c19bb 4769 return ret;
3e4093b6
RS
4770}
4771\f
487a92fe 4772/* Return a compound expression that performs two expressions and
c2255bc4
AH
4773 returns the value of the second of them.
4774
4775 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4776
3e4093b6 4777tree
c2255bc4 4778build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4779{
4d84fe7c 4780 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4781 tree eptype = NULL_TREE;
928c19bb
JM
4782 tree ret;
4783
b72271b9 4784 if (flag_cilkplus
939b37da
BI
4785 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4786 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4787 {
4788 error_at (loc,
4789 "spawned function call cannot be part of a comma expression");
4790 return error_mark_node;
4791 }
4d84fe7c
JM
4792 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4793 if (expr1_int_operands)
4794 expr1 = remove_c_maybe_const_expr (expr1);
4795 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4796 if (expr2_int_operands)
4797 expr2 = remove_c_maybe_const_expr (expr2);
4798
8ce94e44
JM
4799 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4800 expr1 = TREE_OPERAND (expr1, 0);
4801 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4802 {
4803 eptype = TREE_TYPE (expr2);
4804 expr2 = TREE_OPERAND (expr2, 0);
4805 }
4806
3f75a254 4807 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4808 {
4809 /* The left-hand operand of a comma expression is like an expression
c5409249 4810 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4811 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4812 if (warn_unused_value)
47aecf47 4813 {
e14a6540 4814 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4815 && CONVERT_EXPR_P (expr1))
47aecf47 4816 ; /* (void) a, b */
e14a6540
JM
4817 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4818 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4819 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4820 ; /* (void) a, (void) b, c */
4821 else
b8698a0f 4822 warning_at (loc, OPT_Wunused_value,
c2255bc4 4823 "left-hand operand of comma expression has no effect");
47aecf47 4824 }
3e4093b6 4825 }
789eadcd
MP
4826 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4827 && warn_unused_value)
4828 {
4829 tree r = expr1;
4830 location_t cloc = loc;
4831 while (TREE_CODE (r) == COMPOUND_EXPR)
4832 {
4833 if (EXPR_HAS_LOCATION (r))
4834 cloc = EXPR_LOCATION (r);
4835 r = TREE_OPERAND (r, 1);
4836 }
4837 if (!TREE_SIDE_EFFECTS (r)
4838 && !VOID_TYPE_P (TREE_TYPE (r))
4839 && !CONVERT_EXPR_P (r))
4840 warning_at (cloc, OPT_Wunused_value,
4841 "right-hand operand of comma expression has no effect");
4842 }
400fbf9f 4843
3e4093b6
RS
4844 /* With -Wunused, we should also warn if the left-hand operand does have
4845 side-effects, but computes a value which is not used. For example, in
4846 `foo() + bar(), baz()' the result of the `+' operator is not used,
4847 so we should issue a warning. */
4848 else if (warn_unused_value)
c2255bc4 4849 warn_if_unused_value (expr1, loc);
400fbf9f 4850
e63d6886
AP
4851 if (expr2 == error_mark_node)
4852 return error_mark_node;
4853
928c19bb
JM
4854 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4855
4856 if (flag_isoc99
4d84fe7c
JM
4857 && expr1_int_operands
4858 && expr2_int_operands)
928c19bb
JM
4859 ret = note_integer_operands (ret);
4860
8ce94e44
JM
4861 if (eptype)
4862 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4863
c2255bc4 4864 protected_set_expr_location (ret, loc);
928c19bb 4865 return ret;
3e4093b6 4866}
400fbf9f 4867
67165eb3
ILT
4868/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4869 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4870 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4871 of the cast. -Wcast-qual appeared on the command line. Named
4872 address space qualifiers are not handled here, because they result
4873 in different warnings. */
67165eb3
ILT
4874
4875static void
2ee3cb35 4876handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4877{
4878 tree in_type = type;
4879 tree in_otype = otype;
4880 int added = 0;
4881 int discarded = 0;
4882 bool is_const;
4883
4884 /* Check that the qualifiers on IN_TYPE are a superset of the
4885 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4886 nodes is uninteresting and we stop as soon as we hit a
4887 non-POINTER_TYPE node on either type. */
4888 do
4889 {
4890 in_otype = TREE_TYPE (in_otype);
4891 in_type = TREE_TYPE (in_type);
4892
4893 /* GNU C allows cv-qualified function types. 'const' means the
4894 function is very pure, 'volatile' means it can't return. We
4895 need to warn when such qualifiers are added, not when they're
4896 taken away. */
4897 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4898 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4899 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4900 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4901 else
36c5e70a
BE
4902 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4903 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4904 }
4905 while (TREE_CODE (in_type) == POINTER_TYPE
4906 && TREE_CODE (in_otype) == POINTER_TYPE);
4907
4908 if (added)
2ee3cb35
MLI
4909 warning_at (loc, OPT_Wcast_qual,
4910 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4911
4912 if (discarded)
4913 /* There are qualifiers present in IN_OTYPE that are not present
4914 in IN_TYPE. */
2ee3cb35 4915 warning_at (loc, OPT_Wcast_qual,
7485aeea 4916 "cast discards %qv qualifier from pointer target type",
2ee3cb35 4917 discarded);
67165eb3
ILT
4918
4919 if (added || discarded)
4920 return;
4921
4922 /* A cast from **T to const **T is unsafe, because it can cause a
4923 const value to be changed with no additional warning. We only
4924 issue this warning if T is the same on both sides, and we only
4925 issue the warning if there are the same number of pointers on
4926 both sides, as otherwise the cast is clearly unsafe anyhow. A
4927 cast is unsafe when a qualifier is added at one level and const
4928 is not present at all outer levels.
4929
4930 To issue this warning, we check at each level whether the cast
4931 adds new qualifiers not already seen. We don't need to special
4932 case function types, as they won't have the same
4933 TYPE_MAIN_VARIANT. */
4934
4935 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4936 return;
4937 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4938 return;
4939
4940 in_type = type;
4941 in_otype = otype;
4942 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4943 do
4944 {
4945 in_type = TREE_TYPE (in_type);
4946 in_otype = TREE_TYPE (in_otype);
4947 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4948 && !is_const)
4949 {
2ee3cb35
MLI
4950 warning_at (loc, OPT_Wcast_qual,
4951 "to be safe all intermediate pointers in cast from "
4952 "%qT to %qT must be %<const%> qualified",
4953 otype, type);
67165eb3
ILT
4954 break;
4955 }
4956 if (is_const)
4957 is_const = TYPE_READONLY (in_type);
4958 }
4959 while (TREE_CODE (in_type) == POINTER_TYPE);
4960}
4961
b8698a0f 4962/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4963 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4964
3e4093b6 4965tree
c2255bc4 4966build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4967{
8ce94e44
JM
4968 tree value;
4969
4970 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4971 expr = TREE_OPERAND (expr, 0);
4972
4973 value = expr;
400fbf9f 4974
3e4093b6
RS
4975 if (type == error_mark_node || expr == error_mark_node)
4976 return error_mark_node;
400fbf9f 4977
3e4093b6
RS
4978 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4979 only in <protocol> qualifications. But when constructing cast expressions,
4980 the protocols do matter and must be kept around. */
700686fa
ZL
4981 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4982 return build1 (NOP_EXPR, type, expr);
4983
4984 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4985
3e4093b6
RS
4986 if (TREE_CODE (type) == ARRAY_TYPE)
4987 {
c2255bc4 4988 error_at (loc, "cast specifies array type");
3e4093b6
RS
4989 return error_mark_node;
4990 }
400fbf9f 4991
3e4093b6
RS
4992 if (TREE_CODE (type) == FUNCTION_TYPE)
4993 {
c2255bc4 4994 error_at (loc, "cast specifies function type");
3e4093b6
RS
4995 return error_mark_node;
4996 }
400fbf9f 4997
808d6eaa
JM
4998 if (!VOID_TYPE_P (type))
4999 {
5000 value = require_complete_type (value);
5001 if (value == error_mark_node)
5002 return error_mark_node;
5003 }
5004
3e4093b6
RS
5005 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5006 {
fcf73884
MLI
5007 if (TREE_CODE (type) == RECORD_TYPE
5008 || TREE_CODE (type) == UNION_TYPE)
c1771a20 5009 pedwarn (loc, OPT_Wpedantic,
fcf73884 5010 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5011
5012 /* Convert to remove any qualifiers from VALUE's type. */
5013 value = convert (type, value);
3e4093b6
RS
5014 }
5015 else if (TREE_CODE (type) == UNION_TYPE)
5016 {
5017 tree field;
400fbf9f 5018
910ad8de 5019 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5020 if (TREE_TYPE (field) != error_mark_node
5021 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5022 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5023 break;
5024
5025 if (field)
400fbf9f 5026 {
3e4093b6 5027 tree t;
e616f54d 5028 bool maybe_const = true;
3e4093b6 5029
c1771a20 5030 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5031 t = c_fully_fold (value, false, &maybe_const);
5032 t = build_constructor_single (type, field, t);
5033 if (!maybe_const)
5034 t = c_wrap_maybe_const (t, true);
5035 t = digest_init (loc, type, t,
bbbbb16a 5036 NULL_TREE, false, true, 0);
3e4093b6
RS
5037 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5038 return t;
400fbf9f 5039 }
c2255bc4 5040 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5041 return error_mark_node;
5042 }
5043 else
5044 {
5045 tree otype, ovalue;
400fbf9f 5046
3e4093b6 5047 if (type == void_type_node)
c2255bc4
AH
5048 {
5049 tree t = build1 (CONVERT_EXPR, type, value);
5050 SET_EXPR_LOCATION (t, loc);
5051 return t;
5052 }
400fbf9f 5053
3e4093b6 5054 otype = TREE_TYPE (value);
400fbf9f 5055
3e4093b6 5056 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5057 if (warn_cast_qual
5058 && TREE_CODE (type) == POINTER_TYPE
5059 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5060 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5061
36c5e70a
BE
5062 /* Warn about conversions between pointers to disjoint
5063 address spaces. */
5064 if (TREE_CODE (type) == POINTER_TYPE
5065 && TREE_CODE (otype) == POINTER_TYPE
5066 && !null_pointer_constant_p (value))
5067 {
5068 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5069 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5070 addr_space_t as_common;
5071
5072 if (!addr_space_superset (as_to, as_from, &as_common))
5073 {
5074 if (ADDR_SPACE_GENERIC_P (as_from))
5075 warning_at (loc, 0, "cast to %s address space pointer "
5076 "from disjoint generic address space pointer",
5077 c_addr_space_name (as_to));
5078
5079 else if (ADDR_SPACE_GENERIC_P (as_to))
5080 warning_at (loc, 0, "cast to generic address space pointer "
5081 "from disjoint %s address space pointer",
5082 c_addr_space_name (as_from));
5083
5084 else
5085 warning_at (loc, 0, "cast to %s address space pointer "
5086 "from disjoint %s address space pointer",
5087 c_addr_space_name (as_to),
5088 c_addr_space_name (as_from));
5089 }
5090 }
5091
3e4093b6 5092 /* Warn about possible alignment problems. */
3176a0c2 5093 if (STRICT_ALIGNMENT
3e4093b6
RS
5094 && TREE_CODE (type) == POINTER_TYPE
5095 && TREE_CODE (otype) == POINTER_TYPE
5096 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5097 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5098 /* Don't warn about opaque types, where the actual alignment
5099 restriction is unknown. */
5100 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5101 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5102 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5103 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5104 warning_at (loc, OPT_Wcast_align,
5105 "cast increases required alignment of target type");
e9a25f70 5106
3176a0c2 5107 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5108 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5109 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5110 /* Unlike conversion of integers to pointers, where the
5111 warning is disabled for converting constants because
5112 of cases such as SIG_*, warn about converting constant
5113 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5114 sign extension, and a warning is appropriate. */
c2255bc4
AH
5115 warning_at (loc, OPT_Wpointer_to_int_cast,
5116 "cast from pointer to integer of different size");
400fbf9f 5117
3176a0c2 5118 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5119 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5120 warning_at (loc, OPT_Wbad_function_cast,
5121 "cast from function call of type %qT "
5122 "to non-matching type %qT", otype, type);
400fbf9f 5123
3176a0c2 5124 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5125 && TREE_CODE (otype) == INTEGER_TYPE
5126 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5127 /* Don't warn about converting any constant. */
5128 && !TREE_CONSTANT (value))
c2255bc4
AH
5129 warning_at (loc,
5130 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5131 "of different size");
400fbf9f 5132
79bedddc
SR
5133 if (warn_strict_aliasing <= 2)
5134 strict_aliasing_warning (otype, type, expr);
400fbf9f 5135
3897f229
JM
5136 /* If pedantic, warn for conversions between function and object
5137 pointer types, except for converting a null pointer constant
5138 to function pointer type. */
5139 if (pedantic
5140 && TREE_CODE (type) == POINTER_TYPE
5141 && TREE_CODE (otype) == POINTER_TYPE
5142 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5143 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5144 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5145 "conversion of function pointer to object pointer type");
3897f229
JM
5146
5147 if (pedantic
5148 && TREE_CODE (type) == POINTER_TYPE
5149 && TREE_CODE (otype) == POINTER_TYPE
5150 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5151 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5152 && !null_pointer_constant_p (value))
c1771a20 5153 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5154 "conversion of object pointer to function pointer type");
3897f229 5155
3e4093b6 5156 ovalue = value;
3e4093b6 5157 value = convert (type, value);
400fbf9f 5158
3e4093b6 5159 /* Ignore any integer overflow caused by the cast. */
928c19bb 5160 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5161 {
8bcd6380 5162 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5163 {
8bcd6380
RS
5164 if (!TREE_OVERFLOW (value))
5165 {
5166 /* Avoid clobbering a shared constant. */
5167 value = copy_node (value);
5168 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5169 }
6414bad6 5170 }
8bcd6380 5171 else if (TREE_OVERFLOW (value))
d8e1f97b 5172 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5173 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5174 }
5175 }
400fbf9f 5176
53cd18ec
JM
5177 /* Don't let a cast be an lvalue. */
5178 if (value == expr)
db3927fb 5179 value = non_lvalue_loc (loc, value);
e9a25f70 5180
928c19bb
JM
5181 /* Don't allow the results of casting to floating-point or complex
5182 types be confused with actual constants, or casts involving
5183 integer and pointer types other than direct integer-to-integer
5184 and integer-to-pointer be confused with integer constant
5185 expressions and null pointer constants. */
5186 if (TREE_CODE (value) == REAL_CST
5187 || TREE_CODE (value) == COMPLEX_CST
5188 || (TREE_CODE (value) == INTEGER_CST
5189 && !((TREE_CODE (expr) == INTEGER_CST
5190 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5191 || TREE_CODE (expr) == REAL_CST
5192 || TREE_CODE (expr) == COMPLEX_CST)))
5193 value = build1 (NOP_EXPR, type, value);
5194
c2255bc4
AH
5195 if (CAN_HAVE_LOCATION_P (value))
5196 SET_EXPR_LOCATION (value, loc);
3e4093b6 5197 return value;
400fbf9f
JW
5198}
5199
c2255bc4
AH
5200/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5201 location of the open paren of the cast, or the position of the cast
5202 expr. */
3e4093b6 5203tree
c2255bc4 5204c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5205{
f8893e47 5206 tree type;
928c19bb
JM
5207 tree type_expr = NULL_TREE;
5208 bool type_expr_const = true;
5209 tree ret;
3e4093b6 5210 int saved_wsp = warn_strict_prototypes;
c5c76735 5211
3e4093b6
RS
5212 /* This avoids warnings about unprototyped casts on
5213 integers. E.g. "#define SIG_DFL (void(*)())0". */
5214 if (TREE_CODE (expr) == INTEGER_CST)
5215 warn_strict_prototypes = 0;
928c19bb 5216 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5217 warn_strict_prototypes = saved_wsp;
c5c76735 5218
c2255bc4 5219 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5220 if (type_expr)
5221 {
9f33203d
JM
5222 bool inner_expr_const = true;
5223 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5224 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5225 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5226 && inner_expr_const);
c2255bc4 5227 SET_EXPR_LOCATION (ret, loc);
928c19bb 5228 }
24b97832
ILT
5229
5230 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5231 SET_EXPR_LOCATION (ret, loc);
5232
9e5b2115
PB
5233 /* C++ does not permits types to be defined in a cast, but it
5234 allows references to incomplete types. */
5235 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5236 warning_at (loc, OPT_Wc___compat,
5237 "defining a type in a cast is invalid in C++");
5238
928c19bb 5239 return ret;
400fbf9f 5240}
3e4093b6
RS
5241\f
5242/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5243 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5244 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5245 MODIFYCODE is the code for a binary operator that we use
5246 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5247 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5248 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5249 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5250
c2255bc4
AH
5251 LOCATION is the location of the MODIFYCODE operator.
5252 RHS_LOC is the location of the RHS. */
2f6e4e97 5253
3e4093b6 5254tree
32e8bb8e 5255build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5256 enum tree_code modifycode,
c2255bc4 5257 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5258{
3e4093b6
RS
5259 tree result;
5260 tree newrhs;
241f845a 5261 tree rhseval = NULL_TREE;
8ce94e44 5262 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5263 tree lhstype = TREE_TYPE (lhs);
5264 tree olhstype = lhstype;
928c19bb 5265 bool npc;
267bac10 5266 bool is_atomic_op;
e9a25f70 5267
3e4093b6
RS
5268 /* Types that aren't fully specified cannot be used in assignments. */
5269 lhs = require_complete_type (lhs);
e9a25f70 5270
3e4093b6
RS
5271 /* Avoid duplicate error messages from operands that had errors. */
5272 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5273 return error_mark_node;
400fbf9f 5274
4c2ecab0
JM
5275 /* Ensure an error for assigning a non-lvalue array to an array in
5276 C90. */
5277 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5278 {
5279 error_at (location, "assignment to expression with array type");
5280 return error_mark_node;
5281 }
5282
46a88c12 5283 /* For ObjC properties, defer this check. */
7bd11157 5284 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5285 return error_mark_node;
5286
267bac10
JM
5287 is_atomic_op = really_atomic_lvalue (lhs);
5288
8ce94e44
JM
5289 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5290 {
5291 rhs_semantic_type = TREE_TYPE (rhs);
5292 rhs = TREE_OPERAND (rhs, 0);
5293 }
5294
3e4093b6 5295 newrhs = rhs;
400fbf9f 5296
928c19bb
JM
5297 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5298 {
5299 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5300 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5301 rhs_origtype);
928c19bb
JM
5302 if (inner == error_mark_node)
5303 return error_mark_node;
5304 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5305 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5306 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5307 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5308 protected_set_expr_location (result, location);
5309 return result;
5310 }
5311
3e4093b6
RS
5312 /* If a binary op has been requested, combine the old LHS value with the RHS
5313 producing the value we should actually store into the LHS. */
5314
5315 if (modifycode != NOP_EXPR)
400fbf9f 5316 {
928c19bb 5317 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5318 lhs = stabilize_reference (lhs);
bbbbb16a 5319
267bac10
JM
5320 /* Construct the RHS for any non-atomic compound assignemnt. */
5321 if (!is_atomic_op)
5322 {
241f845a
JJ
5323 /* If in LHS op= RHS the RHS has side-effects, ensure they
5324 are preevaluated before the rest of the assignment expression's
5325 side-effects, because RHS could contain e.g. function calls
5326 that modify LHS. */
5327 if (TREE_SIDE_EFFECTS (rhs))
5328 {
5329 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5330 rhseval = newrhs;
5331 }
267bac10 5332 newrhs = build_binary_op (location,
241f845a 5333 modifycode, lhs, newrhs, 1);
267bac10
JM
5334
5335 /* The original type of the right hand side is no longer
5336 meaningful. */
5337 rhs_origtype = NULL_TREE;
5338 }
400fbf9f 5339 }
400fbf9f 5340
668ea4b1
IS
5341 if (c_dialect_objc ())
5342 {
46a88c12
NP
5343 /* Check if we are modifying an Objective-C property reference;
5344 if so, we need to generate setter calls. */
5345 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5346 if (result)
241f845a 5347 goto return_result;
46a88c12
NP
5348
5349 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5350 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5351 return error_mark_node;
5352 }
5353
9bf24266 5354 /* Give an error for storing in something that is 'const'. */
bbbd6700 5355
f37acdf9 5356 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5357 || ((TREE_CODE (lhstype) == RECORD_TYPE
5358 || TREE_CODE (lhstype) == UNION_TYPE)
5359 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5360 {
c02065fc 5361 readonly_error (location, lhs, lv_assign);
953ff289
DN
5362 return error_mark_node;
5363 }
f37acdf9
JM
5364 else if (TREE_READONLY (lhs))
5365 readonly_warning (lhs, lv_assign);
bbbd6700 5366
3e4093b6
RS
5367 /* If storing into a structure or union member,
5368 it has probably been given type `int'.
5369 Compute the type that would go with
5370 the actual amount of storage the member occupies. */
bbbd6700 5371
3e4093b6
RS
5372 if (TREE_CODE (lhs) == COMPONENT_REF
5373 && (TREE_CODE (lhstype) == INTEGER_TYPE
5374 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5375 || TREE_CODE (lhstype) == REAL_TYPE
5376 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5377 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5378
3e4093b6
RS
5379 /* If storing in a field that is in actuality a short or narrower than one,
5380 we must store in the field in its actual type. */
5381
5382 if (lhstype != TREE_TYPE (lhs))
5383 {
5384 lhs = copy_node (lhs);
5385 TREE_TYPE (lhs) = lhstype;
400fbf9f 5386 }
400fbf9f 5387
32e8bb8e
ILT
5388 /* Issue -Wc++-compat warnings about an assignment to an enum type
5389 when LHS does not have its original type. This happens for,
5390 e.g., an enum bitfield in a struct. */
5391 if (warn_cxx_compat
5392 && lhs_origtype != NULL_TREE
5393 && lhs_origtype != lhstype
5394 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5395 {
5396 tree checktype = (rhs_origtype != NULL_TREE
5397 ? rhs_origtype
5398 : TREE_TYPE (rhs));
5399 if (checktype != error_mark_node
267bac10
JM
5400 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5401 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5402 warning_at (location, OPT_Wc___compat,
5403 "enum conversion in assignment is invalid in C++");
5404 }
5405
267bac10
JM
5406 /* If the lhs is atomic, remove that qualifier. */
5407 if (is_atomic_op)
5408 {
5409 lhstype = build_qualified_type (lhstype,
5410 (TYPE_QUALS (lhstype)
5411 & ~TYPE_QUAL_ATOMIC));
5412 olhstype = build_qualified_type (olhstype,
5413 (TYPE_QUALS (lhstype)
5414 & ~TYPE_QUAL_ATOMIC));
5415 }
5416
8ce94e44
JM
5417 /* Convert new value to destination type. Fold it first, then
5418 restore any excess precision information, for the sake of
5419 conversion warnings. */
400fbf9f 5420
267bac10
JM
5421 if (!(is_atomic_op && modifycode != NOP_EXPR))
5422 {
5423 npc = null_pointer_constant_p (newrhs);
5424 newrhs = c_fully_fold (newrhs, false, NULL);
5425 if (rhs_semantic_type)
5426 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5427 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5428 rhs_origtype, ic_assign, npc,
5429 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5430 if (TREE_CODE (newrhs) == ERROR_MARK)
5431 return error_mark_node;
5432 }
400fbf9f 5433
6e955430
ZL
5434 /* Emit ObjC write barrier, if necessary. */
5435 if (c_dialect_objc () && flag_objc_gc)
5436 {
5437 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5438 if (result)
c9f9eb5d
AH
5439 {
5440 protected_set_expr_location (result, location);
241f845a 5441 goto return_result;
c9f9eb5d 5442 }
6e955430
ZL
5443 }
5444
ea4b7848 5445 /* Scan operands. */
400fbf9f 5446
267bac10
JM
5447 if (is_atomic_op)
5448 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5449 else
5450 {
5451 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5452 TREE_SIDE_EFFECTS (result) = 1;
5453 protected_set_expr_location (result, location);
5454 }
400fbf9f 5455
3e4093b6
RS
5456 /* If we got the LHS in a different type for storing in,
5457 convert the result back to the nominal type of LHS
5458 so that the value we return always has the same type
5459 as the LHS argument. */
e855c5ce 5460
3e4093b6 5461 if (olhstype == TREE_TYPE (result))
241f845a 5462 goto return_result;
c9f9eb5d 5463
68fca595
MP
5464 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5465 rhs_origtype, ic_assign, false, NULL_TREE,
5466 NULL_TREE, 0);
c9f9eb5d 5467 protected_set_expr_location (result, location);
241f845a
JJ
5468
5469return_result:
5470 if (rhseval)
5471 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5472 return result;
3e4093b6
RS
5473}
5474\f
478a1c5b
ILT
5475/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5476 This is used to implement -fplan9-extensions. */
5477
5478static bool
5479find_anonymous_field_with_type (tree struct_type, tree type)
5480{
5481 tree field;
5482 bool found;
5483
5484 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5485 || TREE_CODE (struct_type) == UNION_TYPE);
5486 found = false;
5487 for (field = TYPE_FIELDS (struct_type);
5488 field != NULL_TREE;
5489 field = TREE_CHAIN (field))
5490 {
267bac10
JM
5491 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5492 ? c_build_qualified_type (TREE_TYPE (field),
5493 TYPE_QUAL_ATOMIC)
5494 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5495 if (DECL_NAME (field) == NULL
267bac10 5496 && comptypes (type, fieldtype))
478a1c5b
ILT
5497 {
5498 if (found)
5499 return false;
5500 found = true;
5501 }
5502 else if (DECL_NAME (field) == NULL
5503 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5504 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5505 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5506 {
5507 if (found)
5508 return false;
5509 found = true;
5510 }
5511 }
5512 return found;
5513}
5514
5515/* RHS is an expression whose type is pointer to struct. If there is
5516 an anonymous field in RHS with type TYPE, then return a pointer to
5517 that field in RHS. This is used with -fplan9-extensions. This
5518 returns NULL if no conversion could be found. */
5519
5520static tree
5521convert_to_anonymous_field (location_t location, tree type, tree rhs)
5522{
5523 tree rhs_struct_type, lhs_main_type;
5524 tree field, found_field;
5525 bool found_sub_field;
5526 tree ret;
5527
5528 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5529 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5530 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5531 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5532
5533 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5534 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5535 ? c_build_qualified_type (TREE_TYPE (type),
5536 TYPE_QUAL_ATOMIC)
5537 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5538
5539 found_field = NULL_TREE;
5540 found_sub_field = false;
5541 for (field = TYPE_FIELDS (rhs_struct_type);
5542 field != NULL_TREE;
5543 field = TREE_CHAIN (field))
5544 {
5545 if (DECL_NAME (field) != NULL_TREE
5546 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5547 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5548 continue;
267bac10
JM
5549 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5550 ? c_build_qualified_type (TREE_TYPE (field),
5551 TYPE_QUAL_ATOMIC)
5552 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5553 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5554 {
5555 if (found_field != NULL_TREE)
5556 return NULL_TREE;
5557 found_field = field;
5558 }
5559 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5560 lhs_main_type))
5561 {
5562 if (found_field != NULL_TREE)
5563 return NULL_TREE;
5564 found_field = field;
5565 found_sub_field = true;
5566 }
5567 }
5568
5569 if (found_field == NULL_TREE)
5570 return NULL_TREE;
5571
5572 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5573 build_fold_indirect_ref (rhs), found_field,
5574 NULL_TREE);
5575 ret = build_fold_addr_expr_loc (location, ret);
5576
5577 if (found_sub_field)
5578 {
5579 ret = convert_to_anonymous_field (location, type, ret);
5580 gcc_assert (ret != NULL_TREE);
5581 }
5582
5583 return ret;
5584}
5585
63bc4e87
MP
5586/* Issue an error message for a bad initializer component.
5587 GMSGID identifies the message.
5588 The component name is taken from the spelling stack. */
5589
5590static void
ea58ef42 5591error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5592{
5593 char *ofwhat;
5594
5595 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5596 error_at (loc, gmsgid);
63bc4e87
MP
5597 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5598 if (*ofwhat)
d7ff7ae5 5599 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5600}
5601
5602/* Issue a pedantic warning for a bad initializer component. OPT is
5603 the option OPT_* (from options.h) controlling this warning or 0 if
5604 it is unconditionally given. GMSGID identifies the message. The
5605 component name is taken from the spelling stack. */
5606
5607static void
5608pedwarn_init (location_t location, int opt, const char *gmsgid)
5609{
5610 char *ofwhat;
d7ff7ae5 5611 bool warned;
63bc4e87
MP
5612
5613 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5614 warned = pedwarn (location, opt, gmsgid);
63bc4e87 5615 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5616 if (*ofwhat && warned)
5617 inform (location, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5618}
5619
5620/* Issue a warning for a bad initializer component.
5621
5622 OPT is the OPT_W* value corresponding to the warning option that
5623 controls this warning. GMSGID identifies the message. The
5624 component name is taken from the spelling stack. */
5625
5626static void
5627warning_init (location_t loc, int opt, const char *gmsgid)
5628{
5629 char *ofwhat;
d7ff7ae5 5630 bool warned;
63bc4e87
MP
5631
5632 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5633 warned = warning_at (loc, opt, gmsgid);
63bc4e87 5634 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5635 if (*ofwhat && warned)
5636 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5637}
5638\f
5639/* If TYPE is an array type and EXPR is a parenthesized string
5640 constant, warn if pedantic that EXPR is being used to initialize an
5641 object of type TYPE. */
5642
5643void
d033409e 5644maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5645{
5646 if (pedantic
5647 && TREE_CODE (type) == ARRAY_TYPE
5648 && TREE_CODE (expr.value) == STRING_CST
5649 && expr.original_code != STRING_CST)
d033409e 5650 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5651 "array initialized from parenthesized string constant");
5652}
5653
bbbbb16a
ILT
5654/* Convert value RHS to type TYPE as preparation for an assignment to
5655 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5656 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5657 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5658 constant before any folding.
3e4093b6
RS
5659 The real work of conversion is done by `convert'.
5660 The purpose of this function is to generate error messages
5661 for assignments that are not allowed in C.
2ac2f164
JM
5662 ERRTYPE says whether it is argument passing, assignment,
5663 initialization or return.
2f6e4e97 5664
81e5eca8
MP
5665 LOCATION is the location of the assignment, EXPR_LOC is the location of
5666 the RHS or, for a function, location of an argument.
2ac2f164 5667 FUNCTION is a tree for the function being called.
3e4093b6 5668 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5669
3e4093b6 5670static tree
81e5eca8 5671convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5672 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5673 bool null_pointer_constant, tree fundecl,
5674 tree function, int parmnum)
3e4093b6
RS
5675{
5676 enum tree_code codel = TREE_CODE (type);
8ce94e44 5677 tree orig_rhs = rhs;
3e4093b6
RS
5678 tree rhstype;
5679 enum tree_code coder;
2ac2f164 5680 tree rname = NULL_TREE;
58393038 5681 bool objc_ok = false;
2ac2f164 5682
6b4ef5c1 5683 if (errtype == ic_argpass)
2ac2f164
JM
5684 {
5685 tree selector;
5686 /* Change pointer to function to the function itself for
5687 diagnostics. */
5688 if (TREE_CODE (function) == ADDR_EXPR
5689 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5690 function = TREE_OPERAND (function, 0);
5691
5692 /* Handle an ObjC selector specially for diagnostics. */
5693 selector = objc_message_selector ();
5694 rname = function;
5695 if (selector && parmnum > 2)
5696 {
5697 rname = selector;
5698 parmnum -= 2;
5699 }
5700 }
5701
5702 /* This macro is used to emit diagnostics to ensure that all format
5703 strings are complete sentences, visible to gettext and checked at
5704 compile time. */
768952be 5705#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5706 do { \
5707 switch (errtype) \
5708 { \
5709 case ic_argpass: \
5c1bc275 5710 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5711 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5712 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5713 "expected %qT but argument is of type %qT", \
5714 type, rhstype); \
5715 break; \
1e053dfe
MLI
5716 case ic_assign: \
5717 pedwarn (LOCATION, OPT, AS); \
5718 break; \
5719 case ic_init: \
6a8f4e12 5720 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5721 break; \
5722 case ic_return: \
d033409e 5723 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5724 break; \
5725 default: \
5726 gcc_unreachable (); \
5727 } \
2ac2f164 5728 } while (0)
cb3ca04e 5729
49706e39
MLI
5730 /* This macro is used to emit diagnostics to ensure that all format
5731 strings are complete sentences, visible to gettext and checked at
768952be 5732 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 5733 extra parameter to enumerate qualifiers. */
768952be 5734#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
5735 do { \
5736 switch (errtype) \
5737 { \
5738 case ic_argpass: \
5c1bc275 5739 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 5740 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5741 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
5742 "expected %qT but argument is of type %qT", \
5743 type, rhstype); \
5744 break; \
5745 case ic_assign: \
5c1bc275 5746 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
5747 break; \
5748 case ic_init: \
5c1bc275 5749 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
5750 break; \
5751 case ic_return: \
5c1bc275 5752 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
5753 break; \
5754 default: \
5755 gcc_unreachable (); \
5756 } \
5757 } while (0)
5758
768952be
MU
5759 /* This macro is used to emit diagnostics to ensure that all format
5760 strings are complete sentences, visible to gettext and checked at
5761 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5762 warning_at instead of pedwarn. */
5763#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5764 do { \
5765 switch (errtype) \
5766 { \
5767 case ic_argpass: \
5768 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5769 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5770 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5771 "expected %qT but argument is of type %qT", \
5772 type, rhstype); \
5773 break; \
5774 case ic_assign: \
5775 warning_at (LOCATION, OPT, AS, QUALS); \
5776 break; \
5777 case ic_init: \
5778 warning_at (LOCATION, OPT, IN, QUALS); \
5779 break; \
5780 case ic_return: \
5781 warning_at (LOCATION, OPT, RE, QUALS); \
5782 break; \
5783 default: \
5784 gcc_unreachable (); \
5785 } \
5786 } while (0)
5787
8ce94e44
JM
5788 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5789 rhs = TREE_OPERAND (rhs, 0);
5790
3e4093b6
RS
5791 rhstype = TREE_TYPE (rhs);
5792 coder = TREE_CODE (rhstype);
5793
5794 if (coder == ERROR_MARK)
5795 return error_mark_node;
5796
58393038
ZL
5797 if (c_dialect_objc ())
5798 {
5799 int parmno;
5800
5801 switch (errtype)
5802 {
5803 case ic_return:
5804 parmno = 0;
5805 break;
5806
5807 case ic_assign:
5808 parmno = -1;
5809 break;
5810
5811 case ic_init:
5812 parmno = -2;
5813 break;
5814
5815 default:
5816 parmno = parmnum;
5817 break;
5818 }
5819
5820 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5821 }
5822
bbbbb16a
ILT
5823 if (warn_cxx_compat)
5824 {
5825 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5826 if (checktype != error_mark_node
5827 && TREE_CODE (type) == ENUMERAL_TYPE
5828 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5829 {
768952be
MU
5830 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5831 G_("enum conversion when passing argument "
5832 "%d of %qE is invalid in C++"),
5833 G_("enum conversion in assignment is "
5834 "invalid in C++"),
5835 G_("enum conversion in initialization is "
5836 "invalid in C++"),
5837 G_("enum conversion in return is "
5838 "invalid in C++"));
bbbbb16a
ILT
5839 }
5840 }
5841
3e4093b6 5842 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5843 return rhs;
3e4093b6
RS
5844
5845 if (coder == VOID_TYPE)
400fbf9f 5846 {
6dcc04b0
JM
5847 /* Except for passing an argument to an unprototyped function,
5848 this is a constraint violation. When passing an argument to
5849 an unprototyped function, it is compile-time undefined;
5850 making it a constraint in that case was rejected in
5851 DR#252. */
c2255bc4 5852 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5853 return error_mark_node;
400fbf9f 5854 }
808d6eaa
JM
5855 rhs = require_complete_type (rhs);
5856 if (rhs == error_mark_node)
5857 return error_mark_node;
cd192ccc
MS
5858 /* A non-reference type can convert to a reference. This handles
5859 va_start, va_copy and possibly port built-ins. */
5860 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5861 {
3e4093b6 5862 if (!lvalue_p (rhs))
400fbf9f 5863 {
c2255bc4 5864 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5865 return error_mark_node;
400fbf9f 5866 }
3e4093b6
RS
5867 if (!c_mark_addressable (rhs))
5868 return error_mark_node;
5869 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5870 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5871
81e5eca8 5872 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
5873 build_pointer_type (TREE_TYPE (type)),
5874 rhs, origtype, errtype,
5875 null_pointer_constant, fundecl, function,
5876 parmnum);
cd192ccc
MS
5877 if (rhs == error_mark_node)
5878 return error_mark_node;
3e4093b6
RS
5879
5880 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5881 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5882 return rhs;
400fbf9f 5883 }
3e4093b6 5884 /* Some types can interconvert without explicit casts. */
3274deff 5885 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5886 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5887 return convert (type, rhs);
5888 /* Arithmetic types all interconvert, and enum is treated like int. */
5889 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5890 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5891 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5892 || codel == BOOLEAN_TYPE)
5893 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5894 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5895 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5896 || coder == BOOLEAN_TYPE))
928c19bb
JM
5897 {
5898 tree ret;
5899 bool save = in_late_binary_op;
e5341100
JJ
5900 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5901 || (coder == REAL_TYPE
5902 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5903 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 5904 in_late_binary_op = true;
81e5eca8
MP
5905 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5906 ? expr_loc : location, type, orig_rhs);
e5341100 5907 in_late_binary_op = save;
928c19bb
JM
5908 return ret;
5909 }
400fbf9f 5910
79077aea
JJ
5911 /* Aggregates in different TUs might need conversion. */
5912 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5913 && codel == coder
5914 && comptypes (type, rhstype))
81e5eca8
MP
5915 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5916 ? expr_loc : location, type, rhs);
79077aea 5917
ebf0bf7f 5918 /* Conversion to a transparent union or record from its member types.
3e4093b6 5919 This applies only to function arguments. */
ebf0bf7f
JJ
5920 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5921 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5922 && errtype == ic_argpass)
400fbf9f 5923 {
0257e383 5924 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5925
910ad8de 5926 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5927 {
0257e383 5928 tree memb_type = TREE_TYPE (memb);
400fbf9f 5929
3e4093b6 5930 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5931 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5932 break;
e58cd767 5933
3e4093b6
RS
5934 if (TREE_CODE (memb_type) != POINTER_TYPE)
5935 continue;
2f6e4e97 5936
3e4093b6
RS
5937 if (coder == POINTER_TYPE)
5938 {
5939 tree ttl = TREE_TYPE (memb_type);
5940 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5941
3e4093b6
RS
5942 /* Any non-function converts to a [const][volatile] void *
5943 and vice versa; otherwise, targets must be the same.
5944 Meanwhile, the lhs target must have all the qualifiers of
5945 the rhs. */
267bac10
JM
5946 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5947 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5948 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5949 {
267bac10
JM
5950 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5951 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5952 /* If this type won't generate any warnings, use it. */
267bac10 5953 if (lquals == rquals
3e4093b6
RS
5954 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5955 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5956 ? ((lquals | rquals) == rquals)
5957 : ((lquals | rquals) == lquals)))
3e4093b6 5958 break;
400fbf9f 5959
3e4093b6 5960 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5961 if (!marginal_memb)
5962 marginal_memb = memb;
3e4093b6
RS
5963 }
5964 }
82bde854 5965
3e4093b6 5966 /* Can convert integer zero to any pointer type. */
928c19bb 5967 if (null_pointer_constant)
3e4093b6
RS
5968 {
5969 rhs = null_pointer_node;
5970 break;
5971 }
5972 }
400fbf9f 5973
0257e383 5974 if (memb || marginal_memb)
3e4093b6 5975 {
0257e383 5976 if (!memb)
3e4093b6
RS
5977 {
5978 /* We have only a marginally acceptable member type;
5979 it needs a warning. */
0257e383 5980 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5981 tree ttr = TREE_TYPE (rhstype);
714a0864 5982
3e4093b6
RS
5983 /* Const and volatile mean something different for function
5984 types, so the usual warnings are not appropriate. */
5985 if (TREE_CODE (ttr) == FUNCTION_TYPE
5986 && TREE_CODE (ttl) == FUNCTION_TYPE)
5987 {
5988 /* Because const and volatile on functions are
5989 restrictions that say the function will not do
5990 certain things, it is okay to use a const or volatile
5991 function where an ordinary one is wanted, but not
5992 vice-versa. */
36c5e70a
BE
5993 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5994 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
5995 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
5996 OPT_Wdiscarded_qualifiers,
5997 G_("passing argument %d of %qE "
5998 "makes %q#v qualified function "
5999 "pointer from unqualified"),
6000 G_("assignment makes %q#v qualified "
6001 "function pointer from "
6002 "unqualified"),
6003 G_("initialization makes %q#v qualified "
6004 "function pointer from "
6005 "unqualified"),
6006 G_("return makes %q#v qualified function "
6007 "pointer from unqualified"),
6008 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6009 }
36c5e70a
BE
6010 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6011 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6012 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6013 OPT_Wdiscarded_qualifiers,
6014 G_("passing argument %d of %qE discards "
6015 "%qv qualifier from pointer target type"),
6016 G_("assignment discards %qv qualifier "
6017 "from pointer target type"),
6018 G_("initialization discards %qv qualifier "
6019 "from pointer target type"),
6020 G_("return discards %qv qualifier from "
6021 "pointer target type"),
6022 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6023
6024 memb = marginal_memb;
3e4093b6 6025 }
400fbf9f 6026
fcf73884 6027 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6028 pedwarn (location, OPT_Wpedantic,
fcf73884 6029 "ISO C prohibits argument conversion to union type");
0e7c47fa 6030
db3927fb 6031 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6032 return build_constructor_single (type, memb, rhs);
3e4093b6 6033 }
0e7c47fa
RK
6034 }
6035
3e4093b6
RS
6036 /* Conversions among pointers */
6037 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6038 && (coder == codel))
400fbf9f 6039 {
3e4093b6
RS
6040 tree ttl = TREE_TYPE (type);
6041 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6042 tree mvl = ttl;
6043 tree mvr = ttr;
3e4093b6 6044 bool is_opaque_pointer;
264fa2db 6045 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6046 addr_space_t asl;
6047 addr_space_t asr;
400fbf9f 6048
46df2823 6049 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6050 mvl = (TYPE_ATOMIC (mvl)
6051 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6052 TYPE_QUAL_ATOMIC)
6053 : TYPE_MAIN_VARIANT (mvl));
46df2823 6054 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6055 mvr = (TYPE_ATOMIC (mvr)
6056 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6057 TYPE_QUAL_ATOMIC)
6058 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6059 /* Opaque pointers are treated like void pointers. */
f83c7f63 6060 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6061
478a1c5b
ILT
6062 /* The Plan 9 compiler permits a pointer to a struct to be
6063 automatically converted into a pointer to an anonymous field
6064 within the struct. */
6065 if (flag_plan9_extensions
6066 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6067 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6068 && mvl != mvr)
6069 {
6070 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6071 if (new_rhs != NULL_TREE)
6072 {
6073 rhs = new_rhs;
6074 rhstype = TREE_TYPE (rhs);
6075 coder = TREE_CODE (rhstype);
6076 ttr = TREE_TYPE (rhstype);
6077 mvr = TYPE_MAIN_VARIANT (ttr);
6078 }
6079 }
6080
b7e20b53 6081 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6082 for the purpose of reducing the number of false positives, we
6083 tolerate the special case of
b7e20b53 6084
c22cacf3 6085 int *p = NULL;
b7e20b53 6086
c22cacf3 6087 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6088 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6089 warning_at (errtype == ic_argpass ? expr_loc : location,
6090 OPT_Wc___compat,
6091 "request for implicit conversion "
c2255bc4 6092 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6093
36c5e70a
BE
6094 /* See if the pointers point to incompatible address spaces. */
6095 asl = TYPE_ADDR_SPACE (ttl);
6096 asr = TYPE_ADDR_SPACE (ttr);
6097 if (!null_pointer_constant_p (rhs)
6098 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6099 {
6100 switch (errtype)
6101 {
6102 case ic_argpass:
8ffcdea8 6103 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6104 "non-enclosed address space", parmnum, rname);
6105 break;
6106 case ic_assign:
6107 error_at (location, "assignment from pointer to "
6108 "non-enclosed address space");
6109 break;
6110 case ic_init:
6111 error_at (location, "initialization from pointer to "
6112 "non-enclosed address space");
6113 break;
6114 case ic_return:
6115 error_at (location, "return from pointer to "
6116 "non-enclosed address space");
6117 break;
6118 default:
6119 gcc_unreachable ();
6120 }
6121 return error_mark_node;
6122 }
6123
7876a414
KG
6124 /* Check if the right-hand side has a format attribute but the
6125 left-hand side doesn't. */
90137d8f 6126 if (warn_suggest_attribute_format
104f8784 6127 && check_missing_format_attribute (type, rhstype))
c22cacf3 6128 {
104f8784
KG
6129 switch (errtype)
6130 {
6131 case ic_argpass:
8ffcdea8 6132 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6133 "argument %d of %qE might be "
6134 "a candidate for a format attribute",
6135 parmnum, rname);
104f8784
KG
6136 break;
6137 case ic_assign:
90137d8f 6138 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6139 "assignment left-hand side might be "
6140 "a candidate for a format attribute");
104f8784
KG
6141 break;
6142 case ic_init:
90137d8f 6143 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6144 "initialization left-hand side might be "
6145 "a candidate for a format attribute");
104f8784
KG
6146 break;
6147 case ic_return:
90137d8f 6148 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6149 "return type might be "
6150 "a candidate for a format attribute");
104f8784
KG
6151 break;
6152 default:
6153 gcc_unreachable ();
6154 }
7876a414 6155 }
c22cacf3 6156
3e4093b6
RS
6157 /* Any non-function converts to a [const][volatile] void *
6158 and vice versa; otherwise, targets must be the same.
6159 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6160 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6161 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6162 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6163 || is_opaque_pointer
f8a93a2e
JJ
6164 || ((c_common_unsigned_type (mvl)
6165 == c_common_unsigned_type (mvr))
267bac10
JM
6166 && (c_common_signed_type (mvl)
6167 == c_common_signed_type (mvr))
6168 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6169 {
768952be
MU
6170 /* Warn about loss of qualifers from pointers to arrays with
6171 qualifiers on the element type. */
6172 if (TREE_CODE (ttr) == ARRAY_TYPE)
6173 {
6174 ttr = strip_array_types (ttr);
6175 ttl = strip_array_types (ttl);
6176
6177 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6178 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6179 WARNING_FOR_QUALIFIERS (location, expr_loc,
6180 OPT_Wdiscarded_array_qualifiers,
6181 G_("passing argument %d of %qE discards "
6182 "%qv qualifier from pointer target type"),
6183 G_("assignment discards %qv qualifier "
6184 "from pointer target type"),
6185 G_("initialization discards %qv qualifier "
6186 "from pointer target type"),
6187 G_("return discards %qv qualifier from "
6188 "pointer target type"),
6189 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6190 }
6191 else if (pedantic
3e4093b6
RS
6192 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6193 ||
6194 (VOID_TYPE_P (ttr)
928c19bb 6195 && !null_pointer_constant
3e4093b6 6196 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6197 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6198 G_("ISO C forbids passing argument %d of "
6199 "%qE between function pointer "
6200 "and %<void *%>"),
6201 G_("ISO C forbids assignment between "
6202 "function pointer and %<void *%>"),
6203 G_("ISO C forbids initialization between "
6204 "function pointer and %<void *%>"),
6205 G_("ISO C forbids return between function "
6206 "pointer and %<void *%>"));
3e4093b6
RS
6207 /* Const and volatile mean something different for function types,
6208 so the usual warnings are not appropriate. */
6209 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6210 && TREE_CODE (ttl) != FUNCTION_TYPE)
6211 {
768952be
MU
6212 /* Don't warn about loss of qualifier for conversions from
6213 qualified void* to pointers to arrays with corresponding
6214 qualifier on the element type. */
6215 if (!pedantic)
6216 ttl = strip_array_types (ttl);
6217
267bac10
JM
6218 /* Assignments between atomic and non-atomic objects are OK. */
6219 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6220 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6221 {
768952be
MU
6222 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6223 OPT_Wdiscarded_qualifiers,
6224 G_("passing argument %d of %qE discards "
6225 "%qv qualifier from pointer target type"),
6226 G_("assignment discards %qv qualifier "
6227 "from pointer target type"),
6228 G_("initialization discards %qv qualifier "
6229 "from pointer target type"),
6230 G_("return discards %qv qualifier from "
6231 "pointer target type"),
6232 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6233 }
3e4093b6
RS
6234 /* If this is not a case of ignoring a mismatch in signedness,
6235 no warning. */
6236 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6237 || target_cmp)
3e4093b6
RS
6238 ;
6239 /* If there is a mismatch, do warn. */
f2fd3821 6240 else if (warn_pointer_sign)
768952be
MU
6241 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6242 G_("pointer targets in passing argument "
6243 "%d of %qE differ in signedness"),
6244 G_("pointer targets in assignment "
6245 "differ in signedness"),
6246 G_("pointer targets in initialization "
6247 "differ in signedness"),
6248 G_("pointer targets in return differ "
6249 "in signedness"));
3e4093b6
RS
6250 }
6251 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6252 && TREE_CODE (ttr) == FUNCTION_TYPE)
6253 {
6254 /* Because const and volatile on functions are restrictions
6255 that say the function will not do certain things,
6256 it is okay to use a const or volatile function
6257 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6258 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6259 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6260 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6261 OPT_Wdiscarded_qualifiers,
6262 G_("passing argument %d of %qE makes "
6263 "%q#v qualified function pointer "
6264 "from unqualified"),
6265 G_("assignment makes %q#v qualified function "
6266 "pointer from unqualified"),
6267 G_("initialization makes %q#v qualified "
6268 "function pointer from unqualified"),
6269 G_("return makes %q#v qualified function "
6270 "pointer from unqualified"),
6271 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6272 }
6273 }
6274 else
58393038
ZL
6275 /* Avoid warning about the volatile ObjC EH puts on decls. */
6276 if (!objc_ok)
768952be
MU
6277 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6278 OPT_Wincompatible_pointer_types,
6279 G_("passing argument %d of %qE from "
6280 "incompatible pointer type"),
6281 G_("assignment from incompatible pointer type"),
6282 G_("initialization from incompatible "
6283 "pointer type"),
6284 G_("return from incompatible pointer type"));
58393038 6285
3e4093b6
RS
6286 return convert (type, rhs);
6287 }
b494fd98
EB
6288 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6289 {
6dcc04b0
JM
6290 /* ??? This should not be an error when inlining calls to
6291 unprototyped functions. */
c2255bc4 6292 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6293 return error_mark_node;
6294 }
3e4093b6 6295 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6296 {
3e4093b6
RS
6297 /* An explicit constant 0 can convert to a pointer,
6298 or one that results from arithmetic, even including
6299 a cast to integer type. */
928c19bb 6300 if (!null_pointer_constant)
768952be
MU
6301 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6302 OPT_Wint_conversion,
6303 G_("passing argument %d of %qE makes "
6304 "pointer from integer without a cast"),
6305 G_("assignment makes pointer from integer "
6306 "without a cast"),
6307 G_("initialization makes pointer from "
6308 "integer without a cast"),
6309 G_("return makes pointer from integer "
6310 "without a cast"));
b3006337
EB
6311
6312 return convert (type, rhs);
400fbf9f 6313 }
3e4093b6 6314 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6315 {
768952be
MU
6316 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6317 OPT_Wint_conversion,
6318 G_("passing argument %d of %qE makes integer "
6319 "from pointer without a cast"),
6320 G_("assignment makes integer from pointer "
6321 "without a cast"),
6322 G_("initialization makes integer from pointer "
6323 "without a cast"),
6324 G_("return makes integer from pointer "
6325 "without a cast"));
3e4093b6
RS
6326 return convert (type, rhs);
6327 }
6328 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6329 {
6330 tree ret;
6331 bool save = in_late_binary_op;
6332 in_late_binary_op = true;
6333 ret = convert (type, rhs);
6334 in_late_binary_op = save;
6335 return ret;
6336 }
400fbf9f 6337
2ac2f164 6338 switch (errtype)
3e4093b6 6339 {
2ac2f164 6340 case ic_argpass:
8ffcdea8
MP
6341 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6342 rname);
a7da8b42 6343 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6344 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6345 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6346 break;
6347 case ic_assign:
c2255bc4
AH
6348 error_at (location, "incompatible types when assigning to type %qT from "
6349 "type %qT", type, rhstype);
2ac2f164
JM
6350 break;
6351 case ic_init:
c2255bc4 6352 error_at (location,
8ffcdea8 6353 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6354 type, rhstype);
2ac2f164
JM
6355 break;
6356 case ic_return:
c2255bc4 6357 error_at (location,
8ffcdea8 6358 "incompatible types when returning type %qT but %qT was "
c2255bc4 6359 "expected", rhstype, type);
2ac2f164
JM
6360 break;
6361 default:
6362 gcc_unreachable ();
400fbf9f 6363 }
53b01f59 6364
3e4093b6
RS
6365 return error_mark_node;
6366}
3e4093b6
RS
6367\f
6368/* If VALUE is a compound expr all of whose expressions are constant, then
6369 return its value. Otherwise, return error_mark_node.
15b732b2 6370
3e4093b6
RS
6371 This is for handling COMPOUND_EXPRs as initializer elements
6372 which is allowed with a warning when -pedantic is specified. */
15b732b2 6373
3e4093b6
RS
6374static tree
6375valid_compound_expr_initializer (tree value, tree endtype)
6376{
6377 if (TREE_CODE (value) == COMPOUND_EXPR)
6378 {
6379 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6380 == error_mark_node)
6381 return error_mark_node;
6382 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6383 endtype);
6384 }
116df786 6385 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6386 return error_mark_node;
6387 else
6388 return value;
15b732b2 6389}
400fbf9f 6390\f
3e4093b6
RS
6391/* Perform appropriate conversions on the initial value of a variable,
6392 store it in the declaration DECL,
6393 and print any error messages that are appropriate.
bbbbb16a 6394 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6395 If the init is invalid, store an ERROR_MARK.
6396
6397 INIT_LOC is the location of the initial value. */
400fbf9f 6398
3e4093b6 6399void
c2255bc4 6400store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6401{
3e4093b6 6402 tree value, type;
928c19bb 6403 bool npc = false;
400fbf9f 6404
3e4093b6 6405 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6406
3e4093b6
RS
6407 type = TREE_TYPE (decl);
6408 if (TREE_CODE (type) == ERROR_MARK)
6409 return;
400fbf9f 6410
3e4093b6 6411 /* Digest the specified initializer into an expression. */
400fbf9f 6412
928c19bb
JM
6413 if (init)
6414 npc = null_pointer_constant_p (init);
c2255bc4
AH
6415 value = digest_init (init_loc, type, init, origtype, npc,
6416 true, TREE_STATIC (decl));
400fbf9f 6417
3e4093b6 6418 /* Store the expression if valid; else report error. */
400fbf9f 6419
8400e75e 6420 if (!in_system_header_at (input_location)
3f75a254 6421 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6422 warning (OPT_Wtraditional, "traditional C rejects automatic "
6423 "aggregate initialization");
2f6e4e97 6424
dea63e49
JJ
6425 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6426 DECL_INITIAL (decl) = value;
400fbf9f 6427
3e4093b6
RS
6428 /* ANSI wants warnings about out-of-range constant initializers. */
6429 STRIP_TYPE_NOPS (value);
b8698a0f 6430 if (TREE_STATIC (decl))
c2658540 6431 constant_expression_warning (value);
400fbf9f 6432
3e4093b6
RS
6433 /* Check if we need to set array size from compound literal size. */
6434 if (TREE_CODE (type) == ARRAY_TYPE
6435 && TYPE_DOMAIN (type) == 0
6436 && value != error_mark_node)
400fbf9f 6437 {
3e4093b6
RS
6438 tree inside_init = init;
6439
ed248cf7 6440 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6441 inside_init = fold (inside_init);
6442
6443 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6444 {
8d9f82d5 6445 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6446
8d9f82d5 6447 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6448 {
6449 /* For int foo[] = (int [3]){1}; we need to set array size
6450 now since later on array initializer will be just the
6451 brace enclosed list of the compound literal. */
e30ecc5d 6452 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6453 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6454 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6455 layout_type (type);
8d9f82d5 6456 layout_decl (cldecl, 0);
e30ecc5d
JJ
6457 TREE_TYPE (decl)
6458 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6459 }
6460 }
400fbf9f 6461 }
3e4093b6
RS
6462}
6463\f
6464/* Methods for storing and printing names for error messages. */
400fbf9f 6465
3e4093b6
RS
6466/* Implement a spelling stack that allows components of a name to be pushed
6467 and popped. Each element on the stack is this structure. */
400fbf9f 6468
3e4093b6
RS
6469struct spelling
6470{
6471 int kind;
6472 union
400fbf9f 6473 {
a0f0ab9f 6474 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6475 const char *s;
6476 } u;
6477};
2f6e4e97 6478
3e4093b6
RS
6479#define SPELLING_STRING 1
6480#define SPELLING_MEMBER 2
6481#define SPELLING_BOUNDS 3
400fbf9f 6482
3e4093b6
RS
6483static struct spelling *spelling; /* Next stack element (unused). */
6484static struct spelling *spelling_base; /* Spelling stack base. */
6485static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6486
3e4093b6
RS
6487/* Macros to save and restore the spelling stack around push_... functions.
6488 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6489
3e4093b6
RS
6490#define SPELLING_DEPTH() (spelling - spelling_base)
6491#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6492
3e4093b6
RS
6493/* Push an element on the spelling stack with type KIND and assign VALUE
6494 to MEMBER. */
400fbf9f 6495
3e4093b6
RS
6496#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6497{ \
6498 int depth = SPELLING_DEPTH (); \
6499 \
6500 if (depth >= spelling_size) \
6501 { \
6502 spelling_size += 10; \
cca8ead2
BI
6503 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6504 spelling_size); \
3e4093b6
RS
6505 RESTORE_SPELLING_DEPTH (depth); \
6506 } \
6507 \
6508 spelling->kind = (KIND); \
6509 spelling->MEMBER = (VALUE); \
6510 spelling++; \
6511}
400fbf9f 6512
3e4093b6 6513/* Push STRING on the stack. Printed literally. */
400fbf9f 6514
3e4093b6
RS
6515static void
6516push_string (const char *string)
6517{
6518 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6519}
400fbf9f 6520
3e4093b6 6521/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6522
3e4093b6
RS
6523static void
6524push_member_name (tree decl)
6525{
6526 const char *const string
88388a52
JM
6527 = (DECL_NAME (decl)
6528 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6529 : _("<anonymous>"));
3e4093b6
RS
6530 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6531}
400fbf9f 6532
3e4093b6 6533/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6534
3e4093b6 6535static void
a0f0ab9f 6536push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6537{
6538 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6539}
bb58bec5 6540
3e4093b6 6541/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6542
3e4093b6
RS
6543static int
6544spelling_length (void)
6545{
6546 int size = 0;
6547 struct spelling *p;
400fbf9f 6548
3e4093b6
RS
6549 for (p = spelling_base; p < spelling; p++)
6550 {
6551 if (p->kind == SPELLING_BOUNDS)
6552 size += 25;
6553 else
6554 size += strlen (p->u.s) + 1;
6555 }
6556
6557 return size;
400fbf9f 6558}
400fbf9f 6559
3e4093b6 6560/* Print the spelling to BUFFER and return it. */
400fbf9f 6561
3e4093b6
RS
6562static char *
6563print_spelling (char *buffer)
400fbf9f 6564{
3e4093b6
RS
6565 char *d = buffer;
6566 struct spelling *p;
400fbf9f 6567
3e4093b6
RS
6568 for (p = spelling_base; p < spelling; p++)
6569 if (p->kind == SPELLING_BOUNDS)
6570 {
a0f0ab9f 6571 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6572 d += strlen (d);
6573 }
6574 else
6575 {
6576 const char *s;
6577 if (p->kind == SPELLING_MEMBER)
6578 *d++ = '.';
6579 for (s = p->u.s; (*d = *s++); d++)
6580 ;
6581 }
6582 *d++ = '\0';
6583 return buffer;
6584}
400fbf9f 6585
3e4093b6
RS
6586/* Digest the parser output INIT as an initializer for type TYPE.
6587 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6588
bbbbb16a
ILT
6589 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6590
928c19bb
JM
6591 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6592
916c5919
JM
6593 If INIT is a string constant, STRICT_STRING is true if it is
6594 unparenthesized or we should not warn here for it being parenthesized.
6595 For other types of INIT, STRICT_STRING is not used.
6596
c2255bc4
AH
6597 INIT_LOC is the location of the INIT.
6598
3e4093b6
RS
6599 REQUIRE_CONSTANT requests an error if non-constant initializers or
6600 elements are seen. */
7e842ef8 6601
3e4093b6 6602static tree
c2255bc4
AH
6603digest_init (location_t init_loc, tree type, tree init, tree origtype,
6604 bool null_pointer_constant, bool strict_string,
6605 int require_constant)
3e4093b6
RS
6606{
6607 enum tree_code code = TREE_CODE (type);
6608 tree inside_init = init;
8ce94e44 6609 tree semantic_type = NULL_TREE;
928c19bb 6610 bool maybe_const = true;
7e842ef8 6611
3e4093b6 6612 if (type == error_mark_node
f01da1a5 6613 || !init
7a0ca710 6614 || error_operand_p (init))
3e4093b6 6615 return error_mark_node;
7e842ef8 6616
ed248cf7 6617 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6618
8ce94e44
JM
6619 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6620 {
6621 semantic_type = TREE_TYPE (inside_init);
6622 inside_init = TREE_OPERAND (inside_init, 0);
6623 }
928c19bb
JM
6624 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6625 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6626
3e4093b6
RS
6627 /* Initialization of an array of chars from a string constant
6628 optionally enclosed in braces. */
7e842ef8 6629
197463ae
JM
6630 if (code == ARRAY_TYPE && inside_init
6631 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6632 {
267bac10
JM
6633 tree typ1
6634 = (TYPE_ATOMIC (TREE_TYPE (type))
6635 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6636 TYPE_QUAL_ATOMIC)
6637 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6638 /* Note that an array could be both an array of character type
6639 and an array of wchar_t if wchar_t is signed char or unsigned
6640 char. */
6641 bool char_array = (typ1 == char_type_node
6642 || typ1 == signed_char_type_node
6643 || typ1 == unsigned_char_type_node);
6644 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6645 bool char16_array = !!comptypes (typ1, char16_type_node);
6646 bool char32_array = !!comptypes (typ1, char32_type_node);
6647
6648 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6649 {
916c5919 6650 struct c_expr expr;
c466b2cd 6651 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6652 expr.value = inside_init;
6653 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6654 expr.original_type = NULL;
d033409e 6655 maybe_warn_string_init (init_loc, type, expr);
916c5919 6656
a45e580b 6657 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6658 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6659 "initialization of a flexible array member");
6660
3e4093b6 6661 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6662 TYPE_MAIN_VARIANT (type)))
3e4093b6 6663 return inside_init;
7e842ef8 6664
c466b2cd 6665 if (char_array)
3e4093b6 6666 {
c466b2cd
KVH
6667 if (typ2 != char_type_node)
6668 {
ea58ef42
MP
6669 error_init (init_loc, "char-array initialized from wide "
6670 "string");
c466b2cd
KVH
6671 return error_mark_node;
6672 }
3e4093b6 6673 }
c466b2cd 6674 else
3e4093b6 6675 {
c466b2cd
KVH
6676 if (typ2 == char_type_node)
6677 {
ea58ef42
MP
6678 error_init (init_loc, "wide character array initialized "
6679 "from non-wide string");
c466b2cd
KVH
6680 return error_mark_node;
6681 }
6682 else if (!comptypes(typ1, typ2))
6683 {
ea58ef42
MP
6684 error_init (init_loc, "wide character array initialized "
6685 "from incompatible wide string");
c466b2cd
KVH
6686 return error_mark_node;
6687 }
7e842ef8 6688 }
2f6e4e97 6689
3e4093b6
RS
6690 TREE_TYPE (inside_init) = type;
6691 if (TYPE_DOMAIN (type) != 0
6692 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6693 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6694 {
6695 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6696
c466b2cd 6697 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6698 because it's ok to ignore the terminating null char
6699 that is counted in the length of the constant. */
5eb4df45
ILT
6700 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6701 (len
6702 - (TYPE_PRECISION (typ1)
6703 / BITS_PER_UNIT))))
6704 pedwarn_init (init_loc, 0,
6705 ("initializer-string for array of chars "
6706 "is too long"));
6707 else if (warn_cxx_compat
6708 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6709 warning_at (init_loc, OPT_Wc___compat,
6710 ("initializer-string for array chars "
6711 "is too long for C++"));
6712 }
7e842ef8 6713
3e4093b6 6714 return inside_init;
7e842ef8 6715 }
197463ae
JM
6716 else if (INTEGRAL_TYPE_P (typ1))
6717 {
ea58ef42 6718 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6719 "from string constant");
6720 return error_mark_node;
6721 }
7e842ef8
PE
6722 }
6723
3e4093b6
RS
6724 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6725 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6726 below and handle as a constructor. */
e89be13b
JJ
6727 if (code == VECTOR_TYPE
6728 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6729 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6730 && TREE_CONSTANT (inside_init))
6731 {
6732 if (TREE_CODE (inside_init) == VECTOR_CST
6733 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6734 TYPE_MAIN_VARIANT (type)))
6735 return inside_init;
6736
6737 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6738 {
4038c495
GB
6739 unsigned HOST_WIDE_INT ix;
6740 tree value;
6741 bool constant_p = true;
e89be13b
JJ
6742
6743 /* Iterate through elements and check if all constructor
6744 elements are *_CSTs. */
4038c495
GB
6745 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6746 if (!CONSTANT_CLASS_P (value))
6747 {
6748 constant_p = false;
6749 break;
6750 }
e89be13b 6751
4038c495
GB
6752 if (constant_p)
6753 return build_vector_from_ctor (type,
6754 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6755 }
6756 }
6035d635 6757
ca085fd7
MLI
6758 if (warn_sequence_point)
6759 verify_sequence_points (inside_init);
6760
3e4093b6
RS
6761 /* Any type can be initialized
6762 from an expression of the same type, optionally with braces. */
400fbf9f 6763
3e4093b6
RS
6764 if (inside_init && TREE_TYPE (inside_init) != 0
6765 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6766 TYPE_MAIN_VARIANT (type))
3e4093b6 6767 || (code == ARRAY_TYPE
132da1a5 6768 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6769 || (code == VECTOR_TYPE
132da1a5 6770 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6771 || (code == POINTER_TYPE
3897f229 6772 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6773 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6774 TREE_TYPE (type)))))
3e4093b6
RS
6775 {
6776 if (code == POINTER_TYPE)
b494fd98 6777 {
b494fd98
EB
6778 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6779 {
f2a71bbc
JM
6780 if (TREE_CODE (inside_init) == STRING_CST
6781 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6782 inside_init = array_to_pointer_conversion
6783 (init_loc, inside_init);
f2a71bbc
JM
6784 else
6785 {
ea58ef42 6786 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
6787 return error_mark_node;
6788 }
b494fd98 6789 }
f2a71bbc 6790 }
b494fd98 6791
bae39a73
NS
6792 if (code == VECTOR_TYPE)
6793 /* Although the types are compatible, we may require a
6794 conversion. */
6795 inside_init = convert (type, inside_init);
3e4093b6 6796
ca58211b 6797 if (require_constant
3e4093b6 6798 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6799 {
3e4093b6
RS
6800 /* As an extension, allow initializing objects with static storage
6801 duration with compound literals (which are then treated just as
ca58211b
PB
6802 the brace enclosed list they contain). Also allow this for
6803 vectors, as we can only assign them with compound literals. */
7278465e
MP
6804 if (flag_isoc99 && code != VECTOR_TYPE)
6805 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6806 "is not constant");
3e4093b6
RS
6807 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6808 inside_init = DECL_INITIAL (decl);
400fbf9f 6809 }
3e4093b6
RS
6810
6811 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6812 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6813 {
ea58ef42
MP
6814 error_init (init_loc, "array initialized from non-constant array "
6815 "expression");
3e4093b6 6816 return error_mark_node;
400fbf9f 6817 }
400fbf9f 6818
c1771a20 6819 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6820 -pedantic-errors is specified. In the later case, we always want
6821 an error. In the former case, we simply want a warning. */
6822 if (require_constant && pedantic
6823 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6824 {
6825 inside_init
6826 = valid_compound_expr_initializer (inside_init,
6827 TREE_TYPE (inside_init));
6828 if (inside_init == error_mark_node)
ea58ef42 6829 error_init (init_loc, "initializer element is not constant");
2f6e4e97 6830 else
c1771a20 6831 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6832 "initializer element is not constant");
3e4093b6
RS
6833 if (flag_pedantic_errors)
6834 inside_init = error_mark_node;
6835 }
6836 else if (require_constant
116df786
RH
6837 && !initializer_constant_valid_p (inside_init,
6838 TREE_TYPE (inside_init)))
3e4093b6 6839 {
ea58ef42 6840 error_init (init_loc, "initializer element is not constant");
3e4093b6 6841 inside_init = error_mark_node;
8b40563c 6842 }
928c19bb 6843 else if (require_constant && !maybe_const)
c2255bc4 6844 pedwarn_init (init_loc, 0,
928c19bb 6845 "initializer element is not a constant expression");
f735a153 6846
90137d8f 6847 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6848 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
6849 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6850 type, inside_init, origtype,
bbbbb16a 6851 ic_init, null_pointer_constant,
928c19bb 6852 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6853 return inside_init;
6854 }
f735a153 6855
3e4093b6 6856 /* Handle scalar types, including conversions. */
400fbf9f 6857
ab22c1fa
CF
6858 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6859 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6860 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6861 {
f2a71bbc
JM
6862 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6863 && (TREE_CODE (init) == STRING_CST
6864 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6865 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6866 if (semantic_type)
6867 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6868 inside_init);
3e4093b6 6869 inside_init
68fca595
MP
6870 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6871 inside_init, origtype, ic_init,
6872 null_pointer_constant, NULL_TREE, NULL_TREE,
6873 0);
2f6e4e97 6874
3274deff
JW
6875 /* Check to see if we have already given an error message. */
6876 if (inside_init == error_mark_node)
6877 ;
3f75a254 6878 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6879 {
ea58ef42 6880 error_init (init_loc, "initializer element is not constant");
3e4093b6 6881 inside_init = error_mark_node;
400fbf9f 6882 }
3e4093b6 6883 else if (require_constant
116df786
RH
6884 && !initializer_constant_valid_p (inside_init,
6885 TREE_TYPE (inside_init)))
400fbf9f 6886 {
ea58ef42
MP
6887 error_init (init_loc, "initializer element is not computable at "
6888 "load time");
3e4093b6 6889 inside_init = error_mark_node;
400fbf9f 6890 }
928c19bb 6891 else if (require_constant && !maybe_const)
c2255bc4 6892 pedwarn_init (init_loc, 0,
928c19bb 6893 "initializer element is not a constant expression");
3e4093b6
RS
6894
6895 return inside_init;
400fbf9f 6896 }
d9fc6069 6897
3e4093b6 6898 /* Come here only for records and arrays. */
d9fc6069 6899
3e4093b6 6900 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6901 {
ea58ef42 6902 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 6903 return error_mark_node;
d9fc6069 6904 }
3e4093b6 6905
ea58ef42 6906 error_init (init_loc, "invalid initializer");
3e4093b6 6907 return error_mark_node;
d9fc6069 6908}
400fbf9f 6909\f
3e4093b6 6910/* Handle initializers that use braces. */
400fbf9f 6911
3e4093b6
RS
6912/* Type of object we are accumulating a constructor for.
6913 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6914static tree constructor_type;
400fbf9f 6915
3e4093b6
RS
6916/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6917 left to fill. */
6918static tree constructor_fields;
400fbf9f 6919
3e4093b6
RS
6920/* For an ARRAY_TYPE, this is the specified index
6921 at which to store the next element we get. */
6922static tree constructor_index;
400fbf9f 6923
3e4093b6
RS
6924/* For an ARRAY_TYPE, this is the maximum index. */
6925static tree constructor_max_index;
400fbf9f 6926
3e4093b6
RS
6927/* For a RECORD_TYPE, this is the first field not yet written out. */
6928static tree constructor_unfilled_fields;
400fbf9f 6929
3e4093b6
RS
6930/* For an ARRAY_TYPE, this is the index of the first element
6931 not yet written out. */
6932static tree constructor_unfilled_index;
895ea614 6933
3e4093b6
RS
6934/* In a RECORD_TYPE, the byte index of the next consecutive field.
6935 This is so we can generate gaps between fields, when appropriate. */
6936static tree constructor_bit_index;
10d5caec 6937
3e4093b6
RS
6938/* If we are saving up the elements rather than allocating them,
6939 this is the list of elements so far (in reverse order,
6940 most recent first). */
9771b263 6941static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6942
3e4093b6
RS
6943/* 1 if constructor should be incrementally stored into a constructor chain,
6944 0 if all the elements should be kept in AVL tree. */
6945static int constructor_incremental;
ad47f1e5 6946
3e4093b6
RS
6947/* 1 if so far this constructor's elements are all compile-time constants. */
6948static int constructor_constant;
ad47f1e5 6949
3e4093b6
RS
6950/* 1 if so far this constructor's elements are all valid address constants. */
6951static int constructor_simple;
ad47f1e5 6952
928c19bb
JM
6953/* 1 if this constructor has an element that cannot be part of a
6954 constant expression. */
6955static int constructor_nonconst;
6956
3e4093b6
RS
6957/* 1 if this constructor is erroneous so far. */
6958static int constructor_erroneous;
d45cf215 6959
9bac5cbb
G
6960/* 1 if this constructor is the universal zero initializer { 0 }. */
6961static int constructor_zeroinit;
6962
3e4093b6
RS
6963/* Structure for managing pending initializer elements, organized as an
6964 AVL tree. */
d45cf215 6965
3e4093b6 6966struct init_node
d45cf215 6967{
3e4093b6
RS
6968 struct init_node *left, *right;
6969 struct init_node *parent;
6970 int balance;
6971 tree purpose;
6972 tree value;
bbbbb16a 6973 tree origtype;
d45cf215
RS
6974};
6975
3e4093b6
RS
6976/* Tree of pending elements at this constructor level.
6977 These are elements encountered out of order
6978 which belong at places we haven't reached yet in actually
6979 writing the output.
6980 Will never hold tree nodes across GC runs. */
6981static struct init_node *constructor_pending_elts;
d45cf215 6982
3e4093b6
RS
6983/* The SPELLING_DEPTH of this constructor. */
6984static int constructor_depth;
d45cf215 6985
3e4093b6
RS
6986/* DECL node for which an initializer is being read.
6987 0 means we are reading a constructor expression
6988 such as (struct foo) {...}. */
6989static tree constructor_decl;
d45cf215 6990
3e4093b6
RS
6991/* Nonzero if this is an initializer for a top-level decl. */
6992static int constructor_top_level;
d45cf215 6993
3e4093b6
RS
6994/* Nonzero if there were any member designators in this initializer. */
6995static int constructor_designated;
d45cf215 6996
3e4093b6
RS
6997/* Nesting depth of designator list. */
6998static int designator_depth;
d45cf215 6999
3e4093b6 7000/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7001static int designator_erroneous;
d45cf215 7002
3e4093b6
RS
7003\f
7004/* This stack has a level for each implicit or explicit level of
7005 structuring in the initializer, including the outermost one. It
7006 saves the values of most of the variables above. */
d45cf215 7007
3e4093b6
RS
7008struct constructor_range_stack;
7009
7010struct constructor_stack
d45cf215 7011{
3e4093b6
RS
7012 struct constructor_stack *next;
7013 tree type;
7014 tree fields;
7015 tree index;
7016 tree max_index;
7017 tree unfilled_index;
7018 tree unfilled_fields;
7019 tree bit_index;
9771b263 7020 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7021 struct init_node *pending_elts;
7022 int offset;
7023 int depth;
916c5919 7024 /* If value nonzero, this value should replace the entire
3e4093b6 7025 constructor at this level. */
916c5919 7026 struct c_expr replacement_value;
3e4093b6
RS
7027 struct constructor_range_stack *range_stack;
7028 char constant;
7029 char simple;
928c19bb 7030 char nonconst;
3e4093b6
RS
7031 char implicit;
7032 char erroneous;
7033 char outer;
7034 char incremental;
7035 char designated;
976d5a22 7036 int designator_depth;
3e4093b6 7037};
d45cf215 7038
802415d1 7039static struct constructor_stack *constructor_stack;
d45cf215 7040
3e4093b6
RS
7041/* This stack represents designators from some range designator up to
7042 the last designator in the list. */
d45cf215 7043
3e4093b6
RS
7044struct constructor_range_stack
7045{
7046 struct constructor_range_stack *next, *prev;
7047 struct constructor_stack *stack;
7048 tree range_start;
7049 tree index;
7050 tree range_end;
7051 tree fields;
7052};
d45cf215 7053
802415d1 7054static struct constructor_range_stack *constructor_range_stack;
d45cf215 7055
3e4093b6
RS
7056/* This stack records separate initializers that are nested.
7057 Nested initializers can't happen in ANSI C, but GNU C allows them
7058 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7059
3e4093b6 7060struct initializer_stack
d45cf215 7061{
3e4093b6
RS
7062 struct initializer_stack *next;
7063 tree decl;
3e4093b6
RS
7064 struct constructor_stack *constructor_stack;
7065 struct constructor_range_stack *constructor_range_stack;
9771b263 7066 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7067 struct spelling *spelling;
7068 struct spelling *spelling_base;
7069 int spelling_size;
7070 char top_level;
7071 char require_constant_value;
7072 char require_constant_elements;
7073};
d45cf215 7074
802415d1 7075static struct initializer_stack *initializer_stack;
3e4093b6
RS
7076\f
7077/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7078
7079void
a396f8ae 7080start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7081{
3e4093b6 7082 const char *locus;
cceb1885 7083 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7084
3e4093b6 7085 p->decl = constructor_decl;
3e4093b6
RS
7086 p->require_constant_value = require_constant_value;
7087 p->require_constant_elements = require_constant_elements;
7088 p->constructor_stack = constructor_stack;
7089 p->constructor_range_stack = constructor_range_stack;
7090 p->elements = constructor_elements;
7091 p->spelling = spelling;
7092 p->spelling_base = spelling_base;
7093 p->spelling_size = spelling_size;
7094 p->top_level = constructor_top_level;
7095 p->next = initializer_stack;
7096 initializer_stack = p;
400fbf9f 7097
3e4093b6 7098 constructor_decl = decl;
3e4093b6
RS
7099 constructor_designated = 0;
7100 constructor_top_level = top_level;
400fbf9f 7101
6f17bbcf 7102 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7103 {
7104 require_constant_value = TREE_STATIC (decl);
7105 require_constant_elements
7106 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7107 /* For a scalar, you can always use any value to initialize,
7108 even within braces. */
7109 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7110 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7111 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7112 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 7113 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7114 }
7115 else
7116 {
7117 require_constant_value = 0;
7118 require_constant_elements = 0;
88388a52 7119 locus = _("(anonymous)");
3e4093b6 7120 }
b71c7f8a 7121
3e4093b6
RS
7122 constructor_stack = 0;
7123 constructor_range_stack = 0;
b71c7f8a 7124
9bac5cbb 7125 found_missing_braces = 0;
3e4093b6
RS
7126
7127 spelling_base = 0;
7128 spelling_size = 0;
7129 RESTORE_SPELLING_DEPTH (0);
7130
7131 if (locus)
7132 push_string (locus);
7133}
7134
7135void
7136finish_init (void)
b71c7f8a 7137{
3e4093b6 7138 struct initializer_stack *p = initializer_stack;
b71c7f8a 7139
3e4093b6
RS
7140 /* Free the whole constructor stack of this initializer. */
7141 while (constructor_stack)
7142 {
7143 struct constructor_stack *q = constructor_stack;
7144 constructor_stack = q->next;
7145 free (q);
7146 }
7147
366de0ce 7148 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7149
7150 /* Pop back to the data of the outer initializer (if any). */
36579663 7151 free (spelling_base);
3aeb3655 7152
3e4093b6 7153 constructor_decl = p->decl;
3e4093b6
RS
7154 require_constant_value = p->require_constant_value;
7155 require_constant_elements = p->require_constant_elements;
7156 constructor_stack = p->constructor_stack;
7157 constructor_range_stack = p->constructor_range_stack;
7158 constructor_elements = p->elements;
7159 spelling = p->spelling;
7160 spelling_base = p->spelling_base;
7161 spelling_size = p->spelling_size;
7162 constructor_top_level = p->top_level;
7163 initializer_stack = p->next;
7164 free (p);
b71c7f8a 7165}
400fbf9f 7166\f
3e4093b6
RS
7167/* Call here when we see the initializer is surrounded by braces.
7168 This is instead of a call to push_init_level;
7169 it is matched by a call to pop_init_level.
400fbf9f 7170
3e4093b6
RS
7171 TYPE is the type to initialize, for a constructor expression.
7172 For an initializer for a decl, TYPE is zero. */
400fbf9f 7173
3e4093b6
RS
7174void
7175really_start_incremental_init (tree type)
400fbf9f 7176{
5d038c4c 7177 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7178
3e4093b6
RS
7179 if (type == 0)
7180 type = TREE_TYPE (constructor_decl);
400fbf9f 7181
b6fc2cdb
PB
7182 if (TREE_CODE (type) == VECTOR_TYPE
7183 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7184 error ("opaque vector types cannot be initialized");
400fbf9f 7185
3e4093b6
RS
7186 p->type = constructor_type;
7187 p->fields = constructor_fields;
7188 p->index = constructor_index;
7189 p->max_index = constructor_max_index;
7190 p->unfilled_index = constructor_unfilled_index;
7191 p->unfilled_fields = constructor_unfilled_fields;
7192 p->bit_index = constructor_bit_index;
7193 p->elements = constructor_elements;
7194 p->constant = constructor_constant;
7195 p->simple = constructor_simple;
928c19bb 7196 p->nonconst = constructor_nonconst;
3e4093b6
RS
7197 p->erroneous = constructor_erroneous;
7198 p->pending_elts = constructor_pending_elts;
7199 p->depth = constructor_depth;
916c5919
JM
7200 p->replacement_value.value = 0;
7201 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7202 p->replacement_value.original_type = NULL;
3e4093b6
RS
7203 p->implicit = 0;
7204 p->range_stack = 0;
7205 p->outer = 0;
7206 p->incremental = constructor_incremental;
7207 p->designated = constructor_designated;
976d5a22 7208 p->designator_depth = designator_depth;
3e4093b6
RS
7209 p->next = 0;
7210 constructor_stack = p;
b13aca19 7211
3e4093b6
RS
7212 constructor_constant = 1;
7213 constructor_simple = 1;
928c19bb 7214 constructor_nonconst = 0;
3e4093b6 7215 constructor_depth = SPELLING_DEPTH ();
9771b263 7216 constructor_elements = NULL;
3e4093b6
RS
7217 constructor_pending_elts = 0;
7218 constructor_type = type;
7219 constructor_incremental = 1;
7220 constructor_designated = 0;
9bac5cbb 7221 constructor_zeroinit = 1;
3e4093b6 7222 designator_depth = 0;
b06df647 7223 designator_erroneous = 0;
400fbf9f 7224
3e4093b6
RS
7225 if (TREE_CODE (constructor_type) == RECORD_TYPE
7226 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7227 {
3e4093b6
RS
7228 constructor_fields = TYPE_FIELDS (constructor_type);
7229 /* Skip any nameless bit fields at the beginning. */
7230 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7231 && DECL_NAME (constructor_fields) == 0)
910ad8de 7232 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7233
3e4093b6
RS
7234 constructor_unfilled_fields = constructor_fields;
7235 constructor_bit_index = bitsize_zero_node;
400fbf9f 7236 }
3e4093b6
RS
7237 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7238 {
7239 if (TYPE_DOMAIN (constructor_type))
7240 {
7241 constructor_max_index
7242 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7243
3e4093b6
RS
7244 /* Detect non-empty initializations of zero-length arrays. */
7245 if (constructor_max_index == NULL_TREE
7246 && TYPE_SIZE (constructor_type))
9a9d280e 7247 constructor_max_index = integer_minus_one_node;
400fbf9f 7248
3e4093b6
RS
7249 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7250 to initialize VLAs will cause a proper error; avoid tree
7251 checking errors as well by setting a safe value. */
7252 if (constructor_max_index
7253 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7254 constructor_max_index = integer_minus_one_node;
59c83dbf 7255
3e4093b6
RS
7256 constructor_index
7257 = convert (bitsizetype,
7258 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7259 }
3e4093b6 7260 else
493179da
JM
7261 {
7262 constructor_index = bitsize_zero_node;
7263 constructor_max_index = NULL_TREE;
7264 }
59c83dbf 7265
3e4093b6
RS
7266 constructor_unfilled_index = constructor_index;
7267 }
7268 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7269 {
7270 /* Vectors are like simple fixed-size arrays. */
7271 constructor_max_index =
c62c040f 7272 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7273 constructor_index = bitsize_zero_node;
3e4093b6
RS
7274 constructor_unfilled_index = constructor_index;
7275 }
7276 else
7277 {
7278 /* Handle the case of int x = {5}; */
7279 constructor_fields = constructor_type;
7280 constructor_unfilled_fields = constructor_type;
7281 }
7282}
7283\f
7284/* Push down into a subobject, for initialization.
7285 If this is for an explicit set of braces, IMPLICIT is 0.
7286 If it is because the next element belongs at a lower level,
7287 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7288
3e4093b6 7289void
ea58ef42
MP
7290push_init_level (location_t loc, int implicit,
7291 struct obstack *braced_init_obstack)
3e4093b6
RS
7292{
7293 struct constructor_stack *p;
7294 tree value = NULL_TREE;
400fbf9f 7295
3e4093b6 7296 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7297 pop them now. If implicit == 1, this will have been done in
7298 process_init_element; do not repeat it here because in the case
7299 of excess initializers for an empty aggregate this leads to an
7300 infinite cycle of popping a level and immediately recreating
7301 it. */
7302 if (implicit != 1)
3e4093b6 7303 {
472d98b4
JM
7304 while (constructor_stack->implicit)
7305 {
7306 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7307 || TREE_CODE (constructor_type) == UNION_TYPE)
7308 && constructor_fields == 0)
34cf811f 7309 process_init_element (input_location,
ea58ef42 7310 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7311 true, braced_init_obstack);
472d98b4
JM
7312 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7313 && constructor_max_index
7314 && tree_int_cst_lt (constructor_max_index,
7315 constructor_index))
34cf811f 7316 process_init_element (input_location,
ea58ef42 7317 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7318 true, braced_init_obstack);
472d98b4
JM
7319 else
7320 break;
7321 }
3e4093b6 7322 }
400fbf9f 7323
3e4093b6
RS
7324 /* Unless this is an explicit brace, we need to preserve previous
7325 content if any. */
7326 if (implicit)
7327 {
7328 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7329 || TREE_CODE (constructor_type) == UNION_TYPE)
7330 && constructor_fields)
a1e3b3d9 7331 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7332 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7333 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7334 }
7335
5d038c4c 7336 p = XNEW (struct constructor_stack);
3e4093b6
RS
7337 p->type = constructor_type;
7338 p->fields = constructor_fields;
7339 p->index = constructor_index;
7340 p->max_index = constructor_max_index;
7341 p->unfilled_index = constructor_unfilled_index;
7342 p->unfilled_fields = constructor_unfilled_fields;
7343 p->bit_index = constructor_bit_index;
7344 p->elements = constructor_elements;
7345 p->constant = constructor_constant;
7346 p->simple = constructor_simple;
928c19bb 7347 p->nonconst = constructor_nonconst;
3e4093b6
RS
7348 p->erroneous = constructor_erroneous;
7349 p->pending_elts = constructor_pending_elts;
7350 p->depth = constructor_depth;
916c5919
JM
7351 p->replacement_value.value = 0;
7352 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7353 p->replacement_value.original_type = NULL;
3e4093b6
RS
7354 p->implicit = implicit;
7355 p->outer = 0;
7356 p->incremental = constructor_incremental;
7357 p->designated = constructor_designated;
976d5a22 7358 p->designator_depth = designator_depth;
3e4093b6
RS
7359 p->next = constructor_stack;
7360 p->range_stack = 0;
7361 constructor_stack = p;
400fbf9f 7362
3e4093b6
RS
7363 constructor_constant = 1;
7364 constructor_simple = 1;
928c19bb 7365 constructor_nonconst = 0;
3e4093b6 7366 constructor_depth = SPELLING_DEPTH ();
9771b263 7367 constructor_elements = NULL;
3e4093b6
RS
7368 constructor_incremental = 1;
7369 constructor_designated = 0;
7370 constructor_pending_elts = 0;
7371 if (!implicit)
400fbf9f 7372 {
3e4093b6
RS
7373 p->range_stack = constructor_range_stack;
7374 constructor_range_stack = 0;
7375 designator_depth = 0;
b06df647 7376 designator_erroneous = 0;
3e4093b6 7377 }
400fbf9f 7378
3e4093b6
RS
7379 /* Don't die if an entire brace-pair level is superfluous
7380 in the containing level. */
7381 if (constructor_type == 0)
7382 ;
7383 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7384 || TREE_CODE (constructor_type) == UNION_TYPE)
7385 {
7386 /* Don't die if there are extra init elts at the end. */
7387 if (constructor_fields == 0)
7388 constructor_type = 0;
7389 else
400fbf9f 7390 {
3e4093b6
RS
7391 constructor_type = TREE_TYPE (constructor_fields);
7392 push_member_name (constructor_fields);
7393 constructor_depth++;
400fbf9f 7394 }
6a358dcb
MP
7395 /* If upper initializer is designated, then mark this as
7396 designated too to prevent bogus warnings. */
7397 constructor_designated = p->designated;
3e4093b6
RS
7398 }
7399 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7400 {
7401 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7402 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7403 constructor_depth++;
400fbf9f
JW
7404 }
7405
3e4093b6 7406 if (constructor_type == 0)
400fbf9f 7407 {
ea58ef42 7408 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7409 constructor_fields = 0;
7410 constructor_unfilled_fields = 0;
7411 return;
400fbf9f
JW
7412 }
7413
3e4093b6
RS
7414 if (value && TREE_CODE (value) == CONSTRUCTOR)
7415 {
7416 constructor_constant = TREE_CONSTANT (value);
7417 constructor_simple = TREE_STATIC (value);
928c19bb 7418 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7419 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7420 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7421 && (TREE_CODE (constructor_type) == RECORD_TYPE
7422 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7423 set_nonincremental_init (braced_init_obstack);
3e4093b6 7424 }
400fbf9f 7425
9bac5cbb
G
7426 if (implicit == 1)
7427 found_missing_braces = 1;
400fbf9f 7428
3e4093b6
RS
7429 if (TREE_CODE (constructor_type) == RECORD_TYPE
7430 || TREE_CODE (constructor_type) == UNION_TYPE)
7431 {
7432 constructor_fields = TYPE_FIELDS (constructor_type);
7433 /* Skip any nameless bit fields at the beginning. */
7434 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7435 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7436 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7437
3e4093b6
RS
7438 constructor_unfilled_fields = constructor_fields;
7439 constructor_bit_index = bitsize_zero_node;
7440 }
7441 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7442 {
7443 /* Vectors are like simple fixed-size arrays. */
7444 constructor_max_index =
c62c040f
RG
7445 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7446 constructor_index = bitsize_int (0);
3e4093b6
RS
7447 constructor_unfilled_index = constructor_index;
7448 }
7449 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7450 {
7451 if (TYPE_DOMAIN (constructor_type))
7452 {
7453 constructor_max_index
7454 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7455
3e4093b6
RS
7456 /* Detect non-empty initializations of zero-length arrays. */
7457 if (constructor_max_index == NULL_TREE
7458 && TYPE_SIZE (constructor_type))
9a9d280e 7459 constructor_max_index = integer_minus_one_node;
de520661 7460
3e4093b6
RS
7461 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7462 to initialize VLAs will cause a proper error; avoid tree
7463 checking errors as well by setting a safe value. */
7464 if (constructor_max_index
7465 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7466 constructor_max_index = integer_minus_one_node;
b62acd60 7467
3e4093b6
RS
7468 constructor_index
7469 = convert (bitsizetype,
7470 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7471 }
7472 else
7473 constructor_index = bitsize_zero_node;
de520661 7474
3e4093b6
RS
7475 constructor_unfilled_index = constructor_index;
7476 if (value && TREE_CODE (value) == STRING_CST)
7477 {
7478 /* We need to split the char/wchar array into individual
7479 characters, so that we don't have to special case it
7480 everywhere. */
a1e3b3d9 7481 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7482 }
7483 }
7484 else
7485 {
b4519d39 7486 if (constructor_type != error_mark_node)
96b40f8d 7487 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7488 constructor_fields = constructor_type;
7489 constructor_unfilled_fields = constructor_type;
7490 }
7491}
8b6a5902 7492
3e4093b6 7493/* At the end of an implicit or explicit brace level,
916c5919
JM
7494 finish up that level of constructor. If a single expression
7495 with redundant braces initialized that level, return the
7496 c_expr structure for that expression. Otherwise, the original_code
7497 element is set to ERROR_MARK.
7498 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7499 from inner levels (process_init_element ignores that),
916c5919 7500 but return error_mark_node as the value from the outermost level
3e4093b6 7501 (that's what we want to put in DECL_INITIAL).
916c5919 7502 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7503
916c5919 7504struct c_expr
ea58ef42
MP
7505pop_init_level (location_t loc, int implicit,
7506 struct obstack *braced_init_obstack)
3e4093b6
RS
7507{
7508 struct constructor_stack *p;
916c5919
JM
7509 struct c_expr ret;
7510 ret.value = 0;
7511 ret.original_code = ERROR_MARK;
6866c6e8 7512 ret.original_type = NULL;
de520661 7513
3e4093b6
RS
7514 if (implicit == 0)
7515 {
7516 /* When we come to an explicit close brace,
7517 pop any inner levels that didn't have explicit braces. */
7518 while (constructor_stack->implicit)
34cf811f 7519 process_init_element (input_location,
ea58ef42 7520 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7521 true, braced_init_obstack);
366de0ce 7522 gcc_assert (!constructor_range_stack);
3e4093b6 7523 }
e5e809f4 7524
0066ef9c
RH
7525 /* Now output all pending elements. */
7526 constructor_incremental = 1;
a1e3b3d9 7527 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7528
3e4093b6 7529 p = constructor_stack;
e5e809f4 7530
3e4093b6
RS
7531 /* Error for initializing a flexible array member, or a zero-length
7532 array member in an inappropriate context. */
7533 if (constructor_type && constructor_fields
7534 && TREE_CODE (constructor_type) == ARRAY_TYPE
7535 && TYPE_DOMAIN (constructor_type)
3f75a254 7536 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7537 {
7538 /* Silently discard empty initializations. The parser will
7539 already have pedwarned for empty brackets. */
7540 if (integer_zerop (constructor_unfilled_index))
7541 constructor_type = NULL_TREE;
366de0ce 7542 else
3e4093b6 7543 {
366de0ce 7544 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7545
3e4093b6 7546 if (constructor_depth > 2)
ea58ef42 7547 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7548 else
d033409e 7549 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7550 "initialization of a flexible array member");
de520661 7551
3e4093b6
RS
7552 /* We have already issued an error message for the existence
7553 of a flexible array member not at the end of the structure.
535a42b1 7554 Discard the initializer so that we do not die later. */
910ad8de 7555 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7556 constructor_type = NULL_TREE;
7557 }
3e4093b6 7558 }
de520661 7559
7b33f0c8
MP
7560 switch (vec_safe_length (constructor_elements))
7561 {
7562 case 0:
7563 /* Initialization with { } counts as zeroinit. */
7564 constructor_zeroinit = 1;
7565 break;
7566 case 1:
7567 /* This might be zeroinit as well. */
7568 if (integer_zerop ((*constructor_elements)[0].value))
7569 constructor_zeroinit = 1;
7570 break;
7571 default:
7572 /* If the constructor has more than one element, it can't be { 0 }. */
7573 constructor_zeroinit = 0;
7574 break;
7575 }
9bac5cbb
G
7576
7577 /* Warn when some structs are initialized with direct aggregation. */
7578 if (!implicit && found_missing_braces && warn_missing_braces
7579 && !constructor_zeroinit)
7b33f0c8
MP
7580 warning_init (loc, OPT_Wmissing_braces,
7581 "missing braces around initializer");
9bac5cbb 7582
3e4093b6 7583 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7584 if (warn_missing_field_initializers
3e4093b6
RS
7585 && constructor_type
7586 && TREE_CODE (constructor_type) == RECORD_TYPE
7587 && constructor_unfilled_fields)
7588 {
7589 /* Do not warn for flexible array members or zero-length arrays. */
7590 while (constructor_unfilled_fields
3f75a254 7591 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7592 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7593 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7594
49819fef
AM
7595 if (constructor_unfilled_fields
7596 /* Do not warn if this level of the initializer uses member
7597 designators; it is likely to be deliberate. */
7598 && !constructor_designated
84937de2 7599 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7600 && !constructor_zeroinit)
3e4093b6 7601 {
32397f22
MLI
7602 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7603 "missing initializer for field %qD of %qT",
7604 constructor_unfilled_fields,
7605 constructor_type))
7606 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7607 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7608 }
7609 }
de520661 7610
3e4093b6 7611 /* Pad out the end of the structure. */
916c5919 7612 if (p->replacement_value.value)
3e4093b6
RS
7613 /* If this closes a superfluous brace pair,
7614 just pass out the element between them. */
916c5919 7615 ret = p->replacement_value;
3e4093b6
RS
7616 else if (constructor_type == 0)
7617 ;
7618 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7619 && TREE_CODE (constructor_type) != UNION_TYPE
7620 && TREE_CODE (constructor_type) != ARRAY_TYPE
7621 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7622 {
7623 /* A nonincremental scalar initializer--just return
7624 the element, after verifying there is just one. */
9771b263 7625 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7626 {
7627 if (!constructor_erroneous)
ea58ef42 7628 error_init (loc, "empty scalar initializer");
916c5919 7629 ret.value = error_mark_node;
3e4093b6 7630 }
9771b263 7631 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7632 {
ea58ef42 7633 error_init (loc, "extra elements in scalar initializer");
9771b263 7634 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7635 }
7636 else
9771b263 7637 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7638 }
7639 else
7640 {
7641 if (constructor_erroneous)
916c5919 7642 ret.value = error_mark_node;
3e4093b6
RS
7643 else
7644 {
916c5919 7645 ret.value = build_constructor (constructor_type,
4038c495 7646 constructor_elements);
3e4093b6 7647 if (constructor_constant)
51eed280 7648 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7649 if (constructor_constant && constructor_simple)
916c5919 7650 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7651 if (constructor_nonconst)
7652 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7653 }
7654 }
de520661 7655
928c19bb
JM
7656 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7657 {
7658 if (constructor_nonconst)
7659 ret.original_code = C_MAYBE_CONST_EXPR;
7660 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7661 ret.original_code = ERROR_MARK;
7662 }
7663
3e4093b6
RS
7664 constructor_type = p->type;
7665 constructor_fields = p->fields;
7666 constructor_index = p->index;
7667 constructor_max_index = p->max_index;
7668 constructor_unfilled_index = p->unfilled_index;
7669 constructor_unfilled_fields = p->unfilled_fields;
7670 constructor_bit_index = p->bit_index;
7671 constructor_elements = p->elements;
7672 constructor_constant = p->constant;
7673 constructor_simple = p->simple;
928c19bb 7674 constructor_nonconst = p->nonconst;
3e4093b6
RS
7675 constructor_erroneous = p->erroneous;
7676 constructor_incremental = p->incremental;
7677 constructor_designated = p->designated;
976d5a22 7678 designator_depth = p->designator_depth;
3e4093b6
RS
7679 constructor_pending_elts = p->pending_elts;
7680 constructor_depth = p->depth;
7681 if (!p->implicit)
7682 constructor_range_stack = p->range_stack;
7683 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7684
3e4093b6
RS
7685 constructor_stack = p->next;
7686 free (p);
b621a4dd 7687
5d5e98dc
VR
7688 if (ret.value == 0 && constructor_stack == 0)
7689 ret.value = error_mark_node;
916c5919 7690 return ret;
3e4093b6 7691}
8b6a5902 7692
3e4093b6
RS
7693/* Common handling for both array range and field name designators.
7694 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7695
3e4093b6 7696static int
ea58ef42
MP
7697set_designator (location_t loc, int array,
7698 struct obstack *braced_init_obstack)
de520661 7699{
3e4093b6
RS
7700 tree subtype;
7701 enum tree_code subcode;
de520661 7702
3e4093b6
RS
7703 /* Don't die if an entire brace-pair level is superfluous
7704 in the containing level. */
7705 if (constructor_type == 0)
7706 return 1;
de520661 7707
366de0ce
NS
7708 /* If there were errors in this designator list already, bail out
7709 silently. */
b06df647 7710 if (designator_erroneous)
3e4093b6 7711 return 1;
e28cae4f 7712
3e4093b6
RS
7713 if (!designator_depth)
7714 {
366de0ce 7715 gcc_assert (!constructor_range_stack);
de520661 7716
3e4093b6
RS
7717 /* Designator list starts at the level of closest explicit
7718 braces. */
7719 while (constructor_stack->implicit)
34cf811f 7720 process_init_element (input_location,
ea58ef42 7721 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7722 true, braced_init_obstack);
3e4093b6
RS
7723 constructor_designated = 1;
7724 return 0;
7725 }
de520661 7726
366de0ce 7727 switch (TREE_CODE (constructor_type))
3c3fa147 7728 {
366de0ce
NS
7729 case RECORD_TYPE:
7730 case UNION_TYPE:
3e4093b6
RS
7731 subtype = TREE_TYPE (constructor_fields);
7732 if (subtype != error_mark_node)
7733 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7734 break;
7735 case ARRAY_TYPE:
3e4093b6 7736 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7737 break;
7738 default:
7739 gcc_unreachable ();
de520661 7740 }
400fbf9f 7741
3e4093b6
RS
7742 subcode = TREE_CODE (subtype);
7743 if (array && subcode != ARRAY_TYPE)
7744 {
ea58ef42 7745 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
7746 return 1;
7747 }
7748 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7749 {
ea58ef42 7750 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
7751 return 1;
7752 }
d45cf215 7753
3e4093b6 7754 constructor_designated = 1;
ea58ef42 7755 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 7756 return 0;
de520661 7757}
400fbf9f 7758
3e4093b6
RS
7759/* If there are range designators in designator list, push a new designator
7760 to constructor_range_stack. RANGE_END is end of such stack range or
7761 NULL_TREE if there is no range designator at this level. */
400fbf9f 7762
3e4093b6 7763static void
a1e3b3d9 7764push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7765{
7766 struct constructor_range_stack *p;
400fbf9f 7767
a1e3b3d9
LB
7768 p = (struct constructor_range_stack *)
7769 obstack_alloc (braced_init_obstack,
7770 sizeof (struct constructor_range_stack));
3e4093b6
RS
7771 p->prev = constructor_range_stack;
7772 p->next = 0;
7773 p->fields = constructor_fields;
7774 p->range_start = constructor_index;
7775 p->index = constructor_index;
7776 p->stack = constructor_stack;
7777 p->range_end = range_end;
8b6a5902 7778 if (constructor_range_stack)
3e4093b6
RS
7779 constructor_range_stack->next = p;
7780 constructor_range_stack = p;
de520661 7781}
400fbf9f 7782
3e4093b6
RS
7783/* Within an array initializer, specify the next index to be initialized.
7784 FIRST is that index. If LAST is nonzero, then initialize a range
7785 of indices, running from FIRST through LAST. */
5a7ec9d9 7786
de520661 7787void
ea58ef42 7788set_init_index (location_t loc, tree first, tree last,
d033409e 7789 struct obstack *braced_init_obstack)
de520661 7790{
ea58ef42 7791 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 7792 return;
de520661 7793
b06df647 7794 designator_erroneous = 1;
de520661 7795
3ea8cd06
JM
7796 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7797 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7798 {
ea58ef42 7799 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
7800 return;
7801 }
7802
2b6da65c
JM
7803 if (TREE_CODE (first) != INTEGER_CST)
7804 {
7805 first = c_fully_fold (first, false, NULL);
7806 if (TREE_CODE (first) == INTEGER_CST)
d033409e 7807 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7808 "array index in initializer is not "
7809 "an integer constant expression");
7810 }
7811
7812 if (last && TREE_CODE (last) != INTEGER_CST)
7813 {
7814 last = c_fully_fold (last, false, NULL);
7815 if (TREE_CODE (last) == INTEGER_CST)
d033409e 7816 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7817 "array index in initializer is not "
7818 "an integer constant expression");
7819 }
7820
3e4093b6 7821 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 7822 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7823 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 7824 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7825 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 7826 error_init (loc, "array index in non-array initializer");
622adc7e 7827 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 7828 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
7829 else if (constructor_max_index
7830 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 7831 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 7832 else
de520661 7833 {
928c19bb
JM
7834 constant_expression_warning (first);
7835 if (last)
7836 constant_expression_warning (last);
3e4093b6 7837 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7838 if (tree_int_cst_lt (constructor_index, first))
7839 {
7840 constructor_index = copy_node (constructor_index);
7841 TREE_OVERFLOW (constructor_index) = 1;
7842 }
665f2503 7843
3e4093b6 7844 if (last)
2bede729 7845 {
3e4093b6
RS
7846 if (tree_int_cst_equal (first, last))
7847 last = 0;
7848 else if (tree_int_cst_lt (last, first))
7849 {
ea58ef42 7850 error_init (loc, "empty index range in initializer");
3e4093b6
RS
7851 last = 0;
7852 }
7853 else
7854 {
7855 last = convert (bitsizetype, last);
7856 if (constructor_max_index != 0
7857 && tree_int_cst_lt (constructor_max_index, last))
7858 {
ea58ef42
MP
7859 error_init (loc, "array index range in initializer exceeds "
7860 "array bounds");
3e4093b6
RS
7861 last = 0;
7862 }
7863 }
2bede729 7864 }
fed3cef0 7865
3e4093b6 7866 designator_depth++;
b06df647 7867 designator_erroneous = 0;
3e4093b6 7868 if (constructor_range_stack || last)
a1e3b3d9 7869 push_range_stack (last, braced_init_obstack);
de520661 7870 }
de520661 7871}
3e4093b6
RS
7872
7873/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7874
de520661 7875void
ea58ef42
MP
7876set_init_label (location_t loc, tree fieldname,
7877 struct obstack *braced_init_obstack)
de520661 7878{
0fb96aa4 7879 tree field;
94ba5069 7880
ea58ef42 7881 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
7882 return;
7883
b06df647 7884 designator_erroneous = 1;
3e4093b6
RS
7885
7886 if (TREE_CODE (constructor_type) != RECORD_TYPE
7887 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7888 {
ea58ef42 7889 error_init (loc, "field name not in record or union initializer");
3e4093b6 7890 return;
94ba5069
RS
7891 }
7892
0fb96aa4 7893 field = lookup_field (constructor_type, fieldname);
8b6a5902 7894
0fb96aa4 7895 if (field == 0)
c51a1ba9 7896 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 7897 else
0fb96aa4
JM
7898 do
7899 {
7900 constructor_fields = TREE_VALUE (field);
7901 designator_depth++;
7902 designator_erroneous = 0;
7903 if (constructor_range_stack)
7904 push_range_stack (NULL_TREE, braced_init_obstack);
7905 field = TREE_CHAIN (field);
7906 if (field)
7907 {
ea58ef42 7908 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
7909 return;
7910 }
7911 }
7912 while (field != NULL_TREE);
3e4093b6
RS
7913}
7914\f
7915/* Add a new initializer to the tree of pending initializers. PURPOSE
7916 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7917 VALUE is the value of that index or field. If ORIGTYPE is not
7918 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7919
7920 IMPLICIT is true if value comes from pop_init_level (1),
7921 the new initializer has been merged with the existing one
7922 and thus no warnings should be emitted about overriding an
7923 existing initializer. */
de520661 7924
3e4093b6 7925static void
96b40f8d
MP
7926add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7927 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
7928{
7929 struct init_node *p, **q, *r;
7930
7931 q = &constructor_pending_elts;
7932 p = 0;
7933
7934 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7935 {
3e4093b6 7936 while (*q != 0)
91fa3c30 7937 {
3e4093b6
RS
7938 p = *q;
7939 if (tree_int_cst_lt (purpose, p->purpose))
7940 q = &p->left;
7941 else if (tree_int_cst_lt (p->purpose, purpose))
7942 q = &p->right;
7943 else
7944 {
b295aee2
JJ
7945 if (!implicit)
7946 {
7947 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
7948 warning_init (loc, 0,
7949 "initialized field with side-effects "
7950 "overwritten");
b295aee2 7951 else if (warn_override_init)
96b40f8d
MP
7952 warning_init (loc, OPT_Woverride_init,
7953 "initialized field overwritten");
b295aee2 7954 }
3e4093b6 7955 p->value = value;
bbbbb16a 7956 p->origtype = origtype;
3e4093b6
RS
7957 return;
7958 }
91fa3c30 7959 }
de520661 7960 }
3e4093b6 7961 else
de520661 7962 {
3e4093b6 7963 tree bitpos;
400fbf9f 7964
3e4093b6
RS
7965 bitpos = bit_position (purpose);
7966 while (*q != NULL)
7967 {
7968 p = *q;
7969 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7970 q = &p->left;
7971 else if (p->purpose != purpose)
7972 q = &p->right;
7973 else
7974 {
b295aee2
JJ
7975 if (!implicit)
7976 {
7977 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
7978 warning_init (loc, 0,
7979 "initialized field with side-effects "
7980 "overwritten");
b295aee2 7981 else if (warn_override_init)
96b40f8d
MP
7982 warning_init (loc, OPT_Woverride_init,
7983 "initialized field overwritten");
b295aee2 7984 }
3e4093b6 7985 p->value = value;
bbbbb16a 7986 p->origtype = origtype;
3e4093b6
RS
7987 return;
7988 }
7989 }
91fa3c30 7990 }
b71c7f8a 7991
a1e3b3d9
LB
7992 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7993 sizeof (struct init_node));
3e4093b6
RS
7994 r->purpose = purpose;
7995 r->value = value;
bbbbb16a 7996 r->origtype = origtype;
8b6a5902 7997
3e4093b6
RS
7998 *q = r;
7999 r->parent = p;
8000 r->left = 0;
8001 r->right = 0;
8002 r->balance = 0;
b71c7f8a 8003
3e4093b6 8004 while (p)
de520661 8005 {
3e4093b6 8006 struct init_node *s;
665f2503 8007
3e4093b6 8008 if (r == p->left)
2bede729 8009 {
3e4093b6
RS
8010 if (p->balance == 0)
8011 p->balance = -1;
8012 else if (p->balance < 0)
8013 {
8014 if (r->balance < 0)
8015 {
8016 /* L rotation. */
8017 p->left = r->right;
8018 if (p->left)
8019 p->left->parent = p;
8020 r->right = p;
e7b6a0ee 8021
3e4093b6
RS
8022 p->balance = 0;
8023 r->balance = 0;
39bc99c2 8024
3e4093b6
RS
8025 s = p->parent;
8026 p->parent = r;
8027 r->parent = s;
8028 if (s)
8029 {
8030 if (s->left == p)
8031 s->left = r;
8032 else
8033 s->right = r;
8034 }
8035 else
8036 constructor_pending_elts = r;
8037 }
8038 else
8039 {
8040 /* LR rotation. */
8041 struct init_node *t = r->right;
e7b6a0ee 8042
3e4093b6
RS
8043 r->right = t->left;
8044 if (r->right)
8045 r->right->parent = r;
8046 t->left = r;
8047
8048 p->left = t->right;
8049 if (p->left)
8050 p->left->parent = p;
8051 t->right = p;
8052
8053 p->balance = t->balance < 0;
8054 r->balance = -(t->balance > 0);
8055 t->balance = 0;
8056
8057 s = p->parent;
8058 p->parent = t;
8059 r->parent = t;
8060 t->parent = s;
8061 if (s)
8062 {
8063 if (s->left == p)
8064 s->left = t;
8065 else
8066 s->right = t;
8067 }
8068 else
8069 constructor_pending_elts = t;
8070 }
8071 break;
8072 }
8073 else
8074 {
8075 /* p->balance == +1; growth of left side balances the node. */
8076 p->balance = 0;
8077 break;
8078 }
2bede729 8079 }
3e4093b6
RS
8080 else /* r == p->right */
8081 {
8082 if (p->balance == 0)
8083 /* Growth propagation from right side. */
8084 p->balance++;
8085 else if (p->balance > 0)
8086 {
8087 if (r->balance > 0)
8088 {
8089 /* R rotation. */
8090 p->right = r->left;
8091 if (p->right)
8092 p->right->parent = p;
8093 r->left = p;
8094
8095 p->balance = 0;
8096 r->balance = 0;
8097
8098 s = p->parent;
8099 p->parent = r;
8100 r->parent = s;
8101 if (s)
8102 {
8103 if (s->left == p)
8104 s->left = r;
8105 else
8106 s->right = r;
8107 }
8108 else
8109 constructor_pending_elts = r;
8110 }
8111 else /* r->balance == -1 */
8112 {
8113 /* RL rotation */
8114 struct init_node *t = r->left;
8115
8116 r->left = t->right;
8117 if (r->left)
8118 r->left->parent = r;
8119 t->right = r;
8120
8121 p->right = t->left;
8122 if (p->right)
8123 p->right->parent = p;
8124 t->left = p;
8125
8126 r->balance = (t->balance < 0);
8127 p->balance = -(t->balance > 0);
8128 t->balance = 0;
8129
8130 s = p->parent;
8131 p->parent = t;
8132 r->parent = t;
8133 t->parent = s;
8134 if (s)
8135 {
8136 if (s->left == p)
8137 s->left = t;
8138 else
8139 s->right = t;
8140 }
8141 else
8142 constructor_pending_elts = t;
8143 }
8144 break;
8145 }
8146 else
8147 {
8148 /* p->balance == -1; growth of right side balances the node. */
8149 p->balance = 0;
8150 break;
8151 }
8152 }
8153
8154 r = p;
8155 p = p->parent;
8156 }
8157}
8158
8159/* Build AVL tree from a sorted chain. */
8160
8161static void
a1e3b3d9 8162set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8163{
4038c495
GB
8164 unsigned HOST_WIDE_INT ix;
8165 tree index, value;
3e4093b6
RS
8166
8167 if (TREE_CODE (constructor_type) != RECORD_TYPE
8168 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8169 return;
8170
4038c495 8171 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8172 add_pending_init (input_location, index, value, NULL_TREE, true,
8173 braced_init_obstack);
9771b263 8174 constructor_elements = NULL;
3e4093b6
RS
8175 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8176 {
8177 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8178 /* Skip any nameless bit fields at the beginning. */
8179 while (constructor_unfilled_fields != 0
8180 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8181 && DECL_NAME (constructor_unfilled_fields) == 0)
8182 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8183
de520661 8184 }
3e4093b6 8185 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8186 {
3e4093b6
RS
8187 if (TYPE_DOMAIN (constructor_type))
8188 constructor_unfilled_index
8189 = convert (bitsizetype,
8190 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8191 else
8192 constructor_unfilled_index = bitsize_zero_node;
de520661 8193 }
3e4093b6 8194 constructor_incremental = 0;
de520661 8195}
400fbf9f 8196
3e4093b6 8197/* Build AVL tree from a string constant. */
de520661 8198
3e4093b6 8199static void
a1e3b3d9
LB
8200set_nonincremental_init_from_string (tree str,
8201 struct obstack * braced_init_obstack)
de520661 8202{
3e4093b6
RS
8203 tree value, purpose, type;
8204 HOST_WIDE_INT val[2];
8205 const char *p, *end;
8206 int byte, wchar_bytes, charwidth, bitpos;
de520661 8207
366de0ce 8208 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8209
c466b2cd 8210 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8211 charwidth = TYPE_PRECISION (char_type_node);
8212 type = TREE_TYPE (constructor_type);
8213 p = TREE_STRING_POINTER (str);
8214 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8215
3e4093b6 8216 for (purpose = bitsize_zero_node;
8824edff
JJ
8217 p < end
8218 && !(constructor_max_index
8219 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8220 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8221 {
3e4093b6 8222 if (wchar_bytes == 1)
ffc5c6a9 8223 {
807e902e
KZ
8224 val[0] = (unsigned char) *p++;
8225 val[1] = 0;
ffc5c6a9
RH
8226 }
8227 else
3e4093b6 8228 {
3e4093b6 8229 val[1] = 0;
807e902e 8230 val[0] = 0;
3e4093b6
RS
8231 for (byte = 0; byte < wchar_bytes; byte++)
8232 {
8233 if (BYTES_BIG_ENDIAN)
8234 bitpos = (wchar_bytes - byte - 1) * charwidth;
8235 else
8236 bitpos = byte * charwidth;
807e902e 8237 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8238 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8239 << (bitpos % HOST_BITS_PER_WIDE_INT);
8240 }
8241 }
584ef5fe 8242
8df83eae 8243 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8244 {
8245 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8246 if (bitpos < HOST_BITS_PER_WIDE_INT)
8247 {
807e902e 8248 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8249 {
807e902e
KZ
8250 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8251 val[1] = -1;
3e4093b6
RS
8252 }
8253 }
8254 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8255 {
807e902e
KZ
8256 if (val[0] < 0)
8257 val[1] = -1;
3e4093b6 8258 }
807e902e 8259 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8260 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
807e902e 8261 val[1] |= ((HOST_WIDE_INT) -1)
3e4093b6
RS
8262 << (bitpos - HOST_BITS_PER_WIDE_INT);
8263 }
ffc5c6a9 8264
807e902e
KZ
8265 value = wide_int_to_tree (type,
8266 wide_int::from_array (val, 2,
8267 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8268 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8269 braced_init_obstack);
9dfcc8db
BH
8270 }
8271
3e4093b6
RS
8272 constructor_incremental = 0;
8273}
de520661 8274
3e4093b6
RS
8275/* Return value of FIELD in pending initializer or zero if the field was
8276 not initialized yet. */
8277
8278static tree
a1e3b3d9 8279find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8280{
8281 struct init_node *p;
8282
8283 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8284 {
3e4093b6
RS
8285 if (constructor_incremental
8286 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8287 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8288
8289 p = constructor_pending_elts;
8290 while (p)
19d76e60 8291 {
3e4093b6
RS
8292 if (tree_int_cst_lt (field, p->purpose))
8293 p = p->left;
8294 else if (tree_int_cst_lt (p->purpose, field))
8295 p = p->right;
8296 else
8297 return p->value;
19d76e60 8298 }
19d76e60 8299 }
3e4093b6 8300 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8301 {
3e4093b6 8302 tree bitpos = bit_position (field);
de520661 8303
3e4093b6
RS
8304 if (constructor_incremental
8305 && (!constructor_unfilled_fields
8306 || tree_int_cst_lt (bitpos,
8307 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8308 set_nonincremental_init (braced_init_obstack);
de520661 8309
3e4093b6
RS
8310 p = constructor_pending_elts;
8311 while (p)
8312 {
8313 if (field == p->purpose)
8314 return p->value;
8315 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8316 p = p->left;
8317 else
8318 p = p->right;
8319 }
8320 }
8321 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8322 {
9771b263
DN
8323 if (!vec_safe_is_empty (constructor_elements)
8324 && (constructor_elements->last ().index == field))
8325 return constructor_elements->last ().value;
de520661 8326 }
3e4093b6 8327 return 0;
de520661
RS
8328}
8329
3e4093b6
RS
8330/* "Output" the next constructor element.
8331 At top level, really output it to assembler code now.
8332 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8333 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8334 TYPE is the data type that the containing data type wants here.
8335 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8336 If VALUE is a string constant, STRICT_STRING is true if it is
8337 unparenthesized or we should not warn here for it being parenthesized.
8338 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8339
3e4093b6
RS
8340 PENDING if non-nil means output pending elements that belong
8341 right after this element. (PENDING is normally 1;
b295aee2
JJ
8342 it is 0 while outputting pending elements, to avoid recursion.)
8343
8344 IMPLICIT is true if value comes from pop_init_level (1),
8345 the new initializer has been merged with the existing one
8346 and thus no warnings should be emitted about overriding an
8347 existing initializer. */
8b6a5902 8348
3e4093b6 8349static void
34cf811f
MP
8350output_init_element (location_t loc, tree value, tree origtype,
8351 bool strict_string, tree type, tree field, int pending,
8352 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8353{
8ce94e44 8354 tree semantic_type = NULL_TREE;
928c19bb
JM
8355 bool maybe_const = true;
8356 bool npc;
4038c495 8357
0a880880 8358 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8359 {
3e4093b6
RS
8360 constructor_erroneous = 1;
8361 return;
8b6a5902 8362 }
46bdb9cf
JM
8363 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8364 && (TREE_CODE (value) == STRING_CST
8365 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8366 && !(TREE_CODE (value) == STRING_CST
8367 && TREE_CODE (type) == ARRAY_TYPE
8368 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8369 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8370 TYPE_MAIN_VARIANT (type)))
c2255bc4 8371 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8372
3e4093b6 8373 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8374 && require_constant_value && pending)
8b6a5902 8375 {
3e4093b6
RS
8376 /* As an extension, allow initializing objects with static storage
8377 duration with compound literals (which are then treated just as
8378 the brace enclosed list they contain). */
4435bb92
MP
8379 if (flag_isoc99)
8380 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8381 "constant");
3e4093b6
RS
8382 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8383 value = DECL_INITIAL (decl);
8b6a5902
JJ
8384 }
8385
928c19bb 8386 npc = null_pointer_constant_p (value);
8ce94e44
JM
8387 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8388 {
8389 semantic_type = TREE_TYPE (value);
8390 value = TREE_OPERAND (value, 0);
8391 }
928c19bb
JM
8392 value = c_fully_fold (value, require_constant_value, &maybe_const);
8393
3e4093b6
RS
8394 if (value == error_mark_node)
8395 constructor_erroneous = 1;
8396 else if (!TREE_CONSTANT (value))
8397 constructor_constant = 0;
116df786 8398 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8399 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8400 || TREE_CODE (constructor_type) == UNION_TYPE)
8401 && DECL_C_BIT_FIELD (field)
8402 && TREE_CODE (value) != INTEGER_CST))
8403 constructor_simple = 0;
928c19bb
JM
8404 if (!maybe_const)
8405 constructor_nonconst = 1;
3e4093b6 8406
116df786 8407 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8408 {
116df786
RH
8409 if (require_constant_value)
8410 {
ea58ef42 8411 error_init (loc, "initializer element is not constant");
116df786
RH
8412 value = error_mark_node;
8413 }
8414 else if (require_constant_elements)
8337d1db 8415 pedwarn (loc, OPT_Wpedantic,
509c9d60 8416 "initializer element is not computable at load time");
8b6a5902 8417 }
928c19bb
JM
8418 else if (!maybe_const
8419 && (require_constant_value || require_constant_elements))
8337d1db 8420 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8421 "initializer element is not a constant expression");
3e4093b6 8422
81f40b79
ILT
8423 /* Issue -Wc++-compat warnings about initializing a bitfield with
8424 enum type. */
8425 if (warn_cxx_compat
8426 && field != NULL_TREE
8427 && TREE_CODE (field) == FIELD_DECL
8428 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8429 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8430 != TYPE_MAIN_VARIANT (type))
8431 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8432 {
8433 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8434 if (checktype != error_mark_node
8435 && (TYPE_MAIN_VARIANT (checktype)
8436 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8437 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8438 "enum conversion in initialization is invalid in C++");
8439 }
8440
3e4093b6
RS
8441 /* If this field is empty (and not at the end of structure),
8442 don't do anything other than checking the initializer. */
8443 if (field
8444 && (TREE_TYPE (field) == error_mark_node
8445 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8446 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8447 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8448 || DECL_CHAIN (field)))))
3e4093b6
RS
8449 return;
8450
8ce94e44
JM
8451 if (semantic_type)
8452 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8453 value = digest_init (loc, type, value, origtype, npc, strict_string,
8454 require_constant_value);
3e4093b6 8455 if (value == error_mark_node)
8b6a5902 8456 {
3e4093b6
RS
8457 constructor_erroneous = 1;
8458 return;
8b6a5902 8459 }
928c19bb
JM
8460 if (require_constant_value || require_constant_elements)
8461 constant_expression_warning (value);
8b6a5902 8462
3e4093b6
RS
8463 /* If this element doesn't come next in sequence,
8464 put it on constructor_pending_elts. */
8465 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8466 && (!constructor_incremental
8467 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8468 {
3e4093b6
RS
8469 if (constructor_incremental
8470 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8471 set_nonincremental_init (braced_init_obstack);
3e4093b6 8472
96b40f8d 8473 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8474 braced_init_obstack);
3e4093b6 8475 return;
8b6a5902 8476 }
3e4093b6
RS
8477 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8478 && (!constructor_incremental
8479 || field != constructor_unfilled_fields))
8b6a5902 8480 {
3e4093b6
RS
8481 /* We do this for records but not for unions. In a union,
8482 no matter which field is specified, it can be initialized
8483 right away since it starts at the beginning of the union. */
8484 if (constructor_incremental)
8485 {
8486 if (!constructor_unfilled_fields)
a1e3b3d9 8487 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8488 else
8489 {
8490 tree bitpos, unfillpos;
8491
8492 bitpos = bit_position (field);
8493 unfillpos = bit_position (constructor_unfilled_fields);
8494
8495 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8496 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8497 }
8498 }
8499
96b40f8d 8500 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8501 braced_init_obstack);
3e4093b6 8502 return;
8b6a5902 8503 }
3e4093b6 8504 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8505 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8506 {
b295aee2
JJ
8507 if (!implicit)
8508 {
9771b263 8509 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
96b40f8d 8510 warning_init (loc, 0,
b295aee2
JJ
8511 "initialized field with side-effects overwritten");
8512 else if (warn_override_init)
96b40f8d
MP
8513 warning_init (loc, OPT_Woverride_init,
8514 "initialized field overwritten");
b295aee2 8515 }
8b6a5902 8516
3e4093b6 8517 /* We can have just one union field set. */
9771b263 8518 constructor_elements = NULL;
3e4093b6 8519 }
8b6a5902 8520
3e4093b6
RS
8521 /* Otherwise, output this element either to
8522 constructor_elements or to the assembler file. */
8b6a5902 8523
f32682ca 8524 constructor_elt celt = {field, value};
9771b263 8525 vec_safe_push (constructor_elements, celt);
8b6a5902 8526
3e4093b6
RS
8527 /* Advance the variable that indicates sequential elements output. */
8528 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8529 constructor_unfilled_index
db3927fb
AH
8530 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8531 bitsize_one_node);
3e4093b6
RS
8532 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8533 {
8534 constructor_unfilled_fields
910ad8de 8535 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8536
3e4093b6
RS
8537 /* Skip any nameless bit fields. */
8538 while (constructor_unfilled_fields != 0
8539 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8540 && DECL_NAME (constructor_unfilled_fields) == 0)
8541 constructor_unfilled_fields =
910ad8de 8542 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8543 }
8544 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8545 constructor_unfilled_fields = 0;
de520661 8546
3e4093b6
RS
8547 /* Now output any pending elements which have become next. */
8548 if (pending)
a1e3b3d9 8549 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8550}
8b6a5902 8551
3e4093b6
RS
8552/* Output any pending elements which have become next.
8553 As we output elements, constructor_unfilled_{fields,index}
8554 advances, which may cause other elements to become next;
8555 if so, they too are output.
8b6a5902 8556
3e4093b6
RS
8557 If ALL is 0, we return when there are
8558 no more pending elements to output now.
665f2503 8559
3e4093b6
RS
8560 If ALL is 1, we output space as necessary so that
8561 we can output all the pending elements. */
3e4093b6 8562static void
a1e3b3d9 8563output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8564{
8565 struct init_node *elt = constructor_pending_elts;
8566 tree next;
de520661 8567
3e4093b6
RS
8568 retry:
8569
ba228239 8570 /* Look through the whole pending tree.
3e4093b6
RS
8571 If we find an element that should be output now,
8572 output it. Otherwise, set NEXT to the element
8573 that comes first among those still pending. */
8574
8575 next = 0;
8576 while (elt)
8577 {
8578 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8579 {
3e4093b6
RS
8580 if (tree_int_cst_equal (elt->purpose,
8581 constructor_unfilled_index))
34cf811f
MP
8582 output_init_element (input_location, elt->value, elt->origtype,
8583 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8584 constructor_unfilled_index, 0, false,
8585 braced_init_obstack);
3e4093b6
RS
8586 else if (tree_int_cst_lt (constructor_unfilled_index,
8587 elt->purpose))
8b6a5902 8588 {
3e4093b6
RS
8589 /* Advance to the next smaller node. */
8590 if (elt->left)
8591 elt = elt->left;
8592 else
8593 {
8594 /* We have reached the smallest node bigger than the
8595 current unfilled index. Fill the space first. */
8596 next = elt->purpose;
8597 break;
8598 }
8b6a5902 8599 }
ce662d4c
JJ
8600 else
8601 {
3e4093b6
RS
8602 /* Advance to the next bigger node. */
8603 if (elt->right)
8604 elt = elt->right;
8605 else
ce662d4c 8606 {
3e4093b6
RS
8607 /* We have reached the biggest node in a subtree. Find
8608 the parent of it, which is the next bigger node. */
8609 while (elt->parent && elt->parent->right == elt)
8610 elt = elt->parent;
8611 elt = elt->parent;
8612 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8613 elt->purpose))
8614 {
8615 next = elt->purpose;
8616 break;
8617 }
ce662d4c
JJ
8618 }
8619 }
8b6a5902 8620 }
3e4093b6
RS
8621 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8622 || TREE_CODE (constructor_type) == UNION_TYPE)
8623 {
8624 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8625
3e4093b6
RS
8626 /* If the current record is complete we are done. */
8627 if (constructor_unfilled_fields == 0)
8628 break;
de520661 8629
3e4093b6
RS
8630 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8631 elt_bitpos = bit_position (elt->purpose);
8632 /* We can't compare fields here because there might be empty
8633 fields in between. */
8634 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8635 {
8636 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8637 output_init_element (input_location, elt->value, elt->origtype,
8638 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8639 elt->purpose, 0, false,
8640 braced_init_obstack);
3e4093b6
RS
8641 }
8642 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8643 {
8644 /* Advance to the next smaller node. */
8645 if (elt->left)
8646 elt = elt->left;
8647 else
8648 {
8649 /* We have reached the smallest node bigger than the
8650 current unfilled field. Fill the space first. */
8651 next = elt->purpose;
8652 break;
8653 }
8654 }
8655 else
8656 {
8657 /* Advance to the next bigger node. */
8658 if (elt->right)
8659 elt = elt->right;
8660 else
8661 {
8662 /* We have reached the biggest node in a subtree. Find
8663 the parent of it, which is the next bigger node. */
8664 while (elt->parent && elt->parent->right == elt)
8665 elt = elt->parent;
8666 elt = elt->parent;
8667 if (elt
8668 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8669 bit_position (elt->purpose))))
8670 {
8671 next = elt->purpose;
8672 break;
8673 }
8674 }
8675 }
8676 }
8677 }
de520661 8678
3e4093b6
RS
8679 /* Ordinarily return, but not if we want to output all
8680 and there are elements left. */
3f75a254 8681 if (!(all && next != 0))
e5cfb88f
RK
8682 return;
8683
3e4093b6
RS
8684 /* If it's not incremental, just skip over the gap, so that after
8685 jumping to retry we will output the next successive element. */
8686 if (TREE_CODE (constructor_type) == RECORD_TYPE
8687 || TREE_CODE (constructor_type) == UNION_TYPE)
8688 constructor_unfilled_fields = next;
8689 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8690 constructor_unfilled_index = next;
de520661 8691
3e4093b6
RS
8692 /* ELT now points to the node in the pending tree with the next
8693 initializer to output. */
8694 goto retry;
de520661
RS
8695}
8696\f
3e4093b6
RS
8697/* Add one non-braced element to the current constructor level.
8698 This adjusts the current position within the constructor's type.
8699 This may also start or terminate implicit levels
8700 to handle a partly-braced initializer.
e5e809f4 8701
3e4093b6 8702 Once this has found the correct level for the new element,
b295aee2
JJ
8703 it calls output_init_element.
8704
8705 IMPLICIT is true if value comes from pop_init_level (1),
8706 the new initializer has been merged with the existing one
8707 and thus no warnings should be emitted about overriding an
8708 existing initializer. */
3e4093b6
RS
8709
8710void
34cf811f 8711process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8712 struct obstack * braced_init_obstack)
e5e809f4 8713{
916c5919
JM
8714 tree orig_value = value.value;
8715 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8716 bool strict_string = value.original_code == STRING_CST;
340baef7 8717 bool was_designated = designator_depth != 0;
e5e809f4 8718
3e4093b6 8719 designator_depth = 0;
b06df647 8720 designator_erroneous = 0;
e5e809f4 8721
9bac5cbb
G
8722 if (!implicit && value.value && !integer_zerop (value.value))
8723 constructor_zeroinit = 0;
8724
3e4093b6
RS
8725 /* Handle superfluous braces around string cst as in
8726 char x[] = {"foo"}; */
8727 if (string_flag
8728 && constructor_type
340baef7 8729 && !was_designated
3e4093b6 8730 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8731 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8732 && integer_zerop (constructor_unfilled_index))
e5e809f4 8733 {
916c5919 8734 if (constructor_stack->replacement_value.value)
ea58ef42 8735 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8736 constructor_stack->replacement_value = value;
8737 return;
e5e809f4 8738 }
8b6a5902 8739
916c5919 8740 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8741 {
ea58ef42 8742 error_init (loc, "excess elements in struct initializer");
3e4093b6 8743 return;
e5e809f4
JL
8744 }
8745
3e4093b6
RS
8746 /* Ignore elements of a brace group if it is entirely superfluous
8747 and has already been diagnosed. */
8748 if (constructor_type == 0)
8749 return;
e5e809f4 8750
976d5a22
TT
8751 if (!implicit && warn_designated_init && !was_designated
8752 && TREE_CODE (constructor_type) == RECORD_TYPE
8753 && lookup_attribute ("designated_init",
8754 TYPE_ATTRIBUTES (constructor_type)))
8755 warning_init (loc,
8756 OPT_Wdesignated_init,
8757 "positional initialization of field "
8758 "in %<struct%> declared with %<designated_init%> attribute");
8759
3e4093b6
RS
8760 /* If we've exhausted any levels that didn't have braces,
8761 pop them now. */
8762 while (constructor_stack->implicit)
8763 {
8764 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8765 || TREE_CODE (constructor_type) == UNION_TYPE)
8766 && constructor_fields == 0)
ea58ef42
MP
8767 process_init_element (loc,
8768 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8769 true, braced_init_obstack);
53650abe
AP
8770 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8771 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8772 && constructor_max_index
8773 && tree_int_cst_lt (constructor_max_index,
8774 constructor_index))
ea58ef42
MP
8775 process_init_element (loc,
8776 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8777 true, braced_init_obstack);
3e4093b6
RS
8778 else
8779 break;
8780 }
e5e809f4 8781
3e4093b6
RS
8782 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8783 if (constructor_range_stack)
e5e809f4 8784 {
3e4093b6
RS
8785 /* If value is a compound literal and we'll be just using its
8786 content, don't put it into a SAVE_EXPR. */
916c5919 8787 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
8788 || !require_constant_value
8789 || flag_isoc99)
8ce94e44
JM
8790 {
8791 tree semantic_type = NULL_TREE;
8792 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8793 {
8794 semantic_type = TREE_TYPE (value.value);
8795 value.value = TREE_OPERAND (value.value, 0);
8796 }
8797 value.value = c_save_expr (value.value);
8798 if (semantic_type)
8799 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8800 value.value);
8801 }
3e4093b6 8802 }
e5e809f4 8803
3e4093b6
RS
8804 while (1)
8805 {
8806 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8807 {
3e4093b6
RS
8808 tree fieldtype;
8809 enum tree_code fieldcode;
e5e809f4 8810
3e4093b6
RS
8811 if (constructor_fields == 0)
8812 {
ea58ef42 8813 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
8814 break;
8815 }
e5e809f4 8816
3e4093b6
RS
8817 fieldtype = TREE_TYPE (constructor_fields);
8818 if (fieldtype != error_mark_node)
8819 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8820 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8821
3e4093b6
RS
8822 /* Error for non-static initialization of a flexible array member. */
8823 if (fieldcode == ARRAY_TYPE
8824 && !require_constant_value
8825 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8826 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 8827 {
ea58ef42
MP
8828 error_init (loc, "non-static initialization of a flexible "
8829 "array member");
3e4093b6
RS
8830 break;
8831 }
e5e809f4 8832
2cc901dc
MP
8833 /* Error for initialization of a flexible array member with
8834 a string constant if the structure is in an array. E.g.:
8835 struct S { int x; char y[]; };
8836 struct S s[] = { { 1, "foo" } };
8837 is invalid. */
8838 if (string_flag
8839 && fieldcode == ARRAY_TYPE
8840 && constructor_depth > 1
8841 && TYPE_SIZE (fieldtype) == NULL_TREE
8842 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8843 {
8844 bool in_array_p = false;
8845 for (struct constructor_stack *p = constructor_stack;
8846 p && p->type; p = p->next)
8847 if (TREE_CODE (p->type) == ARRAY_TYPE)
8848 {
8849 in_array_p = true;
8850 break;
8851 }
8852 if (in_array_p)
8853 {
8854 error_init (loc, "initialization of flexible array "
8855 "member in a nested context");
8856 break;
8857 }
8858 }
8859
3e4093b6 8860 /* Accept a string constant to initialize a subarray. */
916c5919 8861 if (value.value != 0
3e4093b6 8862 && fieldcode == ARRAY_TYPE
197463ae 8863 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8864 && string_flag)
916c5919 8865 value.value = orig_value;
3e4093b6
RS
8866 /* Otherwise, if we have come to a subaggregate,
8867 and we don't have an element of its type, push into it. */
0953878d 8868 else if (value.value != 0
916c5919
JM
8869 && value.value != error_mark_node
8870 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8871 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8872 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8873 {
ea58ef42 8874 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8875 continue;
8876 }
e5e809f4 8877
916c5919 8878 if (value.value)
3e4093b6
RS
8879 {
8880 push_member_name (constructor_fields);
34cf811f 8881 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8882 strict_string, fieldtype,
a1e3b3d9
LB
8883 constructor_fields, 1, implicit,
8884 braced_init_obstack);
3e4093b6 8885 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8886 }
8887 else
3e4093b6
RS
8888 /* Do the bookkeeping for an element that was
8889 directly output as a constructor. */
e5e809f4 8890 {
3e4093b6
RS
8891 /* For a record, keep track of end position of last field. */
8892 if (DECL_SIZE (constructor_fields))
c22cacf3 8893 constructor_bit_index
db3927fb
AH
8894 = size_binop_loc (input_location, PLUS_EXPR,
8895 bit_position (constructor_fields),
8896 DECL_SIZE (constructor_fields));
3e4093b6
RS
8897
8898 /* If the current field was the first one not yet written out,
8899 it isn't now, so update. */
8900 if (constructor_unfilled_fields == constructor_fields)
8901 {
910ad8de 8902 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8903 /* Skip any nameless bit fields. */
8904 while (constructor_unfilled_fields != 0
8905 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8906 && DECL_NAME (constructor_unfilled_fields) == 0)
8907 constructor_unfilled_fields =
910ad8de 8908 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8909 }
e5e809f4 8910 }
3e4093b6 8911
910ad8de 8912 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8913 /* Skip any nameless bit fields at the beginning. */
8914 while (constructor_fields != 0
8915 && DECL_C_BIT_FIELD (constructor_fields)
8916 && DECL_NAME (constructor_fields) == 0)
910ad8de 8917 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8918 }
3e4093b6 8919 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8920 {
3e4093b6
RS
8921 tree fieldtype;
8922 enum tree_code fieldcode;
e5e809f4 8923
3e4093b6
RS
8924 if (constructor_fields == 0)
8925 {
d033409e 8926 pedwarn_init (loc, 0,
509c9d60 8927 "excess elements in union initializer");
3e4093b6
RS
8928 break;
8929 }
e5e809f4 8930
3e4093b6
RS
8931 fieldtype = TREE_TYPE (constructor_fields);
8932 if (fieldtype != error_mark_node)
8933 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8934 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8935
3e4093b6
RS
8936 /* Warn that traditional C rejects initialization of unions.
8937 We skip the warning if the value is zero. This is done
8938 under the assumption that the zero initializer in user
8939 code appears conditioned on e.g. __STDC__ to avoid
8940 "missing initializer" warnings and relies on default
8941 initialization to zero in the traditional C case.
8942 We also skip the warning if the initializer is designated,
8943 again on the assumption that this must be conditional on
8944 __STDC__ anyway (and we've already complained about the
8945 member-designator already). */
8400e75e 8946 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8947 && !(value.value && (integer_zerop (value.value)
8948 || real_zerop (value.value))))
3176a0c2
DD
8949 warning (OPT_Wtraditional, "traditional C rejects initialization "
8950 "of unions");
e5e809f4 8951
3e4093b6 8952 /* Accept a string constant to initialize a subarray. */
916c5919 8953 if (value.value != 0
3e4093b6 8954 && fieldcode == ARRAY_TYPE
197463ae 8955 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8956 && string_flag)
916c5919 8957 value.value = orig_value;
3e4093b6
RS
8958 /* Otherwise, if we have come to a subaggregate,
8959 and we don't have an element of its type, push into it. */
0953878d 8960 else if (value.value != 0
916c5919
JM
8961 && value.value != error_mark_node
8962 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8963 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8964 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8965 {
ea58ef42 8966 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8967 continue;
8968 }
e5e809f4 8969
916c5919 8970 if (value.value)
3e4093b6
RS
8971 {
8972 push_member_name (constructor_fields);
34cf811f 8973 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8974 strict_string, fieldtype,
a1e3b3d9
LB
8975 constructor_fields, 1, implicit,
8976 braced_init_obstack);
3e4093b6 8977 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8978 }
8979 else
3e4093b6
RS
8980 /* Do the bookkeeping for an element that was
8981 directly output as a constructor. */
e5e809f4 8982 {
3e4093b6 8983 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8984 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8985 }
e5e809f4 8986
3e4093b6
RS
8987 constructor_fields = 0;
8988 }
8989 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8990 {
8991 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8992 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8993
3e4093b6 8994 /* Accept a string constant to initialize a subarray. */
916c5919 8995 if (value.value != 0
3e4093b6 8996 && eltcode == ARRAY_TYPE
197463ae 8997 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8998 && string_flag)
916c5919 8999 value.value = orig_value;
3e4093b6
RS
9000 /* Otherwise, if we have come to a subaggregate,
9001 and we don't have an element of its type, push into it. */
0953878d 9002 else if (value.value != 0
916c5919
JM
9003 && value.value != error_mark_node
9004 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9005 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9006 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9007 {
ea58ef42 9008 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9009 continue;
9010 }
8b6a5902 9011
3e4093b6
RS
9012 if (constructor_max_index != 0
9013 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9014 || integer_all_onesp (constructor_max_index)))
9015 {
d033409e 9016 pedwarn_init (loc, 0,
509c9d60 9017 "excess elements in array initializer");
3e4093b6
RS
9018 break;
9019 }
8b6a5902 9020
3e4093b6 9021 /* Now output the actual element. */
916c5919 9022 if (value.value)
3e4093b6 9023 {
ae7e9ddd 9024 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9025 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9026 strict_string, elttype,
a1e3b3d9
LB
9027 constructor_index, 1, implicit,
9028 braced_init_obstack);
3e4093b6
RS
9029 RESTORE_SPELLING_DEPTH (constructor_depth);
9030 }
2f6e4e97 9031
3e4093b6 9032 constructor_index
db3927fb
AH
9033 = size_binop_loc (input_location, PLUS_EXPR,
9034 constructor_index, bitsize_one_node);
8b6a5902 9035
916c5919 9036 if (!value.value)
3e4093b6
RS
9037 /* If we are doing the bookkeeping for an element that was
9038 directly output as a constructor, we must update
9039 constructor_unfilled_index. */
9040 constructor_unfilled_index = constructor_index;
9041 }
9042 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9043 {
9044 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9045
c22cacf3
MS
9046 /* Do a basic check of initializer size. Note that vectors
9047 always have a fixed size derived from their type. */
3e4093b6
RS
9048 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9049 {
d033409e 9050 pedwarn_init (loc, 0,
509c9d60 9051 "excess elements in vector initializer");
3e4093b6
RS
9052 break;
9053 }
8b6a5902 9054
3e4093b6 9055 /* Now output the actual element. */
916c5919 9056 if (value.value)
53650abe
AP
9057 {
9058 if (TREE_CODE (value.value) == VECTOR_CST)
9059 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9060 output_init_element (loc, value.value, value.original_type,
53650abe 9061 strict_string, elttype,
a1e3b3d9
LB
9062 constructor_index, 1, implicit,
9063 braced_init_obstack);
53650abe 9064 }
8b6a5902 9065
3e4093b6 9066 constructor_index
db3927fb
AH
9067 = size_binop_loc (input_location,
9068 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9069
916c5919 9070 if (!value.value)
3e4093b6
RS
9071 /* If we are doing the bookkeeping for an element that was
9072 directly output as a constructor, we must update
9073 constructor_unfilled_index. */
9074 constructor_unfilled_index = constructor_index;
9075 }
8b6a5902 9076
3e4093b6
RS
9077 /* Handle the sole element allowed in a braced initializer
9078 for a scalar variable. */
b4519d39
SB
9079 else if (constructor_type != error_mark_node
9080 && constructor_fields == 0)
8b6a5902 9081 {
d033409e 9082 pedwarn_init (loc, 0,
509c9d60 9083 "excess elements in scalar initializer");
3e4093b6 9084 break;
8b6a5902
JJ
9085 }
9086 else
9087 {
916c5919 9088 if (value.value)
34cf811f 9089 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9090 strict_string, constructor_type,
a1e3b3d9
LB
9091 NULL_TREE, 1, implicit,
9092 braced_init_obstack);
3e4093b6 9093 constructor_fields = 0;
8b6a5902
JJ
9094 }
9095
3e4093b6
RS
9096 /* Handle range initializers either at this level or anywhere higher
9097 in the designator stack. */
9098 if (constructor_range_stack)
8b6a5902 9099 {
3e4093b6
RS
9100 struct constructor_range_stack *p, *range_stack;
9101 int finish = 0;
9102
9103 range_stack = constructor_range_stack;
9104 constructor_range_stack = 0;
9105 while (constructor_stack != range_stack->stack)
8b6a5902 9106 {
366de0ce 9107 gcc_assert (constructor_stack->implicit);
34cf811f 9108 process_init_element (loc,
ea58ef42
MP
9109 pop_init_level (loc, 1,
9110 braced_init_obstack),
a1e3b3d9 9111 true, braced_init_obstack);
8b6a5902 9112 }
3e4093b6
RS
9113 for (p = range_stack;
9114 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9115 p = p->prev)
8b6a5902 9116 {
366de0ce 9117 gcc_assert (constructor_stack->implicit);
34cf811f 9118 process_init_element (loc,
ea58ef42
MP
9119 pop_init_level (loc, 1,
9120 braced_init_obstack),
a1e3b3d9 9121 true, braced_init_obstack);
8b6a5902 9122 }
3e4093b6 9123
db3927fb
AH
9124 p->index = size_binop_loc (input_location,
9125 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9126 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9127 finish = 1;
9128
9129 while (1)
9130 {
9131 constructor_index = p->index;
9132 constructor_fields = p->fields;
9133 if (finish && p->range_end && p->index == p->range_start)
9134 {
9135 finish = 0;
9136 p->prev = 0;
9137 }
9138 p = p->next;
9139 if (!p)
9140 break;
ea58ef42 9141 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9142 p->stack = constructor_stack;
9143 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9144 p->index = p->range_start;
9145 }
9146
9147 if (!finish)
9148 constructor_range_stack = range_stack;
9149 continue;
8b6a5902
JJ
9150 }
9151
3e4093b6 9152 break;
8b6a5902
JJ
9153 }
9154
3e4093b6
RS
9155 constructor_range_stack = 0;
9156}
9157\f
9f0e2d86
ZW
9158/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9159 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9160 an ASM_EXPR node). */
3e4093b6 9161tree
9f0e2d86 9162build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9163{
6de9cd9a
DN
9164 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9165 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9166 return add_stmt (args);
8b6a5902
JJ
9167}
9168
9f0e2d86
ZW
9169/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9170 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9171 SIMPLE indicates whether there was anything at all after the
9172 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9173 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9174tree
c2255bc4 9175build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9176 tree clobbers, tree labels, bool simple)
e5e809f4 9177{
3e4093b6 9178 tree tail;
9f0e2d86 9179 tree args;
6de9cd9a
DN
9180 int i;
9181 const char *constraint;
74f0c611 9182 const char **oconstraints;
6de9cd9a 9183 bool allows_mem, allows_reg, is_inout;
74f0c611 9184 int ninputs, noutputs;
6de9cd9a
DN
9185
9186 ninputs = list_length (inputs);
9187 noutputs = list_length (outputs);
74f0c611
RH
9188 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9189
1c384bf1 9190 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9191
6de9cd9a
DN
9192 /* Remove output conversions that change the type but not the mode. */
9193 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9194 {
3e4093b6 9195 tree output = TREE_VALUE (tail);
74f0c611 9196
eadd3d0d
JJ
9197 output = c_fully_fold (output, false, NULL);
9198
74f0c611
RH
9199 /* ??? Really, this should not be here. Users should be using a
9200 proper lvalue, dammit. But there's a long history of using casts
9201 in the output operands. In cases like longlong.h, this becomes a
9202 primitive form of typechecking -- if the cast can be removed, then
9203 the output operand had a type of the proper width; otherwise we'll
9204 get an error. Gross, but ... */
3e4093b6 9205 STRIP_NOPS (output);
74f0c611 9206
7bd11157 9207 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9208 output = error_mark_node;
8b6a5902 9209
5544530a
PB
9210 if (output != error_mark_node
9211 && (TREE_READONLY (output)
9212 || TYPE_READONLY (TREE_TYPE (output))
9213 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9214 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9215 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9216 readonly_error (loc, output, lv_asm);
5544530a 9217
6de9cd9a 9218 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9219 oconstraints[i] = constraint;
9220
9221 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9222 &allows_mem, &allows_reg, &is_inout))
9223 {
9224 /* If the operand is going to end up in memory,
9225 mark it addressable. */
9226 if (!allows_reg && !c_mark_addressable (output))
9227 output = error_mark_node;
bae5cddf
JJ
9228 if (!(!allows_reg && allows_mem)
9229 && output != error_mark_node
9230 && VOID_TYPE_P (TREE_TYPE (output)))
9231 {
9232 error_at (loc, "invalid use of void expression");
9233 output = error_mark_node;
9234 }
74f0c611
RH
9235 }
9236 else
c22cacf3 9237 output = error_mark_node;
3e4093b6 9238
74f0c611 9239 TREE_VALUE (tail) = output;
8b6a5902 9240 }
3e4093b6 9241
74f0c611
RH
9242 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9243 {
9244 tree input;
9245
9246 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9247 input = TREE_VALUE (tail);
9248
74f0c611
RH
9249 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9250 oconstraints, &allows_mem, &allows_reg))
9251 {
9252 /* If the operand is going to end up in memory,
9253 mark it addressable. */
b4c33883
AP
9254 if (!allows_reg && allows_mem)
9255 {
eadd3d0d
JJ
9256 input = c_fully_fold (input, false, NULL);
9257
b4c33883
AP
9258 /* Strip the nops as we allow this case. FIXME, this really
9259 should be rejected or made deprecated. */
9260 STRIP_NOPS (input);
9261 if (!c_mark_addressable (input))
9262 input = error_mark_node;
bae5cddf 9263 }
eadd3d0d 9264 else
bae5cddf 9265 {
eadd3d0d
JJ
9266 struct c_expr expr;
9267 memset (&expr, 0, sizeof (expr));
9268 expr.value = input;
267bac10 9269 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9270 input = c_fully_fold (expr.value, false, NULL);
9271
9272 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9273 {
9274 error_at (loc, "invalid use of void expression");
9275 input = error_mark_node;
9276 }
bae5cddf 9277 }
74f0c611
RH
9278 }
9279 else
9280 input = error_mark_node;
9281
9282 TREE_VALUE (tail) = input;
9283 }
3e4093b6 9284
1c384bf1
RH
9285 /* ASMs with labels cannot have outputs. This should have been
9286 enforced by the parser. */
9287 gcc_assert (outputs == NULL || labels == NULL);
9288
9289 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9290
5544530a
PB
9291 /* asm statements without outputs, including simple ones, are treated
9292 as volatile. */
9293 ASM_INPUT_P (args) = simple;
9294 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9295
9f0e2d86 9296 return args;
e5e809f4 9297}
3e4093b6 9298\f
c2255bc4
AH
9299/* Generate a goto statement to LABEL. LOC is the location of the
9300 GOTO. */
506e2710
RH
9301
9302tree
c2255bc4 9303c_finish_goto_label (location_t loc, tree label)
506e2710 9304{
e1b7793c 9305 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9306 if (!decl)
9307 return NULL_TREE;
506e2710 9308 TREE_USED (decl) = 1;
c2255bc4
AH
9309 {
9310 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9311 SET_EXPR_LOCATION (t, loc);
9312 return add_stmt (t);
9313 }
506e2710
RH
9314}
9315
c2255bc4
AH
9316/* Generate a computed goto statement to EXPR. LOC is the location of
9317 the GOTO. */
506e2710
RH
9318
9319tree
c2255bc4 9320c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9321{
c2255bc4 9322 tree t;
c1771a20 9323 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9324 expr = c_fully_fold (expr, false, NULL);
506e2710 9325 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9326 t = build1 (GOTO_EXPR, void_type_node, expr);
9327 SET_EXPR_LOCATION (t, loc);
9328 return add_stmt (t);
506e2710
RH
9329}
9330
5088b058 9331/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9332 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9333 the location of the return statement, or the location of the expression,
9334 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9335 is the original type of RETVAL. */
de520661 9336
506e2710 9337tree
c2255bc4 9338c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9339{
0c9b182b
JJ
9340 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9341 bool no_warning = false;
928c19bb 9342 bool npc = false;
36536d79 9343 size_t rank = 0;
3e4093b6
RS
9344
9345 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9346 warning_at (loc, 0,
9347 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9348
b72271b9 9349 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9350 {
9351 /* Array notations are allowed in a return statement if it is inside a
9352 built-in array notation reduction function. */
9353 if (!find_rank (loc, retval, retval, false, &rank))
9354 return error_mark_node;
9355 if (rank >= 1)
9356 {
9357 error_at (loc, "array notation expression cannot be used as a "
9358 "return value");
9359 return error_mark_node;
9360 }
9361 }
3af9c5e9 9362 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9363 {
9364 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9365 "allowed");
9366 return error_mark_node;
9367 }
928c19bb
JM
9368 if (retval)
9369 {
8ce94e44 9370 tree semantic_type = NULL_TREE;
928c19bb 9371 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9372 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9373 {
9374 semantic_type = TREE_TYPE (retval);
9375 retval = TREE_OPERAND (retval, 0);
9376 }
928c19bb 9377 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9378 if (semantic_type)
9379 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9380 }
9381
3e4093b6 9382 if (!retval)
de520661 9383 {
3e4093b6
RS
9384 current_function_returns_null = 1;
9385 if ((warn_return_type || flag_isoc99)
9386 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9387 {
35aff4fb
MP
9388 if (flag_isoc99)
9389 pedwarn (loc, 0, "%<return%> with no value, in "
9390 "function returning non-void");
9391 else
9392 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9393 "in function returning non-void");
0c9b182b
JJ
9394 no_warning = true;
9395 }
400fbf9f 9396 }
3e4093b6 9397 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9398 {
3e4093b6 9399 current_function_returns_null = 1;
2397c575 9400 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9401 pedwarn (loc, 0,
509c9d60 9402 "%<return%> with a value, in function returning void");
b8698a0f 9403 else
c1771a20 9404 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9405 "%<return%> with expression, in function returning void");
de520661 9406 }
3e4093b6 9407 else
de520661 9408 {
68fca595
MP
9409 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9410 retval, origtype, ic_return,
c2255bc4 9411 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9412 tree res = DECL_RESULT (current_function_decl);
9413 tree inner;
9feb29df 9414 bool save;
3e4093b6
RS
9415
9416 current_function_returns_value = 1;
9417 if (t == error_mark_node)
506e2710 9418 return NULL_TREE;
3e4093b6 9419
9feb29df
JJ
9420 save = in_late_binary_op;
9421 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9422 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9423 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9424 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9425 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9426 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9427 in_late_binary_op = true;
3e4093b6 9428 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9429 in_late_binary_op = save;
3e4093b6
RS
9430
9431 /* Strip any conversions, additions, and subtractions, and see if
9432 we are returning the address of a local variable. Warn if so. */
9433 while (1)
8b6a5902 9434 {
3e4093b6 9435 switch (TREE_CODE (inner))
8b6a5902 9436 {
849421a3
JJ
9437 CASE_CONVERT:
9438 case NON_LVALUE_EXPR:
3e4093b6 9439 case PLUS_EXPR:
849421a3 9440 case POINTER_PLUS_EXPR:
3e4093b6
RS
9441 inner = TREE_OPERAND (inner, 0);
9442 continue;
9443
9444 case MINUS_EXPR:
9445 /* If the second operand of the MINUS_EXPR has a pointer
9446 type (or is converted from it), this may be valid, so
9447 don't give a warning. */
9448 {
9449 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9450
3f75a254 9451 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9452 && (CONVERT_EXPR_P (op1)
9453 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9454 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9455
3e4093b6
RS
9456 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9457 break;
8b6a5902 9458
3e4093b6
RS
9459 inner = TREE_OPERAND (inner, 0);
9460 continue;
9461 }
400fbf9f 9462
3e4093b6
RS
9463 case ADDR_EXPR:
9464 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9465
6615c446 9466 while (REFERENCE_CLASS_P (inner)
c22cacf3 9467 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 9468 inner = TREE_OPERAND (inner, 0);
8b6a5902 9469
a2f1f4c3 9470 if (DECL_P (inner)
3f75a254
JM
9471 && !DECL_EXTERNAL (inner)
9472 && !TREE_STATIC (inner)
3e4093b6 9473 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9474 {
9475 if (TREE_CODE (inner) == LABEL_DECL)
9476 warning_at (loc, OPT_Wreturn_local_addr,
9477 "function returns address of label");
9478 else
b4dfdc11
MG
9479 {
9480 warning_at (loc, OPT_Wreturn_local_addr,
9481 "function returns address of local variable");
9482 tree zero = build_zero_cst (TREE_TYPE (res));
9483 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9484 }
19fc9faa 9485 }
3e4093b6 9486 break;
8b6a5902 9487
3e4093b6
RS
9488 default:
9489 break;
9490 }
de520661 9491
3e4093b6
RS
9492 break;
9493 }
9494
53fb4de3 9495 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9496 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9497
9498 if (warn_sequence_point)
9499 verify_sequence_points (retval);
de520661 9500 }
8b6a5902 9501
c2255bc4 9502 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9503 TREE_NO_WARNING (ret_stmt) |= no_warning;
9504 return add_stmt (ret_stmt);
de520661 9505}
3e4093b6
RS
9506\f
9507struct c_switch {
604f5adf
ILT
9508 /* The SWITCH_EXPR being built. */
9509 tree switch_expr;
a6c0a76c 9510
89dbed81 9511 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9512 default conversion is applied. */
9513 tree orig_type;
9514
3e4093b6
RS
9515 /* A splay-tree mapping the low element of a case range to the high
9516 element, or NULL_TREE if there is no high element. Used to
9517 determine whether or not a new case label duplicates an old case
9518 label. We need a tree, rather than simply a hash table, because
9519 of the GNU case range extension. */
9520 splay_tree cases;
a6c0a76c 9521
e1b7793c
ILT
9522 /* The bindings at the point of the switch. This is used for
9523 warnings crossing decls when branching to a case label. */
9524 struct c_spot_bindings *bindings;
187230a7 9525
3e4093b6
RS
9526 /* The next node on the stack. */
9527 struct c_switch *next;
9528};
400fbf9f 9529
3e4093b6
RS
9530/* A stack of the currently active switch statements. The innermost
9531 switch statement is on the top of the stack. There is no need to
9532 mark the stack for garbage collection because it is only active
9533 during the processing of the body of a function, and we never
9534 collect at that point. */
de520661 9535
506e2710 9536struct c_switch *c_switch_stack;
de520661 9537
3e4093b6 9538/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9539 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef
MP
9540 SWITCH_COND_LOC is the location of the switch's condition.
9541 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
de520661 9542
3e4093b6 9543tree
c2255bc4
AH
9544c_start_case (location_t switch_loc,
9545 location_t switch_cond_loc,
fedfecef 9546 tree exp, bool explicit_cast_p)
de520661 9547{
c58e8676 9548 tree orig_type = error_mark_node;
3e4093b6 9549 struct c_switch *cs;
2f6e4e97 9550
3e4093b6 9551 if (exp != error_mark_node)
de520661 9552 {
3e4093b6
RS
9553 orig_type = TREE_TYPE (exp);
9554
c58e8676 9555 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9556 {
c58e8676
VR
9557 if (orig_type != error_mark_node)
9558 {
c2255bc4 9559 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9560 orig_type = error_mark_node;
9561 }
3e4093b6 9562 exp = integer_zero_node;
de520661 9563 }
3e4093b6 9564 else
de520661 9565 {
c58e8676 9566 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9567 tree e = exp;
9568
9569 /* Warn if the condition has boolean value. */
9570 while (TREE_CODE (e) == COMPOUND_EXPR)
9571 e = TREE_OPERAND (e, 1);
9572
9573 if ((TREE_CODE (type) == BOOLEAN_TYPE
9574 || truth_value_p (TREE_CODE (e)))
9575 /* Explicit cast to int suppresses this warning. */
9576 && !(TREE_CODE (type) == INTEGER_TYPE
9577 && explicit_cast_p))
9578 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9579 "switch condition has boolean value");
8b6a5902 9580
8400e75e 9581 if (!in_system_header_at (input_location)
3e4093b6
RS
9582 && (type == long_integer_type_node
9583 || type == long_unsigned_type_node))
c2255bc4
AH
9584 warning_at (switch_cond_loc,
9585 OPT_Wtraditional, "%<long%> switch expression not "
9586 "converted to %<int%> in ISO C");
8b6a5902 9587
928c19bb 9588 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9589 exp = default_conversion (exp);
ca085fd7
MLI
9590
9591 if (warn_sequence_point)
9592 verify_sequence_points (exp);
3e4093b6
RS
9593 }
9594 }
9595
604f5adf 9596 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9597 cs = XNEW (struct c_switch);
604f5adf 9598 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9599 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9600 cs->orig_type = orig_type;
3e4093b6 9601 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9602 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9603 cs->next = c_switch_stack;
9604 c_switch_stack = cs;
3e4093b6 9605
604f5adf 9606 return add_stmt (cs->switch_expr);
3e4093b6
RS
9607}
9608
c2255bc4 9609/* Process a case label at location LOC. */
3e4093b6
RS
9610
9611tree
c2255bc4 9612do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9613{
9614 tree label = NULL_TREE;
9615
17cede2e
JM
9616 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9617 {
9618 low_value = c_fully_fold (low_value, false, NULL);
9619 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9620 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9621 "case label is not an integer constant expression");
9622 }
9623
9624 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9625 {
9626 high_value = c_fully_fold (high_value, false, NULL);
9627 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9628 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9629 "case label is not an integer constant expression");
9630 }
9631
e1b7793c 9632 if (c_switch_stack == NULL)
187230a7
JM
9633 {
9634 if (low_value)
e1b7793c 9635 error_at (loc, "case label not within a switch statement");
187230a7 9636 else
e1b7793c
ILT
9637 error_at (loc, "%<default%> label not within a switch statement");
9638 return NULL_TREE;
187230a7 9639 }
de520661 9640
e1b7793c
ILT
9641 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9642 EXPR_LOCATION (c_switch_stack->switch_expr),
9643 loc))
9644 return NULL_TREE;
9645
9646 label = c_add_case_label (loc, c_switch_stack->cases,
9647 SWITCH_COND (c_switch_stack->switch_expr),
9648 c_switch_stack->orig_type,
9649 low_value, high_value);
9650 if (label == error_mark_node)
9651 label = NULL_TREE;
3e4093b6
RS
9652 return label;
9653}
de520661 9654
083e891e
MP
9655/* Finish the switch statement. TYPE is the original type of the
9656 controlling expression of the switch, or NULL_TREE. */
de520661 9657
3e4093b6 9658void
083e891e 9659c_finish_case (tree body, tree type)
3e4093b6 9660{
506e2710 9661 struct c_switch *cs = c_switch_stack;
fbc315db 9662 location_t switch_location;
3e4093b6 9663
604f5adf 9664 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9665
6de9cd9a 9666 /* Emit warnings as needed. */
c2255bc4 9667 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 9668 c_do_switch_warnings (cs->cases, switch_location,
083e891e 9669 type ? type : TREE_TYPE (cs->switch_expr),
fbc315db 9670 SWITCH_COND (cs->switch_expr));
6de9cd9a 9671
3e4093b6 9672 /* Pop the stack. */
506e2710 9673 c_switch_stack = cs->next;
3e4093b6 9674 splay_tree_delete (cs->cases);
e1b7793c 9675 c_release_switch_bindings (cs->bindings);
5d038c4c 9676 XDELETE (cs);
de520661 9677}
325c3691 9678\f
506e2710
RH
9679/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9680 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9681 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9682 statement, and was not surrounded with parenthesis. */
325c3691 9683
9e51cf9d 9684void
506e2710
RH
9685c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9686 tree else_block, bool nested_if)
325c3691 9687{
506e2710 9688 tree stmt;
325c3691 9689
25c22937
BI
9690 /* If the condition has array notations, then the rank of the then_block and
9691 else_block must be either 0 or be equal to the rank of the condition. If
9692 the condition does not have array notations then break them up as it is
9693 broken up in a normal expression. */
b72271b9 9694 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9695 {
9696 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9697 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9698 return;
9699 if (then_block
9700 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9701 return;
9702 if (else_block
9703 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9704 return;
9705 if (cond_rank != then_rank && then_rank != 0)
9706 {
9707 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9708 " and the then-block");
9709 return;
9710 }
9711 else if (cond_rank != else_rank && else_rank != 0)
9712 {
9713 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9714 " and the else-block");
9715 return;
9716 }
9717 }
506e2710
RH
9718 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9719 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9720 {
506e2710 9721 tree inner_if = then_block;
16865eaa 9722
61ada8ae 9723 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9724 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9725 it might not be exactly THEN_BLOCK, but should be the last
9726 non-container statement within. */
9727 while (1)
9728 switch (TREE_CODE (inner_if))
9729 {
9730 case COND_EXPR:
9731 goto found;
9732 case BIND_EXPR:
9733 inner_if = BIND_EXPR_BODY (inner_if);
9734 break;
9735 case STATEMENT_LIST:
9736 inner_if = expr_last (then_block);
9737 break;
9738 case TRY_FINALLY_EXPR:
9739 case TRY_CATCH_EXPR:
9740 inner_if = TREE_OPERAND (inner_if, 0);
9741 break;
9742 default:
366de0ce 9743 gcc_unreachable ();
506e2710
RH
9744 }
9745 found:
16865eaa 9746
506e2710 9747 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9748 warning_at (if_locus, OPT_Wparentheses,
9749 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9750 }
16865eaa 9751
2214de30 9752 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9753 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9754 add_stmt (stmt);
325c3691
RH
9755}
9756
506e2710
RH
9757/* Emit a general-purpose loop construct. START_LOCUS is the location of
9758 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9759 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9760 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9761 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9762
9763void
506e2710
RH
9764c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9765 tree blab, tree clab, bool cond_is_first)
325c3691 9766{
506e2710
RH
9767 tree entry = NULL, exit = NULL, t;
9768
e5e44252
AK
9769 /* In theory could forbid cilk spawn for loop increment expression,
9770 but it should work just fine. */
36536d79 9771
28af952a
RS
9772 /* If the condition is zero don't generate a loop construct. */
9773 if (cond && integer_zerop (cond))
9774 {
9775 if (cond_is_first)
9776 {
9777 t = build_and_jump (&blab);
9778 SET_EXPR_LOCATION (t, start_locus);
9779 add_stmt (t);
9780 }
9781 }
9782 else
506e2710
RH
9783 {
9784 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9785
506e2710 9786 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9787 out of the loop, or to the top of it. If there's no exit condition,
9788 then we just build a jump back to the top. */
506e2710 9789 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9790
28af952a 9791 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9792 {
9793 /* Canonicalize the loop condition to the end. This means
9794 generating a branch to the loop condition. Reuse the
9795 continue label, if possible. */
9796 if (cond_is_first)
9797 {
9798 if (incr || !clab)
9799 {
9800 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9801 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9802 }
9803 else
9804 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9805 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9806 add_stmt (t);
9807 }
9808
506e2710 9809 t = build_and_jump (&blab);
506e2710 9810 if (cond_is_first)
db3927fb
AH
9811 exit = fold_build3_loc (start_locus,
9812 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9813 else
db3927fb
AH
9814 exit = fold_build3_loc (input_location,
9815 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9816 }
9817
506e2710
RH
9818 add_stmt (top);
9819 }
c22cacf3 9820
506e2710
RH
9821 if (body)
9822 add_stmt (body);
9823 if (clab)
9824 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9825 if (incr)
9826 add_stmt (incr);
9827 if (entry)
9828 add_stmt (entry);
9829 if (exit)
9830 add_stmt (exit);
9831 if (blab)
9832 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9833}
325c3691
RH
9834
9835tree
c2255bc4 9836c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9837{
089efaa4 9838 bool skip;
506e2710 9839 tree label = *label_p;
325c3691 9840
089efaa4
ILT
9841 /* In switch statements break is sometimes stylistically used after
9842 a return statement. This can lead to spurious warnings about
9843 control reaching the end of a non-void function when it is
9844 inlined. Note that we are calling block_may_fallthru with
9845 language specific tree nodes; this works because
9846 block_may_fallthru returns true when given something it does not
9847 understand. */
9848 skip = !block_may_fallthru (cur_stmt_list);
9849
506e2710 9850 if (!label)
089efaa4
ILT
9851 {
9852 if (!skip)
c2255bc4 9853 *label_p = label = create_artificial_label (loc);
089efaa4 9854 }
953ff289
DN
9855 else if (TREE_CODE (label) == LABEL_DECL)
9856 ;
9857 else switch (TREE_INT_CST_LOW (label))
506e2710 9858 {
953ff289 9859 case 0:
506e2710 9860 if (is_break)
c2255bc4 9861 error_at (loc, "break statement not within loop or switch");
506e2710 9862 else
c2255bc4 9863 error_at (loc, "continue statement not within a loop");
506e2710 9864 return NULL_TREE;
953ff289
DN
9865
9866 case 1:
9867 gcc_assert (is_break);
c2255bc4 9868 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9869 return NULL_TREE;
9870
c02065fc
AH
9871 case 2:
9872 if (is_break)
9873 error ("break statement within %<#pragma simd%> loop body");
9874 else
9875 error ("continue statement within %<#pragma simd%> loop body");
9876 return NULL_TREE;
9877
953ff289
DN
9878 default:
9879 gcc_unreachable ();
506e2710 9880 }
325c3691 9881
089efaa4
ILT
9882 if (skip)
9883 return NULL_TREE;
9884
2e28e797
JH
9885 if (!is_break)
9886 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9887
53fb4de3 9888 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9889}
9890
506e2710 9891/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9892
9893static void
c2255bc4 9894emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9895{
e6b5a630
RH
9896 if (expr == error_mark_node)
9897 ;
9898 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9899 {
9900 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9901 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9902 }
789eadcd
MP
9903 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9904 {
9905 tree r = expr;
9906 location_t cloc = loc;
9907 while (TREE_CODE (r) == COMPOUND_EXPR)
9908 {
9909 if (EXPR_HAS_LOCATION (r))
9910 cloc = EXPR_LOCATION (r);
9911 r = TREE_OPERAND (r, 1);
9912 }
9913 if (!TREE_SIDE_EFFECTS (r)
9914 && !VOID_TYPE_P (TREE_TYPE (r))
9915 && !CONVERT_EXPR_P (r)
cc28fc7f 9916 && !TREE_NO_WARNING (r)
789eadcd
MP
9917 && !TREE_NO_WARNING (expr))
9918 warning_at (cloc, OPT_Wunused_value,
9919 "right-hand operand of comma expression has no effect");
9920 }
27f33b15 9921 else
c2255bc4 9922 warn_if_unused_value (expr, loc);
3a5b9284
RH
9923}
9924
506e2710 9925/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9926 diagnostics, but do not call ADD_STMT. LOC is the location of the
9927 statement. */
3a5b9284 9928
506e2710 9929tree
c2255bc4 9930c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9931{
056928b2
JJ
9932 tree exprv;
9933
3a5b9284 9934 if (!expr)
506e2710 9935 return NULL_TREE;
3a5b9284 9936
928c19bb
JM
9937 expr = c_fully_fold (expr, false, NULL);
9938
3a5b9284
RH
9939 if (warn_sequence_point)
9940 verify_sequence_points (expr);
9941
9942 if (TREE_TYPE (expr) != error_mark_node
9943 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9944 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9945 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9946
9947 /* If we're not processing a statement expression, warn about unused values.
9948 Warnings for statement expressions will be emitted later, once we figure
9949 out which is the result. */
9950 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9951 && warn_unused_value)
c2255bc4 9952 emit_side_effect_warnings (loc, expr);
3a5b9284 9953
056928b2
JJ
9954 exprv = expr;
9955 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9956 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9957 while (CONVERT_EXPR_P (exprv))
9958 exprv = TREE_OPERAND (exprv, 0);
9959 if (DECL_P (exprv)
9960 || handled_component_p (exprv)
9961 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9962 mark_exp_read (exprv);
fa8351f8 9963
3a5b9284
RH
9964 /* If the expression is not of a type to which we cannot assign a line
9965 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9966 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9967 {
9968 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9969 SET_EXPR_LOCATION (expr, loc);
9970 }
506e2710
RH
9971
9972 return expr;
9973}
9974
c2255bc4
AH
9975/* Emit an expression as a statement. LOC is the location of the
9976 expression. */
506e2710
RH
9977
9978tree
c2255bc4 9979c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9980{
9981 if (expr)
c2255bc4 9982 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9983 else
9984 return NULL;
3a5b9284
RH
9985}
9986
9987/* Do the opposite and emit a statement as an expression. To begin,
9988 create a new binding level and return it. */
325c3691
RH
9989
9990tree
9991c_begin_stmt_expr (void)
9992{
9993 tree ret;
9994
9995 /* We must force a BLOCK for this level so that, if it is not expanded
9996 later, there is a way to turn off the entire subtree of blocks that
9997 are contained in it. */
9998 keep_next_level ();
9999 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10000
10001 c_bindings_start_stmt_expr (c_switch_stack == NULL
10002 ? NULL
10003 : c_switch_stack->bindings);
325c3691
RH
10004
10005 /* Mark the current statement list as belonging to a statement list. */
10006 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10007
10008 return ret;
10009}
10010
c2255bc4
AH
10011/* LOC is the location of the compound statement to which this body
10012 belongs. */
10013
325c3691 10014tree
c2255bc4 10015c_finish_stmt_expr (location_t loc, tree body)
325c3691 10016{
3a5b9284 10017 tree last, type, tmp, val;
325c3691
RH
10018 tree *last_p;
10019
c2255bc4 10020 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10021
10022 c_bindings_end_stmt_expr (c_switch_stack == NULL
10023 ? NULL
10024 : c_switch_stack->bindings);
325c3691 10025
3a5b9284
RH
10026 /* Locate the last statement in BODY. See c_end_compound_stmt
10027 about always returning a BIND_EXPR. */
10028 last_p = &BIND_EXPR_BODY (body);
10029 last = BIND_EXPR_BODY (body);
10030
10031 continue_searching:
325c3691
RH
10032 if (TREE_CODE (last) == STATEMENT_LIST)
10033 {
3a5b9284
RH
10034 tree_stmt_iterator i;
10035
10036 /* This can happen with degenerate cases like ({ }). No value. */
10037 if (!TREE_SIDE_EFFECTS (last))
10038 return body;
10039
10040 /* If we're supposed to generate side effects warnings, process
10041 all of the statements except the last. */
27f33b15 10042 if (warn_unused_value)
325c3691 10043 {
3a5b9284 10044 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10045 {
10046 location_t tloc;
10047 tree t = tsi_stmt (i);
10048
10049 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10050 emit_side_effect_warnings (tloc, t);
10051 }
325c3691
RH
10052 }
10053 else
3a5b9284
RH
10054 i = tsi_last (last);
10055 last_p = tsi_stmt_ptr (i);
10056 last = *last_p;
325c3691
RH
10057 }
10058
3a5b9284
RH
10059 /* If the end of the list is exception related, then the list was split
10060 by a call to push_cleanup. Continue searching. */
10061 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10062 || TREE_CODE (last) == TRY_CATCH_EXPR)
10063 {
10064 last_p = &TREE_OPERAND (last, 0);
10065 last = *last_p;
10066 goto continue_searching;
10067 }
10068
26d8af35
JM
10069 if (last == error_mark_node)
10070 return last;
10071
3a5b9284
RH
10072 /* In the case that the BIND_EXPR is not necessary, return the
10073 expression out from inside it. */
26d8af35
JM
10074 if (last == BIND_EXPR_BODY (body)
10075 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10076 {
928c19bb
JM
10077 /* Even if this looks constant, do not allow it in a constant
10078 expression. */
e5a94231 10079 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10080 /* Do not warn if the return value of a statement expression is
10081 unused. */
928c19bb 10082 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10083 return last;
10084 }
325c3691
RH
10085
10086 /* Extract the type of said expression. */
10087 type = TREE_TYPE (last);
325c3691 10088
3a5b9284
RH
10089 /* If we're not returning a value at all, then the BIND_EXPR that
10090 we already have is a fine expression to return. */
10091 if (!type || VOID_TYPE_P (type))
10092 return body;
10093
10094 /* Now that we've located the expression containing the value, it seems
10095 silly to make voidify_wrapper_expr repeat the process. Create a
10096 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10097 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10098
10099 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10100 tree_expr_nonnegative_p giving up immediately. */
10101 val = last;
10102 if (TREE_CODE (val) == NOP_EXPR
10103 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10104 val = TREE_OPERAND (val, 0);
10105
53fb4de3 10106 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10107 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10108
c2255bc4
AH
10109 {
10110 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10111 SET_EXPR_LOCATION (t, loc);
10112 return t;
10113 }
325c3691
RH
10114}
10115\f
10116/* Begin and end compound statements. This is as simple as pushing
10117 and popping new statement lists from the tree. */
10118
10119tree
10120c_begin_compound_stmt (bool do_scope)
10121{
10122 tree stmt = push_stmt_list ();
10123 if (do_scope)
4dfa0342 10124 push_scope ();
325c3691
RH
10125 return stmt;
10126}
10127
c2255bc4
AH
10128/* End a compound statement. STMT is the statement. LOC is the
10129 location of the compound statement-- this is usually the location
10130 of the opening brace. */
10131
325c3691 10132tree
c2255bc4 10133c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10134{
10135 tree block = NULL;
10136
10137 if (do_scope)
10138 {
10139 if (c_dialect_objc ())
10140 objc_clear_super_receiver ();
10141 block = pop_scope ();
10142 }
10143
10144 stmt = pop_stmt_list (stmt);
c2255bc4 10145 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10146
10147 /* If this compound statement is nested immediately inside a statement
10148 expression, then force a BIND_EXPR to be created. Otherwise we'll
10149 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10150 STATEMENT_LISTs merge, and thus we can lose track of what statement
10151 was really last. */
38e01f9e 10152 if (building_stmt_list_p ()
325c3691
RH
10153 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10154 && TREE_CODE (stmt) != BIND_EXPR)
10155 {
53fb4de3 10156 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10157 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10158 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10159 }
10160
10161 return stmt;
10162}
5a508662
RH
10163
10164/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10165 when the current scope is exited. EH_ONLY is true when this is not
10166 meant to apply to normal control flow transfer. */
10167
10168void
c2255bc4 10169push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10170{
3a5b9284
RH
10171 enum tree_code code;
10172 tree stmt, list;
10173 bool stmt_expr;
10174
10175 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10176 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10177 add_stmt (stmt);
3a5b9284
RH
10178 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10179 list = push_stmt_list ();
10180 TREE_OPERAND (stmt, 0) = list;
10181 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10182}
325c3691 10183\f
3e4093b6
RS
10184/* Build a binary-operation expression without default conversions.
10185 CODE is the kind of expression to build.
ba47d38d 10186 LOCATION is the operator's location.
3e4093b6
RS
10187 This function differs from `build' in several ways:
10188 the data type of the result is computed and recorded in it,
10189 warnings are generated if arg data types are invalid,
10190 special handling for addition and subtraction of pointers is known,
10191 and some optimization is done (operations on narrow ints
10192 are done in the narrower type when that gives the same result).
10193 Constant folding is also done before the result is returned.
de520661 10194
3e4093b6
RS
10195 Note that the operands will never have enumeral types, or function
10196 or array types, because either they will have the default conversions
10197 performed or they have both just been converted to some other type in which
10198 the arithmetic is to be done. */
10199
10200tree
ba47d38d
AH
10201build_binary_op (location_t location, enum tree_code code,
10202 tree orig_op0, tree orig_op1, int convert_p)
de520661 10203{
8ce94e44
JM
10204 tree type0, type1, orig_type0, orig_type1;
10205 tree eptype;
3e4093b6
RS
10206 enum tree_code code0, code1;
10207 tree op0, op1;
c9f9eb5d 10208 tree ret = error_mark_node;
4de67c26 10209 const char *invalid_op_diag;
4d84fe7c 10210 bool op0_int_operands, op1_int_operands;
928c19bb 10211 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10212
3e4093b6
RS
10213 /* Expression code to give to the expression when it is built.
10214 Normally this is CODE, which is what the caller asked for,
10215 but in some special cases we change it. */
10216 enum tree_code resultcode = code;
8b6a5902 10217
3e4093b6
RS
10218 /* Data type in which the computation is to be performed.
10219 In the simplest cases this is the common type of the arguments. */
10220 tree result_type = NULL;
10221
8ce94e44
JM
10222 /* When the computation is in excess precision, the type of the
10223 final EXCESS_PRECISION_EXPR. */
2d2e923f 10224 tree semantic_result_type = NULL;
8ce94e44 10225
3e4093b6
RS
10226 /* Nonzero means operands have already been type-converted
10227 in whatever way is necessary.
10228 Zero means they need to be converted to RESULT_TYPE. */
10229 int converted = 0;
10230
10231 /* Nonzero means create the expression with this type, rather than
10232 RESULT_TYPE. */
10233 tree build_type = 0;
10234
10235 /* Nonzero means after finally constructing the expression
10236 convert it to this type. */
10237 tree final_type = 0;
10238
10239 /* Nonzero if this is an operation like MIN or MAX which can
10240 safely be computed in short if both args are promoted shorts.
10241 Also implies COMMON.
10242 -1 indicates a bitwise operation; this makes a difference
10243 in the exact conditions for when it is safe to do the operation
10244 in a narrower mode. */
10245 int shorten = 0;
10246
10247 /* Nonzero if this is a comparison operation;
10248 if both args are promoted shorts, compare the original shorts.
10249 Also implies COMMON. */
10250 int short_compare = 0;
10251
10252 /* Nonzero if this is a right-shift operation, which can be computed on the
10253 original short and then promoted if the operand is a promoted short. */
10254 int short_shift = 0;
10255
10256 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10257 int common = 0;
10258
58393038
ZL
10259 /* True means types are compatible as far as ObjC is concerned. */
10260 bool objc_ok;
10261
8ce94e44
JM
10262 /* True means this is an arithmetic operation that may need excess
10263 precision. */
10264 bool may_need_excess_precision;
10265
180f8dbb
JM
10266 /* True means this is a boolean operation that converts both its
10267 operands to truth-values. */
10268 bool boolean_op = false;
10269
de5a5fa1
MP
10270 /* Remember whether we're doing / or %. */
10271 bool doing_div_or_mod = false;
10272
10273 /* Remember whether we're doing << or >>. */
10274 bool doing_shift = false;
10275
10276 /* Tree holding instrumentation expression. */
10277 tree instrument_expr = NULL;
10278
ba47d38d
AH
10279 if (location == UNKNOWN_LOCATION)
10280 location = input_location;
10281
4d84fe7c
JM
10282 op0 = orig_op0;
10283 op1 = orig_op1;
10284
10285 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10286 if (op0_int_operands)
10287 op0 = remove_c_maybe_const_expr (op0);
10288 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10289 if (op1_int_operands)
10290 op1 = remove_c_maybe_const_expr (op1);
10291 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10292 if (int_operands)
10293 {
10294 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10295 && TREE_CODE (orig_op1) == INTEGER_CST);
10296 int_const = (int_const_or_overflow
10297 && !TREE_OVERFLOW (orig_op0)
10298 && !TREE_OVERFLOW (orig_op1));
10299 }
10300 else
10301 int_const = int_const_or_overflow = false;
10302
0e3a99ae 10303 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
10304 if (convert_p
10305 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 10306 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 10307 {
4d84fe7c
JM
10308 op0 = default_conversion (op0);
10309 op1 = default_conversion (op1);
790e9490
RS
10310 }
10311
36536d79
BI
10312 /* When Cilk Plus is enabled and there are array notations inside op0, then
10313 we check to see if there are builtin array notation functions. If
10314 so, then we take on the type of the array notation inside it. */
b72271b9 10315 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10316 orig_type0 = type0 = find_correct_array_notation_type (op0);
10317 else
10318 orig_type0 = type0 = TREE_TYPE (op0);
10319
b72271b9 10320 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10321 orig_type1 = type1 = find_correct_array_notation_type (op1);
10322 else
10323 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10324
3e4093b6
RS
10325 /* The expression codes of the data types of the arguments tell us
10326 whether the arguments are integers, floating, pointers, etc. */
10327 code0 = TREE_CODE (type0);
10328 code1 = TREE_CODE (type1);
10329
10330 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10331 STRIP_TYPE_NOPS (op0);
10332 STRIP_TYPE_NOPS (op1);
10333
10334 /* If an error was already reported for one of the arguments,
10335 avoid reporting another error. */
10336
10337 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10338 return error_mark_node;
10339
4de67c26
JM
10340 if ((invalid_op_diag
10341 = targetm.invalid_binary_op (code, type0, type1)))
10342 {
ba47d38d 10343 error_at (location, invalid_op_diag);
4de67c26
JM
10344 return error_mark_node;
10345 }
10346
8ce94e44
JM
10347 switch (code)
10348 {
10349 case PLUS_EXPR:
10350 case MINUS_EXPR:
10351 case MULT_EXPR:
10352 case TRUNC_DIV_EXPR:
10353 case CEIL_DIV_EXPR:
10354 case FLOOR_DIV_EXPR:
10355 case ROUND_DIV_EXPR:
10356 case EXACT_DIV_EXPR:
10357 may_need_excess_precision = true;
10358 break;
10359 default:
10360 may_need_excess_precision = false;
10361 break;
10362 }
10363 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10364 {
10365 op0 = TREE_OPERAND (op0, 0);
10366 type0 = TREE_TYPE (op0);
10367 }
10368 else if (may_need_excess_precision
10369 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10370 {
10371 type0 = eptype;
10372 op0 = convert (eptype, op0);
10373 }
10374 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10375 {
10376 op1 = TREE_OPERAND (op1, 0);
10377 type1 = TREE_TYPE (op1);
10378 }
10379 else if (may_need_excess_precision
10380 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10381 {
10382 type1 = eptype;
10383 op1 = convert (eptype, op1);
10384 }
10385
58393038
ZL
10386 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10387
0e3a99ae
AS
10388 /* In case when one of the operands of the binary operation is
10389 a vector and another is a scalar -- convert scalar to vector. */
10390 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10391 {
a212e43f
MG
10392 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10393 true);
f90e8e2e 10394
0e3a99ae
AS
10395 switch (convert_flag)
10396 {
10397 case stv_error:
10398 return error_mark_node;
10399 case stv_firstarg:
10400 {
10401 bool maybe_const = true;
10402 tree sc;
10403 sc = c_fully_fold (op0, false, &maybe_const);
10404 sc = save_expr (sc);
10405 sc = convert (TREE_TYPE (type1), sc);
10406 op0 = build_vector_from_val (type1, sc);
10407 if (!maybe_const)
10408 op0 = c_wrap_maybe_const (op0, true);
10409 orig_type0 = type0 = TREE_TYPE (op0);
10410 code0 = TREE_CODE (type0);
10411 converted = 1;
10412 break;
10413 }
10414 case stv_secondarg:
10415 {
10416 bool maybe_const = true;
10417 tree sc;
10418 sc = c_fully_fold (op1, false, &maybe_const);
10419 sc = save_expr (sc);
10420 sc = convert (TREE_TYPE (type0), sc);
10421 op1 = build_vector_from_val (type0, sc);
10422 if (!maybe_const)
54b9f838 10423 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10424 orig_type1 = type1 = TREE_TYPE (op1);
10425 code1 = TREE_CODE (type1);
10426 converted = 1;
10427 break;
10428 }
10429 default:
10430 break;
10431 }
10432 }
10433
3e4093b6 10434 switch (code)
de520661 10435 {
3e4093b6
RS
10436 case PLUS_EXPR:
10437 /* Handle the pointer + int case. */
10438 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10439 {
db3927fb 10440 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10441 goto return_build_binary_op;
10442 }
3e4093b6 10443 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10444 {
db3927fb 10445 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10446 goto return_build_binary_op;
10447 }
fe67cf58 10448 else
3e4093b6
RS
10449 common = 1;
10450 break;
400fbf9f 10451
3e4093b6
RS
10452 case MINUS_EXPR:
10453 /* Subtraction of two similar pointers.
10454 We must subtract them as integers, then divide by object size. */
10455 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10456 && comp_target_types (location, type0, type1))
c9f9eb5d 10457 {
db3927fb 10458 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10459 goto return_build_binary_op;
10460 }
3e4093b6
RS
10461 /* Handle pointer minus int. Just like pointer plus int. */
10462 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10463 {
db3927fb 10464 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10465 goto return_build_binary_op;
10466 }
3e4093b6
RS
10467 else
10468 common = 1;
10469 break;
8b6a5902 10470
3e4093b6
RS
10471 case MULT_EXPR:
10472 common = 1;
10473 break;
10474
10475 case TRUNC_DIV_EXPR:
10476 case CEIL_DIV_EXPR:
10477 case FLOOR_DIV_EXPR:
10478 case ROUND_DIV_EXPR:
10479 case EXACT_DIV_EXPR:
de5a5fa1 10480 doing_div_or_mod = true;
c9f9eb5d 10481 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10482
10483 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10484 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10485 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10486 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10487 || code1 == FIXED_POINT_TYPE
3e4093b6 10488 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10489 {
5bed876a
AH
10490 enum tree_code tcode0 = code0, tcode1 = code1;
10491
3a021db2 10492 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10493 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10494 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10495 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10496
ab22c1fa
CF
10497 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10498 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10499 resultcode = RDIV_EXPR;
10500 else
10501 /* Although it would be tempting to shorten always here, that
10502 loses on some targets, since the modulo instruction is
10503 undefined if the quotient can't be represented in the
10504 computation mode. We shorten only if unsigned or if
10505 dividing by something we know != -1. */
8df83eae 10506 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10507 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10508 && !integer_all_onesp (op1)));
3e4093b6
RS
10509 common = 1;
10510 }
10511 break;
de520661 10512
3e4093b6 10513 case BIT_AND_EXPR:
3e4093b6
RS
10514 case BIT_IOR_EXPR:
10515 case BIT_XOR_EXPR:
10516 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10517 shorten = -1;
9ef0c8d9
AP
10518 /* Allow vector types which are not floating point types. */
10519 else if (code0 == VECTOR_TYPE
10520 && code1 == VECTOR_TYPE
10521 && !VECTOR_FLOAT_TYPE_P (type0)
10522 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10523 common = 1;
10524 break;
10525
10526 case TRUNC_MOD_EXPR:
10527 case FLOOR_MOD_EXPR:
de5a5fa1 10528 doing_div_or_mod = true;
c9f9eb5d 10529 warn_for_div_by_zero (location, op1);
de520661 10530
5cfd5d9b
AP
10531 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10532 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10533 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10534 common = 1;
10535 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10536 {
10537 /* Although it would be tempting to shorten always here, that loses
10538 on some targets, since the modulo instruction is undefined if the
10539 quotient can't be represented in the computation mode. We shorten
10540 only if unsigned or if dividing by something we know != -1. */
8df83eae 10541 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10542 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10543 && !integer_all_onesp (op1)));
3e4093b6
RS
10544 common = 1;
10545 }
10546 break;
de520661 10547
3e4093b6
RS
10548 case TRUTH_ANDIF_EXPR:
10549 case TRUTH_ORIF_EXPR:
10550 case TRUTH_AND_EXPR:
10551 case TRUTH_OR_EXPR:
10552 case TRUTH_XOR_EXPR:
10553 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10554 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10555 || code0 == FIXED_POINT_TYPE)
3e4093b6 10556 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10557 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10558 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10559 {
10560 /* Result of these operations is always an int,
10561 but that does not mean the operands should be
10562 converted to ints! */
10563 result_type = integer_type_node;
a27d595d
JM
10564 if (op0_int_operands)
10565 {
10566 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10567 op0 = remove_c_maybe_const_expr (op0);
10568 }
10569 else
10570 op0 = c_objc_common_truthvalue_conversion (location, op0);
10571 if (op1_int_operands)
10572 {
10573 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10574 op1 = remove_c_maybe_const_expr (op1);
10575 }
10576 else
10577 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10578 converted = 1;
180f8dbb 10579 boolean_op = true;
3e4093b6 10580 }
928c19bb
JM
10581 if (code == TRUTH_ANDIF_EXPR)
10582 {
10583 int_const_or_overflow = (int_operands
10584 && TREE_CODE (orig_op0) == INTEGER_CST
10585 && (op0 == truthvalue_false_node
10586 || TREE_CODE (orig_op1) == INTEGER_CST));
10587 int_const = (int_const_or_overflow
10588 && !TREE_OVERFLOW (orig_op0)
10589 && (op0 == truthvalue_false_node
10590 || !TREE_OVERFLOW (orig_op1)));
10591 }
10592 else if (code == TRUTH_ORIF_EXPR)
10593 {
10594 int_const_or_overflow = (int_operands
10595 && TREE_CODE (orig_op0) == INTEGER_CST
10596 && (op0 == truthvalue_true_node
10597 || TREE_CODE (orig_op1) == INTEGER_CST));
10598 int_const = (int_const_or_overflow
10599 && !TREE_OVERFLOW (orig_op0)
10600 && (op0 == truthvalue_true_node
10601 || !TREE_OVERFLOW (orig_op1)));
10602 }
3e4093b6 10603 break;
eba80994 10604
3e4093b6
RS
10605 /* Shift operations: result has same type as first operand;
10606 always convert second operand to int.
10607 Also set SHORT_SHIFT if shifting rightward. */
de520661 10608
3e4093b6 10609 case RSHIFT_EXPR:
f87bd04b
AS
10610 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10611 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10612 {
10613 result_type = type0;
10614 converted = 1;
10615 }
10616 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10617 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10618 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10619 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10620 {
10621 result_type = type0;
10622 converted = 1;
10623 }
10624 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10625 && code1 == INTEGER_TYPE)
3e4093b6 10626 {
de5a5fa1 10627 doing_shift = true;
928c19bb 10628 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10629 {
3e4093b6 10630 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10631 {
10632 int_const = false;
7d882b83 10633 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10634 warning_at (location, OPT_Wshift_count_negative,
10635 "right shift count is negative");
928c19bb 10636 }
3e4093b6 10637 else
bbb818c6 10638 {
3f75a254 10639 if (!integer_zerop (op1))
3e4093b6
RS
10640 short_shift = 1;
10641
10642 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10643 {
10644 int_const = false;
7d882b83 10645 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10646 warning_at (location, OPT_Wshift_count_overflow,
10647 "right shift count >= width of type");
928c19bb 10648 }
bbb818c6 10649 }
b62acd60 10650 }
de520661 10651
3e4093b6
RS
10652 /* Use the type of the value to be shifted. */
10653 result_type = type0;
3e4093b6
RS
10654 /* Avoid converting op1 to result_type later. */
10655 converted = 1;
400fbf9f 10656 }
3e4093b6 10657 break;
253b6b82 10658
3e4093b6 10659 case LSHIFT_EXPR:
f87bd04b
AS
10660 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10661 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10662 {
10663 result_type = type0;
10664 converted = 1;
10665 }
10666 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10667 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10668 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10669 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10670 {
10671 result_type = type0;
10672 converted = 1;
10673 }
10674 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10675 && code1 == INTEGER_TYPE)
3e4093b6 10676 {
de5a5fa1 10677 doing_shift = true;
928c19bb 10678 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10679 {
3e4093b6 10680 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10681 {
10682 int_const = false;
7d882b83 10683 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10684 warning_at (location, OPT_Wshift_count_negative,
10685 "left shift count is negative");
928c19bb 10686 }
de520661 10687
3e4093b6 10688 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10689 {
10690 int_const = false;
7d882b83 10691 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10692 warning_at (location, OPT_Wshift_count_overflow,
10693 "left shift count >= width of type");
928c19bb 10694 }
94ba5069 10695 }
de520661 10696
3e4093b6
RS
10697 /* Use the type of the value to be shifted. */
10698 result_type = type0;
3e4093b6
RS
10699 /* Avoid converting op1 to result_type later. */
10700 converted = 1;
400fbf9f 10701 }
3e4093b6 10702 break;
de520661 10703
3e4093b6
RS
10704 case EQ_EXPR:
10705 case NE_EXPR:
d246ab4f
AS
10706 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10707 {
10708 tree intt;
0af94e6f 10709 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10710 {
10711 error_at (location, "comparing vectors with different "
10712 "element types");
10713 return error_mark_node;
10714 }
10715
10716 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10717 {
10718 error_at (location, "comparing vectors with different "
10719 "number of elements");
10720 return error_mark_node;
10721 }
10722
10723 /* Always construct signed integer vector type. */
10724 intt = c_common_type_for_size (GET_MODE_BITSIZE
10725 (TYPE_MODE (TREE_TYPE (type0))), 0);
10726 result_type = build_opaque_vector_type (intt,
10727 TYPE_VECTOR_SUBPARTS (type0));
10728 converted = 1;
10729 break;
10730 }
ae311566 10731 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10732 warning_at (location,
10733 OPT_Wfloat_equal,
10734 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10735 /* Result of comparison is always int,
10736 but don't convert the args to int! */
10737 build_type = integer_type_node;
10738 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10739 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10740 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10741 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10742 short_compare = 1;
637f1455
SZ
10743 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10744 {
10745 if (TREE_CODE (op0) == ADDR_EXPR
10746 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10747 {
10748 if (code == EQ_EXPR)
10749 warning_at (location,
10750 OPT_Waddress,
10751 "the comparison will always evaluate as %<false%> "
10752 "for the address of %qD will never be NULL",
10753 TREE_OPERAND (op0, 0));
10754 else
10755 warning_at (location,
10756 OPT_Waddress,
10757 "the comparison will always evaluate as %<true%> "
10758 "for the address of %qD will never be NULL",
10759 TREE_OPERAND (op0, 0));
10760 }
10761 result_type = type0;
10762 }
10763 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10764 {
10765 if (TREE_CODE (op1) == ADDR_EXPR
10766 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10767 {
10768 if (code == EQ_EXPR)
10769 warning_at (location,
f90e8e2e 10770 OPT_Waddress,
637f1455
SZ
10771 "the comparison will always evaluate as %<false%> "
10772 "for the address of %qD will never be NULL",
10773 TREE_OPERAND (op1, 0));
10774 else
10775 warning_at (location,
10776 OPT_Waddress,
10777 "the comparison will always evaluate as %<true%> "
10778 "for the address of %qD will never be NULL",
10779 TREE_OPERAND (op1, 0));
10780 }
10781 result_type = type1;
10782 }
3e4093b6
RS
10783 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10784 {
10785 tree tt0 = TREE_TYPE (type0);
10786 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10787 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10788 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10789 addr_space_t as_common = ADDR_SPACE_GENERIC;
10790
3e4093b6
RS
10791 /* Anything compares with void *. void * compares with anything.
10792 Otherwise, the targets must be compatible
10793 and both must be object or both incomplete. */
744aa42f 10794 if (comp_target_types (location, type0, type1))
10bc1b1b 10795 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10796 else if (!addr_space_superset (as0, as1, &as_common))
10797 {
10798 error_at (location, "comparison of pointers to "
10799 "disjoint address spaces");
10800 return error_mark_node;
10801 }
267bac10 10802 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10803 {
36c5e70a 10804 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10805 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10806 "comparison of %<void *%> with function pointer");
ee2990e7 10807 }
267bac10 10808 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10809 {
36c5e70a 10810 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10811 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10812 "comparison of %<void *%> with function pointer");
e6834654 10813 }
3e4093b6 10814 else
58393038
ZL
10815 /* Avoid warning about the volatile ObjC EH puts on decls. */
10816 if (!objc_ok)
ba47d38d 10817 pedwarn (location, 0,
509c9d60 10818 "comparison of distinct pointer types lacks a cast");
e6834654 10819
3e4093b6 10820 if (result_type == NULL_TREE)
36c5e70a
BE
10821 {
10822 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10823 result_type = build_pointer_type
10824 (build_qualified_type (void_type_node, qual));
10825 }
e6834654 10826 }
3e4093b6 10827 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10828 {
3e4093b6 10829 result_type = type0;
ba47d38d 10830 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10831 }
3e4093b6 10832 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10833 {
3e4093b6 10834 result_type = type1;
ba47d38d 10835 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10836 }
04159acf
MP
10837 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10838 || truth_value_p (TREE_CODE (orig_op0)))
10839 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10840 || truth_value_p (TREE_CODE (orig_op1))))
10841 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10842 break;
8b6a5902 10843
3e4093b6
RS
10844 case LE_EXPR:
10845 case GE_EXPR:
10846 case LT_EXPR:
10847 case GT_EXPR:
d246ab4f
AS
10848 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10849 {
10850 tree intt;
0af94e6f 10851 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10852 {
10853 error_at (location, "comparing vectors with different "
10854 "element types");
10855 return error_mark_node;
10856 }
10857
10858 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10859 {
10860 error_at (location, "comparing vectors with different "
10861 "number of elements");
10862 return error_mark_node;
10863 }
10864
10865 /* Always construct signed integer vector type. */
10866 intt = c_common_type_for_size (GET_MODE_BITSIZE
10867 (TYPE_MODE (TREE_TYPE (type0))), 0);
10868 result_type = build_opaque_vector_type (intt,
10869 TYPE_VECTOR_SUBPARTS (type0));
10870 converted = 1;
10871 break;
10872 }
3e4093b6 10873 build_type = integer_type_node;
ab22c1fa
CF
10874 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10875 || code0 == FIXED_POINT_TYPE)
10876 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10877 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10878 short_compare = 1;
10879 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10880 {
36c5e70a
BE
10881 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10882 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10883 addr_space_t as_common;
10884
744aa42f 10885 if (comp_target_types (location, type0, type1))
3e4093b6 10886 {
10bc1b1b 10887 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10888 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10889 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10890 pedwarn (location, 0,
509c9d60 10891 "comparison of complete and incomplete pointers");
fcf73884 10892 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10893 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10894 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10895 else if (null_pointer_constant_p (orig_op0)
10896 || null_pointer_constant_p (orig_op1))
10897 warning_at (location, OPT_Wextra,
10898 "ordered comparison of pointer with null pointer");
10899
3e4093b6 10900 }
36c5e70a
BE
10901 else if (!addr_space_superset (as0, as1, &as_common))
10902 {
10903 error_at (location, "comparison of pointers to "
10904 "disjoint address spaces");
10905 return error_mark_node;
10906 }
3e4093b6
RS
10907 else
10908 {
36c5e70a
BE
10909 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10910 result_type = build_pointer_type
10911 (build_qualified_type (void_type_node, qual));
ba47d38d 10912 pedwarn (location, 0,
509c9d60 10913 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10914 }
10915 }
6aa3c60d 10916 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10917 {
10918 result_type = type0;
fcf73884 10919 if (pedantic)
c1771a20 10920 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10921 "ordered comparison of pointer with integer zero");
10922 else if (extra_warnings)
ba47d38d 10923 warning_at (location, OPT_Wextra,
d42ba3b8 10924 "ordered comparison of pointer with integer zero");
3e4093b6 10925 }
6aa3c60d 10926 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10927 {
10928 result_type = type1;
d42ba3b8 10929 if (pedantic)
c1771a20 10930 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10931 "ordered comparison of pointer with integer zero");
10932 else if (extra_warnings)
10933 warning_at (location, OPT_Wextra,
10934 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10935 }
10936 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10937 {
10938 result_type = type0;
ba47d38d 10939 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10940 }
10941 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10942 {
10943 result_type = type1;
ba47d38d 10944 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 10945 }
04159acf
MP
10946 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10947 || truth_value_p (TREE_CODE (orig_op0)))
10948 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10949 || truth_value_p (TREE_CODE (orig_op1))))
10950 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10951 break;
64094f6a 10952
3e4093b6 10953 default:
37b2f290 10954 gcc_unreachable ();
c9fe6f9f 10955 }
8f17b5c5 10956
e57e265b
PB
10957 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10958 return error_mark_node;
10959
5bed876a
AH
10960 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10961 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10962 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10963 {
ba47d38d 10964 binary_op_error (location, code, type0, type1);
5bed876a
AH
10965 return error_mark_node;
10966 }
10967
3e4093b6 10968 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10969 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10970 &&
10971 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10972 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10973 {
2ca862e9
JM
10974 bool first_complex = (code0 == COMPLEX_TYPE);
10975 bool second_complex = (code1 == COMPLEX_TYPE);
10976 int none_complex = (!first_complex && !second_complex);
39b726dd 10977
3e4093b6 10978 if (shorten || common || short_compare)
3bf6bfcc
JJ
10979 {
10980 result_type = c_common_type (type0, type1);
0a0b3574
MM
10981 do_warn_double_promotion (result_type, type0, type1,
10982 "implicit conversion from %qT to %qT "
10983 "to match other operand of binary "
10984 "expression",
10985 location);
3bf6bfcc
JJ
10986 if (result_type == error_mark_node)
10987 return error_mark_node;
10988 }
400fbf9f 10989
2ca862e9
JM
10990 if (first_complex != second_complex
10991 && (code == PLUS_EXPR
10992 || code == MINUS_EXPR
10993 || code == MULT_EXPR
10994 || (code == TRUNC_DIV_EXPR && first_complex))
10995 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10996 && flag_signed_zeros)
10997 {
10998 /* An operation on mixed real/complex operands must be
10999 handled specially, but the language-independent code can
11000 more easily optimize the plain complex arithmetic if
11001 -fno-signed-zeros. */
11002 tree real_type = TREE_TYPE (result_type);
11003 tree real, imag;
11004 if (type0 != orig_type0 || type1 != orig_type1)
11005 {
11006 gcc_assert (may_need_excess_precision && common);
2d2e923f 11007 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11008 }
11009 if (first_complex)
11010 {
11011 if (TREE_TYPE (op0) != result_type)
68fca595 11012 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11013 if (TREE_TYPE (op1) != real_type)
68fca595 11014 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11015 }
11016 else
11017 {
11018 if (TREE_TYPE (op0) != real_type)
68fca595 11019 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11020 if (TREE_TYPE (op1) != result_type)
68fca595 11021 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11022 }
11023 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11024 return error_mark_node;
11025 if (first_complex)
11026 {
11027 op0 = c_save_expr (op0);
11028 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11029 op0, 1);
11030 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11031 op0, 1);
11032 switch (code)
11033 {
11034 case MULT_EXPR:
11035 case TRUNC_DIV_EXPR:
c4603e7c 11036 op1 = c_save_expr (op1);
2ca862e9
JM
11037 imag = build2 (resultcode, real_type, imag, op1);
11038 /* Fall through. */
11039 case PLUS_EXPR:
11040 case MINUS_EXPR:
11041 real = build2 (resultcode, real_type, real, op1);
11042 break;
11043 default:
11044 gcc_unreachable();
11045 }
11046 }
11047 else
11048 {
11049 op1 = c_save_expr (op1);
11050 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11051 op1, 1);
11052 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11053 op1, 1);
11054 switch (code)
11055 {
11056 case MULT_EXPR:
c4603e7c 11057 op0 = c_save_expr (op0);
2ca862e9
JM
11058 imag = build2 (resultcode, real_type, op0, imag);
11059 /* Fall through. */
11060 case PLUS_EXPR:
11061 real = build2 (resultcode, real_type, op0, real);
11062 break;
11063 case MINUS_EXPR:
11064 real = build2 (resultcode, real_type, op0, real);
11065 imag = build1 (NEGATE_EXPR, real_type, imag);
11066 break;
11067 default:
11068 gcc_unreachable();
11069 }
11070 }
11071 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11072 goto return_build_binary_op;
11073 }
11074
3e4093b6
RS
11075 /* For certain operations (which identify themselves by shorten != 0)
11076 if both args were extended from the same smaller type,
11077 do the arithmetic in that type and then extend.
400fbf9f 11078
3e4093b6
RS
11079 shorten !=0 and !=1 indicates a bitwise operation.
11080 For them, this optimization is safe only if
11081 both args are zero-extended or both are sign-extended.
11082 Otherwise, we might change the result.
11083 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11084 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11085
3e4093b6
RS
11086 if (shorten && none_complex)
11087 {
3e4093b6 11088 final_type = result_type;
b8698a0f 11089 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11090 shorten == -1);
3e4093b6 11091 }
88a3dbc1 11092
3e4093b6 11093 /* Shifts can be shortened if shifting right. */
2f6e4e97 11094
3e4093b6
RS
11095 if (short_shift)
11096 {
11097 int unsigned_arg;
11098 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11099
3e4093b6 11100 final_type = result_type;
abe80e6d 11101
3e4093b6 11102 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11103 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11104
3e4093b6 11105 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11106 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11107 /* We can shorten only if the shift count is less than the
11108 number of bits in the smaller type size. */
11109 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11110 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11111 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11112 {
11113 /* Do an unsigned shift if the operand was zero-extended. */
11114 result_type
11115 = c_common_signed_or_unsigned_type (unsigned_arg,
11116 TREE_TYPE (arg0));
11117 /* Convert value-to-be-shifted to that type. */
11118 if (TREE_TYPE (op0) != result_type)
11119 op0 = convert (result_type, op0);
11120 converted = 1;
abe80e6d 11121 }
88a3dbc1
RK
11122 }
11123
3e4093b6
RS
11124 /* Comparison operations are shortened too but differently.
11125 They identify themselves by setting short_compare = 1. */
56cb9733 11126
3e4093b6
RS
11127 if (short_compare)
11128 {
11129 /* Don't write &op0, etc., because that would prevent op0
11130 from being kept in a register.
11131 Instead, make copies of the our local variables and
11132 pass the copies by reference, then copy them back afterward. */
11133 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11134 enum tree_code xresultcode = resultcode;
11135 tree val
393e8e8b
MP
11136 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11137 &xresultcode);
8f17b5c5 11138
3e4093b6 11139 if (val != 0)
c9f9eb5d
AH
11140 {
11141 ret = val;
11142 goto return_build_binary_op;
11143 }
8f17b5c5 11144
3e4093b6
RS
11145 op0 = xop0, op1 = xop1;
11146 converted = 1;
11147 resultcode = xresultcode;
8f17b5c5 11148
7d882b83 11149 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11150 {
11151 bool op0_maybe_const = true;
11152 bool op1_maybe_const = true;
11153 tree orig_op0_folded, orig_op1_folded;
11154
11155 if (in_late_binary_op)
11156 {
11157 orig_op0_folded = orig_op0;
11158 orig_op1_folded = orig_op1;
11159 }
11160 else
11161 {
11162 /* Fold for the sake of possible warnings, as in
11163 build_conditional_expr. This requires the
11164 "original" values to be folded, not just op0 and
11165 op1. */
f5178456 11166 c_inhibit_evaluation_warnings++;
928c19bb
JM
11167 op0 = c_fully_fold (op0, require_constant_value,
11168 &op0_maybe_const);
11169 op1 = c_fully_fold (op1, require_constant_value,
11170 &op1_maybe_const);
f5178456 11171 c_inhibit_evaluation_warnings--;
928c19bb
JM
11172 orig_op0_folded = c_fully_fold (orig_op0,
11173 require_constant_value,
11174 NULL);
11175 orig_op1_folded = c_fully_fold (orig_op1,
11176 require_constant_value,
11177 NULL);
11178 }
11179
11180 if (warn_sign_compare)
11181 warn_for_sign_compare (location, orig_op0_folded,
11182 orig_op1_folded, op0, op1,
11183 result_type, resultcode);
5c2f94b4 11184 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11185 {
11186 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11187 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11188 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11189 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11190 }
3e4093b6 11191 }
2ad1815d 11192 }
64094f6a 11193 }
64094f6a 11194
3e4093b6
RS
11195 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11196 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11197 Then the expression will be built.
11198 It will be given type FINAL_TYPE if that is nonzero;
11199 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11200
3e4093b6
RS
11201 if (!result_type)
11202 {
ba47d38d 11203 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11204 return error_mark_node;
11205 }
400fbf9f 11206
3e4093b6 11207 if (build_type == NULL_TREE)
8ce94e44
JM
11208 {
11209 build_type = result_type;
180f8dbb
JM
11210 if ((type0 != orig_type0 || type1 != orig_type1)
11211 && !boolean_op)
8ce94e44
JM
11212 {
11213 gcc_assert (may_need_excess_precision && common);
2d2e923f 11214 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11215 }
11216 }
400fbf9f 11217
2d2e923f
MLI
11218 if (!converted)
11219 {
68fca595
MP
11220 op0 = ep_convert_and_check (location, result_type, op0,
11221 semantic_result_type);
11222 op1 = ep_convert_and_check (location, result_type, op1,
11223 semantic_result_type);
2d2e923f
MLI
11224
11225 /* This can happen if one operand has a vector type, and the other
11226 has a different type. */
11227 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11228 return error_mark_node;
11229 }
11230
f8ed5150
MP
11231 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11232 | SANITIZE_FLOAT_DIVIDE))
de5a5fa1 11233 && current_function_decl != 0
ce6923c5
MP
11234 && !lookup_attribute ("no_sanitize_undefined",
11235 DECL_ATTRIBUTES (current_function_decl))
de5a5fa1
MP
11236 && (doing_div_or_mod || doing_shift))
11237 {
11238 /* OP0 and/or OP1 might have side-effects. */
11239 op0 = c_save_expr (op0);
11240 op1 = c_save_expr (op1);
11241 op0 = c_fully_fold (op0, false, NULL);
11242 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11243 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11244 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11245 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11246 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11247 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11248 }
11249
c9f9eb5d 11250 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11251 if (int_const_or_overflow)
11252 ret = (require_constant_value
db3927fb
AH
11253 ? fold_build2_initializer_loc (location, resultcode, build_type,
11254 op0, op1)
11255 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11256 else
11257 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11258 if (final_type != 0)
11259 ret = convert (final_type, ret);
11260
11261 return_build_binary_op:
11262 gcc_assert (ret != error_mark_node);
928c19bb
JM
11263 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11264 ret = (int_operands
11265 ? note_integer_operands (ret)
11266 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11267 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11268 && !in_late_binary_op)
11269 ret = note_integer_operands (ret);
2d2e923f
MLI
11270 if (semantic_result_type)
11271 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11272 protected_set_expr_location (ret, location);
de5a5fa1 11273
a24d975c 11274 if (instrument_expr != NULL)
de5a5fa1
MP
11275 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11276 instrument_expr, ret);
11277
c9f9eb5d 11278 return ret;
400fbf9f 11279}
85498824
JM
11280
11281
11282/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11283 purpose. LOCATION is the source location for the expression. */
85498824
JM
11284
11285tree
ba47d38d 11286c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11287{
928c19bb
JM
11288 bool int_const, int_operands;
11289
85498824
JM
11290 switch (TREE_CODE (TREE_TYPE (expr)))
11291 {
11292 case ARRAY_TYPE:
ba47d38d 11293 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11294 return error_mark_node;
11295
11296 case RECORD_TYPE:
ba47d38d 11297 error_at (location, "used struct type value where scalar is required");
85498824
JM
11298 return error_mark_node;
11299
11300 case UNION_TYPE:
ba47d38d 11301 error_at (location, "used union type value where scalar is required");
85498824
JM
11302 return error_mark_node;
11303
04af8788
NP
11304 case VOID_TYPE:
11305 error_at (location, "void value not ignored as it ought to be");
11306 return error_mark_node;
11307
46bdb9cf
JM
11308 case FUNCTION_TYPE:
11309 gcc_unreachable ();
11310
d246ab4f
AS
11311 case VECTOR_TYPE:
11312 error_at (location, "used vector type where scalar is required");
11313 return error_mark_node;
11314
85498824
JM
11315 default:
11316 break;
11317 }
11318
928c19bb
JM
11319 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11320 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11321 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11322 {
11323 expr = remove_c_maybe_const_expr (expr);
11324 expr = build2 (NE_EXPR, integer_type_node, expr,
11325 convert (TREE_TYPE (expr), integer_zero_node));
11326 expr = note_integer_operands (expr);
11327 }
11328 else
11329 /* ??? Should we also give an error for vectors rather than leaving
11330 those to give errors later? */
11331 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11332
11333 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11334 {
11335 if (TREE_OVERFLOW (expr))
11336 return expr;
11337 else
11338 return note_integer_operands (expr);
11339 }
11340 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11341 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11342 return expr;
85498824 11343}
73f397d4
JM
11344\f
11345
11346/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11347 required. */
11348
11349tree
51eed280 11350c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11351{
11352 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11353 {
11354 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11355 /* Executing a compound literal inside a function reinitializes
11356 it. */
11357 if (!TREE_STATIC (decl))
11358 *se = true;
11359 return decl;
11360 }
11361 else
11362 return expr;
11363}
953ff289 11364\f
41dbbb37
TS
11365/* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11366 statement. LOC is the location of the OACC_PARALLEL. */
11367
11368tree
11369c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11370{
11371 tree stmt;
11372
11373 block = c_end_compound_stmt (loc, block, true);
11374
11375 stmt = make_node (OACC_PARALLEL);
11376 TREE_TYPE (stmt) = void_type_node;
11377 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11378 OACC_PARALLEL_BODY (stmt) = block;
11379 SET_EXPR_LOCATION (stmt, loc);
11380
11381 return add_stmt (stmt);
11382}
11383
11384/* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11385 statement. LOC is the location of the OACC_KERNELS. */
11386
11387tree
11388c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11389{
11390 tree stmt;
11391
11392 block = c_end_compound_stmt (loc, block, true);
11393
11394 stmt = make_node (OACC_KERNELS);
11395 TREE_TYPE (stmt) = void_type_node;
11396 OACC_KERNELS_CLAUSES (stmt) = clauses;
11397 OACC_KERNELS_BODY (stmt) = block;
11398 SET_EXPR_LOCATION (stmt, loc);
11399
11400 return add_stmt (stmt);
11401}
11402
11403/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11404 statement. LOC is the location of the OACC_DATA. */
11405
11406tree
11407c_finish_oacc_data (location_t loc, tree clauses, tree block)
11408{
11409 tree stmt;
11410
11411 block = c_end_compound_stmt (loc, block, true);
11412
11413 stmt = make_node (OACC_DATA);
11414 TREE_TYPE (stmt) = void_type_node;
11415 OACC_DATA_CLAUSES (stmt) = clauses;
11416 OACC_DATA_BODY (stmt) = block;
11417 SET_EXPR_LOCATION (stmt, loc);
11418
11419 return add_stmt (stmt);
11420}
11421
c0220ea4 11422/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11423
11424tree
11425c_begin_omp_parallel (void)
11426{
11427 tree block;
11428
11429 keep_next_level ();
11430 block = c_begin_compound_stmt (true);
11431
11432 return block;
11433}
11434
c2255bc4
AH
11435/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11436 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11437
953ff289 11438tree
c2255bc4 11439c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11440{
11441 tree stmt;
11442
c2255bc4 11443 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11444
11445 stmt = make_node (OMP_PARALLEL);
11446 TREE_TYPE (stmt) = void_type_node;
11447 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11448 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11449 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11450
11451 return add_stmt (stmt);
11452}
11453
a68ab351
JJ
11454/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11455
11456tree
11457c_begin_omp_task (void)
11458{
11459 tree block;
11460
11461 keep_next_level ();
11462 block = c_begin_compound_stmt (true);
11463
11464 return block;
11465}
11466
c2255bc4
AH
11467/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11468 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11469
11470tree
c2255bc4 11471c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11472{
11473 tree stmt;
11474
c2255bc4 11475 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11476
11477 stmt = make_node (OMP_TASK);
11478 TREE_TYPE (stmt) = void_type_node;
11479 OMP_TASK_CLAUSES (stmt) = clauses;
11480 OMP_TASK_BODY (stmt) = block;
c2255bc4 11481 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11482
11483 return add_stmt (stmt);
11484}
11485
acf0174b
JJ
11486/* Generate GOMP_cancel call for #pragma omp cancel. */
11487
11488void
11489c_finish_omp_cancel (location_t loc, tree clauses)
11490{
11491 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11492 int mask = 0;
11493 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11494 mask = 1;
11495 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11496 mask = 2;
11497 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11498 mask = 4;
11499 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11500 mask = 8;
11501 else
11502 {
11503 error_at (loc, "%<#pragma omp cancel must specify one of "
11504 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11505 "clauses");
11506 return;
11507 }
11508 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11509 if (ifc != NULL_TREE)
11510 {
11511 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11512 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11513 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11514 build_zero_cst (type));
11515 }
11516 else
11517 ifc = boolean_true_node;
11518 tree stmt = build_call_expr_loc (loc, fn, 2,
11519 build_int_cst (integer_type_node, mask),
11520 ifc);
11521 add_stmt (stmt);
11522}
11523
11524/* Generate GOMP_cancellation_point call for
11525 #pragma omp cancellation point. */
11526
11527void
11528c_finish_omp_cancellation_point (location_t loc, tree clauses)
11529{
11530 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11531 int mask = 0;
11532 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11533 mask = 1;
11534 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11535 mask = 2;
11536 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11537 mask = 4;
11538 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11539 mask = 8;
11540 else
11541 {
11542 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11543 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11544 "clauses");
11545 return;
11546 }
11547 tree stmt = build_call_expr_loc (loc, fn, 1,
11548 build_int_cst (integer_type_node, mask));
11549 add_stmt (stmt);
11550}
11551
11552/* Helper function for handle_omp_array_sections. Called recursively
11553 to handle multiple array-section-subscripts. C is the clause,
11554 T current expression (initially OMP_CLAUSE_DECL), which is either
11555 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11556 expression if specified, TREE_VALUE length expression if specified,
11557 TREE_CHAIN is what it has been specified after, or some decl.
11558 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11559 set to true if any of the array-section-subscript could have length
11560 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11561 first array-section-subscript which is known not to have length
11562 of one. Given say:
11563 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11564 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11565 all are or may have length of 1, array-section-subscript [:2] is the
11566 first one knonwn not to have length 1. For array-section-subscript
11567 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11568 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11569 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11570 case though, as some lengths could be zero. */
11571
11572static tree
11573handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11574 bool &maybe_zero_len, unsigned int &first_non_one)
11575{
11576 tree ret, low_bound, length, type;
11577 if (TREE_CODE (t) != TREE_LIST)
11578 {
7a0ca710 11579 if (error_operand_p (t))
acf0174b
JJ
11580 return error_mark_node;
11581 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11582 {
11583 if (DECL_P (t))
11584 error_at (OMP_CLAUSE_LOCATION (c),
11585 "%qD is not a variable in %qs clause", t,
11586 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11587 else
11588 error_at (OMP_CLAUSE_LOCATION (c),
11589 "%qE is not a variable in %qs clause", t,
11590 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11591 return error_mark_node;
11592 }
11593 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11594 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11595 {
11596 error_at (OMP_CLAUSE_LOCATION (c),
11597 "%qD is threadprivate variable in %qs clause", t,
11598 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11599 return error_mark_node;
11600 }
11601 return t;
11602 }
11603
11604 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11605 maybe_zero_len, first_non_one);
11606 if (ret == error_mark_node || ret == NULL_TREE)
11607 return ret;
11608
11609 type = TREE_TYPE (ret);
11610 low_bound = TREE_PURPOSE (t);
11611 length = TREE_VALUE (t);
11612
11613 if (low_bound == error_mark_node || length == error_mark_node)
11614 return error_mark_node;
11615
11616 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11617 {
11618 error_at (OMP_CLAUSE_LOCATION (c),
11619 "low bound %qE of array section does not have integral type",
11620 low_bound);
11621 return error_mark_node;
11622 }
11623 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11624 {
11625 error_at (OMP_CLAUSE_LOCATION (c),
11626 "length %qE of array section does not have integral type",
11627 length);
11628 return error_mark_node;
11629 }
11630 if (low_bound
11631 && TREE_CODE (low_bound) == INTEGER_CST
11632 && TYPE_PRECISION (TREE_TYPE (low_bound))
11633 > TYPE_PRECISION (sizetype))
11634 low_bound = fold_convert (sizetype, low_bound);
11635 if (length
11636 && TREE_CODE (length) == INTEGER_CST
11637 && TYPE_PRECISION (TREE_TYPE (length))
11638 > TYPE_PRECISION (sizetype))
11639 length = fold_convert (sizetype, length);
11640 if (low_bound == NULL_TREE)
11641 low_bound = integer_zero_node;
11642
11643 if (length != NULL_TREE)
11644 {
11645 if (!integer_nonzerop (length))
11646 maybe_zero_len = true;
11647 if (first_non_one == types.length ()
11648 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11649 first_non_one++;
11650 }
11651 if (TREE_CODE (type) == ARRAY_TYPE)
11652 {
11653 if (length == NULL_TREE
11654 && (TYPE_DOMAIN (type) == NULL_TREE
11655 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11656 {
11657 error_at (OMP_CLAUSE_LOCATION (c),
11658 "for unknown bound array type length expression must "
11659 "be specified");
11660 return error_mark_node;
11661 }
11662 if (TREE_CODE (low_bound) == INTEGER_CST
11663 && tree_int_cst_sgn (low_bound) == -1)
11664 {
11665 error_at (OMP_CLAUSE_LOCATION (c),
11666 "negative low bound in array section in %qs clause",
11667 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11668 return error_mark_node;
11669 }
11670 if (length != NULL_TREE
11671 && TREE_CODE (length) == INTEGER_CST
11672 && tree_int_cst_sgn (length) == -1)
11673 {
11674 error_at (OMP_CLAUSE_LOCATION (c),
11675 "negative length in array section in %qs clause",
11676 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11677 return error_mark_node;
11678 }
11679 if (TYPE_DOMAIN (type)
11680 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11681 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11682 == INTEGER_CST)
11683 {
11684 tree size = size_binop (PLUS_EXPR,
11685 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11686 size_one_node);
11687 if (TREE_CODE (low_bound) == INTEGER_CST)
11688 {
11689 if (tree_int_cst_lt (size, low_bound))
11690 {
11691 error_at (OMP_CLAUSE_LOCATION (c),
11692 "low bound %qE above array section size "
11693 "in %qs clause", low_bound,
11694 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11695 return error_mark_node;
11696 }
11697 if (tree_int_cst_equal (size, low_bound))
11698 maybe_zero_len = true;
11699 else if (length == NULL_TREE
11700 && first_non_one == types.length ()
11701 && tree_int_cst_equal
11702 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11703 low_bound))
11704 first_non_one++;
11705 }
11706 else if (length == NULL_TREE)
11707 {
11708 maybe_zero_len = true;
11709 if (first_non_one == types.length ())
11710 first_non_one++;
11711 }
11712 if (length && TREE_CODE (length) == INTEGER_CST)
11713 {
11714 if (tree_int_cst_lt (size, length))
11715 {
11716 error_at (OMP_CLAUSE_LOCATION (c),
11717 "length %qE above array section size "
11718 "in %qs clause", length,
11719 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11720 return error_mark_node;
11721 }
11722 if (TREE_CODE (low_bound) == INTEGER_CST)
11723 {
11724 tree lbpluslen
11725 = size_binop (PLUS_EXPR,
11726 fold_convert (sizetype, low_bound),
11727 fold_convert (sizetype, length));
11728 if (TREE_CODE (lbpluslen) == INTEGER_CST
11729 && tree_int_cst_lt (size, lbpluslen))
11730 {
11731 error_at (OMP_CLAUSE_LOCATION (c),
11732 "high bound %qE above array section size "
11733 "in %qs clause", lbpluslen,
11734 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11735 return error_mark_node;
11736 }
11737 }
11738 }
11739 }
11740 else if (length == NULL_TREE)
11741 {
11742 maybe_zero_len = true;
11743 if (first_non_one == types.length ())
11744 first_non_one++;
11745 }
11746
11747 /* For [lb:] we will need to evaluate lb more than once. */
11748 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11749 {
11750 tree lb = c_save_expr (low_bound);
11751 if (lb != low_bound)
11752 {
11753 TREE_PURPOSE (t) = lb;
11754 low_bound = lb;
11755 }
11756 }
11757 }
11758 else if (TREE_CODE (type) == POINTER_TYPE)
11759 {
11760 if (length == NULL_TREE)
11761 {
11762 error_at (OMP_CLAUSE_LOCATION (c),
11763 "for pointer type length expression must be specified");
11764 return error_mark_node;
11765 }
11766 /* If there is a pointer type anywhere but in the very first
11767 array-section-subscript, the array section can't be contiguous. */
11768 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11769 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11770 {
11771 error_at (OMP_CLAUSE_LOCATION (c),
11772 "array section is not contiguous in %qs clause",
11773 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11774 return error_mark_node;
11775 }
11776 }
11777 else
11778 {
11779 error_at (OMP_CLAUSE_LOCATION (c),
11780 "%qE does not have pointer or array type", ret);
11781 return error_mark_node;
11782 }
11783 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11784 types.safe_push (TREE_TYPE (ret));
11785 /* We will need to evaluate lb more than once. */
11786 tree lb = c_save_expr (low_bound);
11787 if (lb != low_bound)
11788 {
11789 TREE_PURPOSE (t) = lb;
11790 low_bound = lb;
11791 }
11792 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11793 return ret;
11794}
11795
11796/* Handle array sections for clause C. */
11797
11798static bool
11799handle_omp_array_sections (tree c)
11800{
11801 bool maybe_zero_len = false;
11802 unsigned int first_non_one = 0;
11803 vec<tree> types = vNULL;
11804 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11805 maybe_zero_len, first_non_one);
11806 if (first == error_mark_node)
11807 {
11808 types.release ();
11809 return true;
11810 }
11811 if (first == NULL_TREE)
11812 {
11813 types.release ();
11814 return false;
11815 }
11816 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11817 {
11818 tree t = OMP_CLAUSE_DECL (c);
11819 tree tem = NULL_TREE;
11820 types.release ();
11821 /* Need to evaluate side effects in the length expressions
11822 if any. */
11823 while (TREE_CODE (t) == TREE_LIST)
11824 {
11825 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11826 {
11827 if (tem == NULL_TREE)
11828 tem = TREE_VALUE (t);
11829 else
11830 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11831 TREE_VALUE (t), tem);
11832 }
11833 t = TREE_CHAIN (t);
11834 }
11835 if (tem)
11836 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11837 first = c_fully_fold (first, false, NULL);
11838 OMP_CLAUSE_DECL (c) = first;
11839 }
11840 else
11841 {
11842 unsigned int num = types.length (), i;
11843 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11844 tree condition = NULL_TREE;
11845
11846 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11847 maybe_zero_len = true;
11848
11849 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11850 t = TREE_CHAIN (t))
11851 {
11852 tree low_bound = TREE_PURPOSE (t);
11853 tree length = TREE_VALUE (t);
11854
11855 i--;
11856 if (low_bound
11857 && TREE_CODE (low_bound) == INTEGER_CST
11858 && TYPE_PRECISION (TREE_TYPE (low_bound))
11859 > TYPE_PRECISION (sizetype))
11860 low_bound = fold_convert (sizetype, low_bound);
11861 if (length
11862 && TREE_CODE (length) == INTEGER_CST
11863 && TYPE_PRECISION (TREE_TYPE (length))
11864 > TYPE_PRECISION (sizetype))
11865 length = fold_convert (sizetype, length);
11866 if (low_bound == NULL_TREE)
11867 low_bound = integer_zero_node;
11868 if (!maybe_zero_len && i > first_non_one)
11869 {
11870 if (integer_nonzerop (low_bound))
11871 goto do_warn_noncontiguous;
11872 if (length != NULL_TREE
11873 && TREE_CODE (length) == INTEGER_CST
11874 && TYPE_DOMAIN (types[i])
11875 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11876 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11877 == INTEGER_CST)
11878 {
11879 tree size;
11880 size = size_binop (PLUS_EXPR,
11881 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11882 size_one_node);
11883 if (!tree_int_cst_equal (length, size))
11884 {
11885 do_warn_noncontiguous:
11886 error_at (OMP_CLAUSE_LOCATION (c),
11887 "array section is not contiguous in %qs "
11888 "clause",
11889 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11890 types.release ();
11891 return true;
11892 }
11893 }
11894 if (length != NULL_TREE
11895 && TREE_SIDE_EFFECTS (length))
11896 {
11897 if (side_effects == NULL_TREE)
11898 side_effects = length;
11899 else
11900 side_effects = build2 (COMPOUND_EXPR,
11901 TREE_TYPE (side_effects),
11902 length, side_effects);
11903 }
11904 }
11905 else
11906 {
11907 tree l;
11908
11909 if (i > first_non_one && length && integer_nonzerop (length))
11910 continue;
11911 if (length)
11912 l = fold_convert (sizetype, length);
11913 else
11914 {
11915 l = size_binop (PLUS_EXPR,
11916 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11917 size_one_node);
11918 l = size_binop (MINUS_EXPR, l,
11919 fold_convert (sizetype, low_bound));
11920 }
11921 if (i > first_non_one)
11922 {
11923 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11924 size_zero_node);
11925 if (condition == NULL_TREE)
11926 condition = l;
11927 else
11928 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11929 l, condition);
11930 }
11931 else if (size == NULL_TREE)
11932 {
11933 size = size_in_bytes (TREE_TYPE (types[i]));
11934 size = size_binop (MULT_EXPR, size, l);
11935 if (condition)
11936 size = fold_build3 (COND_EXPR, sizetype, condition,
11937 size, size_zero_node);
11938 }
11939 else
11940 size = size_binop (MULT_EXPR, size, l);
11941 }
11942 }
11943 types.release ();
11944 if (side_effects)
11945 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11946 first = c_fully_fold (first, false, NULL);
11947 OMP_CLAUSE_DECL (c) = first;
11948 if (size)
11949 size = c_fully_fold (size, false, NULL);
11950 OMP_CLAUSE_SIZE (c) = size;
11951 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11952 return false;
41dbbb37 11953 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
acf0174b 11954 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
41dbbb37 11955 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
acf0174b
JJ
11956 if (!c_mark_addressable (t))
11957 return false;
11958 OMP_CLAUSE_DECL (c2) = t;
11959 t = build_fold_addr_expr (first);
11960 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11961 tree ptr = OMP_CLAUSE_DECL (c2);
11962 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11963 ptr = build_fold_addr_expr (ptr);
11964 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11965 ptrdiff_type_node, t,
11966 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11967 ptrdiff_type_node, ptr));
11968 t = c_fully_fold (t, false, NULL);
11969 OMP_CLAUSE_SIZE (c2) = t;
11970 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11971 OMP_CLAUSE_CHAIN (c) = c2;
11972 }
11973 return false;
11974}
11975
11976/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11977 an inline call. But, remap
11978 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11979 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11980
11981static tree
11982c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11983 tree decl, tree placeholder)
11984{
11985 copy_body_data id;
b787e7a2 11986 hash_map<tree, tree> decl_map;
acf0174b 11987
b787e7a2
TS
11988 decl_map.put (omp_decl1, placeholder);
11989 decl_map.put (omp_decl2, decl);
acf0174b
JJ
11990 memset (&id, 0, sizeof (id));
11991 id.src_fn = DECL_CONTEXT (omp_decl1);
11992 id.dst_fn = current_function_decl;
11993 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 11994 id.decl_map = &decl_map;
acf0174b
JJ
11995
11996 id.copy_decl = copy_decl_no_change;
11997 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11998 id.transform_new_cfg = true;
11999 id.transform_return_to_modify = false;
12000 id.transform_lang_insert_block = NULL;
12001 id.eh_lp_nr = 0;
12002 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12003 return stmt;
12004}
12005
12006/* Helper function of c_finish_omp_clauses, called via walk_tree.
12007 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12008
12009static tree
12010c_find_omp_placeholder_r (tree *tp, int *, void *data)
12011{
12012 if (*tp == (tree) data)
12013 return *tp;
12014 return NULL_TREE;
12015}
12016
41dbbb37 12017/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12018 Remove any elements from the list that are invalid. */
12019
12020tree
12021c_finish_omp_clauses (tree clauses)
12022{
12023 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 12024 bitmap_head aligned_head;
f3316c6d 12025 tree c, t, *pc;
acf0174b
JJ
12026 bool branch_seen = false;
12027 bool copyprivate_seen = false;
12028 tree *nowait_clause = NULL;
953ff289
DN
12029
12030 bitmap_obstack_initialize (NULL);
12031 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12032 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12033 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12034 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
12035
12036 for (pc = &clauses, c = clauses; c ; c = *pc)
12037 {
12038 bool remove = false;
12039 bool need_complete = false;
12040 bool need_implicitly_determined = false;
12041
aaf46ef9 12042 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12043 {
12044 case OMP_CLAUSE_SHARED:
953ff289
DN
12045 need_implicitly_determined = true;
12046 goto check_dup_generic;
12047
12048 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12049 need_complete = true;
12050 need_implicitly_determined = true;
12051 goto check_dup_generic;
12052
12053 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12054 need_implicitly_determined = true;
12055 t = OMP_CLAUSE_DECL (c);
acf0174b 12056 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
652fea39
JJ
12057 && (FLOAT_TYPE_P (TREE_TYPE (t))
12058 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
953ff289
DN
12059 {
12060 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12061 const char *r_name = NULL;
12062
12063 switch (r_code)
12064 {
12065 case PLUS_EXPR:
12066 case MULT_EXPR:
12067 case MINUS_EXPR:
652fea39 12068 break;
20906c66 12069 case MIN_EXPR:
652fea39
JJ
12070 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12071 r_name = "min";
12072 break;
20906c66 12073 case MAX_EXPR:
652fea39
JJ
12074 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12075 r_name = "max";
953ff289
DN
12076 break;
12077 case BIT_AND_EXPR:
12078 r_name = "&";
12079 break;
12080 case BIT_XOR_EXPR:
12081 r_name = "^";
12082 break;
12083 case BIT_IOR_EXPR:
12084 r_name = "|";
12085 break;
12086 case TRUTH_ANDIF_EXPR:
652fea39
JJ
12087 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12088 r_name = "&&";
953ff289
DN
12089 break;
12090 case TRUTH_ORIF_EXPR:
652fea39
JJ
12091 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12092 r_name = "||";
953ff289
DN
12093 break;
12094 default:
12095 gcc_unreachable ();
12096 }
12097 if (r_name)
12098 {
c2255bc4
AH
12099 error_at (OMP_CLAUSE_LOCATION (c),
12100 "%qE has invalid type for %<reduction(%s)%>",
12101 t, r_name);
953ff289 12102 remove = true;
ee1d5a02 12103 break;
953ff289
DN
12104 }
12105 }
acf0174b
JJ
12106 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12107 {
12108 error_at (OMP_CLAUSE_LOCATION (c),
12109 "user defined reduction not found for %qD", t);
12110 remove = true;
ee1d5a02 12111 break;
acf0174b
JJ
12112 }
12113 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12114 {
12115 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12116 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12117 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12118 VAR_DECL, NULL_TREE, type);
12119 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12120 DECL_ARTIFICIAL (placeholder) = 1;
12121 DECL_IGNORED_P (placeholder) = 1;
12122 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12123 c_mark_addressable (placeholder);
12124 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12125 c_mark_addressable (OMP_CLAUSE_DECL (c));
12126 OMP_CLAUSE_REDUCTION_MERGE (c)
12127 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12128 TREE_VEC_ELT (list, 0),
12129 TREE_VEC_ELT (list, 1),
12130 OMP_CLAUSE_DECL (c), placeholder);
12131 OMP_CLAUSE_REDUCTION_MERGE (c)
12132 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12133 void_type_node, NULL_TREE,
12134 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12135 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12136 if (TREE_VEC_LENGTH (list) == 6)
12137 {
12138 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12139 c_mark_addressable (OMP_CLAUSE_DECL (c));
12140 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12141 c_mark_addressable (placeholder);
12142 tree init = TREE_VEC_ELT (list, 5);
12143 if (init == error_mark_node)
12144 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12145 OMP_CLAUSE_REDUCTION_INIT (c)
12146 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12147 TREE_VEC_ELT (list, 3),
12148 OMP_CLAUSE_DECL (c), placeholder);
12149 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12150 OMP_CLAUSE_REDUCTION_INIT (c)
12151 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12152 OMP_CLAUSE_REDUCTION_INIT (c));
12153 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12154 c_find_omp_placeholder_r,
12155 placeholder, NULL))
12156 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12157 }
12158 else
12159 {
12160 tree init;
12161 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12162 init = build_constructor (TREE_TYPE (t), NULL);
12163 else
12164 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12165 OMP_CLAUSE_REDUCTION_INIT (c)
12166 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12167 }
12168 OMP_CLAUSE_REDUCTION_INIT (c)
12169 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12170 void_type_node, NULL_TREE,
12171 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12172 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12173 }
953ff289
DN
12174 goto check_dup_generic;
12175
12176 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12177 copyprivate_seen = true;
12178 if (nowait_clause)
12179 {
12180 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12181 "%<nowait%> clause must not be used together "
12182 "with %<copyprivate%>");
12183 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12184 nowait_clause = NULL;
12185 }
953ff289
DN
12186 goto check_dup_generic;
12187
12188 case OMP_CLAUSE_COPYIN:
953ff289
DN
12189 t = OMP_CLAUSE_DECL (c);
12190 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12191 {
c2255bc4
AH
12192 error_at (OMP_CLAUSE_LOCATION (c),
12193 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12194 remove = true;
ee1d5a02 12195 break;
953ff289
DN
12196 }
12197 goto check_dup_generic;
12198
acf0174b
JJ
12199 case OMP_CLAUSE_LINEAR:
12200 t = OMP_CLAUSE_DECL (c);
12201 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12202 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12203 {
12204 error_at (OMP_CLAUSE_LOCATION (c),
12205 "linear clause applied to non-integral non-pointer "
12206 "variable with type %qT", TREE_TYPE (t));
12207 remove = true;
12208 break;
12209 }
12210 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12211 {
12212 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12213 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12214 OMP_CLAUSE_DECL (c), s);
12215 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12216 sizetype, s, OMP_CLAUSE_DECL (c));
12217 if (s == error_mark_node)
12218 s = size_one_node;
12219 OMP_CLAUSE_LINEAR_STEP (c) = s;
12220 }
da6f124d
JJ
12221 else
12222 OMP_CLAUSE_LINEAR_STEP (c)
12223 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12224 goto check_dup_generic;
12225
953ff289
DN
12226 check_dup_generic:
12227 t = OMP_CLAUSE_DECL (c);
12228 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12229 {
c2255bc4 12230 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12231 "%qE is not a variable in clause %qs", t,
12232 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12233 remove = true;
12234 }
12235 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12236 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12237 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12238 {
c2255bc4
AH
12239 error_at (OMP_CLAUSE_LOCATION (c),
12240 "%qE appears more than once in data clauses", t);
953ff289
DN
12241 remove = true;
12242 }
12243 else
12244 bitmap_set_bit (&generic_head, DECL_UID (t));
12245 break;
12246
12247 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12248 t = OMP_CLAUSE_DECL (c);
12249 need_complete = true;
12250 need_implicitly_determined = true;
12251 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12252 {
c2255bc4
AH
12253 error_at (OMP_CLAUSE_LOCATION (c),
12254 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12255 remove = true;
12256 }
12257 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12258 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12259 {
c2255bc4
AH
12260 error_at (OMP_CLAUSE_LOCATION (c),
12261 "%qE appears more than once in data clauses", t);
953ff289
DN
12262 remove = true;
12263 }
12264 else
12265 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12266 break;
12267
12268 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12269 t = OMP_CLAUSE_DECL (c);
12270 need_complete = true;
12271 need_implicitly_determined = true;
12272 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12273 {
c2255bc4
AH
12274 error_at (OMP_CLAUSE_LOCATION (c),
12275 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12276 remove = true;
12277 }
12278 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12279 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12280 {
c2255bc4
AH
12281 error_at (OMP_CLAUSE_LOCATION (c),
12282 "%qE appears more than once in data clauses", t);
953ff289
DN
12283 remove = true;
12284 }
12285 else
12286 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12287 break;
12288
acf0174b
JJ
12289 case OMP_CLAUSE_ALIGNED:
12290 t = OMP_CLAUSE_DECL (c);
12291 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12292 {
12293 error_at (OMP_CLAUSE_LOCATION (c),
12294 "%qE is not a variable in %<aligned%> clause", t);
12295 remove = true;
12296 }
5a9785fb
JJ
12297 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12298 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12299 {
12300 error_at (OMP_CLAUSE_LOCATION (c),
12301 "%qE in %<aligned%> clause is neither a pointer nor "
12302 "an array", t);
12303 remove = true;
12304 }
acf0174b
JJ
12305 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12306 {
12307 error_at (OMP_CLAUSE_LOCATION (c),
12308 "%qE appears more than once in %<aligned%> clauses",
12309 t);
12310 remove = true;
12311 }
12312 else
12313 bitmap_set_bit (&aligned_head, DECL_UID (t));
12314 break;
12315
12316 case OMP_CLAUSE_DEPEND:
12317 t = OMP_CLAUSE_DECL (c);
12318 if (TREE_CODE (t) == TREE_LIST)
12319 {
12320 if (handle_omp_array_sections (c))
12321 remove = true;
12322 break;
12323 }
12324 if (t == error_mark_node)
12325 remove = true;
12326 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12327 {
12328 error_at (OMP_CLAUSE_LOCATION (c),
12329 "%qE is not a variable in %<depend%> clause", t);
12330 remove = true;
12331 }
12332 else if (!c_mark_addressable (t))
12333 remove = true;
12334 break;
12335
12336 case OMP_CLAUSE_MAP:
12337 case OMP_CLAUSE_TO:
12338 case OMP_CLAUSE_FROM:
41dbbb37 12339 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
12340 t = OMP_CLAUSE_DECL (c);
12341 if (TREE_CODE (t) == TREE_LIST)
12342 {
12343 if (handle_omp_array_sections (c))
12344 remove = true;
12345 else
12346 {
12347 t = OMP_CLAUSE_DECL (c);
b17a8b07 12348 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12349 {
12350 error_at (OMP_CLAUSE_LOCATION (c),
12351 "array section does not have mappable type "
12352 "in %qs clause",
12353 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12354 remove = true;
12355 }
12356 }
12357 break;
12358 }
12359 if (t == error_mark_node)
12360 remove = true;
12361 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12362 {
12363 error_at (OMP_CLAUSE_LOCATION (c),
12364 "%qE is not a variable in %qs clause", t,
12365 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12366 remove = true;
12367 }
12368 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12369 {
12370 error_at (OMP_CLAUSE_LOCATION (c),
12371 "%qD is threadprivate variable in %qs clause", t,
12372 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12373 remove = true;
12374 }
12375 else if (!c_mark_addressable (t))
12376 remove = true;
b17a8b07 12377 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37
TS
12378 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12379 || (OMP_CLAUSE_MAP_KIND (c)
12380 == GOMP_MAP_FORCE_DEVICEPTR)))
b17a8b07 12381 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12382 {
12383 error_at (OMP_CLAUSE_LOCATION (c),
12384 "%qD does not have a mappable type in %qs clause", t,
12385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12386 remove = true;
12387 }
12388 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12389 {
12390 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12391 error ("%qD appears more than once in motion clauses", t);
12392 else
12393 error ("%qD appears more than once in map clauses", t);
12394 remove = true;
12395 }
12396 else
12397 bitmap_set_bit (&generic_head, DECL_UID (t));
12398 break;
12399
12400 case OMP_CLAUSE_UNIFORM:
12401 t = OMP_CLAUSE_DECL (c);
12402 if (TREE_CODE (t) != PARM_DECL)
12403 {
12404 if (DECL_P (t))
12405 error_at (OMP_CLAUSE_LOCATION (c),
12406 "%qD is not an argument in %<uniform%> clause", t);
12407 else
12408 error_at (OMP_CLAUSE_LOCATION (c),
12409 "%qE is not an argument in %<uniform%> clause", t);
12410 remove = true;
ee1d5a02 12411 break;
acf0174b 12412 }
ee1d5a02 12413 goto check_dup_generic;
acf0174b
JJ
12414
12415 case OMP_CLAUSE_NOWAIT:
12416 if (copyprivate_seen)
12417 {
12418 error_at (OMP_CLAUSE_LOCATION (c),
12419 "%<nowait%> clause must not be used together "
12420 "with %<copyprivate%>");
12421 remove = true;
12422 break;
12423 }
12424 nowait_clause = pc;
12425 pc = &OMP_CLAUSE_CHAIN (c);
12426 continue;
12427
953ff289
DN
12428 case OMP_CLAUSE_IF:
12429 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12430 case OMP_CLAUSE_NUM_TEAMS:
12431 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12432 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12433 case OMP_CLAUSE_ORDERED:
12434 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12435 case OMP_CLAUSE_UNTIED:
12436 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12437 case OMP_CLAUSE_FINAL:
12438 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12439 case OMP_CLAUSE_SAFELEN:
12440 case OMP_CLAUSE_SIMDLEN:
12441 case OMP_CLAUSE_DEVICE:
12442 case OMP_CLAUSE_DIST_SCHEDULE:
12443 case OMP_CLAUSE_PARALLEL:
12444 case OMP_CLAUSE_FOR:
12445 case OMP_CLAUSE_SECTIONS:
12446 case OMP_CLAUSE_TASKGROUP:
12447 case OMP_CLAUSE_PROC_BIND:
9a771876 12448 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
12449 case OMP_CLAUSE_NUM_GANGS:
12450 case OMP_CLAUSE_NUM_WORKERS:
12451 case OMP_CLAUSE_VECTOR_LENGTH:
12452 case OMP_CLAUSE_ASYNC:
12453 case OMP_CLAUSE_WAIT:
12454 case OMP_CLAUSE_AUTO:
12455 case OMP_CLAUSE_SEQ:
12456 case OMP_CLAUSE_GANG:
12457 case OMP_CLAUSE_WORKER:
12458 case OMP_CLAUSE_VECTOR:
acf0174b
JJ
12459 pc = &OMP_CLAUSE_CHAIN (c);
12460 continue;
12461
12462 case OMP_CLAUSE_INBRANCH:
12463 case OMP_CLAUSE_NOTINBRANCH:
12464 if (branch_seen)
12465 {
12466 error_at (OMP_CLAUSE_LOCATION (c),
12467 "%<inbranch%> clause is incompatible with "
12468 "%<notinbranch%>");
12469 remove = true;
12470 break;
12471 }
12472 branch_seen = true;
953ff289
DN
12473 pc = &OMP_CLAUSE_CHAIN (c);
12474 continue;
12475
12476 default:
12477 gcc_unreachable ();
12478 }
12479
12480 if (!remove)
12481 {
12482 t = OMP_CLAUSE_DECL (c);
12483
12484 if (need_complete)
12485 {
12486 t = require_complete_type (t);
12487 if (t == error_mark_node)
12488 remove = true;
12489 }
12490
12491 if (need_implicitly_determined)
12492 {
12493 const char *share_name = NULL;
12494
12495 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12496 share_name = "threadprivate";
12497 else switch (c_omp_predetermined_sharing (t))
12498 {
12499 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12500 break;
12501 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12502 /* const vars may be specified in firstprivate clause. */
12503 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12504 && TREE_READONLY (t))
12505 break;
953ff289
DN
12506 share_name = "shared";
12507 break;
12508 case OMP_CLAUSE_DEFAULT_PRIVATE:
12509 share_name = "private";
12510 break;
12511 default:
12512 gcc_unreachable ();
12513 }
12514 if (share_name)
12515 {
c2255bc4
AH
12516 error_at (OMP_CLAUSE_LOCATION (c),
12517 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12518 t, share_name,
12519 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12520 remove = true;
12521 }
12522 }
12523 }
12524
12525 if (remove)
12526 *pc = OMP_CLAUSE_CHAIN (c);
12527 else
12528 pc = &OMP_CLAUSE_CHAIN (c);
12529 }
12530
12531 bitmap_obstack_release (NULL);
12532 return clauses;
12533}
9ae165a0 12534
0a35513e
AH
12535/* Create a transaction node. */
12536
12537tree
12538c_finish_transaction (location_t loc, tree block, int flags)
12539{
12540 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12541 if (flags & TM_STMT_ATTR_OUTER)
12542 TRANSACTION_EXPR_OUTER (stmt) = 1;
12543 if (flags & TM_STMT_ATTR_RELAXED)
12544 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12545 return add_stmt (stmt);
12546}
12547
9ae165a0
DG
12548/* Make a variant type in the proper way for C/C++, propagating qualifiers
12549 down to the element type of an array. */
12550
12551tree
12552c_build_qualified_type (tree type, int type_quals)
12553{
12554 if (type == error_mark_node)
12555 return type;
12556
12557 if (TREE_CODE (type) == ARRAY_TYPE)
12558 {
12559 tree t;
12560 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12561 type_quals);
12562
12563 /* See if we already have an identically qualified type. */
12564 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12565 {
12566 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12567 && TYPE_NAME (t) == TYPE_NAME (type)
12568 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12569 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12570 TYPE_ATTRIBUTES (type)))
12571 break;
12572 }
12573 if (!t)
12574 {
12575 tree domain = TYPE_DOMAIN (type);
12576
12577 t = build_variant_type_copy (type);
12578 TREE_TYPE (t) = element_type;
12579
12580 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12581 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12582 SET_TYPE_STRUCTURAL_EQUALITY (t);
12583 else if (TYPE_CANONICAL (element_type) != element_type
12584 || (domain && TYPE_CANONICAL (domain) != domain))
12585 {
b8698a0f 12586 tree unqualified_canon
9ae165a0 12587 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12588 domain? TYPE_CANONICAL (domain)
9ae165a0 12589 : NULL_TREE);
b8698a0f 12590 TYPE_CANONICAL (t)
9ae165a0
DG
12591 = c_build_qualified_type (unqualified_canon, type_quals);
12592 }
12593 else
12594 TYPE_CANONICAL (t) = t;
12595 }
12596 return t;
12597 }
12598
12599 /* A restrict-qualified pointer type must be a pointer to object or
12600 incomplete type. Note that the use of POINTER_TYPE_P also allows
12601 REFERENCE_TYPEs, which is appropriate for C++. */
12602 if ((type_quals & TYPE_QUAL_RESTRICT)
12603 && (!POINTER_TYPE_P (type)
12604 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12605 {
12606 error ("invalid use of %<restrict%>");
12607 type_quals &= ~TYPE_QUAL_RESTRICT;
12608 }
12609
12610 return build_qualified_type (type, type_quals);
12611}
72b5577d
ILT
12612
12613/* Build a VA_ARG_EXPR for the C parser. */
12614
12615tree
c2255bc4 12616c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d
ILT
12617{
12618 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12619 warning_at (loc, OPT_Wc___compat,
12620 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12621 return build_va_arg (loc, expr, type);
72b5577d 12622}
acf0174b
JJ
12623
12624/* Return truthvalue of whether T1 is the same tree structure as T2.
12625 Return 1 if they are the same. Return 0 if they are different. */
12626
12627bool
12628c_tree_equal (tree t1, tree t2)
12629{
12630 enum tree_code code1, code2;
12631
12632 if (t1 == t2)
12633 return true;
12634 if (!t1 || !t2)
12635 return false;
12636
12637 for (code1 = TREE_CODE (t1);
12638 CONVERT_EXPR_CODE_P (code1)
12639 || code1 == NON_LVALUE_EXPR;
12640 code1 = TREE_CODE (t1))
12641 t1 = TREE_OPERAND (t1, 0);
12642 for (code2 = TREE_CODE (t2);
12643 CONVERT_EXPR_CODE_P (code2)
12644 || code2 == NON_LVALUE_EXPR;
12645 code2 = TREE_CODE (t2))
12646 t2 = TREE_OPERAND (t2, 0);
12647
12648 /* They might have become equal now. */
12649 if (t1 == t2)
12650 return true;
12651
12652 if (code1 != code2)
12653 return false;
12654
12655 switch (code1)
12656 {
12657 case INTEGER_CST:
807e902e 12658 return wi::eq_p (t1, t2);
acf0174b
JJ
12659
12660 case REAL_CST:
12661 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12662
12663 case STRING_CST:
12664 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12665 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12666 TREE_STRING_LENGTH (t1));
12667
12668 case FIXED_CST:
12669 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12670 TREE_FIXED_CST (t2));
12671
12672 case COMPLEX_CST:
12673 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12674 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12675
12676 case VECTOR_CST:
12677 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12678
12679 case CONSTRUCTOR:
12680 /* We need to do this when determining whether or not two
12681 non-type pointer to member function template arguments
12682 are the same. */
12683 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12684 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12685 return false;
12686 {
12687 tree field, value;
12688 unsigned int i;
12689 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12690 {
12691 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12692 if (!c_tree_equal (field, elt2->index)
12693 || !c_tree_equal (value, elt2->value))
12694 return false;
12695 }
12696 }
12697 return true;
12698
12699 case TREE_LIST:
12700 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12701 return false;
12702 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12703 return false;
12704 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12705
12706 case SAVE_EXPR:
12707 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12708
12709 case CALL_EXPR:
12710 {
12711 tree arg1, arg2;
12712 call_expr_arg_iterator iter1, iter2;
12713 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12714 return false;
12715 for (arg1 = first_call_expr_arg (t1, &iter1),
12716 arg2 = first_call_expr_arg (t2, &iter2);
12717 arg1 && arg2;
12718 arg1 = next_call_expr_arg (&iter1),
12719 arg2 = next_call_expr_arg (&iter2))
12720 if (!c_tree_equal (arg1, arg2))
12721 return false;
12722 if (arg1 || arg2)
12723 return false;
12724 return true;
12725 }
12726
12727 case TARGET_EXPR:
12728 {
12729 tree o1 = TREE_OPERAND (t1, 0);
12730 tree o2 = TREE_OPERAND (t2, 0);
12731
12732 /* Special case: if either target is an unallocated VAR_DECL,
12733 it means that it's going to be unified with whatever the
12734 TARGET_EXPR is really supposed to initialize, so treat it
12735 as being equivalent to anything. */
12736 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12737 && !DECL_RTL_SET_P (o1))
12738 /*Nop*/;
12739 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12740 && !DECL_RTL_SET_P (o2))
12741 /*Nop*/;
12742 else if (!c_tree_equal (o1, o2))
12743 return false;
12744
12745 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12746 }
12747
12748 case COMPONENT_REF:
12749 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12750 return false;
12751 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12752
12753 case PARM_DECL:
12754 case VAR_DECL:
12755 case CONST_DECL:
12756 case FIELD_DECL:
12757 case FUNCTION_DECL:
12758 case IDENTIFIER_NODE:
12759 case SSA_NAME:
12760 return false;
12761
12762 case TREE_VEC:
12763 {
12764 unsigned ix;
12765 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12766 return false;
12767 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12768 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12769 TREE_VEC_ELT (t2, ix)))
12770 return false;
12771 return true;
12772 }
12773
12774 default:
12775 break;
12776 }
12777
12778 switch (TREE_CODE_CLASS (code1))
12779 {
12780 case tcc_unary:
12781 case tcc_binary:
12782 case tcc_comparison:
12783 case tcc_expression:
12784 case tcc_vl_exp:
12785 case tcc_reference:
12786 case tcc_statement:
12787 {
12788 int i, n = TREE_OPERAND_LENGTH (t1);
12789
12790 switch (code1)
12791 {
12792 case PREINCREMENT_EXPR:
12793 case PREDECREMENT_EXPR:
12794 case POSTINCREMENT_EXPR:
12795 case POSTDECREMENT_EXPR:
12796 n = 1;
12797 break;
12798 case ARRAY_REF:
12799 n = 2;
12800 break;
12801 default:
12802 break;
12803 }
12804
12805 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12806 && n != TREE_OPERAND_LENGTH (t2))
12807 return false;
12808
12809 for (i = 0; i < n; ++i)
12810 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12811 return false;
12812
12813 return true;
12814 }
12815
12816 case tcc_type:
12817 return comptypes (t1, t2);
12818 default:
12819 gcc_unreachable ();
12820 }
12821 /* We can get here with --disable-checking. */
12822 return false;
12823}
12893402
BI
12824
12825/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12826 spawn-helper and BODY is the newly created body for FNDECL. */
12827
12828void
12829cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12830{
12831 tree list = alloc_stmt_list ();
12832 tree frame = make_cilk_frame (fndecl);
12833 tree dtor = create_cilk_function_exit (frame, false, true);
12834 add_local_decl (cfun, frame);
12835
12836 DECL_SAVED_TREE (fndecl) = list;
12837 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12838 frame);
12839 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12840 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12841
12842 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12843 append_to_statement_list (detach_expr, &body_list);
12844
12845 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12846 body = fold_build_cleanup_point_expr (void_type_node, body);
12847
12848 append_to_statement_list (body, &body_list);
12849 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12850 body_list, dtor), &list);
12851}