]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
re PR c/59871 (No unused value warning for comma expression)
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
23a5b65a 2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6
ZW
28#include "coretypes.h"
29#include "tm.h"
400fbf9f 30#include "tree.h"
d8a2d370
DN
31#include "stor-layout.h"
32#include "trans-mem.h"
33#include "varasm.h"
34#include "stmt.h"
e57e265b 35#include "langhooks.h"
400fbf9f 36#include "c-tree.h"
29cc57cf 37#include "c-lang.h"
400fbf9f 38#include "flags.h"
ab87f8c8 39#include "intl.h"
672a6f42 40#include "target.h"
325c3691 41#include "tree-iterator.h"
6662d794 42#include "bitmap.h"
2fb9a547
AM
43#include "pointer-set.h"
44#include "basic-block.h"
45#include "gimple-expr.h"
45b0be94 46#include "gimplify.h"
acf0174b 47#include "tree-inline.h"
0645c1a2 48#include "omp-low.h"
61d3ce20 49#include "c-family/c-objc.h"
a212e43f 50#include "c-family/c-common.h"
de5a5fa1 51#include "c-family/c-ubsan.h"
12893402 52#include "cilk.h"
325c3691 53
2ac2f164
JM
54/* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
56enum impl_conv {
57 ic_argpass,
58 ic_assign,
59 ic_init,
60 ic_return
61};
62
bc4b653b
JM
63/* The level of nesting inside "__alignof__". */
64int in_alignof;
65
66/* The level of nesting inside "sizeof". */
67int in_sizeof;
68
69/* The level of nesting inside "typeof". */
70int in_typeof;
400fbf9f 71
1a4049e7
JJ
72/* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74tree c_last_sizeof_arg;
75
b71c7f8a 76/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 77 message within this initializer. */
b71c7f8a 78static int missing_braces_mentioned;
103b7b17 79
bf730f15
RS
80static int require_constant_value;
81static int require_constant_elements;
82
58f9752a 83static bool null_pointer_constant_p (const_tree);
f55ade6e 84static tree qualify_type (tree, tree);
dc5027f4
JM
85static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
86 bool *);
744aa42f 87static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
88static int function_types_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 91static tree lookup_field (tree, tree);
9771b263
DN
92static int convert_arguments (tree, vec<tree, va_gc> *, vec<tree, va_gc> *,
93 tree, tree);
db3927fb 94static tree pointer_diff (location_t, tree, tree);
744aa42f
ILT
95static tree convert_for_assignment (location_t, tree, tree, tree,
96 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
97static tree valid_compound_expr_initializer (tree, tree);
98static void push_string (const char *);
99static void push_member_name (tree);
f55ade6e
AJ
100static int spelling_length (void);
101static char *print_spelling (char *);
683d6ff9 102static void warning_init (int, const char *);
c2255bc4 103static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
a1e3b3d9
LB
104static void output_init_element (tree, tree, bool, tree, tree, int, bool,
105 struct obstack *);
106static void output_pending_init_elements (int, struct obstack *);
107static int set_designator (int, struct obstack *);
108static void push_range_stack (tree, struct obstack *);
109static void add_pending_init (tree, tree, tree, bool, struct obstack *);
110static void set_nonincremental_init (struct obstack *);
111static void set_nonincremental_init_from_string (tree, struct obstack *);
112static tree find_init_member (tree, struct obstack *);
f37acdf9 113static void readonly_warning (tree, enum lvalue_use);
7bd11157 114static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 115static void record_maybe_used_decl (tree);
dc5027f4 116static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
117\f
118/* Return true if EXP is a null pointer constant, false otherwise. */
119
120static bool
58f9752a 121null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
122{
123 /* This should really operate on c_expr structures, but they aren't
124 yet available everywhere required. */
125 tree type = TREE_TYPE (expr);
126 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 127 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
128 && integer_zerop (expr)
129 && (INTEGRAL_TYPE_P (type)
130 || (TREE_CODE (type) == POINTER_TYPE
131 && VOID_TYPE_P (TREE_TYPE (type))
132 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
133}
928c19bb
JM
134
135/* EXPR may appear in an unevaluated part of an integer constant
136 expression, but not in an evaluated part. Wrap it in a
137 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
138 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
139
140static tree
141note_integer_operands (tree expr)
142{
143 tree ret;
144 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
145 {
146 ret = copy_node (expr);
147 TREE_OVERFLOW (ret) = 1;
148 }
149 else
150 {
151 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
152 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
153 }
154 return ret;
155}
156
4d84fe7c
JM
157/* Having checked whether EXPR may appear in an unevaluated part of an
158 integer constant expression and found that it may, remove any
159 C_MAYBE_CONST_EXPR noting this fact and return the resulting
160 expression. */
161
162static inline tree
163remove_c_maybe_const_expr (tree expr)
164{
165 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
166 return C_MAYBE_CONST_EXPR_EXPR (expr);
167 else
168 return expr;
169}
170
f13c9b2c
AP
171\f/* This is a cache to hold if two types are compatible or not. */
172
173struct tagged_tu_seen_cache {
174 const struct tagged_tu_seen_cache * next;
58f9752a
KG
175 const_tree t1;
176 const_tree t2;
f13c9b2c
AP
177 /* The return value of tagged_types_tu_compatible_p if we had seen
178 these two types already. */
179 int val;
180};
181
182static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
183static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
184
400fbf9f
JW
185/* Do `exp = require_complete_type (exp);' to make sure exp
186 does not have an incomplete type. (That includes void types.) */
187
188tree
2f6e4e97 189require_complete_type (tree value)
400fbf9f
JW
190{
191 tree type = TREE_TYPE (value);
192
c3d5c3fa 193 if (value == error_mark_node || type == error_mark_node)
ea0f786b
CB
194 return error_mark_node;
195
400fbf9f 196 /* First, detect a valid value with a complete type. */
d0f062fb 197 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
198 return value;
199
7a228918 200 c_incomplete_type_error (value, type);
400fbf9f
JW
201 return error_mark_node;
202}
203
204/* Print an error message for invalid use of an incomplete type.
205 VALUE is the expression that was used (or 0 if that isn't known)
206 and TYPE is the type that was invalid. */
207
208void
ac7d7749 209c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 210{
5d5993dd 211 const char *type_code_string;
400fbf9f
JW
212
213 /* Avoid duplicate error message. */
214 if (TREE_CODE (type) == ERROR_MARK)
215 return;
216
217 if (value != 0 && (TREE_CODE (value) == VAR_DECL
218 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 219 error ("%qD has an incomplete type", value);
400fbf9f
JW
220 else
221 {
222 retry:
223 /* We must print an error message. Be clever about what it says. */
224
225 switch (TREE_CODE (type))
226 {
227 case RECORD_TYPE:
ab87f8c8 228 type_code_string = "struct";
400fbf9f
JW
229 break;
230
231 case UNION_TYPE:
ab87f8c8 232 type_code_string = "union";
400fbf9f
JW
233 break;
234
235 case ENUMERAL_TYPE:
ab87f8c8 236 type_code_string = "enum";
400fbf9f
JW
237 break;
238
239 case VOID_TYPE:
240 error ("invalid use of void expression");
241 return;
242
243 case ARRAY_TYPE:
244 if (TYPE_DOMAIN (type))
245 {
fba78abb
RH
246 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
247 {
248 error ("invalid use of flexible array member");
249 return;
250 }
400fbf9f
JW
251 type = TREE_TYPE (type);
252 goto retry;
253 }
254 error ("invalid use of array with unspecified bounds");
255 return;
256
257 default:
366de0ce 258 gcc_unreachable ();
400fbf9f
JW
259 }
260
261 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
262 error ("invalid use of undefined type %<%s %E%>",
263 type_code_string, TYPE_NAME (type));
400fbf9f
JW
264 else
265 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 266 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
267 }
268}
269
ab393bf1
NB
270/* Given a type, apply default promotions wrt unnamed function
271 arguments and return the new type. */
272
273tree
2f6e4e97 274c_type_promotes_to (tree type)
ab393bf1 275{
267bac10 276 tree ret = NULL_TREE;
ab393bf1 277
267bac10
JM
278 if (TYPE_MAIN_VARIANT (type) == float_type_node)
279 ret = double_type_node;
280 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
281 {
282 /* Preserve unsignedness if not really getting any wider. */
8df83eae 283 if (TYPE_UNSIGNED (type)
c22cacf3 284 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
285 ret = unsigned_type_node;
286 else
287 ret = integer_type_node;
ab393bf1
NB
288 }
289
267bac10
JM
290 if (ret != NULL_TREE)
291 return (TYPE_ATOMIC (type)
292 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
293 : ret);
294
ab393bf1
NB
295 return type;
296}
297
36c5e70a
BE
298/* Return true if between two named address spaces, whether there is a superset
299 named address space that encompasses both address spaces. If there is a
300 superset, return which address space is the superset. */
301
302static bool
303addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
304{
305 if (as1 == as2)
306 {
307 *common = as1;
308 return true;
309 }
310 else if (targetm.addr_space.subset_p (as1, as2))
311 {
312 *common = as2;
313 return true;
314 }
315 else if (targetm.addr_space.subset_p (as2, as1))
316 {
317 *common = as1;
318 return true;
319 }
320 else
321 return false;
322}
323
400fbf9f
JW
324/* Return a variant of TYPE which has all the type qualifiers of LIKE
325 as well as those of TYPE. */
326
327static tree
2f6e4e97 328qualify_type (tree type, tree like)
400fbf9f 329{
36c5e70a
BE
330 addr_space_t as_type = TYPE_ADDR_SPACE (type);
331 addr_space_t as_like = TYPE_ADDR_SPACE (like);
332 addr_space_t as_common;
333
334 /* If the two named address spaces are different, determine the common
335 superset address space. If there isn't one, raise an error. */
336 if (!addr_space_superset (as_type, as_like, &as_common))
337 {
338 as_common = as_type;
339 error ("%qT and %qT are in disjoint named address spaces",
340 type, like);
341 }
342
2f6e4e97 343 return c_build_qualified_type (type,
36c5e70a 344 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 345 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 346 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 347}
52ffd86e
MS
348
349/* Return true iff the given tree T is a variable length array. */
350
351bool
ac7d7749 352c_vla_type_p (const_tree t)
52ffd86e
MS
353{
354 if (TREE_CODE (t) == ARRAY_TYPE
355 && C_TYPE_VARIABLE_SIZE (t))
356 return true;
357 return false;
358}
400fbf9f 359\f
10bc1b1b 360/* Return the composite type of two compatible types.
5305f6d7 361
10bc1b1b
JM
362 We assume that comptypes has already been done and returned
363 nonzero; if that isn't so, this may crash. In particular, we
364 assume that qualifiers match. */
400fbf9f
JW
365
366tree
10bc1b1b 367composite_type (tree t1, tree t2)
400fbf9f 368{
b3694847
SS
369 enum tree_code code1;
370 enum tree_code code2;
4b027d16 371 tree attributes;
400fbf9f
JW
372
373 /* Save time if the two types are the same. */
374
375 if (t1 == t2) return t1;
376
377 /* If one type is nonsense, use the other. */
378 if (t1 == error_mark_node)
379 return t2;
380 if (t2 == error_mark_node)
381 return t1;
382
10bc1b1b
JM
383 code1 = TREE_CODE (t1);
384 code2 = TREE_CODE (t2);
385
d9525bec 386 /* Merge the attributes. */
5fd9b178 387 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 388
10bc1b1b
JM
389 /* If one is an enumerated type and the other is the compatible
390 integer type, the composite type might be either of the two
391 (DR#013 question 3). For consistency, use the enumerated type as
392 the composite type. */
400fbf9f 393
10bc1b1b
JM
394 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
395 return t1;
396 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
397 return t2;
75326e8c 398
366de0ce 399 gcc_assert (code1 == code2);
b6a10c9f 400
400fbf9f
JW
401 switch (code1)
402 {
400fbf9f 403 case POINTER_TYPE:
10bc1b1b 404 /* For two pointers, do this recursively on the target type. */
400fbf9f 405 {
3932261a
MM
406 tree pointed_to_1 = TREE_TYPE (t1);
407 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 408 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 409 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
410 t1 = build_type_attribute_variant (t1, attributes);
411 return qualify_type (t1, t2);
400fbf9f 412 }
400fbf9f
JW
413
414 case ARRAY_TYPE:
415 {
10bc1b1b 416 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
417 int quals;
418 tree unqual_elt;
ca8bdb78
JM
419 tree d1 = TYPE_DOMAIN (t1);
420 tree d2 = TYPE_DOMAIN (t2);
421 bool d1_variable, d2_variable;
422 bool d1_zero, d2_zero;
f6294de7 423 bool t1_complete, t2_complete;
46df2823 424
de46b2fe 425 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
426 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
427 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 428
f6294de7
JM
429 t1_complete = COMPLETE_TYPE_P (t1);
430 t2_complete = COMPLETE_TYPE_P (t2);
431
ca8bdb78
JM
432 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
433 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
434
435 d1_variable = (!d1_zero
436 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
437 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
438 d2_variable = (!d2_zero
439 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
440 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
441 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
442 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 443
400fbf9f 444 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
445 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
446 && (d2_variable || d2_zero || !d1_variable))
4b027d16 447 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
448 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
449 && (d1_variable || d1_zero || !d2_variable))
4b027d16 450 return build_type_attribute_variant (t2, attributes);
c22cacf3 451
de46b2fe
AP
452 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
453 return build_type_attribute_variant (t1, attributes);
454 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
455 return build_type_attribute_variant (t2, attributes);
c22cacf3 456
46df2823
JM
457 /* Merge the element types, and have a size if either arg has
458 one. We may have qualifiers on the element types. To set
459 up TYPE_MAIN_VARIANT correctly, we need to form the
460 composite of the unqualified types and add the qualifiers
461 back at the end. */
462 quals = TYPE_QUALS (strip_array_types (elt));
463 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
464 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
465 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
466 && (d2_variable
467 || d2_zero
468 || !d1_variable))
469 ? t1
470 : t2));
f6294de7
JM
471 /* Ensure a composite type involving a zero-length array type
472 is a zero-length type not an incomplete type. */
473 if (d1_zero && d2_zero
474 && (t1_complete || t2_complete)
475 && !COMPLETE_TYPE_P (t1))
476 {
477 TYPE_SIZE (t1) = bitsize_zero_node;
478 TYPE_SIZE_UNIT (t1) = size_zero_node;
479 }
46df2823 480 t1 = c_build_qualified_type (t1, quals);
de46b2fe 481 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
482 }
483
fcb99e7b
JJ
484 case ENUMERAL_TYPE:
485 case RECORD_TYPE:
486 case UNION_TYPE:
487 if (attributes != NULL)
488 {
489 /* Try harder not to create a new aggregate type. */
490 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
491 return t1;
492 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
493 return t2;
494 }
495 return build_type_attribute_variant (t1, attributes);
496
400fbf9f
JW
497 case FUNCTION_TYPE:
498 /* Function types: prefer the one that specified arg types.
499 If both do, merge the arg types. Also merge the return types. */
500 {
10bc1b1b 501 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
502 tree p1 = TYPE_ARG_TYPES (t1);
503 tree p2 = TYPE_ARG_TYPES (t2);
504 int len;
505 tree newargs, n;
506 int i;
507
508 /* Save space: see if the result is identical to one of the args. */
3f75a254 509 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 510 return build_type_attribute_variant (t1, attributes);
3f75a254 511 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 512 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
513
514 /* Simple way if one arg fails to specify argument types. */
515 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 516 {
fe7080d2
AP
517 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
518 t1 = build_type_attribute_variant (t1, attributes);
519 return qualify_type (t1, t2);
4b027d16 520 }
400fbf9f 521 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
522 {
523 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
524 t1 = build_type_attribute_variant (t1, attributes);
525 return qualify_type (t1, t2);
4b027d16 526 }
400fbf9f
JW
527
528 /* If both args specify argument types, we must merge the two
529 lists, argument by argument. */
2f4e8f2b 530
400fbf9f
JW
531 len = list_length (p1);
532 newargs = 0;
533
534 for (i = 0; i < len; i++)
8d9bfdc5 535 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
536
537 n = newargs;
538
539 for (; p1;
540 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
541 {
542 /* A null type means arg type is not specified.
543 Take whatever the other function type has. */
544 if (TREE_VALUE (p1) == 0)
545 {
546 TREE_VALUE (n) = TREE_VALUE (p2);
547 goto parm_done;
548 }
549 if (TREE_VALUE (p2) == 0)
550 {
551 TREE_VALUE (n) = TREE_VALUE (p1);
552 goto parm_done;
553 }
2f6e4e97 554
400fbf9f
JW
555 /* Given wait (union {union wait *u; int *i} *)
556 and wait (union wait *),
557 prefer union wait * as type of parm. */
558 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
559 && TREE_VALUE (p1) != TREE_VALUE (p2))
560 {
561 tree memb;
58cb41e6
JJ
562 tree mv2 = TREE_VALUE (p2);
563 if (mv2 && mv2 != error_mark_node
564 && TREE_CODE (mv2) != ARRAY_TYPE)
565 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 566 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 567 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
568 {
569 tree mv3 = TREE_TYPE (memb);
570 if (mv3 && mv3 != error_mark_node
571 && TREE_CODE (mv3) != ARRAY_TYPE)
572 mv3 = TYPE_MAIN_VARIANT (mv3);
573 if (comptypes (mv3, mv2))
574 {
575 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
576 TREE_VALUE (p2));
c1771a20 577 pedwarn (input_location, OPT_Wpedantic,
fcf73884 578 "function types not truly compatible in ISO C");
58cb41e6
JJ
579 goto parm_done;
580 }
581 }
400fbf9f
JW
582 }
583 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
584 && TREE_VALUE (p2) != TREE_VALUE (p1))
585 {
586 tree memb;
58cb41e6
JJ
587 tree mv1 = TREE_VALUE (p1);
588 if (mv1 && mv1 != error_mark_node
589 && TREE_CODE (mv1) != ARRAY_TYPE)
590 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 591 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 592 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
593 {
594 tree mv3 = TREE_TYPE (memb);
595 if (mv3 && mv3 != error_mark_node
596 && TREE_CODE (mv3) != ARRAY_TYPE)
597 mv3 = TYPE_MAIN_VARIANT (mv3);
598 if (comptypes (mv3, mv1))
599 {
600 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
601 TREE_VALUE (p1));
c1771a20 602 pedwarn (input_location, OPT_Wpedantic,
fcf73884 603 "function types not truly compatible in ISO C");
58cb41e6
JJ
604 goto parm_done;
605 }
606 }
400fbf9f 607 }
10bc1b1b 608 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
609 parm_done: ;
610 }
611
4b027d16 612 t1 = build_function_type (valtype, newargs);
fe7080d2 613 t1 = qualify_type (t1, t2);
0f41302f 614 /* ... falls through ... */
400fbf9f
JW
615 }
616
617 default:
4b027d16 618 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
619 }
620
621}
10bc1b1b
JM
622
623/* Return the type of a conditional expression between pointers to
624 possibly differently qualified versions of compatible types.
625
626 We assume that comp_target_types has already been done and returned
627 nonzero; if that isn't so, this may crash. */
628
629static tree
630common_pointer_type (tree t1, tree t2)
631{
632 tree attributes;
46df2823
JM
633 tree pointed_to_1, mv1;
634 tree pointed_to_2, mv2;
10bc1b1b 635 tree target;
eb1387a0 636 unsigned target_quals;
36c5e70a
BE
637 addr_space_t as1, as2, as_common;
638 int quals1, quals2;
10bc1b1b
JM
639
640 /* Save time if the two types are the same. */
641
642 if (t1 == t2) return t1;
643
644 /* If one type is nonsense, use the other. */
645 if (t1 == error_mark_node)
646 return t2;
647 if (t2 == error_mark_node)
648 return t1;
649
366de0ce 650 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 651 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
652
653 /* Merge the attributes. */
654 attributes = targetm.merge_type_attributes (t1, t2);
655
656 /* Find the composite type of the target types, and combine the
46df2823
JM
657 qualifiers of the two types' targets. Do not lose qualifiers on
658 array element types by taking the TYPE_MAIN_VARIANT. */
659 mv1 = pointed_to_1 = TREE_TYPE (t1);
660 mv2 = pointed_to_2 = TREE_TYPE (t2);
661 if (TREE_CODE (mv1) != ARRAY_TYPE)
662 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
663 if (TREE_CODE (mv2) != ARRAY_TYPE)
664 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
665 target = composite_type (mv1, mv2);
eb1387a0
RG
666
667 /* For function types do not merge const qualifiers, but drop them
668 if used inconsistently. The middle-end uses these to mark const
669 and noreturn functions. */
36c5e70a
BE
670 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
671 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
672
eb1387a0 673 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 674 target_quals = (quals1 & quals2);
eb1387a0 675 else
36c5e70a
BE
676 target_quals = (quals1 | quals2);
677
678 /* If the two named address spaces are different, determine the common
679 superset address space. This is guaranteed to exist due to the
680 assumption that comp_target_type returned non-zero. */
681 as1 = TYPE_ADDR_SPACE (pointed_to_1);
682 as2 = TYPE_ADDR_SPACE (pointed_to_2);
683 if (!addr_space_superset (as1, as2, &as_common))
684 gcc_unreachable ();
685
686 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
687
eb1387a0 688 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
689 return build_type_attribute_variant (t1, attributes);
690}
691
692/* Return the common type for two arithmetic types under the usual
693 arithmetic conversions. The default conversions have already been
694 applied, and enumerated types converted to their compatible integer
695 types. The resulting type is unqualified and has no attributes.
696
697 This is the type for the result of most arithmetic operations
698 if the operands have the given two types. */
699
ccf7f880
JJ
700static tree
701c_common_type (tree t1, tree t2)
10bc1b1b
JM
702{
703 enum tree_code code1;
704 enum tree_code code2;
705
706 /* If one type is nonsense, use the other. */
707 if (t1 == error_mark_node)
708 return t2;
709 if (t2 == error_mark_node)
710 return t1;
711
712 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
713 t1 = TYPE_MAIN_VARIANT (t1);
714
715 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
716 t2 = TYPE_MAIN_VARIANT (t2);
717
718 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
719 t1 = build_type_attribute_variant (t1, NULL_TREE);
720
721 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
722 t2 = build_type_attribute_variant (t2, NULL_TREE);
723
724 /* Save time if the two types are the same. */
725
726 if (t1 == t2) return t1;
727
728 code1 = TREE_CODE (t1);
729 code2 = TREE_CODE (t2);
730
366de0ce 731 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
732 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
733 || code1 == INTEGER_TYPE);
366de0ce 734 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
735 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
736 || code2 == INTEGER_TYPE);
10bc1b1b 737
5fc89bfd
JJ
738 /* When one operand is a decimal float type, the other operand cannot be
739 a generic float type or a complex type. We also disallow vector types
740 here. */
741 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
742 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
743 {
744 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
745 {
746 error ("can%'t mix operands of decimal float and vector types");
747 return error_mark_node;
748 }
749 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
750 {
751 error ("can%'t mix operands of decimal float and complex types");
752 return error_mark_node;
753 }
754 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
755 {
756 error ("can%'t mix operands of decimal float and other float types");
757 return error_mark_node;
758 }
759 }
760
10bc1b1b
JM
761 /* If one type is a vector type, return that type. (How the usual
762 arithmetic conversions apply to the vector types extension is not
763 precisely specified.) */
764 if (code1 == VECTOR_TYPE)
765 return t1;
766
767 if (code2 == VECTOR_TYPE)
768 return t2;
769
770 /* If one type is complex, form the common type of the non-complex
771 components, then make that complex. Use T1 or T2 if it is the
772 required type. */
773 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
774 {
775 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
776 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 777 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
778
779 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
780 return t1;
781 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
782 return t2;
783 else
784 return build_complex_type (subtype);
785 }
786
787 /* If only one is real, use it as the result. */
788
789 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
790 return t1;
791
792 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
793 return t2;
794
9a8ce21f
JG
795 /* If both are real and either are decimal floating point types, use
796 the decimal floating point type with the greater precision. */
797
798 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
799 {
800 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
801 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
802 return dfloat128_type_node;
803 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
804 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
805 return dfloat64_type_node;
806 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
807 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
808 return dfloat32_type_node;
809 }
810
ab22c1fa
CF
811 /* Deal with fixed-point types. */
812 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
813 {
814 unsigned int unsignedp = 0, satp = 0;
815 enum machine_mode m1, m2;
816 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
817
818 m1 = TYPE_MODE (t1);
819 m2 = TYPE_MODE (t2);
820
821 /* If one input type is saturating, the result type is saturating. */
822 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
823 satp = 1;
824
825 /* If both fixed-point types are unsigned, the result type is unsigned.
826 When mixing fixed-point and integer types, follow the sign of the
827 fixed-point type.
828 Otherwise, the result type is signed. */
829 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
830 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
831 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
832 && TYPE_UNSIGNED (t1))
833 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
834 && TYPE_UNSIGNED (t2)))
835 unsignedp = 1;
836
837 /* The result type is signed. */
838 if (unsignedp == 0)
839 {
840 /* If the input type is unsigned, we need to convert to the
841 signed type. */
842 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
843 {
d75d71e0 844 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
845 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
846 mclass = MODE_FRACT;
847 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
848 mclass = MODE_ACCUM;
849 else
850 gcc_unreachable ();
851 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
852 }
853 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
854 {
d75d71e0 855 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
856 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
857 mclass = MODE_FRACT;
858 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
859 mclass = MODE_ACCUM;
860 else
861 gcc_unreachable ();
862 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
863 }
864 }
865
866 if (code1 == FIXED_POINT_TYPE)
867 {
868 fbit1 = GET_MODE_FBIT (m1);
869 ibit1 = GET_MODE_IBIT (m1);
870 }
871 else
872 {
873 fbit1 = 0;
874 /* Signed integers need to subtract one sign bit. */
875 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
876 }
877
878 if (code2 == FIXED_POINT_TYPE)
879 {
880 fbit2 = GET_MODE_FBIT (m2);
881 ibit2 = GET_MODE_IBIT (m2);
882 }
883 else
884 {
885 fbit2 = 0;
886 /* Signed integers need to subtract one sign bit. */
887 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
888 }
889
890 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
891 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
892 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
893 satp);
894 }
895
10bc1b1b
JM
896 /* Both real or both integers; use the one with greater precision. */
897
898 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
899 return t1;
900 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
901 return t2;
902
903 /* Same precision. Prefer long longs to longs to ints when the
904 same precision, following the C99 rules on integer type rank
905 (which are equivalent to the C90 rules for C90 types). */
906
907 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
908 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
909 return long_long_unsigned_type_node;
910
911 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
912 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
913 {
914 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
915 return long_long_unsigned_type_node;
916 else
c22cacf3 917 return long_long_integer_type_node;
10bc1b1b
JM
918 }
919
920 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
921 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
922 return long_unsigned_type_node;
923
924 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
925 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
926 {
927 /* But preserve unsignedness from the other type,
928 since long cannot hold all the values of an unsigned int. */
929 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
930 return long_unsigned_type_node;
931 else
932 return long_integer_type_node;
933 }
934
935 /* Likewise, prefer long double to double even if same size. */
936 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
937 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
938 return long_double_type_node;
939
2531a1d9
JR
940 /* Likewise, prefer double to float even if same size.
941 We got a couple of embedded targets with 32 bit doubles, and the
942 pdp11 might have 64 bit floats. */
943 if (TYPE_MAIN_VARIANT (t1) == double_type_node
944 || TYPE_MAIN_VARIANT (t2) == double_type_node)
945 return double_type_node;
946
10bc1b1b
JM
947 /* Otherwise prefer the unsigned one. */
948
949 if (TYPE_UNSIGNED (t1))
950 return t1;
951 else
952 return t2;
953}
400fbf9f 954\f
5922c215
JM
955/* Wrapper around c_common_type that is used by c-common.c and other
956 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
957 are allowed here and are converted to their compatible integer types.
958 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
959 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
960tree
961common_type (tree t1, tree t2)
962{
963 if (TREE_CODE (t1) == ENUMERAL_TYPE)
964 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
965 if (TREE_CODE (t2) == ENUMERAL_TYPE)
966 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
967
968 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
969 if (TREE_CODE (t1) == BOOLEAN_TYPE
970 && TREE_CODE (t2) == BOOLEAN_TYPE)
971 return boolean_type_node;
972
973 /* If either type is BOOLEAN_TYPE, then return the other. */
974 if (TREE_CODE (t1) == BOOLEAN_TYPE)
975 return t2;
976 if (TREE_CODE (t2) == BOOLEAN_TYPE)
977 return t1;
978
ccf7f880
JJ
979 return c_common_type (t1, t2);
980}
f13c9b2c 981
400fbf9f
JW
982/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
983 or various other operations. Return 2 if they are compatible
984 but a warning may be needed if you use them together. */
985
986int
132da1a5 987comptypes (tree type1, tree type2)
f13c9b2c
AP
988{
989 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
990 int val;
991
dc5027f4 992 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
993 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
994
995 return val;
996}
997
998/* Like comptypes, but if it returns non-zero because enum and int are
999 compatible, it sets *ENUM_AND_INT_P to true. */
1000
1001static int
1002comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1003{
1004 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1005 int val;
1006
dc5027f4
JM
1007 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1008 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1009
1010 return val;
1011}
1012
1013/* Like comptypes, but if it returns nonzero for different types, it
1014 sets *DIFFERENT_TYPES_P to true. */
1015
1016int
1017comptypes_check_different_types (tree type1, tree type2,
1018 bool *different_types_p)
1019{
1020 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1021 int val;
1022
1023 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1024 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1025
f13c9b2c 1026 return val;
c22cacf3
MS
1027}
1028\f
f13c9b2c
AP
1029/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1030 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1031 but a warning may be needed if you use them together. If
1032 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1033 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1034 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1035 NULL, and the types are compatible but different enough not to be
48b0b196 1036 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1037 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1038 false, but may or may not be set if the types are incompatible.
1039 This differs from comptypes, in that we don't free the seen
1040 types. */
f13c9b2c
AP
1041
1042static int
dc5027f4
JM
1043comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1044 bool *different_types_p)
400fbf9f 1045{
58f9752a
KG
1046 const_tree t1 = type1;
1047 const_tree t2 = type2;
4b027d16 1048 int attrval, val;
400fbf9f
JW
1049
1050 /* Suppress errors caused by previously reported errors. */
1051
8d47dfc5
RH
1052 if (t1 == t2 || !t1 || !t2
1053 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1054 return 1;
1055
bca63328
JM
1056 /* Enumerated types are compatible with integer types, but this is
1057 not transitive: two enumerated types in the same translation unit
1058 are compatible with each other only if they are the same type. */
400fbf9f 1059
bca63328 1060 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1061 {
1062 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1063 if (TREE_CODE (t2) != VOID_TYPE)
1064 {
1065 if (enum_and_int_p != NULL)
1066 *enum_and_int_p = true;
1067 if (different_types_p != NULL)
1068 *different_types_p = true;
1069 }
744aa42f 1070 }
bca63328 1071 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1072 {
1073 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1074 if (TREE_CODE (t1) != VOID_TYPE)
1075 {
1076 if (enum_and_int_p != NULL)
1077 *enum_and_int_p = true;
1078 if (different_types_p != NULL)
1079 *different_types_p = true;
1080 }
744aa42f 1081 }
400fbf9f
JW
1082
1083 if (t1 == t2)
1084 return 1;
1085
1086 /* Different classes of types can't be compatible. */
1087
3aeb3655
EC
1088 if (TREE_CODE (t1) != TREE_CODE (t2))
1089 return 0;
400fbf9f 1090
118a3a8b 1091 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1092
3932261a 1093 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1094 return 0;
1095
08632da2
RS
1096 /* Allow for two different type nodes which have essentially the same
1097 definition. Note that we already checked for equality of the type
38e01259 1098 qualifiers (just above). */
400fbf9f 1099
46df2823
JM
1100 if (TREE_CODE (t1) != ARRAY_TYPE
1101 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1102 return 1;
1103
4b027d16 1104 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1105 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1106 return 0;
1107
1108 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1109 val = 0;
1110
400fbf9f
JW
1111 switch (TREE_CODE (t1))
1112 {
1113 case POINTER_TYPE:
106f5de5
UW
1114 /* Do not remove mode or aliasing information. */
1115 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1116 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1117 break;
4b027d16 1118 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1119 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1120 enum_and_int_p, different_types_p));
4b027d16 1121 break;
400fbf9f
JW
1122
1123 case FUNCTION_TYPE:
dc5027f4
JM
1124 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1125 different_types_p);
4b027d16 1126 break;
400fbf9f
JW
1127
1128 case ARRAY_TYPE:
1129 {
400fbf9f
JW
1130 tree d1 = TYPE_DOMAIN (t1);
1131 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1132 bool d1_variable, d2_variable;
1133 bool d1_zero, d2_zero;
4b027d16 1134 val = 1;
400fbf9f
JW
1135
1136 /* Target types must match incl. qualifiers. */
1137 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1138 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1139 enum_and_int_p,
1140 different_types_p)))
400fbf9f
JW
1141 return 0;
1142
dc5027f4
JM
1143 if (different_types_p != NULL
1144 && (d1 == 0) != (d2 == 0))
1145 *different_types_p = true;
400fbf9f 1146 /* Sizes must match unless one is missing or variable. */
3f85558f 1147 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1148 break;
400fbf9f 1149
3f75a254
JM
1150 d1_zero = !TYPE_MAX_VALUE (d1);
1151 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1152
3f75a254 1153 d1_variable = (!d1_zero
3f85558f
RH
1154 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1155 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1156 d2_variable = (!d2_zero
3f85558f
RH
1157 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1158 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1159 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1160 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1161
dc5027f4
JM
1162 if (different_types_p != NULL
1163 && d1_variable != d2_variable)
1164 *different_types_p = true;
3f85558f
RH
1165 if (d1_variable || d2_variable)
1166 break;
1167 if (d1_zero && d2_zero)
1168 break;
1169 if (d1_zero || d2_zero
3f75a254
JM
1170 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1171 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1172 val = 0;
1173
c22cacf3 1174 break;
400fbf9f
JW
1175 }
1176
d1bd0ded 1177 case ENUMERAL_TYPE:
58393038 1178 case RECORD_TYPE:
d1bd0ded 1179 case UNION_TYPE:
766beae1 1180 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1181 {
fcb99e7b
JJ
1182 tree a1 = TYPE_ATTRIBUTES (t1);
1183 tree a2 = TYPE_ATTRIBUTES (t2);
1184
1185 if (! attribute_list_contained (a1, a2)
1186 && ! attribute_list_contained (a2, a1))
1187 break;
1188
f13c9b2c 1189 if (attrval != 2)
dc5027f4
JM
1190 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1191 different_types_p);
1192 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1193 different_types_p);
f13c9b2c 1194 }
4b027d16 1195 break;
e9a25f70 1196
62e1dfcf 1197 case VECTOR_TYPE:
744aa42f
ILT
1198 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1199 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1200 enum_and_int_p, different_types_p));
62e1dfcf
NC
1201 break;
1202
e9a25f70
JL
1203 default:
1204 break;
400fbf9f 1205 }
4b027d16 1206 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1207}
1208
36c5e70a
BE
1209/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1210 their qualifiers, except for named address spaces. If the pointers point to
1211 different named addresses, then we must determine if one address space is a
1212 subset of the other. */
400fbf9f
JW
1213
1214static int
744aa42f 1215comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1216{
392202b0 1217 int val;
36c5e70a
BE
1218 tree mvl = TREE_TYPE (ttl);
1219 tree mvr = TREE_TYPE (ttr);
1220 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1221 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1222 addr_space_t as_common;
744aa42f 1223 bool enum_and_int_p;
8b40563c 1224
36c5e70a
BE
1225 /* Fail if pointers point to incompatible address spaces. */
1226 if (!addr_space_superset (asl, asr, &as_common))
1227 return 0;
1228
46df2823
JM
1229 /* Do not lose qualifiers on element types of array types that are
1230 pointer targets by taking their TYPE_MAIN_VARIANT. */
46df2823 1231 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
1232 mvl = (TYPE_ATOMIC (mvl)
1233 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1234 : TYPE_MAIN_VARIANT (mvl));
46df2823 1235 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
1236 mvr = (TYPE_ATOMIC (mvr)
1237 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1238 : TYPE_MAIN_VARIANT (mvr));
744aa42f
ILT
1239 enum_and_int_p = false;
1240 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1241
fcf73884 1242 if (val == 2)
c1771a20 1243 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1244
1245 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1246 warning_at (location, OPT_Wc___compat,
1247 "pointer target types incompatible in C++");
1248
400fbf9f
JW
1249 return val;
1250}
1251\f
1252/* Subroutines of `comptypes'. */
1253
f75fbaf7
ZW
1254/* Determine whether two trees derive from the same translation unit.
1255 If the CONTEXT chain ends in a null, that tree's context is still
1256 being parsed, so if two trees have context chains ending in null,
766beae1 1257 they're in the same translation unit. */
f75fbaf7 1258int
58f9752a 1259same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1260{
1261 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1262 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1263 {
6615c446
JO
1264 case tcc_declaration:
1265 t1 = DECL_CONTEXT (t1); break;
1266 case tcc_type:
1267 t1 = TYPE_CONTEXT (t1); break;
1268 case tcc_exceptional:
1269 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1270 default: gcc_unreachable ();
766beae1
ZW
1271 }
1272
1273 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1274 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1275 {
6615c446
JO
1276 case tcc_declaration:
1277 t2 = DECL_CONTEXT (t2); break;
1278 case tcc_type:
1279 t2 = TYPE_CONTEXT (t2); break;
1280 case tcc_exceptional:
1281 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1282 default: gcc_unreachable ();
766beae1
ZW
1283 }
1284
1285 return t1 == t2;
1286}
1287
f13c9b2c 1288/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1289
f13c9b2c 1290static struct tagged_tu_seen_cache *
58f9752a 1291alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1292{
cceb1885 1293 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1294 tu->next = tagged_tu_seen_base;
1295 tu->t1 = t1;
1296 tu->t2 = t2;
c22cacf3 1297
f13c9b2c 1298 tagged_tu_seen_base = tu;
c22cacf3 1299
f13c9b2c
AP
1300 /* The C standard says that two structures in different translation
1301 units are compatible with each other only if the types of their
1302 fields are compatible (among other things). We assume that they
1303 are compatible until proven otherwise when building the cache.
1304 An example where this can occur is:
1305 struct a
1306 {
1307 struct a *next;
1308 };
1309 If we are comparing this against a similar struct in another TU,
c83eecad 1310 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1311 loop. */
1312 tu->val = 1;
1313 return tu;
1314}
d1bd0ded 1315
f13c9b2c 1316/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1317
f13c9b2c
AP
1318static void
1319free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1320{
1321 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1322 while (tu != tu_til)
1323 {
741ac903
KG
1324 const struct tagged_tu_seen_cache *const tu1
1325 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1326 tu = tu1->next;
b1d5455a 1327 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1328 }
1329 tagged_tu_seen_base = tu_til;
1330}
d1bd0ded
GK
1331
1332/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1333 compatible. If the two types are not the same (which has been
1334 checked earlier), this can only happen when multiple translation
1335 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1336 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1337 comptypes_internal. */
d1bd0ded
GK
1338
1339static int
744aa42f 1340tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1341 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1342{
1343 tree s1, s2;
1344 bool needs_warning = false;
3aeb3655 1345
d1bd0ded
GK
1346 /* We have to verify that the tags of the types are the same. This
1347 is harder than it looks because this may be a typedef, so we have
1348 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1349 typedef...
1350 In the case of compiler-created builtin structs the TYPE_DECL
1351 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1352 while (TYPE_NAME (t1)
1353 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1354 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1355 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1356
dea984dc
ILT
1357 while (TYPE_NAME (t2)
1358 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1359 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1360 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1361
1362 /* C90 didn't have the requirement that the two tags be the same. */
1363 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1364 return 0;
3aeb3655 1365
d1bd0ded
GK
1366 /* C90 didn't say what happened if one or both of the types were
1367 incomplete; we choose to follow C99 rules here, which is that they
1368 are compatible. */
1369 if (TYPE_SIZE (t1) == NULL
1370 || TYPE_SIZE (t2) == NULL)
1371 return 1;
3aeb3655 1372
d1bd0ded 1373 {
f13c9b2c 1374 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1375 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1376 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1377 return tts_i->val;
d1bd0ded 1378 }
3aeb3655 1379
d1bd0ded
GK
1380 switch (TREE_CODE (t1))
1381 {
1382 case ENUMERAL_TYPE:
1383 {
f13c9b2c 1384 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1385 /* Speed up the case where the type values are in the same order. */
1386 tree tv1 = TYPE_VALUES (t1);
1387 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1388
c22cacf3 1389 if (tv1 == tv2)
f13c9b2c
AP
1390 {
1391 return 1;
1392 }
3aeb3655 1393
c22cacf3
MS
1394 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1395 {
1396 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1397 break;
1398 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1399 {
c22cacf3 1400 tu->val = 0;
f13c9b2c
AP
1401 return 0;
1402 }
c22cacf3 1403 }
3aeb3655 1404
c22cacf3 1405 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1406 {
1407 return 1;
1408 }
c22cacf3 1409 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1410 {
1411 tu->val = 0;
1412 return 0;
1413 }
3aeb3655 1414
d1bd0ded 1415 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1416 {
1417 tu->val = 0;
1418 return 0;
1419 }
3aeb3655 1420
d1bd0ded
GK
1421 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1422 {
1423 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1424 if (s2 == NULL
1425 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1426 {
1427 tu->val = 0;
1428 return 0;
1429 }
d1bd0ded
GK
1430 }
1431 return 1;
1432 }
1433
1434 case UNION_TYPE:
1435 {
f13c9b2c 1436 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1437 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1438 {
1439 tu->val = 0;
1440 return 0;
1441 }
c22cacf3 1442
f13c9b2c
AP
1443 /* Speed up the common case where the fields are in the same order. */
1444 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1445 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1446 {
1447 int result;
c22cacf3 1448
3ae4d3cc 1449 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1450 break;
744aa42f 1451 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1452 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1453
1454 if (result != 1 && !DECL_NAME (s1))
1455 break;
f13c9b2c
AP
1456 if (result == 0)
1457 {
1458 tu->val = 0;
1459 return 0;
1460 }
1461 if (result == 2)
1462 needs_warning = true;
1463
1464 if (TREE_CODE (s1) == FIELD_DECL
1465 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1466 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1467 {
1468 tu->val = 0;
1469 return 0;
1470 }
1471 }
1472 if (!s1 && !s2)
1473 {
1474 tu->val = needs_warning ? 2 : 1;
1475 return tu->val;
1476 }
d1bd0ded 1477
910ad8de 1478 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1479 {
1480 bool ok = false;
3aeb3655 1481
910ad8de 1482 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1483 if (DECL_NAME (s1) == DECL_NAME (s2))
1484 {
1485 int result;
1486
744aa42f 1487 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1488 enum_and_int_p,
1489 different_types_p);
3ae4d3cc
AO
1490
1491 if (result != 1 && !DECL_NAME (s1))
1492 continue;
1493 if (result == 0)
1494 {
1495 tu->val = 0;
1496 return 0;
1497 }
1498 if (result == 2)
1499 needs_warning = true;
1500
1501 if (TREE_CODE (s1) == FIELD_DECL
1502 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1503 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1504 break;
3ae4d3cc
AO
1505
1506 ok = true;
1507 break;
1508 }
3f75a254 1509 if (!ok)
f13c9b2c
AP
1510 {
1511 tu->val = 0;
1512 return 0;
1513 }
d1bd0ded 1514 }
f13c9b2c
AP
1515 tu->val = needs_warning ? 2 : 10;
1516 return tu->val;
d1bd0ded
GK
1517 }
1518
1519 case RECORD_TYPE:
1520 {
c22cacf3 1521 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1522
1523 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1524 s1 && s2;
910ad8de 1525 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1526 {
1527 int result;
1528 if (TREE_CODE (s1) != TREE_CODE (s2)
1529 || DECL_NAME (s1) != DECL_NAME (s2))
1530 break;
744aa42f 1531 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1532 enum_and_int_p, different_types_p);
d1bd0ded
GK
1533 if (result == 0)
1534 break;
1535 if (result == 2)
1536 needs_warning = true;
3aeb3655 1537
d1bd0ded
GK
1538 if (TREE_CODE (s1) == FIELD_DECL
1539 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1540 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1541 break;
1542 }
d1bd0ded 1543 if (s1 && s2)
f13c9b2c
AP
1544 tu->val = 0;
1545 else
1546 tu->val = needs_warning ? 2 : 1;
1547 return tu->val;
d1bd0ded
GK
1548 }
1549
1550 default:
366de0ce 1551 gcc_unreachable ();
d1bd0ded
GK
1552 }
1553}
1554
400fbf9f
JW
1555/* Return 1 if two function types F1 and F2 are compatible.
1556 If either type specifies no argument types,
1557 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1558 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1559 the other must specify that number of self-promoting arg types.
744aa42f 1560 Otherwise, the argument types must match.
dc5027f4 1561 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1562
1563static int
744aa42f 1564function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1565 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1566{
1567 tree args1, args2;
1568 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1569 int val = 1;
1570 int val1;
a6fdc086
GK
1571 tree ret1, ret2;
1572
1573 ret1 = TREE_TYPE (f1);
1574 ret2 = TREE_TYPE (f2);
1575
e508a019
JM
1576 /* 'volatile' qualifiers on a function's return type used to mean
1577 the function is noreturn. */
1578 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1579 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1580 if (TYPE_VOLATILE (ret1))
1581 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1582 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1583 if (TYPE_VOLATILE (ret2))
1584 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1585 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1586 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1587 if (val == 0)
400fbf9f
JW
1588 return 0;
1589
1590 args1 = TYPE_ARG_TYPES (f1);
1591 args2 = TYPE_ARG_TYPES (f2);
1592
dc5027f4
JM
1593 if (different_types_p != NULL
1594 && (args1 == 0) != (args2 == 0))
1595 *different_types_p = true;
1596
400fbf9f
JW
1597 /* An unspecified parmlist matches any specified parmlist
1598 whose argument types don't need default promotions. */
1599
1600 if (args1 == 0)
1601 {
1602 if (!self_promoting_args_p (args2))
1603 return 0;
1604 /* If one of these types comes from a non-prototype fn definition,
1605 compare that with the other type's arglist.
3176a0c2 1606 If they don't match, ask for a warning (but no error). */
400fbf9f 1607 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1608 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1609 enum_and_int_p, different_types_p))
400fbf9f
JW
1610 val = 2;
1611 return val;
1612 }
1613 if (args2 == 0)
1614 {
1615 if (!self_promoting_args_p (args1))
1616 return 0;
1617 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1618 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1619 enum_and_int_p, different_types_p))
400fbf9f
JW
1620 val = 2;
1621 return val;
1622 }
1623
1624 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1625 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1626 different_types_p);
400fbf9f
JW
1627 return val1 != 1 ? val1 : val;
1628}
1629
744aa42f
ILT
1630/* Check two lists of types for compatibility, returning 0 for
1631 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1632 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1633 comptypes_internal. */
400fbf9f
JW
1634
1635static int
744aa42f 1636type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1637 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1638{
1639 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1640 int val = 1;
9d5f3e49 1641 int newval = 0;
400fbf9f
JW
1642
1643 while (1)
1644 {
46df2823 1645 tree a1, mv1, a2, mv2;
400fbf9f
JW
1646 if (args1 == 0 && args2 == 0)
1647 return val;
1648 /* If one list is shorter than the other,
1649 they fail to match. */
1650 if (args1 == 0 || args2 == 0)
1651 return 0;
46df2823
JM
1652 mv1 = a1 = TREE_VALUE (args1);
1653 mv2 = a2 = TREE_VALUE (args2);
1654 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1655 mv1 = (TYPE_ATOMIC (mv1)
1656 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1657 TYPE_QUAL_ATOMIC)
1658 : TYPE_MAIN_VARIANT (mv1));
46df2823 1659 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1660 mv2 = (TYPE_ATOMIC (mv2)
1661 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1662 TYPE_QUAL_ATOMIC)
1663 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1664 /* A null pointer instead of a type
1665 means there is supposed to be an argument
1666 but nothing is specified about what type it has.
1667 So match anything that self-promotes. */
dc5027f4
JM
1668 if (different_types_p != NULL
1669 && (a1 == 0) != (a2 == 0))
1670 *different_types_p = true;
46df2823 1671 if (a1 == 0)
400fbf9f 1672 {
46df2823 1673 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1674 return 0;
1675 }
46df2823 1676 else if (a2 == 0)
400fbf9f 1677 {
46df2823 1678 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1679 return 0;
1680 }
8f5b6d29 1681 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1682 else if (TREE_CODE (a1) == ERROR_MARK
1683 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1684 ;
dc5027f4
JM
1685 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1686 different_types_p)))
400fbf9f 1687 {
dc5027f4
JM
1688 if (different_types_p != NULL)
1689 *different_types_p = true;
400fbf9f
JW
1690 /* Allow wait (union {union wait *u; int *i} *)
1691 and wait (union wait *) to be compatible. */
46df2823
JM
1692 if (TREE_CODE (a1) == UNION_TYPE
1693 && (TYPE_NAME (a1) == 0
ebf0bf7f 1694 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1695 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1696 && tree_int_cst_equal (TYPE_SIZE (a1),
1697 TYPE_SIZE (a2)))
400fbf9f
JW
1698 {
1699 tree memb;
46df2823 1700 for (memb = TYPE_FIELDS (a1);
910ad8de 1701 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1702 {
1703 tree mv3 = TREE_TYPE (memb);
1704 if (mv3 && mv3 != error_mark_node
1705 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1706 mv3 = (TYPE_ATOMIC (mv3)
1707 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1708 TYPE_QUAL_ATOMIC)
1709 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1710 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1711 different_types_p))
58cb41e6
JJ
1712 break;
1713 }
400fbf9f
JW
1714 if (memb == 0)
1715 return 0;
1716 }
46df2823
JM
1717 else if (TREE_CODE (a2) == UNION_TYPE
1718 && (TYPE_NAME (a2) == 0
ebf0bf7f 1719 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1720 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1721 && tree_int_cst_equal (TYPE_SIZE (a2),
1722 TYPE_SIZE (a1)))
400fbf9f
JW
1723 {
1724 tree memb;
46df2823 1725 for (memb = TYPE_FIELDS (a2);
910ad8de 1726 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1727 {
1728 tree mv3 = TREE_TYPE (memb);
1729 if (mv3 && mv3 != error_mark_node
1730 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1731 mv3 = (TYPE_ATOMIC (mv3)
1732 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1733 TYPE_QUAL_ATOMIC)
1734 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1735 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1736 different_types_p))
58cb41e6
JJ
1737 break;
1738 }
400fbf9f
JW
1739 if (memb == 0)
1740 return 0;
1741 }
1742 else
1743 return 0;
1744 }
1745
1746 /* comptypes said ok, but record if it said to warn. */
1747 if (newval > val)
1748 val = newval;
1749
1750 args1 = TREE_CHAIN (args1);
1751 args2 = TREE_CHAIN (args2);
1752 }
1753}
400fbf9f 1754\f
400fbf9f
JW
1755/* Compute the size to increment a pointer by. */
1756
4e2fb7de 1757static tree
58f9752a 1758c_size_in_bytes (const_tree type)
400fbf9f
JW
1759{
1760 enum tree_code code = TREE_CODE (type);
1761
fed3cef0
RK
1762 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1763 return size_one_node;
1764
d0f062fb 1765 if (!COMPLETE_OR_VOID_TYPE_P (type))
400fbf9f
JW
1766 {
1767 error ("arithmetic on pointer to an incomplete type");
fed3cef0 1768 return size_one_node;
400fbf9f
JW
1769 }
1770
1771 /* Convert in case a char is more than one unit. */
db3927fb
AH
1772 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1773 size_int (TYPE_PRECISION (char_type_node)
1774 / BITS_PER_UNIT));
400fbf9f 1775}
400fbf9f 1776\f
400fbf9f
JW
1777/* Return either DECL or its known constant value (if it has one). */
1778
56cb9733 1779tree
2f6e4e97 1780decl_constant_value (tree decl)
400fbf9f 1781{
a7c1916a 1782 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1783 in a place where a variable is invalid. Note that DECL_INITIAL
1784 isn't valid for a PARM_DECL. */
a7c1916a 1785 current_function_decl != 0
4f976745 1786 && TREE_CODE (decl) != PARM_DECL
3f75a254 1787 && !TREE_THIS_VOLATILE (decl)
83bab8db 1788 && TREE_READONLY (decl)
400fbf9f
JW
1789 && DECL_INITIAL (decl) != 0
1790 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1791 /* This is invalid if initial value is not constant.
1792 If it has either a function call, a memory reference,
1793 or a variable, then re-evaluating it could give different results. */
1794 && TREE_CONSTANT (DECL_INITIAL (decl))
1795 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1796 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1797 return DECL_INITIAL (decl);
1798 return decl;
1799}
1800
f2a71bbc
JM
1801/* Convert the array expression EXP to a pointer. */
1802static tree
c2255bc4 1803array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1804{
f2a71bbc 1805 tree orig_exp = exp;
207bf485 1806 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1807 tree adr;
1808 tree restype = TREE_TYPE (type);
1809 tree ptrtype;
207bf485 1810
f2a71bbc 1811 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1812
f2a71bbc 1813 STRIP_TYPE_NOPS (exp);
207bf485 1814
487a92fe
JM
1815 if (TREE_NO_WARNING (orig_exp))
1816 TREE_NO_WARNING (exp) = 1;
207bf485 1817
f2a71bbc
JM
1818 ptrtype = build_pointer_type (restype);
1819
1820 if (TREE_CODE (exp) == INDIRECT_REF)
1821 return convert (ptrtype, TREE_OPERAND (exp, 0));
1822
1f37c583
JM
1823 /* In C++ array compound literals are temporary objects unless they are
1824 const or appear in namespace scope, so they are destroyed too soon
1825 to use them for much of anything (c++/53220). */
1826 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1827 {
1828 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1829 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1830 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1831 "converting an array compound literal to a pointer "
1832 "is ill-formed in C++");
1833 }
1834
c2255bc4 1835 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1836 return convert (ptrtype, adr);
1837}
207bf485 1838
f2a71bbc
JM
1839/* Convert the function expression EXP to a pointer. */
1840static tree
c2255bc4 1841function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1842{
1843 tree orig_exp = exp;
207bf485 1844
f2a71bbc 1845 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1846
f2a71bbc 1847 STRIP_TYPE_NOPS (exp);
207bf485 1848
f2a71bbc
JM
1849 if (TREE_NO_WARNING (orig_exp))
1850 TREE_NO_WARNING (exp) = 1;
207bf485 1851
c2255bc4 1852 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1853}
207bf485 1854
ebfbbdc5
JJ
1855/* Mark EXP as read, not just set, for set but not used -Wunused
1856 warning purposes. */
1857
1858void
1859mark_exp_read (tree exp)
1860{
1861 switch (TREE_CODE (exp))
1862 {
1863 case VAR_DECL:
1864 case PARM_DECL:
1865 DECL_READ_P (exp) = 1;
1866 break;
1867 case ARRAY_REF:
1868 case COMPONENT_REF:
1869 case MODIFY_EXPR:
1870 case REALPART_EXPR:
1871 case IMAGPART_EXPR:
1872 CASE_CONVERT:
1873 case ADDR_EXPR:
1874 mark_exp_read (TREE_OPERAND (exp, 0));
1875 break;
1876 case COMPOUND_EXPR:
82c3c067 1877 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1878 mark_exp_read (TREE_OPERAND (exp, 1));
1879 break;
1880 default:
1881 break;
1882 }
1883}
1884
f2a71bbc
JM
1885/* Perform the default conversion of arrays and functions to pointers.
1886 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1887 return EXP.
1888
1889 LOC is the location of the expression. */
f2a71bbc
JM
1890
1891struct c_expr
c2255bc4 1892default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1893{
1894 tree orig_exp = exp.value;
1895 tree type = TREE_TYPE (exp.value);
1896 enum tree_code code = TREE_CODE (type);
1897
1898 switch (code)
1899 {
1900 case ARRAY_TYPE:
1901 {
1902 bool not_lvalue = false;
1903 bool lvalue_array_p;
1904
1905 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1906 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1907 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1908 {
1909 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1910 not_lvalue = true;
1911 exp.value = TREE_OPERAND (exp.value, 0);
1912 }
1913
1914 if (TREE_NO_WARNING (orig_exp))
1915 TREE_NO_WARNING (exp.value) = 1;
1916
1917 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1918 if (!flag_isoc99 && !lvalue_array_p)
1919 {
1920 /* Before C99, non-lvalue arrays do not decay to pointers.
1921 Normally, using such an array would be invalid; but it can
1922 be used correctly inside sizeof or as a statement expression.
1923 Thus, do not give an error here; an error will result later. */
1924 return exp;
1925 }
1926
c2255bc4 1927 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1928 }
1929 break;
1930 case FUNCTION_TYPE:
c2255bc4 1931 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1932 break;
1933 default:
f2a71bbc 1934 break;
207bf485 1935 }
f2a71bbc 1936
207bf485
JM
1937 return exp;
1938}
1939
ebfbbdc5
JJ
1940struct c_expr
1941default_function_array_read_conversion (location_t loc, struct c_expr exp)
1942{
1943 mark_exp_read (exp.value);
1944 return default_function_array_conversion (loc, exp);
1945}
522ddfa2 1946
267bac10
JM
1947/* Return whether EXPR should be treated as an atomic lvalue for the
1948 purposes of load and store handling. */
1949
1950static bool
1951really_atomic_lvalue (tree expr)
1952{
1953 if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node)
1954 return false;
1955 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1956 return false;
1957 if (!lvalue_p (expr))
1958 return false;
1959
1960 /* Ignore _Atomic on register variables, since their addresses can't
1961 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1962 sequences wouldn't work. Ignore _Atomic on structures containing
1963 bit-fields, since accessing elements of atomic structures or
1964 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1965 it's undefined at translation time or execution time, and the
1966 normal atomic sequences again wouldn't work. */
1967 while (handled_component_p (expr))
1968 {
1969 if (TREE_CODE (expr) == COMPONENT_REF
1970 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1971 return false;
1972 expr = TREE_OPERAND (expr, 0);
1973 }
1974 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1975 return false;
1976 return true;
1977}
1978
1979/* Convert expression EXP (location LOC) from lvalue to rvalue,
1980 including converting functions and arrays to pointers if CONVERT_P.
1981 If READ_P, also mark the expression as having been read. */
1982
1983struct c_expr
1984convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1985 bool convert_p, bool read_p)
1986{
1987 if (read_p)
1988 mark_exp_read (exp.value);
1989 if (convert_p)
1990 exp = default_function_array_conversion (loc, exp);
1991 if (really_atomic_lvalue (exp.value))
1992 {
1993 vec<tree, va_gc> *params;
1994 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1995 tree expr_type = TREE_TYPE (exp.value);
1996 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1997 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
1998
1999 gcc_assert (TYPE_ATOMIC (expr_type));
2000
2001 /* Expansion of a generic atomic load may require an addition
2002 element, so allocate enough to prevent a resize. */
2003 vec_alloc (params, 4);
2004
2005 /* Remove the qualifiers for the rest of the expressions and
2006 create the VAL temp variable to hold the RHS. */
2007 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2008 tmp = create_tmp_var (nonatomic_type, NULL);
2009 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2010 TREE_ADDRESSABLE (tmp) = 1;
2011
2012 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2013 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2014 params->quick_push (expr_addr);
2015 params->quick_push (tmp_addr);
2016 params->quick_push (seq_cst);
2017 func_call = build_function_call_vec (loc, fndecl, params, NULL);
2018
2019 /* Return tmp which contains the value loaded. */
2020 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2021 }
2022 return exp;
2023}
2024
522ddfa2
JM
2025/* EXP is an expression of integer type. Apply the integer promotions
2026 to it and return the promoted value. */
400fbf9f
JW
2027
2028tree
522ddfa2 2029perform_integral_promotions (tree exp)
400fbf9f 2030{
b3694847
SS
2031 tree type = TREE_TYPE (exp);
2032 enum tree_code code = TREE_CODE (type);
400fbf9f 2033
522ddfa2 2034 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2035
400fbf9f
JW
2036 /* Normally convert enums to int,
2037 but convert wide enums to something wider. */
2038 if (code == ENUMERAL_TYPE)
2039 {
b0c48229
NB
2040 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2041 TYPE_PRECISION (integer_type_node)),
2042 ((TYPE_PRECISION (type)
2043 >= TYPE_PRECISION (integer_type_node))
8df83eae 2044 && TYPE_UNSIGNED (type)));
05bccae2 2045
400fbf9f
JW
2046 return convert (type, exp);
2047 }
2048
522ddfa2
JM
2049 /* ??? This should no longer be needed now bit-fields have their
2050 proper types. */
9753f113 2051 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2052 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2053 /* If it's thinner than an int, promote it like a
d72040f5 2054 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2055 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2056 TYPE_PRECISION (integer_type_node)))
f458d1d5 2057 return convert (integer_type_node, exp);
9753f113 2058
d72040f5 2059 if (c_promoting_integer_type_p (type))
400fbf9f 2060 {
f458d1d5 2061 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2062 if (TYPE_UNSIGNED (type)
f458d1d5 2063 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2064 return convert (unsigned_type_node, exp);
05bccae2 2065
400fbf9f
JW
2066 return convert (integer_type_node, exp);
2067 }
05bccae2 2068
522ddfa2
JM
2069 return exp;
2070}
2071
2072
2073/* Perform default promotions for C data used in expressions.
46bdb9cf 2074 Enumeral types or short or char are converted to int.
522ddfa2
JM
2075 In addition, manifest constants symbols are replaced by their values. */
2076
2077tree
2078default_conversion (tree exp)
2079{
2080 tree orig_exp;
2081 tree type = TREE_TYPE (exp);
2082 enum tree_code code = TREE_CODE (type);
40449a90 2083 tree promoted_type;
522ddfa2 2084
ebfbbdc5
JJ
2085 mark_exp_read (exp);
2086
46bdb9cf
JM
2087 /* Functions and arrays have been converted during parsing. */
2088 gcc_assert (code != FUNCTION_TYPE);
2089 if (code == ARRAY_TYPE)
2090 return exp;
522ddfa2
JM
2091
2092 /* Constants can be used directly unless they're not loadable. */
2093 if (TREE_CODE (exp) == CONST_DECL)
2094 exp = DECL_INITIAL (exp);
2095
522ddfa2
JM
2096 /* Strip no-op conversions. */
2097 orig_exp = exp;
2098 STRIP_TYPE_NOPS (exp);
2099
2100 if (TREE_NO_WARNING (orig_exp))
2101 TREE_NO_WARNING (exp) = 1;
2102
400fbf9f
JW
2103 if (code == VOID_TYPE)
2104 {
2105 error ("void value not ignored as it ought to be");
2106 return error_mark_node;
2107 }
808d6eaa
JM
2108
2109 exp = require_complete_type (exp);
2110 if (exp == error_mark_node)
2111 return error_mark_node;
2112
40449a90
SL
2113 promoted_type = targetm.promoted_type (type);
2114 if (promoted_type)
2115 return convert (promoted_type, exp);
2116
808d6eaa
JM
2117 if (INTEGRAL_TYPE_P (type))
2118 return perform_integral_promotions (exp);
2119
400fbf9f
JW
2120 return exp;
2121}
2122\f
0fb96aa4 2123/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2124
2125 If the component name is not found, returns NULL_TREE. Otherwise,
2126 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2127 stepping down the chain to the component, which is in the last
2128 TREE_VALUE of the list. Normally the list is of length one, but if
2129 the component is embedded within (nested) anonymous structures or
2130 unions, the list steps down the chain to the component. */
2f6e4e97 2131
2f2d13da 2132static tree
0fb96aa4 2133lookup_field (tree type, tree component)
2f2d13da
DE
2134{
2135 tree field;
2136
2137 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2138 to the field elements. Use a binary search on this array to quickly
2139 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2140 will always be set for structures which have many elements. */
2141
22a0b85f 2142 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2143 {
2144 int bot, top, half;
d07605f5 2145 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2146
2147 field = TYPE_FIELDS (type);
2148 bot = 0;
d07605f5 2149 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2150 while (top - bot > 1)
2151 {
2f2d13da
DE
2152 half = (top - bot + 1) >> 1;
2153 field = field_array[bot+half];
2154
2155 if (DECL_NAME (field) == NULL_TREE)
2156 {
2157 /* Step through all anon unions in linear fashion. */
2158 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2159 {
2f2d13da 2160 field = field_array[bot++];
a68b98cf
RK
2161 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2162 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2163 {
0fb96aa4 2164 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2165
2166 if (anon)
2167 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2168
2169 /* The Plan 9 compiler permits referring
2170 directly to an anonymous struct/union field
2171 using a typedef name. */
2172 if (flag_plan9_extensions
2173 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2174 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2175 == TYPE_DECL)
2176 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2177 == component))
2178 break;
2f6e4e97 2179 }
2f2d13da
DE
2180 }
2181
2182 /* Entire record is only anon unions. */
2183 if (bot > top)
2184 return NULL_TREE;
2185
2186 /* Restart the binary search, with new lower bound. */
2187 continue;
2188 }
2189
e8b87aac 2190 if (DECL_NAME (field) == component)
2f2d13da 2191 break;
e8b87aac 2192 if (DECL_NAME (field) < component)
2f2d13da
DE
2193 bot += half;
2194 else
2195 top = bot + half;
2196 }
2197
2198 if (DECL_NAME (field_array[bot]) == component)
2199 field = field_array[bot];
2200 else if (DECL_NAME (field) != component)
e9b2c823 2201 return NULL_TREE;
2f2d13da
DE
2202 }
2203 else
2204 {
910ad8de 2205 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2206 {
e9b2c823
NB
2207 if (DECL_NAME (field) == NULL_TREE
2208 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2209 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2210 {
0fb96aa4 2211 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2212
e9b2c823
NB
2213 if (anon)
2214 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2215
2216 /* The Plan 9 compiler permits referring directly to an
2217 anonymous struct/union field using a typedef
2218 name. */
2219 if (flag_plan9_extensions
2220 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2221 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2222 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2223 == component))
2224 break;
2f2d13da
DE
2225 }
2226
2227 if (DECL_NAME (field) == component)
2228 break;
2229 }
e9b2c823
NB
2230
2231 if (field == NULL_TREE)
2232 return NULL_TREE;
2f2d13da
DE
2233 }
2234
e9b2c823 2235 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2236}
2237
c2255bc4
AH
2238/* Make an expression to refer to the COMPONENT field of structure or
2239 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2240 location of the COMPONENT_REF. */
400fbf9f
JW
2241
2242tree
c2255bc4 2243build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2244{
b3694847
SS
2245 tree type = TREE_TYPE (datum);
2246 enum tree_code code = TREE_CODE (type);
2247 tree field = NULL;
2248 tree ref;
1e57bf47 2249 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2250
7a3ea201
RH
2251 if (!objc_is_public (datum, component))
2252 return error_mark_node;
2253
46a88c12 2254 /* Detect Objective-C property syntax object.property. */
668ea4b1 2255 if (c_dialect_objc ()
46a88c12 2256 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2257 return ref;
2258
400fbf9f
JW
2259 /* See if there is a field or component with name COMPONENT. */
2260
2261 if (code == RECORD_TYPE || code == UNION_TYPE)
2262 {
d0f062fb 2263 if (!COMPLETE_TYPE_P (type))
400fbf9f 2264 {
7a228918 2265 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2266 return error_mark_node;
2267 }
2268
0fb96aa4 2269 field = lookup_field (type, component);
400fbf9f
JW
2270
2271 if (!field)
2272 {
c2255bc4 2273 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2274 return error_mark_node;
2275 }
400fbf9f 2276
e9b2c823
NB
2277 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2278 This might be better solved in future the way the C++ front
2279 end does it - by giving the anonymous entities each a
2280 separate name and type, and then have build_component_ref
2281 recursively call itself. We can't do that here. */
46ea50cb 2282 do
19d76e60 2283 {
e9b2c823 2284 tree subdatum = TREE_VALUE (field);
efed193e
JM
2285 int quals;
2286 tree subtype;
1e57bf47 2287 bool use_datum_quals;
e9b2c823
NB
2288
2289 if (TREE_TYPE (subdatum) == error_mark_node)
2290 return error_mark_node;
2291
1e57bf47
JM
2292 /* If this is an rvalue, it does not have qualifiers in C
2293 standard terms and we must avoid propagating such
2294 qualifiers down to a non-lvalue array that is then
2295 converted to a pointer. */
2296 use_datum_quals = (datum_lvalue
2297 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2298
efed193e 2299 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2300 if (use_datum_quals)
2301 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2302 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2303
2304 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2305 NULL_TREE);
c2255bc4 2306 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2307 if (TREE_READONLY (subdatum)
2308 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2309 TREE_READONLY (ref) = 1;
1e57bf47
JM
2310 if (TREE_THIS_VOLATILE (subdatum)
2311 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2312 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2313
2314 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2315 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2316
19d76e60 2317 datum = ref;
46ea50cb
RS
2318
2319 field = TREE_CHAIN (field);
19d76e60 2320 }
46ea50cb 2321 while (field);
19d76e60 2322
400fbf9f
JW
2323 return ref;
2324 }
2325 else if (code != ERROR_MARK)
c2255bc4
AH
2326 error_at (loc,
2327 "request for member %qE in something not a structure or union",
2328 component);
400fbf9f
JW
2329
2330 return error_mark_node;
2331}
2332\f
2333/* Given an expression PTR for a pointer, return an expression
2334 for the value pointed to.
6a3799eb
AH
2335 ERRORSTRING is the name of the operator to appear in error messages.
2336
2337 LOC is the location to use for the generated tree. */
400fbf9f
JW
2338
2339tree
dd865ef6 2340build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2341{
b3694847
SS
2342 tree pointer = default_conversion (ptr);
2343 tree type = TREE_TYPE (pointer);
6a3799eb 2344 tree ref;
400fbf9f
JW
2345
2346 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2347 {
1043771b 2348 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2349 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2350 {
2351 /* If a warning is issued, mark it to avoid duplicates from
2352 the backend. This only needs to be done at
2353 warn_strict_aliasing > 2. */
2354 if (warn_strict_aliasing > 2)
2355 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2356 type, TREE_OPERAND (pointer, 0)))
2357 TREE_NO_WARNING (pointer) = 1;
2358 }
2359
870cc33b 2360 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2361 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2362 == TREE_TYPE (type)))
6a3799eb
AH
2363 {
2364 ref = TREE_OPERAND (pointer, 0);
2365 protected_set_expr_location (ref, loc);
2366 return ref;
2367 }
870cc33b
RS
2368 else
2369 {
2370 tree t = TREE_TYPE (type);
46df2823 2371
984dfd8c 2372 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2373
baae9b65 2374 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2375 {
c9f9eb5d 2376 error_at (loc, "dereferencing pointer to incomplete type");
870cc33b
RS
2377 return error_mark_node;
2378 }
7d882b83 2379 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2380 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2381
2382 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2383 so that we get the proper error message if the result is used
2384 to assign to. Also, &* is supposed to be a no-op.
2385 And ANSI C seems to specify that the type of the result
2386 should be the const type. */
2387 /* A de-reference of a pointer to const is not a const. It is valid
2388 to change it via some other pointer. */
2389 TREE_READONLY (ref) = TYPE_READONLY (t);
2390 TREE_SIDE_EFFECTS (ref)
271bd540 2391 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2392 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2393 protected_set_expr_location (ref, loc);
870cc33b
RS
2394 return ref;
2395 }
2396 }
400fbf9f 2397 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2398 invalid_indirection_error (loc, type, errstring);
2399
400fbf9f
JW
2400 return error_mark_node;
2401}
2402
2403/* This handles expressions of the form "a[i]", which denotes
2404 an array reference.
2405
2406 This is logically equivalent in C to *(a+i), but we may do it differently.
2407 If A is a variable or a member, we generate a primitive ARRAY_REF.
2408 This avoids forcing the array out of registers, and can work on
2409 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2410 by functions).
2411
30cd1c5d
AS
2412 For vector types, allow vector[i] but not i[vector], and create
2413 *(((type*)&vectortype) + i) for the expression.
2414
6a3799eb 2415 LOC is the location to use for the returned expression. */
400fbf9f
JW
2416
2417tree
c2255bc4 2418build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2419{
6a3799eb 2420 tree ret;
a4ab7973 2421 bool swapped = false;
400fbf9f
JW
2422 if (TREE_TYPE (array) == error_mark_node
2423 || TREE_TYPE (index) == error_mark_node)
2424 return error_mark_node;
2425
36536d79
BI
2426 if (flag_enable_cilkplus && contains_array_notation_expr (index))
2427 {
2428 size_t rank = 0;
2429 if (!find_rank (loc, index, index, true, &rank))
2430 return error_mark_node;
2431 if (rank > 1)
2432 {
2433 error_at (loc, "rank of the array's index is greater than 1");
2434 return error_mark_node;
2435 }
2436 }
a4ab7973 2437 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2438 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2439 /* Allow vector[index] but not index[vector]. */
2440 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2441 {
a4ab7973
JM
2442 tree temp;
2443 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2444 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2445 {
f90e8e2e 2446 error_at (loc,
30cd1c5d
AS
2447 "subscripted value is neither array nor pointer nor vector");
2448
fdeefd49
RS
2449 return error_mark_node;
2450 }
a4ab7973
JM
2451 temp = array;
2452 array = index;
2453 index = temp;
2454 swapped = true;
2455 }
2456
2457 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2458 {
a63068b6 2459 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2460 return error_mark_node;
2461 }
2462
2463 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2464 {
a63068b6 2465 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2466 return error_mark_node;
2467 }
2468
ff6b6641
GDR
2469 /* ??? Existing practice has been to warn only when the char
2470 index is syntactically the index, not for char[array]. */
2471 if (!swapped)
2472 warn_array_subscript_with_type_char (index);
a4ab7973
JM
2473
2474 /* Apply default promotions *after* noticing character types. */
2475 index = default_conversion (index);
2476
2477 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2478
7edaa4d2 2479 convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2480
2481 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2482 {
e4d35515 2483 tree rval, type;
fdeefd49 2484
400fbf9f
JW
2485 /* An array that is indexed by a non-constant
2486 cannot be stored in a register; we must be able to do
2487 address arithmetic on its address.
2488 Likewise an array of elements of variable size. */
2489 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2490 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2491 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2492 {
dffd7eb6 2493 if (!c_mark_addressable (array))
400fbf9f
JW
2494 return error_mark_node;
2495 }
e6d52559
JW
2496 /* An array that is indexed by a constant value which is not within
2497 the array bounds cannot be stored in a register either; because we
2498 would get a crash in store_bit_field/extract_bit_field when trying
2499 to access a non-existent part of the register. */
2500 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2501 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2502 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2503 {
dffd7eb6 2504 if (!c_mark_addressable (array))
e6d52559
JW
2505 return error_mark_node;
2506 }
400fbf9f 2507
400fbf9f
JW
2508 if (pedantic)
2509 {
2510 tree foo = array;
2511 while (TREE_CODE (foo) == COMPONENT_REF)
2512 foo = TREE_OPERAND (foo, 0);
5baeaac0 2513 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2514 pedwarn (loc, OPT_Wpedantic,
fcf73884 2515 "ISO C forbids subscripting %<register%> array");
3f75a254 2516 else if (!flag_isoc99 && !lvalue_p (foo))
c1771a20 2517 pedwarn (loc, OPT_Wpedantic,
fcf73884 2518 "ISO C90 forbids subscripting non-lvalue array");
400fbf9f
JW
2519 }
2520
46df2823 2521 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2522 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2523 /* Array ref is const/volatile if the array elements are
c22cacf3 2524 or if the array is. */
400fbf9f
JW
2525 TREE_READONLY (rval)
2526 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2527 | TREE_READONLY (array));
2528 TREE_SIDE_EFFECTS (rval)
2529 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2530 | TREE_SIDE_EFFECTS (array));
2531 TREE_THIS_VOLATILE (rval)
2532 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2533 /* This was added by rms on 16 Nov 91.
2f6e4e97 2534 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2535 in an inline function.
2536 Hope it doesn't break something else. */
2537 | TREE_THIS_VOLATILE (array));
928c19bb 2538 ret = require_complete_type (rval);
6a3799eb
AH
2539 protected_set_expr_location (ret, loc);
2540 return ret;
400fbf9f 2541 }
a4ab7973
JM
2542 else
2543 {
2544 tree ar = default_conversion (array);
400fbf9f 2545
a4ab7973
JM
2546 if (ar == error_mark_node)
2547 return ar;
400fbf9f 2548
a4ab7973
JM
2549 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2550 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2551
ba47d38d 2552 return build_indirect_ref
c9f9eb5d 2553 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
dd865ef6 2554 RO_ARRAY_INDEXING);
a4ab7973 2555 }
400fbf9f
JW
2556}
2557\f
7e585d16 2558/* Build an external reference to identifier ID. FUN indicates
766beb40 2559 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2560 location of the identifier. This sets *TYPE to the type of the
2561 identifier, which is not the same as the type of the returned value
2562 for CONST_DECLs defined as enum constants. If the type of the
2563 identifier is not available, *TYPE is set to NULL. */
7e585d16 2564tree
c2255bc4 2565build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2566{
2567 tree ref;
2568 tree decl = lookup_name (id);
16b34ad6
ZL
2569
2570 /* In Objective-C, an instance variable (ivar) may be preferred to
2571 whatever lookup_name() found. */
2572 decl = objc_lookup_ivar (decl, id);
7e585d16 2573
6866c6e8 2574 *type = NULL;
339a28b9 2575 if (decl && decl != error_mark_node)
6866c6e8
ILT
2576 {
2577 ref = decl;
2578 *type = TREE_TYPE (ref);
2579 }
339a28b9
ZW
2580 else if (fun)
2581 /* Implicit function declaration. */
c2255bc4 2582 ref = implicitly_declare (loc, id);
339a28b9
ZW
2583 else if (decl == error_mark_node)
2584 /* Don't complain about something that's already been
2585 complained about. */
2586 return error_mark_node;
2587 else
2588 {
c2255bc4 2589 undeclared_variable (loc, id);
339a28b9
ZW
2590 return error_mark_node;
2591 }
7e585d16
ZW
2592
2593 if (TREE_TYPE (ref) == error_mark_node)
2594 return error_mark_node;
2595
339a28b9 2596 if (TREE_DEPRECATED (ref))
9b86d6bb 2597 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2598
ad960f56 2599 /* Recursive call does not count as usage. */
b8698a0f 2600 if (ref != current_function_decl)
ad960f56 2601 {
ad960f56
MLI
2602 TREE_USED (ref) = 1;
2603 }
7e585d16 2604
bc4b653b
JM
2605 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2606 {
2607 if (!in_sizeof && !in_typeof)
2608 C_DECL_USED (ref) = 1;
2609 else if (DECL_INITIAL (ref) == 0
2610 && DECL_EXTERNAL (ref)
2611 && !TREE_PUBLIC (ref))
2612 record_maybe_used_decl (ref);
2613 }
2614
7e585d16
ZW
2615 if (TREE_CODE (ref) == CONST_DECL)
2616 {
6193b8b7 2617 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2618
2619 if (warn_cxx_compat
2620 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2621 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2622 {
2623 warning_at (loc, OPT_Wc___compat,
2624 ("enum constant defined in struct or union "
2625 "is not visible in C++"));
2626 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2627 }
2628
7e585d16
ZW
2629 ref = DECL_INITIAL (ref);
2630 TREE_CONSTANT (ref) = 1;
2631 }
6a29edea 2632 else if (current_function_decl != 0
4b1e44be 2633 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2634 && (TREE_CODE (ref) == VAR_DECL
2635 || TREE_CODE (ref) == PARM_DECL
2636 || TREE_CODE (ref) == FUNCTION_DECL))
2637 {
2638 tree context = decl_function_context (ref);
2f6e4e97 2639
6a29edea
EB
2640 if (context != 0 && context != current_function_decl)
2641 DECL_NONLOCAL (ref) = 1;
2642 }
71113fcd
GK
2643 /* C99 6.7.4p3: An inline definition of a function with external
2644 linkage ... shall not contain a reference to an identifier with
2645 internal linkage. */
2646 else if (current_function_decl != 0
2647 && DECL_DECLARED_INLINE_P (current_function_decl)
2648 && DECL_EXTERNAL (current_function_decl)
2649 && VAR_OR_FUNCTION_DECL_P (ref)
2650 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2651 && ! TREE_PUBLIC (ref)
2652 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2653 record_inline_static (loc, current_function_decl, ref,
2654 csi_internal);
7e585d16
ZW
2655
2656 return ref;
2657}
2658
bc4b653b
JM
2659/* Record details of decls possibly used inside sizeof or typeof. */
2660struct maybe_used_decl
2661{
2662 /* The decl. */
2663 tree decl;
2664 /* The level seen at (in_sizeof + in_typeof). */
2665 int level;
2666 /* The next one at this level or above, or NULL. */
2667 struct maybe_used_decl *next;
2668};
2669
2670static struct maybe_used_decl *maybe_used_decls;
2671
2672/* Record that DECL, an undefined static function reference seen
2673 inside sizeof or typeof, might be used if the operand of sizeof is
2674 a VLA type or the operand of typeof is a variably modified
2675 type. */
2676
4e2fb7de 2677static void
bc4b653b
JM
2678record_maybe_used_decl (tree decl)
2679{
2680 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2681 t->decl = decl;
2682 t->level = in_sizeof + in_typeof;
2683 t->next = maybe_used_decls;
2684 maybe_used_decls = t;
2685}
2686
2687/* Pop the stack of decls possibly used inside sizeof or typeof. If
2688 USED is false, just discard them. If it is true, mark them used
2689 (if no longer inside sizeof or typeof) or move them to the next
2690 level up (if still inside sizeof or typeof). */
2691
2692void
2693pop_maybe_used (bool used)
2694{
2695 struct maybe_used_decl *p = maybe_used_decls;
2696 int cur_level = in_sizeof + in_typeof;
2697 while (p && p->level > cur_level)
2698 {
2699 if (used)
2700 {
2701 if (cur_level == 0)
2702 C_DECL_USED (p->decl) = 1;
2703 else
2704 p->level = cur_level;
2705 }
2706 p = p->next;
2707 }
2708 if (!used || cur_level == 0)
2709 maybe_used_decls = p;
2710}
2711
2712/* Return the result of sizeof applied to EXPR. */
2713
2714struct c_expr
c2255bc4 2715c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2716{
2717 struct c_expr ret;
ad97f4be
JM
2718 if (expr.value == error_mark_node)
2719 {
2720 ret.value = error_mark_node;
2721 ret.original_code = ERROR_MARK;
6866c6e8 2722 ret.original_type = NULL;
ad97f4be
JM
2723 pop_maybe_used (false);
2724 }
2725 else
2726 {
928c19bb
JM
2727 bool expr_const_operands = true;
2728 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2729 &expr_const_operands);
c2255bc4 2730 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2731 c_last_sizeof_arg = expr.value;
2732 ret.original_code = SIZEOF_EXPR;
6866c6e8 2733 ret.original_type = NULL;
928c19bb 2734 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2735 {
2736 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2737 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2738 folded_expr, ret.value);
2739 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2740 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2741 }
928c19bb 2742 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2743 }
bc4b653b
JM
2744 return ret;
2745}
2746
2747/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2748 name passed to sizeof (rather than the type itself). LOC is the
2749 location of the original expression. */
bc4b653b
JM
2750
2751struct c_expr
c2255bc4 2752c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2753{
2754 tree type;
2755 struct c_expr ret;
928c19bb
JM
2756 tree type_expr = NULL_TREE;
2757 bool type_expr_const = true;
2758 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2759 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2760 c_last_sizeof_arg = type;
2761 ret.original_code = SIZEOF_EXPR;
6866c6e8 2762 ret.original_type = NULL;
24070fcb
JM
2763 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2764 && c_vla_type_p (type))
928c19bb 2765 {
24070fcb
JM
2766 /* If the type is a [*] array, it is a VLA but is represented as
2767 having a size of zero. In such a case we must ensure that
2768 the result of sizeof does not get folded to a constant by
2769 c_fully_fold, because if the size is evaluated the result is
2770 not constant and so constraints on zero or negative size
2771 arrays must not be applied when this sizeof call is inside
2772 another array declarator. */
2773 if (!type_expr)
2774 type_expr = integer_zero_node;
928c19bb
JM
2775 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2776 type_expr, ret.value);
2777 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2778 }
16464cc1
VR
2779 pop_maybe_used (type != error_mark_node
2780 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2781 return ret;
2782}
2783
400fbf9f 2784/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2785 The function call is at LOC.
400fbf9f
JW
2786 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2787 TREE_VALUE of each node is a parameter-expression.
2788 FUNCTION's data type may be a function type or a pointer-to-function. */
2789
2790tree
c2255bc4 2791build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2792{
9771b263 2793 vec<tree, va_gc> *v;
bbbbb16a
ILT
2794 tree ret;
2795
9771b263 2796 vec_alloc (v, list_length (params));
bbbbb16a 2797 for (; params; params = TREE_CHAIN (params))
9771b263
DN
2798 v->quick_push (TREE_VALUE (params));
2799 ret = build_function_call_vec (loc, function, v, NULL);
2800 vec_free (v);
bbbbb16a
ILT
2801 return ret;
2802}
2803
ae52741c
MLI
2804/* Give a note about the location of the declaration of DECL. */
2805
2806static void inform_declaration (tree decl)
2807{
2808 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2809 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2810}
2811
bbbbb16a
ILT
2812/* Build a function call to function FUNCTION with parameters PARAMS.
2813 ORIGTYPES, if not NULL, is a vector of types; each element is
2814 either NULL or the original type of the corresponding element in
2815 PARAMS. The original type may differ from TREE_TYPE of the
2816 parameter for enums. FUNCTION's data type may be a function type
2817 or pointer-to-function. This function changes the elements of
2818 PARAMS. */
2819
2820tree
9771b263
DN
2821build_function_call_vec (location_t loc, tree function,
2822 vec<tree, va_gc> *params,
2823 vec<tree, va_gc> *origtypes)
400fbf9f 2824{
b3694847 2825 tree fntype, fundecl = 0;
4977bab6 2826 tree name = NULL_TREE, result;
c96f4f73 2827 tree tem;
94a0dd7b
SL
2828 int nargs;
2829 tree *argarray;
b8698a0f 2830
400fbf9f 2831
fc76e425 2832 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2833 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2834
2835 /* Convert anything with function type to a pointer-to-function. */
2836 if (TREE_CODE (function) == FUNCTION_DECL)
2837 {
58646b77
PB
2838 /* Implement type-directed function overloading for builtins.
2839 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2840 handle all the type checking. The result is a complete expression
2841 that implements this function call. */
c2255bc4 2842 tem = resolve_overloaded_builtin (loc, function, params);
58646b77
PB
2843 if (tem)
2844 return tem;
48ae6c13 2845
400fbf9f 2846 name = DECL_NAME (function);
0a35513e
AH
2847
2848 if (flag_tm)
2849 tm_malloc_replacement (function);
a5eadacc 2850 fundecl = function;
86951993
AM
2851 /* Atomic functions have type checking/casting already done. They are
2852 often rewritten and don't match the original parameter list. */
2853 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2854 origtypes = NULL;
36536d79
BI
2855
2856 if (flag_enable_cilkplus
2857 && is_cilkplus_reduce_builtin (function))
2858 origtypes = NULL;
400fbf9f 2859 }
f2a71bbc 2860 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2861 function = function_to_pointer_conversion (loc, function);
400fbf9f 2862
6e955430
ZL
2863 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2864 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2865 if (params && !params->is_empty ())
2866 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2867
928c19bb
JM
2868 function = c_fully_fold (function, false, NULL);
2869
400fbf9f
JW
2870 fntype = TREE_TYPE (function);
2871
2872 if (TREE_CODE (fntype) == ERROR_MARK)
2873 return error_mark_node;
2874
2875 if (!(TREE_CODE (fntype) == POINTER_TYPE
2876 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2877 {
ae52741c
MLI
2878 if (!flag_diagnostics_show_caret)
2879 error_at (loc,
2880 "called object %qE is not a function or function pointer",
2881 function);
2882 else if (DECL_P (function))
2883 {
2884 error_at (loc,
2885 "called object %qD is not a function or function pointer",
2886 function);
2887 inform_declaration (function);
2888 }
2889 else
2890 error_at (loc,
2891 "called object is not a function or function pointer");
400fbf9f
JW
2892 return error_mark_node;
2893 }
2894
5ce89b2e
JM
2895 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2896 current_function_returns_abnormally = 1;
2897
400fbf9f
JW
2898 /* fntype now gets the type of function pointed to. */
2899 fntype = TREE_TYPE (fntype);
2900
ab4194da
JM
2901 /* Convert the parameters to the types declared in the
2902 function prototype, or apply default promotions. */
2903
bbbbb16a
ILT
2904 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2905 function, fundecl);
ab4194da
JM
2906 if (nargs < 0)
2907 return error_mark_node;
2908
c96f4f73
EB
2909 /* Check that the function is called through a compatible prototype.
2910 If it is not, replace the call by a trap, wrapped up in a compound
2911 expression if necessary. This has the nice side-effect to prevent
2912 the tree-inliner from generating invalid assignment trees which may
58393038 2913 blow up in the RTL expander later. */
1043771b 2914 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2915 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2916 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2917 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2918 {
2919 tree return_type = TREE_TYPE (fntype);
e79983f4
MM
2920 tree trap = build_function_call (loc,
2921 builtin_decl_explicit (BUILT_IN_TRAP),
c96f4f73 2922 NULL_TREE);
ab4194da 2923 int i;
c96f4f73
EB
2924
2925 /* This situation leads to run-time undefined behavior. We can't,
2926 therefore, simply error unless we can prove that all possible
2927 executions of the program must execute the code. */
c2255bc4 2928 if (warning_at (loc, 0, "function called through a non-compatible type"))
71205d17
MLI
2929 /* We can, however, treat "undefined" any way we please.
2930 Call abort to encourage the user to fix the program. */
c2255bc4 2931 inform (loc, "if this code is reached, the program will abort");
ab4194da
JM
2932 /* Before the abort, allow the function arguments to exit or
2933 call longjmp. */
2934 for (i = 0; i < nargs; i++)
9771b263 2935 trap = build2 (COMPOUND_EXPR, void_type_node, (*params)[i], trap);
bba745c1 2936
c96f4f73 2937 if (VOID_TYPE_P (return_type))
3ce62965
JM
2938 {
2939 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
db3927fb 2940 pedwarn (loc, 0,
3ce62965
JM
2941 "function with qualified void return type called");
2942 return trap;
2943 }
c96f4f73
EB
2944 else
2945 {
2946 tree rhs;
2947
2948 if (AGGREGATE_TYPE_P (return_type))
c2255bc4 2949 rhs = build_compound_literal (loc, return_type,
9771b263
DN
2950 build_constructor (return_type,
2951 NULL),
928c19bb 2952 false);
c96f4f73 2953 else
e8160c9a 2954 rhs = build_zero_cst (return_type);
c96f4f73 2955
3ce62965
JM
2956 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2957 trap, rhs));
c96f4f73
EB
2958 }
2959 }
2960
9771b263 2961 argarray = vec_safe_address (params);
bbbbb16a 2962
83322951
RG
2963 /* Check that arguments to builtin functions match the expectations. */
2964 if (fundecl
2965 && DECL_BUILT_IN (fundecl)
2966 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2967 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2968 return error_mark_node;
400fbf9f 2969
83322951 2970 /* Check that the arguments to the function are valid. */
dde05067 2971 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2972
928c19bb
JM
2973 if (name != NULL_TREE
2974 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2975 {
928c19bb 2976 if (require_constant_value)
b8698a0f 2977 result =
db3927fb
AH
2978 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2979 function, nargs, argarray);
928c19bb 2980 else
db3927fb
AH
2981 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2982 function, nargs, argarray);
928c19bb
JM
2983 if (TREE_CODE (result) == NOP_EXPR
2984 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2985 STRIP_TYPE_NOPS (result);
bf730f15
RS
2986 }
2987 else
db3927fb
AH
2988 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2989 function, nargs, argarray);
b0b3afb2 2990
71653180 2991 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2992 {
2993 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2994 pedwarn (loc, 0,
3ce62965
JM
2995 "function with qualified void return type called");
2996 return result;
2997 }
1eb8759b 2998 return require_complete_type (result);
400fbf9f
JW
2999}
3000\f
bbbbb16a
ILT
3001/* Convert the argument expressions in the vector VALUES
3002 to the types in the list TYPELIST.
400fbf9f
JW
3003
3004 If TYPELIST is exhausted, or when an element has NULL as its type,
3005 perform the default conversions.
3006
bbbbb16a
ILT
3007 ORIGTYPES is the original types of the expressions in VALUES. This
3008 holds the type of enum values which have been converted to integral
3009 types. It may be NULL.
400fbf9f 3010
03dafa61
JM
3011 FUNCTION is a tree for the called function. It is used only for
3012 error messages, where it is formatted with %qE.
400fbf9f
JW
3013
3014 This is also where warnings about wrong number of args are generated.
3015
94a0dd7b 3016 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3017 than the length of VALUES in some error situations), or -1 on
3018 failure. */
94a0dd7b
SL
3019
3020static int
9771b263
DN
3021convert_arguments (tree typelist, vec<tree, va_gc> *values,
3022 vec<tree, va_gc> *origtypes, tree function, tree fundecl)
400fbf9f 3023{
bbbbb16a
ILT
3024 tree typetail, val;
3025 unsigned int parmnum;
06302a02 3026 bool error_args = false;
b5d32c25
KG
3027 const bool type_generic = fundecl
3028 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
8ce94e44 3029 bool type_generic_remove_excess_precision = false;
03dafa61 3030 tree selector;
03dafa61 3031
2ac2f164
JM
3032 /* Change pointer to function to the function itself for
3033 diagnostics. */
03dafa61
JM
3034 if (TREE_CODE (function) == ADDR_EXPR
3035 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3036 function = TREE_OPERAND (function, 0);
03dafa61
JM
3037
3038 /* Handle an ObjC selector specially for diagnostics. */
3039 selector = objc_message_selector ();
400fbf9f 3040
8ce94e44
JM
3041 /* For type-generic built-in functions, determine whether excess
3042 precision should be removed (classification) or not
3043 (comparison). */
3044 if (type_generic
3045 && DECL_BUILT_IN (fundecl)
3046 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3047 {
3048 switch (DECL_FUNCTION_CODE (fundecl))
3049 {
3050 case BUILT_IN_ISFINITE:
3051 case BUILT_IN_ISINF:
3052 case BUILT_IN_ISINF_SIGN:
3053 case BUILT_IN_ISNAN:
3054 case BUILT_IN_ISNORMAL:
3055 case BUILT_IN_FPCLASSIFY:
3056 type_generic_remove_excess_precision = true;
3057 break;
3058
3059 default:
3060 type_generic_remove_excess_precision = false;
3061 break;
3062 }
3063 }
6d6efbb3
BI
3064 if (flag_enable_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3065 return vec_safe_length (values);
8ce94e44 3066
400fbf9f 3067 /* Scan the given expressions and types, producing individual
bbbbb16a 3068 converted arguments. */
400fbf9f 3069
bbbbb16a 3070 for (typetail = typelist, parmnum = 0;
9771b263 3071 values && values->iterate (parmnum, &val);
bbbbb16a 3072 ++parmnum)
400fbf9f 3073 {
b3694847 3074 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3075 tree valtype = TREE_TYPE (val);
03dafa61
JM
3076 tree rname = function;
3077 int argnum = parmnum + 1;
4d3e6fae 3078 const char *invalid_func_diag;
8ce94e44 3079 bool excess_precision = false;
928c19bb 3080 bool npc;
bbbbb16a 3081 tree parmval;
400fbf9f
JW
3082
3083 if (type == void_type_node)
3084 {
19dc6d01
NP
3085 if (selector)
3086 error_at (input_location,
3087 "too many arguments to method %qE", selector);
3088 else
3089 error_at (input_location,
3090 "too many arguments to function %qE", function);
ae52741c 3091 inform_declaration (fundecl);
94a0dd7b 3092 return parmnum;
400fbf9f
JW
3093 }
3094
03dafa61
JM
3095 if (selector && argnum > 2)
3096 {
3097 rname = selector;
3098 argnum -= 2;
3099 }
3100
928c19bb 3101 npc = null_pointer_constant_p (val);
8ce94e44
JM
3102
3103 /* If there is excess precision and a prototype, convert once to
3104 the required type rather than converting via the semantic
3105 type. Likewise without a prototype a float value represented
3106 as long double should be converted once to double. But for
3107 type-generic classification functions excess precision must
3108 be removed here. */
3109 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3110 && (type || !type_generic || !type_generic_remove_excess_precision))
3111 {
3112 val = TREE_OPERAND (val, 0);
3113 excess_precision = true;
3114 }
928c19bb 3115 val = c_fully_fold (val, false, NULL);
ed248cf7 3116 STRIP_TYPE_NOPS (val);
400fbf9f 3117
400fbf9f
JW
3118 val = require_complete_type (val);
3119
3120 if (type != 0)
3121 {
3122 /* Formal parm type is specified by a function prototype. */
400fbf9f 3123
20913689 3124 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f
JW
3125 {
3126 error ("type of formal parameter %d is incomplete", parmnum + 1);
3127 parmval = val;
3128 }
3129 else
3130 {
bbbbb16a
ILT
3131 tree origtype;
3132
d45cf215
RS
3133 /* Optionally warn about conversions that
3134 differ from the default conversions. */
05170031 3135 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3136 {
e3a64162 3137 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3138
aae43c5f 3139 if (INTEGRAL_TYPE_P (type)
8ce94e44 3140 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3141 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3142 "rather than floating due to prototype",
3143 argnum, rname);
03829ad2 3144 if (INTEGRAL_TYPE_P (type)
8ce94e44 3145 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3146 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3147 "rather than complex due to prototype",
3148 argnum, rname);
aae43c5f 3149 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3150 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3151 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3152 "rather than floating due to prototype",
3153 argnum, rname);
400fbf9f 3154 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3155 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3156 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3157 "rather than integer due to prototype",
3158 argnum, rname);
03829ad2 3159 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3160 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3161 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3162 "rather than integer due to prototype",
3163 argnum, rname);
aae43c5f 3164 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3165 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3166 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3167 "rather than complex due to prototype",
3168 argnum, rname);
aae43c5f
RK
3169 /* ??? At some point, messages should be written about
3170 conversions between complex types, but that's too messy
3171 to do now. */
d45cf215 3172 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3173 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3174 {
3175 /* Warn if any argument is passed as `float',
047de90b 3176 since without a prototype it would be `double'. */
9a8ce21f
JG
3177 if (formal_prec == TYPE_PRECISION (float_type_node)
3178 && type != dfloat32_type_node)
d4ee4d25 3179 warning (0, "passing argument %d of %qE as %<float%> "
03dafa61
JM
3180 "rather than %<double%> due to prototype",
3181 argnum, rname);
9a8ce21f
JG
3182
3183 /* Warn if mismatch between argument and prototype
3184 for decimal float types. Warn of conversions with
3185 binary float types and of precision narrowing due to
3186 prototype. */
8ce94e44 3187 else if (type != valtype
9a8ce21f
JG
3188 && (type == dfloat32_type_node
3189 || type == dfloat64_type_node
c22cacf3 3190 || type == dfloat128_type_node
8ce94e44
JM
3191 || valtype == dfloat32_type_node
3192 || valtype == dfloat64_type_node
3193 || valtype == dfloat128_type_node)
c22cacf3 3194 && (formal_prec
8ce94e44 3195 <= TYPE_PRECISION (valtype)
9a8ce21f 3196 || (type == dfloat128_type_node
8ce94e44 3197 && (valtype
c22cacf3 3198 != dfloat64_type_node
8ce94e44 3199 && (valtype
9a8ce21f
JG
3200 != dfloat32_type_node)))
3201 || (type == dfloat64_type_node
8ce94e44 3202 && (valtype
9a8ce21f
JG
3203 != dfloat32_type_node))))
3204 warning (0, "passing argument %d of %qE as %qT "
3205 "rather than %qT due to prototype",
8ce94e44 3206 argnum, rname, type, valtype);
9a8ce21f 3207
d45cf215 3208 }
3ed56f8a
KG
3209 /* Detect integer changing in width or signedness.
3210 These warnings are only activated with
05170031
MLI
3211 -Wtraditional-conversion, not with -Wtraditional. */
3212 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3213 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3214 {
d45cf215
RS
3215 tree would_have_been = default_conversion (val);
3216 tree type1 = TREE_TYPE (would_have_been);
3217
754a4d82 3218 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3219 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3220 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3221 /* No warning if function asks for enum
3222 and the actual arg is that enum type. */
3223 ;
3224 else if (formal_prec != TYPE_PRECISION (type1))
c2255bc4
AH
3225 warning (OPT_Wtraditional_conversion,
3226 "passing argument %d of %qE "
3176a0c2
DD
3227 "with different width due to prototype",
3228 argnum, rname);
8df83eae 3229 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3230 ;
800cd3b9
RS
3231 /* Don't complain if the formal parameter type
3232 is an enum, because we can't tell now whether
3233 the value was an enum--even the same enum. */
3234 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3235 ;
400fbf9f
JW
3236 else if (TREE_CODE (val) == INTEGER_CST
3237 && int_fits_type_p (val, type))
3238 /* Change in signedness doesn't matter
3239 if a constant value is unaffected. */
3240 ;
ce9895ae
RS
3241 /* If the value is extended from a narrower
3242 unsigned type, it doesn't matter whether we
3243 pass it as signed or unsigned; the value
3244 certainly is the same either way. */
8ce94e44
JM
3245 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3246 && TYPE_UNSIGNED (valtype))
ce9895ae 3247 ;
8df83eae 3248 else if (TYPE_UNSIGNED (type))
c2255bc4
AH
3249 warning (OPT_Wtraditional_conversion,
3250 "passing argument %d of %qE "
3176a0c2
DD
3251 "as unsigned due to prototype",
3252 argnum, rname);
3ed56f8a 3253 else
c2255bc4
AH
3254 warning (OPT_Wtraditional_conversion,
3255 "passing argument %d of %qE "
3176a0c2 3256 "as signed due to prototype", argnum, rname);
400fbf9f
JW
3257 }
3258 }
3259
8ce94e44
JM
3260 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3261 sake of better warnings from convert_and_check. */
3262 if (excess_precision)
3263 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3264 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
744aa42f
ILT
3265 parmval = convert_for_assignment (input_location, type, val,
3266 origtype, ic_argpass, npc,
2ac2f164
JM
3267 fundecl, function,
3268 parmnum + 1);
2f6e4e97 3269
61f71b34 3270 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3271 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3272 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3273 parmval = default_conversion (parmval);
400fbf9f 3274 }
400fbf9f 3275 }
8ce94e44
JM
3276 else if (TREE_CODE (valtype) == REAL_TYPE
3277 && (TYPE_PRECISION (valtype)
2531a1d9 3278 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3279 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3280 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3281 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3282 {
3283 if (type_generic)
bbbbb16a 3284 parmval = val;
b5d32c25 3285 else
0a0b3574
MM
3286 {
3287 /* Convert `float' to `double'. */
3288 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3289 warning (OPT_Wdouble_promotion,
3290 "implicit conversion from %qT to %qT when passing "
3291 "argument to function",
3292 valtype, double_type_node);
3293 parmval = convert (double_type_node, val);
3294 }
b5d32c25 3295 }
8ce94e44
JM
3296 else if (excess_precision && !type_generic)
3297 /* A "double" argument with excess precision being passed
3298 without a prototype or in variable arguments. */
bbbbb16a 3299 parmval = convert (valtype, val);
c22cacf3
MS
3300 else if ((invalid_func_diag =
3301 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3302 {
3303 error (invalid_func_diag);
94a0dd7b 3304 return -1;
4d3e6fae 3305 }
400fbf9f
JW
3306 else
3307 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3308 parmval = default_conversion (val);
3309
9771b263 3310 (*values)[parmnum] = parmval;
06302a02
JJ
3311 if (parmval == error_mark_node)
3312 error_args = true;
400fbf9f
JW
3313
3314 if (typetail)
3315 typetail = TREE_CHAIN (typetail);
3316 }
3317
9771b263 3318 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3319
400fbf9f 3320 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3321 {
6d6efbb3
BI
3322 error_at (input_location,
3323 "too few arguments to function %qE", function);
3324 inform_declaration (fundecl);
3325 return -1;
3789b316 3326 }
400fbf9f 3327
06302a02 3328 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3329}
3330\f
43f6dfd3
RS
3331/* This is the entry point used by the parser to build unary operators
3332 in the input. CODE, a tree_code, specifies the unary operator, and
3333 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3334 CONVERT_EXPR for code.
3335
3336 LOC is the location to use for the tree generated.
3337*/
43f6dfd3
RS
3338
3339struct c_expr
c2255bc4 3340parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3341{
3342 struct c_expr result;
3343
c9f9eb5d 3344 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3345 result.original_code = code;
6866c6e8
ILT
3346 result.original_type = NULL;
3347
59c0753d 3348 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3349 overflow_warning (loc, result.value);
59c0753d 3350
43f6dfd3
RS
3351 return result;
3352}
3353
3354/* This is the entry point used by the parser to build binary operators
3355 in the input. CODE, a tree_code, specifies the binary operator, and
3356 ARG1 and ARG2 are the operands. In addition to constructing the
3357 expression, we check for operands that were written with other binary
ba47d38d
AH
3358 operators in a way that is likely to confuse the user.
3359
3360 LOCATION is the location of the binary operator. */
edc7c4ec 3361
487a92fe 3362struct c_expr
ba47d38d
AH
3363parser_build_binary_op (location_t location, enum tree_code code,
3364 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3365{
487a92fe 3366 struct c_expr result;
400fbf9f 3367
487a92fe
JM
3368 enum tree_code code1 = arg1.original_code;
3369 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3370 tree type1 = (arg1.original_type
3371 ? arg1.original_type
3372 : TREE_TYPE (arg1.value));
3373 tree type2 = (arg2.original_type
3374 ? arg2.original_type
3375 : TREE_TYPE (arg2.value));
400fbf9f 3376
ba47d38d
AH
3377 result.value = build_binary_op (location, code,
3378 arg1.value, arg2.value, 1);
487a92fe 3379 result.original_code = code;
6866c6e8 3380 result.original_type = NULL;
58bf601b 3381
487a92fe
JM
3382 if (TREE_CODE (result.value) == ERROR_MARK)
3383 return result;
400fbf9f 3384
ba47d38d
AH
3385 if (location != UNKNOWN_LOCATION)
3386 protected_set_expr_location (result.value, location);
3387
400fbf9f 3388 /* Check for cases such as x+y<<z which users are likely
487a92fe 3389 to misinterpret. */
400fbf9f 3390 if (warn_parentheses)
5d9de0d0
PC
3391 warn_about_parentheses (input_location, code,
3392 code1, arg1.value, code2, arg2.value);
001af587 3393
ca409efd 3394 if (warn_logical_op)
a243fb4a 3395 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
ca409efd 3396 code1, arg1.value, code2, arg2.value);
63a08740 3397
e994a705
RS
3398 /* Warn about comparisons against string literals, with the exception
3399 of testing for equality or inequality of a string literal with NULL. */
3400 if (code == EQ_EXPR || code == NE_EXPR)
3401 {
3402 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3403 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3404 warning_at (location, OPT_Waddress,
3405 "comparison with string literal results in unspecified behavior");
e994a705
RS
3406 }
3407 else if (TREE_CODE_CLASS (code) == tcc_comparison
3408 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3409 warning_at (location, OPT_Waddress,
3410 "comparison with string literal results in unspecified behavior");
e994a705 3411
b8698a0f
L
3412 if (TREE_OVERFLOW_P (result.value)
3413 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3414 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3415 overflow_warning (location, result.value);
400fbf9f 3416
6866c6e8
ILT
3417 /* Warn about comparisons of different enum types. */
3418 if (warn_enum_compare
3419 && TREE_CODE_CLASS (code) == tcc_comparison
3420 && TREE_CODE (type1) == ENUMERAL_TYPE
3421 && TREE_CODE (type2) == ENUMERAL_TYPE
3422 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3423 warning_at (location, OPT_Wenum_compare,
3424 "comparison between %qT and %qT",
3425 type1, type2);
3426
400fbf9f
JW
3427 return result;
3428}
3e4093b6 3429\f
3e4093b6
RS
3430/* Return a tree for the difference of pointers OP0 and OP1.
3431 The resulting tree has type int. */
293c9fdd 3432
3e4093b6 3433static tree
db3927fb 3434pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3435{
3e4093b6 3436 tree restype = ptrdiff_type_node;
36c5e70a 3437 tree result, inttype;
400fbf9f 3438
36c5e70a
BE
3439 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3440 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6
RS
3441 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3442 tree con0, con1, lit0, lit1;
3443 tree orig_op1 = op1;
400fbf9f 3444
36c5e70a
BE
3445 /* If the operands point into different address spaces, we need to
3446 explicitly convert them to pointers into the common address space
3447 before we can subtract the numerical address values. */
3448 if (as0 != as1)
3449 {
3450 addr_space_t as_common;
3451 tree common_type;
3452
3453 /* Determine the common superset address space. This is guaranteed
3454 to exist because the caller verified that comp_target_types
3455 returned non-zero. */
3456 if (!addr_space_superset (as0, as1, &as_common))
3457 gcc_unreachable ();
3458
3459 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3460 op0 = convert (common_type, op0);
3461 op1 = convert (common_type, op1);
3462 }
3463
3464 /* Determine integer type to perform computations in. This will usually
3465 be the same as the result type (ptrdiff_t), but may need to be a wider
3466 type if pointers for the address space are wider than ptrdiff_t. */
3467 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3468 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3469 else
3470 inttype = restype;
3471
3472
fcf73884 3473 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3474 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3475 "pointer of type %<void *%> used in subtraction");
3476 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3477 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3478 "pointer to a function used in subtraction");
400fbf9f 3479
3e4093b6
RS
3480 /* If the conversion to ptrdiff_type does anything like widening or
3481 converting a partial to an integral mode, we get a convert_expression
3482 that is in the way to do any simplifications.
3483 (fold-const.c doesn't know that the extra bits won't be needed.
3484 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3485 different mode in place.)
3486 So first try to find a common term here 'by hand'; we want to cover
3487 at least the cases that occur in legal static initializers. */
1043771b 3488 if (CONVERT_EXPR_P (op0)
1344f9a3
JM
3489 && (TYPE_PRECISION (TREE_TYPE (op0))
3490 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3491 con0 = TREE_OPERAND (op0, 0);
3492 else
3493 con0 = op0;
1043771b 3494 if (CONVERT_EXPR_P (op1)
1344f9a3
JM
3495 && (TYPE_PRECISION (TREE_TYPE (op1))
3496 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3497 con1 = TREE_OPERAND (op1, 0);
3498 else
3499 con1 = op1;
400fbf9f 3500
5de72424 3501 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3e4093b6
RS
3502 {
3503 lit0 = TREE_OPERAND (con0, 1);
3504 con0 = TREE_OPERAND (con0, 0);
3505 }
3506 else
3507 lit0 = integer_zero_node;
400fbf9f 3508
5de72424 3509 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
400fbf9f 3510 {
3e4093b6
RS
3511 lit1 = TREE_OPERAND (con1, 1);
3512 con1 = TREE_OPERAND (con1, 0);
400fbf9f
JW
3513 }
3514 else
3e4093b6
RS
3515 lit1 = integer_zero_node;
3516
3517 if (operand_equal_p (con0, con1, 0))
400fbf9f 3518 {
3e4093b6
RS
3519 op0 = lit0;
3520 op1 = lit1;
400fbf9f
JW
3521 }
3522
400fbf9f 3523
3e4093b6
RS
3524 /* First do the subtraction as integers;
3525 then drop through to build the divide operator.
3526 Do not do default conversions on the minus operator
3527 in case restype is a short type. */
400fbf9f 3528
db3927fb 3529 op0 = build_binary_op (loc,
36c5e70a
BE
3530 MINUS_EXPR, convert (inttype, op0),
3531 convert (inttype, op1), 0);
3e4093b6
RS
3532 /* This generates an error if op1 is pointer to incomplete type. */
3533 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3534 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3535
3e4093b6
RS
3536 /* This generates an error if op0 is pointer to incomplete type. */
3537 op1 = c_size_in_bytes (target_type);
400fbf9f 3538
3e4093b6 3539 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3540 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3541 op0, convert (inttype, op1));
3542
3543 /* Convert to final result type if necessary. */
3544 return convert (restype, result);
3e4093b6
RS
3545}
3546\f
267bac10
JM
3547/* Expand atomic compound assignments into an approriate sequence as
3548 specified by the C11 standard section 6.5.16.2.
3549 given
3550 _Atomic T1 E1
3551 T2 E2
3552 E1 op= E2
3553
3554 This sequence is used for all types for which these operations are
3555 supported.
3556
3557 In addition, built-in versions of the 'fe' prefixed routines may
3558 need to be invoked for floating point (real, complex or vector) when
3559 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3560
3561 T1 newval;
3562 T1 old;
3563 T1 *addr
3564 T2 val
3565 fenv_t fenv
3566
3567 addr = &E1;
3568 val = (E2);
3569 __atomic_load (addr, &old, SEQ_CST);
3570 feholdexcept (&fenv);
3571loop:
3572 newval = old op val;
3573 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3574 SEQ_CST))
3575 goto done;
3576 feclearexcept (FE_ALL_EXCEPT);
3577 goto loop:
3578done:
3579 feupdateenv (&fenv);
3580
3581 Also note that the compiler is simply issuing the generic form of
3582 the atomic operations. This requires temp(s) and has their address
3583 taken. The atomic processing is smart enough to figure out when the
3584 size of an object can utilize a lock-free version, and convert the
3585 built-in call to the appropriate lock-free routine. The optimizers
3586 will then dispose of any temps that are no longer required, and
3587 lock-free implementations are utilized as long as there is target
3588 support for the required size.
3589
3590 If the operator is NOP_EXPR, then this is a simple assignment, and
3591 an __atomic_store is issued to perform the assignment rather than
3592 the above loop.
3593
3594*/
3595
3596/* Build an atomic assignment at LOC, expanding into the proper
3597 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3598 the result of the operation, unless RETURN_OLD_P in which case
3599 return the old value of LHS (this is only for postincrement and
3600 postdecrement). */
3601static tree
3602build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3603 tree rhs, bool return_old_p)
3604{
3605 tree fndecl, func_call;
3606 vec<tree, va_gc> *params;
3607 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3608 tree old, old_addr;
3609 tree compound_stmt;
3610 tree stmt, goto_stmt;
3611 tree loop_label, loop_decl, done_label, done_decl;
3612
3613 tree lhs_type = TREE_TYPE (lhs);
3614 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3615 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3616 tree rhs_type = TREE_TYPE (rhs);
3617
3618 gcc_assert (TYPE_ATOMIC (lhs_type));
3619
3620 if (return_old_p)
3621 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3622
3623 /* Allocate enough vector items for a compare_exchange. */
3624 vec_alloc (params, 6);
3625
3626 /* Create a compound statement to hold the sequence of statements
3627 with a loop. */
3628 compound_stmt = c_begin_compound_stmt (false);
3629
3630 /* Fold the RHS if it hasn't already been folded. */
3631 if (modifycode != NOP_EXPR)
3632 rhs = c_fully_fold (rhs, false, NULL);
3633
3634 /* Remove the qualifiers for the rest of the expressions and create
3635 the VAL temp variable to hold the RHS. */
3636 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3637 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3638 val = create_tmp_var (nonatomic_rhs_type, NULL);
3639 TREE_ADDRESSABLE (val) = 1;
3640 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3641 SET_EXPR_LOCATION (rhs, loc);
3642 add_stmt (rhs);
3643
3644 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3645 an atomic_store. */
3646 if (modifycode == NOP_EXPR)
3647 {
3648 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3649 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3650 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3651 params->quick_push (lhs_addr);
3652 params->quick_push (rhs);
3653 params->quick_push (seq_cst);
3654 func_call = build_function_call_vec (loc, fndecl, params, NULL);
3655 add_stmt (func_call);
3656
3657 /* Finish the compound statement. */
3658 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3659
3660 /* VAL is the value which was stored, return a COMPOUND_STMT of
3661 the statement and that value. */
3662 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3663 }
3664
3665 /* Create the variables and labels required for the op= form. */
3666 old = create_tmp_var (nonatomic_lhs_type, NULL);
3667 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3668 TREE_ADDRESSABLE (val) = 1;
3669
3670 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3671 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3672 TREE_ADDRESSABLE (newval) = 1;
3673
3674 loop_decl = create_artificial_label (loc);
3675 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3676
3677 done_decl = create_artificial_label (loc);
3678 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3679
3680 /* __atomic_load (addr, &old, SEQ_CST). */
3681 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3682 params->quick_push (lhs_addr);
3683 params->quick_push (old_addr);
3684 params->quick_push (seq_cst);
3685 func_call = build_function_call_vec (loc, fndecl, params, NULL);
3686 add_stmt (func_call);
3687 params->truncate (0);
3688
3689 /* Create the expressions for floating-point environment
3690 manipulation, if required. */
3691 bool need_fenv = (flag_trapping_math
3692 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3693 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3694 if (need_fenv)
3695 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3696
3697 if (hold_call)
3698 add_stmt (hold_call);
3699
3700 /* loop: */
3701 add_stmt (loop_label);
3702
3703 /* newval = old + val; */
3704 rhs = build_binary_op (loc, modifycode, old, val, 1);
3705 rhs = convert_for_assignment (loc, nonatomic_lhs_type, rhs, NULL_TREE,
3706 ic_assign, false, NULL_TREE,
3707 NULL_TREE, 0);
3708 if (rhs != error_mark_node)
3709 {
3710 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3711 SET_EXPR_LOCATION (rhs, loc);
3712 add_stmt (rhs);
3713 }
3714
3715 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3716 goto done; */
3717 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3718 params->quick_push (lhs_addr);
3719 params->quick_push (old_addr);
3720 params->quick_push (newval_addr);
3721 params->quick_push (integer_zero_node);
3722 params->quick_push (seq_cst);
3723 params->quick_push (seq_cst);
3724 func_call = build_function_call_vec (loc, fndecl, params, NULL);
3725
3726 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3727 SET_EXPR_LOCATION (goto_stmt, loc);
3728
3729 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3730 SET_EXPR_LOCATION (stmt, loc);
3731 add_stmt (stmt);
3732
3733 if (clear_call)
3734 add_stmt (clear_call);
3735
3736 /* goto loop; */
3737 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3738 SET_EXPR_LOCATION (goto_stmt, loc);
3739 add_stmt (goto_stmt);
3740
3741 /* done: */
3742 add_stmt (done_label);
3743
3744 if (update_call)
3745 add_stmt (update_call);
3746
3747 /* Finish the compound statement. */
3748 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3749
3750 /* NEWVAL is the value that was successfully stored, return a
3751 COMPOUND_EXPR of the statement and the appropriate value. */
3752 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3753 return_old_p ? old : newval);
3754}
3755
3e4093b6
RS
3756/* Construct and perhaps optimize a tree representation
3757 for a unary operation. CODE, a tree_code, specifies the operation
3758 and XARG is the operand.
3759 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3760 the default promotions (such as from short to int).
3761 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3762 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3763 arrays to pointers in C99.
3764
3765 LOCATION is the location of the operator. */
400fbf9f 3766
3e4093b6 3767tree
c9f9eb5d
AH
3768build_unary_op (location_t location,
3769 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3770{
3771 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3772 tree arg = xarg;
3773 tree argtype = 0;
808d6eaa 3774 enum tree_code typecode;
3e4093b6 3775 tree val;
c9f9eb5d 3776 tree ret = error_mark_node;
8ce94e44 3777 tree eptype = NULL_TREE;
3e4093b6 3778 int noconvert = flag;
4de67c26 3779 const char *invalid_op_diag;
928c19bb
JM
3780 bool int_operands;
3781
3782 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3783 if (int_operands)
3784 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3785
808d6eaa
JM
3786 if (code != ADDR_EXPR)
3787 arg = require_complete_type (arg);
3788
3789 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3790 if (typecode == ERROR_MARK)
3791 return error_mark_node;
3792 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3793 typecode = INTEGER_TYPE;
6c36d76b 3794
4de67c26
JM
3795 if ((invalid_op_diag
3796 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3797 {
c9f9eb5d 3798 error_at (location, invalid_op_diag);
4de67c26
JM
3799 return error_mark_node;
3800 }
3801
8ce94e44
JM
3802 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3803 {
3804 eptype = TREE_TYPE (arg);
3805 arg = TREE_OPERAND (arg, 0);
3806 }
3807
3e4093b6
RS
3808 switch (code)
3809 {
3810 case CONVERT_EXPR:
3811 /* This is used for unary plus, because a CONVERT_EXPR
3812 is enough to prevent anybody from looking inside for
3813 associativity, but won't generate any code. */
3814 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3815 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3816 || typecode == VECTOR_TYPE))
400fbf9f 3817 {
c9f9eb5d 3818 error_at (location, "wrong type argument to unary plus");
3e4093b6 3819 return error_mark_node;
400fbf9f 3820 }
3e4093b6
RS
3821 else if (!noconvert)
3822 arg = default_conversion (arg);
db3927fb 3823 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3824 break;
3825
3e4093b6
RS
3826 case NEGATE_EXPR:
3827 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3828 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3829 || typecode == VECTOR_TYPE))
3830 {
c9f9eb5d 3831 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3832 return error_mark_node;
3833 }
3834 else if (!noconvert)
3835 arg = default_conversion (arg);
400fbf9f
JW
3836 break;
3837
3e4093b6 3838 case BIT_NOT_EXPR:
462643f0
AP
3839 /* ~ works on integer types and non float vectors. */
3840 if (typecode == INTEGER_TYPE
3841 || (typecode == VECTOR_TYPE
3842 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3843 {
3e4093b6
RS
3844 if (!noconvert)
3845 arg = default_conversion (arg);
03d5b1f5 3846 }
3e4093b6 3847 else if (typecode == COMPLEX_TYPE)
400fbf9f 3848 {
3e4093b6 3849 code = CONJ_EXPR;
c1771a20 3850 pedwarn (location, OPT_Wpedantic,
fcf73884 3851 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3852 if (!noconvert)
3853 arg = default_conversion (arg);
3854 }
3855 else
3856 {
c9f9eb5d 3857 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3858 return error_mark_node;
400fbf9f
JW
3859 }
3860 break;
3861
3e4093b6 3862 case ABS_EXPR:
11017cc7 3863 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3864 {
c9f9eb5d 3865 error_at (location, "wrong type argument to abs");
3e4093b6 3866 return error_mark_node;
400fbf9f 3867 }
3e4093b6
RS
3868 else if (!noconvert)
3869 arg = default_conversion (arg);
400fbf9f
JW
3870 break;
3871
3e4093b6
RS
3872 case CONJ_EXPR:
3873 /* Conjugating a real value is a no-op, but allow it anyway. */
3874 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3875 || typecode == COMPLEX_TYPE))
400fbf9f 3876 {
c9f9eb5d 3877 error_at (location, "wrong type argument to conjugation");
3e4093b6 3878 return error_mark_node;
400fbf9f 3879 }
3e4093b6
RS
3880 else if (!noconvert)
3881 arg = default_conversion (arg);
400fbf9f
JW
3882 break;
3883
3e4093b6 3884 case TRUTH_NOT_EXPR:
ab22c1fa 3885 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3886 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3887 && typecode != COMPLEX_TYPE)
400fbf9f 3888 {
c9f9eb5d
AH
3889 error_at (location,
3890 "wrong type argument to unary exclamation mark");
3e4093b6 3891 return error_mark_node;
400fbf9f 3892 }
a27d595d
JM
3893 if (int_operands)
3894 {
3895 arg = c_objc_common_truthvalue_conversion (location, xarg);
3896 arg = remove_c_maybe_const_expr (arg);
3897 }
3898 else
3899 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3900 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3901 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3902 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3903 location = EXPR_LOCATION (ret);
c9f9eb5d 3904 goto return_build_unary_op;
3e4093b6 3905
3e4093b6 3906 case REALPART_EXPR:
3e4093b6 3907 case IMAGPART_EXPR:
fb52b50a
NF
3908 ret = build_real_imag_expr (location, code, arg);
3909 if (ret == error_mark_node)
3910 return error_mark_node;
8ce94e44
JM
3911 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3912 eptype = TREE_TYPE (eptype);
c9f9eb5d 3913 goto return_build_unary_op;
3e4093b6
RS
3914
3915 case PREINCREMENT_EXPR:
3916 case POSTINCREMENT_EXPR:
3917 case PREDECREMENT_EXPR:
3918 case POSTDECREMENT_EXPR:
3e4093b6 3919
928c19bb
JM
3920 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3921 {
3922 tree inner = build_unary_op (location, code,
3923 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3924 if (inner == error_mark_node)
3925 return error_mark_node;
3926 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3927 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3928 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3929 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3930 goto return_build_unary_op;
3931 }
3932
925e8657
NP
3933 /* Complain about anything that is not a true lvalue. In
3934 Objective-C, skip this check for property_refs. */
f90e8e2e 3935 if (!objc_is_property_ref (arg)
7bd11157
TT
3936 && !lvalue_or_else (location,
3937 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3938 || code == POSTINCREMENT_EXPR)
3939 ? lv_increment
3940 : lv_decrement)))
928c19bb
JM
3941 return error_mark_node;
3942
09639a83
ILT
3943 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3944 {
3945 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3946 warning_at (location, OPT_Wc___compat,
3947 "increment of enumeration value is invalid in C++");
3948 else
3949 warning_at (location, OPT_Wc___compat,
3950 "decrement of enumeration value is invalid in C++");
3951 }
3952
928c19bb
JM
3953 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3954 arg = c_fully_fold (arg, false, NULL);
3955
267bac10
JM
3956 bool atomic_op;
3957 atomic_op = really_atomic_lvalue (arg);
3958
3e4093b6
RS
3959 /* Increment or decrement the real part of the value,
3960 and don't change the imaginary part. */
3961 if (typecode == COMPLEX_TYPE)
400fbf9f 3962 {
3e4093b6
RS
3963 tree real, imag;
3964
c1771a20 3965 pedwarn (location, OPT_Wpedantic,
509c9d60 3966 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3967
267bac10
JM
3968 if (!atomic_op)
3969 {
3970 arg = stabilize_reference (arg);
3971 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3972 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3973 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3974 if (real == error_mark_node || imag == error_mark_node)
3975 return error_mark_node;
3976 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3977 real, imag);
3978 goto return_build_unary_op;
3979 }
400fbf9f 3980 }
3e4093b6
RS
3981
3982 /* Report invalid types. */
3983
ab22c1fa 3984 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 3985 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 3986 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 3987 {
3e4093b6 3988 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 3989 error_at (location, "wrong type argument to increment");
c22cacf3 3990 else
c9f9eb5d 3991 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
3992
3993 return error_mark_node;
400fbf9f 3994 }
400fbf9f 3995
3e4093b6
RS
3996 {
3997 tree inc;
400fbf9f 3998
3e4093b6
RS
3999 argtype = TREE_TYPE (arg);
4000
4001 /* Compute the increment. */
4002
4003 if (typecode == POINTER_TYPE)
4004 {
4005 /* If pointer target is an undefined struct,
4006 we just cannot know how to do the arithmetic. */
b70cef5d 4007 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4008 {
4009 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d
AH
4010 error_at (location,
4011 "increment of pointer to unknown structure");
3e4093b6 4012 else
c9f9eb5d
AH
4013 error_at (location,
4014 "decrement of pointer to unknown structure");
3e4093b6 4015 }
b70cef5d
JJ
4016 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4017 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4018 {
3e4093b6 4019 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4020 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4021 "wrong type argument to increment");
3e4093b6 4022 else
44d90fe1 4023 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4024 "wrong type argument to decrement");
3e4093b6
RS
4025 }
4026
b70cef5d 4027 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4028 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4029 }
b70cef5d 4030 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4031 {
4032 /* For signed fract types, we invert ++ to -- or
4033 -- to ++, and change inc from 1 to -1, because
4034 it is not possible to represent 1 in signed fract constants.
4035 For unsigned fract types, the result always overflows and
4036 we get an undefined (original) or the maximum value. */
4037 if (code == PREINCREMENT_EXPR)
4038 code = PREDECREMENT_EXPR;
4039 else if (code == PREDECREMENT_EXPR)
4040 code = PREINCREMENT_EXPR;
4041 else if (code == POSTINCREMENT_EXPR)
4042 code = POSTDECREMENT_EXPR;
4043 else /* code == POSTDECREMENT_EXPR */
4044 code = POSTINCREMENT_EXPR;
4045
4046 inc = integer_minus_one_node;
4047 inc = convert (argtype, inc);
4048 }
3e4093b6 4049 else
5be014d5 4050 {
241b71bb
TV
4051 inc = VECTOR_TYPE_P (argtype)
4052 ? build_one_cst (argtype)
4053 : integer_one_node;
5be014d5
AP
4054 inc = convert (argtype, inc);
4055 }
3e4093b6 4056
925e8657
NP
4057 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4058 need to ask Objective-C to build the increment or decrement
4059 expression for it. */
4060 if (objc_is_property_ref (arg))
f90e8e2e 4061 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4062 arg, inc);
4063
3e4093b6 4064 /* Report a read-only lvalue. */
f37acdf9 4065 if (TYPE_READONLY (argtype))
953ff289 4066 {
c02065fc 4067 readonly_error (location, arg,
953ff289
DN
4068 ((code == PREINCREMENT_EXPR
4069 || code == POSTINCREMENT_EXPR)
4070 ? lv_increment : lv_decrement));
4071 return error_mark_node;
4072 }
f37acdf9
JM
4073 else if (TREE_READONLY (arg))
4074 readonly_warning (arg,
4075 ((code == PREINCREMENT_EXPR
4076 || code == POSTINCREMENT_EXPR)
4077 ? lv_increment : lv_decrement));
3e4093b6 4078
267bac10
JM
4079 /* If the argument is atomic, use the special code sequences for
4080 atomic compound assignment. */
4081 if (atomic_op)
4082 {
4083 arg = stabilize_reference (arg);
4084 ret = build_atomic_assign (location, arg,
4085 ((code == PREINCREMENT_EXPR
4086 || code == POSTINCREMENT_EXPR)
4087 ? PLUS_EXPR
4088 : MINUS_EXPR),
4089 (FRACT_MODE_P (TYPE_MODE (argtype))
4090 ? inc
4091 : integer_one_node),
4092 (code == POSTINCREMENT_EXPR
4093 || code == POSTDECREMENT_EXPR));
4094 goto return_build_unary_op;
4095 }
4096
3e4093b6
RS
4097 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4098 val = boolean_increment (code, arg);
4099 else
53fb4de3 4100 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4101 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4102 if (TREE_CODE (val) != code)
6de9cd9a 4103 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4104 ret = val;
4105 goto return_build_unary_op;
3e4093b6
RS
4106 }
4107
4108 case ADDR_EXPR:
4109 /* Note that this operation never does default_conversion. */
4110
2b4b7036
JM
4111 /* The operand of unary '&' must be an lvalue (which excludes
4112 expressions of type void), or, in C99, the result of a [] or
4113 unary '*' operator. */
4114 if (VOID_TYPE_P (TREE_TYPE (arg))
4115 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4116 && (TREE_CODE (arg) != INDIRECT_REF
4117 || !flag_isoc99))
4118 pedwarn (location, 0, "taking address of expression of type %<void%>");
4119
3e4093b6
RS
4120 /* Let &* cancel out to simplify resulting code. */
4121 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 4122 {
3e4093b6
RS
4123 /* Don't let this be an lvalue. */
4124 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4125 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4126 ret = TREE_OPERAND (arg, 0);
4127 goto return_build_unary_op;
400fbf9f 4128 }
1eb8759b 4129
7c672dfc 4130 /* For &x[y], return x+y */
3e4093b6 4131 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4132 {
f2a71bbc
JM
4133 tree op0 = TREE_OPERAND (arg, 0);
4134 if (!c_mark_addressable (op0))
3e4093b6 4135 return error_mark_node;
1eb8759b 4136 }
1eb8759b 4137
3e4093b6
RS
4138 /* Anything not already handled and not a true memory reference
4139 or a non-lvalue array is an error. */
4140 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4141 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4142 return error_mark_node;
b6a10c9f 4143
928c19bb
JM
4144 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4145 folding later. */
4146 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4147 {
4148 tree inner = build_unary_op (location, code,
4149 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4150 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4151 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4152 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4153 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4154 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4155 goto return_build_unary_op;
4156 }
4157
3e4093b6
RS
4158 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4159 argtype = TREE_TYPE (arg);
400fbf9f 4160
3e4093b6 4161 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4162 to which the address will point. This is only needed
f2c1da78 4163 for function types. */
6615c446 4164 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4165 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4166 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4167 {
4168 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4169 int quals = orig_quals;
4170
4171 if (TREE_READONLY (arg))
4172 quals |= TYPE_QUAL_CONST;
4173 if (TREE_THIS_VOLATILE (arg))
4174 quals |= TYPE_QUAL_VOLATILE;
4175
f2c1da78
JM
4176 argtype = c_build_qualified_type (argtype, quals);
4177 }
400fbf9f 4178
3e4093b6
RS
4179 if (!c_mark_addressable (arg))
4180 return error_mark_node;
400fbf9f 4181
abb54d14
JM
4182 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4183 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4184
5cc200fc 4185 argtype = build_pointer_type (argtype);
5e55f99d
RH
4186
4187 /* ??? Cope with user tricks that amount to offsetof. Delete this
4188 when we have proper support for integer constant expressions. */
4189 val = get_base_address (arg);
4190 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
4191 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4192 {
cf9e9959 4193 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4194 goto return_build_unary_op;
3aa2ddb8 4195 }
5e55f99d 4196
5cc200fc 4197 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4198
c9f9eb5d
AH
4199 ret = val;
4200 goto return_build_unary_op;
400fbf9f 4201
3e4093b6 4202 default:
1344f9a3 4203 gcc_unreachable ();
3e4093b6 4204 }
400fbf9f 4205
3e4093b6
RS
4206 if (argtype == 0)
4207 argtype = TREE_TYPE (arg);
928c19bb
JM
4208 if (TREE_CODE (arg) == INTEGER_CST)
4209 ret = (require_constant_value
db3927fb
AH
4210 ? fold_build1_initializer_loc (location, code, argtype, arg)
4211 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4212 else
4213 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4214 return_build_unary_op:
4215 gcc_assert (ret != error_mark_node);
928c19bb
JM
4216 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4217 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4218 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4219 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4220 ret = note_integer_operands (ret);
8ce94e44
JM
4221 if (eptype)
4222 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4223 protected_set_expr_location (ret, location);
4224 return ret;
3e4093b6 4225}
400fbf9f 4226
3e4093b6
RS
4227/* Return nonzero if REF is an lvalue valid for this language.
4228 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4229 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4230
1e4ae551 4231bool
58f9752a 4232lvalue_p (const_tree ref)
3e4093b6 4233{
58f9752a 4234 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4235
3e4093b6
RS
4236 switch (code)
4237 {
4238 case REALPART_EXPR:
4239 case IMAGPART_EXPR:
4240 case COMPONENT_REF:
4241 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4242
928c19bb
JM
4243 case C_MAYBE_CONST_EXPR:
4244 return lvalue_p (TREE_OPERAND (ref, 1));
4245
3e4093b6
RS
4246 case COMPOUND_LITERAL_EXPR:
4247 case STRING_CST:
4248 return 1;
400fbf9f 4249
3e4093b6
RS
4250 case INDIRECT_REF:
4251 case ARRAY_REF:
36536d79 4252 case ARRAY_NOTATION_REF:
3e4093b6
RS
4253 case VAR_DECL:
4254 case PARM_DECL:
4255 case RESULT_DECL:
4256 case ERROR_MARK:
4257 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4258 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4259
3e4093b6 4260 case BIND_EXPR:
3e4093b6 4261 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4262
3e4093b6
RS
4263 default:
4264 return 0;
4265 }
4266}
400fbf9f 4267\f
f37acdf9
JM
4268/* Give a warning for storing in something that is read-only in GCC
4269 terms but not const in ISO C terms. */
4270
4271static void
4272readonly_warning (tree arg, enum lvalue_use use)
4273{
4274 switch (use)
4275 {
4276 case lv_assign:
4277 warning (0, "assignment of read-only location %qE", arg);
4278 break;
4279 case lv_increment:
4280 warning (0, "increment of read-only location %qE", arg);
4281 break;
4282 case lv_decrement:
4283 warning (0, "decrement of read-only location %qE", arg);
4284 break;
4285 default:
4286 gcc_unreachable ();
4287 }
4288 return;
4289}
4290
37dc0d8d
JM
4291
4292/* Return nonzero if REF is an lvalue valid for this language;
4293 otherwise, print an error message and return zero. USE says
7bd11157
TT
4294 how the lvalue is being used and so selects the error message.
4295 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4296
4297static int
7bd11157 4298lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4299{
4300 int win = lvalue_p (ref);
4301
4302 if (!win)
7bd11157 4303 lvalue_error (loc, use);
37dc0d8d
JM
4304
4305 return win;
4306}
3e4093b6
RS
4307\f
4308/* Mark EXP saying that we need to be able to take the
4309 address of it; it should not be allocated in a register.
4310 Returns true if successful. */
54c93c30 4311
3e4093b6
RS
4312bool
4313c_mark_addressable (tree exp)
400fbf9f 4314{
3e4093b6 4315 tree x = exp;
95602da1 4316
3e4093b6
RS
4317 while (1)
4318 switch (TREE_CODE (x))
4319 {
4320 case COMPONENT_REF:
4321 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4322 {
0039fa55
AN
4323 error
4324 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4325 return false;
4326 }
95602da1 4327
3e4093b6 4328 /* ... fall through ... */
95602da1 4329
3e4093b6
RS
4330 case ADDR_EXPR:
4331 case ARRAY_REF:
4332 case REALPART_EXPR:
4333 case IMAGPART_EXPR:
4334 x = TREE_OPERAND (x, 0);
4335 break;
95602da1 4336
3e4093b6
RS
4337 case COMPOUND_LITERAL_EXPR:
4338 case CONSTRUCTOR:
4339 TREE_ADDRESSABLE (x) = 1;
4340 return true;
95602da1 4341
3e4093b6
RS
4342 case VAR_DECL:
4343 case CONST_DECL:
4344 case PARM_DECL:
4345 case RESULT_DECL:
5baeaac0 4346 if (C_DECL_REGISTER (x)
3e4093b6
RS
4347 && DECL_NONLOCAL (x))
4348 {
e697b20f 4349 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4350 {
0039fa55
AN
4351 error
4352 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4353 return false;
4354 }
509c9d60 4355 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4356 }
5baeaac0 4357 else if (C_DECL_REGISTER (x))
3e4093b6 4358 {
e697b20f 4359 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4360 error ("address of global register variable %qD requested", x);
4361 else
4362 error ("address of register variable %qD requested", x);
4363 return false;
3e4093b6 4364 }
400fbf9f 4365
3e4093b6
RS
4366 /* drops in */
4367 case FUNCTION_DECL:
4368 TREE_ADDRESSABLE (x) = 1;
4369 /* drops out */
4370 default:
4371 return true;
4372 }
4373}
4374\f
2d2e923f
MLI
4375/* Convert EXPR to TYPE, warning about conversion problems with
4376 constants. SEMANTIC_TYPE is the type this conversion would use
4377 without excess precision. If SEMANTIC_TYPE is NULL, this function
4378 is equivalent to convert_and_check. This function is a wrapper that
4379 handles conversions that may be different than
4380 the usual ones because of excess precision. */
4381
4382static tree
4383ep_convert_and_check (tree type, tree expr, tree semantic_type)
4384{
4385 if (TREE_TYPE (expr) == type)
4386 return expr;
4387
4388 if (!semantic_type)
4389 return convert_and_check (type, expr);
4390
4391 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4392 && TREE_TYPE (expr) != semantic_type)
4393 {
4394 /* For integers, we need to check the real conversion, not
4395 the conversion to the excess precision type. */
4396 expr = convert_and_check (semantic_type, expr);
4397 }
4398 /* Result type is the excess precision type, which should be
4399 large enough, so do not check. */
4400 return convert (type, expr);
4401}
4402
928c19bb
JM
4403/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4404 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4405 if folded to an integer constant then the unselected half may
4406 contain arbitrary operations not normally permitted in constant
c2255bc4 4407 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4408
4409tree
744aa42f 4410build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4411 tree op1, tree op1_original_type, tree op2,
4412 tree op2_original_type)
400fbf9f 4413{
3e4093b6
RS
4414 tree type1;
4415 tree type2;
4416 enum tree_code code1;
4417 enum tree_code code2;
4418 tree result_type = NULL;
2d2e923f 4419 tree semantic_result_type = NULL;
3e4093b6 4420 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4421 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4422 bool ifexp_int_operands;
928c19bb 4423 tree ret;
400fbf9f 4424
4d84fe7c
JM
4425 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4426 if (op1_int_operands)
4427 op1 = remove_c_maybe_const_expr (op1);
4428 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4429 if (op2_int_operands)
4430 op2 = remove_c_maybe_const_expr (op2);
4431 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4432 if (ifexp_int_operands)
4433 ifexp = remove_c_maybe_const_expr (ifexp);
4434
3e4093b6
RS
4435 /* Promote both alternatives. */
4436
4437 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4438 op1 = default_conversion (op1);
4439 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4440 op2 = default_conversion (op2);
4441
4442 if (TREE_CODE (ifexp) == ERROR_MARK
4443 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4444 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4445 return error_mark_node;
400fbf9f 4446
3e4093b6
RS
4447 type1 = TREE_TYPE (op1);
4448 code1 = TREE_CODE (type1);
4449 type2 = TREE_TYPE (op2);
4450 code2 = TREE_CODE (type2);
4451
b1adf557
JM
4452 /* C90 does not permit non-lvalue arrays in conditional expressions.
4453 In C99 they will be pointers by now. */
4454 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4455 {
744aa42f 4456 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4457 return error_mark_node;
4458 }
4459
8ce94e44
JM
4460 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4461 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4462 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4463 || code1 == COMPLEX_TYPE)
4464 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4465 || code2 == COMPLEX_TYPE))
4466 {
2d2e923f 4467 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4468 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4469 {
4470 op1 = TREE_OPERAND (op1, 0);
4471 type1 = TREE_TYPE (op1);
4472 gcc_assert (TREE_CODE (type1) == code1);
4473 }
4474 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4475 {
4476 op2 = TREE_OPERAND (op2, 0);
4477 type2 = TREE_TYPE (op2);
4478 gcc_assert (TREE_CODE (type2) == code2);
4479 }
4480 }
4481
d130ae11
ILT
4482 if (warn_cxx_compat)
4483 {
4484 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4485 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4486
4487 if (TREE_CODE (t1) == ENUMERAL_TYPE
4488 && TREE_CODE (t2) == ENUMERAL_TYPE
4489 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4490 warning_at (colon_loc, OPT_Wc___compat,
4491 ("different enum types in conditional is "
4492 "invalid in C++: %qT vs %qT"),
4493 t1, t2);
4494 }
4495
3e4093b6
RS
4496 /* Quickly detect the usual case where op1 and op2 have the same type
4497 after promotion. */
4498 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4499 {
3e4093b6
RS
4500 if (type1 == type2)
4501 result_type = type1;
4502 else
4503 result_type = TYPE_MAIN_VARIANT (type1);
4504 }
4505 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4506 || code1 == COMPLEX_TYPE)
4507 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4508 || code2 == COMPLEX_TYPE))
3e4093b6 4509 {
ccf7f880 4510 result_type = c_common_type (type1, type2);
0a0b3574
MM
4511 do_warn_double_promotion (result_type, type1, type2,
4512 "implicit conversion from %qT to %qT to "
4513 "match other result of conditional",
4514 colon_loc);
400fbf9f 4515
3e4093b6
RS
4516 /* If -Wsign-compare, warn here if type1 and type2 have
4517 different signedness. We'll promote the signed to unsigned
4518 and later code won't know it used to be different.
4519 Do this check on the original types, so that explicit casts
4520 will be considered, but default promotions won't. */
7d882b83 4521 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4522 {
8df83eae
RK
4523 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4524 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4525
3e4093b6
RS
4526 if (unsigned_op1 ^ unsigned_op2)
4527 {
6ac01510
ILT
4528 bool ovf;
4529
3e4093b6
RS
4530 /* Do not warn if the result type is signed, since the
4531 signed type will only be chosen if it can represent
4532 all the values of the unsigned type. */
3f75a254 4533 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4534 /* OK */;
3e4093b6 4535 else
928c19bb
JM
4536 {
4537 bool op1_maybe_const = true;
4538 bool op2_maybe_const = true;
4539
4540 /* Do not warn if the signed quantity is an
4541 unsuffixed integer literal (or some static
4542 constant expression involving such literals) and
4543 it is non-negative. This warning requires the
4544 operands to be folded for best results, so do
4545 that folding in this case even without
4546 warn_sign_compare to avoid warning options
4547 possibly affecting code generation. */
f5178456
RS
4548 c_inhibit_evaluation_warnings
4549 += (ifexp == truthvalue_false_node);
928c19bb
JM
4550 op1 = c_fully_fold (op1, require_constant_value,
4551 &op1_maybe_const);
f5178456
RS
4552 c_inhibit_evaluation_warnings
4553 -= (ifexp == truthvalue_false_node);
4554
4555 c_inhibit_evaluation_warnings
4556 += (ifexp == truthvalue_true_node);
928c19bb
JM
4557 op2 = c_fully_fold (op2, require_constant_value,
4558 &op2_maybe_const);
f5178456
RS
4559 c_inhibit_evaluation_warnings
4560 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4561
4562 if (warn_sign_compare)
4563 {
4564 if ((unsigned_op2
4565 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4566 || (unsigned_op1
4567 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4568 /* OK */;
4569 else
744aa42f
ILT
4570 warning_at (colon_loc, OPT_Wsign_compare,
4571 ("signed and unsigned type in "
4572 "conditional expression"));
928c19bb
JM
4573 }
4574 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4575 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4576 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4577 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4578 }
3e4093b6
RS
4579 }
4580 }
4581 }
4582 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4583 {
fcf73884 4584 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4585 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4586 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4587 result_type = void_type_node;
4588 }
4589 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4590 {
36c5e70a
BE
4591 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4592 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4593 addr_space_t as_common;
4594
744aa42f 4595 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4596 result_type = common_pointer_type (type1, type2);
6aa3c60d 4597 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4598 result_type = type2;
6aa3c60d 4599 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4600 result_type = type1;
4601 else if (!addr_space_superset (as1, as2, &as_common))
4602 {
4603 error_at (colon_loc, "pointers to disjoint address spaces "
4604 "used in conditional expression");
4605 return error_mark_node;
4606 }
267bac10
JM
4607 else if (VOID_TYPE_P (TREE_TYPE (type1))
4608 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4609 {
fcf73884 4610 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4611 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4612 "ISO C forbids conditional expr between "
bda67431 4613 "%<void *%> and function pointer");
3e4093b6
RS
4614 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4615 TREE_TYPE (type2)));
34a80643 4616 }
267bac10
JM
4617 else if (VOID_TYPE_P (TREE_TYPE (type2))
4618 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4619 {
fcf73884 4620 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4621 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4622 "ISO C forbids conditional expr between "
bda67431 4623 "%<void *%> and function pointer");
3e4093b6
RS
4624 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4625 TREE_TYPE (type1)));
1c2a9b35 4626 }
b581b85b
NP
4627 /* Objective-C pointer comparisons are a bit more lenient. */
4628 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4629 result_type = objc_common_type (type1, type2);
34a80643 4630 else
ab87f8c8 4631 {
36c5e70a
BE
4632 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4633
b581b85b
NP
4634 pedwarn (colon_loc, 0,
4635 "pointer type mismatch in conditional expression");
36c5e70a
BE
4636 result_type = build_pointer_type
4637 (build_qualified_type (void_type_node, qual));
ab87f8c8 4638 }
3e4093b6
RS
4639 }
4640 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4641 {
6aa3c60d 4642 if (!null_pointer_constant_p (orig_op2))
744aa42f 4643 pedwarn (colon_loc, 0,
509c9d60 4644 "pointer/integer type mismatch in conditional expression");
3e4093b6 4645 else
ab87f8c8 4646 {
3e4093b6 4647 op2 = null_pointer_node;
ab87f8c8 4648 }
3e4093b6
RS
4649 result_type = type1;
4650 }
4651 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4652 {
6aa3c60d 4653 if (!null_pointer_constant_p (orig_op1))
744aa42f 4654 pedwarn (colon_loc, 0,
509c9d60 4655 "pointer/integer type mismatch in conditional expression");
3e4093b6 4656 else
ab87f8c8 4657 {
3e4093b6 4658 op1 = null_pointer_node;
ab87f8c8 4659 }
3e4093b6
RS
4660 result_type = type2;
4661 }
1c2a9b35 4662
3e4093b6
RS
4663 if (!result_type)
4664 {
4665 if (flag_cond_mismatch)
4666 result_type = void_type_node;
4667 else
400fbf9f 4668 {
c2255bc4 4669 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4670 return error_mark_node;
400fbf9f 4671 }
3e4093b6 4672 }
400fbf9f 4673
3e4093b6
RS
4674 /* Merge const and volatile flags of the incoming types. */
4675 result_type
4676 = build_type_variant (result_type,
afbd0665
AS
4677 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4678 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4679
2d2e923f
MLI
4680 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4681 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
b6a10c9f 4682
928c19bb
JM
4683 if (ifexp_bcp && ifexp == truthvalue_true_node)
4684 {
4685 op2_int_operands = true;
4686 op1 = c_fully_fold (op1, require_constant_value, NULL);
4687 }
4688 if (ifexp_bcp && ifexp == truthvalue_false_node)
4689 {
4690 op1_int_operands = true;
4691 op2 = c_fully_fold (op2, require_constant_value, NULL);
4692 }
4d84fe7c 4693 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4694 && op1_int_operands
4695 && op2_int_operands);
4696 if (int_operands)
4697 {
4698 int_const = ((ifexp == truthvalue_true_node
4699 && TREE_CODE (orig_op1) == INTEGER_CST
4700 && !TREE_OVERFLOW (orig_op1))
4701 || (ifexp == truthvalue_false_node
4702 && TREE_CODE (orig_op2) == INTEGER_CST
4703 && !TREE_OVERFLOW (orig_op2)));
4704 }
4705 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4706 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4707 else
4708 {
01c7ccbb
JM
4709 if (int_operands)
4710 {
f34f1c87
MP
4711 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4712 nested inside of the expression. */
4713 op1 = c_fully_fold (op1, false, NULL);
4714 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4715 }
928c19bb
JM
4716 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4717 if (int_operands)
4718 ret = note_integer_operands (ret);
4719 }
2d2e923f
MLI
4720 if (semantic_result_type)
4721 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4722
c2255bc4 4723 protected_set_expr_location (ret, colon_loc);
928c19bb 4724 return ret;
3e4093b6
RS
4725}
4726\f
487a92fe 4727/* Return a compound expression that performs two expressions and
c2255bc4
AH
4728 returns the value of the second of them.
4729
4730 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4731
3e4093b6 4732tree
c2255bc4 4733build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4734{
4d84fe7c 4735 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4736 tree eptype = NULL_TREE;
928c19bb
JM
4737 tree ret;
4738
939b37da
BI
4739 if (flag_enable_cilkplus
4740 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4741 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4742 {
4743 error_at (loc,
4744 "spawned function call cannot be part of a comma expression");
4745 return error_mark_node;
4746 }
4d84fe7c
JM
4747 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4748 if (expr1_int_operands)
4749 expr1 = remove_c_maybe_const_expr (expr1);
4750 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4751 if (expr2_int_operands)
4752 expr2 = remove_c_maybe_const_expr (expr2);
4753
8ce94e44
JM
4754 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4755 expr1 = TREE_OPERAND (expr1, 0);
4756 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4757 {
4758 eptype = TREE_TYPE (expr2);
4759 expr2 = TREE_OPERAND (expr2, 0);
4760 }
4761
3f75a254 4762 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4763 {
4764 /* The left-hand operand of a comma expression is like an expression
c5409249 4765 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4766 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4767 if (warn_unused_value)
47aecf47 4768 {
e14a6540 4769 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4770 && CONVERT_EXPR_P (expr1))
47aecf47 4771 ; /* (void) a, b */
e14a6540
JM
4772 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4773 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4774 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4775 ; /* (void) a, (void) b, c */
4776 else
b8698a0f 4777 warning_at (loc, OPT_Wunused_value,
c2255bc4 4778 "left-hand operand of comma expression has no effect");
47aecf47 4779 }
3e4093b6 4780 }
789eadcd
MP
4781 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4782 && warn_unused_value)
4783 {
4784 tree r = expr1;
4785 location_t cloc = loc;
4786 while (TREE_CODE (r) == COMPOUND_EXPR)
4787 {
4788 if (EXPR_HAS_LOCATION (r))
4789 cloc = EXPR_LOCATION (r);
4790 r = TREE_OPERAND (r, 1);
4791 }
4792 if (!TREE_SIDE_EFFECTS (r)
4793 && !VOID_TYPE_P (TREE_TYPE (r))
4794 && !CONVERT_EXPR_P (r))
4795 warning_at (cloc, OPT_Wunused_value,
4796 "right-hand operand of comma expression has no effect");
4797 }
400fbf9f 4798
3e4093b6
RS
4799 /* With -Wunused, we should also warn if the left-hand operand does have
4800 side-effects, but computes a value which is not used. For example, in
4801 `foo() + bar(), baz()' the result of the `+' operator is not used,
4802 so we should issue a warning. */
4803 else if (warn_unused_value)
c2255bc4 4804 warn_if_unused_value (expr1, loc);
400fbf9f 4805
e63d6886
AP
4806 if (expr2 == error_mark_node)
4807 return error_mark_node;
4808
928c19bb
JM
4809 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4810
4811 if (flag_isoc99
4d84fe7c
JM
4812 && expr1_int_operands
4813 && expr2_int_operands)
928c19bb
JM
4814 ret = note_integer_operands (ret);
4815
8ce94e44
JM
4816 if (eptype)
4817 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4818
c2255bc4 4819 protected_set_expr_location (ret, loc);
928c19bb 4820 return ret;
3e4093b6 4821}
400fbf9f 4822
67165eb3
ILT
4823/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4824 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4825 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4826 of the cast. -Wcast-qual appeared on the command line. Named
4827 address space qualifiers are not handled here, because they result
4828 in different warnings. */
67165eb3
ILT
4829
4830static void
2ee3cb35 4831handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4832{
4833 tree in_type = type;
4834 tree in_otype = otype;
4835 int added = 0;
4836 int discarded = 0;
4837 bool is_const;
4838
4839 /* Check that the qualifiers on IN_TYPE are a superset of the
4840 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4841 nodes is uninteresting and we stop as soon as we hit a
4842 non-POINTER_TYPE node on either type. */
4843 do
4844 {
4845 in_otype = TREE_TYPE (in_otype);
4846 in_type = TREE_TYPE (in_type);
4847
4848 /* GNU C allows cv-qualified function types. 'const' means the
4849 function is very pure, 'volatile' means it can't return. We
4850 need to warn when such qualifiers are added, not when they're
4851 taken away. */
4852 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4853 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4854 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4855 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4856 else
36c5e70a
BE
4857 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4858 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4859 }
4860 while (TREE_CODE (in_type) == POINTER_TYPE
4861 && TREE_CODE (in_otype) == POINTER_TYPE);
4862
4863 if (added)
2ee3cb35
MLI
4864 warning_at (loc, OPT_Wcast_qual,
4865 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4866
4867 if (discarded)
4868 /* There are qualifiers present in IN_OTYPE that are not present
4869 in IN_TYPE. */
2ee3cb35
MLI
4870 warning_at (loc, OPT_Wcast_qual,
4871 "cast discards %q#v qualifier from pointer target type",
4872 discarded);
67165eb3
ILT
4873
4874 if (added || discarded)
4875 return;
4876
4877 /* A cast from **T to const **T is unsafe, because it can cause a
4878 const value to be changed with no additional warning. We only
4879 issue this warning if T is the same on both sides, and we only
4880 issue the warning if there are the same number of pointers on
4881 both sides, as otherwise the cast is clearly unsafe anyhow. A
4882 cast is unsafe when a qualifier is added at one level and const
4883 is not present at all outer levels.
4884
4885 To issue this warning, we check at each level whether the cast
4886 adds new qualifiers not already seen. We don't need to special
4887 case function types, as they won't have the same
4888 TYPE_MAIN_VARIANT. */
4889
4890 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4891 return;
4892 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4893 return;
4894
4895 in_type = type;
4896 in_otype = otype;
4897 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4898 do
4899 {
4900 in_type = TREE_TYPE (in_type);
4901 in_otype = TREE_TYPE (in_otype);
4902 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4903 && !is_const)
4904 {
2ee3cb35
MLI
4905 warning_at (loc, OPT_Wcast_qual,
4906 "to be safe all intermediate pointers in cast from "
4907 "%qT to %qT must be %<const%> qualified",
4908 otype, type);
67165eb3
ILT
4909 break;
4910 }
4911 if (is_const)
4912 is_const = TYPE_READONLY (in_type);
4913 }
4914 while (TREE_CODE (in_type) == POINTER_TYPE);
4915}
4916
b8698a0f 4917/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4918 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4919
3e4093b6 4920tree
c2255bc4 4921build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4922{
8ce94e44
JM
4923 tree value;
4924
4925 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4926 expr = TREE_OPERAND (expr, 0);
4927
4928 value = expr;
400fbf9f 4929
3e4093b6
RS
4930 if (type == error_mark_node || expr == error_mark_node)
4931 return error_mark_node;
400fbf9f 4932
3e4093b6
RS
4933 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4934 only in <protocol> qualifications. But when constructing cast expressions,
4935 the protocols do matter and must be kept around. */
700686fa
ZL
4936 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4937 return build1 (NOP_EXPR, type, expr);
4938
4939 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4940
3e4093b6
RS
4941 if (TREE_CODE (type) == ARRAY_TYPE)
4942 {
c2255bc4 4943 error_at (loc, "cast specifies array type");
3e4093b6
RS
4944 return error_mark_node;
4945 }
400fbf9f 4946
3e4093b6
RS
4947 if (TREE_CODE (type) == FUNCTION_TYPE)
4948 {
c2255bc4 4949 error_at (loc, "cast specifies function type");
3e4093b6
RS
4950 return error_mark_node;
4951 }
400fbf9f 4952
808d6eaa
JM
4953 if (!VOID_TYPE_P (type))
4954 {
4955 value = require_complete_type (value);
4956 if (value == error_mark_node)
4957 return error_mark_node;
4958 }
4959
3e4093b6
RS
4960 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4961 {
fcf73884
MLI
4962 if (TREE_CODE (type) == RECORD_TYPE
4963 || TREE_CODE (type) == UNION_TYPE)
c1771a20 4964 pedwarn (loc, OPT_Wpedantic,
fcf73884 4965 "ISO C forbids casting nonscalar to the same type");
3e4093b6
RS
4966 }
4967 else if (TREE_CODE (type) == UNION_TYPE)
4968 {
4969 tree field;
400fbf9f 4970
910ad8de 4971 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
4972 if (TREE_TYPE (field) != error_mark_node
4973 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4974 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
4975 break;
4976
4977 if (field)
400fbf9f 4978 {
3e4093b6 4979 tree t;
e616f54d 4980 bool maybe_const = true;
3e4093b6 4981
c1771a20 4982 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
4983 t = c_fully_fold (value, false, &maybe_const);
4984 t = build_constructor_single (type, field, t);
4985 if (!maybe_const)
4986 t = c_wrap_maybe_const (t, true);
4987 t = digest_init (loc, type, t,
bbbbb16a 4988 NULL_TREE, false, true, 0);
3e4093b6
RS
4989 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4990 return t;
400fbf9f 4991 }
c2255bc4 4992 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
4993 return error_mark_node;
4994 }
4995 else
4996 {
4997 tree otype, ovalue;
400fbf9f 4998
3e4093b6 4999 if (type == void_type_node)
c2255bc4
AH
5000 {
5001 tree t = build1 (CONVERT_EXPR, type, value);
5002 SET_EXPR_LOCATION (t, loc);
5003 return t;
5004 }
400fbf9f 5005
3e4093b6 5006 otype = TREE_TYPE (value);
400fbf9f 5007
3e4093b6 5008 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5009 if (warn_cast_qual
5010 && TREE_CODE (type) == POINTER_TYPE
5011 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5012 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5013
36c5e70a
BE
5014 /* Warn about conversions between pointers to disjoint
5015 address spaces. */
5016 if (TREE_CODE (type) == POINTER_TYPE
5017 && TREE_CODE (otype) == POINTER_TYPE
5018 && !null_pointer_constant_p (value))
5019 {
5020 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5021 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5022 addr_space_t as_common;
5023
5024 if (!addr_space_superset (as_to, as_from, &as_common))
5025 {
5026 if (ADDR_SPACE_GENERIC_P (as_from))
5027 warning_at (loc, 0, "cast to %s address space pointer "
5028 "from disjoint generic address space pointer",
5029 c_addr_space_name (as_to));
5030
5031 else if (ADDR_SPACE_GENERIC_P (as_to))
5032 warning_at (loc, 0, "cast to generic address space pointer "
5033 "from disjoint %s address space pointer",
5034 c_addr_space_name (as_from));
5035
5036 else
5037 warning_at (loc, 0, "cast to %s address space pointer "
5038 "from disjoint %s address space pointer",
5039 c_addr_space_name (as_to),
5040 c_addr_space_name (as_from));
5041 }
5042 }
5043
3e4093b6 5044 /* Warn about possible alignment problems. */
3176a0c2 5045 if (STRICT_ALIGNMENT
3e4093b6
RS
5046 && TREE_CODE (type) == POINTER_TYPE
5047 && TREE_CODE (otype) == POINTER_TYPE
5048 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5049 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5050 /* Don't warn about opaque types, where the actual alignment
5051 restriction is unknown. */
5052 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5053 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5054 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5055 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5056 warning_at (loc, OPT_Wcast_align,
5057 "cast increases required alignment of target type");
e9a25f70 5058
3176a0c2 5059 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5060 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5061 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5062 /* Unlike conversion of integers to pointers, where the
5063 warning is disabled for converting constants because
5064 of cases such as SIG_*, warn about converting constant
5065 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5066 sign extension, and a warning is appropriate. */
c2255bc4
AH
5067 warning_at (loc, OPT_Wpointer_to_int_cast,
5068 "cast from pointer to integer of different size");
400fbf9f 5069
3176a0c2 5070 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5071 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5072 warning_at (loc, OPT_Wbad_function_cast,
5073 "cast from function call of type %qT "
5074 "to non-matching type %qT", otype, type);
400fbf9f 5075
3176a0c2 5076 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5077 && TREE_CODE (otype) == INTEGER_TYPE
5078 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5079 /* Don't warn about converting any constant. */
5080 && !TREE_CONSTANT (value))
c2255bc4
AH
5081 warning_at (loc,
5082 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5083 "of different size");
400fbf9f 5084
79bedddc
SR
5085 if (warn_strict_aliasing <= 2)
5086 strict_aliasing_warning (otype, type, expr);
400fbf9f 5087
3897f229
JM
5088 /* If pedantic, warn for conversions between function and object
5089 pointer types, except for converting a null pointer constant
5090 to function pointer type. */
5091 if (pedantic
5092 && TREE_CODE (type) == POINTER_TYPE
5093 && TREE_CODE (otype) == POINTER_TYPE
5094 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5095 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5096 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5097 "conversion of function pointer to object pointer type");
3897f229
JM
5098
5099 if (pedantic
5100 && TREE_CODE (type) == POINTER_TYPE
5101 && TREE_CODE (otype) == POINTER_TYPE
5102 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5103 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5104 && !null_pointer_constant_p (value))
c1771a20 5105 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5106 "conversion of object pointer to function pointer type");
3897f229 5107
3e4093b6 5108 ovalue = value;
3e4093b6 5109 value = convert (type, value);
400fbf9f 5110
3e4093b6 5111 /* Ignore any integer overflow caused by the cast. */
928c19bb 5112 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5113 {
8bcd6380 5114 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5115 {
8bcd6380
RS
5116 if (!TREE_OVERFLOW (value))
5117 {
5118 /* Avoid clobbering a shared constant. */
5119 value = copy_node (value);
5120 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5121 }
6414bad6 5122 }
8bcd6380 5123 else if (TREE_OVERFLOW (value))
d8e1f97b
PB
5124 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5125 value = build_int_cst_wide (TREE_TYPE (value),
5126 TREE_INT_CST_LOW (value),
5127 TREE_INT_CST_HIGH (value));
3e4093b6
RS
5128 }
5129 }
400fbf9f 5130
53cd18ec
JM
5131 /* Don't let a cast be an lvalue. */
5132 if (value == expr)
db3927fb 5133 value = non_lvalue_loc (loc, value);
e9a25f70 5134
928c19bb
JM
5135 /* Don't allow the results of casting to floating-point or complex
5136 types be confused with actual constants, or casts involving
5137 integer and pointer types other than direct integer-to-integer
5138 and integer-to-pointer be confused with integer constant
5139 expressions and null pointer constants. */
5140 if (TREE_CODE (value) == REAL_CST
5141 || TREE_CODE (value) == COMPLEX_CST
5142 || (TREE_CODE (value) == INTEGER_CST
5143 && !((TREE_CODE (expr) == INTEGER_CST
5144 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5145 || TREE_CODE (expr) == REAL_CST
5146 || TREE_CODE (expr) == COMPLEX_CST)))
5147 value = build1 (NOP_EXPR, type, value);
5148
c2255bc4
AH
5149 if (CAN_HAVE_LOCATION_P (value))
5150 SET_EXPR_LOCATION (value, loc);
3e4093b6 5151 return value;
400fbf9f
JW
5152}
5153
c2255bc4
AH
5154/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5155 location of the open paren of the cast, or the position of the cast
5156 expr. */
3e4093b6 5157tree
c2255bc4 5158c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5159{
f8893e47 5160 tree type;
928c19bb
JM
5161 tree type_expr = NULL_TREE;
5162 bool type_expr_const = true;
5163 tree ret;
3e4093b6 5164 int saved_wsp = warn_strict_prototypes;
c5c76735 5165
3e4093b6
RS
5166 /* This avoids warnings about unprototyped casts on
5167 integers. E.g. "#define SIG_DFL (void(*)())0". */
5168 if (TREE_CODE (expr) == INTEGER_CST)
5169 warn_strict_prototypes = 0;
928c19bb 5170 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5171 warn_strict_prototypes = saved_wsp;
c5c76735 5172
c2255bc4 5173 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5174 if (type_expr)
5175 {
9f33203d
JM
5176 bool inner_expr_const = true;
5177 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5178 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5179 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5180 && inner_expr_const);
c2255bc4 5181 SET_EXPR_LOCATION (ret, loc);
928c19bb 5182 }
24b97832
ILT
5183
5184 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5185 SET_EXPR_LOCATION (ret, loc);
5186
9e5b2115
PB
5187 /* C++ does not permits types to be defined in a cast, but it
5188 allows references to incomplete types. */
5189 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5190 warning_at (loc, OPT_Wc___compat,
5191 "defining a type in a cast is invalid in C++");
5192
928c19bb 5193 return ret;
400fbf9f 5194}
3e4093b6
RS
5195\f
5196/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5197 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5198 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5199 MODIFYCODE is the code for a binary operator that we use
5200 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5201 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5202 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5203 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5204
c2255bc4
AH
5205 LOCATION is the location of the MODIFYCODE operator.
5206 RHS_LOC is the location of the RHS. */
2f6e4e97 5207
3e4093b6 5208tree
32e8bb8e 5209build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5210 enum tree_code modifycode,
c2255bc4 5211 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5212{
3e4093b6
RS
5213 tree result;
5214 tree newrhs;
241f845a 5215 tree rhseval = NULL_TREE;
8ce94e44 5216 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5217 tree lhstype = TREE_TYPE (lhs);
5218 tree olhstype = lhstype;
928c19bb 5219 bool npc;
267bac10 5220 bool is_atomic_op;
e9a25f70 5221
3e4093b6
RS
5222 /* Types that aren't fully specified cannot be used in assignments. */
5223 lhs = require_complete_type (lhs);
e9a25f70 5224
3e4093b6
RS
5225 /* Avoid duplicate error messages from operands that had errors. */
5226 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5227 return error_mark_node;
400fbf9f 5228
4c2ecab0
JM
5229 /* Ensure an error for assigning a non-lvalue array to an array in
5230 C90. */
5231 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5232 {
5233 error_at (location, "assignment to expression with array type");
5234 return error_mark_node;
5235 }
5236
46a88c12 5237 /* For ObjC properties, defer this check. */
7bd11157 5238 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5239 return error_mark_node;
5240
267bac10
JM
5241 is_atomic_op = really_atomic_lvalue (lhs);
5242
8ce94e44
JM
5243 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5244 {
5245 rhs_semantic_type = TREE_TYPE (rhs);
5246 rhs = TREE_OPERAND (rhs, 0);
5247 }
5248
3e4093b6 5249 newrhs = rhs;
400fbf9f 5250
928c19bb
JM
5251 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5252 {
5253 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5254 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5255 rhs_origtype);
928c19bb
JM
5256 if (inner == error_mark_node)
5257 return error_mark_node;
5258 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5259 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5260 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5261 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5262 protected_set_expr_location (result, location);
5263 return result;
5264 }
5265
3e4093b6
RS
5266 /* If a binary op has been requested, combine the old LHS value with the RHS
5267 producing the value we should actually store into the LHS. */
5268
5269 if (modifycode != NOP_EXPR)
400fbf9f 5270 {
928c19bb 5271 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5272 lhs = stabilize_reference (lhs);
bbbbb16a 5273
267bac10
JM
5274 /* Construct the RHS for any non-atomic compound assignemnt. */
5275 if (!is_atomic_op)
5276 {
241f845a
JJ
5277 /* If in LHS op= RHS the RHS has side-effects, ensure they
5278 are preevaluated before the rest of the assignment expression's
5279 side-effects, because RHS could contain e.g. function calls
5280 that modify LHS. */
5281 if (TREE_SIDE_EFFECTS (rhs))
5282 {
5283 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5284 rhseval = newrhs;
5285 }
267bac10 5286 newrhs = build_binary_op (location,
241f845a 5287 modifycode, lhs, newrhs, 1);
267bac10
JM
5288
5289 /* The original type of the right hand side is no longer
5290 meaningful. */
5291 rhs_origtype = NULL_TREE;
5292 }
400fbf9f 5293 }
400fbf9f 5294
668ea4b1
IS
5295 if (c_dialect_objc ())
5296 {
46a88c12
NP
5297 /* Check if we are modifying an Objective-C property reference;
5298 if so, we need to generate setter calls. */
5299 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5300 if (result)
241f845a 5301 goto return_result;
46a88c12
NP
5302
5303 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5304 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5305 return error_mark_node;
5306 }
5307
9bf24266 5308 /* Give an error for storing in something that is 'const'. */
bbbd6700 5309
f37acdf9 5310 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5311 || ((TREE_CODE (lhstype) == RECORD_TYPE
5312 || TREE_CODE (lhstype) == UNION_TYPE)
5313 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5314 {
c02065fc 5315 readonly_error (location, lhs, lv_assign);
953ff289
DN
5316 return error_mark_node;
5317 }
f37acdf9
JM
5318 else if (TREE_READONLY (lhs))
5319 readonly_warning (lhs, lv_assign);
bbbd6700 5320
3e4093b6
RS
5321 /* If storing into a structure or union member,
5322 it has probably been given type `int'.
5323 Compute the type that would go with
5324 the actual amount of storage the member occupies. */
bbbd6700 5325
3e4093b6
RS
5326 if (TREE_CODE (lhs) == COMPONENT_REF
5327 && (TREE_CODE (lhstype) == INTEGER_TYPE
5328 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5329 || TREE_CODE (lhstype) == REAL_TYPE
5330 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5331 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5332
3e4093b6
RS
5333 /* If storing in a field that is in actuality a short or narrower than one,
5334 we must store in the field in its actual type. */
5335
5336 if (lhstype != TREE_TYPE (lhs))
5337 {
5338 lhs = copy_node (lhs);
5339 TREE_TYPE (lhs) = lhstype;
400fbf9f 5340 }
400fbf9f 5341
32e8bb8e
ILT
5342 /* Issue -Wc++-compat warnings about an assignment to an enum type
5343 when LHS does not have its original type. This happens for,
5344 e.g., an enum bitfield in a struct. */
5345 if (warn_cxx_compat
5346 && lhs_origtype != NULL_TREE
5347 && lhs_origtype != lhstype
5348 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5349 {
5350 tree checktype = (rhs_origtype != NULL_TREE
5351 ? rhs_origtype
5352 : TREE_TYPE (rhs));
5353 if (checktype != error_mark_node
267bac10
JM
5354 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5355 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5356 warning_at (location, OPT_Wc___compat,
5357 "enum conversion in assignment is invalid in C++");
5358 }
5359
267bac10
JM
5360 /* If the lhs is atomic, remove that qualifier. */
5361 if (is_atomic_op)
5362 {
5363 lhstype = build_qualified_type (lhstype,
5364 (TYPE_QUALS (lhstype)
5365 & ~TYPE_QUAL_ATOMIC));
5366 olhstype = build_qualified_type (olhstype,
5367 (TYPE_QUALS (lhstype)
5368 & ~TYPE_QUAL_ATOMIC));
5369 }
5370
8ce94e44
JM
5371 /* Convert new value to destination type. Fold it first, then
5372 restore any excess precision information, for the sake of
5373 conversion warnings. */
400fbf9f 5374
267bac10
JM
5375 if (!(is_atomic_op && modifycode != NOP_EXPR))
5376 {
5377 npc = null_pointer_constant_p (newrhs);
5378 newrhs = c_fully_fold (newrhs, false, NULL);
5379 if (rhs_semantic_type)
5380 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5381 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
5382 ic_assign, npc, NULL_TREE,
5383 NULL_TREE, 0);
5384 if (TREE_CODE (newrhs) == ERROR_MARK)
5385 return error_mark_node;
5386 }
400fbf9f 5387
6e955430
ZL
5388 /* Emit ObjC write barrier, if necessary. */
5389 if (c_dialect_objc () && flag_objc_gc)
5390 {
5391 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5392 if (result)
c9f9eb5d
AH
5393 {
5394 protected_set_expr_location (result, location);
241f845a 5395 goto return_result;
c9f9eb5d 5396 }
6e955430
ZL
5397 }
5398
ea4b7848 5399 /* Scan operands. */
400fbf9f 5400
267bac10
JM
5401 if (is_atomic_op)
5402 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5403 else
5404 {
5405 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5406 TREE_SIDE_EFFECTS (result) = 1;
5407 protected_set_expr_location (result, location);
5408 }
400fbf9f 5409
3e4093b6
RS
5410 /* If we got the LHS in a different type for storing in,
5411 convert the result back to the nominal type of LHS
5412 so that the value we return always has the same type
5413 as the LHS argument. */
e855c5ce 5414
3e4093b6 5415 if (olhstype == TREE_TYPE (result))
241f845a 5416 goto return_result;
c9f9eb5d 5417
744aa42f
ILT
5418 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5419 ic_assign, false, NULL_TREE, NULL_TREE, 0);
c9f9eb5d 5420 protected_set_expr_location (result, location);
241f845a
JJ
5421
5422return_result:
5423 if (rhseval)
5424 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5425 return result;
3e4093b6
RS
5426}
5427\f
478a1c5b
ILT
5428/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5429 This is used to implement -fplan9-extensions. */
5430
5431static bool
5432find_anonymous_field_with_type (tree struct_type, tree type)
5433{
5434 tree field;
5435 bool found;
5436
5437 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5438 || TREE_CODE (struct_type) == UNION_TYPE);
5439 found = false;
5440 for (field = TYPE_FIELDS (struct_type);
5441 field != NULL_TREE;
5442 field = TREE_CHAIN (field))
5443 {
267bac10
JM
5444 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5445 ? c_build_qualified_type (TREE_TYPE (field),
5446 TYPE_QUAL_ATOMIC)
5447 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5448 if (DECL_NAME (field) == NULL
267bac10 5449 && comptypes (type, fieldtype))
478a1c5b
ILT
5450 {
5451 if (found)
5452 return false;
5453 found = true;
5454 }
5455 else if (DECL_NAME (field) == NULL
5456 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5457 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5458 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5459 {
5460 if (found)
5461 return false;
5462 found = true;
5463 }
5464 }
5465 return found;
5466}
5467
5468/* RHS is an expression whose type is pointer to struct. If there is
5469 an anonymous field in RHS with type TYPE, then return a pointer to
5470 that field in RHS. This is used with -fplan9-extensions. This
5471 returns NULL if no conversion could be found. */
5472
5473static tree
5474convert_to_anonymous_field (location_t location, tree type, tree rhs)
5475{
5476 tree rhs_struct_type, lhs_main_type;
5477 tree field, found_field;
5478 bool found_sub_field;
5479 tree ret;
5480
5481 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5482 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5483 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5484 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5485
5486 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5487 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5488 ? c_build_qualified_type (TREE_TYPE (type),
5489 TYPE_QUAL_ATOMIC)
5490 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5491
5492 found_field = NULL_TREE;
5493 found_sub_field = false;
5494 for (field = TYPE_FIELDS (rhs_struct_type);
5495 field != NULL_TREE;
5496 field = TREE_CHAIN (field))
5497 {
5498 if (DECL_NAME (field) != NULL_TREE
5499 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5500 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5501 continue;
267bac10
JM
5502 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5503 ? c_build_qualified_type (TREE_TYPE (field),
5504 TYPE_QUAL_ATOMIC)
5505 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5506 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5507 {
5508 if (found_field != NULL_TREE)
5509 return NULL_TREE;
5510 found_field = field;
5511 }
5512 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5513 lhs_main_type))
5514 {
5515 if (found_field != NULL_TREE)
5516 return NULL_TREE;
5517 found_field = field;
5518 found_sub_field = true;
5519 }
5520 }
5521
5522 if (found_field == NULL_TREE)
5523 return NULL_TREE;
5524
5525 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5526 build_fold_indirect_ref (rhs), found_field,
5527 NULL_TREE);
5528 ret = build_fold_addr_expr_loc (location, ret);
5529
5530 if (found_sub_field)
5531 {
5532 ret = convert_to_anonymous_field (location, type, ret);
5533 gcc_assert (ret != NULL_TREE);
5534 }
5535
5536 return ret;
5537}
5538
bbbbb16a
ILT
5539/* Convert value RHS to type TYPE as preparation for an assignment to
5540 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5541 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5542 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5543 constant before any folding.
3e4093b6
RS
5544 The real work of conversion is done by `convert'.
5545 The purpose of this function is to generate error messages
5546 for assignments that are not allowed in C.
2ac2f164
JM
5547 ERRTYPE says whether it is argument passing, assignment,
5548 initialization or return.
2f6e4e97 5549
c2255bc4 5550 LOCATION is the location of the RHS.
2ac2f164 5551 FUNCTION is a tree for the function being called.
3e4093b6 5552 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5553
3e4093b6 5554static tree
744aa42f
ILT
5555convert_for_assignment (location_t location, tree type, tree rhs,
5556 tree origtype, enum impl_conv errtype,
5557 bool null_pointer_constant, tree fundecl,
5558 tree function, int parmnum)
3e4093b6
RS
5559{
5560 enum tree_code codel = TREE_CODE (type);
8ce94e44 5561 tree orig_rhs = rhs;
3e4093b6
RS
5562 tree rhstype;
5563 enum tree_code coder;
2ac2f164 5564 tree rname = NULL_TREE;
58393038 5565 bool objc_ok = false;
2ac2f164 5566
6b4ef5c1 5567 if (errtype == ic_argpass)
2ac2f164
JM
5568 {
5569 tree selector;
5570 /* Change pointer to function to the function itself for
5571 diagnostics. */
5572 if (TREE_CODE (function) == ADDR_EXPR
5573 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5574 function = TREE_OPERAND (function, 0);
5575
5576 /* Handle an ObjC selector specially for diagnostics. */
5577 selector = objc_message_selector ();
5578 rname = function;
5579 if (selector && parmnum > 2)
5580 {
5581 rname = selector;
5582 parmnum -= 2;
5583 }
5584 }
5585
5586 /* This macro is used to emit diagnostics to ensure that all format
5587 strings are complete sentences, visible to gettext and checked at
5588 compile time. */
c2255bc4 5589#define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5590 do { \
5591 switch (errtype) \
5592 { \
5593 case ic_argpass: \
5594 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
c2255bc4
AH
5595 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5596 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
1e053dfe
MLI
5597 "expected %qT but argument is of type %qT", \
5598 type, rhstype); \
5599 break; \
1e053dfe
MLI
5600 case ic_assign: \
5601 pedwarn (LOCATION, OPT, AS); \
5602 break; \
5603 case ic_init: \
6a8f4e12 5604 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5605 break; \
5606 case ic_return: \
c2255bc4 5607 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5608 break; \
5609 default: \
5610 gcc_unreachable (); \
5611 } \
2ac2f164 5612 } while (0)
cb3ca04e 5613
49706e39
MLI
5614 /* This macro is used to emit diagnostics to ensure that all format
5615 strings are complete sentences, visible to gettext and checked at
5616 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5617 extra parameter to enumerate qualifiers. */
5618
5619#define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5620 do { \
5621 switch (errtype) \
5622 { \
5623 case ic_argpass: \
5624 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5625 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5626 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5627 "expected %qT but argument is of type %qT", \
5628 type, rhstype); \
5629 break; \
5630 case ic_assign: \
5631 pedwarn (LOCATION, OPT, AS, QUALS); \
5632 break; \
5633 case ic_init: \
5634 pedwarn (LOCATION, OPT, IN, QUALS); \
5635 break; \
5636 case ic_return: \
5637 pedwarn (LOCATION, OPT, RE, QUALS); \
5638 break; \
5639 default: \
5640 gcc_unreachable (); \
5641 } \
5642 } while (0)
5643
8ce94e44
JM
5644 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5645 rhs = TREE_OPERAND (rhs, 0);
5646
3e4093b6
RS
5647 rhstype = TREE_TYPE (rhs);
5648 coder = TREE_CODE (rhstype);
5649
5650 if (coder == ERROR_MARK)
5651 return error_mark_node;
5652
58393038
ZL
5653 if (c_dialect_objc ())
5654 {
5655 int parmno;
5656
5657 switch (errtype)
5658 {
5659 case ic_return:
5660 parmno = 0;
5661 break;
5662
5663 case ic_assign:
5664 parmno = -1;
5665 break;
5666
5667 case ic_init:
5668 parmno = -2;
5669 break;
5670
5671 default:
5672 parmno = parmnum;
5673 break;
5674 }
5675
5676 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5677 }
5678
bbbbb16a
ILT
5679 if (warn_cxx_compat)
5680 {
5681 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5682 if (checktype != error_mark_node
5683 && TREE_CODE (type) == ENUMERAL_TYPE
5684 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5685 {
81f40b79
ILT
5686 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5687 G_("enum conversion when passing argument "
5688 "%d of %qE is invalid in C++"),
5689 G_("enum conversion in assignment is "
5690 "invalid in C++"),
5691 G_("enum conversion in initialization is "
5692 "invalid in C++"),
5693 G_("enum conversion in return is "
5694 "invalid in C++"));
bbbbb16a
ILT
5695 }
5696 }
5697
3e4093b6 5698 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5699 return rhs;
3e4093b6
RS
5700
5701 if (coder == VOID_TYPE)
400fbf9f 5702 {
6dcc04b0
JM
5703 /* Except for passing an argument to an unprototyped function,
5704 this is a constraint violation. When passing an argument to
5705 an unprototyped function, it is compile-time undefined;
5706 making it a constraint in that case was rejected in
5707 DR#252. */
c2255bc4 5708 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5709 return error_mark_node;
400fbf9f 5710 }
808d6eaa
JM
5711 rhs = require_complete_type (rhs);
5712 if (rhs == error_mark_node)
5713 return error_mark_node;
cd192ccc
MS
5714 /* A non-reference type can convert to a reference. This handles
5715 va_start, va_copy and possibly port built-ins. */
5716 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5717 {
3e4093b6 5718 if (!lvalue_p (rhs))
400fbf9f 5719 {
c2255bc4 5720 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5721 return error_mark_node;
400fbf9f 5722 }
3e4093b6
RS
5723 if (!c_mark_addressable (rhs))
5724 return error_mark_node;
5725 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5726 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5727
cd192ccc
MS
5728 rhs = convert_for_assignment (location, build_pointer_type (TREE_TYPE (type)),
5729 rhs, origtype, errtype, null_pointer_constant,
5730 fundecl, function, parmnum);
5731 if (rhs == error_mark_node)
5732 return error_mark_node;
3e4093b6
RS
5733
5734 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5735 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5736 return rhs;
400fbf9f 5737 }
3e4093b6 5738 /* Some types can interconvert without explicit casts. */
3274deff 5739 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5740 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5741 return convert (type, rhs);
5742 /* Arithmetic types all interconvert, and enum is treated like int. */
5743 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5744 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5745 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5746 || codel == BOOLEAN_TYPE)
5747 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5748 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5749 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5750 || coder == BOOLEAN_TYPE))
928c19bb
JM
5751 {
5752 tree ret;
5753 bool save = in_late_binary_op;
dfe776dd 5754 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb 5755 in_late_binary_op = true;
8ce94e44 5756 ret = convert_and_check (type, orig_rhs);
dfe776dd 5757 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb
JM
5758 in_late_binary_op = save;
5759 return ret;
5760 }
400fbf9f 5761
79077aea
JJ
5762 /* Aggregates in different TUs might need conversion. */
5763 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5764 && codel == coder
5765 && comptypes (type, rhstype))
5766 return convert_and_check (type, rhs);
5767
ebf0bf7f 5768 /* Conversion to a transparent union or record from its member types.
3e4093b6 5769 This applies only to function arguments. */
ebf0bf7f
JJ
5770 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5771 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5772 && errtype == ic_argpass)
400fbf9f 5773 {
0257e383 5774 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5775
910ad8de 5776 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5777 {
0257e383 5778 tree memb_type = TREE_TYPE (memb);
400fbf9f 5779
3e4093b6 5780 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5781 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5782 break;
e58cd767 5783
3e4093b6
RS
5784 if (TREE_CODE (memb_type) != POINTER_TYPE)
5785 continue;
2f6e4e97 5786
3e4093b6
RS
5787 if (coder == POINTER_TYPE)
5788 {
5789 tree ttl = TREE_TYPE (memb_type);
5790 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5791
3e4093b6
RS
5792 /* Any non-function converts to a [const][volatile] void *
5793 and vice versa; otherwise, targets must be the same.
5794 Meanwhile, the lhs target must have all the qualifiers of
5795 the rhs. */
267bac10
JM
5796 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5797 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5798 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5799 {
267bac10
JM
5800 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5801 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5802 /* If this type won't generate any warnings, use it. */
267bac10 5803 if (lquals == rquals
3e4093b6
RS
5804 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5805 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5806 ? ((lquals | rquals) == rquals)
5807 : ((lquals | rquals) == lquals)))
3e4093b6 5808 break;
400fbf9f 5809
3e4093b6 5810 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5811 if (!marginal_memb)
5812 marginal_memb = memb;
3e4093b6
RS
5813 }
5814 }
82bde854 5815
3e4093b6 5816 /* Can convert integer zero to any pointer type. */
928c19bb 5817 if (null_pointer_constant)
3e4093b6
RS
5818 {
5819 rhs = null_pointer_node;
5820 break;
5821 }
5822 }
400fbf9f 5823
0257e383 5824 if (memb || marginal_memb)
3e4093b6 5825 {
0257e383 5826 if (!memb)
3e4093b6
RS
5827 {
5828 /* We have only a marginally acceptable member type;
5829 it needs a warning. */
0257e383 5830 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5831 tree ttr = TREE_TYPE (rhstype);
714a0864 5832
3e4093b6
RS
5833 /* Const and volatile mean something different for function
5834 types, so the usual warnings are not appropriate. */
5835 if (TREE_CODE (ttr) == FUNCTION_TYPE
5836 && TREE_CODE (ttl) == FUNCTION_TYPE)
5837 {
5838 /* Because const and volatile on functions are
5839 restrictions that say the function will not do
5840 certain things, it is okay to use a const or volatile
5841 function where an ordinary one is wanted, but not
5842 vice-versa. */
36c5e70a
BE
5843 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5844 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 5845 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5846 G_("passing argument %d of %qE "
49706e39 5847 "makes %q#v qualified function "
2ac2f164 5848 "pointer from unqualified"),
49706e39 5849 G_("assignment makes %q#v qualified "
2ac2f164
JM
5850 "function pointer from "
5851 "unqualified"),
49706e39 5852 G_("initialization makes %q#v qualified "
2ac2f164
JM
5853 "function pointer from "
5854 "unqualified"),
49706e39
MLI
5855 G_("return makes %q#v qualified function "
5856 "pointer from unqualified"),
5857 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 5858 }
36c5e70a
BE
5859 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5860 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
49706e39 5861 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5862 G_("passing argument %d of %qE discards "
49706e39
MLI
5863 "%qv qualifier from pointer target type"),
5864 G_("assignment discards %qv qualifier "
2ac2f164 5865 "from pointer target type"),
49706e39 5866 G_("initialization discards %qv qualifier "
2ac2f164 5867 "from pointer target type"),
49706e39
MLI
5868 G_("return discards %qv qualifier from "
5869 "pointer target type"),
5870 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
5871
5872 memb = marginal_memb;
3e4093b6 5873 }
400fbf9f 5874
fcf73884 5875 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 5876 pedwarn (location, OPT_Wpedantic,
fcf73884 5877 "ISO C prohibits argument conversion to union type");
0e7c47fa 5878
db3927fb 5879 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 5880 return build_constructor_single (type, memb, rhs);
3e4093b6 5881 }
0e7c47fa
RK
5882 }
5883
3e4093b6
RS
5884 /* Conversions among pointers */
5885 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5886 && (coder == codel))
400fbf9f 5887 {
3e4093b6
RS
5888 tree ttl = TREE_TYPE (type);
5889 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
5890 tree mvl = ttl;
5891 tree mvr = ttr;
3e4093b6 5892 bool is_opaque_pointer;
264fa2db 5893 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
5894 addr_space_t asl;
5895 addr_space_t asr;
400fbf9f 5896
46df2823 5897 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
5898 mvl = (TYPE_ATOMIC (mvl)
5899 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5900 TYPE_QUAL_ATOMIC)
5901 : TYPE_MAIN_VARIANT (mvl));
46df2823 5902 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
5903 mvr = (TYPE_ATOMIC (mvr)
5904 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5905 TYPE_QUAL_ATOMIC)
5906 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 5907 /* Opaque pointers are treated like void pointers. */
f83c7f63 5908 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 5909
478a1c5b
ILT
5910 /* The Plan 9 compiler permits a pointer to a struct to be
5911 automatically converted into a pointer to an anonymous field
5912 within the struct. */
5913 if (flag_plan9_extensions
5914 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5915 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5916 && mvl != mvr)
5917 {
5918 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5919 if (new_rhs != NULL_TREE)
5920 {
5921 rhs = new_rhs;
5922 rhstype = TREE_TYPE (rhs);
5923 coder = TREE_CODE (rhstype);
5924 ttr = TREE_TYPE (rhstype);
5925 mvr = TYPE_MAIN_VARIANT (ttr);
5926 }
5927 }
5928
b7e20b53 5929 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
5930 for the purpose of reducing the number of false positives, we
5931 tolerate the special case of
b7e20b53 5932
c22cacf3 5933 int *p = NULL;
b7e20b53 5934
c22cacf3 5935 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 5936 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
c2255bc4
AH
5937 warning_at (location, OPT_Wc___compat,
5938 "request for implicit conversion "
5939 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 5940
36c5e70a
BE
5941 /* See if the pointers point to incompatible address spaces. */
5942 asl = TYPE_ADDR_SPACE (ttl);
5943 asr = TYPE_ADDR_SPACE (ttr);
5944 if (!null_pointer_constant_p (rhs)
5945 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5946 {
5947 switch (errtype)
5948 {
5949 case ic_argpass:
5950 error_at (location, "passing argument %d of %qE from pointer to "
5951 "non-enclosed address space", parmnum, rname);
5952 break;
5953 case ic_assign:
5954 error_at (location, "assignment from pointer to "
5955 "non-enclosed address space");
5956 break;
5957 case ic_init:
5958 error_at (location, "initialization from pointer to "
5959 "non-enclosed address space");
5960 break;
5961 case ic_return:
5962 error_at (location, "return from pointer to "
5963 "non-enclosed address space");
5964 break;
5965 default:
5966 gcc_unreachable ();
5967 }
5968 return error_mark_node;
5969 }
5970
7876a414
KG
5971 /* Check if the right-hand side has a format attribute but the
5972 left-hand side doesn't. */
90137d8f 5973 if (warn_suggest_attribute_format
104f8784 5974 && check_missing_format_attribute (type, rhstype))
c22cacf3 5975 {
104f8784
KG
5976 switch (errtype)
5977 {
5978 case ic_argpass:
90137d8f 5979 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5980 "argument %d of %qE might be "
5981 "a candidate for a format attribute",
5982 parmnum, rname);
104f8784
KG
5983 break;
5984 case ic_assign:
90137d8f 5985 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5986 "assignment left-hand side might be "
5987 "a candidate for a format attribute");
104f8784
KG
5988 break;
5989 case ic_init:
90137d8f 5990 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5991 "initialization left-hand side might be "
5992 "a candidate for a format attribute");
104f8784
KG
5993 break;
5994 case ic_return:
90137d8f 5995 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5996 "return type might be "
5997 "a candidate for a format attribute");
104f8784
KG
5998 break;
5999 default:
6000 gcc_unreachable ();
6001 }
7876a414 6002 }
c22cacf3 6003
3e4093b6
RS
6004 /* Any non-function converts to a [const][volatile] void *
6005 and vice versa; otherwise, targets must be the same.
6006 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6007 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6008 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6009 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6010 || is_opaque_pointer
f8a93a2e
JJ
6011 || ((c_common_unsigned_type (mvl)
6012 == c_common_unsigned_type (mvr))
267bac10
JM
6013 && (c_common_signed_type (mvl)
6014 == c_common_signed_type (mvr))
6015 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6
RS
6016 {
6017 if (pedantic
6018 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6019 ||
6020 (VOID_TYPE_P (ttr)
928c19bb 6021 && !null_pointer_constant
3e4093b6 6022 && TREE_CODE (ttl) == FUNCTION_TYPE)))
c1771a20 6023 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
509c9d60 6024 G_("ISO C forbids passing argument %d of "
2ac2f164
JM
6025 "%qE between function pointer "
6026 "and %<void *%>"),
4b794eaf 6027 G_("ISO C forbids assignment between "
2ac2f164 6028 "function pointer and %<void *%>"),
4b794eaf 6029 G_("ISO C forbids initialization between "
2ac2f164 6030 "function pointer and %<void *%>"),
4b794eaf 6031 G_("ISO C forbids return between function "
2ac2f164 6032 "pointer and %<void *%>"));
3e4093b6
RS
6033 /* Const and volatile mean something different for function types,
6034 so the usual warnings are not appropriate. */
6035 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6036 && TREE_CODE (ttl) != FUNCTION_TYPE)
6037 {
267bac10
JM
6038 /* Assignments between atomic and non-atomic objects are OK. */
6039 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6040 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6041 {
6c39e757
NP
6042 WARN_FOR_QUALIFIERS (location, 0,
6043 G_("passing argument %d of %qE discards "
6044 "%qv qualifier from pointer target type"),
6045 G_("assignment discards %qv qualifier "
6046 "from pointer target type"),
6047 G_("initialization discards %qv qualifier "
6048 "from pointer target type"),
6049 G_("return discards %qv qualifier from "
6050 "pointer target type"),
6051 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6052 }
3e4093b6
RS
6053 /* If this is not a case of ignoring a mismatch in signedness,
6054 no warning. */
6055 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6056 || target_cmp)
3e4093b6
RS
6057 ;
6058 /* If there is a mismatch, do warn. */
f2fd3821 6059 else if (warn_pointer_sign)
c2255bc4 6060 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
509c9d60 6061 G_("pointer targets in passing argument "
2ac2f164 6062 "%d of %qE differ in signedness"),
4b794eaf 6063 G_("pointer targets in assignment "
2ac2f164 6064 "differ in signedness"),
4b794eaf 6065 G_("pointer targets in initialization "
2ac2f164 6066 "differ in signedness"),
4b794eaf 6067 G_("pointer targets in return differ "
2ac2f164 6068 "in signedness"));
3e4093b6
RS
6069 }
6070 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6071 && TREE_CODE (ttr) == FUNCTION_TYPE)
6072 {
6073 /* Because const and volatile on functions are restrictions
6074 that say the function will not do certain things,
6075 it is okay to use a const or volatile function
6076 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6077 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6078 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 6079 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 6080 G_("passing argument %d of %qE makes "
49706e39 6081 "%q#v qualified function pointer "
2ac2f164 6082 "from unqualified"),
49706e39 6083 G_("assignment makes %q#v qualified function "
2ac2f164 6084 "pointer from unqualified"),
49706e39 6085 G_("initialization makes %q#v qualified "
2ac2f164 6086 "function pointer from unqualified"),
49706e39
MLI
6087 G_("return makes %q#v qualified function "
6088 "pointer from unqualified"),
6089 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6090 }
6091 }
6092 else
58393038
ZL
6093 /* Avoid warning about the volatile ObjC EH puts on decls. */
6094 if (!objc_ok)
c2255bc4 6095 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6096 G_("passing argument %d of %qE from "
58393038 6097 "incompatible pointer type"),
4b794eaf
JJ
6098 G_("assignment from incompatible pointer type"),
6099 G_("initialization from incompatible "
58393038 6100 "pointer type"),
4b794eaf 6101 G_("return from incompatible pointer type"));
58393038 6102
3e4093b6
RS
6103 return convert (type, rhs);
6104 }
b494fd98
EB
6105 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6106 {
6dcc04b0
JM
6107 /* ??? This should not be an error when inlining calls to
6108 unprototyped functions. */
c2255bc4 6109 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6110 return error_mark_node;
6111 }
3e4093b6 6112 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6113 {
3e4093b6
RS
6114 /* An explicit constant 0 can convert to a pointer,
6115 or one that results from arithmetic, even including
6116 a cast to integer type. */
928c19bb 6117 if (!null_pointer_constant)
c2255bc4 6118 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6119 G_("passing argument %d of %qE makes "
2ac2f164 6120 "pointer from integer without a cast"),
4b794eaf 6121 G_("assignment makes pointer from integer "
2ac2f164 6122 "without a cast"),
4b794eaf 6123 G_("initialization makes pointer from "
2ac2f164 6124 "integer without a cast"),
4b794eaf 6125 G_("return makes pointer from integer "
2ac2f164 6126 "without a cast"));
b3006337
EB
6127
6128 return convert (type, rhs);
400fbf9f 6129 }
3e4093b6 6130 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6131 {
c2255bc4 6132 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6133 G_("passing argument %d of %qE makes integer "
2ac2f164 6134 "from pointer without a cast"),
4b794eaf 6135 G_("assignment makes integer from pointer "
2ac2f164 6136 "without a cast"),
4b794eaf 6137 G_("initialization makes integer from pointer "
2ac2f164 6138 "without a cast"),
4b794eaf 6139 G_("return makes integer from pointer "
2ac2f164 6140 "without a cast"));
3e4093b6
RS
6141 return convert (type, rhs);
6142 }
6143 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6144 {
6145 tree ret;
6146 bool save = in_late_binary_op;
6147 in_late_binary_op = true;
6148 ret = convert (type, rhs);
6149 in_late_binary_op = save;
6150 return ret;
6151 }
400fbf9f 6152
2ac2f164 6153 switch (errtype)
3e4093b6 6154 {
2ac2f164 6155 case ic_argpass:
c2255bc4 6156 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
a7da8b42
MLI
6157 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6158 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
6159 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6160 break;
6161 case ic_assign:
c2255bc4
AH
6162 error_at (location, "incompatible types when assigning to type %qT from "
6163 "type %qT", type, rhstype);
2ac2f164
JM
6164 break;
6165 case ic_init:
c2255bc4
AH
6166 error_at (location,
6167 "incompatible types when initializing type %qT using type %qT",
6168 type, rhstype);
2ac2f164
JM
6169 break;
6170 case ic_return:
c2255bc4
AH
6171 error_at (location,
6172 "incompatible types when returning type %qT but %qT was "
6173 "expected", rhstype, type);
2ac2f164
JM
6174 break;
6175 default:
6176 gcc_unreachable ();
400fbf9f 6177 }
53b01f59 6178
3e4093b6
RS
6179 return error_mark_node;
6180}
3e4093b6
RS
6181\f
6182/* If VALUE is a compound expr all of whose expressions are constant, then
6183 return its value. Otherwise, return error_mark_node.
15b732b2 6184
3e4093b6
RS
6185 This is for handling COMPOUND_EXPRs as initializer elements
6186 which is allowed with a warning when -pedantic is specified. */
15b732b2 6187
3e4093b6
RS
6188static tree
6189valid_compound_expr_initializer (tree value, tree endtype)
6190{
6191 if (TREE_CODE (value) == COMPOUND_EXPR)
6192 {
6193 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6194 == error_mark_node)
6195 return error_mark_node;
6196 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6197 endtype);
6198 }
116df786 6199 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6200 return error_mark_node;
6201 else
6202 return value;
15b732b2 6203}
400fbf9f 6204\f
3e4093b6
RS
6205/* Perform appropriate conversions on the initial value of a variable,
6206 store it in the declaration DECL,
6207 and print any error messages that are appropriate.
bbbbb16a 6208 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6209 If the init is invalid, store an ERROR_MARK.
6210
6211 INIT_LOC is the location of the initial value. */
400fbf9f 6212
3e4093b6 6213void
c2255bc4 6214store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6215{
3e4093b6 6216 tree value, type;
928c19bb 6217 bool npc = false;
400fbf9f 6218
3e4093b6 6219 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6220
3e4093b6
RS
6221 type = TREE_TYPE (decl);
6222 if (TREE_CODE (type) == ERROR_MARK)
6223 return;
400fbf9f 6224
3e4093b6 6225 /* Digest the specified initializer into an expression. */
400fbf9f 6226
928c19bb
JM
6227 if (init)
6228 npc = null_pointer_constant_p (init);
c2255bc4
AH
6229 value = digest_init (init_loc, type, init, origtype, npc,
6230 true, TREE_STATIC (decl));
400fbf9f 6231
3e4093b6 6232 /* Store the expression if valid; else report error. */
400fbf9f 6233
8400e75e 6234 if (!in_system_header_at (input_location)
3f75a254 6235 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6236 warning (OPT_Wtraditional, "traditional C rejects automatic "
6237 "aggregate initialization");
2f6e4e97 6238
3e4093b6 6239 DECL_INITIAL (decl) = value;
400fbf9f 6240
3e4093b6
RS
6241 /* ANSI wants warnings about out-of-range constant initializers. */
6242 STRIP_TYPE_NOPS (value);
b8698a0f 6243 if (TREE_STATIC (decl))
c2658540 6244 constant_expression_warning (value);
400fbf9f 6245
3e4093b6
RS
6246 /* Check if we need to set array size from compound literal size. */
6247 if (TREE_CODE (type) == ARRAY_TYPE
6248 && TYPE_DOMAIN (type) == 0
6249 && value != error_mark_node)
400fbf9f 6250 {
3e4093b6
RS
6251 tree inside_init = init;
6252
ed248cf7 6253 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6254 inside_init = fold (inside_init);
6255
6256 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6257 {
8d9f82d5 6258 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6259
8d9f82d5 6260 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6261 {
6262 /* For int foo[] = (int [3]){1}; we need to set array size
6263 now since later on array initializer will be just the
6264 brace enclosed list of the compound literal. */
e30ecc5d 6265 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6266 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6267 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6268 layout_type (type);
8d9f82d5 6269 layout_decl (cldecl, 0);
e30ecc5d
JJ
6270 TREE_TYPE (decl)
6271 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6272 }
6273 }
400fbf9f 6274 }
3e4093b6
RS
6275}
6276\f
6277/* Methods for storing and printing names for error messages. */
400fbf9f 6278
3e4093b6
RS
6279/* Implement a spelling stack that allows components of a name to be pushed
6280 and popped. Each element on the stack is this structure. */
400fbf9f 6281
3e4093b6
RS
6282struct spelling
6283{
6284 int kind;
6285 union
400fbf9f 6286 {
a0f0ab9f 6287 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6288 const char *s;
6289 } u;
6290};
2f6e4e97 6291
3e4093b6
RS
6292#define SPELLING_STRING 1
6293#define SPELLING_MEMBER 2
6294#define SPELLING_BOUNDS 3
400fbf9f 6295
3e4093b6
RS
6296static struct spelling *spelling; /* Next stack element (unused). */
6297static struct spelling *spelling_base; /* Spelling stack base. */
6298static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6299
3e4093b6
RS
6300/* Macros to save and restore the spelling stack around push_... functions.
6301 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6302
3e4093b6
RS
6303#define SPELLING_DEPTH() (spelling - spelling_base)
6304#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6305
3e4093b6
RS
6306/* Push an element on the spelling stack with type KIND and assign VALUE
6307 to MEMBER. */
400fbf9f 6308
3e4093b6
RS
6309#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6310{ \
6311 int depth = SPELLING_DEPTH (); \
6312 \
6313 if (depth >= spelling_size) \
6314 { \
6315 spelling_size += 10; \
cca8ead2
BI
6316 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6317 spelling_size); \
3e4093b6
RS
6318 RESTORE_SPELLING_DEPTH (depth); \
6319 } \
6320 \
6321 spelling->kind = (KIND); \
6322 spelling->MEMBER = (VALUE); \
6323 spelling++; \
6324}
400fbf9f 6325
3e4093b6 6326/* Push STRING on the stack. Printed literally. */
400fbf9f 6327
3e4093b6
RS
6328static void
6329push_string (const char *string)
6330{
6331 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6332}
400fbf9f 6333
3e4093b6 6334/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6335
3e4093b6
RS
6336static void
6337push_member_name (tree decl)
6338{
6339 const char *const string
88388a52
JM
6340 = (DECL_NAME (decl)
6341 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6342 : _("<anonymous>"));
3e4093b6
RS
6343 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6344}
400fbf9f 6345
3e4093b6 6346/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6347
3e4093b6 6348static void
a0f0ab9f 6349push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6350{
6351 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6352}
bb58bec5 6353
3e4093b6 6354/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6355
3e4093b6
RS
6356static int
6357spelling_length (void)
6358{
6359 int size = 0;
6360 struct spelling *p;
400fbf9f 6361
3e4093b6
RS
6362 for (p = spelling_base; p < spelling; p++)
6363 {
6364 if (p->kind == SPELLING_BOUNDS)
6365 size += 25;
6366 else
6367 size += strlen (p->u.s) + 1;
6368 }
6369
6370 return size;
400fbf9f 6371}
400fbf9f 6372
3e4093b6 6373/* Print the spelling to BUFFER and return it. */
400fbf9f 6374
3e4093b6
RS
6375static char *
6376print_spelling (char *buffer)
400fbf9f 6377{
3e4093b6
RS
6378 char *d = buffer;
6379 struct spelling *p;
400fbf9f 6380
3e4093b6
RS
6381 for (p = spelling_base; p < spelling; p++)
6382 if (p->kind == SPELLING_BOUNDS)
6383 {
a0f0ab9f 6384 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6385 d += strlen (d);
6386 }
6387 else
6388 {
6389 const char *s;
6390 if (p->kind == SPELLING_MEMBER)
6391 *d++ = '.';
6392 for (s = p->u.s; (*d = *s++); d++)
6393 ;
6394 }
6395 *d++ = '\0';
6396 return buffer;
6397}
400fbf9f 6398
3e4093b6 6399/* Issue an error message for a bad initializer component.
6a8f4e12 6400 GMSGID identifies the message.
3e4093b6 6401 The component name is taken from the spelling stack. */
400fbf9f 6402
3e4093b6 6403void
6a8f4e12 6404error_init (const char *gmsgid)
3e4093b6
RS
6405{
6406 char *ofwhat;
400fbf9f 6407
6a8f4e12
AP
6408 /* The gmsgid may be a format string with %< and %>. */
6409 error (gmsgid);
28dab132 6410 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6411 if (*ofwhat)
bda67431 6412 error ("(near initialization for %qs)", ofwhat);
3e4093b6 6413}
400fbf9f 6414
fcf73884
MLI
6415/* Issue a pedantic warning for a bad initializer component. OPT is
6416 the option OPT_* (from options.h) controlling this warning or 0 if
6a8f4e12 6417 it is unconditionally given. GMSGID identifies the message. The
fcf73884 6418 component name is taken from the spelling stack. */
400fbf9f 6419
3e4093b6 6420void
6a8f4e12 6421pedwarn_init (location_t location, int opt, const char *gmsgid)
3e4093b6
RS
6422{
6423 char *ofwhat;
f90e8e2e 6424
6a8f4e12
AP
6425 /* The gmsgid may be a format string with %< and %>. */
6426 pedwarn (location, opt, gmsgid);
28dab132 6427 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6428 if (*ofwhat)
509c9d60 6429 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
3e4093b6 6430}
9f720c3e 6431
b8698a0f 6432/* Issue a warning for a bad initializer component.
683d6ff9
MLI
6433
6434 OPT is the OPT_W* value corresponding to the warning option that
6a8f4e12 6435 controls this warning. GMSGID identifies the message. The
683d6ff9 6436 component name is taken from the spelling stack. */
61179109 6437
3e4093b6 6438static void
6a8f4e12 6439warning_init (int opt, const char *gmsgid)
3e4093b6
RS
6440{
6441 char *ofwhat;
7e842ef8 6442
6a8f4e12
AP
6443 /* The gmsgid may be a format string with %< and %>. */
6444 warning (opt, gmsgid);
28dab132 6445 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6446 if (*ofwhat)
683d6ff9 6447 warning (opt, "(near initialization for %qs)", ofwhat);
3e4093b6
RS
6448}
6449\f
916c5919
JM
6450/* If TYPE is an array type and EXPR is a parenthesized string
6451 constant, warn if pedantic that EXPR is being used to initialize an
6452 object of type TYPE. */
6453
6454void
6455maybe_warn_string_init (tree type, struct c_expr expr)
6456{
6457 if (pedantic
6458 && TREE_CODE (type) == ARRAY_TYPE
6459 && TREE_CODE (expr.value) == STRING_CST
6460 && expr.original_code != STRING_CST)
c1771a20 6461 pedwarn_init (input_location, OPT_Wpedantic,
fcf73884 6462 "array initialized from parenthesized string constant");
916c5919
JM
6463}
6464
3e4093b6
RS
6465/* Digest the parser output INIT as an initializer for type TYPE.
6466 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6467
bbbbb16a
ILT
6468 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6469
928c19bb
JM
6470 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6471
916c5919
JM
6472 If INIT is a string constant, STRICT_STRING is true if it is
6473 unparenthesized or we should not warn here for it being parenthesized.
6474 For other types of INIT, STRICT_STRING is not used.
6475
c2255bc4
AH
6476 INIT_LOC is the location of the INIT.
6477
3e4093b6
RS
6478 REQUIRE_CONSTANT requests an error if non-constant initializers or
6479 elements are seen. */
7e842ef8 6480
3e4093b6 6481static tree
c2255bc4
AH
6482digest_init (location_t init_loc, tree type, tree init, tree origtype,
6483 bool null_pointer_constant, bool strict_string,
6484 int require_constant)
3e4093b6
RS
6485{
6486 enum tree_code code = TREE_CODE (type);
6487 tree inside_init = init;
8ce94e44 6488 tree semantic_type = NULL_TREE;
928c19bb 6489 bool maybe_const = true;
7e842ef8 6490
3e4093b6 6491 if (type == error_mark_node
f01da1a5 6492 || !init
3e4093b6
RS
6493 || init == error_mark_node
6494 || TREE_TYPE (init) == error_mark_node)
6495 return error_mark_node;
7e842ef8 6496
ed248cf7 6497 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6498
8ce94e44
JM
6499 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6500 {
6501 semantic_type = TREE_TYPE (inside_init);
6502 inside_init = TREE_OPERAND (inside_init, 0);
6503 }
928c19bb
JM
6504 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6505 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6506
3e4093b6
RS
6507 /* Initialization of an array of chars from a string constant
6508 optionally enclosed in braces. */
7e842ef8 6509
197463ae
JM
6510 if (code == ARRAY_TYPE && inside_init
6511 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6512 {
267bac10
JM
6513 tree typ1
6514 = (TYPE_ATOMIC (TREE_TYPE (type))
6515 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6516 TYPE_QUAL_ATOMIC)
6517 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6518 /* Note that an array could be both an array of character type
6519 and an array of wchar_t if wchar_t is signed char or unsigned
6520 char. */
6521 bool char_array = (typ1 == char_type_node
6522 || typ1 == signed_char_type_node
6523 || typ1 == unsigned_char_type_node);
6524 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6525 bool char16_array = !!comptypes (typ1, char16_type_node);
6526 bool char32_array = !!comptypes (typ1, char32_type_node);
6527
6528 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6529 {
916c5919 6530 struct c_expr expr;
c466b2cd 6531 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6532 expr.value = inside_init;
6533 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6534 expr.original_type = NULL;
916c5919
JM
6535 maybe_warn_string_init (type, expr);
6536
a45e580b 6537 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6538 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6539 "initialization of a flexible array member");
6540
3e4093b6 6541 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6542 TYPE_MAIN_VARIANT (type)))
3e4093b6 6543 return inside_init;
7e842ef8 6544
c466b2cd 6545 if (char_array)
3e4093b6 6546 {
c466b2cd
KVH
6547 if (typ2 != char_type_node)
6548 {
6549 error_init ("char-array initialized from wide string");
6550 return error_mark_node;
6551 }
3e4093b6 6552 }
c466b2cd 6553 else
3e4093b6 6554 {
c466b2cd
KVH
6555 if (typ2 == char_type_node)
6556 {
6557 error_init ("wide character array initialized from non-wide "
6558 "string");
6559 return error_mark_node;
6560 }
6561 else if (!comptypes(typ1, typ2))
6562 {
6563 error_init ("wide character array initialized from "
6564 "incompatible wide string");
6565 return error_mark_node;
6566 }
7e842ef8 6567 }
2f6e4e97 6568
3e4093b6
RS
6569 TREE_TYPE (inside_init) = type;
6570 if (TYPE_DOMAIN (type) != 0
6571 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6572 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6573 {
6574 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6575
c466b2cd 6576 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6577 because it's ok to ignore the terminating null char
6578 that is counted in the length of the constant. */
5eb4df45
ILT
6579 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6580 (len
6581 - (TYPE_PRECISION (typ1)
6582 / BITS_PER_UNIT))))
6583 pedwarn_init (init_loc, 0,
6584 ("initializer-string for array of chars "
6585 "is too long"));
6586 else if (warn_cxx_compat
6587 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6588 warning_at (init_loc, OPT_Wc___compat,
6589 ("initializer-string for array chars "
6590 "is too long for C++"));
6591 }
7e842ef8 6592
3e4093b6 6593 return inside_init;
7e842ef8 6594 }
197463ae
JM
6595 else if (INTEGRAL_TYPE_P (typ1))
6596 {
6597 error_init ("array of inappropriate type initialized "
6598 "from string constant");
6599 return error_mark_node;
6600 }
7e842ef8
PE
6601 }
6602
3e4093b6
RS
6603 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6604 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6605 below and handle as a constructor. */
e89be13b
JJ
6606 if (code == VECTOR_TYPE
6607 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6608 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6609 && TREE_CONSTANT (inside_init))
6610 {
6611 if (TREE_CODE (inside_init) == VECTOR_CST
6612 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6613 TYPE_MAIN_VARIANT (type)))
6614 return inside_init;
6615
6616 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6617 {
4038c495
GB
6618 unsigned HOST_WIDE_INT ix;
6619 tree value;
6620 bool constant_p = true;
e89be13b
JJ
6621
6622 /* Iterate through elements and check if all constructor
6623 elements are *_CSTs. */
4038c495
GB
6624 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6625 if (!CONSTANT_CLASS_P (value))
6626 {
6627 constant_p = false;
6628 break;
6629 }
e89be13b 6630
4038c495
GB
6631 if (constant_p)
6632 return build_vector_from_ctor (type,
6633 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6634 }
6635 }
6035d635 6636
ca085fd7
MLI
6637 if (warn_sequence_point)
6638 verify_sequence_points (inside_init);
6639
3e4093b6
RS
6640 /* Any type can be initialized
6641 from an expression of the same type, optionally with braces. */
400fbf9f 6642
3e4093b6
RS
6643 if (inside_init && TREE_TYPE (inside_init) != 0
6644 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6645 TYPE_MAIN_VARIANT (type))
3e4093b6 6646 || (code == ARRAY_TYPE
132da1a5 6647 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6648 || (code == VECTOR_TYPE
132da1a5 6649 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6650 || (code == POINTER_TYPE
3897f229 6651 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6652 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6653 TREE_TYPE (type)))))
3e4093b6
RS
6654 {
6655 if (code == POINTER_TYPE)
b494fd98 6656 {
b494fd98
EB
6657 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6658 {
f2a71bbc
JM
6659 if (TREE_CODE (inside_init) == STRING_CST
6660 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6661 inside_init = array_to_pointer_conversion
6662 (init_loc, inside_init);
f2a71bbc
JM
6663 else
6664 {
6665 error_init ("invalid use of non-lvalue array");
6666 return error_mark_node;
6667 }
b494fd98 6668 }
f2a71bbc 6669 }
b494fd98 6670
bae39a73
NS
6671 if (code == VECTOR_TYPE)
6672 /* Although the types are compatible, we may require a
6673 conversion. */
6674 inside_init = convert (type, inside_init);
3e4093b6 6675
ca58211b
PB
6676 if (require_constant
6677 && (code == VECTOR_TYPE || !flag_isoc99)
3e4093b6 6678 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6679 {
3e4093b6
RS
6680 /* As an extension, allow initializing objects with static storage
6681 duration with compound literals (which are then treated just as
ca58211b
PB
6682 the brace enclosed list they contain). Also allow this for
6683 vectors, as we can only assign them with compound literals. */
3e4093b6
RS
6684 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6685 inside_init = DECL_INITIAL (decl);
400fbf9f 6686 }
3e4093b6
RS
6687
6688 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6689 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6690 {
3e4093b6
RS
6691 error_init ("array initialized from non-constant array expression");
6692 return error_mark_node;
400fbf9f 6693 }
400fbf9f 6694
c1771a20 6695 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6696 -pedantic-errors is specified. In the later case, we always want
6697 an error. In the former case, we simply want a warning. */
6698 if (require_constant && pedantic
6699 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6700 {
6701 inside_init
6702 = valid_compound_expr_initializer (inside_init,
6703 TREE_TYPE (inside_init));
6704 if (inside_init == error_mark_node)
6705 error_init ("initializer element is not constant");
2f6e4e97 6706 else
c1771a20 6707 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6708 "initializer element is not constant");
3e4093b6
RS
6709 if (flag_pedantic_errors)
6710 inside_init = error_mark_node;
6711 }
6712 else if (require_constant
116df786
RH
6713 && !initializer_constant_valid_p (inside_init,
6714 TREE_TYPE (inside_init)))
3e4093b6
RS
6715 {
6716 error_init ("initializer element is not constant");
6717 inside_init = error_mark_node;
8b40563c 6718 }
928c19bb 6719 else if (require_constant && !maybe_const)
c2255bc4 6720 pedwarn_init (init_loc, 0,
928c19bb 6721 "initializer element is not a constant expression");
f735a153 6722
90137d8f 6723 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6724 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
c2255bc4
AH
6725 inside_init = convert_for_assignment (init_loc, type, inside_init,
6726 origtype,
bbbbb16a 6727 ic_init, null_pointer_constant,
928c19bb 6728 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6729 return inside_init;
6730 }
f735a153 6731
3e4093b6 6732 /* Handle scalar types, including conversions. */
400fbf9f 6733
ab22c1fa
CF
6734 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6735 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6736 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6737 {
f2a71bbc
JM
6738 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6739 && (TREE_CODE (init) == STRING_CST
6740 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6741 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6742 if (semantic_type)
6743 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6744 inside_init);
3e4093b6 6745 inside_init
c2255bc4
AH
6746 = convert_for_assignment (init_loc, type, inside_init, origtype,
6747 ic_init, null_pointer_constant,
3e4093b6 6748 NULL_TREE, NULL_TREE, 0);
2f6e4e97 6749
3274deff
JW
6750 /* Check to see if we have already given an error message. */
6751 if (inside_init == error_mark_node)
6752 ;
3f75a254 6753 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6754 {
3e4093b6
RS
6755 error_init ("initializer element is not constant");
6756 inside_init = error_mark_node;
400fbf9f 6757 }
3e4093b6 6758 else if (require_constant
116df786
RH
6759 && !initializer_constant_valid_p (inside_init,
6760 TREE_TYPE (inside_init)))
400fbf9f 6761 {
3e4093b6
RS
6762 error_init ("initializer element is not computable at load time");
6763 inside_init = error_mark_node;
400fbf9f 6764 }
928c19bb 6765 else if (require_constant && !maybe_const)
c2255bc4 6766 pedwarn_init (init_loc, 0,
928c19bb 6767 "initializer element is not a constant expression");
3e4093b6
RS
6768
6769 return inside_init;
400fbf9f 6770 }
d9fc6069 6771
3e4093b6 6772 /* Come here only for records and arrays. */
d9fc6069 6773
3e4093b6 6774 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6775 {
3e4093b6
RS
6776 error_init ("variable-sized object may not be initialized");
6777 return error_mark_node;
d9fc6069 6778 }
3e4093b6
RS
6779
6780 error_init ("invalid initializer");
6781 return error_mark_node;
d9fc6069 6782}
400fbf9f 6783\f
3e4093b6 6784/* Handle initializers that use braces. */
400fbf9f 6785
3e4093b6
RS
6786/* Type of object we are accumulating a constructor for.
6787 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6788static tree constructor_type;
400fbf9f 6789
3e4093b6
RS
6790/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6791 left to fill. */
6792static tree constructor_fields;
400fbf9f 6793
3e4093b6
RS
6794/* For an ARRAY_TYPE, this is the specified index
6795 at which to store the next element we get. */
6796static tree constructor_index;
400fbf9f 6797
3e4093b6
RS
6798/* For an ARRAY_TYPE, this is the maximum index. */
6799static tree constructor_max_index;
400fbf9f 6800
3e4093b6
RS
6801/* For a RECORD_TYPE, this is the first field not yet written out. */
6802static tree constructor_unfilled_fields;
400fbf9f 6803
3e4093b6
RS
6804/* For an ARRAY_TYPE, this is the index of the first element
6805 not yet written out. */
6806static tree constructor_unfilled_index;
895ea614 6807
3e4093b6
RS
6808/* In a RECORD_TYPE, the byte index of the next consecutive field.
6809 This is so we can generate gaps between fields, when appropriate. */
6810static tree constructor_bit_index;
10d5caec 6811
3e4093b6
RS
6812/* If we are saving up the elements rather than allocating them,
6813 this is the list of elements so far (in reverse order,
6814 most recent first). */
9771b263 6815static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6816
3e4093b6
RS
6817/* 1 if constructor should be incrementally stored into a constructor chain,
6818 0 if all the elements should be kept in AVL tree. */
6819static int constructor_incremental;
ad47f1e5 6820
3e4093b6
RS
6821/* 1 if so far this constructor's elements are all compile-time constants. */
6822static int constructor_constant;
ad47f1e5 6823
3e4093b6
RS
6824/* 1 if so far this constructor's elements are all valid address constants. */
6825static int constructor_simple;
ad47f1e5 6826
928c19bb
JM
6827/* 1 if this constructor has an element that cannot be part of a
6828 constant expression. */
6829static int constructor_nonconst;
6830
3e4093b6
RS
6831/* 1 if this constructor is erroneous so far. */
6832static int constructor_erroneous;
d45cf215 6833
3e4093b6
RS
6834/* Structure for managing pending initializer elements, organized as an
6835 AVL tree. */
d45cf215 6836
3e4093b6 6837struct init_node
d45cf215 6838{
3e4093b6
RS
6839 struct init_node *left, *right;
6840 struct init_node *parent;
6841 int balance;
6842 tree purpose;
6843 tree value;
bbbbb16a 6844 tree origtype;
d45cf215
RS
6845};
6846
3e4093b6
RS
6847/* Tree of pending elements at this constructor level.
6848 These are elements encountered out of order
6849 which belong at places we haven't reached yet in actually
6850 writing the output.
6851 Will never hold tree nodes across GC runs. */
6852static struct init_node *constructor_pending_elts;
d45cf215 6853
3e4093b6
RS
6854/* The SPELLING_DEPTH of this constructor. */
6855static int constructor_depth;
d45cf215 6856
3e4093b6
RS
6857/* DECL node for which an initializer is being read.
6858 0 means we are reading a constructor expression
6859 such as (struct foo) {...}. */
6860static tree constructor_decl;
d45cf215 6861
3e4093b6
RS
6862/* Nonzero if this is an initializer for a top-level decl. */
6863static int constructor_top_level;
d45cf215 6864
3e4093b6
RS
6865/* Nonzero if there were any member designators in this initializer. */
6866static int constructor_designated;
d45cf215 6867
3e4093b6
RS
6868/* Nesting depth of designator list. */
6869static int designator_depth;
d45cf215 6870
3e4093b6 6871/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 6872static int designator_erroneous;
d45cf215 6873
3e4093b6
RS
6874\f
6875/* This stack has a level for each implicit or explicit level of
6876 structuring in the initializer, including the outermost one. It
6877 saves the values of most of the variables above. */
d45cf215 6878
3e4093b6
RS
6879struct constructor_range_stack;
6880
6881struct constructor_stack
d45cf215 6882{
3e4093b6
RS
6883 struct constructor_stack *next;
6884 tree type;
6885 tree fields;
6886 tree index;
6887 tree max_index;
6888 tree unfilled_index;
6889 tree unfilled_fields;
6890 tree bit_index;
9771b263 6891 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6892 struct init_node *pending_elts;
6893 int offset;
6894 int depth;
916c5919 6895 /* If value nonzero, this value should replace the entire
3e4093b6 6896 constructor at this level. */
916c5919 6897 struct c_expr replacement_value;
3e4093b6
RS
6898 struct constructor_range_stack *range_stack;
6899 char constant;
6900 char simple;
928c19bb 6901 char nonconst;
3e4093b6
RS
6902 char implicit;
6903 char erroneous;
6904 char outer;
6905 char incremental;
6906 char designated;
6907};
d45cf215 6908
802415d1 6909static struct constructor_stack *constructor_stack;
d45cf215 6910
3e4093b6
RS
6911/* This stack represents designators from some range designator up to
6912 the last designator in the list. */
d45cf215 6913
3e4093b6
RS
6914struct constructor_range_stack
6915{
6916 struct constructor_range_stack *next, *prev;
6917 struct constructor_stack *stack;
6918 tree range_start;
6919 tree index;
6920 tree range_end;
6921 tree fields;
6922};
d45cf215 6923
802415d1 6924static struct constructor_range_stack *constructor_range_stack;
d45cf215 6925
3e4093b6
RS
6926/* This stack records separate initializers that are nested.
6927 Nested initializers can't happen in ANSI C, but GNU C allows them
6928 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 6929
3e4093b6 6930struct initializer_stack
d45cf215 6931{
3e4093b6
RS
6932 struct initializer_stack *next;
6933 tree decl;
3e4093b6
RS
6934 struct constructor_stack *constructor_stack;
6935 struct constructor_range_stack *constructor_range_stack;
9771b263 6936 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6937 struct spelling *spelling;
6938 struct spelling *spelling_base;
6939 int spelling_size;
6940 char top_level;
6941 char require_constant_value;
6942 char require_constant_elements;
6943};
d45cf215 6944
802415d1 6945static struct initializer_stack *initializer_stack;
3e4093b6
RS
6946\f
6947/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
6948
6949void
a396f8ae 6950start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 6951{
3e4093b6 6952 const char *locus;
cceb1885 6953 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 6954
3e4093b6 6955 p->decl = constructor_decl;
3e4093b6
RS
6956 p->require_constant_value = require_constant_value;
6957 p->require_constant_elements = require_constant_elements;
6958 p->constructor_stack = constructor_stack;
6959 p->constructor_range_stack = constructor_range_stack;
6960 p->elements = constructor_elements;
6961 p->spelling = spelling;
6962 p->spelling_base = spelling_base;
6963 p->spelling_size = spelling_size;
6964 p->top_level = constructor_top_level;
6965 p->next = initializer_stack;
6966 initializer_stack = p;
400fbf9f 6967
3e4093b6 6968 constructor_decl = decl;
3e4093b6
RS
6969 constructor_designated = 0;
6970 constructor_top_level = top_level;
400fbf9f 6971
6f17bbcf 6972 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
6973 {
6974 require_constant_value = TREE_STATIC (decl);
6975 require_constant_elements
6976 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6977 /* For a scalar, you can always use any value to initialize,
6978 even within braces. */
6979 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6980 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6981 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6982 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 6983 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
6984 }
6985 else
6986 {
6987 require_constant_value = 0;
6988 require_constant_elements = 0;
88388a52 6989 locus = _("(anonymous)");
3e4093b6 6990 }
b71c7f8a 6991
3e4093b6
RS
6992 constructor_stack = 0;
6993 constructor_range_stack = 0;
b71c7f8a 6994
3e4093b6
RS
6995 missing_braces_mentioned = 0;
6996
6997 spelling_base = 0;
6998 spelling_size = 0;
6999 RESTORE_SPELLING_DEPTH (0);
7000
7001 if (locus)
7002 push_string (locus);
7003}
7004
7005void
7006finish_init (void)
b71c7f8a 7007{
3e4093b6 7008 struct initializer_stack *p = initializer_stack;
b71c7f8a 7009
3e4093b6
RS
7010 /* Free the whole constructor stack of this initializer. */
7011 while (constructor_stack)
7012 {
7013 struct constructor_stack *q = constructor_stack;
7014 constructor_stack = q->next;
7015 free (q);
7016 }
7017
366de0ce 7018 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7019
7020 /* Pop back to the data of the outer initializer (if any). */
36579663 7021 free (spelling_base);
3aeb3655 7022
3e4093b6 7023 constructor_decl = p->decl;
3e4093b6
RS
7024 require_constant_value = p->require_constant_value;
7025 require_constant_elements = p->require_constant_elements;
7026 constructor_stack = p->constructor_stack;
7027 constructor_range_stack = p->constructor_range_stack;
7028 constructor_elements = p->elements;
7029 spelling = p->spelling;
7030 spelling_base = p->spelling_base;
7031 spelling_size = p->spelling_size;
7032 constructor_top_level = p->top_level;
7033 initializer_stack = p->next;
7034 free (p);
b71c7f8a 7035}
400fbf9f 7036\f
3e4093b6
RS
7037/* Call here when we see the initializer is surrounded by braces.
7038 This is instead of a call to push_init_level;
7039 it is matched by a call to pop_init_level.
400fbf9f 7040
3e4093b6
RS
7041 TYPE is the type to initialize, for a constructor expression.
7042 For an initializer for a decl, TYPE is zero. */
400fbf9f 7043
3e4093b6
RS
7044void
7045really_start_incremental_init (tree type)
400fbf9f 7046{
5d038c4c 7047 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7048
3e4093b6
RS
7049 if (type == 0)
7050 type = TREE_TYPE (constructor_decl);
400fbf9f 7051
b6fc2cdb
PB
7052 if (TREE_CODE (type) == VECTOR_TYPE
7053 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7054 error ("opaque vector types cannot be initialized");
400fbf9f 7055
3e4093b6
RS
7056 p->type = constructor_type;
7057 p->fields = constructor_fields;
7058 p->index = constructor_index;
7059 p->max_index = constructor_max_index;
7060 p->unfilled_index = constructor_unfilled_index;
7061 p->unfilled_fields = constructor_unfilled_fields;
7062 p->bit_index = constructor_bit_index;
7063 p->elements = constructor_elements;
7064 p->constant = constructor_constant;
7065 p->simple = constructor_simple;
928c19bb 7066 p->nonconst = constructor_nonconst;
3e4093b6
RS
7067 p->erroneous = constructor_erroneous;
7068 p->pending_elts = constructor_pending_elts;
7069 p->depth = constructor_depth;
916c5919
JM
7070 p->replacement_value.value = 0;
7071 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7072 p->replacement_value.original_type = NULL;
3e4093b6
RS
7073 p->implicit = 0;
7074 p->range_stack = 0;
7075 p->outer = 0;
7076 p->incremental = constructor_incremental;
7077 p->designated = constructor_designated;
7078 p->next = 0;
7079 constructor_stack = p;
b13aca19 7080
3e4093b6
RS
7081 constructor_constant = 1;
7082 constructor_simple = 1;
928c19bb 7083 constructor_nonconst = 0;
3e4093b6 7084 constructor_depth = SPELLING_DEPTH ();
9771b263 7085 constructor_elements = NULL;
3e4093b6
RS
7086 constructor_pending_elts = 0;
7087 constructor_type = type;
7088 constructor_incremental = 1;
7089 constructor_designated = 0;
7090 designator_depth = 0;
b06df647 7091 designator_erroneous = 0;
400fbf9f 7092
3e4093b6
RS
7093 if (TREE_CODE (constructor_type) == RECORD_TYPE
7094 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7095 {
3e4093b6
RS
7096 constructor_fields = TYPE_FIELDS (constructor_type);
7097 /* Skip any nameless bit fields at the beginning. */
7098 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7099 && DECL_NAME (constructor_fields) == 0)
910ad8de 7100 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7101
3e4093b6
RS
7102 constructor_unfilled_fields = constructor_fields;
7103 constructor_bit_index = bitsize_zero_node;
400fbf9f 7104 }
3e4093b6
RS
7105 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7106 {
7107 if (TYPE_DOMAIN (constructor_type))
7108 {
7109 constructor_max_index
7110 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7111
3e4093b6
RS
7112 /* Detect non-empty initializations of zero-length arrays. */
7113 if (constructor_max_index == NULL_TREE
7114 && TYPE_SIZE (constructor_type))
9a9d280e 7115 constructor_max_index = integer_minus_one_node;
400fbf9f 7116
3e4093b6
RS
7117 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7118 to initialize VLAs will cause a proper error; avoid tree
7119 checking errors as well by setting a safe value. */
7120 if (constructor_max_index
7121 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7122 constructor_max_index = integer_minus_one_node;
59c83dbf 7123
3e4093b6
RS
7124 constructor_index
7125 = convert (bitsizetype,
7126 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7127 }
3e4093b6 7128 else
493179da
JM
7129 {
7130 constructor_index = bitsize_zero_node;
7131 constructor_max_index = NULL_TREE;
7132 }
59c83dbf 7133
3e4093b6
RS
7134 constructor_unfilled_index = constructor_index;
7135 }
7136 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7137 {
7138 /* Vectors are like simple fixed-size arrays. */
7139 constructor_max_index =
c62c040f 7140 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7141 constructor_index = bitsize_zero_node;
3e4093b6
RS
7142 constructor_unfilled_index = constructor_index;
7143 }
7144 else
7145 {
7146 /* Handle the case of int x = {5}; */
7147 constructor_fields = constructor_type;
7148 constructor_unfilled_fields = constructor_type;
7149 }
7150}
7151\f
7152/* Push down into a subobject, for initialization.
7153 If this is for an explicit set of braces, IMPLICIT is 0.
7154 If it is because the next element belongs at a lower level,
7155 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7156
3e4093b6 7157void
a1e3b3d9 7158push_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
7159{
7160 struct constructor_stack *p;
7161 tree value = NULL_TREE;
400fbf9f 7162
3e4093b6 7163 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7164 pop them now. If implicit == 1, this will have been done in
7165 process_init_element; do not repeat it here because in the case
7166 of excess initializers for an empty aggregate this leads to an
7167 infinite cycle of popping a level and immediately recreating
7168 it. */
7169 if (implicit != 1)
3e4093b6 7170 {
472d98b4
JM
7171 while (constructor_stack->implicit)
7172 {
7173 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7174 || TREE_CODE (constructor_type) == UNION_TYPE)
7175 && constructor_fields == 0)
a1e3b3d9
LB
7176 process_init_element (pop_init_level (1, braced_init_obstack),
7177 true, braced_init_obstack);
472d98b4
JM
7178 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7179 && constructor_max_index
7180 && tree_int_cst_lt (constructor_max_index,
7181 constructor_index))
a1e3b3d9
LB
7182 process_init_element (pop_init_level (1, braced_init_obstack),
7183 true, braced_init_obstack);
472d98b4
JM
7184 else
7185 break;
7186 }
3e4093b6 7187 }
400fbf9f 7188
3e4093b6
RS
7189 /* Unless this is an explicit brace, we need to preserve previous
7190 content if any. */
7191 if (implicit)
7192 {
7193 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7194 || TREE_CODE (constructor_type) == UNION_TYPE)
7195 && constructor_fields)
a1e3b3d9 7196 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7197 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7198 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7199 }
7200
5d038c4c 7201 p = XNEW (struct constructor_stack);
3e4093b6
RS
7202 p->type = constructor_type;
7203 p->fields = constructor_fields;
7204 p->index = constructor_index;
7205 p->max_index = constructor_max_index;
7206 p->unfilled_index = constructor_unfilled_index;
7207 p->unfilled_fields = constructor_unfilled_fields;
7208 p->bit_index = constructor_bit_index;
7209 p->elements = constructor_elements;
7210 p->constant = constructor_constant;
7211 p->simple = constructor_simple;
928c19bb 7212 p->nonconst = constructor_nonconst;
3e4093b6
RS
7213 p->erroneous = constructor_erroneous;
7214 p->pending_elts = constructor_pending_elts;
7215 p->depth = constructor_depth;
916c5919
JM
7216 p->replacement_value.value = 0;
7217 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7218 p->replacement_value.original_type = NULL;
3e4093b6
RS
7219 p->implicit = implicit;
7220 p->outer = 0;
7221 p->incremental = constructor_incremental;
7222 p->designated = constructor_designated;
7223 p->next = constructor_stack;
7224 p->range_stack = 0;
7225 constructor_stack = p;
400fbf9f 7226
3e4093b6
RS
7227 constructor_constant = 1;
7228 constructor_simple = 1;
928c19bb 7229 constructor_nonconst = 0;
3e4093b6 7230 constructor_depth = SPELLING_DEPTH ();
9771b263 7231 constructor_elements = NULL;
3e4093b6
RS
7232 constructor_incremental = 1;
7233 constructor_designated = 0;
7234 constructor_pending_elts = 0;
7235 if (!implicit)
400fbf9f 7236 {
3e4093b6
RS
7237 p->range_stack = constructor_range_stack;
7238 constructor_range_stack = 0;
7239 designator_depth = 0;
b06df647 7240 designator_erroneous = 0;
3e4093b6 7241 }
400fbf9f 7242
3e4093b6
RS
7243 /* Don't die if an entire brace-pair level is superfluous
7244 in the containing level. */
7245 if (constructor_type == 0)
7246 ;
7247 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7248 || TREE_CODE (constructor_type) == UNION_TYPE)
7249 {
7250 /* Don't die if there are extra init elts at the end. */
7251 if (constructor_fields == 0)
7252 constructor_type = 0;
7253 else
400fbf9f 7254 {
3e4093b6
RS
7255 constructor_type = TREE_TYPE (constructor_fields);
7256 push_member_name (constructor_fields);
7257 constructor_depth++;
400fbf9f 7258 }
3e4093b6
RS
7259 }
7260 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7261 {
7262 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7263 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7264 constructor_depth++;
400fbf9f
JW
7265 }
7266
3e4093b6 7267 if (constructor_type == 0)
400fbf9f 7268 {
3e4093b6
RS
7269 error_init ("extra brace group at end of initializer");
7270 constructor_fields = 0;
7271 constructor_unfilled_fields = 0;
7272 return;
400fbf9f
JW
7273 }
7274
3e4093b6
RS
7275 if (value && TREE_CODE (value) == CONSTRUCTOR)
7276 {
7277 constructor_constant = TREE_CONSTANT (value);
7278 constructor_simple = TREE_STATIC (value);
928c19bb 7279 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7280 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7281 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7282 && (TREE_CODE (constructor_type) == RECORD_TYPE
7283 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7284 set_nonincremental_init (braced_init_obstack);
3e4093b6 7285 }
400fbf9f 7286
3e4093b6
RS
7287 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
7288 {
7289 missing_braces_mentioned = 1;
683d6ff9 7290 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
3e4093b6 7291 }
400fbf9f 7292
3e4093b6
RS
7293 if (TREE_CODE (constructor_type) == RECORD_TYPE
7294 || TREE_CODE (constructor_type) == UNION_TYPE)
7295 {
7296 constructor_fields = TYPE_FIELDS (constructor_type);
7297 /* Skip any nameless bit fields at the beginning. */
7298 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7299 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7300 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7301
3e4093b6
RS
7302 constructor_unfilled_fields = constructor_fields;
7303 constructor_bit_index = bitsize_zero_node;
7304 }
7305 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7306 {
7307 /* Vectors are like simple fixed-size arrays. */
7308 constructor_max_index =
c62c040f
RG
7309 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7310 constructor_index = bitsize_int (0);
3e4093b6
RS
7311 constructor_unfilled_index = constructor_index;
7312 }
7313 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7314 {
7315 if (TYPE_DOMAIN (constructor_type))
7316 {
7317 constructor_max_index
7318 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7319
3e4093b6
RS
7320 /* Detect non-empty initializations of zero-length arrays. */
7321 if (constructor_max_index == NULL_TREE
7322 && TYPE_SIZE (constructor_type))
9a9d280e 7323 constructor_max_index = integer_minus_one_node;
de520661 7324
3e4093b6
RS
7325 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7326 to initialize VLAs will cause a proper error; avoid tree
7327 checking errors as well by setting a safe value. */
7328 if (constructor_max_index
7329 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7330 constructor_max_index = integer_minus_one_node;
b62acd60 7331
3e4093b6
RS
7332 constructor_index
7333 = convert (bitsizetype,
7334 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7335 }
7336 else
7337 constructor_index = bitsize_zero_node;
de520661 7338
3e4093b6
RS
7339 constructor_unfilled_index = constructor_index;
7340 if (value && TREE_CODE (value) == STRING_CST)
7341 {
7342 /* We need to split the char/wchar array into individual
7343 characters, so that we don't have to special case it
7344 everywhere. */
a1e3b3d9 7345 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7346 }
7347 }
7348 else
7349 {
b4519d39 7350 if (constructor_type != error_mark_node)
683d6ff9 7351 warning_init (0, "braces around scalar initializer");
3e4093b6
RS
7352 constructor_fields = constructor_type;
7353 constructor_unfilled_fields = constructor_type;
7354 }
7355}
8b6a5902 7356
3e4093b6 7357/* At the end of an implicit or explicit brace level,
916c5919
JM
7358 finish up that level of constructor. If a single expression
7359 with redundant braces initialized that level, return the
7360 c_expr structure for that expression. Otherwise, the original_code
7361 element is set to ERROR_MARK.
7362 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7363 from inner levels (process_init_element ignores that),
916c5919 7364 but return error_mark_node as the value from the outermost level
3e4093b6 7365 (that's what we want to put in DECL_INITIAL).
916c5919 7366 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7367
916c5919 7368struct c_expr
a1e3b3d9 7369pop_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
7370{
7371 struct constructor_stack *p;
916c5919
JM
7372 struct c_expr ret;
7373 ret.value = 0;
7374 ret.original_code = ERROR_MARK;
6866c6e8 7375 ret.original_type = NULL;
de520661 7376
3e4093b6
RS
7377 if (implicit == 0)
7378 {
7379 /* When we come to an explicit close brace,
7380 pop any inner levels that didn't have explicit braces. */
7381 while (constructor_stack->implicit)
a1e3b3d9
LB
7382 {
7383 process_init_element (pop_init_level (1, braced_init_obstack),
7384 true, braced_init_obstack);
7385 }
366de0ce 7386 gcc_assert (!constructor_range_stack);
3e4093b6 7387 }
e5e809f4 7388
0066ef9c
RH
7389 /* Now output all pending elements. */
7390 constructor_incremental = 1;
a1e3b3d9 7391 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7392
3e4093b6 7393 p = constructor_stack;
e5e809f4 7394
3e4093b6
RS
7395 /* Error for initializing a flexible array member, or a zero-length
7396 array member in an inappropriate context. */
7397 if (constructor_type && constructor_fields
7398 && TREE_CODE (constructor_type) == ARRAY_TYPE
7399 && TYPE_DOMAIN (constructor_type)
3f75a254 7400 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7401 {
7402 /* Silently discard empty initializations. The parser will
7403 already have pedwarned for empty brackets. */
7404 if (integer_zerop (constructor_unfilled_index))
7405 constructor_type = NULL_TREE;
366de0ce 7406 else
3e4093b6 7407 {
366de0ce 7408 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7409
3e4093b6
RS
7410 if (constructor_depth > 2)
7411 error_init ("initialization of flexible array member in a nested context");
fcf73884 7412 else
c1771a20 7413 pedwarn_init (input_location, OPT_Wpedantic,
509c9d60 7414 "initialization of a flexible array member");
de520661 7415
3e4093b6
RS
7416 /* We have already issued an error message for the existence
7417 of a flexible array member not at the end of the structure.
535a42b1 7418 Discard the initializer so that we do not die later. */
910ad8de 7419 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7420 constructor_type = NULL_TREE;
7421 }
3e4093b6 7422 }
de520661 7423
3e4093b6 7424 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7425 if (warn_missing_field_initializers
3e4093b6
RS
7426 && constructor_type
7427 && TREE_CODE (constructor_type) == RECORD_TYPE
7428 && constructor_unfilled_fields)
7429 {
49819fef 7430 bool constructor_zeroinit =
9771b263
DN
7431 (vec_safe_length (constructor_elements) == 1
7432 && integer_zerop ((*constructor_elements)[0].value));
49819fef 7433
3e4093b6
RS
7434 /* Do not warn for flexible array members or zero-length arrays. */
7435 while (constructor_unfilled_fields
3f75a254 7436 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7437 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7438 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7439
49819fef
AM
7440 if (constructor_unfilled_fields
7441 /* Do not warn if this level of the initializer uses member
7442 designators; it is likely to be deliberate. */
7443 && !constructor_designated
7444 /* Do not warn about initializing with ` = {0}'. */
7445 && !constructor_zeroinit)
3e4093b6 7446 {
32397f22
MLI
7447 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7448 "missing initializer for field %qD of %qT",
7449 constructor_unfilled_fields,
7450 constructor_type))
7451 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7452 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7453 }
7454 }
de520661 7455
3e4093b6 7456 /* Pad out the end of the structure. */
916c5919 7457 if (p->replacement_value.value)
3e4093b6
RS
7458 /* If this closes a superfluous brace pair,
7459 just pass out the element between them. */
916c5919 7460 ret = p->replacement_value;
3e4093b6
RS
7461 else if (constructor_type == 0)
7462 ;
7463 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7464 && TREE_CODE (constructor_type) != UNION_TYPE
7465 && TREE_CODE (constructor_type) != ARRAY_TYPE
7466 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7467 {
7468 /* A nonincremental scalar initializer--just return
7469 the element, after verifying there is just one. */
9771b263 7470 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7471 {
7472 if (!constructor_erroneous)
7473 error_init ("empty scalar initializer");
916c5919 7474 ret.value = error_mark_node;
3e4093b6 7475 }
9771b263 7476 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6
RS
7477 {
7478 error_init ("extra elements in scalar initializer");
9771b263 7479 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7480 }
7481 else
9771b263 7482 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7483 }
7484 else
7485 {
7486 if (constructor_erroneous)
916c5919 7487 ret.value = error_mark_node;
3e4093b6
RS
7488 else
7489 {
916c5919 7490 ret.value = build_constructor (constructor_type,
4038c495 7491 constructor_elements);
3e4093b6 7492 if (constructor_constant)
51eed280 7493 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7494 if (constructor_constant && constructor_simple)
916c5919 7495 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7496 if (constructor_nonconst)
7497 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7498 }
7499 }
de520661 7500
928c19bb
JM
7501 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7502 {
7503 if (constructor_nonconst)
7504 ret.original_code = C_MAYBE_CONST_EXPR;
7505 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7506 ret.original_code = ERROR_MARK;
7507 }
7508
3e4093b6
RS
7509 constructor_type = p->type;
7510 constructor_fields = p->fields;
7511 constructor_index = p->index;
7512 constructor_max_index = p->max_index;
7513 constructor_unfilled_index = p->unfilled_index;
7514 constructor_unfilled_fields = p->unfilled_fields;
7515 constructor_bit_index = p->bit_index;
7516 constructor_elements = p->elements;
7517 constructor_constant = p->constant;
7518 constructor_simple = p->simple;
928c19bb 7519 constructor_nonconst = p->nonconst;
3e4093b6
RS
7520 constructor_erroneous = p->erroneous;
7521 constructor_incremental = p->incremental;
7522 constructor_designated = p->designated;
7523 constructor_pending_elts = p->pending_elts;
7524 constructor_depth = p->depth;
7525 if (!p->implicit)
7526 constructor_range_stack = p->range_stack;
7527 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7528
3e4093b6
RS
7529 constructor_stack = p->next;
7530 free (p);
b621a4dd 7531
5d5e98dc
VR
7532 if (ret.value == 0 && constructor_stack == 0)
7533 ret.value = error_mark_node;
916c5919 7534 return ret;
3e4093b6 7535}
8b6a5902 7536
3e4093b6
RS
7537/* Common handling for both array range and field name designators.
7538 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7539
3e4093b6 7540static int
a1e3b3d9 7541set_designator (int array, struct obstack * braced_init_obstack)
de520661 7542{
3e4093b6
RS
7543 tree subtype;
7544 enum tree_code subcode;
de520661 7545
3e4093b6
RS
7546 /* Don't die if an entire brace-pair level is superfluous
7547 in the containing level. */
7548 if (constructor_type == 0)
7549 return 1;
de520661 7550
366de0ce
NS
7551 /* If there were errors in this designator list already, bail out
7552 silently. */
b06df647 7553 if (designator_erroneous)
3e4093b6 7554 return 1;
e28cae4f 7555
3e4093b6
RS
7556 if (!designator_depth)
7557 {
366de0ce 7558 gcc_assert (!constructor_range_stack);
de520661 7559
3e4093b6
RS
7560 /* Designator list starts at the level of closest explicit
7561 braces. */
7562 while (constructor_stack->implicit)
a1e3b3d9
LB
7563 {
7564 process_init_element (pop_init_level (1, braced_init_obstack),
7565 true, braced_init_obstack);
7566 }
3e4093b6
RS
7567 constructor_designated = 1;
7568 return 0;
7569 }
de520661 7570
366de0ce 7571 switch (TREE_CODE (constructor_type))
3c3fa147 7572 {
366de0ce
NS
7573 case RECORD_TYPE:
7574 case UNION_TYPE:
3e4093b6
RS
7575 subtype = TREE_TYPE (constructor_fields);
7576 if (subtype != error_mark_node)
7577 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7578 break;
7579 case ARRAY_TYPE:
3e4093b6 7580 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7581 break;
7582 default:
7583 gcc_unreachable ();
de520661 7584 }
400fbf9f 7585
3e4093b6
RS
7586 subcode = TREE_CODE (subtype);
7587 if (array && subcode != ARRAY_TYPE)
7588 {
7589 error_init ("array index in non-array initializer");
7590 return 1;
7591 }
7592 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7593 {
7594 error_init ("field name not in record or union initializer");
7595 return 1;
7596 }
d45cf215 7597
3e4093b6 7598 constructor_designated = 1;
a1e3b3d9 7599 push_init_level (2, braced_init_obstack);
3e4093b6 7600 return 0;
de520661 7601}
400fbf9f 7602
3e4093b6
RS
7603/* If there are range designators in designator list, push a new designator
7604 to constructor_range_stack. RANGE_END is end of such stack range or
7605 NULL_TREE if there is no range designator at this level. */
400fbf9f 7606
3e4093b6 7607static void
a1e3b3d9 7608push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7609{
7610 struct constructor_range_stack *p;
400fbf9f 7611
a1e3b3d9
LB
7612 p = (struct constructor_range_stack *)
7613 obstack_alloc (braced_init_obstack,
7614 sizeof (struct constructor_range_stack));
3e4093b6
RS
7615 p->prev = constructor_range_stack;
7616 p->next = 0;
7617 p->fields = constructor_fields;
7618 p->range_start = constructor_index;
7619 p->index = constructor_index;
7620 p->stack = constructor_stack;
7621 p->range_end = range_end;
8b6a5902 7622 if (constructor_range_stack)
3e4093b6
RS
7623 constructor_range_stack->next = p;
7624 constructor_range_stack = p;
de520661 7625}
400fbf9f 7626
3e4093b6
RS
7627/* Within an array initializer, specify the next index to be initialized.
7628 FIRST is that index. If LAST is nonzero, then initialize a range
7629 of indices, running from FIRST through LAST. */
5a7ec9d9 7630
de520661 7631void
a1e3b3d9
LB
7632set_init_index (tree first, tree last,
7633 struct obstack * braced_init_obstack)
de520661 7634{
a1e3b3d9 7635 if (set_designator (1, braced_init_obstack))
3e4093b6 7636 return;
de520661 7637
b06df647 7638 designator_erroneous = 1;
de520661 7639
3ea8cd06
JM
7640 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7641 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7642 {
7643 error_init ("array index in initializer not of integer type");
7644 return;
7645 }
7646
2b6da65c
JM
7647 if (TREE_CODE (first) != INTEGER_CST)
7648 {
7649 first = c_fully_fold (first, false, NULL);
7650 if (TREE_CODE (first) == INTEGER_CST)
c1771a20 7651 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7652 "array index in initializer is not "
7653 "an integer constant expression");
7654 }
7655
7656 if (last && TREE_CODE (last) != INTEGER_CST)
7657 {
7658 last = c_fully_fold (last, false, NULL);
7659 if (TREE_CODE (last) == INTEGER_CST)
c1771a20 7660 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7661 "array index in initializer is not "
7662 "an integer constant expression");
7663 }
7664
3e4093b6
RS
7665 if (TREE_CODE (first) != INTEGER_CST)
7666 error_init ("nonconstant array index in initializer");
7667 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7668 error_init ("nonconstant array index in initializer");
7669 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7670 error_init ("array index in non-array initializer");
622adc7e
MK
7671 else if (tree_int_cst_sgn (first) == -1)
7672 error_init ("array index in initializer exceeds array bounds");
3e4093b6
RS
7673 else if (constructor_max_index
7674 && tree_int_cst_lt (constructor_max_index, first))
7675 error_init ("array index in initializer exceeds array bounds");
7676 else
de520661 7677 {
928c19bb
JM
7678 constant_expression_warning (first);
7679 if (last)
7680 constant_expression_warning (last);
3e4093b6 7681 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7682 if (tree_int_cst_lt (constructor_index, first))
7683 {
7684 constructor_index = copy_node (constructor_index);
7685 TREE_OVERFLOW (constructor_index) = 1;
7686 }
665f2503 7687
3e4093b6 7688 if (last)
2bede729 7689 {
3e4093b6
RS
7690 if (tree_int_cst_equal (first, last))
7691 last = 0;
7692 else if (tree_int_cst_lt (last, first))
7693 {
7694 error_init ("empty index range in initializer");
7695 last = 0;
7696 }
7697 else
7698 {
7699 last = convert (bitsizetype, last);
7700 if (constructor_max_index != 0
7701 && tree_int_cst_lt (constructor_max_index, last))
7702 {
7703 error_init ("array index range in initializer exceeds array bounds");
7704 last = 0;
7705 }
7706 }
2bede729 7707 }
fed3cef0 7708
3e4093b6 7709 designator_depth++;
b06df647 7710 designator_erroneous = 0;
3e4093b6 7711 if (constructor_range_stack || last)
a1e3b3d9 7712 push_range_stack (last, braced_init_obstack);
de520661 7713 }
de520661 7714}
3e4093b6
RS
7715
7716/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7717
de520661 7718void
a1e3b3d9 7719set_init_label (tree fieldname, struct obstack * braced_init_obstack)
de520661 7720{
0fb96aa4 7721 tree field;
94ba5069 7722
a1e3b3d9 7723 if (set_designator (0, braced_init_obstack))
3e4093b6
RS
7724 return;
7725
b06df647 7726 designator_erroneous = 1;
3e4093b6
RS
7727
7728 if (TREE_CODE (constructor_type) != RECORD_TYPE
7729 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7730 {
3e4093b6
RS
7731 error_init ("field name not in record or union initializer");
7732 return;
94ba5069
RS
7733 }
7734
0fb96aa4 7735 field = lookup_field (constructor_type, fieldname);
8b6a5902 7736
0fb96aa4 7737 if (field == 0)
c51a1ba9 7738 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 7739 else
0fb96aa4
JM
7740 do
7741 {
7742 constructor_fields = TREE_VALUE (field);
7743 designator_depth++;
7744 designator_erroneous = 0;
7745 if (constructor_range_stack)
7746 push_range_stack (NULL_TREE, braced_init_obstack);
7747 field = TREE_CHAIN (field);
7748 if (field)
7749 {
7750 if (set_designator (0, braced_init_obstack))
7751 return;
7752 }
7753 }
7754 while (field != NULL_TREE);
3e4093b6
RS
7755}
7756\f
7757/* Add a new initializer to the tree of pending initializers. PURPOSE
7758 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7759 VALUE is the value of that index or field. If ORIGTYPE is not
7760 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7761
7762 IMPLICIT is true if value comes from pop_init_level (1),
7763 the new initializer has been merged with the existing one
7764 and thus no warnings should be emitted about overriding an
7765 existing initializer. */
de520661 7766
3e4093b6 7767static void
a1e3b3d9
LB
7768add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7769 struct obstack * braced_init_obstack)
3e4093b6
RS
7770{
7771 struct init_node *p, **q, *r;
7772
7773 q = &constructor_pending_elts;
7774 p = 0;
7775
7776 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7777 {
3e4093b6 7778 while (*q != 0)
91fa3c30 7779 {
3e4093b6
RS
7780 p = *q;
7781 if (tree_int_cst_lt (purpose, p->purpose))
7782 q = &p->left;
7783 else if (tree_int_cst_lt (p->purpose, purpose))
7784 q = &p->right;
7785 else
7786 {
b295aee2
JJ
7787 if (!implicit)
7788 {
7789 if (TREE_SIDE_EFFECTS (p->value))
7790 warning_init (0, "initialized field with side-effects overwritten");
7791 else if (warn_override_init)
7792 warning_init (OPT_Woverride_init, "initialized field overwritten");
7793 }
3e4093b6 7794 p->value = value;
bbbbb16a 7795 p->origtype = origtype;
3e4093b6
RS
7796 return;
7797 }
91fa3c30 7798 }
de520661 7799 }
3e4093b6 7800 else
de520661 7801 {
3e4093b6 7802 tree bitpos;
400fbf9f 7803
3e4093b6
RS
7804 bitpos = bit_position (purpose);
7805 while (*q != NULL)
7806 {
7807 p = *q;
7808 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7809 q = &p->left;
7810 else if (p->purpose != purpose)
7811 q = &p->right;
7812 else
7813 {
b295aee2
JJ
7814 if (!implicit)
7815 {
7816 if (TREE_SIDE_EFFECTS (p->value))
7817 warning_init (0, "initialized field with side-effects overwritten");
7818 else if (warn_override_init)
7819 warning_init (OPT_Woverride_init, "initialized field overwritten");
7820 }
3e4093b6 7821 p->value = value;
bbbbb16a 7822 p->origtype = origtype;
3e4093b6
RS
7823 return;
7824 }
7825 }
91fa3c30 7826 }
b71c7f8a 7827
a1e3b3d9
LB
7828 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7829 sizeof (struct init_node));
3e4093b6
RS
7830 r->purpose = purpose;
7831 r->value = value;
bbbbb16a 7832 r->origtype = origtype;
8b6a5902 7833
3e4093b6
RS
7834 *q = r;
7835 r->parent = p;
7836 r->left = 0;
7837 r->right = 0;
7838 r->balance = 0;
b71c7f8a 7839
3e4093b6 7840 while (p)
de520661 7841 {
3e4093b6 7842 struct init_node *s;
665f2503 7843
3e4093b6 7844 if (r == p->left)
2bede729 7845 {
3e4093b6
RS
7846 if (p->balance == 0)
7847 p->balance = -1;
7848 else if (p->balance < 0)
7849 {
7850 if (r->balance < 0)
7851 {
7852 /* L rotation. */
7853 p->left = r->right;
7854 if (p->left)
7855 p->left->parent = p;
7856 r->right = p;
e7b6a0ee 7857
3e4093b6
RS
7858 p->balance = 0;
7859 r->balance = 0;
39bc99c2 7860
3e4093b6
RS
7861 s = p->parent;
7862 p->parent = r;
7863 r->parent = s;
7864 if (s)
7865 {
7866 if (s->left == p)
7867 s->left = r;
7868 else
7869 s->right = r;
7870 }
7871 else
7872 constructor_pending_elts = r;
7873 }
7874 else
7875 {
7876 /* LR rotation. */
7877 struct init_node *t = r->right;
e7b6a0ee 7878
3e4093b6
RS
7879 r->right = t->left;
7880 if (r->right)
7881 r->right->parent = r;
7882 t->left = r;
7883
7884 p->left = t->right;
7885 if (p->left)
7886 p->left->parent = p;
7887 t->right = p;
7888
7889 p->balance = t->balance < 0;
7890 r->balance = -(t->balance > 0);
7891 t->balance = 0;
7892
7893 s = p->parent;
7894 p->parent = t;
7895 r->parent = t;
7896 t->parent = s;
7897 if (s)
7898 {
7899 if (s->left == p)
7900 s->left = t;
7901 else
7902 s->right = t;
7903 }
7904 else
7905 constructor_pending_elts = t;
7906 }
7907 break;
7908 }
7909 else
7910 {
7911 /* p->balance == +1; growth of left side balances the node. */
7912 p->balance = 0;
7913 break;
7914 }
2bede729 7915 }
3e4093b6
RS
7916 else /* r == p->right */
7917 {
7918 if (p->balance == 0)
7919 /* Growth propagation from right side. */
7920 p->balance++;
7921 else if (p->balance > 0)
7922 {
7923 if (r->balance > 0)
7924 {
7925 /* R rotation. */
7926 p->right = r->left;
7927 if (p->right)
7928 p->right->parent = p;
7929 r->left = p;
7930
7931 p->balance = 0;
7932 r->balance = 0;
7933
7934 s = p->parent;
7935 p->parent = r;
7936 r->parent = s;
7937 if (s)
7938 {
7939 if (s->left == p)
7940 s->left = r;
7941 else
7942 s->right = r;
7943 }
7944 else
7945 constructor_pending_elts = r;
7946 }
7947 else /* r->balance == -1 */
7948 {
7949 /* RL rotation */
7950 struct init_node *t = r->left;
7951
7952 r->left = t->right;
7953 if (r->left)
7954 r->left->parent = r;
7955 t->right = r;
7956
7957 p->right = t->left;
7958 if (p->right)
7959 p->right->parent = p;
7960 t->left = p;
7961
7962 r->balance = (t->balance < 0);
7963 p->balance = -(t->balance > 0);
7964 t->balance = 0;
7965
7966 s = p->parent;
7967 p->parent = t;
7968 r->parent = t;
7969 t->parent = s;
7970 if (s)
7971 {
7972 if (s->left == p)
7973 s->left = t;
7974 else
7975 s->right = t;
7976 }
7977 else
7978 constructor_pending_elts = t;
7979 }
7980 break;
7981 }
7982 else
7983 {
7984 /* p->balance == -1; growth of right side balances the node. */
7985 p->balance = 0;
7986 break;
7987 }
7988 }
7989
7990 r = p;
7991 p = p->parent;
7992 }
7993}
7994
7995/* Build AVL tree from a sorted chain. */
7996
7997static void
a1e3b3d9 7998set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 7999{
4038c495
GB
8000 unsigned HOST_WIDE_INT ix;
8001 tree index, value;
3e4093b6
RS
8002
8003 if (TREE_CODE (constructor_type) != RECORD_TYPE
8004 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8005 return;
8006
4038c495 8007 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
a1e3b3d9 8008 {
fdce1719 8009 add_pending_init (index, value, NULL_TREE, true,
a1e3b3d9
LB
8010 braced_init_obstack);
8011 }
9771b263 8012 constructor_elements = NULL;
3e4093b6
RS
8013 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8014 {
8015 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8016 /* Skip any nameless bit fields at the beginning. */
8017 while (constructor_unfilled_fields != 0
8018 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8019 && DECL_NAME (constructor_unfilled_fields) == 0)
8020 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8021
de520661 8022 }
3e4093b6 8023 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8024 {
3e4093b6
RS
8025 if (TYPE_DOMAIN (constructor_type))
8026 constructor_unfilled_index
8027 = convert (bitsizetype,
8028 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8029 else
8030 constructor_unfilled_index = bitsize_zero_node;
de520661 8031 }
3e4093b6 8032 constructor_incremental = 0;
de520661 8033}
400fbf9f 8034
3e4093b6 8035/* Build AVL tree from a string constant. */
de520661 8036
3e4093b6 8037static void
a1e3b3d9
LB
8038set_nonincremental_init_from_string (tree str,
8039 struct obstack * braced_init_obstack)
de520661 8040{
3e4093b6
RS
8041 tree value, purpose, type;
8042 HOST_WIDE_INT val[2];
8043 const char *p, *end;
8044 int byte, wchar_bytes, charwidth, bitpos;
de520661 8045
366de0ce 8046 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8047
c466b2cd 8048 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8049 charwidth = TYPE_PRECISION (char_type_node);
8050 type = TREE_TYPE (constructor_type);
8051 p = TREE_STRING_POINTER (str);
8052 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8053
3e4093b6 8054 for (purpose = bitsize_zero_node;
8824edff
JJ
8055 p < end
8056 && !(constructor_max_index
8057 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8058 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8059 {
3e4093b6 8060 if (wchar_bytes == 1)
ffc5c6a9 8061 {
3e4093b6
RS
8062 val[1] = (unsigned char) *p++;
8063 val[0] = 0;
ffc5c6a9
RH
8064 }
8065 else
3e4093b6
RS
8066 {
8067 val[0] = 0;
8068 val[1] = 0;
8069 for (byte = 0; byte < wchar_bytes; byte++)
8070 {
8071 if (BYTES_BIG_ENDIAN)
8072 bitpos = (wchar_bytes - byte - 1) * charwidth;
8073 else
8074 bitpos = byte * charwidth;
8075 val[bitpos < HOST_BITS_PER_WIDE_INT]
8076 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8077 << (bitpos % HOST_BITS_PER_WIDE_INT);
8078 }
8079 }
584ef5fe 8080
8df83eae 8081 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8082 {
8083 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8084 if (bitpos < HOST_BITS_PER_WIDE_INT)
8085 {
8086 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8087 {
8088 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
8089 val[0] = -1;
8090 }
8091 }
8092 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8093 {
8094 if (val[1] < 0)
c22cacf3 8095 val[0] = -1;
3e4093b6
RS
8096 }
8097 else if (val[0] & (((HOST_WIDE_INT) 1)
8098 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8099 val[0] |= ((HOST_WIDE_INT) -1)
8100 << (bitpos - HOST_BITS_PER_WIDE_INT);
8101 }
ffc5c6a9 8102
7d60be94 8103 value = build_int_cst_wide (type, val[1], val[0]);
fdce1719 8104 add_pending_init (purpose, value, NULL_TREE, true,
a1e3b3d9 8105 braced_init_obstack);
9dfcc8db
BH
8106 }
8107
3e4093b6
RS
8108 constructor_incremental = 0;
8109}
de520661 8110
3e4093b6
RS
8111/* Return value of FIELD in pending initializer or zero if the field was
8112 not initialized yet. */
8113
8114static tree
a1e3b3d9 8115find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8116{
8117 struct init_node *p;
8118
8119 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8120 {
3e4093b6
RS
8121 if (constructor_incremental
8122 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8123 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8124
8125 p = constructor_pending_elts;
8126 while (p)
19d76e60 8127 {
3e4093b6
RS
8128 if (tree_int_cst_lt (field, p->purpose))
8129 p = p->left;
8130 else if (tree_int_cst_lt (p->purpose, field))
8131 p = p->right;
8132 else
8133 return p->value;
19d76e60 8134 }
19d76e60 8135 }
3e4093b6 8136 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8137 {
3e4093b6 8138 tree bitpos = bit_position (field);
de520661 8139
3e4093b6
RS
8140 if (constructor_incremental
8141 && (!constructor_unfilled_fields
8142 || tree_int_cst_lt (bitpos,
8143 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8144 set_nonincremental_init (braced_init_obstack);
de520661 8145
3e4093b6
RS
8146 p = constructor_pending_elts;
8147 while (p)
8148 {
8149 if (field == p->purpose)
8150 return p->value;
8151 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8152 p = p->left;
8153 else
8154 p = p->right;
8155 }
8156 }
8157 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8158 {
9771b263
DN
8159 if (!vec_safe_is_empty (constructor_elements)
8160 && (constructor_elements->last ().index == field))
8161 return constructor_elements->last ().value;
de520661 8162 }
3e4093b6 8163 return 0;
de520661
RS
8164}
8165
3e4093b6
RS
8166/* "Output" the next constructor element.
8167 At top level, really output it to assembler code now.
8168 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8169 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8170 TYPE is the data type that the containing data type wants here.
8171 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8172 If VALUE is a string constant, STRICT_STRING is true if it is
8173 unparenthesized or we should not warn here for it being parenthesized.
8174 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8175
3e4093b6
RS
8176 PENDING if non-nil means output pending elements that belong
8177 right after this element. (PENDING is normally 1;
b295aee2
JJ
8178 it is 0 while outputting pending elements, to avoid recursion.)
8179
8180 IMPLICIT is true if value comes from pop_init_level (1),
8181 the new initializer has been merged with the existing one
8182 and thus no warnings should be emitted about overriding an
8183 existing initializer. */
8b6a5902 8184
3e4093b6 8185static void
bbbbb16a 8186output_init_element (tree value, tree origtype, bool strict_string, tree type,
a1e3b3d9
LB
8187 tree field, int pending, bool implicit,
8188 struct obstack * braced_init_obstack)
3e4093b6 8189{
8ce94e44 8190 tree semantic_type = NULL_TREE;
928c19bb
JM
8191 bool maybe_const = true;
8192 bool npc;
4038c495 8193
0a880880 8194 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8195 {
3e4093b6
RS
8196 constructor_erroneous = 1;
8197 return;
8b6a5902 8198 }
46bdb9cf
JM
8199 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8200 && (TREE_CODE (value) == STRING_CST
8201 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8202 && !(TREE_CODE (value) == STRING_CST
8203 && TREE_CODE (type) == ARRAY_TYPE
8204 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8205 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8206 TYPE_MAIN_VARIANT (type)))
c2255bc4 8207 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8208
3e4093b6
RS
8209 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8210 && require_constant_value && !flag_isoc99 && pending)
8b6a5902 8211 {
3e4093b6
RS
8212 /* As an extension, allow initializing objects with static storage
8213 duration with compound literals (which are then treated just as
8214 the brace enclosed list they contain). */
8215 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8216 value = DECL_INITIAL (decl);
8b6a5902
JJ
8217 }
8218
928c19bb 8219 npc = null_pointer_constant_p (value);
8ce94e44
JM
8220 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8221 {
8222 semantic_type = TREE_TYPE (value);
8223 value = TREE_OPERAND (value, 0);
8224 }
928c19bb
JM
8225 value = c_fully_fold (value, require_constant_value, &maybe_const);
8226
3e4093b6
RS
8227 if (value == error_mark_node)
8228 constructor_erroneous = 1;
8229 else if (!TREE_CONSTANT (value))
8230 constructor_constant = 0;
116df786 8231 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8232 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8233 || TREE_CODE (constructor_type) == UNION_TYPE)
8234 && DECL_C_BIT_FIELD (field)
8235 && TREE_CODE (value) != INTEGER_CST))
8236 constructor_simple = 0;
928c19bb
JM
8237 if (!maybe_const)
8238 constructor_nonconst = 1;
3e4093b6 8239
116df786 8240 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8241 {
116df786
RH
8242 if (require_constant_value)
8243 {
8244 error_init ("initializer element is not constant");
8245 value = error_mark_node;
8246 }
8247 else if (require_constant_elements)
509c9d60
MLI
8248 pedwarn (input_location, 0,
8249 "initializer element is not computable at load time");
8b6a5902 8250 }
928c19bb
JM
8251 else if (!maybe_const
8252 && (require_constant_value || require_constant_elements))
8253 pedwarn_init (input_location, 0,
8254 "initializer element is not a constant expression");
3e4093b6 8255
81f40b79
ILT
8256 /* Issue -Wc++-compat warnings about initializing a bitfield with
8257 enum type. */
8258 if (warn_cxx_compat
8259 && field != NULL_TREE
8260 && TREE_CODE (field) == FIELD_DECL
8261 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8262 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8263 != TYPE_MAIN_VARIANT (type))
8264 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8265 {
8266 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8267 if (checktype != error_mark_node
8268 && (TYPE_MAIN_VARIANT (checktype)
8269 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8270 warning_init (OPT_Wc___compat,
8271 "enum conversion in initialization is invalid in C++");
8272 }
8273
3e4093b6
RS
8274 /* If this field is empty (and not at the end of structure),
8275 don't do anything other than checking the initializer. */
8276 if (field
8277 && (TREE_TYPE (field) == error_mark_node
8278 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8279 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8280 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8281 || DECL_CHAIN (field)))))
3e4093b6
RS
8282 return;
8283
8ce94e44
JM
8284 if (semantic_type)
8285 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
c2255bc4
AH
8286 value = digest_init (input_location, type, value, origtype, npc,
8287 strict_string, require_constant_value);
3e4093b6 8288 if (value == error_mark_node)
8b6a5902 8289 {
3e4093b6
RS
8290 constructor_erroneous = 1;
8291 return;
8b6a5902 8292 }
928c19bb
JM
8293 if (require_constant_value || require_constant_elements)
8294 constant_expression_warning (value);
8b6a5902 8295
3e4093b6
RS
8296 /* If this element doesn't come next in sequence,
8297 put it on constructor_pending_elts. */
8298 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8299 && (!constructor_incremental
8300 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8301 {
3e4093b6
RS
8302 if (constructor_incremental
8303 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8304 set_nonincremental_init (braced_init_obstack);
3e4093b6 8305
a1e3b3d9
LB
8306 add_pending_init (field, value, origtype, implicit,
8307 braced_init_obstack);
3e4093b6 8308 return;
8b6a5902 8309 }
3e4093b6
RS
8310 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8311 && (!constructor_incremental
8312 || field != constructor_unfilled_fields))
8b6a5902 8313 {
3e4093b6
RS
8314 /* We do this for records but not for unions. In a union,
8315 no matter which field is specified, it can be initialized
8316 right away since it starts at the beginning of the union. */
8317 if (constructor_incremental)
8318 {
8319 if (!constructor_unfilled_fields)
a1e3b3d9 8320 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8321 else
8322 {
8323 tree bitpos, unfillpos;
8324
8325 bitpos = bit_position (field);
8326 unfillpos = bit_position (constructor_unfilled_fields);
8327
8328 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8329 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8330 }
8331 }
8332
a1e3b3d9
LB
8333 add_pending_init (field, value, origtype, implicit,
8334 braced_init_obstack);
3e4093b6 8335 return;
8b6a5902 8336 }
3e4093b6 8337 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8338 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8339 {
b295aee2
JJ
8340 if (!implicit)
8341 {
9771b263 8342 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
b295aee2
JJ
8343 warning_init (0,
8344 "initialized field with side-effects overwritten");
8345 else if (warn_override_init)
8346 warning_init (OPT_Woverride_init, "initialized field overwritten");
8347 }
8b6a5902 8348
3e4093b6 8349 /* We can have just one union field set. */
9771b263 8350 constructor_elements = NULL;
3e4093b6 8351 }
8b6a5902 8352
3e4093b6
RS
8353 /* Otherwise, output this element either to
8354 constructor_elements or to the assembler file. */
8b6a5902 8355
f32682ca 8356 constructor_elt celt = {field, value};
9771b263 8357 vec_safe_push (constructor_elements, celt);
8b6a5902 8358
3e4093b6
RS
8359 /* Advance the variable that indicates sequential elements output. */
8360 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8361 constructor_unfilled_index
db3927fb
AH
8362 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8363 bitsize_one_node);
3e4093b6
RS
8364 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8365 {
8366 constructor_unfilled_fields
910ad8de 8367 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8368
3e4093b6
RS
8369 /* Skip any nameless bit fields. */
8370 while (constructor_unfilled_fields != 0
8371 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8372 && DECL_NAME (constructor_unfilled_fields) == 0)
8373 constructor_unfilled_fields =
910ad8de 8374 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8375 }
8376 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8377 constructor_unfilled_fields = 0;
de520661 8378
3e4093b6
RS
8379 /* Now output any pending elements which have become next. */
8380 if (pending)
a1e3b3d9 8381 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8382}
8b6a5902 8383
3e4093b6
RS
8384/* Output any pending elements which have become next.
8385 As we output elements, constructor_unfilled_{fields,index}
8386 advances, which may cause other elements to become next;
8387 if so, they too are output.
8b6a5902 8388
3e4093b6
RS
8389 If ALL is 0, we return when there are
8390 no more pending elements to output now.
665f2503 8391
3e4093b6
RS
8392 If ALL is 1, we output space as necessary so that
8393 we can output all the pending elements. */
3e4093b6 8394static void
a1e3b3d9 8395output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8396{
8397 struct init_node *elt = constructor_pending_elts;
8398 tree next;
de520661 8399
3e4093b6
RS
8400 retry:
8401
ba228239 8402 /* Look through the whole pending tree.
3e4093b6
RS
8403 If we find an element that should be output now,
8404 output it. Otherwise, set NEXT to the element
8405 that comes first among those still pending. */
8406
8407 next = 0;
8408 while (elt)
8409 {
8410 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8411 {
3e4093b6
RS
8412 if (tree_int_cst_equal (elt->purpose,
8413 constructor_unfilled_index))
bbbbb16a 8414 output_init_element (elt->value, elt->origtype, true,
3e4093b6 8415 TREE_TYPE (constructor_type),
a1e3b3d9
LB
8416 constructor_unfilled_index, 0, false,
8417 braced_init_obstack);
3e4093b6
RS
8418 else if (tree_int_cst_lt (constructor_unfilled_index,
8419 elt->purpose))
8b6a5902 8420 {
3e4093b6
RS
8421 /* Advance to the next smaller node. */
8422 if (elt->left)
8423 elt = elt->left;
8424 else
8425 {
8426 /* We have reached the smallest node bigger than the
8427 current unfilled index. Fill the space first. */
8428 next = elt->purpose;
8429 break;
8430 }
8b6a5902 8431 }
ce662d4c
JJ
8432 else
8433 {
3e4093b6
RS
8434 /* Advance to the next bigger node. */
8435 if (elt->right)
8436 elt = elt->right;
8437 else
ce662d4c 8438 {
3e4093b6
RS
8439 /* We have reached the biggest node in a subtree. Find
8440 the parent of it, which is the next bigger node. */
8441 while (elt->parent && elt->parent->right == elt)
8442 elt = elt->parent;
8443 elt = elt->parent;
8444 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8445 elt->purpose))
8446 {
8447 next = elt->purpose;
8448 break;
8449 }
ce662d4c
JJ
8450 }
8451 }
8b6a5902 8452 }
3e4093b6
RS
8453 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8454 || TREE_CODE (constructor_type) == UNION_TYPE)
8455 {
8456 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8457
3e4093b6
RS
8458 /* If the current record is complete we are done. */
8459 if (constructor_unfilled_fields == 0)
8460 break;
de520661 8461
3e4093b6
RS
8462 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8463 elt_bitpos = bit_position (elt->purpose);
8464 /* We can't compare fields here because there might be empty
8465 fields in between. */
8466 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8467 {
8468 constructor_unfilled_fields = elt->purpose;
bbbbb16a
ILT
8469 output_init_element (elt->value, elt->origtype, true,
8470 TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8471 elt->purpose, 0, false,
8472 braced_init_obstack);
3e4093b6
RS
8473 }
8474 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8475 {
8476 /* Advance to the next smaller node. */
8477 if (elt->left)
8478 elt = elt->left;
8479 else
8480 {
8481 /* We have reached the smallest node bigger than the
8482 current unfilled field. Fill the space first. */
8483 next = elt->purpose;
8484 break;
8485 }
8486 }
8487 else
8488 {
8489 /* Advance to the next bigger node. */
8490 if (elt->right)
8491 elt = elt->right;
8492 else
8493 {
8494 /* We have reached the biggest node in a subtree. Find
8495 the parent of it, which is the next bigger node. */
8496 while (elt->parent && elt->parent->right == elt)
8497 elt = elt->parent;
8498 elt = elt->parent;
8499 if (elt
8500 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8501 bit_position (elt->purpose))))
8502 {
8503 next = elt->purpose;
8504 break;
8505 }
8506 }
8507 }
8508 }
8509 }
de520661 8510
3e4093b6
RS
8511 /* Ordinarily return, but not if we want to output all
8512 and there are elements left. */
3f75a254 8513 if (!(all && next != 0))
e5cfb88f
RK
8514 return;
8515
3e4093b6
RS
8516 /* If it's not incremental, just skip over the gap, so that after
8517 jumping to retry we will output the next successive element. */
8518 if (TREE_CODE (constructor_type) == RECORD_TYPE
8519 || TREE_CODE (constructor_type) == UNION_TYPE)
8520 constructor_unfilled_fields = next;
8521 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8522 constructor_unfilled_index = next;
de520661 8523
3e4093b6
RS
8524 /* ELT now points to the node in the pending tree with the next
8525 initializer to output. */
8526 goto retry;
de520661
RS
8527}
8528\f
3e4093b6
RS
8529/* Add one non-braced element to the current constructor level.
8530 This adjusts the current position within the constructor's type.
8531 This may also start or terminate implicit levels
8532 to handle a partly-braced initializer.
e5e809f4 8533
3e4093b6 8534 Once this has found the correct level for the new element,
b295aee2
JJ
8535 it calls output_init_element.
8536
8537 IMPLICIT is true if value comes from pop_init_level (1),
8538 the new initializer has been merged with the existing one
8539 and thus no warnings should be emitted about overriding an
8540 existing initializer. */
3e4093b6
RS
8541
8542void
a1e3b3d9
LB
8543process_init_element (struct c_expr value, bool implicit,
8544 struct obstack * braced_init_obstack)
e5e809f4 8545{
916c5919
JM
8546 tree orig_value = value.value;
8547 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8548 bool strict_string = value.original_code == STRING_CST;
340baef7 8549 bool was_designated = designator_depth != 0;
e5e809f4 8550
3e4093b6 8551 designator_depth = 0;
b06df647 8552 designator_erroneous = 0;
e5e809f4 8553
3e4093b6
RS
8554 /* Handle superfluous braces around string cst as in
8555 char x[] = {"foo"}; */
8556 if (string_flag
8557 && constructor_type
340baef7 8558 && !was_designated
3e4093b6 8559 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8560 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8561 && integer_zerop (constructor_unfilled_index))
e5e809f4 8562 {
916c5919 8563 if (constructor_stack->replacement_value.value)
c22cacf3 8564 error_init ("excess elements in char array initializer");
3e4093b6
RS
8565 constructor_stack->replacement_value = value;
8566 return;
e5e809f4 8567 }
8b6a5902 8568
916c5919 8569 if (constructor_stack->replacement_value.value != 0)
3e4093b6
RS
8570 {
8571 error_init ("excess elements in struct initializer");
8572 return;
e5e809f4
JL
8573 }
8574
3e4093b6
RS
8575 /* Ignore elements of a brace group if it is entirely superfluous
8576 and has already been diagnosed. */
8577 if (constructor_type == 0)
8578 return;
e5e809f4 8579
3e4093b6
RS
8580 /* If we've exhausted any levels that didn't have braces,
8581 pop them now. */
8582 while (constructor_stack->implicit)
8583 {
8584 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8585 || TREE_CODE (constructor_type) == UNION_TYPE)
8586 && constructor_fields == 0)
a1e3b3d9
LB
8587 process_init_element (pop_init_level (1, braced_init_obstack),
8588 true, braced_init_obstack);
53650abe
AP
8589 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8590 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8591 && constructor_max_index
8592 && tree_int_cst_lt (constructor_max_index,
8593 constructor_index))
a1e3b3d9
LB
8594 process_init_element (pop_init_level (1, braced_init_obstack),
8595 true, braced_init_obstack);
3e4093b6
RS
8596 else
8597 break;
8598 }
e5e809f4 8599
3e4093b6
RS
8600 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8601 if (constructor_range_stack)
e5e809f4 8602 {
3e4093b6
RS
8603 /* If value is a compound literal and we'll be just using its
8604 content, don't put it into a SAVE_EXPR. */
916c5919 8605 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
8606 || !require_constant_value
8607 || flag_isoc99)
8ce94e44
JM
8608 {
8609 tree semantic_type = NULL_TREE;
8610 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8611 {
8612 semantic_type = TREE_TYPE (value.value);
8613 value.value = TREE_OPERAND (value.value, 0);
8614 }
8615 value.value = c_save_expr (value.value);
8616 if (semantic_type)
8617 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8618 value.value);
8619 }
3e4093b6 8620 }
e5e809f4 8621
3e4093b6
RS
8622 while (1)
8623 {
8624 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8625 {
3e4093b6
RS
8626 tree fieldtype;
8627 enum tree_code fieldcode;
e5e809f4 8628
3e4093b6
RS
8629 if (constructor_fields == 0)
8630 {
509c9d60
MLI
8631 pedwarn_init (input_location, 0,
8632 "excess elements in struct initializer");
3e4093b6
RS
8633 break;
8634 }
e5e809f4 8635
3e4093b6
RS
8636 fieldtype = TREE_TYPE (constructor_fields);
8637 if (fieldtype != error_mark_node)
8638 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8639 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8640
3e4093b6
RS
8641 /* Error for non-static initialization of a flexible array member. */
8642 if (fieldcode == ARRAY_TYPE
8643 && !require_constant_value
8644 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8645 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6
RS
8646 {
8647 error_init ("non-static initialization of a flexible array member");
8648 break;
8649 }
e5e809f4 8650
3e4093b6 8651 /* Accept a string constant to initialize a subarray. */
916c5919 8652 if (value.value != 0
3e4093b6 8653 && fieldcode == ARRAY_TYPE
197463ae 8654 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8655 && string_flag)
916c5919 8656 value.value = orig_value;
3e4093b6
RS
8657 /* Otherwise, if we have come to a subaggregate,
8658 and we don't have an element of its type, push into it. */
0953878d 8659 else if (value.value != 0
916c5919
JM
8660 && value.value != error_mark_node
8661 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8662 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8663 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8664 {
a1e3b3d9 8665 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8666 continue;
8667 }
e5e809f4 8668
916c5919 8669 if (value.value)
3e4093b6
RS
8670 {
8671 push_member_name (constructor_fields);
bbbbb16a
ILT
8672 output_init_element (value.value, value.original_type,
8673 strict_string, fieldtype,
a1e3b3d9
LB
8674 constructor_fields, 1, implicit,
8675 braced_init_obstack);
3e4093b6 8676 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8677 }
8678 else
3e4093b6
RS
8679 /* Do the bookkeeping for an element that was
8680 directly output as a constructor. */
e5e809f4 8681 {
3e4093b6
RS
8682 /* For a record, keep track of end position of last field. */
8683 if (DECL_SIZE (constructor_fields))
c22cacf3 8684 constructor_bit_index
db3927fb
AH
8685 = size_binop_loc (input_location, PLUS_EXPR,
8686 bit_position (constructor_fields),
8687 DECL_SIZE (constructor_fields));
3e4093b6
RS
8688
8689 /* If the current field was the first one not yet written out,
8690 it isn't now, so update. */
8691 if (constructor_unfilled_fields == constructor_fields)
8692 {
910ad8de 8693 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8694 /* Skip any nameless bit fields. */
8695 while (constructor_unfilled_fields != 0
8696 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8697 && DECL_NAME (constructor_unfilled_fields) == 0)
8698 constructor_unfilled_fields =
910ad8de 8699 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8700 }
e5e809f4 8701 }
3e4093b6 8702
910ad8de 8703 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8704 /* Skip any nameless bit fields at the beginning. */
8705 while (constructor_fields != 0
8706 && DECL_C_BIT_FIELD (constructor_fields)
8707 && DECL_NAME (constructor_fields) == 0)
910ad8de 8708 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8709 }
3e4093b6 8710 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8711 {
3e4093b6
RS
8712 tree fieldtype;
8713 enum tree_code fieldcode;
e5e809f4 8714
3e4093b6
RS
8715 if (constructor_fields == 0)
8716 {
509c9d60
MLI
8717 pedwarn_init (input_location, 0,
8718 "excess elements in union initializer");
3e4093b6
RS
8719 break;
8720 }
e5e809f4 8721
3e4093b6
RS
8722 fieldtype = TREE_TYPE (constructor_fields);
8723 if (fieldtype != error_mark_node)
8724 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8725 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8726
3e4093b6
RS
8727 /* Warn that traditional C rejects initialization of unions.
8728 We skip the warning if the value is zero. This is done
8729 under the assumption that the zero initializer in user
8730 code appears conditioned on e.g. __STDC__ to avoid
8731 "missing initializer" warnings and relies on default
8732 initialization to zero in the traditional C case.
8733 We also skip the warning if the initializer is designated,
8734 again on the assumption that this must be conditional on
8735 __STDC__ anyway (and we've already complained about the
8736 member-designator already). */
8400e75e 8737 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8738 && !(value.value && (integer_zerop (value.value)
8739 || real_zerop (value.value))))
3176a0c2
DD
8740 warning (OPT_Wtraditional, "traditional C rejects initialization "
8741 "of unions");
e5e809f4 8742
3e4093b6 8743 /* Accept a string constant to initialize a subarray. */
916c5919 8744 if (value.value != 0
3e4093b6 8745 && fieldcode == ARRAY_TYPE
197463ae 8746 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8747 && string_flag)
916c5919 8748 value.value = orig_value;
3e4093b6
RS
8749 /* Otherwise, if we have come to a subaggregate,
8750 and we don't have an element of its type, push into it. */
0953878d 8751 else if (value.value != 0
916c5919
JM
8752 && value.value != error_mark_node
8753 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8754 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8755 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8756 {
a1e3b3d9 8757 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8758 continue;
8759 }
e5e809f4 8760
916c5919 8761 if (value.value)
3e4093b6
RS
8762 {
8763 push_member_name (constructor_fields);
bbbbb16a
ILT
8764 output_init_element (value.value, value.original_type,
8765 strict_string, fieldtype,
a1e3b3d9
LB
8766 constructor_fields, 1, implicit,
8767 braced_init_obstack);
3e4093b6 8768 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8769 }
8770 else
3e4093b6
RS
8771 /* Do the bookkeeping for an element that was
8772 directly output as a constructor. */
e5e809f4 8773 {
3e4093b6 8774 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8775 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8776 }
e5e809f4 8777
3e4093b6
RS
8778 constructor_fields = 0;
8779 }
8780 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8781 {
8782 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8783 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8784
3e4093b6 8785 /* Accept a string constant to initialize a subarray. */
916c5919 8786 if (value.value != 0
3e4093b6 8787 && eltcode == ARRAY_TYPE
197463ae 8788 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8789 && string_flag)
916c5919 8790 value.value = orig_value;
3e4093b6
RS
8791 /* Otherwise, if we have come to a subaggregate,
8792 and we don't have an element of its type, push into it. */
0953878d 8793 else if (value.value != 0
916c5919
JM
8794 && value.value != error_mark_node
8795 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 8796 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 8797 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 8798 {
a1e3b3d9 8799 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8800 continue;
8801 }
8b6a5902 8802
3e4093b6
RS
8803 if (constructor_max_index != 0
8804 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8805 || integer_all_onesp (constructor_max_index)))
8806 {
509c9d60
MLI
8807 pedwarn_init (input_location, 0,
8808 "excess elements in array initializer");
3e4093b6
RS
8809 break;
8810 }
8b6a5902 8811
3e4093b6 8812 /* Now output the actual element. */
916c5919 8813 if (value.value)
3e4093b6 8814 {
ae7e9ddd 8815 push_array_bounds (tree_to_uhwi (constructor_index));
bbbbb16a
ILT
8816 output_init_element (value.value, value.original_type,
8817 strict_string, elttype,
a1e3b3d9
LB
8818 constructor_index, 1, implicit,
8819 braced_init_obstack);
3e4093b6
RS
8820 RESTORE_SPELLING_DEPTH (constructor_depth);
8821 }
2f6e4e97 8822
3e4093b6 8823 constructor_index
db3927fb
AH
8824 = size_binop_loc (input_location, PLUS_EXPR,
8825 constructor_index, bitsize_one_node);
8b6a5902 8826
916c5919 8827 if (!value.value)
3e4093b6
RS
8828 /* If we are doing the bookkeeping for an element that was
8829 directly output as a constructor, we must update
8830 constructor_unfilled_index. */
8831 constructor_unfilled_index = constructor_index;
8832 }
8833 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8834 {
8835 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 8836
c22cacf3
MS
8837 /* Do a basic check of initializer size. Note that vectors
8838 always have a fixed size derived from their type. */
3e4093b6
RS
8839 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8840 {
509c9d60
MLI
8841 pedwarn_init (input_location, 0,
8842 "excess elements in vector initializer");
3e4093b6
RS
8843 break;
8844 }
8b6a5902 8845
3e4093b6 8846 /* Now output the actual element. */
916c5919 8847 if (value.value)
53650abe
AP
8848 {
8849 if (TREE_CODE (value.value) == VECTOR_CST)
8850 elttype = TYPE_MAIN_VARIANT (constructor_type);
8851 output_init_element (value.value, value.original_type,
8852 strict_string, elttype,
a1e3b3d9
LB
8853 constructor_index, 1, implicit,
8854 braced_init_obstack);
53650abe 8855 }
8b6a5902 8856
3e4093b6 8857 constructor_index
db3927fb
AH
8858 = size_binop_loc (input_location,
8859 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 8860
916c5919 8861 if (!value.value)
3e4093b6
RS
8862 /* If we are doing the bookkeeping for an element that was
8863 directly output as a constructor, we must update
8864 constructor_unfilled_index. */
8865 constructor_unfilled_index = constructor_index;
8866 }
8b6a5902 8867
3e4093b6
RS
8868 /* Handle the sole element allowed in a braced initializer
8869 for a scalar variable. */
b4519d39
SB
8870 else if (constructor_type != error_mark_node
8871 && constructor_fields == 0)
8b6a5902 8872 {
509c9d60
MLI
8873 pedwarn_init (input_location, 0,
8874 "excess elements in scalar initializer");
3e4093b6 8875 break;
8b6a5902
JJ
8876 }
8877 else
8878 {
916c5919 8879 if (value.value)
bbbbb16a
ILT
8880 output_init_element (value.value, value.original_type,
8881 strict_string, constructor_type,
a1e3b3d9
LB
8882 NULL_TREE, 1, implicit,
8883 braced_init_obstack);
3e4093b6 8884 constructor_fields = 0;
8b6a5902
JJ
8885 }
8886
3e4093b6
RS
8887 /* Handle range initializers either at this level or anywhere higher
8888 in the designator stack. */
8889 if (constructor_range_stack)
8b6a5902 8890 {
3e4093b6
RS
8891 struct constructor_range_stack *p, *range_stack;
8892 int finish = 0;
8893
8894 range_stack = constructor_range_stack;
8895 constructor_range_stack = 0;
8896 while (constructor_stack != range_stack->stack)
8b6a5902 8897 {
366de0ce 8898 gcc_assert (constructor_stack->implicit);
a1e3b3d9
LB
8899 process_init_element (pop_init_level (1,
8900 braced_init_obstack),
8901 true, braced_init_obstack);
8b6a5902 8902 }
3e4093b6
RS
8903 for (p = range_stack;
8904 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8905 p = p->prev)
8b6a5902 8906 {
366de0ce 8907 gcc_assert (constructor_stack->implicit);
a1e3b3d9
LB
8908 process_init_element (pop_init_level (1, braced_init_obstack),
8909 true, braced_init_obstack);
8b6a5902 8910 }
3e4093b6 8911
db3927fb
AH
8912 p->index = size_binop_loc (input_location,
8913 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
8914 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8915 finish = 1;
8916
8917 while (1)
8918 {
8919 constructor_index = p->index;
8920 constructor_fields = p->fields;
8921 if (finish && p->range_end && p->index == p->range_start)
8922 {
8923 finish = 0;
8924 p->prev = 0;
8925 }
8926 p = p->next;
8927 if (!p)
8928 break;
a1e3b3d9 8929 push_init_level (2, braced_init_obstack);
3e4093b6
RS
8930 p->stack = constructor_stack;
8931 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8932 p->index = p->range_start;
8933 }
8934
8935 if (!finish)
8936 constructor_range_stack = range_stack;
8937 continue;
8b6a5902
JJ
8938 }
8939
3e4093b6 8940 break;
8b6a5902
JJ
8941 }
8942
3e4093b6
RS
8943 constructor_range_stack = 0;
8944}
8945\f
9f0e2d86
ZW
8946/* Build a complete asm-statement, whose components are a CV_QUALIFIER
8947 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 8948 an ASM_EXPR node). */
3e4093b6 8949tree
9f0e2d86 8950build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 8951{
6de9cd9a
DN
8952 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8953 ASM_VOLATILE_P (args) = 1;
9f0e2d86 8954 return add_stmt (args);
8b6a5902
JJ
8955}
8956
9f0e2d86
ZW
8957/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8958 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8959 SIMPLE indicates whether there was anything at all after the
8960 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 8961 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 8962tree
c2255bc4 8963build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 8964 tree clobbers, tree labels, bool simple)
e5e809f4 8965{
3e4093b6 8966 tree tail;
9f0e2d86 8967 tree args;
6de9cd9a
DN
8968 int i;
8969 const char *constraint;
74f0c611 8970 const char **oconstraints;
6de9cd9a 8971 bool allows_mem, allows_reg, is_inout;
74f0c611 8972 int ninputs, noutputs;
6de9cd9a
DN
8973
8974 ninputs = list_length (inputs);
8975 noutputs = list_length (outputs);
74f0c611
RH
8976 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8977
1c384bf1 8978 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 8979
6de9cd9a
DN
8980 /* Remove output conversions that change the type but not the mode. */
8981 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 8982 {
3e4093b6 8983 tree output = TREE_VALUE (tail);
74f0c611 8984
eadd3d0d
JJ
8985 output = c_fully_fold (output, false, NULL);
8986
74f0c611
RH
8987 /* ??? Really, this should not be here. Users should be using a
8988 proper lvalue, dammit. But there's a long history of using casts
8989 in the output operands. In cases like longlong.h, this becomes a
8990 primitive form of typechecking -- if the cast can be removed, then
8991 the output operand had a type of the proper width; otherwise we'll
8992 get an error. Gross, but ... */
3e4093b6 8993 STRIP_NOPS (output);
74f0c611 8994
7bd11157 8995 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 8996 output = error_mark_node;
8b6a5902 8997
5544530a
PB
8998 if (output != error_mark_node
8999 && (TREE_READONLY (output)
9000 || TYPE_READONLY (TREE_TYPE (output))
9001 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9002 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9003 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9004 readonly_error (loc, output, lv_asm);
5544530a 9005
6de9cd9a 9006 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9007 oconstraints[i] = constraint;
9008
9009 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9010 &allows_mem, &allows_reg, &is_inout))
9011 {
9012 /* If the operand is going to end up in memory,
9013 mark it addressable. */
9014 if (!allows_reg && !c_mark_addressable (output))
9015 output = error_mark_node;
bae5cddf
JJ
9016 if (!(!allows_reg && allows_mem)
9017 && output != error_mark_node
9018 && VOID_TYPE_P (TREE_TYPE (output)))
9019 {
9020 error_at (loc, "invalid use of void expression");
9021 output = error_mark_node;
9022 }
74f0c611
RH
9023 }
9024 else
c22cacf3 9025 output = error_mark_node;
3e4093b6 9026
74f0c611 9027 TREE_VALUE (tail) = output;
8b6a5902 9028 }
3e4093b6 9029
74f0c611
RH
9030 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9031 {
9032 tree input;
9033
9034 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9035 input = TREE_VALUE (tail);
9036
74f0c611
RH
9037 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9038 oconstraints, &allows_mem, &allows_reg))
9039 {
9040 /* If the operand is going to end up in memory,
9041 mark it addressable. */
b4c33883
AP
9042 if (!allows_reg && allows_mem)
9043 {
eadd3d0d
JJ
9044 input = c_fully_fold (input, false, NULL);
9045
b4c33883
AP
9046 /* Strip the nops as we allow this case. FIXME, this really
9047 should be rejected or made deprecated. */
9048 STRIP_NOPS (input);
9049 if (!c_mark_addressable (input))
9050 input = error_mark_node;
bae5cddf 9051 }
eadd3d0d 9052 else
bae5cddf 9053 {
eadd3d0d
JJ
9054 struct c_expr expr;
9055 memset (&expr, 0, sizeof (expr));
9056 expr.value = input;
267bac10 9057 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9058 input = c_fully_fold (expr.value, false, NULL);
9059
9060 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9061 {
9062 error_at (loc, "invalid use of void expression");
9063 input = error_mark_node;
9064 }
bae5cddf 9065 }
74f0c611
RH
9066 }
9067 else
9068 input = error_mark_node;
9069
9070 TREE_VALUE (tail) = input;
9071 }
3e4093b6 9072
1c384bf1
RH
9073 /* ASMs with labels cannot have outputs. This should have been
9074 enforced by the parser. */
9075 gcc_assert (outputs == NULL || labels == NULL);
9076
9077 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9078
5544530a
PB
9079 /* asm statements without outputs, including simple ones, are treated
9080 as volatile. */
9081 ASM_INPUT_P (args) = simple;
9082 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9083
9f0e2d86 9084 return args;
e5e809f4 9085}
3e4093b6 9086\f
c2255bc4
AH
9087/* Generate a goto statement to LABEL. LOC is the location of the
9088 GOTO. */
506e2710
RH
9089
9090tree
c2255bc4 9091c_finish_goto_label (location_t loc, tree label)
506e2710 9092{
e1b7793c 9093 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9094 if (!decl)
9095 return NULL_TREE;
506e2710 9096 TREE_USED (decl) = 1;
c2255bc4
AH
9097 {
9098 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9099 SET_EXPR_LOCATION (t, loc);
9100 return add_stmt (t);
9101 }
506e2710
RH
9102}
9103
c2255bc4
AH
9104/* Generate a computed goto statement to EXPR. LOC is the location of
9105 the GOTO. */
506e2710
RH
9106
9107tree
c2255bc4 9108c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9109{
c2255bc4 9110 tree t;
c1771a20 9111 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9112 expr = c_fully_fold (expr, false, NULL);
506e2710 9113 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9114 t = build1 (GOTO_EXPR, void_type_node, expr);
9115 SET_EXPR_LOCATION (t, loc);
9116 return add_stmt (t);
506e2710
RH
9117}
9118
5088b058 9119/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4
AH
9120 to return, or a null pointer for `return;' with no value. LOC is
9121 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
9122 is the original type of RETVAL. */
de520661 9123
506e2710 9124tree
c2255bc4 9125c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9126{
0c9b182b
JJ
9127 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9128 bool no_warning = false;
928c19bb 9129 bool npc = false;
36536d79 9130 size_t rank = 0;
3e4093b6
RS
9131
9132 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9133 warning_at (loc, 0,
9134 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9135
36536d79
BI
9136 if (flag_enable_cilkplus && contains_array_notation_expr (retval))
9137 {
9138 /* Array notations are allowed in a return statement if it is inside a
9139 built-in array notation reduction function. */
9140 if (!find_rank (loc, retval, retval, false, &rank))
9141 return error_mark_node;
9142 if (rank >= 1)
9143 {
9144 error_at (loc, "array notation expression cannot be used as a "
9145 "return value");
9146 return error_mark_node;
9147 }
9148 }
939b37da
BI
9149 if (flag_enable_cilkplus && retval && TREE_CODE (retval) == CILK_SPAWN_STMT)
9150 {
9151 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9152 "allowed");
9153 return error_mark_node;
9154 }
928c19bb
JM
9155 if (retval)
9156 {
8ce94e44 9157 tree semantic_type = NULL_TREE;
928c19bb 9158 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9159 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9160 {
9161 semantic_type = TREE_TYPE (retval);
9162 retval = TREE_OPERAND (retval, 0);
9163 }
928c19bb 9164 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9165 if (semantic_type)
9166 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9167 }
9168
3e4093b6 9169 if (!retval)
de520661 9170 {
3e4093b6
RS
9171 current_function_returns_null = 1;
9172 if ((warn_return_type || flag_isoc99)
9173 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9174 {
b8698a0f 9175 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
fcf73884 9176 "%<return%> with no value, in "
0c9b182b
JJ
9177 "function returning non-void");
9178 no_warning = true;
9179 }
400fbf9f 9180 }
3e4093b6 9181 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9182 {
3e4093b6 9183 current_function_returns_null = 1;
2397c575 9184 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9185 pedwarn (loc, 0,
509c9d60 9186 "%<return%> with a value, in function returning void");
b8698a0f 9187 else
c1771a20 9188 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9189 "%<return%> with expression, in function returning void");
de520661 9190 }
3e4093b6 9191 else
de520661 9192 {
c2255bc4
AH
9193 tree t = convert_for_assignment (loc, valtype, retval, origtype,
9194 ic_return,
9195 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9196 tree res = DECL_RESULT (current_function_decl);
9197 tree inner;
9feb29df 9198 bool save;
3e4093b6
RS
9199
9200 current_function_returns_value = 1;
9201 if (t == error_mark_node)
506e2710 9202 return NULL_TREE;
3e4093b6 9203
9feb29df
JJ
9204 save = in_late_binary_op;
9205 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9206 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9207 in_late_binary_op = true;
3e4093b6 9208 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9209 in_late_binary_op = save;
3e4093b6
RS
9210
9211 /* Strip any conversions, additions, and subtractions, and see if
9212 we are returning the address of a local variable. Warn if so. */
9213 while (1)
8b6a5902 9214 {
3e4093b6 9215 switch (TREE_CODE (inner))
8b6a5902 9216 {
849421a3
JJ
9217 CASE_CONVERT:
9218 case NON_LVALUE_EXPR:
3e4093b6 9219 case PLUS_EXPR:
849421a3 9220 case POINTER_PLUS_EXPR:
3e4093b6
RS
9221 inner = TREE_OPERAND (inner, 0);
9222 continue;
9223
9224 case MINUS_EXPR:
9225 /* If the second operand of the MINUS_EXPR has a pointer
9226 type (or is converted from it), this may be valid, so
9227 don't give a warning. */
9228 {
9229 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9230
3f75a254 9231 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9232 && (CONVERT_EXPR_P (op1)
9233 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9234 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9235
3e4093b6
RS
9236 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9237 break;
8b6a5902 9238
3e4093b6
RS
9239 inner = TREE_OPERAND (inner, 0);
9240 continue;
9241 }
400fbf9f 9242
3e4093b6
RS
9243 case ADDR_EXPR:
9244 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9245
6615c446 9246 while (REFERENCE_CLASS_P (inner)
c22cacf3 9247 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 9248 inner = TREE_OPERAND (inner, 0);
8b6a5902 9249
a2f1f4c3 9250 if (DECL_P (inner)
3f75a254
JM
9251 && !DECL_EXTERNAL (inner)
9252 && !TREE_STATIC (inner)
3e4093b6 9253 && DECL_CONTEXT (inner) == current_function_decl)
c2255bc4 9254 warning_at (loc,
880661a4
JW
9255 OPT_Wreturn_local_addr, "function returns address "
9256 "of local variable");
3e4093b6 9257 break;
8b6a5902 9258
3e4093b6
RS
9259 default:
9260 break;
9261 }
de520661 9262
3e4093b6
RS
9263 break;
9264 }
9265
53fb4de3 9266 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9267 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9268
9269 if (warn_sequence_point)
9270 verify_sequence_points (retval);
de520661 9271 }
8b6a5902 9272
c2255bc4 9273 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9274 TREE_NO_WARNING (ret_stmt) |= no_warning;
9275 return add_stmt (ret_stmt);
de520661 9276}
3e4093b6
RS
9277\f
9278struct c_switch {
604f5adf
ILT
9279 /* The SWITCH_EXPR being built. */
9280 tree switch_expr;
a6c0a76c 9281
89dbed81 9282 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9283 default conversion is applied. */
9284 tree orig_type;
9285
3e4093b6
RS
9286 /* A splay-tree mapping the low element of a case range to the high
9287 element, or NULL_TREE if there is no high element. Used to
9288 determine whether or not a new case label duplicates an old case
9289 label. We need a tree, rather than simply a hash table, because
9290 of the GNU case range extension. */
9291 splay_tree cases;
a6c0a76c 9292
e1b7793c
ILT
9293 /* The bindings at the point of the switch. This is used for
9294 warnings crossing decls when branching to a case label. */
9295 struct c_spot_bindings *bindings;
187230a7 9296
3e4093b6
RS
9297 /* The next node on the stack. */
9298 struct c_switch *next;
9299};
400fbf9f 9300
3e4093b6
RS
9301/* A stack of the currently active switch statements. The innermost
9302 switch statement is on the top of the stack. There is no need to
9303 mark the stack for garbage collection because it is only active
9304 during the processing of the body of a function, and we never
9305 collect at that point. */
de520661 9306
506e2710 9307struct c_switch *c_switch_stack;
de520661 9308
3e4093b6 9309/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4
AH
9310 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9311 SWITCH_COND_LOC is the location of the switch's condition. */
de520661 9312
3e4093b6 9313tree
c2255bc4
AH
9314c_start_case (location_t switch_loc,
9315 location_t switch_cond_loc,
9316 tree exp)
de520661 9317{
c58e8676 9318 tree orig_type = error_mark_node;
3e4093b6 9319 struct c_switch *cs;
2f6e4e97 9320
3e4093b6 9321 if (exp != error_mark_node)
de520661 9322 {
3e4093b6
RS
9323 orig_type = TREE_TYPE (exp);
9324
c58e8676 9325 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9326 {
c58e8676
VR
9327 if (orig_type != error_mark_node)
9328 {
c2255bc4 9329 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9330 orig_type = error_mark_node;
9331 }
3e4093b6 9332 exp = integer_zero_node;
de520661 9333 }
3e4093b6 9334 else
de520661 9335 {
c58e8676 9336 tree type = TYPE_MAIN_VARIANT (orig_type);
8b6a5902 9337
8400e75e 9338 if (!in_system_header_at (input_location)
3e4093b6
RS
9339 && (type == long_integer_type_node
9340 || type == long_unsigned_type_node))
c2255bc4
AH
9341 warning_at (switch_cond_loc,
9342 OPT_Wtraditional, "%<long%> switch expression not "
9343 "converted to %<int%> in ISO C");
8b6a5902 9344
928c19bb 9345 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9346 exp = default_conversion (exp);
ca085fd7
MLI
9347
9348 if (warn_sequence_point)
9349 verify_sequence_points (exp);
3e4093b6
RS
9350 }
9351 }
9352
604f5adf 9353 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9354 cs = XNEW (struct c_switch);
604f5adf 9355 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9356 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9357 cs->orig_type = orig_type;
3e4093b6 9358 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9359 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9360 cs->next = c_switch_stack;
9361 c_switch_stack = cs;
3e4093b6 9362
604f5adf 9363 return add_stmt (cs->switch_expr);
3e4093b6
RS
9364}
9365
c2255bc4 9366/* Process a case label at location LOC. */
3e4093b6
RS
9367
9368tree
c2255bc4 9369do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9370{
9371 tree label = NULL_TREE;
9372
17cede2e
JM
9373 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9374 {
9375 low_value = c_fully_fold (low_value, false, NULL);
9376 if (TREE_CODE (low_value) == INTEGER_CST)
c1771a20 9377 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9378 "case label is not an integer constant expression");
9379 }
9380
9381 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9382 {
9383 high_value = c_fully_fold (high_value, false, NULL);
9384 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9385 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9386 "case label is not an integer constant expression");
9387 }
9388
e1b7793c 9389 if (c_switch_stack == NULL)
187230a7
JM
9390 {
9391 if (low_value)
e1b7793c 9392 error_at (loc, "case label not within a switch statement");
187230a7 9393 else
e1b7793c
ILT
9394 error_at (loc, "%<default%> label not within a switch statement");
9395 return NULL_TREE;
187230a7 9396 }
de520661 9397
e1b7793c
ILT
9398 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9399 EXPR_LOCATION (c_switch_stack->switch_expr),
9400 loc))
9401 return NULL_TREE;
9402
9403 label = c_add_case_label (loc, c_switch_stack->cases,
9404 SWITCH_COND (c_switch_stack->switch_expr),
9405 c_switch_stack->orig_type,
9406 low_value, high_value);
9407 if (label == error_mark_node)
9408 label = NULL_TREE;
3e4093b6
RS
9409 return label;
9410}
de520661 9411
3e4093b6 9412/* Finish the switch statement. */
de520661 9413
3e4093b6 9414void
325c3691 9415c_finish_case (tree body)
3e4093b6 9416{
506e2710 9417 struct c_switch *cs = c_switch_stack;
fbc315db 9418 location_t switch_location;
3e4093b6 9419
604f5adf 9420 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9421
6de9cd9a 9422 /* Emit warnings as needed. */
c2255bc4 9423 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db
ILT
9424 c_do_switch_warnings (cs->cases, switch_location,
9425 TREE_TYPE (cs->switch_expr),
9426 SWITCH_COND (cs->switch_expr));
6de9cd9a 9427
3e4093b6 9428 /* Pop the stack. */
506e2710 9429 c_switch_stack = cs->next;
3e4093b6 9430 splay_tree_delete (cs->cases);
e1b7793c 9431 c_release_switch_bindings (cs->bindings);
5d038c4c 9432 XDELETE (cs);
de520661 9433}
325c3691 9434\f
506e2710
RH
9435/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9436 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9437 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9438 statement, and was not surrounded with parenthesis. */
325c3691 9439
9e51cf9d 9440void
506e2710
RH
9441c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9442 tree else_block, bool nested_if)
325c3691 9443{
506e2710 9444 tree stmt;
325c3691 9445
25c22937
BI
9446 /* If the condition has array notations, then the rank of the then_block and
9447 else_block must be either 0 or be equal to the rank of the condition. If
9448 the condition does not have array notations then break them up as it is
9449 broken up in a normal expression. */
9450 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
9451 {
9452 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9453 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9454 return;
9455 if (then_block
9456 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9457 return;
9458 if (else_block
9459 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9460 return;
9461 if (cond_rank != then_rank && then_rank != 0)
9462 {
9463 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9464 " and the then-block");
9465 return;
9466 }
9467 else if (cond_rank != else_rank && else_rank != 0)
9468 {
9469 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9470 " and the else-block");
9471 return;
9472 }
9473 }
506e2710
RH
9474 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9475 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9476 {
506e2710 9477 tree inner_if = then_block;
16865eaa 9478
61ada8ae 9479 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9480 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9481 it might not be exactly THEN_BLOCK, but should be the last
9482 non-container statement within. */
9483 while (1)
9484 switch (TREE_CODE (inner_if))
9485 {
9486 case COND_EXPR:
9487 goto found;
9488 case BIND_EXPR:
9489 inner_if = BIND_EXPR_BODY (inner_if);
9490 break;
9491 case STATEMENT_LIST:
9492 inner_if = expr_last (then_block);
9493 break;
9494 case TRY_FINALLY_EXPR:
9495 case TRY_CATCH_EXPR:
9496 inner_if = TREE_OPERAND (inner_if, 0);
9497 break;
9498 default:
366de0ce 9499 gcc_unreachable ();
506e2710
RH
9500 }
9501 found:
16865eaa 9502
506e2710 9503 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9504 warning_at (if_locus, OPT_Wparentheses,
9505 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9506 }
16865eaa 9507
2214de30 9508 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9509 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9510 add_stmt (stmt);
325c3691
RH
9511}
9512
506e2710
RH
9513/* Emit a general-purpose loop construct. START_LOCUS is the location of
9514 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9515 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9516 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9517 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9518
9519void
506e2710
RH
9520c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9521 tree blab, tree clab, bool cond_is_first)
325c3691 9522{
506e2710
RH
9523 tree entry = NULL, exit = NULL, t;
9524
36536d79
BI
9525 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
9526 {
9527 error_at (start_locus, "array notation expression cannot be used in a "
9528 "loop%'s condition");
9529 return;
9530 }
9531
28af952a
RS
9532 /* If the condition is zero don't generate a loop construct. */
9533 if (cond && integer_zerop (cond))
9534 {
9535 if (cond_is_first)
9536 {
9537 t = build_and_jump (&blab);
9538 SET_EXPR_LOCATION (t, start_locus);
9539 add_stmt (t);
9540 }
9541 }
9542 else
506e2710
RH
9543 {
9544 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9545
506e2710 9546 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9547 out of the loop, or to the top of it. If there's no exit condition,
9548 then we just build a jump back to the top. */
506e2710 9549 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9550
28af952a 9551 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9552 {
9553 /* Canonicalize the loop condition to the end. This means
9554 generating a branch to the loop condition. Reuse the
9555 continue label, if possible. */
9556 if (cond_is_first)
9557 {
9558 if (incr || !clab)
9559 {
9560 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9561 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9562 }
9563 else
9564 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9565 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9566 add_stmt (t);
9567 }
9568
506e2710 9569 t = build_and_jump (&blab);
506e2710 9570 if (cond_is_first)
db3927fb
AH
9571 exit = fold_build3_loc (start_locus,
9572 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9573 else
db3927fb
AH
9574 exit = fold_build3_loc (input_location,
9575 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9576 }
9577
506e2710
RH
9578 add_stmt (top);
9579 }
c22cacf3 9580
506e2710
RH
9581 if (body)
9582 add_stmt (body);
9583 if (clab)
9584 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9585 if (incr)
9586 add_stmt (incr);
9587 if (entry)
9588 add_stmt (entry);
9589 if (exit)
9590 add_stmt (exit);
9591 if (blab)
9592 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9593}
325c3691
RH
9594
9595tree
c2255bc4 9596c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9597{
089efaa4 9598 bool skip;
506e2710 9599 tree label = *label_p;
325c3691 9600
089efaa4
ILT
9601 /* In switch statements break is sometimes stylistically used after
9602 a return statement. This can lead to spurious warnings about
9603 control reaching the end of a non-void function when it is
9604 inlined. Note that we are calling block_may_fallthru with
9605 language specific tree nodes; this works because
9606 block_may_fallthru returns true when given something it does not
9607 understand. */
9608 skip = !block_may_fallthru (cur_stmt_list);
9609
506e2710 9610 if (!label)
089efaa4
ILT
9611 {
9612 if (!skip)
c2255bc4 9613 *label_p = label = create_artificial_label (loc);
089efaa4 9614 }
953ff289
DN
9615 else if (TREE_CODE (label) == LABEL_DECL)
9616 ;
9617 else switch (TREE_INT_CST_LOW (label))
506e2710 9618 {
953ff289 9619 case 0:
506e2710 9620 if (is_break)
c2255bc4 9621 error_at (loc, "break statement not within loop or switch");
506e2710 9622 else
c2255bc4 9623 error_at (loc, "continue statement not within a loop");
506e2710 9624 return NULL_TREE;
953ff289
DN
9625
9626 case 1:
9627 gcc_assert (is_break);
c2255bc4 9628 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9629 return NULL_TREE;
9630
c02065fc
AH
9631 case 2:
9632 if (is_break)
9633 error ("break statement within %<#pragma simd%> loop body");
9634 else
9635 error ("continue statement within %<#pragma simd%> loop body");
9636 return NULL_TREE;
9637
953ff289
DN
9638 default:
9639 gcc_unreachable ();
506e2710 9640 }
325c3691 9641
089efaa4
ILT
9642 if (skip)
9643 return NULL_TREE;
9644
2e28e797
JH
9645 if (!is_break)
9646 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9647
53fb4de3 9648 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9649}
9650
506e2710 9651/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9652
9653static void
c2255bc4 9654emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9655{
e6b5a630
RH
9656 if (expr == error_mark_node)
9657 ;
9658 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9659 {
9660 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9661 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9662 }
789eadcd
MP
9663 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9664 {
9665 tree r = expr;
9666 location_t cloc = loc;
9667 while (TREE_CODE (r) == COMPOUND_EXPR)
9668 {
9669 if (EXPR_HAS_LOCATION (r))
9670 cloc = EXPR_LOCATION (r);
9671 r = TREE_OPERAND (r, 1);
9672 }
9673 if (!TREE_SIDE_EFFECTS (r)
9674 && !VOID_TYPE_P (TREE_TYPE (r))
9675 && !CONVERT_EXPR_P (r)
9676 && !TREE_NO_WARNING (expr))
9677 warning_at (cloc, OPT_Wunused_value,
9678 "right-hand operand of comma expression has no effect");
9679 }
27f33b15 9680 else
c2255bc4 9681 warn_if_unused_value (expr, loc);
3a5b9284
RH
9682}
9683
506e2710 9684/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9685 diagnostics, but do not call ADD_STMT. LOC is the location of the
9686 statement. */
3a5b9284 9687
506e2710 9688tree
c2255bc4 9689c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9690{
056928b2
JJ
9691 tree exprv;
9692
3a5b9284 9693 if (!expr)
506e2710 9694 return NULL_TREE;
3a5b9284 9695
928c19bb
JM
9696 expr = c_fully_fold (expr, false, NULL);
9697
3a5b9284
RH
9698 if (warn_sequence_point)
9699 verify_sequence_points (expr);
9700
9701 if (TREE_TYPE (expr) != error_mark_node
9702 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9703 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9704 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9705
9706 /* If we're not processing a statement expression, warn about unused values.
9707 Warnings for statement expressions will be emitted later, once we figure
9708 out which is the result. */
9709 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9710 && warn_unused_value)
c2255bc4 9711 emit_side_effect_warnings (loc, expr);
3a5b9284 9712
056928b2
JJ
9713 exprv = expr;
9714 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9715 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9716 while (CONVERT_EXPR_P (exprv))
9717 exprv = TREE_OPERAND (exprv, 0);
9718 if (DECL_P (exprv)
9719 || handled_component_p (exprv)
9720 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9721 mark_exp_read (exprv);
fa8351f8 9722
3a5b9284
RH
9723 /* If the expression is not of a type to which we cannot assign a line
9724 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9725 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9726 {
9727 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9728 SET_EXPR_LOCATION (expr, loc);
9729 }
506e2710
RH
9730
9731 return expr;
9732}
9733
c2255bc4
AH
9734/* Emit an expression as a statement. LOC is the location of the
9735 expression. */
506e2710
RH
9736
9737tree
c2255bc4 9738c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9739{
9740 if (expr)
c2255bc4 9741 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9742 else
9743 return NULL;
3a5b9284
RH
9744}
9745
9746/* Do the opposite and emit a statement as an expression. To begin,
9747 create a new binding level and return it. */
325c3691
RH
9748
9749tree
9750c_begin_stmt_expr (void)
9751{
9752 tree ret;
9753
9754 /* We must force a BLOCK for this level so that, if it is not expanded
9755 later, there is a way to turn off the entire subtree of blocks that
9756 are contained in it. */
9757 keep_next_level ();
9758 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
9759
9760 c_bindings_start_stmt_expr (c_switch_stack == NULL
9761 ? NULL
9762 : c_switch_stack->bindings);
325c3691
RH
9763
9764 /* Mark the current statement list as belonging to a statement list. */
9765 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9766
9767 return ret;
9768}
9769
c2255bc4
AH
9770/* LOC is the location of the compound statement to which this body
9771 belongs. */
9772
325c3691 9773tree
c2255bc4 9774c_finish_stmt_expr (location_t loc, tree body)
325c3691 9775{
3a5b9284 9776 tree last, type, tmp, val;
325c3691
RH
9777 tree *last_p;
9778
c2255bc4 9779 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
9780
9781 c_bindings_end_stmt_expr (c_switch_stack == NULL
9782 ? NULL
9783 : c_switch_stack->bindings);
325c3691 9784
3a5b9284
RH
9785 /* Locate the last statement in BODY. See c_end_compound_stmt
9786 about always returning a BIND_EXPR. */
9787 last_p = &BIND_EXPR_BODY (body);
9788 last = BIND_EXPR_BODY (body);
9789
9790 continue_searching:
325c3691
RH
9791 if (TREE_CODE (last) == STATEMENT_LIST)
9792 {
3a5b9284
RH
9793 tree_stmt_iterator i;
9794
9795 /* This can happen with degenerate cases like ({ }). No value. */
9796 if (!TREE_SIDE_EFFECTS (last))
9797 return body;
9798
9799 /* If we're supposed to generate side effects warnings, process
9800 all of the statements except the last. */
27f33b15 9801 if (warn_unused_value)
325c3691 9802 {
3a5b9284 9803 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
9804 {
9805 location_t tloc;
9806 tree t = tsi_stmt (i);
9807
9808 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9809 emit_side_effect_warnings (tloc, t);
9810 }
325c3691
RH
9811 }
9812 else
3a5b9284
RH
9813 i = tsi_last (last);
9814 last_p = tsi_stmt_ptr (i);
9815 last = *last_p;
325c3691
RH
9816 }
9817
3a5b9284
RH
9818 /* If the end of the list is exception related, then the list was split
9819 by a call to push_cleanup. Continue searching. */
9820 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9821 || TREE_CODE (last) == TRY_CATCH_EXPR)
9822 {
9823 last_p = &TREE_OPERAND (last, 0);
9824 last = *last_p;
9825 goto continue_searching;
9826 }
9827
26d8af35
JM
9828 if (last == error_mark_node)
9829 return last;
9830
3a5b9284
RH
9831 /* In the case that the BIND_EXPR is not necessary, return the
9832 expression out from inside it. */
26d8af35
JM
9833 if (last == BIND_EXPR_BODY (body)
9834 && BIND_EXPR_VARS (body) == NULL)
591baeb0 9835 {
928c19bb
JM
9836 /* Even if this looks constant, do not allow it in a constant
9837 expression. */
e5a94231 9838 last = c_wrap_maybe_const (last, true);
591baeb0
JM
9839 /* Do not warn if the return value of a statement expression is
9840 unused. */
928c19bb 9841 TREE_NO_WARNING (last) = 1;
591baeb0
JM
9842 return last;
9843 }
325c3691
RH
9844
9845 /* Extract the type of said expression. */
9846 type = TREE_TYPE (last);
325c3691 9847
3a5b9284
RH
9848 /* If we're not returning a value at all, then the BIND_EXPR that
9849 we already have is a fine expression to return. */
9850 if (!type || VOID_TYPE_P (type))
9851 return body;
9852
9853 /* Now that we've located the expression containing the value, it seems
9854 silly to make voidify_wrapper_expr repeat the process. Create a
9855 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9856 tmp = create_tmp_var_raw (type, NULL);
9857
9858 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9859 tree_expr_nonnegative_p giving up immediately. */
9860 val = last;
9861 if (TREE_CODE (val) == NOP_EXPR
9862 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9863 val = TREE_OPERAND (val, 0);
9864
53fb4de3 9865 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 9866 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 9867
c2255bc4
AH
9868 {
9869 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9870 SET_EXPR_LOCATION (t, loc);
9871 return t;
9872 }
325c3691
RH
9873}
9874\f
9875/* Begin and end compound statements. This is as simple as pushing
9876 and popping new statement lists from the tree. */
9877
9878tree
9879c_begin_compound_stmt (bool do_scope)
9880{
9881 tree stmt = push_stmt_list ();
9882 if (do_scope)
4dfa0342 9883 push_scope ();
325c3691
RH
9884 return stmt;
9885}
9886
c2255bc4
AH
9887/* End a compound statement. STMT is the statement. LOC is the
9888 location of the compound statement-- this is usually the location
9889 of the opening brace. */
9890
325c3691 9891tree
c2255bc4 9892c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
9893{
9894 tree block = NULL;
9895
9896 if (do_scope)
9897 {
9898 if (c_dialect_objc ())
9899 objc_clear_super_receiver ();
9900 block = pop_scope ();
9901 }
9902
9903 stmt = pop_stmt_list (stmt);
c2255bc4 9904 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
9905
9906 /* If this compound statement is nested immediately inside a statement
9907 expression, then force a BIND_EXPR to be created. Otherwise we'll
9908 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9909 STATEMENT_LISTs merge, and thus we can lose track of what statement
9910 was really last. */
38e01f9e 9911 if (building_stmt_list_p ()
325c3691
RH
9912 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9913 && TREE_CODE (stmt) != BIND_EXPR)
9914 {
53fb4de3 9915 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 9916 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 9917 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
9918 }
9919
9920 return stmt;
9921}
5a508662
RH
9922
9923/* Queue a cleanup. CLEANUP is an expression/statement to be executed
9924 when the current scope is exited. EH_ONLY is true when this is not
9925 meant to apply to normal control flow transfer. */
9926
9927void
c2255bc4 9928push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 9929{
3a5b9284
RH
9930 enum tree_code code;
9931 tree stmt, list;
9932 bool stmt_expr;
9933
9934 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 9935 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 9936 add_stmt (stmt);
3a5b9284
RH
9937 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9938 list = push_stmt_list ();
9939 TREE_OPERAND (stmt, 0) = list;
9940 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 9941}
325c3691 9942\f
3e4093b6
RS
9943/* Build a binary-operation expression without default conversions.
9944 CODE is the kind of expression to build.
ba47d38d 9945 LOCATION is the operator's location.
3e4093b6
RS
9946 This function differs from `build' in several ways:
9947 the data type of the result is computed and recorded in it,
9948 warnings are generated if arg data types are invalid,
9949 special handling for addition and subtraction of pointers is known,
9950 and some optimization is done (operations on narrow ints
9951 are done in the narrower type when that gives the same result).
9952 Constant folding is also done before the result is returned.
de520661 9953
3e4093b6
RS
9954 Note that the operands will never have enumeral types, or function
9955 or array types, because either they will have the default conversions
9956 performed or they have both just been converted to some other type in which
9957 the arithmetic is to be done. */
9958
9959tree
ba47d38d
AH
9960build_binary_op (location_t location, enum tree_code code,
9961 tree orig_op0, tree orig_op1, int convert_p)
de520661 9962{
8ce94e44
JM
9963 tree type0, type1, orig_type0, orig_type1;
9964 tree eptype;
3e4093b6
RS
9965 enum tree_code code0, code1;
9966 tree op0, op1;
c9f9eb5d 9967 tree ret = error_mark_node;
4de67c26 9968 const char *invalid_op_diag;
4d84fe7c 9969 bool op0_int_operands, op1_int_operands;
928c19bb 9970 bool int_const, int_const_or_overflow, int_operands;
b62acd60 9971
3e4093b6
RS
9972 /* Expression code to give to the expression when it is built.
9973 Normally this is CODE, which is what the caller asked for,
9974 but in some special cases we change it. */
9975 enum tree_code resultcode = code;
8b6a5902 9976
3e4093b6
RS
9977 /* Data type in which the computation is to be performed.
9978 In the simplest cases this is the common type of the arguments. */
9979 tree result_type = NULL;
9980
8ce94e44
JM
9981 /* When the computation is in excess precision, the type of the
9982 final EXCESS_PRECISION_EXPR. */
2d2e923f 9983 tree semantic_result_type = NULL;
8ce94e44 9984
3e4093b6
RS
9985 /* Nonzero means operands have already been type-converted
9986 in whatever way is necessary.
9987 Zero means they need to be converted to RESULT_TYPE. */
9988 int converted = 0;
9989
9990 /* Nonzero means create the expression with this type, rather than
9991 RESULT_TYPE. */
9992 tree build_type = 0;
9993
9994 /* Nonzero means after finally constructing the expression
9995 convert it to this type. */
9996 tree final_type = 0;
9997
9998 /* Nonzero if this is an operation like MIN or MAX which can
9999 safely be computed in short if both args are promoted shorts.
10000 Also implies COMMON.
10001 -1 indicates a bitwise operation; this makes a difference
10002 in the exact conditions for when it is safe to do the operation
10003 in a narrower mode. */
10004 int shorten = 0;
10005
10006 /* Nonzero if this is a comparison operation;
10007 if both args are promoted shorts, compare the original shorts.
10008 Also implies COMMON. */
10009 int short_compare = 0;
10010
10011 /* Nonzero if this is a right-shift operation, which can be computed on the
10012 original short and then promoted if the operand is a promoted short. */
10013 int short_shift = 0;
10014
10015 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10016 int common = 0;
10017
58393038
ZL
10018 /* True means types are compatible as far as ObjC is concerned. */
10019 bool objc_ok;
10020
8ce94e44
JM
10021 /* True means this is an arithmetic operation that may need excess
10022 precision. */
10023 bool may_need_excess_precision;
10024
180f8dbb
JM
10025 /* True means this is a boolean operation that converts both its
10026 operands to truth-values. */
10027 bool boolean_op = false;
10028
de5a5fa1
MP
10029 /* Remember whether we're doing / or %. */
10030 bool doing_div_or_mod = false;
10031
10032 /* Remember whether we're doing << or >>. */
10033 bool doing_shift = false;
10034
10035 /* Tree holding instrumentation expression. */
10036 tree instrument_expr = NULL;
10037
ba47d38d
AH
10038 if (location == UNKNOWN_LOCATION)
10039 location = input_location;
10040
4d84fe7c
JM
10041 op0 = orig_op0;
10042 op1 = orig_op1;
10043
10044 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10045 if (op0_int_operands)
10046 op0 = remove_c_maybe_const_expr (op0);
10047 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10048 if (op1_int_operands)
10049 op1 = remove_c_maybe_const_expr (op1);
10050 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10051 if (int_operands)
10052 {
10053 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10054 && TREE_CODE (orig_op1) == INTEGER_CST);
10055 int_const = (int_const_or_overflow
10056 && !TREE_OVERFLOW (orig_op0)
10057 && !TREE_OVERFLOW (orig_op1));
10058 }
10059 else
10060 int_const = int_const_or_overflow = false;
10061
0e3a99ae 10062 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
10063 if (convert_p
10064 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 10065 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 10066 {
4d84fe7c
JM
10067 op0 = default_conversion (op0);
10068 op1 = default_conversion (op1);
790e9490
RS
10069 }
10070
36536d79
BI
10071 /* When Cilk Plus is enabled and there are array notations inside op0, then
10072 we check to see if there are builtin array notation functions. If
10073 so, then we take on the type of the array notation inside it. */
10074 if (flag_enable_cilkplus && contains_array_notation_expr (op0))
10075 orig_type0 = type0 = find_correct_array_notation_type (op0);
10076 else
10077 orig_type0 = type0 = TREE_TYPE (op0);
10078
10079 if (flag_enable_cilkplus && contains_array_notation_expr (op1))
10080 orig_type1 = type1 = find_correct_array_notation_type (op1);
10081 else
10082 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10083
3e4093b6
RS
10084 /* The expression codes of the data types of the arguments tell us
10085 whether the arguments are integers, floating, pointers, etc. */
10086 code0 = TREE_CODE (type0);
10087 code1 = TREE_CODE (type1);
10088
10089 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10090 STRIP_TYPE_NOPS (op0);
10091 STRIP_TYPE_NOPS (op1);
10092
10093 /* If an error was already reported for one of the arguments,
10094 avoid reporting another error. */
10095
10096 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10097 return error_mark_node;
10098
4de67c26
JM
10099 if ((invalid_op_diag
10100 = targetm.invalid_binary_op (code, type0, type1)))
10101 {
ba47d38d 10102 error_at (location, invalid_op_diag);
4de67c26
JM
10103 return error_mark_node;
10104 }
10105
8ce94e44
JM
10106 switch (code)
10107 {
10108 case PLUS_EXPR:
10109 case MINUS_EXPR:
10110 case MULT_EXPR:
10111 case TRUNC_DIV_EXPR:
10112 case CEIL_DIV_EXPR:
10113 case FLOOR_DIV_EXPR:
10114 case ROUND_DIV_EXPR:
10115 case EXACT_DIV_EXPR:
10116 may_need_excess_precision = true;
10117 break;
10118 default:
10119 may_need_excess_precision = false;
10120 break;
10121 }
10122 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10123 {
10124 op0 = TREE_OPERAND (op0, 0);
10125 type0 = TREE_TYPE (op0);
10126 }
10127 else if (may_need_excess_precision
10128 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10129 {
10130 type0 = eptype;
10131 op0 = convert (eptype, op0);
10132 }
10133 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10134 {
10135 op1 = TREE_OPERAND (op1, 0);
10136 type1 = TREE_TYPE (op1);
10137 }
10138 else if (may_need_excess_precision
10139 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10140 {
10141 type1 = eptype;
10142 op1 = convert (eptype, op1);
10143 }
10144
58393038
ZL
10145 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10146
0e3a99ae
AS
10147 /* In case when one of the operands of the binary operation is
10148 a vector and another is a scalar -- convert scalar to vector. */
10149 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10150 {
a212e43f
MG
10151 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10152 true);
f90e8e2e 10153
0e3a99ae
AS
10154 switch (convert_flag)
10155 {
10156 case stv_error:
10157 return error_mark_node;
10158 case stv_firstarg:
10159 {
10160 bool maybe_const = true;
10161 tree sc;
10162 sc = c_fully_fold (op0, false, &maybe_const);
10163 sc = save_expr (sc);
10164 sc = convert (TREE_TYPE (type1), sc);
10165 op0 = build_vector_from_val (type1, sc);
10166 if (!maybe_const)
10167 op0 = c_wrap_maybe_const (op0, true);
10168 orig_type0 = type0 = TREE_TYPE (op0);
10169 code0 = TREE_CODE (type0);
10170 converted = 1;
10171 break;
10172 }
10173 case stv_secondarg:
10174 {
10175 bool maybe_const = true;
10176 tree sc;
10177 sc = c_fully_fold (op1, false, &maybe_const);
10178 sc = save_expr (sc);
10179 sc = convert (TREE_TYPE (type0), sc);
10180 op1 = build_vector_from_val (type0, sc);
10181 if (!maybe_const)
54b9f838 10182 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10183 orig_type1 = type1 = TREE_TYPE (op1);
10184 code1 = TREE_CODE (type1);
10185 converted = 1;
10186 break;
10187 }
10188 default:
10189 break;
10190 }
10191 }
10192
3e4093b6 10193 switch (code)
de520661 10194 {
3e4093b6
RS
10195 case PLUS_EXPR:
10196 /* Handle the pointer + int case. */
10197 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10198 {
db3927fb 10199 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10200 goto return_build_binary_op;
10201 }
3e4093b6 10202 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10203 {
db3927fb 10204 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10205 goto return_build_binary_op;
10206 }
fe67cf58 10207 else
3e4093b6
RS
10208 common = 1;
10209 break;
400fbf9f 10210
3e4093b6
RS
10211 case MINUS_EXPR:
10212 /* Subtraction of two similar pointers.
10213 We must subtract them as integers, then divide by object size. */
10214 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10215 && comp_target_types (location, type0, type1))
c9f9eb5d 10216 {
db3927fb 10217 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10218 goto return_build_binary_op;
10219 }
3e4093b6
RS
10220 /* Handle pointer minus int. Just like pointer plus int. */
10221 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10222 {
db3927fb 10223 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10224 goto return_build_binary_op;
10225 }
3e4093b6
RS
10226 else
10227 common = 1;
10228 break;
8b6a5902 10229
3e4093b6
RS
10230 case MULT_EXPR:
10231 common = 1;
10232 break;
10233
10234 case TRUNC_DIV_EXPR:
10235 case CEIL_DIV_EXPR:
10236 case FLOOR_DIV_EXPR:
10237 case ROUND_DIV_EXPR:
10238 case EXACT_DIV_EXPR:
de5a5fa1 10239 doing_div_or_mod = true;
c9f9eb5d 10240 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10241
10242 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10243 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10244 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10245 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10246 || code1 == FIXED_POINT_TYPE
3e4093b6 10247 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10248 {
5bed876a
AH
10249 enum tree_code tcode0 = code0, tcode1 = code1;
10250
3a021db2 10251 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10252 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10253 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10254 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10255
ab22c1fa
CF
10256 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10257 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10258 resultcode = RDIV_EXPR;
10259 else
10260 /* Although it would be tempting to shorten always here, that
10261 loses on some targets, since the modulo instruction is
10262 undefined if the quotient can't be represented in the
10263 computation mode. We shorten only if unsigned or if
10264 dividing by something we know != -1. */
8df83eae 10265 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10266 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10267 && !integer_all_onesp (op1)));
3e4093b6
RS
10268 common = 1;
10269 }
10270 break;
de520661 10271
3e4093b6 10272 case BIT_AND_EXPR:
3e4093b6
RS
10273 case BIT_IOR_EXPR:
10274 case BIT_XOR_EXPR:
10275 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10276 shorten = -1;
9ef0c8d9
AP
10277 /* Allow vector types which are not floating point types. */
10278 else if (code0 == VECTOR_TYPE
10279 && code1 == VECTOR_TYPE
10280 && !VECTOR_FLOAT_TYPE_P (type0)
10281 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10282 common = 1;
10283 break;
10284
10285 case TRUNC_MOD_EXPR:
10286 case FLOOR_MOD_EXPR:
de5a5fa1 10287 doing_div_or_mod = true;
c9f9eb5d 10288 warn_for_div_by_zero (location, op1);
de520661 10289
5cfd5d9b
AP
10290 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10291 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10292 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10293 common = 1;
10294 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10295 {
10296 /* Although it would be tempting to shorten always here, that loses
10297 on some targets, since the modulo instruction is undefined if the
10298 quotient can't be represented in the computation mode. We shorten
10299 only if unsigned or if dividing by something we know != -1. */
8df83eae 10300 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10301 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10302 && !integer_all_onesp (op1)));
3e4093b6
RS
10303 common = 1;
10304 }
10305 break;
de520661 10306
3e4093b6
RS
10307 case TRUTH_ANDIF_EXPR:
10308 case TRUTH_ORIF_EXPR:
10309 case TRUTH_AND_EXPR:
10310 case TRUTH_OR_EXPR:
10311 case TRUTH_XOR_EXPR:
10312 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10313 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10314 || code0 == FIXED_POINT_TYPE)
3e4093b6 10315 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10316 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10317 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10318 {
10319 /* Result of these operations is always an int,
10320 but that does not mean the operands should be
10321 converted to ints! */
10322 result_type = integer_type_node;
a27d595d
JM
10323 if (op0_int_operands)
10324 {
10325 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10326 op0 = remove_c_maybe_const_expr (op0);
10327 }
10328 else
10329 op0 = c_objc_common_truthvalue_conversion (location, op0);
10330 if (op1_int_operands)
10331 {
10332 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10333 op1 = remove_c_maybe_const_expr (op1);
10334 }
10335 else
10336 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10337 converted = 1;
180f8dbb 10338 boolean_op = true;
3e4093b6 10339 }
928c19bb
JM
10340 if (code == TRUTH_ANDIF_EXPR)
10341 {
10342 int_const_or_overflow = (int_operands
10343 && TREE_CODE (orig_op0) == INTEGER_CST
10344 && (op0 == truthvalue_false_node
10345 || TREE_CODE (orig_op1) == INTEGER_CST));
10346 int_const = (int_const_or_overflow
10347 && !TREE_OVERFLOW (orig_op0)
10348 && (op0 == truthvalue_false_node
10349 || !TREE_OVERFLOW (orig_op1)));
10350 }
10351 else if (code == TRUTH_ORIF_EXPR)
10352 {
10353 int_const_or_overflow = (int_operands
10354 && TREE_CODE (orig_op0) == INTEGER_CST
10355 && (op0 == truthvalue_true_node
10356 || TREE_CODE (orig_op1) == INTEGER_CST));
10357 int_const = (int_const_or_overflow
10358 && !TREE_OVERFLOW (orig_op0)
10359 && (op0 == truthvalue_true_node
10360 || !TREE_OVERFLOW (orig_op1)));
10361 }
3e4093b6 10362 break;
eba80994 10363
3e4093b6
RS
10364 /* Shift operations: result has same type as first operand;
10365 always convert second operand to int.
10366 Also set SHORT_SHIFT if shifting rightward. */
de520661 10367
3e4093b6 10368 case RSHIFT_EXPR:
f87bd04b
AS
10369 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10370 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10371 {
10372 result_type = type0;
10373 converted = 1;
10374 }
10375 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10376 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10377 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10378 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10379 {
10380 result_type = type0;
10381 converted = 1;
10382 }
10383 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10384 && code1 == INTEGER_TYPE)
3e4093b6 10385 {
de5a5fa1 10386 doing_shift = true;
928c19bb 10387 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10388 {
3e4093b6 10389 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10390 {
10391 int_const = false;
7d882b83 10392 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10393 warning (0, "right shift count is negative");
10394 }
3e4093b6 10395 else
bbb818c6 10396 {
3f75a254 10397 if (!integer_zerop (op1))
3e4093b6
RS
10398 short_shift = 1;
10399
10400 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10401 {
10402 int_const = false;
7d882b83 10403 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10404 warning (0, "right shift count >= width of type");
10405 }
bbb818c6 10406 }
b62acd60 10407 }
de520661 10408
3e4093b6
RS
10409 /* Use the type of the value to be shifted. */
10410 result_type = type0;
f87bd04b
AS
10411 /* Convert the non vector shift-count to an integer, regardless
10412 of size of value being shifted. */
10413 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10414 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10415 op1 = convert (integer_type_node, op1);
10416 /* Avoid converting op1 to result_type later. */
10417 converted = 1;
400fbf9f 10418 }
3e4093b6 10419 break;
253b6b82 10420
3e4093b6 10421 case LSHIFT_EXPR:
f87bd04b
AS
10422 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10423 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10424 {
10425 result_type = type0;
10426 converted = 1;
10427 }
10428 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10429 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10430 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10431 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10432 {
10433 result_type = type0;
10434 converted = 1;
10435 }
10436 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10437 && code1 == INTEGER_TYPE)
3e4093b6 10438 {
de5a5fa1 10439 doing_shift = true;
928c19bb 10440 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10441 {
3e4093b6 10442 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10443 {
10444 int_const = false;
7d882b83 10445 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10446 warning (0, "left shift count is negative");
10447 }
de520661 10448
3e4093b6 10449 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10450 {
10451 int_const = false;
7d882b83 10452 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10453 warning (0, "left shift count >= width of type");
10454 }
94ba5069 10455 }
de520661 10456
3e4093b6
RS
10457 /* Use the type of the value to be shifted. */
10458 result_type = type0;
f87bd04b
AS
10459 /* Convert the non vector shift-count to an integer, regardless
10460 of size of value being shifted. */
10461 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10462 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10463 op1 = convert (integer_type_node, op1);
10464 /* Avoid converting op1 to result_type later. */
10465 converted = 1;
400fbf9f 10466 }
3e4093b6 10467 break;
de520661 10468
3e4093b6
RS
10469 case EQ_EXPR:
10470 case NE_EXPR:
d246ab4f
AS
10471 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10472 {
10473 tree intt;
0af94e6f 10474 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10475 {
10476 error_at (location, "comparing vectors with different "
10477 "element types");
10478 return error_mark_node;
10479 }
10480
10481 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10482 {
10483 error_at (location, "comparing vectors with different "
10484 "number of elements");
10485 return error_mark_node;
10486 }
10487
10488 /* Always construct signed integer vector type. */
10489 intt = c_common_type_for_size (GET_MODE_BITSIZE
10490 (TYPE_MODE (TREE_TYPE (type0))), 0);
10491 result_type = build_opaque_vector_type (intt,
10492 TYPE_VECTOR_SUBPARTS (type0));
10493 converted = 1;
10494 break;
10495 }
ae311566 10496 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10497 warning_at (location,
10498 OPT_Wfloat_equal,
10499 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10500 /* Result of comparison is always int,
10501 but don't convert the args to int! */
10502 build_type = integer_type_node;
10503 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10504 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10505 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10506 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10507 short_compare = 1;
637f1455
SZ
10508 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10509 {
10510 if (TREE_CODE (op0) == ADDR_EXPR
10511 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10512 {
10513 if (code == EQ_EXPR)
10514 warning_at (location,
10515 OPT_Waddress,
10516 "the comparison will always evaluate as %<false%> "
10517 "for the address of %qD will never be NULL",
10518 TREE_OPERAND (op0, 0));
10519 else
10520 warning_at (location,
10521 OPT_Waddress,
10522 "the comparison will always evaluate as %<true%> "
10523 "for the address of %qD will never be NULL",
10524 TREE_OPERAND (op0, 0));
10525 }
10526 result_type = type0;
10527 }
10528 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10529 {
10530 if (TREE_CODE (op1) == ADDR_EXPR
10531 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10532 {
10533 if (code == EQ_EXPR)
10534 warning_at (location,
f90e8e2e 10535 OPT_Waddress,
637f1455
SZ
10536 "the comparison will always evaluate as %<false%> "
10537 "for the address of %qD will never be NULL",
10538 TREE_OPERAND (op1, 0));
10539 else
10540 warning_at (location,
10541 OPT_Waddress,
10542 "the comparison will always evaluate as %<true%> "
10543 "for the address of %qD will never be NULL",
10544 TREE_OPERAND (op1, 0));
10545 }
10546 result_type = type1;
10547 }
3e4093b6
RS
10548 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10549 {
10550 tree tt0 = TREE_TYPE (type0);
10551 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10552 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10553 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10554 addr_space_t as_common = ADDR_SPACE_GENERIC;
10555
3e4093b6
RS
10556 /* Anything compares with void *. void * compares with anything.
10557 Otherwise, the targets must be compatible
10558 and both must be object or both incomplete. */
744aa42f 10559 if (comp_target_types (location, type0, type1))
10bc1b1b 10560 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10561 else if (!addr_space_superset (as0, as1, &as_common))
10562 {
10563 error_at (location, "comparison of pointers to "
10564 "disjoint address spaces");
10565 return error_mark_node;
10566 }
267bac10 10567 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10568 {
36c5e70a 10569 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10570 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10571 "comparison of %<void *%> with function pointer");
ee2990e7 10572 }
267bac10 10573 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10574 {
36c5e70a 10575 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10576 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10577 "comparison of %<void *%> with function pointer");
e6834654 10578 }
3e4093b6 10579 else
58393038
ZL
10580 /* Avoid warning about the volatile ObjC EH puts on decls. */
10581 if (!objc_ok)
ba47d38d 10582 pedwarn (location, 0,
509c9d60 10583 "comparison of distinct pointer types lacks a cast");
e6834654 10584
3e4093b6 10585 if (result_type == NULL_TREE)
36c5e70a
BE
10586 {
10587 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10588 result_type = build_pointer_type
10589 (build_qualified_type (void_type_node, qual));
10590 }
e6834654 10591 }
3e4093b6 10592 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10593 {
3e4093b6 10594 result_type = type0;
ba47d38d 10595 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10596 }
3e4093b6 10597 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10598 {
3e4093b6 10599 result_type = type1;
ba47d38d 10600 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10601 }
3e4093b6 10602 break;
8b6a5902 10603
3e4093b6
RS
10604 case LE_EXPR:
10605 case GE_EXPR:
10606 case LT_EXPR:
10607 case GT_EXPR:
d246ab4f
AS
10608 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10609 {
10610 tree intt;
0af94e6f 10611 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10612 {
10613 error_at (location, "comparing vectors with different "
10614 "element types");
10615 return error_mark_node;
10616 }
10617
10618 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10619 {
10620 error_at (location, "comparing vectors with different "
10621 "number of elements");
10622 return error_mark_node;
10623 }
10624
10625 /* Always construct signed integer vector type. */
10626 intt = c_common_type_for_size (GET_MODE_BITSIZE
10627 (TYPE_MODE (TREE_TYPE (type0))), 0);
10628 result_type = build_opaque_vector_type (intt,
10629 TYPE_VECTOR_SUBPARTS (type0));
10630 converted = 1;
10631 break;
10632 }
3e4093b6 10633 build_type = integer_type_node;
ab22c1fa
CF
10634 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10635 || code0 == FIXED_POINT_TYPE)
10636 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10637 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10638 short_compare = 1;
10639 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10640 {
36c5e70a
BE
10641 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10642 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10643 addr_space_t as_common;
10644
744aa42f 10645 if (comp_target_types (location, type0, type1))
3e4093b6 10646 {
10bc1b1b 10647 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10648 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10649 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10650 pedwarn (location, 0,
509c9d60 10651 "comparison of complete and incomplete pointers");
fcf73884 10652 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10653 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10654 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10655 else if (null_pointer_constant_p (orig_op0)
10656 || null_pointer_constant_p (orig_op1))
10657 warning_at (location, OPT_Wextra,
10658 "ordered comparison of pointer with null pointer");
10659
3e4093b6 10660 }
36c5e70a
BE
10661 else if (!addr_space_superset (as0, as1, &as_common))
10662 {
10663 error_at (location, "comparison of pointers to "
10664 "disjoint address spaces");
10665 return error_mark_node;
10666 }
3e4093b6
RS
10667 else
10668 {
36c5e70a
BE
10669 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10670 result_type = build_pointer_type
10671 (build_qualified_type (void_type_node, qual));
ba47d38d 10672 pedwarn (location, 0,
509c9d60 10673 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10674 }
10675 }
6aa3c60d 10676 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10677 {
10678 result_type = type0;
fcf73884 10679 if (pedantic)
c1771a20 10680 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10681 "ordered comparison of pointer with integer zero");
10682 else if (extra_warnings)
ba47d38d 10683 warning_at (location, OPT_Wextra,
d42ba3b8 10684 "ordered comparison of pointer with integer zero");
3e4093b6 10685 }
6aa3c60d 10686 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10687 {
10688 result_type = type1;
d42ba3b8 10689 if (pedantic)
c1771a20 10690 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10691 "ordered comparison of pointer with integer zero");
10692 else if (extra_warnings)
10693 warning_at (location, OPT_Wextra,
10694 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10695 }
10696 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10697 {
10698 result_type = type0;
ba47d38d 10699 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10700 }
10701 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10702 {
10703 result_type = type1;
ba47d38d 10704 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10705 }
10706 break;
64094f6a 10707
3e4093b6 10708 default:
37b2f290 10709 gcc_unreachable ();
c9fe6f9f 10710 }
8f17b5c5 10711
e57e265b
PB
10712 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10713 return error_mark_node;
10714
5bed876a
AH
10715 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10716 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10717 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10718 {
ba47d38d 10719 binary_op_error (location, code, type0, type1);
5bed876a
AH
10720 return error_mark_node;
10721 }
10722
3e4093b6 10723 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10724 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10725 &&
10726 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10727 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10728 {
2ca862e9
JM
10729 bool first_complex = (code0 == COMPLEX_TYPE);
10730 bool second_complex = (code1 == COMPLEX_TYPE);
10731 int none_complex = (!first_complex && !second_complex);
39b726dd 10732
3e4093b6 10733 if (shorten || common || short_compare)
3bf6bfcc
JJ
10734 {
10735 result_type = c_common_type (type0, type1);
0a0b3574
MM
10736 do_warn_double_promotion (result_type, type0, type1,
10737 "implicit conversion from %qT to %qT "
10738 "to match other operand of binary "
10739 "expression",
10740 location);
3bf6bfcc
JJ
10741 if (result_type == error_mark_node)
10742 return error_mark_node;
10743 }
400fbf9f 10744
2ca862e9
JM
10745 if (first_complex != second_complex
10746 && (code == PLUS_EXPR
10747 || code == MINUS_EXPR
10748 || code == MULT_EXPR
10749 || (code == TRUNC_DIV_EXPR && first_complex))
10750 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10751 && flag_signed_zeros)
10752 {
10753 /* An operation on mixed real/complex operands must be
10754 handled specially, but the language-independent code can
10755 more easily optimize the plain complex arithmetic if
10756 -fno-signed-zeros. */
10757 tree real_type = TREE_TYPE (result_type);
10758 tree real, imag;
10759 if (type0 != orig_type0 || type1 != orig_type1)
10760 {
10761 gcc_assert (may_need_excess_precision && common);
2d2e923f 10762 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
10763 }
10764 if (first_complex)
10765 {
10766 if (TREE_TYPE (op0) != result_type)
10767 op0 = convert_and_check (result_type, op0);
10768 if (TREE_TYPE (op1) != real_type)
10769 op1 = convert_and_check (real_type, op1);
10770 }
10771 else
10772 {
10773 if (TREE_TYPE (op0) != real_type)
10774 op0 = convert_and_check (real_type, op0);
10775 if (TREE_TYPE (op1) != result_type)
10776 op1 = convert_and_check (result_type, op1);
10777 }
10778 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10779 return error_mark_node;
10780 if (first_complex)
10781 {
10782 op0 = c_save_expr (op0);
10783 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10784 op0, 1);
10785 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10786 op0, 1);
10787 switch (code)
10788 {
10789 case MULT_EXPR:
10790 case TRUNC_DIV_EXPR:
c4603e7c 10791 op1 = c_save_expr (op1);
2ca862e9
JM
10792 imag = build2 (resultcode, real_type, imag, op1);
10793 /* Fall through. */
10794 case PLUS_EXPR:
10795 case MINUS_EXPR:
10796 real = build2 (resultcode, real_type, real, op1);
10797 break;
10798 default:
10799 gcc_unreachable();
10800 }
10801 }
10802 else
10803 {
10804 op1 = c_save_expr (op1);
10805 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10806 op1, 1);
10807 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10808 op1, 1);
10809 switch (code)
10810 {
10811 case MULT_EXPR:
c4603e7c 10812 op0 = c_save_expr (op0);
2ca862e9
JM
10813 imag = build2 (resultcode, real_type, op0, imag);
10814 /* Fall through. */
10815 case PLUS_EXPR:
10816 real = build2 (resultcode, real_type, op0, real);
10817 break;
10818 case MINUS_EXPR:
10819 real = build2 (resultcode, real_type, op0, real);
10820 imag = build1 (NEGATE_EXPR, real_type, imag);
10821 break;
10822 default:
10823 gcc_unreachable();
10824 }
10825 }
10826 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10827 goto return_build_binary_op;
10828 }
10829
3e4093b6
RS
10830 /* For certain operations (which identify themselves by shorten != 0)
10831 if both args were extended from the same smaller type,
10832 do the arithmetic in that type and then extend.
400fbf9f 10833
3e4093b6
RS
10834 shorten !=0 and !=1 indicates a bitwise operation.
10835 For them, this optimization is safe only if
10836 both args are zero-extended or both are sign-extended.
10837 Otherwise, we might change the result.
10838 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10839 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 10840
3e4093b6
RS
10841 if (shorten && none_complex)
10842 {
3e4093b6 10843 final_type = result_type;
b8698a0f 10844 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 10845 shorten == -1);
3e4093b6 10846 }
88a3dbc1 10847
3e4093b6 10848 /* Shifts can be shortened if shifting right. */
2f6e4e97 10849
3e4093b6
RS
10850 if (short_shift)
10851 {
10852 int unsigned_arg;
10853 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 10854
3e4093b6 10855 final_type = result_type;
abe80e6d 10856
3e4093b6 10857 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 10858 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 10859
3e4093b6 10860 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 10861 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
10862 /* We can shorten only if the shift count is less than the
10863 number of bits in the smaller type size. */
10864 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10865 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 10866 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
10867 {
10868 /* Do an unsigned shift if the operand was zero-extended. */
10869 result_type
10870 = c_common_signed_or_unsigned_type (unsigned_arg,
10871 TREE_TYPE (arg0));
10872 /* Convert value-to-be-shifted to that type. */
10873 if (TREE_TYPE (op0) != result_type)
10874 op0 = convert (result_type, op0);
10875 converted = 1;
abe80e6d 10876 }
88a3dbc1
RK
10877 }
10878
3e4093b6
RS
10879 /* Comparison operations are shortened too but differently.
10880 They identify themselves by setting short_compare = 1. */
56cb9733 10881
3e4093b6
RS
10882 if (short_compare)
10883 {
10884 /* Don't write &op0, etc., because that would prevent op0
10885 from being kept in a register.
10886 Instead, make copies of the our local variables and
10887 pass the copies by reference, then copy them back afterward. */
10888 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10889 enum tree_code xresultcode = resultcode;
10890 tree val
10891 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8f17b5c5 10892
3e4093b6 10893 if (val != 0)
c9f9eb5d
AH
10894 {
10895 ret = val;
10896 goto return_build_binary_op;
10897 }
8f17b5c5 10898
3e4093b6
RS
10899 op0 = xop0, op1 = xop1;
10900 converted = 1;
10901 resultcode = xresultcode;
8f17b5c5 10902
7d882b83 10903 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10904 {
10905 bool op0_maybe_const = true;
10906 bool op1_maybe_const = true;
10907 tree orig_op0_folded, orig_op1_folded;
10908
10909 if (in_late_binary_op)
10910 {
10911 orig_op0_folded = orig_op0;
10912 orig_op1_folded = orig_op1;
10913 }
10914 else
10915 {
10916 /* Fold for the sake of possible warnings, as in
10917 build_conditional_expr. This requires the
10918 "original" values to be folded, not just op0 and
10919 op1. */
f5178456 10920 c_inhibit_evaluation_warnings++;
928c19bb
JM
10921 op0 = c_fully_fold (op0, require_constant_value,
10922 &op0_maybe_const);
10923 op1 = c_fully_fold (op1, require_constant_value,
10924 &op1_maybe_const);
f5178456 10925 c_inhibit_evaluation_warnings--;
928c19bb
JM
10926 orig_op0_folded = c_fully_fold (orig_op0,
10927 require_constant_value,
10928 NULL);
10929 orig_op1_folded = c_fully_fold (orig_op1,
10930 require_constant_value,
10931 NULL);
10932 }
10933
10934 if (warn_sign_compare)
10935 warn_for_sign_compare (location, orig_op0_folded,
10936 orig_op1_folded, op0, op1,
10937 result_type, resultcode);
5c2f94b4 10938 if (!in_late_binary_op && !int_operands)
928c19bb
JM
10939 {
10940 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 10941 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 10942 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 10943 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 10944 }
3e4093b6 10945 }
2ad1815d 10946 }
64094f6a 10947 }
64094f6a 10948
3e4093b6
RS
10949 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10950 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10951 Then the expression will be built.
10952 It will be given type FINAL_TYPE if that is nonzero;
10953 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 10954
3e4093b6
RS
10955 if (!result_type)
10956 {
ba47d38d 10957 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
10958 return error_mark_node;
10959 }
400fbf9f 10960
3e4093b6 10961 if (build_type == NULL_TREE)
8ce94e44
JM
10962 {
10963 build_type = result_type;
180f8dbb
JM
10964 if ((type0 != orig_type0 || type1 != orig_type1)
10965 && !boolean_op)
8ce94e44
JM
10966 {
10967 gcc_assert (may_need_excess_precision && common);
2d2e923f 10968 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
10969 }
10970 }
400fbf9f 10971
2d2e923f
MLI
10972 if (!converted)
10973 {
10974 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10975 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10976
10977 /* This can happen if one operand has a vector type, and the other
10978 has a different type. */
10979 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10980 return error_mark_node;
10981 }
10982
a24d975c 10983 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
de5a5fa1 10984 && current_function_decl != 0
ce6923c5
MP
10985 && !lookup_attribute ("no_sanitize_undefined",
10986 DECL_ATTRIBUTES (current_function_decl))
de5a5fa1
MP
10987 && (doing_div_or_mod || doing_shift))
10988 {
10989 /* OP0 and/or OP1 might have side-effects. */
10990 op0 = c_save_expr (op0);
10991 op1 = c_save_expr (op1);
10992 op0 = c_fully_fold (op0, false, NULL);
10993 op1 = c_fully_fold (op1, false, NULL);
a24d975c 10994 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
de5a5fa1 10995 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 10996 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
10997 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
10998 }
10999
c9f9eb5d 11000 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11001 if (int_const_or_overflow)
11002 ret = (require_constant_value
db3927fb
AH
11003 ? fold_build2_initializer_loc (location, resultcode, build_type,
11004 op0, op1)
11005 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11006 else
11007 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11008 if (final_type != 0)
11009 ret = convert (final_type, ret);
11010
11011 return_build_binary_op:
11012 gcc_assert (ret != error_mark_node);
928c19bb
JM
11013 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11014 ret = (int_operands
11015 ? note_integer_operands (ret)
11016 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11017 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11018 && !in_late_binary_op)
11019 ret = note_integer_operands (ret);
2d2e923f
MLI
11020 if (semantic_result_type)
11021 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11022 protected_set_expr_location (ret, location);
de5a5fa1 11023
a24d975c 11024 if (instrument_expr != NULL)
de5a5fa1
MP
11025 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11026 instrument_expr, ret);
11027
c9f9eb5d 11028 return ret;
400fbf9f 11029}
85498824
JM
11030
11031
11032/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11033 purpose. LOCATION is the source location for the expression. */
85498824
JM
11034
11035tree
ba47d38d 11036c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11037{
928c19bb
JM
11038 bool int_const, int_operands;
11039
85498824
JM
11040 switch (TREE_CODE (TREE_TYPE (expr)))
11041 {
11042 case ARRAY_TYPE:
ba47d38d 11043 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11044 return error_mark_node;
11045
11046 case RECORD_TYPE:
ba47d38d 11047 error_at (location, "used struct type value where scalar is required");
85498824
JM
11048 return error_mark_node;
11049
11050 case UNION_TYPE:
ba47d38d 11051 error_at (location, "used union type value where scalar is required");
85498824
JM
11052 return error_mark_node;
11053
04af8788
NP
11054 case VOID_TYPE:
11055 error_at (location, "void value not ignored as it ought to be");
11056 return error_mark_node;
11057
46bdb9cf
JM
11058 case FUNCTION_TYPE:
11059 gcc_unreachable ();
11060
d246ab4f
AS
11061 case VECTOR_TYPE:
11062 error_at (location, "used vector type where scalar is required");
11063 return error_mark_node;
11064
85498824
JM
11065 default:
11066 break;
11067 }
11068
928c19bb
JM
11069 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11070 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11071 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11072 {
11073 expr = remove_c_maybe_const_expr (expr);
11074 expr = build2 (NE_EXPR, integer_type_node, expr,
11075 convert (TREE_TYPE (expr), integer_zero_node));
11076 expr = note_integer_operands (expr);
11077 }
11078 else
11079 /* ??? Should we also give an error for vectors rather than leaving
11080 those to give errors later? */
11081 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11082
11083 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11084 {
11085 if (TREE_OVERFLOW (expr))
11086 return expr;
11087 else
11088 return note_integer_operands (expr);
11089 }
11090 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11091 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11092 return expr;
85498824 11093}
73f397d4
JM
11094\f
11095
11096/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11097 required. */
11098
11099tree
51eed280 11100c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11101{
11102 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11103 {
11104 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11105 /* Executing a compound literal inside a function reinitializes
11106 it. */
11107 if (!TREE_STATIC (decl))
11108 *se = true;
11109 return decl;
11110 }
11111 else
11112 return expr;
11113}
953ff289 11114\f
c0220ea4 11115/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11116
11117tree
11118c_begin_omp_parallel (void)
11119{
11120 tree block;
11121
11122 keep_next_level ();
11123 block = c_begin_compound_stmt (true);
11124
11125 return block;
11126}
11127
c2255bc4
AH
11128/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11129 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11130
953ff289 11131tree
c2255bc4 11132c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11133{
11134 tree stmt;
11135
c2255bc4 11136 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11137
11138 stmt = make_node (OMP_PARALLEL);
11139 TREE_TYPE (stmt) = void_type_node;
11140 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11141 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11142 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11143
11144 return add_stmt (stmt);
11145}
11146
a68ab351
JJ
11147/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11148
11149tree
11150c_begin_omp_task (void)
11151{
11152 tree block;
11153
11154 keep_next_level ();
11155 block = c_begin_compound_stmt (true);
11156
11157 return block;
11158}
11159
c2255bc4
AH
11160/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11161 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11162
11163tree
c2255bc4 11164c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11165{
11166 tree stmt;
11167
c2255bc4 11168 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11169
11170 stmt = make_node (OMP_TASK);
11171 TREE_TYPE (stmt) = void_type_node;
11172 OMP_TASK_CLAUSES (stmt) = clauses;
11173 OMP_TASK_BODY (stmt) = block;
c2255bc4 11174 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11175
11176 return add_stmt (stmt);
11177}
11178
acf0174b
JJ
11179/* Generate GOMP_cancel call for #pragma omp cancel. */
11180
11181void
11182c_finish_omp_cancel (location_t loc, tree clauses)
11183{
11184 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11185 int mask = 0;
11186 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11187 mask = 1;
11188 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11189 mask = 2;
11190 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11191 mask = 4;
11192 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11193 mask = 8;
11194 else
11195 {
11196 error_at (loc, "%<#pragma omp cancel must specify one of "
11197 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11198 "clauses");
11199 return;
11200 }
11201 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11202 if (ifc != NULL_TREE)
11203 {
11204 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11205 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11206 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11207 build_zero_cst (type));
11208 }
11209 else
11210 ifc = boolean_true_node;
11211 tree stmt = build_call_expr_loc (loc, fn, 2,
11212 build_int_cst (integer_type_node, mask),
11213 ifc);
11214 add_stmt (stmt);
11215}
11216
11217/* Generate GOMP_cancellation_point call for
11218 #pragma omp cancellation point. */
11219
11220void
11221c_finish_omp_cancellation_point (location_t loc, tree clauses)
11222{
11223 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11224 int mask = 0;
11225 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11226 mask = 1;
11227 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11228 mask = 2;
11229 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11230 mask = 4;
11231 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11232 mask = 8;
11233 else
11234 {
11235 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11236 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11237 "clauses");
11238 return;
11239 }
11240 tree stmt = build_call_expr_loc (loc, fn, 1,
11241 build_int_cst (integer_type_node, mask));
11242 add_stmt (stmt);
11243}
11244
11245/* Helper function for handle_omp_array_sections. Called recursively
11246 to handle multiple array-section-subscripts. C is the clause,
11247 T current expression (initially OMP_CLAUSE_DECL), which is either
11248 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11249 expression if specified, TREE_VALUE length expression if specified,
11250 TREE_CHAIN is what it has been specified after, or some decl.
11251 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11252 set to true if any of the array-section-subscript could have length
11253 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11254 first array-section-subscript which is known not to have length
11255 of one. Given say:
11256 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11257 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11258 all are or may have length of 1, array-section-subscript [:2] is the
11259 first one knonwn not to have length 1. For array-section-subscript
11260 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11261 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11262 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11263 case though, as some lengths could be zero. */
11264
11265static tree
11266handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11267 bool &maybe_zero_len, unsigned int &first_non_one)
11268{
11269 tree ret, low_bound, length, type;
11270 if (TREE_CODE (t) != TREE_LIST)
11271 {
11272 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11273 return error_mark_node;
11274 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11275 {
11276 if (DECL_P (t))
11277 error_at (OMP_CLAUSE_LOCATION (c),
11278 "%qD is not a variable in %qs clause", t,
11279 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11280 else
11281 error_at (OMP_CLAUSE_LOCATION (c),
11282 "%qE is not a variable in %qs clause", t,
11283 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11284 return error_mark_node;
11285 }
11286 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11287 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11288 {
11289 error_at (OMP_CLAUSE_LOCATION (c),
11290 "%qD is threadprivate variable in %qs clause", t,
11291 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11292 return error_mark_node;
11293 }
11294 return t;
11295 }
11296
11297 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11298 maybe_zero_len, first_non_one);
11299 if (ret == error_mark_node || ret == NULL_TREE)
11300 return ret;
11301
11302 type = TREE_TYPE (ret);
11303 low_bound = TREE_PURPOSE (t);
11304 length = TREE_VALUE (t);
11305
11306 if (low_bound == error_mark_node || length == error_mark_node)
11307 return error_mark_node;
11308
11309 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11310 {
11311 error_at (OMP_CLAUSE_LOCATION (c),
11312 "low bound %qE of array section does not have integral type",
11313 low_bound);
11314 return error_mark_node;
11315 }
11316 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11317 {
11318 error_at (OMP_CLAUSE_LOCATION (c),
11319 "length %qE of array section does not have integral type",
11320 length);
11321 return error_mark_node;
11322 }
11323 if (low_bound
11324 && TREE_CODE (low_bound) == INTEGER_CST
11325 && TYPE_PRECISION (TREE_TYPE (low_bound))
11326 > TYPE_PRECISION (sizetype))
11327 low_bound = fold_convert (sizetype, low_bound);
11328 if (length
11329 && TREE_CODE (length) == INTEGER_CST
11330 && TYPE_PRECISION (TREE_TYPE (length))
11331 > TYPE_PRECISION (sizetype))
11332 length = fold_convert (sizetype, length);
11333 if (low_bound == NULL_TREE)
11334 low_bound = integer_zero_node;
11335
11336 if (length != NULL_TREE)
11337 {
11338 if (!integer_nonzerop (length))
11339 maybe_zero_len = true;
11340 if (first_non_one == types.length ()
11341 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11342 first_non_one++;
11343 }
11344 if (TREE_CODE (type) == ARRAY_TYPE)
11345 {
11346 if (length == NULL_TREE
11347 && (TYPE_DOMAIN (type) == NULL_TREE
11348 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11349 {
11350 error_at (OMP_CLAUSE_LOCATION (c),
11351 "for unknown bound array type length expression must "
11352 "be specified");
11353 return error_mark_node;
11354 }
11355 if (TREE_CODE (low_bound) == INTEGER_CST
11356 && tree_int_cst_sgn (low_bound) == -1)
11357 {
11358 error_at (OMP_CLAUSE_LOCATION (c),
11359 "negative low bound in array section in %qs clause",
11360 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11361 return error_mark_node;
11362 }
11363 if (length != NULL_TREE
11364 && TREE_CODE (length) == INTEGER_CST
11365 && tree_int_cst_sgn (length) == -1)
11366 {
11367 error_at (OMP_CLAUSE_LOCATION (c),
11368 "negative length in array section in %qs clause",
11369 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11370 return error_mark_node;
11371 }
11372 if (TYPE_DOMAIN (type)
11373 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11374 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11375 == INTEGER_CST)
11376 {
11377 tree size = size_binop (PLUS_EXPR,
11378 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11379 size_one_node);
11380 if (TREE_CODE (low_bound) == INTEGER_CST)
11381 {
11382 if (tree_int_cst_lt (size, low_bound))
11383 {
11384 error_at (OMP_CLAUSE_LOCATION (c),
11385 "low bound %qE above array section size "
11386 "in %qs clause", low_bound,
11387 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11388 return error_mark_node;
11389 }
11390 if (tree_int_cst_equal (size, low_bound))
11391 maybe_zero_len = true;
11392 else if (length == NULL_TREE
11393 && first_non_one == types.length ()
11394 && tree_int_cst_equal
11395 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11396 low_bound))
11397 first_non_one++;
11398 }
11399 else if (length == NULL_TREE)
11400 {
11401 maybe_zero_len = true;
11402 if (first_non_one == types.length ())
11403 first_non_one++;
11404 }
11405 if (length && TREE_CODE (length) == INTEGER_CST)
11406 {
11407 if (tree_int_cst_lt (size, length))
11408 {
11409 error_at (OMP_CLAUSE_LOCATION (c),
11410 "length %qE above array section size "
11411 "in %qs clause", length,
11412 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11413 return error_mark_node;
11414 }
11415 if (TREE_CODE (low_bound) == INTEGER_CST)
11416 {
11417 tree lbpluslen
11418 = size_binop (PLUS_EXPR,
11419 fold_convert (sizetype, low_bound),
11420 fold_convert (sizetype, length));
11421 if (TREE_CODE (lbpluslen) == INTEGER_CST
11422 && tree_int_cst_lt (size, lbpluslen))
11423 {
11424 error_at (OMP_CLAUSE_LOCATION (c),
11425 "high bound %qE above array section size "
11426 "in %qs clause", lbpluslen,
11427 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11428 return error_mark_node;
11429 }
11430 }
11431 }
11432 }
11433 else if (length == NULL_TREE)
11434 {
11435 maybe_zero_len = true;
11436 if (first_non_one == types.length ())
11437 first_non_one++;
11438 }
11439
11440 /* For [lb:] we will need to evaluate lb more than once. */
11441 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11442 {
11443 tree lb = c_save_expr (low_bound);
11444 if (lb != low_bound)
11445 {
11446 TREE_PURPOSE (t) = lb;
11447 low_bound = lb;
11448 }
11449 }
11450 }
11451 else if (TREE_CODE (type) == POINTER_TYPE)
11452 {
11453 if (length == NULL_TREE)
11454 {
11455 error_at (OMP_CLAUSE_LOCATION (c),
11456 "for pointer type length expression must be specified");
11457 return error_mark_node;
11458 }
11459 /* If there is a pointer type anywhere but in the very first
11460 array-section-subscript, the array section can't be contiguous. */
11461 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11462 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11463 {
11464 error_at (OMP_CLAUSE_LOCATION (c),
11465 "array section is not contiguous in %qs clause",
11466 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11467 return error_mark_node;
11468 }
11469 }
11470 else
11471 {
11472 error_at (OMP_CLAUSE_LOCATION (c),
11473 "%qE does not have pointer or array type", ret);
11474 return error_mark_node;
11475 }
11476 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11477 types.safe_push (TREE_TYPE (ret));
11478 /* We will need to evaluate lb more than once. */
11479 tree lb = c_save_expr (low_bound);
11480 if (lb != low_bound)
11481 {
11482 TREE_PURPOSE (t) = lb;
11483 low_bound = lb;
11484 }
11485 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11486 return ret;
11487}
11488
11489/* Handle array sections for clause C. */
11490
11491static bool
11492handle_omp_array_sections (tree c)
11493{
11494 bool maybe_zero_len = false;
11495 unsigned int first_non_one = 0;
11496 vec<tree> types = vNULL;
11497 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11498 maybe_zero_len, first_non_one);
11499 if (first == error_mark_node)
11500 {
11501 types.release ();
11502 return true;
11503 }
11504 if (first == NULL_TREE)
11505 {
11506 types.release ();
11507 return false;
11508 }
11509 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11510 {
11511 tree t = OMP_CLAUSE_DECL (c);
11512 tree tem = NULL_TREE;
11513 types.release ();
11514 /* Need to evaluate side effects in the length expressions
11515 if any. */
11516 while (TREE_CODE (t) == TREE_LIST)
11517 {
11518 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11519 {
11520 if (tem == NULL_TREE)
11521 tem = TREE_VALUE (t);
11522 else
11523 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11524 TREE_VALUE (t), tem);
11525 }
11526 t = TREE_CHAIN (t);
11527 }
11528 if (tem)
11529 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11530 first = c_fully_fold (first, false, NULL);
11531 OMP_CLAUSE_DECL (c) = first;
11532 }
11533 else
11534 {
11535 unsigned int num = types.length (), i;
11536 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11537 tree condition = NULL_TREE;
11538
11539 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11540 maybe_zero_len = true;
11541
11542 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11543 t = TREE_CHAIN (t))
11544 {
11545 tree low_bound = TREE_PURPOSE (t);
11546 tree length = TREE_VALUE (t);
11547
11548 i--;
11549 if (low_bound
11550 && TREE_CODE (low_bound) == INTEGER_CST
11551 && TYPE_PRECISION (TREE_TYPE (low_bound))
11552 > TYPE_PRECISION (sizetype))
11553 low_bound = fold_convert (sizetype, low_bound);
11554 if (length
11555 && TREE_CODE (length) == INTEGER_CST
11556 && TYPE_PRECISION (TREE_TYPE (length))
11557 > TYPE_PRECISION (sizetype))
11558 length = fold_convert (sizetype, length);
11559 if (low_bound == NULL_TREE)
11560 low_bound = integer_zero_node;
11561 if (!maybe_zero_len && i > first_non_one)
11562 {
11563 if (integer_nonzerop (low_bound))
11564 goto do_warn_noncontiguous;
11565 if (length != NULL_TREE
11566 && TREE_CODE (length) == INTEGER_CST
11567 && TYPE_DOMAIN (types[i])
11568 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11569 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11570 == INTEGER_CST)
11571 {
11572 tree size;
11573 size = size_binop (PLUS_EXPR,
11574 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11575 size_one_node);
11576 if (!tree_int_cst_equal (length, size))
11577 {
11578 do_warn_noncontiguous:
11579 error_at (OMP_CLAUSE_LOCATION (c),
11580 "array section is not contiguous in %qs "
11581 "clause",
11582 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11583 types.release ();
11584 return true;
11585 }
11586 }
11587 if (length != NULL_TREE
11588 && TREE_SIDE_EFFECTS (length))
11589 {
11590 if (side_effects == NULL_TREE)
11591 side_effects = length;
11592 else
11593 side_effects = build2 (COMPOUND_EXPR,
11594 TREE_TYPE (side_effects),
11595 length, side_effects);
11596 }
11597 }
11598 else
11599 {
11600 tree l;
11601
11602 if (i > first_non_one && length && integer_nonzerop (length))
11603 continue;
11604 if (length)
11605 l = fold_convert (sizetype, length);
11606 else
11607 {
11608 l = size_binop (PLUS_EXPR,
11609 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11610 size_one_node);
11611 l = size_binop (MINUS_EXPR, l,
11612 fold_convert (sizetype, low_bound));
11613 }
11614 if (i > first_non_one)
11615 {
11616 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11617 size_zero_node);
11618 if (condition == NULL_TREE)
11619 condition = l;
11620 else
11621 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11622 l, condition);
11623 }
11624 else if (size == NULL_TREE)
11625 {
11626 size = size_in_bytes (TREE_TYPE (types[i]));
11627 size = size_binop (MULT_EXPR, size, l);
11628 if (condition)
11629 size = fold_build3 (COND_EXPR, sizetype, condition,
11630 size, size_zero_node);
11631 }
11632 else
11633 size = size_binop (MULT_EXPR, size, l);
11634 }
11635 }
11636 types.release ();
11637 if (side_effects)
11638 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11639 first = c_fully_fold (first, false, NULL);
11640 OMP_CLAUSE_DECL (c) = first;
11641 if (size)
11642 size = c_fully_fold (size, false, NULL);
11643 OMP_CLAUSE_SIZE (c) = size;
11644 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11645 return false;
11646 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11647 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11648 if (!c_mark_addressable (t))
11649 return false;
11650 OMP_CLAUSE_DECL (c2) = t;
11651 t = build_fold_addr_expr (first);
11652 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11653 tree ptr = OMP_CLAUSE_DECL (c2);
11654 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11655 ptr = build_fold_addr_expr (ptr);
11656 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11657 ptrdiff_type_node, t,
11658 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11659 ptrdiff_type_node, ptr));
11660 t = c_fully_fold (t, false, NULL);
11661 OMP_CLAUSE_SIZE (c2) = t;
11662 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11663 OMP_CLAUSE_CHAIN (c) = c2;
11664 }
11665 return false;
11666}
11667
11668/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11669 an inline call. But, remap
11670 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11671 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11672
11673static tree
11674c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11675 tree decl, tree placeholder)
11676{
11677 copy_body_data id;
11678 struct pointer_map_t *decl_map = pointer_map_create ();
11679
11680 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
11681 *pointer_map_insert (decl_map, omp_decl2) = decl;
11682 memset (&id, 0, sizeof (id));
11683 id.src_fn = DECL_CONTEXT (omp_decl1);
11684 id.dst_fn = current_function_decl;
11685 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11686 id.decl_map = decl_map;
11687
11688 id.copy_decl = copy_decl_no_change;
11689 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11690 id.transform_new_cfg = true;
11691 id.transform_return_to_modify = false;
11692 id.transform_lang_insert_block = NULL;
11693 id.eh_lp_nr = 0;
11694 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11695 pointer_map_destroy (decl_map);
11696 return stmt;
11697}
11698
11699/* Helper function of c_finish_omp_clauses, called via walk_tree.
11700 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11701
11702static tree
11703c_find_omp_placeholder_r (tree *tp, int *, void *data)
11704{
11705 if (*tp == (tree) data)
11706 return *tp;
11707 return NULL_TREE;
11708}
11709
953ff289
DN
11710/* For all elements of CLAUSES, validate them vs OpenMP constraints.
11711 Remove any elements from the list that are invalid. */
11712
11713tree
11714c_finish_omp_clauses (tree clauses)
11715{
11716 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 11717 bitmap_head aligned_head;
953ff289 11718 tree c, t, *pc = &clauses;
acf0174b
JJ
11719 bool branch_seen = false;
11720 bool copyprivate_seen = false;
11721 tree *nowait_clause = NULL;
953ff289
DN
11722
11723 bitmap_obstack_initialize (NULL);
11724 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11725 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11726 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 11727 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
11728
11729 for (pc = &clauses, c = clauses; c ; c = *pc)
11730 {
11731 bool remove = false;
11732 bool need_complete = false;
11733 bool need_implicitly_determined = false;
11734
aaf46ef9 11735 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
11736 {
11737 case OMP_CLAUSE_SHARED:
953ff289
DN
11738 need_implicitly_determined = true;
11739 goto check_dup_generic;
11740
11741 case OMP_CLAUSE_PRIVATE:
953ff289
DN
11742 need_complete = true;
11743 need_implicitly_determined = true;
11744 goto check_dup_generic;
11745
11746 case OMP_CLAUSE_REDUCTION:
953ff289
DN
11747 need_implicitly_determined = true;
11748 t = OMP_CLAUSE_DECL (c);
acf0174b 11749 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
652fea39
JJ
11750 && (FLOAT_TYPE_P (TREE_TYPE (t))
11751 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
953ff289
DN
11752 {
11753 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11754 const char *r_name = NULL;
11755
11756 switch (r_code)
11757 {
11758 case PLUS_EXPR:
11759 case MULT_EXPR:
11760 case MINUS_EXPR:
652fea39 11761 break;
20906c66 11762 case MIN_EXPR:
652fea39
JJ
11763 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11764 r_name = "min";
11765 break;
20906c66 11766 case MAX_EXPR:
652fea39
JJ
11767 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11768 r_name = "max";
953ff289
DN
11769 break;
11770 case BIT_AND_EXPR:
11771 r_name = "&";
11772 break;
11773 case BIT_XOR_EXPR:
11774 r_name = "^";
11775 break;
11776 case BIT_IOR_EXPR:
11777 r_name = "|";
11778 break;
11779 case TRUTH_ANDIF_EXPR:
652fea39
JJ
11780 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11781 r_name = "&&";
953ff289
DN
11782 break;
11783 case TRUTH_ORIF_EXPR:
652fea39
JJ
11784 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11785 r_name = "||";
953ff289
DN
11786 break;
11787 default:
11788 gcc_unreachable ();
11789 }
11790 if (r_name)
11791 {
c2255bc4
AH
11792 error_at (OMP_CLAUSE_LOCATION (c),
11793 "%qE has invalid type for %<reduction(%s)%>",
11794 t, r_name);
953ff289 11795 remove = true;
ee1d5a02 11796 break;
953ff289
DN
11797 }
11798 }
acf0174b
JJ
11799 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11800 {
11801 error_at (OMP_CLAUSE_LOCATION (c),
11802 "user defined reduction not found for %qD", t);
11803 remove = true;
ee1d5a02 11804 break;
acf0174b
JJ
11805 }
11806 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11807 {
11808 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11809 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11810 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11811 VAR_DECL, NULL_TREE, type);
11812 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11813 DECL_ARTIFICIAL (placeholder) = 1;
11814 DECL_IGNORED_P (placeholder) = 1;
11815 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11816 c_mark_addressable (placeholder);
11817 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11818 c_mark_addressable (OMP_CLAUSE_DECL (c));
11819 OMP_CLAUSE_REDUCTION_MERGE (c)
11820 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11821 TREE_VEC_ELT (list, 0),
11822 TREE_VEC_ELT (list, 1),
11823 OMP_CLAUSE_DECL (c), placeholder);
11824 OMP_CLAUSE_REDUCTION_MERGE (c)
11825 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11826 void_type_node, NULL_TREE,
11827 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11828 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11829 if (TREE_VEC_LENGTH (list) == 6)
11830 {
11831 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11832 c_mark_addressable (OMP_CLAUSE_DECL (c));
11833 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11834 c_mark_addressable (placeholder);
11835 tree init = TREE_VEC_ELT (list, 5);
11836 if (init == error_mark_node)
11837 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11838 OMP_CLAUSE_REDUCTION_INIT (c)
11839 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11840 TREE_VEC_ELT (list, 3),
11841 OMP_CLAUSE_DECL (c), placeholder);
11842 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11843 OMP_CLAUSE_REDUCTION_INIT (c)
11844 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11845 OMP_CLAUSE_REDUCTION_INIT (c));
11846 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11847 c_find_omp_placeholder_r,
11848 placeholder, NULL))
11849 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11850 }
11851 else
11852 {
11853 tree init;
11854 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11855 init = build_constructor (TREE_TYPE (t), NULL);
11856 else
11857 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11858 OMP_CLAUSE_REDUCTION_INIT (c)
11859 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11860 }
11861 OMP_CLAUSE_REDUCTION_INIT (c)
11862 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11863 void_type_node, NULL_TREE,
11864 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11865 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11866 }
953ff289
DN
11867 goto check_dup_generic;
11868
11869 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
11870 copyprivate_seen = true;
11871 if (nowait_clause)
11872 {
11873 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11874 "%<nowait%> clause must not be used together "
11875 "with %<copyprivate%>");
11876 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11877 nowait_clause = NULL;
11878 }
953ff289
DN
11879 goto check_dup_generic;
11880
11881 case OMP_CLAUSE_COPYIN:
953ff289
DN
11882 t = OMP_CLAUSE_DECL (c);
11883 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11884 {
c2255bc4
AH
11885 error_at (OMP_CLAUSE_LOCATION (c),
11886 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 11887 remove = true;
ee1d5a02 11888 break;
953ff289
DN
11889 }
11890 goto check_dup_generic;
11891
acf0174b
JJ
11892 case OMP_CLAUSE_LINEAR:
11893 t = OMP_CLAUSE_DECL (c);
11894 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11895 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11896 {
11897 error_at (OMP_CLAUSE_LOCATION (c),
11898 "linear clause applied to non-integral non-pointer "
11899 "variable with type %qT", TREE_TYPE (t));
11900 remove = true;
11901 break;
11902 }
11903 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11904 {
11905 tree s = OMP_CLAUSE_LINEAR_STEP (c);
11906 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
11907 OMP_CLAUSE_DECL (c), s);
11908 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11909 sizetype, s, OMP_CLAUSE_DECL (c));
11910 if (s == error_mark_node)
11911 s = size_one_node;
11912 OMP_CLAUSE_LINEAR_STEP (c) = s;
11913 }
11914 goto check_dup_generic;
11915
953ff289
DN
11916 check_dup_generic:
11917 t = OMP_CLAUSE_DECL (c);
11918 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11919 {
c2255bc4 11920 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
11921 "%qE is not a variable in clause %qs", t,
11922 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
11923 remove = true;
11924 }
11925 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11926 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
11927 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11928 {
c2255bc4
AH
11929 error_at (OMP_CLAUSE_LOCATION (c),
11930 "%qE appears more than once in data clauses", t);
953ff289
DN
11931 remove = true;
11932 }
11933 else
11934 bitmap_set_bit (&generic_head, DECL_UID (t));
11935 break;
11936
11937 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
11938 t = OMP_CLAUSE_DECL (c);
11939 need_complete = true;
11940 need_implicitly_determined = true;
11941 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11942 {
c2255bc4
AH
11943 error_at (OMP_CLAUSE_LOCATION (c),
11944 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
11945 remove = true;
11946 }
11947 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11948 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
11949 {
c2255bc4
AH
11950 error_at (OMP_CLAUSE_LOCATION (c),
11951 "%qE appears more than once in data clauses", t);
953ff289
DN
11952 remove = true;
11953 }
11954 else
11955 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
11956 break;
11957
11958 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
11959 t = OMP_CLAUSE_DECL (c);
11960 need_complete = true;
11961 need_implicitly_determined = true;
11962 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11963 {
c2255bc4
AH
11964 error_at (OMP_CLAUSE_LOCATION (c),
11965 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
11966 remove = true;
11967 }
11968 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11969 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11970 {
c2255bc4
AH
11971 error_at (OMP_CLAUSE_LOCATION (c),
11972 "%qE appears more than once in data clauses", t);
953ff289
DN
11973 remove = true;
11974 }
11975 else
11976 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
11977 break;
11978
acf0174b
JJ
11979 case OMP_CLAUSE_ALIGNED:
11980 t = OMP_CLAUSE_DECL (c);
11981 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11982 {
11983 error_at (OMP_CLAUSE_LOCATION (c),
11984 "%qE is not a variable in %<aligned%> clause", t);
11985 remove = true;
11986 }
5a9785fb
JJ
11987 else if (!POINTER_TYPE_P (TREE_TYPE (t))
11988 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
11989 {
11990 error_at (OMP_CLAUSE_LOCATION (c),
11991 "%qE in %<aligned%> clause is neither a pointer nor "
11992 "an array", t);
11993 remove = true;
11994 }
acf0174b
JJ
11995 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
11996 {
11997 error_at (OMP_CLAUSE_LOCATION (c),
11998 "%qE appears more than once in %<aligned%> clauses",
11999 t);
12000 remove = true;
12001 }
12002 else
12003 bitmap_set_bit (&aligned_head, DECL_UID (t));
12004 break;
12005
12006 case OMP_CLAUSE_DEPEND:
12007 t = OMP_CLAUSE_DECL (c);
12008 if (TREE_CODE (t) == TREE_LIST)
12009 {
12010 if (handle_omp_array_sections (c))
12011 remove = true;
12012 break;
12013 }
12014 if (t == error_mark_node)
12015 remove = true;
12016 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12017 {
12018 error_at (OMP_CLAUSE_LOCATION (c),
12019 "%qE is not a variable in %<depend%> clause", t);
12020 remove = true;
12021 }
12022 else if (!c_mark_addressable (t))
12023 remove = true;
12024 break;
12025
12026 case OMP_CLAUSE_MAP:
12027 case OMP_CLAUSE_TO:
12028 case OMP_CLAUSE_FROM:
12029 t = OMP_CLAUSE_DECL (c);
12030 if (TREE_CODE (t) == TREE_LIST)
12031 {
12032 if (handle_omp_array_sections (c))
12033 remove = true;
12034 else
12035 {
12036 t = OMP_CLAUSE_DECL (c);
12037 if (!COMPLETE_TYPE_P (TREE_TYPE (t)))
12038 {
12039 error_at (OMP_CLAUSE_LOCATION (c),
12040 "array section does not have mappable type "
12041 "in %qs clause",
12042 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12043 remove = true;
12044 }
12045 }
12046 break;
12047 }
12048 if (t == error_mark_node)
12049 remove = true;
12050 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12051 {
12052 error_at (OMP_CLAUSE_LOCATION (c),
12053 "%qE is not a variable in %qs clause", t,
12054 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12055 remove = true;
12056 }
12057 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12058 {
12059 error_at (OMP_CLAUSE_LOCATION (c),
12060 "%qD is threadprivate variable in %qs clause", t,
12061 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12062 remove = true;
12063 }
12064 else if (!c_mark_addressable (t))
12065 remove = true;
12066 else if (!COMPLETE_TYPE_P (TREE_TYPE (t))
12067 && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12068 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER))
12069 {
12070 error_at (OMP_CLAUSE_LOCATION (c),
12071 "%qD does not have a mappable type in %qs clause", t,
12072 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12073 remove = true;
12074 }
12075 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12076 {
12077 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12078 error ("%qD appears more than once in motion clauses", t);
12079 else
12080 error ("%qD appears more than once in map clauses", t);
12081 remove = true;
12082 }
12083 else
12084 bitmap_set_bit (&generic_head, DECL_UID (t));
12085 break;
12086
12087 case OMP_CLAUSE_UNIFORM:
12088 t = OMP_CLAUSE_DECL (c);
12089 if (TREE_CODE (t) != PARM_DECL)
12090 {
12091 if (DECL_P (t))
12092 error_at (OMP_CLAUSE_LOCATION (c),
12093 "%qD is not an argument in %<uniform%> clause", t);
12094 else
12095 error_at (OMP_CLAUSE_LOCATION (c),
12096 "%qE is not an argument in %<uniform%> clause", t);
12097 remove = true;
ee1d5a02 12098 break;
acf0174b 12099 }
ee1d5a02 12100 goto check_dup_generic;
acf0174b
JJ
12101
12102 case OMP_CLAUSE_NOWAIT:
12103 if (copyprivate_seen)
12104 {
12105 error_at (OMP_CLAUSE_LOCATION (c),
12106 "%<nowait%> clause must not be used together "
12107 "with %<copyprivate%>");
12108 remove = true;
12109 break;
12110 }
12111 nowait_clause = pc;
12112 pc = &OMP_CLAUSE_CHAIN (c);
12113 continue;
12114
953ff289
DN
12115 case OMP_CLAUSE_IF:
12116 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12117 case OMP_CLAUSE_NUM_TEAMS:
12118 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12119 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12120 case OMP_CLAUSE_ORDERED:
12121 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12122 case OMP_CLAUSE_UNTIED:
12123 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12124 case OMP_CLAUSE_FINAL:
12125 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12126 case OMP_CLAUSE_SAFELEN:
12127 case OMP_CLAUSE_SIMDLEN:
12128 case OMP_CLAUSE_DEVICE:
12129 case OMP_CLAUSE_DIST_SCHEDULE:
12130 case OMP_CLAUSE_PARALLEL:
12131 case OMP_CLAUSE_FOR:
12132 case OMP_CLAUSE_SECTIONS:
12133 case OMP_CLAUSE_TASKGROUP:
12134 case OMP_CLAUSE_PROC_BIND:
12135 pc = &OMP_CLAUSE_CHAIN (c);
12136 continue;
12137
12138 case OMP_CLAUSE_INBRANCH:
12139 case OMP_CLAUSE_NOTINBRANCH:
12140 if (branch_seen)
12141 {
12142 error_at (OMP_CLAUSE_LOCATION (c),
12143 "%<inbranch%> clause is incompatible with "
12144 "%<notinbranch%>");
12145 remove = true;
12146 break;
12147 }
12148 branch_seen = true;
953ff289
DN
12149 pc = &OMP_CLAUSE_CHAIN (c);
12150 continue;
12151
12152 default:
12153 gcc_unreachable ();
12154 }
12155
12156 if (!remove)
12157 {
12158 t = OMP_CLAUSE_DECL (c);
12159
12160 if (need_complete)
12161 {
12162 t = require_complete_type (t);
12163 if (t == error_mark_node)
12164 remove = true;
12165 }
12166
12167 if (need_implicitly_determined)
12168 {
12169 const char *share_name = NULL;
12170
12171 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12172 share_name = "threadprivate";
12173 else switch (c_omp_predetermined_sharing (t))
12174 {
12175 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12176 break;
12177 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12178 /* const vars may be specified in firstprivate clause. */
12179 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12180 && TREE_READONLY (t))
12181 break;
953ff289
DN
12182 share_name = "shared";
12183 break;
12184 case OMP_CLAUSE_DEFAULT_PRIVATE:
12185 share_name = "private";
12186 break;
12187 default:
12188 gcc_unreachable ();
12189 }
12190 if (share_name)
12191 {
c2255bc4
AH
12192 error_at (OMP_CLAUSE_LOCATION (c),
12193 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12194 t, share_name,
12195 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12196 remove = true;
12197 }
12198 }
12199 }
12200
12201 if (remove)
12202 *pc = OMP_CLAUSE_CHAIN (c);
12203 else
12204 pc = &OMP_CLAUSE_CHAIN (c);
12205 }
12206
12207 bitmap_obstack_release (NULL);
12208 return clauses;
12209}
9ae165a0 12210
0a35513e
AH
12211/* Create a transaction node. */
12212
12213tree
12214c_finish_transaction (location_t loc, tree block, int flags)
12215{
12216 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12217 if (flags & TM_STMT_ATTR_OUTER)
12218 TRANSACTION_EXPR_OUTER (stmt) = 1;
12219 if (flags & TM_STMT_ATTR_RELAXED)
12220 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12221 return add_stmt (stmt);
12222}
12223
9ae165a0
DG
12224/* Make a variant type in the proper way for C/C++, propagating qualifiers
12225 down to the element type of an array. */
12226
12227tree
12228c_build_qualified_type (tree type, int type_quals)
12229{
12230 if (type == error_mark_node)
12231 return type;
12232
12233 if (TREE_CODE (type) == ARRAY_TYPE)
12234 {
12235 tree t;
12236 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12237 type_quals);
12238
12239 /* See if we already have an identically qualified type. */
12240 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12241 {
12242 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12243 && TYPE_NAME (t) == TYPE_NAME (type)
12244 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12245 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12246 TYPE_ATTRIBUTES (type)))
12247 break;
12248 }
12249 if (!t)
12250 {
12251 tree domain = TYPE_DOMAIN (type);
12252
12253 t = build_variant_type_copy (type);
12254 TREE_TYPE (t) = element_type;
12255
12256 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12257 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12258 SET_TYPE_STRUCTURAL_EQUALITY (t);
12259 else if (TYPE_CANONICAL (element_type) != element_type
12260 || (domain && TYPE_CANONICAL (domain) != domain))
12261 {
b8698a0f 12262 tree unqualified_canon
9ae165a0 12263 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12264 domain? TYPE_CANONICAL (domain)
9ae165a0 12265 : NULL_TREE);
b8698a0f 12266 TYPE_CANONICAL (t)
9ae165a0
DG
12267 = c_build_qualified_type (unqualified_canon, type_quals);
12268 }
12269 else
12270 TYPE_CANONICAL (t) = t;
12271 }
12272 return t;
12273 }
12274
12275 /* A restrict-qualified pointer type must be a pointer to object or
12276 incomplete type. Note that the use of POINTER_TYPE_P also allows
12277 REFERENCE_TYPEs, which is appropriate for C++. */
12278 if ((type_quals & TYPE_QUAL_RESTRICT)
12279 && (!POINTER_TYPE_P (type)
12280 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12281 {
12282 error ("invalid use of %<restrict%>");
12283 type_quals &= ~TYPE_QUAL_RESTRICT;
12284 }
12285
12286 return build_qualified_type (type, type_quals);
12287}
72b5577d
ILT
12288
12289/* Build a VA_ARG_EXPR for the C parser. */
12290
12291tree
c2255bc4 12292c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d
ILT
12293{
12294 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12295 warning_at (loc, OPT_Wc___compat,
12296 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12297 return build_va_arg (loc, expr, type);
72b5577d 12298}
acf0174b
JJ
12299
12300/* Return truthvalue of whether T1 is the same tree structure as T2.
12301 Return 1 if they are the same. Return 0 if they are different. */
12302
12303bool
12304c_tree_equal (tree t1, tree t2)
12305{
12306 enum tree_code code1, code2;
12307
12308 if (t1 == t2)
12309 return true;
12310 if (!t1 || !t2)
12311 return false;
12312
12313 for (code1 = TREE_CODE (t1);
12314 CONVERT_EXPR_CODE_P (code1)
12315 || code1 == NON_LVALUE_EXPR;
12316 code1 = TREE_CODE (t1))
12317 t1 = TREE_OPERAND (t1, 0);
12318 for (code2 = TREE_CODE (t2);
12319 CONVERT_EXPR_CODE_P (code2)
12320 || code2 == NON_LVALUE_EXPR;
12321 code2 = TREE_CODE (t2))
12322 t2 = TREE_OPERAND (t2, 0);
12323
12324 /* They might have become equal now. */
12325 if (t1 == t2)
12326 return true;
12327
12328 if (code1 != code2)
12329 return false;
12330
12331 switch (code1)
12332 {
12333 case INTEGER_CST:
12334 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
12335 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
12336
12337 case REAL_CST:
12338 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12339
12340 case STRING_CST:
12341 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12342 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12343 TREE_STRING_LENGTH (t1));
12344
12345 case FIXED_CST:
12346 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12347 TREE_FIXED_CST (t2));
12348
12349 case COMPLEX_CST:
12350 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12351 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12352
12353 case VECTOR_CST:
12354 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12355
12356 case CONSTRUCTOR:
12357 /* We need to do this when determining whether or not two
12358 non-type pointer to member function template arguments
12359 are the same. */
12360 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12361 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12362 return false;
12363 {
12364 tree field, value;
12365 unsigned int i;
12366 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12367 {
12368 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12369 if (!c_tree_equal (field, elt2->index)
12370 || !c_tree_equal (value, elt2->value))
12371 return false;
12372 }
12373 }
12374 return true;
12375
12376 case TREE_LIST:
12377 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12378 return false;
12379 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12380 return false;
12381 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12382
12383 case SAVE_EXPR:
12384 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12385
12386 case CALL_EXPR:
12387 {
12388 tree arg1, arg2;
12389 call_expr_arg_iterator iter1, iter2;
12390 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12391 return false;
12392 for (arg1 = first_call_expr_arg (t1, &iter1),
12393 arg2 = first_call_expr_arg (t2, &iter2);
12394 arg1 && arg2;
12395 arg1 = next_call_expr_arg (&iter1),
12396 arg2 = next_call_expr_arg (&iter2))
12397 if (!c_tree_equal (arg1, arg2))
12398 return false;
12399 if (arg1 || arg2)
12400 return false;
12401 return true;
12402 }
12403
12404 case TARGET_EXPR:
12405 {
12406 tree o1 = TREE_OPERAND (t1, 0);
12407 tree o2 = TREE_OPERAND (t2, 0);
12408
12409 /* Special case: if either target is an unallocated VAR_DECL,
12410 it means that it's going to be unified with whatever the
12411 TARGET_EXPR is really supposed to initialize, so treat it
12412 as being equivalent to anything. */
12413 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12414 && !DECL_RTL_SET_P (o1))
12415 /*Nop*/;
12416 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12417 && !DECL_RTL_SET_P (o2))
12418 /*Nop*/;
12419 else if (!c_tree_equal (o1, o2))
12420 return false;
12421
12422 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12423 }
12424
12425 case COMPONENT_REF:
12426 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12427 return false;
12428 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12429
12430 case PARM_DECL:
12431 case VAR_DECL:
12432 case CONST_DECL:
12433 case FIELD_DECL:
12434 case FUNCTION_DECL:
12435 case IDENTIFIER_NODE:
12436 case SSA_NAME:
12437 return false;
12438
12439 case TREE_VEC:
12440 {
12441 unsigned ix;
12442 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12443 return false;
12444 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12445 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12446 TREE_VEC_ELT (t2, ix)))
12447 return false;
12448 return true;
12449 }
12450
12451 default:
12452 break;
12453 }
12454
12455 switch (TREE_CODE_CLASS (code1))
12456 {
12457 case tcc_unary:
12458 case tcc_binary:
12459 case tcc_comparison:
12460 case tcc_expression:
12461 case tcc_vl_exp:
12462 case tcc_reference:
12463 case tcc_statement:
12464 {
12465 int i, n = TREE_OPERAND_LENGTH (t1);
12466
12467 switch (code1)
12468 {
12469 case PREINCREMENT_EXPR:
12470 case PREDECREMENT_EXPR:
12471 case POSTINCREMENT_EXPR:
12472 case POSTDECREMENT_EXPR:
12473 n = 1;
12474 break;
12475 case ARRAY_REF:
12476 n = 2;
12477 break;
12478 default:
12479 break;
12480 }
12481
12482 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12483 && n != TREE_OPERAND_LENGTH (t2))
12484 return false;
12485
12486 for (i = 0; i < n; ++i)
12487 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12488 return false;
12489
12490 return true;
12491 }
12492
12493 case tcc_type:
12494 return comptypes (t1, t2);
12495 default:
12496 gcc_unreachable ();
12497 }
12498 /* We can get here with --disable-checking. */
12499 return false;
12500}
12893402
BI
12501
12502/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12503 spawn-helper and BODY is the newly created body for FNDECL. */
12504
12505void
12506cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12507{
12508 tree list = alloc_stmt_list ();
12509 tree frame = make_cilk_frame (fndecl);
12510 tree dtor = create_cilk_function_exit (frame, false, true);
12511 add_local_decl (cfun, frame);
12512
12513 DECL_SAVED_TREE (fndecl) = list;
12514 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12515 frame);
12516 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12517 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12518
12519 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12520 append_to_statement_list (detach_expr, &body_list);
12521
12522 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12523 body = fold_build_cleanup_point_expr (void_type_node, body);
12524
12525 append_to_statement_list (body, &body_list);
12526 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12527 body_list, dtor), &list);
12528}