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