]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
jit: prevent ICE with type mismatch in gcc_jit_block_add_assignment_op
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
5624e564 2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6
ZW
28#include "coretypes.h"
29#include "tm.h"
40e23961
MC
30#include "hash-set.h"
31#include "vec.h"
32#include "symtab.h"
33#include "input.h"
34#include "alias.h"
35#include "double-int.h"
36#include "machmode.h"
37#include "inchash.h"
38#include "real.h"
39#include "fixed-value.h"
400fbf9f 40#include "tree.h"
40e23961 41#include "fold-const.h"
d8a2d370
DN
42#include "stor-layout.h"
43#include "trans-mem.h"
44#include "varasm.h"
45#include "stmt.h"
e57e265b 46#include "langhooks.h"
400fbf9f 47#include "c-tree.h"
29cc57cf 48#include "c-lang.h"
400fbf9f 49#include "flags.h"
ab87f8c8 50#include "intl.h"
672a6f42 51#include "target.h"
325c3691 52#include "tree-iterator.h"
6662d794 53#include "bitmap.h"
60393bbc
AM
54#include "predict.h"
55#include "vec.h"
56#include "hashtab.h"
57#include "hash-set.h"
58#include "machmode.h"
59#include "hard-reg-set.h"
60#include "input.h"
61#include "function.h"
2fb9a547 62#include "gimple-expr.h"
45b0be94 63#include "gimplify.h"
acf0174b 64#include "tree-inline.h"
0645c1a2 65#include "omp-low.h"
61d3ce20 66#include "c-family/c-objc.h"
a212e43f 67#include "c-family/c-common.h"
de5a5fa1 68#include "c-family/c-ubsan.h"
12893402 69#include "cilk.h"
807e902e 70#include "wide-int.h"
325c3691 71
2ac2f164
JM
72/* Possible cases of implicit bad conversions. Used to select
73 diagnostic messages in convert_for_assignment. */
74enum impl_conv {
75 ic_argpass,
76 ic_assign,
77 ic_init,
78 ic_return
79};
80
bc4b653b
JM
81/* The level of nesting inside "__alignof__". */
82int in_alignof;
83
84/* The level of nesting inside "sizeof". */
85int in_sizeof;
86
87/* The level of nesting inside "typeof". */
88int in_typeof;
400fbf9f 89
1a4049e7
JJ
90/* The argument of last parsed sizeof expression, only to be tested
91 if expr.original_code == SIZEOF_EXPR. */
92tree c_last_sizeof_arg;
93
9bac5cbb
G
94/* Nonzero if we might need to print a "missing braces around
95 initializer" message within this initializer. */
96static int found_missing_braces;
103b7b17 97
bf730f15
RS
98static int require_constant_value;
99static int require_constant_elements;
100
58f9752a 101static bool null_pointer_constant_p (const_tree);
f55ade6e 102static tree qualify_type (tree, tree);
dc5027f4
JM
103static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
104 bool *);
744aa42f 105static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
106static int function_types_compatible_p (const_tree, const_tree, bool *,
107 bool *);
108static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 109static tree lookup_field (tree, tree);
81e5eca8
MP
110static int convert_arguments (location_t, vec<location_t>, tree,
111 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
112 tree);
db3927fb 113static tree pointer_diff (location_t, tree, tree);
68fca595 114static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 115 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
116static tree valid_compound_expr_initializer (tree, tree);
117static void push_string (const char *);
118static void push_member_name (tree);
f55ade6e
AJ
119static int spelling_length (void);
120static char *print_spelling (char *);
96b40f8d 121static void warning_init (location_t, int, const char *);
c2255bc4 122static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
123static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
124 bool, struct obstack *);
a1e3b3d9 125static void output_pending_init_elements (int, struct obstack *);
ea58ef42 126static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 127static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
128static void add_pending_init (location_t, tree, tree, tree, bool,
129 struct obstack *);
a1e3b3d9
LB
130static void set_nonincremental_init (struct obstack *);
131static void set_nonincremental_init_from_string (tree, struct obstack *);
132static tree find_init_member (tree, struct obstack *);
f37acdf9 133static void readonly_warning (tree, enum lvalue_use);
7bd11157 134static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 135static void record_maybe_used_decl (tree);
dc5027f4 136static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
137\f
138/* Return true if EXP is a null pointer constant, false otherwise. */
139
140static bool
58f9752a 141null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
142{
143 /* This should really operate on c_expr structures, but they aren't
144 yet available everywhere required. */
145 tree type = TREE_TYPE (expr);
146 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 147 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
148 && integer_zerop (expr)
149 && (INTEGRAL_TYPE_P (type)
150 || (TREE_CODE (type) == POINTER_TYPE
151 && VOID_TYPE_P (TREE_TYPE (type))
152 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
153}
928c19bb
JM
154
155/* EXPR may appear in an unevaluated part of an integer constant
156 expression, but not in an evaluated part. Wrap it in a
157 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
158 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
159
160static tree
161note_integer_operands (tree expr)
162{
163 tree ret;
164 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
165 {
166 ret = copy_node (expr);
167 TREE_OVERFLOW (ret) = 1;
168 }
169 else
170 {
171 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
172 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
173 }
174 return ret;
175}
176
4d84fe7c
JM
177/* Having checked whether EXPR may appear in an unevaluated part of an
178 integer constant expression and found that it may, remove any
179 C_MAYBE_CONST_EXPR noting this fact and return the resulting
180 expression. */
181
182static inline tree
183remove_c_maybe_const_expr (tree expr)
184{
185 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
186 return C_MAYBE_CONST_EXPR_EXPR (expr);
187 else
188 return expr;
189}
190
f13c9b2c
AP
191\f/* This is a cache to hold if two types are compatible or not. */
192
193struct tagged_tu_seen_cache {
194 const struct tagged_tu_seen_cache * next;
58f9752a
KG
195 const_tree t1;
196 const_tree t2;
f13c9b2c
AP
197 /* The return value of tagged_types_tu_compatible_p if we had seen
198 these two types already. */
199 int val;
200};
201
202static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
203static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
204
400fbf9f
JW
205/* Do `exp = require_complete_type (exp);' to make sure exp
206 does not have an incomplete type. (That includes void types.) */
207
208tree
2f6e4e97 209require_complete_type (tree value)
400fbf9f
JW
210{
211 tree type = TREE_TYPE (value);
212
7a0ca710 213 if (error_operand_p (value))
ea0f786b
CB
214 return error_mark_node;
215
400fbf9f 216 /* First, detect a valid value with a complete type. */
d0f062fb 217 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
218 return value;
219
7a228918 220 c_incomplete_type_error (value, type);
400fbf9f
JW
221 return error_mark_node;
222}
223
224/* Print an error message for invalid use of an incomplete type.
225 VALUE is the expression that was used (or 0 if that isn't known)
226 and TYPE is the type that was invalid. */
227
228void
ac7d7749 229c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 230{
5d5993dd 231 const char *type_code_string;
400fbf9f
JW
232
233 /* Avoid duplicate error message. */
234 if (TREE_CODE (type) == ERROR_MARK)
235 return;
236
237 if (value != 0 && (TREE_CODE (value) == VAR_DECL
238 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 239 error ("%qD has an incomplete type", value);
400fbf9f
JW
240 else
241 {
242 retry:
243 /* We must print an error message. Be clever about what it says. */
244
245 switch (TREE_CODE (type))
246 {
247 case RECORD_TYPE:
ab87f8c8 248 type_code_string = "struct";
400fbf9f
JW
249 break;
250
251 case UNION_TYPE:
ab87f8c8 252 type_code_string = "union";
400fbf9f
JW
253 break;
254
255 case ENUMERAL_TYPE:
ab87f8c8 256 type_code_string = "enum";
400fbf9f
JW
257 break;
258
259 case VOID_TYPE:
260 error ("invalid use of void expression");
261 return;
262
263 case ARRAY_TYPE:
264 if (TYPE_DOMAIN (type))
265 {
fba78abb
RH
266 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
267 {
268 error ("invalid use of flexible array member");
269 return;
270 }
400fbf9f
JW
271 type = TREE_TYPE (type);
272 goto retry;
273 }
274 error ("invalid use of array with unspecified bounds");
275 return;
276
277 default:
366de0ce 278 gcc_unreachable ();
400fbf9f
JW
279 }
280
281 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
282 error ("invalid use of undefined type %<%s %E%>",
283 type_code_string, TYPE_NAME (type));
400fbf9f
JW
284 else
285 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 286 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
287 }
288}
289
ab393bf1
NB
290/* Given a type, apply default promotions wrt unnamed function
291 arguments and return the new type. */
292
293tree
2f6e4e97 294c_type_promotes_to (tree type)
ab393bf1 295{
267bac10 296 tree ret = NULL_TREE;
ab393bf1 297
267bac10
JM
298 if (TYPE_MAIN_VARIANT (type) == float_type_node)
299 ret = double_type_node;
300 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
301 {
302 /* Preserve unsignedness if not really getting any wider. */
8df83eae 303 if (TYPE_UNSIGNED (type)
c22cacf3 304 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
305 ret = unsigned_type_node;
306 else
307 ret = integer_type_node;
ab393bf1
NB
308 }
309
267bac10
JM
310 if (ret != NULL_TREE)
311 return (TYPE_ATOMIC (type)
312 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
313 : ret);
314
ab393bf1
NB
315 return type;
316}
317
36c5e70a
BE
318/* Return true if between two named address spaces, whether there is a superset
319 named address space that encompasses both address spaces. If there is a
320 superset, return which address space is the superset. */
321
322static bool
323addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
324{
325 if (as1 == as2)
326 {
327 *common = as1;
328 return true;
329 }
330 else if (targetm.addr_space.subset_p (as1, as2))
331 {
332 *common = as2;
333 return true;
334 }
335 else if (targetm.addr_space.subset_p (as2, as1))
336 {
337 *common = as1;
338 return true;
339 }
340 else
341 return false;
342}
343
400fbf9f
JW
344/* Return a variant of TYPE which has all the type qualifiers of LIKE
345 as well as those of TYPE. */
346
347static tree
2f6e4e97 348qualify_type (tree type, tree like)
400fbf9f 349{
36c5e70a
BE
350 addr_space_t as_type = TYPE_ADDR_SPACE (type);
351 addr_space_t as_like = TYPE_ADDR_SPACE (like);
352 addr_space_t as_common;
353
354 /* If the two named address spaces are different, determine the common
355 superset address space. If there isn't one, raise an error. */
356 if (!addr_space_superset (as_type, as_like, &as_common))
357 {
358 as_common = as_type;
359 error ("%qT and %qT are in disjoint named address spaces",
360 type, like);
361 }
362
2f6e4e97 363 return c_build_qualified_type (type,
36c5e70a 364 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 365 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 366 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 367}
52ffd86e
MS
368
369/* Return true iff the given tree T is a variable length array. */
370
371bool
ac7d7749 372c_vla_type_p (const_tree t)
52ffd86e
MS
373{
374 if (TREE_CODE (t) == ARRAY_TYPE
375 && C_TYPE_VARIABLE_SIZE (t))
376 return true;
377 return false;
378}
400fbf9f 379\f
10bc1b1b 380/* Return the composite type of two compatible types.
5305f6d7 381
10bc1b1b
JM
382 We assume that comptypes has already been done and returned
383 nonzero; if that isn't so, this may crash. In particular, we
384 assume that qualifiers match. */
400fbf9f
JW
385
386tree
10bc1b1b 387composite_type (tree t1, tree t2)
400fbf9f 388{
b3694847
SS
389 enum tree_code code1;
390 enum tree_code code2;
4b027d16 391 tree attributes;
400fbf9f
JW
392
393 /* Save time if the two types are the same. */
394
395 if (t1 == t2) return t1;
396
397 /* If one type is nonsense, use the other. */
398 if (t1 == error_mark_node)
399 return t2;
400 if (t2 == error_mark_node)
401 return t1;
402
10bc1b1b
JM
403 code1 = TREE_CODE (t1);
404 code2 = TREE_CODE (t2);
405
d9525bec 406 /* Merge the attributes. */
5fd9b178 407 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 408
10bc1b1b
JM
409 /* If one is an enumerated type and the other is the compatible
410 integer type, the composite type might be either of the two
411 (DR#013 question 3). For consistency, use the enumerated type as
412 the composite type. */
400fbf9f 413
10bc1b1b
JM
414 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
415 return t1;
416 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
417 return t2;
75326e8c 418
366de0ce 419 gcc_assert (code1 == code2);
b6a10c9f 420
400fbf9f
JW
421 switch (code1)
422 {
400fbf9f 423 case POINTER_TYPE:
10bc1b1b 424 /* For two pointers, do this recursively on the target type. */
400fbf9f 425 {
3932261a
MM
426 tree pointed_to_1 = TREE_TYPE (t1);
427 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 428 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 429 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
430 t1 = build_type_attribute_variant (t1, attributes);
431 return qualify_type (t1, t2);
400fbf9f 432 }
400fbf9f
JW
433
434 case ARRAY_TYPE:
435 {
10bc1b1b 436 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
437 int quals;
438 tree unqual_elt;
ca8bdb78
JM
439 tree d1 = TYPE_DOMAIN (t1);
440 tree d2 = TYPE_DOMAIN (t2);
441 bool d1_variable, d2_variable;
442 bool d1_zero, d2_zero;
f6294de7 443 bool t1_complete, t2_complete;
46df2823 444
de46b2fe 445 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
446 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
447 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 448
f6294de7
JM
449 t1_complete = COMPLETE_TYPE_P (t1);
450 t2_complete = COMPLETE_TYPE_P (t2);
451
ca8bdb78
JM
452 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
453 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
454
455 d1_variable = (!d1_zero
456 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
457 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
458 d2_variable = (!d2_zero
459 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
460 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
461 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
462 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 463
400fbf9f 464 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
465 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
466 && (d2_variable || d2_zero || !d1_variable))
4b027d16 467 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
468 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
469 && (d1_variable || d1_zero || !d2_variable))
4b027d16 470 return build_type_attribute_variant (t2, attributes);
c22cacf3 471
de46b2fe
AP
472 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
473 return build_type_attribute_variant (t1, attributes);
474 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
475 return build_type_attribute_variant (t2, attributes);
c22cacf3 476
46df2823
JM
477 /* Merge the element types, and have a size if either arg has
478 one. We may have qualifiers on the element types. To set
479 up TYPE_MAIN_VARIANT correctly, we need to form the
480 composite of the unqualified types and add the qualifiers
481 back at the end. */
482 quals = TYPE_QUALS (strip_array_types (elt));
483 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
484 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
485 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
486 && (d2_variable
487 || d2_zero
488 || !d1_variable))
489 ? t1
490 : t2));
f6294de7
JM
491 /* Ensure a composite type involving a zero-length array type
492 is a zero-length type not an incomplete type. */
493 if (d1_zero && d2_zero
494 && (t1_complete || t2_complete)
495 && !COMPLETE_TYPE_P (t1))
496 {
497 TYPE_SIZE (t1) = bitsize_zero_node;
498 TYPE_SIZE_UNIT (t1) = size_zero_node;
499 }
46df2823 500 t1 = c_build_qualified_type (t1, quals);
de46b2fe 501 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
502 }
503
fcb99e7b
JJ
504 case ENUMERAL_TYPE:
505 case RECORD_TYPE:
506 case UNION_TYPE:
507 if (attributes != NULL)
508 {
509 /* Try harder not to create a new aggregate type. */
510 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
511 return t1;
512 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
513 return t2;
514 }
515 return build_type_attribute_variant (t1, attributes);
516
400fbf9f
JW
517 case FUNCTION_TYPE:
518 /* Function types: prefer the one that specified arg types.
519 If both do, merge the arg types. Also merge the return types. */
520 {
10bc1b1b 521 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
522 tree p1 = TYPE_ARG_TYPES (t1);
523 tree p2 = TYPE_ARG_TYPES (t2);
524 int len;
525 tree newargs, n;
526 int i;
527
528 /* Save space: see if the result is identical to one of the args. */
3f75a254 529 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 530 return build_type_attribute_variant (t1, attributes);
3f75a254 531 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 532 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
533
534 /* Simple way if one arg fails to specify argument types. */
535 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 536 {
fe7080d2
AP
537 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
538 t1 = build_type_attribute_variant (t1, attributes);
539 return qualify_type (t1, t2);
4b027d16 540 }
400fbf9f 541 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
542 {
543 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
544 t1 = build_type_attribute_variant (t1, attributes);
545 return qualify_type (t1, t2);
4b027d16 546 }
400fbf9f
JW
547
548 /* If both args specify argument types, we must merge the two
549 lists, argument by argument. */
2f4e8f2b 550
400fbf9f
JW
551 len = list_length (p1);
552 newargs = 0;
553
554 for (i = 0; i < len; i++)
8d9bfdc5 555 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
556
557 n = newargs;
558
559 for (; p1;
560 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
561 {
562 /* A null type means arg type is not specified.
563 Take whatever the other function type has. */
564 if (TREE_VALUE (p1) == 0)
565 {
566 TREE_VALUE (n) = TREE_VALUE (p2);
567 goto parm_done;
568 }
569 if (TREE_VALUE (p2) == 0)
570 {
571 TREE_VALUE (n) = TREE_VALUE (p1);
572 goto parm_done;
573 }
2f6e4e97 574
400fbf9f
JW
575 /* Given wait (union {union wait *u; int *i} *)
576 and wait (union wait *),
577 prefer union wait * as type of parm. */
578 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
579 && TREE_VALUE (p1) != TREE_VALUE (p2))
580 {
581 tree memb;
58cb41e6
JJ
582 tree mv2 = TREE_VALUE (p2);
583 if (mv2 && mv2 != error_mark_node
584 && TREE_CODE (mv2) != ARRAY_TYPE)
585 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 586 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 587 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
588 {
589 tree mv3 = TREE_TYPE (memb);
590 if (mv3 && mv3 != error_mark_node
591 && TREE_CODE (mv3) != ARRAY_TYPE)
592 mv3 = TYPE_MAIN_VARIANT (mv3);
593 if (comptypes (mv3, mv2))
594 {
595 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
596 TREE_VALUE (p2));
c1771a20 597 pedwarn (input_location, OPT_Wpedantic,
fcf73884 598 "function types not truly compatible in ISO C");
58cb41e6
JJ
599 goto parm_done;
600 }
601 }
400fbf9f
JW
602 }
603 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
604 && TREE_VALUE (p2) != TREE_VALUE (p1))
605 {
606 tree memb;
58cb41e6
JJ
607 tree mv1 = TREE_VALUE (p1);
608 if (mv1 && mv1 != error_mark_node
609 && TREE_CODE (mv1) != ARRAY_TYPE)
610 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 611 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 612 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
613 {
614 tree mv3 = TREE_TYPE (memb);
615 if (mv3 && mv3 != error_mark_node
616 && TREE_CODE (mv3) != ARRAY_TYPE)
617 mv3 = TYPE_MAIN_VARIANT (mv3);
618 if (comptypes (mv3, mv1))
619 {
620 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
621 TREE_VALUE (p1));
c1771a20 622 pedwarn (input_location, OPT_Wpedantic,
fcf73884 623 "function types not truly compatible in ISO C");
58cb41e6
JJ
624 goto parm_done;
625 }
626 }
400fbf9f 627 }
10bc1b1b 628 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
629 parm_done: ;
630 }
631
4b027d16 632 t1 = build_function_type (valtype, newargs);
fe7080d2 633 t1 = qualify_type (t1, t2);
0f41302f 634 /* ... falls through ... */
400fbf9f
JW
635 }
636
637 default:
4b027d16 638 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
639 }
640
641}
10bc1b1b
JM
642
643/* Return the type of a conditional expression between pointers to
644 possibly differently qualified versions of compatible types.
645
646 We assume that comp_target_types has already been done and returned
647 nonzero; if that isn't so, this may crash. */
648
649static tree
650common_pointer_type (tree t1, tree t2)
651{
652 tree attributes;
46df2823
JM
653 tree pointed_to_1, mv1;
654 tree pointed_to_2, mv2;
10bc1b1b 655 tree target;
eb1387a0 656 unsigned target_quals;
36c5e70a
BE
657 addr_space_t as1, as2, as_common;
658 int quals1, quals2;
10bc1b1b
JM
659
660 /* Save time if the two types are the same. */
661
662 if (t1 == t2) return t1;
663
664 /* If one type is nonsense, use the other. */
665 if (t1 == error_mark_node)
666 return t2;
667 if (t2 == error_mark_node)
668 return t1;
669
366de0ce 670 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 671 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
672
673 /* Merge the attributes. */
674 attributes = targetm.merge_type_attributes (t1, t2);
675
676 /* Find the composite type of the target types, and combine the
46df2823
JM
677 qualifiers of the two types' targets. Do not lose qualifiers on
678 array element types by taking the TYPE_MAIN_VARIANT. */
679 mv1 = pointed_to_1 = TREE_TYPE (t1);
680 mv2 = pointed_to_2 = TREE_TYPE (t2);
681 if (TREE_CODE (mv1) != ARRAY_TYPE)
682 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
683 if (TREE_CODE (mv2) != ARRAY_TYPE)
684 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
685 target = composite_type (mv1, mv2);
eb1387a0 686
768952be
MU
687 /* Strip array types to get correct qualifier for pointers to arrays */
688 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
689 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
690
eb1387a0
RG
691 /* For function types do not merge const qualifiers, but drop them
692 if used inconsistently. The middle-end uses these to mark const
693 and noreturn functions. */
694 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 695 target_quals = (quals1 & quals2);
eb1387a0 696 else
36c5e70a
BE
697 target_quals = (quals1 | quals2);
698
699 /* If the two named address spaces are different, determine the common
700 superset address space. This is guaranteed to exist due to the
701 assumption that comp_target_type returned non-zero. */
702 as1 = TYPE_ADDR_SPACE (pointed_to_1);
703 as2 = TYPE_ADDR_SPACE (pointed_to_2);
704 if (!addr_space_superset (as1, as2, &as_common))
705 gcc_unreachable ();
706
707 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
708
eb1387a0 709 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
710 return build_type_attribute_variant (t1, attributes);
711}
712
713/* Return the common type for two arithmetic types under the usual
714 arithmetic conversions. The default conversions have already been
715 applied, and enumerated types converted to their compatible integer
716 types. The resulting type is unqualified and has no attributes.
717
718 This is the type for the result of most arithmetic operations
719 if the operands have the given two types. */
720
ccf7f880
JJ
721static tree
722c_common_type (tree t1, tree t2)
10bc1b1b
JM
723{
724 enum tree_code code1;
725 enum tree_code code2;
726
727 /* If one type is nonsense, use the other. */
728 if (t1 == error_mark_node)
729 return t2;
730 if (t2 == error_mark_node)
731 return t1;
732
733 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
734 t1 = TYPE_MAIN_VARIANT (t1);
735
736 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
737 t2 = TYPE_MAIN_VARIANT (t2);
738
739 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
740 t1 = build_type_attribute_variant (t1, NULL_TREE);
741
742 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
743 t2 = build_type_attribute_variant (t2, NULL_TREE);
744
745 /* Save time if the two types are the same. */
746
747 if (t1 == t2) return t1;
748
749 code1 = TREE_CODE (t1);
750 code2 = TREE_CODE (t2);
751
366de0ce 752 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
753 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
754 || code1 == INTEGER_TYPE);
366de0ce 755 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
756 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
757 || code2 == INTEGER_TYPE);
10bc1b1b 758
5fc89bfd
JJ
759 /* When one operand is a decimal float type, the other operand cannot be
760 a generic float type or a complex type. We also disallow vector types
761 here. */
762 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
763 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
764 {
765 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
766 {
767 error ("can%'t mix operands of decimal float and vector types");
768 return error_mark_node;
769 }
770 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
771 {
772 error ("can%'t mix operands of decimal float and complex types");
773 return error_mark_node;
774 }
775 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
776 {
777 error ("can%'t mix operands of decimal float and other float types");
778 return error_mark_node;
779 }
780 }
781
10bc1b1b
JM
782 /* If one type is a vector type, return that type. (How the usual
783 arithmetic conversions apply to the vector types extension is not
784 precisely specified.) */
785 if (code1 == VECTOR_TYPE)
786 return t1;
787
788 if (code2 == VECTOR_TYPE)
789 return t2;
790
791 /* If one type is complex, form the common type of the non-complex
792 components, then make that complex. Use T1 or T2 if it is the
793 required type. */
794 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
795 {
796 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
797 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 798 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
799
800 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
801 return t1;
802 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
803 return t2;
804 else
805 return build_complex_type (subtype);
806 }
807
808 /* If only one is real, use it as the result. */
809
810 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
811 return t1;
812
813 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
814 return t2;
815
9a8ce21f
JG
816 /* If both are real and either are decimal floating point types, use
817 the decimal floating point type with the greater precision. */
818
819 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
820 {
821 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
822 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
823 return dfloat128_type_node;
824 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
825 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
826 return dfloat64_type_node;
827 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
828 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
829 return dfloat32_type_node;
830 }
831
ab22c1fa
CF
832 /* Deal with fixed-point types. */
833 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
834 {
835 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 836 machine_mode m1, m2;
ab22c1fa
CF
837 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
838
839 m1 = TYPE_MODE (t1);
840 m2 = TYPE_MODE (t2);
841
842 /* If one input type is saturating, the result type is saturating. */
843 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
844 satp = 1;
845
846 /* If both fixed-point types are unsigned, the result type is unsigned.
847 When mixing fixed-point and integer types, follow the sign of the
848 fixed-point type.
849 Otherwise, the result type is signed. */
850 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
851 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
852 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
853 && TYPE_UNSIGNED (t1))
854 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
855 && TYPE_UNSIGNED (t2)))
856 unsignedp = 1;
857
858 /* The result type is signed. */
859 if (unsignedp == 0)
860 {
861 /* If the input type is unsigned, we need to convert to the
862 signed type. */
863 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
864 {
d75d71e0 865 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
866 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
867 mclass = MODE_FRACT;
868 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
869 mclass = MODE_ACCUM;
870 else
871 gcc_unreachable ();
872 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
873 }
874 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
875 {
d75d71e0 876 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
877 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
878 mclass = MODE_FRACT;
879 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
880 mclass = MODE_ACCUM;
881 else
882 gcc_unreachable ();
883 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
884 }
885 }
886
887 if (code1 == FIXED_POINT_TYPE)
888 {
889 fbit1 = GET_MODE_FBIT (m1);
890 ibit1 = GET_MODE_IBIT (m1);
891 }
892 else
893 {
894 fbit1 = 0;
895 /* Signed integers need to subtract one sign bit. */
896 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
897 }
898
899 if (code2 == FIXED_POINT_TYPE)
900 {
901 fbit2 = GET_MODE_FBIT (m2);
902 ibit2 = GET_MODE_IBIT (m2);
903 }
904 else
905 {
906 fbit2 = 0;
907 /* Signed integers need to subtract one sign bit. */
908 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
909 }
910
911 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
912 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
913 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
914 satp);
915 }
916
10bc1b1b
JM
917 /* Both real or both integers; use the one with greater precision. */
918
919 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
920 return t1;
921 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
922 return t2;
923
924 /* Same precision. Prefer long longs to longs to ints when the
925 same precision, following the C99 rules on integer type rank
926 (which are equivalent to the C90 rules for C90 types). */
927
928 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
929 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
930 return long_long_unsigned_type_node;
931
932 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
933 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
934 {
935 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
936 return long_long_unsigned_type_node;
937 else
c22cacf3 938 return long_long_integer_type_node;
10bc1b1b
JM
939 }
940
941 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
942 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
943 return long_unsigned_type_node;
944
945 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
946 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
947 {
948 /* But preserve unsignedness from the other type,
949 since long cannot hold all the values of an unsigned int. */
950 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
951 return long_unsigned_type_node;
952 else
953 return long_integer_type_node;
954 }
955
956 /* Likewise, prefer long double to double even if same size. */
957 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
958 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
959 return long_double_type_node;
960
2531a1d9
JR
961 /* Likewise, prefer double to float even if same size.
962 We got a couple of embedded targets with 32 bit doubles, and the
963 pdp11 might have 64 bit floats. */
964 if (TYPE_MAIN_VARIANT (t1) == double_type_node
965 || TYPE_MAIN_VARIANT (t2) == double_type_node)
966 return double_type_node;
967
10bc1b1b
JM
968 /* Otherwise prefer the unsigned one. */
969
970 if (TYPE_UNSIGNED (t1))
971 return t1;
972 else
973 return t2;
974}
400fbf9f 975\f
5922c215
JM
976/* Wrapper around c_common_type that is used by c-common.c and other
977 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
978 are allowed here and are converted to their compatible integer types.
979 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
980 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
981tree
982common_type (tree t1, tree t2)
983{
984 if (TREE_CODE (t1) == ENUMERAL_TYPE)
985 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
986 if (TREE_CODE (t2) == ENUMERAL_TYPE)
987 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
988
989 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
990 if (TREE_CODE (t1) == BOOLEAN_TYPE
991 && TREE_CODE (t2) == BOOLEAN_TYPE)
992 return boolean_type_node;
993
994 /* If either type is BOOLEAN_TYPE, then return the other. */
995 if (TREE_CODE (t1) == BOOLEAN_TYPE)
996 return t2;
997 if (TREE_CODE (t2) == BOOLEAN_TYPE)
998 return t1;
999
ccf7f880
JJ
1000 return c_common_type (t1, t2);
1001}
f13c9b2c 1002
400fbf9f
JW
1003/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1004 or various other operations. Return 2 if they are compatible
1005 but a warning may be needed if you use them together. */
1006
1007int
132da1a5 1008comptypes (tree type1, tree type2)
f13c9b2c
AP
1009{
1010 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1011 int val;
1012
dc5027f4 1013 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
1014 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1015
1016 return val;
1017}
1018
1019/* Like comptypes, but if it returns non-zero because enum and int are
1020 compatible, it sets *ENUM_AND_INT_P to true. */
1021
1022static int
1023comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1024{
1025 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1026 int val;
1027
dc5027f4
JM
1028 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1029 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1030
1031 return val;
1032}
1033
1034/* Like comptypes, but if it returns nonzero for different types, it
1035 sets *DIFFERENT_TYPES_P to true. */
1036
1037int
1038comptypes_check_different_types (tree type1, tree type2,
1039 bool *different_types_p)
1040{
1041 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1042 int val;
1043
1044 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1045 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1046
f13c9b2c 1047 return val;
c22cacf3
MS
1048}
1049\f
f13c9b2c
AP
1050/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1051 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1052 but a warning may be needed if you use them together. If
1053 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1054 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1055 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1056 NULL, and the types are compatible but different enough not to be
48b0b196 1057 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1058 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1059 false, but may or may not be set if the types are incompatible.
1060 This differs from comptypes, in that we don't free the seen
1061 types. */
f13c9b2c
AP
1062
1063static int
dc5027f4
JM
1064comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1065 bool *different_types_p)
400fbf9f 1066{
58f9752a
KG
1067 const_tree t1 = type1;
1068 const_tree t2 = type2;
4b027d16 1069 int attrval, val;
400fbf9f
JW
1070
1071 /* Suppress errors caused by previously reported errors. */
1072
8d47dfc5
RH
1073 if (t1 == t2 || !t1 || !t2
1074 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1075 return 1;
1076
bca63328
JM
1077 /* Enumerated types are compatible with integer types, but this is
1078 not transitive: two enumerated types in the same translation unit
1079 are compatible with each other only if they are the same type. */
400fbf9f 1080
bca63328 1081 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1082 {
1083 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1084 if (TREE_CODE (t2) != VOID_TYPE)
1085 {
1086 if (enum_and_int_p != NULL)
1087 *enum_and_int_p = true;
1088 if (different_types_p != NULL)
1089 *different_types_p = true;
1090 }
744aa42f 1091 }
bca63328 1092 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1093 {
1094 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1095 if (TREE_CODE (t1) != VOID_TYPE)
1096 {
1097 if (enum_and_int_p != NULL)
1098 *enum_and_int_p = true;
1099 if (different_types_p != NULL)
1100 *different_types_p = true;
1101 }
744aa42f 1102 }
400fbf9f
JW
1103
1104 if (t1 == t2)
1105 return 1;
1106
1107 /* Different classes of types can't be compatible. */
1108
3aeb3655
EC
1109 if (TREE_CODE (t1) != TREE_CODE (t2))
1110 return 0;
400fbf9f 1111
118a3a8b 1112 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1113
3932261a 1114 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1115 return 0;
1116
08632da2
RS
1117 /* Allow for two different type nodes which have essentially the same
1118 definition. Note that we already checked for equality of the type
38e01259 1119 qualifiers (just above). */
400fbf9f 1120
46df2823
JM
1121 if (TREE_CODE (t1) != ARRAY_TYPE
1122 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1123 return 1;
1124
4b027d16 1125 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1126 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1127 return 0;
1128
1129 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1130 val = 0;
1131
400fbf9f
JW
1132 switch (TREE_CODE (t1))
1133 {
1134 case POINTER_TYPE:
106f5de5
UW
1135 /* Do not remove mode or aliasing information. */
1136 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1137 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1138 break;
4b027d16 1139 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1140 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1141 enum_and_int_p, different_types_p));
4b027d16 1142 break;
400fbf9f
JW
1143
1144 case FUNCTION_TYPE:
dc5027f4
JM
1145 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1146 different_types_p);
4b027d16 1147 break;
400fbf9f
JW
1148
1149 case ARRAY_TYPE:
1150 {
400fbf9f
JW
1151 tree d1 = TYPE_DOMAIN (t1);
1152 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1153 bool d1_variable, d2_variable;
1154 bool d1_zero, d2_zero;
4b027d16 1155 val = 1;
400fbf9f
JW
1156
1157 /* Target types must match incl. qualifiers. */
1158 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1159 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1160 enum_and_int_p,
1161 different_types_p)))
400fbf9f
JW
1162 return 0;
1163
dc5027f4
JM
1164 if (different_types_p != NULL
1165 && (d1 == 0) != (d2 == 0))
1166 *different_types_p = true;
400fbf9f 1167 /* Sizes must match unless one is missing or variable. */
3f85558f 1168 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1169 break;
400fbf9f 1170
3f75a254
JM
1171 d1_zero = !TYPE_MAX_VALUE (d1);
1172 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1173
3f75a254 1174 d1_variable = (!d1_zero
3f85558f
RH
1175 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1176 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1177 d2_variable = (!d2_zero
3f85558f
RH
1178 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1179 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1180 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1181 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1182
dc5027f4
JM
1183 if (different_types_p != NULL
1184 && d1_variable != d2_variable)
1185 *different_types_p = true;
3f85558f
RH
1186 if (d1_variable || d2_variable)
1187 break;
1188 if (d1_zero && d2_zero)
1189 break;
1190 if (d1_zero || d2_zero
3f75a254
JM
1191 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1192 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1193 val = 0;
1194
c22cacf3 1195 break;
400fbf9f
JW
1196 }
1197
d1bd0ded 1198 case ENUMERAL_TYPE:
58393038 1199 case RECORD_TYPE:
d1bd0ded 1200 case UNION_TYPE:
766beae1 1201 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1202 {
fcb99e7b
JJ
1203 tree a1 = TYPE_ATTRIBUTES (t1);
1204 tree a2 = TYPE_ATTRIBUTES (t2);
1205
1206 if (! attribute_list_contained (a1, a2)
1207 && ! attribute_list_contained (a2, a1))
1208 break;
1209
f13c9b2c 1210 if (attrval != 2)
dc5027f4
JM
1211 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1212 different_types_p);
1213 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1214 different_types_p);
f13c9b2c 1215 }
4b027d16 1216 break;
e9a25f70 1217
62e1dfcf 1218 case VECTOR_TYPE:
744aa42f
ILT
1219 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1220 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1221 enum_and_int_p, different_types_p));
62e1dfcf
NC
1222 break;
1223
e9a25f70
JL
1224 default:
1225 break;
400fbf9f 1226 }
4b027d16 1227 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1228}
1229
36c5e70a
BE
1230/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1231 their qualifiers, except for named address spaces. If the pointers point to
1232 different named addresses, then we must determine if one address space is a
1233 subset of the other. */
400fbf9f
JW
1234
1235static int
744aa42f 1236comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1237{
392202b0 1238 int val;
768952be 1239 int val_ped;
36c5e70a
BE
1240 tree mvl = TREE_TYPE (ttl);
1241 tree mvr = TREE_TYPE (ttr);
1242 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1243 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1244 addr_space_t as_common;
744aa42f 1245 bool enum_and_int_p;
8b40563c 1246
36c5e70a
BE
1247 /* Fail if pointers point to incompatible address spaces. */
1248 if (!addr_space_superset (asl, asr, &as_common))
1249 return 0;
1250
768952be
MU
1251 /* For pedantic record result of comptypes on arrays before losing
1252 qualifiers on the element type below. */
1253 val_ped = 1;
1254
1255 if (TREE_CODE (mvl) == ARRAY_TYPE
1256 && TREE_CODE (mvr) == ARRAY_TYPE)
1257 val_ped = comptypes (mvl, mvr);
1258
1259 /* Qualifiers on element types of array types that are
1260 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1261
1262 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1263 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1264 : TYPE_MAIN_VARIANT (mvl));
1265
1266 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1267 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1268 : TYPE_MAIN_VARIANT (mvr));
1269
744aa42f
ILT
1270 enum_and_int_p = false;
1271 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1272
768952be
MU
1273 if (val == 1 && val_ped != 1)
1274 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1275 "are incompatible in ISO C");
1276
fcf73884 1277 if (val == 2)
c1771a20 1278 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1279
1280 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1281 warning_at (location, OPT_Wc___compat,
1282 "pointer target types incompatible in C++");
1283
400fbf9f
JW
1284 return val;
1285}
1286\f
1287/* Subroutines of `comptypes'. */
1288
f75fbaf7
ZW
1289/* Determine whether two trees derive from the same translation unit.
1290 If the CONTEXT chain ends in a null, that tree's context is still
1291 being parsed, so if two trees have context chains ending in null,
766beae1 1292 they're in the same translation unit. */
f75fbaf7 1293int
58f9752a 1294same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1295{
1296 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1297 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1298 {
6615c446
JO
1299 case tcc_declaration:
1300 t1 = DECL_CONTEXT (t1); break;
1301 case tcc_type:
1302 t1 = TYPE_CONTEXT (t1); break;
1303 case tcc_exceptional:
1304 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1305 default: gcc_unreachable ();
766beae1
ZW
1306 }
1307
1308 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1309 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1310 {
6615c446
JO
1311 case tcc_declaration:
1312 t2 = DECL_CONTEXT (t2); break;
1313 case tcc_type:
1314 t2 = TYPE_CONTEXT (t2); break;
1315 case tcc_exceptional:
1316 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1317 default: gcc_unreachable ();
766beae1
ZW
1318 }
1319
1320 return t1 == t2;
1321}
1322
f13c9b2c 1323/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1324
f13c9b2c 1325static struct tagged_tu_seen_cache *
58f9752a 1326alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1327{
cceb1885 1328 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1329 tu->next = tagged_tu_seen_base;
1330 tu->t1 = t1;
1331 tu->t2 = t2;
c22cacf3 1332
f13c9b2c 1333 tagged_tu_seen_base = tu;
c22cacf3 1334
f13c9b2c
AP
1335 /* The C standard says that two structures in different translation
1336 units are compatible with each other only if the types of their
1337 fields are compatible (among other things). We assume that they
1338 are compatible until proven otherwise when building the cache.
1339 An example where this can occur is:
1340 struct a
1341 {
1342 struct a *next;
1343 };
1344 If we are comparing this against a similar struct in another TU,
c83eecad 1345 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1346 loop. */
1347 tu->val = 1;
1348 return tu;
1349}
d1bd0ded 1350
f13c9b2c 1351/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1352
f13c9b2c
AP
1353static void
1354free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1355{
1356 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1357 while (tu != tu_til)
1358 {
741ac903
KG
1359 const struct tagged_tu_seen_cache *const tu1
1360 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1361 tu = tu1->next;
b1d5455a 1362 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1363 }
1364 tagged_tu_seen_base = tu_til;
1365}
d1bd0ded
GK
1366
1367/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1368 compatible. If the two types are not the same (which has been
1369 checked earlier), this can only happen when multiple translation
1370 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1371 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1372 comptypes_internal. */
d1bd0ded
GK
1373
1374static int
744aa42f 1375tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1376 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1377{
1378 tree s1, s2;
1379 bool needs_warning = false;
3aeb3655 1380
d1bd0ded
GK
1381 /* We have to verify that the tags of the types are the same. This
1382 is harder than it looks because this may be a typedef, so we have
1383 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1384 typedef...
1385 In the case of compiler-created builtin structs the TYPE_DECL
1386 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1387 while (TYPE_NAME (t1)
1388 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1389 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1390 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1391
dea984dc
ILT
1392 while (TYPE_NAME (t2)
1393 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1394 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1395 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1396
1397 /* C90 didn't have the requirement that the two tags be the same. */
1398 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1399 return 0;
3aeb3655 1400
d1bd0ded
GK
1401 /* C90 didn't say what happened if one or both of the types were
1402 incomplete; we choose to follow C99 rules here, which is that they
1403 are compatible. */
1404 if (TYPE_SIZE (t1) == NULL
1405 || TYPE_SIZE (t2) == NULL)
1406 return 1;
3aeb3655 1407
d1bd0ded 1408 {
f13c9b2c 1409 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1410 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1411 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1412 return tts_i->val;
d1bd0ded 1413 }
3aeb3655 1414
d1bd0ded
GK
1415 switch (TREE_CODE (t1))
1416 {
1417 case ENUMERAL_TYPE:
1418 {
f13c9b2c 1419 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1420 /* Speed up the case where the type values are in the same order. */
1421 tree tv1 = TYPE_VALUES (t1);
1422 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1423
c22cacf3 1424 if (tv1 == tv2)
f13c9b2c
AP
1425 {
1426 return 1;
1427 }
3aeb3655 1428
c22cacf3
MS
1429 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1430 {
1431 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1432 break;
1433 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1434 {
c22cacf3 1435 tu->val = 0;
f13c9b2c
AP
1436 return 0;
1437 }
c22cacf3 1438 }
3aeb3655 1439
c22cacf3 1440 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1441 {
1442 return 1;
1443 }
c22cacf3 1444 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1445 {
1446 tu->val = 0;
1447 return 0;
1448 }
3aeb3655 1449
d1bd0ded 1450 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1451 {
1452 tu->val = 0;
1453 return 0;
1454 }
3aeb3655 1455
d1bd0ded
GK
1456 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1457 {
1458 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1459 if (s2 == NULL
1460 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1461 {
1462 tu->val = 0;
1463 return 0;
1464 }
d1bd0ded
GK
1465 }
1466 return 1;
1467 }
1468
1469 case UNION_TYPE:
1470 {
f13c9b2c 1471 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1472 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1473 {
1474 tu->val = 0;
1475 return 0;
1476 }
c22cacf3 1477
f13c9b2c
AP
1478 /* Speed up the common case where the fields are in the same order. */
1479 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1480 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1481 {
1482 int result;
c22cacf3 1483
3ae4d3cc 1484 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1485 break;
744aa42f 1486 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1487 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1488
1489 if (result != 1 && !DECL_NAME (s1))
1490 break;
f13c9b2c
AP
1491 if (result == 0)
1492 {
1493 tu->val = 0;
1494 return 0;
1495 }
1496 if (result == 2)
1497 needs_warning = true;
1498
1499 if (TREE_CODE (s1) == FIELD_DECL
1500 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1501 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1502 {
1503 tu->val = 0;
1504 return 0;
1505 }
1506 }
1507 if (!s1 && !s2)
1508 {
1509 tu->val = needs_warning ? 2 : 1;
1510 return tu->val;
1511 }
d1bd0ded 1512
910ad8de 1513 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1514 {
1515 bool ok = false;
3aeb3655 1516
910ad8de 1517 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1518 if (DECL_NAME (s1) == DECL_NAME (s2))
1519 {
1520 int result;
1521
744aa42f 1522 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1523 enum_and_int_p,
1524 different_types_p);
3ae4d3cc
AO
1525
1526 if (result != 1 && !DECL_NAME (s1))
1527 continue;
1528 if (result == 0)
1529 {
1530 tu->val = 0;
1531 return 0;
1532 }
1533 if (result == 2)
1534 needs_warning = true;
1535
1536 if (TREE_CODE (s1) == FIELD_DECL
1537 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1538 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1539 break;
3ae4d3cc
AO
1540
1541 ok = true;
1542 break;
1543 }
3f75a254 1544 if (!ok)
f13c9b2c
AP
1545 {
1546 tu->val = 0;
1547 return 0;
1548 }
d1bd0ded 1549 }
f13c9b2c
AP
1550 tu->val = needs_warning ? 2 : 10;
1551 return tu->val;
d1bd0ded
GK
1552 }
1553
1554 case RECORD_TYPE:
1555 {
c22cacf3 1556 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1557
1558 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1559 s1 && s2;
910ad8de 1560 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1561 {
1562 int result;
1563 if (TREE_CODE (s1) != TREE_CODE (s2)
1564 || DECL_NAME (s1) != DECL_NAME (s2))
1565 break;
744aa42f 1566 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1567 enum_and_int_p, different_types_p);
d1bd0ded
GK
1568 if (result == 0)
1569 break;
1570 if (result == 2)
1571 needs_warning = true;
3aeb3655 1572
d1bd0ded
GK
1573 if (TREE_CODE (s1) == FIELD_DECL
1574 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1575 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1576 break;
1577 }
d1bd0ded 1578 if (s1 && s2)
f13c9b2c
AP
1579 tu->val = 0;
1580 else
1581 tu->val = needs_warning ? 2 : 1;
1582 return tu->val;
d1bd0ded
GK
1583 }
1584
1585 default:
366de0ce 1586 gcc_unreachable ();
d1bd0ded
GK
1587 }
1588}
1589
400fbf9f
JW
1590/* Return 1 if two function types F1 and F2 are compatible.
1591 If either type specifies no argument types,
1592 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1593 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1594 the other must specify that number of self-promoting arg types.
744aa42f 1595 Otherwise, the argument types must match.
dc5027f4 1596 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1597
1598static int
744aa42f 1599function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1600 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1601{
1602 tree args1, args2;
1603 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1604 int val = 1;
1605 int val1;
a6fdc086
GK
1606 tree ret1, ret2;
1607
1608 ret1 = TREE_TYPE (f1);
1609 ret2 = TREE_TYPE (f2);
1610
e508a019
JM
1611 /* 'volatile' qualifiers on a function's return type used to mean
1612 the function is noreturn. */
1613 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1614 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1615 if (TYPE_VOLATILE (ret1))
1616 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1617 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1618 if (TYPE_VOLATILE (ret2))
1619 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1620 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1621 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1622 if (val == 0)
400fbf9f
JW
1623 return 0;
1624
1625 args1 = TYPE_ARG_TYPES (f1);
1626 args2 = TYPE_ARG_TYPES (f2);
1627
dc5027f4
JM
1628 if (different_types_p != NULL
1629 && (args1 == 0) != (args2 == 0))
1630 *different_types_p = true;
1631
400fbf9f
JW
1632 /* An unspecified parmlist matches any specified parmlist
1633 whose argument types don't need default promotions. */
1634
1635 if (args1 == 0)
1636 {
1637 if (!self_promoting_args_p (args2))
1638 return 0;
1639 /* If one of these types comes from a non-prototype fn definition,
1640 compare that with the other type's arglist.
3176a0c2 1641 If they don't match, ask for a warning (but no error). */
400fbf9f 1642 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1643 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1644 enum_and_int_p, different_types_p))
400fbf9f
JW
1645 val = 2;
1646 return val;
1647 }
1648 if (args2 == 0)
1649 {
1650 if (!self_promoting_args_p (args1))
1651 return 0;
1652 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1653 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1654 enum_and_int_p, different_types_p))
400fbf9f
JW
1655 val = 2;
1656 return val;
1657 }
1658
1659 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1660 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1661 different_types_p);
400fbf9f
JW
1662 return val1 != 1 ? val1 : val;
1663}
1664
744aa42f
ILT
1665/* Check two lists of types for compatibility, returning 0 for
1666 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1667 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1668 comptypes_internal. */
400fbf9f
JW
1669
1670static int
744aa42f 1671type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1672 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1673{
1674 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1675 int val = 1;
9d5f3e49 1676 int newval = 0;
400fbf9f
JW
1677
1678 while (1)
1679 {
46df2823 1680 tree a1, mv1, a2, mv2;
400fbf9f
JW
1681 if (args1 == 0 && args2 == 0)
1682 return val;
1683 /* If one list is shorter than the other,
1684 they fail to match. */
1685 if (args1 == 0 || args2 == 0)
1686 return 0;
46df2823
JM
1687 mv1 = a1 = TREE_VALUE (args1);
1688 mv2 = a2 = TREE_VALUE (args2);
1689 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1690 mv1 = (TYPE_ATOMIC (mv1)
1691 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1692 TYPE_QUAL_ATOMIC)
1693 : TYPE_MAIN_VARIANT (mv1));
46df2823 1694 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1695 mv2 = (TYPE_ATOMIC (mv2)
1696 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1697 TYPE_QUAL_ATOMIC)
1698 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1699 /* A null pointer instead of a type
1700 means there is supposed to be an argument
1701 but nothing is specified about what type it has.
1702 So match anything that self-promotes. */
dc5027f4
JM
1703 if (different_types_p != NULL
1704 && (a1 == 0) != (a2 == 0))
1705 *different_types_p = true;
46df2823 1706 if (a1 == 0)
400fbf9f 1707 {
46df2823 1708 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1709 return 0;
1710 }
46df2823 1711 else if (a2 == 0)
400fbf9f 1712 {
46df2823 1713 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1714 return 0;
1715 }
8f5b6d29 1716 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1717 else if (TREE_CODE (a1) == ERROR_MARK
1718 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1719 ;
dc5027f4
JM
1720 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1721 different_types_p)))
400fbf9f 1722 {
dc5027f4
JM
1723 if (different_types_p != NULL)
1724 *different_types_p = true;
400fbf9f
JW
1725 /* Allow wait (union {union wait *u; int *i} *)
1726 and wait (union wait *) to be compatible. */
46df2823
JM
1727 if (TREE_CODE (a1) == UNION_TYPE
1728 && (TYPE_NAME (a1) == 0
ebf0bf7f 1729 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1730 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1731 && tree_int_cst_equal (TYPE_SIZE (a1),
1732 TYPE_SIZE (a2)))
400fbf9f
JW
1733 {
1734 tree memb;
46df2823 1735 for (memb = TYPE_FIELDS (a1);
910ad8de 1736 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1737 {
1738 tree mv3 = TREE_TYPE (memb);
1739 if (mv3 && mv3 != error_mark_node
1740 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1741 mv3 = (TYPE_ATOMIC (mv3)
1742 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1743 TYPE_QUAL_ATOMIC)
1744 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1745 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1746 different_types_p))
58cb41e6
JJ
1747 break;
1748 }
400fbf9f
JW
1749 if (memb == 0)
1750 return 0;
1751 }
46df2823
JM
1752 else if (TREE_CODE (a2) == UNION_TYPE
1753 && (TYPE_NAME (a2) == 0
ebf0bf7f 1754 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1755 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1756 && tree_int_cst_equal (TYPE_SIZE (a2),
1757 TYPE_SIZE (a1)))
400fbf9f
JW
1758 {
1759 tree memb;
46df2823 1760 for (memb = TYPE_FIELDS (a2);
910ad8de 1761 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1762 {
1763 tree mv3 = TREE_TYPE (memb);
1764 if (mv3 && mv3 != error_mark_node
1765 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1766 mv3 = (TYPE_ATOMIC (mv3)
1767 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1768 TYPE_QUAL_ATOMIC)
1769 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1770 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1771 different_types_p))
58cb41e6
JJ
1772 break;
1773 }
400fbf9f
JW
1774 if (memb == 0)
1775 return 0;
1776 }
1777 else
1778 return 0;
1779 }
1780
1781 /* comptypes said ok, but record if it said to warn. */
1782 if (newval > val)
1783 val = newval;
1784
1785 args1 = TREE_CHAIN (args1);
1786 args2 = TREE_CHAIN (args2);
1787 }
1788}
400fbf9f 1789\f
a0e24419
MP
1790/* Compute the size to increment a pointer by. When a function type or void
1791 type or incomplete type is passed, size_one_node is returned.
1792 This function does not emit any diagnostics; the caller is responsible
1793 for that. */
400fbf9f 1794
4e2fb7de 1795static tree
58f9752a 1796c_size_in_bytes (const_tree type)
400fbf9f
JW
1797{
1798 enum tree_code code = TREE_CODE (type);
1799
a0e24419
MP
1800 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1801 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1802 return size_one_node;
1803
400fbf9f 1804 /* Convert in case a char is more than one unit. */
db3927fb
AH
1805 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1806 size_int (TYPE_PRECISION (char_type_node)
1807 / BITS_PER_UNIT));
400fbf9f 1808}
400fbf9f 1809\f
400fbf9f
JW
1810/* Return either DECL or its known constant value (if it has one). */
1811
56cb9733 1812tree
2f6e4e97 1813decl_constant_value (tree decl)
400fbf9f 1814{
a7c1916a 1815 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1816 in a place where a variable is invalid. Note that DECL_INITIAL
1817 isn't valid for a PARM_DECL. */
a7c1916a 1818 current_function_decl != 0
4f976745 1819 && TREE_CODE (decl) != PARM_DECL
3f75a254 1820 && !TREE_THIS_VOLATILE (decl)
83bab8db 1821 && TREE_READONLY (decl)
400fbf9f
JW
1822 && DECL_INITIAL (decl) != 0
1823 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1824 /* This is invalid if initial value is not constant.
1825 If it has either a function call, a memory reference,
1826 or a variable, then re-evaluating it could give different results. */
1827 && TREE_CONSTANT (DECL_INITIAL (decl))
1828 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1829 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1830 return DECL_INITIAL (decl);
1831 return decl;
1832}
1833
f2a71bbc
JM
1834/* Convert the array expression EXP to a pointer. */
1835static tree
c2255bc4 1836array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1837{
f2a71bbc 1838 tree orig_exp = exp;
207bf485 1839 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1840 tree adr;
1841 tree restype = TREE_TYPE (type);
1842 tree ptrtype;
207bf485 1843
f2a71bbc 1844 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1845
f2a71bbc 1846 STRIP_TYPE_NOPS (exp);
207bf485 1847
487a92fe
JM
1848 if (TREE_NO_WARNING (orig_exp))
1849 TREE_NO_WARNING (exp) = 1;
207bf485 1850
f2a71bbc
JM
1851 ptrtype = build_pointer_type (restype);
1852
1853 if (TREE_CODE (exp) == INDIRECT_REF)
1854 return convert (ptrtype, TREE_OPERAND (exp, 0));
1855
1f37c583
JM
1856 /* In C++ array compound literals are temporary objects unless they are
1857 const or appear in namespace scope, so they are destroyed too soon
1858 to use them for much of anything (c++/53220). */
1859 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1860 {
1861 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1862 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1863 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1864 "converting an array compound literal to a pointer "
1865 "is ill-formed in C++");
1866 }
1867
c2255bc4 1868 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1869 return convert (ptrtype, adr);
1870}
207bf485 1871
f2a71bbc
JM
1872/* Convert the function expression EXP to a pointer. */
1873static tree
c2255bc4 1874function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1875{
1876 tree orig_exp = exp;
207bf485 1877
f2a71bbc 1878 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1879
f2a71bbc 1880 STRIP_TYPE_NOPS (exp);
207bf485 1881
f2a71bbc
JM
1882 if (TREE_NO_WARNING (orig_exp))
1883 TREE_NO_WARNING (exp) = 1;
207bf485 1884
c2255bc4 1885 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1886}
207bf485 1887
ebfbbdc5
JJ
1888/* Mark EXP as read, not just set, for set but not used -Wunused
1889 warning purposes. */
1890
1891void
1892mark_exp_read (tree exp)
1893{
1894 switch (TREE_CODE (exp))
1895 {
1896 case VAR_DECL:
1897 case PARM_DECL:
1898 DECL_READ_P (exp) = 1;
1899 break;
1900 case ARRAY_REF:
1901 case COMPONENT_REF:
1902 case MODIFY_EXPR:
1903 case REALPART_EXPR:
1904 case IMAGPART_EXPR:
1905 CASE_CONVERT:
1906 case ADDR_EXPR:
1907 mark_exp_read (TREE_OPERAND (exp, 0));
1908 break;
1909 case COMPOUND_EXPR:
82c3c067 1910 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1911 mark_exp_read (TREE_OPERAND (exp, 1));
1912 break;
1913 default:
1914 break;
1915 }
1916}
1917
f2a71bbc
JM
1918/* Perform the default conversion of arrays and functions to pointers.
1919 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1920 return EXP.
1921
1922 LOC is the location of the expression. */
f2a71bbc
JM
1923
1924struct c_expr
c2255bc4 1925default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1926{
1927 tree orig_exp = exp.value;
1928 tree type = TREE_TYPE (exp.value);
1929 enum tree_code code = TREE_CODE (type);
1930
1931 switch (code)
1932 {
1933 case ARRAY_TYPE:
1934 {
1935 bool not_lvalue = false;
1936 bool lvalue_array_p;
1937
1938 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1939 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1940 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1941 {
1942 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1943 not_lvalue = true;
1944 exp.value = TREE_OPERAND (exp.value, 0);
1945 }
1946
1947 if (TREE_NO_WARNING (orig_exp))
1948 TREE_NO_WARNING (exp.value) = 1;
1949
1950 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1951 if (!flag_isoc99 && !lvalue_array_p)
1952 {
1953 /* Before C99, non-lvalue arrays do not decay to pointers.
1954 Normally, using such an array would be invalid; but it can
1955 be used correctly inside sizeof or as a statement expression.
1956 Thus, do not give an error here; an error will result later. */
1957 return exp;
1958 }
1959
c2255bc4 1960 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1961 }
1962 break;
1963 case FUNCTION_TYPE:
c2255bc4 1964 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1965 break;
1966 default:
f2a71bbc 1967 break;
207bf485 1968 }
f2a71bbc 1969
207bf485
JM
1970 return exp;
1971}
1972
ebfbbdc5
JJ
1973struct c_expr
1974default_function_array_read_conversion (location_t loc, struct c_expr exp)
1975{
1976 mark_exp_read (exp.value);
1977 return default_function_array_conversion (loc, exp);
1978}
522ddfa2 1979
267bac10
JM
1980/* Return whether EXPR should be treated as an atomic lvalue for the
1981 purposes of load and store handling. */
1982
1983static bool
1984really_atomic_lvalue (tree expr)
1985{
7a0ca710 1986 if (error_operand_p (expr))
267bac10
JM
1987 return false;
1988 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1989 return false;
1990 if (!lvalue_p (expr))
1991 return false;
1992
1993 /* Ignore _Atomic on register variables, since their addresses can't
1994 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1995 sequences wouldn't work. Ignore _Atomic on structures containing
1996 bit-fields, since accessing elements of atomic structures or
1997 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1998 it's undefined at translation time or execution time, and the
1999 normal atomic sequences again wouldn't work. */
2000 while (handled_component_p (expr))
2001 {
2002 if (TREE_CODE (expr) == COMPONENT_REF
2003 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2004 return false;
2005 expr = TREE_OPERAND (expr, 0);
2006 }
2007 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2008 return false;
2009 return true;
2010}
2011
2012/* Convert expression EXP (location LOC) from lvalue to rvalue,
2013 including converting functions and arrays to pointers if CONVERT_P.
2014 If READ_P, also mark the expression as having been read. */
2015
2016struct c_expr
2017convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2018 bool convert_p, bool read_p)
2019{
2020 if (read_p)
2021 mark_exp_read (exp.value);
2022 if (convert_p)
2023 exp = default_function_array_conversion (loc, exp);
2024 if (really_atomic_lvalue (exp.value))
2025 {
2026 vec<tree, va_gc> *params;
2027 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2028 tree expr_type = TREE_TYPE (exp.value);
2029 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2030 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2031
2032 gcc_assert (TYPE_ATOMIC (expr_type));
2033
2034 /* Expansion of a generic atomic load may require an addition
2035 element, so allocate enough to prevent a resize. */
2036 vec_alloc (params, 4);
2037
2038 /* Remove the qualifiers for the rest of the expressions and
2039 create the VAL temp variable to hold the RHS. */
2040 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
b731b390 2041 tmp = create_tmp_var (nonatomic_type);
267bac10
JM
2042 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2043 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2044 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2045
2046 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2047 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2048 params->quick_push (expr_addr);
2049 params->quick_push (tmp_addr);
2050 params->quick_push (seq_cst);
8edbfaa6 2051 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2052
cc28fc7f
MP
2053 /* EXPR is always read. */
2054 mark_exp_read (exp.value);
2055
267bac10
JM
2056 /* Return tmp which contains the value loaded. */
2057 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2058 }
2059 return exp;
2060}
2061
522ddfa2
JM
2062/* EXP is an expression of integer type. Apply the integer promotions
2063 to it and return the promoted value. */
400fbf9f
JW
2064
2065tree
522ddfa2 2066perform_integral_promotions (tree exp)
400fbf9f 2067{
b3694847
SS
2068 tree type = TREE_TYPE (exp);
2069 enum tree_code code = TREE_CODE (type);
400fbf9f 2070
522ddfa2 2071 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2072
400fbf9f
JW
2073 /* Normally convert enums to int,
2074 but convert wide enums to something wider. */
2075 if (code == ENUMERAL_TYPE)
2076 {
b0c48229
NB
2077 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2078 TYPE_PRECISION (integer_type_node)),
2079 ((TYPE_PRECISION (type)
2080 >= TYPE_PRECISION (integer_type_node))
8df83eae 2081 && TYPE_UNSIGNED (type)));
05bccae2 2082
400fbf9f
JW
2083 return convert (type, exp);
2084 }
2085
522ddfa2
JM
2086 /* ??? This should no longer be needed now bit-fields have their
2087 proper types. */
9753f113 2088 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2089 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2090 /* If it's thinner than an int, promote it like a
d72040f5 2091 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2092 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2093 TYPE_PRECISION (integer_type_node)))
f458d1d5 2094 return convert (integer_type_node, exp);
9753f113 2095
d72040f5 2096 if (c_promoting_integer_type_p (type))
400fbf9f 2097 {
f458d1d5 2098 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2099 if (TYPE_UNSIGNED (type)
f458d1d5 2100 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2101 return convert (unsigned_type_node, exp);
05bccae2 2102
400fbf9f
JW
2103 return convert (integer_type_node, exp);
2104 }
05bccae2 2105
522ddfa2
JM
2106 return exp;
2107}
2108
2109
2110/* Perform default promotions for C data used in expressions.
46bdb9cf 2111 Enumeral types or short or char are converted to int.
522ddfa2
JM
2112 In addition, manifest constants symbols are replaced by their values. */
2113
2114tree
2115default_conversion (tree exp)
2116{
2117 tree orig_exp;
2118 tree type = TREE_TYPE (exp);
2119 enum tree_code code = TREE_CODE (type);
40449a90 2120 tree promoted_type;
522ddfa2 2121
ebfbbdc5
JJ
2122 mark_exp_read (exp);
2123
46bdb9cf
JM
2124 /* Functions and arrays have been converted during parsing. */
2125 gcc_assert (code != FUNCTION_TYPE);
2126 if (code == ARRAY_TYPE)
2127 return exp;
522ddfa2
JM
2128
2129 /* Constants can be used directly unless they're not loadable. */
2130 if (TREE_CODE (exp) == CONST_DECL)
2131 exp = DECL_INITIAL (exp);
2132
522ddfa2
JM
2133 /* Strip no-op conversions. */
2134 orig_exp = exp;
2135 STRIP_TYPE_NOPS (exp);
2136
2137 if (TREE_NO_WARNING (orig_exp))
2138 TREE_NO_WARNING (exp) = 1;
2139
400fbf9f
JW
2140 if (code == VOID_TYPE)
2141 {
5436fa2e
MP
2142 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2143 "void value not ignored as it ought to be");
400fbf9f
JW
2144 return error_mark_node;
2145 }
808d6eaa
JM
2146
2147 exp = require_complete_type (exp);
2148 if (exp == error_mark_node)
2149 return error_mark_node;
2150
40449a90
SL
2151 promoted_type = targetm.promoted_type (type);
2152 if (promoted_type)
2153 return convert (promoted_type, exp);
2154
808d6eaa
JM
2155 if (INTEGRAL_TYPE_P (type))
2156 return perform_integral_promotions (exp);
2157
400fbf9f
JW
2158 return exp;
2159}
2160\f
0fb96aa4 2161/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2162
2163 If the component name is not found, returns NULL_TREE. Otherwise,
2164 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2165 stepping down the chain to the component, which is in the last
2166 TREE_VALUE of the list. Normally the list is of length one, but if
2167 the component is embedded within (nested) anonymous structures or
2168 unions, the list steps down the chain to the component. */
2f6e4e97 2169
2f2d13da 2170static tree
0fb96aa4 2171lookup_field (tree type, tree component)
2f2d13da
DE
2172{
2173 tree field;
2174
2175 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2176 to the field elements. Use a binary search on this array to quickly
2177 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2178 will always be set for structures which have many elements. */
2179
22a0b85f 2180 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2181 {
2182 int bot, top, half;
d07605f5 2183 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2184
2185 field = TYPE_FIELDS (type);
2186 bot = 0;
d07605f5 2187 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2188 while (top - bot > 1)
2189 {
2f2d13da
DE
2190 half = (top - bot + 1) >> 1;
2191 field = field_array[bot+half];
2192
2193 if (DECL_NAME (field) == NULL_TREE)
2194 {
2195 /* Step through all anon unions in linear fashion. */
2196 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2197 {
2f2d13da 2198 field = field_array[bot++];
a68b98cf
RK
2199 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2200 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2201 {
0fb96aa4 2202 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2203
2204 if (anon)
2205 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2206
2207 /* The Plan 9 compiler permits referring
2208 directly to an anonymous struct/union field
2209 using a typedef name. */
2210 if (flag_plan9_extensions
2211 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2212 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2213 == TYPE_DECL)
2214 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2215 == component))
2216 break;
2f6e4e97 2217 }
2f2d13da
DE
2218 }
2219
2220 /* Entire record is only anon unions. */
2221 if (bot > top)
2222 return NULL_TREE;
2223
2224 /* Restart the binary search, with new lower bound. */
2225 continue;
2226 }
2227
e8b87aac 2228 if (DECL_NAME (field) == component)
2f2d13da 2229 break;
e8b87aac 2230 if (DECL_NAME (field) < component)
2f2d13da
DE
2231 bot += half;
2232 else
2233 top = bot + half;
2234 }
2235
2236 if (DECL_NAME (field_array[bot]) == component)
2237 field = field_array[bot];
2238 else if (DECL_NAME (field) != component)
e9b2c823 2239 return NULL_TREE;
2f2d13da
DE
2240 }
2241 else
2242 {
910ad8de 2243 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2244 {
e9b2c823
NB
2245 if (DECL_NAME (field) == NULL_TREE
2246 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2247 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2248 {
0fb96aa4 2249 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2250
e9b2c823
NB
2251 if (anon)
2252 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2253
2254 /* The Plan 9 compiler permits referring directly to an
2255 anonymous struct/union field using a typedef
2256 name. */
2257 if (flag_plan9_extensions
2258 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2259 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2260 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2261 == component))
2262 break;
2f2d13da
DE
2263 }
2264
2265 if (DECL_NAME (field) == component)
2266 break;
2267 }
e9b2c823
NB
2268
2269 if (field == NULL_TREE)
2270 return NULL_TREE;
2f2d13da
DE
2271 }
2272
e9b2c823 2273 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2274}
2275
c2255bc4
AH
2276/* Make an expression to refer to the COMPONENT field of structure or
2277 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2278 location of the COMPONENT_REF. */
400fbf9f
JW
2279
2280tree
c2255bc4 2281build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2282{
b3694847
SS
2283 tree type = TREE_TYPE (datum);
2284 enum tree_code code = TREE_CODE (type);
2285 tree field = NULL;
2286 tree ref;
1e57bf47 2287 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2288
7a3ea201
RH
2289 if (!objc_is_public (datum, component))
2290 return error_mark_node;
2291
46a88c12 2292 /* Detect Objective-C property syntax object.property. */
668ea4b1 2293 if (c_dialect_objc ()
46a88c12 2294 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2295 return ref;
2296
400fbf9f
JW
2297 /* See if there is a field or component with name COMPONENT. */
2298
2299 if (code == RECORD_TYPE || code == UNION_TYPE)
2300 {
d0f062fb 2301 if (!COMPLETE_TYPE_P (type))
400fbf9f 2302 {
7a228918 2303 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2304 return error_mark_node;
2305 }
2306
0fb96aa4 2307 field = lookup_field (type, component);
400fbf9f
JW
2308
2309 if (!field)
2310 {
c2255bc4 2311 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2312 return error_mark_node;
2313 }
400fbf9f 2314
e9b2c823
NB
2315 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2316 This might be better solved in future the way the C++ front
2317 end does it - by giving the anonymous entities each a
2318 separate name and type, and then have build_component_ref
2319 recursively call itself. We can't do that here. */
46ea50cb 2320 do
19d76e60 2321 {
e9b2c823 2322 tree subdatum = TREE_VALUE (field);
efed193e
JM
2323 int quals;
2324 tree subtype;
1e57bf47 2325 bool use_datum_quals;
e9b2c823
NB
2326
2327 if (TREE_TYPE (subdatum) == error_mark_node)
2328 return error_mark_node;
2329
1e57bf47
JM
2330 /* If this is an rvalue, it does not have qualifiers in C
2331 standard terms and we must avoid propagating such
2332 qualifiers down to a non-lvalue array that is then
2333 converted to a pointer. */
2334 use_datum_quals = (datum_lvalue
2335 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2336
efed193e 2337 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2338 if (use_datum_quals)
2339 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2340 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2341
2342 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2343 NULL_TREE);
c2255bc4 2344 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2345 if (TREE_READONLY (subdatum)
2346 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2347 TREE_READONLY (ref) = 1;
1e57bf47
JM
2348 if (TREE_THIS_VOLATILE (subdatum)
2349 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2350 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2351
2352 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2353 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2354
19d76e60 2355 datum = ref;
46ea50cb
RS
2356
2357 field = TREE_CHAIN (field);
19d76e60 2358 }
46ea50cb 2359 while (field);
19d76e60 2360
400fbf9f
JW
2361 return ref;
2362 }
2363 else if (code != ERROR_MARK)
c2255bc4
AH
2364 error_at (loc,
2365 "request for member %qE in something not a structure or union",
2366 component);
400fbf9f
JW
2367
2368 return error_mark_node;
2369}
2370\f
2371/* Given an expression PTR for a pointer, return an expression
2372 for the value pointed to.
6a3799eb
AH
2373 ERRORSTRING is the name of the operator to appear in error messages.
2374
2375 LOC is the location to use for the generated tree. */
400fbf9f
JW
2376
2377tree
dd865ef6 2378build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2379{
b3694847
SS
2380 tree pointer = default_conversion (ptr);
2381 tree type = TREE_TYPE (pointer);
6a3799eb 2382 tree ref;
400fbf9f
JW
2383
2384 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2385 {
1043771b 2386 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2387 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2388 {
2389 /* If a warning is issued, mark it to avoid duplicates from
2390 the backend. This only needs to be done at
2391 warn_strict_aliasing > 2. */
2392 if (warn_strict_aliasing > 2)
2393 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2394 type, TREE_OPERAND (pointer, 0)))
2395 TREE_NO_WARNING (pointer) = 1;
2396 }
2397
870cc33b 2398 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2399 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2400 == TREE_TYPE (type)))
6a3799eb
AH
2401 {
2402 ref = TREE_OPERAND (pointer, 0);
2403 protected_set_expr_location (ref, loc);
2404 return ref;
2405 }
870cc33b
RS
2406 else
2407 {
2408 tree t = TREE_TYPE (type);
46df2823 2409
984dfd8c 2410 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2411
baae9b65 2412 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2413 {
d9b7be2e
MP
2414 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2415 {
2416 error_at (loc, "dereferencing pointer to incomplete type "
2417 "%qT", t);
2418 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2419 }
870cc33b
RS
2420 return error_mark_node;
2421 }
7d882b83 2422 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2423 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2424
2425 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2426 so that we get the proper error message if the result is used
2427 to assign to. Also, &* is supposed to be a no-op.
2428 And ANSI C seems to specify that the type of the result
2429 should be the const type. */
2430 /* A de-reference of a pointer to const is not a const. It is valid
2431 to change it via some other pointer. */
2432 TREE_READONLY (ref) = TYPE_READONLY (t);
2433 TREE_SIDE_EFFECTS (ref)
271bd540 2434 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2435 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2436 protected_set_expr_location (ref, loc);
870cc33b
RS
2437 return ref;
2438 }
2439 }
400fbf9f 2440 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2441 invalid_indirection_error (loc, type, errstring);
2442
400fbf9f
JW
2443 return error_mark_node;
2444}
2445
2446/* This handles expressions of the form "a[i]", which denotes
2447 an array reference.
2448
2449 This is logically equivalent in C to *(a+i), but we may do it differently.
2450 If A is a variable or a member, we generate a primitive ARRAY_REF.
2451 This avoids forcing the array out of registers, and can work on
2452 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2453 by functions).
2454
30cd1c5d
AS
2455 For vector types, allow vector[i] but not i[vector], and create
2456 *(((type*)&vectortype) + i) for the expression.
2457
6a3799eb 2458 LOC is the location to use for the returned expression. */
400fbf9f
JW
2459
2460tree
c2255bc4 2461build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2462{
6a3799eb 2463 tree ret;
a4ab7973 2464 bool swapped = false;
400fbf9f
JW
2465 if (TREE_TYPE (array) == error_mark_node
2466 || TREE_TYPE (index) == error_mark_node)
2467 return error_mark_node;
2468
b72271b9 2469 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2470 {
2471 size_t rank = 0;
2472 if (!find_rank (loc, index, index, true, &rank))
2473 return error_mark_node;
2474 if (rank > 1)
2475 {
2476 error_at (loc, "rank of the array's index is greater than 1");
2477 return error_mark_node;
2478 }
2479 }
a4ab7973 2480 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2481 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2482 /* Allow vector[index] but not index[vector]. */
2483 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2484 {
a4ab7973
JM
2485 tree temp;
2486 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2487 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2488 {
f90e8e2e 2489 error_at (loc,
30cd1c5d
AS
2490 "subscripted value is neither array nor pointer nor vector");
2491
fdeefd49
RS
2492 return error_mark_node;
2493 }
a4ab7973
JM
2494 temp = array;
2495 array = index;
2496 index = temp;
2497 swapped = true;
2498 }
2499
2500 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2501 {
a63068b6 2502 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2503 return error_mark_node;
2504 }
2505
2506 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2507 {
a63068b6 2508 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2509 return error_mark_node;
2510 }
2511
ff6b6641
GDR
2512 /* ??? Existing practice has been to warn only when the char
2513 index is syntactically the index, not for char[array]. */
2514 if (!swapped)
5bd012f8 2515 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2516
2517 /* Apply default promotions *after* noticing character types. */
2518 index = default_conversion (index);
f406ae1f
MP
2519 if (index == error_mark_node)
2520 return error_mark_node;
a4ab7973
JM
2521
2522 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2523
aa7da51a
JJ
2524 bool non_lvalue
2525 = convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2526
2527 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2528 {
e4d35515 2529 tree rval, type;
fdeefd49 2530
400fbf9f
JW
2531 /* An array that is indexed by a non-constant
2532 cannot be stored in a register; we must be able to do
2533 address arithmetic on its address.
2534 Likewise an array of elements of variable size. */
2535 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2536 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2537 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2538 {
dffd7eb6 2539 if (!c_mark_addressable (array))
400fbf9f
JW
2540 return error_mark_node;
2541 }
e6d52559
JW
2542 /* An array that is indexed by a constant value which is not within
2543 the array bounds cannot be stored in a register either; because we
2544 would get a crash in store_bit_field/extract_bit_field when trying
2545 to access a non-existent part of the register. */
2546 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2547 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2548 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2549 {
dffd7eb6 2550 if (!c_mark_addressable (array))
e6d52559
JW
2551 return error_mark_node;
2552 }
400fbf9f 2553
f3bede71 2554 if (pedantic || warn_c90_c99_compat)
400fbf9f
JW
2555 {
2556 tree foo = array;
2557 while (TREE_CODE (foo) == COMPONENT_REF)
2558 foo = TREE_OPERAND (foo, 0);
5baeaac0 2559 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2560 pedwarn (loc, OPT_Wpedantic,
fcf73884 2561 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2562 else if (!lvalue_p (foo))
2563 pedwarn_c90 (loc, OPT_Wpedantic,
2564 "ISO C90 forbids subscripting non-lvalue "
2565 "array");
400fbf9f
JW
2566 }
2567
46df2823 2568 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2569 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2570 /* Array ref is const/volatile if the array elements are
c22cacf3 2571 or if the array is. */
400fbf9f
JW
2572 TREE_READONLY (rval)
2573 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2574 | TREE_READONLY (array));
2575 TREE_SIDE_EFFECTS (rval)
2576 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2577 | TREE_SIDE_EFFECTS (array));
2578 TREE_THIS_VOLATILE (rval)
2579 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2580 /* This was added by rms on 16 Nov 91.
2f6e4e97 2581 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2582 in an inline function.
2583 Hope it doesn't break something else. */
2584 | TREE_THIS_VOLATILE (array));
928c19bb 2585 ret = require_complete_type (rval);
6a3799eb 2586 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2587 if (non_lvalue)
2588 ret = non_lvalue_loc (loc, ret);
6a3799eb 2589 return ret;
400fbf9f 2590 }
a4ab7973
JM
2591 else
2592 {
2593 tree ar = default_conversion (array);
400fbf9f 2594
a4ab7973
JM
2595 if (ar == error_mark_node)
2596 return ar;
400fbf9f 2597
a4ab7973
JM
2598 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2599 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2600
aa7da51a
JJ
2601 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2602 index, 0),
2603 RO_ARRAY_INDEXING);
2604 if (non_lvalue)
2605 ret = non_lvalue_loc (loc, ret);
2606 return ret;
a4ab7973 2607 }
400fbf9f
JW
2608}
2609\f
7e585d16 2610/* Build an external reference to identifier ID. FUN indicates
766beb40 2611 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2612 location of the identifier. This sets *TYPE to the type of the
2613 identifier, which is not the same as the type of the returned value
2614 for CONST_DECLs defined as enum constants. If the type of the
2615 identifier is not available, *TYPE is set to NULL. */
7e585d16 2616tree
c2255bc4 2617build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2618{
2619 tree ref;
2620 tree decl = lookup_name (id);
16b34ad6
ZL
2621
2622 /* In Objective-C, an instance variable (ivar) may be preferred to
2623 whatever lookup_name() found. */
2624 decl = objc_lookup_ivar (decl, id);
7e585d16 2625
6866c6e8 2626 *type = NULL;
339a28b9 2627 if (decl && decl != error_mark_node)
6866c6e8
ILT
2628 {
2629 ref = decl;
2630 *type = TREE_TYPE (ref);
2631 }
339a28b9
ZW
2632 else if (fun)
2633 /* Implicit function declaration. */
c2255bc4 2634 ref = implicitly_declare (loc, id);
339a28b9
ZW
2635 else if (decl == error_mark_node)
2636 /* Don't complain about something that's already been
2637 complained about. */
2638 return error_mark_node;
2639 else
2640 {
c2255bc4 2641 undeclared_variable (loc, id);
339a28b9
ZW
2642 return error_mark_node;
2643 }
7e585d16
ZW
2644
2645 if (TREE_TYPE (ref) == error_mark_node)
2646 return error_mark_node;
2647
339a28b9 2648 if (TREE_DEPRECATED (ref))
9b86d6bb 2649 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2650
ad960f56 2651 /* Recursive call does not count as usage. */
b8698a0f 2652 if (ref != current_function_decl)
ad960f56 2653 {
ad960f56
MLI
2654 TREE_USED (ref) = 1;
2655 }
7e585d16 2656
bc4b653b
JM
2657 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2658 {
2659 if (!in_sizeof && !in_typeof)
2660 C_DECL_USED (ref) = 1;
2661 else if (DECL_INITIAL (ref) == 0
2662 && DECL_EXTERNAL (ref)
2663 && !TREE_PUBLIC (ref))
2664 record_maybe_used_decl (ref);
2665 }
2666
7e585d16
ZW
2667 if (TREE_CODE (ref) == CONST_DECL)
2668 {
6193b8b7 2669 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2670
2671 if (warn_cxx_compat
2672 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2673 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2674 {
2675 warning_at (loc, OPT_Wc___compat,
2676 ("enum constant defined in struct or union "
2677 "is not visible in C++"));
2678 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2679 }
2680
7e585d16
ZW
2681 ref = DECL_INITIAL (ref);
2682 TREE_CONSTANT (ref) = 1;
2683 }
6a29edea 2684 else if (current_function_decl != 0
4b1e44be 2685 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2686 && (TREE_CODE (ref) == VAR_DECL
2687 || TREE_CODE (ref) == PARM_DECL
2688 || TREE_CODE (ref) == FUNCTION_DECL))
2689 {
2690 tree context = decl_function_context (ref);
2f6e4e97 2691
6a29edea
EB
2692 if (context != 0 && context != current_function_decl)
2693 DECL_NONLOCAL (ref) = 1;
2694 }
71113fcd
GK
2695 /* C99 6.7.4p3: An inline definition of a function with external
2696 linkage ... shall not contain a reference to an identifier with
2697 internal linkage. */
2698 else if (current_function_decl != 0
2699 && DECL_DECLARED_INLINE_P (current_function_decl)
2700 && DECL_EXTERNAL (current_function_decl)
2701 && VAR_OR_FUNCTION_DECL_P (ref)
2702 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2703 && ! TREE_PUBLIC (ref)
2704 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2705 record_inline_static (loc, current_function_decl, ref,
2706 csi_internal);
7e585d16
ZW
2707
2708 return ref;
2709}
2710
bc4b653b
JM
2711/* Record details of decls possibly used inside sizeof or typeof. */
2712struct maybe_used_decl
2713{
2714 /* The decl. */
2715 tree decl;
2716 /* The level seen at (in_sizeof + in_typeof). */
2717 int level;
2718 /* The next one at this level or above, or NULL. */
2719 struct maybe_used_decl *next;
2720};
2721
2722static struct maybe_used_decl *maybe_used_decls;
2723
2724/* Record that DECL, an undefined static function reference seen
2725 inside sizeof or typeof, might be used if the operand of sizeof is
2726 a VLA type or the operand of typeof is a variably modified
2727 type. */
2728
4e2fb7de 2729static void
bc4b653b
JM
2730record_maybe_used_decl (tree decl)
2731{
2732 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2733 t->decl = decl;
2734 t->level = in_sizeof + in_typeof;
2735 t->next = maybe_used_decls;
2736 maybe_used_decls = t;
2737}
2738
2739/* Pop the stack of decls possibly used inside sizeof or typeof. If
2740 USED is false, just discard them. If it is true, mark them used
2741 (if no longer inside sizeof or typeof) or move them to the next
2742 level up (if still inside sizeof or typeof). */
2743
2744void
2745pop_maybe_used (bool used)
2746{
2747 struct maybe_used_decl *p = maybe_used_decls;
2748 int cur_level = in_sizeof + in_typeof;
2749 while (p && p->level > cur_level)
2750 {
2751 if (used)
2752 {
2753 if (cur_level == 0)
2754 C_DECL_USED (p->decl) = 1;
2755 else
2756 p->level = cur_level;
2757 }
2758 p = p->next;
2759 }
2760 if (!used || cur_level == 0)
2761 maybe_used_decls = p;
2762}
2763
2764/* Return the result of sizeof applied to EXPR. */
2765
2766struct c_expr
c2255bc4 2767c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2768{
2769 struct c_expr ret;
ad97f4be
JM
2770 if (expr.value == error_mark_node)
2771 {
2772 ret.value = error_mark_node;
2773 ret.original_code = ERROR_MARK;
6866c6e8 2774 ret.original_type = NULL;
ad97f4be
JM
2775 pop_maybe_used (false);
2776 }
2777 else
2778 {
928c19bb 2779 bool expr_const_operands = true;
773ec47f
MP
2780
2781 if (TREE_CODE (expr.value) == PARM_DECL
2782 && C_ARRAY_PARAMETER (expr.value))
2783 {
2784 if (warning_at (loc, OPT_Wsizeof_array_argument,
2785 "%<sizeof%> on array function parameter %qE will "
2786 "return size of %qT", expr.value,
2787 expr.original_type))
2788 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2789 }
928c19bb
JM
2790 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2791 &expr_const_operands);
c2255bc4 2792 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2793 c_last_sizeof_arg = expr.value;
2794 ret.original_code = SIZEOF_EXPR;
6866c6e8 2795 ret.original_type = NULL;
928c19bb 2796 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2797 {
2798 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2799 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2800 folded_expr, ret.value);
2801 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2802 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2803 }
928c19bb 2804 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2805 }
bc4b653b
JM
2806 return ret;
2807}
2808
2809/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2810 name passed to sizeof (rather than the type itself). LOC is the
2811 location of the original expression. */
bc4b653b
JM
2812
2813struct c_expr
c2255bc4 2814c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2815{
2816 tree type;
2817 struct c_expr ret;
928c19bb
JM
2818 tree type_expr = NULL_TREE;
2819 bool type_expr_const = true;
2820 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2821 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2822 c_last_sizeof_arg = type;
2823 ret.original_code = SIZEOF_EXPR;
6866c6e8 2824 ret.original_type = NULL;
24070fcb
JM
2825 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2826 && c_vla_type_p (type))
928c19bb 2827 {
24070fcb
JM
2828 /* If the type is a [*] array, it is a VLA but is represented as
2829 having a size of zero. In such a case we must ensure that
2830 the result of sizeof does not get folded to a constant by
2831 c_fully_fold, because if the size is evaluated the result is
2832 not constant and so constraints on zero or negative size
2833 arrays must not be applied when this sizeof call is inside
2834 another array declarator. */
2835 if (!type_expr)
2836 type_expr = integer_zero_node;
928c19bb
JM
2837 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2838 type_expr, ret.value);
2839 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2840 }
16464cc1
VR
2841 pop_maybe_used (type != error_mark_node
2842 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2843 return ret;
2844}
2845
400fbf9f 2846/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2847 The function call is at LOC.
400fbf9f
JW
2848 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2849 TREE_VALUE of each node is a parameter-expression.
2850 FUNCTION's data type may be a function type or a pointer-to-function. */
2851
2852tree
c2255bc4 2853build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2854{
9771b263 2855 vec<tree, va_gc> *v;
bbbbb16a
ILT
2856 tree ret;
2857
9771b263 2858 vec_alloc (v, list_length (params));
bbbbb16a 2859 for (; params; params = TREE_CHAIN (params))
9771b263 2860 v->quick_push (TREE_VALUE (params));
8edbfaa6 2861 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2862 vec_free (v);
bbbbb16a
ILT
2863 return ret;
2864}
2865
ae52741c
MLI
2866/* Give a note about the location of the declaration of DECL. */
2867
2868static void inform_declaration (tree decl)
2869{
2870 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2871 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2872}
2873
bbbbb16a
ILT
2874/* Build a function call to function FUNCTION with parameters PARAMS.
2875 ORIGTYPES, if not NULL, is a vector of types; each element is
2876 either NULL or the original type of the corresponding element in
2877 PARAMS. The original type may differ from TREE_TYPE of the
2878 parameter for enums. FUNCTION's data type may be a function type
2879 or pointer-to-function. This function changes the elements of
2880 PARAMS. */
2881
2882tree
81e5eca8
MP
2883build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2884 tree function, vec<tree, va_gc> *params,
9771b263 2885 vec<tree, va_gc> *origtypes)
400fbf9f 2886{
b3694847 2887 tree fntype, fundecl = 0;
4977bab6 2888 tree name = NULL_TREE, result;
c96f4f73 2889 tree tem;
94a0dd7b
SL
2890 int nargs;
2891 tree *argarray;
b8698a0f 2892
400fbf9f 2893
fc76e425 2894 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2895 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2896
2897 /* Convert anything with function type to a pointer-to-function. */
2898 if (TREE_CODE (function) == FUNCTION_DECL)
2899 {
2900 name = DECL_NAME (function);
0a35513e
AH
2901
2902 if (flag_tm)
2903 tm_malloc_replacement (function);
a5eadacc 2904 fundecl = function;
86951993
AM
2905 /* Atomic functions have type checking/casting already done. They are
2906 often rewritten and don't match the original parameter list. */
2907 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2908 origtypes = NULL;
36536d79 2909
b72271b9 2910 if (flag_cilkplus
36536d79
BI
2911 && is_cilkplus_reduce_builtin (function))
2912 origtypes = NULL;
400fbf9f 2913 }
f2a71bbc 2914 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2915 function = function_to_pointer_conversion (loc, function);
400fbf9f 2916
6e955430
ZL
2917 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2918 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2919 if (params && !params->is_empty ())
2920 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2921
928c19bb
JM
2922 function = c_fully_fold (function, false, NULL);
2923
400fbf9f
JW
2924 fntype = TREE_TYPE (function);
2925
2926 if (TREE_CODE (fntype) == ERROR_MARK)
2927 return error_mark_node;
2928
2929 if (!(TREE_CODE (fntype) == POINTER_TYPE
2930 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2931 {
ae52741c
MLI
2932 if (!flag_diagnostics_show_caret)
2933 error_at (loc,
2934 "called object %qE is not a function or function pointer",
2935 function);
2936 else if (DECL_P (function))
2937 {
2938 error_at (loc,
2939 "called object %qD is not a function or function pointer",
2940 function);
2941 inform_declaration (function);
2942 }
2943 else
2944 error_at (loc,
2945 "called object is not a function or function pointer");
400fbf9f
JW
2946 return error_mark_node;
2947 }
2948
5ce89b2e
JM
2949 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2950 current_function_returns_abnormally = 1;
2951
400fbf9f
JW
2952 /* fntype now gets the type of function pointed to. */
2953 fntype = TREE_TYPE (fntype);
2954
ab4194da
JM
2955 /* Convert the parameters to the types declared in the
2956 function prototype, or apply default promotions. */
2957
81e5eca8
MP
2958 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2959 origtypes, function, fundecl);
ab4194da
JM
2960 if (nargs < 0)
2961 return error_mark_node;
2962
c96f4f73 2963 /* Check that the function is called through a compatible prototype.
fa337f3a 2964 If it is not, warn. */
1043771b 2965 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2966 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2967 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2968 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2969 {
2970 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
2971
2972 /* This situation leads to run-time undefined behavior. We can't,
2973 therefore, simply error unless we can prove that all possible
2974 executions of the program must execute the code. */
fa337f3a 2975 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 2976
fa337f3a
RB
2977 if (VOID_TYPE_P (return_type)
2978 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2979 pedwarn (loc, 0,
2980 "function with qualified void return type called");
2981 }
c96f4f73 2982
9771b263 2983 argarray = vec_safe_address (params);
bbbbb16a 2984
83322951
RG
2985 /* Check that arguments to builtin functions match the expectations. */
2986 if (fundecl
2987 && DECL_BUILT_IN (fundecl)
2988 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2989 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2990 return error_mark_node;
400fbf9f 2991
83322951 2992 /* Check that the arguments to the function are valid. */
dde05067 2993 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2994
928c19bb
JM
2995 if (name != NULL_TREE
2996 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2997 {
928c19bb 2998 if (require_constant_value)
b8698a0f 2999 result =
db3927fb
AH
3000 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3001 function, nargs, argarray);
928c19bb 3002 else
db3927fb
AH
3003 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3004 function, nargs, argarray);
928c19bb
JM
3005 if (TREE_CODE (result) == NOP_EXPR
3006 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3007 STRIP_TYPE_NOPS (result);
bf730f15
RS
3008 }
3009 else
db3927fb
AH
3010 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3011 function, nargs, argarray);
b0b3afb2 3012
71653180 3013 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
3014 {
3015 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 3016 pedwarn (loc, 0,
3ce62965
JM
3017 "function with qualified void return type called");
3018 return result;
3019 }
1eb8759b 3020 return require_complete_type (result);
400fbf9f 3021}
8edbfaa6
JJ
3022
3023/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3024
3025tree
3026c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3027 tree function, vec<tree, va_gc> *params,
3028 vec<tree, va_gc> *origtypes)
3029{
3030 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3031 STRIP_TYPE_NOPS (function);
3032
3033 /* Convert anything with function type to a pointer-to-function. */
3034 if (TREE_CODE (function) == FUNCTION_DECL)
3035 {
3036 /* Implement type-directed function overloading for builtins.
3037 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3038 handle all the type checking. The result is a complete expression
3039 that implements this function call. */
3040 tree tem = resolve_overloaded_builtin (loc, function, params);
3041 if (tem)
3042 return tem;
3043 }
3044 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3045}
400fbf9f 3046\f
bbbbb16a
ILT
3047/* Convert the argument expressions in the vector VALUES
3048 to the types in the list TYPELIST.
400fbf9f
JW
3049
3050 If TYPELIST is exhausted, or when an element has NULL as its type,
3051 perform the default conversions.
3052
bbbbb16a
ILT
3053 ORIGTYPES is the original types of the expressions in VALUES. This
3054 holds the type of enum values which have been converted to integral
3055 types. It may be NULL.
400fbf9f 3056
03dafa61
JM
3057 FUNCTION is a tree for the called function. It is used only for
3058 error messages, where it is formatted with %qE.
400fbf9f
JW
3059
3060 This is also where warnings about wrong number of args are generated.
3061
81e5eca8
MP
3062 ARG_LOC are locations of function arguments (if any).
3063
94a0dd7b 3064 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3065 than the length of VALUES in some error situations), or -1 on
3066 failure. */
94a0dd7b
SL
3067
3068static int
81e5eca8
MP
3069convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3070 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3071 tree function, tree fundecl)
400fbf9f 3072{
bbbbb16a
ILT
3073 tree typetail, val;
3074 unsigned int parmnum;
06302a02 3075 bool error_args = false;
b5d32c25 3076 const bool type_generic = fundecl
81e5eca8 3077 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3078 bool type_generic_remove_excess_precision = false;
03dafa61 3079 tree selector;
03dafa61 3080
2ac2f164
JM
3081 /* Change pointer to function to the function itself for
3082 diagnostics. */
03dafa61
JM
3083 if (TREE_CODE (function) == ADDR_EXPR
3084 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3085 function = TREE_OPERAND (function, 0);
03dafa61
JM
3086
3087 /* Handle an ObjC selector specially for diagnostics. */
3088 selector = objc_message_selector ();
400fbf9f 3089
8ce94e44
JM
3090 /* For type-generic built-in functions, determine whether excess
3091 precision should be removed (classification) or not
3092 (comparison). */
3093 if (type_generic
3094 && DECL_BUILT_IN (fundecl)
3095 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3096 {
3097 switch (DECL_FUNCTION_CODE (fundecl))
3098 {
3099 case BUILT_IN_ISFINITE:
3100 case BUILT_IN_ISINF:
3101 case BUILT_IN_ISINF_SIGN:
3102 case BUILT_IN_ISNAN:
3103 case BUILT_IN_ISNORMAL:
3104 case BUILT_IN_FPCLASSIFY:
3105 type_generic_remove_excess_precision = true;
3106 break;
3107
3108 default:
3109 type_generic_remove_excess_precision = false;
3110 break;
3111 }
3112 }
b72271b9 3113 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3114 return vec_safe_length (values);
8ce94e44 3115
400fbf9f 3116 /* Scan the given expressions and types, producing individual
bbbbb16a 3117 converted arguments. */
400fbf9f 3118
bbbbb16a 3119 for (typetail = typelist, parmnum = 0;
9771b263 3120 values && values->iterate (parmnum, &val);
bbbbb16a 3121 ++parmnum)
400fbf9f 3122 {
b3694847 3123 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3124 tree valtype = TREE_TYPE (val);
03dafa61
JM
3125 tree rname = function;
3126 int argnum = parmnum + 1;
4d3e6fae 3127 const char *invalid_func_diag;
8ce94e44 3128 bool excess_precision = false;
928c19bb 3129 bool npc;
bbbbb16a 3130 tree parmval;
5c1bc275
MP
3131 /* Some __atomic_* builtins have additional hidden argument at
3132 position 0. */
3133 location_t ploc
3134 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3135 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3136 : input_location;
400fbf9f
JW
3137
3138 if (type == void_type_node)
3139 {
19dc6d01 3140 if (selector)
68fca595 3141 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3142 else
68fca595 3143 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3144 inform_declaration (fundecl);
94a0dd7b 3145 return parmnum;
400fbf9f
JW
3146 }
3147
03dafa61
JM
3148 if (selector && argnum > 2)
3149 {
3150 rname = selector;
3151 argnum -= 2;
3152 }
3153
928c19bb 3154 npc = null_pointer_constant_p (val);
8ce94e44
JM
3155
3156 /* If there is excess precision and a prototype, convert once to
3157 the required type rather than converting via the semantic
3158 type. Likewise without a prototype a float value represented
3159 as long double should be converted once to double. But for
3160 type-generic classification functions excess precision must
3161 be removed here. */
3162 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3163 && (type || !type_generic || !type_generic_remove_excess_precision))
3164 {
3165 val = TREE_OPERAND (val, 0);
3166 excess_precision = true;
3167 }
928c19bb 3168 val = c_fully_fold (val, false, NULL);
ed248cf7 3169 STRIP_TYPE_NOPS (val);
400fbf9f 3170
400fbf9f
JW
3171 val = require_complete_type (val);
3172
3173 if (type != 0)
3174 {
3175 /* Formal parm type is specified by a function prototype. */
400fbf9f 3176
20913689 3177 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3178 {
5c1bc275
MP
3179 error_at (ploc, "type of formal parameter %d is incomplete",
3180 parmnum + 1);
400fbf9f
JW
3181 parmval = val;
3182 }
3183 else
3184 {
bbbbb16a
ILT
3185 tree origtype;
3186
d45cf215
RS
3187 /* Optionally warn about conversions that
3188 differ from the default conversions. */
05170031 3189 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3190 {
e3a64162 3191 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3192
aae43c5f 3193 if (INTEGRAL_TYPE_P (type)
8ce94e44 3194 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3195 warning_at (ploc, OPT_Wtraditional_conversion,
3196 "passing argument %d of %qE as integer rather "
3197 "than floating due to prototype",
3198 argnum, rname);
03829ad2 3199 if (INTEGRAL_TYPE_P (type)
8ce94e44 3200 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3201 warning_at (ploc, OPT_Wtraditional_conversion,
3202 "passing argument %d of %qE as integer rather "
3203 "than complex due to prototype",
3204 argnum, rname);
aae43c5f 3205 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3206 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3207 warning_at (ploc, OPT_Wtraditional_conversion,
3208 "passing argument %d of %qE as complex rather "
3209 "than floating due to prototype",
3210 argnum, rname);
400fbf9f 3211 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3212 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3213 warning_at (ploc, OPT_Wtraditional_conversion,
3214 "passing argument %d of %qE as floating rather "
3215 "than integer due to prototype",
3216 argnum, rname);
03829ad2 3217 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3218 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3219 warning_at (ploc, OPT_Wtraditional_conversion,
3220 "passing argument %d of %qE as complex rather "
3221 "than integer due to prototype",
3222 argnum, rname);
aae43c5f 3223 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3224 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3225 warning_at (ploc, OPT_Wtraditional_conversion,
3226 "passing argument %d of %qE as floating rather "
3227 "than complex due to prototype",
3228 argnum, rname);
aae43c5f
RK
3229 /* ??? At some point, messages should be written about
3230 conversions between complex types, but that's too messy
3231 to do now. */
d45cf215 3232 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3233 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3234 {
3235 /* Warn if any argument is passed as `float',
047de90b 3236 since without a prototype it would be `double'. */
9a8ce21f
JG
3237 if (formal_prec == TYPE_PRECISION (float_type_node)
3238 && type != dfloat32_type_node)
5c1bc275
MP
3239 warning_at (ploc, 0,
3240 "passing argument %d of %qE as %<float%> "
3241 "rather than %<double%> due to prototype",
3242 argnum, rname);
9a8ce21f
JG
3243
3244 /* Warn if mismatch between argument and prototype
3245 for decimal float types. Warn of conversions with
3246 binary float types and of precision narrowing due to
3247 prototype. */
8ce94e44 3248 else if (type != valtype
9a8ce21f
JG
3249 && (type == dfloat32_type_node
3250 || type == dfloat64_type_node
c22cacf3 3251 || type == dfloat128_type_node
8ce94e44
JM
3252 || valtype == dfloat32_type_node
3253 || valtype == dfloat64_type_node
3254 || valtype == dfloat128_type_node)
c22cacf3 3255 && (formal_prec
8ce94e44 3256 <= TYPE_PRECISION (valtype)
9a8ce21f 3257 || (type == dfloat128_type_node
8ce94e44 3258 && (valtype
c22cacf3 3259 != dfloat64_type_node
8ce94e44 3260 && (valtype
9a8ce21f
JG
3261 != dfloat32_type_node)))
3262 || (type == dfloat64_type_node
8ce94e44 3263 && (valtype
9a8ce21f 3264 != dfloat32_type_node))))
5c1bc275
MP
3265 warning_at (ploc, 0,
3266 "passing argument %d of %qE as %qT "
3267 "rather than %qT due to prototype",
3268 argnum, rname, type, valtype);
9a8ce21f 3269
d45cf215 3270 }
3ed56f8a
KG
3271 /* Detect integer changing in width or signedness.
3272 These warnings are only activated with
05170031
MLI
3273 -Wtraditional-conversion, not with -Wtraditional. */
3274 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3275 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3276 {
d45cf215
RS
3277 tree would_have_been = default_conversion (val);
3278 tree type1 = TREE_TYPE (would_have_been);
3279
754a4d82 3280 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3281 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3282 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3283 /* No warning if function asks for enum
3284 and the actual arg is that enum type. */
3285 ;
3286 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3287 warning_at (ploc, OPT_Wtraditional_conversion,
3288 "passing argument %d of %qE "
3289 "with different width due to prototype",
3290 argnum, rname);
8df83eae 3291 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3292 ;
800cd3b9
RS
3293 /* Don't complain if the formal parameter type
3294 is an enum, because we can't tell now whether
3295 the value was an enum--even the same enum. */
3296 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3297 ;
400fbf9f
JW
3298 else if (TREE_CODE (val) == INTEGER_CST
3299 && int_fits_type_p (val, type))
3300 /* Change in signedness doesn't matter
3301 if a constant value is unaffected. */
3302 ;
ce9895ae
RS
3303 /* If the value is extended from a narrower
3304 unsigned type, it doesn't matter whether we
3305 pass it as signed or unsigned; the value
3306 certainly is the same either way. */
8ce94e44
JM
3307 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3308 && TYPE_UNSIGNED (valtype))
ce9895ae 3309 ;
8df83eae 3310 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3311 warning_at (ploc, OPT_Wtraditional_conversion,
3312 "passing argument %d of %qE "
3313 "as unsigned due to prototype",
3314 argnum, rname);
3ed56f8a 3315 else
5c1bc275
MP
3316 warning_at (ploc, OPT_Wtraditional_conversion,
3317 "passing argument %d of %qE "
3318 "as signed due to prototype",
3319 argnum, rname);
400fbf9f
JW
3320 }
3321 }
3322
8ce94e44
JM
3323 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3324 sake of better warnings from convert_and_check. */
3325 if (excess_precision)
3326 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3327 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3328 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3329 val, origtype, ic_argpass,
3330 npc, fundecl, function,
2ac2f164 3331 parmnum + 1);
2f6e4e97 3332
61f71b34 3333 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3334 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3335 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3336 parmval = default_conversion (parmval);
400fbf9f 3337 }
400fbf9f 3338 }
8ce94e44
JM
3339 else if (TREE_CODE (valtype) == REAL_TYPE
3340 && (TYPE_PRECISION (valtype)
2531a1d9 3341 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3342 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3343 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3344 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3345 {
3346 if (type_generic)
bbbbb16a 3347 parmval = val;
b5d32c25 3348 else
0a0b3574
MM
3349 {
3350 /* Convert `float' to `double'. */
3351 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3352 warning_at (ploc, OPT_Wdouble_promotion,
3353 "implicit conversion from %qT to %qT when passing "
3354 "argument to function",
3355 valtype, double_type_node);
0a0b3574
MM
3356 parmval = convert (double_type_node, val);
3357 }
b5d32c25 3358 }
8ce94e44
JM
3359 else if (excess_precision && !type_generic)
3360 /* A "double" argument with excess precision being passed
3361 without a prototype or in variable arguments. */
bbbbb16a 3362 parmval = convert (valtype, val);
c22cacf3
MS
3363 else if ((invalid_func_diag =
3364 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3365 {
3366 error (invalid_func_diag);
94a0dd7b 3367 return -1;
4d3e6fae 3368 }
400fbf9f
JW
3369 else
3370 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3371 parmval = default_conversion (val);
3372
9771b263 3373 (*values)[parmnum] = parmval;
06302a02
JJ
3374 if (parmval == error_mark_node)
3375 error_args = true;
400fbf9f
JW
3376
3377 if (typetail)
3378 typetail = TREE_CHAIN (typetail);
3379 }
3380
9771b263 3381 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3382
400fbf9f 3383 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3384 {
68fca595 3385 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3386 inform_declaration (fundecl);
3387 return -1;
3789b316 3388 }
400fbf9f 3389
06302a02 3390 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3391}
3392\f
43f6dfd3
RS
3393/* This is the entry point used by the parser to build unary operators
3394 in the input. CODE, a tree_code, specifies the unary operator, and
3395 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3396 CONVERT_EXPR for code.
3397
3398 LOC is the location to use for the tree generated.
3399*/
43f6dfd3
RS
3400
3401struct c_expr
c2255bc4 3402parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3403{
3404 struct c_expr result;
3405
c9f9eb5d 3406 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3407 result.original_code = code;
6866c6e8
ILT
3408 result.original_type = NULL;
3409
59c0753d 3410 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3411 overflow_warning (loc, result.value);
59c0753d 3412
43f6dfd3
RS
3413 return result;
3414}
3415
3416/* This is the entry point used by the parser to build binary operators
3417 in the input. CODE, a tree_code, specifies the binary operator, and
3418 ARG1 and ARG2 are the operands. In addition to constructing the
3419 expression, we check for operands that were written with other binary
ba47d38d
AH
3420 operators in a way that is likely to confuse the user.
3421
3422 LOCATION is the location of the binary operator. */
edc7c4ec 3423
487a92fe 3424struct c_expr
ba47d38d
AH
3425parser_build_binary_op (location_t location, enum tree_code code,
3426 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3427{
487a92fe 3428 struct c_expr result;
400fbf9f 3429
487a92fe
JM
3430 enum tree_code code1 = arg1.original_code;
3431 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3432 tree type1 = (arg1.original_type
3433 ? arg1.original_type
3434 : TREE_TYPE (arg1.value));
3435 tree type2 = (arg2.original_type
3436 ? arg2.original_type
3437 : TREE_TYPE (arg2.value));
400fbf9f 3438
ba47d38d
AH
3439 result.value = build_binary_op (location, code,
3440 arg1.value, arg2.value, 1);
487a92fe 3441 result.original_code = code;
6866c6e8 3442 result.original_type = NULL;
58bf601b 3443
487a92fe
JM
3444 if (TREE_CODE (result.value) == ERROR_MARK)
3445 return result;
400fbf9f 3446
ba47d38d
AH
3447 if (location != UNKNOWN_LOCATION)
3448 protected_set_expr_location (result.value, location);
3449
400fbf9f 3450 /* Check for cases such as x+y<<z which users are likely
487a92fe 3451 to misinterpret. */
400fbf9f 3452 if (warn_parentheses)
393e8e8b
MP
3453 warn_about_parentheses (location, code, code1, arg1.value, code2,
3454 arg2.value);
001af587 3455
ca409efd 3456 if (warn_logical_op)
393e8e8b 3457 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3458 code1, arg1.value, code2, arg2.value);
63a08740 3459
742938c9 3460 if (warn_logical_not_paren
47c2554f
MP
3461 && code1 == TRUTH_NOT_EXPR
3462 && code2 != TRUTH_NOT_EXPR)
59ea0364 3463 warn_logical_not_parentheses (location, code, arg2.value);
742938c9 3464
e994a705
RS
3465 /* Warn about comparisons against string literals, with the exception
3466 of testing for equality or inequality of a string literal with NULL. */
3467 if (code == EQ_EXPR || code == NE_EXPR)
3468 {
3469 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3470 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3471 warning_at (location, OPT_Waddress,
3472 "comparison with string literal results in unspecified behavior");
e994a705
RS
3473 }
3474 else if (TREE_CODE_CLASS (code) == tcc_comparison
3475 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3476 warning_at (location, OPT_Waddress,
3477 "comparison with string literal results in unspecified behavior");
e994a705 3478
b8698a0f
L
3479 if (TREE_OVERFLOW_P (result.value)
3480 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3481 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3482 overflow_warning (location, result.value);
400fbf9f 3483
6866c6e8
ILT
3484 /* Warn about comparisons of different enum types. */
3485 if (warn_enum_compare
3486 && TREE_CODE_CLASS (code) == tcc_comparison
3487 && TREE_CODE (type1) == ENUMERAL_TYPE
3488 && TREE_CODE (type2) == ENUMERAL_TYPE
3489 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3490 warning_at (location, OPT_Wenum_compare,
3491 "comparison between %qT and %qT",
3492 type1, type2);
3493
400fbf9f
JW
3494 return result;
3495}
3e4093b6 3496\f
3e4093b6
RS
3497/* Return a tree for the difference of pointers OP0 and OP1.
3498 The resulting tree has type int. */
293c9fdd 3499
3e4093b6 3500static tree
db3927fb 3501pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3502{
3e4093b6 3503 tree restype = ptrdiff_type_node;
36c5e70a 3504 tree result, inttype;
400fbf9f 3505
36c5e70a
BE
3506 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3507 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3508 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3509 tree orig_op1 = op1;
400fbf9f 3510
36c5e70a
BE
3511 /* If the operands point into different address spaces, we need to
3512 explicitly convert them to pointers into the common address space
3513 before we can subtract the numerical address values. */
3514 if (as0 != as1)
3515 {
3516 addr_space_t as_common;
3517 tree common_type;
3518
3519 /* Determine the common superset address space. This is guaranteed
3520 to exist because the caller verified that comp_target_types
3521 returned non-zero. */
3522 if (!addr_space_superset (as0, as1, &as_common))
3523 gcc_unreachable ();
3524
3525 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3526 op0 = convert (common_type, op0);
3527 op1 = convert (common_type, op1);
3528 }
3529
3530 /* Determine integer type to perform computations in. This will usually
3531 be the same as the result type (ptrdiff_t), but may need to be a wider
3532 type if pointers for the address space are wider than ptrdiff_t. */
3533 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3534 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3535 else
3536 inttype = restype;
3537
fcf73884 3538 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3539 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3540 "pointer of type %<void *%> used in subtraction");
3541 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3542 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3543 "pointer to a function used in subtraction");
400fbf9f 3544
3e4093b6
RS
3545 /* First do the subtraction as integers;
3546 then drop through to build the divide operator.
3547 Do not do default conversions on the minus operator
3548 in case restype is a short type. */
400fbf9f 3549
db3927fb 3550 op0 = build_binary_op (loc,
36c5e70a
BE
3551 MINUS_EXPR, convert (inttype, op0),
3552 convert (inttype, op1), 0);
3e4093b6
RS
3553 /* This generates an error if op1 is pointer to incomplete type. */
3554 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3555 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3556
3e4093b6 3557 op1 = c_size_in_bytes (target_type);
400fbf9f 3558
f04dda30
MP
3559 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3560 error_at (loc, "arithmetic on pointer to an empty aggregate");
3561
3e4093b6 3562 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3563 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3564 op0, convert (inttype, op1));
3565
3566 /* Convert to final result type if necessary. */
3567 return convert (restype, result);
3e4093b6
RS
3568}
3569\f
267bac10
JM
3570/* Expand atomic compound assignments into an approriate sequence as
3571 specified by the C11 standard section 6.5.16.2.
3572 given
3573 _Atomic T1 E1
3574 T2 E2
3575 E1 op= E2
3576
3577 This sequence is used for all types for which these operations are
3578 supported.
3579
3580 In addition, built-in versions of the 'fe' prefixed routines may
3581 need to be invoked for floating point (real, complex or vector) when
3582 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3583
3584 T1 newval;
3585 T1 old;
3586 T1 *addr
3587 T2 val
3588 fenv_t fenv
3589
3590 addr = &E1;
3591 val = (E2);
3592 __atomic_load (addr, &old, SEQ_CST);
3593 feholdexcept (&fenv);
3594loop:
3595 newval = old op val;
3596 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3597 SEQ_CST))
3598 goto done;
3599 feclearexcept (FE_ALL_EXCEPT);
3600 goto loop:
3601done:
3602 feupdateenv (&fenv);
3603
3604 Also note that the compiler is simply issuing the generic form of
3605 the atomic operations. This requires temp(s) and has their address
3606 taken. The atomic processing is smart enough to figure out when the
3607 size of an object can utilize a lock-free version, and convert the
3608 built-in call to the appropriate lock-free routine. The optimizers
3609 will then dispose of any temps that are no longer required, and
3610 lock-free implementations are utilized as long as there is target
3611 support for the required size.
3612
3613 If the operator is NOP_EXPR, then this is a simple assignment, and
3614 an __atomic_store is issued to perform the assignment rather than
3615 the above loop.
3616
3617*/
3618
3619/* Build an atomic assignment at LOC, expanding into the proper
3620 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3621 the result of the operation, unless RETURN_OLD_P in which case
3622 return the old value of LHS (this is only for postincrement and
3623 postdecrement). */
3624static tree
3625build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3626 tree rhs, bool return_old_p)
3627{
3628 tree fndecl, func_call;
3629 vec<tree, va_gc> *params;
3630 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3631 tree old, old_addr;
3632 tree compound_stmt;
3633 tree stmt, goto_stmt;
3634 tree loop_label, loop_decl, done_label, done_decl;
3635
3636 tree lhs_type = TREE_TYPE (lhs);
3637 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3638 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3639 tree rhs_type = TREE_TYPE (rhs);
3640
3641 gcc_assert (TYPE_ATOMIC (lhs_type));
3642
3643 if (return_old_p)
3644 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3645
3646 /* Allocate enough vector items for a compare_exchange. */
3647 vec_alloc (params, 6);
3648
3649 /* Create a compound statement to hold the sequence of statements
3650 with a loop. */
3651 compound_stmt = c_begin_compound_stmt (false);
3652
3653 /* Fold the RHS if it hasn't already been folded. */
3654 if (modifycode != NOP_EXPR)
3655 rhs = c_fully_fold (rhs, false, NULL);
3656
3657 /* Remove the qualifiers for the rest of the expressions and create
3658 the VAL temp variable to hold the RHS. */
3659 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3660 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
b731b390 3661 val = create_tmp_var (nonatomic_rhs_type);
267bac10 3662 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3663 TREE_NO_WARNING (val) = 1;
267bac10
JM
3664 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3665 SET_EXPR_LOCATION (rhs, loc);
3666 add_stmt (rhs);
3667
3668 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3669 an atomic_store. */
3670 if (modifycode == NOP_EXPR)
3671 {
3672 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3673 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3674 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3675 params->quick_push (lhs_addr);
3676 params->quick_push (rhs);
3677 params->quick_push (seq_cst);
8edbfaa6 3678 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3679 add_stmt (func_call);
3680
3681 /* Finish the compound statement. */
3682 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3683
3684 /* VAL is the value which was stored, return a COMPOUND_STMT of
3685 the statement and that value. */
3686 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3687 }
3688
3689 /* Create the variables and labels required for the op= form. */
b731b390 3690 old = create_tmp_var (nonatomic_lhs_type);
267bac10 3691 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3692 TREE_ADDRESSABLE (old) = 1;
3693 TREE_NO_WARNING (old) = 1;
267bac10 3694
b731b390 3695 newval = create_tmp_var (nonatomic_lhs_type);
267bac10
JM
3696 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3697 TREE_ADDRESSABLE (newval) = 1;
3698
3699 loop_decl = create_artificial_label (loc);
3700 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3701
3702 done_decl = create_artificial_label (loc);
3703 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3704
3705 /* __atomic_load (addr, &old, SEQ_CST). */
3706 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3707 params->quick_push (lhs_addr);
3708 params->quick_push (old_addr);
3709 params->quick_push (seq_cst);
8edbfaa6 3710 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3711 add_stmt (func_call);
3712 params->truncate (0);
3713
3714 /* Create the expressions for floating-point environment
3715 manipulation, if required. */
3716 bool need_fenv = (flag_trapping_math
3717 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3718 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3719 if (need_fenv)
3720 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3721
3722 if (hold_call)
3723 add_stmt (hold_call);
3724
3725 /* loop: */
3726 add_stmt (loop_label);
3727
3728 /* newval = old + val; */
3729 rhs = build_binary_op (loc, modifycode, old, val, 1);
68fca595
MP
3730 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3731 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3732 NULL_TREE, 0);
3733 if (rhs != error_mark_node)
3734 {
3735 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3736 SET_EXPR_LOCATION (rhs, loc);
3737 add_stmt (rhs);
3738 }
3739
3740 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3741 goto done; */
3742 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3743 params->quick_push (lhs_addr);
3744 params->quick_push (old_addr);
3745 params->quick_push (newval_addr);
3746 params->quick_push (integer_zero_node);
3747 params->quick_push (seq_cst);
3748 params->quick_push (seq_cst);
8edbfaa6 3749 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3750
3751 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3752 SET_EXPR_LOCATION (goto_stmt, loc);
3753
3754 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3755 SET_EXPR_LOCATION (stmt, loc);
3756 add_stmt (stmt);
3757
3758 if (clear_call)
3759 add_stmt (clear_call);
3760
3761 /* goto loop; */
3762 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3763 SET_EXPR_LOCATION (goto_stmt, loc);
3764 add_stmt (goto_stmt);
3765
3766 /* done: */
3767 add_stmt (done_label);
3768
3769 if (update_call)
3770 add_stmt (update_call);
3771
3772 /* Finish the compound statement. */
3773 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3774
3775 /* NEWVAL is the value that was successfully stored, return a
3776 COMPOUND_EXPR of the statement and the appropriate value. */
3777 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3778 return_old_p ? old : newval);
3779}
3780
3e4093b6
RS
3781/* Construct and perhaps optimize a tree representation
3782 for a unary operation. CODE, a tree_code, specifies the operation
3783 and XARG is the operand.
3784 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3785 the default promotions (such as from short to int).
3786 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3787 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3788 arrays to pointers in C99.
3789
3790 LOCATION is the location of the operator. */
400fbf9f 3791
3e4093b6 3792tree
c9f9eb5d
AH
3793build_unary_op (location_t location,
3794 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3795{
3796 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3797 tree arg = xarg;
3798 tree argtype = 0;
808d6eaa 3799 enum tree_code typecode;
3e4093b6 3800 tree val;
c9f9eb5d 3801 tree ret = error_mark_node;
8ce94e44 3802 tree eptype = NULL_TREE;
3e4093b6 3803 int noconvert = flag;
4de67c26 3804 const char *invalid_op_diag;
928c19bb
JM
3805 bool int_operands;
3806
3807 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3808 if (int_operands)
3809 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3810
808d6eaa
JM
3811 if (code != ADDR_EXPR)
3812 arg = require_complete_type (arg);
3813
3814 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3815 if (typecode == ERROR_MARK)
3816 return error_mark_node;
3817 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3818 typecode = INTEGER_TYPE;
6c36d76b 3819
4de67c26
JM
3820 if ((invalid_op_diag
3821 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3822 {
c9f9eb5d 3823 error_at (location, invalid_op_diag);
4de67c26
JM
3824 return error_mark_node;
3825 }
3826
8ce94e44
JM
3827 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3828 {
3829 eptype = TREE_TYPE (arg);
3830 arg = TREE_OPERAND (arg, 0);
3831 }
3832
3e4093b6
RS
3833 switch (code)
3834 {
3835 case CONVERT_EXPR:
3836 /* This is used for unary plus, because a CONVERT_EXPR
3837 is enough to prevent anybody from looking inside for
3838 associativity, but won't generate any code. */
3839 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3840 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3841 || typecode == VECTOR_TYPE))
400fbf9f 3842 {
c9f9eb5d 3843 error_at (location, "wrong type argument to unary plus");
3e4093b6 3844 return error_mark_node;
400fbf9f 3845 }
3e4093b6
RS
3846 else if (!noconvert)
3847 arg = default_conversion (arg);
db3927fb 3848 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3849 break;
3850
3e4093b6
RS
3851 case NEGATE_EXPR:
3852 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3853 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3854 || typecode == VECTOR_TYPE))
3855 {
c9f9eb5d 3856 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3857 return error_mark_node;
3858 }
3859 else if (!noconvert)
3860 arg = default_conversion (arg);
400fbf9f
JW
3861 break;
3862
3e4093b6 3863 case BIT_NOT_EXPR:
462643f0
AP
3864 /* ~ works on integer types and non float vectors. */
3865 if (typecode == INTEGER_TYPE
3866 || (typecode == VECTOR_TYPE
3867 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3868 {
3e4093b6
RS
3869 if (!noconvert)
3870 arg = default_conversion (arg);
03d5b1f5 3871 }
3e4093b6 3872 else if (typecode == COMPLEX_TYPE)
400fbf9f 3873 {
3e4093b6 3874 code = CONJ_EXPR;
c1771a20 3875 pedwarn (location, OPT_Wpedantic,
fcf73884 3876 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3877 if (!noconvert)
3878 arg = default_conversion (arg);
3879 }
3880 else
3881 {
c9f9eb5d 3882 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3883 return error_mark_node;
400fbf9f
JW
3884 }
3885 break;
3886
3e4093b6 3887 case ABS_EXPR:
11017cc7 3888 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3889 {
c9f9eb5d 3890 error_at (location, "wrong type argument to abs");
3e4093b6 3891 return error_mark_node;
400fbf9f 3892 }
3e4093b6
RS
3893 else if (!noconvert)
3894 arg = default_conversion (arg);
400fbf9f
JW
3895 break;
3896
3e4093b6
RS
3897 case CONJ_EXPR:
3898 /* Conjugating a real value is a no-op, but allow it anyway. */
3899 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3900 || typecode == COMPLEX_TYPE))
400fbf9f 3901 {
c9f9eb5d 3902 error_at (location, "wrong type argument to conjugation");
3e4093b6 3903 return error_mark_node;
400fbf9f 3904 }
3e4093b6
RS
3905 else if (!noconvert)
3906 arg = default_conversion (arg);
400fbf9f
JW
3907 break;
3908
3e4093b6 3909 case TRUTH_NOT_EXPR:
ab22c1fa 3910 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3911 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3912 && typecode != COMPLEX_TYPE)
400fbf9f 3913 {
c9f9eb5d
AH
3914 error_at (location,
3915 "wrong type argument to unary exclamation mark");
3e4093b6 3916 return error_mark_node;
400fbf9f 3917 }
a27d595d
JM
3918 if (int_operands)
3919 {
3920 arg = c_objc_common_truthvalue_conversion (location, xarg);
3921 arg = remove_c_maybe_const_expr (arg);
3922 }
3923 else
3924 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3925 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3926 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3927 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3928 location = EXPR_LOCATION (ret);
c9f9eb5d 3929 goto return_build_unary_op;
3e4093b6 3930
3e4093b6 3931 case REALPART_EXPR:
3e4093b6 3932 case IMAGPART_EXPR:
fb52b50a
NF
3933 ret = build_real_imag_expr (location, code, arg);
3934 if (ret == error_mark_node)
3935 return error_mark_node;
8ce94e44
JM
3936 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3937 eptype = TREE_TYPE (eptype);
c9f9eb5d 3938 goto return_build_unary_op;
3e4093b6
RS
3939
3940 case PREINCREMENT_EXPR:
3941 case POSTINCREMENT_EXPR:
3942 case PREDECREMENT_EXPR:
3943 case POSTDECREMENT_EXPR:
3e4093b6 3944
928c19bb
JM
3945 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3946 {
3947 tree inner = build_unary_op (location, code,
3948 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3949 if (inner == error_mark_node)
3950 return error_mark_node;
3951 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3952 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3953 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3954 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3955 goto return_build_unary_op;
3956 }
3957
925e8657
NP
3958 /* Complain about anything that is not a true lvalue. In
3959 Objective-C, skip this check for property_refs. */
f90e8e2e 3960 if (!objc_is_property_ref (arg)
7bd11157
TT
3961 && !lvalue_or_else (location,
3962 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3963 || code == POSTINCREMENT_EXPR)
3964 ? lv_increment
3965 : lv_decrement)))
928c19bb
JM
3966 return error_mark_node;
3967
09639a83
ILT
3968 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3969 {
3970 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3971 warning_at (location, OPT_Wc___compat,
3972 "increment of enumeration value is invalid in C++");
3973 else
3974 warning_at (location, OPT_Wc___compat,
3975 "decrement of enumeration value is invalid in C++");
3976 }
3977
928c19bb
JM
3978 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3979 arg = c_fully_fold (arg, false, NULL);
3980
267bac10
JM
3981 bool atomic_op;
3982 atomic_op = really_atomic_lvalue (arg);
3983
3e4093b6
RS
3984 /* Increment or decrement the real part of the value,
3985 and don't change the imaginary part. */
3986 if (typecode == COMPLEX_TYPE)
400fbf9f 3987 {
3e4093b6
RS
3988 tree real, imag;
3989
c1771a20 3990 pedwarn (location, OPT_Wpedantic,
509c9d60 3991 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3992
267bac10
JM
3993 if (!atomic_op)
3994 {
3995 arg = stabilize_reference (arg);
3996 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3997 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3998 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3999 if (real == error_mark_node || imag == error_mark_node)
4000 return error_mark_node;
4001 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4002 real, imag);
4003 goto return_build_unary_op;
4004 }
400fbf9f 4005 }
3e4093b6
RS
4006
4007 /* Report invalid types. */
4008
ab22c1fa 4009 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4010 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4011 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4012 {
3e4093b6 4013 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4014 error_at (location, "wrong type argument to increment");
c22cacf3 4015 else
c9f9eb5d 4016 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4017
4018 return error_mark_node;
400fbf9f 4019 }
400fbf9f 4020
3e4093b6
RS
4021 {
4022 tree inc;
400fbf9f 4023
3e4093b6
RS
4024 argtype = TREE_TYPE (arg);
4025
4026 /* Compute the increment. */
4027
4028 if (typecode == POINTER_TYPE)
4029 {
a0e24419 4030 /* If pointer target is an incomplete type,
3e4093b6 4031 we just cannot know how to do the arithmetic. */
b70cef5d 4032 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4033 {
4034 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4035 error_at (location,
a0e24419
MP
4036 "increment of pointer to an incomplete type %qT",
4037 TREE_TYPE (argtype));
3e4093b6 4038 else
c9f9eb5d 4039 error_at (location,
a0e24419
MP
4040 "decrement of pointer to an incomplete type %qT",
4041 TREE_TYPE (argtype));
3e4093b6 4042 }
b70cef5d
JJ
4043 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4044 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4045 {
3e4093b6 4046 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4047 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4048 "wrong type argument to increment");
3e4093b6 4049 else
44d90fe1 4050 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4051 "wrong type argument to decrement");
3e4093b6
RS
4052 }
4053
b70cef5d 4054 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4055 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4056 }
b70cef5d 4057 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4058 {
4059 /* For signed fract types, we invert ++ to -- or
4060 -- to ++, and change inc from 1 to -1, because
4061 it is not possible to represent 1 in signed fract constants.
4062 For unsigned fract types, the result always overflows and
4063 we get an undefined (original) or the maximum value. */
4064 if (code == PREINCREMENT_EXPR)
4065 code = PREDECREMENT_EXPR;
4066 else if (code == PREDECREMENT_EXPR)
4067 code = PREINCREMENT_EXPR;
4068 else if (code == POSTINCREMENT_EXPR)
4069 code = POSTDECREMENT_EXPR;
4070 else /* code == POSTDECREMENT_EXPR */
4071 code = POSTINCREMENT_EXPR;
4072
4073 inc = integer_minus_one_node;
4074 inc = convert (argtype, inc);
4075 }
3e4093b6 4076 else
5be014d5 4077 {
241b71bb
TV
4078 inc = VECTOR_TYPE_P (argtype)
4079 ? build_one_cst (argtype)
4080 : integer_one_node;
5be014d5
AP
4081 inc = convert (argtype, inc);
4082 }
3e4093b6 4083
925e8657
NP
4084 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4085 need to ask Objective-C to build the increment or decrement
4086 expression for it. */
4087 if (objc_is_property_ref (arg))
f90e8e2e 4088 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4089 arg, inc);
4090
3e4093b6 4091 /* Report a read-only lvalue. */
f37acdf9 4092 if (TYPE_READONLY (argtype))
953ff289 4093 {
c02065fc 4094 readonly_error (location, arg,
953ff289
DN
4095 ((code == PREINCREMENT_EXPR
4096 || code == POSTINCREMENT_EXPR)
4097 ? lv_increment : lv_decrement));
4098 return error_mark_node;
4099 }
f37acdf9
JM
4100 else if (TREE_READONLY (arg))
4101 readonly_warning (arg,
4102 ((code == PREINCREMENT_EXPR
4103 || code == POSTINCREMENT_EXPR)
4104 ? lv_increment : lv_decrement));
3e4093b6 4105
267bac10
JM
4106 /* If the argument is atomic, use the special code sequences for
4107 atomic compound assignment. */
4108 if (atomic_op)
4109 {
4110 arg = stabilize_reference (arg);
4111 ret = build_atomic_assign (location, arg,
4112 ((code == PREINCREMENT_EXPR
4113 || code == POSTINCREMENT_EXPR)
4114 ? PLUS_EXPR
4115 : MINUS_EXPR),
4116 (FRACT_MODE_P (TYPE_MODE (argtype))
4117 ? inc
4118 : integer_one_node),
4119 (code == POSTINCREMENT_EXPR
4120 || code == POSTDECREMENT_EXPR));
4121 goto return_build_unary_op;
4122 }
4123
3e4093b6
RS
4124 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4125 val = boolean_increment (code, arg);
4126 else
53fb4de3 4127 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4128 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4129 if (TREE_CODE (val) != code)
6de9cd9a 4130 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4131 ret = val;
4132 goto return_build_unary_op;
3e4093b6
RS
4133 }
4134
4135 case ADDR_EXPR:
4136 /* Note that this operation never does default_conversion. */
4137
2b4b7036
JM
4138 /* The operand of unary '&' must be an lvalue (which excludes
4139 expressions of type void), or, in C99, the result of a [] or
4140 unary '*' operator. */
4141 if (VOID_TYPE_P (TREE_TYPE (arg))
4142 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4143 && (TREE_CODE (arg) != INDIRECT_REF
4144 || !flag_isoc99))
4145 pedwarn (location, 0, "taking address of expression of type %<void%>");
4146
3e4093b6
RS
4147 /* Let &* cancel out to simplify resulting code. */
4148 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 4149 {
3e4093b6
RS
4150 /* Don't let this be an lvalue. */
4151 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4152 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4153 ret = TREE_OPERAND (arg, 0);
4154 goto return_build_unary_op;
400fbf9f 4155 }
1eb8759b 4156
7c672dfc 4157 /* For &x[y], return x+y */
3e4093b6 4158 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4159 {
f2a71bbc
JM
4160 tree op0 = TREE_OPERAND (arg, 0);
4161 if (!c_mark_addressable (op0))
3e4093b6 4162 return error_mark_node;
1eb8759b 4163 }
1eb8759b 4164
3e4093b6
RS
4165 /* Anything not already handled and not a true memory reference
4166 or a non-lvalue array is an error. */
4167 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4168 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4169 return error_mark_node;
b6a10c9f 4170
928c19bb
JM
4171 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4172 folding later. */
4173 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4174 {
4175 tree inner = build_unary_op (location, code,
4176 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4177 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4178 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4179 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4180 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4181 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4182 goto return_build_unary_op;
4183 }
4184
3e4093b6
RS
4185 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4186 argtype = TREE_TYPE (arg);
400fbf9f 4187
3e4093b6 4188 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4189 to which the address will point. This is only needed
f2c1da78 4190 for function types. */
6615c446 4191 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4192 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4193 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4194 {
4195 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4196 int quals = orig_quals;
4197
4198 if (TREE_READONLY (arg))
4199 quals |= TYPE_QUAL_CONST;
4200 if (TREE_THIS_VOLATILE (arg))
4201 quals |= TYPE_QUAL_VOLATILE;
4202
f2c1da78
JM
4203 argtype = c_build_qualified_type (argtype, quals);
4204 }
400fbf9f 4205
3e4093b6
RS
4206 if (!c_mark_addressable (arg))
4207 return error_mark_node;
400fbf9f 4208
abb54d14
JM
4209 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4210 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4211
5cc200fc 4212 argtype = build_pointer_type (argtype);
5e55f99d
RH
4213
4214 /* ??? Cope with user tricks that amount to offsetof. Delete this
4215 when we have proper support for integer constant expressions. */
4216 val = get_base_address (arg);
4217 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
4218 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4219 {
cf9e9959 4220 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4221 goto return_build_unary_op;
3aa2ddb8 4222 }
5e55f99d 4223
5cc200fc 4224 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4225
c9f9eb5d
AH
4226 ret = val;
4227 goto return_build_unary_op;
400fbf9f 4228
3e4093b6 4229 default:
1344f9a3 4230 gcc_unreachable ();
3e4093b6 4231 }
400fbf9f 4232
3e4093b6
RS
4233 if (argtype == 0)
4234 argtype = TREE_TYPE (arg);
928c19bb
JM
4235 if (TREE_CODE (arg) == INTEGER_CST)
4236 ret = (require_constant_value
db3927fb
AH
4237 ? fold_build1_initializer_loc (location, code, argtype, arg)
4238 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4239 else
4240 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4241 return_build_unary_op:
4242 gcc_assert (ret != error_mark_node);
928c19bb
JM
4243 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4244 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4245 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4246 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4247 ret = note_integer_operands (ret);
8ce94e44
JM
4248 if (eptype)
4249 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4250 protected_set_expr_location (ret, location);
4251 return ret;
3e4093b6 4252}
400fbf9f 4253
3e4093b6
RS
4254/* Return nonzero if REF is an lvalue valid for this language.
4255 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4256 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4257
1e4ae551 4258bool
58f9752a 4259lvalue_p (const_tree ref)
3e4093b6 4260{
58f9752a 4261 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4262
3e4093b6
RS
4263 switch (code)
4264 {
4265 case REALPART_EXPR:
4266 case IMAGPART_EXPR:
4267 case COMPONENT_REF:
4268 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4269
928c19bb
JM
4270 case C_MAYBE_CONST_EXPR:
4271 return lvalue_p (TREE_OPERAND (ref, 1));
4272
3e4093b6
RS
4273 case COMPOUND_LITERAL_EXPR:
4274 case STRING_CST:
4275 return 1;
400fbf9f 4276
3e4093b6
RS
4277 case INDIRECT_REF:
4278 case ARRAY_REF:
36536d79 4279 case ARRAY_NOTATION_REF:
3e4093b6
RS
4280 case VAR_DECL:
4281 case PARM_DECL:
4282 case RESULT_DECL:
4283 case ERROR_MARK:
4284 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4285 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4286
3e4093b6 4287 case BIND_EXPR:
3e4093b6 4288 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4289
3e4093b6
RS
4290 default:
4291 return 0;
4292 }
4293}
400fbf9f 4294\f
f37acdf9
JM
4295/* Give a warning for storing in something that is read-only in GCC
4296 terms but not const in ISO C terms. */
4297
4298static void
4299readonly_warning (tree arg, enum lvalue_use use)
4300{
4301 switch (use)
4302 {
4303 case lv_assign:
4304 warning (0, "assignment of read-only location %qE", arg);
4305 break;
4306 case lv_increment:
4307 warning (0, "increment of read-only location %qE", arg);
4308 break;
4309 case lv_decrement:
4310 warning (0, "decrement of read-only location %qE", arg);
4311 break;
4312 default:
4313 gcc_unreachable ();
4314 }
4315 return;
4316}
4317
37dc0d8d
JM
4318
4319/* Return nonzero if REF is an lvalue valid for this language;
4320 otherwise, print an error message and return zero. USE says
7bd11157
TT
4321 how the lvalue is being used and so selects the error message.
4322 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4323
4324static int
7bd11157 4325lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4326{
4327 int win = lvalue_p (ref);
4328
4329 if (!win)
7bd11157 4330 lvalue_error (loc, use);
37dc0d8d
JM
4331
4332 return win;
4333}
3e4093b6
RS
4334\f
4335/* Mark EXP saying that we need to be able to take the
4336 address of it; it should not be allocated in a register.
4337 Returns true if successful. */
54c93c30 4338
3e4093b6
RS
4339bool
4340c_mark_addressable (tree exp)
400fbf9f 4341{
3e4093b6 4342 tree x = exp;
95602da1 4343
3e4093b6
RS
4344 while (1)
4345 switch (TREE_CODE (x))
4346 {
4347 case COMPONENT_REF:
4348 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4349 {
0039fa55
AN
4350 error
4351 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4352 return false;
4353 }
95602da1 4354
3e4093b6 4355 /* ... fall through ... */
95602da1 4356
3e4093b6
RS
4357 case ADDR_EXPR:
4358 case ARRAY_REF:
4359 case REALPART_EXPR:
4360 case IMAGPART_EXPR:
4361 x = TREE_OPERAND (x, 0);
4362 break;
95602da1 4363
3e4093b6
RS
4364 case COMPOUND_LITERAL_EXPR:
4365 case CONSTRUCTOR:
4366 TREE_ADDRESSABLE (x) = 1;
4367 return true;
95602da1 4368
3e4093b6
RS
4369 case VAR_DECL:
4370 case CONST_DECL:
4371 case PARM_DECL:
4372 case RESULT_DECL:
5baeaac0 4373 if (C_DECL_REGISTER (x)
3e4093b6
RS
4374 && DECL_NONLOCAL (x))
4375 {
e697b20f 4376 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4377 {
0039fa55
AN
4378 error
4379 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4380 return false;
4381 }
509c9d60 4382 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4383 }
5baeaac0 4384 else if (C_DECL_REGISTER (x))
3e4093b6 4385 {
e697b20f 4386 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4387 error ("address of global register variable %qD requested", x);
4388 else
4389 error ("address of register variable %qD requested", x);
4390 return false;
3e4093b6 4391 }
400fbf9f 4392
3e4093b6
RS
4393 /* drops in */
4394 case FUNCTION_DECL:
4395 TREE_ADDRESSABLE (x) = 1;
4396 /* drops out */
4397 default:
4398 return true;
4399 }
4400}
4401\f
2d2e923f
MLI
4402/* Convert EXPR to TYPE, warning about conversion problems with
4403 constants. SEMANTIC_TYPE is the type this conversion would use
4404 without excess precision. If SEMANTIC_TYPE is NULL, this function
4405 is equivalent to convert_and_check. This function is a wrapper that
4406 handles conversions that may be different than
4407 the usual ones because of excess precision. */
4408
4409static tree
68fca595
MP
4410ep_convert_and_check (location_t loc, tree type, tree expr,
4411 tree semantic_type)
2d2e923f
MLI
4412{
4413 if (TREE_TYPE (expr) == type)
4414 return expr;
4415
4416 if (!semantic_type)
68fca595 4417 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4418
4419 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4420 && TREE_TYPE (expr) != semantic_type)
4421 {
4422 /* For integers, we need to check the real conversion, not
4423 the conversion to the excess precision type. */
68fca595 4424 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4425 }
4426 /* Result type is the excess precision type, which should be
4427 large enough, so do not check. */
4428 return convert (type, expr);
4429}
4430
928c19bb
JM
4431/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4432 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4433 if folded to an integer constant then the unselected half may
4434 contain arbitrary operations not normally permitted in constant
c2255bc4 4435 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4436
4437tree
744aa42f 4438build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4439 tree op1, tree op1_original_type, tree op2,
4440 tree op2_original_type)
400fbf9f 4441{
3e4093b6
RS
4442 tree type1;
4443 tree type2;
4444 enum tree_code code1;
4445 enum tree_code code2;
4446 tree result_type = NULL;
2d2e923f 4447 tree semantic_result_type = NULL;
3e4093b6 4448 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4449 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4450 bool ifexp_int_operands;
928c19bb 4451 tree ret;
400fbf9f 4452
4d84fe7c
JM
4453 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4454 if (op1_int_operands)
4455 op1 = remove_c_maybe_const_expr (op1);
4456 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4457 if (op2_int_operands)
4458 op2 = remove_c_maybe_const_expr (op2);
4459 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4460 if (ifexp_int_operands)
4461 ifexp = remove_c_maybe_const_expr (ifexp);
4462
3e4093b6
RS
4463 /* Promote both alternatives. */
4464
4465 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4466 op1 = default_conversion (op1);
4467 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4468 op2 = default_conversion (op2);
4469
4470 if (TREE_CODE (ifexp) == ERROR_MARK
4471 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4472 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4473 return error_mark_node;
400fbf9f 4474
3e4093b6
RS
4475 type1 = TREE_TYPE (op1);
4476 code1 = TREE_CODE (type1);
4477 type2 = TREE_TYPE (op2);
4478 code2 = TREE_CODE (type2);
4479
b1adf557
JM
4480 /* C90 does not permit non-lvalue arrays in conditional expressions.
4481 In C99 they will be pointers by now. */
4482 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4483 {
744aa42f 4484 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4485 return error_mark_node;
4486 }
4487
8ce94e44
JM
4488 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4489 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4490 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4491 || code1 == COMPLEX_TYPE)
4492 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4493 || code2 == COMPLEX_TYPE))
4494 {
2d2e923f 4495 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4496 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4497 {
4498 op1 = TREE_OPERAND (op1, 0);
4499 type1 = TREE_TYPE (op1);
4500 gcc_assert (TREE_CODE (type1) == code1);
4501 }
4502 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4503 {
4504 op2 = TREE_OPERAND (op2, 0);
4505 type2 = TREE_TYPE (op2);
4506 gcc_assert (TREE_CODE (type2) == code2);
4507 }
4508 }
4509
d130ae11
ILT
4510 if (warn_cxx_compat)
4511 {
4512 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4513 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4514
4515 if (TREE_CODE (t1) == ENUMERAL_TYPE
4516 && TREE_CODE (t2) == ENUMERAL_TYPE
4517 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4518 warning_at (colon_loc, OPT_Wc___compat,
4519 ("different enum types in conditional is "
4520 "invalid in C++: %qT vs %qT"),
4521 t1, t2);
4522 }
4523
3e4093b6
RS
4524 /* Quickly detect the usual case where op1 and op2 have the same type
4525 after promotion. */
4526 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4527 {
3e4093b6
RS
4528 if (type1 == type2)
4529 result_type = type1;
4530 else
4531 result_type = TYPE_MAIN_VARIANT (type1);
4532 }
4533 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4534 || code1 == COMPLEX_TYPE)
4535 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4536 || code2 == COMPLEX_TYPE))
3e4093b6 4537 {
ccf7f880 4538 result_type = c_common_type (type1, type2);
0a0b3574
MM
4539 do_warn_double_promotion (result_type, type1, type2,
4540 "implicit conversion from %qT to %qT to "
4541 "match other result of conditional",
4542 colon_loc);
400fbf9f 4543
3e4093b6
RS
4544 /* If -Wsign-compare, warn here if type1 and type2 have
4545 different signedness. We'll promote the signed to unsigned
4546 and later code won't know it used to be different.
4547 Do this check on the original types, so that explicit casts
4548 will be considered, but default promotions won't. */
7d882b83 4549 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4550 {
8df83eae
RK
4551 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4552 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4553
3e4093b6
RS
4554 if (unsigned_op1 ^ unsigned_op2)
4555 {
6ac01510
ILT
4556 bool ovf;
4557
3e4093b6
RS
4558 /* Do not warn if the result type is signed, since the
4559 signed type will only be chosen if it can represent
4560 all the values of the unsigned type. */
3f75a254 4561 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4562 /* OK */;
3e4093b6 4563 else
928c19bb
JM
4564 {
4565 bool op1_maybe_const = true;
4566 bool op2_maybe_const = true;
4567
4568 /* Do not warn if the signed quantity is an
4569 unsuffixed integer literal (or some static
4570 constant expression involving such literals) and
4571 it is non-negative. This warning requires the
4572 operands to be folded for best results, so do
4573 that folding in this case even without
4574 warn_sign_compare to avoid warning options
4575 possibly affecting code generation. */
f5178456
RS
4576 c_inhibit_evaluation_warnings
4577 += (ifexp == truthvalue_false_node);
928c19bb
JM
4578 op1 = c_fully_fold (op1, require_constant_value,
4579 &op1_maybe_const);
f5178456
RS
4580 c_inhibit_evaluation_warnings
4581 -= (ifexp == truthvalue_false_node);
4582
4583 c_inhibit_evaluation_warnings
4584 += (ifexp == truthvalue_true_node);
928c19bb
JM
4585 op2 = c_fully_fold (op2, require_constant_value,
4586 &op2_maybe_const);
f5178456
RS
4587 c_inhibit_evaluation_warnings
4588 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4589
4590 if (warn_sign_compare)
4591 {
4592 if ((unsigned_op2
4593 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4594 || (unsigned_op1
4595 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4596 /* OK */;
4597 else
744aa42f
ILT
4598 warning_at (colon_loc, OPT_Wsign_compare,
4599 ("signed and unsigned type in "
4600 "conditional expression"));
928c19bb
JM
4601 }
4602 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4603 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4604 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4605 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4606 }
3e4093b6
RS
4607 }
4608 }
4609 }
4610 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4611 {
fcf73884 4612 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4613 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4614 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4615 result_type = void_type_node;
4616 }
4617 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4618 {
36c5e70a
BE
4619 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4620 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4621 addr_space_t as_common;
4622
744aa42f 4623 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4624 result_type = common_pointer_type (type1, type2);
6aa3c60d 4625 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4626 result_type = type2;
6aa3c60d 4627 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4628 result_type = type1;
4629 else if (!addr_space_superset (as1, as2, &as_common))
4630 {
4631 error_at (colon_loc, "pointers to disjoint address spaces "
4632 "used in conditional expression");
4633 return error_mark_node;
4634 }
267bac10
JM
4635 else if (VOID_TYPE_P (TREE_TYPE (type1))
4636 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4637 {
768952be
MU
4638 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4639 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4640 & ~TYPE_QUALS (TREE_TYPE (type1))))
4641 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4642 "pointer to array loses qualifier "
4643 "in conditional expression");
4644
fcf73884 4645 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4646 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4647 "ISO C forbids conditional expr between "
bda67431 4648 "%<void *%> and function pointer");
3e4093b6
RS
4649 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4650 TREE_TYPE (type2)));
34a80643 4651 }
267bac10
JM
4652 else if (VOID_TYPE_P (TREE_TYPE (type2))
4653 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4654 {
768952be
MU
4655 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4656 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4657 & ~TYPE_QUALS (TREE_TYPE (type2))))
4658 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4659 "pointer to array loses qualifier "
4660 "in conditional expression");
4661
fcf73884 4662 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4663 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4664 "ISO C forbids conditional expr between "
bda67431 4665 "%<void *%> and function pointer");
3e4093b6
RS
4666 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4667 TREE_TYPE (type1)));
1c2a9b35 4668 }
b581b85b
NP
4669 /* Objective-C pointer comparisons are a bit more lenient. */
4670 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4671 result_type = objc_common_type (type1, type2);
34a80643 4672 else
ab87f8c8 4673 {
36c5e70a
BE
4674 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4675
b581b85b
NP
4676 pedwarn (colon_loc, 0,
4677 "pointer type mismatch in conditional expression");
36c5e70a
BE
4678 result_type = build_pointer_type
4679 (build_qualified_type (void_type_node, qual));
ab87f8c8 4680 }
3e4093b6
RS
4681 }
4682 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4683 {
6aa3c60d 4684 if (!null_pointer_constant_p (orig_op2))
744aa42f 4685 pedwarn (colon_loc, 0,
509c9d60 4686 "pointer/integer type mismatch in conditional expression");
3e4093b6 4687 else
ab87f8c8 4688 {
3e4093b6 4689 op2 = null_pointer_node;
ab87f8c8 4690 }
3e4093b6
RS
4691 result_type = type1;
4692 }
4693 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4694 {
6aa3c60d 4695 if (!null_pointer_constant_p (orig_op1))
744aa42f 4696 pedwarn (colon_loc, 0,
509c9d60 4697 "pointer/integer type mismatch in conditional expression");
3e4093b6 4698 else
ab87f8c8 4699 {
3e4093b6 4700 op1 = null_pointer_node;
ab87f8c8 4701 }
3e4093b6
RS
4702 result_type = type2;
4703 }
1c2a9b35 4704
3e4093b6
RS
4705 if (!result_type)
4706 {
4707 if (flag_cond_mismatch)
4708 result_type = void_type_node;
4709 else
400fbf9f 4710 {
c2255bc4 4711 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4712 return error_mark_node;
400fbf9f 4713 }
3e4093b6 4714 }
400fbf9f 4715
3e4093b6
RS
4716 /* Merge const and volatile flags of the incoming types. */
4717 result_type
4718 = build_type_variant (result_type,
afbd0665
AS
4719 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4720 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4721
68fca595
MP
4722 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4723 semantic_result_type);
4724 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4725 semantic_result_type);
b6a10c9f 4726
928c19bb
JM
4727 if (ifexp_bcp && ifexp == truthvalue_true_node)
4728 {
4729 op2_int_operands = true;
4730 op1 = c_fully_fold (op1, require_constant_value, NULL);
4731 }
4732 if (ifexp_bcp && ifexp == truthvalue_false_node)
4733 {
4734 op1_int_operands = true;
4735 op2 = c_fully_fold (op2, require_constant_value, NULL);
4736 }
4d84fe7c 4737 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4738 && op1_int_operands
4739 && op2_int_operands);
4740 if (int_operands)
4741 {
4742 int_const = ((ifexp == truthvalue_true_node
4743 && TREE_CODE (orig_op1) == INTEGER_CST
4744 && !TREE_OVERFLOW (orig_op1))
4745 || (ifexp == truthvalue_false_node
4746 && TREE_CODE (orig_op2) == INTEGER_CST
4747 && !TREE_OVERFLOW (orig_op2)));
4748 }
4749 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4750 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4751 else
4752 {
01c7ccbb
JM
4753 if (int_operands)
4754 {
f34f1c87
MP
4755 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4756 nested inside of the expression. */
4757 op1 = c_fully_fold (op1, false, NULL);
4758 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4759 }
928c19bb
JM
4760 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4761 if (int_operands)
4762 ret = note_integer_operands (ret);
4763 }
2d2e923f
MLI
4764 if (semantic_result_type)
4765 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4766
c2255bc4 4767 protected_set_expr_location (ret, colon_loc);
928c19bb 4768 return ret;
3e4093b6
RS
4769}
4770\f
487a92fe 4771/* Return a compound expression that performs two expressions and
c2255bc4
AH
4772 returns the value of the second of them.
4773
4774 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4775
3e4093b6 4776tree
c2255bc4 4777build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4778{
4d84fe7c 4779 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4780 tree eptype = NULL_TREE;
928c19bb
JM
4781 tree ret;
4782
b72271b9 4783 if (flag_cilkplus
939b37da
BI
4784 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4785 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4786 {
4787 error_at (loc,
4788 "spawned function call cannot be part of a comma expression");
4789 return error_mark_node;
4790 }
4d84fe7c
JM
4791 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4792 if (expr1_int_operands)
4793 expr1 = remove_c_maybe_const_expr (expr1);
4794 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4795 if (expr2_int_operands)
4796 expr2 = remove_c_maybe_const_expr (expr2);
4797
8ce94e44
JM
4798 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4799 expr1 = TREE_OPERAND (expr1, 0);
4800 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4801 {
4802 eptype = TREE_TYPE (expr2);
4803 expr2 = TREE_OPERAND (expr2, 0);
4804 }
4805
3f75a254 4806 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4807 {
4808 /* The left-hand operand of a comma expression is like an expression
c5409249 4809 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4810 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4811 if (warn_unused_value)
47aecf47 4812 {
e14a6540 4813 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4814 && CONVERT_EXPR_P (expr1))
47aecf47 4815 ; /* (void) a, b */
e14a6540
JM
4816 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4817 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4818 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4819 ; /* (void) a, (void) b, c */
4820 else
b8698a0f 4821 warning_at (loc, OPT_Wunused_value,
c2255bc4 4822 "left-hand operand of comma expression has no effect");
47aecf47 4823 }
3e4093b6 4824 }
789eadcd
MP
4825 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4826 && warn_unused_value)
4827 {
4828 tree r = expr1;
4829 location_t cloc = loc;
4830 while (TREE_CODE (r) == COMPOUND_EXPR)
4831 {
4832 if (EXPR_HAS_LOCATION (r))
4833 cloc = EXPR_LOCATION (r);
4834 r = TREE_OPERAND (r, 1);
4835 }
4836 if (!TREE_SIDE_EFFECTS (r)
4837 && !VOID_TYPE_P (TREE_TYPE (r))
4838 && !CONVERT_EXPR_P (r))
4839 warning_at (cloc, OPT_Wunused_value,
4840 "right-hand operand of comma expression has no effect");
4841 }
400fbf9f 4842
3e4093b6
RS
4843 /* With -Wunused, we should also warn if the left-hand operand does have
4844 side-effects, but computes a value which is not used. For example, in
4845 `foo() + bar(), baz()' the result of the `+' operator is not used,
4846 so we should issue a warning. */
4847 else if (warn_unused_value)
c2255bc4 4848 warn_if_unused_value (expr1, loc);
400fbf9f 4849
e63d6886
AP
4850 if (expr2 == error_mark_node)
4851 return error_mark_node;
4852
928c19bb
JM
4853 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4854
4855 if (flag_isoc99
4d84fe7c
JM
4856 && expr1_int_operands
4857 && expr2_int_operands)
928c19bb
JM
4858 ret = note_integer_operands (ret);
4859
8ce94e44
JM
4860 if (eptype)
4861 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4862
c2255bc4 4863 protected_set_expr_location (ret, loc);
928c19bb 4864 return ret;
3e4093b6 4865}
400fbf9f 4866
67165eb3
ILT
4867/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4868 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4869 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4870 of the cast. -Wcast-qual appeared on the command line. Named
4871 address space qualifiers are not handled here, because they result
4872 in different warnings. */
67165eb3
ILT
4873
4874static void
2ee3cb35 4875handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4876{
4877 tree in_type = type;
4878 tree in_otype = otype;
4879 int added = 0;
4880 int discarded = 0;
4881 bool is_const;
4882
4883 /* Check that the qualifiers on IN_TYPE are a superset of the
4884 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4885 nodes is uninteresting and we stop as soon as we hit a
4886 non-POINTER_TYPE node on either type. */
4887 do
4888 {
4889 in_otype = TREE_TYPE (in_otype);
4890 in_type = TREE_TYPE (in_type);
4891
4892 /* GNU C allows cv-qualified function types. 'const' means the
4893 function is very pure, 'volatile' means it can't return. We
4894 need to warn when such qualifiers are added, not when they're
4895 taken away. */
4896 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4897 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4898 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4899 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4900 else
36c5e70a
BE
4901 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4902 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4903 }
4904 while (TREE_CODE (in_type) == POINTER_TYPE
4905 && TREE_CODE (in_otype) == POINTER_TYPE);
4906
4907 if (added)
2ee3cb35
MLI
4908 warning_at (loc, OPT_Wcast_qual,
4909 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4910
4911 if (discarded)
4912 /* There are qualifiers present in IN_OTYPE that are not present
4913 in IN_TYPE. */
2ee3cb35 4914 warning_at (loc, OPT_Wcast_qual,
7485aeea 4915 "cast discards %qv qualifier from pointer target type",
2ee3cb35 4916 discarded);
67165eb3
ILT
4917
4918 if (added || discarded)
4919 return;
4920
4921 /* A cast from **T to const **T is unsafe, because it can cause a
4922 const value to be changed with no additional warning. We only
4923 issue this warning if T is the same on both sides, and we only
4924 issue the warning if there are the same number of pointers on
4925 both sides, as otherwise the cast is clearly unsafe anyhow. A
4926 cast is unsafe when a qualifier is added at one level and const
4927 is not present at all outer levels.
4928
4929 To issue this warning, we check at each level whether the cast
4930 adds new qualifiers not already seen. We don't need to special
4931 case function types, as they won't have the same
4932 TYPE_MAIN_VARIANT. */
4933
4934 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4935 return;
4936 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4937 return;
4938
4939 in_type = type;
4940 in_otype = otype;
4941 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4942 do
4943 {
4944 in_type = TREE_TYPE (in_type);
4945 in_otype = TREE_TYPE (in_otype);
4946 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4947 && !is_const)
4948 {
2ee3cb35
MLI
4949 warning_at (loc, OPT_Wcast_qual,
4950 "to be safe all intermediate pointers in cast from "
4951 "%qT to %qT must be %<const%> qualified",
4952 otype, type);
67165eb3
ILT
4953 break;
4954 }
4955 if (is_const)
4956 is_const = TYPE_READONLY (in_type);
4957 }
4958 while (TREE_CODE (in_type) == POINTER_TYPE);
4959}
4960
b8698a0f 4961/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4962 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4963
3e4093b6 4964tree
c2255bc4 4965build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4966{
8ce94e44
JM
4967 tree value;
4968
4969 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4970 expr = TREE_OPERAND (expr, 0);
4971
4972 value = expr;
400fbf9f 4973
3e4093b6
RS
4974 if (type == error_mark_node || expr == error_mark_node)
4975 return error_mark_node;
400fbf9f 4976
3e4093b6
RS
4977 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4978 only in <protocol> qualifications. But when constructing cast expressions,
4979 the protocols do matter and must be kept around. */
700686fa
ZL
4980 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4981 return build1 (NOP_EXPR, type, expr);
4982
4983 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4984
3e4093b6
RS
4985 if (TREE_CODE (type) == ARRAY_TYPE)
4986 {
c2255bc4 4987 error_at (loc, "cast specifies array type");
3e4093b6
RS
4988 return error_mark_node;
4989 }
400fbf9f 4990
3e4093b6
RS
4991 if (TREE_CODE (type) == FUNCTION_TYPE)
4992 {
c2255bc4 4993 error_at (loc, "cast specifies function type");
3e4093b6
RS
4994 return error_mark_node;
4995 }
400fbf9f 4996
808d6eaa
JM
4997 if (!VOID_TYPE_P (type))
4998 {
4999 value = require_complete_type (value);
5000 if (value == error_mark_node)
5001 return error_mark_node;
5002 }
5003
3e4093b6
RS
5004 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5005 {
fcf73884
MLI
5006 if (TREE_CODE (type) == RECORD_TYPE
5007 || TREE_CODE (type) == UNION_TYPE)
c1771a20 5008 pedwarn (loc, OPT_Wpedantic,
fcf73884 5009 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5010
5011 /* Convert to remove any qualifiers from VALUE's type. */
5012 value = convert (type, value);
3e4093b6
RS
5013 }
5014 else if (TREE_CODE (type) == UNION_TYPE)
5015 {
5016 tree field;
400fbf9f 5017
910ad8de 5018 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5019 if (TREE_TYPE (field) != error_mark_node
5020 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5021 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5022 break;
5023
5024 if (field)
400fbf9f 5025 {
3e4093b6 5026 tree t;
e616f54d 5027 bool maybe_const = true;
3e4093b6 5028
c1771a20 5029 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5030 t = c_fully_fold (value, false, &maybe_const);
5031 t = build_constructor_single (type, field, t);
5032 if (!maybe_const)
5033 t = c_wrap_maybe_const (t, true);
5034 t = digest_init (loc, type, t,
bbbbb16a 5035 NULL_TREE, false, true, 0);
3e4093b6
RS
5036 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5037 return t;
400fbf9f 5038 }
c2255bc4 5039 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5040 return error_mark_node;
5041 }
5042 else
5043 {
5044 tree otype, ovalue;
400fbf9f 5045
3e4093b6 5046 if (type == void_type_node)
c2255bc4
AH
5047 {
5048 tree t = build1 (CONVERT_EXPR, type, value);
5049 SET_EXPR_LOCATION (t, loc);
5050 return t;
5051 }
400fbf9f 5052
3e4093b6 5053 otype = TREE_TYPE (value);
400fbf9f 5054
3e4093b6 5055 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5056 if (warn_cast_qual
5057 && TREE_CODE (type) == POINTER_TYPE
5058 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5059 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5060
36c5e70a
BE
5061 /* Warn about conversions between pointers to disjoint
5062 address spaces. */
5063 if (TREE_CODE (type) == POINTER_TYPE
5064 && TREE_CODE (otype) == POINTER_TYPE
5065 && !null_pointer_constant_p (value))
5066 {
5067 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5068 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5069 addr_space_t as_common;
5070
5071 if (!addr_space_superset (as_to, as_from, &as_common))
5072 {
5073 if (ADDR_SPACE_GENERIC_P (as_from))
5074 warning_at (loc, 0, "cast to %s address space pointer "
5075 "from disjoint generic address space pointer",
5076 c_addr_space_name (as_to));
5077
5078 else if (ADDR_SPACE_GENERIC_P (as_to))
5079 warning_at (loc, 0, "cast to generic address space pointer "
5080 "from disjoint %s address space pointer",
5081 c_addr_space_name (as_from));
5082
5083 else
5084 warning_at (loc, 0, "cast to %s address space pointer "
5085 "from disjoint %s address space pointer",
5086 c_addr_space_name (as_to),
5087 c_addr_space_name (as_from));
5088 }
5089 }
5090
3e4093b6 5091 /* Warn about possible alignment problems. */
3176a0c2 5092 if (STRICT_ALIGNMENT
3e4093b6
RS
5093 && TREE_CODE (type) == POINTER_TYPE
5094 && TREE_CODE (otype) == POINTER_TYPE
5095 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5096 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5097 /* Don't warn about opaque types, where the actual alignment
5098 restriction is unknown. */
5099 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5100 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5101 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5102 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5103 warning_at (loc, OPT_Wcast_align,
5104 "cast increases required alignment of target type");
e9a25f70 5105
3176a0c2 5106 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5107 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5108 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5109 /* Unlike conversion of integers to pointers, where the
5110 warning is disabled for converting constants because
5111 of cases such as SIG_*, warn about converting constant
5112 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5113 sign extension, and a warning is appropriate. */
c2255bc4
AH
5114 warning_at (loc, OPT_Wpointer_to_int_cast,
5115 "cast from pointer to integer of different size");
400fbf9f 5116
3176a0c2 5117 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5118 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5119 warning_at (loc, OPT_Wbad_function_cast,
5120 "cast from function call of type %qT "
5121 "to non-matching type %qT", otype, type);
400fbf9f 5122
3176a0c2 5123 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5124 && TREE_CODE (otype) == INTEGER_TYPE
5125 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5126 /* Don't warn about converting any constant. */
5127 && !TREE_CONSTANT (value))
c2255bc4
AH
5128 warning_at (loc,
5129 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5130 "of different size");
400fbf9f 5131
79bedddc
SR
5132 if (warn_strict_aliasing <= 2)
5133 strict_aliasing_warning (otype, type, expr);
400fbf9f 5134
3897f229
JM
5135 /* If pedantic, warn for conversions between function and object
5136 pointer types, except for converting a null pointer constant
5137 to function pointer type. */
5138 if (pedantic
5139 && TREE_CODE (type) == POINTER_TYPE
5140 && TREE_CODE (otype) == POINTER_TYPE
5141 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5142 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5143 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5144 "conversion of function pointer to object pointer type");
3897f229
JM
5145
5146 if (pedantic
5147 && TREE_CODE (type) == POINTER_TYPE
5148 && TREE_CODE (otype) == POINTER_TYPE
5149 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5150 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5151 && !null_pointer_constant_p (value))
c1771a20 5152 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5153 "conversion of object pointer to function pointer type");
3897f229 5154
3e4093b6 5155 ovalue = value;
3e4093b6 5156 value = convert (type, value);
400fbf9f 5157
3e4093b6 5158 /* Ignore any integer overflow caused by the cast. */
928c19bb 5159 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5160 {
8bcd6380 5161 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5162 {
8bcd6380
RS
5163 if (!TREE_OVERFLOW (value))
5164 {
5165 /* Avoid clobbering a shared constant. */
5166 value = copy_node (value);
5167 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5168 }
6414bad6 5169 }
8bcd6380 5170 else if (TREE_OVERFLOW (value))
d8e1f97b 5171 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5172 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5173 }
5174 }
400fbf9f 5175
53cd18ec
JM
5176 /* Don't let a cast be an lvalue. */
5177 if (value == expr)
db3927fb 5178 value = non_lvalue_loc (loc, value);
e9a25f70 5179
928c19bb
JM
5180 /* Don't allow the results of casting to floating-point or complex
5181 types be confused with actual constants, or casts involving
5182 integer and pointer types other than direct integer-to-integer
5183 and integer-to-pointer be confused with integer constant
5184 expressions and null pointer constants. */
5185 if (TREE_CODE (value) == REAL_CST
5186 || TREE_CODE (value) == COMPLEX_CST
5187 || (TREE_CODE (value) == INTEGER_CST
5188 && !((TREE_CODE (expr) == INTEGER_CST
5189 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5190 || TREE_CODE (expr) == REAL_CST
5191 || TREE_CODE (expr) == COMPLEX_CST)))
5192 value = build1 (NOP_EXPR, type, value);
5193
c2255bc4
AH
5194 if (CAN_HAVE_LOCATION_P (value))
5195 SET_EXPR_LOCATION (value, loc);
3e4093b6 5196 return value;
400fbf9f
JW
5197}
5198
c2255bc4
AH
5199/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5200 location of the open paren of the cast, or the position of the cast
5201 expr. */
3e4093b6 5202tree
c2255bc4 5203c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5204{
f8893e47 5205 tree type;
928c19bb
JM
5206 tree type_expr = NULL_TREE;
5207 bool type_expr_const = true;
5208 tree ret;
3e4093b6 5209 int saved_wsp = warn_strict_prototypes;
c5c76735 5210
3e4093b6
RS
5211 /* This avoids warnings about unprototyped casts on
5212 integers. E.g. "#define SIG_DFL (void(*)())0". */
5213 if (TREE_CODE (expr) == INTEGER_CST)
5214 warn_strict_prototypes = 0;
928c19bb 5215 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5216 warn_strict_prototypes = saved_wsp;
c5c76735 5217
c2255bc4 5218 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5219 if (type_expr)
5220 {
9f33203d
JM
5221 bool inner_expr_const = true;
5222 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5223 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5224 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5225 && inner_expr_const);
c2255bc4 5226 SET_EXPR_LOCATION (ret, loc);
928c19bb 5227 }
24b97832
ILT
5228
5229 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5230 SET_EXPR_LOCATION (ret, loc);
5231
9e5b2115
PB
5232 /* C++ does not permits types to be defined in a cast, but it
5233 allows references to incomplete types. */
5234 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5235 warning_at (loc, OPT_Wc___compat,
5236 "defining a type in a cast is invalid in C++");
5237
928c19bb 5238 return ret;
400fbf9f 5239}
3e4093b6
RS
5240\f
5241/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5242 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5243 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5244 MODIFYCODE is the code for a binary operator that we use
5245 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5246 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5247 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5248 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5249
c2255bc4
AH
5250 LOCATION is the location of the MODIFYCODE operator.
5251 RHS_LOC is the location of the RHS. */
2f6e4e97 5252
3e4093b6 5253tree
32e8bb8e 5254build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5255 enum tree_code modifycode,
c2255bc4 5256 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5257{
3e4093b6
RS
5258 tree result;
5259 tree newrhs;
241f845a 5260 tree rhseval = NULL_TREE;
8ce94e44 5261 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5262 tree lhstype = TREE_TYPE (lhs);
5263 tree olhstype = lhstype;
928c19bb 5264 bool npc;
267bac10 5265 bool is_atomic_op;
e9a25f70 5266
3e4093b6
RS
5267 /* Types that aren't fully specified cannot be used in assignments. */
5268 lhs = require_complete_type (lhs);
e9a25f70 5269
3e4093b6
RS
5270 /* Avoid duplicate error messages from operands that had errors. */
5271 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5272 return error_mark_node;
400fbf9f 5273
4c2ecab0
JM
5274 /* Ensure an error for assigning a non-lvalue array to an array in
5275 C90. */
5276 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5277 {
5278 error_at (location, "assignment to expression with array type");
5279 return error_mark_node;
5280 }
5281
46a88c12 5282 /* For ObjC properties, defer this check. */
7bd11157 5283 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5284 return error_mark_node;
5285
267bac10
JM
5286 is_atomic_op = really_atomic_lvalue (lhs);
5287
8ce94e44
JM
5288 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5289 {
5290 rhs_semantic_type = TREE_TYPE (rhs);
5291 rhs = TREE_OPERAND (rhs, 0);
5292 }
5293
3e4093b6 5294 newrhs = rhs;
400fbf9f 5295
928c19bb
JM
5296 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5297 {
5298 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5299 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5300 rhs_origtype);
928c19bb
JM
5301 if (inner == error_mark_node)
5302 return error_mark_node;
5303 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5304 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5305 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5306 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5307 protected_set_expr_location (result, location);
5308 return result;
5309 }
5310
3e4093b6
RS
5311 /* If a binary op has been requested, combine the old LHS value with the RHS
5312 producing the value we should actually store into the LHS. */
5313
5314 if (modifycode != NOP_EXPR)
400fbf9f 5315 {
928c19bb 5316 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5317 lhs = stabilize_reference (lhs);
bbbbb16a 5318
267bac10
JM
5319 /* Construct the RHS for any non-atomic compound assignemnt. */
5320 if (!is_atomic_op)
5321 {
241f845a
JJ
5322 /* If in LHS op= RHS the RHS has side-effects, ensure they
5323 are preevaluated before the rest of the assignment expression's
5324 side-effects, because RHS could contain e.g. function calls
5325 that modify LHS. */
5326 if (TREE_SIDE_EFFECTS (rhs))
5327 {
5328 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5329 rhseval = newrhs;
5330 }
267bac10 5331 newrhs = build_binary_op (location,
241f845a 5332 modifycode, lhs, newrhs, 1);
267bac10
JM
5333
5334 /* The original type of the right hand side is no longer
5335 meaningful. */
5336 rhs_origtype = NULL_TREE;
5337 }
400fbf9f 5338 }
400fbf9f 5339
668ea4b1
IS
5340 if (c_dialect_objc ())
5341 {
46a88c12
NP
5342 /* Check if we are modifying an Objective-C property reference;
5343 if so, we need to generate setter calls. */
5344 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5345 if (result)
241f845a 5346 goto return_result;
46a88c12
NP
5347
5348 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5349 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5350 return error_mark_node;
5351 }
5352
9bf24266 5353 /* Give an error for storing in something that is 'const'. */
bbbd6700 5354
f37acdf9 5355 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5356 || ((TREE_CODE (lhstype) == RECORD_TYPE
5357 || TREE_CODE (lhstype) == UNION_TYPE)
5358 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5359 {
c02065fc 5360 readonly_error (location, lhs, lv_assign);
953ff289
DN
5361 return error_mark_node;
5362 }
f37acdf9
JM
5363 else if (TREE_READONLY (lhs))
5364 readonly_warning (lhs, lv_assign);
bbbd6700 5365
3e4093b6
RS
5366 /* If storing into a structure or union member,
5367 it has probably been given type `int'.
5368 Compute the type that would go with
5369 the actual amount of storage the member occupies. */
bbbd6700 5370
3e4093b6
RS
5371 if (TREE_CODE (lhs) == COMPONENT_REF
5372 && (TREE_CODE (lhstype) == INTEGER_TYPE
5373 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5374 || TREE_CODE (lhstype) == REAL_TYPE
5375 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5376 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5377
3e4093b6
RS
5378 /* If storing in a field that is in actuality a short or narrower than one,
5379 we must store in the field in its actual type. */
5380
5381 if (lhstype != TREE_TYPE (lhs))
5382 {
5383 lhs = copy_node (lhs);
5384 TREE_TYPE (lhs) = lhstype;
400fbf9f 5385 }
400fbf9f 5386
32e8bb8e
ILT
5387 /* Issue -Wc++-compat warnings about an assignment to an enum type
5388 when LHS does not have its original type. This happens for,
5389 e.g., an enum bitfield in a struct. */
5390 if (warn_cxx_compat
5391 && lhs_origtype != NULL_TREE
5392 && lhs_origtype != lhstype
5393 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5394 {
5395 tree checktype = (rhs_origtype != NULL_TREE
5396 ? rhs_origtype
5397 : TREE_TYPE (rhs));
5398 if (checktype != error_mark_node
267bac10
JM
5399 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5400 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5401 warning_at (location, OPT_Wc___compat,
5402 "enum conversion in assignment is invalid in C++");
5403 }
5404
267bac10
JM
5405 /* If the lhs is atomic, remove that qualifier. */
5406 if (is_atomic_op)
5407 {
5408 lhstype = build_qualified_type (lhstype,
5409 (TYPE_QUALS (lhstype)
5410 & ~TYPE_QUAL_ATOMIC));
5411 olhstype = build_qualified_type (olhstype,
5412 (TYPE_QUALS (lhstype)
5413 & ~TYPE_QUAL_ATOMIC));
5414 }
5415
8ce94e44
JM
5416 /* Convert new value to destination type. Fold it first, then
5417 restore any excess precision information, for the sake of
5418 conversion warnings. */
400fbf9f 5419
267bac10
JM
5420 if (!(is_atomic_op && modifycode != NOP_EXPR))
5421 {
5422 npc = null_pointer_constant_p (newrhs);
5423 newrhs = c_fully_fold (newrhs, false, NULL);
5424 if (rhs_semantic_type)
5425 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5426 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5427 rhs_origtype, ic_assign, npc,
5428 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5429 if (TREE_CODE (newrhs) == ERROR_MARK)
5430 return error_mark_node;
5431 }
400fbf9f 5432
6e955430
ZL
5433 /* Emit ObjC write barrier, if necessary. */
5434 if (c_dialect_objc () && flag_objc_gc)
5435 {
5436 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5437 if (result)
c9f9eb5d
AH
5438 {
5439 protected_set_expr_location (result, location);
241f845a 5440 goto return_result;
c9f9eb5d 5441 }
6e955430
ZL
5442 }
5443
ea4b7848 5444 /* Scan operands. */
400fbf9f 5445
267bac10
JM
5446 if (is_atomic_op)
5447 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5448 else
5449 {
5450 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5451 TREE_SIDE_EFFECTS (result) = 1;
5452 protected_set_expr_location (result, location);
5453 }
400fbf9f 5454
3e4093b6
RS
5455 /* If we got the LHS in a different type for storing in,
5456 convert the result back to the nominal type of LHS
5457 so that the value we return always has the same type
5458 as the LHS argument. */
e855c5ce 5459
3e4093b6 5460 if (olhstype == TREE_TYPE (result))
241f845a 5461 goto return_result;
c9f9eb5d 5462
68fca595
MP
5463 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5464 rhs_origtype, ic_assign, false, NULL_TREE,
5465 NULL_TREE, 0);
c9f9eb5d 5466 protected_set_expr_location (result, location);
241f845a
JJ
5467
5468return_result:
5469 if (rhseval)
5470 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5471 return result;
3e4093b6
RS
5472}
5473\f
478a1c5b
ILT
5474/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5475 This is used to implement -fplan9-extensions. */
5476
5477static bool
5478find_anonymous_field_with_type (tree struct_type, tree type)
5479{
5480 tree field;
5481 bool found;
5482
5483 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5484 || TREE_CODE (struct_type) == UNION_TYPE);
5485 found = false;
5486 for (field = TYPE_FIELDS (struct_type);
5487 field != NULL_TREE;
5488 field = TREE_CHAIN (field))
5489 {
267bac10
JM
5490 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5491 ? c_build_qualified_type (TREE_TYPE (field),
5492 TYPE_QUAL_ATOMIC)
5493 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5494 if (DECL_NAME (field) == NULL
267bac10 5495 && comptypes (type, fieldtype))
478a1c5b
ILT
5496 {
5497 if (found)
5498 return false;
5499 found = true;
5500 }
5501 else if (DECL_NAME (field) == NULL
5502 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5503 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5504 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5505 {
5506 if (found)
5507 return false;
5508 found = true;
5509 }
5510 }
5511 return found;
5512}
5513
5514/* RHS is an expression whose type is pointer to struct. If there is
5515 an anonymous field in RHS with type TYPE, then return a pointer to
5516 that field in RHS. This is used with -fplan9-extensions. This
5517 returns NULL if no conversion could be found. */
5518
5519static tree
5520convert_to_anonymous_field (location_t location, tree type, tree rhs)
5521{
5522 tree rhs_struct_type, lhs_main_type;
5523 tree field, found_field;
5524 bool found_sub_field;
5525 tree ret;
5526
5527 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5528 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5529 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5530 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5531
5532 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5533 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5534 ? c_build_qualified_type (TREE_TYPE (type),
5535 TYPE_QUAL_ATOMIC)
5536 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5537
5538 found_field = NULL_TREE;
5539 found_sub_field = false;
5540 for (field = TYPE_FIELDS (rhs_struct_type);
5541 field != NULL_TREE;
5542 field = TREE_CHAIN (field))
5543 {
5544 if (DECL_NAME (field) != NULL_TREE
5545 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5546 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5547 continue;
267bac10
JM
5548 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5549 ? c_build_qualified_type (TREE_TYPE (field),
5550 TYPE_QUAL_ATOMIC)
5551 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5552 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5553 {
5554 if (found_field != NULL_TREE)
5555 return NULL_TREE;
5556 found_field = field;
5557 }
5558 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5559 lhs_main_type))
5560 {
5561 if (found_field != NULL_TREE)
5562 return NULL_TREE;
5563 found_field = field;
5564 found_sub_field = true;
5565 }
5566 }
5567
5568 if (found_field == NULL_TREE)
5569 return NULL_TREE;
5570
5571 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5572 build_fold_indirect_ref (rhs), found_field,
5573 NULL_TREE);
5574 ret = build_fold_addr_expr_loc (location, ret);
5575
5576 if (found_sub_field)
5577 {
5578 ret = convert_to_anonymous_field (location, type, ret);
5579 gcc_assert (ret != NULL_TREE);
5580 }
5581
5582 return ret;
5583}
5584
63bc4e87
MP
5585/* Issue an error message for a bad initializer component.
5586 GMSGID identifies the message.
5587 The component name is taken from the spelling stack. */
5588
5589static void
ea58ef42 5590error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5591{
5592 char *ofwhat;
5593
5594 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5595 error_at (loc, gmsgid);
63bc4e87
MP
5596 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5597 if (*ofwhat)
d7ff7ae5 5598 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5599}
5600
5601/* Issue a pedantic warning for a bad initializer component. OPT is
5602 the option OPT_* (from options.h) controlling this warning or 0 if
5603 it is unconditionally given. GMSGID identifies the message. The
5604 component name is taken from the spelling stack. */
5605
5606static void
5607pedwarn_init (location_t location, int opt, const char *gmsgid)
5608{
5609 char *ofwhat;
d7ff7ae5 5610 bool warned;
63bc4e87
MP
5611
5612 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5613 warned = pedwarn (location, opt, gmsgid);
63bc4e87 5614 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5615 if (*ofwhat && warned)
5616 inform (location, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5617}
5618
5619/* Issue a warning for a bad initializer component.
5620
5621 OPT is the OPT_W* value corresponding to the warning option that
5622 controls this warning. GMSGID identifies the message. The
5623 component name is taken from the spelling stack. */
5624
5625static void
5626warning_init (location_t loc, int opt, const char *gmsgid)
5627{
5628 char *ofwhat;
d7ff7ae5 5629 bool warned;
63bc4e87
MP
5630
5631 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5632 warned = warning_at (loc, opt, gmsgid);
63bc4e87 5633 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5634 if (*ofwhat && warned)
5635 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5636}
5637\f
5638/* If TYPE is an array type and EXPR is a parenthesized string
5639 constant, warn if pedantic that EXPR is being used to initialize an
5640 object of type TYPE. */
5641
5642void
d033409e 5643maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5644{
5645 if (pedantic
5646 && TREE_CODE (type) == ARRAY_TYPE
5647 && TREE_CODE (expr.value) == STRING_CST
5648 && expr.original_code != STRING_CST)
d033409e 5649 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5650 "array initialized from parenthesized string constant");
5651}
5652
bbbbb16a
ILT
5653/* Convert value RHS to type TYPE as preparation for an assignment to
5654 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5655 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5656 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5657 constant before any folding.
3e4093b6
RS
5658 The real work of conversion is done by `convert'.
5659 The purpose of this function is to generate error messages
5660 for assignments that are not allowed in C.
2ac2f164
JM
5661 ERRTYPE says whether it is argument passing, assignment,
5662 initialization or return.
2f6e4e97 5663
81e5eca8
MP
5664 LOCATION is the location of the assignment, EXPR_LOC is the location of
5665 the RHS or, for a function, location of an argument.
2ac2f164 5666 FUNCTION is a tree for the function being called.
3e4093b6 5667 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5668
3e4093b6 5669static tree
81e5eca8 5670convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5671 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5672 bool null_pointer_constant, tree fundecl,
5673 tree function, int parmnum)
3e4093b6
RS
5674{
5675 enum tree_code codel = TREE_CODE (type);
8ce94e44 5676 tree orig_rhs = rhs;
3e4093b6
RS
5677 tree rhstype;
5678 enum tree_code coder;
2ac2f164 5679 tree rname = NULL_TREE;
58393038 5680 bool objc_ok = false;
2ac2f164 5681
6b4ef5c1 5682 if (errtype == ic_argpass)
2ac2f164
JM
5683 {
5684 tree selector;
5685 /* Change pointer to function to the function itself for
5686 diagnostics. */
5687 if (TREE_CODE (function) == ADDR_EXPR
5688 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5689 function = TREE_OPERAND (function, 0);
5690
5691 /* Handle an ObjC selector specially for diagnostics. */
5692 selector = objc_message_selector ();
5693 rname = function;
5694 if (selector && parmnum > 2)
5695 {
5696 rname = selector;
5697 parmnum -= 2;
5698 }
5699 }
5700
5701 /* This macro is used to emit diagnostics to ensure that all format
5702 strings are complete sentences, visible to gettext and checked at
5703 compile time. */
768952be 5704#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5705 do { \
5706 switch (errtype) \
5707 { \
5708 case ic_argpass: \
5c1bc275 5709 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5710 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5711 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5712 "expected %qT but argument is of type %qT", \
5713 type, rhstype); \
5714 break; \
1e053dfe
MLI
5715 case ic_assign: \
5716 pedwarn (LOCATION, OPT, AS); \
5717 break; \
5718 case ic_init: \
6a8f4e12 5719 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5720 break; \
5721 case ic_return: \
d033409e 5722 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5723 break; \
5724 default: \
5725 gcc_unreachable (); \
5726 } \
2ac2f164 5727 } while (0)
cb3ca04e 5728
49706e39
MLI
5729 /* This macro is used to emit diagnostics to ensure that all format
5730 strings are complete sentences, visible to gettext and checked at
768952be 5731 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 5732 extra parameter to enumerate qualifiers. */
768952be 5733#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
5734 do { \
5735 switch (errtype) \
5736 { \
5737 case ic_argpass: \
5c1bc275 5738 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 5739 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5740 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
5741 "expected %qT but argument is of type %qT", \
5742 type, rhstype); \
5743 break; \
5744 case ic_assign: \
5c1bc275 5745 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
5746 break; \
5747 case ic_init: \
5c1bc275 5748 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
5749 break; \
5750 case ic_return: \
5c1bc275 5751 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
5752 break; \
5753 default: \
5754 gcc_unreachable (); \
5755 } \
5756 } while (0)
5757
768952be
MU
5758 /* This macro is used to emit diagnostics to ensure that all format
5759 strings are complete sentences, visible to gettext and checked at
5760 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5761 warning_at instead of pedwarn. */
5762#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5763 do { \
5764 switch (errtype) \
5765 { \
5766 case ic_argpass: \
5767 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5768 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5769 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5770 "expected %qT but argument is of type %qT", \
5771 type, rhstype); \
5772 break; \
5773 case ic_assign: \
5774 warning_at (LOCATION, OPT, AS, QUALS); \
5775 break; \
5776 case ic_init: \
5777 warning_at (LOCATION, OPT, IN, QUALS); \
5778 break; \
5779 case ic_return: \
5780 warning_at (LOCATION, OPT, RE, QUALS); \
5781 break; \
5782 default: \
5783 gcc_unreachable (); \
5784 } \
5785 } while (0)
5786
8ce94e44
JM
5787 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5788 rhs = TREE_OPERAND (rhs, 0);
5789
3e4093b6
RS
5790 rhstype = TREE_TYPE (rhs);
5791 coder = TREE_CODE (rhstype);
5792
5793 if (coder == ERROR_MARK)
5794 return error_mark_node;
5795
58393038
ZL
5796 if (c_dialect_objc ())
5797 {
5798 int parmno;
5799
5800 switch (errtype)
5801 {
5802 case ic_return:
5803 parmno = 0;
5804 break;
5805
5806 case ic_assign:
5807 parmno = -1;
5808 break;
5809
5810 case ic_init:
5811 parmno = -2;
5812 break;
5813
5814 default:
5815 parmno = parmnum;
5816 break;
5817 }
5818
5819 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5820 }
5821
bbbbb16a
ILT
5822 if (warn_cxx_compat)
5823 {
5824 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5825 if (checktype != error_mark_node
5826 && TREE_CODE (type) == ENUMERAL_TYPE
5827 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5828 {
768952be
MU
5829 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5830 G_("enum conversion when passing argument "
5831 "%d of %qE is invalid in C++"),
5832 G_("enum conversion in assignment is "
5833 "invalid in C++"),
5834 G_("enum conversion in initialization is "
5835 "invalid in C++"),
5836 G_("enum conversion in return is "
5837 "invalid in C++"));
bbbbb16a
ILT
5838 }
5839 }
5840
3e4093b6 5841 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5842 return rhs;
3e4093b6
RS
5843
5844 if (coder == VOID_TYPE)
400fbf9f 5845 {
6dcc04b0
JM
5846 /* Except for passing an argument to an unprototyped function,
5847 this is a constraint violation. When passing an argument to
5848 an unprototyped function, it is compile-time undefined;
5849 making it a constraint in that case was rejected in
5850 DR#252. */
c2255bc4 5851 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5852 return error_mark_node;
400fbf9f 5853 }
808d6eaa
JM
5854 rhs = require_complete_type (rhs);
5855 if (rhs == error_mark_node)
5856 return error_mark_node;
cd192ccc
MS
5857 /* A non-reference type can convert to a reference. This handles
5858 va_start, va_copy and possibly port built-ins. */
5859 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5860 {
3e4093b6 5861 if (!lvalue_p (rhs))
400fbf9f 5862 {
c2255bc4 5863 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5864 return error_mark_node;
400fbf9f 5865 }
3e4093b6
RS
5866 if (!c_mark_addressable (rhs))
5867 return error_mark_node;
5868 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5869 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5870
81e5eca8 5871 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
5872 build_pointer_type (TREE_TYPE (type)),
5873 rhs, origtype, errtype,
5874 null_pointer_constant, fundecl, function,
5875 parmnum);
cd192ccc
MS
5876 if (rhs == error_mark_node)
5877 return error_mark_node;
3e4093b6
RS
5878
5879 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5880 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5881 return rhs;
400fbf9f 5882 }
3e4093b6 5883 /* Some types can interconvert without explicit casts. */
3274deff 5884 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5885 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5886 return convert (type, rhs);
5887 /* Arithmetic types all interconvert, and enum is treated like int. */
5888 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5889 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5890 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5891 || codel == BOOLEAN_TYPE)
5892 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5893 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5894 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5895 || coder == BOOLEAN_TYPE))
928c19bb
JM
5896 {
5897 tree ret;
5898 bool save = in_late_binary_op;
e5341100
JJ
5899 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5900 || (coder == REAL_TYPE
5901 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5902 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 5903 in_late_binary_op = true;
81e5eca8
MP
5904 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5905 ? expr_loc : location, type, orig_rhs);
e5341100 5906 in_late_binary_op = save;
928c19bb
JM
5907 return ret;
5908 }
400fbf9f 5909
79077aea
JJ
5910 /* Aggregates in different TUs might need conversion. */
5911 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5912 && codel == coder
5913 && comptypes (type, rhstype))
81e5eca8
MP
5914 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5915 ? expr_loc : location, type, rhs);
79077aea 5916
ebf0bf7f 5917 /* Conversion to a transparent union or record from its member types.
3e4093b6 5918 This applies only to function arguments. */
ebf0bf7f
JJ
5919 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5920 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5921 && errtype == ic_argpass)
400fbf9f 5922 {
0257e383 5923 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5924
910ad8de 5925 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5926 {
0257e383 5927 tree memb_type = TREE_TYPE (memb);
400fbf9f 5928
3e4093b6 5929 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5930 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5931 break;
e58cd767 5932
3e4093b6
RS
5933 if (TREE_CODE (memb_type) != POINTER_TYPE)
5934 continue;
2f6e4e97 5935
3e4093b6
RS
5936 if (coder == POINTER_TYPE)
5937 {
5938 tree ttl = TREE_TYPE (memb_type);
5939 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5940
3e4093b6
RS
5941 /* Any non-function converts to a [const][volatile] void *
5942 and vice versa; otherwise, targets must be the same.
5943 Meanwhile, the lhs target must have all the qualifiers of
5944 the rhs. */
267bac10
JM
5945 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5946 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5947 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5948 {
267bac10
JM
5949 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5950 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5951 /* If this type won't generate any warnings, use it. */
267bac10 5952 if (lquals == rquals
3e4093b6
RS
5953 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5954 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5955 ? ((lquals | rquals) == rquals)
5956 : ((lquals | rquals) == lquals)))
3e4093b6 5957 break;
400fbf9f 5958
3e4093b6 5959 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5960 if (!marginal_memb)
5961 marginal_memb = memb;
3e4093b6
RS
5962 }
5963 }
82bde854 5964
3e4093b6 5965 /* Can convert integer zero to any pointer type. */
928c19bb 5966 if (null_pointer_constant)
3e4093b6
RS
5967 {
5968 rhs = null_pointer_node;
5969 break;
5970 }
5971 }
400fbf9f 5972
0257e383 5973 if (memb || marginal_memb)
3e4093b6 5974 {
0257e383 5975 if (!memb)
3e4093b6
RS
5976 {
5977 /* We have only a marginally acceptable member type;
5978 it needs a warning. */
0257e383 5979 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5980 tree ttr = TREE_TYPE (rhstype);
714a0864 5981
3e4093b6
RS
5982 /* Const and volatile mean something different for function
5983 types, so the usual warnings are not appropriate. */
5984 if (TREE_CODE (ttr) == FUNCTION_TYPE
5985 && TREE_CODE (ttl) == FUNCTION_TYPE)
5986 {
5987 /* Because const and volatile on functions are
5988 restrictions that say the function will not do
5989 certain things, it is okay to use a const or volatile
5990 function where an ordinary one is wanted, but not
5991 vice-versa. */
36c5e70a
BE
5992 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5993 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
5994 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
5995 OPT_Wdiscarded_qualifiers,
5996 G_("passing argument %d of %qE "
5997 "makes %q#v qualified function "
5998 "pointer from unqualified"),
5999 G_("assignment makes %q#v qualified "
6000 "function pointer from "
6001 "unqualified"),
6002 G_("initialization makes %q#v qualified "
6003 "function pointer from "
6004 "unqualified"),
6005 G_("return makes %q#v qualified function "
6006 "pointer from unqualified"),
6007 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6008 }
36c5e70a
BE
6009 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6010 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6011 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6012 OPT_Wdiscarded_qualifiers,
6013 G_("passing argument %d of %qE discards "
6014 "%qv qualifier from pointer target type"),
6015 G_("assignment discards %qv qualifier "
6016 "from pointer target type"),
6017 G_("initialization discards %qv qualifier "
6018 "from pointer target type"),
6019 G_("return discards %qv qualifier from "
6020 "pointer target type"),
6021 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6022
6023 memb = marginal_memb;
3e4093b6 6024 }
400fbf9f 6025
fcf73884 6026 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6027 pedwarn (location, OPT_Wpedantic,
fcf73884 6028 "ISO C prohibits argument conversion to union type");
0e7c47fa 6029
db3927fb 6030 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6031 return build_constructor_single (type, memb, rhs);
3e4093b6 6032 }
0e7c47fa
RK
6033 }
6034
3e4093b6
RS
6035 /* Conversions among pointers */
6036 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6037 && (coder == codel))
400fbf9f 6038 {
3e4093b6
RS
6039 tree ttl = TREE_TYPE (type);
6040 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6041 tree mvl = ttl;
6042 tree mvr = ttr;
3e4093b6 6043 bool is_opaque_pointer;
264fa2db 6044 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6045 addr_space_t asl;
6046 addr_space_t asr;
400fbf9f 6047
46df2823 6048 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6049 mvl = (TYPE_ATOMIC (mvl)
6050 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6051 TYPE_QUAL_ATOMIC)
6052 : TYPE_MAIN_VARIANT (mvl));
46df2823 6053 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6054 mvr = (TYPE_ATOMIC (mvr)
6055 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6056 TYPE_QUAL_ATOMIC)
6057 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6058 /* Opaque pointers are treated like void pointers. */
f83c7f63 6059 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6060
478a1c5b
ILT
6061 /* The Plan 9 compiler permits a pointer to a struct to be
6062 automatically converted into a pointer to an anonymous field
6063 within the struct. */
6064 if (flag_plan9_extensions
6065 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6066 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6067 && mvl != mvr)
6068 {
6069 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6070 if (new_rhs != NULL_TREE)
6071 {
6072 rhs = new_rhs;
6073 rhstype = TREE_TYPE (rhs);
6074 coder = TREE_CODE (rhstype);
6075 ttr = TREE_TYPE (rhstype);
6076 mvr = TYPE_MAIN_VARIANT (ttr);
6077 }
6078 }
6079
b7e20b53 6080 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6081 for the purpose of reducing the number of false positives, we
6082 tolerate the special case of
b7e20b53 6083
c22cacf3 6084 int *p = NULL;
b7e20b53 6085
c22cacf3 6086 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6087 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6088 warning_at (errtype == ic_argpass ? expr_loc : location,
6089 OPT_Wc___compat,
6090 "request for implicit conversion "
c2255bc4 6091 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6092
36c5e70a
BE
6093 /* See if the pointers point to incompatible address spaces. */
6094 asl = TYPE_ADDR_SPACE (ttl);
6095 asr = TYPE_ADDR_SPACE (ttr);
6096 if (!null_pointer_constant_p (rhs)
6097 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6098 {
6099 switch (errtype)
6100 {
6101 case ic_argpass:
8ffcdea8 6102 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6103 "non-enclosed address space", parmnum, rname);
6104 break;
6105 case ic_assign:
6106 error_at (location, "assignment from pointer to "
6107 "non-enclosed address space");
6108 break;
6109 case ic_init:
6110 error_at (location, "initialization from pointer to "
6111 "non-enclosed address space");
6112 break;
6113 case ic_return:
6114 error_at (location, "return from pointer to "
6115 "non-enclosed address space");
6116 break;
6117 default:
6118 gcc_unreachable ();
6119 }
6120 return error_mark_node;
6121 }
6122
7876a414
KG
6123 /* Check if the right-hand side has a format attribute but the
6124 left-hand side doesn't. */
90137d8f 6125 if (warn_suggest_attribute_format
104f8784 6126 && check_missing_format_attribute (type, rhstype))
c22cacf3 6127 {
104f8784
KG
6128 switch (errtype)
6129 {
6130 case ic_argpass:
8ffcdea8 6131 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6132 "argument %d of %qE might be "
6133 "a candidate for a format attribute",
6134 parmnum, rname);
104f8784
KG
6135 break;
6136 case ic_assign:
90137d8f 6137 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6138 "assignment left-hand side might be "
6139 "a candidate for a format attribute");
104f8784
KG
6140 break;
6141 case ic_init:
90137d8f 6142 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6143 "initialization left-hand side might be "
6144 "a candidate for a format attribute");
104f8784
KG
6145 break;
6146 case ic_return:
90137d8f 6147 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6148 "return type might be "
6149 "a candidate for a format attribute");
104f8784
KG
6150 break;
6151 default:
6152 gcc_unreachable ();
6153 }
7876a414 6154 }
c22cacf3 6155
3e4093b6
RS
6156 /* Any non-function converts to a [const][volatile] void *
6157 and vice versa; otherwise, targets must be the same.
6158 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6159 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6160 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6161 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6162 || is_opaque_pointer
f8a93a2e
JJ
6163 || ((c_common_unsigned_type (mvl)
6164 == c_common_unsigned_type (mvr))
267bac10
JM
6165 && (c_common_signed_type (mvl)
6166 == c_common_signed_type (mvr))
6167 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6168 {
768952be
MU
6169 /* Warn about loss of qualifers from pointers to arrays with
6170 qualifiers on the element type. */
6171 if (TREE_CODE (ttr) == ARRAY_TYPE)
6172 {
6173 ttr = strip_array_types (ttr);
6174 ttl = strip_array_types (ttl);
6175
6176 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6177 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6178 WARNING_FOR_QUALIFIERS (location, expr_loc,
6179 OPT_Wdiscarded_array_qualifiers,
6180 G_("passing argument %d of %qE discards "
6181 "%qv qualifier from pointer target type"),
6182 G_("assignment discards %qv qualifier "
6183 "from pointer target type"),
6184 G_("initialization discards %qv qualifier "
6185 "from pointer target type"),
6186 G_("return discards %qv qualifier from "
6187 "pointer target type"),
6188 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6189 }
6190 else if (pedantic
3e4093b6
RS
6191 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6192 ||
6193 (VOID_TYPE_P (ttr)
928c19bb 6194 && !null_pointer_constant
3e4093b6 6195 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6196 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6197 G_("ISO C forbids passing argument %d of "
6198 "%qE between function pointer "
6199 "and %<void *%>"),
6200 G_("ISO C forbids assignment between "
6201 "function pointer and %<void *%>"),
6202 G_("ISO C forbids initialization between "
6203 "function pointer and %<void *%>"),
6204 G_("ISO C forbids return between function "
6205 "pointer and %<void *%>"));
3e4093b6
RS
6206 /* Const and volatile mean something different for function types,
6207 so the usual warnings are not appropriate. */
6208 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6209 && TREE_CODE (ttl) != FUNCTION_TYPE)
6210 {
768952be
MU
6211 /* Don't warn about loss of qualifier for conversions from
6212 qualified void* to pointers to arrays with corresponding
6213 qualifier on the element type. */
6214 if (!pedantic)
6215 ttl = strip_array_types (ttl);
6216
267bac10
JM
6217 /* Assignments between atomic and non-atomic objects are OK. */
6218 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6219 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6220 {
768952be
MU
6221 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6222 OPT_Wdiscarded_qualifiers,
6223 G_("passing argument %d of %qE discards "
6224 "%qv qualifier from pointer target type"),
6225 G_("assignment discards %qv qualifier "
6226 "from pointer target type"),
6227 G_("initialization discards %qv qualifier "
6228 "from pointer target type"),
6229 G_("return discards %qv qualifier from "
6230 "pointer target type"),
6231 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6232 }
3e4093b6
RS
6233 /* If this is not a case of ignoring a mismatch in signedness,
6234 no warning. */
6235 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6236 || target_cmp)
3e4093b6
RS
6237 ;
6238 /* If there is a mismatch, do warn. */
f2fd3821 6239 else if (warn_pointer_sign)
768952be
MU
6240 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6241 G_("pointer targets in passing argument "
6242 "%d of %qE differ in signedness"),
6243 G_("pointer targets in assignment "
6244 "differ in signedness"),
6245 G_("pointer targets in initialization "
6246 "differ in signedness"),
6247 G_("pointer targets in return differ "
6248 "in signedness"));
3e4093b6
RS
6249 }
6250 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6251 && TREE_CODE (ttr) == FUNCTION_TYPE)
6252 {
6253 /* Because const and volatile on functions are restrictions
6254 that say the function will not do certain things,
6255 it is okay to use a const or volatile function
6256 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6257 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6258 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6259 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6260 OPT_Wdiscarded_qualifiers,
6261 G_("passing argument %d of %qE makes "
6262 "%q#v qualified function pointer "
6263 "from unqualified"),
6264 G_("assignment makes %q#v qualified function "
6265 "pointer from unqualified"),
6266 G_("initialization makes %q#v qualified "
6267 "function pointer from unqualified"),
6268 G_("return makes %q#v qualified function "
6269 "pointer from unqualified"),
6270 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6271 }
6272 }
6273 else
58393038
ZL
6274 /* Avoid warning about the volatile ObjC EH puts on decls. */
6275 if (!objc_ok)
768952be
MU
6276 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6277 OPT_Wincompatible_pointer_types,
6278 G_("passing argument %d of %qE from "
6279 "incompatible pointer type"),
6280 G_("assignment from incompatible pointer type"),
6281 G_("initialization from incompatible "
6282 "pointer type"),
6283 G_("return from incompatible pointer type"));
58393038 6284
3e4093b6
RS
6285 return convert (type, rhs);
6286 }
b494fd98
EB
6287 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6288 {
6dcc04b0
JM
6289 /* ??? This should not be an error when inlining calls to
6290 unprototyped functions. */
c2255bc4 6291 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6292 return error_mark_node;
6293 }
3e4093b6 6294 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6295 {
3e4093b6
RS
6296 /* An explicit constant 0 can convert to a pointer,
6297 or one that results from arithmetic, even including
6298 a cast to integer type. */
928c19bb 6299 if (!null_pointer_constant)
768952be
MU
6300 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6301 OPT_Wint_conversion,
6302 G_("passing argument %d of %qE makes "
6303 "pointer from integer without a cast"),
6304 G_("assignment makes pointer from integer "
6305 "without a cast"),
6306 G_("initialization makes pointer from "
6307 "integer without a cast"),
6308 G_("return makes pointer from integer "
6309 "without a cast"));
b3006337
EB
6310
6311 return convert (type, rhs);
400fbf9f 6312 }
3e4093b6 6313 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6314 {
768952be
MU
6315 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6316 OPT_Wint_conversion,
6317 G_("passing argument %d of %qE makes integer "
6318 "from pointer without a cast"),
6319 G_("assignment makes integer from pointer "
6320 "without a cast"),
6321 G_("initialization makes integer from pointer "
6322 "without a cast"),
6323 G_("return makes integer from pointer "
6324 "without a cast"));
3e4093b6
RS
6325 return convert (type, rhs);
6326 }
6327 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6328 {
6329 tree ret;
6330 bool save = in_late_binary_op;
6331 in_late_binary_op = true;
6332 ret = convert (type, rhs);
6333 in_late_binary_op = save;
6334 return ret;
6335 }
400fbf9f 6336
2ac2f164 6337 switch (errtype)
3e4093b6 6338 {
2ac2f164 6339 case ic_argpass:
8ffcdea8
MP
6340 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6341 rname);
a7da8b42 6342 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6343 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6344 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6345 break;
6346 case ic_assign:
c2255bc4
AH
6347 error_at (location, "incompatible types when assigning to type %qT from "
6348 "type %qT", type, rhstype);
2ac2f164
JM
6349 break;
6350 case ic_init:
c2255bc4 6351 error_at (location,
8ffcdea8 6352 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6353 type, rhstype);
2ac2f164
JM
6354 break;
6355 case ic_return:
c2255bc4 6356 error_at (location,
8ffcdea8 6357 "incompatible types when returning type %qT but %qT was "
c2255bc4 6358 "expected", rhstype, type);
2ac2f164
JM
6359 break;
6360 default:
6361 gcc_unreachable ();
400fbf9f 6362 }
53b01f59 6363
3e4093b6
RS
6364 return error_mark_node;
6365}
3e4093b6
RS
6366\f
6367/* If VALUE is a compound expr all of whose expressions are constant, then
6368 return its value. Otherwise, return error_mark_node.
15b732b2 6369
3e4093b6
RS
6370 This is for handling COMPOUND_EXPRs as initializer elements
6371 which is allowed with a warning when -pedantic is specified. */
15b732b2 6372
3e4093b6
RS
6373static tree
6374valid_compound_expr_initializer (tree value, tree endtype)
6375{
6376 if (TREE_CODE (value) == COMPOUND_EXPR)
6377 {
6378 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6379 == error_mark_node)
6380 return error_mark_node;
6381 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6382 endtype);
6383 }
116df786 6384 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6385 return error_mark_node;
6386 else
6387 return value;
15b732b2 6388}
400fbf9f 6389\f
3e4093b6
RS
6390/* Perform appropriate conversions on the initial value of a variable,
6391 store it in the declaration DECL,
6392 and print any error messages that are appropriate.
bbbbb16a 6393 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6394 If the init is invalid, store an ERROR_MARK.
6395
6396 INIT_LOC is the location of the initial value. */
400fbf9f 6397
3e4093b6 6398void
c2255bc4 6399store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6400{
3e4093b6 6401 tree value, type;
928c19bb 6402 bool npc = false;
400fbf9f 6403
3e4093b6 6404 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6405
3e4093b6
RS
6406 type = TREE_TYPE (decl);
6407 if (TREE_CODE (type) == ERROR_MARK)
6408 return;
400fbf9f 6409
3e4093b6 6410 /* Digest the specified initializer into an expression. */
400fbf9f 6411
928c19bb
JM
6412 if (init)
6413 npc = null_pointer_constant_p (init);
c2255bc4
AH
6414 value = digest_init (init_loc, type, init, origtype, npc,
6415 true, TREE_STATIC (decl));
400fbf9f 6416
3e4093b6 6417 /* Store the expression if valid; else report error. */
400fbf9f 6418
8400e75e 6419 if (!in_system_header_at (input_location)
3f75a254 6420 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6421 warning (OPT_Wtraditional, "traditional C rejects automatic "
6422 "aggregate initialization");
2f6e4e97 6423
3e4093b6 6424 DECL_INITIAL (decl) = value;
400fbf9f 6425
3e4093b6
RS
6426 /* ANSI wants warnings about out-of-range constant initializers. */
6427 STRIP_TYPE_NOPS (value);
b8698a0f 6428 if (TREE_STATIC (decl))
c2658540 6429 constant_expression_warning (value);
400fbf9f 6430
3e4093b6
RS
6431 /* Check if we need to set array size from compound literal size. */
6432 if (TREE_CODE (type) == ARRAY_TYPE
6433 && TYPE_DOMAIN (type) == 0
6434 && value != error_mark_node)
400fbf9f 6435 {
3e4093b6
RS
6436 tree inside_init = init;
6437
ed248cf7 6438 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6439 inside_init = fold (inside_init);
6440
6441 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6442 {
8d9f82d5 6443 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6444
8d9f82d5 6445 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6446 {
6447 /* For int foo[] = (int [3]){1}; we need to set array size
6448 now since later on array initializer will be just the
6449 brace enclosed list of the compound literal. */
e30ecc5d 6450 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6451 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6452 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6453 layout_type (type);
8d9f82d5 6454 layout_decl (cldecl, 0);
e30ecc5d
JJ
6455 TREE_TYPE (decl)
6456 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6457 }
6458 }
400fbf9f 6459 }
3e4093b6
RS
6460}
6461\f
6462/* Methods for storing and printing names for error messages. */
400fbf9f 6463
3e4093b6
RS
6464/* Implement a spelling stack that allows components of a name to be pushed
6465 and popped. Each element on the stack is this structure. */
400fbf9f 6466
3e4093b6
RS
6467struct spelling
6468{
6469 int kind;
6470 union
400fbf9f 6471 {
a0f0ab9f 6472 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6473 const char *s;
6474 } u;
6475};
2f6e4e97 6476
3e4093b6
RS
6477#define SPELLING_STRING 1
6478#define SPELLING_MEMBER 2
6479#define SPELLING_BOUNDS 3
400fbf9f 6480
3e4093b6
RS
6481static struct spelling *spelling; /* Next stack element (unused). */
6482static struct spelling *spelling_base; /* Spelling stack base. */
6483static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6484
3e4093b6
RS
6485/* Macros to save and restore the spelling stack around push_... functions.
6486 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6487
3e4093b6
RS
6488#define SPELLING_DEPTH() (spelling - spelling_base)
6489#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6490
3e4093b6
RS
6491/* Push an element on the spelling stack with type KIND and assign VALUE
6492 to MEMBER. */
400fbf9f 6493
3e4093b6
RS
6494#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6495{ \
6496 int depth = SPELLING_DEPTH (); \
6497 \
6498 if (depth >= spelling_size) \
6499 { \
6500 spelling_size += 10; \
cca8ead2
BI
6501 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6502 spelling_size); \
3e4093b6
RS
6503 RESTORE_SPELLING_DEPTH (depth); \
6504 } \
6505 \
6506 spelling->kind = (KIND); \
6507 spelling->MEMBER = (VALUE); \
6508 spelling++; \
6509}
400fbf9f 6510
3e4093b6 6511/* Push STRING on the stack. Printed literally. */
400fbf9f 6512
3e4093b6
RS
6513static void
6514push_string (const char *string)
6515{
6516 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6517}
400fbf9f 6518
3e4093b6 6519/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6520
3e4093b6
RS
6521static void
6522push_member_name (tree decl)
6523{
6524 const char *const string
88388a52
JM
6525 = (DECL_NAME (decl)
6526 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6527 : _("<anonymous>"));
3e4093b6
RS
6528 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6529}
400fbf9f 6530
3e4093b6 6531/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6532
3e4093b6 6533static void
a0f0ab9f 6534push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6535{
6536 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6537}
bb58bec5 6538
3e4093b6 6539/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6540
3e4093b6
RS
6541static int
6542spelling_length (void)
6543{
6544 int size = 0;
6545 struct spelling *p;
400fbf9f 6546
3e4093b6
RS
6547 for (p = spelling_base; p < spelling; p++)
6548 {
6549 if (p->kind == SPELLING_BOUNDS)
6550 size += 25;
6551 else
6552 size += strlen (p->u.s) + 1;
6553 }
6554
6555 return size;
400fbf9f 6556}
400fbf9f 6557
3e4093b6 6558/* Print the spelling to BUFFER and return it. */
400fbf9f 6559
3e4093b6
RS
6560static char *
6561print_spelling (char *buffer)
400fbf9f 6562{
3e4093b6
RS
6563 char *d = buffer;
6564 struct spelling *p;
400fbf9f 6565
3e4093b6
RS
6566 for (p = spelling_base; p < spelling; p++)
6567 if (p->kind == SPELLING_BOUNDS)
6568 {
a0f0ab9f 6569 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6570 d += strlen (d);
6571 }
6572 else
6573 {
6574 const char *s;
6575 if (p->kind == SPELLING_MEMBER)
6576 *d++ = '.';
6577 for (s = p->u.s; (*d = *s++); d++)
6578 ;
6579 }
6580 *d++ = '\0';
6581 return buffer;
6582}
400fbf9f 6583
3e4093b6
RS
6584/* Digest the parser output INIT as an initializer for type TYPE.
6585 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6586
bbbbb16a
ILT
6587 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6588
928c19bb
JM
6589 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6590
916c5919
JM
6591 If INIT is a string constant, STRICT_STRING is true if it is
6592 unparenthesized or we should not warn here for it being parenthesized.
6593 For other types of INIT, STRICT_STRING is not used.
6594
c2255bc4
AH
6595 INIT_LOC is the location of the INIT.
6596
3e4093b6
RS
6597 REQUIRE_CONSTANT requests an error if non-constant initializers or
6598 elements are seen. */
7e842ef8 6599
3e4093b6 6600static tree
c2255bc4
AH
6601digest_init (location_t init_loc, tree type, tree init, tree origtype,
6602 bool null_pointer_constant, bool strict_string,
6603 int require_constant)
3e4093b6
RS
6604{
6605 enum tree_code code = TREE_CODE (type);
6606 tree inside_init = init;
8ce94e44 6607 tree semantic_type = NULL_TREE;
928c19bb 6608 bool maybe_const = true;
7e842ef8 6609
3e4093b6 6610 if (type == error_mark_node
f01da1a5 6611 || !init
7a0ca710 6612 || error_operand_p (init))
3e4093b6 6613 return error_mark_node;
7e842ef8 6614
ed248cf7 6615 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6616
8ce94e44
JM
6617 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6618 {
6619 semantic_type = TREE_TYPE (inside_init);
6620 inside_init = TREE_OPERAND (inside_init, 0);
6621 }
928c19bb
JM
6622 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6623 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6624
3e4093b6
RS
6625 /* Initialization of an array of chars from a string constant
6626 optionally enclosed in braces. */
7e842ef8 6627
197463ae
JM
6628 if (code == ARRAY_TYPE && inside_init
6629 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6630 {
267bac10
JM
6631 tree typ1
6632 = (TYPE_ATOMIC (TREE_TYPE (type))
6633 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6634 TYPE_QUAL_ATOMIC)
6635 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6636 /* Note that an array could be both an array of character type
6637 and an array of wchar_t if wchar_t is signed char or unsigned
6638 char. */
6639 bool char_array = (typ1 == char_type_node
6640 || typ1 == signed_char_type_node
6641 || typ1 == unsigned_char_type_node);
6642 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6643 bool char16_array = !!comptypes (typ1, char16_type_node);
6644 bool char32_array = !!comptypes (typ1, char32_type_node);
6645
6646 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6647 {
916c5919 6648 struct c_expr expr;
c466b2cd 6649 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6650 expr.value = inside_init;
6651 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6652 expr.original_type = NULL;
d033409e 6653 maybe_warn_string_init (init_loc, type, expr);
916c5919 6654
a45e580b 6655 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6656 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6657 "initialization of a flexible array member");
6658
3e4093b6 6659 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6660 TYPE_MAIN_VARIANT (type)))
3e4093b6 6661 return inside_init;
7e842ef8 6662
c466b2cd 6663 if (char_array)
3e4093b6 6664 {
c466b2cd
KVH
6665 if (typ2 != char_type_node)
6666 {
ea58ef42
MP
6667 error_init (init_loc, "char-array initialized from wide "
6668 "string");
c466b2cd
KVH
6669 return error_mark_node;
6670 }
3e4093b6 6671 }
c466b2cd 6672 else
3e4093b6 6673 {
c466b2cd
KVH
6674 if (typ2 == char_type_node)
6675 {
ea58ef42
MP
6676 error_init (init_loc, "wide character array initialized "
6677 "from non-wide string");
c466b2cd
KVH
6678 return error_mark_node;
6679 }
6680 else if (!comptypes(typ1, typ2))
6681 {
ea58ef42
MP
6682 error_init (init_loc, "wide character array initialized "
6683 "from incompatible wide string");
c466b2cd
KVH
6684 return error_mark_node;
6685 }
7e842ef8 6686 }
2f6e4e97 6687
3e4093b6
RS
6688 TREE_TYPE (inside_init) = type;
6689 if (TYPE_DOMAIN (type) != 0
6690 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6691 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6692 {
6693 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6694
c466b2cd 6695 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6696 because it's ok to ignore the terminating null char
6697 that is counted in the length of the constant. */
5eb4df45
ILT
6698 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6699 (len
6700 - (TYPE_PRECISION (typ1)
6701 / BITS_PER_UNIT))))
6702 pedwarn_init (init_loc, 0,
6703 ("initializer-string for array of chars "
6704 "is too long"));
6705 else if (warn_cxx_compat
6706 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6707 warning_at (init_loc, OPT_Wc___compat,
6708 ("initializer-string for array chars "
6709 "is too long for C++"));
6710 }
7e842ef8 6711
3e4093b6 6712 return inside_init;
7e842ef8 6713 }
197463ae
JM
6714 else if (INTEGRAL_TYPE_P (typ1))
6715 {
ea58ef42 6716 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6717 "from string constant");
6718 return error_mark_node;
6719 }
7e842ef8
PE
6720 }
6721
3e4093b6
RS
6722 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6723 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6724 below and handle as a constructor. */
e89be13b
JJ
6725 if (code == VECTOR_TYPE
6726 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6727 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6728 && TREE_CONSTANT (inside_init))
6729 {
6730 if (TREE_CODE (inside_init) == VECTOR_CST
6731 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6732 TYPE_MAIN_VARIANT (type)))
6733 return inside_init;
6734
6735 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6736 {
4038c495
GB
6737 unsigned HOST_WIDE_INT ix;
6738 tree value;
6739 bool constant_p = true;
e89be13b
JJ
6740
6741 /* Iterate through elements and check if all constructor
6742 elements are *_CSTs. */
4038c495
GB
6743 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6744 if (!CONSTANT_CLASS_P (value))
6745 {
6746 constant_p = false;
6747 break;
6748 }
e89be13b 6749
4038c495
GB
6750 if (constant_p)
6751 return build_vector_from_ctor (type,
6752 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6753 }
6754 }
6035d635 6755
ca085fd7
MLI
6756 if (warn_sequence_point)
6757 verify_sequence_points (inside_init);
6758
3e4093b6
RS
6759 /* Any type can be initialized
6760 from an expression of the same type, optionally with braces. */
400fbf9f 6761
3e4093b6
RS
6762 if (inside_init && TREE_TYPE (inside_init) != 0
6763 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6764 TYPE_MAIN_VARIANT (type))
3e4093b6 6765 || (code == ARRAY_TYPE
132da1a5 6766 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6767 || (code == VECTOR_TYPE
132da1a5 6768 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6769 || (code == POINTER_TYPE
3897f229 6770 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6771 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6772 TREE_TYPE (type)))))
3e4093b6
RS
6773 {
6774 if (code == POINTER_TYPE)
b494fd98 6775 {
b494fd98
EB
6776 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6777 {
f2a71bbc
JM
6778 if (TREE_CODE (inside_init) == STRING_CST
6779 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6780 inside_init = array_to_pointer_conversion
6781 (init_loc, inside_init);
f2a71bbc
JM
6782 else
6783 {
ea58ef42 6784 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
6785 return error_mark_node;
6786 }
b494fd98 6787 }
f2a71bbc 6788 }
b494fd98 6789
bae39a73
NS
6790 if (code == VECTOR_TYPE)
6791 /* Although the types are compatible, we may require a
6792 conversion. */
6793 inside_init = convert (type, inside_init);
3e4093b6 6794
ca58211b 6795 if (require_constant
3e4093b6 6796 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6797 {
3e4093b6
RS
6798 /* As an extension, allow initializing objects with static storage
6799 duration with compound literals (which are then treated just as
ca58211b
PB
6800 the brace enclosed list they contain). Also allow this for
6801 vectors, as we can only assign them with compound literals. */
7278465e
MP
6802 if (flag_isoc99 && code != VECTOR_TYPE)
6803 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6804 "is not constant");
3e4093b6
RS
6805 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6806 inside_init = DECL_INITIAL (decl);
400fbf9f 6807 }
3e4093b6
RS
6808
6809 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6810 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6811 {
ea58ef42
MP
6812 error_init (init_loc, "array initialized from non-constant array "
6813 "expression");
3e4093b6 6814 return error_mark_node;
400fbf9f 6815 }
400fbf9f 6816
c1771a20 6817 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6818 -pedantic-errors is specified. In the later case, we always want
6819 an error. In the former case, we simply want a warning. */
6820 if (require_constant && pedantic
6821 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6822 {
6823 inside_init
6824 = valid_compound_expr_initializer (inside_init,
6825 TREE_TYPE (inside_init));
6826 if (inside_init == error_mark_node)
ea58ef42 6827 error_init (init_loc, "initializer element is not constant");
2f6e4e97 6828 else
c1771a20 6829 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6830 "initializer element is not constant");
3e4093b6
RS
6831 if (flag_pedantic_errors)
6832 inside_init = error_mark_node;
6833 }
6834 else if (require_constant
116df786
RH
6835 && !initializer_constant_valid_p (inside_init,
6836 TREE_TYPE (inside_init)))
3e4093b6 6837 {
ea58ef42 6838 error_init (init_loc, "initializer element is not constant");
3e4093b6 6839 inside_init = error_mark_node;
8b40563c 6840 }
928c19bb 6841 else if (require_constant && !maybe_const)
c2255bc4 6842 pedwarn_init (init_loc, 0,
928c19bb 6843 "initializer element is not a constant expression");
f735a153 6844
90137d8f 6845 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6846 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
6847 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6848 type, inside_init, origtype,
bbbbb16a 6849 ic_init, null_pointer_constant,
928c19bb 6850 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6851 return inside_init;
6852 }
f735a153 6853
3e4093b6 6854 /* Handle scalar types, including conversions. */
400fbf9f 6855
ab22c1fa
CF
6856 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6857 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6858 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6859 {
f2a71bbc
JM
6860 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6861 && (TREE_CODE (init) == STRING_CST
6862 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6863 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6864 if (semantic_type)
6865 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6866 inside_init);
3e4093b6 6867 inside_init
68fca595
MP
6868 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6869 inside_init, origtype, ic_init,
6870 null_pointer_constant, NULL_TREE, NULL_TREE,
6871 0);
2f6e4e97 6872
3274deff
JW
6873 /* Check to see if we have already given an error message. */
6874 if (inside_init == error_mark_node)
6875 ;
3f75a254 6876 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6877 {
ea58ef42 6878 error_init (init_loc, "initializer element is not constant");
3e4093b6 6879 inside_init = error_mark_node;
400fbf9f 6880 }
3e4093b6 6881 else if (require_constant
116df786
RH
6882 && !initializer_constant_valid_p (inside_init,
6883 TREE_TYPE (inside_init)))
400fbf9f 6884 {
ea58ef42
MP
6885 error_init (init_loc, "initializer element is not computable at "
6886 "load time");
3e4093b6 6887 inside_init = error_mark_node;
400fbf9f 6888 }
928c19bb 6889 else if (require_constant && !maybe_const)
c2255bc4 6890 pedwarn_init (init_loc, 0,
928c19bb 6891 "initializer element is not a constant expression");
3e4093b6
RS
6892
6893 return inside_init;
400fbf9f 6894 }
d9fc6069 6895
3e4093b6 6896 /* Come here only for records and arrays. */
d9fc6069 6897
3e4093b6 6898 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6899 {
ea58ef42 6900 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 6901 return error_mark_node;
d9fc6069 6902 }
3e4093b6 6903
ea58ef42 6904 error_init (init_loc, "invalid initializer");
3e4093b6 6905 return error_mark_node;
d9fc6069 6906}
400fbf9f 6907\f
3e4093b6 6908/* Handle initializers that use braces. */
400fbf9f 6909
3e4093b6
RS
6910/* Type of object we are accumulating a constructor for.
6911 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6912static tree constructor_type;
400fbf9f 6913
3e4093b6
RS
6914/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6915 left to fill. */
6916static tree constructor_fields;
400fbf9f 6917
3e4093b6
RS
6918/* For an ARRAY_TYPE, this is the specified index
6919 at which to store the next element we get. */
6920static tree constructor_index;
400fbf9f 6921
3e4093b6
RS
6922/* For an ARRAY_TYPE, this is the maximum index. */
6923static tree constructor_max_index;
400fbf9f 6924
3e4093b6
RS
6925/* For a RECORD_TYPE, this is the first field not yet written out. */
6926static tree constructor_unfilled_fields;
400fbf9f 6927
3e4093b6
RS
6928/* For an ARRAY_TYPE, this is the index of the first element
6929 not yet written out. */
6930static tree constructor_unfilled_index;
895ea614 6931
3e4093b6
RS
6932/* In a RECORD_TYPE, the byte index of the next consecutive field.
6933 This is so we can generate gaps between fields, when appropriate. */
6934static tree constructor_bit_index;
10d5caec 6935
3e4093b6
RS
6936/* If we are saving up the elements rather than allocating them,
6937 this is the list of elements so far (in reverse order,
6938 most recent first). */
9771b263 6939static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6940
3e4093b6
RS
6941/* 1 if constructor should be incrementally stored into a constructor chain,
6942 0 if all the elements should be kept in AVL tree. */
6943static int constructor_incremental;
ad47f1e5 6944
3e4093b6
RS
6945/* 1 if so far this constructor's elements are all compile-time constants. */
6946static int constructor_constant;
ad47f1e5 6947
3e4093b6
RS
6948/* 1 if so far this constructor's elements are all valid address constants. */
6949static int constructor_simple;
ad47f1e5 6950
928c19bb
JM
6951/* 1 if this constructor has an element that cannot be part of a
6952 constant expression. */
6953static int constructor_nonconst;
6954
3e4093b6
RS
6955/* 1 if this constructor is erroneous so far. */
6956static int constructor_erroneous;
d45cf215 6957
9bac5cbb
G
6958/* 1 if this constructor is the universal zero initializer { 0 }. */
6959static int constructor_zeroinit;
6960
3e4093b6
RS
6961/* Structure for managing pending initializer elements, organized as an
6962 AVL tree. */
d45cf215 6963
3e4093b6 6964struct init_node
d45cf215 6965{
3e4093b6
RS
6966 struct init_node *left, *right;
6967 struct init_node *parent;
6968 int balance;
6969 tree purpose;
6970 tree value;
bbbbb16a 6971 tree origtype;
d45cf215
RS
6972};
6973
3e4093b6
RS
6974/* Tree of pending elements at this constructor level.
6975 These are elements encountered out of order
6976 which belong at places we haven't reached yet in actually
6977 writing the output.
6978 Will never hold tree nodes across GC runs. */
6979static struct init_node *constructor_pending_elts;
d45cf215 6980
3e4093b6
RS
6981/* The SPELLING_DEPTH of this constructor. */
6982static int constructor_depth;
d45cf215 6983
3e4093b6
RS
6984/* DECL node for which an initializer is being read.
6985 0 means we are reading a constructor expression
6986 such as (struct foo) {...}. */
6987static tree constructor_decl;
d45cf215 6988
3e4093b6
RS
6989/* Nonzero if this is an initializer for a top-level decl. */
6990static int constructor_top_level;
d45cf215 6991
3e4093b6
RS
6992/* Nonzero if there were any member designators in this initializer. */
6993static int constructor_designated;
d45cf215 6994
3e4093b6
RS
6995/* Nesting depth of designator list. */
6996static int designator_depth;
d45cf215 6997
3e4093b6 6998/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 6999static int designator_erroneous;
d45cf215 7000
3e4093b6
RS
7001\f
7002/* This stack has a level for each implicit or explicit level of
7003 structuring in the initializer, including the outermost one. It
7004 saves the values of most of the variables above. */
d45cf215 7005
3e4093b6
RS
7006struct constructor_range_stack;
7007
7008struct constructor_stack
d45cf215 7009{
3e4093b6
RS
7010 struct constructor_stack *next;
7011 tree type;
7012 tree fields;
7013 tree index;
7014 tree max_index;
7015 tree unfilled_index;
7016 tree unfilled_fields;
7017 tree bit_index;
9771b263 7018 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7019 struct init_node *pending_elts;
7020 int offset;
7021 int depth;
916c5919 7022 /* If value nonzero, this value should replace the entire
3e4093b6 7023 constructor at this level. */
916c5919 7024 struct c_expr replacement_value;
3e4093b6
RS
7025 struct constructor_range_stack *range_stack;
7026 char constant;
7027 char simple;
928c19bb 7028 char nonconst;
3e4093b6
RS
7029 char implicit;
7030 char erroneous;
7031 char outer;
7032 char incremental;
7033 char designated;
976d5a22 7034 int designator_depth;
3e4093b6 7035};
d45cf215 7036
802415d1 7037static struct constructor_stack *constructor_stack;
d45cf215 7038
3e4093b6
RS
7039/* This stack represents designators from some range designator up to
7040 the last designator in the list. */
d45cf215 7041
3e4093b6
RS
7042struct constructor_range_stack
7043{
7044 struct constructor_range_stack *next, *prev;
7045 struct constructor_stack *stack;
7046 tree range_start;
7047 tree index;
7048 tree range_end;
7049 tree fields;
7050};
d45cf215 7051
802415d1 7052static struct constructor_range_stack *constructor_range_stack;
d45cf215 7053
3e4093b6
RS
7054/* This stack records separate initializers that are nested.
7055 Nested initializers can't happen in ANSI C, but GNU C allows them
7056 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7057
3e4093b6 7058struct initializer_stack
d45cf215 7059{
3e4093b6
RS
7060 struct initializer_stack *next;
7061 tree decl;
3e4093b6
RS
7062 struct constructor_stack *constructor_stack;
7063 struct constructor_range_stack *constructor_range_stack;
9771b263 7064 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7065 struct spelling *spelling;
7066 struct spelling *spelling_base;
7067 int spelling_size;
7068 char top_level;
7069 char require_constant_value;
7070 char require_constant_elements;
7071};
d45cf215 7072
802415d1 7073static struct initializer_stack *initializer_stack;
3e4093b6
RS
7074\f
7075/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7076
7077void
a396f8ae 7078start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7079{
3e4093b6 7080 const char *locus;
cceb1885 7081 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7082
3e4093b6 7083 p->decl = constructor_decl;
3e4093b6
RS
7084 p->require_constant_value = require_constant_value;
7085 p->require_constant_elements = require_constant_elements;
7086 p->constructor_stack = constructor_stack;
7087 p->constructor_range_stack = constructor_range_stack;
7088 p->elements = constructor_elements;
7089 p->spelling = spelling;
7090 p->spelling_base = spelling_base;
7091 p->spelling_size = spelling_size;
7092 p->top_level = constructor_top_level;
7093 p->next = initializer_stack;
7094 initializer_stack = p;
400fbf9f 7095
3e4093b6 7096 constructor_decl = decl;
3e4093b6
RS
7097 constructor_designated = 0;
7098 constructor_top_level = top_level;
400fbf9f 7099
6f17bbcf 7100 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7101 {
7102 require_constant_value = TREE_STATIC (decl);
7103 require_constant_elements
7104 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7105 /* For a scalar, you can always use any value to initialize,
7106 even within braces. */
7107 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7108 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7109 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7110 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 7111 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7112 }
7113 else
7114 {
7115 require_constant_value = 0;
7116 require_constant_elements = 0;
88388a52 7117 locus = _("(anonymous)");
3e4093b6 7118 }
b71c7f8a 7119
3e4093b6
RS
7120 constructor_stack = 0;
7121 constructor_range_stack = 0;
b71c7f8a 7122
9bac5cbb 7123 found_missing_braces = 0;
3e4093b6
RS
7124
7125 spelling_base = 0;
7126 spelling_size = 0;
7127 RESTORE_SPELLING_DEPTH (0);
7128
7129 if (locus)
7130 push_string (locus);
7131}
7132
7133void
7134finish_init (void)
b71c7f8a 7135{
3e4093b6 7136 struct initializer_stack *p = initializer_stack;
b71c7f8a 7137
3e4093b6
RS
7138 /* Free the whole constructor stack of this initializer. */
7139 while (constructor_stack)
7140 {
7141 struct constructor_stack *q = constructor_stack;
7142 constructor_stack = q->next;
7143 free (q);
7144 }
7145
366de0ce 7146 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7147
7148 /* Pop back to the data of the outer initializer (if any). */
36579663 7149 free (spelling_base);
3aeb3655 7150
3e4093b6 7151 constructor_decl = p->decl;
3e4093b6
RS
7152 require_constant_value = p->require_constant_value;
7153 require_constant_elements = p->require_constant_elements;
7154 constructor_stack = p->constructor_stack;
7155 constructor_range_stack = p->constructor_range_stack;
7156 constructor_elements = p->elements;
7157 spelling = p->spelling;
7158 spelling_base = p->spelling_base;
7159 spelling_size = p->spelling_size;
7160 constructor_top_level = p->top_level;
7161 initializer_stack = p->next;
7162 free (p);
b71c7f8a 7163}
400fbf9f 7164\f
3e4093b6
RS
7165/* Call here when we see the initializer is surrounded by braces.
7166 This is instead of a call to push_init_level;
7167 it is matched by a call to pop_init_level.
400fbf9f 7168
3e4093b6
RS
7169 TYPE is the type to initialize, for a constructor expression.
7170 For an initializer for a decl, TYPE is zero. */
400fbf9f 7171
3e4093b6
RS
7172void
7173really_start_incremental_init (tree type)
400fbf9f 7174{
5d038c4c 7175 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7176
3e4093b6
RS
7177 if (type == 0)
7178 type = TREE_TYPE (constructor_decl);
400fbf9f 7179
b6fc2cdb
PB
7180 if (TREE_CODE (type) == VECTOR_TYPE
7181 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7182 error ("opaque vector types cannot be initialized");
400fbf9f 7183
3e4093b6
RS
7184 p->type = constructor_type;
7185 p->fields = constructor_fields;
7186 p->index = constructor_index;
7187 p->max_index = constructor_max_index;
7188 p->unfilled_index = constructor_unfilled_index;
7189 p->unfilled_fields = constructor_unfilled_fields;
7190 p->bit_index = constructor_bit_index;
7191 p->elements = constructor_elements;
7192 p->constant = constructor_constant;
7193 p->simple = constructor_simple;
928c19bb 7194 p->nonconst = constructor_nonconst;
3e4093b6
RS
7195 p->erroneous = constructor_erroneous;
7196 p->pending_elts = constructor_pending_elts;
7197 p->depth = constructor_depth;
916c5919
JM
7198 p->replacement_value.value = 0;
7199 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7200 p->replacement_value.original_type = NULL;
3e4093b6
RS
7201 p->implicit = 0;
7202 p->range_stack = 0;
7203 p->outer = 0;
7204 p->incremental = constructor_incremental;
7205 p->designated = constructor_designated;
976d5a22 7206 p->designator_depth = designator_depth;
3e4093b6
RS
7207 p->next = 0;
7208 constructor_stack = p;
b13aca19 7209
3e4093b6
RS
7210 constructor_constant = 1;
7211 constructor_simple = 1;
928c19bb 7212 constructor_nonconst = 0;
3e4093b6 7213 constructor_depth = SPELLING_DEPTH ();
9771b263 7214 constructor_elements = NULL;
3e4093b6
RS
7215 constructor_pending_elts = 0;
7216 constructor_type = type;
7217 constructor_incremental = 1;
7218 constructor_designated = 0;
9bac5cbb 7219 constructor_zeroinit = 1;
3e4093b6 7220 designator_depth = 0;
b06df647 7221 designator_erroneous = 0;
400fbf9f 7222
3e4093b6
RS
7223 if (TREE_CODE (constructor_type) == RECORD_TYPE
7224 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7225 {
3e4093b6
RS
7226 constructor_fields = TYPE_FIELDS (constructor_type);
7227 /* Skip any nameless bit fields at the beginning. */
7228 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7229 && DECL_NAME (constructor_fields) == 0)
910ad8de 7230 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7231
3e4093b6
RS
7232 constructor_unfilled_fields = constructor_fields;
7233 constructor_bit_index = bitsize_zero_node;
400fbf9f 7234 }
3e4093b6
RS
7235 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7236 {
7237 if (TYPE_DOMAIN (constructor_type))
7238 {
7239 constructor_max_index
7240 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7241
3e4093b6
RS
7242 /* Detect non-empty initializations of zero-length arrays. */
7243 if (constructor_max_index == NULL_TREE
7244 && TYPE_SIZE (constructor_type))
9a9d280e 7245 constructor_max_index = integer_minus_one_node;
400fbf9f 7246
3e4093b6
RS
7247 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7248 to initialize VLAs will cause a proper error; avoid tree
7249 checking errors as well by setting a safe value. */
7250 if (constructor_max_index
7251 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7252 constructor_max_index = integer_minus_one_node;
59c83dbf 7253
3e4093b6
RS
7254 constructor_index
7255 = convert (bitsizetype,
7256 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7257 }
3e4093b6 7258 else
493179da
JM
7259 {
7260 constructor_index = bitsize_zero_node;
7261 constructor_max_index = NULL_TREE;
7262 }
59c83dbf 7263
3e4093b6
RS
7264 constructor_unfilled_index = constructor_index;
7265 }
7266 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7267 {
7268 /* Vectors are like simple fixed-size arrays. */
7269 constructor_max_index =
c62c040f 7270 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7271 constructor_index = bitsize_zero_node;
3e4093b6
RS
7272 constructor_unfilled_index = constructor_index;
7273 }
7274 else
7275 {
7276 /* Handle the case of int x = {5}; */
7277 constructor_fields = constructor_type;
7278 constructor_unfilled_fields = constructor_type;
7279 }
7280}
7281\f
7282/* Push down into a subobject, for initialization.
7283 If this is for an explicit set of braces, IMPLICIT is 0.
7284 If it is because the next element belongs at a lower level,
7285 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7286
3e4093b6 7287void
ea58ef42
MP
7288push_init_level (location_t loc, int implicit,
7289 struct obstack *braced_init_obstack)
3e4093b6
RS
7290{
7291 struct constructor_stack *p;
7292 tree value = NULL_TREE;
400fbf9f 7293
3e4093b6 7294 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7295 pop them now. If implicit == 1, this will have been done in
7296 process_init_element; do not repeat it here because in the case
7297 of excess initializers for an empty aggregate this leads to an
7298 infinite cycle of popping a level and immediately recreating
7299 it. */
7300 if (implicit != 1)
3e4093b6 7301 {
472d98b4
JM
7302 while (constructor_stack->implicit)
7303 {
7304 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7305 || TREE_CODE (constructor_type) == UNION_TYPE)
7306 && constructor_fields == 0)
34cf811f 7307 process_init_element (input_location,
ea58ef42 7308 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7309 true, braced_init_obstack);
472d98b4
JM
7310 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7311 && constructor_max_index
7312 && tree_int_cst_lt (constructor_max_index,
7313 constructor_index))
34cf811f 7314 process_init_element (input_location,
ea58ef42 7315 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7316 true, braced_init_obstack);
472d98b4
JM
7317 else
7318 break;
7319 }
3e4093b6 7320 }
400fbf9f 7321
3e4093b6
RS
7322 /* Unless this is an explicit brace, we need to preserve previous
7323 content if any. */
7324 if (implicit)
7325 {
7326 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7327 || TREE_CODE (constructor_type) == UNION_TYPE)
7328 && constructor_fields)
a1e3b3d9 7329 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7330 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7331 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7332 }
7333
5d038c4c 7334 p = XNEW (struct constructor_stack);
3e4093b6
RS
7335 p->type = constructor_type;
7336 p->fields = constructor_fields;
7337 p->index = constructor_index;
7338 p->max_index = constructor_max_index;
7339 p->unfilled_index = constructor_unfilled_index;
7340 p->unfilled_fields = constructor_unfilled_fields;
7341 p->bit_index = constructor_bit_index;
7342 p->elements = constructor_elements;
7343 p->constant = constructor_constant;
7344 p->simple = constructor_simple;
928c19bb 7345 p->nonconst = constructor_nonconst;
3e4093b6
RS
7346 p->erroneous = constructor_erroneous;
7347 p->pending_elts = constructor_pending_elts;
7348 p->depth = constructor_depth;
916c5919
JM
7349 p->replacement_value.value = 0;
7350 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7351 p->replacement_value.original_type = NULL;
3e4093b6
RS
7352 p->implicit = implicit;
7353 p->outer = 0;
7354 p->incremental = constructor_incremental;
7355 p->designated = constructor_designated;
976d5a22 7356 p->designator_depth = designator_depth;
3e4093b6
RS
7357 p->next = constructor_stack;
7358 p->range_stack = 0;
7359 constructor_stack = p;
400fbf9f 7360
3e4093b6
RS
7361 constructor_constant = 1;
7362 constructor_simple = 1;
928c19bb 7363 constructor_nonconst = 0;
3e4093b6 7364 constructor_depth = SPELLING_DEPTH ();
9771b263 7365 constructor_elements = NULL;
3e4093b6
RS
7366 constructor_incremental = 1;
7367 constructor_designated = 0;
7368 constructor_pending_elts = 0;
7369 if (!implicit)
400fbf9f 7370 {
3e4093b6
RS
7371 p->range_stack = constructor_range_stack;
7372 constructor_range_stack = 0;
7373 designator_depth = 0;
b06df647 7374 designator_erroneous = 0;
3e4093b6 7375 }
400fbf9f 7376
3e4093b6
RS
7377 /* Don't die if an entire brace-pair level is superfluous
7378 in the containing level. */
7379 if (constructor_type == 0)
7380 ;
7381 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7382 || TREE_CODE (constructor_type) == UNION_TYPE)
7383 {
7384 /* Don't die if there are extra init elts at the end. */
7385 if (constructor_fields == 0)
7386 constructor_type = 0;
7387 else
400fbf9f 7388 {
3e4093b6
RS
7389 constructor_type = TREE_TYPE (constructor_fields);
7390 push_member_name (constructor_fields);
7391 constructor_depth++;
400fbf9f 7392 }
6a358dcb
MP
7393 /* If upper initializer is designated, then mark this as
7394 designated too to prevent bogus warnings. */
7395 constructor_designated = p->designated;
3e4093b6
RS
7396 }
7397 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7398 {
7399 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7400 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7401 constructor_depth++;
400fbf9f
JW
7402 }
7403
3e4093b6 7404 if (constructor_type == 0)
400fbf9f 7405 {
ea58ef42 7406 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7407 constructor_fields = 0;
7408 constructor_unfilled_fields = 0;
7409 return;
400fbf9f
JW
7410 }
7411
3e4093b6
RS
7412 if (value && TREE_CODE (value) == CONSTRUCTOR)
7413 {
7414 constructor_constant = TREE_CONSTANT (value);
7415 constructor_simple = TREE_STATIC (value);
928c19bb 7416 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7417 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7418 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7419 && (TREE_CODE (constructor_type) == RECORD_TYPE
7420 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7421 set_nonincremental_init (braced_init_obstack);
3e4093b6 7422 }
400fbf9f 7423
9bac5cbb
G
7424 if (implicit == 1)
7425 found_missing_braces = 1;
400fbf9f 7426
3e4093b6
RS
7427 if (TREE_CODE (constructor_type) == RECORD_TYPE
7428 || TREE_CODE (constructor_type) == UNION_TYPE)
7429 {
7430 constructor_fields = TYPE_FIELDS (constructor_type);
7431 /* Skip any nameless bit fields at the beginning. */
7432 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7433 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7434 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7435
3e4093b6
RS
7436 constructor_unfilled_fields = constructor_fields;
7437 constructor_bit_index = bitsize_zero_node;
7438 }
7439 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7440 {
7441 /* Vectors are like simple fixed-size arrays. */
7442 constructor_max_index =
c62c040f
RG
7443 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7444 constructor_index = bitsize_int (0);
3e4093b6
RS
7445 constructor_unfilled_index = constructor_index;
7446 }
7447 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7448 {
7449 if (TYPE_DOMAIN (constructor_type))
7450 {
7451 constructor_max_index
7452 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7453
3e4093b6
RS
7454 /* Detect non-empty initializations of zero-length arrays. */
7455 if (constructor_max_index == NULL_TREE
7456 && TYPE_SIZE (constructor_type))
9a9d280e 7457 constructor_max_index = integer_minus_one_node;
de520661 7458
3e4093b6
RS
7459 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7460 to initialize VLAs will cause a proper error; avoid tree
7461 checking errors as well by setting a safe value. */
7462 if (constructor_max_index
7463 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7464 constructor_max_index = integer_minus_one_node;
b62acd60 7465
3e4093b6
RS
7466 constructor_index
7467 = convert (bitsizetype,
7468 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7469 }
7470 else
7471 constructor_index = bitsize_zero_node;
de520661 7472
3e4093b6
RS
7473 constructor_unfilled_index = constructor_index;
7474 if (value && TREE_CODE (value) == STRING_CST)
7475 {
7476 /* We need to split the char/wchar array into individual
7477 characters, so that we don't have to special case it
7478 everywhere. */
a1e3b3d9 7479 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7480 }
7481 }
7482 else
7483 {
b4519d39 7484 if (constructor_type != error_mark_node)
96b40f8d 7485 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7486 constructor_fields = constructor_type;
7487 constructor_unfilled_fields = constructor_type;
7488 }
7489}
8b6a5902 7490
3e4093b6 7491/* At the end of an implicit or explicit brace level,
916c5919
JM
7492 finish up that level of constructor. If a single expression
7493 with redundant braces initialized that level, return the
7494 c_expr structure for that expression. Otherwise, the original_code
7495 element is set to ERROR_MARK.
7496 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7497 from inner levels (process_init_element ignores that),
916c5919 7498 but return error_mark_node as the value from the outermost level
3e4093b6 7499 (that's what we want to put in DECL_INITIAL).
916c5919 7500 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7501
916c5919 7502struct c_expr
ea58ef42
MP
7503pop_init_level (location_t loc, int implicit,
7504 struct obstack *braced_init_obstack)
3e4093b6
RS
7505{
7506 struct constructor_stack *p;
916c5919
JM
7507 struct c_expr ret;
7508 ret.value = 0;
7509 ret.original_code = ERROR_MARK;
6866c6e8 7510 ret.original_type = NULL;
de520661 7511
3e4093b6
RS
7512 if (implicit == 0)
7513 {
7514 /* When we come to an explicit close brace,
7515 pop any inner levels that didn't have explicit braces. */
7516 while (constructor_stack->implicit)
34cf811f 7517 process_init_element (input_location,
ea58ef42 7518 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7519 true, braced_init_obstack);
366de0ce 7520 gcc_assert (!constructor_range_stack);
3e4093b6 7521 }
e5e809f4 7522
0066ef9c
RH
7523 /* Now output all pending elements. */
7524 constructor_incremental = 1;
a1e3b3d9 7525 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7526
3e4093b6 7527 p = constructor_stack;
e5e809f4 7528
3e4093b6
RS
7529 /* Error for initializing a flexible array member, or a zero-length
7530 array member in an inappropriate context. */
7531 if (constructor_type && constructor_fields
7532 && TREE_CODE (constructor_type) == ARRAY_TYPE
7533 && TYPE_DOMAIN (constructor_type)
3f75a254 7534 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7535 {
7536 /* Silently discard empty initializations. The parser will
7537 already have pedwarned for empty brackets. */
7538 if (integer_zerop (constructor_unfilled_index))
7539 constructor_type = NULL_TREE;
366de0ce 7540 else
3e4093b6 7541 {
366de0ce 7542 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7543
3e4093b6 7544 if (constructor_depth > 2)
ea58ef42 7545 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7546 else
d033409e 7547 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7548 "initialization of a flexible array member");
de520661 7549
3e4093b6
RS
7550 /* We have already issued an error message for the existence
7551 of a flexible array member not at the end of the structure.
535a42b1 7552 Discard the initializer so that we do not die later. */
910ad8de 7553 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7554 constructor_type = NULL_TREE;
7555 }
3e4093b6 7556 }
de520661 7557
84937de2
MP
7558 /* Initialization with { } counts as zeroinit. */
7559 if (vec_safe_length (constructor_elements) == 0)
7560 constructor_zeroinit = 1;
7561 /* If the constructor has more than one element, it can't be { 0 }. */
7562 else if (vec_safe_length (constructor_elements) != 1)
9bac5cbb
G
7563 constructor_zeroinit = 0;
7564
7565 /* Warn when some structs are initialized with direct aggregation. */
7566 if (!implicit && found_missing_braces && warn_missing_braces
7567 && !constructor_zeroinit)
7568 {
7569 warning_init (loc, OPT_Wmissing_braces,
7570 "missing braces around initializer");
7571 }
7572
3e4093b6 7573 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7574 if (warn_missing_field_initializers
3e4093b6
RS
7575 && constructor_type
7576 && TREE_CODE (constructor_type) == RECORD_TYPE
7577 && constructor_unfilled_fields)
7578 {
7579 /* Do not warn for flexible array members or zero-length arrays. */
7580 while (constructor_unfilled_fields
3f75a254 7581 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7582 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7583 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7584
49819fef
AM
7585 if (constructor_unfilled_fields
7586 /* Do not warn if this level of the initializer uses member
7587 designators; it is likely to be deliberate. */
7588 && !constructor_designated
84937de2 7589 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7590 && !constructor_zeroinit)
3e4093b6 7591 {
32397f22
MLI
7592 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7593 "missing initializer for field %qD of %qT",
7594 constructor_unfilled_fields,
7595 constructor_type))
7596 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7597 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7598 }
7599 }
de520661 7600
3e4093b6 7601 /* Pad out the end of the structure. */
916c5919 7602 if (p->replacement_value.value)
3e4093b6
RS
7603 /* If this closes a superfluous brace pair,
7604 just pass out the element between them. */
916c5919 7605 ret = p->replacement_value;
3e4093b6
RS
7606 else if (constructor_type == 0)
7607 ;
7608 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7609 && TREE_CODE (constructor_type) != UNION_TYPE
7610 && TREE_CODE (constructor_type) != ARRAY_TYPE
7611 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7612 {
7613 /* A nonincremental scalar initializer--just return
7614 the element, after verifying there is just one. */
9771b263 7615 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7616 {
7617 if (!constructor_erroneous)
ea58ef42 7618 error_init (loc, "empty scalar initializer");
916c5919 7619 ret.value = error_mark_node;
3e4093b6 7620 }
9771b263 7621 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7622 {
ea58ef42 7623 error_init (loc, "extra elements in scalar initializer");
9771b263 7624 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7625 }
7626 else
9771b263 7627 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7628 }
7629 else
7630 {
7631 if (constructor_erroneous)
916c5919 7632 ret.value = error_mark_node;
3e4093b6
RS
7633 else
7634 {
916c5919 7635 ret.value = build_constructor (constructor_type,
4038c495 7636 constructor_elements);
3e4093b6 7637 if (constructor_constant)
51eed280 7638 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7639 if (constructor_constant && constructor_simple)
916c5919 7640 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7641 if (constructor_nonconst)
7642 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7643 }
7644 }
de520661 7645
928c19bb
JM
7646 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7647 {
7648 if (constructor_nonconst)
7649 ret.original_code = C_MAYBE_CONST_EXPR;
7650 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7651 ret.original_code = ERROR_MARK;
7652 }
7653
3e4093b6
RS
7654 constructor_type = p->type;
7655 constructor_fields = p->fields;
7656 constructor_index = p->index;
7657 constructor_max_index = p->max_index;
7658 constructor_unfilled_index = p->unfilled_index;
7659 constructor_unfilled_fields = p->unfilled_fields;
7660 constructor_bit_index = p->bit_index;
7661 constructor_elements = p->elements;
7662 constructor_constant = p->constant;
7663 constructor_simple = p->simple;
928c19bb 7664 constructor_nonconst = p->nonconst;
3e4093b6
RS
7665 constructor_erroneous = p->erroneous;
7666 constructor_incremental = p->incremental;
7667 constructor_designated = p->designated;
976d5a22 7668 designator_depth = p->designator_depth;
3e4093b6
RS
7669 constructor_pending_elts = p->pending_elts;
7670 constructor_depth = p->depth;
7671 if (!p->implicit)
7672 constructor_range_stack = p->range_stack;
7673 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7674
3e4093b6
RS
7675 constructor_stack = p->next;
7676 free (p);
b621a4dd 7677
5d5e98dc
VR
7678 if (ret.value == 0 && constructor_stack == 0)
7679 ret.value = error_mark_node;
916c5919 7680 return ret;
3e4093b6 7681}
8b6a5902 7682
3e4093b6
RS
7683/* Common handling for both array range and field name designators.
7684 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7685
3e4093b6 7686static int
ea58ef42
MP
7687set_designator (location_t loc, int array,
7688 struct obstack *braced_init_obstack)
de520661 7689{
3e4093b6
RS
7690 tree subtype;
7691 enum tree_code subcode;
de520661 7692
3e4093b6
RS
7693 /* Don't die if an entire brace-pair level is superfluous
7694 in the containing level. */
7695 if (constructor_type == 0)
7696 return 1;
de520661 7697
366de0ce
NS
7698 /* If there were errors in this designator list already, bail out
7699 silently. */
b06df647 7700 if (designator_erroneous)
3e4093b6 7701 return 1;
e28cae4f 7702
3e4093b6
RS
7703 if (!designator_depth)
7704 {
366de0ce 7705 gcc_assert (!constructor_range_stack);
de520661 7706
3e4093b6
RS
7707 /* Designator list starts at the level of closest explicit
7708 braces. */
7709 while (constructor_stack->implicit)
34cf811f 7710 process_init_element (input_location,
ea58ef42 7711 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7712 true, braced_init_obstack);
3e4093b6
RS
7713 constructor_designated = 1;
7714 return 0;
7715 }
de520661 7716
366de0ce 7717 switch (TREE_CODE (constructor_type))
3c3fa147 7718 {
366de0ce
NS
7719 case RECORD_TYPE:
7720 case UNION_TYPE:
3e4093b6
RS
7721 subtype = TREE_TYPE (constructor_fields);
7722 if (subtype != error_mark_node)
7723 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7724 break;
7725 case ARRAY_TYPE:
3e4093b6 7726 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7727 break;
7728 default:
7729 gcc_unreachable ();
de520661 7730 }
400fbf9f 7731
3e4093b6
RS
7732 subcode = TREE_CODE (subtype);
7733 if (array && subcode != ARRAY_TYPE)
7734 {
ea58ef42 7735 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
7736 return 1;
7737 }
7738 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7739 {
ea58ef42 7740 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
7741 return 1;
7742 }
d45cf215 7743
3e4093b6 7744 constructor_designated = 1;
ea58ef42 7745 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 7746 return 0;
de520661 7747}
400fbf9f 7748
3e4093b6
RS
7749/* If there are range designators in designator list, push a new designator
7750 to constructor_range_stack. RANGE_END is end of such stack range or
7751 NULL_TREE if there is no range designator at this level. */
400fbf9f 7752
3e4093b6 7753static void
a1e3b3d9 7754push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7755{
7756 struct constructor_range_stack *p;
400fbf9f 7757
a1e3b3d9
LB
7758 p = (struct constructor_range_stack *)
7759 obstack_alloc (braced_init_obstack,
7760 sizeof (struct constructor_range_stack));
3e4093b6
RS
7761 p->prev = constructor_range_stack;
7762 p->next = 0;
7763 p->fields = constructor_fields;
7764 p->range_start = constructor_index;
7765 p->index = constructor_index;
7766 p->stack = constructor_stack;
7767 p->range_end = range_end;
8b6a5902 7768 if (constructor_range_stack)
3e4093b6
RS
7769 constructor_range_stack->next = p;
7770 constructor_range_stack = p;
de520661 7771}
400fbf9f 7772
3e4093b6
RS
7773/* Within an array initializer, specify the next index to be initialized.
7774 FIRST is that index. If LAST is nonzero, then initialize a range
7775 of indices, running from FIRST through LAST. */
5a7ec9d9 7776
de520661 7777void
ea58ef42 7778set_init_index (location_t loc, tree first, tree last,
d033409e 7779 struct obstack *braced_init_obstack)
de520661 7780{
ea58ef42 7781 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 7782 return;
de520661 7783
b06df647 7784 designator_erroneous = 1;
de520661 7785
3ea8cd06
JM
7786 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7787 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7788 {
ea58ef42 7789 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
7790 return;
7791 }
7792
2b6da65c
JM
7793 if (TREE_CODE (first) != INTEGER_CST)
7794 {
7795 first = c_fully_fold (first, false, NULL);
7796 if (TREE_CODE (first) == INTEGER_CST)
d033409e 7797 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7798 "array index in initializer is not "
7799 "an integer constant expression");
7800 }
7801
7802 if (last && TREE_CODE (last) != INTEGER_CST)
7803 {
7804 last = c_fully_fold (last, false, NULL);
7805 if (TREE_CODE (last) == INTEGER_CST)
d033409e 7806 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7807 "array index in initializer is not "
7808 "an integer constant expression");
7809 }
7810
3e4093b6 7811 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 7812 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7813 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 7814 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7815 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 7816 error_init (loc, "array index in non-array initializer");
622adc7e 7817 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 7818 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
7819 else if (constructor_max_index
7820 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 7821 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 7822 else
de520661 7823 {
928c19bb
JM
7824 constant_expression_warning (first);
7825 if (last)
7826 constant_expression_warning (last);
3e4093b6 7827 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7828 if (tree_int_cst_lt (constructor_index, first))
7829 {
7830 constructor_index = copy_node (constructor_index);
7831 TREE_OVERFLOW (constructor_index) = 1;
7832 }
665f2503 7833
3e4093b6 7834 if (last)
2bede729 7835 {
3e4093b6
RS
7836 if (tree_int_cst_equal (first, last))
7837 last = 0;
7838 else if (tree_int_cst_lt (last, first))
7839 {
ea58ef42 7840 error_init (loc, "empty index range in initializer");
3e4093b6
RS
7841 last = 0;
7842 }
7843 else
7844 {
7845 last = convert (bitsizetype, last);
7846 if (constructor_max_index != 0
7847 && tree_int_cst_lt (constructor_max_index, last))
7848 {
ea58ef42
MP
7849 error_init (loc, "array index range in initializer exceeds "
7850 "array bounds");
3e4093b6
RS
7851 last = 0;
7852 }
7853 }
2bede729 7854 }
fed3cef0 7855
3e4093b6 7856 designator_depth++;
b06df647 7857 designator_erroneous = 0;
3e4093b6 7858 if (constructor_range_stack || last)
a1e3b3d9 7859 push_range_stack (last, braced_init_obstack);
de520661 7860 }
de520661 7861}
3e4093b6
RS
7862
7863/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7864
de520661 7865void
ea58ef42
MP
7866set_init_label (location_t loc, tree fieldname,
7867 struct obstack *braced_init_obstack)
de520661 7868{
0fb96aa4 7869 tree field;
94ba5069 7870
ea58ef42 7871 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
7872 return;
7873
b06df647 7874 designator_erroneous = 1;
3e4093b6
RS
7875
7876 if (TREE_CODE (constructor_type) != RECORD_TYPE
7877 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7878 {
ea58ef42 7879 error_init (loc, "field name not in record or union initializer");
3e4093b6 7880 return;
94ba5069
RS
7881 }
7882
0fb96aa4 7883 field = lookup_field (constructor_type, fieldname);
8b6a5902 7884
0fb96aa4 7885 if (field == 0)
c51a1ba9 7886 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 7887 else
0fb96aa4
JM
7888 do
7889 {
7890 constructor_fields = TREE_VALUE (field);
7891 designator_depth++;
7892 designator_erroneous = 0;
7893 if (constructor_range_stack)
7894 push_range_stack (NULL_TREE, braced_init_obstack);
7895 field = TREE_CHAIN (field);
7896 if (field)
7897 {
ea58ef42 7898 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
7899 return;
7900 }
7901 }
7902 while (field != NULL_TREE);
3e4093b6
RS
7903}
7904\f
7905/* Add a new initializer to the tree of pending initializers. PURPOSE
7906 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7907 VALUE is the value of that index or field. If ORIGTYPE is not
7908 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7909
7910 IMPLICIT is true if value comes from pop_init_level (1),
7911 the new initializer has been merged with the existing one
7912 and thus no warnings should be emitted about overriding an
7913 existing initializer. */
de520661 7914
3e4093b6 7915static void
96b40f8d
MP
7916add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7917 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
7918{
7919 struct init_node *p, **q, *r;
7920
7921 q = &constructor_pending_elts;
7922 p = 0;
7923
7924 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7925 {
3e4093b6 7926 while (*q != 0)
91fa3c30 7927 {
3e4093b6
RS
7928 p = *q;
7929 if (tree_int_cst_lt (purpose, p->purpose))
7930 q = &p->left;
7931 else if (tree_int_cst_lt (p->purpose, purpose))
7932 q = &p->right;
7933 else
7934 {
b295aee2
JJ
7935 if (!implicit)
7936 {
7937 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
7938 warning_init (loc, 0,
7939 "initialized field with side-effects "
7940 "overwritten");
b295aee2 7941 else if (warn_override_init)
96b40f8d
MP
7942 warning_init (loc, OPT_Woverride_init,
7943 "initialized field overwritten");
b295aee2 7944 }
3e4093b6 7945 p->value = value;
bbbbb16a 7946 p->origtype = origtype;
3e4093b6
RS
7947 return;
7948 }
91fa3c30 7949 }
de520661 7950 }
3e4093b6 7951 else
de520661 7952 {
3e4093b6 7953 tree bitpos;
400fbf9f 7954
3e4093b6
RS
7955 bitpos = bit_position (purpose);
7956 while (*q != NULL)
7957 {
7958 p = *q;
7959 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7960 q = &p->left;
7961 else if (p->purpose != purpose)
7962 q = &p->right;
7963 else
7964 {
b295aee2
JJ
7965 if (!implicit)
7966 {
7967 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
7968 warning_init (loc, 0,
7969 "initialized field with side-effects "
7970 "overwritten");
b295aee2 7971 else if (warn_override_init)
96b40f8d
MP
7972 warning_init (loc, OPT_Woverride_init,
7973 "initialized field overwritten");
b295aee2 7974 }
3e4093b6 7975 p->value = value;
bbbbb16a 7976 p->origtype = origtype;
3e4093b6
RS
7977 return;
7978 }
7979 }
91fa3c30 7980 }
b71c7f8a 7981
a1e3b3d9
LB
7982 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7983 sizeof (struct init_node));
3e4093b6
RS
7984 r->purpose = purpose;
7985 r->value = value;
bbbbb16a 7986 r->origtype = origtype;
8b6a5902 7987
3e4093b6
RS
7988 *q = r;
7989 r->parent = p;
7990 r->left = 0;
7991 r->right = 0;
7992 r->balance = 0;
b71c7f8a 7993
3e4093b6 7994 while (p)
de520661 7995 {
3e4093b6 7996 struct init_node *s;
665f2503 7997
3e4093b6 7998 if (r == p->left)
2bede729 7999 {
3e4093b6
RS
8000 if (p->balance == 0)
8001 p->balance = -1;
8002 else if (p->balance < 0)
8003 {
8004 if (r->balance < 0)
8005 {
8006 /* L rotation. */
8007 p->left = r->right;
8008 if (p->left)
8009 p->left->parent = p;
8010 r->right = p;
e7b6a0ee 8011
3e4093b6
RS
8012 p->balance = 0;
8013 r->balance = 0;
39bc99c2 8014
3e4093b6
RS
8015 s = p->parent;
8016 p->parent = r;
8017 r->parent = s;
8018 if (s)
8019 {
8020 if (s->left == p)
8021 s->left = r;
8022 else
8023 s->right = r;
8024 }
8025 else
8026 constructor_pending_elts = r;
8027 }
8028 else
8029 {
8030 /* LR rotation. */
8031 struct init_node *t = r->right;
e7b6a0ee 8032
3e4093b6
RS
8033 r->right = t->left;
8034 if (r->right)
8035 r->right->parent = r;
8036 t->left = r;
8037
8038 p->left = t->right;
8039 if (p->left)
8040 p->left->parent = p;
8041 t->right = p;
8042
8043 p->balance = t->balance < 0;
8044 r->balance = -(t->balance > 0);
8045 t->balance = 0;
8046
8047 s = p->parent;
8048 p->parent = t;
8049 r->parent = t;
8050 t->parent = s;
8051 if (s)
8052 {
8053 if (s->left == p)
8054 s->left = t;
8055 else
8056 s->right = t;
8057 }
8058 else
8059 constructor_pending_elts = t;
8060 }
8061 break;
8062 }
8063 else
8064 {
8065 /* p->balance == +1; growth of left side balances the node. */
8066 p->balance = 0;
8067 break;
8068 }
2bede729 8069 }
3e4093b6
RS
8070 else /* r == p->right */
8071 {
8072 if (p->balance == 0)
8073 /* Growth propagation from right side. */
8074 p->balance++;
8075 else if (p->balance > 0)
8076 {
8077 if (r->balance > 0)
8078 {
8079 /* R rotation. */
8080 p->right = r->left;
8081 if (p->right)
8082 p->right->parent = p;
8083 r->left = p;
8084
8085 p->balance = 0;
8086 r->balance = 0;
8087
8088 s = p->parent;
8089 p->parent = r;
8090 r->parent = s;
8091 if (s)
8092 {
8093 if (s->left == p)
8094 s->left = r;
8095 else
8096 s->right = r;
8097 }
8098 else
8099 constructor_pending_elts = r;
8100 }
8101 else /* r->balance == -1 */
8102 {
8103 /* RL rotation */
8104 struct init_node *t = r->left;
8105
8106 r->left = t->right;
8107 if (r->left)
8108 r->left->parent = r;
8109 t->right = r;
8110
8111 p->right = t->left;
8112 if (p->right)
8113 p->right->parent = p;
8114 t->left = p;
8115
8116 r->balance = (t->balance < 0);
8117 p->balance = -(t->balance > 0);
8118 t->balance = 0;
8119
8120 s = p->parent;
8121 p->parent = t;
8122 r->parent = t;
8123 t->parent = s;
8124 if (s)
8125 {
8126 if (s->left == p)
8127 s->left = t;
8128 else
8129 s->right = t;
8130 }
8131 else
8132 constructor_pending_elts = t;
8133 }
8134 break;
8135 }
8136 else
8137 {
8138 /* p->balance == -1; growth of right side balances the node. */
8139 p->balance = 0;
8140 break;
8141 }
8142 }
8143
8144 r = p;
8145 p = p->parent;
8146 }
8147}
8148
8149/* Build AVL tree from a sorted chain. */
8150
8151static void
a1e3b3d9 8152set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8153{
4038c495
GB
8154 unsigned HOST_WIDE_INT ix;
8155 tree index, value;
3e4093b6
RS
8156
8157 if (TREE_CODE (constructor_type) != RECORD_TYPE
8158 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8159 return;
8160
4038c495 8161 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8162 add_pending_init (input_location, index, value, NULL_TREE, true,
8163 braced_init_obstack);
9771b263 8164 constructor_elements = NULL;
3e4093b6
RS
8165 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8166 {
8167 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8168 /* Skip any nameless bit fields at the beginning. */
8169 while (constructor_unfilled_fields != 0
8170 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8171 && DECL_NAME (constructor_unfilled_fields) == 0)
8172 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8173
de520661 8174 }
3e4093b6 8175 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8176 {
3e4093b6
RS
8177 if (TYPE_DOMAIN (constructor_type))
8178 constructor_unfilled_index
8179 = convert (bitsizetype,
8180 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8181 else
8182 constructor_unfilled_index = bitsize_zero_node;
de520661 8183 }
3e4093b6 8184 constructor_incremental = 0;
de520661 8185}
400fbf9f 8186
3e4093b6 8187/* Build AVL tree from a string constant. */
de520661 8188
3e4093b6 8189static void
a1e3b3d9
LB
8190set_nonincremental_init_from_string (tree str,
8191 struct obstack * braced_init_obstack)
de520661 8192{
3e4093b6
RS
8193 tree value, purpose, type;
8194 HOST_WIDE_INT val[2];
8195 const char *p, *end;
8196 int byte, wchar_bytes, charwidth, bitpos;
de520661 8197
366de0ce 8198 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8199
c466b2cd 8200 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8201 charwidth = TYPE_PRECISION (char_type_node);
8202 type = TREE_TYPE (constructor_type);
8203 p = TREE_STRING_POINTER (str);
8204 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8205
3e4093b6 8206 for (purpose = bitsize_zero_node;
8824edff
JJ
8207 p < end
8208 && !(constructor_max_index
8209 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8210 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8211 {
3e4093b6 8212 if (wchar_bytes == 1)
ffc5c6a9 8213 {
807e902e
KZ
8214 val[0] = (unsigned char) *p++;
8215 val[1] = 0;
ffc5c6a9
RH
8216 }
8217 else
3e4093b6 8218 {
3e4093b6 8219 val[1] = 0;
807e902e 8220 val[0] = 0;
3e4093b6
RS
8221 for (byte = 0; byte < wchar_bytes; byte++)
8222 {
8223 if (BYTES_BIG_ENDIAN)
8224 bitpos = (wchar_bytes - byte - 1) * charwidth;
8225 else
8226 bitpos = byte * charwidth;
807e902e 8227 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8228 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8229 << (bitpos % HOST_BITS_PER_WIDE_INT);
8230 }
8231 }
584ef5fe 8232
8df83eae 8233 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8234 {
8235 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8236 if (bitpos < HOST_BITS_PER_WIDE_INT)
8237 {
807e902e 8238 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8239 {
807e902e
KZ
8240 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8241 val[1] = -1;
3e4093b6
RS
8242 }
8243 }
8244 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8245 {
807e902e
KZ
8246 if (val[0] < 0)
8247 val[1] = -1;
3e4093b6 8248 }
807e902e 8249 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8250 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
807e902e 8251 val[1] |= ((HOST_WIDE_INT) -1)
3e4093b6
RS
8252 << (bitpos - HOST_BITS_PER_WIDE_INT);
8253 }
ffc5c6a9 8254
807e902e
KZ
8255 value = wide_int_to_tree (type,
8256 wide_int::from_array (val, 2,
8257 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8258 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8259 braced_init_obstack);
9dfcc8db
BH
8260 }
8261
3e4093b6
RS
8262 constructor_incremental = 0;
8263}
de520661 8264
3e4093b6
RS
8265/* Return value of FIELD in pending initializer or zero if the field was
8266 not initialized yet. */
8267
8268static tree
a1e3b3d9 8269find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8270{
8271 struct init_node *p;
8272
8273 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8274 {
3e4093b6
RS
8275 if (constructor_incremental
8276 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8277 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8278
8279 p = constructor_pending_elts;
8280 while (p)
19d76e60 8281 {
3e4093b6
RS
8282 if (tree_int_cst_lt (field, p->purpose))
8283 p = p->left;
8284 else if (tree_int_cst_lt (p->purpose, field))
8285 p = p->right;
8286 else
8287 return p->value;
19d76e60 8288 }
19d76e60 8289 }
3e4093b6 8290 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8291 {
3e4093b6 8292 tree bitpos = bit_position (field);
de520661 8293
3e4093b6
RS
8294 if (constructor_incremental
8295 && (!constructor_unfilled_fields
8296 || tree_int_cst_lt (bitpos,
8297 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8298 set_nonincremental_init (braced_init_obstack);
de520661 8299
3e4093b6
RS
8300 p = constructor_pending_elts;
8301 while (p)
8302 {
8303 if (field == p->purpose)
8304 return p->value;
8305 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8306 p = p->left;
8307 else
8308 p = p->right;
8309 }
8310 }
8311 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8312 {
9771b263
DN
8313 if (!vec_safe_is_empty (constructor_elements)
8314 && (constructor_elements->last ().index == field))
8315 return constructor_elements->last ().value;
de520661 8316 }
3e4093b6 8317 return 0;
de520661
RS
8318}
8319
3e4093b6
RS
8320/* "Output" the next constructor element.
8321 At top level, really output it to assembler code now.
8322 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8323 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8324 TYPE is the data type that the containing data type wants here.
8325 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8326 If VALUE is a string constant, STRICT_STRING is true if it is
8327 unparenthesized or we should not warn here for it being parenthesized.
8328 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8329
3e4093b6
RS
8330 PENDING if non-nil means output pending elements that belong
8331 right after this element. (PENDING is normally 1;
b295aee2
JJ
8332 it is 0 while outputting pending elements, to avoid recursion.)
8333
8334 IMPLICIT is true if value comes from pop_init_level (1),
8335 the new initializer has been merged with the existing one
8336 and thus no warnings should be emitted about overriding an
8337 existing initializer. */
8b6a5902 8338
3e4093b6 8339static void
34cf811f
MP
8340output_init_element (location_t loc, tree value, tree origtype,
8341 bool strict_string, tree type, tree field, int pending,
8342 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8343{
8ce94e44 8344 tree semantic_type = NULL_TREE;
928c19bb
JM
8345 bool maybe_const = true;
8346 bool npc;
4038c495 8347
0a880880 8348 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8349 {
3e4093b6
RS
8350 constructor_erroneous = 1;
8351 return;
8b6a5902 8352 }
46bdb9cf
JM
8353 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8354 && (TREE_CODE (value) == STRING_CST
8355 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8356 && !(TREE_CODE (value) == STRING_CST
8357 && TREE_CODE (type) == ARRAY_TYPE
8358 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8359 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8360 TYPE_MAIN_VARIANT (type)))
c2255bc4 8361 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8362
3e4093b6 8363 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8364 && require_constant_value && pending)
8b6a5902 8365 {
3e4093b6
RS
8366 /* As an extension, allow initializing objects with static storage
8367 duration with compound literals (which are then treated just as
8368 the brace enclosed list they contain). */
4435bb92
MP
8369 if (flag_isoc99)
8370 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8371 "constant");
3e4093b6
RS
8372 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8373 value = DECL_INITIAL (decl);
8b6a5902
JJ
8374 }
8375
928c19bb 8376 npc = null_pointer_constant_p (value);
8ce94e44
JM
8377 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8378 {
8379 semantic_type = TREE_TYPE (value);
8380 value = TREE_OPERAND (value, 0);
8381 }
928c19bb
JM
8382 value = c_fully_fold (value, require_constant_value, &maybe_const);
8383
3e4093b6
RS
8384 if (value == error_mark_node)
8385 constructor_erroneous = 1;
8386 else if (!TREE_CONSTANT (value))
8387 constructor_constant = 0;
116df786 8388 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8389 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8390 || TREE_CODE (constructor_type) == UNION_TYPE)
8391 && DECL_C_BIT_FIELD (field)
8392 && TREE_CODE (value) != INTEGER_CST))
8393 constructor_simple = 0;
928c19bb
JM
8394 if (!maybe_const)
8395 constructor_nonconst = 1;
3e4093b6 8396
116df786 8397 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8398 {
116df786
RH
8399 if (require_constant_value)
8400 {
ea58ef42 8401 error_init (loc, "initializer element is not constant");
116df786
RH
8402 value = error_mark_node;
8403 }
8404 else if (require_constant_elements)
8337d1db 8405 pedwarn (loc, OPT_Wpedantic,
509c9d60 8406 "initializer element is not computable at load time");
8b6a5902 8407 }
928c19bb
JM
8408 else if (!maybe_const
8409 && (require_constant_value || require_constant_elements))
8337d1db 8410 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8411 "initializer element is not a constant expression");
3e4093b6 8412
81f40b79
ILT
8413 /* Issue -Wc++-compat warnings about initializing a bitfield with
8414 enum type. */
8415 if (warn_cxx_compat
8416 && field != NULL_TREE
8417 && TREE_CODE (field) == FIELD_DECL
8418 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8419 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8420 != TYPE_MAIN_VARIANT (type))
8421 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8422 {
8423 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8424 if (checktype != error_mark_node
8425 && (TYPE_MAIN_VARIANT (checktype)
8426 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8427 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8428 "enum conversion in initialization is invalid in C++");
8429 }
8430
3e4093b6
RS
8431 /* If this field is empty (and not at the end of structure),
8432 don't do anything other than checking the initializer. */
8433 if (field
8434 && (TREE_TYPE (field) == error_mark_node
8435 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8436 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8437 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8438 || DECL_CHAIN (field)))))
3e4093b6
RS
8439 return;
8440
8ce94e44
JM
8441 if (semantic_type)
8442 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8443 value = digest_init (loc, type, value, origtype, npc, strict_string,
8444 require_constant_value);
3e4093b6 8445 if (value == error_mark_node)
8b6a5902 8446 {
3e4093b6
RS
8447 constructor_erroneous = 1;
8448 return;
8b6a5902 8449 }
928c19bb
JM
8450 if (require_constant_value || require_constant_elements)
8451 constant_expression_warning (value);
8b6a5902 8452
3e4093b6
RS
8453 /* If this element doesn't come next in sequence,
8454 put it on constructor_pending_elts. */
8455 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8456 && (!constructor_incremental
8457 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8458 {
3e4093b6
RS
8459 if (constructor_incremental
8460 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8461 set_nonincremental_init (braced_init_obstack);
3e4093b6 8462
96b40f8d 8463 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8464 braced_init_obstack);
3e4093b6 8465 return;
8b6a5902 8466 }
3e4093b6
RS
8467 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8468 && (!constructor_incremental
8469 || field != constructor_unfilled_fields))
8b6a5902 8470 {
3e4093b6
RS
8471 /* We do this for records but not for unions. In a union,
8472 no matter which field is specified, it can be initialized
8473 right away since it starts at the beginning of the union. */
8474 if (constructor_incremental)
8475 {
8476 if (!constructor_unfilled_fields)
a1e3b3d9 8477 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8478 else
8479 {
8480 tree bitpos, unfillpos;
8481
8482 bitpos = bit_position (field);
8483 unfillpos = bit_position (constructor_unfilled_fields);
8484
8485 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8486 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8487 }
8488 }
8489
96b40f8d 8490 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8491 braced_init_obstack);
3e4093b6 8492 return;
8b6a5902 8493 }
3e4093b6 8494 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8495 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8496 {
b295aee2
JJ
8497 if (!implicit)
8498 {
9771b263 8499 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
96b40f8d 8500 warning_init (loc, 0,
b295aee2
JJ
8501 "initialized field with side-effects overwritten");
8502 else if (warn_override_init)
96b40f8d
MP
8503 warning_init (loc, OPT_Woverride_init,
8504 "initialized field overwritten");
b295aee2 8505 }
8b6a5902 8506
3e4093b6 8507 /* We can have just one union field set. */
9771b263 8508 constructor_elements = NULL;
3e4093b6 8509 }
8b6a5902 8510
3e4093b6
RS
8511 /* Otherwise, output this element either to
8512 constructor_elements or to the assembler file. */
8b6a5902 8513
f32682ca 8514 constructor_elt celt = {field, value};
9771b263 8515 vec_safe_push (constructor_elements, celt);
8b6a5902 8516
3e4093b6
RS
8517 /* Advance the variable that indicates sequential elements output. */
8518 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8519 constructor_unfilled_index
db3927fb
AH
8520 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8521 bitsize_one_node);
3e4093b6
RS
8522 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8523 {
8524 constructor_unfilled_fields
910ad8de 8525 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8526
3e4093b6
RS
8527 /* Skip any nameless bit fields. */
8528 while (constructor_unfilled_fields != 0
8529 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8530 && DECL_NAME (constructor_unfilled_fields) == 0)
8531 constructor_unfilled_fields =
910ad8de 8532 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8533 }
8534 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8535 constructor_unfilled_fields = 0;
de520661 8536
3e4093b6
RS
8537 /* Now output any pending elements which have become next. */
8538 if (pending)
a1e3b3d9 8539 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8540}
8b6a5902 8541
3e4093b6
RS
8542/* Output any pending elements which have become next.
8543 As we output elements, constructor_unfilled_{fields,index}
8544 advances, which may cause other elements to become next;
8545 if so, they too are output.
8b6a5902 8546
3e4093b6
RS
8547 If ALL is 0, we return when there are
8548 no more pending elements to output now.
665f2503 8549
3e4093b6
RS
8550 If ALL is 1, we output space as necessary so that
8551 we can output all the pending elements. */
3e4093b6 8552static void
a1e3b3d9 8553output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8554{
8555 struct init_node *elt = constructor_pending_elts;
8556 tree next;
de520661 8557
3e4093b6
RS
8558 retry:
8559
ba228239 8560 /* Look through the whole pending tree.
3e4093b6
RS
8561 If we find an element that should be output now,
8562 output it. Otherwise, set NEXT to the element
8563 that comes first among those still pending. */
8564
8565 next = 0;
8566 while (elt)
8567 {
8568 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8569 {
3e4093b6
RS
8570 if (tree_int_cst_equal (elt->purpose,
8571 constructor_unfilled_index))
34cf811f
MP
8572 output_init_element (input_location, elt->value, elt->origtype,
8573 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8574 constructor_unfilled_index, 0, false,
8575 braced_init_obstack);
3e4093b6
RS
8576 else if (tree_int_cst_lt (constructor_unfilled_index,
8577 elt->purpose))
8b6a5902 8578 {
3e4093b6
RS
8579 /* Advance to the next smaller node. */
8580 if (elt->left)
8581 elt = elt->left;
8582 else
8583 {
8584 /* We have reached the smallest node bigger than the
8585 current unfilled index. Fill the space first. */
8586 next = elt->purpose;
8587 break;
8588 }
8b6a5902 8589 }
ce662d4c
JJ
8590 else
8591 {
3e4093b6
RS
8592 /* Advance to the next bigger node. */
8593 if (elt->right)
8594 elt = elt->right;
8595 else
ce662d4c 8596 {
3e4093b6
RS
8597 /* We have reached the biggest node in a subtree. Find
8598 the parent of it, which is the next bigger node. */
8599 while (elt->parent && elt->parent->right == elt)
8600 elt = elt->parent;
8601 elt = elt->parent;
8602 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8603 elt->purpose))
8604 {
8605 next = elt->purpose;
8606 break;
8607 }
ce662d4c
JJ
8608 }
8609 }
8b6a5902 8610 }
3e4093b6
RS
8611 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8612 || TREE_CODE (constructor_type) == UNION_TYPE)
8613 {
8614 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8615
3e4093b6
RS
8616 /* If the current record is complete we are done. */
8617 if (constructor_unfilled_fields == 0)
8618 break;
de520661 8619
3e4093b6
RS
8620 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8621 elt_bitpos = bit_position (elt->purpose);
8622 /* We can't compare fields here because there might be empty
8623 fields in between. */
8624 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8625 {
8626 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8627 output_init_element (input_location, elt->value, elt->origtype,
8628 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8629 elt->purpose, 0, false,
8630 braced_init_obstack);
3e4093b6
RS
8631 }
8632 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8633 {
8634 /* Advance to the next smaller node. */
8635 if (elt->left)
8636 elt = elt->left;
8637 else
8638 {
8639 /* We have reached the smallest node bigger than the
8640 current unfilled field. Fill the space first. */
8641 next = elt->purpose;
8642 break;
8643 }
8644 }
8645 else
8646 {
8647 /* Advance to the next bigger node. */
8648 if (elt->right)
8649 elt = elt->right;
8650 else
8651 {
8652 /* We have reached the biggest node in a subtree. Find
8653 the parent of it, which is the next bigger node. */
8654 while (elt->parent && elt->parent->right == elt)
8655 elt = elt->parent;
8656 elt = elt->parent;
8657 if (elt
8658 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8659 bit_position (elt->purpose))))
8660 {
8661 next = elt->purpose;
8662 break;
8663 }
8664 }
8665 }
8666 }
8667 }
de520661 8668
3e4093b6
RS
8669 /* Ordinarily return, but not if we want to output all
8670 and there are elements left. */
3f75a254 8671 if (!(all && next != 0))
e5cfb88f
RK
8672 return;
8673
3e4093b6
RS
8674 /* If it's not incremental, just skip over the gap, so that after
8675 jumping to retry we will output the next successive element. */
8676 if (TREE_CODE (constructor_type) == RECORD_TYPE
8677 || TREE_CODE (constructor_type) == UNION_TYPE)
8678 constructor_unfilled_fields = next;
8679 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8680 constructor_unfilled_index = next;
de520661 8681
3e4093b6
RS
8682 /* ELT now points to the node in the pending tree with the next
8683 initializer to output. */
8684 goto retry;
de520661
RS
8685}
8686\f
3e4093b6
RS
8687/* Add one non-braced element to the current constructor level.
8688 This adjusts the current position within the constructor's type.
8689 This may also start or terminate implicit levels
8690 to handle a partly-braced initializer.
e5e809f4 8691
3e4093b6 8692 Once this has found the correct level for the new element,
b295aee2
JJ
8693 it calls output_init_element.
8694
8695 IMPLICIT is true if value comes from pop_init_level (1),
8696 the new initializer has been merged with the existing one
8697 and thus no warnings should be emitted about overriding an
8698 existing initializer. */
3e4093b6
RS
8699
8700void
34cf811f 8701process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8702 struct obstack * braced_init_obstack)
e5e809f4 8703{
916c5919
JM
8704 tree orig_value = value.value;
8705 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8706 bool strict_string = value.original_code == STRING_CST;
340baef7 8707 bool was_designated = designator_depth != 0;
e5e809f4 8708
3e4093b6 8709 designator_depth = 0;
b06df647 8710 designator_erroneous = 0;
e5e809f4 8711
9bac5cbb
G
8712 if (!implicit && value.value && !integer_zerop (value.value))
8713 constructor_zeroinit = 0;
8714
3e4093b6
RS
8715 /* Handle superfluous braces around string cst as in
8716 char x[] = {"foo"}; */
8717 if (string_flag
8718 && constructor_type
340baef7 8719 && !was_designated
3e4093b6 8720 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8721 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8722 && integer_zerop (constructor_unfilled_index))
e5e809f4 8723 {
916c5919 8724 if (constructor_stack->replacement_value.value)
ea58ef42 8725 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8726 constructor_stack->replacement_value = value;
8727 return;
e5e809f4 8728 }
8b6a5902 8729
916c5919 8730 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8731 {
ea58ef42 8732 error_init (loc, "excess elements in struct initializer");
3e4093b6 8733 return;
e5e809f4
JL
8734 }
8735
3e4093b6
RS
8736 /* Ignore elements of a brace group if it is entirely superfluous
8737 and has already been diagnosed. */
8738 if (constructor_type == 0)
8739 return;
e5e809f4 8740
976d5a22
TT
8741 if (!implicit && warn_designated_init && !was_designated
8742 && TREE_CODE (constructor_type) == RECORD_TYPE
8743 && lookup_attribute ("designated_init",
8744 TYPE_ATTRIBUTES (constructor_type)))
8745 warning_init (loc,
8746 OPT_Wdesignated_init,
8747 "positional initialization of field "
8748 "in %<struct%> declared with %<designated_init%> attribute");
8749
3e4093b6
RS
8750 /* If we've exhausted any levels that didn't have braces,
8751 pop them now. */
8752 while (constructor_stack->implicit)
8753 {
8754 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8755 || TREE_CODE (constructor_type) == UNION_TYPE)
8756 && constructor_fields == 0)
ea58ef42
MP
8757 process_init_element (loc,
8758 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8759 true, braced_init_obstack);
53650abe
AP
8760 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8761 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8762 && constructor_max_index
8763 && tree_int_cst_lt (constructor_max_index,
8764 constructor_index))
ea58ef42
MP
8765 process_init_element (loc,
8766 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8767 true, braced_init_obstack);
3e4093b6
RS
8768 else
8769 break;
8770 }
e5e809f4 8771
3e4093b6
RS
8772 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8773 if (constructor_range_stack)
e5e809f4 8774 {
3e4093b6
RS
8775 /* If value is a compound literal and we'll be just using its
8776 content, don't put it into a SAVE_EXPR. */
916c5919 8777 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
8778 || !require_constant_value
8779 || flag_isoc99)
8ce94e44
JM
8780 {
8781 tree semantic_type = NULL_TREE;
8782 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8783 {
8784 semantic_type = TREE_TYPE (value.value);
8785 value.value = TREE_OPERAND (value.value, 0);
8786 }
8787 value.value = c_save_expr (value.value);
8788 if (semantic_type)
8789 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8790 value.value);
8791 }
3e4093b6 8792 }
e5e809f4 8793
3e4093b6
RS
8794 while (1)
8795 {
8796 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8797 {
3e4093b6
RS
8798 tree fieldtype;
8799 enum tree_code fieldcode;
e5e809f4 8800
3e4093b6
RS
8801 if (constructor_fields == 0)
8802 {
ea58ef42 8803 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
8804 break;
8805 }
e5e809f4 8806
3e4093b6
RS
8807 fieldtype = TREE_TYPE (constructor_fields);
8808 if (fieldtype != error_mark_node)
8809 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8810 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8811
3e4093b6
RS
8812 /* Error for non-static initialization of a flexible array member. */
8813 if (fieldcode == ARRAY_TYPE
8814 && !require_constant_value
8815 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8816 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 8817 {
ea58ef42
MP
8818 error_init (loc, "non-static initialization of a flexible "
8819 "array member");
3e4093b6
RS
8820 break;
8821 }
e5e809f4 8822
2cc901dc
MP
8823 /* Error for initialization of a flexible array member with
8824 a string constant if the structure is in an array. E.g.:
8825 struct S { int x; char y[]; };
8826 struct S s[] = { { 1, "foo" } };
8827 is invalid. */
8828 if (string_flag
8829 && fieldcode == ARRAY_TYPE
8830 && constructor_depth > 1
8831 && TYPE_SIZE (fieldtype) == NULL_TREE
8832 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8833 {
8834 bool in_array_p = false;
8835 for (struct constructor_stack *p = constructor_stack;
8836 p && p->type; p = p->next)
8837 if (TREE_CODE (p->type) == ARRAY_TYPE)
8838 {
8839 in_array_p = true;
8840 break;
8841 }
8842 if (in_array_p)
8843 {
8844 error_init (loc, "initialization of flexible array "
8845 "member in a nested context");
8846 break;
8847 }
8848 }
8849
3e4093b6 8850 /* Accept a string constant to initialize a subarray. */
916c5919 8851 if (value.value != 0
3e4093b6 8852 && fieldcode == ARRAY_TYPE
197463ae 8853 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8854 && string_flag)
916c5919 8855 value.value = orig_value;
3e4093b6
RS
8856 /* Otherwise, if we have come to a subaggregate,
8857 and we don't have an element of its type, push into it. */
0953878d 8858 else if (value.value != 0
916c5919
JM
8859 && value.value != error_mark_node
8860 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8861 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8862 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8863 {
ea58ef42 8864 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8865 continue;
8866 }
e5e809f4 8867
916c5919 8868 if (value.value)
3e4093b6
RS
8869 {
8870 push_member_name (constructor_fields);
34cf811f 8871 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8872 strict_string, fieldtype,
a1e3b3d9
LB
8873 constructor_fields, 1, implicit,
8874 braced_init_obstack);
3e4093b6 8875 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8876 }
8877 else
3e4093b6
RS
8878 /* Do the bookkeeping for an element that was
8879 directly output as a constructor. */
e5e809f4 8880 {
3e4093b6
RS
8881 /* For a record, keep track of end position of last field. */
8882 if (DECL_SIZE (constructor_fields))
c22cacf3 8883 constructor_bit_index
db3927fb
AH
8884 = size_binop_loc (input_location, PLUS_EXPR,
8885 bit_position (constructor_fields),
8886 DECL_SIZE (constructor_fields));
3e4093b6
RS
8887
8888 /* If the current field was the first one not yet written out,
8889 it isn't now, so update. */
8890 if (constructor_unfilled_fields == constructor_fields)
8891 {
910ad8de 8892 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8893 /* Skip any nameless bit fields. */
8894 while (constructor_unfilled_fields != 0
8895 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8896 && DECL_NAME (constructor_unfilled_fields) == 0)
8897 constructor_unfilled_fields =
910ad8de 8898 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8899 }
e5e809f4 8900 }
3e4093b6 8901
910ad8de 8902 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8903 /* Skip any nameless bit fields at the beginning. */
8904 while (constructor_fields != 0
8905 && DECL_C_BIT_FIELD (constructor_fields)
8906 && DECL_NAME (constructor_fields) == 0)
910ad8de 8907 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8908 }
3e4093b6 8909 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8910 {
3e4093b6
RS
8911 tree fieldtype;
8912 enum tree_code fieldcode;
e5e809f4 8913
3e4093b6
RS
8914 if (constructor_fields == 0)
8915 {
d033409e 8916 pedwarn_init (loc, 0,
509c9d60 8917 "excess elements in union initializer");
3e4093b6
RS
8918 break;
8919 }
e5e809f4 8920
3e4093b6
RS
8921 fieldtype = TREE_TYPE (constructor_fields);
8922 if (fieldtype != error_mark_node)
8923 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8924 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8925
3e4093b6
RS
8926 /* Warn that traditional C rejects initialization of unions.
8927 We skip the warning if the value is zero. This is done
8928 under the assumption that the zero initializer in user
8929 code appears conditioned on e.g. __STDC__ to avoid
8930 "missing initializer" warnings and relies on default
8931 initialization to zero in the traditional C case.
8932 We also skip the warning if the initializer is designated,
8933 again on the assumption that this must be conditional on
8934 __STDC__ anyway (and we've already complained about the
8935 member-designator already). */
8400e75e 8936 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8937 && !(value.value && (integer_zerop (value.value)
8938 || real_zerop (value.value))))
3176a0c2
DD
8939 warning (OPT_Wtraditional, "traditional C rejects initialization "
8940 "of unions");
e5e809f4 8941
3e4093b6 8942 /* Accept a string constant to initialize a subarray. */
916c5919 8943 if (value.value != 0
3e4093b6 8944 && fieldcode == ARRAY_TYPE
197463ae 8945 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8946 && string_flag)
916c5919 8947 value.value = orig_value;
3e4093b6
RS
8948 /* Otherwise, if we have come to a subaggregate,
8949 and we don't have an element of its type, push into it. */
0953878d 8950 else if (value.value != 0
916c5919
JM
8951 && value.value != error_mark_node
8952 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8953 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8954 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8955 {
ea58ef42 8956 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8957 continue;
8958 }
e5e809f4 8959
916c5919 8960 if (value.value)
3e4093b6
RS
8961 {
8962 push_member_name (constructor_fields);
34cf811f 8963 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8964 strict_string, fieldtype,
a1e3b3d9
LB
8965 constructor_fields, 1, implicit,
8966 braced_init_obstack);
3e4093b6 8967 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8968 }
8969 else
3e4093b6
RS
8970 /* Do the bookkeeping for an element that was
8971 directly output as a constructor. */
e5e809f4 8972 {
3e4093b6 8973 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8974 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8975 }
e5e809f4 8976
3e4093b6
RS
8977 constructor_fields = 0;
8978 }
8979 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8980 {
8981 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8982 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8983
3e4093b6 8984 /* Accept a string constant to initialize a subarray. */
916c5919 8985 if (value.value != 0
3e4093b6 8986 && eltcode == ARRAY_TYPE
197463ae 8987 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8988 && string_flag)
916c5919 8989 value.value = orig_value;
3e4093b6
RS
8990 /* Otherwise, if we have come to a subaggregate,
8991 and we don't have an element of its type, push into it. */
0953878d 8992 else if (value.value != 0
916c5919
JM
8993 && value.value != error_mark_node
8994 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 8995 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 8996 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 8997 {
ea58ef42 8998 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8999 continue;
9000 }
8b6a5902 9001
3e4093b6
RS
9002 if (constructor_max_index != 0
9003 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9004 || integer_all_onesp (constructor_max_index)))
9005 {
d033409e 9006 pedwarn_init (loc, 0,
509c9d60 9007 "excess elements in array initializer");
3e4093b6
RS
9008 break;
9009 }
8b6a5902 9010
3e4093b6 9011 /* Now output the actual element. */
916c5919 9012 if (value.value)
3e4093b6 9013 {
ae7e9ddd 9014 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9015 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9016 strict_string, elttype,
a1e3b3d9
LB
9017 constructor_index, 1, implicit,
9018 braced_init_obstack);
3e4093b6
RS
9019 RESTORE_SPELLING_DEPTH (constructor_depth);
9020 }
2f6e4e97 9021
3e4093b6 9022 constructor_index
db3927fb
AH
9023 = size_binop_loc (input_location, PLUS_EXPR,
9024 constructor_index, bitsize_one_node);
8b6a5902 9025
916c5919 9026 if (!value.value)
3e4093b6
RS
9027 /* If we are doing the bookkeeping for an element that was
9028 directly output as a constructor, we must update
9029 constructor_unfilled_index. */
9030 constructor_unfilled_index = constructor_index;
9031 }
9032 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9033 {
9034 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9035
c22cacf3
MS
9036 /* Do a basic check of initializer size. Note that vectors
9037 always have a fixed size derived from their type. */
3e4093b6
RS
9038 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9039 {
d033409e 9040 pedwarn_init (loc, 0,
509c9d60 9041 "excess elements in vector initializer");
3e4093b6
RS
9042 break;
9043 }
8b6a5902 9044
3e4093b6 9045 /* Now output the actual element. */
916c5919 9046 if (value.value)
53650abe
AP
9047 {
9048 if (TREE_CODE (value.value) == VECTOR_CST)
9049 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9050 output_init_element (loc, value.value, value.original_type,
53650abe 9051 strict_string, elttype,
a1e3b3d9
LB
9052 constructor_index, 1, implicit,
9053 braced_init_obstack);
53650abe 9054 }
8b6a5902 9055
3e4093b6 9056 constructor_index
db3927fb
AH
9057 = size_binop_loc (input_location,
9058 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9059
916c5919 9060 if (!value.value)
3e4093b6
RS
9061 /* If we are doing the bookkeeping for an element that was
9062 directly output as a constructor, we must update
9063 constructor_unfilled_index. */
9064 constructor_unfilled_index = constructor_index;
9065 }
8b6a5902 9066
3e4093b6
RS
9067 /* Handle the sole element allowed in a braced initializer
9068 for a scalar variable. */
b4519d39
SB
9069 else if (constructor_type != error_mark_node
9070 && constructor_fields == 0)
8b6a5902 9071 {
d033409e 9072 pedwarn_init (loc, 0,
509c9d60 9073 "excess elements in scalar initializer");
3e4093b6 9074 break;
8b6a5902
JJ
9075 }
9076 else
9077 {
916c5919 9078 if (value.value)
34cf811f 9079 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9080 strict_string, constructor_type,
a1e3b3d9
LB
9081 NULL_TREE, 1, implicit,
9082 braced_init_obstack);
3e4093b6 9083 constructor_fields = 0;
8b6a5902
JJ
9084 }
9085
3e4093b6
RS
9086 /* Handle range initializers either at this level or anywhere higher
9087 in the designator stack. */
9088 if (constructor_range_stack)
8b6a5902 9089 {
3e4093b6
RS
9090 struct constructor_range_stack *p, *range_stack;
9091 int finish = 0;
9092
9093 range_stack = constructor_range_stack;
9094 constructor_range_stack = 0;
9095 while (constructor_stack != range_stack->stack)
8b6a5902 9096 {
366de0ce 9097 gcc_assert (constructor_stack->implicit);
34cf811f 9098 process_init_element (loc,
ea58ef42
MP
9099 pop_init_level (loc, 1,
9100 braced_init_obstack),
a1e3b3d9 9101 true, braced_init_obstack);
8b6a5902 9102 }
3e4093b6
RS
9103 for (p = range_stack;
9104 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9105 p = p->prev)
8b6a5902 9106 {
366de0ce 9107 gcc_assert (constructor_stack->implicit);
34cf811f 9108 process_init_element (loc,
ea58ef42
MP
9109 pop_init_level (loc, 1,
9110 braced_init_obstack),
a1e3b3d9 9111 true, braced_init_obstack);
8b6a5902 9112 }
3e4093b6 9113
db3927fb
AH
9114 p->index = size_binop_loc (input_location,
9115 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9116 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9117 finish = 1;
9118
9119 while (1)
9120 {
9121 constructor_index = p->index;
9122 constructor_fields = p->fields;
9123 if (finish && p->range_end && p->index == p->range_start)
9124 {
9125 finish = 0;
9126 p->prev = 0;
9127 }
9128 p = p->next;
9129 if (!p)
9130 break;
ea58ef42 9131 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9132 p->stack = constructor_stack;
9133 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9134 p->index = p->range_start;
9135 }
9136
9137 if (!finish)
9138 constructor_range_stack = range_stack;
9139 continue;
8b6a5902
JJ
9140 }
9141
3e4093b6 9142 break;
8b6a5902
JJ
9143 }
9144
3e4093b6
RS
9145 constructor_range_stack = 0;
9146}
9147\f
9f0e2d86
ZW
9148/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9149 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9150 an ASM_EXPR node). */
3e4093b6 9151tree
9f0e2d86 9152build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9153{
6de9cd9a
DN
9154 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9155 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9156 return add_stmt (args);
8b6a5902
JJ
9157}
9158
9f0e2d86
ZW
9159/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9160 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9161 SIMPLE indicates whether there was anything at all after the
9162 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9163 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9164tree
c2255bc4 9165build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9166 tree clobbers, tree labels, bool simple)
e5e809f4 9167{
3e4093b6 9168 tree tail;
9f0e2d86 9169 tree args;
6de9cd9a
DN
9170 int i;
9171 const char *constraint;
74f0c611 9172 const char **oconstraints;
6de9cd9a 9173 bool allows_mem, allows_reg, is_inout;
74f0c611 9174 int ninputs, noutputs;
6de9cd9a
DN
9175
9176 ninputs = list_length (inputs);
9177 noutputs = list_length (outputs);
74f0c611
RH
9178 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9179
1c384bf1 9180 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9181
6de9cd9a
DN
9182 /* Remove output conversions that change the type but not the mode. */
9183 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9184 {
3e4093b6 9185 tree output = TREE_VALUE (tail);
74f0c611 9186
eadd3d0d
JJ
9187 output = c_fully_fold (output, false, NULL);
9188
74f0c611
RH
9189 /* ??? Really, this should not be here. Users should be using a
9190 proper lvalue, dammit. But there's a long history of using casts
9191 in the output operands. In cases like longlong.h, this becomes a
9192 primitive form of typechecking -- if the cast can be removed, then
9193 the output operand had a type of the proper width; otherwise we'll
9194 get an error. Gross, but ... */
3e4093b6 9195 STRIP_NOPS (output);
74f0c611 9196
7bd11157 9197 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9198 output = error_mark_node;
8b6a5902 9199
5544530a
PB
9200 if (output != error_mark_node
9201 && (TREE_READONLY (output)
9202 || TYPE_READONLY (TREE_TYPE (output))
9203 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9204 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9205 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9206 readonly_error (loc, output, lv_asm);
5544530a 9207
6de9cd9a 9208 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9209 oconstraints[i] = constraint;
9210
9211 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9212 &allows_mem, &allows_reg, &is_inout))
9213 {
9214 /* If the operand is going to end up in memory,
9215 mark it addressable. */
9216 if (!allows_reg && !c_mark_addressable (output))
9217 output = error_mark_node;
bae5cddf
JJ
9218 if (!(!allows_reg && allows_mem)
9219 && output != error_mark_node
9220 && VOID_TYPE_P (TREE_TYPE (output)))
9221 {
9222 error_at (loc, "invalid use of void expression");
9223 output = error_mark_node;
9224 }
74f0c611
RH
9225 }
9226 else
c22cacf3 9227 output = error_mark_node;
3e4093b6 9228
74f0c611 9229 TREE_VALUE (tail) = output;
8b6a5902 9230 }
3e4093b6 9231
74f0c611
RH
9232 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9233 {
9234 tree input;
9235
9236 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9237 input = TREE_VALUE (tail);
9238
74f0c611
RH
9239 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9240 oconstraints, &allows_mem, &allows_reg))
9241 {
9242 /* If the operand is going to end up in memory,
9243 mark it addressable. */
b4c33883
AP
9244 if (!allows_reg && allows_mem)
9245 {
eadd3d0d
JJ
9246 input = c_fully_fold (input, false, NULL);
9247
b4c33883
AP
9248 /* Strip the nops as we allow this case. FIXME, this really
9249 should be rejected or made deprecated. */
9250 STRIP_NOPS (input);
9251 if (!c_mark_addressable (input))
9252 input = error_mark_node;
bae5cddf 9253 }
eadd3d0d 9254 else
bae5cddf 9255 {
eadd3d0d
JJ
9256 struct c_expr expr;
9257 memset (&expr, 0, sizeof (expr));
9258 expr.value = input;
267bac10 9259 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9260 input = c_fully_fold (expr.value, false, NULL);
9261
9262 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9263 {
9264 error_at (loc, "invalid use of void expression");
9265 input = error_mark_node;
9266 }
bae5cddf 9267 }
74f0c611
RH
9268 }
9269 else
9270 input = error_mark_node;
9271
9272 TREE_VALUE (tail) = input;
9273 }
3e4093b6 9274
1c384bf1
RH
9275 /* ASMs with labels cannot have outputs. This should have been
9276 enforced by the parser. */
9277 gcc_assert (outputs == NULL || labels == NULL);
9278
9279 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9280
5544530a
PB
9281 /* asm statements without outputs, including simple ones, are treated
9282 as volatile. */
9283 ASM_INPUT_P (args) = simple;
9284 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9285
9f0e2d86 9286 return args;
e5e809f4 9287}
3e4093b6 9288\f
c2255bc4
AH
9289/* Generate a goto statement to LABEL. LOC is the location of the
9290 GOTO. */
506e2710
RH
9291
9292tree
c2255bc4 9293c_finish_goto_label (location_t loc, tree label)
506e2710 9294{
e1b7793c 9295 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9296 if (!decl)
9297 return NULL_TREE;
506e2710 9298 TREE_USED (decl) = 1;
c2255bc4
AH
9299 {
9300 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9301 SET_EXPR_LOCATION (t, loc);
9302 return add_stmt (t);
9303 }
506e2710
RH
9304}
9305
c2255bc4
AH
9306/* Generate a computed goto statement to EXPR. LOC is the location of
9307 the GOTO. */
506e2710
RH
9308
9309tree
c2255bc4 9310c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9311{
c2255bc4 9312 tree t;
c1771a20 9313 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9314 expr = c_fully_fold (expr, false, NULL);
506e2710 9315 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9316 t = build1 (GOTO_EXPR, void_type_node, expr);
9317 SET_EXPR_LOCATION (t, loc);
9318 return add_stmt (t);
506e2710
RH
9319}
9320
5088b058 9321/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9322 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9323 the location of the return statement, or the location of the expression,
9324 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9325 is the original type of RETVAL. */
de520661 9326
506e2710 9327tree
c2255bc4 9328c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9329{
0c9b182b
JJ
9330 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9331 bool no_warning = false;
928c19bb 9332 bool npc = false;
36536d79 9333 size_t rank = 0;
3e4093b6
RS
9334
9335 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9336 warning_at (loc, 0,
9337 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9338
b72271b9 9339 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9340 {
9341 /* Array notations are allowed in a return statement if it is inside a
9342 built-in array notation reduction function. */
9343 if (!find_rank (loc, retval, retval, false, &rank))
9344 return error_mark_node;
9345 if (rank >= 1)
9346 {
9347 error_at (loc, "array notation expression cannot be used as a "
9348 "return value");
9349 return error_mark_node;
9350 }
9351 }
3af9c5e9 9352 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9353 {
9354 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9355 "allowed");
9356 return error_mark_node;
9357 }
928c19bb
JM
9358 if (retval)
9359 {
8ce94e44 9360 tree semantic_type = NULL_TREE;
928c19bb 9361 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9362 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9363 {
9364 semantic_type = TREE_TYPE (retval);
9365 retval = TREE_OPERAND (retval, 0);
9366 }
928c19bb 9367 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9368 if (semantic_type)
9369 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9370 }
9371
3e4093b6 9372 if (!retval)
de520661 9373 {
3e4093b6
RS
9374 current_function_returns_null = 1;
9375 if ((warn_return_type || flag_isoc99)
9376 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9377 {
35aff4fb
MP
9378 if (flag_isoc99)
9379 pedwarn (loc, 0, "%<return%> with no value, in "
9380 "function returning non-void");
9381 else
9382 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9383 "in function returning non-void");
0c9b182b
JJ
9384 no_warning = true;
9385 }
400fbf9f 9386 }
3e4093b6 9387 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9388 {
3e4093b6 9389 current_function_returns_null = 1;
2397c575 9390 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9391 pedwarn (loc, 0,
509c9d60 9392 "%<return%> with a value, in function returning void");
b8698a0f 9393 else
c1771a20 9394 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9395 "%<return%> with expression, in function returning void");
de520661 9396 }
3e4093b6 9397 else
de520661 9398 {
68fca595
MP
9399 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9400 retval, origtype, ic_return,
c2255bc4 9401 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9402 tree res = DECL_RESULT (current_function_decl);
9403 tree inner;
9feb29df 9404 bool save;
3e4093b6
RS
9405
9406 current_function_returns_value = 1;
9407 if (t == error_mark_node)
506e2710 9408 return NULL_TREE;
3e4093b6 9409
9feb29df
JJ
9410 save = in_late_binary_op;
9411 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9412 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9413 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9414 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9415 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9416 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9417 in_late_binary_op = true;
3e4093b6 9418 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9419 in_late_binary_op = save;
3e4093b6
RS
9420
9421 /* Strip any conversions, additions, and subtractions, and see if
9422 we are returning the address of a local variable. Warn if so. */
9423 while (1)
8b6a5902 9424 {
3e4093b6 9425 switch (TREE_CODE (inner))
8b6a5902 9426 {
849421a3
JJ
9427 CASE_CONVERT:
9428 case NON_LVALUE_EXPR:
3e4093b6 9429 case PLUS_EXPR:
849421a3 9430 case POINTER_PLUS_EXPR:
3e4093b6
RS
9431 inner = TREE_OPERAND (inner, 0);
9432 continue;
9433
9434 case MINUS_EXPR:
9435 /* If the second operand of the MINUS_EXPR has a pointer
9436 type (or is converted from it), this may be valid, so
9437 don't give a warning. */
9438 {
9439 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9440
3f75a254 9441 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9442 && (CONVERT_EXPR_P (op1)
9443 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9444 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9445
3e4093b6
RS
9446 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9447 break;
8b6a5902 9448
3e4093b6
RS
9449 inner = TREE_OPERAND (inner, 0);
9450 continue;
9451 }
400fbf9f 9452
3e4093b6
RS
9453 case ADDR_EXPR:
9454 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9455
6615c446 9456 while (REFERENCE_CLASS_P (inner)
c22cacf3 9457 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 9458 inner = TREE_OPERAND (inner, 0);
8b6a5902 9459
a2f1f4c3 9460 if (DECL_P (inner)
3f75a254
JM
9461 && !DECL_EXTERNAL (inner)
9462 && !TREE_STATIC (inner)
3e4093b6 9463 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9464 {
9465 if (TREE_CODE (inner) == LABEL_DECL)
9466 warning_at (loc, OPT_Wreturn_local_addr,
9467 "function returns address of label");
9468 else
b4dfdc11
MG
9469 {
9470 warning_at (loc, OPT_Wreturn_local_addr,
9471 "function returns address of local variable");
9472 tree zero = build_zero_cst (TREE_TYPE (res));
9473 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9474 }
19fc9faa 9475 }
3e4093b6 9476 break;
8b6a5902 9477
3e4093b6
RS
9478 default:
9479 break;
9480 }
de520661 9481
3e4093b6
RS
9482 break;
9483 }
9484
53fb4de3 9485 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9486 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9487
9488 if (warn_sequence_point)
9489 verify_sequence_points (retval);
de520661 9490 }
8b6a5902 9491
c2255bc4 9492 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9493 TREE_NO_WARNING (ret_stmt) |= no_warning;
9494 return add_stmt (ret_stmt);
de520661 9495}
3e4093b6
RS
9496\f
9497struct c_switch {
604f5adf
ILT
9498 /* The SWITCH_EXPR being built. */
9499 tree switch_expr;
a6c0a76c 9500
89dbed81 9501 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9502 default conversion is applied. */
9503 tree orig_type;
9504
3e4093b6
RS
9505 /* A splay-tree mapping the low element of a case range to the high
9506 element, or NULL_TREE if there is no high element. Used to
9507 determine whether or not a new case label duplicates an old case
9508 label. We need a tree, rather than simply a hash table, because
9509 of the GNU case range extension. */
9510 splay_tree cases;
a6c0a76c 9511
e1b7793c
ILT
9512 /* The bindings at the point of the switch. This is used for
9513 warnings crossing decls when branching to a case label. */
9514 struct c_spot_bindings *bindings;
187230a7 9515
3e4093b6
RS
9516 /* The next node on the stack. */
9517 struct c_switch *next;
9518};
400fbf9f 9519
3e4093b6
RS
9520/* A stack of the currently active switch statements. The innermost
9521 switch statement is on the top of the stack. There is no need to
9522 mark the stack for garbage collection because it is only active
9523 during the processing of the body of a function, and we never
9524 collect at that point. */
de520661 9525
506e2710 9526struct c_switch *c_switch_stack;
de520661 9527
3e4093b6 9528/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9529 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef
MP
9530 SWITCH_COND_LOC is the location of the switch's condition.
9531 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
de520661 9532
3e4093b6 9533tree
c2255bc4
AH
9534c_start_case (location_t switch_loc,
9535 location_t switch_cond_loc,
fedfecef 9536 tree exp, bool explicit_cast_p)
de520661 9537{
c58e8676 9538 tree orig_type = error_mark_node;
3e4093b6 9539 struct c_switch *cs;
2f6e4e97 9540
3e4093b6 9541 if (exp != error_mark_node)
de520661 9542 {
3e4093b6
RS
9543 orig_type = TREE_TYPE (exp);
9544
c58e8676 9545 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9546 {
c58e8676
VR
9547 if (orig_type != error_mark_node)
9548 {
c2255bc4 9549 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9550 orig_type = error_mark_node;
9551 }
3e4093b6 9552 exp = integer_zero_node;
de520661 9553 }
3e4093b6 9554 else
de520661 9555 {
c58e8676 9556 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9557 tree e = exp;
9558
9559 /* Warn if the condition has boolean value. */
9560 while (TREE_CODE (e) == COMPOUND_EXPR)
9561 e = TREE_OPERAND (e, 1);
9562
9563 if ((TREE_CODE (type) == BOOLEAN_TYPE
9564 || truth_value_p (TREE_CODE (e)))
9565 /* Explicit cast to int suppresses this warning. */
9566 && !(TREE_CODE (type) == INTEGER_TYPE
9567 && explicit_cast_p))
9568 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9569 "switch condition has boolean value");
8b6a5902 9570
8400e75e 9571 if (!in_system_header_at (input_location)
3e4093b6
RS
9572 && (type == long_integer_type_node
9573 || type == long_unsigned_type_node))
c2255bc4
AH
9574 warning_at (switch_cond_loc,
9575 OPT_Wtraditional, "%<long%> switch expression not "
9576 "converted to %<int%> in ISO C");
8b6a5902 9577
928c19bb 9578 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9579 exp = default_conversion (exp);
ca085fd7
MLI
9580
9581 if (warn_sequence_point)
9582 verify_sequence_points (exp);
3e4093b6
RS
9583 }
9584 }
9585
604f5adf 9586 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9587 cs = XNEW (struct c_switch);
604f5adf 9588 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9589 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9590 cs->orig_type = orig_type;
3e4093b6 9591 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9592 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9593 cs->next = c_switch_stack;
9594 c_switch_stack = cs;
3e4093b6 9595
604f5adf 9596 return add_stmt (cs->switch_expr);
3e4093b6
RS
9597}
9598
c2255bc4 9599/* Process a case label at location LOC. */
3e4093b6
RS
9600
9601tree
c2255bc4 9602do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9603{
9604 tree label = NULL_TREE;
9605
17cede2e
JM
9606 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9607 {
9608 low_value = c_fully_fold (low_value, false, NULL);
9609 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9610 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9611 "case label is not an integer constant expression");
9612 }
9613
9614 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9615 {
9616 high_value = c_fully_fold (high_value, false, NULL);
9617 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9618 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9619 "case label is not an integer constant expression");
9620 }
9621
e1b7793c 9622 if (c_switch_stack == NULL)
187230a7
JM
9623 {
9624 if (low_value)
e1b7793c 9625 error_at (loc, "case label not within a switch statement");
187230a7 9626 else
e1b7793c
ILT
9627 error_at (loc, "%<default%> label not within a switch statement");
9628 return NULL_TREE;
187230a7 9629 }
de520661 9630
e1b7793c
ILT
9631 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9632 EXPR_LOCATION (c_switch_stack->switch_expr),
9633 loc))
9634 return NULL_TREE;
9635
9636 label = c_add_case_label (loc, c_switch_stack->cases,
9637 SWITCH_COND (c_switch_stack->switch_expr),
9638 c_switch_stack->orig_type,
9639 low_value, high_value);
9640 if (label == error_mark_node)
9641 label = NULL_TREE;
3e4093b6
RS
9642 return label;
9643}
de520661 9644
083e891e
MP
9645/* Finish the switch statement. TYPE is the original type of the
9646 controlling expression of the switch, or NULL_TREE. */
de520661 9647
3e4093b6 9648void
083e891e 9649c_finish_case (tree body, tree type)
3e4093b6 9650{
506e2710 9651 struct c_switch *cs = c_switch_stack;
fbc315db 9652 location_t switch_location;
3e4093b6 9653
604f5adf 9654 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9655
6de9cd9a 9656 /* Emit warnings as needed. */
c2255bc4 9657 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 9658 c_do_switch_warnings (cs->cases, switch_location,
083e891e 9659 type ? type : TREE_TYPE (cs->switch_expr),
fbc315db 9660 SWITCH_COND (cs->switch_expr));
6de9cd9a 9661
3e4093b6 9662 /* Pop the stack. */
506e2710 9663 c_switch_stack = cs->next;
3e4093b6 9664 splay_tree_delete (cs->cases);
e1b7793c 9665 c_release_switch_bindings (cs->bindings);
5d038c4c 9666 XDELETE (cs);
de520661 9667}
325c3691 9668\f
506e2710
RH
9669/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9670 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9671 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9672 statement, and was not surrounded with parenthesis. */
325c3691 9673
9e51cf9d 9674void
506e2710
RH
9675c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9676 tree else_block, bool nested_if)
325c3691 9677{
506e2710 9678 tree stmt;
325c3691 9679
25c22937
BI
9680 /* If the condition has array notations, then the rank of the then_block and
9681 else_block must be either 0 or be equal to the rank of the condition. If
9682 the condition does not have array notations then break them up as it is
9683 broken up in a normal expression. */
b72271b9 9684 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9685 {
9686 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9687 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9688 return;
9689 if (then_block
9690 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9691 return;
9692 if (else_block
9693 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9694 return;
9695 if (cond_rank != then_rank && then_rank != 0)
9696 {
9697 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9698 " and the then-block");
9699 return;
9700 }
9701 else if (cond_rank != else_rank && else_rank != 0)
9702 {
9703 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9704 " and the else-block");
9705 return;
9706 }
9707 }
506e2710
RH
9708 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9709 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9710 {
506e2710 9711 tree inner_if = then_block;
16865eaa 9712
61ada8ae 9713 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9714 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9715 it might not be exactly THEN_BLOCK, but should be the last
9716 non-container statement within. */
9717 while (1)
9718 switch (TREE_CODE (inner_if))
9719 {
9720 case COND_EXPR:
9721 goto found;
9722 case BIND_EXPR:
9723 inner_if = BIND_EXPR_BODY (inner_if);
9724 break;
9725 case STATEMENT_LIST:
9726 inner_if = expr_last (then_block);
9727 break;
9728 case TRY_FINALLY_EXPR:
9729 case TRY_CATCH_EXPR:
9730 inner_if = TREE_OPERAND (inner_if, 0);
9731 break;
9732 default:
366de0ce 9733 gcc_unreachable ();
506e2710
RH
9734 }
9735 found:
16865eaa 9736
506e2710 9737 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9738 warning_at (if_locus, OPT_Wparentheses,
9739 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9740 }
16865eaa 9741
2214de30 9742 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9743 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9744 add_stmt (stmt);
325c3691
RH
9745}
9746
506e2710
RH
9747/* Emit a general-purpose loop construct. START_LOCUS is the location of
9748 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9749 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9750 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9751 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9752
9753void
506e2710
RH
9754c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9755 tree blab, tree clab, bool cond_is_first)
325c3691 9756{
506e2710
RH
9757 tree entry = NULL, exit = NULL, t;
9758
e5e44252
AK
9759 /* In theory could forbid cilk spawn for loop increment expression,
9760 but it should work just fine. */
36536d79 9761
28af952a
RS
9762 /* If the condition is zero don't generate a loop construct. */
9763 if (cond && integer_zerop (cond))
9764 {
9765 if (cond_is_first)
9766 {
9767 t = build_and_jump (&blab);
9768 SET_EXPR_LOCATION (t, start_locus);
9769 add_stmt (t);
9770 }
9771 }
9772 else
506e2710
RH
9773 {
9774 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9775
506e2710 9776 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9777 out of the loop, or to the top of it. If there's no exit condition,
9778 then we just build a jump back to the top. */
506e2710 9779 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9780
28af952a 9781 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9782 {
9783 /* Canonicalize the loop condition to the end. This means
9784 generating a branch to the loop condition. Reuse the
9785 continue label, if possible. */
9786 if (cond_is_first)
9787 {
9788 if (incr || !clab)
9789 {
9790 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9791 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9792 }
9793 else
9794 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9795 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9796 add_stmt (t);
9797 }
9798
506e2710 9799 t = build_and_jump (&blab);
506e2710 9800 if (cond_is_first)
db3927fb
AH
9801 exit = fold_build3_loc (start_locus,
9802 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9803 else
db3927fb
AH
9804 exit = fold_build3_loc (input_location,
9805 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9806 }
9807
506e2710
RH
9808 add_stmt (top);
9809 }
c22cacf3 9810
506e2710
RH
9811 if (body)
9812 add_stmt (body);
9813 if (clab)
9814 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9815 if (incr)
9816 add_stmt (incr);
9817 if (entry)
9818 add_stmt (entry);
9819 if (exit)
9820 add_stmt (exit);
9821 if (blab)
9822 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9823}
325c3691
RH
9824
9825tree
c2255bc4 9826c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9827{
089efaa4 9828 bool skip;
506e2710 9829 tree label = *label_p;
325c3691 9830
089efaa4
ILT
9831 /* In switch statements break is sometimes stylistically used after
9832 a return statement. This can lead to spurious warnings about
9833 control reaching the end of a non-void function when it is
9834 inlined. Note that we are calling block_may_fallthru with
9835 language specific tree nodes; this works because
9836 block_may_fallthru returns true when given something it does not
9837 understand. */
9838 skip = !block_may_fallthru (cur_stmt_list);
9839
506e2710 9840 if (!label)
089efaa4
ILT
9841 {
9842 if (!skip)
c2255bc4 9843 *label_p = label = create_artificial_label (loc);
089efaa4 9844 }
953ff289
DN
9845 else if (TREE_CODE (label) == LABEL_DECL)
9846 ;
9847 else switch (TREE_INT_CST_LOW (label))
506e2710 9848 {
953ff289 9849 case 0:
506e2710 9850 if (is_break)
c2255bc4 9851 error_at (loc, "break statement not within loop or switch");
506e2710 9852 else
c2255bc4 9853 error_at (loc, "continue statement not within a loop");
506e2710 9854 return NULL_TREE;
953ff289
DN
9855
9856 case 1:
9857 gcc_assert (is_break);
c2255bc4 9858 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9859 return NULL_TREE;
9860
c02065fc
AH
9861 case 2:
9862 if (is_break)
9863 error ("break statement within %<#pragma simd%> loop body");
9864 else
9865 error ("continue statement within %<#pragma simd%> loop body");
9866 return NULL_TREE;
9867
953ff289
DN
9868 default:
9869 gcc_unreachable ();
506e2710 9870 }
325c3691 9871
089efaa4
ILT
9872 if (skip)
9873 return NULL_TREE;
9874
2e28e797
JH
9875 if (!is_break)
9876 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9877
53fb4de3 9878 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9879}
9880
506e2710 9881/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9882
9883static void
c2255bc4 9884emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9885{
e6b5a630
RH
9886 if (expr == error_mark_node)
9887 ;
9888 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9889 {
9890 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9891 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9892 }
789eadcd
MP
9893 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9894 {
9895 tree r = expr;
9896 location_t cloc = loc;
9897 while (TREE_CODE (r) == COMPOUND_EXPR)
9898 {
9899 if (EXPR_HAS_LOCATION (r))
9900 cloc = EXPR_LOCATION (r);
9901 r = TREE_OPERAND (r, 1);
9902 }
9903 if (!TREE_SIDE_EFFECTS (r)
9904 && !VOID_TYPE_P (TREE_TYPE (r))
9905 && !CONVERT_EXPR_P (r)
cc28fc7f 9906 && !TREE_NO_WARNING (r)
789eadcd
MP
9907 && !TREE_NO_WARNING (expr))
9908 warning_at (cloc, OPT_Wunused_value,
9909 "right-hand operand of comma expression has no effect");
9910 }
27f33b15 9911 else
c2255bc4 9912 warn_if_unused_value (expr, loc);
3a5b9284
RH
9913}
9914
506e2710 9915/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9916 diagnostics, but do not call ADD_STMT. LOC is the location of the
9917 statement. */
3a5b9284 9918
506e2710 9919tree
c2255bc4 9920c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9921{
056928b2
JJ
9922 tree exprv;
9923
3a5b9284 9924 if (!expr)
506e2710 9925 return NULL_TREE;
3a5b9284 9926
928c19bb
JM
9927 expr = c_fully_fold (expr, false, NULL);
9928
3a5b9284
RH
9929 if (warn_sequence_point)
9930 verify_sequence_points (expr);
9931
9932 if (TREE_TYPE (expr) != error_mark_node
9933 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9934 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9935 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9936
9937 /* If we're not processing a statement expression, warn about unused values.
9938 Warnings for statement expressions will be emitted later, once we figure
9939 out which is the result. */
9940 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9941 && warn_unused_value)
c2255bc4 9942 emit_side_effect_warnings (loc, expr);
3a5b9284 9943
056928b2
JJ
9944 exprv = expr;
9945 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9946 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9947 while (CONVERT_EXPR_P (exprv))
9948 exprv = TREE_OPERAND (exprv, 0);
9949 if (DECL_P (exprv)
9950 || handled_component_p (exprv)
9951 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9952 mark_exp_read (exprv);
fa8351f8 9953
3a5b9284
RH
9954 /* If the expression is not of a type to which we cannot assign a line
9955 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9956 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9957 {
9958 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9959 SET_EXPR_LOCATION (expr, loc);
9960 }
506e2710
RH
9961
9962 return expr;
9963}
9964
c2255bc4
AH
9965/* Emit an expression as a statement. LOC is the location of the
9966 expression. */
506e2710
RH
9967
9968tree
c2255bc4 9969c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9970{
9971 if (expr)
c2255bc4 9972 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9973 else
9974 return NULL;
3a5b9284
RH
9975}
9976
9977/* Do the opposite and emit a statement as an expression. To begin,
9978 create a new binding level and return it. */
325c3691
RH
9979
9980tree
9981c_begin_stmt_expr (void)
9982{
9983 tree ret;
9984
9985 /* We must force a BLOCK for this level so that, if it is not expanded
9986 later, there is a way to turn off the entire subtree of blocks that
9987 are contained in it. */
9988 keep_next_level ();
9989 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
9990
9991 c_bindings_start_stmt_expr (c_switch_stack == NULL
9992 ? NULL
9993 : c_switch_stack->bindings);
325c3691
RH
9994
9995 /* Mark the current statement list as belonging to a statement list. */
9996 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9997
9998 return ret;
9999}
10000
c2255bc4
AH
10001/* LOC is the location of the compound statement to which this body
10002 belongs. */
10003
325c3691 10004tree
c2255bc4 10005c_finish_stmt_expr (location_t loc, tree body)
325c3691 10006{
3a5b9284 10007 tree last, type, tmp, val;
325c3691
RH
10008 tree *last_p;
10009
c2255bc4 10010 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10011
10012 c_bindings_end_stmt_expr (c_switch_stack == NULL
10013 ? NULL
10014 : c_switch_stack->bindings);
325c3691 10015
3a5b9284
RH
10016 /* Locate the last statement in BODY. See c_end_compound_stmt
10017 about always returning a BIND_EXPR. */
10018 last_p = &BIND_EXPR_BODY (body);
10019 last = BIND_EXPR_BODY (body);
10020
10021 continue_searching:
325c3691
RH
10022 if (TREE_CODE (last) == STATEMENT_LIST)
10023 {
3a5b9284
RH
10024 tree_stmt_iterator i;
10025
10026 /* This can happen with degenerate cases like ({ }). No value. */
10027 if (!TREE_SIDE_EFFECTS (last))
10028 return body;
10029
10030 /* If we're supposed to generate side effects warnings, process
10031 all of the statements except the last. */
27f33b15 10032 if (warn_unused_value)
325c3691 10033 {
3a5b9284 10034 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10035 {
10036 location_t tloc;
10037 tree t = tsi_stmt (i);
10038
10039 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10040 emit_side_effect_warnings (tloc, t);
10041 }
325c3691
RH
10042 }
10043 else
3a5b9284
RH
10044 i = tsi_last (last);
10045 last_p = tsi_stmt_ptr (i);
10046 last = *last_p;
325c3691
RH
10047 }
10048
3a5b9284
RH
10049 /* If the end of the list is exception related, then the list was split
10050 by a call to push_cleanup. Continue searching. */
10051 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10052 || TREE_CODE (last) == TRY_CATCH_EXPR)
10053 {
10054 last_p = &TREE_OPERAND (last, 0);
10055 last = *last_p;
10056 goto continue_searching;
10057 }
10058
26d8af35
JM
10059 if (last == error_mark_node)
10060 return last;
10061
3a5b9284
RH
10062 /* In the case that the BIND_EXPR is not necessary, return the
10063 expression out from inside it. */
26d8af35
JM
10064 if (last == BIND_EXPR_BODY (body)
10065 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10066 {
928c19bb
JM
10067 /* Even if this looks constant, do not allow it in a constant
10068 expression. */
e5a94231 10069 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10070 /* Do not warn if the return value of a statement expression is
10071 unused. */
928c19bb 10072 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10073 return last;
10074 }
325c3691
RH
10075
10076 /* Extract the type of said expression. */
10077 type = TREE_TYPE (last);
325c3691 10078
3a5b9284
RH
10079 /* If we're not returning a value at all, then the BIND_EXPR that
10080 we already have is a fine expression to return. */
10081 if (!type || VOID_TYPE_P (type))
10082 return body;
10083
10084 /* Now that we've located the expression containing the value, it seems
10085 silly to make voidify_wrapper_expr repeat the process. Create a
10086 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10087 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10088
10089 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10090 tree_expr_nonnegative_p giving up immediately. */
10091 val = last;
10092 if (TREE_CODE (val) == NOP_EXPR
10093 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10094 val = TREE_OPERAND (val, 0);
10095
53fb4de3 10096 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10097 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10098
c2255bc4
AH
10099 {
10100 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10101 SET_EXPR_LOCATION (t, loc);
10102 return t;
10103 }
325c3691
RH
10104}
10105\f
10106/* Begin and end compound statements. This is as simple as pushing
10107 and popping new statement lists from the tree. */
10108
10109tree
10110c_begin_compound_stmt (bool do_scope)
10111{
10112 tree stmt = push_stmt_list ();
10113 if (do_scope)
4dfa0342 10114 push_scope ();
325c3691
RH
10115 return stmt;
10116}
10117
c2255bc4
AH
10118/* End a compound statement. STMT is the statement. LOC is the
10119 location of the compound statement-- this is usually the location
10120 of the opening brace. */
10121
325c3691 10122tree
c2255bc4 10123c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10124{
10125 tree block = NULL;
10126
10127 if (do_scope)
10128 {
10129 if (c_dialect_objc ())
10130 objc_clear_super_receiver ();
10131 block = pop_scope ();
10132 }
10133
10134 stmt = pop_stmt_list (stmt);
c2255bc4 10135 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10136
10137 /* If this compound statement is nested immediately inside a statement
10138 expression, then force a BIND_EXPR to be created. Otherwise we'll
10139 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10140 STATEMENT_LISTs merge, and thus we can lose track of what statement
10141 was really last. */
38e01f9e 10142 if (building_stmt_list_p ()
325c3691
RH
10143 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10144 && TREE_CODE (stmt) != BIND_EXPR)
10145 {
53fb4de3 10146 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10147 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10148 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10149 }
10150
10151 return stmt;
10152}
5a508662
RH
10153
10154/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10155 when the current scope is exited. EH_ONLY is true when this is not
10156 meant to apply to normal control flow transfer. */
10157
10158void
c2255bc4 10159push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10160{
3a5b9284
RH
10161 enum tree_code code;
10162 tree stmt, list;
10163 bool stmt_expr;
10164
10165 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10166 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10167 add_stmt (stmt);
3a5b9284
RH
10168 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10169 list = push_stmt_list ();
10170 TREE_OPERAND (stmt, 0) = list;
10171 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10172}
325c3691 10173\f
3e4093b6
RS
10174/* Build a binary-operation expression without default conversions.
10175 CODE is the kind of expression to build.
ba47d38d 10176 LOCATION is the operator's location.
3e4093b6
RS
10177 This function differs from `build' in several ways:
10178 the data type of the result is computed and recorded in it,
10179 warnings are generated if arg data types are invalid,
10180 special handling for addition and subtraction of pointers is known,
10181 and some optimization is done (operations on narrow ints
10182 are done in the narrower type when that gives the same result).
10183 Constant folding is also done before the result is returned.
de520661 10184
3e4093b6
RS
10185 Note that the operands will never have enumeral types, or function
10186 or array types, because either they will have the default conversions
10187 performed or they have both just been converted to some other type in which
10188 the arithmetic is to be done. */
10189
10190tree
ba47d38d
AH
10191build_binary_op (location_t location, enum tree_code code,
10192 tree orig_op0, tree orig_op1, int convert_p)
de520661 10193{
8ce94e44
JM
10194 tree type0, type1, orig_type0, orig_type1;
10195 tree eptype;
3e4093b6
RS
10196 enum tree_code code0, code1;
10197 tree op0, op1;
c9f9eb5d 10198 tree ret = error_mark_node;
4de67c26 10199 const char *invalid_op_diag;
4d84fe7c 10200 bool op0_int_operands, op1_int_operands;
928c19bb 10201 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10202
3e4093b6
RS
10203 /* Expression code to give to the expression when it is built.
10204 Normally this is CODE, which is what the caller asked for,
10205 but in some special cases we change it. */
10206 enum tree_code resultcode = code;
8b6a5902 10207
3e4093b6
RS
10208 /* Data type in which the computation is to be performed.
10209 In the simplest cases this is the common type of the arguments. */
10210 tree result_type = NULL;
10211
8ce94e44
JM
10212 /* When the computation is in excess precision, the type of the
10213 final EXCESS_PRECISION_EXPR. */
2d2e923f 10214 tree semantic_result_type = NULL;
8ce94e44 10215
3e4093b6
RS
10216 /* Nonzero means operands have already been type-converted
10217 in whatever way is necessary.
10218 Zero means they need to be converted to RESULT_TYPE. */
10219 int converted = 0;
10220
10221 /* Nonzero means create the expression with this type, rather than
10222 RESULT_TYPE. */
10223 tree build_type = 0;
10224
10225 /* Nonzero means after finally constructing the expression
10226 convert it to this type. */
10227 tree final_type = 0;
10228
10229 /* Nonzero if this is an operation like MIN or MAX which can
10230 safely be computed in short if both args are promoted shorts.
10231 Also implies COMMON.
10232 -1 indicates a bitwise operation; this makes a difference
10233 in the exact conditions for when it is safe to do the operation
10234 in a narrower mode. */
10235 int shorten = 0;
10236
10237 /* Nonzero if this is a comparison operation;
10238 if both args are promoted shorts, compare the original shorts.
10239 Also implies COMMON. */
10240 int short_compare = 0;
10241
10242 /* Nonzero if this is a right-shift operation, which can be computed on the
10243 original short and then promoted if the operand is a promoted short. */
10244 int short_shift = 0;
10245
10246 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10247 int common = 0;
10248
58393038
ZL
10249 /* True means types are compatible as far as ObjC is concerned. */
10250 bool objc_ok;
10251
8ce94e44
JM
10252 /* True means this is an arithmetic operation that may need excess
10253 precision. */
10254 bool may_need_excess_precision;
10255
180f8dbb
JM
10256 /* True means this is a boolean operation that converts both its
10257 operands to truth-values. */
10258 bool boolean_op = false;
10259
de5a5fa1
MP
10260 /* Remember whether we're doing / or %. */
10261 bool doing_div_or_mod = false;
10262
10263 /* Remember whether we're doing << or >>. */
10264 bool doing_shift = false;
10265
10266 /* Tree holding instrumentation expression. */
10267 tree instrument_expr = NULL;
10268
ba47d38d
AH
10269 if (location == UNKNOWN_LOCATION)
10270 location = input_location;
10271
4d84fe7c
JM
10272 op0 = orig_op0;
10273 op1 = orig_op1;
10274
10275 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10276 if (op0_int_operands)
10277 op0 = remove_c_maybe_const_expr (op0);
10278 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10279 if (op1_int_operands)
10280 op1 = remove_c_maybe_const_expr (op1);
10281 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10282 if (int_operands)
10283 {
10284 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10285 && TREE_CODE (orig_op1) == INTEGER_CST);
10286 int_const = (int_const_or_overflow
10287 && !TREE_OVERFLOW (orig_op0)
10288 && !TREE_OVERFLOW (orig_op1));
10289 }
10290 else
10291 int_const = int_const_or_overflow = false;
10292
0e3a99ae 10293 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
10294 if (convert_p
10295 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 10296 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 10297 {
4d84fe7c
JM
10298 op0 = default_conversion (op0);
10299 op1 = default_conversion (op1);
790e9490
RS
10300 }
10301
36536d79
BI
10302 /* When Cilk Plus is enabled and there are array notations inside op0, then
10303 we check to see if there are builtin array notation functions. If
10304 so, then we take on the type of the array notation inside it. */
b72271b9 10305 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10306 orig_type0 = type0 = find_correct_array_notation_type (op0);
10307 else
10308 orig_type0 = type0 = TREE_TYPE (op0);
10309
b72271b9 10310 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10311 orig_type1 = type1 = find_correct_array_notation_type (op1);
10312 else
10313 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10314
3e4093b6
RS
10315 /* The expression codes of the data types of the arguments tell us
10316 whether the arguments are integers, floating, pointers, etc. */
10317 code0 = TREE_CODE (type0);
10318 code1 = TREE_CODE (type1);
10319
10320 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10321 STRIP_TYPE_NOPS (op0);
10322 STRIP_TYPE_NOPS (op1);
10323
10324 /* If an error was already reported for one of the arguments,
10325 avoid reporting another error. */
10326
10327 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10328 return error_mark_node;
10329
4de67c26
JM
10330 if ((invalid_op_diag
10331 = targetm.invalid_binary_op (code, type0, type1)))
10332 {
ba47d38d 10333 error_at (location, invalid_op_diag);
4de67c26
JM
10334 return error_mark_node;
10335 }
10336
8ce94e44
JM
10337 switch (code)
10338 {
10339 case PLUS_EXPR:
10340 case MINUS_EXPR:
10341 case MULT_EXPR:
10342 case TRUNC_DIV_EXPR:
10343 case CEIL_DIV_EXPR:
10344 case FLOOR_DIV_EXPR:
10345 case ROUND_DIV_EXPR:
10346 case EXACT_DIV_EXPR:
10347 may_need_excess_precision = true;
10348 break;
10349 default:
10350 may_need_excess_precision = false;
10351 break;
10352 }
10353 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10354 {
10355 op0 = TREE_OPERAND (op0, 0);
10356 type0 = TREE_TYPE (op0);
10357 }
10358 else if (may_need_excess_precision
10359 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10360 {
10361 type0 = eptype;
10362 op0 = convert (eptype, op0);
10363 }
10364 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10365 {
10366 op1 = TREE_OPERAND (op1, 0);
10367 type1 = TREE_TYPE (op1);
10368 }
10369 else if (may_need_excess_precision
10370 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10371 {
10372 type1 = eptype;
10373 op1 = convert (eptype, op1);
10374 }
10375
58393038
ZL
10376 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10377
0e3a99ae
AS
10378 /* In case when one of the operands of the binary operation is
10379 a vector and another is a scalar -- convert scalar to vector. */
10380 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10381 {
a212e43f
MG
10382 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10383 true);
f90e8e2e 10384
0e3a99ae
AS
10385 switch (convert_flag)
10386 {
10387 case stv_error:
10388 return error_mark_node;
10389 case stv_firstarg:
10390 {
10391 bool maybe_const = true;
10392 tree sc;
10393 sc = c_fully_fold (op0, false, &maybe_const);
10394 sc = save_expr (sc);
10395 sc = convert (TREE_TYPE (type1), sc);
10396 op0 = build_vector_from_val (type1, sc);
10397 if (!maybe_const)
10398 op0 = c_wrap_maybe_const (op0, true);
10399 orig_type0 = type0 = TREE_TYPE (op0);
10400 code0 = TREE_CODE (type0);
10401 converted = 1;
10402 break;
10403 }
10404 case stv_secondarg:
10405 {
10406 bool maybe_const = true;
10407 tree sc;
10408 sc = c_fully_fold (op1, false, &maybe_const);
10409 sc = save_expr (sc);
10410 sc = convert (TREE_TYPE (type0), sc);
10411 op1 = build_vector_from_val (type0, sc);
10412 if (!maybe_const)
54b9f838 10413 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10414 orig_type1 = type1 = TREE_TYPE (op1);
10415 code1 = TREE_CODE (type1);
10416 converted = 1;
10417 break;
10418 }
10419 default:
10420 break;
10421 }
10422 }
10423
3e4093b6 10424 switch (code)
de520661 10425 {
3e4093b6
RS
10426 case PLUS_EXPR:
10427 /* Handle the pointer + int case. */
10428 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10429 {
db3927fb 10430 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10431 goto return_build_binary_op;
10432 }
3e4093b6 10433 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10434 {
db3927fb 10435 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10436 goto return_build_binary_op;
10437 }
fe67cf58 10438 else
3e4093b6
RS
10439 common = 1;
10440 break;
400fbf9f 10441
3e4093b6
RS
10442 case MINUS_EXPR:
10443 /* Subtraction of two similar pointers.
10444 We must subtract them as integers, then divide by object size. */
10445 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10446 && comp_target_types (location, type0, type1))
c9f9eb5d 10447 {
db3927fb 10448 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10449 goto return_build_binary_op;
10450 }
3e4093b6
RS
10451 /* Handle pointer minus int. Just like pointer plus int. */
10452 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10453 {
db3927fb 10454 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10455 goto return_build_binary_op;
10456 }
3e4093b6
RS
10457 else
10458 common = 1;
10459 break;
8b6a5902 10460
3e4093b6
RS
10461 case MULT_EXPR:
10462 common = 1;
10463 break;
10464
10465 case TRUNC_DIV_EXPR:
10466 case CEIL_DIV_EXPR:
10467 case FLOOR_DIV_EXPR:
10468 case ROUND_DIV_EXPR:
10469 case EXACT_DIV_EXPR:
de5a5fa1 10470 doing_div_or_mod = true;
c9f9eb5d 10471 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10472
10473 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10474 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10475 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10476 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10477 || code1 == FIXED_POINT_TYPE
3e4093b6 10478 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10479 {
5bed876a
AH
10480 enum tree_code tcode0 = code0, tcode1 = code1;
10481
3a021db2 10482 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10483 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10484 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10485 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10486
ab22c1fa
CF
10487 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10488 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10489 resultcode = RDIV_EXPR;
10490 else
10491 /* Although it would be tempting to shorten always here, that
10492 loses on some targets, since the modulo instruction is
10493 undefined if the quotient can't be represented in the
10494 computation mode. We shorten only if unsigned or if
10495 dividing by something we know != -1. */
8df83eae 10496 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10497 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10498 && !integer_all_onesp (op1)));
3e4093b6
RS
10499 common = 1;
10500 }
10501 break;
de520661 10502
3e4093b6 10503 case BIT_AND_EXPR:
3e4093b6
RS
10504 case BIT_IOR_EXPR:
10505 case BIT_XOR_EXPR:
10506 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10507 shorten = -1;
9ef0c8d9
AP
10508 /* Allow vector types which are not floating point types. */
10509 else if (code0 == VECTOR_TYPE
10510 && code1 == VECTOR_TYPE
10511 && !VECTOR_FLOAT_TYPE_P (type0)
10512 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10513 common = 1;
10514 break;
10515
10516 case TRUNC_MOD_EXPR:
10517 case FLOOR_MOD_EXPR:
de5a5fa1 10518 doing_div_or_mod = true;
c9f9eb5d 10519 warn_for_div_by_zero (location, op1);
de520661 10520
5cfd5d9b
AP
10521 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10522 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10523 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10524 common = 1;
10525 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10526 {
10527 /* Although it would be tempting to shorten always here, that loses
10528 on some targets, since the modulo instruction is undefined if the
10529 quotient can't be represented in the computation mode. We shorten
10530 only if unsigned or if dividing by something we know != -1. */
8df83eae 10531 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10532 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10533 && !integer_all_onesp (op1)));
3e4093b6
RS
10534 common = 1;
10535 }
10536 break;
de520661 10537
3e4093b6
RS
10538 case TRUTH_ANDIF_EXPR:
10539 case TRUTH_ORIF_EXPR:
10540 case TRUTH_AND_EXPR:
10541 case TRUTH_OR_EXPR:
10542 case TRUTH_XOR_EXPR:
10543 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10544 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10545 || code0 == FIXED_POINT_TYPE)
3e4093b6 10546 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10547 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10548 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10549 {
10550 /* Result of these operations is always an int,
10551 but that does not mean the operands should be
10552 converted to ints! */
10553 result_type = integer_type_node;
a27d595d
JM
10554 if (op0_int_operands)
10555 {
10556 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10557 op0 = remove_c_maybe_const_expr (op0);
10558 }
10559 else
10560 op0 = c_objc_common_truthvalue_conversion (location, op0);
10561 if (op1_int_operands)
10562 {
10563 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10564 op1 = remove_c_maybe_const_expr (op1);
10565 }
10566 else
10567 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10568 converted = 1;
180f8dbb 10569 boolean_op = true;
3e4093b6 10570 }
928c19bb
JM
10571 if (code == TRUTH_ANDIF_EXPR)
10572 {
10573 int_const_or_overflow = (int_operands
10574 && TREE_CODE (orig_op0) == INTEGER_CST
10575 && (op0 == truthvalue_false_node
10576 || TREE_CODE (orig_op1) == INTEGER_CST));
10577 int_const = (int_const_or_overflow
10578 && !TREE_OVERFLOW (orig_op0)
10579 && (op0 == truthvalue_false_node
10580 || !TREE_OVERFLOW (orig_op1)));
10581 }
10582 else if (code == TRUTH_ORIF_EXPR)
10583 {
10584 int_const_or_overflow = (int_operands
10585 && TREE_CODE (orig_op0) == INTEGER_CST
10586 && (op0 == truthvalue_true_node
10587 || TREE_CODE (orig_op1) == INTEGER_CST));
10588 int_const = (int_const_or_overflow
10589 && !TREE_OVERFLOW (orig_op0)
10590 && (op0 == truthvalue_true_node
10591 || !TREE_OVERFLOW (orig_op1)));
10592 }
3e4093b6 10593 break;
eba80994 10594
3e4093b6
RS
10595 /* Shift operations: result has same type as first operand;
10596 always convert second operand to int.
10597 Also set SHORT_SHIFT if shifting rightward. */
de520661 10598
3e4093b6 10599 case RSHIFT_EXPR:
f87bd04b
AS
10600 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10601 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10602 {
10603 result_type = type0;
10604 converted = 1;
10605 }
10606 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10607 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10608 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10609 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10610 {
10611 result_type = type0;
10612 converted = 1;
10613 }
10614 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10615 && code1 == INTEGER_TYPE)
3e4093b6 10616 {
de5a5fa1 10617 doing_shift = true;
928c19bb 10618 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10619 {
3e4093b6 10620 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10621 {
10622 int_const = false;
7d882b83 10623 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10624 warning_at (location, OPT_Wshift_count_negative,
10625 "right shift count is negative");
928c19bb 10626 }
3e4093b6 10627 else
bbb818c6 10628 {
3f75a254 10629 if (!integer_zerop (op1))
3e4093b6
RS
10630 short_shift = 1;
10631
10632 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10633 {
10634 int_const = false;
7d882b83 10635 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10636 warning_at (location, OPT_Wshift_count_overflow,
10637 "right shift count >= width of type");
928c19bb 10638 }
bbb818c6 10639 }
b62acd60 10640 }
de520661 10641
3e4093b6
RS
10642 /* Use the type of the value to be shifted. */
10643 result_type = type0;
3e4093b6
RS
10644 /* Avoid converting op1 to result_type later. */
10645 converted = 1;
400fbf9f 10646 }
3e4093b6 10647 break;
253b6b82 10648
3e4093b6 10649 case LSHIFT_EXPR:
f87bd04b
AS
10650 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10651 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10652 {
10653 result_type = type0;
10654 converted = 1;
10655 }
10656 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10657 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10658 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10659 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10660 {
10661 result_type = type0;
10662 converted = 1;
10663 }
10664 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10665 && code1 == INTEGER_TYPE)
3e4093b6 10666 {
de5a5fa1 10667 doing_shift = true;
928c19bb 10668 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10669 {
3e4093b6 10670 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10671 {
10672 int_const = false;
7d882b83 10673 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10674 warning_at (location, OPT_Wshift_count_negative,
10675 "left shift count is negative");
928c19bb 10676 }
de520661 10677
3e4093b6 10678 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10679 {
10680 int_const = false;
7d882b83 10681 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10682 warning_at (location, OPT_Wshift_count_overflow,
10683 "left shift count >= width of type");
928c19bb 10684 }
94ba5069 10685 }
de520661 10686
3e4093b6
RS
10687 /* Use the type of the value to be shifted. */
10688 result_type = type0;
3e4093b6
RS
10689 /* Avoid converting op1 to result_type later. */
10690 converted = 1;
400fbf9f 10691 }
3e4093b6 10692 break;
de520661 10693
3e4093b6
RS
10694 case EQ_EXPR:
10695 case NE_EXPR:
d246ab4f
AS
10696 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10697 {
10698 tree intt;
0af94e6f 10699 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10700 {
10701 error_at (location, "comparing vectors with different "
10702 "element types");
10703 return error_mark_node;
10704 }
10705
10706 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10707 {
10708 error_at (location, "comparing vectors with different "
10709 "number of elements");
10710 return error_mark_node;
10711 }
10712
10713 /* Always construct signed integer vector type. */
10714 intt = c_common_type_for_size (GET_MODE_BITSIZE
10715 (TYPE_MODE (TREE_TYPE (type0))), 0);
10716 result_type = build_opaque_vector_type (intt,
10717 TYPE_VECTOR_SUBPARTS (type0));
10718 converted = 1;
10719 break;
10720 }
ae311566 10721 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10722 warning_at (location,
10723 OPT_Wfloat_equal,
10724 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10725 /* Result of comparison is always int,
10726 but don't convert the args to int! */
10727 build_type = integer_type_node;
10728 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10729 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10730 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10731 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10732 short_compare = 1;
637f1455
SZ
10733 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10734 {
10735 if (TREE_CODE (op0) == ADDR_EXPR
10736 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10737 {
10738 if (code == EQ_EXPR)
10739 warning_at (location,
10740 OPT_Waddress,
10741 "the comparison will always evaluate as %<false%> "
10742 "for the address of %qD will never be NULL",
10743 TREE_OPERAND (op0, 0));
10744 else
10745 warning_at (location,
10746 OPT_Waddress,
10747 "the comparison will always evaluate as %<true%> "
10748 "for the address of %qD will never be NULL",
10749 TREE_OPERAND (op0, 0));
10750 }
10751 result_type = type0;
10752 }
10753 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10754 {
10755 if (TREE_CODE (op1) == ADDR_EXPR
10756 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10757 {
10758 if (code == EQ_EXPR)
10759 warning_at (location,
f90e8e2e 10760 OPT_Waddress,
637f1455
SZ
10761 "the comparison will always evaluate as %<false%> "
10762 "for the address of %qD will never be NULL",
10763 TREE_OPERAND (op1, 0));
10764 else
10765 warning_at (location,
10766 OPT_Waddress,
10767 "the comparison will always evaluate as %<true%> "
10768 "for the address of %qD will never be NULL",
10769 TREE_OPERAND (op1, 0));
10770 }
10771 result_type = type1;
10772 }
3e4093b6
RS
10773 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10774 {
10775 tree tt0 = TREE_TYPE (type0);
10776 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10777 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10778 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10779 addr_space_t as_common = ADDR_SPACE_GENERIC;
10780
3e4093b6
RS
10781 /* Anything compares with void *. void * compares with anything.
10782 Otherwise, the targets must be compatible
10783 and both must be object or both incomplete. */
744aa42f 10784 if (comp_target_types (location, type0, type1))
10bc1b1b 10785 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10786 else if (!addr_space_superset (as0, as1, &as_common))
10787 {
10788 error_at (location, "comparison of pointers to "
10789 "disjoint address spaces");
10790 return error_mark_node;
10791 }
267bac10 10792 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10793 {
36c5e70a 10794 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10795 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10796 "comparison of %<void *%> with function pointer");
ee2990e7 10797 }
267bac10 10798 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10799 {
36c5e70a 10800 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10801 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10802 "comparison of %<void *%> with function pointer");
e6834654 10803 }
3e4093b6 10804 else
58393038
ZL
10805 /* Avoid warning about the volatile ObjC EH puts on decls. */
10806 if (!objc_ok)
ba47d38d 10807 pedwarn (location, 0,
509c9d60 10808 "comparison of distinct pointer types lacks a cast");
e6834654 10809
3e4093b6 10810 if (result_type == NULL_TREE)
36c5e70a
BE
10811 {
10812 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10813 result_type = build_pointer_type
10814 (build_qualified_type (void_type_node, qual));
10815 }
e6834654 10816 }
3e4093b6 10817 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10818 {
3e4093b6 10819 result_type = type0;
ba47d38d 10820 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10821 }
3e4093b6 10822 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10823 {
3e4093b6 10824 result_type = type1;
ba47d38d 10825 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10826 }
04159acf
MP
10827 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10828 || truth_value_p (TREE_CODE (orig_op0)))
10829 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10830 || truth_value_p (TREE_CODE (orig_op1))))
10831 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10832 break;
8b6a5902 10833
3e4093b6
RS
10834 case LE_EXPR:
10835 case GE_EXPR:
10836 case LT_EXPR:
10837 case GT_EXPR:
d246ab4f
AS
10838 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10839 {
10840 tree intt;
0af94e6f 10841 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10842 {
10843 error_at (location, "comparing vectors with different "
10844 "element types");
10845 return error_mark_node;
10846 }
10847
10848 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10849 {
10850 error_at (location, "comparing vectors with different "
10851 "number of elements");
10852 return error_mark_node;
10853 }
10854
10855 /* Always construct signed integer vector type. */
10856 intt = c_common_type_for_size (GET_MODE_BITSIZE
10857 (TYPE_MODE (TREE_TYPE (type0))), 0);
10858 result_type = build_opaque_vector_type (intt,
10859 TYPE_VECTOR_SUBPARTS (type0));
10860 converted = 1;
10861 break;
10862 }
3e4093b6 10863 build_type = integer_type_node;
ab22c1fa
CF
10864 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10865 || code0 == FIXED_POINT_TYPE)
10866 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10867 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10868 short_compare = 1;
10869 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10870 {
36c5e70a
BE
10871 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10872 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10873 addr_space_t as_common;
10874
744aa42f 10875 if (comp_target_types (location, type0, type1))
3e4093b6 10876 {
10bc1b1b 10877 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10878 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10879 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10880 pedwarn (location, 0,
509c9d60 10881 "comparison of complete and incomplete pointers");
fcf73884 10882 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10883 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10884 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10885 else if (null_pointer_constant_p (orig_op0)
10886 || null_pointer_constant_p (orig_op1))
10887 warning_at (location, OPT_Wextra,
10888 "ordered comparison of pointer with null pointer");
10889
3e4093b6 10890 }
36c5e70a
BE
10891 else if (!addr_space_superset (as0, as1, &as_common))
10892 {
10893 error_at (location, "comparison of pointers to "
10894 "disjoint address spaces");
10895 return error_mark_node;
10896 }
3e4093b6
RS
10897 else
10898 {
36c5e70a
BE
10899 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10900 result_type = build_pointer_type
10901 (build_qualified_type (void_type_node, qual));
ba47d38d 10902 pedwarn (location, 0,
509c9d60 10903 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10904 }
10905 }
6aa3c60d 10906 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10907 {
10908 result_type = type0;
fcf73884 10909 if (pedantic)
c1771a20 10910 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10911 "ordered comparison of pointer with integer zero");
10912 else if (extra_warnings)
ba47d38d 10913 warning_at (location, OPT_Wextra,
d42ba3b8 10914 "ordered comparison of pointer with integer zero");
3e4093b6 10915 }
6aa3c60d 10916 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10917 {
10918 result_type = type1;
d42ba3b8 10919 if (pedantic)
c1771a20 10920 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10921 "ordered comparison of pointer with integer zero");
10922 else if (extra_warnings)
10923 warning_at (location, OPT_Wextra,
10924 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10925 }
10926 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10927 {
10928 result_type = type0;
ba47d38d 10929 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10930 }
10931 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10932 {
10933 result_type = type1;
ba47d38d 10934 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 10935 }
04159acf
MP
10936 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10937 || truth_value_p (TREE_CODE (orig_op0)))
10938 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10939 || truth_value_p (TREE_CODE (orig_op1))))
10940 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10941 break;
64094f6a 10942
3e4093b6 10943 default:
37b2f290 10944 gcc_unreachable ();
c9fe6f9f 10945 }
8f17b5c5 10946
e57e265b
PB
10947 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10948 return error_mark_node;
10949
5bed876a
AH
10950 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10951 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10952 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10953 {
ba47d38d 10954 binary_op_error (location, code, type0, type1);
5bed876a
AH
10955 return error_mark_node;
10956 }
10957
3e4093b6 10958 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10959 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10960 &&
10961 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10962 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10963 {
2ca862e9
JM
10964 bool first_complex = (code0 == COMPLEX_TYPE);
10965 bool second_complex = (code1 == COMPLEX_TYPE);
10966 int none_complex = (!first_complex && !second_complex);
39b726dd 10967
3e4093b6 10968 if (shorten || common || short_compare)
3bf6bfcc
JJ
10969 {
10970 result_type = c_common_type (type0, type1);
0a0b3574
MM
10971 do_warn_double_promotion (result_type, type0, type1,
10972 "implicit conversion from %qT to %qT "
10973 "to match other operand of binary "
10974 "expression",
10975 location);
3bf6bfcc
JJ
10976 if (result_type == error_mark_node)
10977 return error_mark_node;
10978 }
400fbf9f 10979
2ca862e9
JM
10980 if (first_complex != second_complex
10981 && (code == PLUS_EXPR
10982 || code == MINUS_EXPR
10983 || code == MULT_EXPR
10984 || (code == TRUNC_DIV_EXPR && first_complex))
10985 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10986 && flag_signed_zeros)
10987 {
10988 /* An operation on mixed real/complex operands must be
10989 handled specially, but the language-independent code can
10990 more easily optimize the plain complex arithmetic if
10991 -fno-signed-zeros. */
10992 tree real_type = TREE_TYPE (result_type);
10993 tree real, imag;
10994 if (type0 != orig_type0 || type1 != orig_type1)
10995 {
10996 gcc_assert (may_need_excess_precision && common);
2d2e923f 10997 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
10998 }
10999 if (first_complex)
11000 {
11001 if (TREE_TYPE (op0) != result_type)
68fca595 11002 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11003 if (TREE_TYPE (op1) != real_type)
68fca595 11004 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11005 }
11006 else
11007 {
11008 if (TREE_TYPE (op0) != real_type)
68fca595 11009 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11010 if (TREE_TYPE (op1) != result_type)
68fca595 11011 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11012 }
11013 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11014 return error_mark_node;
11015 if (first_complex)
11016 {
11017 op0 = c_save_expr (op0);
11018 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11019 op0, 1);
11020 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11021 op0, 1);
11022 switch (code)
11023 {
11024 case MULT_EXPR:
11025 case TRUNC_DIV_EXPR:
c4603e7c 11026 op1 = c_save_expr (op1);
2ca862e9
JM
11027 imag = build2 (resultcode, real_type, imag, op1);
11028 /* Fall through. */
11029 case PLUS_EXPR:
11030 case MINUS_EXPR:
11031 real = build2 (resultcode, real_type, real, op1);
11032 break;
11033 default:
11034 gcc_unreachable();
11035 }
11036 }
11037 else
11038 {
11039 op1 = c_save_expr (op1);
11040 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11041 op1, 1);
11042 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11043 op1, 1);
11044 switch (code)
11045 {
11046 case MULT_EXPR:
c4603e7c 11047 op0 = c_save_expr (op0);
2ca862e9
JM
11048 imag = build2 (resultcode, real_type, op0, imag);
11049 /* Fall through. */
11050 case PLUS_EXPR:
11051 real = build2 (resultcode, real_type, op0, real);
11052 break;
11053 case MINUS_EXPR:
11054 real = build2 (resultcode, real_type, op0, real);
11055 imag = build1 (NEGATE_EXPR, real_type, imag);
11056 break;
11057 default:
11058 gcc_unreachable();
11059 }
11060 }
11061 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11062 goto return_build_binary_op;
11063 }
11064
3e4093b6
RS
11065 /* For certain operations (which identify themselves by shorten != 0)
11066 if both args were extended from the same smaller type,
11067 do the arithmetic in that type and then extend.
400fbf9f 11068
3e4093b6
RS
11069 shorten !=0 and !=1 indicates a bitwise operation.
11070 For them, this optimization is safe only if
11071 both args are zero-extended or both are sign-extended.
11072 Otherwise, we might change the result.
11073 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11074 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11075
3e4093b6
RS
11076 if (shorten && none_complex)
11077 {
3e4093b6 11078 final_type = result_type;
b8698a0f 11079 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11080 shorten == -1);
3e4093b6 11081 }
88a3dbc1 11082
3e4093b6 11083 /* Shifts can be shortened if shifting right. */
2f6e4e97 11084
3e4093b6
RS
11085 if (short_shift)
11086 {
11087 int unsigned_arg;
11088 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11089
3e4093b6 11090 final_type = result_type;
abe80e6d 11091
3e4093b6 11092 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11093 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11094
3e4093b6 11095 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11096 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11097 /* We can shorten only if the shift count is less than the
11098 number of bits in the smaller type size. */
11099 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11100 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11101 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11102 {
11103 /* Do an unsigned shift if the operand was zero-extended. */
11104 result_type
11105 = c_common_signed_or_unsigned_type (unsigned_arg,
11106 TREE_TYPE (arg0));
11107 /* Convert value-to-be-shifted to that type. */
11108 if (TREE_TYPE (op0) != result_type)
11109 op0 = convert (result_type, op0);
11110 converted = 1;
abe80e6d 11111 }
88a3dbc1
RK
11112 }
11113
3e4093b6
RS
11114 /* Comparison operations are shortened too but differently.
11115 They identify themselves by setting short_compare = 1. */
56cb9733 11116
3e4093b6
RS
11117 if (short_compare)
11118 {
11119 /* Don't write &op0, etc., because that would prevent op0
11120 from being kept in a register.
11121 Instead, make copies of the our local variables and
11122 pass the copies by reference, then copy them back afterward. */
11123 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11124 enum tree_code xresultcode = resultcode;
11125 tree val
393e8e8b
MP
11126 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11127 &xresultcode);
8f17b5c5 11128
3e4093b6 11129 if (val != 0)
c9f9eb5d
AH
11130 {
11131 ret = val;
11132 goto return_build_binary_op;
11133 }
8f17b5c5 11134
3e4093b6
RS
11135 op0 = xop0, op1 = xop1;
11136 converted = 1;
11137 resultcode = xresultcode;
8f17b5c5 11138
7d882b83 11139 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11140 {
11141 bool op0_maybe_const = true;
11142 bool op1_maybe_const = true;
11143 tree orig_op0_folded, orig_op1_folded;
11144
11145 if (in_late_binary_op)
11146 {
11147 orig_op0_folded = orig_op0;
11148 orig_op1_folded = orig_op1;
11149 }
11150 else
11151 {
11152 /* Fold for the sake of possible warnings, as in
11153 build_conditional_expr. This requires the
11154 "original" values to be folded, not just op0 and
11155 op1. */
f5178456 11156 c_inhibit_evaluation_warnings++;
928c19bb
JM
11157 op0 = c_fully_fold (op0, require_constant_value,
11158 &op0_maybe_const);
11159 op1 = c_fully_fold (op1, require_constant_value,
11160 &op1_maybe_const);
f5178456 11161 c_inhibit_evaluation_warnings--;
928c19bb
JM
11162 orig_op0_folded = c_fully_fold (orig_op0,
11163 require_constant_value,
11164 NULL);
11165 orig_op1_folded = c_fully_fold (orig_op1,
11166 require_constant_value,
11167 NULL);
11168 }
11169
11170 if (warn_sign_compare)
11171 warn_for_sign_compare (location, orig_op0_folded,
11172 orig_op1_folded, op0, op1,
11173 result_type, resultcode);
5c2f94b4 11174 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11175 {
11176 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11177 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11178 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11179 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11180 }
3e4093b6 11181 }
2ad1815d 11182 }
64094f6a 11183 }
64094f6a 11184
3e4093b6
RS
11185 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11186 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11187 Then the expression will be built.
11188 It will be given type FINAL_TYPE if that is nonzero;
11189 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11190
3e4093b6
RS
11191 if (!result_type)
11192 {
ba47d38d 11193 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11194 return error_mark_node;
11195 }
400fbf9f 11196
3e4093b6 11197 if (build_type == NULL_TREE)
8ce94e44
JM
11198 {
11199 build_type = result_type;
180f8dbb
JM
11200 if ((type0 != orig_type0 || type1 != orig_type1)
11201 && !boolean_op)
8ce94e44
JM
11202 {
11203 gcc_assert (may_need_excess_precision && common);
2d2e923f 11204 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11205 }
11206 }
400fbf9f 11207
2d2e923f
MLI
11208 if (!converted)
11209 {
68fca595
MP
11210 op0 = ep_convert_and_check (location, result_type, op0,
11211 semantic_result_type);
11212 op1 = ep_convert_and_check (location, result_type, op1,
11213 semantic_result_type);
2d2e923f
MLI
11214
11215 /* This can happen if one operand has a vector type, and the other
11216 has a different type. */
11217 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11218 return error_mark_node;
11219 }
11220
f8ed5150
MP
11221 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11222 | SANITIZE_FLOAT_DIVIDE))
de5a5fa1 11223 && current_function_decl != 0
ce6923c5
MP
11224 && !lookup_attribute ("no_sanitize_undefined",
11225 DECL_ATTRIBUTES (current_function_decl))
de5a5fa1
MP
11226 && (doing_div_or_mod || doing_shift))
11227 {
11228 /* OP0 and/or OP1 might have side-effects. */
11229 op0 = c_save_expr (op0);
11230 op1 = c_save_expr (op1);
11231 op0 = c_fully_fold (op0, false, NULL);
11232 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11233 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11234 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11235 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11236 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11237 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11238 }
11239
c9f9eb5d 11240 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11241 if (int_const_or_overflow)
11242 ret = (require_constant_value
db3927fb
AH
11243 ? fold_build2_initializer_loc (location, resultcode, build_type,
11244 op0, op1)
11245 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11246 else
11247 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11248 if (final_type != 0)
11249 ret = convert (final_type, ret);
11250
11251 return_build_binary_op:
11252 gcc_assert (ret != error_mark_node);
928c19bb
JM
11253 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11254 ret = (int_operands
11255 ? note_integer_operands (ret)
11256 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11257 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11258 && !in_late_binary_op)
11259 ret = note_integer_operands (ret);
2d2e923f
MLI
11260 if (semantic_result_type)
11261 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11262 protected_set_expr_location (ret, location);
de5a5fa1 11263
a24d975c 11264 if (instrument_expr != NULL)
de5a5fa1
MP
11265 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11266 instrument_expr, ret);
11267
c9f9eb5d 11268 return ret;
400fbf9f 11269}
85498824
JM
11270
11271
11272/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11273 purpose. LOCATION is the source location for the expression. */
85498824
JM
11274
11275tree
ba47d38d 11276c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11277{
928c19bb
JM
11278 bool int_const, int_operands;
11279
85498824
JM
11280 switch (TREE_CODE (TREE_TYPE (expr)))
11281 {
11282 case ARRAY_TYPE:
ba47d38d 11283 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11284 return error_mark_node;
11285
11286 case RECORD_TYPE:
ba47d38d 11287 error_at (location, "used struct type value where scalar is required");
85498824
JM
11288 return error_mark_node;
11289
11290 case UNION_TYPE:
ba47d38d 11291 error_at (location, "used union type value where scalar is required");
85498824
JM
11292 return error_mark_node;
11293
04af8788
NP
11294 case VOID_TYPE:
11295 error_at (location, "void value not ignored as it ought to be");
11296 return error_mark_node;
11297
46bdb9cf
JM
11298 case FUNCTION_TYPE:
11299 gcc_unreachable ();
11300
d246ab4f
AS
11301 case VECTOR_TYPE:
11302 error_at (location, "used vector type where scalar is required");
11303 return error_mark_node;
11304
85498824
JM
11305 default:
11306 break;
11307 }
11308
928c19bb
JM
11309 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11310 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11311 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11312 {
11313 expr = remove_c_maybe_const_expr (expr);
11314 expr = build2 (NE_EXPR, integer_type_node, expr,
11315 convert (TREE_TYPE (expr), integer_zero_node));
11316 expr = note_integer_operands (expr);
11317 }
11318 else
11319 /* ??? Should we also give an error for vectors rather than leaving
11320 those to give errors later? */
11321 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11322
11323 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11324 {
11325 if (TREE_OVERFLOW (expr))
11326 return expr;
11327 else
11328 return note_integer_operands (expr);
11329 }
11330 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11331 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11332 return expr;
85498824 11333}
73f397d4
JM
11334\f
11335
11336/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11337 required. */
11338
11339tree
51eed280 11340c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11341{
11342 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11343 {
11344 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11345 /* Executing a compound literal inside a function reinitializes
11346 it. */
11347 if (!TREE_STATIC (decl))
11348 *se = true;
11349 return decl;
11350 }
11351 else
11352 return expr;
11353}
953ff289 11354\f
c0220ea4 11355/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11356
11357tree
11358c_begin_omp_parallel (void)
11359{
11360 tree block;
11361
11362 keep_next_level ();
11363 block = c_begin_compound_stmt (true);
11364
11365 return block;
11366}
11367
c2255bc4
AH
11368/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11369 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11370
953ff289 11371tree
c2255bc4 11372c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11373{
11374 tree stmt;
11375
c2255bc4 11376 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11377
11378 stmt = make_node (OMP_PARALLEL);
11379 TREE_TYPE (stmt) = void_type_node;
11380 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11381 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11382 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11383
11384 return add_stmt (stmt);
11385}
11386
a68ab351
JJ
11387/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11388
11389tree
11390c_begin_omp_task (void)
11391{
11392 tree block;
11393
11394 keep_next_level ();
11395 block = c_begin_compound_stmt (true);
11396
11397 return block;
11398}
11399
c2255bc4
AH
11400/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11401 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11402
11403tree
c2255bc4 11404c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11405{
11406 tree stmt;
11407
c2255bc4 11408 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11409
11410 stmt = make_node (OMP_TASK);
11411 TREE_TYPE (stmt) = void_type_node;
11412 OMP_TASK_CLAUSES (stmt) = clauses;
11413 OMP_TASK_BODY (stmt) = block;
c2255bc4 11414 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11415
11416 return add_stmt (stmt);
11417}
11418
acf0174b
JJ
11419/* Generate GOMP_cancel call for #pragma omp cancel. */
11420
11421void
11422c_finish_omp_cancel (location_t loc, tree clauses)
11423{
11424 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11425 int mask = 0;
11426 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11427 mask = 1;
11428 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11429 mask = 2;
11430 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11431 mask = 4;
11432 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11433 mask = 8;
11434 else
11435 {
11436 error_at (loc, "%<#pragma omp cancel must specify one of "
11437 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11438 "clauses");
11439 return;
11440 }
11441 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11442 if (ifc != NULL_TREE)
11443 {
11444 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11445 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11446 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11447 build_zero_cst (type));
11448 }
11449 else
11450 ifc = boolean_true_node;
11451 tree stmt = build_call_expr_loc (loc, fn, 2,
11452 build_int_cst (integer_type_node, mask),
11453 ifc);
11454 add_stmt (stmt);
11455}
11456
11457/* Generate GOMP_cancellation_point call for
11458 #pragma omp cancellation point. */
11459
11460void
11461c_finish_omp_cancellation_point (location_t loc, tree clauses)
11462{
11463 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11464 int mask = 0;
11465 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11466 mask = 1;
11467 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11468 mask = 2;
11469 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11470 mask = 4;
11471 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11472 mask = 8;
11473 else
11474 {
11475 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11476 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11477 "clauses");
11478 return;
11479 }
11480 tree stmt = build_call_expr_loc (loc, fn, 1,
11481 build_int_cst (integer_type_node, mask));
11482 add_stmt (stmt);
11483}
11484
11485/* Helper function for handle_omp_array_sections. Called recursively
11486 to handle multiple array-section-subscripts. C is the clause,
11487 T current expression (initially OMP_CLAUSE_DECL), which is either
11488 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11489 expression if specified, TREE_VALUE length expression if specified,
11490 TREE_CHAIN is what it has been specified after, or some decl.
11491 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11492 set to true if any of the array-section-subscript could have length
11493 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11494 first array-section-subscript which is known not to have length
11495 of one. Given say:
11496 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11497 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11498 all are or may have length of 1, array-section-subscript [:2] is the
11499 first one knonwn not to have length 1. For array-section-subscript
11500 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11501 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11502 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11503 case though, as some lengths could be zero. */
11504
11505static tree
11506handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11507 bool &maybe_zero_len, unsigned int &first_non_one)
11508{
11509 tree ret, low_bound, length, type;
11510 if (TREE_CODE (t) != TREE_LIST)
11511 {
7a0ca710 11512 if (error_operand_p (t))
acf0174b
JJ
11513 return error_mark_node;
11514 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11515 {
11516 if (DECL_P (t))
11517 error_at (OMP_CLAUSE_LOCATION (c),
11518 "%qD is not a variable in %qs clause", t,
11519 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11520 else
11521 error_at (OMP_CLAUSE_LOCATION (c),
11522 "%qE is not a variable in %qs clause", t,
11523 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11524 return error_mark_node;
11525 }
11526 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11527 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11528 {
11529 error_at (OMP_CLAUSE_LOCATION (c),
11530 "%qD is threadprivate variable in %qs clause", t,
11531 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11532 return error_mark_node;
11533 }
11534 return t;
11535 }
11536
11537 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11538 maybe_zero_len, first_non_one);
11539 if (ret == error_mark_node || ret == NULL_TREE)
11540 return ret;
11541
11542 type = TREE_TYPE (ret);
11543 low_bound = TREE_PURPOSE (t);
11544 length = TREE_VALUE (t);
11545
11546 if (low_bound == error_mark_node || length == error_mark_node)
11547 return error_mark_node;
11548
11549 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11550 {
11551 error_at (OMP_CLAUSE_LOCATION (c),
11552 "low bound %qE of array section does not have integral type",
11553 low_bound);
11554 return error_mark_node;
11555 }
11556 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11557 {
11558 error_at (OMP_CLAUSE_LOCATION (c),
11559 "length %qE of array section does not have integral type",
11560 length);
11561 return error_mark_node;
11562 }
11563 if (low_bound
11564 && TREE_CODE (low_bound) == INTEGER_CST
11565 && TYPE_PRECISION (TREE_TYPE (low_bound))
11566 > TYPE_PRECISION (sizetype))
11567 low_bound = fold_convert (sizetype, low_bound);
11568 if (length
11569 && TREE_CODE (length) == INTEGER_CST
11570 && TYPE_PRECISION (TREE_TYPE (length))
11571 > TYPE_PRECISION (sizetype))
11572 length = fold_convert (sizetype, length);
11573 if (low_bound == NULL_TREE)
11574 low_bound = integer_zero_node;
11575
11576 if (length != NULL_TREE)
11577 {
11578 if (!integer_nonzerop (length))
11579 maybe_zero_len = true;
11580 if (first_non_one == types.length ()
11581 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11582 first_non_one++;
11583 }
11584 if (TREE_CODE (type) == ARRAY_TYPE)
11585 {
11586 if (length == NULL_TREE
11587 && (TYPE_DOMAIN (type) == NULL_TREE
11588 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11589 {
11590 error_at (OMP_CLAUSE_LOCATION (c),
11591 "for unknown bound array type length expression must "
11592 "be specified");
11593 return error_mark_node;
11594 }
11595 if (TREE_CODE (low_bound) == INTEGER_CST
11596 && tree_int_cst_sgn (low_bound) == -1)
11597 {
11598 error_at (OMP_CLAUSE_LOCATION (c),
11599 "negative low bound in array section in %qs clause",
11600 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11601 return error_mark_node;
11602 }
11603 if (length != NULL_TREE
11604 && TREE_CODE (length) == INTEGER_CST
11605 && tree_int_cst_sgn (length) == -1)
11606 {
11607 error_at (OMP_CLAUSE_LOCATION (c),
11608 "negative length in array section in %qs clause",
11609 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11610 return error_mark_node;
11611 }
11612 if (TYPE_DOMAIN (type)
11613 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11614 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11615 == INTEGER_CST)
11616 {
11617 tree size = size_binop (PLUS_EXPR,
11618 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11619 size_one_node);
11620 if (TREE_CODE (low_bound) == INTEGER_CST)
11621 {
11622 if (tree_int_cst_lt (size, low_bound))
11623 {
11624 error_at (OMP_CLAUSE_LOCATION (c),
11625 "low bound %qE above array section size "
11626 "in %qs clause", low_bound,
11627 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11628 return error_mark_node;
11629 }
11630 if (tree_int_cst_equal (size, low_bound))
11631 maybe_zero_len = true;
11632 else if (length == NULL_TREE
11633 && first_non_one == types.length ()
11634 && tree_int_cst_equal
11635 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11636 low_bound))
11637 first_non_one++;
11638 }
11639 else if (length == NULL_TREE)
11640 {
11641 maybe_zero_len = true;
11642 if (first_non_one == types.length ())
11643 first_non_one++;
11644 }
11645 if (length && TREE_CODE (length) == INTEGER_CST)
11646 {
11647 if (tree_int_cst_lt (size, length))
11648 {
11649 error_at (OMP_CLAUSE_LOCATION (c),
11650 "length %qE above array section size "
11651 "in %qs clause", length,
11652 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11653 return error_mark_node;
11654 }
11655 if (TREE_CODE (low_bound) == INTEGER_CST)
11656 {
11657 tree lbpluslen
11658 = size_binop (PLUS_EXPR,
11659 fold_convert (sizetype, low_bound),
11660 fold_convert (sizetype, length));
11661 if (TREE_CODE (lbpluslen) == INTEGER_CST
11662 && tree_int_cst_lt (size, lbpluslen))
11663 {
11664 error_at (OMP_CLAUSE_LOCATION (c),
11665 "high bound %qE above array section size "
11666 "in %qs clause", lbpluslen,
11667 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11668 return error_mark_node;
11669 }
11670 }
11671 }
11672 }
11673 else if (length == NULL_TREE)
11674 {
11675 maybe_zero_len = true;
11676 if (first_non_one == types.length ())
11677 first_non_one++;
11678 }
11679
11680 /* For [lb:] we will need to evaluate lb more than once. */
11681 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11682 {
11683 tree lb = c_save_expr (low_bound);
11684 if (lb != low_bound)
11685 {
11686 TREE_PURPOSE (t) = lb;
11687 low_bound = lb;
11688 }
11689 }
11690 }
11691 else if (TREE_CODE (type) == POINTER_TYPE)
11692 {
11693 if (length == NULL_TREE)
11694 {
11695 error_at (OMP_CLAUSE_LOCATION (c),
11696 "for pointer type length expression must be specified");
11697 return error_mark_node;
11698 }
11699 /* If there is a pointer type anywhere but in the very first
11700 array-section-subscript, the array section can't be contiguous. */
11701 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11702 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11703 {
11704 error_at (OMP_CLAUSE_LOCATION (c),
11705 "array section is not contiguous in %qs clause",
11706 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11707 return error_mark_node;
11708 }
11709 }
11710 else
11711 {
11712 error_at (OMP_CLAUSE_LOCATION (c),
11713 "%qE does not have pointer or array type", ret);
11714 return error_mark_node;
11715 }
11716 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11717 types.safe_push (TREE_TYPE (ret));
11718 /* We will need to evaluate lb more than once. */
11719 tree lb = c_save_expr (low_bound);
11720 if (lb != low_bound)
11721 {
11722 TREE_PURPOSE (t) = lb;
11723 low_bound = lb;
11724 }
11725 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11726 return ret;
11727}
11728
11729/* Handle array sections for clause C. */
11730
11731static bool
11732handle_omp_array_sections (tree c)
11733{
11734 bool maybe_zero_len = false;
11735 unsigned int first_non_one = 0;
11736 vec<tree> types = vNULL;
11737 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11738 maybe_zero_len, first_non_one);
11739 if (first == error_mark_node)
11740 {
11741 types.release ();
11742 return true;
11743 }
11744 if (first == NULL_TREE)
11745 {
11746 types.release ();
11747 return false;
11748 }
11749 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11750 {
11751 tree t = OMP_CLAUSE_DECL (c);
11752 tree tem = NULL_TREE;
11753 types.release ();
11754 /* Need to evaluate side effects in the length expressions
11755 if any. */
11756 while (TREE_CODE (t) == TREE_LIST)
11757 {
11758 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11759 {
11760 if (tem == NULL_TREE)
11761 tem = TREE_VALUE (t);
11762 else
11763 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11764 TREE_VALUE (t), tem);
11765 }
11766 t = TREE_CHAIN (t);
11767 }
11768 if (tem)
11769 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11770 first = c_fully_fold (first, false, NULL);
11771 OMP_CLAUSE_DECL (c) = first;
11772 }
11773 else
11774 {
11775 unsigned int num = types.length (), i;
11776 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11777 tree condition = NULL_TREE;
11778
11779 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11780 maybe_zero_len = true;
11781
11782 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11783 t = TREE_CHAIN (t))
11784 {
11785 tree low_bound = TREE_PURPOSE (t);
11786 tree length = TREE_VALUE (t);
11787
11788 i--;
11789 if (low_bound
11790 && TREE_CODE (low_bound) == INTEGER_CST
11791 && TYPE_PRECISION (TREE_TYPE (low_bound))
11792 > TYPE_PRECISION (sizetype))
11793 low_bound = fold_convert (sizetype, low_bound);
11794 if (length
11795 && TREE_CODE (length) == INTEGER_CST
11796 && TYPE_PRECISION (TREE_TYPE (length))
11797 > TYPE_PRECISION (sizetype))
11798 length = fold_convert (sizetype, length);
11799 if (low_bound == NULL_TREE)
11800 low_bound = integer_zero_node;
11801 if (!maybe_zero_len && i > first_non_one)
11802 {
11803 if (integer_nonzerop (low_bound))
11804 goto do_warn_noncontiguous;
11805 if (length != NULL_TREE
11806 && TREE_CODE (length) == INTEGER_CST
11807 && TYPE_DOMAIN (types[i])
11808 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11809 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11810 == INTEGER_CST)
11811 {
11812 tree size;
11813 size = size_binop (PLUS_EXPR,
11814 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11815 size_one_node);
11816 if (!tree_int_cst_equal (length, size))
11817 {
11818 do_warn_noncontiguous:
11819 error_at (OMP_CLAUSE_LOCATION (c),
11820 "array section is not contiguous in %qs "
11821 "clause",
11822 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11823 types.release ();
11824 return true;
11825 }
11826 }
11827 if (length != NULL_TREE
11828 && TREE_SIDE_EFFECTS (length))
11829 {
11830 if (side_effects == NULL_TREE)
11831 side_effects = length;
11832 else
11833 side_effects = build2 (COMPOUND_EXPR,
11834 TREE_TYPE (side_effects),
11835 length, side_effects);
11836 }
11837 }
11838 else
11839 {
11840 tree l;
11841
11842 if (i > first_non_one && length && integer_nonzerop (length))
11843 continue;
11844 if (length)
11845 l = fold_convert (sizetype, length);
11846 else
11847 {
11848 l = size_binop (PLUS_EXPR,
11849 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11850 size_one_node);
11851 l = size_binop (MINUS_EXPR, l,
11852 fold_convert (sizetype, low_bound));
11853 }
11854 if (i > first_non_one)
11855 {
11856 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11857 size_zero_node);
11858 if (condition == NULL_TREE)
11859 condition = l;
11860 else
11861 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11862 l, condition);
11863 }
11864 else if (size == NULL_TREE)
11865 {
11866 size = size_in_bytes (TREE_TYPE (types[i]));
11867 size = size_binop (MULT_EXPR, size, l);
11868 if (condition)
11869 size = fold_build3 (COND_EXPR, sizetype, condition,
11870 size, size_zero_node);
11871 }
11872 else
11873 size = size_binop (MULT_EXPR, size, l);
11874 }
11875 }
11876 types.release ();
11877 if (side_effects)
11878 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11879 first = c_fully_fold (first, false, NULL);
11880 OMP_CLAUSE_DECL (c) = first;
11881 if (size)
11882 size = c_fully_fold (size, false, NULL);
11883 OMP_CLAUSE_SIZE (c) = size;
11884 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11885 return false;
11886 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11887 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11888 if (!c_mark_addressable (t))
11889 return false;
11890 OMP_CLAUSE_DECL (c2) = t;
11891 t = build_fold_addr_expr (first);
11892 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11893 tree ptr = OMP_CLAUSE_DECL (c2);
11894 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11895 ptr = build_fold_addr_expr (ptr);
11896 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11897 ptrdiff_type_node, t,
11898 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11899 ptrdiff_type_node, ptr));
11900 t = c_fully_fold (t, false, NULL);
11901 OMP_CLAUSE_SIZE (c2) = t;
11902 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11903 OMP_CLAUSE_CHAIN (c) = c2;
11904 }
11905 return false;
11906}
11907
11908/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11909 an inline call. But, remap
11910 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11911 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11912
11913static tree
11914c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11915 tree decl, tree placeholder)
11916{
11917 copy_body_data id;
b787e7a2 11918 hash_map<tree, tree> decl_map;
acf0174b 11919
b787e7a2
TS
11920 decl_map.put (omp_decl1, placeholder);
11921 decl_map.put (omp_decl2, decl);
acf0174b
JJ
11922 memset (&id, 0, sizeof (id));
11923 id.src_fn = DECL_CONTEXT (omp_decl1);
11924 id.dst_fn = current_function_decl;
11925 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 11926 id.decl_map = &decl_map;
acf0174b
JJ
11927
11928 id.copy_decl = copy_decl_no_change;
11929 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11930 id.transform_new_cfg = true;
11931 id.transform_return_to_modify = false;
11932 id.transform_lang_insert_block = NULL;
11933 id.eh_lp_nr = 0;
11934 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
11935 return stmt;
11936}
11937
11938/* Helper function of c_finish_omp_clauses, called via walk_tree.
11939 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11940
11941static tree
11942c_find_omp_placeholder_r (tree *tp, int *, void *data)
11943{
11944 if (*tp == (tree) data)
11945 return *tp;
11946 return NULL_TREE;
11947}
11948
953ff289
DN
11949/* For all elements of CLAUSES, validate them vs OpenMP constraints.
11950 Remove any elements from the list that are invalid. */
11951
11952tree
11953c_finish_omp_clauses (tree clauses)
11954{
11955 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 11956 bitmap_head aligned_head;
f3316c6d 11957 tree c, t, *pc;
acf0174b
JJ
11958 bool branch_seen = false;
11959 bool copyprivate_seen = false;
11960 tree *nowait_clause = NULL;
953ff289
DN
11961
11962 bitmap_obstack_initialize (NULL);
11963 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11964 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11965 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 11966 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
11967
11968 for (pc = &clauses, c = clauses; c ; c = *pc)
11969 {
11970 bool remove = false;
11971 bool need_complete = false;
11972 bool need_implicitly_determined = false;
11973
aaf46ef9 11974 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
11975 {
11976 case OMP_CLAUSE_SHARED:
953ff289
DN
11977 need_implicitly_determined = true;
11978 goto check_dup_generic;
11979
11980 case OMP_CLAUSE_PRIVATE:
953ff289
DN
11981 need_complete = true;
11982 need_implicitly_determined = true;
11983 goto check_dup_generic;
11984
11985 case OMP_CLAUSE_REDUCTION:
953ff289
DN
11986 need_implicitly_determined = true;
11987 t = OMP_CLAUSE_DECL (c);
acf0174b 11988 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
652fea39
JJ
11989 && (FLOAT_TYPE_P (TREE_TYPE (t))
11990 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
953ff289
DN
11991 {
11992 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11993 const char *r_name = NULL;
11994
11995 switch (r_code)
11996 {
11997 case PLUS_EXPR:
11998 case MULT_EXPR:
11999 case MINUS_EXPR:
652fea39 12000 break;
20906c66 12001 case MIN_EXPR:
652fea39
JJ
12002 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12003 r_name = "min";
12004 break;
20906c66 12005 case MAX_EXPR:
652fea39
JJ
12006 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12007 r_name = "max";
953ff289
DN
12008 break;
12009 case BIT_AND_EXPR:
12010 r_name = "&";
12011 break;
12012 case BIT_XOR_EXPR:
12013 r_name = "^";
12014 break;
12015 case BIT_IOR_EXPR:
12016 r_name = "|";
12017 break;
12018 case TRUTH_ANDIF_EXPR:
652fea39
JJ
12019 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12020 r_name = "&&";
953ff289
DN
12021 break;
12022 case TRUTH_ORIF_EXPR:
652fea39
JJ
12023 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12024 r_name = "||";
953ff289
DN
12025 break;
12026 default:
12027 gcc_unreachable ();
12028 }
12029 if (r_name)
12030 {
c2255bc4
AH
12031 error_at (OMP_CLAUSE_LOCATION (c),
12032 "%qE has invalid type for %<reduction(%s)%>",
12033 t, r_name);
953ff289 12034 remove = true;
ee1d5a02 12035 break;
953ff289
DN
12036 }
12037 }
acf0174b
JJ
12038 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12039 {
12040 error_at (OMP_CLAUSE_LOCATION (c),
12041 "user defined reduction not found for %qD", t);
12042 remove = true;
ee1d5a02 12043 break;
acf0174b
JJ
12044 }
12045 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12046 {
12047 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12048 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12049 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12050 VAR_DECL, NULL_TREE, type);
12051 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12052 DECL_ARTIFICIAL (placeholder) = 1;
12053 DECL_IGNORED_P (placeholder) = 1;
12054 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12055 c_mark_addressable (placeholder);
12056 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12057 c_mark_addressable (OMP_CLAUSE_DECL (c));
12058 OMP_CLAUSE_REDUCTION_MERGE (c)
12059 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12060 TREE_VEC_ELT (list, 0),
12061 TREE_VEC_ELT (list, 1),
12062 OMP_CLAUSE_DECL (c), placeholder);
12063 OMP_CLAUSE_REDUCTION_MERGE (c)
12064 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12065 void_type_node, NULL_TREE,
12066 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12067 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12068 if (TREE_VEC_LENGTH (list) == 6)
12069 {
12070 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12071 c_mark_addressable (OMP_CLAUSE_DECL (c));
12072 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12073 c_mark_addressable (placeholder);
12074 tree init = TREE_VEC_ELT (list, 5);
12075 if (init == error_mark_node)
12076 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12077 OMP_CLAUSE_REDUCTION_INIT (c)
12078 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12079 TREE_VEC_ELT (list, 3),
12080 OMP_CLAUSE_DECL (c), placeholder);
12081 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12082 OMP_CLAUSE_REDUCTION_INIT (c)
12083 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12084 OMP_CLAUSE_REDUCTION_INIT (c));
12085 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12086 c_find_omp_placeholder_r,
12087 placeholder, NULL))
12088 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12089 }
12090 else
12091 {
12092 tree init;
12093 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12094 init = build_constructor (TREE_TYPE (t), NULL);
12095 else
12096 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12097 OMP_CLAUSE_REDUCTION_INIT (c)
12098 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12099 }
12100 OMP_CLAUSE_REDUCTION_INIT (c)
12101 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12102 void_type_node, NULL_TREE,
12103 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12104 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12105 }
953ff289
DN
12106 goto check_dup_generic;
12107
12108 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12109 copyprivate_seen = true;
12110 if (nowait_clause)
12111 {
12112 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12113 "%<nowait%> clause must not be used together "
12114 "with %<copyprivate%>");
12115 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12116 nowait_clause = NULL;
12117 }
953ff289
DN
12118 goto check_dup_generic;
12119
12120 case OMP_CLAUSE_COPYIN:
953ff289
DN
12121 t = OMP_CLAUSE_DECL (c);
12122 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12123 {
c2255bc4
AH
12124 error_at (OMP_CLAUSE_LOCATION (c),
12125 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12126 remove = true;
ee1d5a02 12127 break;
953ff289
DN
12128 }
12129 goto check_dup_generic;
12130
acf0174b
JJ
12131 case OMP_CLAUSE_LINEAR:
12132 t = OMP_CLAUSE_DECL (c);
12133 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12134 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12135 {
12136 error_at (OMP_CLAUSE_LOCATION (c),
12137 "linear clause applied to non-integral non-pointer "
12138 "variable with type %qT", TREE_TYPE (t));
12139 remove = true;
12140 break;
12141 }
12142 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12143 {
12144 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12145 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12146 OMP_CLAUSE_DECL (c), s);
12147 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12148 sizetype, s, OMP_CLAUSE_DECL (c));
12149 if (s == error_mark_node)
12150 s = size_one_node;
12151 OMP_CLAUSE_LINEAR_STEP (c) = s;
12152 }
da6f124d
JJ
12153 else
12154 OMP_CLAUSE_LINEAR_STEP (c)
12155 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12156 goto check_dup_generic;
12157
953ff289
DN
12158 check_dup_generic:
12159 t = OMP_CLAUSE_DECL (c);
12160 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12161 {
c2255bc4 12162 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12163 "%qE is not a variable in clause %qs", t,
12164 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12165 remove = true;
12166 }
12167 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12168 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12169 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12170 {
c2255bc4
AH
12171 error_at (OMP_CLAUSE_LOCATION (c),
12172 "%qE appears more than once in data clauses", t);
953ff289
DN
12173 remove = true;
12174 }
12175 else
12176 bitmap_set_bit (&generic_head, DECL_UID (t));
12177 break;
12178
12179 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12180 t = OMP_CLAUSE_DECL (c);
12181 need_complete = true;
12182 need_implicitly_determined = true;
12183 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12184 {
c2255bc4
AH
12185 error_at (OMP_CLAUSE_LOCATION (c),
12186 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12187 remove = true;
12188 }
12189 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12190 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12191 {
c2255bc4
AH
12192 error_at (OMP_CLAUSE_LOCATION (c),
12193 "%qE appears more than once in data clauses", t);
953ff289
DN
12194 remove = true;
12195 }
12196 else
12197 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12198 break;
12199
12200 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12201 t = OMP_CLAUSE_DECL (c);
12202 need_complete = true;
12203 need_implicitly_determined = true;
12204 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12205 {
c2255bc4
AH
12206 error_at (OMP_CLAUSE_LOCATION (c),
12207 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12208 remove = true;
12209 }
12210 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12211 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12212 {
c2255bc4
AH
12213 error_at (OMP_CLAUSE_LOCATION (c),
12214 "%qE appears more than once in data clauses", t);
953ff289
DN
12215 remove = true;
12216 }
12217 else
12218 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12219 break;
12220
acf0174b
JJ
12221 case OMP_CLAUSE_ALIGNED:
12222 t = OMP_CLAUSE_DECL (c);
12223 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12224 {
12225 error_at (OMP_CLAUSE_LOCATION (c),
12226 "%qE is not a variable in %<aligned%> clause", t);
12227 remove = true;
12228 }
5a9785fb
JJ
12229 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12230 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12231 {
12232 error_at (OMP_CLAUSE_LOCATION (c),
12233 "%qE in %<aligned%> clause is neither a pointer nor "
12234 "an array", t);
12235 remove = true;
12236 }
acf0174b
JJ
12237 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12238 {
12239 error_at (OMP_CLAUSE_LOCATION (c),
12240 "%qE appears more than once in %<aligned%> clauses",
12241 t);
12242 remove = true;
12243 }
12244 else
12245 bitmap_set_bit (&aligned_head, DECL_UID (t));
12246 break;
12247
12248 case OMP_CLAUSE_DEPEND:
12249 t = OMP_CLAUSE_DECL (c);
12250 if (TREE_CODE (t) == TREE_LIST)
12251 {
12252 if (handle_omp_array_sections (c))
12253 remove = true;
12254 break;
12255 }
12256 if (t == error_mark_node)
12257 remove = true;
12258 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12259 {
12260 error_at (OMP_CLAUSE_LOCATION (c),
12261 "%qE is not a variable in %<depend%> clause", t);
12262 remove = true;
12263 }
12264 else if (!c_mark_addressable (t))
12265 remove = true;
12266 break;
12267
12268 case OMP_CLAUSE_MAP:
12269 case OMP_CLAUSE_TO:
12270 case OMP_CLAUSE_FROM:
12271 t = OMP_CLAUSE_DECL (c);
12272 if (TREE_CODE (t) == TREE_LIST)
12273 {
12274 if (handle_omp_array_sections (c))
12275 remove = true;
12276 else
12277 {
12278 t = OMP_CLAUSE_DECL (c);
b17a8b07 12279 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12280 {
12281 error_at (OMP_CLAUSE_LOCATION (c),
12282 "array section does not have mappable type "
12283 "in %qs clause",
12284 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12285 remove = true;
12286 }
12287 }
12288 break;
12289 }
12290 if (t == error_mark_node)
12291 remove = true;
12292 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12293 {
12294 error_at (OMP_CLAUSE_LOCATION (c),
12295 "%qE is not a variable in %qs clause", t,
12296 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12297 remove = true;
12298 }
12299 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12300 {
12301 error_at (OMP_CLAUSE_LOCATION (c),
12302 "%qD is threadprivate variable in %qs clause", t,
12303 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12304 remove = true;
12305 }
12306 else if (!c_mark_addressable (t))
12307 remove = true;
b17a8b07
TS
12308 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12309 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12310 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12311 {
12312 error_at (OMP_CLAUSE_LOCATION (c),
12313 "%qD does not have a mappable type in %qs clause", t,
12314 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12315 remove = true;
12316 }
12317 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12318 {
12319 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12320 error ("%qD appears more than once in motion clauses", t);
12321 else
12322 error ("%qD appears more than once in map clauses", t);
12323 remove = true;
12324 }
12325 else
12326 bitmap_set_bit (&generic_head, DECL_UID (t));
12327 break;
12328
12329 case OMP_CLAUSE_UNIFORM:
12330 t = OMP_CLAUSE_DECL (c);
12331 if (TREE_CODE (t) != PARM_DECL)
12332 {
12333 if (DECL_P (t))
12334 error_at (OMP_CLAUSE_LOCATION (c),
12335 "%qD is not an argument in %<uniform%> clause", t);
12336 else
12337 error_at (OMP_CLAUSE_LOCATION (c),
12338 "%qE is not an argument in %<uniform%> clause", t);
12339 remove = true;
ee1d5a02 12340 break;
acf0174b 12341 }
ee1d5a02 12342 goto check_dup_generic;
acf0174b
JJ
12343
12344 case OMP_CLAUSE_NOWAIT:
12345 if (copyprivate_seen)
12346 {
12347 error_at (OMP_CLAUSE_LOCATION (c),
12348 "%<nowait%> clause must not be used together "
12349 "with %<copyprivate%>");
12350 remove = true;
12351 break;
12352 }
12353 nowait_clause = pc;
12354 pc = &OMP_CLAUSE_CHAIN (c);
12355 continue;
12356
953ff289
DN
12357 case OMP_CLAUSE_IF:
12358 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12359 case OMP_CLAUSE_NUM_TEAMS:
12360 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12361 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12362 case OMP_CLAUSE_ORDERED:
12363 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12364 case OMP_CLAUSE_UNTIED:
12365 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12366 case OMP_CLAUSE_FINAL:
12367 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12368 case OMP_CLAUSE_SAFELEN:
12369 case OMP_CLAUSE_SIMDLEN:
12370 case OMP_CLAUSE_DEVICE:
12371 case OMP_CLAUSE_DIST_SCHEDULE:
12372 case OMP_CLAUSE_PARALLEL:
12373 case OMP_CLAUSE_FOR:
12374 case OMP_CLAUSE_SECTIONS:
12375 case OMP_CLAUSE_TASKGROUP:
12376 case OMP_CLAUSE_PROC_BIND:
9a771876 12377 case OMP_CLAUSE__CILK_FOR_COUNT_:
acf0174b
JJ
12378 pc = &OMP_CLAUSE_CHAIN (c);
12379 continue;
12380
12381 case OMP_CLAUSE_INBRANCH:
12382 case OMP_CLAUSE_NOTINBRANCH:
12383 if (branch_seen)
12384 {
12385 error_at (OMP_CLAUSE_LOCATION (c),
12386 "%<inbranch%> clause is incompatible with "
12387 "%<notinbranch%>");
12388 remove = true;
12389 break;
12390 }
12391 branch_seen = true;
953ff289
DN
12392 pc = &OMP_CLAUSE_CHAIN (c);
12393 continue;
12394
12395 default:
12396 gcc_unreachable ();
12397 }
12398
12399 if (!remove)
12400 {
12401 t = OMP_CLAUSE_DECL (c);
12402
12403 if (need_complete)
12404 {
12405 t = require_complete_type (t);
12406 if (t == error_mark_node)
12407 remove = true;
12408 }
12409
12410 if (need_implicitly_determined)
12411 {
12412 const char *share_name = NULL;
12413
12414 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12415 share_name = "threadprivate";
12416 else switch (c_omp_predetermined_sharing (t))
12417 {
12418 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12419 break;
12420 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12421 /* const vars may be specified in firstprivate clause. */
12422 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12423 && TREE_READONLY (t))
12424 break;
953ff289
DN
12425 share_name = "shared";
12426 break;
12427 case OMP_CLAUSE_DEFAULT_PRIVATE:
12428 share_name = "private";
12429 break;
12430 default:
12431 gcc_unreachable ();
12432 }
12433 if (share_name)
12434 {
c2255bc4
AH
12435 error_at (OMP_CLAUSE_LOCATION (c),
12436 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12437 t, share_name,
12438 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12439 remove = true;
12440 }
12441 }
12442 }
12443
12444 if (remove)
12445 *pc = OMP_CLAUSE_CHAIN (c);
12446 else
12447 pc = &OMP_CLAUSE_CHAIN (c);
12448 }
12449
12450 bitmap_obstack_release (NULL);
12451 return clauses;
12452}
9ae165a0 12453
0a35513e
AH
12454/* Create a transaction node. */
12455
12456tree
12457c_finish_transaction (location_t loc, tree block, int flags)
12458{
12459 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12460 if (flags & TM_STMT_ATTR_OUTER)
12461 TRANSACTION_EXPR_OUTER (stmt) = 1;
12462 if (flags & TM_STMT_ATTR_RELAXED)
12463 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12464 return add_stmt (stmt);
12465}
12466
9ae165a0
DG
12467/* Make a variant type in the proper way for C/C++, propagating qualifiers
12468 down to the element type of an array. */
12469
12470tree
12471c_build_qualified_type (tree type, int type_quals)
12472{
12473 if (type == error_mark_node)
12474 return type;
12475
12476 if (TREE_CODE (type) == ARRAY_TYPE)
12477 {
12478 tree t;
12479 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12480 type_quals);
12481
12482 /* See if we already have an identically qualified type. */
12483 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12484 {
12485 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12486 && TYPE_NAME (t) == TYPE_NAME (type)
12487 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12488 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12489 TYPE_ATTRIBUTES (type)))
12490 break;
12491 }
12492 if (!t)
12493 {
12494 tree domain = TYPE_DOMAIN (type);
12495
12496 t = build_variant_type_copy (type);
12497 TREE_TYPE (t) = element_type;
12498
12499 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12500 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12501 SET_TYPE_STRUCTURAL_EQUALITY (t);
12502 else if (TYPE_CANONICAL (element_type) != element_type
12503 || (domain && TYPE_CANONICAL (domain) != domain))
12504 {
b8698a0f 12505 tree unqualified_canon
9ae165a0 12506 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12507 domain? TYPE_CANONICAL (domain)
9ae165a0 12508 : NULL_TREE);
b8698a0f 12509 TYPE_CANONICAL (t)
9ae165a0
DG
12510 = c_build_qualified_type (unqualified_canon, type_quals);
12511 }
12512 else
12513 TYPE_CANONICAL (t) = t;
12514 }
12515 return t;
12516 }
12517
12518 /* A restrict-qualified pointer type must be a pointer to object or
12519 incomplete type. Note that the use of POINTER_TYPE_P also allows
12520 REFERENCE_TYPEs, which is appropriate for C++. */
12521 if ((type_quals & TYPE_QUAL_RESTRICT)
12522 && (!POINTER_TYPE_P (type)
12523 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12524 {
12525 error ("invalid use of %<restrict%>");
12526 type_quals &= ~TYPE_QUAL_RESTRICT;
12527 }
12528
12529 return build_qualified_type (type, type_quals);
12530}
72b5577d
ILT
12531
12532/* Build a VA_ARG_EXPR for the C parser. */
12533
12534tree
c2255bc4 12535c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d
ILT
12536{
12537 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12538 warning_at (loc, OPT_Wc___compat,
12539 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12540 return build_va_arg (loc, expr, type);
72b5577d 12541}
acf0174b
JJ
12542
12543/* Return truthvalue of whether T1 is the same tree structure as T2.
12544 Return 1 if they are the same. Return 0 if they are different. */
12545
12546bool
12547c_tree_equal (tree t1, tree t2)
12548{
12549 enum tree_code code1, code2;
12550
12551 if (t1 == t2)
12552 return true;
12553 if (!t1 || !t2)
12554 return false;
12555
12556 for (code1 = TREE_CODE (t1);
12557 CONVERT_EXPR_CODE_P (code1)
12558 || code1 == NON_LVALUE_EXPR;
12559 code1 = TREE_CODE (t1))
12560 t1 = TREE_OPERAND (t1, 0);
12561 for (code2 = TREE_CODE (t2);
12562 CONVERT_EXPR_CODE_P (code2)
12563 || code2 == NON_LVALUE_EXPR;
12564 code2 = TREE_CODE (t2))
12565 t2 = TREE_OPERAND (t2, 0);
12566
12567 /* They might have become equal now. */
12568 if (t1 == t2)
12569 return true;
12570
12571 if (code1 != code2)
12572 return false;
12573
12574 switch (code1)
12575 {
12576 case INTEGER_CST:
807e902e 12577 return wi::eq_p (t1, t2);
acf0174b
JJ
12578
12579 case REAL_CST:
12580 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12581
12582 case STRING_CST:
12583 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12584 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12585 TREE_STRING_LENGTH (t1));
12586
12587 case FIXED_CST:
12588 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12589 TREE_FIXED_CST (t2));
12590
12591 case COMPLEX_CST:
12592 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12593 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12594
12595 case VECTOR_CST:
12596 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12597
12598 case CONSTRUCTOR:
12599 /* We need to do this when determining whether or not two
12600 non-type pointer to member function template arguments
12601 are the same. */
12602 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12603 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12604 return false;
12605 {
12606 tree field, value;
12607 unsigned int i;
12608 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12609 {
12610 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12611 if (!c_tree_equal (field, elt2->index)
12612 || !c_tree_equal (value, elt2->value))
12613 return false;
12614 }
12615 }
12616 return true;
12617
12618 case TREE_LIST:
12619 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12620 return false;
12621 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12622 return false;
12623 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12624
12625 case SAVE_EXPR:
12626 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12627
12628 case CALL_EXPR:
12629 {
12630 tree arg1, arg2;
12631 call_expr_arg_iterator iter1, iter2;
12632 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12633 return false;
12634 for (arg1 = first_call_expr_arg (t1, &iter1),
12635 arg2 = first_call_expr_arg (t2, &iter2);
12636 arg1 && arg2;
12637 arg1 = next_call_expr_arg (&iter1),
12638 arg2 = next_call_expr_arg (&iter2))
12639 if (!c_tree_equal (arg1, arg2))
12640 return false;
12641 if (arg1 || arg2)
12642 return false;
12643 return true;
12644 }
12645
12646 case TARGET_EXPR:
12647 {
12648 tree o1 = TREE_OPERAND (t1, 0);
12649 tree o2 = TREE_OPERAND (t2, 0);
12650
12651 /* Special case: if either target is an unallocated VAR_DECL,
12652 it means that it's going to be unified with whatever the
12653 TARGET_EXPR is really supposed to initialize, so treat it
12654 as being equivalent to anything. */
12655 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12656 && !DECL_RTL_SET_P (o1))
12657 /*Nop*/;
12658 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12659 && !DECL_RTL_SET_P (o2))
12660 /*Nop*/;
12661 else if (!c_tree_equal (o1, o2))
12662 return false;
12663
12664 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12665 }
12666
12667 case COMPONENT_REF:
12668 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12669 return false;
12670 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12671
12672 case PARM_DECL:
12673 case VAR_DECL:
12674 case CONST_DECL:
12675 case FIELD_DECL:
12676 case FUNCTION_DECL:
12677 case IDENTIFIER_NODE:
12678 case SSA_NAME:
12679 return false;
12680
12681 case TREE_VEC:
12682 {
12683 unsigned ix;
12684 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12685 return false;
12686 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12687 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12688 TREE_VEC_ELT (t2, ix)))
12689 return false;
12690 return true;
12691 }
12692
12693 default:
12694 break;
12695 }
12696
12697 switch (TREE_CODE_CLASS (code1))
12698 {
12699 case tcc_unary:
12700 case tcc_binary:
12701 case tcc_comparison:
12702 case tcc_expression:
12703 case tcc_vl_exp:
12704 case tcc_reference:
12705 case tcc_statement:
12706 {
12707 int i, n = TREE_OPERAND_LENGTH (t1);
12708
12709 switch (code1)
12710 {
12711 case PREINCREMENT_EXPR:
12712 case PREDECREMENT_EXPR:
12713 case POSTINCREMENT_EXPR:
12714 case POSTDECREMENT_EXPR:
12715 n = 1;
12716 break;
12717 case ARRAY_REF:
12718 n = 2;
12719 break;
12720 default:
12721 break;
12722 }
12723
12724 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12725 && n != TREE_OPERAND_LENGTH (t2))
12726 return false;
12727
12728 for (i = 0; i < n; ++i)
12729 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12730 return false;
12731
12732 return true;
12733 }
12734
12735 case tcc_type:
12736 return comptypes (t1, t2);
12737 default:
12738 gcc_unreachable ();
12739 }
12740 /* We can get here with --disable-checking. */
12741 return false;
12742}
12893402
BI
12743
12744/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12745 spawn-helper and BODY is the newly created body for FNDECL. */
12746
12747void
12748cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12749{
12750 tree list = alloc_stmt_list ();
12751 tree frame = make_cilk_frame (fndecl);
12752 tree dtor = create_cilk_function_exit (frame, false, true);
12753 add_local_decl (cfun, frame);
12754
12755 DECL_SAVED_TREE (fndecl) = list;
12756 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12757 frame);
12758 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12759 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12760
12761 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12762 append_to_statement_list (detach_expr, &body_list);
12763
12764 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12765 body = fold_build_cleanup_point_expr (void_type_node, body);
12766
12767 append_to_statement_list (body, &body_list);
12768 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12769 body_list, dtor), &list);
12770}