]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
re PR middle-end/64465 (internal compiler error: verify_flow_info failed)
[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"
400fbf9f 30#include "tree.h"
d8a2d370
DN
31#include "stor-layout.h"
32#include "trans-mem.h"
33#include "varasm.h"
34#include "stmt.h"
e57e265b 35#include "langhooks.h"
400fbf9f 36#include "c-tree.h"
29cc57cf 37#include "c-lang.h"
400fbf9f 38#include "flags.h"
ab87f8c8 39#include "intl.h"
672a6f42 40#include "target.h"
325c3691 41#include "tree-iterator.h"
6662d794 42#include "bitmap.h"
60393bbc
AM
43#include "predict.h"
44#include "vec.h"
45#include "hashtab.h"
46#include "hash-set.h"
47#include "machmode.h"
48#include "hard-reg-set.h"
49#include "input.h"
50#include "function.h"
2fb9a547 51#include "gimple-expr.h"
45b0be94 52#include "gimplify.h"
acf0174b 53#include "tree-inline.h"
0645c1a2 54#include "omp-low.h"
61d3ce20 55#include "c-family/c-objc.h"
a212e43f 56#include "c-family/c-common.h"
de5a5fa1 57#include "c-family/c-ubsan.h"
12893402 58#include "cilk.h"
807e902e 59#include "wide-int.h"
325c3691 60
2ac2f164
JM
61/* Possible cases of implicit bad conversions. Used to select
62 diagnostic messages in convert_for_assignment. */
63enum impl_conv {
64 ic_argpass,
65 ic_assign,
66 ic_init,
67 ic_return
68};
69
bc4b653b
JM
70/* The level of nesting inside "__alignof__". */
71int in_alignof;
72
73/* The level of nesting inside "sizeof". */
74int in_sizeof;
75
76/* The level of nesting inside "typeof". */
77int in_typeof;
400fbf9f 78
1a4049e7
JJ
79/* The argument of last parsed sizeof expression, only to be tested
80 if expr.original_code == SIZEOF_EXPR. */
81tree c_last_sizeof_arg;
82
9bac5cbb
G
83/* Nonzero if we might need to print a "missing braces around
84 initializer" message within this initializer. */
85static int found_missing_braces;
103b7b17 86
bf730f15
RS
87static int require_constant_value;
88static int require_constant_elements;
89
58f9752a 90static bool null_pointer_constant_p (const_tree);
f55ade6e 91static tree qualify_type (tree, tree);
dc5027f4
JM
92static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
93 bool *);
744aa42f 94static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
95static int function_types_compatible_p (const_tree, const_tree, bool *,
96 bool *);
97static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 98static tree lookup_field (tree, tree);
81e5eca8
MP
99static int convert_arguments (location_t, vec<location_t>, tree,
100 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
101 tree);
db3927fb 102static tree pointer_diff (location_t, tree, tree);
68fca595 103static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 104 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
105static tree valid_compound_expr_initializer (tree, tree);
106static void push_string (const char *);
107static void push_member_name (tree);
f55ade6e
AJ
108static int spelling_length (void);
109static char *print_spelling (char *);
96b40f8d 110static void warning_init (location_t, int, const char *);
c2255bc4 111static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
112static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
113 bool, struct obstack *);
a1e3b3d9 114static void output_pending_init_elements (int, struct obstack *);
ea58ef42 115static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 116static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
117static void add_pending_init (location_t, tree, tree, tree, bool,
118 struct obstack *);
a1e3b3d9
LB
119static void set_nonincremental_init (struct obstack *);
120static void set_nonincremental_init_from_string (tree, struct obstack *);
121static tree find_init_member (tree, struct obstack *);
f37acdf9 122static void readonly_warning (tree, enum lvalue_use);
7bd11157 123static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 124static void record_maybe_used_decl (tree);
dc5027f4 125static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
126\f
127/* Return true if EXP is a null pointer constant, false otherwise. */
128
129static bool
58f9752a 130null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
131{
132 /* This should really operate on c_expr structures, but they aren't
133 yet available everywhere required. */
134 tree type = TREE_TYPE (expr);
135 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 136 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
137 && integer_zerop (expr)
138 && (INTEGRAL_TYPE_P (type)
139 || (TREE_CODE (type) == POINTER_TYPE
140 && VOID_TYPE_P (TREE_TYPE (type))
141 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
142}
928c19bb
JM
143
144/* EXPR may appear in an unevaluated part of an integer constant
145 expression, but not in an evaluated part. Wrap it in a
146 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
147 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
148
149static tree
150note_integer_operands (tree expr)
151{
152 tree ret;
153 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
154 {
155 ret = copy_node (expr);
156 TREE_OVERFLOW (ret) = 1;
157 }
158 else
159 {
160 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
161 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
162 }
163 return ret;
164}
165
4d84fe7c
JM
166/* Having checked whether EXPR may appear in an unevaluated part of an
167 integer constant expression and found that it may, remove any
168 C_MAYBE_CONST_EXPR noting this fact and return the resulting
169 expression. */
170
171static inline tree
172remove_c_maybe_const_expr (tree expr)
173{
174 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
175 return C_MAYBE_CONST_EXPR_EXPR (expr);
176 else
177 return expr;
178}
179
f13c9b2c
AP
180\f/* This is a cache to hold if two types are compatible or not. */
181
182struct tagged_tu_seen_cache {
183 const struct tagged_tu_seen_cache * next;
58f9752a
KG
184 const_tree t1;
185 const_tree t2;
f13c9b2c
AP
186 /* The return value of tagged_types_tu_compatible_p if we had seen
187 these two types already. */
188 int val;
189};
190
191static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
192static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
193
400fbf9f
JW
194/* Do `exp = require_complete_type (exp);' to make sure exp
195 does not have an incomplete type. (That includes void types.) */
196
197tree
2f6e4e97 198require_complete_type (tree value)
400fbf9f
JW
199{
200 tree type = TREE_TYPE (value);
201
7a0ca710 202 if (error_operand_p (value))
ea0f786b
CB
203 return error_mark_node;
204
400fbf9f 205 /* First, detect a valid value with a complete type. */
d0f062fb 206 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
207 return value;
208
7a228918 209 c_incomplete_type_error (value, type);
400fbf9f
JW
210 return error_mark_node;
211}
212
213/* Print an error message for invalid use of an incomplete type.
214 VALUE is the expression that was used (or 0 if that isn't known)
215 and TYPE is the type that was invalid. */
216
217void
ac7d7749 218c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 219{
5d5993dd 220 const char *type_code_string;
400fbf9f
JW
221
222 /* Avoid duplicate error message. */
223 if (TREE_CODE (type) == ERROR_MARK)
224 return;
225
226 if (value != 0 && (TREE_CODE (value) == VAR_DECL
227 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 228 error ("%qD has an incomplete type", value);
400fbf9f
JW
229 else
230 {
231 retry:
232 /* We must print an error message. Be clever about what it says. */
233
234 switch (TREE_CODE (type))
235 {
236 case RECORD_TYPE:
ab87f8c8 237 type_code_string = "struct";
400fbf9f
JW
238 break;
239
240 case UNION_TYPE:
ab87f8c8 241 type_code_string = "union";
400fbf9f
JW
242 break;
243
244 case ENUMERAL_TYPE:
ab87f8c8 245 type_code_string = "enum";
400fbf9f
JW
246 break;
247
248 case VOID_TYPE:
249 error ("invalid use of void expression");
250 return;
251
252 case ARRAY_TYPE:
253 if (TYPE_DOMAIN (type))
254 {
fba78abb
RH
255 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
256 {
257 error ("invalid use of flexible array member");
258 return;
259 }
400fbf9f
JW
260 type = TREE_TYPE (type);
261 goto retry;
262 }
263 error ("invalid use of array with unspecified bounds");
264 return;
265
266 default:
366de0ce 267 gcc_unreachable ();
400fbf9f
JW
268 }
269
270 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
271 error ("invalid use of undefined type %<%s %E%>",
272 type_code_string, TYPE_NAME (type));
400fbf9f
JW
273 else
274 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 275 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
276 }
277}
278
ab393bf1
NB
279/* Given a type, apply default promotions wrt unnamed function
280 arguments and return the new type. */
281
282tree
2f6e4e97 283c_type_promotes_to (tree type)
ab393bf1 284{
267bac10 285 tree ret = NULL_TREE;
ab393bf1 286
267bac10
JM
287 if (TYPE_MAIN_VARIANT (type) == float_type_node)
288 ret = double_type_node;
289 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
290 {
291 /* Preserve unsignedness if not really getting any wider. */
8df83eae 292 if (TYPE_UNSIGNED (type)
c22cacf3 293 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
294 ret = unsigned_type_node;
295 else
296 ret = integer_type_node;
ab393bf1
NB
297 }
298
267bac10
JM
299 if (ret != NULL_TREE)
300 return (TYPE_ATOMIC (type)
301 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
302 : ret);
303
ab393bf1
NB
304 return type;
305}
306
36c5e70a
BE
307/* Return true if between two named address spaces, whether there is a superset
308 named address space that encompasses both address spaces. If there is a
309 superset, return which address space is the superset. */
310
311static bool
312addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
313{
314 if (as1 == as2)
315 {
316 *common = as1;
317 return true;
318 }
319 else if (targetm.addr_space.subset_p (as1, as2))
320 {
321 *common = as2;
322 return true;
323 }
324 else if (targetm.addr_space.subset_p (as2, as1))
325 {
326 *common = as1;
327 return true;
328 }
329 else
330 return false;
331}
332
400fbf9f
JW
333/* Return a variant of TYPE which has all the type qualifiers of LIKE
334 as well as those of TYPE. */
335
336static tree
2f6e4e97 337qualify_type (tree type, tree like)
400fbf9f 338{
36c5e70a
BE
339 addr_space_t as_type = TYPE_ADDR_SPACE (type);
340 addr_space_t as_like = TYPE_ADDR_SPACE (like);
341 addr_space_t as_common;
342
343 /* If the two named address spaces are different, determine the common
344 superset address space. If there isn't one, raise an error. */
345 if (!addr_space_superset (as_type, as_like, &as_common))
346 {
347 as_common = as_type;
348 error ("%qT and %qT are in disjoint named address spaces",
349 type, like);
350 }
351
2f6e4e97 352 return c_build_qualified_type (type,
36c5e70a 353 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 354 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 355 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 356}
52ffd86e
MS
357
358/* Return true iff the given tree T is a variable length array. */
359
360bool
ac7d7749 361c_vla_type_p (const_tree t)
52ffd86e
MS
362{
363 if (TREE_CODE (t) == ARRAY_TYPE
364 && C_TYPE_VARIABLE_SIZE (t))
365 return true;
366 return false;
367}
400fbf9f 368\f
10bc1b1b 369/* Return the composite type of two compatible types.
5305f6d7 370
10bc1b1b
JM
371 We assume that comptypes has already been done and returned
372 nonzero; if that isn't so, this may crash. In particular, we
373 assume that qualifiers match. */
400fbf9f
JW
374
375tree
10bc1b1b 376composite_type (tree t1, tree t2)
400fbf9f 377{
b3694847
SS
378 enum tree_code code1;
379 enum tree_code code2;
4b027d16 380 tree attributes;
400fbf9f
JW
381
382 /* Save time if the two types are the same. */
383
384 if (t1 == t2) return t1;
385
386 /* If one type is nonsense, use the other. */
387 if (t1 == error_mark_node)
388 return t2;
389 if (t2 == error_mark_node)
390 return t1;
391
10bc1b1b
JM
392 code1 = TREE_CODE (t1);
393 code2 = TREE_CODE (t2);
394
d9525bec 395 /* Merge the attributes. */
5fd9b178 396 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 397
10bc1b1b
JM
398 /* If one is an enumerated type and the other is the compatible
399 integer type, the composite type might be either of the two
400 (DR#013 question 3). For consistency, use the enumerated type as
401 the composite type. */
400fbf9f 402
10bc1b1b
JM
403 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
404 return t1;
405 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
406 return t2;
75326e8c 407
366de0ce 408 gcc_assert (code1 == code2);
b6a10c9f 409
400fbf9f
JW
410 switch (code1)
411 {
400fbf9f 412 case POINTER_TYPE:
10bc1b1b 413 /* For two pointers, do this recursively on the target type. */
400fbf9f 414 {
3932261a
MM
415 tree pointed_to_1 = TREE_TYPE (t1);
416 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 417 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 418 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
419 t1 = build_type_attribute_variant (t1, attributes);
420 return qualify_type (t1, t2);
400fbf9f 421 }
400fbf9f
JW
422
423 case ARRAY_TYPE:
424 {
10bc1b1b 425 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
426 int quals;
427 tree unqual_elt;
ca8bdb78
JM
428 tree d1 = TYPE_DOMAIN (t1);
429 tree d2 = TYPE_DOMAIN (t2);
430 bool d1_variable, d2_variable;
431 bool d1_zero, d2_zero;
f6294de7 432 bool t1_complete, t2_complete;
46df2823 433
de46b2fe 434 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
435 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
436 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 437
f6294de7
JM
438 t1_complete = COMPLETE_TYPE_P (t1);
439 t2_complete = COMPLETE_TYPE_P (t2);
440
ca8bdb78
JM
441 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
442 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
443
444 d1_variable = (!d1_zero
445 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
446 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
447 d2_variable = (!d2_zero
448 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
449 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
450 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
451 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 452
400fbf9f 453 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
454 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
455 && (d2_variable || d2_zero || !d1_variable))
4b027d16 456 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
457 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
458 && (d1_variable || d1_zero || !d2_variable))
4b027d16 459 return build_type_attribute_variant (t2, attributes);
c22cacf3 460
de46b2fe
AP
461 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
462 return build_type_attribute_variant (t1, attributes);
463 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
464 return build_type_attribute_variant (t2, attributes);
c22cacf3 465
46df2823
JM
466 /* Merge the element types, and have a size if either arg has
467 one. We may have qualifiers on the element types. To set
468 up TYPE_MAIN_VARIANT correctly, we need to form the
469 composite of the unqualified types and add the qualifiers
470 back at the end. */
471 quals = TYPE_QUALS (strip_array_types (elt));
472 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
473 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
474 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
475 && (d2_variable
476 || d2_zero
477 || !d1_variable))
478 ? t1
479 : t2));
f6294de7
JM
480 /* Ensure a composite type involving a zero-length array type
481 is a zero-length type not an incomplete type. */
482 if (d1_zero && d2_zero
483 && (t1_complete || t2_complete)
484 && !COMPLETE_TYPE_P (t1))
485 {
486 TYPE_SIZE (t1) = bitsize_zero_node;
487 TYPE_SIZE_UNIT (t1) = size_zero_node;
488 }
46df2823 489 t1 = c_build_qualified_type (t1, quals);
de46b2fe 490 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
491 }
492
fcb99e7b
JJ
493 case ENUMERAL_TYPE:
494 case RECORD_TYPE:
495 case UNION_TYPE:
496 if (attributes != NULL)
497 {
498 /* Try harder not to create a new aggregate type. */
499 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
500 return t1;
501 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
502 return t2;
503 }
504 return build_type_attribute_variant (t1, attributes);
505
400fbf9f
JW
506 case FUNCTION_TYPE:
507 /* Function types: prefer the one that specified arg types.
508 If both do, merge the arg types. Also merge the return types. */
509 {
10bc1b1b 510 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
511 tree p1 = TYPE_ARG_TYPES (t1);
512 tree p2 = TYPE_ARG_TYPES (t2);
513 int len;
514 tree newargs, n;
515 int i;
516
517 /* Save space: see if the result is identical to one of the args. */
3f75a254 518 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 519 return build_type_attribute_variant (t1, attributes);
3f75a254 520 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 521 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
522
523 /* Simple way if one arg fails to specify argument types. */
524 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 525 {
fe7080d2
AP
526 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
527 t1 = build_type_attribute_variant (t1, attributes);
528 return qualify_type (t1, t2);
4b027d16 529 }
400fbf9f 530 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
531 {
532 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
533 t1 = build_type_attribute_variant (t1, attributes);
534 return qualify_type (t1, t2);
4b027d16 535 }
400fbf9f
JW
536
537 /* If both args specify argument types, we must merge the two
538 lists, argument by argument. */
2f4e8f2b 539
400fbf9f
JW
540 len = list_length (p1);
541 newargs = 0;
542
543 for (i = 0; i < len; i++)
8d9bfdc5 544 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
545
546 n = newargs;
547
548 for (; p1;
549 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
550 {
551 /* A null type means arg type is not specified.
552 Take whatever the other function type has. */
553 if (TREE_VALUE (p1) == 0)
554 {
555 TREE_VALUE (n) = TREE_VALUE (p2);
556 goto parm_done;
557 }
558 if (TREE_VALUE (p2) == 0)
559 {
560 TREE_VALUE (n) = TREE_VALUE (p1);
561 goto parm_done;
562 }
2f6e4e97 563
400fbf9f
JW
564 /* Given wait (union {union wait *u; int *i} *)
565 and wait (union wait *),
566 prefer union wait * as type of parm. */
567 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
568 && TREE_VALUE (p1) != TREE_VALUE (p2))
569 {
570 tree memb;
58cb41e6
JJ
571 tree mv2 = TREE_VALUE (p2);
572 if (mv2 && mv2 != error_mark_node
573 && TREE_CODE (mv2) != ARRAY_TYPE)
574 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 575 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 576 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
577 {
578 tree mv3 = TREE_TYPE (memb);
579 if (mv3 && mv3 != error_mark_node
580 && TREE_CODE (mv3) != ARRAY_TYPE)
581 mv3 = TYPE_MAIN_VARIANT (mv3);
582 if (comptypes (mv3, mv2))
583 {
584 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
585 TREE_VALUE (p2));
c1771a20 586 pedwarn (input_location, OPT_Wpedantic,
fcf73884 587 "function types not truly compatible in ISO C");
58cb41e6
JJ
588 goto parm_done;
589 }
590 }
400fbf9f
JW
591 }
592 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
593 && TREE_VALUE (p2) != TREE_VALUE (p1))
594 {
595 tree memb;
58cb41e6
JJ
596 tree mv1 = TREE_VALUE (p1);
597 if (mv1 && mv1 != error_mark_node
598 && TREE_CODE (mv1) != ARRAY_TYPE)
599 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 600 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 601 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
602 {
603 tree mv3 = TREE_TYPE (memb);
604 if (mv3 && mv3 != error_mark_node
605 && TREE_CODE (mv3) != ARRAY_TYPE)
606 mv3 = TYPE_MAIN_VARIANT (mv3);
607 if (comptypes (mv3, mv1))
608 {
609 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
610 TREE_VALUE (p1));
c1771a20 611 pedwarn (input_location, OPT_Wpedantic,
fcf73884 612 "function types not truly compatible in ISO C");
58cb41e6
JJ
613 goto parm_done;
614 }
615 }
400fbf9f 616 }
10bc1b1b 617 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
618 parm_done: ;
619 }
620
4b027d16 621 t1 = build_function_type (valtype, newargs);
fe7080d2 622 t1 = qualify_type (t1, t2);
0f41302f 623 /* ... falls through ... */
400fbf9f
JW
624 }
625
626 default:
4b027d16 627 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
628 }
629
630}
10bc1b1b
JM
631
632/* Return the type of a conditional expression between pointers to
633 possibly differently qualified versions of compatible types.
634
635 We assume that comp_target_types has already been done and returned
636 nonzero; if that isn't so, this may crash. */
637
638static tree
639common_pointer_type (tree t1, tree t2)
640{
641 tree attributes;
46df2823
JM
642 tree pointed_to_1, mv1;
643 tree pointed_to_2, mv2;
10bc1b1b 644 tree target;
eb1387a0 645 unsigned target_quals;
36c5e70a
BE
646 addr_space_t as1, as2, as_common;
647 int quals1, quals2;
10bc1b1b
JM
648
649 /* Save time if the two types are the same. */
650
651 if (t1 == t2) return t1;
652
653 /* If one type is nonsense, use the other. */
654 if (t1 == error_mark_node)
655 return t2;
656 if (t2 == error_mark_node)
657 return t1;
658
366de0ce 659 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 660 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
661
662 /* Merge the attributes. */
663 attributes = targetm.merge_type_attributes (t1, t2);
664
665 /* Find the composite type of the target types, and combine the
46df2823
JM
666 qualifiers of the two types' targets. Do not lose qualifiers on
667 array element types by taking the TYPE_MAIN_VARIANT. */
668 mv1 = pointed_to_1 = TREE_TYPE (t1);
669 mv2 = pointed_to_2 = TREE_TYPE (t2);
670 if (TREE_CODE (mv1) != ARRAY_TYPE)
671 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
672 if (TREE_CODE (mv2) != ARRAY_TYPE)
673 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
674 target = composite_type (mv1, mv2);
eb1387a0 675
768952be
MU
676 /* Strip array types to get correct qualifier for pointers to arrays */
677 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
678 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
679
eb1387a0
RG
680 /* For function types do not merge const qualifiers, but drop them
681 if used inconsistently. The middle-end uses these to mark const
682 and noreturn functions. */
683 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 684 target_quals = (quals1 & quals2);
eb1387a0 685 else
36c5e70a
BE
686 target_quals = (quals1 | quals2);
687
688 /* If the two named address spaces are different, determine the common
689 superset address space. This is guaranteed to exist due to the
690 assumption that comp_target_type returned non-zero. */
691 as1 = TYPE_ADDR_SPACE (pointed_to_1);
692 as2 = TYPE_ADDR_SPACE (pointed_to_2);
693 if (!addr_space_superset (as1, as2, &as_common))
694 gcc_unreachable ();
695
696 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
697
eb1387a0 698 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
699 return build_type_attribute_variant (t1, attributes);
700}
701
702/* Return the common type for two arithmetic types under the usual
703 arithmetic conversions. The default conversions have already been
704 applied, and enumerated types converted to their compatible integer
705 types. The resulting type is unqualified and has no attributes.
706
707 This is the type for the result of most arithmetic operations
708 if the operands have the given two types. */
709
ccf7f880
JJ
710static tree
711c_common_type (tree t1, tree t2)
10bc1b1b
JM
712{
713 enum tree_code code1;
714 enum tree_code code2;
715
716 /* If one type is nonsense, use the other. */
717 if (t1 == error_mark_node)
718 return t2;
719 if (t2 == error_mark_node)
720 return t1;
721
722 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
723 t1 = TYPE_MAIN_VARIANT (t1);
724
725 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
726 t2 = TYPE_MAIN_VARIANT (t2);
727
728 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
729 t1 = build_type_attribute_variant (t1, NULL_TREE);
730
731 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
732 t2 = build_type_attribute_variant (t2, NULL_TREE);
733
734 /* Save time if the two types are the same. */
735
736 if (t1 == t2) return t1;
737
738 code1 = TREE_CODE (t1);
739 code2 = TREE_CODE (t2);
740
366de0ce 741 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
742 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
743 || code1 == INTEGER_TYPE);
366de0ce 744 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
745 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
746 || code2 == INTEGER_TYPE);
10bc1b1b 747
5fc89bfd
JJ
748 /* When one operand is a decimal float type, the other operand cannot be
749 a generic float type or a complex type. We also disallow vector types
750 here. */
751 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
752 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
753 {
754 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
755 {
756 error ("can%'t mix operands of decimal float and vector types");
757 return error_mark_node;
758 }
759 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
760 {
761 error ("can%'t mix operands of decimal float and complex types");
762 return error_mark_node;
763 }
764 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
765 {
766 error ("can%'t mix operands of decimal float and other float types");
767 return error_mark_node;
768 }
769 }
770
10bc1b1b
JM
771 /* If one type is a vector type, return that type. (How the usual
772 arithmetic conversions apply to the vector types extension is not
773 precisely specified.) */
774 if (code1 == VECTOR_TYPE)
775 return t1;
776
777 if (code2 == VECTOR_TYPE)
778 return t2;
779
780 /* If one type is complex, form the common type of the non-complex
781 components, then make that complex. Use T1 or T2 if it is the
782 required type. */
783 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
784 {
785 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
786 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 787 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
788
789 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
790 return t1;
791 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
792 return t2;
793 else
794 return build_complex_type (subtype);
795 }
796
797 /* If only one is real, use it as the result. */
798
799 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
800 return t1;
801
802 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
803 return t2;
804
9a8ce21f
JG
805 /* If both are real and either are decimal floating point types, use
806 the decimal floating point type with the greater precision. */
807
808 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
809 {
810 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
811 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
812 return dfloat128_type_node;
813 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
814 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
815 return dfloat64_type_node;
816 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
817 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
818 return dfloat32_type_node;
819 }
820
ab22c1fa
CF
821 /* Deal with fixed-point types. */
822 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
823 {
824 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 825 machine_mode m1, m2;
ab22c1fa
CF
826 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
827
828 m1 = TYPE_MODE (t1);
829 m2 = TYPE_MODE (t2);
830
831 /* If one input type is saturating, the result type is saturating. */
832 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
833 satp = 1;
834
835 /* If both fixed-point types are unsigned, the result type is unsigned.
836 When mixing fixed-point and integer types, follow the sign of the
837 fixed-point type.
838 Otherwise, the result type is signed. */
839 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
840 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
841 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
842 && TYPE_UNSIGNED (t1))
843 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
844 && TYPE_UNSIGNED (t2)))
845 unsignedp = 1;
846
847 /* The result type is signed. */
848 if (unsignedp == 0)
849 {
850 /* If the input type is unsigned, we need to convert to the
851 signed type. */
852 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
853 {
d75d71e0 854 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
855 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
856 mclass = MODE_FRACT;
857 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
858 mclass = MODE_ACCUM;
859 else
860 gcc_unreachable ();
861 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
862 }
863 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
864 {
d75d71e0 865 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
866 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
867 mclass = MODE_FRACT;
868 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
869 mclass = MODE_ACCUM;
870 else
871 gcc_unreachable ();
872 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
873 }
874 }
875
876 if (code1 == FIXED_POINT_TYPE)
877 {
878 fbit1 = GET_MODE_FBIT (m1);
879 ibit1 = GET_MODE_IBIT (m1);
880 }
881 else
882 {
883 fbit1 = 0;
884 /* Signed integers need to subtract one sign bit. */
885 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
886 }
887
888 if (code2 == FIXED_POINT_TYPE)
889 {
890 fbit2 = GET_MODE_FBIT (m2);
891 ibit2 = GET_MODE_IBIT (m2);
892 }
893 else
894 {
895 fbit2 = 0;
896 /* Signed integers need to subtract one sign bit. */
897 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
898 }
899
900 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
901 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
902 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
903 satp);
904 }
905
10bc1b1b
JM
906 /* Both real or both integers; use the one with greater precision. */
907
908 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
909 return t1;
910 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
911 return t2;
912
913 /* Same precision. Prefer long longs to longs to ints when the
914 same precision, following the C99 rules on integer type rank
915 (which are equivalent to the C90 rules for C90 types). */
916
917 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
918 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
919 return long_long_unsigned_type_node;
920
921 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
923 {
924 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
925 return long_long_unsigned_type_node;
926 else
c22cacf3 927 return long_long_integer_type_node;
10bc1b1b
JM
928 }
929
930 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
931 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
932 return long_unsigned_type_node;
933
934 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
935 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
936 {
937 /* But preserve unsignedness from the other type,
938 since long cannot hold all the values of an unsigned int. */
939 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
940 return long_unsigned_type_node;
941 else
942 return long_integer_type_node;
943 }
944
945 /* Likewise, prefer long double to double even if same size. */
946 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
947 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
948 return long_double_type_node;
949
2531a1d9
JR
950 /* Likewise, prefer double to float even if same size.
951 We got a couple of embedded targets with 32 bit doubles, and the
952 pdp11 might have 64 bit floats. */
953 if (TYPE_MAIN_VARIANT (t1) == double_type_node
954 || TYPE_MAIN_VARIANT (t2) == double_type_node)
955 return double_type_node;
956
10bc1b1b
JM
957 /* Otherwise prefer the unsigned one. */
958
959 if (TYPE_UNSIGNED (t1))
960 return t1;
961 else
962 return t2;
963}
400fbf9f 964\f
5922c215
JM
965/* Wrapper around c_common_type that is used by c-common.c and other
966 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
967 are allowed here and are converted to their compatible integer types.
968 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
969 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
970tree
971common_type (tree t1, tree t2)
972{
973 if (TREE_CODE (t1) == ENUMERAL_TYPE)
974 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
975 if (TREE_CODE (t2) == ENUMERAL_TYPE)
976 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
977
978 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
979 if (TREE_CODE (t1) == BOOLEAN_TYPE
980 && TREE_CODE (t2) == BOOLEAN_TYPE)
981 return boolean_type_node;
982
983 /* If either type is BOOLEAN_TYPE, then return the other. */
984 if (TREE_CODE (t1) == BOOLEAN_TYPE)
985 return t2;
986 if (TREE_CODE (t2) == BOOLEAN_TYPE)
987 return t1;
988
ccf7f880
JJ
989 return c_common_type (t1, t2);
990}
f13c9b2c 991
400fbf9f
JW
992/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
993 or various other operations. Return 2 if they are compatible
994 but a warning may be needed if you use them together. */
995
996int
132da1a5 997comptypes (tree type1, tree type2)
f13c9b2c
AP
998{
999 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1000 int val;
1001
dc5027f4 1002 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
1003 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1004
1005 return val;
1006}
1007
1008/* Like comptypes, but if it returns non-zero because enum and int are
1009 compatible, it sets *ENUM_AND_INT_P to true. */
1010
1011static int
1012comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1013{
1014 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1015 int val;
1016
dc5027f4
JM
1017 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1018 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1019
1020 return val;
1021}
1022
1023/* Like comptypes, but if it returns nonzero for different types, it
1024 sets *DIFFERENT_TYPES_P to true. */
1025
1026int
1027comptypes_check_different_types (tree type1, tree type2,
1028 bool *different_types_p)
1029{
1030 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1031 int val;
1032
1033 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1034 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1035
f13c9b2c 1036 return val;
c22cacf3
MS
1037}
1038\f
f13c9b2c
AP
1039/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1040 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1041 but a warning may be needed if you use them together. If
1042 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1043 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1044 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1045 NULL, and the types are compatible but different enough not to be
48b0b196 1046 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1047 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1048 false, but may or may not be set if the types are incompatible.
1049 This differs from comptypes, in that we don't free the seen
1050 types. */
f13c9b2c
AP
1051
1052static int
dc5027f4
JM
1053comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1054 bool *different_types_p)
400fbf9f 1055{
58f9752a
KG
1056 const_tree t1 = type1;
1057 const_tree t2 = type2;
4b027d16 1058 int attrval, val;
400fbf9f
JW
1059
1060 /* Suppress errors caused by previously reported errors. */
1061
8d47dfc5
RH
1062 if (t1 == t2 || !t1 || !t2
1063 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1064 return 1;
1065
bca63328
JM
1066 /* Enumerated types are compatible with integer types, but this is
1067 not transitive: two enumerated types in the same translation unit
1068 are compatible with each other only if they are the same type. */
400fbf9f 1069
bca63328 1070 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1071 {
1072 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1073 if (TREE_CODE (t2) != VOID_TYPE)
1074 {
1075 if (enum_and_int_p != NULL)
1076 *enum_and_int_p = true;
1077 if (different_types_p != NULL)
1078 *different_types_p = true;
1079 }
744aa42f 1080 }
bca63328 1081 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1082 {
1083 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1084 if (TREE_CODE (t1) != 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 }
400fbf9f
JW
1092
1093 if (t1 == t2)
1094 return 1;
1095
1096 /* Different classes of types can't be compatible. */
1097
3aeb3655
EC
1098 if (TREE_CODE (t1) != TREE_CODE (t2))
1099 return 0;
400fbf9f 1100
118a3a8b 1101 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1102
3932261a 1103 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1104 return 0;
1105
08632da2
RS
1106 /* Allow for two different type nodes which have essentially the same
1107 definition. Note that we already checked for equality of the type
38e01259 1108 qualifiers (just above). */
400fbf9f 1109
46df2823
JM
1110 if (TREE_CODE (t1) != ARRAY_TYPE
1111 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1112 return 1;
1113
4b027d16 1114 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1115 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1116 return 0;
1117
1118 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1119 val = 0;
1120
400fbf9f
JW
1121 switch (TREE_CODE (t1))
1122 {
1123 case POINTER_TYPE:
106f5de5
UW
1124 /* Do not remove mode or aliasing information. */
1125 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1126 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1127 break;
4b027d16 1128 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1129 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1130 enum_and_int_p, different_types_p));
4b027d16 1131 break;
400fbf9f
JW
1132
1133 case FUNCTION_TYPE:
dc5027f4
JM
1134 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1135 different_types_p);
4b027d16 1136 break;
400fbf9f
JW
1137
1138 case ARRAY_TYPE:
1139 {
400fbf9f
JW
1140 tree d1 = TYPE_DOMAIN (t1);
1141 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1142 bool d1_variable, d2_variable;
1143 bool d1_zero, d2_zero;
4b027d16 1144 val = 1;
400fbf9f
JW
1145
1146 /* Target types must match incl. qualifiers. */
1147 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1148 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1149 enum_and_int_p,
1150 different_types_p)))
400fbf9f
JW
1151 return 0;
1152
dc5027f4
JM
1153 if (different_types_p != NULL
1154 && (d1 == 0) != (d2 == 0))
1155 *different_types_p = true;
400fbf9f 1156 /* Sizes must match unless one is missing or variable. */
3f85558f 1157 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1158 break;
400fbf9f 1159
3f75a254
JM
1160 d1_zero = !TYPE_MAX_VALUE (d1);
1161 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1162
3f75a254 1163 d1_variable = (!d1_zero
3f85558f
RH
1164 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1165 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1166 d2_variable = (!d2_zero
3f85558f
RH
1167 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1168 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1169 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1170 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1171
dc5027f4
JM
1172 if (different_types_p != NULL
1173 && d1_variable != d2_variable)
1174 *different_types_p = true;
3f85558f
RH
1175 if (d1_variable || d2_variable)
1176 break;
1177 if (d1_zero && d2_zero)
1178 break;
1179 if (d1_zero || d2_zero
3f75a254
JM
1180 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1181 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1182 val = 0;
1183
c22cacf3 1184 break;
400fbf9f
JW
1185 }
1186
d1bd0ded 1187 case ENUMERAL_TYPE:
58393038 1188 case RECORD_TYPE:
d1bd0ded 1189 case UNION_TYPE:
766beae1 1190 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1191 {
fcb99e7b
JJ
1192 tree a1 = TYPE_ATTRIBUTES (t1);
1193 tree a2 = TYPE_ATTRIBUTES (t2);
1194
1195 if (! attribute_list_contained (a1, a2)
1196 && ! attribute_list_contained (a2, a1))
1197 break;
1198
f13c9b2c 1199 if (attrval != 2)
dc5027f4
JM
1200 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1201 different_types_p);
1202 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1203 different_types_p);
f13c9b2c 1204 }
4b027d16 1205 break;
e9a25f70 1206
62e1dfcf 1207 case VECTOR_TYPE:
744aa42f
ILT
1208 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1209 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1210 enum_and_int_p, different_types_p));
62e1dfcf
NC
1211 break;
1212
e9a25f70
JL
1213 default:
1214 break;
400fbf9f 1215 }
4b027d16 1216 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1217}
1218
36c5e70a
BE
1219/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1220 their qualifiers, except for named address spaces. If the pointers point to
1221 different named addresses, then we must determine if one address space is a
1222 subset of the other. */
400fbf9f
JW
1223
1224static int
744aa42f 1225comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1226{
392202b0 1227 int val;
768952be 1228 int val_ped;
36c5e70a
BE
1229 tree mvl = TREE_TYPE (ttl);
1230 tree mvr = TREE_TYPE (ttr);
1231 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1232 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1233 addr_space_t as_common;
744aa42f 1234 bool enum_and_int_p;
8b40563c 1235
36c5e70a
BE
1236 /* Fail if pointers point to incompatible address spaces. */
1237 if (!addr_space_superset (asl, asr, &as_common))
1238 return 0;
1239
768952be
MU
1240 /* For pedantic record result of comptypes on arrays before losing
1241 qualifiers on the element type below. */
1242 val_ped = 1;
1243
1244 if (TREE_CODE (mvl) == ARRAY_TYPE
1245 && TREE_CODE (mvr) == ARRAY_TYPE)
1246 val_ped = comptypes (mvl, mvr);
1247
1248 /* Qualifiers on element types of array types that are
1249 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1250
1251 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1252 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1253 : TYPE_MAIN_VARIANT (mvl));
1254
1255 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1256 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1257 : TYPE_MAIN_VARIANT (mvr));
1258
744aa42f
ILT
1259 enum_and_int_p = false;
1260 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1261
768952be
MU
1262 if (val == 1 && val_ped != 1)
1263 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1264 "are incompatible in ISO C");
1265
fcf73884 1266 if (val == 2)
c1771a20 1267 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1268
1269 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1270 warning_at (location, OPT_Wc___compat,
1271 "pointer target types incompatible in C++");
1272
400fbf9f
JW
1273 return val;
1274}
1275\f
1276/* Subroutines of `comptypes'. */
1277
f75fbaf7
ZW
1278/* Determine whether two trees derive from the same translation unit.
1279 If the CONTEXT chain ends in a null, that tree's context is still
1280 being parsed, so if two trees have context chains ending in null,
766beae1 1281 they're in the same translation unit. */
f75fbaf7 1282int
58f9752a 1283same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1284{
1285 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1286 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1287 {
6615c446
JO
1288 case tcc_declaration:
1289 t1 = DECL_CONTEXT (t1); break;
1290 case tcc_type:
1291 t1 = TYPE_CONTEXT (t1); break;
1292 case tcc_exceptional:
1293 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1294 default: gcc_unreachable ();
766beae1
ZW
1295 }
1296
1297 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1298 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1299 {
6615c446
JO
1300 case tcc_declaration:
1301 t2 = DECL_CONTEXT (t2); break;
1302 case tcc_type:
1303 t2 = TYPE_CONTEXT (t2); break;
1304 case tcc_exceptional:
1305 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1306 default: gcc_unreachable ();
766beae1
ZW
1307 }
1308
1309 return t1 == t2;
1310}
1311
f13c9b2c 1312/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1313
f13c9b2c 1314static struct tagged_tu_seen_cache *
58f9752a 1315alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1316{
cceb1885 1317 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1318 tu->next = tagged_tu_seen_base;
1319 tu->t1 = t1;
1320 tu->t2 = t2;
c22cacf3 1321
f13c9b2c 1322 tagged_tu_seen_base = tu;
c22cacf3 1323
f13c9b2c
AP
1324 /* The C standard says that two structures in different translation
1325 units are compatible with each other only if the types of their
1326 fields are compatible (among other things). We assume that they
1327 are compatible until proven otherwise when building the cache.
1328 An example where this can occur is:
1329 struct a
1330 {
1331 struct a *next;
1332 };
1333 If we are comparing this against a similar struct in another TU,
c83eecad 1334 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1335 loop. */
1336 tu->val = 1;
1337 return tu;
1338}
d1bd0ded 1339
f13c9b2c 1340/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1341
f13c9b2c
AP
1342static void
1343free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1344{
1345 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1346 while (tu != tu_til)
1347 {
741ac903
KG
1348 const struct tagged_tu_seen_cache *const tu1
1349 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1350 tu = tu1->next;
b1d5455a 1351 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1352 }
1353 tagged_tu_seen_base = tu_til;
1354}
d1bd0ded
GK
1355
1356/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1357 compatible. If the two types are not the same (which has been
1358 checked earlier), this can only happen when multiple translation
1359 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1360 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1361 comptypes_internal. */
d1bd0ded
GK
1362
1363static int
744aa42f 1364tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1365 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1366{
1367 tree s1, s2;
1368 bool needs_warning = false;
3aeb3655 1369
d1bd0ded
GK
1370 /* We have to verify that the tags of the types are the same. This
1371 is harder than it looks because this may be a typedef, so we have
1372 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1373 typedef...
1374 In the case of compiler-created builtin structs the TYPE_DECL
1375 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1376 while (TYPE_NAME (t1)
1377 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1378 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1379 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1380
dea984dc
ILT
1381 while (TYPE_NAME (t2)
1382 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1383 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1384 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1385
1386 /* C90 didn't have the requirement that the two tags be the same. */
1387 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1388 return 0;
3aeb3655 1389
d1bd0ded
GK
1390 /* C90 didn't say what happened if one or both of the types were
1391 incomplete; we choose to follow C99 rules here, which is that they
1392 are compatible. */
1393 if (TYPE_SIZE (t1) == NULL
1394 || TYPE_SIZE (t2) == NULL)
1395 return 1;
3aeb3655 1396
d1bd0ded 1397 {
f13c9b2c 1398 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1399 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1400 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1401 return tts_i->val;
d1bd0ded 1402 }
3aeb3655 1403
d1bd0ded
GK
1404 switch (TREE_CODE (t1))
1405 {
1406 case ENUMERAL_TYPE:
1407 {
f13c9b2c 1408 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1409 /* Speed up the case where the type values are in the same order. */
1410 tree tv1 = TYPE_VALUES (t1);
1411 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1412
c22cacf3 1413 if (tv1 == tv2)
f13c9b2c
AP
1414 {
1415 return 1;
1416 }
3aeb3655 1417
c22cacf3
MS
1418 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1419 {
1420 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1421 break;
1422 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1423 {
c22cacf3 1424 tu->val = 0;
f13c9b2c
AP
1425 return 0;
1426 }
c22cacf3 1427 }
3aeb3655 1428
c22cacf3 1429 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1430 {
1431 return 1;
1432 }
c22cacf3 1433 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1434 {
1435 tu->val = 0;
1436 return 0;
1437 }
3aeb3655 1438
d1bd0ded 1439 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1440 {
1441 tu->val = 0;
1442 return 0;
1443 }
3aeb3655 1444
d1bd0ded
GK
1445 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1446 {
1447 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1448 if (s2 == NULL
1449 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1450 {
1451 tu->val = 0;
1452 return 0;
1453 }
d1bd0ded
GK
1454 }
1455 return 1;
1456 }
1457
1458 case UNION_TYPE:
1459 {
f13c9b2c 1460 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1461 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1462 {
1463 tu->val = 0;
1464 return 0;
1465 }
c22cacf3 1466
f13c9b2c
AP
1467 /* Speed up the common case where the fields are in the same order. */
1468 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1469 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1470 {
1471 int result;
c22cacf3 1472
3ae4d3cc 1473 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1474 break;
744aa42f 1475 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1476 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1477
1478 if (result != 1 && !DECL_NAME (s1))
1479 break;
f13c9b2c
AP
1480 if (result == 0)
1481 {
1482 tu->val = 0;
1483 return 0;
1484 }
1485 if (result == 2)
1486 needs_warning = true;
1487
1488 if (TREE_CODE (s1) == FIELD_DECL
1489 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1490 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1491 {
1492 tu->val = 0;
1493 return 0;
1494 }
1495 }
1496 if (!s1 && !s2)
1497 {
1498 tu->val = needs_warning ? 2 : 1;
1499 return tu->val;
1500 }
d1bd0ded 1501
910ad8de 1502 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1503 {
1504 bool ok = false;
3aeb3655 1505
910ad8de 1506 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1507 if (DECL_NAME (s1) == DECL_NAME (s2))
1508 {
1509 int result;
1510
744aa42f 1511 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1512 enum_and_int_p,
1513 different_types_p);
3ae4d3cc
AO
1514
1515 if (result != 1 && !DECL_NAME (s1))
1516 continue;
1517 if (result == 0)
1518 {
1519 tu->val = 0;
1520 return 0;
1521 }
1522 if (result == 2)
1523 needs_warning = true;
1524
1525 if (TREE_CODE (s1) == FIELD_DECL
1526 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1527 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1528 break;
3ae4d3cc
AO
1529
1530 ok = true;
1531 break;
1532 }
3f75a254 1533 if (!ok)
f13c9b2c
AP
1534 {
1535 tu->val = 0;
1536 return 0;
1537 }
d1bd0ded 1538 }
f13c9b2c
AP
1539 tu->val = needs_warning ? 2 : 10;
1540 return tu->val;
d1bd0ded
GK
1541 }
1542
1543 case RECORD_TYPE:
1544 {
c22cacf3 1545 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1546
1547 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1548 s1 && s2;
910ad8de 1549 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1550 {
1551 int result;
1552 if (TREE_CODE (s1) != TREE_CODE (s2)
1553 || DECL_NAME (s1) != DECL_NAME (s2))
1554 break;
744aa42f 1555 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1556 enum_and_int_p, different_types_p);
d1bd0ded
GK
1557 if (result == 0)
1558 break;
1559 if (result == 2)
1560 needs_warning = true;
3aeb3655 1561
d1bd0ded
GK
1562 if (TREE_CODE (s1) == FIELD_DECL
1563 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1564 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1565 break;
1566 }
d1bd0ded 1567 if (s1 && s2)
f13c9b2c
AP
1568 tu->val = 0;
1569 else
1570 tu->val = needs_warning ? 2 : 1;
1571 return tu->val;
d1bd0ded
GK
1572 }
1573
1574 default:
366de0ce 1575 gcc_unreachable ();
d1bd0ded
GK
1576 }
1577}
1578
400fbf9f
JW
1579/* Return 1 if two function types F1 and F2 are compatible.
1580 If either type specifies no argument types,
1581 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1582 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1583 the other must specify that number of self-promoting arg types.
744aa42f 1584 Otherwise, the argument types must match.
dc5027f4 1585 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1586
1587static int
744aa42f 1588function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1589 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1590{
1591 tree args1, args2;
1592 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1593 int val = 1;
1594 int val1;
a6fdc086
GK
1595 tree ret1, ret2;
1596
1597 ret1 = TREE_TYPE (f1);
1598 ret2 = TREE_TYPE (f2);
1599
e508a019
JM
1600 /* 'volatile' qualifiers on a function's return type used to mean
1601 the function is noreturn. */
1602 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1603 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1604 if (TYPE_VOLATILE (ret1))
1605 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1606 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1607 if (TYPE_VOLATILE (ret2))
1608 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1609 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1610 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1611 if (val == 0)
400fbf9f
JW
1612 return 0;
1613
1614 args1 = TYPE_ARG_TYPES (f1);
1615 args2 = TYPE_ARG_TYPES (f2);
1616
dc5027f4
JM
1617 if (different_types_p != NULL
1618 && (args1 == 0) != (args2 == 0))
1619 *different_types_p = true;
1620
400fbf9f
JW
1621 /* An unspecified parmlist matches any specified parmlist
1622 whose argument types don't need default promotions. */
1623
1624 if (args1 == 0)
1625 {
1626 if (!self_promoting_args_p (args2))
1627 return 0;
1628 /* If one of these types comes from a non-prototype fn definition,
1629 compare that with the other type's arglist.
3176a0c2 1630 If they don't match, ask for a warning (but no error). */
400fbf9f 1631 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1632 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1633 enum_and_int_p, different_types_p))
400fbf9f
JW
1634 val = 2;
1635 return val;
1636 }
1637 if (args2 == 0)
1638 {
1639 if (!self_promoting_args_p (args1))
1640 return 0;
1641 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1642 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1643 enum_and_int_p, different_types_p))
400fbf9f
JW
1644 val = 2;
1645 return val;
1646 }
1647
1648 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1649 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1650 different_types_p);
400fbf9f
JW
1651 return val1 != 1 ? val1 : val;
1652}
1653
744aa42f
ILT
1654/* Check two lists of types for compatibility, returning 0 for
1655 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1656 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1657 comptypes_internal. */
400fbf9f
JW
1658
1659static int
744aa42f 1660type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1661 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1662{
1663 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1664 int val = 1;
9d5f3e49 1665 int newval = 0;
400fbf9f
JW
1666
1667 while (1)
1668 {
46df2823 1669 tree a1, mv1, a2, mv2;
400fbf9f
JW
1670 if (args1 == 0 && args2 == 0)
1671 return val;
1672 /* If one list is shorter than the other,
1673 they fail to match. */
1674 if (args1 == 0 || args2 == 0)
1675 return 0;
46df2823
JM
1676 mv1 = a1 = TREE_VALUE (args1);
1677 mv2 = a2 = TREE_VALUE (args2);
1678 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1679 mv1 = (TYPE_ATOMIC (mv1)
1680 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1681 TYPE_QUAL_ATOMIC)
1682 : TYPE_MAIN_VARIANT (mv1));
46df2823 1683 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1684 mv2 = (TYPE_ATOMIC (mv2)
1685 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1686 TYPE_QUAL_ATOMIC)
1687 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1688 /* A null pointer instead of a type
1689 means there is supposed to be an argument
1690 but nothing is specified about what type it has.
1691 So match anything that self-promotes. */
dc5027f4
JM
1692 if (different_types_p != NULL
1693 && (a1 == 0) != (a2 == 0))
1694 *different_types_p = true;
46df2823 1695 if (a1 == 0)
400fbf9f 1696 {
46df2823 1697 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1698 return 0;
1699 }
46df2823 1700 else if (a2 == 0)
400fbf9f 1701 {
46df2823 1702 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1703 return 0;
1704 }
8f5b6d29 1705 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1706 else if (TREE_CODE (a1) == ERROR_MARK
1707 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1708 ;
dc5027f4
JM
1709 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1710 different_types_p)))
400fbf9f 1711 {
dc5027f4
JM
1712 if (different_types_p != NULL)
1713 *different_types_p = true;
400fbf9f
JW
1714 /* Allow wait (union {union wait *u; int *i} *)
1715 and wait (union wait *) to be compatible. */
46df2823
JM
1716 if (TREE_CODE (a1) == UNION_TYPE
1717 && (TYPE_NAME (a1) == 0
ebf0bf7f 1718 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1719 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1720 && tree_int_cst_equal (TYPE_SIZE (a1),
1721 TYPE_SIZE (a2)))
400fbf9f
JW
1722 {
1723 tree memb;
46df2823 1724 for (memb = TYPE_FIELDS (a1);
910ad8de 1725 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1726 {
1727 tree mv3 = TREE_TYPE (memb);
1728 if (mv3 && mv3 != error_mark_node
1729 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1730 mv3 = (TYPE_ATOMIC (mv3)
1731 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1732 TYPE_QUAL_ATOMIC)
1733 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1734 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1735 different_types_p))
58cb41e6
JJ
1736 break;
1737 }
400fbf9f
JW
1738 if (memb == 0)
1739 return 0;
1740 }
46df2823
JM
1741 else if (TREE_CODE (a2) == UNION_TYPE
1742 && (TYPE_NAME (a2) == 0
ebf0bf7f 1743 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1744 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1745 && tree_int_cst_equal (TYPE_SIZE (a2),
1746 TYPE_SIZE (a1)))
400fbf9f
JW
1747 {
1748 tree memb;
46df2823 1749 for (memb = TYPE_FIELDS (a2);
910ad8de 1750 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1751 {
1752 tree mv3 = TREE_TYPE (memb);
1753 if (mv3 && mv3 != error_mark_node
1754 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1755 mv3 = (TYPE_ATOMIC (mv3)
1756 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1757 TYPE_QUAL_ATOMIC)
1758 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1759 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1760 different_types_p))
58cb41e6
JJ
1761 break;
1762 }
400fbf9f
JW
1763 if (memb == 0)
1764 return 0;
1765 }
1766 else
1767 return 0;
1768 }
1769
1770 /* comptypes said ok, but record if it said to warn. */
1771 if (newval > val)
1772 val = newval;
1773
1774 args1 = TREE_CHAIN (args1);
1775 args2 = TREE_CHAIN (args2);
1776 }
1777}
400fbf9f 1778\f
a0e24419
MP
1779/* Compute the size to increment a pointer by. When a function type or void
1780 type or incomplete type is passed, size_one_node is returned.
1781 This function does not emit any diagnostics; the caller is responsible
1782 for that. */
400fbf9f 1783
4e2fb7de 1784static tree
58f9752a 1785c_size_in_bytes (const_tree type)
400fbf9f
JW
1786{
1787 enum tree_code code = TREE_CODE (type);
1788
a0e24419
MP
1789 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1790 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1791 return size_one_node;
1792
400fbf9f 1793 /* Convert in case a char is more than one unit. */
db3927fb
AH
1794 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1795 size_int (TYPE_PRECISION (char_type_node)
1796 / BITS_PER_UNIT));
400fbf9f 1797}
400fbf9f 1798\f
400fbf9f
JW
1799/* Return either DECL or its known constant value (if it has one). */
1800
56cb9733 1801tree
2f6e4e97 1802decl_constant_value (tree decl)
400fbf9f 1803{
a7c1916a 1804 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1805 in a place where a variable is invalid. Note that DECL_INITIAL
1806 isn't valid for a PARM_DECL. */
a7c1916a 1807 current_function_decl != 0
4f976745 1808 && TREE_CODE (decl) != PARM_DECL
3f75a254 1809 && !TREE_THIS_VOLATILE (decl)
83bab8db 1810 && TREE_READONLY (decl)
400fbf9f
JW
1811 && DECL_INITIAL (decl) != 0
1812 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1813 /* This is invalid if initial value is not constant.
1814 If it has either a function call, a memory reference,
1815 or a variable, then re-evaluating it could give different results. */
1816 && TREE_CONSTANT (DECL_INITIAL (decl))
1817 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1818 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1819 return DECL_INITIAL (decl);
1820 return decl;
1821}
1822
f2a71bbc
JM
1823/* Convert the array expression EXP to a pointer. */
1824static tree
c2255bc4 1825array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1826{
f2a71bbc 1827 tree orig_exp = exp;
207bf485 1828 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1829 tree adr;
1830 tree restype = TREE_TYPE (type);
1831 tree ptrtype;
207bf485 1832
f2a71bbc 1833 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1834
f2a71bbc 1835 STRIP_TYPE_NOPS (exp);
207bf485 1836
487a92fe
JM
1837 if (TREE_NO_WARNING (orig_exp))
1838 TREE_NO_WARNING (exp) = 1;
207bf485 1839
f2a71bbc
JM
1840 ptrtype = build_pointer_type (restype);
1841
1842 if (TREE_CODE (exp) == INDIRECT_REF)
1843 return convert (ptrtype, TREE_OPERAND (exp, 0));
1844
1f37c583
JM
1845 /* In C++ array compound literals are temporary objects unless they are
1846 const or appear in namespace scope, so they are destroyed too soon
1847 to use them for much of anything (c++/53220). */
1848 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1849 {
1850 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1851 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1852 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1853 "converting an array compound literal to a pointer "
1854 "is ill-formed in C++");
1855 }
1856
c2255bc4 1857 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1858 return convert (ptrtype, adr);
1859}
207bf485 1860
f2a71bbc
JM
1861/* Convert the function expression EXP to a pointer. */
1862static tree
c2255bc4 1863function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1864{
1865 tree orig_exp = exp;
207bf485 1866
f2a71bbc 1867 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1868
f2a71bbc 1869 STRIP_TYPE_NOPS (exp);
207bf485 1870
f2a71bbc
JM
1871 if (TREE_NO_WARNING (orig_exp))
1872 TREE_NO_WARNING (exp) = 1;
207bf485 1873
c2255bc4 1874 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1875}
207bf485 1876
ebfbbdc5
JJ
1877/* Mark EXP as read, not just set, for set but not used -Wunused
1878 warning purposes. */
1879
1880void
1881mark_exp_read (tree exp)
1882{
1883 switch (TREE_CODE (exp))
1884 {
1885 case VAR_DECL:
1886 case PARM_DECL:
1887 DECL_READ_P (exp) = 1;
1888 break;
1889 case ARRAY_REF:
1890 case COMPONENT_REF:
1891 case MODIFY_EXPR:
1892 case REALPART_EXPR:
1893 case IMAGPART_EXPR:
1894 CASE_CONVERT:
1895 case ADDR_EXPR:
1896 mark_exp_read (TREE_OPERAND (exp, 0));
1897 break;
1898 case COMPOUND_EXPR:
82c3c067 1899 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1900 mark_exp_read (TREE_OPERAND (exp, 1));
1901 break;
1902 default:
1903 break;
1904 }
1905}
1906
f2a71bbc
JM
1907/* Perform the default conversion of arrays and functions to pointers.
1908 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1909 return EXP.
1910
1911 LOC is the location of the expression. */
f2a71bbc
JM
1912
1913struct c_expr
c2255bc4 1914default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1915{
1916 tree orig_exp = exp.value;
1917 tree type = TREE_TYPE (exp.value);
1918 enum tree_code code = TREE_CODE (type);
1919
1920 switch (code)
1921 {
1922 case ARRAY_TYPE:
1923 {
1924 bool not_lvalue = false;
1925 bool lvalue_array_p;
1926
1927 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1928 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1929 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1930 {
1931 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1932 not_lvalue = true;
1933 exp.value = TREE_OPERAND (exp.value, 0);
1934 }
1935
1936 if (TREE_NO_WARNING (orig_exp))
1937 TREE_NO_WARNING (exp.value) = 1;
1938
1939 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1940 if (!flag_isoc99 && !lvalue_array_p)
1941 {
1942 /* Before C99, non-lvalue arrays do not decay to pointers.
1943 Normally, using such an array would be invalid; but it can
1944 be used correctly inside sizeof or as a statement expression.
1945 Thus, do not give an error here; an error will result later. */
1946 return exp;
1947 }
1948
c2255bc4 1949 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1950 }
1951 break;
1952 case FUNCTION_TYPE:
c2255bc4 1953 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1954 break;
1955 default:
f2a71bbc 1956 break;
207bf485 1957 }
f2a71bbc 1958
207bf485
JM
1959 return exp;
1960}
1961
ebfbbdc5
JJ
1962struct c_expr
1963default_function_array_read_conversion (location_t loc, struct c_expr exp)
1964{
1965 mark_exp_read (exp.value);
1966 return default_function_array_conversion (loc, exp);
1967}
522ddfa2 1968
267bac10
JM
1969/* Return whether EXPR should be treated as an atomic lvalue for the
1970 purposes of load and store handling. */
1971
1972static bool
1973really_atomic_lvalue (tree expr)
1974{
7a0ca710 1975 if (error_operand_p (expr))
267bac10
JM
1976 return false;
1977 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1978 return false;
1979 if (!lvalue_p (expr))
1980 return false;
1981
1982 /* Ignore _Atomic on register variables, since their addresses can't
1983 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1984 sequences wouldn't work. Ignore _Atomic on structures containing
1985 bit-fields, since accessing elements of atomic structures or
1986 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1987 it's undefined at translation time or execution time, and the
1988 normal atomic sequences again wouldn't work. */
1989 while (handled_component_p (expr))
1990 {
1991 if (TREE_CODE (expr) == COMPONENT_REF
1992 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1993 return false;
1994 expr = TREE_OPERAND (expr, 0);
1995 }
1996 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1997 return false;
1998 return true;
1999}
2000
2001/* Convert expression EXP (location LOC) from lvalue to rvalue,
2002 including converting functions and arrays to pointers if CONVERT_P.
2003 If READ_P, also mark the expression as having been read. */
2004
2005struct c_expr
2006convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2007 bool convert_p, bool read_p)
2008{
2009 if (read_p)
2010 mark_exp_read (exp.value);
2011 if (convert_p)
2012 exp = default_function_array_conversion (loc, exp);
2013 if (really_atomic_lvalue (exp.value))
2014 {
2015 vec<tree, va_gc> *params;
2016 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2017 tree expr_type = TREE_TYPE (exp.value);
2018 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2019 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2020
2021 gcc_assert (TYPE_ATOMIC (expr_type));
2022
2023 /* Expansion of a generic atomic load may require an addition
2024 element, so allocate enough to prevent a resize. */
2025 vec_alloc (params, 4);
2026
2027 /* Remove the qualifiers for the rest of the expressions and
2028 create the VAL temp variable to hold the RHS. */
2029 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
b731b390 2030 tmp = create_tmp_var (nonatomic_type);
267bac10
JM
2031 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2032 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2033 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2034
2035 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2036 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2037 params->quick_push (expr_addr);
2038 params->quick_push (tmp_addr);
2039 params->quick_push (seq_cst);
8edbfaa6 2040 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2041
cc28fc7f
MP
2042 /* EXPR is always read. */
2043 mark_exp_read (exp.value);
2044
267bac10
JM
2045 /* Return tmp which contains the value loaded. */
2046 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2047 }
2048 return exp;
2049}
2050
522ddfa2
JM
2051/* EXP is an expression of integer type. Apply the integer promotions
2052 to it and return the promoted value. */
400fbf9f
JW
2053
2054tree
522ddfa2 2055perform_integral_promotions (tree exp)
400fbf9f 2056{
b3694847
SS
2057 tree type = TREE_TYPE (exp);
2058 enum tree_code code = TREE_CODE (type);
400fbf9f 2059
522ddfa2 2060 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2061
400fbf9f
JW
2062 /* Normally convert enums to int,
2063 but convert wide enums to something wider. */
2064 if (code == ENUMERAL_TYPE)
2065 {
b0c48229
NB
2066 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2067 TYPE_PRECISION (integer_type_node)),
2068 ((TYPE_PRECISION (type)
2069 >= TYPE_PRECISION (integer_type_node))
8df83eae 2070 && TYPE_UNSIGNED (type)));
05bccae2 2071
400fbf9f
JW
2072 return convert (type, exp);
2073 }
2074
522ddfa2
JM
2075 /* ??? This should no longer be needed now bit-fields have their
2076 proper types. */
9753f113 2077 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2078 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2079 /* If it's thinner than an int, promote it like a
d72040f5 2080 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2081 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2082 TYPE_PRECISION (integer_type_node)))
f458d1d5 2083 return convert (integer_type_node, exp);
9753f113 2084
d72040f5 2085 if (c_promoting_integer_type_p (type))
400fbf9f 2086 {
f458d1d5 2087 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2088 if (TYPE_UNSIGNED (type)
f458d1d5 2089 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2090 return convert (unsigned_type_node, exp);
05bccae2 2091
400fbf9f
JW
2092 return convert (integer_type_node, exp);
2093 }
05bccae2 2094
522ddfa2
JM
2095 return exp;
2096}
2097
2098
2099/* Perform default promotions for C data used in expressions.
46bdb9cf 2100 Enumeral types or short or char are converted to int.
522ddfa2
JM
2101 In addition, manifest constants symbols are replaced by their values. */
2102
2103tree
2104default_conversion (tree exp)
2105{
2106 tree orig_exp;
2107 tree type = TREE_TYPE (exp);
2108 enum tree_code code = TREE_CODE (type);
40449a90 2109 tree promoted_type;
522ddfa2 2110
ebfbbdc5
JJ
2111 mark_exp_read (exp);
2112
46bdb9cf
JM
2113 /* Functions and arrays have been converted during parsing. */
2114 gcc_assert (code != FUNCTION_TYPE);
2115 if (code == ARRAY_TYPE)
2116 return exp;
522ddfa2
JM
2117
2118 /* Constants can be used directly unless they're not loadable. */
2119 if (TREE_CODE (exp) == CONST_DECL)
2120 exp = DECL_INITIAL (exp);
2121
522ddfa2
JM
2122 /* Strip no-op conversions. */
2123 orig_exp = exp;
2124 STRIP_TYPE_NOPS (exp);
2125
2126 if (TREE_NO_WARNING (orig_exp))
2127 TREE_NO_WARNING (exp) = 1;
2128
400fbf9f
JW
2129 if (code == VOID_TYPE)
2130 {
5436fa2e
MP
2131 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2132 "void value not ignored as it ought to be");
400fbf9f
JW
2133 return error_mark_node;
2134 }
808d6eaa
JM
2135
2136 exp = require_complete_type (exp);
2137 if (exp == error_mark_node)
2138 return error_mark_node;
2139
40449a90
SL
2140 promoted_type = targetm.promoted_type (type);
2141 if (promoted_type)
2142 return convert (promoted_type, exp);
2143
808d6eaa
JM
2144 if (INTEGRAL_TYPE_P (type))
2145 return perform_integral_promotions (exp);
2146
400fbf9f
JW
2147 return exp;
2148}
2149\f
0fb96aa4 2150/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2151
2152 If the component name is not found, returns NULL_TREE. Otherwise,
2153 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2154 stepping down the chain to the component, which is in the last
2155 TREE_VALUE of the list. Normally the list is of length one, but if
2156 the component is embedded within (nested) anonymous structures or
2157 unions, the list steps down the chain to the component. */
2f6e4e97 2158
2f2d13da 2159static tree
0fb96aa4 2160lookup_field (tree type, tree component)
2f2d13da
DE
2161{
2162 tree field;
2163
2164 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2165 to the field elements. Use a binary search on this array to quickly
2166 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2167 will always be set for structures which have many elements. */
2168
22a0b85f 2169 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2170 {
2171 int bot, top, half;
d07605f5 2172 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2173
2174 field = TYPE_FIELDS (type);
2175 bot = 0;
d07605f5 2176 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2177 while (top - bot > 1)
2178 {
2f2d13da
DE
2179 half = (top - bot + 1) >> 1;
2180 field = field_array[bot+half];
2181
2182 if (DECL_NAME (field) == NULL_TREE)
2183 {
2184 /* Step through all anon unions in linear fashion. */
2185 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2186 {
2f2d13da 2187 field = field_array[bot++];
a68b98cf
RK
2188 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2189 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2190 {
0fb96aa4 2191 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2192
2193 if (anon)
2194 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2195
2196 /* The Plan 9 compiler permits referring
2197 directly to an anonymous struct/union field
2198 using a typedef name. */
2199 if (flag_plan9_extensions
2200 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2201 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2202 == TYPE_DECL)
2203 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2204 == component))
2205 break;
2f6e4e97 2206 }
2f2d13da
DE
2207 }
2208
2209 /* Entire record is only anon unions. */
2210 if (bot > top)
2211 return NULL_TREE;
2212
2213 /* Restart the binary search, with new lower bound. */
2214 continue;
2215 }
2216
e8b87aac 2217 if (DECL_NAME (field) == component)
2f2d13da 2218 break;
e8b87aac 2219 if (DECL_NAME (field) < component)
2f2d13da
DE
2220 bot += half;
2221 else
2222 top = bot + half;
2223 }
2224
2225 if (DECL_NAME (field_array[bot]) == component)
2226 field = field_array[bot];
2227 else if (DECL_NAME (field) != component)
e9b2c823 2228 return NULL_TREE;
2f2d13da
DE
2229 }
2230 else
2231 {
910ad8de 2232 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2233 {
e9b2c823
NB
2234 if (DECL_NAME (field) == NULL_TREE
2235 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2236 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2237 {
0fb96aa4 2238 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2239
e9b2c823
NB
2240 if (anon)
2241 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2242
2243 /* The Plan 9 compiler permits referring directly to an
2244 anonymous struct/union field using a typedef
2245 name. */
2246 if (flag_plan9_extensions
2247 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2248 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2249 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2250 == component))
2251 break;
2f2d13da
DE
2252 }
2253
2254 if (DECL_NAME (field) == component)
2255 break;
2256 }
e9b2c823
NB
2257
2258 if (field == NULL_TREE)
2259 return NULL_TREE;
2f2d13da
DE
2260 }
2261
e9b2c823 2262 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2263}
2264
c2255bc4
AH
2265/* Make an expression to refer to the COMPONENT field of structure or
2266 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2267 location of the COMPONENT_REF. */
400fbf9f
JW
2268
2269tree
c2255bc4 2270build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2271{
b3694847
SS
2272 tree type = TREE_TYPE (datum);
2273 enum tree_code code = TREE_CODE (type);
2274 tree field = NULL;
2275 tree ref;
1e57bf47 2276 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2277
7a3ea201
RH
2278 if (!objc_is_public (datum, component))
2279 return error_mark_node;
2280
46a88c12 2281 /* Detect Objective-C property syntax object.property. */
668ea4b1 2282 if (c_dialect_objc ()
46a88c12 2283 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2284 return ref;
2285
400fbf9f
JW
2286 /* See if there is a field or component with name COMPONENT. */
2287
2288 if (code == RECORD_TYPE || code == UNION_TYPE)
2289 {
d0f062fb 2290 if (!COMPLETE_TYPE_P (type))
400fbf9f 2291 {
7a228918 2292 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2293 return error_mark_node;
2294 }
2295
0fb96aa4 2296 field = lookup_field (type, component);
400fbf9f
JW
2297
2298 if (!field)
2299 {
c2255bc4 2300 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2301 return error_mark_node;
2302 }
400fbf9f 2303
e9b2c823
NB
2304 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2305 This might be better solved in future the way the C++ front
2306 end does it - by giving the anonymous entities each a
2307 separate name and type, and then have build_component_ref
2308 recursively call itself. We can't do that here. */
46ea50cb 2309 do
19d76e60 2310 {
e9b2c823 2311 tree subdatum = TREE_VALUE (field);
efed193e
JM
2312 int quals;
2313 tree subtype;
1e57bf47 2314 bool use_datum_quals;
e9b2c823
NB
2315
2316 if (TREE_TYPE (subdatum) == error_mark_node)
2317 return error_mark_node;
2318
1e57bf47
JM
2319 /* If this is an rvalue, it does not have qualifiers in C
2320 standard terms and we must avoid propagating such
2321 qualifiers down to a non-lvalue array that is then
2322 converted to a pointer. */
2323 use_datum_quals = (datum_lvalue
2324 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2325
efed193e 2326 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2327 if (use_datum_quals)
2328 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2329 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2330
2331 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2332 NULL_TREE);
c2255bc4 2333 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2334 if (TREE_READONLY (subdatum)
2335 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2336 TREE_READONLY (ref) = 1;
1e57bf47
JM
2337 if (TREE_THIS_VOLATILE (subdatum)
2338 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2339 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2340
2341 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2342 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2343
19d76e60 2344 datum = ref;
46ea50cb
RS
2345
2346 field = TREE_CHAIN (field);
19d76e60 2347 }
46ea50cb 2348 while (field);
19d76e60 2349
400fbf9f
JW
2350 return ref;
2351 }
2352 else if (code != ERROR_MARK)
c2255bc4
AH
2353 error_at (loc,
2354 "request for member %qE in something not a structure or union",
2355 component);
400fbf9f
JW
2356
2357 return error_mark_node;
2358}
2359\f
2360/* Given an expression PTR for a pointer, return an expression
2361 for the value pointed to.
6a3799eb
AH
2362 ERRORSTRING is the name of the operator to appear in error messages.
2363
2364 LOC is the location to use for the generated tree. */
400fbf9f
JW
2365
2366tree
dd865ef6 2367build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2368{
b3694847
SS
2369 tree pointer = default_conversion (ptr);
2370 tree type = TREE_TYPE (pointer);
6a3799eb 2371 tree ref;
400fbf9f
JW
2372
2373 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2374 {
1043771b 2375 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2376 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2377 {
2378 /* If a warning is issued, mark it to avoid duplicates from
2379 the backend. This only needs to be done at
2380 warn_strict_aliasing > 2. */
2381 if (warn_strict_aliasing > 2)
2382 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2383 type, TREE_OPERAND (pointer, 0)))
2384 TREE_NO_WARNING (pointer) = 1;
2385 }
2386
870cc33b 2387 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2388 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2389 == TREE_TYPE (type)))
6a3799eb
AH
2390 {
2391 ref = TREE_OPERAND (pointer, 0);
2392 protected_set_expr_location (ref, loc);
2393 return ref;
2394 }
870cc33b
RS
2395 else
2396 {
2397 tree t = TREE_TYPE (type);
46df2823 2398
984dfd8c 2399 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2400
baae9b65 2401 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2402 {
d9b7be2e
MP
2403 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2404 {
2405 error_at (loc, "dereferencing pointer to incomplete type "
2406 "%qT", t);
2407 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2408 }
870cc33b
RS
2409 return error_mark_node;
2410 }
7d882b83 2411 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2412 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2413
2414 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2415 so that we get the proper error message if the result is used
2416 to assign to. Also, &* is supposed to be a no-op.
2417 And ANSI C seems to specify that the type of the result
2418 should be the const type. */
2419 /* A de-reference of a pointer to const is not a const. It is valid
2420 to change it via some other pointer. */
2421 TREE_READONLY (ref) = TYPE_READONLY (t);
2422 TREE_SIDE_EFFECTS (ref)
271bd540 2423 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2424 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2425 protected_set_expr_location (ref, loc);
870cc33b
RS
2426 return ref;
2427 }
2428 }
400fbf9f 2429 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2430 invalid_indirection_error (loc, type, errstring);
2431
400fbf9f
JW
2432 return error_mark_node;
2433}
2434
2435/* This handles expressions of the form "a[i]", which denotes
2436 an array reference.
2437
2438 This is logically equivalent in C to *(a+i), but we may do it differently.
2439 If A is a variable or a member, we generate a primitive ARRAY_REF.
2440 This avoids forcing the array out of registers, and can work on
2441 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2442 by functions).
2443
30cd1c5d
AS
2444 For vector types, allow vector[i] but not i[vector], and create
2445 *(((type*)&vectortype) + i) for the expression.
2446
6a3799eb 2447 LOC is the location to use for the returned expression. */
400fbf9f
JW
2448
2449tree
c2255bc4 2450build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2451{
6a3799eb 2452 tree ret;
a4ab7973 2453 bool swapped = false;
400fbf9f
JW
2454 if (TREE_TYPE (array) == error_mark_node
2455 || TREE_TYPE (index) == error_mark_node)
2456 return error_mark_node;
2457
b72271b9 2458 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2459 {
2460 size_t rank = 0;
2461 if (!find_rank (loc, index, index, true, &rank))
2462 return error_mark_node;
2463 if (rank > 1)
2464 {
2465 error_at (loc, "rank of the array's index is greater than 1");
2466 return error_mark_node;
2467 }
2468 }
a4ab7973 2469 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2470 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2471 /* Allow vector[index] but not index[vector]. */
2472 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2473 {
a4ab7973
JM
2474 tree temp;
2475 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2476 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2477 {
f90e8e2e 2478 error_at (loc,
30cd1c5d
AS
2479 "subscripted value is neither array nor pointer nor vector");
2480
fdeefd49
RS
2481 return error_mark_node;
2482 }
a4ab7973
JM
2483 temp = array;
2484 array = index;
2485 index = temp;
2486 swapped = true;
2487 }
2488
2489 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2490 {
a63068b6 2491 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2492 return error_mark_node;
2493 }
2494
2495 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2496 {
a63068b6 2497 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2498 return error_mark_node;
2499 }
2500
ff6b6641
GDR
2501 /* ??? Existing practice has been to warn only when the char
2502 index is syntactically the index, not for char[array]. */
2503 if (!swapped)
5bd012f8 2504 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2505
2506 /* Apply default promotions *after* noticing character types. */
2507 index = default_conversion (index);
f406ae1f
MP
2508 if (index == error_mark_node)
2509 return error_mark_node;
a4ab7973
JM
2510
2511 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2512
aa7da51a
JJ
2513 bool non_lvalue
2514 = convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2515
2516 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2517 {
e4d35515 2518 tree rval, type;
fdeefd49 2519
400fbf9f
JW
2520 /* An array that is indexed by a non-constant
2521 cannot be stored in a register; we must be able to do
2522 address arithmetic on its address.
2523 Likewise an array of elements of variable size. */
2524 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2525 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2526 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2527 {
dffd7eb6 2528 if (!c_mark_addressable (array))
400fbf9f
JW
2529 return error_mark_node;
2530 }
e6d52559
JW
2531 /* An array that is indexed by a constant value which is not within
2532 the array bounds cannot be stored in a register either; because we
2533 would get a crash in store_bit_field/extract_bit_field when trying
2534 to access a non-existent part of the register. */
2535 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2536 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2537 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2538 {
dffd7eb6 2539 if (!c_mark_addressable (array))
e6d52559
JW
2540 return error_mark_node;
2541 }
400fbf9f 2542
f3bede71 2543 if (pedantic || warn_c90_c99_compat)
400fbf9f
JW
2544 {
2545 tree foo = array;
2546 while (TREE_CODE (foo) == COMPONENT_REF)
2547 foo = TREE_OPERAND (foo, 0);
5baeaac0 2548 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2549 pedwarn (loc, OPT_Wpedantic,
fcf73884 2550 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2551 else if (!lvalue_p (foo))
2552 pedwarn_c90 (loc, OPT_Wpedantic,
2553 "ISO C90 forbids subscripting non-lvalue "
2554 "array");
400fbf9f
JW
2555 }
2556
46df2823 2557 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2558 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2559 /* Array ref is const/volatile if the array elements are
c22cacf3 2560 or if the array is. */
400fbf9f
JW
2561 TREE_READONLY (rval)
2562 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2563 | TREE_READONLY (array));
2564 TREE_SIDE_EFFECTS (rval)
2565 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2566 | TREE_SIDE_EFFECTS (array));
2567 TREE_THIS_VOLATILE (rval)
2568 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2569 /* This was added by rms on 16 Nov 91.
2f6e4e97 2570 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2571 in an inline function.
2572 Hope it doesn't break something else. */
2573 | TREE_THIS_VOLATILE (array));
928c19bb 2574 ret = require_complete_type (rval);
6a3799eb 2575 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2576 if (non_lvalue)
2577 ret = non_lvalue_loc (loc, ret);
6a3799eb 2578 return ret;
400fbf9f 2579 }
a4ab7973
JM
2580 else
2581 {
2582 tree ar = default_conversion (array);
400fbf9f 2583
a4ab7973
JM
2584 if (ar == error_mark_node)
2585 return ar;
400fbf9f 2586
a4ab7973
JM
2587 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2588 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2589
aa7da51a
JJ
2590 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2591 index, 0),
2592 RO_ARRAY_INDEXING);
2593 if (non_lvalue)
2594 ret = non_lvalue_loc (loc, ret);
2595 return ret;
a4ab7973 2596 }
400fbf9f
JW
2597}
2598\f
7e585d16 2599/* Build an external reference to identifier ID. FUN indicates
766beb40 2600 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2601 location of the identifier. This sets *TYPE to the type of the
2602 identifier, which is not the same as the type of the returned value
2603 for CONST_DECLs defined as enum constants. If the type of the
2604 identifier is not available, *TYPE is set to NULL. */
7e585d16 2605tree
c2255bc4 2606build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2607{
2608 tree ref;
2609 tree decl = lookup_name (id);
16b34ad6
ZL
2610
2611 /* In Objective-C, an instance variable (ivar) may be preferred to
2612 whatever lookup_name() found. */
2613 decl = objc_lookup_ivar (decl, id);
7e585d16 2614
6866c6e8 2615 *type = NULL;
339a28b9 2616 if (decl && decl != error_mark_node)
6866c6e8
ILT
2617 {
2618 ref = decl;
2619 *type = TREE_TYPE (ref);
2620 }
339a28b9
ZW
2621 else if (fun)
2622 /* Implicit function declaration. */
c2255bc4 2623 ref = implicitly_declare (loc, id);
339a28b9
ZW
2624 else if (decl == error_mark_node)
2625 /* Don't complain about something that's already been
2626 complained about. */
2627 return error_mark_node;
2628 else
2629 {
c2255bc4 2630 undeclared_variable (loc, id);
339a28b9
ZW
2631 return error_mark_node;
2632 }
7e585d16
ZW
2633
2634 if (TREE_TYPE (ref) == error_mark_node)
2635 return error_mark_node;
2636
339a28b9 2637 if (TREE_DEPRECATED (ref))
9b86d6bb 2638 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2639
ad960f56 2640 /* Recursive call does not count as usage. */
b8698a0f 2641 if (ref != current_function_decl)
ad960f56 2642 {
ad960f56
MLI
2643 TREE_USED (ref) = 1;
2644 }
7e585d16 2645
bc4b653b
JM
2646 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2647 {
2648 if (!in_sizeof && !in_typeof)
2649 C_DECL_USED (ref) = 1;
2650 else if (DECL_INITIAL (ref) == 0
2651 && DECL_EXTERNAL (ref)
2652 && !TREE_PUBLIC (ref))
2653 record_maybe_used_decl (ref);
2654 }
2655
7e585d16
ZW
2656 if (TREE_CODE (ref) == CONST_DECL)
2657 {
6193b8b7 2658 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2659
2660 if (warn_cxx_compat
2661 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2662 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2663 {
2664 warning_at (loc, OPT_Wc___compat,
2665 ("enum constant defined in struct or union "
2666 "is not visible in C++"));
2667 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2668 }
2669
7e585d16
ZW
2670 ref = DECL_INITIAL (ref);
2671 TREE_CONSTANT (ref) = 1;
2672 }
6a29edea 2673 else if (current_function_decl != 0
4b1e44be 2674 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2675 && (TREE_CODE (ref) == VAR_DECL
2676 || TREE_CODE (ref) == PARM_DECL
2677 || TREE_CODE (ref) == FUNCTION_DECL))
2678 {
2679 tree context = decl_function_context (ref);
2f6e4e97 2680
6a29edea
EB
2681 if (context != 0 && context != current_function_decl)
2682 DECL_NONLOCAL (ref) = 1;
2683 }
71113fcd
GK
2684 /* C99 6.7.4p3: An inline definition of a function with external
2685 linkage ... shall not contain a reference to an identifier with
2686 internal linkage. */
2687 else if (current_function_decl != 0
2688 && DECL_DECLARED_INLINE_P (current_function_decl)
2689 && DECL_EXTERNAL (current_function_decl)
2690 && VAR_OR_FUNCTION_DECL_P (ref)
2691 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2692 && ! TREE_PUBLIC (ref)
2693 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2694 record_inline_static (loc, current_function_decl, ref,
2695 csi_internal);
7e585d16
ZW
2696
2697 return ref;
2698}
2699
bc4b653b
JM
2700/* Record details of decls possibly used inside sizeof or typeof. */
2701struct maybe_used_decl
2702{
2703 /* The decl. */
2704 tree decl;
2705 /* The level seen at (in_sizeof + in_typeof). */
2706 int level;
2707 /* The next one at this level or above, or NULL. */
2708 struct maybe_used_decl *next;
2709};
2710
2711static struct maybe_used_decl *maybe_used_decls;
2712
2713/* Record that DECL, an undefined static function reference seen
2714 inside sizeof or typeof, might be used if the operand of sizeof is
2715 a VLA type or the operand of typeof is a variably modified
2716 type. */
2717
4e2fb7de 2718static void
bc4b653b
JM
2719record_maybe_used_decl (tree decl)
2720{
2721 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2722 t->decl = decl;
2723 t->level = in_sizeof + in_typeof;
2724 t->next = maybe_used_decls;
2725 maybe_used_decls = t;
2726}
2727
2728/* Pop the stack of decls possibly used inside sizeof or typeof. If
2729 USED is false, just discard them. If it is true, mark them used
2730 (if no longer inside sizeof or typeof) or move them to the next
2731 level up (if still inside sizeof or typeof). */
2732
2733void
2734pop_maybe_used (bool used)
2735{
2736 struct maybe_used_decl *p = maybe_used_decls;
2737 int cur_level = in_sizeof + in_typeof;
2738 while (p && p->level > cur_level)
2739 {
2740 if (used)
2741 {
2742 if (cur_level == 0)
2743 C_DECL_USED (p->decl) = 1;
2744 else
2745 p->level = cur_level;
2746 }
2747 p = p->next;
2748 }
2749 if (!used || cur_level == 0)
2750 maybe_used_decls = p;
2751}
2752
2753/* Return the result of sizeof applied to EXPR. */
2754
2755struct c_expr
c2255bc4 2756c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2757{
2758 struct c_expr ret;
ad97f4be
JM
2759 if (expr.value == error_mark_node)
2760 {
2761 ret.value = error_mark_node;
2762 ret.original_code = ERROR_MARK;
6866c6e8 2763 ret.original_type = NULL;
ad97f4be
JM
2764 pop_maybe_used (false);
2765 }
2766 else
2767 {
928c19bb 2768 bool expr_const_operands = true;
773ec47f
MP
2769
2770 if (TREE_CODE (expr.value) == PARM_DECL
2771 && C_ARRAY_PARAMETER (expr.value))
2772 {
2773 if (warning_at (loc, OPT_Wsizeof_array_argument,
2774 "%<sizeof%> on array function parameter %qE will "
2775 "return size of %qT", expr.value,
2776 expr.original_type))
2777 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2778 }
928c19bb
JM
2779 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2780 &expr_const_operands);
c2255bc4 2781 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2782 c_last_sizeof_arg = expr.value;
2783 ret.original_code = SIZEOF_EXPR;
6866c6e8 2784 ret.original_type = NULL;
928c19bb 2785 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2786 {
2787 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2788 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2789 folded_expr, ret.value);
2790 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2791 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2792 }
928c19bb 2793 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2794 }
bc4b653b
JM
2795 return ret;
2796}
2797
2798/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2799 name passed to sizeof (rather than the type itself). LOC is the
2800 location of the original expression. */
bc4b653b
JM
2801
2802struct c_expr
c2255bc4 2803c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2804{
2805 tree type;
2806 struct c_expr ret;
928c19bb
JM
2807 tree type_expr = NULL_TREE;
2808 bool type_expr_const = true;
2809 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2810 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2811 c_last_sizeof_arg = type;
2812 ret.original_code = SIZEOF_EXPR;
6866c6e8 2813 ret.original_type = NULL;
24070fcb
JM
2814 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2815 && c_vla_type_p (type))
928c19bb 2816 {
24070fcb
JM
2817 /* If the type is a [*] array, it is a VLA but is represented as
2818 having a size of zero. In such a case we must ensure that
2819 the result of sizeof does not get folded to a constant by
2820 c_fully_fold, because if the size is evaluated the result is
2821 not constant and so constraints on zero or negative size
2822 arrays must not be applied when this sizeof call is inside
2823 another array declarator. */
2824 if (!type_expr)
2825 type_expr = integer_zero_node;
928c19bb
JM
2826 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2827 type_expr, ret.value);
2828 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2829 }
16464cc1
VR
2830 pop_maybe_used (type != error_mark_node
2831 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2832 return ret;
2833}
2834
400fbf9f 2835/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2836 The function call is at LOC.
400fbf9f
JW
2837 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2838 TREE_VALUE of each node is a parameter-expression.
2839 FUNCTION's data type may be a function type or a pointer-to-function. */
2840
2841tree
c2255bc4 2842build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2843{
9771b263 2844 vec<tree, va_gc> *v;
bbbbb16a
ILT
2845 tree ret;
2846
9771b263 2847 vec_alloc (v, list_length (params));
bbbbb16a 2848 for (; params; params = TREE_CHAIN (params))
9771b263 2849 v->quick_push (TREE_VALUE (params));
8edbfaa6 2850 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2851 vec_free (v);
bbbbb16a
ILT
2852 return ret;
2853}
2854
ae52741c
MLI
2855/* Give a note about the location of the declaration of DECL. */
2856
2857static void inform_declaration (tree decl)
2858{
2859 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2860 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2861}
2862
bbbbb16a
ILT
2863/* Build a function call to function FUNCTION with parameters PARAMS.
2864 ORIGTYPES, if not NULL, is a vector of types; each element is
2865 either NULL or the original type of the corresponding element in
2866 PARAMS. The original type may differ from TREE_TYPE of the
2867 parameter for enums. FUNCTION's data type may be a function type
2868 or pointer-to-function. This function changes the elements of
2869 PARAMS. */
2870
2871tree
81e5eca8
MP
2872build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2873 tree function, vec<tree, va_gc> *params,
9771b263 2874 vec<tree, va_gc> *origtypes)
400fbf9f 2875{
b3694847 2876 tree fntype, fundecl = 0;
4977bab6 2877 tree name = NULL_TREE, result;
c96f4f73 2878 tree tem;
94a0dd7b
SL
2879 int nargs;
2880 tree *argarray;
b8698a0f 2881
400fbf9f 2882
fc76e425 2883 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2884 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2885
2886 /* Convert anything with function type to a pointer-to-function. */
2887 if (TREE_CODE (function) == FUNCTION_DECL)
2888 {
2889 name = DECL_NAME (function);
0a35513e
AH
2890
2891 if (flag_tm)
2892 tm_malloc_replacement (function);
a5eadacc 2893 fundecl = function;
86951993
AM
2894 /* Atomic functions have type checking/casting already done. They are
2895 often rewritten and don't match the original parameter list. */
2896 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2897 origtypes = NULL;
36536d79 2898
b72271b9 2899 if (flag_cilkplus
36536d79
BI
2900 && is_cilkplus_reduce_builtin (function))
2901 origtypes = NULL;
400fbf9f 2902 }
f2a71bbc 2903 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2904 function = function_to_pointer_conversion (loc, function);
400fbf9f 2905
6e955430
ZL
2906 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2907 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2908 if (params && !params->is_empty ())
2909 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2910
928c19bb
JM
2911 function = c_fully_fold (function, false, NULL);
2912
400fbf9f
JW
2913 fntype = TREE_TYPE (function);
2914
2915 if (TREE_CODE (fntype) == ERROR_MARK)
2916 return error_mark_node;
2917
2918 if (!(TREE_CODE (fntype) == POINTER_TYPE
2919 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2920 {
ae52741c
MLI
2921 if (!flag_diagnostics_show_caret)
2922 error_at (loc,
2923 "called object %qE is not a function or function pointer",
2924 function);
2925 else if (DECL_P (function))
2926 {
2927 error_at (loc,
2928 "called object %qD is not a function or function pointer",
2929 function);
2930 inform_declaration (function);
2931 }
2932 else
2933 error_at (loc,
2934 "called object is not a function or function pointer");
400fbf9f
JW
2935 return error_mark_node;
2936 }
2937
5ce89b2e
JM
2938 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2939 current_function_returns_abnormally = 1;
2940
400fbf9f
JW
2941 /* fntype now gets the type of function pointed to. */
2942 fntype = TREE_TYPE (fntype);
2943
ab4194da
JM
2944 /* Convert the parameters to the types declared in the
2945 function prototype, or apply default promotions. */
2946
81e5eca8
MP
2947 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2948 origtypes, function, fundecl);
ab4194da
JM
2949 if (nargs < 0)
2950 return error_mark_node;
2951
c96f4f73 2952 /* Check that the function is called through a compatible prototype.
fa337f3a 2953 If it is not, warn. */
1043771b 2954 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2955 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2956 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2957 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2958 {
2959 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
2960
2961 /* This situation leads to run-time undefined behavior. We can't,
2962 therefore, simply error unless we can prove that all possible
2963 executions of the program must execute the code. */
fa337f3a 2964 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 2965
fa337f3a
RB
2966 if (VOID_TYPE_P (return_type)
2967 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2968 pedwarn (loc, 0,
2969 "function with qualified void return type called");
2970 }
c96f4f73 2971
9771b263 2972 argarray = vec_safe_address (params);
bbbbb16a 2973
83322951
RG
2974 /* Check that arguments to builtin functions match the expectations. */
2975 if (fundecl
2976 && DECL_BUILT_IN (fundecl)
2977 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2978 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2979 return error_mark_node;
400fbf9f 2980
83322951 2981 /* Check that the arguments to the function are valid. */
dde05067 2982 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2983
928c19bb
JM
2984 if (name != NULL_TREE
2985 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2986 {
928c19bb 2987 if (require_constant_value)
b8698a0f 2988 result =
db3927fb
AH
2989 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2990 function, nargs, argarray);
928c19bb 2991 else
db3927fb
AH
2992 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2993 function, nargs, argarray);
928c19bb
JM
2994 if (TREE_CODE (result) == NOP_EXPR
2995 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2996 STRIP_TYPE_NOPS (result);
bf730f15
RS
2997 }
2998 else
db3927fb
AH
2999 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3000 function, nargs, argarray);
b0b3afb2 3001
71653180 3002 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
3003 {
3004 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 3005 pedwarn (loc, 0,
3ce62965
JM
3006 "function with qualified void return type called");
3007 return result;
3008 }
1eb8759b 3009 return require_complete_type (result);
400fbf9f 3010}
8edbfaa6
JJ
3011
3012/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3013
3014tree
3015c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3016 tree function, vec<tree, va_gc> *params,
3017 vec<tree, va_gc> *origtypes)
3018{
3019 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3020 STRIP_TYPE_NOPS (function);
3021
3022 /* Convert anything with function type to a pointer-to-function. */
3023 if (TREE_CODE (function) == FUNCTION_DECL)
3024 {
3025 /* Implement type-directed function overloading for builtins.
3026 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3027 handle all the type checking. The result is a complete expression
3028 that implements this function call. */
3029 tree tem = resolve_overloaded_builtin (loc, function, params);
3030 if (tem)
3031 return tem;
3032 }
3033 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3034}
400fbf9f 3035\f
bbbbb16a
ILT
3036/* Convert the argument expressions in the vector VALUES
3037 to the types in the list TYPELIST.
400fbf9f
JW
3038
3039 If TYPELIST is exhausted, or when an element has NULL as its type,
3040 perform the default conversions.
3041
bbbbb16a
ILT
3042 ORIGTYPES is the original types of the expressions in VALUES. This
3043 holds the type of enum values which have been converted to integral
3044 types. It may be NULL.
400fbf9f 3045
03dafa61
JM
3046 FUNCTION is a tree for the called function. It is used only for
3047 error messages, where it is formatted with %qE.
400fbf9f
JW
3048
3049 This is also where warnings about wrong number of args are generated.
3050
81e5eca8
MP
3051 ARG_LOC are locations of function arguments (if any).
3052
94a0dd7b 3053 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3054 than the length of VALUES in some error situations), or -1 on
3055 failure. */
94a0dd7b
SL
3056
3057static int
81e5eca8
MP
3058convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3059 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3060 tree function, tree fundecl)
400fbf9f 3061{
bbbbb16a
ILT
3062 tree typetail, val;
3063 unsigned int parmnum;
06302a02 3064 bool error_args = false;
b5d32c25 3065 const bool type_generic = fundecl
81e5eca8 3066 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3067 bool type_generic_remove_excess_precision = false;
03dafa61 3068 tree selector;
03dafa61 3069
2ac2f164
JM
3070 /* Change pointer to function to the function itself for
3071 diagnostics. */
03dafa61
JM
3072 if (TREE_CODE (function) == ADDR_EXPR
3073 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3074 function = TREE_OPERAND (function, 0);
03dafa61
JM
3075
3076 /* Handle an ObjC selector specially for diagnostics. */
3077 selector = objc_message_selector ();
400fbf9f 3078
8ce94e44
JM
3079 /* For type-generic built-in functions, determine whether excess
3080 precision should be removed (classification) or not
3081 (comparison). */
3082 if (type_generic
3083 && DECL_BUILT_IN (fundecl)
3084 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3085 {
3086 switch (DECL_FUNCTION_CODE (fundecl))
3087 {
3088 case BUILT_IN_ISFINITE:
3089 case BUILT_IN_ISINF:
3090 case BUILT_IN_ISINF_SIGN:
3091 case BUILT_IN_ISNAN:
3092 case BUILT_IN_ISNORMAL:
3093 case BUILT_IN_FPCLASSIFY:
3094 type_generic_remove_excess_precision = true;
3095 break;
3096
3097 default:
3098 type_generic_remove_excess_precision = false;
3099 break;
3100 }
3101 }
b72271b9 3102 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3103 return vec_safe_length (values);
8ce94e44 3104
400fbf9f 3105 /* Scan the given expressions and types, producing individual
bbbbb16a 3106 converted arguments. */
400fbf9f 3107
bbbbb16a 3108 for (typetail = typelist, parmnum = 0;
9771b263 3109 values && values->iterate (parmnum, &val);
bbbbb16a 3110 ++parmnum)
400fbf9f 3111 {
b3694847 3112 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3113 tree valtype = TREE_TYPE (val);
03dafa61
JM
3114 tree rname = function;
3115 int argnum = parmnum + 1;
4d3e6fae 3116 const char *invalid_func_diag;
8ce94e44 3117 bool excess_precision = false;
928c19bb 3118 bool npc;
bbbbb16a 3119 tree parmval;
5c1bc275
MP
3120 /* Some __atomic_* builtins have additional hidden argument at
3121 position 0. */
3122 location_t ploc
3123 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3124 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3125 : input_location;
400fbf9f
JW
3126
3127 if (type == void_type_node)
3128 {
19dc6d01 3129 if (selector)
68fca595 3130 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3131 else
68fca595 3132 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3133 inform_declaration (fundecl);
94a0dd7b 3134 return parmnum;
400fbf9f
JW
3135 }
3136
03dafa61
JM
3137 if (selector && argnum > 2)
3138 {
3139 rname = selector;
3140 argnum -= 2;
3141 }
3142
928c19bb 3143 npc = null_pointer_constant_p (val);
8ce94e44
JM
3144
3145 /* If there is excess precision and a prototype, convert once to
3146 the required type rather than converting via the semantic
3147 type. Likewise without a prototype a float value represented
3148 as long double should be converted once to double. But for
3149 type-generic classification functions excess precision must
3150 be removed here. */
3151 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3152 && (type || !type_generic || !type_generic_remove_excess_precision))
3153 {
3154 val = TREE_OPERAND (val, 0);
3155 excess_precision = true;
3156 }
928c19bb 3157 val = c_fully_fold (val, false, NULL);
ed248cf7 3158 STRIP_TYPE_NOPS (val);
400fbf9f 3159
400fbf9f
JW
3160 val = require_complete_type (val);
3161
3162 if (type != 0)
3163 {
3164 /* Formal parm type is specified by a function prototype. */
400fbf9f 3165
20913689 3166 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3167 {
5c1bc275
MP
3168 error_at (ploc, "type of formal parameter %d is incomplete",
3169 parmnum + 1);
400fbf9f
JW
3170 parmval = val;
3171 }
3172 else
3173 {
bbbbb16a
ILT
3174 tree origtype;
3175
d45cf215
RS
3176 /* Optionally warn about conversions that
3177 differ from the default conversions. */
05170031 3178 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3179 {
e3a64162 3180 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3181
aae43c5f 3182 if (INTEGRAL_TYPE_P (type)
8ce94e44 3183 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3184 warning_at (ploc, OPT_Wtraditional_conversion,
3185 "passing argument %d of %qE as integer rather "
3186 "than floating due to prototype",
3187 argnum, rname);
03829ad2 3188 if (INTEGRAL_TYPE_P (type)
8ce94e44 3189 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3190 warning_at (ploc, OPT_Wtraditional_conversion,
3191 "passing argument %d of %qE as integer rather "
3192 "than complex due to prototype",
3193 argnum, rname);
aae43c5f 3194 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3195 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3196 warning_at (ploc, OPT_Wtraditional_conversion,
3197 "passing argument %d of %qE as complex rather "
3198 "than floating due to prototype",
3199 argnum, rname);
400fbf9f 3200 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3201 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3202 warning_at (ploc, OPT_Wtraditional_conversion,
3203 "passing argument %d of %qE as floating rather "
3204 "than integer due to prototype",
3205 argnum, rname);
03829ad2 3206 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3207 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3208 warning_at (ploc, OPT_Wtraditional_conversion,
3209 "passing argument %d of %qE as complex rather "
3210 "than integer due to prototype",
3211 argnum, rname);
aae43c5f 3212 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3213 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3214 warning_at (ploc, OPT_Wtraditional_conversion,
3215 "passing argument %d of %qE as floating rather "
3216 "than complex due to prototype",
3217 argnum, rname);
aae43c5f
RK
3218 /* ??? At some point, messages should be written about
3219 conversions between complex types, but that's too messy
3220 to do now. */
d45cf215 3221 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3222 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3223 {
3224 /* Warn if any argument is passed as `float',
047de90b 3225 since without a prototype it would be `double'. */
9a8ce21f
JG
3226 if (formal_prec == TYPE_PRECISION (float_type_node)
3227 && type != dfloat32_type_node)
5c1bc275
MP
3228 warning_at (ploc, 0,
3229 "passing argument %d of %qE as %<float%> "
3230 "rather than %<double%> due to prototype",
3231 argnum, rname);
9a8ce21f
JG
3232
3233 /* Warn if mismatch between argument and prototype
3234 for decimal float types. Warn of conversions with
3235 binary float types and of precision narrowing due to
3236 prototype. */
8ce94e44 3237 else if (type != valtype
9a8ce21f
JG
3238 && (type == dfloat32_type_node
3239 || type == dfloat64_type_node
c22cacf3 3240 || type == dfloat128_type_node
8ce94e44
JM
3241 || valtype == dfloat32_type_node
3242 || valtype == dfloat64_type_node
3243 || valtype == dfloat128_type_node)
c22cacf3 3244 && (formal_prec
8ce94e44 3245 <= TYPE_PRECISION (valtype)
9a8ce21f 3246 || (type == dfloat128_type_node
8ce94e44 3247 && (valtype
c22cacf3 3248 != dfloat64_type_node
8ce94e44 3249 && (valtype
9a8ce21f
JG
3250 != dfloat32_type_node)))
3251 || (type == dfloat64_type_node
8ce94e44 3252 && (valtype
9a8ce21f 3253 != dfloat32_type_node))))
5c1bc275
MP
3254 warning_at (ploc, 0,
3255 "passing argument %d of %qE as %qT "
3256 "rather than %qT due to prototype",
3257 argnum, rname, type, valtype);
9a8ce21f 3258
d45cf215 3259 }
3ed56f8a
KG
3260 /* Detect integer changing in width or signedness.
3261 These warnings are only activated with
05170031
MLI
3262 -Wtraditional-conversion, not with -Wtraditional. */
3263 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3264 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3265 {
d45cf215
RS
3266 tree would_have_been = default_conversion (val);
3267 tree type1 = TREE_TYPE (would_have_been);
3268
754a4d82 3269 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3270 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3271 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3272 /* No warning if function asks for enum
3273 and the actual arg is that enum type. */
3274 ;
3275 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3276 warning_at (ploc, OPT_Wtraditional_conversion,
3277 "passing argument %d of %qE "
3278 "with different width due to prototype",
3279 argnum, rname);
8df83eae 3280 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3281 ;
800cd3b9
RS
3282 /* Don't complain if the formal parameter type
3283 is an enum, because we can't tell now whether
3284 the value was an enum--even the same enum. */
3285 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3286 ;
400fbf9f
JW
3287 else if (TREE_CODE (val) == INTEGER_CST
3288 && int_fits_type_p (val, type))
3289 /* Change in signedness doesn't matter
3290 if a constant value is unaffected. */
3291 ;
ce9895ae
RS
3292 /* If the value is extended from a narrower
3293 unsigned type, it doesn't matter whether we
3294 pass it as signed or unsigned; the value
3295 certainly is the same either way. */
8ce94e44
JM
3296 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3297 && TYPE_UNSIGNED (valtype))
ce9895ae 3298 ;
8df83eae 3299 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3300 warning_at (ploc, OPT_Wtraditional_conversion,
3301 "passing argument %d of %qE "
3302 "as unsigned due to prototype",
3303 argnum, rname);
3ed56f8a 3304 else
5c1bc275
MP
3305 warning_at (ploc, OPT_Wtraditional_conversion,
3306 "passing argument %d of %qE "
3307 "as signed due to prototype",
3308 argnum, rname);
400fbf9f
JW
3309 }
3310 }
3311
8ce94e44
JM
3312 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3313 sake of better warnings from convert_and_check. */
3314 if (excess_precision)
3315 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3316 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3317 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3318 val, origtype, ic_argpass,
3319 npc, fundecl, function,
2ac2f164 3320 parmnum + 1);
2f6e4e97 3321
61f71b34 3322 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3323 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3324 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3325 parmval = default_conversion (parmval);
400fbf9f 3326 }
400fbf9f 3327 }
8ce94e44
JM
3328 else if (TREE_CODE (valtype) == REAL_TYPE
3329 && (TYPE_PRECISION (valtype)
2531a1d9 3330 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3331 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3332 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3333 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3334 {
3335 if (type_generic)
bbbbb16a 3336 parmval = val;
b5d32c25 3337 else
0a0b3574
MM
3338 {
3339 /* Convert `float' to `double'. */
3340 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3341 warning_at (ploc, OPT_Wdouble_promotion,
3342 "implicit conversion from %qT to %qT when passing "
3343 "argument to function",
3344 valtype, double_type_node);
0a0b3574
MM
3345 parmval = convert (double_type_node, val);
3346 }
b5d32c25 3347 }
8ce94e44
JM
3348 else if (excess_precision && !type_generic)
3349 /* A "double" argument with excess precision being passed
3350 without a prototype or in variable arguments. */
bbbbb16a 3351 parmval = convert (valtype, val);
c22cacf3
MS
3352 else if ((invalid_func_diag =
3353 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3354 {
3355 error (invalid_func_diag);
94a0dd7b 3356 return -1;
4d3e6fae 3357 }
400fbf9f
JW
3358 else
3359 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3360 parmval = default_conversion (val);
3361
9771b263 3362 (*values)[parmnum] = parmval;
06302a02
JJ
3363 if (parmval == error_mark_node)
3364 error_args = true;
400fbf9f
JW
3365
3366 if (typetail)
3367 typetail = TREE_CHAIN (typetail);
3368 }
3369
9771b263 3370 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3371
400fbf9f 3372 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3373 {
68fca595 3374 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3375 inform_declaration (fundecl);
3376 return -1;
3789b316 3377 }
400fbf9f 3378
06302a02 3379 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3380}
3381\f
43f6dfd3
RS
3382/* This is the entry point used by the parser to build unary operators
3383 in the input. CODE, a tree_code, specifies the unary operator, and
3384 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3385 CONVERT_EXPR for code.
3386
3387 LOC is the location to use for the tree generated.
3388*/
43f6dfd3
RS
3389
3390struct c_expr
c2255bc4 3391parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3392{
3393 struct c_expr result;
3394
c9f9eb5d 3395 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3396 result.original_code = code;
6866c6e8
ILT
3397 result.original_type = NULL;
3398
59c0753d 3399 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3400 overflow_warning (loc, result.value);
59c0753d 3401
43f6dfd3
RS
3402 return result;
3403}
3404
3405/* This is the entry point used by the parser to build binary operators
3406 in the input. CODE, a tree_code, specifies the binary operator, and
3407 ARG1 and ARG2 are the operands. In addition to constructing the
3408 expression, we check for operands that were written with other binary
ba47d38d
AH
3409 operators in a way that is likely to confuse the user.
3410
3411 LOCATION is the location of the binary operator. */
edc7c4ec 3412
487a92fe 3413struct c_expr
ba47d38d
AH
3414parser_build_binary_op (location_t location, enum tree_code code,
3415 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3416{
487a92fe 3417 struct c_expr result;
400fbf9f 3418
487a92fe
JM
3419 enum tree_code code1 = arg1.original_code;
3420 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3421 tree type1 = (arg1.original_type
3422 ? arg1.original_type
3423 : TREE_TYPE (arg1.value));
3424 tree type2 = (arg2.original_type
3425 ? arg2.original_type
3426 : TREE_TYPE (arg2.value));
400fbf9f 3427
ba47d38d
AH
3428 result.value = build_binary_op (location, code,
3429 arg1.value, arg2.value, 1);
487a92fe 3430 result.original_code = code;
6866c6e8 3431 result.original_type = NULL;
58bf601b 3432
487a92fe
JM
3433 if (TREE_CODE (result.value) == ERROR_MARK)
3434 return result;
400fbf9f 3435
ba47d38d
AH
3436 if (location != UNKNOWN_LOCATION)
3437 protected_set_expr_location (result.value, location);
3438
400fbf9f 3439 /* Check for cases such as x+y<<z which users are likely
487a92fe 3440 to misinterpret. */
400fbf9f 3441 if (warn_parentheses)
393e8e8b
MP
3442 warn_about_parentheses (location, code, code1, arg1.value, code2,
3443 arg2.value);
001af587 3444
ca409efd 3445 if (warn_logical_op)
393e8e8b 3446 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3447 code1, arg1.value, code2, arg2.value);
63a08740 3448
742938c9 3449 if (warn_logical_not_paren
47c2554f
MP
3450 && code1 == TRUTH_NOT_EXPR
3451 && code2 != TRUTH_NOT_EXPR)
59ea0364 3452 warn_logical_not_parentheses (location, code, arg2.value);
742938c9 3453
e994a705
RS
3454 /* Warn about comparisons against string literals, with the exception
3455 of testing for equality or inequality of a string literal with NULL. */
3456 if (code == EQ_EXPR || code == NE_EXPR)
3457 {
3458 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3459 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3460 warning_at (location, OPT_Waddress,
3461 "comparison with string literal results in unspecified behavior");
e994a705
RS
3462 }
3463 else if (TREE_CODE_CLASS (code) == tcc_comparison
3464 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3465 warning_at (location, OPT_Waddress,
3466 "comparison with string literal results in unspecified behavior");
e994a705 3467
b8698a0f
L
3468 if (TREE_OVERFLOW_P (result.value)
3469 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3470 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3471 overflow_warning (location, result.value);
400fbf9f 3472
6866c6e8
ILT
3473 /* Warn about comparisons of different enum types. */
3474 if (warn_enum_compare
3475 && TREE_CODE_CLASS (code) == tcc_comparison
3476 && TREE_CODE (type1) == ENUMERAL_TYPE
3477 && TREE_CODE (type2) == ENUMERAL_TYPE
3478 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3479 warning_at (location, OPT_Wenum_compare,
3480 "comparison between %qT and %qT",
3481 type1, type2);
3482
400fbf9f
JW
3483 return result;
3484}
3e4093b6 3485\f
3e4093b6
RS
3486/* Return a tree for the difference of pointers OP0 and OP1.
3487 The resulting tree has type int. */
293c9fdd 3488
3e4093b6 3489static tree
db3927fb 3490pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3491{
3e4093b6 3492 tree restype = ptrdiff_type_node;
36c5e70a 3493 tree result, inttype;
400fbf9f 3494
36c5e70a
BE
3495 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3496 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3497 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3498 tree orig_op1 = op1;
400fbf9f 3499
36c5e70a
BE
3500 /* If the operands point into different address spaces, we need to
3501 explicitly convert them to pointers into the common address space
3502 before we can subtract the numerical address values. */
3503 if (as0 != as1)
3504 {
3505 addr_space_t as_common;
3506 tree common_type;
3507
3508 /* Determine the common superset address space. This is guaranteed
3509 to exist because the caller verified that comp_target_types
3510 returned non-zero. */
3511 if (!addr_space_superset (as0, as1, &as_common))
3512 gcc_unreachable ();
3513
3514 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3515 op0 = convert (common_type, op0);
3516 op1 = convert (common_type, op1);
3517 }
3518
3519 /* Determine integer type to perform computations in. This will usually
3520 be the same as the result type (ptrdiff_t), but may need to be a wider
3521 type if pointers for the address space are wider than ptrdiff_t. */
3522 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3523 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3524 else
3525 inttype = restype;
3526
fcf73884 3527 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3528 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3529 "pointer of type %<void *%> used in subtraction");
3530 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3531 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3532 "pointer to a function used in subtraction");
400fbf9f 3533
3e4093b6
RS
3534 /* First do the subtraction as integers;
3535 then drop through to build the divide operator.
3536 Do not do default conversions on the minus operator
3537 in case restype is a short type. */
400fbf9f 3538
db3927fb 3539 op0 = build_binary_op (loc,
36c5e70a
BE
3540 MINUS_EXPR, convert (inttype, op0),
3541 convert (inttype, op1), 0);
3e4093b6
RS
3542 /* This generates an error if op1 is pointer to incomplete type. */
3543 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3544 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3545
3e4093b6 3546 op1 = c_size_in_bytes (target_type);
400fbf9f 3547
f04dda30
MP
3548 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3549 error_at (loc, "arithmetic on pointer to an empty aggregate");
3550
3e4093b6 3551 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3552 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3553 op0, convert (inttype, op1));
3554
3555 /* Convert to final result type if necessary. */
3556 return convert (restype, result);
3e4093b6
RS
3557}
3558\f
267bac10
JM
3559/* Expand atomic compound assignments into an approriate sequence as
3560 specified by the C11 standard section 6.5.16.2.
3561 given
3562 _Atomic T1 E1
3563 T2 E2
3564 E1 op= E2
3565
3566 This sequence is used for all types for which these operations are
3567 supported.
3568
3569 In addition, built-in versions of the 'fe' prefixed routines may
3570 need to be invoked for floating point (real, complex or vector) when
3571 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3572
3573 T1 newval;
3574 T1 old;
3575 T1 *addr
3576 T2 val
3577 fenv_t fenv
3578
3579 addr = &E1;
3580 val = (E2);
3581 __atomic_load (addr, &old, SEQ_CST);
3582 feholdexcept (&fenv);
3583loop:
3584 newval = old op val;
3585 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3586 SEQ_CST))
3587 goto done;
3588 feclearexcept (FE_ALL_EXCEPT);
3589 goto loop:
3590done:
3591 feupdateenv (&fenv);
3592
3593 Also note that the compiler is simply issuing the generic form of
3594 the atomic operations. This requires temp(s) and has their address
3595 taken. The atomic processing is smart enough to figure out when the
3596 size of an object can utilize a lock-free version, and convert the
3597 built-in call to the appropriate lock-free routine. The optimizers
3598 will then dispose of any temps that are no longer required, and
3599 lock-free implementations are utilized as long as there is target
3600 support for the required size.
3601
3602 If the operator is NOP_EXPR, then this is a simple assignment, and
3603 an __atomic_store is issued to perform the assignment rather than
3604 the above loop.
3605
3606*/
3607
3608/* Build an atomic assignment at LOC, expanding into the proper
3609 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3610 the result of the operation, unless RETURN_OLD_P in which case
3611 return the old value of LHS (this is only for postincrement and
3612 postdecrement). */
3613static tree
3614build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3615 tree rhs, bool return_old_p)
3616{
3617 tree fndecl, func_call;
3618 vec<tree, va_gc> *params;
3619 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3620 tree old, old_addr;
3621 tree compound_stmt;
3622 tree stmt, goto_stmt;
3623 tree loop_label, loop_decl, done_label, done_decl;
3624
3625 tree lhs_type = TREE_TYPE (lhs);
3626 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3627 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3628 tree rhs_type = TREE_TYPE (rhs);
3629
3630 gcc_assert (TYPE_ATOMIC (lhs_type));
3631
3632 if (return_old_p)
3633 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3634
3635 /* Allocate enough vector items for a compare_exchange. */
3636 vec_alloc (params, 6);
3637
3638 /* Create a compound statement to hold the sequence of statements
3639 with a loop. */
3640 compound_stmt = c_begin_compound_stmt (false);
3641
3642 /* Fold the RHS if it hasn't already been folded. */
3643 if (modifycode != NOP_EXPR)
3644 rhs = c_fully_fold (rhs, false, NULL);
3645
3646 /* Remove the qualifiers for the rest of the expressions and create
3647 the VAL temp variable to hold the RHS. */
3648 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3649 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
b731b390 3650 val = create_tmp_var (nonatomic_rhs_type);
267bac10 3651 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3652 TREE_NO_WARNING (val) = 1;
267bac10
JM
3653 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3654 SET_EXPR_LOCATION (rhs, loc);
3655 add_stmt (rhs);
3656
3657 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3658 an atomic_store. */
3659 if (modifycode == NOP_EXPR)
3660 {
3661 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3662 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3663 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3664 params->quick_push (lhs_addr);
3665 params->quick_push (rhs);
3666 params->quick_push (seq_cst);
8edbfaa6 3667 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3668 add_stmt (func_call);
3669
3670 /* Finish the compound statement. */
3671 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3672
3673 /* VAL is the value which was stored, return a COMPOUND_STMT of
3674 the statement and that value. */
3675 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3676 }
3677
3678 /* Create the variables and labels required for the op= form. */
b731b390 3679 old = create_tmp_var (nonatomic_lhs_type);
267bac10 3680 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3681 TREE_ADDRESSABLE (old) = 1;
3682 TREE_NO_WARNING (old) = 1;
267bac10 3683
b731b390 3684 newval = create_tmp_var (nonatomic_lhs_type);
267bac10
JM
3685 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3686 TREE_ADDRESSABLE (newval) = 1;
3687
3688 loop_decl = create_artificial_label (loc);
3689 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3690
3691 done_decl = create_artificial_label (loc);
3692 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3693
3694 /* __atomic_load (addr, &old, SEQ_CST). */
3695 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3696 params->quick_push (lhs_addr);
3697 params->quick_push (old_addr);
3698 params->quick_push (seq_cst);
8edbfaa6 3699 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3700 add_stmt (func_call);
3701 params->truncate (0);
3702
3703 /* Create the expressions for floating-point environment
3704 manipulation, if required. */
3705 bool need_fenv = (flag_trapping_math
3706 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3707 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3708 if (need_fenv)
3709 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3710
3711 if (hold_call)
3712 add_stmt (hold_call);
3713
3714 /* loop: */
3715 add_stmt (loop_label);
3716
3717 /* newval = old + val; */
3718 rhs = build_binary_op (loc, modifycode, old, val, 1);
68fca595
MP
3719 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3720 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3721 NULL_TREE, 0);
3722 if (rhs != error_mark_node)
3723 {
3724 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3725 SET_EXPR_LOCATION (rhs, loc);
3726 add_stmt (rhs);
3727 }
3728
3729 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3730 goto done; */
3731 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3732 params->quick_push (lhs_addr);
3733 params->quick_push (old_addr);
3734 params->quick_push (newval_addr);
3735 params->quick_push (integer_zero_node);
3736 params->quick_push (seq_cst);
3737 params->quick_push (seq_cst);
8edbfaa6 3738 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3739
3740 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3741 SET_EXPR_LOCATION (goto_stmt, loc);
3742
3743 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3744 SET_EXPR_LOCATION (stmt, loc);
3745 add_stmt (stmt);
3746
3747 if (clear_call)
3748 add_stmt (clear_call);
3749
3750 /* goto loop; */
3751 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3752 SET_EXPR_LOCATION (goto_stmt, loc);
3753 add_stmt (goto_stmt);
3754
3755 /* done: */
3756 add_stmt (done_label);
3757
3758 if (update_call)
3759 add_stmt (update_call);
3760
3761 /* Finish the compound statement. */
3762 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3763
3764 /* NEWVAL is the value that was successfully stored, return a
3765 COMPOUND_EXPR of the statement and the appropriate value. */
3766 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3767 return_old_p ? old : newval);
3768}
3769
3e4093b6
RS
3770/* Construct and perhaps optimize a tree representation
3771 for a unary operation. CODE, a tree_code, specifies the operation
3772 and XARG is the operand.
3773 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3774 the default promotions (such as from short to int).
3775 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3776 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3777 arrays to pointers in C99.
3778
3779 LOCATION is the location of the operator. */
400fbf9f 3780
3e4093b6 3781tree
c9f9eb5d
AH
3782build_unary_op (location_t location,
3783 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3784{
3785 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3786 tree arg = xarg;
3787 tree argtype = 0;
808d6eaa 3788 enum tree_code typecode;
3e4093b6 3789 tree val;
c9f9eb5d 3790 tree ret = error_mark_node;
8ce94e44 3791 tree eptype = NULL_TREE;
3e4093b6 3792 int noconvert = flag;
4de67c26 3793 const char *invalid_op_diag;
928c19bb
JM
3794 bool int_operands;
3795
3796 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3797 if (int_operands)
3798 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3799
808d6eaa
JM
3800 if (code != ADDR_EXPR)
3801 arg = require_complete_type (arg);
3802
3803 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3804 if (typecode == ERROR_MARK)
3805 return error_mark_node;
3806 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3807 typecode = INTEGER_TYPE;
6c36d76b 3808
4de67c26
JM
3809 if ((invalid_op_diag
3810 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3811 {
c9f9eb5d 3812 error_at (location, invalid_op_diag);
4de67c26
JM
3813 return error_mark_node;
3814 }
3815
8ce94e44
JM
3816 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3817 {
3818 eptype = TREE_TYPE (arg);
3819 arg = TREE_OPERAND (arg, 0);
3820 }
3821
3e4093b6
RS
3822 switch (code)
3823 {
3824 case CONVERT_EXPR:
3825 /* This is used for unary plus, because a CONVERT_EXPR
3826 is enough to prevent anybody from looking inside for
3827 associativity, but won't generate any code. */
3828 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3829 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3830 || typecode == VECTOR_TYPE))
400fbf9f 3831 {
c9f9eb5d 3832 error_at (location, "wrong type argument to unary plus");
3e4093b6 3833 return error_mark_node;
400fbf9f 3834 }
3e4093b6
RS
3835 else if (!noconvert)
3836 arg = default_conversion (arg);
db3927fb 3837 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3838 break;
3839
3e4093b6
RS
3840 case NEGATE_EXPR:
3841 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3842 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3843 || typecode == VECTOR_TYPE))
3844 {
c9f9eb5d 3845 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3846 return error_mark_node;
3847 }
3848 else if (!noconvert)
3849 arg = default_conversion (arg);
400fbf9f
JW
3850 break;
3851
3e4093b6 3852 case BIT_NOT_EXPR:
462643f0
AP
3853 /* ~ works on integer types and non float vectors. */
3854 if (typecode == INTEGER_TYPE
3855 || (typecode == VECTOR_TYPE
3856 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3857 {
3e4093b6
RS
3858 if (!noconvert)
3859 arg = default_conversion (arg);
03d5b1f5 3860 }
3e4093b6 3861 else if (typecode == COMPLEX_TYPE)
400fbf9f 3862 {
3e4093b6 3863 code = CONJ_EXPR;
c1771a20 3864 pedwarn (location, OPT_Wpedantic,
fcf73884 3865 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3866 if (!noconvert)
3867 arg = default_conversion (arg);
3868 }
3869 else
3870 {
c9f9eb5d 3871 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3872 return error_mark_node;
400fbf9f
JW
3873 }
3874 break;
3875
3e4093b6 3876 case ABS_EXPR:
11017cc7 3877 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3878 {
c9f9eb5d 3879 error_at (location, "wrong type argument to abs");
3e4093b6 3880 return error_mark_node;
400fbf9f 3881 }
3e4093b6
RS
3882 else if (!noconvert)
3883 arg = default_conversion (arg);
400fbf9f
JW
3884 break;
3885
3e4093b6
RS
3886 case CONJ_EXPR:
3887 /* Conjugating a real value is a no-op, but allow it anyway. */
3888 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3889 || typecode == COMPLEX_TYPE))
400fbf9f 3890 {
c9f9eb5d 3891 error_at (location, "wrong type argument to conjugation");
3e4093b6 3892 return error_mark_node;
400fbf9f 3893 }
3e4093b6
RS
3894 else if (!noconvert)
3895 arg = default_conversion (arg);
400fbf9f
JW
3896 break;
3897
3e4093b6 3898 case TRUTH_NOT_EXPR:
ab22c1fa 3899 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3900 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3901 && typecode != COMPLEX_TYPE)
400fbf9f 3902 {
c9f9eb5d
AH
3903 error_at (location,
3904 "wrong type argument to unary exclamation mark");
3e4093b6 3905 return error_mark_node;
400fbf9f 3906 }
a27d595d
JM
3907 if (int_operands)
3908 {
3909 arg = c_objc_common_truthvalue_conversion (location, xarg);
3910 arg = remove_c_maybe_const_expr (arg);
3911 }
3912 else
3913 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3914 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3915 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3916 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3917 location = EXPR_LOCATION (ret);
c9f9eb5d 3918 goto return_build_unary_op;
3e4093b6 3919
3e4093b6 3920 case REALPART_EXPR:
3e4093b6 3921 case IMAGPART_EXPR:
fb52b50a
NF
3922 ret = build_real_imag_expr (location, code, arg);
3923 if (ret == error_mark_node)
3924 return error_mark_node;
8ce94e44
JM
3925 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3926 eptype = TREE_TYPE (eptype);
c9f9eb5d 3927 goto return_build_unary_op;
3e4093b6
RS
3928
3929 case PREINCREMENT_EXPR:
3930 case POSTINCREMENT_EXPR:
3931 case PREDECREMENT_EXPR:
3932 case POSTDECREMENT_EXPR:
3e4093b6 3933
928c19bb
JM
3934 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3935 {
3936 tree inner = build_unary_op (location, code,
3937 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3938 if (inner == error_mark_node)
3939 return error_mark_node;
3940 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3941 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3942 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3943 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3944 goto return_build_unary_op;
3945 }
3946
925e8657
NP
3947 /* Complain about anything that is not a true lvalue. In
3948 Objective-C, skip this check for property_refs. */
f90e8e2e 3949 if (!objc_is_property_ref (arg)
7bd11157
TT
3950 && !lvalue_or_else (location,
3951 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3952 || code == POSTINCREMENT_EXPR)
3953 ? lv_increment
3954 : lv_decrement)))
928c19bb
JM
3955 return error_mark_node;
3956
09639a83
ILT
3957 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3958 {
3959 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3960 warning_at (location, OPT_Wc___compat,
3961 "increment of enumeration value is invalid in C++");
3962 else
3963 warning_at (location, OPT_Wc___compat,
3964 "decrement of enumeration value is invalid in C++");
3965 }
3966
928c19bb
JM
3967 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3968 arg = c_fully_fold (arg, false, NULL);
3969
267bac10
JM
3970 bool atomic_op;
3971 atomic_op = really_atomic_lvalue (arg);
3972
3e4093b6
RS
3973 /* Increment or decrement the real part of the value,
3974 and don't change the imaginary part. */
3975 if (typecode == COMPLEX_TYPE)
400fbf9f 3976 {
3e4093b6
RS
3977 tree real, imag;
3978
c1771a20 3979 pedwarn (location, OPT_Wpedantic,
509c9d60 3980 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3981
267bac10
JM
3982 if (!atomic_op)
3983 {
3984 arg = stabilize_reference (arg);
3985 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3986 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3987 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3988 if (real == error_mark_node || imag == error_mark_node)
3989 return error_mark_node;
3990 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3991 real, imag);
3992 goto return_build_unary_op;
3993 }
400fbf9f 3994 }
3e4093b6
RS
3995
3996 /* Report invalid types. */
3997
ab22c1fa 3998 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 3999 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4000 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4001 {
3e4093b6 4002 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4003 error_at (location, "wrong type argument to increment");
c22cacf3 4004 else
c9f9eb5d 4005 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4006
4007 return error_mark_node;
400fbf9f 4008 }
400fbf9f 4009
3e4093b6
RS
4010 {
4011 tree inc;
400fbf9f 4012
3e4093b6
RS
4013 argtype = TREE_TYPE (arg);
4014
4015 /* Compute the increment. */
4016
4017 if (typecode == POINTER_TYPE)
4018 {
a0e24419 4019 /* If pointer target is an incomplete type,
3e4093b6 4020 we just cannot know how to do the arithmetic. */
b70cef5d 4021 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4022 {
4023 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4024 error_at (location,
a0e24419
MP
4025 "increment of pointer to an incomplete type %qT",
4026 TREE_TYPE (argtype));
3e4093b6 4027 else
c9f9eb5d 4028 error_at (location,
a0e24419
MP
4029 "decrement of pointer to an incomplete type %qT",
4030 TREE_TYPE (argtype));
3e4093b6 4031 }
b70cef5d
JJ
4032 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4033 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4034 {
3e4093b6 4035 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4036 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4037 "wrong type argument to increment");
3e4093b6 4038 else
44d90fe1 4039 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4040 "wrong type argument to decrement");
3e4093b6
RS
4041 }
4042
b70cef5d 4043 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4044 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4045 }
b70cef5d 4046 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4047 {
4048 /* For signed fract types, we invert ++ to -- or
4049 -- to ++, and change inc from 1 to -1, because
4050 it is not possible to represent 1 in signed fract constants.
4051 For unsigned fract types, the result always overflows and
4052 we get an undefined (original) or the maximum value. */
4053 if (code == PREINCREMENT_EXPR)
4054 code = PREDECREMENT_EXPR;
4055 else if (code == PREDECREMENT_EXPR)
4056 code = PREINCREMENT_EXPR;
4057 else if (code == POSTINCREMENT_EXPR)
4058 code = POSTDECREMENT_EXPR;
4059 else /* code == POSTDECREMENT_EXPR */
4060 code = POSTINCREMENT_EXPR;
4061
4062 inc = integer_minus_one_node;
4063 inc = convert (argtype, inc);
4064 }
3e4093b6 4065 else
5be014d5 4066 {
241b71bb
TV
4067 inc = VECTOR_TYPE_P (argtype)
4068 ? build_one_cst (argtype)
4069 : integer_one_node;
5be014d5
AP
4070 inc = convert (argtype, inc);
4071 }
3e4093b6 4072
925e8657
NP
4073 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4074 need to ask Objective-C to build the increment or decrement
4075 expression for it. */
4076 if (objc_is_property_ref (arg))
f90e8e2e 4077 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4078 arg, inc);
4079
3e4093b6 4080 /* Report a read-only lvalue. */
f37acdf9 4081 if (TYPE_READONLY (argtype))
953ff289 4082 {
c02065fc 4083 readonly_error (location, arg,
953ff289
DN
4084 ((code == PREINCREMENT_EXPR
4085 || code == POSTINCREMENT_EXPR)
4086 ? lv_increment : lv_decrement));
4087 return error_mark_node;
4088 }
f37acdf9
JM
4089 else if (TREE_READONLY (arg))
4090 readonly_warning (arg,
4091 ((code == PREINCREMENT_EXPR
4092 || code == POSTINCREMENT_EXPR)
4093 ? lv_increment : lv_decrement));
3e4093b6 4094
267bac10
JM
4095 /* If the argument is atomic, use the special code sequences for
4096 atomic compound assignment. */
4097 if (atomic_op)
4098 {
4099 arg = stabilize_reference (arg);
4100 ret = build_atomic_assign (location, arg,
4101 ((code == PREINCREMENT_EXPR
4102 || code == POSTINCREMENT_EXPR)
4103 ? PLUS_EXPR
4104 : MINUS_EXPR),
4105 (FRACT_MODE_P (TYPE_MODE (argtype))
4106 ? inc
4107 : integer_one_node),
4108 (code == POSTINCREMENT_EXPR
4109 || code == POSTDECREMENT_EXPR));
4110 goto return_build_unary_op;
4111 }
4112
3e4093b6
RS
4113 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4114 val = boolean_increment (code, arg);
4115 else
53fb4de3 4116 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4117 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4118 if (TREE_CODE (val) != code)
6de9cd9a 4119 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4120 ret = val;
4121 goto return_build_unary_op;
3e4093b6
RS
4122 }
4123
4124 case ADDR_EXPR:
4125 /* Note that this operation never does default_conversion. */
4126
2b4b7036
JM
4127 /* The operand of unary '&' must be an lvalue (which excludes
4128 expressions of type void), or, in C99, the result of a [] or
4129 unary '*' operator. */
4130 if (VOID_TYPE_P (TREE_TYPE (arg))
4131 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4132 && (TREE_CODE (arg) != INDIRECT_REF
4133 || !flag_isoc99))
4134 pedwarn (location, 0, "taking address of expression of type %<void%>");
4135
3e4093b6
RS
4136 /* Let &* cancel out to simplify resulting code. */
4137 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 4138 {
3e4093b6
RS
4139 /* Don't let this be an lvalue. */
4140 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4141 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4142 ret = TREE_OPERAND (arg, 0);
4143 goto return_build_unary_op;
400fbf9f 4144 }
1eb8759b 4145
7c672dfc 4146 /* For &x[y], return x+y */
3e4093b6 4147 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4148 {
f2a71bbc
JM
4149 tree op0 = TREE_OPERAND (arg, 0);
4150 if (!c_mark_addressable (op0))
3e4093b6 4151 return error_mark_node;
1eb8759b 4152 }
1eb8759b 4153
3e4093b6
RS
4154 /* Anything not already handled and not a true memory reference
4155 or a non-lvalue array is an error. */
4156 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4157 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4158 return error_mark_node;
b6a10c9f 4159
928c19bb
JM
4160 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4161 folding later. */
4162 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4163 {
4164 tree inner = build_unary_op (location, code,
4165 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4166 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4167 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4168 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4169 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4170 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4171 goto return_build_unary_op;
4172 }
4173
3e4093b6
RS
4174 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4175 argtype = TREE_TYPE (arg);
400fbf9f 4176
3e4093b6 4177 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4178 to which the address will point. This is only needed
f2c1da78 4179 for function types. */
6615c446 4180 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4181 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4182 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4183 {
4184 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4185 int quals = orig_quals;
4186
4187 if (TREE_READONLY (arg))
4188 quals |= TYPE_QUAL_CONST;
4189 if (TREE_THIS_VOLATILE (arg))
4190 quals |= TYPE_QUAL_VOLATILE;
4191
f2c1da78
JM
4192 argtype = c_build_qualified_type (argtype, quals);
4193 }
400fbf9f 4194
3e4093b6
RS
4195 if (!c_mark_addressable (arg))
4196 return error_mark_node;
400fbf9f 4197
abb54d14
JM
4198 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4199 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4200
5cc200fc 4201 argtype = build_pointer_type (argtype);
5e55f99d
RH
4202
4203 /* ??? Cope with user tricks that amount to offsetof. Delete this
4204 when we have proper support for integer constant expressions. */
4205 val = get_base_address (arg);
4206 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
4207 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4208 {
cf9e9959 4209 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4210 goto return_build_unary_op;
3aa2ddb8 4211 }
5e55f99d 4212
5cc200fc 4213 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4214
c9f9eb5d
AH
4215 ret = val;
4216 goto return_build_unary_op;
400fbf9f 4217
3e4093b6 4218 default:
1344f9a3 4219 gcc_unreachable ();
3e4093b6 4220 }
400fbf9f 4221
3e4093b6
RS
4222 if (argtype == 0)
4223 argtype = TREE_TYPE (arg);
928c19bb
JM
4224 if (TREE_CODE (arg) == INTEGER_CST)
4225 ret = (require_constant_value
db3927fb
AH
4226 ? fold_build1_initializer_loc (location, code, argtype, arg)
4227 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4228 else
4229 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4230 return_build_unary_op:
4231 gcc_assert (ret != error_mark_node);
928c19bb
JM
4232 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4233 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4234 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4235 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4236 ret = note_integer_operands (ret);
8ce94e44
JM
4237 if (eptype)
4238 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4239 protected_set_expr_location (ret, location);
4240 return ret;
3e4093b6 4241}
400fbf9f 4242
3e4093b6
RS
4243/* Return nonzero if REF is an lvalue valid for this language.
4244 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4245 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4246
1e4ae551 4247bool
58f9752a 4248lvalue_p (const_tree ref)
3e4093b6 4249{
58f9752a 4250 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4251
3e4093b6
RS
4252 switch (code)
4253 {
4254 case REALPART_EXPR:
4255 case IMAGPART_EXPR:
4256 case COMPONENT_REF:
4257 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4258
928c19bb
JM
4259 case C_MAYBE_CONST_EXPR:
4260 return lvalue_p (TREE_OPERAND (ref, 1));
4261
3e4093b6
RS
4262 case COMPOUND_LITERAL_EXPR:
4263 case STRING_CST:
4264 return 1;
400fbf9f 4265
3e4093b6
RS
4266 case INDIRECT_REF:
4267 case ARRAY_REF:
36536d79 4268 case ARRAY_NOTATION_REF:
3e4093b6
RS
4269 case VAR_DECL:
4270 case PARM_DECL:
4271 case RESULT_DECL:
4272 case ERROR_MARK:
4273 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4274 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4275
3e4093b6 4276 case BIND_EXPR:
3e4093b6 4277 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4278
3e4093b6
RS
4279 default:
4280 return 0;
4281 }
4282}
400fbf9f 4283\f
f37acdf9
JM
4284/* Give a warning for storing in something that is read-only in GCC
4285 terms but not const in ISO C terms. */
4286
4287static void
4288readonly_warning (tree arg, enum lvalue_use use)
4289{
4290 switch (use)
4291 {
4292 case lv_assign:
4293 warning (0, "assignment of read-only location %qE", arg);
4294 break;
4295 case lv_increment:
4296 warning (0, "increment of read-only location %qE", arg);
4297 break;
4298 case lv_decrement:
4299 warning (0, "decrement of read-only location %qE", arg);
4300 break;
4301 default:
4302 gcc_unreachable ();
4303 }
4304 return;
4305}
4306
37dc0d8d
JM
4307
4308/* Return nonzero if REF is an lvalue valid for this language;
4309 otherwise, print an error message and return zero. USE says
7bd11157
TT
4310 how the lvalue is being used and so selects the error message.
4311 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4312
4313static int
7bd11157 4314lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4315{
4316 int win = lvalue_p (ref);
4317
4318 if (!win)
7bd11157 4319 lvalue_error (loc, use);
37dc0d8d
JM
4320
4321 return win;
4322}
3e4093b6
RS
4323\f
4324/* Mark EXP saying that we need to be able to take the
4325 address of it; it should not be allocated in a register.
4326 Returns true if successful. */
54c93c30 4327
3e4093b6
RS
4328bool
4329c_mark_addressable (tree exp)
400fbf9f 4330{
3e4093b6 4331 tree x = exp;
95602da1 4332
3e4093b6
RS
4333 while (1)
4334 switch (TREE_CODE (x))
4335 {
4336 case COMPONENT_REF:
4337 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4338 {
0039fa55
AN
4339 error
4340 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4341 return false;
4342 }
95602da1 4343
3e4093b6 4344 /* ... fall through ... */
95602da1 4345
3e4093b6
RS
4346 case ADDR_EXPR:
4347 case ARRAY_REF:
4348 case REALPART_EXPR:
4349 case IMAGPART_EXPR:
4350 x = TREE_OPERAND (x, 0);
4351 break;
95602da1 4352
3e4093b6
RS
4353 case COMPOUND_LITERAL_EXPR:
4354 case CONSTRUCTOR:
4355 TREE_ADDRESSABLE (x) = 1;
4356 return true;
95602da1 4357
3e4093b6
RS
4358 case VAR_DECL:
4359 case CONST_DECL:
4360 case PARM_DECL:
4361 case RESULT_DECL:
5baeaac0 4362 if (C_DECL_REGISTER (x)
3e4093b6
RS
4363 && DECL_NONLOCAL (x))
4364 {
e697b20f 4365 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4366 {
0039fa55
AN
4367 error
4368 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4369 return false;
4370 }
509c9d60 4371 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4372 }
5baeaac0 4373 else if (C_DECL_REGISTER (x))
3e4093b6 4374 {
e697b20f 4375 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4376 error ("address of global register variable %qD requested", x);
4377 else
4378 error ("address of register variable %qD requested", x);
4379 return false;
3e4093b6 4380 }
400fbf9f 4381
3e4093b6
RS
4382 /* drops in */
4383 case FUNCTION_DECL:
4384 TREE_ADDRESSABLE (x) = 1;
4385 /* drops out */
4386 default:
4387 return true;
4388 }
4389}
4390\f
2d2e923f
MLI
4391/* Convert EXPR to TYPE, warning about conversion problems with
4392 constants. SEMANTIC_TYPE is the type this conversion would use
4393 without excess precision. If SEMANTIC_TYPE is NULL, this function
4394 is equivalent to convert_and_check. This function is a wrapper that
4395 handles conversions that may be different than
4396 the usual ones because of excess precision. */
4397
4398static tree
68fca595
MP
4399ep_convert_and_check (location_t loc, tree type, tree expr,
4400 tree semantic_type)
2d2e923f
MLI
4401{
4402 if (TREE_TYPE (expr) == type)
4403 return expr;
4404
4405 if (!semantic_type)
68fca595 4406 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4407
4408 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4409 && TREE_TYPE (expr) != semantic_type)
4410 {
4411 /* For integers, we need to check the real conversion, not
4412 the conversion to the excess precision type. */
68fca595 4413 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4414 }
4415 /* Result type is the excess precision type, which should be
4416 large enough, so do not check. */
4417 return convert (type, expr);
4418}
4419
928c19bb
JM
4420/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4421 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4422 if folded to an integer constant then the unselected half may
4423 contain arbitrary operations not normally permitted in constant
c2255bc4 4424 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4425
4426tree
744aa42f 4427build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4428 tree op1, tree op1_original_type, tree op2,
4429 tree op2_original_type)
400fbf9f 4430{
3e4093b6
RS
4431 tree type1;
4432 tree type2;
4433 enum tree_code code1;
4434 enum tree_code code2;
4435 tree result_type = NULL;
2d2e923f 4436 tree semantic_result_type = NULL;
3e4093b6 4437 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4438 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4439 bool ifexp_int_operands;
928c19bb 4440 tree ret;
400fbf9f 4441
4d84fe7c
JM
4442 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4443 if (op1_int_operands)
4444 op1 = remove_c_maybe_const_expr (op1);
4445 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4446 if (op2_int_operands)
4447 op2 = remove_c_maybe_const_expr (op2);
4448 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4449 if (ifexp_int_operands)
4450 ifexp = remove_c_maybe_const_expr (ifexp);
4451
3e4093b6
RS
4452 /* Promote both alternatives. */
4453
4454 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4455 op1 = default_conversion (op1);
4456 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4457 op2 = default_conversion (op2);
4458
4459 if (TREE_CODE (ifexp) == ERROR_MARK
4460 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4461 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4462 return error_mark_node;
400fbf9f 4463
3e4093b6
RS
4464 type1 = TREE_TYPE (op1);
4465 code1 = TREE_CODE (type1);
4466 type2 = TREE_TYPE (op2);
4467 code2 = TREE_CODE (type2);
4468
b1adf557
JM
4469 /* C90 does not permit non-lvalue arrays in conditional expressions.
4470 In C99 they will be pointers by now. */
4471 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4472 {
744aa42f 4473 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4474 return error_mark_node;
4475 }
4476
8ce94e44
JM
4477 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4478 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4479 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4480 || code1 == COMPLEX_TYPE)
4481 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4482 || code2 == COMPLEX_TYPE))
4483 {
2d2e923f 4484 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4485 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4486 {
4487 op1 = TREE_OPERAND (op1, 0);
4488 type1 = TREE_TYPE (op1);
4489 gcc_assert (TREE_CODE (type1) == code1);
4490 }
4491 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4492 {
4493 op2 = TREE_OPERAND (op2, 0);
4494 type2 = TREE_TYPE (op2);
4495 gcc_assert (TREE_CODE (type2) == code2);
4496 }
4497 }
4498
d130ae11
ILT
4499 if (warn_cxx_compat)
4500 {
4501 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4502 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4503
4504 if (TREE_CODE (t1) == ENUMERAL_TYPE
4505 && TREE_CODE (t2) == ENUMERAL_TYPE
4506 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4507 warning_at (colon_loc, OPT_Wc___compat,
4508 ("different enum types in conditional is "
4509 "invalid in C++: %qT vs %qT"),
4510 t1, t2);
4511 }
4512
3e4093b6
RS
4513 /* Quickly detect the usual case where op1 and op2 have the same type
4514 after promotion. */
4515 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4516 {
3e4093b6
RS
4517 if (type1 == type2)
4518 result_type = type1;
4519 else
4520 result_type = TYPE_MAIN_VARIANT (type1);
4521 }
4522 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4523 || code1 == COMPLEX_TYPE)
4524 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4525 || code2 == COMPLEX_TYPE))
3e4093b6 4526 {
ccf7f880 4527 result_type = c_common_type (type1, type2);
0a0b3574
MM
4528 do_warn_double_promotion (result_type, type1, type2,
4529 "implicit conversion from %qT to %qT to "
4530 "match other result of conditional",
4531 colon_loc);
400fbf9f 4532
3e4093b6
RS
4533 /* If -Wsign-compare, warn here if type1 and type2 have
4534 different signedness. We'll promote the signed to unsigned
4535 and later code won't know it used to be different.
4536 Do this check on the original types, so that explicit casts
4537 will be considered, but default promotions won't. */
7d882b83 4538 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4539 {
8df83eae
RK
4540 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4541 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4542
3e4093b6
RS
4543 if (unsigned_op1 ^ unsigned_op2)
4544 {
6ac01510
ILT
4545 bool ovf;
4546
3e4093b6
RS
4547 /* Do not warn if the result type is signed, since the
4548 signed type will only be chosen if it can represent
4549 all the values of the unsigned type. */
3f75a254 4550 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4551 /* OK */;
3e4093b6 4552 else
928c19bb
JM
4553 {
4554 bool op1_maybe_const = true;
4555 bool op2_maybe_const = true;
4556
4557 /* Do not warn if the signed quantity is an
4558 unsuffixed integer literal (or some static
4559 constant expression involving such literals) and
4560 it is non-negative. This warning requires the
4561 operands to be folded for best results, so do
4562 that folding in this case even without
4563 warn_sign_compare to avoid warning options
4564 possibly affecting code generation. */
f5178456
RS
4565 c_inhibit_evaluation_warnings
4566 += (ifexp == truthvalue_false_node);
928c19bb
JM
4567 op1 = c_fully_fold (op1, require_constant_value,
4568 &op1_maybe_const);
f5178456
RS
4569 c_inhibit_evaluation_warnings
4570 -= (ifexp == truthvalue_false_node);
4571
4572 c_inhibit_evaluation_warnings
4573 += (ifexp == truthvalue_true_node);
928c19bb
JM
4574 op2 = c_fully_fold (op2, require_constant_value,
4575 &op2_maybe_const);
f5178456
RS
4576 c_inhibit_evaluation_warnings
4577 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4578
4579 if (warn_sign_compare)
4580 {
4581 if ((unsigned_op2
4582 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4583 || (unsigned_op1
4584 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4585 /* OK */;
4586 else
744aa42f
ILT
4587 warning_at (colon_loc, OPT_Wsign_compare,
4588 ("signed and unsigned type in "
4589 "conditional expression"));
928c19bb
JM
4590 }
4591 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4592 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4593 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4594 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4595 }
3e4093b6
RS
4596 }
4597 }
4598 }
4599 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4600 {
fcf73884 4601 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4602 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4603 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4604 result_type = void_type_node;
4605 }
4606 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4607 {
36c5e70a
BE
4608 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4609 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4610 addr_space_t as_common;
4611
744aa42f 4612 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4613 result_type = common_pointer_type (type1, type2);
6aa3c60d 4614 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4615 result_type = type2;
6aa3c60d 4616 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4617 result_type = type1;
4618 else if (!addr_space_superset (as1, as2, &as_common))
4619 {
4620 error_at (colon_loc, "pointers to disjoint address spaces "
4621 "used in conditional expression");
4622 return error_mark_node;
4623 }
267bac10
JM
4624 else if (VOID_TYPE_P (TREE_TYPE (type1))
4625 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4626 {
768952be
MU
4627 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4628 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4629 & ~TYPE_QUALS (TREE_TYPE (type1))))
4630 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4631 "pointer to array loses qualifier "
4632 "in conditional expression");
4633
fcf73884 4634 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4635 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4636 "ISO C forbids conditional expr between "
bda67431 4637 "%<void *%> and function pointer");
3e4093b6
RS
4638 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4639 TREE_TYPE (type2)));
34a80643 4640 }
267bac10
JM
4641 else if (VOID_TYPE_P (TREE_TYPE (type2))
4642 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4643 {
768952be
MU
4644 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4645 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4646 & ~TYPE_QUALS (TREE_TYPE (type2))))
4647 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4648 "pointer to array loses qualifier "
4649 "in conditional expression");
4650
fcf73884 4651 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4652 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4653 "ISO C forbids conditional expr between "
bda67431 4654 "%<void *%> and function pointer");
3e4093b6
RS
4655 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4656 TREE_TYPE (type1)));
1c2a9b35 4657 }
b581b85b
NP
4658 /* Objective-C pointer comparisons are a bit more lenient. */
4659 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4660 result_type = objc_common_type (type1, type2);
34a80643 4661 else
ab87f8c8 4662 {
36c5e70a
BE
4663 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4664
b581b85b
NP
4665 pedwarn (colon_loc, 0,
4666 "pointer type mismatch in conditional expression");
36c5e70a
BE
4667 result_type = build_pointer_type
4668 (build_qualified_type (void_type_node, qual));
ab87f8c8 4669 }
3e4093b6
RS
4670 }
4671 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4672 {
6aa3c60d 4673 if (!null_pointer_constant_p (orig_op2))
744aa42f 4674 pedwarn (colon_loc, 0,
509c9d60 4675 "pointer/integer type mismatch in conditional expression");
3e4093b6 4676 else
ab87f8c8 4677 {
3e4093b6 4678 op2 = null_pointer_node;
ab87f8c8 4679 }
3e4093b6
RS
4680 result_type = type1;
4681 }
4682 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4683 {
6aa3c60d 4684 if (!null_pointer_constant_p (orig_op1))
744aa42f 4685 pedwarn (colon_loc, 0,
509c9d60 4686 "pointer/integer type mismatch in conditional expression");
3e4093b6 4687 else
ab87f8c8 4688 {
3e4093b6 4689 op1 = null_pointer_node;
ab87f8c8 4690 }
3e4093b6
RS
4691 result_type = type2;
4692 }
1c2a9b35 4693
3e4093b6
RS
4694 if (!result_type)
4695 {
4696 if (flag_cond_mismatch)
4697 result_type = void_type_node;
4698 else
400fbf9f 4699 {
c2255bc4 4700 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4701 return error_mark_node;
400fbf9f 4702 }
3e4093b6 4703 }
400fbf9f 4704
3e4093b6
RS
4705 /* Merge const and volatile flags of the incoming types. */
4706 result_type
4707 = build_type_variant (result_type,
afbd0665
AS
4708 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4709 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4710
68fca595
MP
4711 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4712 semantic_result_type);
4713 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4714 semantic_result_type);
b6a10c9f 4715
928c19bb
JM
4716 if (ifexp_bcp && ifexp == truthvalue_true_node)
4717 {
4718 op2_int_operands = true;
4719 op1 = c_fully_fold (op1, require_constant_value, NULL);
4720 }
4721 if (ifexp_bcp && ifexp == truthvalue_false_node)
4722 {
4723 op1_int_operands = true;
4724 op2 = c_fully_fold (op2, require_constant_value, NULL);
4725 }
4d84fe7c 4726 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4727 && op1_int_operands
4728 && op2_int_operands);
4729 if (int_operands)
4730 {
4731 int_const = ((ifexp == truthvalue_true_node
4732 && TREE_CODE (orig_op1) == INTEGER_CST
4733 && !TREE_OVERFLOW (orig_op1))
4734 || (ifexp == truthvalue_false_node
4735 && TREE_CODE (orig_op2) == INTEGER_CST
4736 && !TREE_OVERFLOW (orig_op2)));
4737 }
4738 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4739 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4740 else
4741 {
01c7ccbb
JM
4742 if (int_operands)
4743 {
f34f1c87
MP
4744 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4745 nested inside of the expression. */
4746 op1 = c_fully_fold (op1, false, NULL);
4747 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4748 }
928c19bb
JM
4749 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4750 if (int_operands)
4751 ret = note_integer_operands (ret);
4752 }
2d2e923f
MLI
4753 if (semantic_result_type)
4754 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4755
c2255bc4 4756 protected_set_expr_location (ret, colon_loc);
928c19bb 4757 return ret;
3e4093b6
RS
4758}
4759\f
487a92fe 4760/* Return a compound expression that performs two expressions and
c2255bc4
AH
4761 returns the value of the second of them.
4762
4763 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4764
3e4093b6 4765tree
c2255bc4 4766build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4767{
4d84fe7c 4768 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4769 tree eptype = NULL_TREE;
928c19bb
JM
4770 tree ret;
4771
b72271b9 4772 if (flag_cilkplus
939b37da
BI
4773 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4774 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4775 {
4776 error_at (loc,
4777 "spawned function call cannot be part of a comma expression");
4778 return error_mark_node;
4779 }
4d84fe7c
JM
4780 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4781 if (expr1_int_operands)
4782 expr1 = remove_c_maybe_const_expr (expr1);
4783 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4784 if (expr2_int_operands)
4785 expr2 = remove_c_maybe_const_expr (expr2);
4786
8ce94e44
JM
4787 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4788 expr1 = TREE_OPERAND (expr1, 0);
4789 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4790 {
4791 eptype = TREE_TYPE (expr2);
4792 expr2 = TREE_OPERAND (expr2, 0);
4793 }
4794
3f75a254 4795 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4796 {
4797 /* The left-hand operand of a comma expression is like an expression
c5409249 4798 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4799 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4800 if (warn_unused_value)
47aecf47 4801 {
e14a6540 4802 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4803 && CONVERT_EXPR_P (expr1))
47aecf47 4804 ; /* (void) a, b */
e14a6540
JM
4805 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4806 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4807 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4808 ; /* (void) a, (void) b, c */
4809 else
b8698a0f 4810 warning_at (loc, OPT_Wunused_value,
c2255bc4 4811 "left-hand operand of comma expression has no effect");
47aecf47 4812 }
3e4093b6 4813 }
789eadcd
MP
4814 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4815 && warn_unused_value)
4816 {
4817 tree r = expr1;
4818 location_t cloc = loc;
4819 while (TREE_CODE (r) == COMPOUND_EXPR)
4820 {
4821 if (EXPR_HAS_LOCATION (r))
4822 cloc = EXPR_LOCATION (r);
4823 r = TREE_OPERAND (r, 1);
4824 }
4825 if (!TREE_SIDE_EFFECTS (r)
4826 && !VOID_TYPE_P (TREE_TYPE (r))
4827 && !CONVERT_EXPR_P (r))
4828 warning_at (cloc, OPT_Wunused_value,
4829 "right-hand operand of comma expression has no effect");
4830 }
400fbf9f 4831
3e4093b6
RS
4832 /* With -Wunused, we should also warn if the left-hand operand does have
4833 side-effects, but computes a value which is not used. For example, in
4834 `foo() + bar(), baz()' the result of the `+' operator is not used,
4835 so we should issue a warning. */
4836 else if (warn_unused_value)
c2255bc4 4837 warn_if_unused_value (expr1, loc);
400fbf9f 4838
e63d6886
AP
4839 if (expr2 == error_mark_node)
4840 return error_mark_node;
4841
928c19bb
JM
4842 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4843
4844 if (flag_isoc99
4d84fe7c
JM
4845 && expr1_int_operands
4846 && expr2_int_operands)
928c19bb
JM
4847 ret = note_integer_operands (ret);
4848
8ce94e44
JM
4849 if (eptype)
4850 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4851
c2255bc4 4852 protected_set_expr_location (ret, loc);
928c19bb 4853 return ret;
3e4093b6 4854}
400fbf9f 4855
67165eb3
ILT
4856/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4857 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4858 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4859 of the cast. -Wcast-qual appeared on the command line. Named
4860 address space qualifiers are not handled here, because they result
4861 in different warnings. */
67165eb3
ILT
4862
4863static void
2ee3cb35 4864handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4865{
4866 tree in_type = type;
4867 tree in_otype = otype;
4868 int added = 0;
4869 int discarded = 0;
4870 bool is_const;
4871
4872 /* Check that the qualifiers on IN_TYPE are a superset of the
4873 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4874 nodes is uninteresting and we stop as soon as we hit a
4875 non-POINTER_TYPE node on either type. */
4876 do
4877 {
4878 in_otype = TREE_TYPE (in_otype);
4879 in_type = TREE_TYPE (in_type);
4880
4881 /* GNU C allows cv-qualified function types. 'const' means the
4882 function is very pure, 'volatile' means it can't return. We
4883 need to warn when such qualifiers are added, not when they're
4884 taken away. */
4885 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4886 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4887 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4888 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4889 else
36c5e70a
BE
4890 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4891 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4892 }
4893 while (TREE_CODE (in_type) == POINTER_TYPE
4894 && TREE_CODE (in_otype) == POINTER_TYPE);
4895
4896 if (added)
2ee3cb35
MLI
4897 warning_at (loc, OPT_Wcast_qual,
4898 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4899
4900 if (discarded)
4901 /* There are qualifiers present in IN_OTYPE that are not present
4902 in IN_TYPE. */
2ee3cb35 4903 warning_at (loc, OPT_Wcast_qual,
7485aeea 4904 "cast discards %qv qualifier from pointer target type",
2ee3cb35 4905 discarded);
67165eb3
ILT
4906
4907 if (added || discarded)
4908 return;
4909
4910 /* A cast from **T to const **T is unsafe, because it can cause a
4911 const value to be changed with no additional warning. We only
4912 issue this warning if T is the same on both sides, and we only
4913 issue the warning if there are the same number of pointers on
4914 both sides, as otherwise the cast is clearly unsafe anyhow. A
4915 cast is unsafe when a qualifier is added at one level and const
4916 is not present at all outer levels.
4917
4918 To issue this warning, we check at each level whether the cast
4919 adds new qualifiers not already seen. We don't need to special
4920 case function types, as they won't have the same
4921 TYPE_MAIN_VARIANT. */
4922
4923 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4924 return;
4925 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4926 return;
4927
4928 in_type = type;
4929 in_otype = otype;
4930 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4931 do
4932 {
4933 in_type = TREE_TYPE (in_type);
4934 in_otype = TREE_TYPE (in_otype);
4935 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4936 && !is_const)
4937 {
2ee3cb35
MLI
4938 warning_at (loc, OPT_Wcast_qual,
4939 "to be safe all intermediate pointers in cast from "
4940 "%qT to %qT must be %<const%> qualified",
4941 otype, type);
67165eb3
ILT
4942 break;
4943 }
4944 if (is_const)
4945 is_const = TYPE_READONLY (in_type);
4946 }
4947 while (TREE_CODE (in_type) == POINTER_TYPE);
4948}
4949
b8698a0f 4950/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4951 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4952
3e4093b6 4953tree
c2255bc4 4954build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4955{
8ce94e44
JM
4956 tree value;
4957
4958 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4959 expr = TREE_OPERAND (expr, 0);
4960
4961 value = expr;
400fbf9f 4962
3e4093b6
RS
4963 if (type == error_mark_node || expr == error_mark_node)
4964 return error_mark_node;
400fbf9f 4965
3e4093b6
RS
4966 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4967 only in <protocol> qualifications. But when constructing cast expressions,
4968 the protocols do matter and must be kept around. */
700686fa
ZL
4969 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4970 return build1 (NOP_EXPR, type, expr);
4971
4972 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4973
3e4093b6
RS
4974 if (TREE_CODE (type) == ARRAY_TYPE)
4975 {
c2255bc4 4976 error_at (loc, "cast specifies array type");
3e4093b6
RS
4977 return error_mark_node;
4978 }
400fbf9f 4979
3e4093b6
RS
4980 if (TREE_CODE (type) == FUNCTION_TYPE)
4981 {
c2255bc4 4982 error_at (loc, "cast specifies function type");
3e4093b6
RS
4983 return error_mark_node;
4984 }
400fbf9f 4985
808d6eaa
JM
4986 if (!VOID_TYPE_P (type))
4987 {
4988 value = require_complete_type (value);
4989 if (value == error_mark_node)
4990 return error_mark_node;
4991 }
4992
3e4093b6
RS
4993 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4994 {
fcf73884
MLI
4995 if (TREE_CODE (type) == RECORD_TYPE
4996 || TREE_CODE (type) == UNION_TYPE)
c1771a20 4997 pedwarn (loc, OPT_Wpedantic,
fcf73884 4998 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
4999
5000 /* Convert to remove any qualifiers from VALUE's type. */
5001 value = convert (type, value);
3e4093b6
RS
5002 }
5003 else if (TREE_CODE (type) == UNION_TYPE)
5004 {
5005 tree field;
400fbf9f 5006
910ad8de 5007 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5008 if (TREE_TYPE (field) != error_mark_node
5009 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5010 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5011 break;
5012
5013 if (field)
400fbf9f 5014 {
3e4093b6 5015 tree t;
e616f54d 5016 bool maybe_const = true;
3e4093b6 5017
c1771a20 5018 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5019 t = c_fully_fold (value, false, &maybe_const);
5020 t = build_constructor_single (type, field, t);
5021 if (!maybe_const)
5022 t = c_wrap_maybe_const (t, true);
5023 t = digest_init (loc, type, t,
bbbbb16a 5024 NULL_TREE, false, true, 0);
3e4093b6
RS
5025 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5026 return t;
400fbf9f 5027 }
c2255bc4 5028 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5029 return error_mark_node;
5030 }
5031 else
5032 {
5033 tree otype, ovalue;
400fbf9f 5034
3e4093b6 5035 if (type == void_type_node)
c2255bc4
AH
5036 {
5037 tree t = build1 (CONVERT_EXPR, type, value);
5038 SET_EXPR_LOCATION (t, loc);
5039 return t;
5040 }
400fbf9f 5041
3e4093b6 5042 otype = TREE_TYPE (value);
400fbf9f 5043
3e4093b6 5044 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5045 if (warn_cast_qual
5046 && TREE_CODE (type) == POINTER_TYPE
5047 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5048 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5049
36c5e70a
BE
5050 /* Warn about conversions between pointers to disjoint
5051 address spaces. */
5052 if (TREE_CODE (type) == POINTER_TYPE
5053 && TREE_CODE (otype) == POINTER_TYPE
5054 && !null_pointer_constant_p (value))
5055 {
5056 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5057 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5058 addr_space_t as_common;
5059
5060 if (!addr_space_superset (as_to, as_from, &as_common))
5061 {
5062 if (ADDR_SPACE_GENERIC_P (as_from))
5063 warning_at (loc, 0, "cast to %s address space pointer "
5064 "from disjoint generic address space pointer",
5065 c_addr_space_name (as_to));
5066
5067 else if (ADDR_SPACE_GENERIC_P (as_to))
5068 warning_at (loc, 0, "cast to generic address space pointer "
5069 "from disjoint %s address space pointer",
5070 c_addr_space_name (as_from));
5071
5072 else
5073 warning_at (loc, 0, "cast to %s address space pointer "
5074 "from disjoint %s address space pointer",
5075 c_addr_space_name (as_to),
5076 c_addr_space_name (as_from));
5077 }
5078 }
5079
3e4093b6 5080 /* Warn about possible alignment problems. */
3176a0c2 5081 if (STRICT_ALIGNMENT
3e4093b6
RS
5082 && TREE_CODE (type) == POINTER_TYPE
5083 && TREE_CODE (otype) == POINTER_TYPE
5084 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5085 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5086 /* Don't warn about opaque types, where the actual alignment
5087 restriction is unknown. */
5088 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5089 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5090 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5091 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5092 warning_at (loc, OPT_Wcast_align,
5093 "cast increases required alignment of target type");
e9a25f70 5094
3176a0c2 5095 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5096 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5097 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5098 /* Unlike conversion of integers to pointers, where the
5099 warning is disabled for converting constants because
5100 of cases such as SIG_*, warn about converting constant
5101 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5102 sign extension, and a warning is appropriate. */
c2255bc4
AH
5103 warning_at (loc, OPT_Wpointer_to_int_cast,
5104 "cast from pointer to integer of different size");
400fbf9f 5105
3176a0c2 5106 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5107 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5108 warning_at (loc, OPT_Wbad_function_cast,
5109 "cast from function call of type %qT "
5110 "to non-matching type %qT", otype, type);
400fbf9f 5111
3176a0c2 5112 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5113 && TREE_CODE (otype) == INTEGER_TYPE
5114 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5115 /* Don't warn about converting any constant. */
5116 && !TREE_CONSTANT (value))
c2255bc4
AH
5117 warning_at (loc,
5118 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5119 "of different size");
400fbf9f 5120
79bedddc
SR
5121 if (warn_strict_aliasing <= 2)
5122 strict_aliasing_warning (otype, type, expr);
400fbf9f 5123
3897f229
JM
5124 /* If pedantic, warn for conversions between function and object
5125 pointer types, except for converting a null pointer constant
5126 to function pointer type. */
5127 if (pedantic
5128 && TREE_CODE (type) == POINTER_TYPE
5129 && TREE_CODE (otype) == POINTER_TYPE
5130 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5131 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5132 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5133 "conversion of function pointer to object pointer type");
3897f229
JM
5134
5135 if (pedantic
5136 && TREE_CODE (type) == POINTER_TYPE
5137 && TREE_CODE (otype) == POINTER_TYPE
5138 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5139 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5140 && !null_pointer_constant_p (value))
c1771a20 5141 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5142 "conversion of object pointer to function pointer type");
3897f229 5143
3e4093b6 5144 ovalue = value;
3e4093b6 5145 value = convert (type, value);
400fbf9f 5146
3e4093b6 5147 /* Ignore any integer overflow caused by the cast. */
928c19bb 5148 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5149 {
8bcd6380 5150 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5151 {
8bcd6380
RS
5152 if (!TREE_OVERFLOW (value))
5153 {
5154 /* Avoid clobbering a shared constant. */
5155 value = copy_node (value);
5156 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5157 }
6414bad6 5158 }
8bcd6380 5159 else if (TREE_OVERFLOW (value))
d8e1f97b 5160 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5161 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5162 }
5163 }
400fbf9f 5164
53cd18ec
JM
5165 /* Don't let a cast be an lvalue. */
5166 if (value == expr)
db3927fb 5167 value = non_lvalue_loc (loc, value);
e9a25f70 5168
928c19bb
JM
5169 /* Don't allow the results of casting to floating-point or complex
5170 types be confused with actual constants, or casts involving
5171 integer and pointer types other than direct integer-to-integer
5172 and integer-to-pointer be confused with integer constant
5173 expressions and null pointer constants. */
5174 if (TREE_CODE (value) == REAL_CST
5175 || TREE_CODE (value) == COMPLEX_CST
5176 || (TREE_CODE (value) == INTEGER_CST
5177 && !((TREE_CODE (expr) == INTEGER_CST
5178 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5179 || TREE_CODE (expr) == REAL_CST
5180 || TREE_CODE (expr) == COMPLEX_CST)))
5181 value = build1 (NOP_EXPR, type, value);
5182
c2255bc4
AH
5183 if (CAN_HAVE_LOCATION_P (value))
5184 SET_EXPR_LOCATION (value, loc);
3e4093b6 5185 return value;
400fbf9f
JW
5186}
5187
c2255bc4
AH
5188/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5189 location of the open paren of the cast, or the position of the cast
5190 expr. */
3e4093b6 5191tree
c2255bc4 5192c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5193{
f8893e47 5194 tree type;
928c19bb
JM
5195 tree type_expr = NULL_TREE;
5196 bool type_expr_const = true;
5197 tree ret;
3e4093b6 5198 int saved_wsp = warn_strict_prototypes;
c5c76735 5199
3e4093b6
RS
5200 /* This avoids warnings about unprototyped casts on
5201 integers. E.g. "#define SIG_DFL (void(*)())0". */
5202 if (TREE_CODE (expr) == INTEGER_CST)
5203 warn_strict_prototypes = 0;
928c19bb 5204 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5205 warn_strict_prototypes = saved_wsp;
c5c76735 5206
c2255bc4 5207 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5208 if (type_expr)
5209 {
9f33203d
JM
5210 bool inner_expr_const = true;
5211 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5212 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5213 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5214 && inner_expr_const);
c2255bc4 5215 SET_EXPR_LOCATION (ret, loc);
928c19bb 5216 }
24b97832
ILT
5217
5218 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5219 SET_EXPR_LOCATION (ret, loc);
5220
9e5b2115
PB
5221 /* C++ does not permits types to be defined in a cast, but it
5222 allows references to incomplete types. */
5223 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5224 warning_at (loc, OPT_Wc___compat,
5225 "defining a type in a cast is invalid in C++");
5226
928c19bb 5227 return ret;
400fbf9f 5228}
3e4093b6
RS
5229\f
5230/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5231 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5232 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5233 MODIFYCODE is the code for a binary operator that we use
5234 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5235 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5236 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5237 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5238
c2255bc4
AH
5239 LOCATION is the location of the MODIFYCODE operator.
5240 RHS_LOC is the location of the RHS. */
2f6e4e97 5241
3e4093b6 5242tree
32e8bb8e 5243build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5244 enum tree_code modifycode,
c2255bc4 5245 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5246{
3e4093b6
RS
5247 tree result;
5248 tree newrhs;
241f845a 5249 tree rhseval = NULL_TREE;
8ce94e44 5250 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5251 tree lhstype = TREE_TYPE (lhs);
5252 tree olhstype = lhstype;
928c19bb 5253 bool npc;
267bac10 5254 bool is_atomic_op;
e9a25f70 5255
3e4093b6
RS
5256 /* Types that aren't fully specified cannot be used in assignments. */
5257 lhs = require_complete_type (lhs);
e9a25f70 5258
3e4093b6
RS
5259 /* Avoid duplicate error messages from operands that had errors. */
5260 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5261 return error_mark_node;
400fbf9f 5262
4c2ecab0
JM
5263 /* Ensure an error for assigning a non-lvalue array to an array in
5264 C90. */
5265 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5266 {
5267 error_at (location, "assignment to expression with array type");
5268 return error_mark_node;
5269 }
5270
46a88c12 5271 /* For ObjC properties, defer this check. */
7bd11157 5272 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5273 return error_mark_node;
5274
267bac10
JM
5275 is_atomic_op = really_atomic_lvalue (lhs);
5276
8ce94e44
JM
5277 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5278 {
5279 rhs_semantic_type = TREE_TYPE (rhs);
5280 rhs = TREE_OPERAND (rhs, 0);
5281 }
5282
3e4093b6 5283 newrhs = rhs;
400fbf9f 5284
928c19bb
JM
5285 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5286 {
5287 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5288 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5289 rhs_origtype);
928c19bb
JM
5290 if (inner == error_mark_node)
5291 return error_mark_node;
5292 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5293 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5294 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5295 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5296 protected_set_expr_location (result, location);
5297 return result;
5298 }
5299
3e4093b6
RS
5300 /* If a binary op has been requested, combine the old LHS value with the RHS
5301 producing the value we should actually store into the LHS. */
5302
5303 if (modifycode != NOP_EXPR)
400fbf9f 5304 {
928c19bb 5305 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5306 lhs = stabilize_reference (lhs);
bbbbb16a 5307
267bac10
JM
5308 /* Construct the RHS for any non-atomic compound assignemnt. */
5309 if (!is_atomic_op)
5310 {
241f845a
JJ
5311 /* If in LHS op= RHS the RHS has side-effects, ensure they
5312 are preevaluated before the rest of the assignment expression's
5313 side-effects, because RHS could contain e.g. function calls
5314 that modify LHS. */
5315 if (TREE_SIDE_EFFECTS (rhs))
5316 {
5317 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5318 rhseval = newrhs;
5319 }
267bac10 5320 newrhs = build_binary_op (location,
241f845a 5321 modifycode, lhs, newrhs, 1);
267bac10
JM
5322
5323 /* The original type of the right hand side is no longer
5324 meaningful. */
5325 rhs_origtype = NULL_TREE;
5326 }
400fbf9f 5327 }
400fbf9f 5328
668ea4b1
IS
5329 if (c_dialect_objc ())
5330 {
46a88c12
NP
5331 /* Check if we are modifying an Objective-C property reference;
5332 if so, we need to generate setter calls. */
5333 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5334 if (result)
241f845a 5335 goto return_result;
46a88c12
NP
5336
5337 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5338 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5339 return error_mark_node;
5340 }
5341
9bf24266 5342 /* Give an error for storing in something that is 'const'. */
bbbd6700 5343
f37acdf9 5344 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5345 || ((TREE_CODE (lhstype) == RECORD_TYPE
5346 || TREE_CODE (lhstype) == UNION_TYPE)
5347 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5348 {
c02065fc 5349 readonly_error (location, lhs, lv_assign);
953ff289
DN
5350 return error_mark_node;
5351 }
f37acdf9
JM
5352 else if (TREE_READONLY (lhs))
5353 readonly_warning (lhs, lv_assign);
bbbd6700 5354
3e4093b6
RS
5355 /* If storing into a structure or union member,
5356 it has probably been given type `int'.
5357 Compute the type that would go with
5358 the actual amount of storage the member occupies. */
bbbd6700 5359
3e4093b6
RS
5360 if (TREE_CODE (lhs) == COMPONENT_REF
5361 && (TREE_CODE (lhstype) == INTEGER_TYPE
5362 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5363 || TREE_CODE (lhstype) == REAL_TYPE
5364 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5365 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5366
3e4093b6
RS
5367 /* If storing in a field that is in actuality a short or narrower than one,
5368 we must store in the field in its actual type. */
5369
5370 if (lhstype != TREE_TYPE (lhs))
5371 {
5372 lhs = copy_node (lhs);
5373 TREE_TYPE (lhs) = lhstype;
400fbf9f 5374 }
400fbf9f 5375
32e8bb8e
ILT
5376 /* Issue -Wc++-compat warnings about an assignment to an enum type
5377 when LHS does not have its original type. This happens for,
5378 e.g., an enum bitfield in a struct. */
5379 if (warn_cxx_compat
5380 && lhs_origtype != NULL_TREE
5381 && lhs_origtype != lhstype
5382 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5383 {
5384 tree checktype = (rhs_origtype != NULL_TREE
5385 ? rhs_origtype
5386 : TREE_TYPE (rhs));
5387 if (checktype != error_mark_node
267bac10
JM
5388 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5389 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5390 warning_at (location, OPT_Wc___compat,
5391 "enum conversion in assignment is invalid in C++");
5392 }
5393
267bac10
JM
5394 /* If the lhs is atomic, remove that qualifier. */
5395 if (is_atomic_op)
5396 {
5397 lhstype = build_qualified_type (lhstype,
5398 (TYPE_QUALS (lhstype)
5399 & ~TYPE_QUAL_ATOMIC));
5400 olhstype = build_qualified_type (olhstype,
5401 (TYPE_QUALS (lhstype)
5402 & ~TYPE_QUAL_ATOMIC));
5403 }
5404
8ce94e44
JM
5405 /* Convert new value to destination type. Fold it first, then
5406 restore any excess precision information, for the sake of
5407 conversion warnings. */
400fbf9f 5408
267bac10
JM
5409 if (!(is_atomic_op && modifycode != NOP_EXPR))
5410 {
5411 npc = null_pointer_constant_p (newrhs);
5412 newrhs = c_fully_fold (newrhs, false, NULL);
5413 if (rhs_semantic_type)
5414 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5415 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5416 rhs_origtype, ic_assign, npc,
5417 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5418 if (TREE_CODE (newrhs) == ERROR_MARK)
5419 return error_mark_node;
5420 }
400fbf9f 5421
6e955430
ZL
5422 /* Emit ObjC write barrier, if necessary. */
5423 if (c_dialect_objc () && flag_objc_gc)
5424 {
5425 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5426 if (result)
c9f9eb5d
AH
5427 {
5428 protected_set_expr_location (result, location);
241f845a 5429 goto return_result;
c9f9eb5d 5430 }
6e955430
ZL
5431 }
5432
ea4b7848 5433 /* Scan operands. */
400fbf9f 5434
267bac10
JM
5435 if (is_atomic_op)
5436 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5437 else
5438 {
5439 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5440 TREE_SIDE_EFFECTS (result) = 1;
5441 protected_set_expr_location (result, location);
5442 }
400fbf9f 5443
3e4093b6
RS
5444 /* If we got the LHS in a different type for storing in,
5445 convert the result back to the nominal type of LHS
5446 so that the value we return always has the same type
5447 as the LHS argument. */
e855c5ce 5448
3e4093b6 5449 if (olhstype == TREE_TYPE (result))
241f845a 5450 goto return_result;
c9f9eb5d 5451
68fca595
MP
5452 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5453 rhs_origtype, ic_assign, false, NULL_TREE,
5454 NULL_TREE, 0);
c9f9eb5d 5455 protected_set_expr_location (result, location);
241f845a
JJ
5456
5457return_result:
5458 if (rhseval)
5459 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5460 return result;
3e4093b6
RS
5461}
5462\f
478a1c5b
ILT
5463/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5464 This is used to implement -fplan9-extensions. */
5465
5466static bool
5467find_anonymous_field_with_type (tree struct_type, tree type)
5468{
5469 tree field;
5470 bool found;
5471
5472 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5473 || TREE_CODE (struct_type) == UNION_TYPE);
5474 found = false;
5475 for (field = TYPE_FIELDS (struct_type);
5476 field != NULL_TREE;
5477 field = TREE_CHAIN (field))
5478 {
267bac10
JM
5479 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5480 ? c_build_qualified_type (TREE_TYPE (field),
5481 TYPE_QUAL_ATOMIC)
5482 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5483 if (DECL_NAME (field) == NULL
267bac10 5484 && comptypes (type, fieldtype))
478a1c5b
ILT
5485 {
5486 if (found)
5487 return false;
5488 found = true;
5489 }
5490 else if (DECL_NAME (field) == NULL
5491 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5492 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5493 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5494 {
5495 if (found)
5496 return false;
5497 found = true;
5498 }
5499 }
5500 return found;
5501}
5502
5503/* RHS is an expression whose type is pointer to struct. If there is
5504 an anonymous field in RHS with type TYPE, then return a pointer to
5505 that field in RHS. This is used with -fplan9-extensions. This
5506 returns NULL if no conversion could be found. */
5507
5508static tree
5509convert_to_anonymous_field (location_t location, tree type, tree rhs)
5510{
5511 tree rhs_struct_type, lhs_main_type;
5512 tree field, found_field;
5513 bool found_sub_field;
5514 tree ret;
5515
5516 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5517 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5518 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5519 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5520
5521 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5522 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5523 ? c_build_qualified_type (TREE_TYPE (type),
5524 TYPE_QUAL_ATOMIC)
5525 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5526
5527 found_field = NULL_TREE;
5528 found_sub_field = false;
5529 for (field = TYPE_FIELDS (rhs_struct_type);
5530 field != NULL_TREE;
5531 field = TREE_CHAIN (field))
5532 {
5533 if (DECL_NAME (field) != NULL_TREE
5534 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5535 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5536 continue;
267bac10
JM
5537 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5538 ? c_build_qualified_type (TREE_TYPE (field),
5539 TYPE_QUAL_ATOMIC)
5540 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5541 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5542 {
5543 if (found_field != NULL_TREE)
5544 return NULL_TREE;
5545 found_field = field;
5546 }
5547 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5548 lhs_main_type))
5549 {
5550 if (found_field != NULL_TREE)
5551 return NULL_TREE;
5552 found_field = field;
5553 found_sub_field = true;
5554 }
5555 }
5556
5557 if (found_field == NULL_TREE)
5558 return NULL_TREE;
5559
5560 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5561 build_fold_indirect_ref (rhs), found_field,
5562 NULL_TREE);
5563 ret = build_fold_addr_expr_loc (location, ret);
5564
5565 if (found_sub_field)
5566 {
5567 ret = convert_to_anonymous_field (location, type, ret);
5568 gcc_assert (ret != NULL_TREE);
5569 }
5570
5571 return ret;
5572}
5573
63bc4e87
MP
5574/* Issue an error message for a bad initializer component.
5575 GMSGID identifies the message.
5576 The component name is taken from the spelling stack. */
5577
5578static void
ea58ef42 5579error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5580{
5581 char *ofwhat;
5582
5583 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5584 error_at (loc, gmsgid);
63bc4e87
MP
5585 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5586 if (*ofwhat)
d7ff7ae5 5587 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5588}
5589
5590/* Issue a pedantic warning for a bad initializer component. OPT is
5591 the option OPT_* (from options.h) controlling this warning or 0 if
5592 it is unconditionally given. GMSGID identifies the message. The
5593 component name is taken from the spelling stack. */
5594
5595static void
5596pedwarn_init (location_t location, int opt, const char *gmsgid)
5597{
5598 char *ofwhat;
d7ff7ae5 5599 bool warned;
63bc4e87
MP
5600
5601 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5602 warned = pedwarn (location, opt, gmsgid);
63bc4e87 5603 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5604 if (*ofwhat && warned)
5605 inform (location, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5606}
5607
5608/* Issue a warning for a bad initializer component.
5609
5610 OPT is the OPT_W* value corresponding to the warning option that
5611 controls this warning. GMSGID identifies the message. The
5612 component name is taken from the spelling stack. */
5613
5614static void
5615warning_init (location_t loc, int opt, const char *gmsgid)
5616{
5617 char *ofwhat;
d7ff7ae5 5618 bool warned;
63bc4e87
MP
5619
5620 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5621 warned = warning_at (loc, opt, gmsgid);
63bc4e87 5622 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5623 if (*ofwhat && warned)
5624 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5625}
5626\f
5627/* If TYPE is an array type and EXPR is a parenthesized string
5628 constant, warn if pedantic that EXPR is being used to initialize an
5629 object of type TYPE. */
5630
5631void
d033409e 5632maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5633{
5634 if (pedantic
5635 && TREE_CODE (type) == ARRAY_TYPE
5636 && TREE_CODE (expr.value) == STRING_CST
5637 && expr.original_code != STRING_CST)
d033409e 5638 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5639 "array initialized from parenthesized string constant");
5640}
5641
bbbbb16a
ILT
5642/* Convert value RHS to type TYPE as preparation for an assignment to
5643 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5644 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5645 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5646 constant before any folding.
3e4093b6
RS
5647 The real work of conversion is done by `convert'.
5648 The purpose of this function is to generate error messages
5649 for assignments that are not allowed in C.
2ac2f164
JM
5650 ERRTYPE says whether it is argument passing, assignment,
5651 initialization or return.
2f6e4e97 5652
81e5eca8
MP
5653 LOCATION is the location of the assignment, EXPR_LOC is the location of
5654 the RHS or, for a function, location of an argument.
2ac2f164 5655 FUNCTION is a tree for the function being called.
3e4093b6 5656 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5657
3e4093b6 5658static tree
81e5eca8 5659convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5660 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5661 bool null_pointer_constant, tree fundecl,
5662 tree function, int parmnum)
3e4093b6
RS
5663{
5664 enum tree_code codel = TREE_CODE (type);
8ce94e44 5665 tree orig_rhs = rhs;
3e4093b6
RS
5666 tree rhstype;
5667 enum tree_code coder;
2ac2f164 5668 tree rname = NULL_TREE;
58393038 5669 bool objc_ok = false;
2ac2f164 5670
6b4ef5c1 5671 if (errtype == ic_argpass)
2ac2f164
JM
5672 {
5673 tree selector;
5674 /* Change pointer to function to the function itself for
5675 diagnostics. */
5676 if (TREE_CODE (function) == ADDR_EXPR
5677 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5678 function = TREE_OPERAND (function, 0);
5679
5680 /* Handle an ObjC selector specially for diagnostics. */
5681 selector = objc_message_selector ();
5682 rname = function;
5683 if (selector && parmnum > 2)
5684 {
5685 rname = selector;
5686 parmnum -= 2;
5687 }
5688 }
5689
5690 /* This macro is used to emit diagnostics to ensure that all format
5691 strings are complete sentences, visible to gettext and checked at
5692 compile time. */
768952be 5693#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5694 do { \
5695 switch (errtype) \
5696 { \
5697 case ic_argpass: \
5c1bc275 5698 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5699 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5700 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5701 "expected %qT but argument is of type %qT", \
5702 type, rhstype); \
5703 break; \
1e053dfe
MLI
5704 case ic_assign: \
5705 pedwarn (LOCATION, OPT, AS); \
5706 break; \
5707 case ic_init: \
6a8f4e12 5708 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5709 break; \
5710 case ic_return: \
d033409e 5711 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5712 break; \
5713 default: \
5714 gcc_unreachable (); \
5715 } \
2ac2f164 5716 } while (0)
cb3ca04e 5717
49706e39
MLI
5718 /* This macro is used to emit diagnostics to ensure that all format
5719 strings are complete sentences, visible to gettext and checked at
768952be 5720 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 5721 extra parameter to enumerate qualifiers. */
768952be 5722#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
5723 do { \
5724 switch (errtype) \
5725 { \
5726 case ic_argpass: \
5c1bc275 5727 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 5728 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5729 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
5730 "expected %qT but argument is of type %qT", \
5731 type, rhstype); \
5732 break; \
5733 case ic_assign: \
5c1bc275 5734 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
5735 break; \
5736 case ic_init: \
5c1bc275 5737 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
5738 break; \
5739 case ic_return: \
5c1bc275 5740 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
5741 break; \
5742 default: \
5743 gcc_unreachable (); \
5744 } \
5745 } while (0)
5746
768952be
MU
5747 /* This macro is used to emit diagnostics to ensure that all format
5748 strings are complete sentences, visible to gettext and checked at
5749 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5750 warning_at instead of pedwarn. */
5751#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5752 do { \
5753 switch (errtype) \
5754 { \
5755 case ic_argpass: \
5756 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5757 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5758 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5759 "expected %qT but argument is of type %qT", \
5760 type, rhstype); \
5761 break; \
5762 case ic_assign: \
5763 warning_at (LOCATION, OPT, AS, QUALS); \
5764 break; \
5765 case ic_init: \
5766 warning_at (LOCATION, OPT, IN, QUALS); \
5767 break; \
5768 case ic_return: \
5769 warning_at (LOCATION, OPT, RE, QUALS); \
5770 break; \
5771 default: \
5772 gcc_unreachable (); \
5773 } \
5774 } while (0)
5775
8ce94e44
JM
5776 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5777 rhs = TREE_OPERAND (rhs, 0);
5778
3e4093b6
RS
5779 rhstype = TREE_TYPE (rhs);
5780 coder = TREE_CODE (rhstype);
5781
5782 if (coder == ERROR_MARK)
5783 return error_mark_node;
5784
58393038
ZL
5785 if (c_dialect_objc ())
5786 {
5787 int parmno;
5788
5789 switch (errtype)
5790 {
5791 case ic_return:
5792 parmno = 0;
5793 break;
5794
5795 case ic_assign:
5796 parmno = -1;
5797 break;
5798
5799 case ic_init:
5800 parmno = -2;
5801 break;
5802
5803 default:
5804 parmno = parmnum;
5805 break;
5806 }
5807
5808 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5809 }
5810
bbbbb16a
ILT
5811 if (warn_cxx_compat)
5812 {
5813 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5814 if (checktype != error_mark_node
5815 && TREE_CODE (type) == ENUMERAL_TYPE
5816 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5817 {
768952be
MU
5818 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5819 G_("enum conversion when passing argument "
5820 "%d of %qE is invalid in C++"),
5821 G_("enum conversion in assignment is "
5822 "invalid in C++"),
5823 G_("enum conversion in initialization is "
5824 "invalid in C++"),
5825 G_("enum conversion in return is "
5826 "invalid in C++"));
bbbbb16a
ILT
5827 }
5828 }
5829
3e4093b6 5830 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5831 return rhs;
3e4093b6
RS
5832
5833 if (coder == VOID_TYPE)
400fbf9f 5834 {
6dcc04b0
JM
5835 /* Except for passing an argument to an unprototyped function,
5836 this is a constraint violation. When passing an argument to
5837 an unprototyped function, it is compile-time undefined;
5838 making it a constraint in that case was rejected in
5839 DR#252. */
c2255bc4 5840 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5841 return error_mark_node;
400fbf9f 5842 }
808d6eaa
JM
5843 rhs = require_complete_type (rhs);
5844 if (rhs == error_mark_node)
5845 return error_mark_node;
cd192ccc
MS
5846 /* A non-reference type can convert to a reference. This handles
5847 va_start, va_copy and possibly port built-ins. */
5848 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5849 {
3e4093b6 5850 if (!lvalue_p (rhs))
400fbf9f 5851 {
c2255bc4 5852 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5853 return error_mark_node;
400fbf9f 5854 }
3e4093b6
RS
5855 if (!c_mark_addressable (rhs))
5856 return error_mark_node;
5857 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5858 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5859
81e5eca8 5860 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
5861 build_pointer_type (TREE_TYPE (type)),
5862 rhs, origtype, errtype,
5863 null_pointer_constant, fundecl, function,
5864 parmnum);
cd192ccc
MS
5865 if (rhs == error_mark_node)
5866 return error_mark_node;
3e4093b6
RS
5867
5868 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5869 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5870 return rhs;
400fbf9f 5871 }
3e4093b6 5872 /* Some types can interconvert without explicit casts. */
3274deff 5873 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5874 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5875 return convert (type, rhs);
5876 /* Arithmetic types all interconvert, and enum is treated like int. */
5877 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5878 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5879 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5880 || codel == BOOLEAN_TYPE)
5881 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5882 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5883 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5884 || coder == BOOLEAN_TYPE))
928c19bb
JM
5885 {
5886 tree ret;
5887 bool save = in_late_binary_op;
dfe776dd 5888 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb 5889 in_late_binary_op = true;
81e5eca8
MP
5890 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5891 ? expr_loc : location, type, orig_rhs);
dfe776dd 5892 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb
JM
5893 in_late_binary_op = save;
5894 return ret;
5895 }
400fbf9f 5896
79077aea
JJ
5897 /* Aggregates in different TUs might need conversion. */
5898 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5899 && codel == coder
5900 && comptypes (type, rhstype))
81e5eca8
MP
5901 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5902 ? expr_loc : location, type, rhs);
79077aea 5903
ebf0bf7f 5904 /* Conversion to a transparent union or record from its member types.
3e4093b6 5905 This applies only to function arguments. */
ebf0bf7f
JJ
5906 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5907 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5908 && errtype == ic_argpass)
400fbf9f 5909 {
0257e383 5910 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5911
910ad8de 5912 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5913 {
0257e383 5914 tree memb_type = TREE_TYPE (memb);
400fbf9f 5915
3e4093b6 5916 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5917 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5918 break;
e58cd767 5919
3e4093b6
RS
5920 if (TREE_CODE (memb_type) != POINTER_TYPE)
5921 continue;
2f6e4e97 5922
3e4093b6
RS
5923 if (coder == POINTER_TYPE)
5924 {
5925 tree ttl = TREE_TYPE (memb_type);
5926 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5927
3e4093b6
RS
5928 /* Any non-function converts to a [const][volatile] void *
5929 and vice versa; otherwise, targets must be the same.
5930 Meanwhile, the lhs target must have all the qualifiers of
5931 the rhs. */
267bac10
JM
5932 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5933 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5934 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5935 {
267bac10
JM
5936 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5937 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5938 /* If this type won't generate any warnings, use it. */
267bac10 5939 if (lquals == rquals
3e4093b6
RS
5940 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5941 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5942 ? ((lquals | rquals) == rquals)
5943 : ((lquals | rquals) == lquals)))
3e4093b6 5944 break;
400fbf9f 5945
3e4093b6 5946 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5947 if (!marginal_memb)
5948 marginal_memb = memb;
3e4093b6
RS
5949 }
5950 }
82bde854 5951
3e4093b6 5952 /* Can convert integer zero to any pointer type. */
928c19bb 5953 if (null_pointer_constant)
3e4093b6
RS
5954 {
5955 rhs = null_pointer_node;
5956 break;
5957 }
5958 }
400fbf9f 5959
0257e383 5960 if (memb || marginal_memb)
3e4093b6 5961 {
0257e383 5962 if (!memb)
3e4093b6
RS
5963 {
5964 /* We have only a marginally acceptable member type;
5965 it needs a warning. */
0257e383 5966 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5967 tree ttr = TREE_TYPE (rhstype);
714a0864 5968
3e4093b6
RS
5969 /* Const and volatile mean something different for function
5970 types, so the usual warnings are not appropriate. */
5971 if (TREE_CODE (ttr) == FUNCTION_TYPE
5972 && TREE_CODE (ttl) == FUNCTION_TYPE)
5973 {
5974 /* Because const and volatile on functions are
5975 restrictions that say the function will not do
5976 certain things, it is okay to use a const or volatile
5977 function where an ordinary one is wanted, but not
5978 vice-versa. */
36c5e70a
BE
5979 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5980 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
5981 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
5982 OPT_Wdiscarded_qualifiers,
5983 G_("passing argument %d of %qE "
5984 "makes %q#v qualified function "
5985 "pointer from unqualified"),
5986 G_("assignment makes %q#v qualified "
5987 "function pointer from "
5988 "unqualified"),
5989 G_("initialization makes %q#v qualified "
5990 "function pointer from "
5991 "unqualified"),
5992 G_("return makes %q#v qualified function "
5993 "pointer from unqualified"),
5994 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 5995 }
36c5e70a
BE
5996 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5997 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
5998 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
5999 OPT_Wdiscarded_qualifiers,
6000 G_("passing argument %d of %qE discards "
6001 "%qv qualifier from pointer target type"),
6002 G_("assignment discards %qv qualifier "
6003 "from pointer target type"),
6004 G_("initialization discards %qv qualifier "
6005 "from pointer target type"),
6006 G_("return discards %qv qualifier from "
6007 "pointer target type"),
6008 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6009
6010 memb = marginal_memb;
3e4093b6 6011 }
400fbf9f 6012
fcf73884 6013 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6014 pedwarn (location, OPT_Wpedantic,
fcf73884 6015 "ISO C prohibits argument conversion to union type");
0e7c47fa 6016
db3927fb 6017 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6018 return build_constructor_single (type, memb, rhs);
3e4093b6 6019 }
0e7c47fa
RK
6020 }
6021
3e4093b6
RS
6022 /* Conversions among pointers */
6023 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6024 && (coder == codel))
400fbf9f 6025 {
3e4093b6
RS
6026 tree ttl = TREE_TYPE (type);
6027 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6028 tree mvl = ttl;
6029 tree mvr = ttr;
3e4093b6 6030 bool is_opaque_pointer;
264fa2db 6031 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6032 addr_space_t asl;
6033 addr_space_t asr;
400fbf9f 6034
46df2823 6035 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6036 mvl = (TYPE_ATOMIC (mvl)
6037 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6038 TYPE_QUAL_ATOMIC)
6039 : TYPE_MAIN_VARIANT (mvl));
46df2823 6040 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6041 mvr = (TYPE_ATOMIC (mvr)
6042 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6043 TYPE_QUAL_ATOMIC)
6044 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6045 /* Opaque pointers are treated like void pointers. */
f83c7f63 6046 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6047
478a1c5b
ILT
6048 /* The Plan 9 compiler permits a pointer to a struct to be
6049 automatically converted into a pointer to an anonymous field
6050 within the struct. */
6051 if (flag_plan9_extensions
6052 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6053 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6054 && mvl != mvr)
6055 {
6056 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6057 if (new_rhs != NULL_TREE)
6058 {
6059 rhs = new_rhs;
6060 rhstype = TREE_TYPE (rhs);
6061 coder = TREE_CODE (rhstype);
6062 ttr = TREE_TYPE (rhstype);
6063 mvr = TYPE_MAIN_VARIANT (ttr);
6064 }
6065 }
6066
b7e20b53 6067 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6068 for the purpose of reducing the number of false positives, we
6069 tolerate the special case of
b7e20b53 6070
c22cacf3 6071 int *p = NULL;
b7e20b53 6072
c22cacf3 6073 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6074 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6075 warning_at (errtype == ic_argpass ? expr_loc : location,
6076 OPT_Wc___compat,
6077 "request for implicit conversion "
c2255bc4 6078 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6079
36c5e70a
BE
6080 /* See if the pointers point to incompatible address spaces. */
6081 asl = TYPE_ADDR_SPACE (ttl);
6082 asr = TYPE_ADDR_SPACE (ttr);
6083 if (!null_pointer_constant_p (rhs)
6084 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6085 {
6086 switch (errtype)
6087 {
6088 case ic_argpass:
8ffcdea8 6089 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6090 "non-enclosed address space", parmnum, rname);
6091 break;
6092 case ic_assign:
6093 error_at (location, "assignment from pointer to "
6094 "non-enclosed address space");
6095 break;
6096 case ic_init:
6097 error_at (location, "initialization from pointer to "
6098 "non-enclosed address space");
6099 break;
6100 case ic_return:
6101 error_at (location, "return from pointer to "
6102 "non-enclosed address space");
6103 break;
6104 default:
6105 gcc_unreachable ();
6106 }
6107 return error_mark_node;
6108 }
6109
7876a414
KG
6110 /* Check if the right-hand side has a format attribute but the
6111 left-hand side doesn't. */
90137d8f 6112 if (warn_suggest_attribute_format
104f8784 6113 && check_missing_format_attribute (type, rhstype))
c22cacf3 6114 {
104f8784
KG
6115 switch (errtype)
6116 {
6117 case ic_argpass:
8ffcdea8 6118 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6119 "argument %d of %qE might be "
6120 "a candidate for a format attribute",
6121 parmnum, rname);
104f8784
KG
6122 break;
6123 case ic_assign:
90137d8f 6124 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6125 "assignment left-hand side might be "
6126 "a candidate for a format attribute");
104f8784
KG
6127 break;
6128 case ic_init:
90137d8f 6129 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6130 "initialization left-hand side might be "
6131 "a candidate for a format attribute");
104f8784
KG
6132 break;
6133 case ic_return:
90137d8f 6134 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6135 "return type might be "
6136 "a candidate for a format attribute");
104f8784
KG
6137 break;
6138 default:
6139 gcc_unreachable ();
6140 }
7876a414 6141 }
c22cacf3 6142
3e4093b6
RS
6143 /* Any non-function converts to a [const][volatile] void *
6144 and vice versa; otherwise, targets must be the same.
6145 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6146 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6147 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6148 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6149 || is_opaque_pointer
f8a93a2e
JJ
6150 || ((c_common_unsigned_type (mvl)
6151 == c_common_unsigned_type (mvr))
267bac10
JM
6152 && (c_common_signed_type (mvl)
6153 == c_common_signed_type (mvr))
6154 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6155 {
768952be
MU
6156 /* Warn about loss of qualifers from pointers to arrays with
6157 qualifiers on the element type. */
6158 if (TREE_CODE (ttr) == ARRAY_TYPE)
6159 {
6160 ttr = strip_array_types (ttr);
6161 ttl = strip_array_types (ttl);
6162
6163 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6164 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6165 WARNING_FOR_QUALIFIERS (location, expr_loc,
6166 OPT_Wdiscarded_array_qualifiers,
6167 G_("passing argument %d of %qE discards "
6168 "%qv qualifier from pointer target type"),
6169 G_("assignment discards %qv qualifier "
6170 "from pointer target type"),
6171 G_("initialization discards %qv qualifier "
6172 "from pointer target type"),
6173 G_("return discards %qv qualifier from "
6174 "pointer target type"),
6175 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6176 }
6177 else if (pedantic
3e4093b6
RS
6178 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6179 ||
6180 (VOID_TYPE_P (ttr)
928c19bb 6181 && !null_pointer_constant
3e4093b6 6182 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6183 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6184 G_("ISO C forbids passing argument %d of "
6185 "%qE between function pointer "
6186 "and %<void *%>"),
6187 G_("ISO C forbids assignment between "
6188 "function pointer and %<void *%>"),
6189 G_("ISO C forbids initialization between "
6190 "function pointer and %<void *%>"),
6191 G_("ISO C forbids return between function "
6192 "pointer and %<void *%>"));
3e4093b6
RS
6193 /* Const and volatile mean something different for function types,
6194 so the usual warnings are not appropriate. */
6195 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6196 && TREE_CODE (ttl) != FUNCTION_TYPE)
6197 {
768952be
MU
6198 /* Don't warn about loss of qualifier for conversions from
6199 qualified void* to pointers to arrays with corresponding
6200 qualifier on the element type. */
6201 if (!pedantic)
6202 ttl = strip_array_types (ttl);
6203
267bac10
JM
6204 /* Assignments between atomic and non-atomic objects are OK. */
6205 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6206 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6207 {
768952be
MU
6208 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6209 OPT_Wdiscarded_qualifiers,
6210 G_("passing argument %d of %qE discards "
6211 "%qv qualifier from pointer target type"),
6212 G_("assignment discards %qv qualifier "
6213 "from pointer target type"),
6214 G_("initialization discards %qv qualifier "
6215 "from pointer target type"),
6216 G_("return discards %qv qualifier from "
6217 "pointer target type"),
6218 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6219 }
3e4093b6
RS
6220 /* If this is not a case of ignoring a mismatch in signedness,
6221 no warning. */
6222 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6223 || target_cmp)
3e4093b6
RS
6224 ;
6225 /* If there is a mismatch, do warn. */
f2fd3821 6226 else if (warn_pointer_sign)
768952be
MU
6227 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6228 G_("pointer targets in passing argument "
6229 "%d of %qE differ in signedness"),
6230 G_("pointer targets in assignment "
6231 "differ in signedness"),
6232 G_("pointer targets in initialization "
6233 "differ in signedness"),
6234 G_("pointer targets in return differ "
6235 "in signedness"));
3e4093b6
RS
6236 }
6237 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6238 && TREE_CODE (ttr) == FUNCTION_TYPE)
6239 {
6240 /* Because const and volatile on functions are restrictions
6241 that say the function will not do certain things,
6242 it is okay to use a const or volatile function
6243 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6244 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6245 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6246 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6247 OPT_Wdiscarded_qualifiers,
6248 G_("passing argument %d of %qE makes "
6249 "%q#v qualified function pointer "
6250 "from unqualified"),
6251 G_("assignment makes %q#v qualified function "
6252 "pointer from unqualified"),
6253 G_("initialization makes %q#v qualified "
6254 "function pointer from unqualified"),
6255 G_("return makes %q#v qualified function "
6256 "pointer from unqualified"),
6257 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6258 }
6259 }
6260 else
58393038
ZL
6261 /* Avoid warning about the volatile ObjC EH puts on decls. */
6262 if (!objc_ok)
768952be
MU
6263 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6264 OPT_Wincompatible_pointer_types,
6265 G_("passing argument %d of %qE from "
6266 "incompatible pointer type"),
6267 G_("assignment from incompatible pointer type"),
6268 G_("initialization from incompatible "
6269 "pointer type"),
6270 G_("return from incompatible pointer type"));
58393038 6271
3e4093b6
RS
6272 return convert (type, rhs);
6273 }
b494fd98
EB
6274 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6275 {
6dcc04b0
JM
6276 /* ??? This should not be an error when inlining calls to
6277 unprototyped functions. */
c2255bc4 6278 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6279 return error_mark_node;
6280 }
3e4093b6 6281 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6282 {
3e4093b6
RS
6283 /* An explicit constant 0 can convert to a pointer,
6284 or one that results from arithmetic, even including
6285 a cast to integer type. */
928c19bb 6286 if (!null_pointer_constant)
768952be
MU
6287 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6288 OPT_Wint_conversion,
6289 G_("passing argument %d of %qE makes "
6290 "pointer from integer without a cast"),
6291 G_("assignment makes pointer from integer "
6292 "without a cast"),
6293 G_("initialization makes pointer from "
6294 "integer without a cast"),
6295 G_("return makes pointer from integer "
6296 "without a cast"));
b3006337
EB
6297
6298 return convert (type, rhs);
400fbf9f 6299 }
3e4093b6 6300 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6301 {
768952be
MU
6302 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6303 OPT_Wint_conversion,
6304 G_("passing argument %d of %qE makes integer "
6305 "from pointer without a cast"),
6306 G_("assignment makes integer from pointer "
6307 "without a cast"),
6308 G_("initialization makes integer from pointer "
6309 "without a cast"),
6310 G_("return makes integer from pointer "
6311 "without a cast"));
3e4093b6
RS
6312 return convert (type, rhs);
6313 }
6314 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6315 {
6316 tree ret;
6317 bool save = in_late_binary_op;
6318 in_late_binary_op = true;
6319 ret = convert (type, rhs);
6320 in_late_binary_op = save;
6321 return ret;
6322 }
400fbf9f 6323
2ac2f164 6324 switch (errtype)
3e4093b6 6325 {
2ac2f164 6326 case ic_argpass:
8ffcdea8
MP
6327 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6328 rname);
a7da8b42 6329 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6330 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6331 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6332 break;
6333 case ic_assign:
c2255bc4
AH
6334 error_at (location, "incompatible types when assigning to type %qT from "
6335 "type %qT", type, rhstype);
2ac2f164
JM
6336 break;
6337 case ic_init:
c2255bc4 6338 error_at (location,
8ffcdea8 6339 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6340 type, rhstype);
2ac2f164
JM
6341 break;
6342 case ic_return:
c2255bc4 6343 error_at (location,
8ffcdea8 6344 "incompatible types when returning type %qT but %qT was "
c2255bc4 6345 "expected", rhstype, type);
2ac2f164
JM
6346 break;
6347 default:
6348 gcc_unreachable ();
400fbf9f 6349 }
53b01f59 6350
3e4093b6
RS
6351 return error_mark_node;
6352}
3e4093b6
RS
6353\f
6354/* If VALUE is a compound expr all of whose expressions are constant, then
6355 return its value. Otherwise, return error_mark_node.
15b732b2 6356
3e4093b6
RS
6357 This is for handling COMPOUND_EXPRs as initializer elements
6358 which is allowed with a warning when -pedantic is specified. */
15b732b2 6359
3e4093b6
RS
6360static tree
6361valid_compound_expr_initializer (tree value, tree endtype)
6362{
6363 if (TREE_CODE (value) == COMPOUND_EXPR)
6364 {
6365 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6366 == error_mark_node)
6367 return error_mark_node;
6368 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6369 endtype);
6370 }
116df786 6371 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6372 return error_mark_node;
6373 else
6374 return value;
15b732b2 6375}
400fbf9f 6376\f
3e4093b6
RS
6377/* Perform appropriate conversions on the initial value of a variable,
6378 store it in the declaration DECL,
6379 and print any error messages that are appropriate.
bbbbb16a 6380 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6381 If the init is invalid, store an ERROR_MARK.
6382
6383 INIT_LOC is the location of the initial value. */
400fbf9f 6384
3e4093b6 6385void
c2255bc4 6386store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6387{
3e4093b6 6388 tree value, type;
928c19bb 6389 bool npc = false;
400fbf9f 6390
3e4093b6 6391 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6392
3e4093b6
RS
6393 type = TREE_TYPE (decl);
6394 if (TREE_CODE (type) == ERROR_MARK)
6395 return;
400fbf9f 6396
3e4093b6 6397 /* Digest the specified initializer into an expression. */
400fbf9f 6398
928c19bb
JM
6399 if (init)
6400 npc = null_pointer_constant_p (init);
c2255bc4
AH
6401 value = digest_init (init_loc, type, init, origtype, npc,
6402 true, TREE_STATIC (decl));
400fbf9f 6403
3e4093b6 6404 /* Store the expression if valid; else report error. */
400fbf9f 6405
8400e75e 6406 if (!in_system_header_at (input_location)
3f75a254 6407 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6408 warning (OPT_Wtraditional, "traditional C rejects automatic "
6409 "aggregate initialization");
2f6e4e97 6410
3e4093b6 6411 DECL_INITIAL (decl) = value;
400fbf9f 6412
3e4093b6
RS
6413 /* ANSI wants warnings about out-of-range constant initializers. */
6414 STRIP_TYPE_NOPS (value);
b8698a0f 6415 if (TREE_STATIC (decl))
c2658540 6416 constant_expression_warning (value);
400fbf9f 6417
3e4093b6
RS
6418 /* Check if we need to set array size from compound literal size. */
6419 if (TREE_CODE (type) == ARRAY_TYPE
6420 && TYPE_DOMAIN (type) == 0
6421 && value != error_mark_node)
400fbf9f 6422 {
3e4093b6
RS
6423 tree inside_init = init;
6424
ed248cf7 6425 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6426 inside_init = fold (inside_init);
6427
6428 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6429 {
8d9f82d5 6430 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6431
8d9f82d5 6432 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6433 {
6434 /* For int foo[] = (int [3]){1}; we need to set array size
6435 now since later on array initializer will be just the
6436 brace enclosed list of the compound literal. */
e30ecc5d 6437 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6438 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6439 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6440 layout_type (type);
8d9f82d5 6441 layout_decl (cldecl, 0);
e30ecc5d
JJ
6442 TREE_TYPE (decl)
6443 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6444 }
6445 }
400fbf9f 6446 }
3e4093b6
RS
6447}
6448\f
6449/* Methods for storing and printing names for error messages. */
400fbf9f 6450
3e4093b6
RS
6451/* Implement a spelling stack that allows components of a name to be pushed
6452 and popped. Each element on the stack is this structure. */
400fbf9f 6453
3e4093b6
RS
6454struct spelling
6455{
6456 int kind;
6457 union
400fbf9f 6458 {
a0f0ab9f 6459 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6460 const char *s;
6461 } u;
6462};
2f6e4e97 6463
3e4093b6
RS
6464#define SPELLING_STRING 1
6465#define SPELLING_MEMBER 2
6466#define SPELLING_BOUNDS 3
400fbf9f 6467
3e4093b6
RS
6468static struct spelling *spelling; /* Next stack element (unused). */
6469static struct spelling *spelling_base; /* Spelling stack base. */
6470static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6471
3e4093b6
RS
6472/* Macros to save and restore the spelling stack around push_... functions.
6473 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6474
3e4093b6
RS
6475#define SPELLING_DEPTH() (spelling - spelling_base)
6476#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6477
3e4093b6
RS
6478/* Push an element on the spelling stack with type KIND and assign VALUE
6479 to MEMBER. */
400fbf9f 6480
3e4093b6
RS
6481#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6482{ \
6483 int depth = SPELLING_DEPTH (); \
6484 \
6485 if (depth >= spelling_size) \
6486 { \
6487 spelling_size += 10; \
cca8ead2
BI
6488 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6489 spelling_size); \
3e4093b6
RS
6490 RESTORE_SPELLING_DEPTH (depth); \
6491 } \
6492 \
6493 spelling->kind = (KIND); \
6494 spelling->MEMBER = (VALUE); \
6495 spelling++; \
6496}
400fbf9f 6497
3e4093b6 6498/* Push STRING on the stack. Printed literally. */
400fbf9f 6499
3e4093b6
RS
6500static void
6501push_string (const char *string)
6502{
6503 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6504}
400fbf9f 6505
3e4093b6 6506/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6507
3e4093b6
RS
6508static void
6509push_member_name (tree decl)
6510{
6511 const char *const string
88388a52
JM
6512 = (DECL_NAME (decl)
6513 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6514 : _("<anonymous>"));
3e4093b6
RS
6515 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6516}
400fbf9f 6517
3e4093b6 6518/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6519
3e4093b6 6520static void
a0f0ab9f 6521push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6522{
6523 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6524}
bb58bec5 6525
3e4093b6 6526/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6527
3e4093b6
RS
6528static int
6529spelling_length (void)
6530{
6531 int size = 0;
6532 struct spelling *p;
400fbf9f 6533
3e4093b6
RS
6534 for (p = spelling_base; p < spelling; p++)
6535 {
6536 if (p->kind == SPELLING_BOUNDS)
6537 size += 25;
6538 else
6539 size += strlen (p->u.s) + 1;
6540 }
6541
6542 return size;
400fbf9f 6543}
400fbf9f 6544
3e4093b6 6545/* Print the spelling to BUFFER and return it. */
400fbf9f 6546
3e4093b6
RS
6547static char *
6548print_spelling (char *buffer)
400fbf9f 6549{
3e4093b6
RS
6550 char *d = buffer;
6551 struct spelling *p;
400fbf9f 6552
3e4093b6
RS
6553 for (p = spelling_base; p < spelling; p++)
6554 if (p->kind == SPELLING_BOUNDS)
6555 {
a0f0ab9f 6556 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6557 d += strlen (d);
6558 }
6559 else
6560 {
6561 const char *s;
6562 if (p->kind == SPELLING_MEMBER)
6563 *d++ = '.';
6564 for (s = p->u.s; (*d = *s++); d++)
6565 ;
6566 }
6567 *d++ = '\0';
6568 return buffer;
6569}
400fbf9f 6570
3e4093b6
RS
6571/* Digest the parser output INIT as an initializer for type TYPE.
6572 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6573
bbbbb16a
ILT
6574 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6575
928c19bb
JM
6576 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6577
916c5919
JM
6578 If INIT is a string constant, STRICT_STRING is true if it is
6579 unparenthesized or we should not warn here for it being parenthesized.
6580 For other types of INIT, STRICT_STRING is not used.
6581
c2255bc4
AH
6582 INIT_LOC is the location of the INIT.
6583
3e4093b6
RS
6584 REQUIRE_CONSTANT requests an error if non-constant initializers or
6585 elements are seen. */
7e842ef8 6586
3e4093b6 6587static tree
c2255bc4
AH
6588digest_init (location_t init_loc, tree type, tree init, tree origtype,
6589 bool null_pointer_constant, bool strict_string,
6590 int require_constant)
3e4093b6
RS
6591{
6592 enum tree_code code = TREE_CODE (type);
6593 tree inside_init = init;
8ce94e44 6594 tree semantic_type = NULL_TREE;
928c19bb 6595 bool maybe_const = true;
7e842ef8 6596
3e4093b6 6597 if (type == error_mark_node
f01da1a5 6598 || !init
7a0ca710 6599 || error_operand_p (init))
3e4093b6 6600 return error_mark_node;
7e842ef8 6601
ed248cf7 6602 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6603
8ce94e44
JM
6604 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6605 {
6606 semantic_type = TREE_TYPE (inside_init);
6607 inside_init = TREE_OPERAND (inside_init, 0);
6608 }
928c19bb
JM
6609 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6610 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6611
3e4093b6
RS
6612 /* Initialization of an array of chars from a string constant
6613 optionally enclosed in braces. */
7e842ef8 6614
197463ae
JM
6615 if (code == ARRAY_TYPE && inside_init
6616 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6617 {
267bac10
JM
6618 tree typ1
6619 = (TYPE_ATOMIC (TREE_TYPE (type))
6620 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6621 TYPE_QUAL_ATOMIC)
6622 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6623 /* Note that an array could be both an array of character type
6624 and an array of wchar_t if wchar_t is signed char or unsigned
6625 char. */
6626 bool char_array = (typ1 == char_type_node
6627 || typ1 == signed_char_type_node
6628 || typ1 == unsigned_char_type_node);
6629 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6630 bool char16_array = !!comptypes (typ1, char16_type_node);
6631 bool char32_array = !!comptypes (typ1, char32_type_node);
6632
6633 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6634 {
916c5919 6635 struct c_expr expr;
c466b2cd 6636 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6637 expr.value = inside_init;
6638 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6639 expr.original_type = NULL;
d033409e 6640 maybe_warn_string_init (init_loc, type, expr);
916c5919 6641
a45e580b 6642 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6643 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6644 "initialization of a flexible array member");
6645
3e4093b6 6646 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6647 TYPE_MAIN_VARIANT (type)))
3e4093b6 6648 return inside_init;
7e842ef8 6649
c466b2cd 6650 if (char_array)
3e4093b6 6651 {
c466b2cd
KVH
6652 if (typ2 != char_type_node)
6653 {
ea58ef42
MP
6654 error_init (init_loc, "char-array initialized from wide "
6655 "string");
c466b2cd
KVH
6656 return error_mark_node;
6657 }
3e4093b6 6658 }
c466b2cd 6659 else
3e4093b6 6660 {
c466b2cd
KVH
6661 if (typ2 == char_type_node)
6662 {
ea58ef42
MP
6663 error_init (init_loc, "wide character array initialized "
6664 "from non-wide string");
c466b2cd
KVH
6665 return error_mark_node;
6666 }
6667 else if (!comptypes(typ1, typ2))
6668 {
ea58ef42
MP
6669 error_init (init_loc, "wide character array initialized "
6670 "from incompatible wide string");
c466b2cd
KVH
6671 return error_mark_node;
6672 }
7e842ef8 6673 }
2f6e4e97 6674
3e4093b6
RS
6675 TREE_TYPE (inside_init) = type;
6676 if (TYPE_DOMAIN (type) != 0
6677 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6678 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6679 {
6680 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6681
c466b2cd 6682 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6683 because it's ok to ignore the terminating null char
6684 that is counted in the length of the constant. */
5eb4df45
ILT
6685 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6686 (len
6687 - (TYPE_PRECISION (typ1)
6688 / BITS_PER_UNIT))))
6689 pedwarn_init (init_loc, 0,
6690 ("initializer-string for array of chars "
6691 "is too long"));
6692 else if (warn_cxx_compat
6693 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6694 warning_at (init_loc, OPT_Wc___compat,
6695 ("initializer-string for array chars "
6696 "is too long for C++"));
6697 }
7e842ef8 6698
3e4093b6 6699 return inside_init;
7e842ef8 6700 }
197463ae
JM
6701 else if (INTEGRAL_TYPE_P (typ1))
6702 {
ea58ef42 6703 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6704 "from string constant");
6705 return error_mark_node;
6706 }
7e842ef8
PE
6707 }
6708
3e4093b6
RS
6709 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6710 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6711 below and handle as a constructor. */
e89be13b
JJ
6712 if (code == VECTOR_TYPE
6713 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6714 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6715 && TREE_CONSTANT (inside_init))
6716 {
6717 if (TREE_CODE (inside_init) == VECTOR_CST
6718 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6719 TYPE_MAIN_VARIANT (type)))
6720 return inside_init;
6721
6722 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6723 {
4038c495
GB
6724 unsigned HOST_WIDE_INT ix;
6725 tree value;
6726 bool constant_p = true;
e89be13b
JJ
6727
6728 /* Iterate through elements and check if all constructor
6729 elements are *_CSTs. */
4038c495
GB
6730 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6731 if (!CONSTANT_CLASS_P (value))
6732 {
6733 constant_p = false;
6734 break;
6735 }
e89be13b 6736
4038c495
GB
6737 if (constant_p)
6738 return build_vector_from_ctor (type,
6739 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6740 }
6741 }
6035d635 6742
ca085fd7
MLI
6743 if (warn_sequence_point)
6744 verify_sequence_points (inside_init);
6745
3e4093b6
RS
6746 /* Any type can be initialized
6747 from an expression of the same type, optionally with braces. */
400fbf9f 6748
3e4093b6
RS
6749 if (inside_init && TREE_TYPE (inside_init) != 0
6750 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6751 TYPE_MAIN_VARIANT (type))
3e4093b6 6752 || (code == ARRAY_TYPE
132da1a5 6753 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6754 || (code == VECTOR_TYPE
132da1a5 6755 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6756 || (code == POINTER_TYPE
3897f229 6757 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6758 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6759 TREE_TYPE (type)))))
3e4093b6
RS
6760 {
6761 if (code == POINTER_TYPE)
b494fd98 6762 {
b494fd98
EB
6763 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6764 {
f2a71bbc
JM
6765 if (TREE_CODE (inside_init) == STRING_CST
6766 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6767 inside_init = array_to_pointer_conversion
6768 (init_loc, inside_init);
f2a71bbc
JM
6769 else
6770 {
ea58ef42 6771 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
6772 return error_mark_node;
6773 }
b494fd98 6774 }
f2a71bbc 6775 }
b494fd98 6776
bae39a73
NS
6777 if (code == VECTOR_TYPE)
6778 /* Although the types are compatible, we may require a
6779 conversion. */
6780 inside_init = convert (type, inside_init);
3e4093b6 6781
ca58211b 6782 if (require_constant
3e4093b6 6783 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6784 {
3e4093b6
RS
6785 /* As an extension, allow initializing objects with static storage
6786 duration with compound literals (which are then treated just as
ca58211b
PB
6787 the brace enclosed list they contain). Also allow this for
6788 vectors, as we can only assign them with compound literals. */
7278465e
MP
6789 if (flag_isoc99 && code != VECTOR_TYPE)
6790 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6791 "is not constant");
3e4093b6
RS
6792 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6793 inside_init = DECL_INITIAL (decl);
400fbf9f 6794 }
3e4093b6
RS
6795
6796 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6797 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6798 {
ea58ef42
MP
6799 error_init (init_loc, "array initialized from non-constant array "
6800 "expression");
3e4093b6 6801 return error_mark_node;
400fbf9f 6802 }
400fbf9f 6803
c1771a20 6804 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6805 -pedantic-errors is specified. In the later case, we always want
6806 an error. In the former case, we simply want a warning. */
6807 if (require_constant && pedantic
6808 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6809 {
6810 inside_init
6811 = valid_compound_expr_initializer (inside_init,
6812 TREE_TYPE (inside_init));
6813 if (inside_init == error_mark_node)
ea58ef42 6814 error_init (init_loc, "initializer element is not constant");
2f6e4e97 6815 else
c1771a20 6816 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6817 "initializer element is not constant");
3e4093b6
RS
6818 if (flag_pedantic_errors)
6819 inside_init = error_mark_node;
6820 }
6821 else if (require_constant
116df786
RH
6822 && !initializer_constant_valid_p (inside_init,
6823 TREE_TYPE (inside_init)))
3e4093b6 6824 {
ea58ef42 6825 error_init (init_loc, "initializer element is not constant");
3e4093b6 6826 inside_init = error_mark_node;
8b40563c 6827 }
928c19bb 6828 else if (require_constant && !maybe_const)
c2255bc4 6829 pedwarn_init (init_loc, 0,
928c19bb 6830 "initializer element is not a constant expression");
f735a153 6831
90137d8f 6832 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6833 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
6834 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6835 type, inside_init, origtype,
bbbbb16a 6836 ic_init, null_pointer_constant,
928c19bb 6837 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6838 return inside_init;
6839 }
f735a153 6840
3e4093b6 6841 /* Handle scalar types, including conversions. */
400fbf9f 6842
ab22c1fa
CF
6843 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6844 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6845 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6846 {
f2a71bbc
JM
6847 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6848 && (TREE_CODE (init) == STRING_CST
6849 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6850 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6851 if (semantic_type)
6852 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6853 inside_init);
3e4093b6 6854 inside_init
68fca595
MP
6855 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6856 inside_init, origtype, ic_init,
6857 null_pointer_constant, NULL_TREE, NULL_TREE,
6858 0);
2f6e4e97 6859
3274deff
JW
6860 /* Check to see if we have already given an error message. */
6861 if (inside_init == error_mark_node)
6862 ;
3f75a254 6863 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6864 {
ea58ef42 6865 error_init (init_loc, "initializer element is not constant");
3e4093b6 6866 inside_init = error_mark_node;
400fbf9f 6867 }
3e4093b6 6868 else if (require_constant
116df786
RH
6869 && !initializer_constant_valid_p (inside_init,
6870 TREE_TYPE (inside_init)))
400fbf9f 6871 {
ea58ef42
MP
6872 error_init (init_loc, "initializer element is not computable at "
6873 "load time");
3e4093b6 6874 inside_init = error_mark_node;
400fbf9f 6875 }
928c19bb 6876 else if (require_constant && !maybe_const)
c2255bc4 6877 pedwarn_init (init_loc, 0,
928c19bb 6878 "initializer element is not a constant expression");
3e4093b6
RS
6879
6880 return inside_init;
400fbf9f 6881 }
d9fc6069 6882
3e4093b6 6883 /* Come here only for records and arrays. */
d9fc6069 6884
3e4093b6 6885 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6886 {
ea58ef42 6887 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 6888 return error_mark_node;
d9fc6069 6889 }
3e4093b6 6890
ea58ef42 6891 error_init (init_loc, "invalid initializer");
3e4093b6 6892 return error_mark_node;
d9fc6069 6893}
400fbf9f 6894\f
3e4093b6 6895/* Handle initializers that use braces. */
400fbf9f 6896
3e4093b6
RS
6897/* Type of object we are accumulating a constructor for.
6898 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6899static tree constructor_type;
400fbf9f 6900
3e4093b6
RS
6901/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6902 left to fill. */
6903static tree constructor_fields;
400fbf9f 6904
3e4093b6
RS
6905/* For an ARRAY_TYPE, this is the specified index
6906 at which to store the next element we get. */
6907static tree constructor_index;
400fbf9f 6908
3e4093b6
RS
6909/* For an ARRAY_TYPE, this is the maximum index. */
6910static tree constructor_max_index;
400fbf9f 6911
3e4093b6
RS
6912/* For a RECORD_TYPE, this is the first field not yet written out. */
6913static tree constructor_unfilled_fields;
400fbf9f 6914
3e4093b6
RS
6915/* For an ARRAY_TYPE, this is the index of the first element
6916 not yet written out. */
6917static tree constructor_unfilled_index;
895ea614 6918
3e4093b6
RS
6919/* In a RECORD_TYPE, the byte index of the next consecutive field.
6920 This is so we can generate gaps between fields, when appropriate. */
6921static tree constructor_bit_index;
10d5caec 6922
3e4093b6
RS
6923/* If we are saving up the elements rather than allocating them,
6924 this is the list of elements so far (in reverse order,
6925 most recent first). */
9771b263 6926static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6927
3e4093b6
RS
6928/* 1 if constructor should be incrementally stored into a constructor chain,
6929 0 if all the elements should be kept in AVL tree. */
6930static int constructor_incremental;
ad47f1e5 6931
3e4093b6
RS
6932/* 1 if so far this constructor's elements are all compile-time constants. */
6933static int constructor_constant;
ad47f1e5 6934
3e4093b6
RS
6935/* 1 if so far this constructor's elements are all valid address constants. */
6936static int constructor_simple;
ad47f1e5 6937
928c19bb
JM
6938/* 1 if this constructor has an element that cannot be part of a
6939 constant expression. */
6940static int constructor_nonconst;
6941
3e4093b6
RS
6942/* 1 if this constructor is erroneous so far. */
6943static int constructor_erroneous;
d45cf215 6944
9bac5cbb
G
6945/* 1 if this constructor is the universal zero initializer { 0 }. */
6946static int constructor_zeroinit;
6947
3e4093b6
RS
6948/* Structure for managing pending initializer elements, organized as an
6949 AVL tree. */
d45cf215 6950
3e4093b6 6951struct init_node
d45cf215 6952{
3e4093b6
RS
6953 struct init_node *left, *right;
6954 struct init_node *parent;
6955 int balance;
6956 tree purpose;
6957 tree value;
bbbbb16a 6958 tree origtype;
d45cf215
RS
6959};
6960
3e4093b6
RS
6961/* Tree of pending elements at this constructor level.
6962 These are elements encountered out of order
6963 which belong at places we haven't reached yet in actually
6964 writing the output.
6965 Will never hold tree nodes across GC runs. */
6966static struct init_node *constructor_pending_elts;
d45cf215 6967
3e4093b6
RS
6968/* The SPELLING_DEPTH of this constructor. */
6969static int constructor_depth;
d45cf215 6970
3e4093b6
RS
6971/* DECL node for which an initializer is being read.
6972 0 means we are reading a constructor expression
6973 such as (struct foo) {...}. */
6974static tree constructor_decl;
d45cf215 6975
3e4093b6
RS
6976/* Nonzero if this is an initializer for a top-level decl. */
6977static int constructor_top_level;
d45cf215 6978
3e4093b6
RS
6979/* Nonzero if there were any member designators in this initializer. */
6980static int constructor_designated;
d45cf215 6981
3e4093b6
RS
6982/* Nesting depth of designator list. */
6983static int designator_depth;
d45cf215 6984
3e4093b6 6985/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 6986static int designator_erroneous;
d45cf215 6987
3e4093b6
RS
6988\f
6989/* This stack has a level for each implicit or explicit level of
6990 structuring in the initializer, including the outermost one. It
6991 saves the values of most of the variables above. */
d45cf215 6992
3e4093b6
RS
6993struct constructor_range_stack;
6994
6995struct constructor_stack
d45cf215 6996{
3e4093b6
RS
6997 struct constructor_stack *next;
6998 tree type;
6999 tree fields;
7000 tree index;
7001 tree max_index;
7002 tree unfilled_index;
7003 tree unfilled_fields;
7004 tree bit_index;
9771b263 7005 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7006 struct init_node *pending_elts;
7007 int offset;
7008 int depth;
916c5919 7009 /* If value nonzero, this value should replace the entire
3e4093b6 7010 constructor at this level. */
916c5919 7011 struct c_expr replacement_value;
3e4093b6
RS
7012 struct constructor_range_stack *range_stack;
7013 char constant;
7014 char simple;
928c19bb 7015 char nonconst;
3e4093b6
RS
7016 char implicit;
7017 char erroneous;
7018 char outer;
7019 char incremental;
7020 char designated;
976d5a22 7021 int designator_depth;
3e4093b6 7022};
d45cf215 7023
802415d1 7024static struct constructor_stack *constructor_stack;
d45cf215 7025
3e4093b6
RS
7026/* This stack represents designators from some range designator up to
7027 the last designator in the list. */
d45cf215 7028
3e4093b6
RS
7029struct constructor_range_stack
7030{
7031 struct constructor_range_stack *next, *prev;
7032 struct constructor_stack *stack;
7033 tree range_start;
7034 tree index;
7035 tree range_end;
7036 tree fields;
7037};
d45cf215 7038
802415d1 7039static struct constructor_range_stack *constructor_range_stack;
d45cf215 7040
3e4093b6
RS
7041/* This stack records separate initializers that are nested.
7042 Nested initializers can't happen in ANSI C, but GNU C allows them
7043 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7044
3e4093b6 7045struct initializer_stack
d45cf215 7046{
3e4093b6
RS
7047 struct initializer_stack *next;
7048 tree decl;
3e4093b6
RS
7049 struct constructor_stack *constructor_stack;
7050 struct constructor_range_stack *constructor_range_stack;
9771b263 7051 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7052 struct spelling *spelling;
7053 struct spelling *spelling_base;
7054 int spelling_size;
7055 char top_level;
7056 char require_constant_value;
7057 char require_constant_elements;
7058};
d45cf215 7059
802415d1 7060static struct initializer_stack *initializer_stack;
3e4093b6
RS
7061\f
7062/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7063
7064void
a396f8ae 7065start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7066{
3e4093b6 7067 const char *locus;
cceb1885 7068 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7069
3e4093b6 7070 p->decl = constructor_decl;
3e4093b6
RS
7071 p->require_constant_value = require_constant_value;
7072 p->require_constant_elements = require_constant_elements;
7073 p->constructor_stack = constructor_stack;
7074 p->constructor_range_stack = constructor_range_stack;
7075 p->elements = constructor_elements;
7076 p->spelling = spelling;
7077 p->spelling_base = spelling_base;
7078 p->spelling_size = spelling_size;
7079 p->top_level = constructor_top_level;
7080 p->next = initializer_stack;
7081 initializer_stack = p;
400fbf9f 7082
3e4093b6 7083 constructor_decl = decl;
3e4093b6
RS
7084 constructor_designated = 0;
7085 constructor_top_level = top_level;
400fbf9f 7086
6f17bbcf 7087 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7088 {
7089 require_constant_value = TREE_STATIC (decl);
7090 require_constant_elements
7091 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7092 /* For a scalar, you can always use any value to initialize,
7093 even within braces. */
7094 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7095 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7096 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7097 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 7098 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7099 }
7100 else
7101 {
7102 require_constant_value = 0;
7103 require_constant_elements = 0;
88388a52 7104 locus = _("(anonymous)");
3e4093b6 7105 }
b71c7f8a 7106
3e4093b6
RS
7107 constructor_stack = 0;
7108 constructor_range_stack = 0;
b71c7f8a 7109
9bac5cbb 7110 found_missing_braces = 0;
3e4093b6
RS
7111
7112 spelling_base = 0;
7113 spelling_size = 0;
7114 RESTORE_SPELLING_DEPTH (0);
7115
7116 if (locus)
7117 push_string (locus);
7118}
7119
7120void
7121finish_init (void)
b71c7f8a 7122{
3e4093b6 7123 struct initializer_stack *p = initializer_stack;
b71c7f8a 7124
3e4093b6
RS
7125 /* Free the whole constructor stack of this initializer. */
7126 while (constructor_stack)
7127 {
7128 struct constructor_stack *q = constructor_stack;
7129 constructor_stack = q->next;
7130 free (q);
7131 }
7132
366de0ce 7133 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7134
7135 /* Pop back to the data of the outer initializer (if any). */
36579663 7136 free (spelling_base);
3aeb3655 7137
3e4093b6 7138 constructor_decl = p->decl;
3e4093b6
RS
7139 require_constant_value = p->require_constant_value;
7140 require_constant_elements = p->require_constant_elements;
7141 constructor_stack = p->constructor_stack;
7142 constructor_range_stack = p->constructor_range_stack;
7143 constructor_elements = p->elements;
7144 spelling = p->spelling;
7145 spelling_base = p->spelling_base;
7146 spelling_size = p->spelling_size;
7147 constructor_top_level = p->top_level;
7148 initializer_stack = p->next;
7149 free (p);
b71c7f8a 7150}
400fbf9f 7151\f
3e4093b6
RS
7152/* Call here when we see the initializer is surrounded by braces.
7153 This is instead of a call to push_init_level;
7154 it is matched by a call to pop_init_level.
400fbf9f 7155
3e4093b6
RS
7156 TYPE is the type to initialize, for a constructor expression.
7157 For an initializer for a decl, TYPE is zero. */
400fbf9f 7158
3e4093b6
RS
7159void
7160really_start_incremental_init (tree type)
400fbf9f 7161{
5d038c4c 7162 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7163
3e4093b6
RS
7164 if (type == 0)
7165 type = TREE_TYPE (constructor_decl);
400fbf9f 7166
b6fc2cdb
PB
7167 if (TREE_CODE (type) == VECTOR_TYPE
7168 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7169 error ("opaque vector types cannot be initialized");
400fbf9f 7170
3e4093b6
RS
7171 p->type = constructor_type;
7172 p->fields = constructor_fields;
7173 p->index = constructor_index;
7174 p->max_index = constructor_max_index;
7175 p->unfilled_index = constructor_unfilled_index;
7176 p->unfilled_fields = constructor_unfilled_fields;
7177 p->bit_index = constructor_bit_index;
7178 p->elements = constructor_elements;
7179 p->constant = constructor_constant;
7180 p->simple = constructor_simple;
928c19bb 7181 p->nonconst = constructor_nonconst;
3e4093b6
RS
7182 p->erroneous = constructor_erroneous;
7183 p->pending_elts = constructor_pending_elts;
7184 p->depth = constructor_depth;
916c5919
JM
7185 p->replacement_value.value = 0;
7186 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7187 p->replacement_value.original_type = NULL;
3e4093b6
RS
7188 p->implicit = 0;
7189 p->range_stack = 0;
7190 p->outer = 0;
7191 p->incremental = constructor_incremental;
7192 p->designated = constructor_designated;
976d5a22 7193 p->designator_depth = designator_depth;
3e4093b6
RS
7194 p->next = 0;
7195 constructor_stack = p;
b13aca19 7196
3e4093b6
RS
7197 constructor_constant = 1;
7198 constructor_simple = 1;
928c19bb 7199 constructor_nonconst = 0;
3e4093b6 7200 constructor_depth = SPELLING_DEPTH ();
9771b263 7201 constructor_elements = NULL;
3e4093b6
RS
7202 constructor_pending_elts = 0;
7203 constructor_type = type;
7204 constructor_incremental = 1;
7205 constructor_designated = 0;
9bac5cbb 7206 constructor_zeroinit = 1;
3e4093b6 7207 designator_depth = 0;
b06df647 7208 designator_erroneous = 0;
400fbf9f 7209
3e4093b6
RS
7210 if (TREE_CODE (constructor_type) == RECORD_TYPE
7211 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7212 {
3e4093b6
RS
7213 constructor_fields = TYPE_FIELDS (constructor_type);
7214 /* Skip any nameless bit fields at the beginning. */
7215 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7216 && DECL_NAME (constructor_fields) == 0)
910ad8de 7217 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7218
3e4093b6
RS
7219 constructor_unfilled_fields = constructor_fields;
7220 constructor_bit_index = bitsize_zero_node;
400fbf9f 7221 }
3e4093b6
RS
7222 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7223 {
7224 if (TYPE_DOMAIN (constructor_type))
7225 {
7226 constructor_max_index
7227 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7228
3e4093b6
RS
7229 /* Detect non-empty initializations of zero-length arrays. */
7230 if (constructor_max_index == NULL_TREE
7231 && TYPE_SIZE (constructor_type))
9a9d280e 7232 constructor_max_index = integer_minus_one_node;
400fbf9f 7233
3e4093b6
RS
7234 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7235 to initialize VLAs will cause a proper error; avoid tree
7236 checking errors as well by setting a safe value. */
7237 if (constructor_max_index
7238 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7239 constructor_max_index = integer_minus_one_node;
59c83dbf 7240
3e4093b6
RS
7241 constructor_index
7242 = convert (bitsizetype,
7243 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7244 }
3e4093b6 7245 else
493179da
JM
7246 {
7247 constructor_index = bitsize_zero_node;
7248 constructor_max_index = NULL_TREE;
7249 }
59c83dbf 7250
3e4093b6
RS
7251 constructor_unfilled_index = constructor_index;
7252 }
7253 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7254 {
7255 /* Vectors are like simple fixed-size arrays. */
7256 constructor_max_index =
c62c040f 7257 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7258 constructor_index = bitsize_zero_node;
3e4093b6
RS
7259 constructor_unfilled_index = constructor_index;
7260 }
7261 else
7262 {
7263 /* Handle the case of int x = {5}; */
7264 constructor_fields = constructor_type;
7265 constructor_unfilled_fields = constructor_type;
7266 }
7267}
7268\f
7269/* Push down into a subobject, for initialization.
7270 If this is for an explicit set of braces, IMPLICIT is 0.
7271 If it is because the next element belongs at a lower level,
7272 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7273
3e4093b6 7274void
ea58ef42
MP
7275push_init_level (location_t loc, int implicit,
7276 struct obstack *braced_init_obstack)
3e4093b6
RS
7277{
7278 struct constructor_stack *p;
7279 tree value = NULL_TREE;
400fbf9f 7280
3e4093b6 7281 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7282 pop them now. If implicit == 1, this will have been done in
7283 process_init_element; do not repeat it here because in the case
7284 of excess initializers for an empty aggregate this leads to an
7285 infinite cycle of popping a level and immediately recreating
7286 it. */
7287 if (implicit != 1)
3e4093b6 7288 {
472d98b4
JM
7289 while (constructor_stack->implicit)
7290 {
7291 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7292 || TREE_CODE (constructor_type) == UNION_TYPE)
7293 && constructor_fields == 0)
34cf811f 7294 process_init_element (input_location,
ea58ef42 7295 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7296 true, braced_init_obstack);
472d98b4
JM
7297 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7298 && constructor_max_index
7299 && tree_int_cst_lt (constructor_max_index,
7300 constructor_index))
34cf811f 7301 process_init_element (input_location,
ea58ef42 7302 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7303 true, braced_init_obstack);
472d98b4
JM
7304 else
7305 break;
7306 }
3e4093b6 7307 }
400fbf9f 7308
3e4093b6
RS
7309 /* Unless this is an explicit brace, we need to preserve previous
7310 content if any. */
7311 if (implicit)
7312 {
7313 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7314 || TREE_CODE (constructor_type) == UNION_TYPE)
7315 && constructor_fields)
a1e3b3d9 7316 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7317 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7318 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7319 }
7320
5d038c4c 7321 p = XNEW (struct constructor_stack);
3e4093b6
RS
7322 p->type = constructor_type;
7323 p->fields = constructor_fields;
7324 p->index = constructor_index;
7325 p->max_index = constructor_max_index;
7326 p->unfilled_index = constructor_unfilled_index;
7327 p->unfilled_fields = constructor_unfilled_fields;
7328 p->bit_index = constructor_bit_index;
7329 p->elements = constructor_elements;
7330 p->constant = constructor_constant;
7331 p->simple = constructor_simple;
928c19bb 7332 p->nonconst = constructor_nonconst;
3e4093b6
RS
7333 p->erroneous = constructor_erroneous;
7334 p->pending_elts = constructor_pending_elts;
7335 p->depth = constructor_depth;
916c5919
JM
7336 p->replacement_value.value = 0;
7337 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7338 p->replacement_value.original_type = NULL;
3e4093b6
RS
7339 p->implicit = implicit;
7340 p->outer = 0;
7341 p->incremental = constructor_incremental;
7342 p->designated = constructor_designated;
976d5a22 7343 p->designator_depth = designator_depth;
3e4093b6
RS
7344 p->next = constructor_stack;
7345 p->range_stack = 0;
7346 constructor_stack = p;
400fbf9f 7347
3e4093b6
RS
7348 constructor_constant = 1;
7349 constructor_simple = 1;
928c19bb 7350 constructor_nonconst = 0;
3e4093b6 7351 constructor_depth = SPELLING_DEPTH ();
9771b263 7352 constructor_elements = NULL;
3e4093b6
RS
7353 constructor_incremental = 1;
7354 constructor_designated = 0;
7355 constructor_pending_elts = 0;
7356 if (!implicit)
400fbf9f 7357 {
3e4093b6
RS
7358 p->range_stack = constructor_range_stack;
7359 constructor_range_stack = 0;
7360 designator_depth = 0;
b06df647 7361 designator_erroneous = 0;
3e4093b6 7362 }
400fbf9f 7363
3e4093b6
RS
7364 /* Don't die if an entire brace-pair level is superfluous
7365 in the containing level. */
7366 if (constructor_type == 0)
7367 ;
7368 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7369 || TREE_CODE (constructor_type) == UNION_TYPE)
7370 {
7371 /* Don't die if there are extra init elts at the end. */
7372 if (constructor_fields == 0)
7373 constructor_type = 0;
7374 else
400fbf9f 7375 {
3e4093b6
RS
7376 constructor_type = TREE_TYPE (constructor_fields);
7377 push_member_name (constructor_fields);
7378 constructor_depth++;
400fbf9f 7379 }
6a358dcb
MP
7380 /* If upper initializer is designated, then mark this as
7381 designated too to prevent bogus warnings. */
7382 constructor_designated = p->designated;
3e4093b6
RS
7383 }
7384 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7385 {
7386 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7387 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7388 constructor_depth++;
400fbf9f
JW
7389 }
7390
3e4093b6 7391 if (constructor_type == 0)
400fbf9f 7392 {
ea58ef42 7393 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7394 constructor_fields = 0;
7395 constructor_unfilled_fields = 0;
7396 return;
400fbf9f
JW
7397 }
7398
3e4093b6
RS
7399 if (value && TREE_CODE (value) == CONSTRUCTOR)
7400 {
7401 constructor_constant = TREE_CONSTANT (value);
7402 constructor_simple = TREE_STATIC (value);
928c19bb 7403 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7404 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7405 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7406 && (TREE_CODE (constructor_type) == RECORD_TYPE
7407 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7408 set_nonincremental_init (braced_init_obstack);
3e4093b6 7409 }
400fbf9f 7410
9bac5cbb
G
7411 if (implicit == 1)
7412 found_missing_braces = 1;
400fbf9f 7413
3e4093b6
RS
7414 if (TREE_CODE (constructor_type) == RECORD_TYPE
7415 || TREE_CODE (constructor_type) == UNION_TYPE)
7416 {
7417 constructor_fields = TYPE_FIELDS (constructor_type);
7418 /* Skip any nameless bit fields at the beginning. */
7419 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7420 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7421 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7422
3e4093b6
RS
7423 constructor_unfilled_fields = constructor_fields;
7424 constructor_bit_index = bitsize_zero_node;
7425 }
7426 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7427 {
7428 /* Vectors are like simple fixed-size arrays. */
7429 constructor_max_index =
c62c040f
RG
7430 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7431 constructor_index = bitsize_int (0);
3e4093b6
RS
7432 constructor_unfilled_index = constructor_index;
7433 }
7434 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7435 {
7436 if (TYPE_DOMAIN (constructor_type))
7437 {
7438 constructor_max_index
7439 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7440
3e4093b6
RS
7441 /* Detect non-empty initializations of zero-length arrays. */
7442 if (constructor_max_index == NULL_TREE
7443 && TYPE_SIZE (constructor_type))
9a9d280e 7444 constructor_max_index = integer_minus_one_node;
de520661 7445
3e4093b6
RS
7446 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7447 to initialize VLAs will cause a proper error; avoid tree
7448 checking errors as well by setting a safe value. */
7449 if (constructor_max_index
7450 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7451 constructor_max_index = integer_minus_one_node;
b62acd60 7452
3e4093b6
RS
7453 constructor_index
7454 = convert (bitsizetype,
7455 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7456 }
7457 else
7458 constructor_index = bitsize_zero_node;
de520661 7459
3e4093b6
RS
7460 constructor_unfilled_index = constructor_index;
7461 if (value && TREE_CODE (value) == STRING_CST)
7462 {
7463 /* We need to split the char/wchar array into individual
7464 characters, so that we don't have to special case it
7465 everywhere. */
a1e3b3d9 7466 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7467 }
7468 }
7469 else
7470 {
b4519d39 7471 if (constructor_type != error_mark_node)
96b40f8d 7472 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7473 constructor_fields = constructor_type;
7474 constructor_unfilled_fields = constructor_type;
7475 }
7476}
8b6a5902 7477
3e4093b6 7478/* At the end of an implicit or explicit brace level,
916c5919
JM
7479 finish up that level of constructor. If a single expression
7480 with redundant braces initialized that level, return the
7481 c_expr structure for that expression. Otherwise, the original_code
7482 element is set to ERROR_MARK.
7483 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7484 from inner levels (process_init_element ignores that),
916c5919 7485 but return error_mark_node as the value from the outermost level
3e4093b6 7486 (that's what we want to put in DECL_INITIAL).
916c5919 7487 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7488
916c5919 7489struct c_expr
ea58ef42
MP
7490pop_init_level (location_t loc, int implicit,
7491 struct obstack *braced_init_obstack)
3e4093b6
RS
7492{
7493 struct constructor_stack *p;
916c5919
JM
7494 struct c_expr ret;
7495 ret.value = 0;
7496 ret.original_code = ERROR_MARK;
6866c6e8 7497 ret.original_type = NULL;
de520661 7498
3e4093b6
RS
7499 if (implicit == 0)
7500 {
7501 /* When we come to an explicit close brace,
7502 pop any inner levels that didn't have explicit braces. */
7503 while (constructor_stack->implicit)
34cf811f 7504 process_init_element (input_location,
ea58ef42 7505 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7506 true, braced_init_obstack);
366de0ce 7507 gcc_assert (!constructor_range_stack);
3e4093b6 7508 }
e5e809f4 7509
0066ef9c
RH
7510 /* Now output all pending elements. */
7511 constructor_incremental = 1;
a1e3b3d9 7512 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7513
3e4093b6 7514 p = constructor_stack;
e5e809f4 7515
3e4093b6
RS
7516 /* Error for initializing a flexible array member, or a zero-length
7517 array member in an inappropriate context. */
7518 if (constructor_type && constructor_fields
7519 && TREE_CODE (constructor_type) == ARRAY_TYPE
7520 && TYPE_DOMAIN (constructor_type)
3f75a254 7521 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7522 {
7523 /* Silently discard empty initializations. The parser will
7524 already have pedwarned for empty brackets. */
7525 if (integer_zerop (constructor_unfilled_index))
7526 constructor_type = NULL_TREE;
366de0ce 7527 else
3e4093b6 7528 {
366de0ce 7529 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7530
3e4093b6 7531 if (constructor_depth > 2)
ea58ef42 7532 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7533 else
d033409e 7534 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7535 "initialization of a flexible array member");
de520661 7536
3e4093b6
RS
7537 /* We have already issued an error message for the existence
7538 of a flexible array member not at the end of the structure.
535a42b1 7539 Discard the initializer so that we do not die later. */
910ad8de 7540 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7541 constructor_type = NULL_TREE;
7542 }
3e4093b6 7543 }
de520661 7544
84937de2
MP
7545 /* Initialization with { } counts as zeroinit. */
7546 if (vec_safe_length (constructor_elements) == 0)
7547 constructor_zeroinit = 1;
7548 /* If the constructor has more than one element, it can't be { 0 }. */
7549 else if (vec_safe_length (constructor_elements) != 1)
9bac5cbb
G
7550 constructor_zeroinit = 0;
7551
7552 /* Warn when some structs are initialized with direct aggregation. */
7553 if (!implicit && found_missing_braces && warn_missing_braces
7554 && !constructor_zeroinit)
7555 {
7556 warning_init (loc, OPT_Wmissing_braces,
7557 "missing braces around initializer");
7558 }
7559
3e4093b6 7560 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7561 if (warn_missing_field_initializers
3e4093b6
RS
7562 && constructor_type
7563 && TREE_CODE (constructor_type) == RECORD_TYPE
7564 && constructor_unfilled_fields)
7565 {
7566 /* Do not warn for flexible array members or zero-length arrays. */
7567 while (constructor_unfilled_fields
3f75a254 7568 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7569 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7570 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7571
49819fef
AM
7572 if (constructor_unfilled_fields
7573 /* Do not warn if this level of the initializer uses member
7574 designators; it is likely to be deliberate. */
7575 && !constructor_designated
84937de2 7576 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7577 && !constructor_zeroinit)
3e4093b6 7578 {
32397f22
MLI
7579 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7580 "missing initializer for field %qD of %qT",
7581 constructor_unfilled_fields,
7582 constructor_type))
7583 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7584 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7585 }
7586 }
de520661 7587
3e4093b6 7588 /* Pad out the end of the structure. */
916c5919 7589 if (p->replacement_value.value)
3e4093b6
RS
7590 /* If this closes a superfluous brace pair,
7591 just pass out the element between them. */
916c5919 7592 ret = p->replacement_value;
3e4093b6
RS
7593 else if (constructor_type == 0)
7594 ;
7595 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7596 && TREE_CODE (constructor_type) != UNION_TYPE
7597 && TREE_CODE (constructor_type) != ARRAY_TYPE
7598 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7599 {
7600 /* A nonincremental scalar initializer--just return
7601 the element, after verifying there is just one. */
9771b263 7602 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7603 {
7604 if (!constructor_erroneous)
ea58ef42 7605 error_init (loc, "empty scalar initializer");
916c5919 7606 ret.value = error_mark_node;
3e4093b6 7607 }
9771b263 7608 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7609 {
ea58ef42 7610 error_init (loc, "extra elements in scalar initializer");
9771b263 7611 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7612 }
7613 else
9771b263 7614 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7615 }
7616 else
7617 {
7618 if (constructor_erroneous)
916c5919 7619 ret.value = error_mark_node;
3e4093b6
RS
7620 else
7621 {
916c5919 7622 ret.value = build_constructor (constructor_type,
4038c495 7623 constructor_elements);
3e4093b6 7624 if (constructor_constant)
51eed280 7625 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7626 if (constructor_constant && constructor_simple)
916c5919 7627 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7628 if (constructor_nonconst)
7629 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7630 }
7631 }
de520661 7632
928c19bb
JM
7633 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7634 {
7635 if (constructor_nonconst)
7636 ret.original_code = C_MAYBE_CONST_EXPR;
7637 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7638 ret.original_code = ERROR_MARK;
7639 }
7640
3e4093b6
RS
7641 constructor_type = p->type;
7642 constructor_fields = p->fields;
7643 constructor_index = p->index;
7644 constructor_max_index = p->max_index;
7645 constructor_unfilled_index = p->unfilled_index;
7646 constructor_unfilled_fields = p->unfilled_fields;
7647 constructor_bit_index = p->bit_index;
7648 constructor_elements = p->elements;
7649 constructor_constant = p->constant;
7650 constructor_simple = p->simple;
928c19bb 7651 constructor_nonconst = p->nonconst;
3e4093b6
RS
7652 constructor_erroneous = p->erroneous;
7653 constructor_incremental = p->incremental;
7654 constructor_designated = p->designated;
976d5a22 7655 designator_depth = p->designator_depth;
3e4093b6
RS
7656 constructor_pending_elts = p->pending_elts;
7657 constructor_depth = p->depth;
7658 if (!p->implicit)
7659 constructor_range_stack = p->range_stack;
7660 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7661
3e4093b6
RS
7662 constructor_stack = p->next;
7663 free (p);
b621a4dd 7664
5d5e98dc
VR
7665 if (ret.value == 0 && constructor_stack == 0)
7666 ret.value = error_mark_node;
916c5919 7667 return ret;
3e4093b6 7668}
8b6a5902 7669
3e4093b6
RS
7670/* Common handling for both array range and field name designators.
7671 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7672
3e4093b6 7673static int
ea58ef42
MP
7674set_designator (location_t loc, int array,
7675 struct obstack *braced_init_obstack)
de520661 7676{
3e4093b6
RS
7677 tree subtype;
7678 enum tree_code subcode;
de520661 7679
3e4093b6
RS
7680 /* Don't die if an entire brace-pair level is superfluous
7681 in the containing level. */
7682 if (constructor_type == 0)
7683 return 1;
de520661 7684
366de0ce
NS
7685 /* If there were errors in this designator list already, bail out
7686 silently. */
b06df647 7687 if (designator_erroneous)
3e4093b6 7688 return 1;
e28cae4f 7689
3e4093b6
RS
7690 if (!designator_depth)
7691 {
366de0ce 7692 gcc_assert (!constructor_range_stack);
de520661 7693
3e4093b6
RS
7694 /* Designator list starts at the level of closest explicit
7695 braces. */
7696 while (constructor_stack->implicit)
34cf811f 7697 process_init_element (input_location,
ea58ef42 7698 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7699 true, braced_init_obstack);
3e4093b6
RS
7700 constructor_designated = 1;
7701 return 0;
7702 }
de520661 7703
366de0ce 7704 switch (TREE_CODE (constructor_type))
3c3fa147 7705 {
366de0ce
NS
7706 case RECORD_TYPE:
7707 case UNION_TYPE:
3e4093b6
RS
7708 subtype = TREE_TYPE (constructor_fields);
7709 if (subtype != error_mark_node)
7710 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7711 break;
7712 case ARRAY_TYPE:
3e4093b6 7713 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7714 break;
7715 default:
7716 gcc_unreachable ();
de520661 7717 }
400fbf9f 7718
3e4093b6
RS
7719 subcode = TREE_CODE (subtype);
7720 if (array && subcode != ARRAY_TYPE)
7721 {
ea58ef42 7722 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
7723 return 1;
7724 }
7725 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7726 {
ea58ef42 7727 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
7728 return 1;
7729 }
d45cf215 7730
3e4093b6 7731 constructor_designated = 1;
ea58ef42 7732 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 7733 return 0;
de520661 7734}
400fbf9f 7735
3e4093b6
RS
7736/* If there are range designators in designator list, push a new designator
7737 to constructor_range_stack. RANGE_END is end of such stack range or
7738 NULL_TREE if there is no range designator at this level. */
400fbf9f 7739
3e4093b6 7740static void
a1e3b3d9 7741push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7742{
7743 struct constructor_range_stack *p;
400fbf9f 7744
a1e3b3d9
LB
7745 p = (struct constructor_range_stack *)
7746 obstack_alloc (braced_init_obstack,
7747 sizeof (struct constructor_range_stack));
3e4093b6
RS
7748 p->prev = constructor_range_stack;
7749 p->next = 0;
7750 p->fields = constructor_fields;
7751 p->range_start = constructor_index;
7752 p->index = constructor_index;
7753 p->stack = constructor_stack;
7754 p->range_end = range_end;
8b6a5902 7755 if (constructor_range_stack)
3e4093b6
RS
7756 constructor_range_stack->next = p;
7757 constructor_range_stack = p;
de520661 7758}
400fbf9f 7759
3e4093b6
RS
7760/* Within an array initializer, specify the next index to be initialized.
7761 FIRST is that index. If LAST is nonzero, then initialize a range
7762 of indices, running from FIRST through LAST. */
5a7ec9d9 7763
de520661 7764void
ea58ef42 7765set_init_index (location_t loc, tree first, tree last,
d033409e 7766 struct obstack *braced_init_obstack)
de520661 7767{
ea58ef42 7768 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 7769 return;
de520661 7770
b06df647 7771 designator_erroneous = 1;
de520661 7772
3ea8cd06
JM
7773 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7774 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7775 {
ea58ef42 7776 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
7777 return;
7778 }
7779
2b6da65c
JM
7780 if (TREE_CODE (first) != INTEGER_CST)
7781 {
7782 first = c_fully_fold (first, false, NULL);
7783 if (TREE_CODE (first) == INTEGER_CST)
d033409e 7784 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7785 "array index in initializer is not "
7786 "an integer constant expression");
7787 }
7788
7789 if (last && TREE_CODE (last) != INTEGER_CST)
7790 {
7791 last = c_fully_fold (last, false, NULL);
7792 if (TREE_CODE (last) == INTEGER_CST)
d033409e 7793 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7794 "array index in initializer is not "
7795 "an integer constant expression");
7796 }
7797
3e4093b6 7798 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 7799 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7800 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 7801 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7802 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 7803 error_init (loc, "array index in non-array initializer");
622adc7e 7804 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 7805 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
7806 else if (constructor_max_index
7807 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 7808 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 7809 else
de520661 7810 {
928c19bb
JM
7811 constant_expression_warning (first);
7812 if (last)
7813 constant_expression_warning (last);
3e4093b6 7814 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7815 if (tree_int_cst_lt (constructor_index, first))
7816 {
7817 constructor_index = copy_node (constructor_index);
7818 TREE_OVERFLOW (constructor_index) = 1;
7819 }
665f2503 7820
3e4093b6 7821 if (last)
2bede729 7822 {
3e4093b6
RS
7823 if (tree_int_cst_equal (first, last))
7824 last = 0;
7825 else if (tree_int_cst_lt (last, first))
7826 {
ea58ef42 7827 error_init (loc, "empty index range in initializer");
3e4093b6
RS
7828 last = 0;
7829 }
7830 else
7831 {
7832 last = convert (bitsizetype, last);
7833 if (constructor_max_index != 0
7834 && tree_int_cst_lt (constructor_max_index, last))
7835 {
ea58ef42
MP
7836 error_init (loc, "array index range in initializer exceeds "
7837 "array bounds");
3e4093b6
RS
7838 last = 0;
7839 }
7840 }
2bede729 7841 }
fed3cef0 7842
3e4093b6 7843 designator_depth++;
b06df647 7844 designator_erroneous = 0;
3e4093b6 7845 if (constructor_range_stack || last)
a1e3b3d9 7846 push_range_stack (last, braced_init_obstack);
de520661 7847 }
de520661 7848}
3e4093b6
RS
7849
7850/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7851
de520661 7852void
ea58ef42
MP
7853set_init_label (location_t loc, tree fieldname,
7854 struct obstack *braced_init_obstack)
de520661 7855{
0fb96aa4 7856 tree field;
94ba5069 7857
ea58ef42 7858 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
7859 return;
7860
b06df647 7861 designator_erroneous = 1;
3e4093b6
RS
7862
7863 if (TREE_CODE (constructor_type) != RECORD_TYPE
7864 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7865 {
ea58ef42 7866 error_init (loc, "field name not in record or union initializer");
3e4093b6 7867 return;
94ba5069
RS
7868 }
7869
0fb96aa4 7870 field = lookup_field (constructor_type, fieldname);
8b6a5902 7871
0fb96aa4 7872 if (field == 0)
c51a1ba9 7873 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 7874 else
0fb96aa4
JM
7875 do
7876 {
7877 constructor_fields = TREE_VALUE (field);
7878 designator_depth++;
7879 designator_erroneous = 0;
7880 if (constructor_range_stack)
7881 push_range_stack (NULL_TREE, braced_init_obstack);
7882 field = TREE_CHAIN (field);
7883 if (field)
7884 {
ea58ef42 7885 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
7886 return;
7887 }
7888 }
7889 while (field != NULL_TREE);
3e4093b6
RS
7890}
7891\f
7892/* Add a new initializer to the tree of pending initializers. PURPOSE
7893 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7894 VALUE is the value of that index or field. If ORIGTYPE is not
7895 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7896
7897 IMPLICIT is true if value comes from pop_init_level (1),
7898 the new initializer has been merged with the existing one
7899 and thus no warnings should be emitted about overriding an
7900 existing initializer. */
de520661 7901
3e4093b6 7902static void
96b40f8d
MP
7903add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7904 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
7905{
7906 struct init_node *p, **q, *r;
7907
7908 q = &constructor_pending_elts;
7909 p = 0;
7910
7911 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7912 {
3e4093b6 7913 while (*q != 0)
91fa3c30 7914 {
3e4093b6
RS
7915 p = *q;
7916 if (tree_int_cst_lt (purpose, p->purpose))
7917 q = &p->left;
7918 else if (tree_int_cst_lt (p->purpose, purpose))
7919 q = &p->right;
7920 else
7921 {
b295aee2
JJ
7922 if (!implicit)
7923 {
7924 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
7925 warning_init (loc, 0,
7926 "initialized field with side-effects "
7927 "overwritten");
b295aee2 7928 else if (warn_override_init)
96b40f8d
MP
7929 warning_init (loc, OPT_Woverride_init,
7930 "initialized field overwritten");
b295aee2 7931 }
3e4093b6 7932 p->value = value;
bbbbb16a 7933 p->origtype = origtype;
3e4093b6
RS
7934 return;
7935 }
91fa3c30 7936 }
de520661 7937 }
3e4093b6 7938 else
de520661 7939 {
3e4093b6 7940 tree bitpos;
400fbf9f 7941
3e4093b6
RS
7942 bitpos = bit_position (purpose);
7943 while (*q != NULL)
7944 {
7945 p = *q;
7946 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7947 q = &p->left;
7948 else if (p->purpose != purpose)
7949 q = &p->right;
7950 else
7951 {
b295aee2
JJ
7952 if (!implicit)
7953 {
7954 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
7955 warning_init (loc, 0,
7956 "initialized field with side-effects "
7957 "overwritten");
b295aee2 7958 else if (warn_override_init)
96b40f8d
MP
7959 warning_init (loc, OPT_Woverride_init,
7960 "initialized field overwritten");
b295aee2 7961 }
3e4093b6 7962 p->value = value;
bbbbb16a 7963 p->origtype = origtype;
3e4093b6
RS
7964 return;
7965 }
7966 }
91fa3c30 7967 }
b71c7f8a 7968
a1e3b3d9
LB
7969 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7970 sizeof (struct init_node));
3e4093b6
RS
7971 r->purpose = purpose;
7972 r->value = value;
bbbbb16a 7973 r->origtype = origtype;
8b6a5902 7974
3e4093b6
RS
7975 *q = r;
7976 r->parent = p;
7977 r->left = 0;
7978 r->right = 0;
7979 r->balance = 0;
b71c7f8a 7980
3e4093b6 7981 while (p)
de520661 7982 {
3e4093b6 7983 struct init_node *s;
665f2503 7984
3e4093b6 7985 if (r == p->left)
2bede729 7986 {
3e4093b6
RS
7987 if (p->balance == 0)
7988 p->balance = -1;
7989 else if (p->balance < 0)
7990 {
7991 if (r->balance < 0)
7992 {
7993 /* L rotation. */
7994 p->left = r->right;
7995 if (p->left)
7996 p->left->parent = p;
7997 r->right = p;
e7b6a0ee 7998
3e4093b6
RS
7999 p->balance = 0;
8000 r->balance = 0;
39bc99c2 8001
3e4093b6
RS
8002 s = p->parent;
8003 p->parent = r;
8004 r->parent = s;
8005 if (s)
8006 {
8007 if (s->left == p)
8008 s->left = r;
8009 else
8010 s->right = r;
8011 }
8012 else
8013 constructor_pending_elts = r;
8014 }
8015 else
8016 {
8017 /* LR rotation. */
8018 struct init_node *t = r->right;
e7b6a0ee 8019
3e4093b6
RS
8020 r->right = t->left;
8021 if (r->right)
8022 r->right->parent = r;
8023 t->left = r;
8024
8025 p->left = t->right;
8026 if (p->left)
8027 p->left->parent = p;
8028 t->right = p;
8029
8030 p->balance = t->balance < 0;
8031 r->balance = -(t->balance > 0);
8032 t->balance = 0;
8033
8034 s = p->parent;
8035 p->parent = t;
8036 r->parent = t;
8037 t->parent = s;
8038 if (s)
8039 {
8040 if (s->left == p)
8041 s->left = t;
8042 else
8043 s->right = t;
8044 }
8045 else
8046 constructor_pending_elts = t;
8047 }
8048 break;
8049 }
8050 else
8051 {
8052 /* p->balance == +1; growth of left side balances the node. */
8053 p->balance = 0;
8054 break;
8055 }
2bede729 8056 }
3e4093b6
RS
8057 else /* r == p->right */
8058 {
8059 if (p->balance == 0)
8060 /* Growth propagation from right side. */
8061 p->balance++;
8062 else if (p->balance > 0)
8063 {
8064 if (r->balance > 0)
8065 {
8066 /* R rotation. */
8067 p->right = r->left;
8068 if (p->right)
8069 p->right->parent = p;
8070 r->left = p;
8071
8072 p->balance = 0;
8073 r->balance = 0;
8074
8075 s = p->parent;
8076 p->parent = r;
8077 r->parent = s;
8078 if (s)
8079 {
8080 if (s->left == p)
8081 s->left = r;
8082 else
8083 s->right = r;
8084 }
8085 else
8086 constructor_pending_elts = r;
8087 }
8088 else /* r->balance == -1 */
8089 {
8090 /* RL rotation */
8091 struct init_node *t = r->left;
8092
8093 r->left = t->right;
8094 if (r->left)
8095 r->left->parent = r;
8096 t->right = r;
8097
8098 p->right = t->left;
8099 if (p->right)
8100 p->right->parent = p;
8101 t->left = p;
8102
8103 r->balance = (t->balance < 0);
8104 p->balance = -(t->balance > 0);
8105 t->balance = 0;
8106
8107 s = p->parent;
8108 p->parent = t;
8109 r->parent = t;
8110 t->parent = s;
8111 if (s)
8112 {
8113 if (s->left == p)
8114 s->left = t;
8115 else
8116 s->right = t;
8117 }
8118 else
8119 constructor_pending_elts = t;
8120 }
8121 break;
8122 }
8123 else
8124 {
8125 /* p->balance == -1; growth of right side balances the node. */
8126 p->balance = 0;
8127 break;
8128 }
8129 }
8130
8131 r = p;
8132 p = p->parent;
8133 }
8134}
8135
8136/* Build AVL tree from a sorted chain. */
8137
8138static void
a1e3b3d9 8139set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8140{
4038c495
GB
8141 unsigned HOST_WIDE_INT ix;
8142 tree index, value;
3e4093b6
RS
8143
8144 if (TREE_CODE (constructor_type) != RECORD_TYPE
8145 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8146 return;
8147
4038c495 8148 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8149 add_pending_init (input_location, index, value, NULL_TREE, true,
8150 braced_init_obstack);
9771b263 8151 constructor_elements = NULL;
3e4093b6
RS
8152 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8153 {
8154 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8155 /* Skip any nameless bit fields at the beginning. */
8156 while (constructor_unfilled_fields != 0
8157 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8158 && DECL_NAME (constructor_unfilled_fields) == 0)
8159 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8160
de520661 8161 }
3e4093b6 8162 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8163 {
3e4093b6
RS
8164 if (TYPE_DOMAIN (constructor_type))
8165 constructor_unfilled_index
8166 = convert (bitsizetype,
8167 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8168 else
8169 constructor_unfilled_index = bitsize_zero_node;
de520661 8170 }
3e4093b6 8171 constructor_incremental = 0;
de520661 8172}
400fbf9f 8173
3e4093b6 8174/* Build AVL tree from a string constant. */
de520661 8175
3e4093b6 8176static void
a1e3b3d9
LB
8177set_nonincremental_init_from_string (tree str,
8178 struct obstack * braced_init_obstack)
de520661 8179{
3e4093b6
RS
8180 tree value, purpose, type;
8181 HOST_WIDE_INT val[2];
8182 const char *p, *end;
8183 int byte, wchar_bytes, charwidth, bitpos;
de520661 8184
366de0ce 8185 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8186
c466b2cd 8187 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8188 charwidth = TYPE_PRECISION (char_type_node);
8189 type = TREE_TYPE (constructor_type);
8190 p = TREE_STRING_POINTER (str);
8191 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8192
3e4093b6 8193 for (purpose = bitsize_zero_node;
8824edff
JJ
8194 p < end
8195 && !(constructor_max_index
8196 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8197 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8198 {
3e4093b6 8199 if (wchar_bytes == 1)
ffc5c6a9 8200 {
807e902e
KZ
8201 val[0] = (unsigned char) *p++;
8202 val[1] = 0;
ffc5c6a9
RH
8203 }
8204 else
3e4093b6 8205 {
3e4093b6 8206 val[1] = 0;
807e902e 8207 val[0] = 0;
3e4093b6
RS
8208 for (byte = 0; byte < wchar_bytes; byte++)
8209 {
8210 if (BYTES_BIG_ENDIAN)
8211 bitpos = (wchar_bytes - byte - 1) * charwidth;
8212 else
8213 bitpos = byte * charwidth;
807e902e 8214 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8215 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8216 << (bitpos % HOST_BITS_PER_WIDE_INT);
8217 }
8218 }
584ef5fe 8219
8df83eae 8220 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8221 {
8222 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8223 if (bitpos < HOST_BITS_PER_WIDE_INT)
8224 {
807e902e 8225 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8226 {
807e902e
KZ
8227 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8228 val[1] = -1;
3e4093b6
RS
8229 }
8230 }
8231 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8232 {
807e902e
KZ
8233 if (val[0] < 0)
8234 val[1] = -1;
3e4093b6 8235 }
807e902e 8236 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8237 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
807e902e 8238 val[1] |= ((HOST_WIDE_INT) -1)
3e4093b6
RS
8239 << (bitpos - HOST_BITS_PER_WIDE_INT);
8240 }
ffc5c6a9 8241
807e902e
KZ
8242 value = wide_int_to_tree (type,
8243 wide_int::from_array (val, 2,
8244 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8245 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8246 braced_init_obstack);
9dfcc8db
BH
8247 }
8248
3e4093b6
RS
8249 constructor_incremental = 0;
8250}
de520661 8251
3e4093b6
RS
8252/* Return value of FIELD in pending initializer or zero if the field was
8253 not initialized yet. */
8254
8255static tree
a1e3b3d9 8256find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8257{
8258 struct init_node *p;
8259
8260 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8261 {
3e4093b6
RS
8262 if (constructor_incremental
8263 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8264 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8265
8266 p = constructor_pending_elts;
8267 while (p)
19d76e60 8268 {
3e4093b6
RS
8269 if (tree_int_cst_lt (field, p->purpose))
8270 p = p->left;
8271 else if (tree_int_cst_lt (p->purpose, field))
8272 p = p->right;
8273 else
8274 return p->value;
19d76e60 8275 }
19d76e60 8276 }
3e4093b6 8277 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8278 {
3e4093b6 8279 tree bitpos = bit_position (field);
de520661 8280
3e4093b6
RS
8281 if (constructor_incremental
8282 && (!constructor_unfilled_fields
8283 || tree_int_cst_lt (bitpos,
8284 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8285 set_nonincremental_init (braced_init_obstack);
de520661 8286
3e4093b6
RS
8287 p = constructor_pending_elts;
8288 while (p)
8289 {
8290 if (field == p->purpose)
8291 return p->value;
8292 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8293 p = p->left;
8294 else
8295 p = p->right;
8296 }
8297 }
8298 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8299 {
9771b263
DN
8300 if (!vec_safe_is_empty (constructor_elements)
8301 && (constructor_elements->last ().index == field))
8302 return constructor_elements->last ().value;
de520661 8303 }
3e4093b6 8304 return 0;
de520661
RS
8305}
8306
3e4093b6
RS
8307/* "Output" the next constructor element.
8308 At top level, really output it to assembler code now.
8309 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8310 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8311 TYPE is the data type that the containing data type wants here.
8312 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8313 If VALUE is a string constant, STRICT_STRING is true if it is
8314 unparenthesized or we should not warn here for it being parenthesized.
8315 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8316
3e4093b6
RS
8317 PENDING if non-nil means output pending elements that belong
8318 right after this element. (PENDING is normally 1;
b295aee2
JJ
8319 it is 0 while outputting pending elements, to avoid recursion.)
8320
8321 IMPLICIT is true if value comes from pop_init_level (1),
8322 the new initializer has been merged with the existing one
8323 and thus no warnings should be emitted about overriding an
8324 existing initializer. */
8b6a5902 8325
3e4093b6 8326static void
34cf811f
MP
8327output_init_element (location_t loc, tree value, tree origtype,
8328 bool strict_string, tree type, tree field, int pending,
8329 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8330{
8ce94e44 8331 tree semantic_type = NULL_TREE;
928c19bb
JM
8332 bool maybe_const = true;
8333 bool npc;
4038c495 8334
0a880880 8335 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8336 {
3e4093b6
RS
8337 constructor_erroneous = 1;
8338 return;
8b6a5902 8339 }
46bdb9cf
JM
8340 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8341 && (TREE_CODE (value) == STRING_CST
8342 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8343 && !(TREE_CODE (value) == STRING_CST
8344 && TREE_CODE (type) == ARRAY_TYPE
8345 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8346 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8347 TYPE_MAIN_VARIANT (type)))
c2255bc4 8348 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8349
3e4093b6 8350 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8351 && require_constant_value && pending)
8b6a5902 8352 {
3e4093b6
RS
8353 /* As an extension, allow initializing objects with static storage
8354 duration with compound literals (which are then treated just as
8355 the brace enclosed list they contain). */
4435bb92
MP
8356 if (flag_isoc99)
8357 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8358 "constant");
3e4093b6
RS
8359 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8360 value = DECL_INITIAL (decl);
8b6a5902
JJ
8361 }
8362
928c19bb 8363 npc = null_pointer_constant_p (value);
8ce94e44
JM
8364 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8365 {
8366 semantic_type = TREE_TYPE (value);
8367 value = TREE_OPERAND (value, 0);
8368 }
928c19bb
JM
8369 value = c_fully_fold (value, require_constant_value, &maybe_const);
8370
3e4093b6
RS
8371 if (value == error_mark_node)
8372 constructor_erroneous = 1;
8373 else if (!TREE_CONSTANT (value))
8374 constructor_constant = 0;
116df786 8375 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8376 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8377 || TREE_CODE (constructor_type) == UNION_TYPE)
8378 && DECL_C_BIT_FIELD (field)
8379 && TREE_CODE (value) != INTEGER_CST))
8380 constructor_simple = 0;
928c19bb
JM
8381 if (!maybe_const)
8382 constructor_nonconst = 1;
3e4093b6 8383
116df786 8384 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8385 {
116df786
RH
8386 if (require_constant_value)
8387 {
ea58ef42 8388 error_init (loc, "initializer element is not constant");
116df786
RH
8389 value = error_mark_node;
8390 }
8391 else if (require_constant_elements)
8337d1db 8392 pedwarn (loc, OPT_Wpedantic,
509c9d60 8393 "initializer element is not computable at load time");
8b6a5902 8394 }
928c19bb
JM
8395 else if (!maybe_const
8396 && (require_constant_value || require_constant_elements))
8337d1db 8397 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8398 "initializer element is not a constant expression");
3e4093b6 8399
81f40b79
ILT
8400 /* Issue -Wc++-compat warnings about initializing a bitfield with
8401 enum type. */
8402 if (warn_cxx_compat
8403 && field != NULL_TREE
8404 && TREE_CODE (field) == FIELD_DECL
8405 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8406 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8407 != TYPE_MAIN_VARIANT (type))
8408 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8409 {
8410 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8411 if (checktype != error_mark_node
8412 && (TYPE_MAIN_VARIANT (checktype)
8413 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8414 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8415 "enum conversion in initialization is invalid in C++");
8416 }
8417
3e4093b6
RS
8418 /* If this field is empty (and not at the end of structure),
8419 don't do anything other than checking the initializer. */
8420 if (field
8421 && (TREE_TYPE (field) == error_mark_node
8422 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8423 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8424 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8425 || DECL_CHAIN (field)))))
3e4093b6
RS
8426 return;
8427
8ce94e44
JM
8428 if (semantic_type)
8429 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8430 value = digest_init (loc, type, value, origtype, npc, strict_string,
8431 require_constant_value);
3e4093b6 8432 if (value == error_mark_node)
8b6a5902 8433 {
3e4093b6
RS
8434 constructor_erroneous = 1;
8435 return;
8b6a5902 8436 }
928c19bb
JM
8437 if (require_constant_value || require_constant_elements)
8438 constant_expression_warning (value);
8b6a5902 8439
3e4093b6
RS
8440 /* If this element doesn't come next in sequence,
8441 put it on constructor_pending_elts. */
8442 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8443 && (!constructor_incremental
8444 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8445 {
3e4093b6
RS
8446 if (constructor_incremental
8447 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8448 set_nonincremental_init (braced_init_obstack);
3e4093b6 8449
96b40f8d 8450 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8451 braced_init_obstack);
3e4093b6 8452 return;
8b6a5902 8453 }
3e4093b6
RS
8454 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8455 && (!constructor_incremental
8456 || field != constructor_unfilled_fields))
8b6a5902 8457 {
3e4093b6
RS
8458 /* We do this for records but not for unions. In a union,
8459 no matter which field is specified, it can be initialized
8460 right away since it starts at the beginning of the union. */
8461 if (constructor_incremental)
8462 {
8463 if (!constructor_unfilled_fields)
a1e3b3d9 8464 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8465 else
8466 {
8467 tree bitpos, unfillpos;
8468
8469 bitpos = bit_position (field);
8470 unfillpos = bit_position (constructor_unfilled_fields);
8471
8472 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8473 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8474 }
8475 }
8476
96b40f8d 8477 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8478 braced_init_obstack);
3e4093b6 8479 return;
8b6a5902 8480 }
3e4093b6 8481 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8482 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8483 {
b295aee2
JJ
8484 if (!implicit)
8485 {
9771b263 8486 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
96b40f8d 8487 warning_init (loc, 0,
b295aee2
JJ
8488 "initialized field with side-effects overwritten");
8489 else if (warn_override_init)
96b40f8d
MP
8490 warning_init (loc, OPT_Woverride_init,
8491 "initialized field overwritten");
b295aee2 8492 }
8b6a5902 8493
3e4093b6 8494 /* We can have just one union field set. */
9771b263 8495 constructor_elements = NULL;
3e4093b6 8496 }
8b6a5902 8497
3e4093b6
RS
8498 /* Otherwise, output this element either to
8499 constructor_elements or to the assembler file. */
8b6a5902 8500
f32682ca 8501 constructor_elt celt = {field, value};
9771b263 8502 vec_safe_push (constructor_elements, celt);
8b6a5902 8503
3e4093b6
RS
8504 /* Advance the variable that indicates sequential elements output. */
8505 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8506 constructor_unfilled_index
db3927fb
AH
8507 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8508 bitsize_one_node);
3e4093b6
RS
8509 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8510 {
8511 constructor_unfilled_fields
910ad8de 8512 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8513
3e4093b6
RS
8514 /* Skip any nameless bit fields. */
8515 while (constructor_unfilled_fields != 0
8516 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8517 && DECL_NAME (constructor_unfilled_fields) == 0)
8518 constructor_unfilled_fields =
910ad8de 8519 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8520 }
8521 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8522 constructor_unfilled_fields = 0;
de520661 8523
3e4093b6
RS
8524 /* Now output any pending elements which have become next. */
8525 if (pending)
a1e3b3d9 8526 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8527}
8b6a5902 8528
3e4093b6
RS
8529/* Output any pending elements which have become next.
8530 As we output elements, constructor_unfilled_{fields,index}
8531 advances, which may cause other elements to become next;
8532 if so, they too are output.
8b6a5902 8533
3e4093b6
RS
8534 If ALL is 0, we return when there are
8535 no more pending elements to output now.
665f2503 8536
3e4093b6
RS
8537 If ALL is 1, we output space as necessary so that
8538 we can output all the pending elements. */
3e4093b6 8539static void
a1e3b3d9 8540output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8541{
8542 struct init_node *elt = constructor_pending_elts;
8543 tree next;
de520661 8544
3e4093b6
RS
8545 retry:
8546
ba228239 8547 /* Look through the whole pending tree.
3e4093b6
RS
8548 If we find an element that should be output now,
8549 output it. Otherwise, set NEXT to the element
8550 that comes first among those still pending. */
8551
8552 next = 0;
8553 while (elt)
8554 {
8555 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8556 {
3e4093b6
RS
8557 if (tree_int_cst_equal (elt->purpose,
8558 constructor_unfilled_index))
34cf811f
MP
8559 output_init_element (input_location, elt->value, elt->origtype,
8560 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8561 constructor_unfilled_index, 0, false,
8562 braced_init_obstack);
3e4093b6
RS
8563 else if (tree_int_cst_lt (constructor_unfilled_index,
8564 elt->purpose))
8b6a5902 8565 {
3e4093b6
RS
8566 /* Advance to the next smaller node. */
8567 if (elt->left)
8568 elt = elt->left;
8569 else
8570 {
8571 /* We have reached the smallest node bigger than the
8572 current unfilled index. Fill the space first. */
8573 next = elt->purpose;
8574 break;
8575 }
8b6a5902 8576 }
ce662d4c
JJ
8577 else
8578 {
3e4093b6
RS
8579 /* Advance to the next bigger node. */
8580 if (elt->right)
8581 elt = elt->right;
8582 else
ce662d4c 8583 {
3e4093b6
RS
8584 /* We have reached the biggest node in a subtree. Find
8585 the parent of it, which is the next bigger node. */
8586 while (elt->parent && elt->parent->right == elt)
8587 elt = elt->parent;
8588 elt = elt->parent;
8589 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8590 elt->purpose))
8591 {
8592 next = elt->purpose;
8593 break;
8594 }
ce662d4c
JJ
8595 }
8596 }
8b6a5902 8597 }
3e4093b6
RS
8598 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8599 || TREE_CODE (constructor_type) == UNION_TYPE)
8600 {
8601 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8602
3e4093b6
RS
8603 /* If the current record is complete we are done. */
8604 if (constructor_unfilled_fields == 0)
8605 break;
de520661 8606
3e4093b6
RS
8607 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8608 elt_bitpos = bit_position (elt->purpose);
8609 /* We can't compare fields here because there might be empty
8610 fields in between. */
8611 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8612 {
8613 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8614 output_init_element (input_location, elt->value, elt->origtype,
8615 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8616 elt->purpose, 0, false,
8617 braced_init_obstack);
3e4093b6
RS
8618 }
8619 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8620 {
8621 /* Advance to the next smaller node. */
8622 if (elt->left)
8623 elt = elt->left;
8624 else
8625 {
8626 /* We have reached the smallest node bigger than the
8627 current unfilled field. Fill the space first. */
8628 next = elt->purpose;
8629 break;
8630 }
8631 }
8632 else
8633 {
8634 /* Advance to the next bigger node. */
8635 if (elt->right)
8636 elt = elt->right;
8637 else
8638 {
8639 /* We have reached the biggest node in a subtree. Find
8640 the parent of it, which is the next bigger node. */
8641 while (elt->parent && elt->parent->right == elt)
8642 elt = elt->parent;
8643 elt = elt->parent;
8644 if (elt
8645 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8646 bit_position (elt->purpose))))
8647 {
8648 next = elt->purpose;
8649 break;
8650 }
8651 }
8652 }
8653 }
8654 }
de520661 8655
3e4093b6
RS
8656 /* Ordinarily return, but not if we want to output all
8657 and there are elements left. */
3f75a254 8658 if (!(all && next != 0))
e5cfb88f
RK
8659 return;
8660
3e4093b6
RS
8661 /* If it's not incremental, just skip over the gap, so that after
8662 jumping to retry we will output the next successive element. */
8663 if (TREE_CODE (constructor_type) == RECORD_TYPE
8664 || TREE_CODE (constructor_type) == UNION_TYPE)
8665 constructor_unfilled_fields = next;
8666 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8667 constructor_unfilled_index = next;
de520661 8668
3e4093b6
RS
8669 /* ELT now points to the node in the pending tree with the next
8670 initializer to output. */
8671 goto retry;
de520661
RS
8672}
8673\f
3e4093b6
RS
8674/* Add one non-braced element to the current constructor level.
8675 This adjusts the current position within the constructor's type.
8676 This may also start or terminate implicit levels
8677 to handle a partly-braced initializer.
e5e809f4 8678
3e4093b6 8679 Once this has found the correct level for the new element,
b295aee2
JJ
8680 it calls output_init_element.
8681
8682 IMPLICIT is true if value comes from pop_init_level (1),
8683 the new initializer has been merged with the existing one
8684 and thus no warnings should be emitted about overriding an
8685 existing initializer. */
3e4093b6
RS
8686
8687void
34cf811f 8688process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8689 struct obstack * braced_init_obstack)
e5e809f4 8690{
916c5919
JM
8691 tree orig_value = value.value;
8692 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8693 bool strict_string = value.original_code == STRING_CST;
340baef7 8694 bool was_designated = designator_depth != 0;
e5e809f4 8695
3e4093b6 8696 designator_depth = 0;
b06df647 8697 designator_erroneous = 0;
e5e809f4 8698
9bac5cbb
G
8699 if (!implicit && value.value && !integer_zerop (value.value))
8700 constructor_zeroinit = 0;
8701
3e4093b6
RS
8702 /* Handle superfluous braces around string cst as in
8703 char x[] = {"foo"}; */
8704 if (string_flag
8705 && constructor_type
340baef7 8706 && !was_designated
3e4093b6 8707 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8708 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8709 && integer_zerop (constructor_unfilled_index))
e5e809f4 8710 {
916c5919 8711 if (constructor_stack->replacement_value.value)
ea58ef42 8712 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8713 constructor_stack->replacement_value = value;
8714 return;
e5e809f4 8715 }
8b6a5902 8716
916c5919 8717 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8718 {
ea58ef42 8719 error_init (loc, "excess elements in struct initializer");
3e4093b6 8720 return;
e5e809f4
JL
8721 }
8722
3e4093b6
RS
8723 /* Ignore elements of a brace group if it is entirely superfluous
8724 and has already been diagnosed. */
8725 if (constructor_type == 0)
8726 return;
e5e809f4 8727
976d5a22
TT
8728 if (!implicit && warn_designated_init && !was_designated
8729 && TREE_CODE (constructor_type) == RECORD_TYPE
8730 && lookup_attribute ("designated_init",
8731 TYPE_ATTRIBUTES (constructor_type)))
8732 warning_init (loc,
8733 OPT_Wdesignated_init,
8734 "positional initialization of field "
8735 "in %<struct%> declared with %<designated_init%> attribute");
8736
3e4093b6
RS
8737 /* If we've exhausted any levels that didn't have braces,
8738 pop them now. */
8739 while (constructor_stack->implicit)
8740 {
8741 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8742 || TREE_CODE (constructor_type) == UNION_TYPE)
8743 && constructor_fields == 0)
ea58ef42
MP
8744 process_init_element (loc,
8745 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8746 true, braced_init_obstack);
53650abe
AP
8747 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8748 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8749 && constructor_max_index
8750 && tree_int_cst_lt (constructor_max_index,
8751 constructor_index))
ea58ef42
MP
8752 process_init_element (loc,
8753 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8754 true, braced_init_obstack);
3e4093b6
RS
8755 else
8756 break;
8757 }
e5e809f4 8758
3e4093b6
RS
8759 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8760 if (constructor_range_stack)
e5e809f4 8761 {
3e4093b6
RS
8762 /* If value is a compound literal and we'll be just using its
8763 content, don't put it into a SAVE_EXPR. */
916c5919 8764 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
8765 || !require_constant_value
8766 || flag_isoc99)
8ce94e44
JM
8767 {
8768 tree semantic_type = NULL_TREE;
8769 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8770 {
8771 semantic_type = TREE_TYPE (value.value);
8772 value.value = TREE_OPERAND (value.value, 0);
8773 }
8774 value.value = c_save_expr (value.value);
8775 if (semantic_type)
8776 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8777 value.value);
8778 }
3e4093b6 8779 }
e5e809f4 8780
3e4093b6
RS
8781 while (1)
8782 {
8783 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8784 {
3e4093b6
RS
8785 tree fieldtype;
8786 enum tree_code fieldcode;
e5e809f4 8787
3e4093b6
RS
8788 if (constructor_fields == 0)
8789 {
ea58ef42 8790 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
8791 break;
8792 }
e5e809f4 8793
3e4093b6
RS
8794 fieldtype = TREE_TYPE (constructor_fields);
8795 if (fieldtype != error_mark_node)
8796 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8797 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8798
3e4093b6
RS
8799 /* Error for non-static initialization of a flexible array member. */
8800 if (fieldcode == ARRAY_TYPE
8801 && !require_constant_value
8802 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8803 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 8804 {
ea58ef42
MP
8805 error_init (loc, "non-static initialization of a flexible "
8806 "array member");
3e4093b6
RS
8807 break;
8808 }
e5e809f4 8809
3e4093b6 8810 /* Accept a string constant to initialize a subarray. */
916c5919 8811 if (value.value != 0
3e4093b6 8812 && fieldcode == ARRAY_TYPE
197463ae 8813 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8814 && string_flag)
916c5919 8815 value.value = orig_value;
3e4093b6
RS
8816 /* Otherwise, if we have come to a subaggregate,
8817 and we don't have an element of its type, push into it. */
0953878d 8818 else if (value.value != 0
916c5919
JM
8819 && value.value != error_mark_node
8820 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8821 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8822 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8823 {
ea58ef42 8824 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8825 continue;
8826 }
e5e809f4 8827
916c5919 8828 if (value.value)
3e4093b6
RS
8829 {
8830 push_member_name (constructor_fields);
34cf811f 8831 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8832 strict_string, fieldtype,
a1e3b3d9
LB
8833 constructor_fields, 1, implicit,
8834 braced_init_obstack);
3e4093b6 8835 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8836 }
8837 else
3e4093b6
RS
8838 /* Do the bookkeeping for an element that was
8839 directly output as a constructor. */
e5e809f4 8840 {
3e4093b6
RS
8841 /* For a record, keep track of end position of last field. */
8842 if (DECL_SIZE (constructor_fields))
c22cacf3 8843 constructor_bit_index
db3927fb
AH
8844 = size_binop_loc (input_location, PLUS_EXPR,
8845 bit_position (constructor_fields),
8846 DECL_SIZE (constructor_fields));
3e4093b6
RS
8847
8848 /* If the current field was the first one not yet written out,
8849 it isn't now, so update. */
8850 if (constructor_unfilled_fields == constructor_fields)
8851 {
910ad8de 8852 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8853 /* Skip any nameless bit fields. */
8854 while (constructor_unfilled_fields != 0
8855 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8856 && DECL_NAME (constructor_unfilled_fields) == 0)
8857 constructor_unfilled_fields =
910ad8de 8858 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8859 }
e5e809f4 8860 }
3e4093b6 8861
910ad8de 8862 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8863 /* Skip any nameless bit fields at the beginning. */
8864 while (constructor_fields != 0
8865 && DECL_C_BIT_FIELD (constructor_fields)
8866 && DECL_NAME (constructor_fields) == 0)
910ad8de 8867 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8868 }
3e4093b6 8869 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8870 {
3e4093b6
RS
8871 tree fieldtype;
8872 enum tree_code fieldcode;
e5e809f4 8873
3e4093b6
RS
8874 if (constructor_fields == 0)
8875 {
d033409e 8876 pedwarn_init (loc, 0,
509c9d60 8877 "excess elements in union initializer");
3e4093b6
RS
8878 break;
8879 }
e5e809f4 8880
3e4093b6
RS
8881 fieldtype = TREE_TYPE (constructor_fields);
8882 if (fieldtype != error_mark_node)
8883 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8884 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8885
3e4093b6
RS
8886 /* Warn that traditional C rejects initialization of unions.
8887 We skip the warning if the value is zero. This is done
8888 under the assumption that the zero initializer in user
8889 code appears conditioned on e.g. __STDC__ to avoid
8890 "missing initializer" warnings and relies on default
8891 initialization to zero in the traditional C case.
8892 We also skip the warning if the initializer is designated,
8893 again on the assumption that this must be conditional on
8894 __STDC__ anyway (and we've already complained about the
8895 member-designator already). */
8400e75e 8896 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8897 && !(value.value && (integer_zerop (value.value)
8898 || real_zerop (value.value))))
3176a0c2
DD
8899 warning (OPT_Wtraditional, "traditional C rejects initialization "
8900 "of unions");
e5e809f4 8901
3e4093b6 8902 /* Accept a string constant to initialize a subarray. */
916c5919 8903 if (value.value != 0
3e4093b6 8904 && fieldcode == ARRAY_TYPE
197463ae 8905 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8906 && string_flag)
916c5919 8907 value.value = orig_value;
3e4093b6
RS
8908 /* Otherwise, if we have come to a subaggregate,
8909 and we don't have an element of its type, push into it. */
0953878d 8910 else if (value.value != 0
916c5919
JM
8911 && value.value != error_mark_node
8912 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8913 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8914 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8915 {
ea58ef42 8916 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8917 continue;
8918 }
e5e809f4 8919
916c5919 8920 if (value.value)
3e4093b6
RS
8921 {
8922 push_member_name (constructor_fields);
34cf811f 8923 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8924 strict_string, fieldtype,
a1e3b3d9
LB
8925 constructor_fields, 1, implicit,
8926 braced_init_obstack);
3e4093b6 8927 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8928 }
8929 else
3e4093b6
RS
8930 /* Do the bookkeeping for an element that was
8931 directly output as a constructor. */
e5e809f4 8932 {
3e4093b6 8933 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8934 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8935 }
e5e809f4 8936
3e4093b6
RS
8937 constructor_fields = 0;
8938 }
8939 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8940 {
8941 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8942 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8943
3e4093b6 8944 /* Accept a string constant to initialize a subarray. */
916c5919 8945 if (value.value != 0
3e4093b6 8946 && eltcode == ARRAY_TYPE
197463ae 8947 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8948 && string_flag)
916c5919 8949 value.value = orig_value;
3e4093b6
RS
8950 /* Otherwise, if we have come to a subaggregate,
8951 and we don't have an element of its type, push into it. */
0953878d 8952 else if (value.value != 0
916c5919
JM
8953 && value.value != error_mark_node
8954 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 8955 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 8956 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 8957 {
ea58ef42 8958 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8959 continue;
8960 }
8b6a5902 8961
3e4093b6
RS
8962 if (constructor_max_index != 0
8963 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8964 || integer_all_onesp (constructor_max_index)))
8965 {
d033409e 8966 pedwarn_init (loc, 0,
509c9d60 8967 "excess elements in array initializer");
3e4093b6
RS
8968 break;
8969 }
8b6a5902 8970
3e4093b6 8971 /* Now output the actual element. */
916c5919 8972 if (value.value)
3e4093b6 8973 {
ae7e9ddd 8974 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 8975 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8976 strict_string, elttype,
a1e3b3d9
LB
8977 constructor_index, 1, implicit,
8978 braced_init_obstack);
3e4093b6
RS
8979 RESTORE_SPELLING_DEPTH (constructor_depth);
8980 }
2f6e4e97 8981
3e4093b6 8982 constructor_index
db3927fb
AH
8983 = size_binop_loc (input_location, PLUS_EXPR,
8984 constructor_index, bitsize_one_node);
8b6a5902 8985
916c5919 8986 if (!value.value)
3e4093b6
RS
8987 /* If we are doing the bookkeeping for an element that was
8988 directly output as a constructor, we must update
8989 constructor_unfilled_index. */
8990 constructor_unfilled_index = constructor_index;
8991 }
8992 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8993 {
8994 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 8995
c22cacf3
MS
8996 /* Do a basic check of initializer size. Note that vectors
8997 always have a fixed size derived from their type. */
3e4093b6
RS
8998 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8999 {
d033409e 9000 pedwarn_init (loc, 0,
509c9d60 9001 "excess elements in vector initializer");
3e4093b6
RS
9002 break;
9003 }
8b6a5902 9004
3e4093b6 9005 /* Now output the actual element. */
916c5919 9006 if (value.value)
53650abe
AP
9007 {
9008 if (TREE_CODE (value.value) == VECTOR_CST)
9009 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9010 output_init_element (loc, value.value, value.original_type,
53650abe 9011 strict_string, elttype,
a1e3b3d9
LB
9012 constructor_index, 1, implicit,
9013 braced_init_obstack);
53650abe 9014 }
8b6a5902 9015
3e4093b6 9016 constructor_index
db3927fb
AH
9017 = size_binop_loc (input_location,
9018 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9019
916c5919 9020 if (!value.value)
3e4093b6
RS
9021 /* If we are doing the bookkeeping for an element that was
9022 directly output as a constructor, we must update
9023 constructor_unfilled_index. */
9024 constructor_unfilled_index = constructor_index;
9025 }
8b6a5902 9026
3e4093b6
RS
9027 /* Handle the sole element allowed in a braced initializer
9028 for a scalar variable. */
b4519d39
SB
9029 else if (constructor_type != error_mark_node
9030 && constructor_fields == 0)
8b6a5902 9031 {
d033409e 9032 pedwarn_init (loc, 0,
509c9d60 9033 "excess elements in scalar initializer");
3e4093b6 9034 break;
8b6a5902
JJ
9035 }
9036 else
9037 {
916c5919 9038 if (value.value)
34cf811f 9039 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9040 strict_string, constructor_type,
a1e3b3d9
LB
9041 NULL_TREE, 1, implicit,
9042 braced_init_obstack);
3e4093b6 9043 constructor_fields = 0;
8b6a5902
JJ
9044 }
9045
3e4093b6
RS
9046 /* Handle range initializers either at this level or anywhere higher
9047 in the designator stack. */
9048 if (constructor_range_stack)
8b6a5902 9049 {
3e4093b6
RS
9050 struct constructor_range_stack *p, *range_stack;
9051 int finish = 0;
9052
9053 range_stack = constructor_range_stack;
9054 constructor_range_stack = 0;
9055 while (constructor_stack != range_stack->stack)
8b6a5902 9056 {
366de0ce 9057 gcc_assert (constructor_stack->implicit);
34cf811f 9058 process_init_element (loc,
ea58ef42
MP
9059 pop_init_level (loc, 1,
9060 braced_init_obstack),
a1e3b3d9 9061 true, braced_init_obstack);
8b6a5902 9062 }
3e4093b6
RS
9063 for (p = range_stack;
9064 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9065 p = p->prev)
8b6a5902 9066 {
366de0ce 9067 gcc_assert (constructor_stack->implicit);
34cf811f 9068 process_init_element (loc,
ea58ef42
MP
9069 pop_init_level (loc, 1,
9070 braced_init_obstack),
a1e3b3d9 9071 true, braced_init_obstack);
8b6a5902 9072 }
3e4093b6 9073
db3927fb
AH
9074 p->index = size_binop_loc (input_location,
9075 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9076 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9077 finish = 1;
9078
9079 while (1)
9080 {
9081 constructor_index = p->index;
9082 constructor_fields = p->fields;
9083 if (finish && p->range_end && p->index == p->range_start)
9084 {
9085 finish = 0;
9086 p->prev = 0;
9087 }
9088 p = p->next;
9089 if (!p)
9090 break;
ea58ef42 9091 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9092 p->stack = constructor_stack;
9093 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9094 p->index = p->range_start;
9095 }
9096
9097 if (!finish)
9098 constructor_range_stack = range_stack;
9099 continue;
8b6a5902
JJ
9100 }
9101
3e4093b6 9102 break;
8b6a5902
JJ
9103 }
9104
3e4093b6
RS
9105 constructor_range_stack = 0;
9106}
9107\f
9f0e2d86
ZW
9108/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9109 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9110 an ASM_EXPR node). */
3e4093b6 9111tree
9f0e2d86 9112build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9113{
6de9cd9a
DN
9114 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9115 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9116 return add_stmt (args);
8b6a5902
JJ
9117}
9118
9f0e2d86
ZW
9119/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9120 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9121 SIMPLE indicates whether there was anything at all after the
9122 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9123 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9124tree
c2255bc4 9125build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9126 tree clobbers, tree labels, bool simple)
e5e809f4 9127{
3e4093b6 9128 tree tail;
9f0e2d86 9129 tree args;
6de9cd9a
DN
9130 int i;
9131 const char *constraint;
74f0c611 9132 const char **oconstraints;
6de9cd9a 9133 bool allows_mem, allows_reg, is_inout;
74f0c611 9134 int ninputs, noutputs;
6de9cd9a
DN
9135
9136 ninputs = list_length (inputs);
9137 noutputs = list_length (outputs);
74f0c611
RH
9138 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9139
1c384bf1 9140 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9141
6de9cd9a
DN
9142 /* Remove output conversions that change the type but not the mode. */
9143 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9144 {
3e4093b6 9145 tree output = TREE_VALUE (tail);
74f0c611 9146
eadd3d0d
JJ
9147 output = c_fully_fold (output, false, NULL);
9148
74f0c611
RH
9149 /* ??? Really, this should not be here. Users should be using a
9150 proper lvalue, dammit. But there's a long history of using casts
9151 in the output operands. In cases like longlong.h, this becomes a
9152 primitive form of typechecking -- if the cast can be removed, then
9153 the output operand had a type of the proper width; otherwise we'll
9154 get an error. Gross, but ... */
3e4093b6 9155 STRIP_NOPS (output);
74f0c611 9156
7bd11157 9157 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9158 output = error_mark_node;
8b6a5902 9159
5544530a
PB
9160 if (output != error_mark_node
9161 && (TREE_READONLY (output)
9162 || TYPE_READONLY (TREE_TYPE (output))
9163 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9164 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9165 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9166 readonly_error (loc, output, lv_asm);
5544530a 9167
6de9cd9a 9168 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9169 oconstraints[i] = constraint;
9170
9171 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9172 &allows_mem, &allows_reg, &is_inout))
9173 {
9174 /* If the operand is going to end up in memory,
9175 mark it addressable. */
9176 if (!allows_reg && !c_mark_addressable (output))
9177 output = error_mark_node;
bae5cddf
JJ
9178 if (!(!allows_reg && allows_mem)
9179 && output != error_mark_node
9180 && VOID_TYPE_P (TREE_TYPE (output)))
9181 {
9182 error_at (loc, "invalid use of void expression");
9183 output = error_mark_node;
9184 }
74f0c611
RH
9185 }
9186 else
c22cacf3 9187 output = error_mark_node;
3e4093b6 9188
74f0c611 9189 TREE_VALUE (tail) = output;
8b6a5902 9190 }
3e4093b6 9191
74f0c611
RH
9192 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9193 {
9194 tree input;
9195
9196 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9197 input = TREE_VALUE (tail);
9198
74f0c611
RH
9199 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9200 oconstraints, &allows_mem, &allows_reg))
9201 {
9202 /* If the operand is going to end up in memory,
9203 mark it addressable. */
b4c33883
AP
9204 if (!allows_reg && allows_mem)
9205 {
eadd3d0d
JJ
9206 input = c_fully_fold (input, false, NULL);
9207
b4c33883
AP
9208 /* Strip the nops as we allow this case. FIXME, this really
9209 should be rejected or made deprecated. */
9210 STRIP_NOPS (input);
9211 if (!c_mark_addressable (input))
9212 input = error_mark_node;
bae5cddf 9213 }
eadd3d0d 9214 else
bae5cddf 9215 {
eadd3d0d
JJ
9216 struct c_expr expr;
9217 memset (&expr, 0, sizeof (expr));
9218 expr.value = input;
267bac10 9219 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9220 input = c_fully_fold (expr.value, false, NULL);
9221
9222 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9223 {
9224 error_at (loc, "invalid use of void expression");
9225 input = error_mark_node;
9226 }
bae5cddf 9227 }
74f0c611
RH
9228 }
9229 else
9230 input = error_mark_node;
9231
9232 TREE_VALUE (tail) = input;
9233 }
3e4093b6 9234
1c384bf1
RH
9235 /* ASMs with labels cannot have outputs. This should have been
9236 enforced by the parser. */
9237 gcc_assert (outputs == NULL || labels == NULL);
9238
9239 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9240
5544530a
PB
9241 /* asm statements without outputs, including simple ones, are treated
9242 as volatile. */
9243 ASM_INPUT_P (args) = simple;
9244 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9245
9f0e2d86 9246 return args;
e5e809f4 9247}
3e4093b6 9248\f
c2255bc4
AH
9249/* Generate a goto statement to LABEL. LOC is the location of the
9250 GOTO. */
506e2710
RH
9251
9252tree
c2255bc4 9253c_finish_goto_label (location_t loc, tree label)
506e2710 9254{
e1b7793c 9255 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9256 if (!decl)
9257 return NULL_TREE;
506e2710 9258 TREE_USED (decl) = 1;
c2255bc4
AH
9259 {
9260 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9261 SET_EXPR_LOCATION (t, loc);
9262 return add_stmt (t);
9263 }
506e2710
RH
9264}
9265
c2255bc4
AH
9266/* Generate a computed goto statement to EXPR. LOC is the location of
9267 the GOTO. */
506e2710
RH
9268
9269tree
c2255bc4 9270c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9271{
c2255bc4 9272 tree t;
c1771a20 9273 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9274 expr = c_fully_fold (expr, false, NULL);
506e2710 9275 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9276 t = build1 (GOTO_EXPR, void_type_node, expr);
9277 SET_EXPR_LOCATION (t, loc);
9278 return add_stmt (t);
506e2710
RH
9279}
9280
5088b058 9281/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9282 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9283 the location of the return statement, or the location of the expression,
9284 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9285 is the original type of RETVAL. */
de520661 9286
506e2710 9287tree
c2255bc4 9288c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9289{
0c9b182b
JJ
9290 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9291 bool no_warning = false;
928c19bb 9292 bool npc = false;
36536d79 9293 size_t rank = 0;
3e4093b6
RS
9294
9295 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9296 warning_at (loc, 0,
9297 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9298
b72271b9 9299 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9300 {
9301 /* Array notations are allowed in a return statement if it is inside a
9302 built-in array notation reduction function. */
9303 if (!find_rank (loc, retval, retval, false, &rank))
9304 return error_mark_node;
9305 if (rank >= 1)
9306 {
9307 error_at (loc, "array notation expression cannot be used as a "
9308 "return value");
9309 return error_mark_node;
9310 }
9311 }
3af9c5e9 9312 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9313 {
9314 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9315 "allowed");
9316 return error_mark_node;
9317 }
928c19bb
JM
9318 if (retval)
9319 {
8ce94e44 9320 tree semantic_type = NULL_TREE;
928c19bb 9321 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9322 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9323 {
9324 semantic_type = TREE_TYPE (retval);
9325 retval = TREE_OPERAND (retval, 0);
9326 }
928c19bb 9327 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9328 if (semantic_type)
9329 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9330 }
9331
3e4093b6 9332 if (!retval)
de520661 9333 {
3e4093b6
RS
9334 current_function_returns_null = 1;
9335 if ((warn_return_type || flag_isoc99)
9336 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9337 {
35aff4fb
MP
9338 if (flag_isoc99)
9339 pedwarn (loc, 0, "%<return%> with no value, in "
9340 "function returning non-void");
9341 else
9342 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9343 "in function returning non-void");
0c9b182b
JJ
9344 no_warning = true;
9345 }
400fbf9f 9346 }
3e4093b6 9347 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9348 {
3e4093b6 9349 current_function_returns_null = 1;
2397c575 9350 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9351 pedwarn (loc, 0,
509c9d60 9352 "%<return%> with a value, in function returning void");
b8698a0f 9353 else
c1771a20 9354 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9355 "%<return%> with expression, in function returning void");
de520661 9356 }
3e4093b6 9357 else
de520661 9358 {
68fca595
MP
9359 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9360 retval, origtype, ic_return,
c2255bc4 9361 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9362 tree res = DECL_RESULT (current_function_decl);
9363 tree inner;
9feb29df 9364 bool save;
3e4093b6
RS
9365
9366 current_function_returns_value = 1;
9367 if (t == error_mark_node)
506e2710 9368 return NULL_TREE;
3e4093b6 9369
9feb29df
JJ
9370 save = in_late_binary_op;
9371 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9372 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9373 in_late_binary_op = true;
3e4093b6 9374 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9375 in_late_binary_op = save;
3e4093b6
RS
9376
9377 /* Strip any conversions, additions, and subtractions, and see if
9378 we are returning the address of a local variable. Warn if so. */
9379 while (1)
8b6a5902 9380 {
3e4093b6 9381 switch (TREE_CODE (inner))
8b6a5902 9382 {
849421a3
JJ
9383 CASE_CONVERT:
9384 case NON_LVALUE_EXPR:
3e4093b6 9385 case PLUS_EXPR:
849421a3 9386 case POINTER_PLUS_EXPR:
3e4093b6
RS
9387 inner = TREE_OPERAND (inner, 0);
9388 continue;
9389
9390 case MINUS_EXPR:
9391 /* If the second operand of the MINUS_EXPR has a pointer
9392 type (or is converted from it), this may be valid, so
9393 don't give a warning. */
9394 {
9395 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9396
3f75a254 9397 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9398 && (CONVERT_EXPR_P (op1)
9399 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9400 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9401
3e4093b6
RS
9402 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9403 break;
8b6a5902 9404
3e4093b6
RS
9405 inner = TREE_OPERAND (inner, 0);
9406 continue;
9407 }
400fbf9f 9408
3e4093b6
RS
9409 case ADDR_EXPR:
9410 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9411
6615c446 9412 while (REFERENCE_CLASS_P (inner)
c22cacf3 9413 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 9414 inner = TREE_OPERAND (inner, 0);
8b6a5902 9415
a2f1f4c3 9416 if (DECL_P (inner)
3f75a254
JM
9417 && !DECL_EXTERNAL (inner)
9418 && !TREE_STATIC (inner)
3e4093b6 9419 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9420 {
9421 if (TREE_CODE (inner) == LABEL_DECL)
9422 warning_at (loc, OPT_Wreturn_local_addr,
9423 "function returns address of label");
9424 else
b4dfdc11
MG
9425 {
9426 warning_at (loc, OPT_Wreturn_local_addr,
9427 "function returns address of local variable");
9428 tree zero = build_zero_cst (TREE_TYPE (res));
9429 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9430 }
19fc9faa 9431 }
3e4093b6 9432 break;
8b6a5902 9433
3e4093b6
RS
9434 default:
9435 break;
9436 }
de520661 9437
3e4093b6
RS
9438 break;
9439 }
9440
53fb4de3 9441 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9442 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9443
9444 if (warn_sequence_point)
9445 verify_sequence_points (retval);
de520661 9446 }
8b6a5902 9447
c2255bc4 9448 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9449 TREE_NO_WARNING (ret_stmt) |= no_warning;
9450 return add_stmt (ret_stmt);
de520661 9451}
3e4093b6
RS
9452\f
9453struct c_switch {
604f5adf
ILT
9454 /* The SWITCH_EXPR being built. */
9455 tree switch_expr;
a6c0a76c 9456
89dbed81 9457 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9458 default conversion is applied. */
9459 tree orig_type;
9460
3e4093b6
RS
9461 /* A splay-tree mapping the low element of a case range to the high
9462 element, or NULL_TREE if there is no high element. Used to
9463 determine whether or not a new case label duplicates an old case
9464 label. We need a tree, rather than simply a hash table, because
9465 of the GNU case range extension. */
9466 splay_tree cases;
a6c0a76c 9467
e1b7793c
ILT
9468 /* The bindings at the point of the switch. This is used for
9469 warnings crossing decls when branching to a case label. */
9470 struct c_spot_bindings *bindings;
187230a7 9471
3e4093b6
RS
9472 /* The next node on the stack. */
9473 struct c_switch *next;
9474};
400fbf9f 9475
3e4093b6
RS
9476/* A stack of the currently active switch statements. The innermost
9477 switch statement is on the top of the stack. There is no need to
9478 mark the stack for garbage collection because it is only active
9479 during the processing of the body of a function, and we never
9480 collect at that point. */
de520661 9481
506e2710 9482struct c_switch *c_switch_stack;
de520661 9483
3e4093b6 9484/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9485 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef
MP
9486 SWITCH_COND_LOC is the location of the switch's condition.
9487 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
de520661 9488
3e4093b6 9489tree
c2255bc4
AH
9490c_start_case (location_t switch_loc,
9491 location_t switch_cond_loc,
fedfecef 9492 tree exp, bool explicit_cast_p)
de520661 9493{
c58e8676 9494 tree orig_type = error_mark_node;
3e4093b6 9495 struct c_switch *cs;
2f6e4e97 9496
3e4093b6 9497 if (exp != error_mark_node)
de520661 9498 {
3e4093b6
RS
9499 orig_type = TREE_TYPE (exp);
9500
c58e8676 9501 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9502 {
c58e8676
VR
9503 if (orig_type != error_mark_node)
9504 {
c2255bc4 9505 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9506 orig_type = error_mark_node;
9507 }
3e4093b6 9508 exp = integer_zero_node;
de520661 9509 }
3e4093b6 9510 else
de520661 9511 {
c58e8676 9512 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9513 tree e = exp;
9514
9515 /* Warn if the condition has boolean value. */
9516 while (TREE_CODE (e) == COMPOUND_EXPR)
9517 e = TREE_OPERAND (e, 1);
9518
9519 if ((TREE_CODE (type) == BOOLEAN_TYPE
9520 || truth_value_p (TREE_CODE (e)))
9521 /* Explicit cast to int suppresses this warning. */
9522 && !(TREE_CODE (type) == INTEGER_TYPE
9523 && explicit_cast_p))
9524 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9525 "switch condition has boolean value");
8b6a5902 9526
8400e75e 9527 if (!in_system_header_at (input_location)
3e4093b6
RS
9528 && (type == long_integer_type_node
9529 || type == long_unsigned_type_node))
c2255bc4
AH
9530 warning_at (switch_cond_loc,
9531 OPT_Wtraditional, "%<long%> switch expression not "
9532 "converted to %<int%> in ISO C");
8b6a5902 9533
928c19bb 9534 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9535 exp = default_conversion (exp);
ca085fd7
MLI
9536
9537 if (warn_sequence_point)
9538 verify_sequence_points (exp);
3e4093b6
RS
9539 }
9540 }
9541
604f5adf 9542 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9543 cs = XNEW (struct c_switch);
604f5adf 9544 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9545 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9546 cs->orig_type = orig_type;
3e4093b6 9547 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9548 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9549 cs->next = c_switch_stack;
9550 c_switch_stack = cs;
3e4093b6 9551
604f5adf 9552 return add_stmt (cs->switch_expr);
3e4093b6
RS
9553}
9554
c2255bc4 9555/* Process a case label at location LOC. */
3e4093b6
RS
9556
9557tree
c2255bc4 9558do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9559{
9560 tree label = NULL_TREE;
9561
17cede2e
JM
9562 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9563 {
9564 low_value = c_fully_fold (low_value, false, NULL);
9565 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9566 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9567 "case label is not an integer constant expression");
9568 }
9569
9570 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9571 {
9572 high_value = c_fully_fold (high_value, false, NULL);
9573 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9574 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9575 "case label is not an integer constant expression");
9576 }
9577
e1b7793c 9578 if (c_switch_stack == NULL)
187230a7
JM
9579 {
9580 if (low_value)
e1b7793c 9581 error_at (loc, "case label not within a switch statement");
187230a7 9582 else
e1b7793c
ILT
9583 error_at (loc, "%<default%> label not within a switch statement");
9584 return NULL_TREE;
187230a7 9585 }
de520661 9586
e1b7793c
ILT
9587 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9588 EXPR_LOCATION (c_switch_stack->switch_expr),
9589 loc))
9590 return NULL_TREE;
9591
9592 label = c_add_case_label (loc, c_switch_stack->cases,
9593 SWITCH_COND (c_switch_stack->switch_expr),
9594 c_switch_stack->orig_type,
9595 low_value, high_value);
9596 if (label == error_mark_node)
9597 label = NULL_TREE;
3e4093b6
RS
9598 return label;
9599}
de520661 9600
083e891e
MP
9601/* Finish the switch statement. TYPE is the original type of the
9602 controlling expression of the switch, or NULL_TREE. */
de520661 9603
3e4093b6 9604void
083e891e 9605c_finish_case (tree body, tree type)
3e4093b6 9606{
506e2710 9607 struct c_switch *cs = c_switch_stack;
fbc315db 9608 location_t switch_location;
3e4093b6 9609
604f5adf 9610 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9611
6de9cd9a 9612 /* Emit warnings as needed. */
c2255bc4 9613 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 9614 c_do_switch_warnings (cs->cases, switch_location,
083e891e 9615 type ? type : TREE_TYPE (cs->switch_expr),
fbc315db 9616 SWITCH_COND (cs->switch_expr));
6de9cd9a 9617
3e4093b6 9618 /* Pop the stack. */
506e2710 9619 c_switch_stack = cs->next;
3e4093b6 9620 splay_tree_delete (cs->cases);
e1b7793c 9621 c_release_switch_bindings (cs->bindings);
5d038c4c 9622 XDELETE (cs);
de520661 9623}
325c3691 9624\f
506e2710
RH
9625/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9626 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9627 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9628 statement, and was not surrounded with parenthesis. */
325c3691 9629
9e51cf9d 9630void
506e2710
RH
9631c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9632 tree else_block, bool nested_if)
325c3691 9633{
506e2710 9634 tree stmt;
325c3691 9635
25c22937
BI
9636 /* If the condition has array notations, then the rank of the then_block and
9637 else_block must be either 0 or be equal to the rank of the condition. If
9638 the condition does not have array notations then break them up as it is
9639 broken up in a normal expression. */
b72271b9 9640 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9641 {
9642 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9643 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9644 return;
9645 if (then_block
9646 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9647 return;
9648 if (else_block
9649 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9650 return;
9651 if (cond_rank != then_rank && then_rank != 0)
9652 {
9653 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9654 " and the then-block");
9655 return;
9656 }
9657 else if (cond_rank != else_rank && else_rank != 0)
9658 {
9659 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9660 " and the else-block");
9661 return;
9662 }
9663 }
506e2710
RH
9664 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9665 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9666 {
506e2710 9667 tree inner_if = then_block;
16865eaa 9668
61ada8ae 9669 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9670 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9671 it might not be exactly THEN_BLOCK, but should be the last
9672 non-container statement within. */
9673 while (1)
9674 switch (TREE_CODE (inner_if))
9675 {
9676 case COND_EXPR:
9677 goto found;
9678 case BIND_EXPR:
9679 inner_if = BIND_EXPR_BODY (inner_if);
9680 break;
9681 case STATEMENT_LIST:
9682 inner_if = expr_last (then_block);
9683 break;
9684 case TRY_FINALLY_EXPR:
9685 case TRY_CATCH_EXPR:
9686 inner_if = TREE_OPERAND (inner_if, 0);
9687 break;
9688 default:
366de0ce 9689 gcc_unreachable ();
506e2710
RH
9690 }
9691 found:
16865eaa 9692
506e2710 9693 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9694 warning_at (if_locus, OPT_Wparentheses,
9695 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9696 }
16865eaa 9697
2214de30 9698 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9699 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9700 add_stmt (stmt);
325c3691
RH
9701}
9702
506e2710
RH
9703/* Emit a general-purpose loop construct. START_LOCUS is the location of
9704 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9705 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9706 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9707 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9708
9709void
506e2710
RH
9710c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9711 tree blab, tree clab, bool cond_is_first)
325c3691 9712{
506e2710
RH
9713 tree entry = NULL, exit = NULL, t;
9714
e5e44252
AK
9715 /* In theory could forbid cilk spawn for loop increment expression,
9716 but it should work just fine. */
36536d79 9717
28af952a
RS
9718 /* If the condition is zero don't generate a loop construct. */
9719 if (cond && integer_zerop (cond))
9720 {
9721 if (cond_is_first)
9722 {
9723 t = build_and_jump (&blab);
9724 SET_EXPR_LOCATION (t, start_locus);
9725 add_stmt (t);
9726 }
9727 }
9728 else
506e2710
RH
9729 {
9730 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9731
506e2710 9732 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9733 out of the loop, or to the top of it. If there's no exit condition,
9734 then we just build a jump back to the top. */
506e2710 9735 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9736
28af952a 9737 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9738 {
9739 /* Canonicalize the loop condition to the end. This means
9740 generating a branch to the loop condition. Reuse the
9741 continue label, if possible. */
9742 if (cond_is_first)
9743 {
9744 if (incr || !clab)
9745 {
9746 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9747 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9748 }
9749 else
9750 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9751 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9752 add_stmt (t);
9753 }
9754
506e2710 9755 t = build_and_jump (&blab);
506e2710 9756 if (cond_is_first)
db3927fb
AH
9757 exit = fold_build3_loc (start_locus,
9758 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9759 else
db3927fb
AH
9760 exit = fold_build3_loc (input_location,
9761 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9762 }
9763
506e2710
RH
9764 add_stmt (top);
9765 }
c22cacf3 9766
506e2710
RH
9767 if (body)
9768 add_stmt (body);
9769 if (clab)
9770 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9771 if (incr)
9772 add_stmt (incr);
9773 if (entry)
9774 add_stmt (entry);
9775 if (exit)
9776 add_stmt (exit);
9777 if (blab)
9778 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9779}
325c3691
RH
9780
9781tree
c2255bc4 9782c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9783{
089efaa4 9784 bool skip;
506e2710 9785 tree label = *label_p;
325c3691 9786
089efaa4
ILT
9787 /* In switch statements break is sometimes stylistically used after
9788 a return statement. This can lead to spurious warnings about
9789 control reaching the end of a non-void function when it is
9790 inlined. Note that we are calling block_may_fallthru with
9791 language specific tree nodes; this works because
9792 block_may_fallthru returns true when given something it does not
9793 understand. */
9794 skip = !block_may_fallthru (cur_stmt_list);
9795
506e2710 9796 if (!label)
089efaa4
ILT
9797 {
9798 if (!skip)
c2255bc4 9799 *label_p = label = create_artificial_label (loc);
089efaa4 9800 }
953ff289
DN
9801 else if (TREE_CODE (label) == LABEL_DECL)
9802 ;
9803 else switch (TREE_INT_CST_LOW (label))
506e2710 9804 {
953ff289 9805 case 0:
506e2710 9806 if (is_break)
c2255bc4 9807 error_at (loc, "break statement not within loop or switch");
506e2710 9808 else
c2255bc4 9809 error_at (loc, "continue statement not within a loop");
506e2710 9810 return NULL_TREE;
953ff289
DN
9811
9812 case 1:
9813 gcc_assert (is_break);
c2255bc4 9814 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9815 return NULL_TREE;
9816
c02065fc
AH
9817 case 2:
9818 if (is_break)
9819 error ("break statement within %<#pragma simd%> loop body");
9820 else
9821 error ("continue statement within %<#pragma simd%> loop body");
9822 return NULL_TREE;
9823
953ff289
DN
9824 default:
9825 gcc_unreachable ();
506e2710 9826 }
325c3691 9827
089efaa4
ILT
9828 if (skip)
9829 return NULL_TREE;
9830
2e28e797
JH
9831 if (!is_break)
9832 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9833
53fb4de3 9834 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9835}
9836
506e2710 9837/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9838
9839static void
c2255bc4 9840emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9841{
e6b5a630
RH
9842 if (expr == error_mark_node)
9843 ;
9844 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9845 {
9846 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9847 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9848 }
789eadcd
MP
9849 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9850 {
9851 tree r = expr;
9852 location_t cloc = loc;
9853 while (TREE_CODE (r) == COMPOUND_EXPR)
9854 {
9855 if (EXPR_HAS_LOCATION (r))
9856 cloc = EXPR_LOCATION (r);
9857 r = TREE_OPERAND (r, 1);
9858 }
9859 if (!TREE_SIDE_EFFECTS (r)
9860 && !VOID_TYPE_P (TREE_TYPE (r))
9861 && !CONVERT_EXPR_P (r)
cc28fc7f 9862 && !TREE_NO_WARNING (r)
789eadcd
MP
9863 && !TREE_NO_WARNING (expr))
9864 warning_at (cloc, OPT_Wunused_value,
9865 "right-hand operand of comma expression has no effect");
9866 }
27f33b15 9867 else
c2255bc4 9868 warn_if_unused_value (expr, loc);
3a5b9284
RH
9869}
9870
506e2710 9871/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9872 diagnostics, but do not call ADD_STMT. LOC is the location of the
9873 statement. */
3a5b9284 9874
506e2710 9875tree
c2255bc4 9876c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9877{
056928b2
JJ
9878 tree exprv;
9879
3a5b9284 9880 if (!expr)
506e2710 9881 return NULL_TREE;
3a5b9284 9882
928c19bb
JM
9883 expr = c_fully_fold (expr, false, NULL);
9884
3a5b9284
RH
9885 if (warn_sequence_point)
9886 verify_sequence_points (expr);
9887
9888 if (TREE_TYPE (expr) != error_mark_node
9889 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9890 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9891 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9892
9893 /* If we're not processing a statement expression, warn about unused values.
9894 Warnings for statement expressions will be emitted later, once we figure
9895 out which is the result. */
9896 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9897 && warn_unused_value)
c2255bc4 9898 emit_side_effect_warnings (loc, expr);
3a5b9284 9899
056928b2
JJ
9900 exprv = expr;
9901 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9902 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9903 while (CONVERT_EXPR_P (exprv))
9904 exprv = TREE_OPERAND (exprv, 0);
9905 if (DECL_P (exprv)
9906 || handled_component_p (exprv)
9907 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9908 mark_exp_read (exprv);
fa8351f8 9909
3a5b9284
RH
9910 /* If the expression is not of a type to which we cannot assign a line
9911 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9912 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9913 {
9914 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9915 SET_EXPR_LOCATION (expr, loc);
9916 }
506e2710
RH
9917
9918 return expr;
9919}
9920
c2255bc4
AH
9921/* Emit an expression as a statement. LOC is the location of the
9922 expression. */
506e2710
RH
9923
9924tree
c2255bc4 9925c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9926{
9927 if (expr)
c2255bc4 9928 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9929 else
9930 return NULL;
3a5b9284
RH
9931}
9932
9933/* Do the opposite and emit a statement as an expression. To begin,
9934 create a new binding level and return it. */
325c3691
RH
9935
9936tree
9937c_begin_stmt_expr (void)
9938{
9939 tree ret;
9940
9941 /* We must force a BLOCK for this level so that, if it is not expanded
9942 later, there is a way to turn off the entire subtree of blocks that
9943 are contained in it. */
9944 keep_next_level ();
9945 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
9946
9947 c_bindings_start_stmt_expr (c_switch_stack == NULL
9948 ? NULL
9949 : c_switch_stack->bindings);
325c3691
RH
9950
9951 /* Mark the current statement list as belonging to a statement list. */
9952 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9953
9954 return ret;
9955}
9956
c2255bc4
AH
9957/* LOC is the location of the compound statement to which this body
9958 belongs. */
9959
325c3691 9960tree
c2255bc4 9961c_finish_stmt_expr (location_t loc, tree body)
325c3691 9962{
3a5b9284 9963 tree last, type, tmp, val;
325c3691
RH
9964 tree *last_p;
9965
c2255bc4 9966 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
9967
9968 c_bindings_end_stmt_expr (c_switch_stack == NULL
9969 ? NULL
9970 : c_switch_stack->bindings);
325c3691 9971
3a5b9284
RH
9972 /* Locate the last statement in BODY. See c_end_compound_stmt
9973 about always returning a BIND_EXPR. */
9974 last_p = &BIND_EXPR_BODY (body);
9975 last = BIND_EXPR_BODY (body);
9976
9977 continue_searching:
325c3691
RH
9978 if (TREE_CODE (last) == STATEMENT_LIST)
9979 {
3a5b9284
RH
9980 tree_stmt_iterator i;
9981
9982 /* This can happen with degenerate cases like ({ }). No value. */
9983 if (!TREE_SIDE_EFFECTS (last))
9984 return body;
9985
9986 /* If we're supposed to generate side effects warnings, process
9987 all of the statements except the last. */
27f33b15 9988 if (warn_unused_value)
325c3691 9989 {
3a5b9284 9990 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
9991 {
9992 location_t tloc;
9993 tree t = tsi_stmt (i);
9994
9995 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9996 emit_side_effect_warnings (tloc, t);
9997 }
325c3691
RH
9998 }
9999 else
3a5b9284
RH
10000 i = tsi_last (last);
10001 last_p = tsi_stmt_ptr (i);
10002 last = *last_p;
325c3691
RH
10003 }
10004
3a5b9284
RH
10005 /* If the end of the list is exception related, then the list was split
10006 by a call to push_cleanup. Continue searching. */
10007 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10008 || TREE_CODE (last) == TRY_CATCH_EXPR)
10009 {
10010 last_p = &TREE_OPERAND (last, 0);
10011 last = *last_p;
10012 goto continue_searching;
10013 }
10014
26d8af35
JM
10015 if (last == error_mark_node)
10016 return last;
10017
3a5b9284
RH
10018 /* In the case that the BIND_EXPR is not necessary, return the
10019 expression out from inside it. */
26d8af35
JM
10020 if (last == BIND_EXPR_BODY (body)
10021 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10022 {
928c19bb
JM
10023 /* Even if this looks constant, do not allow it in a constant
10024 expression. */
e5a94231 10025 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10026 /* Do not warn if the return value of a statement expression is
10027 unused. */
928c19bb 10028 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10029 return last;
10030 }
325c3691
RH
10031
10032 /* Extract the type of said expression. */
10033 type = TREE_TYPE (last);
325c3691 10034
3a5b9284
RH
10035 /* If we're not returning a value at all, then the BIND_EXPR that
10036 we already have is a fine expression to return. */
10037 if (!type || VOID_TYPE_P (type))
10038 return body;
10039
10040 /* Now that we've located the expression containing the value, it seems
10041 silly to make voidify_wrapper_expr repeat the process. Create a
10042 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10043 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10044
10045 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10046 tree_expr_nonnegative_p giving up immediately. */
10047 val = last;
10048 if (TREE_CODE (val) == NOP_EXPR
10049 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10050 val = TREE_OPERAND (val, 0);
10051
53fb4de3 10052 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10053 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10054
c2255bc4
AH
10055 {
10056 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10057 SET_EXPR_LOCATION (t, loc);
10058 return t;
10059 }
325c3691
RH
10060}
10061\f
10062/* Begin and end compound statements. This is as simple as pushing
10063 and popping new statement lists from the tree. */
10064
10065tree
10066c_begin_compound_stmt (bool do_scope)
10067{
10068 tree stmt = push_stmt_list ();
10069 if (do_scope)
4dfa0342 10070 push_scope ();
325c3691
RH
10071 return stmt;
10072}
10073
c2255bc4
AH
10074/* End a compound statement. STMT is the statement. LOC is the
10075 location of the compound statement-- this is usually the location
10076 of the opening brace. */
10077
325c3691 10078tree
c2255bc4 10079c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10080{
10081 tree block = NULL;
10082
10083 if (do_scope)
10084 {
10085 if (c_dialect_objc ())
10086 objc_clear_super_receiver ();
10087 block = pop_scope ();
10088 }
10089
10090 stmt = pop_stmt_list (stmt);
c2255bc4 10091 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10092
10093 /* If this compound statement is nested immediately inside a statement
10094 expression, then force a BIND_EXPR to be created. Otherwise we'll
10095 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10096 STATEMENT_LISTs merge, and thus we can lose track of what statement
10097 was really last. */
38e01f9e 10098 if (building_stmt_list_p ()
325c3691
RH
10099 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10100 && TREE_CODE (stmt) != BIND_EXPR)
10101 {
53fb4de3 10102 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10103 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10104 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10105 }
10106
10107 return stmt;
10108}
5a508662
RH
10109
10110/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10111 when the current scope is exited. EH_ONLY is true when this is not
10112 meant to apply to normal control flow transfer. */
10113
10114void
c2255bc4 10115push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10116{
3a5b9284
RH
10117 enum tree_code code;
10118 tree stmt, list;
10119 bool stmt_expr;
10120
10121 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10122 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10123 add_stmt (stmt);
3a5b9284
RH
10124 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10125 list = push_stmt_list ();
10126 TREE_OPERAND (stmt, 0) = list;
10127 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10128}
325c3691 10129\f
3e4093b6
RS
10130/* Build a binary-operation expression without default conversions.
10131 CODE is the kind of expression to build.
ba47d38d 10132 LOCATION is the operator's location.
3e4093b6
RS
10133 This function differs from `build' in several ways:
10134 the data type of the result is computed and recorded in it,
10135 warnings are generated if arg data types are invalid,
10136 special handling for addition and subtraction of pointers is known,
10137 and some optimization is done (operations on narrow ints
10138 are done in the narrower type when that gives the same result).
10139 Constant folding is also done before the result is returned.
de520661 10140
3e4093b6
RS
10141 Note that the operands will never have enumeral types, or function
10142 or array types, because either they will have the default conversions
10143 performed or they have both just been converted to some other type in which
10144 the arithmetic is to be done. */
10145
10146tree
ba47d38d
AH
10147build_binary_op (location_t location, enum tree_code code,
10148 tree orig_op0, tree orig_op1, int convert_p)
de520661 10149{
8ce94e44
JM
10150 tree type0, type1, orig_type0, orig_type1;
10151 tree eptype;
3e4093b6
RS
10152 enum tree_code code0, code1;
10153 tree op0, op1;
c9f9eb5d 10154 tree ret = error_mark_node;
4de67c26 10155 const char *invalid_op_diag;
4d84fe7c 10156 bool op0_int_operands, op1_int_operands;
928c19bb 10157 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10158
3e4093b6
RS
10159 /* Expression code to give to the expression when it is built.
10160 Normally this is CODE, which is what the caller asked for,
10161 but in some special cases we change it. */
10162 enum tree_code resultcode = code;
8b6a5902 10163
3e4093b6
RS
10164 /* Data type in which the computation is to be performed.
10165 In the simplest cases this is the common type of the arguments. */
10166 tree result_type = NULL;
10167
8ce94e44
JM
10168 /* When the computation is in excess precision, the type of the
10169 final EXCESS_PRECISION_EXPR. */
2d2e923f 10170 tree semantic_result_type = NULL;
8ce94e44 10171
3e4093b6
RS
10172 /* Nonzero means operands have already been type-converted
10173 in whatever way is necessary.
10174 Zero means they need to be converted to RESULT_TYPE. */
10175 int converted = 0;
10176
10177 /* Nonzero means create the expression with this type, rather than
10178 RESULT_TYPE. */
10179 tree build_type = 0;
10180
10181 /* Nonzero means after finally constructing the expression
10182 convert it to this type. */
10183 tree final_type = 0;
10184
10185 /* Nonzero if this is an operation like MIN or MAX which can
10186 safely be computed in short if both args are promoted shorts.
10187 Also implies COMMON.
10188 -1 indicates a bitwise operation; this makes a difference
10189 in the exact conditions for when it is safe to do the operation
10190 in a narrower mode. */
10191 int shorten = 0;
10192
10193 /* Nonzero if this is a comparison operation;
10194 if both args are promoted shorts, compare the original shorts.
10195 Also implies COMMON. */
10196 int short_compare = 0;
10197
10198 /* Nonzero if this is a right-shift operation, which can be computed on the
10199 original short and then promoted if the operand is a promoted short. */
10200 int short_shift = 0;
10201
10202 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10203 int common = 0;
10204
58393038
ZL
10205 /* True means types are compatible as far as ObjC is concerned. */
10206 bool objc_ok;
10207
8ce94e44
JM
10208 /* True means this is an arithmetic operation that may need excess
10209 precision. */
10210 bool may_need_excess_precision;
10211
180f8dbb
JM
10212 /* True means this is a boolean operation that converts both its
10213 operands to truth-values. */
10214 bool boolean_op = false;
10215
de5a5fa1
MP
10216 /* Remember whether we're doing / or %. */
10217 bool doing_div_or_mod = false;
10218
10219 /* Remember whether we're doing << or >>. */
10220 bool doing_shift = false;
10221
10222 /* Tree holding instrumentation expression. */
10223 tree instrument_expr = NULL;
10224
ba47d38d
AH
10225 if (location == UNKNOWN_LOCATION)
10226 location = input_location;
10227
4d84fe7c
JM
10228 op0 = orig_op0;
10229 op1 = orig_op1;
10230
10231 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10232 if (op0_int_operands)
10233 op0 = remove_c_maybe_const_expr (op0);
10234 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10235 if (op1_int_operands)
10236 op1 = remove_c_maybe_const_expr (op1);
10237 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10238 if (int_operands)
10239 {
10240 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10241 && TREE_CODE (orig_op1) == INTEGER_CST);
10242 int_const = (int_const_or_overflow
10243 && !TREE_OVERFLOW (orig_op0)
10244 && !TREE_OVERFLOW (orig_op1));
10245 }
10246 else
10247 int_const = int_const_or_overflow = false;
10248
0e3a99ae 10249 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
10250 if (convert_p
10251 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 10252 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 10253 {
4d84fe7c
JM
10254 op0 = default_conversion (op0);
10255 op1 = default_conversion (op1);
790e9490
RS
10256 }
10257
36536d79
BI
10258 /* When Cilk Plus is enabled and there are array notations inside op0, then
10259 we check to see if there are builtin array notation functions. If
10260 so, then we take on the type of the array notation inside it. */
b72271b9 10261 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10262 orig_type0 = type0 = find_correct_array_notation_type (op0);
10263 else
10264 orig_type0 = type0 = TREE_TYPE (op0);
10265
b72271b9 10266 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10267 orig_type1 = type1 = find_correct_array_notation_type (op1);
10268 else
10269 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10270
3e4093b6
RS
10271 /* The expression codes of the data types of the arguments tell us
10272 whether the arguments are integers, floating, pointers, etc. */
10273 code0 = TREE_CODE (type0);
10274 code1 = TREE_CODE (type1);
10275
10276 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10277 STRIP_TYPE_NOPS (op0);
10278 STRIP_TYPE_NOPS (op1);
10279
10280 /* If an error was already reported for one of the arguments,
10281 avoid reporting another error. */
10282
10283 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10284 return error_mark_node;
10285
4de67c26
JM
10286 if ((invalid_op_diag
10287 = targetm.invalid_binary_op (code, type0, type1)))
10288 {
ba47d38d 10289 error_at (location, invalid_op_diag);
4de67c26
JM
10290 return error_mark_node;
10291 }
10292
8ce94e44
JM
10293 switch (code)
10294 {
10295 case PLUS_EXPR:
10296 case MINUS_EXPR:
10297 case MULT_EXPR:
10298 case TRUNC_DIV_EXPR:
10299 case CEIL_DIV_EXPR:
10300 case FLOOR_DIV_EXPR:
10301 case ROUND_DIV_EXPR:
10302 case EXACT_DIV_EXPR:
10303 may_need_excess_precision = true;
10304 break;
10305 default:
10306 may_need_excess_precision = false;
10307 break;
10308 }
10309 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10310 {
10311 op0 = TREE_OPERAND (op0, 0);
10312 type0 = TREE_TYPE (op0);
10313 }
10314 else if (may_need_excess_precision
10315 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10316 {
10317 type0 = eptype;
10318 op0 = convert (eptype, op0);
10319 }
10320 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10321 {
10322 op1 = TREE_OPERAND (op1, 0);
10323 type1 = TREE_TYPE (op1);
10324 }
10325 else if (may_need_excess_precision
10326 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10327 {
10328 type1 = eptype;
10329 op1 = convert (eptype, op1);
10330 }
10331
58393038
ZL
10332 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10333
0e3a99ae
AS
10334 /* In case when one of the operands of the binary operation is
10335 a vector and another is a scalar -- convert scalar to vector. */
10336 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10337 {
a212e43f
MG
10338 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10339 true);
f90e8e2e 10340
0e3a99ae
AS
10341 switch (convert_flag)
10342 {
10343 case stv_error:
10344 return error_mark_node;
10345 case stv_firstarg:
10346 {
10347 bool maybe_const = true;
10348 tree sc;
10349 sc = c_fully_fold (op0, false, &maybe_const);
10350 sc = save_expr (sc);
10351 sc = convert (TREE_TYPE (type1), sc);
10352 op0 = build_vector_from_val (type1, sc);
10353 if (!maybe_const)
10354 op0 = c_wrap_maybe_const (op0, true);
10355 orig_type0 = type0 = TREE_TYPE (op0);
10356 code0 = TREE_CODE (type0);
10357 converted = 1;
10358 break;
10359 }
10360 case stv_secondarg:
10361 {
10362 bool maybe_const = true;
10363 tree sc;
10364 sc = c_fully_fold (op1, false, &maybe_const);
10365 sc = save_expr (sc);
10366 sc = convert (TREE_TYPE (type0), sc);
10367 op1 = build_vector_from_val (type0, sc);
10368 if (!maybe_const)
54b9f838 10369 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10370 orig_type1 = type1 = TREE_TYPE (op1);
10371 code1 = TREE_CODE (type1);
10372 converted = 1;
10373 break;
10374 }
10375 default:
10376 break;
10377 }
10378 }
10379
3e4093b6 10380 switch (code)
de520661 10381 {
3e4093b6
RS
10382 case PLUS_EXPR:
10383 /* Handle the pointer + int case. */
10384 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10385 {
db3927fb 10386 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10387 goto return_build_binary_op;
10388 }
3e4093b6 10389 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10390 {
db3927fb 10391 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10392 goto return_build_binary_op;
10393 }
fe67cf58 10394 else
3e4093b6
RS
10395 common = 1;
10396 break;
400fbf9f 10397
3e4093b6
RS
10398 case MINUS_EXPR:
10399 /* Subtraction of two similar pointers.
10400 We must subtract them as integers, then divide by object size. */
10401 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10402 && comp_target_types (location, type0, type1))
c9f9eb5d 10403 {
db3927fb 10404 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10405 goto return_build_binary_op;
10406 }
3e4093b6
RS
10407 /* Handle pointer minus int. Just like pointer plus int. */
10408 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10409 {
db3927fb 10410 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10411 goto return_build_binary_op;
10412 }
3e4093b6
RS
10413 else
10414 common = 1;
10415 break;
8b6a5902 10416
3e4093b6
RS
10417 case MULT_EXPR:
10418 common = 1;
10419 break;
10420
10421 case TRUNC_DIV_EXPR:
10422 case CEIL_DIV_EXPR:
10423 case FLOOR_DIV_EXPR:
10424 case ROUND_DIV_EXPR:
10425 case EXACT_DIV_EXPR:
de5a5fa1 10426 doing_div_or_mod = true;
c9f9eb5d 10427 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10428
10429 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10430 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10431 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10432 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10433 || code1 == FIXED_POINT_TYPE
3e4093b6 10434 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10435 {
5bed876a
AH
10436 enum tree_code tcode0 = code0, tcode1 = code1;
10437
3a021db2 10438 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10439 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10440 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10441 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10442
ab22c1fa
CF
10443 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10444 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10445 resultcode = RDIV_EXPR;
10446 else
10447 /* Although it would be tempting to shorten always here, that
10448 loses on some targets, since the modulo instruction is
10449 undefined if the quotient can't be represented in the
10450 computation mode. We shorten only if unsigned or if
10451 dividing by something we know != -1. */
8df83eae 10452 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10453 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10454 && !integer_all_onesp (op1)));
3e4093b6
RS
10455 common = 1;
10456 }
10457 break;
de520661 10458
3e4093b6 10459 case BIT_AND_EXPR:
3e4093b6
RS
10460 case BIT_IOR_EXPR:
10461 case BIT_XOR_EXPR:
10462 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10463 shorten = -1;
9ef0c8d9
AP
10464 /* Allow vector types which are not floating point types. */
10465 else if (code0 == VECTOR_TYPE
10466 && code1 == VECTOR_TYPE
10467 && !VECTOR_FLOAT_TYPE_P (type0)
10468 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10469 common = 1;
10470 break;
10471
10472 case TRUNC_MOD_EXPR:
10473 case FLOOR_MOD_EXPR:
de5a5fa1 10474 doing_div_or_mod = true;
c9f9eb5d 10475 warn_for_div_by_zero (location, op1);
de520661 10476
5cfd5d9b
AP
10477 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10478 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10479 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10480 common = 1;
10481 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10482 {
10483 /* Although it would be tempting to shorten always here, that loses
10484 on some targets, since the modulo instruction is undefined if the
10485 quotient can't be represented in the computation mode. We shorten
10486 only if unsigned or if dividing by something we know != -1. */
8df83eae 10487 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10488 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10489 && !integer_all_onesp (op1)));
3e4093b6
RS
10490 common = 1;
10491 }
10492 break;
de520661 10493
3e4093b6
RS
10494 case TRUTH_ANDIF_EXPR:
10495 case TRUTH_ORIF_EXPR:
10496 case TRUTH_AND_EXPR:
10497 case TRUTH_OR_EXPR:
10498 case TRUTH_XOR_EXPR:
10499 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10500 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10501 || code0 == FIXED_POINT_TYPE)
3e4093b6 10502 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10503 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10504 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10505 {
10506 /* Result of these operations is always an int,
10507 but that does not mean the operands should be
10508 converted to ints! */
10509 result_type = integer_type_node;
a27d595d
JM
10510 if (op0_int_operands)
10511 {
10512 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10513 op0 = remove_c_maybe_const_expr (op0);
10514 }
10515 else
10516 op0 = c_objc_common_truthvalue_conversion (location, op0);
10517 if (op1_int_operands)
10518 {
10519 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10520 op1 = remove_c_maybe_const_expr (op1);
10521 }
10522 else
10523 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10524 converted = 1;
180f8dbb 10525 boolean_op = true;
3e4093b6 10526 }
928c19bb
JM
10527 if (code == TRUTH_ANDIF_EXPR)
10528 {
10529 int_const_or_overflow = (int_operands
10530 && TREE_CODE (orig_op0) == INTEGER_CST
10531 && (op0 == truthvalue_false_node
10532 || TREE_CODE (orig_op1) == INTEGER_CST));
10533 int_const = (int_const_or_overflow
10534 && !TREE_OVERFLOW (orig_op0)
10535 && (op0 == truthvalue_false_node
10536 || !TREE_OVERFLOW (orig_op1)));
10537 }
10538 else if (code == TRUTH_ORIF_EXPR)
10539 {
10540 int_const_or_overflow = (int_operands
10541 && TREE_CODE (orig_op0) == INTEGER_CST
10542 && (op0 == truthvalue_true_node
10543 || TREE_CODE (orig_op1) == INTEGER_CST));
10544 int_const = (int_const_or_overflow
10545 && !TREE_OVERFLOW (orig_op0)
10546 && (op0 == truthvalue_true_node
10547 || !TREE_OVERFLOW (orig_op1)));
10548 }
3e4093b6 10549 break;
eba80994 10550
3e4093b6
RS
10551 /* Shift operations: result has same type as first operand;
10552 always convert second operand to int.
10553 Also set SHORT_SHIFT if shifting rightward. */
de520661 10554
3e4093b6 10555 case RSHIFT_EXPR:
f87bd04b
AS
10556 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10557 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10558 {
10559 result_type = type0;
10560 converted = 1;
10561 }
10562 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10563 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10564 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10565 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10566 {
10567 result_type = type0;
10568 converted = 1;
10569 }
10570 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10571 && code1 == INTEGER_TYPE)
3e4093b6 10572 {
de5a5fa1 10573 doing_shift = true;
928c19bb 10574 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10575 {
3e4093b6 10576 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10577 {
10578 int_const = false;
7d882b83 10579 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10580 warning_at (location, OPT_Wshift_count_negative,
10581 "right shift count is negative");
928c19bb 10582 }
3e4093b6 10583 else
bbb818c6 10584 {
3f75a254 10585 if (!integer_zerop (op1))
3e4093b6
RS
10586 short_shift = 1;
10587
10588 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10589 {
10590 int_const = false;
7d882b83 10591 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10592 warning_at (location, OPT_Wshift_count_overflow,
10593 "right shift count >= width of type");
928c19bb 10594 }
bbb818c6 10595 }
b62acd60 10596 }
de520661 10597
3e4093b6
RS
10598 /* Use the type of the value to be shifted. */
10599 result_type = type0;
3e4093b6
RS
10600 /* Avoid converting op1 to result_type later. */
10601 converted = 1;
400fbf9f 10602 }
3e4093b6 10603 break;
253b6b82 10604
3e4093b6 10605 case LSHIFT_EXPR:
f87bd04b
AS
10606 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10607 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10608 {
10609 result_type = type0;
10610 converted = 1;
10611 }
10612 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10613 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10614 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10615 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10616 {
10617 result_type = type0;
10618 converted = 1;
10619 }
10620 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10621 && code1 == INTEGER_TYPE)
3e4093b6 10622 {
de5a5fa1 10623 doing_shift = true;
928c19bb 10624 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10625 {
3e4093b6 10626 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10627 {
10628 int_const = false;
7d882b83 10629 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10630 warning_at (location, OPT_Wshift_count_negative,
10631 "left shift count is negative");
928c19bb 10632 }
de520661 10633
3e4093b6 10634 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10635 {
10636 int_const = false;
7d882b83 10637 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10638 warning_at (location, OPT_Wshift_count_overflow,
10639 "left shift count >= width of type");
928c19bb 10640 }
94ba5069 10641 }
de520661 10642
3e4093b6
RS
10643 /* Use the type of the value to be shifted. */
10644 result_type = type0;
3e4093b6
RS
10645 /* Avoid converting op1 to result_type later. */
10646 converted = 1;
400fbf9f 10647 }
3e4093b6 10648 break;
de520661 10649
3e4093b6
RS
10650 case EQ_EXPR:
10651 case NE_EXPR:
d246ab4f
AS
10652 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10653 {
10654 tree intt;
0af94e6f 10655 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10656 {
10657 error_at (location, "comparing vectors with different "
10658 "element types");
10659 return error_mark_node;
10660 }
10661
10662 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10663 {
10664 error_at (location, "comparing vectors with different "
10665 "number of elements");
10666 return error_mark_node;
10667 }
10668
10669 /* Always construct signed integer vector type. */
10670 intt = c_common_type_for_size (GET_MODE_BITSIZE
10671 (TYPE_MODE (TREE_TYPE (type0))), 0);
10672 result_type = build_opaque_vector_type (intt,
10673 TYPE_VECTOR_SUBPARTS (type0));
10674 converted = 1;
10675 break;
10676 }
ae311566 10677 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10678 warning_at (location,
10679 OPT_Wfloat_equal,
10680 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10681 /* Result of comparison is always int,
10682 but don't convert the args to int! */
10683 build_type = integer_type_node;
10684 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10685 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10686 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10687 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10688 short_compare = 1;
637f1455
SZ
10689 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10690 {
10691 if (TREE_CODE (op0) == ADDR_EXPR
10692 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10693 {
10694 if (code == EQ_EXPR)
10695 warning_at (location,
10696 OPT_Waddress,
10697 "the comparison will always evaluate as %<false%> "
10698 "for the address of %qD will never be NULL",
10699 TREE_OPERAND (op0, 0));
10700 else
10701 warning_at (location,
10702 OPT_Waddress,
10703 "the comparison will always evaluate as %<true%> "
10704 "for the address of %qD will never be NULL",
10705 TREE_OPERAND (op0, 0));
10706 }
10707 result_type = type0;
10708 }
10709 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10710 {
10711 if (TREE_CODE (op1) == ADDR_EXPR
10712 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10713 {
10714 if (code == EQ_EXPR)
10715 warning_at (location,
f90e8e2e 10716 OPT_Waddress,
637f1455
SZ
10717 "the comparison will always evaluate as %<false%> "
10718 "for the address of %qD will never be NULL",
10719 TREE_OPERAND (op1, 0));
10720 else
10721 warning_at (location,
10722 OPT_Waddress,
10723 "the comparison will always evaluate as %<true%> "
10724 "for the address of %qD will never be NULL",
10725 TREE_OPERAND (op1, 0));
10726 }
10727 result_type = type1;
10728 }
3e4093b6
RS
10729 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10730 {
10731 tree tt0 = TREE_TYPE (type0);
10732 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10733 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10734 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10735 addr_space_t as_common = ADDR_SPACE_GENERIC;
10736
3e4093b6
RS
10737 /* Anything compares with void *. void * compares with anything.
10738 Otherwise, the targets must be compatible
10739 and both must be object or both incomplete. */
744aa42f 10740 if (comp_target_types (location, type0, type1))
10bc1b1b 10741 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10742 else if (!addr_space_superset (as0, as1, &as_common))
10743 {
10744 error_at (location, "comparison of pointers to "
10745 "disjoint address spaces");
10746 return error_mark_node;
10747 }
267bac10 10748 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10749 {
36c5e70a 10750 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10751 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10752 "comparison of %<void *%> with function pointer");
ee2990e7 10753 }
267bac10 10754 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10755 {
36c5e70a 10756 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10757 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10758 "comparison of %<void *%> with function pointer");
e6834654 10759 }
3e4093b6 10760 else
58393038
ZL
10761 /* Avoid warning about the volatile ObjC EH puts on decls. */
10762 if (!objc_ok)
ba47d38d 10763 pedwarn (location, 0,
509c9d60 10764 "comparison of distinct pointer types lacks a cast");
e6834654 10765
3e4093b6 10766 if (result_type == NULL_TREE)
36c5e70a
BE
10767 {
10768 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10769 result_type = build_pointer_type
10770 (build_qualified_type (void_type_node, qual));
10771 }
e6834654 10772 }
3e4093b6 10773 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10774 {
3e4093b6 10775 result_type = type0;
ba47d38d 10776 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10777 }
3e4093b6 10778 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10779 {
3e4093b6 10780 result_type = type1;
ba47d38d 10781 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10782 }
04159acf
MP
10783 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10784 || truth_value_p (TREE_CODE (orig_op0)))
10785 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10786 || truth_value_p (TREE_CODE (orig_op1))))
10787 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10788 break;
8b6a5902 10789
3e4093b6
RS
10790 case LE_EXPR:
10791 case GE_EXPR:
10792 case LT_EXPR:
10793 case GT_EXPR:
d246ab4f
AS
10794 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10795 {
10796 tree intt;
0af94e6f 10797 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10798 {
10799 error_at (location, "comparing vectors with different "
10800 "element types");
10801 return error_mark_node;
10802 }
10803
10804 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10805 {
10806 error_at (location, "comparing vectors with different "
10807 "number of elements");
10808 return error_mark_node;
10809 }
10810
10811 /* Always construct signed integer vector type. */
10812 intt = c_common_type_for_size (GET_MODE_BITSIZE
10813 (TYPE_MODE (TREE_TYPE (type0))), 0);
10814 result_type = build_opaque_vector_type (intt,
10815 TYPE_VECTOR_SUBPARTS (type0));
10816 converted = 1;
10817 break;
10818 }
3e4093b6 10819 build_type = integer_type_node;
ab22c1fa
CF
10820 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10821 || code0 == FIXED_POINT_TYPE)
10822 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10823 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10824 short_compare = 1;
10825 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10826 {
36c5e70a
BE
10827 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10828 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10829 addr_space_t as_common;
10830
744aa42f 10831 if (comp_target_types (location, type0, type1))
3e4093b6 10832 {
10bc1b1b 10833 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10834 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10835 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10836 pedwarn (location, 0,
509c9d60 10837 "comparison of complete and incomplete pointers");
fcf73884 10838 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10839 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10840 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10841 else if (null_pointer_constant_p (orig_op0)
10842 || null_pointer_constant_p (orig_op1))
10843 warning_at (location, OPT_Wextra,
10844 "ordered comparison of pointer with null pointer");
10845
3e4093b6 10846 }
36c5e70a
BE
10847 else if (!addr_space_superset (as0, as1, &as_common))
10848 {
10849 error_at (location, "comparison of pointers to "
10850 "disjoint address spaces");
10851 return error_mark_node;
10852 }
3e4093b6
RS
10853 else
10854 {
36c5e70a
BE
10855 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10856 result_type = build_pointer_type
10857 (build_qualified_type (void_type_node, qual));
ba47d38d 10858 pedwarn (location, 0,
509c9d60 10859 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10860 }
10861 }
6aa3c60d 10862 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10863 {
10864 result_type = type0;
fcf73884 10865 if (pedantic)
c1771a20 10866 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10867 "ordered comparison of pointer with integer zero");
10868 else if (extra_warnings)
ba47d38d 10869 warning_at (location, OPT_Wextra,
d42ba3b8 10870 "ordered comparison of pointer with integer zero");
3e4093b6 10871 }
6aa3c60d 10872 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10873 {
10874 result_type = type1;
d42ba3b8 10875 if (pedantic)
c1771a20 10876 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10877 "ordered comparison of pointer with integer zero");
10878 else if (extra_warnings)
10879 warning_at (location, OPT_Wextra,
10880 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10881 }
10882 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10883 {
10884 result_type = type0;
ba47d38d 10885 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10886 }
10887 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10888 {
10889 result_type = type1;
ba47d38d 10890 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 10891 }
04159acf
MP
10892 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10893 || truth_value_p (TREE_CODE (orig_op0)))
10894 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10895 || truth_value_p (TREE_CODE (orig_op1))))
10896 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10897 break;
64094f6a 10898
3e4093b6 10899 default:
37b2f290 10900 gcc_unreachable ();
c9fe6f9f 10901 }
8f17b5c5 10902
e57e265b
PB
10903 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10904 return error_mark_node;
10905
5bed876a
AH
10906 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10907 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10908 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10909 {
ba47d38d 10910 binary_op_error (location, code, type0, type1);
5bed876a
AH
10911 return error_mark_node;
10912 }
10913
3e4093b6 10914 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10915 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10916 &&
10917 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10918 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10919 {
2ca862e9
JM
10920 bool first_complex = (code0 == COMPLEX_TYPE);
10921 bool second_complex = (code1 == COMPLEX_TYPE);
10922 int none_complex = (!first_complex && !second_complex);
39b726dd 10923
3e4093b6 10924 if (shorten || common || short_compare)
3bf6bfcc
JJ
10925 {
10926 result_type = c_common_type (type0, type1);
0a0b3574
MM
10927 do_warn_double_promotion (result_type, type0, type1,
10928 "implicit conversion from %qT to %qT "
10929 "to match other operand of binary "
10930 "expression",
10931 location);
3bf6bfcc
JJ
10932 if (result_type == error_mark_node)
10933 return error_mark_node;
10934 }
400fbf9f 10935
2ca862e9
JM
10936 if (first_complex != second_complex
10937 && (code == PLUS_EXPR
10938 || code == MINUS_EXPR
10939 || code == MULT_EXPR
10940 || (code == TRUNC_DIV_EXPR && first_complex))
10941 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10942 && flag_signed_zeros)
10943 {
10944 /* An operation on mixed real/complex operands must be
10945 handled specially, but the language-independent code can
10946 more easily optimize the plain complex arithmetic if
10947 -fno-signed-zeros. */
10948 tree real_type = TREE_TYPE (result_type);
10949 tree real, imag;
10950 if (type0 != orig_type0 || type1 != orig_type1)
10951 {
10952 gcc_assert (may_need_excess_precision && common);
2d2e923f 10953 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
10954 }
10955 if (first_complex)
10956 {
10957 if (TREE_TYPE (op0) != result_type)
68fca595 10958 op0 = convert_and_check (location, result_type, op0);
2ca862e9 10959 if (TREE_TYPE (op1) != real_type)
68fca595 10960 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
10961 }
10962 else
10963 {
10964 if (TREE_TYPE (op0) != real_type)
68fca595 10965 op0 = convert_and_check (location, real_type, op0);
2ca862e9 10966 if (TREE_TYPE (op1) != result_type)
68fca595 10967 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
10968 }
10969 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10970 return error_mark_node;
10971 if (first_complex)
10972 {
10973 op0 = c_save_expr (op0);
10974 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10975 op0, 1);
10976 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10977 op0, 1);
10978 switch (code)
10979 {
10980 case MULT_EXPR:
10981 case TRUNC_DIV_EXPR:
c4603e7c 10982 op1 = c_save_expr (op1);
2ca862e9
JM
10983 imag = build2 (resultcode, real_type, imag, op1);
10984 /* Fall through. */
10985 case PLUS_EXPR:
10986 case MINUS_EXPR:
10987 real = build2 (resultcode, real_type, real, op1);
10988 break;
10989 default:
10990 gcc_unreachable();
10991 }
10992 }
10993 else
10994 {
10995 op1 = c_save_expr (op1);
10996 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10997 op1, 1);
10998 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10999 op1, 1);
11000 switch (code)
11001 {
11002 case MULT_EXPR:
c4603e7c 11003 op0 = c_save_expr (op0);
2ca862e9
JM
11004 imag = build2 (resultcode, real_type, op0, imag);
11005 /* Fall through. */
11006 case PLUS_EXPR:
11007 real = build2 (resultcode, real_type, op0, real);
11008 break;
11009 case MINUS_EXPR:
11010 real = build2 (resultcode, real_type, op0, real);
11011 imag = build1 (NEGATE_EXPR, real_type, imag);
11012 break;
11013 default:
11014 gcc_unreachable();
11015 }
11016 }
11017 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11018 goto return_build_binary_op;
11019 }
11020
3e4093b6
RS
11021 /* For certain operations (which identify themselves by shorten != 0)
11022 if both args were extended from the same smaller type,
11023 do the arithmetic in that type and then extend.
400fbf9f 11024
3e4093b6
RS
11025 shorten !=0 and !=1 indicates a bitwise operation.
11026 For them, this optimization is safe only if
11027 both args are zero-extended or both are sign-extended.
11028 Otherwise, we might change the result.
11029 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11030 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11031
3e4093b6
RS
11032 if (shorten && none_complex)
11033 {
3e4093b6 11034 final_type = result_type;
b8698a0f 11035 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11036 shorten == -1);
3e4093b6 11037 }
88a3dbc1 11038
3e4093b6 11039 /* Shifts can be shortened if shifting right. */
2f6e4e97 11040
3e4093b6
RS
11041 if (short_shift)
11042 {
11043 int unsigned_arg;
11044 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11045
3e4093b6 11046 final_type = result_type;
abe80e6d 11047
3e4093b6 11048 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11049 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11050
3e4093b6 11051 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11052 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11053 /* We can shorten only if the shift count is less than the
11054 number of bits in the smaller type size. */
11055 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11056 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11057 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11058 {
11059 /* Do an unsigned shift if the operand was zero-extended. */
11060 result_type
11061 = c_common_signed_or_unsigned_type (unsigned_arg,
11062 TREE_TYPE (arg0));
11063 /* Convert value-to-be-shifted to that type. */
11064 if (TREE_TYPE (op0) != result_type)
11065 op0 = convert (result_type, op0);
11066 converted = 1;
abe80e6d 11067 }
88a3dbc1
RK
11068 }
11069
3e4093b6
RS
11070 /* Comparison operations are shortened too but differently.
11071 They identify themselves by setting short_compare = 1. */
56cb9733 11072
3e4093b6
RS
11073 if (short_compare)
11074 {
11075 /* Don't write &op0, etc., because that would prevent op0
11076 from being kept in a register.
11077 Instead, make copies of the our local variables and
11078 pass the copies by reference, then copy them back afterward. */
11079 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11080 enum tree_code xresultcode = resultcode;
11081 tree val
393e8e8b
MP
11082 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11083 &xresultcode);
8f17b5c5 11084
3e4093b6 11085 if (val != 0)
c9f9eb5d
AH
11086 {
11087 ret = val;
11088 goto return_build_binary_op;
11089 }
8f17b5c5 11090
3e4093b6
RS
11091 op0 = xop0, op1 = xop1;
11092 converted = 1;
11093 resultcode = xresultcode;
8f17b5c5 11094
7d882b83 11095 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11096 {
11097 bool op0_maybe_const = true;
11098 bool op1_maybe_const = true;
11099 tree orig_op0_folded, orig_op1_folded;
11100
11101 if (in_late_binary_op)
11102 {
11103 orig_op0_folded = orig_op0;
11104 orig_op1_folded = orig_op1;
11105 }
11106 else
11107 {
11108 /* Fold for the sake of possible warnings, as in
11109 build_conditional_expr. This requires the
11110 "original" values to be folded, not just op0 and
11111 op1. */
f5178456 11112 c_inhibit_evaluation_warnings++;
928c19bb
JM
11113 op0 = c_fully_fold (op0, require_constant_value,
11114 &op0_maybe_const);
11115 op1 = c_fully_fold (op1, require_constant_value,
11116 &op1_maybe_const);
f5178456 11117 c_inhibit_evaluation_warnings--;
928c19bb
JM
11118 orig_op0_folded = c_fully_fold (orig_op0,
11119 require_constant_value,
11120 NULL);
11121 orig_op1_folded = c_fully_fold (orig_op1,
11122 require_constant_value,
11123 NULL);
11124 }
11125
11126 if (warn_sign_compare)
11127 warn_for_sign_compare (location, orig_op0_folded,
11128 orig_op1_folded, op0, op1,
11129 result_type, resultcode);
5c2f94b4 11130 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11131 {
11132 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11133 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11134 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11135 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11136 }
3e4093b6 11137 }
2ad1815d 11138 }
64094f6a 11139 }
64094f6a 11140
3e4093b6
RS
11141 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11142 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11143 Then the expression will be built.
11144 It will be given type FINAL_TYPE if that is nonzero;
11145 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11146
3e4093b6
RS
11147 if (!result_type)
11148 {
ba47d38d 11149 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11150 return error_mark_node;
11151 }
400fbf9f 11152
3e4093b6 11153 if (build_type == NULL_TREE)
8ce94e44
JM
11154 {
11155 build_type = result_type;
180f8dbb
JM
11156 if ((type0 != orig_type0 || type1 != orig_type1)
11157 && !boolean_op)
8ce94e44
JM
11158 {
11159 gcc_assert (may_need_excess_precision && common);
2d2e923f 11160 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11161 }
11162 }
400fbf9f 11163
2d2e923f
MLI
11164 if (!converted)
11165 {
68fca595
MP
11166 op0 = ep_convert_and_check (location, result_type, op0,
11167 semantic_result_type);
11168 op1 = ep_convert_and_check (location, result_type, op1,
11169 semantic_result_type);
2d2e923f
MLI
11170
11171 /* This can happen if one operand has a vector type, and the other
11172 has a different type. */
11173 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11174 return error_mark_node;
11175 }
11176
f8ed5150
MP
11177 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11178 | SANITIZE_FLOAT_DIVIDE))
de5a5fa1 11179 && current_function_decl != 0
ce6923c5
MP
11180 && !lookup_attribute ("no_sanitize_undefined",
11181 DECL_ATTRIBUTES (current_function_decl))
de5a5fa1
MP
11182 && (doing_div_or_mod || doing_shift))
11183 {
11184 /* OP0 and/or OP1 might have side-effects. */
11185 op0 = c_save_expr (op0);
11186 op1 = c_save_expr (op1);
11187 op0 = c_fully_fold (op0, false, NULL);
11188 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11189 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11190 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11191 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11192 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11193 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11194 }
11195
c9f9eb5d 11196 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11197 if (int_const_or_overflow)
11198 ret = (require_constant_value
db3927fb
AH
11199 ? fold_build2_initializer_loc (location, resultcode, build_type,
11200 op0, op1)
11201 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11202 else
11203 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11204 if (final_type != 0)
11205 ret = convert (final_type, ret);
11206
11207 return_build_binary_op:
11208 gcc_assert (ret != error_mark_node);
928c19bb
JM
11209 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11210 ret = (int_operands
11211 ? note_integer_operands (ret)
11212 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11213 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11214 && !in_late_binary_op)
11215 ret = note_integer_operands (ret);
2d2e923f
MLI
11216 if (semantic_result_type)
11217 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11218 protected_set_expr_location (ret, location);
de5a5fa1 11219
a24d975c 11220 if (instrument_expr != NULL)
de5a5fa1
MP
11221 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11222 instrument_expr, ret);
11223
c9f9eb5d 11224 return ret;
400fbf9f 11225}
85498824
JM
11226
11227
11228/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11229 purpose. LOCATION is the source location for the expression. */
85498824
JM
11230
11231tree
ba47d38d 11232c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11233{
928c19bb
JM
11234 bool int_const, int_operands;
11235
85498824
JM
11236 switch (TREE_CODE (TREE_TYPE (expr)))
11237 {
11238 case ARRAY_TYPE:
ba47d38d 11239 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11240 return error_mark_node;
11241
11242 case RECORD_TYPE:
ba47d38d 11243 error_at (location, "used struct type value where scalar is required");
85498824
JM
11244 return error_mark_node;
11245
11246 case UNION_TYPE:
ba47d38d 11247 error_at (location, "used union type value where scalar is required");
85498824
JM
11248 return error_mark_node;
11249
04af8788
NP
11250 case VOID_TYPE:
11251 error_at (location, "void value not ignored as it ought to be");
11252 return error_mark_node;
11253
46bdb9cf
JM
11254 case FUNCTION_TYPE:
11255 gcc_unreachable ();
11256
d246ab4f
AS
11257 case VECTOR_TYPE:
11258 error_at (location, "used vector type where scalar is required");
11259 return error_mark_node;
11260
85498824
JM
11261 default:
11262 break;
11263 }
11264
928c19bb
JM
11265 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11266 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11267 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11268 {
11269 expr = remove_c_maybe_const_expr (expr);
11270 expr = build2 (NE_EXPR, integer_type_node, expr,
11271 convert (TREE_TYPE (expr), integer_zero_node));
11272 expr = note_integer_operands (expr);
11273 }
11274 else
11275 /* ??? Should we also give an error for vectors rather than leaving
11276 those to give errors later? */
11277 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11278
11279 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11280 {
11281 if (TREE_OVERFLOW (expr))
11282 return expr;
11283 else
11284 return note_integer_operands (expr);
11285 }
11286 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11287 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11288 return expr;
85498824 11289}
73f397d4
JM
11290\f
11291
11292/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11293 required. */
11294
11295tree
51eed280 11296c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11297{
11298 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11299 {
11300 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11301 /* Executing a compound literal inside a function reinitializes
11302 it. */
11303 if (!TREE_STATIC (decl))
11304 *se = true;
11305 return decl;
11306 }
11307 else
11308 return expr;
11309}
953ff289 11310\f
c0220ea4 11311/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11312
11313tree
11314c_begin_omp_parallel (void)
11315{
11316 tree block;
11317
11318 keep_next_level ();
11319 block = c_begin_compound_stmt (true);
11320
11321 return block;
11322}
11323
c2255bc4
AH
11324/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11325 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11326
953ff289 11327tree
c2255bc4 11328c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11329{
11330 tree stmt;
11331
c2255bc4 11332 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11333
11334 stmt = make_node (OMP_PARALLEL);
11335 TREE_TYPE (stmt) = void_type_node;
11336 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11337 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11338 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11339
11340 return add_stmt (stmt);
11341}
11342
a68ab351
JJ
11343/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11344
11345tree
11346c_begin_omp_task (void)
11347{
11348 tree block;
11349
11350 keep_next_level ();
11351 block = c_begin_compound_stmt (true);
11352
11353 return block;
11354}
11355
c2255bc4
AH
11356/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11357 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11358
11359tree
c2255bc4 11360c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11361{
11362 tree stmt;
11363
c2255bc4 11364 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11365
11366 stmt = make_node (OMP_TASK);
11367 TREE_TYPE (stmt) = void_type_node;
11368 OMP_TASK_CLAUSES (stmt) = clauses;
11369 OMP_TASK_BODY (stmt) = block;
c2255bc4 11370 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11371
11372 return add_stmt (stmt);
11373}
11374
acf0174b
JJ
11375/* Generate GOMP_cancel call for #pragma omp cancel. */
11376
11377void
11378c_finish_omp_cancel (location_t loc, tree clauses)
11379{
11380 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11381 int mask = 0;
11382 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11383 mask = 1;
11384 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11385 mask = 2;
11386 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11387 mask = 4;
11388 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11389 mask = 8;
11390 else
11391 {
11392 error_at (loc, "%<#pragma omp cancel must specify one of "
11393 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11394 "clauses");
11395 return;
11396 }
11397 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11398 if (ifc != NULL_TREE)
11399 {
11400 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11401 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11402 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11403 build_zero_cst (type));
11404 }
11405 else
11406 ifc = boolean_true_node;
11407 tree stmt = build_call_expr_loc (loc, fn, 2,
11408 build_int_cst (integer_type_node, mask),
11409 ifc);
11410 add_stmt (stmt);
11411}
11412
11413/* Generate GOMP_cancellation_point call for
11414 #pragma omp cancellation point. */
11415
11416void
11417c_finish_omp_cancellation_point (location_t loc, tree clauses)
11418{
11419 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11420 int mask = 0;
11421 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11422 mask = 1;
11423 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11424 mask = 2;
11425 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11426 mask = 4;
11427 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11428 mask = 8;
11429 else
11430 {
11431 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11432 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11433 "clauses");
11434 return;
11435 }
11436 tree stmt = build_call_expr_loc (loc, fn, 1,
11437 build_int_cst (integer_type_node, mask));
11438 add_stmt (stmt);
11439}
11440
11441/* Helper function for handle_omp_array_sections. Called recursively
11442 to handle multiple array-section-subscripts. C is the clause,
11443 T current expression (initially OMP_CLAUSE_DECL), which is either
11444 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11445 expression if specified, TREE_VALUE length expression if specified,
11446 TREE_CHAIN is what it has been specified after, or some decl.
11447 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11448 set to true if any of the array-section-subscript could have length
11449 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11450 first array-section-subscript which is known not to have length
11451 of one. Given say:
11452 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11453 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11454 all are or may have length of 1, array-section-subscript [:2] is the
11455 first one knonwn not to have length 1. For array-section-subscript
11456 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11457 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11458 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11459 case though, as some lengths could be zero. */
11460
11461static tree
11462handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11463 bool &maybe_zero_len, unsigned int &first_non_one)
11464{
11465 tree ret, low_bound, length, type;
11466 if (TREE_CODE (t) != TREE_LIST)
11467 {
7a0ca710 11468 if (error_operand_p (t))
acf0174b
JJ
11469 return error_mark_node;
11470 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11471 {
11472 if (DECL_P (t))
11473 error_at (OMP_CLAUSE_LOCATION (c),
11474 "%qD is not a variable in %qs clause", t,
11475 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11476 else
11477 error_at (OMP_CLAUSE_LOCATION (c),
11478 "%qE is not a variable in %qs clause", t,
11479 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11480 return error_mark_node;
11481 }
11482 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11483 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11484 {
11485 error_at (OMP_CLAUSE_LOCATION (c),
11486 "%qD is threadprivate variable in %qs clause", t,
11487 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11488 return error_mark_node;
11489 }
11490 return t;
11491 }
11492
11493 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11494 maybe_zero_len, first_non_one);
11495 if (ret == error_mark_node || ret == NULL_TREE)
11496 return ret;
11497
11498 type = TREE_TYPE (ret);
11499 low_bound = TREE_PURPOSE (t);
11500 length = TREE_VALUE (t);
11501
11502 if (low_bound == error_mark_node || length == error_mark_node)
11503 return error_mark_node;
11504
11505 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11506 {
11507 error_at (OMP_CLAUSE_LOCATION (c),
11508 "low bound %qE of array section does not have integral type",
11509 low_bound);
11510 return error_mark_node;
11511 }
11512 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11513 {
11514 error_at (OMP_CLAUSE_LOCATION (c),
11515 "length %qE of array section does not have integral type",
11516 length);
11517 return error_mark_node;
11518 }
11519 if (low_bound
11520 && TREE_CODE (low_bound) == INTEGER_CST
11521 && TYPE_PRECISION (TREE_TYPE (low_bound))
11522 > TYPE_PRECISION (sizetype))
11523 low_bound = fold_convert (sizetype, low_bound);
11524 if (length
11525 && TREE_CODE (length) == INTEGER_CST
11526 && TYPE_PRECISION (TREE_TYPE (length))
11527 > TYPE_PRECISION (sizetype))
11528 length = fold_convert (sizetype, length);
11529 if (low_bound == NULL_TREE)
11530 low_bound = integer_zero_node;
11531
11532 if (length != NULL_TREE)
11533 {
11534 if (!integer_nonzerop (length))
11535 maybe_zero_len = true;
11536 if (first_non_one == types.length ()
11537 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11538 first_non_one++;
11539 }
11540 if (TREE_CODE (type) == ARRAY_TYPE)
11541 {
11542 if (length == NULL_TREE
11543 && (TYPE_DOMAIN (type) == NULL_TREE
11544 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11545 {
11546 error_at (OMP_CLAUSE_LOCATION (c),
11547 "for unknown bound array type length expression must "
11548 "be specified");
11549 return error_mark_node;
11550 }
11551 if (TREE_CODE (low_bound) == INTEGER_CST
11552 && tree_int_cst_sgn (low_bound) == -1)
11553 {
11554 error_at (OMP_CLAUSE_LOCATION (c),
11555 "negative low bound in array section in %qs clause",
11556 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11557 return error_mark_node;
11558 }
11559 if (length != NULL_TREE
11560 && TREE_CODE (length) == INTEGER_CST
11561 && tree_int_cst_sgn (length) == -1)
11562 {
11563 error_at (OMP_CLAUSE_LOCATION (c),
11564 "negative length in array section in %qs clause",
11565 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11566 return error_mark_node;
11567 }
11568 if (TYPE_DOMAIN (type)
11569 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11570 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11571 == INTEGER_CST)
11572 {
11573 tree size = size_binop (PLUS_EXPR,
11574 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11575 size_one_node);
11576 if (TREE_CODE (low_bound) == INTEGER_CST)
11577 {
11578 if (tree_int_cst_lt (size, low_bound))
11579 {
11580 error_at (OMP_CLAUSE_LOCATION (c),
11581 "low bound %qE above array section size "
11582 "in %qs clause", low_bound,
11583 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11584 return error_mark_node;
11585 }
11586 if (tree_int_cst_equal (size, low_bound))
11587 maybe_zero_len = true;
11588 else if (length == NULL_TREE
11589 && first_non_one == types.length ()
11590 && tree_int_cst_equal
11591 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11592 low_bound))
11593 first_non_one++;
11594 }
11595 else if (length == NULL_TREE)
11596 {
11597 maybe_zero_len = true;
11598 if (first_non_one == types.length ())
11599 first_non_one++;
11600 }
11601 if (length && TREE_CODE (length) == INTEGER_CST)
11602 {
11603 if (tree_int_cst_lt (size, length))
11604 {
11605 error_at (OMP_CLAUSE_LOCATION (c),
11606 "length %qE above array section size "
11607 "in %qs clause", length,
11608 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11609 return error_mark_node;
11610 }
11611 if (TREE_CODE (low_bound) == INTEGER_CST)
11612 {
11613 tree lbpluslen
11614 = size_binop (PLUS_EXPR,
11615 fold_convert (sizetype, low_bound),
11616 fold_convert (sizetype, length));
11617 if (TREE_CODE (lbpluslen) == INTEGER_CST
11618 && tree_int_cst_lt (size, lbpluslen))
11619 {
11620 error_at (OMP_CLAUSE_LOCATION (c),
11621 "high bound %qE above array section size "
11622 "in %qs clause", lbpluslen,
11623 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11624 return error_mark_node;
11625 }
11626 }
11627 }
11628 }
11629 else if (length == NULL_TREE)
11630 {
11631 maybe_zero_len = true;
11632 if (first_non_one == types.length ())
11633 first_non_one++;
11634 }
11635
11636 /* For [lb:] we will need to evaluate lb more than once. */
11637 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11638 {
11639 tree lb = c_save_expr (low_bound);
11640 if (lb != low_bound)
11641 {
11642 TREE_PURPOSE (t) = lb;
11643 low_bound = lb;
11644 }
11645 }
11646 }
11647 else if (TREE_CODE (type) == POINTER_TYPE)
11648 {
11649 if (length == NULL_TREE)
11650 {
11651 error_at (OMP_CLAUSE_LOCATION (c),
11652 "for pointer type length expression must be specified");
11653 return error_mark_node;
11654 }
11655 /* If there is a pointer type anywhere but in the very first
11656 array-section-subscript, the array section can't be contiguous. */
11657 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11658 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11659 {
11660 error_at (OMP_CLAUSE_LOCATION (c),
11661 "array section is not contiguous in %qs clause",
11662 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11663 return error_mark_node;
11664 }
11665 }
11666 else
11667 {
11668 error_at (OMP_CLAUSE_LOCATION (c),
11669 "%qE does not have pointer or array type", ret);
11670 return error_mark_node;
11671 }
11672 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11673 types.safe_push (TREE_TYPE (ret));
11674 /* We will need to evaluate lb more than once. */
11675 tree lb = c_save_expr (low_bound);
11676 if (lb != low_bound)
11677 {
11678 TREE_PURPOSE (t) = lb;
11679 low_bound = lb;
11680 }
11681 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11682 return ret;
11683}
11684
11685/* Handle array sections for clause C. */
11686
11687static bool
11688handle_omp_array_sections (tree c)
11689{
11690 bool maybe_zero_len = false;
11691 unsigned int first_non_one = 0;
11692 vec<tree> types = vNULL;
11693 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11694 maybe_zero_len, first_non_one);
11695 if (first == error_mark_node)
11696 {
11697 types.release ();
11698 return true;
11699 }
11700 if (first == NULL_TREE)
11701 {
11702 types.release ();
11703 return false;
11704 }
11705 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11706 {
11707 tree t = OMP_CLAUSE_DECL (c);
11708 tree tem = NULL_TREE;
11709 types.release ();
11710 /* Need to evaluate side effects in the length expressions
11711 if any. */
11712 while (TREE_CODE (t) == TREE_LIST)
11713 {
11714 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11715 {
11716 if (tem == NULL_TREE)
11717 tem = TREE_VALUE (t);
11718 else
11719 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11720 TREE_VALUE (t), tem);
11721 }
11722 t = TREE_CHAIN (t);
11723 }
11724 if (tem)
11725 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11726 first = c_fully_fold (first, false, NULL);
11727 OMP_CLAUSE_DECL (c) = first;
11728 }
11729 else
11730 {
11731 unsigned int num = types.length (), i;
11732 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11733 tree condition = NULL_TREE;
11734
11735 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11736 maybe_zero_len = true;
11737
11738 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11739 t = TREE_CHAIN (t))
11740 {
11741 tree low_bound = TREE_PURPOSE (t);
11742 tree length = TREE_VALUE (t);
11743
11744 i--;
11745 if (low_bound
11746 && TREE_CODE (low_bound) == INTEGER_CST
11747 && TYPE_PRECISION (TREE_TYPE (low_bound))
11748 > TYPE_PRECISION (sizetype))
11749 low_bound = fold_convert (sizetype, low_bound);
11750 if (length
11751 && TREE_CODE (length) == INTEGER_CST
11752 && TYPE_PRECISION (TREE_TYPE (length))
11753 > TYPE_PRECISION (sizetype))
11754 length = fold_convert (sizetype, length);
11755 if (low_bound == NULL_TREE)
11756 low_bound = integer_zero_node;
11757 if (!maybe_zero_len && i > first_non_one)
11758 {
11759 if (integer_nonzerop (low_bound))
11760 goto do_warn_noncontiguous;
11761 if (length != NULL_TREE
11762 && TREE_CODE (length) == INTEGER_CST
11763 && TYPE_DOMAIN (types[i])
11764 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11765 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11766 == INTEGER_CST)
11767 {
11768 tree size;
11769 size = size_binop (PLUS_EXPR,
11770 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11771 size_one_node);
11772 if (!tree_int_cst_equal (length, size))
11773 {
11774 do_warn_noncontiguous:
11775 error_at (OMP_CLAUSE_LOCATION (c),
11776 "array section is not contiguous in %qs "
11777 "clause",
11778 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11779 types.release ();
11780 return true;
11781 }
11782 }
11783 if (length != NULL_TREE
11784 && TREE_SIDE_EFFECTS (length))
11785 {
11786 if (side_effects == NULL_TREE)
11787 side_effects = length;
11788 else
11789 side_effects = build2 (COMPOUND_EXPR,
11790 TREE_TYPE (side_effects),
11791 length, side_effects);
11792 }
11793 }
11794 else
11795 {
11796 tree l;
11797
11798 if (i > first_non_one && length && integer_nonzerop (length))
11799 continue;
11800 if (length)
11801 l = fold_convert (sizetype, length);
11802 else
11803 {
11804 l = size_binop (PLUS_EXPR,
11805 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11806 size_one_node);
11807 l = size_binop (MINUS_EXPR, l,
11808 fold_convert (sizetype, low_bound));
11809 }
11810 if (i > first_non_one)
11811 {
11812 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11813 size_zero_node);
11814 if (condition == NULL_TREE)
11815 condition = l;
11816 else
11817 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11818 l, condition);
11819 }
11820 else if (size == NULL_TREE)
11821 {
11822 size = size_in_bytes (TREE_TYPE (types[i]));
11823 size = size_binop (MULT_EXPR, size, l);
11824 if (condition)
11825 size = fold_build3 (COND_EXPR, sizetype, condition,
11826 size, size_zero_node);
11827 }
11828 else
11829 size = size_binop (MULT_EXPR, size, l);
11830 }
11831 }
11832 types.release ();
11833 if (side_effects)
11834 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11835 first = c_fully_fold (first, false, NULL);
11836 OMP_CLAUSE_DECL (c) = first;
11837 if (size)
11838 size = c_fully_fold (size, false, NULL);
11839 OMP_CLAUSE_SIZE (c) = size;
11840 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11841 return false;
11842 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11843 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11844 if (!c_mark_addressable (t))
11845 return false;
11846 OMP_CLAUSE_DECL (c2) = t;
11847 t = build_fold_addr_expr (first);
11848 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11849 tree ptr = OMP_CLAUSE_DECL (c2);
11850 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11851 ptr = build_fold_addr_expr (ptr);
11852 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11853 ptrdiff_type_node, t,
11854 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11855 ptrdiff_type_node, ptr));
11856 t = c_fully_fold (t, false, NULL);
11857 OMP_CLAUSE_SIZE (c2) = t;
11858 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11859 OMP_CLAUSE_CHAIN (c) = c2;
11860 }
11861 return false;
11862}
11863
11864/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11865 an inline call. But, remap
11866 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11867 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11868
11869static tree
11870c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11871 tree decl, tree placeholder)
11872{
11873 copy_body_data id;
b787e7a2 11874 hash_map<tree, tree> decl_map;
acf0174b 11875
b787e7a2
TS
11876 decl_map.put (omp_decl1, placeholder);
11877 decl_map.put (omp_decl2, decl);
acf0174b
JJ
11878 memset (&id, 0, sizeof (id));
11879 id.src_fn = DECL_CONTEXT (omp_decl1);
11880 id.dst_fn = current_function_decl;
11881 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 11882 id.decl_map = &decl_map;
acf0174b
JJ
11883
11884 id.copy_decl = copy_decl_no_change;
11885 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11886 id.transform_new_cfg = true;
11887 id.transform_return_to_modify = false;
11888 id.transform_lang_insert_block = NULL;
11889 id.eh_lp_nr = 0;
11890 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
11891 return stmt;
11892}
11893
11894/* Helper function of c_finish_omp_clauses, called via walk_tree.
11895 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11896
11897static tree
11898c_find_omp_placeholder_r (tree *tp, int *, void *data)
11899{
11900 if (*tp == (tree) data)
11901 return *tp;
11902 return NULL_TREE;
11903}
11904
953ff289
DN
11905/* For all elements of CLAUSES, validate them vs OpenMP constraints.
11906 Remove any elements from the list that are invalid. */
11907
11908tree
11909c_finish_omp_clauses (tree clauses)
11910{
11911 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 11912 bitmap_head aligned_head;
f3316c6d 11913 tree c, t, *pc;
acf0174b
JJ
11914 bool branch_seen = false;
11915 bool copyprivate_seen = false;
11916 tree *nowait_clause = NULL;
953ff289
DN
11917
11918 bitmap_obstack_initialize (NULL);
11919 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11920 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11921 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 11922 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
11923
11924 for (pc = &clauses, c = clauses; c ; c = *pc)
11925 {
11926 bool remove = false;
11927 bool need_complete = false;
11928 bool need_implicitly_determined = false;
11929
aaf46ef9 11930 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
11931 {
11932 case OMP_CLAUSE_SHARED:
953ff289
DN
11933 need_implicitly_determined = true;
11934 goto check_dup_generic;
11935
11936 case OMP_CLAUSE_PRIVATE:
953ff289
DN
11937 need_complete = true;
11938 need_implicitly_determined = true;
11939 goto check_dup_generic;
11940
11941 case OMP_CLAUSE_REDUCTION:
953ff289
DN
11942 need_implicitly_determined = true;
11943 t = OMP_CLAUSE_DECL (c);
acf0174b 11944 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
652fea39
JJ
11945 && (FLOAT_TYPE_P (TREE_TYPE (t))
11946 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
953ff289
DN
11947 {
11948 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11949 const char *r_name = NULL;
11950
11951 switch (r_code)
11952 {
11953 case PLUS_EXPR:
11954 case MULT_EXPR:
11955 case MINUS_EXPR:
652fea39 11956 break;
20906c66 11957 case MIN_EXPR:
652fea39
JJ
11958 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11959 r_name = "min";
11960 break;
20906c66 11961 case MAX_EXPR:
652fea39
JJ
11962 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11963 r_name = "max";
953ff289
DN
11964 break;
11965 case BIT_AND_EXPR:
11966 r_name = "&";
11967 break;
11968 case BIT_XOR_EXPR:
11969 r_name = "^";
11970 break;
11971 case BIT_IOR_EXPR:
11972 r_name = "|";
11973 break;
11974 case TRUTH_ANDIF_EXPR:
652fea39
JJ
11975 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11976 r_name = "&&";
953ff289
DN
11977 break;
11978 case TRUTH_ORIF_EXPR:
652fea39
JJ
11979 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11980 r_name = "||";
953ff289
DN
11981 break;
11982 default:
11983 gcc_unreachable ();
11984 }
11985 if (r_name)
11986 {
c2255bc4
AH
11987 error_at (OMP_CLAUSE_LOCATION (c),
11988 "%qE has invalid type for %<reduction(%s)%>",
11989 t, r_name);
953ff289 11990 remove = true;
ee1d5a02 11991 break;
953ff289
DN
11992 }
11993 }
acf0174b
JJ
11994 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11995 {
11996 error_at (OMP_CLAUSE_LOCATION (c),
11997 "user defined reduction not found for %qD", t);
11998 remove = true;
ee1d5a02 11999 break;
acf0174b
JJ
12000 }
12001 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12002 {
12003 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12004 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12005 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12006 VAR_DECL, NULL_TREE, type);
12007 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12008 DECL_ARTIFICIAL (placeholder) = 1;
12009 DECL_IGNORED_P (placeholder) = 1;
12010 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12011 c_mark_addressable (placeholder);
12012 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12013 c_mark_addressable (OMP_CLAUSE_DECL (c));
12014 OMP_CLAUSE_REDUCTION_MERGE (c)
12015 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12016 TREE_VEC_ELT (list, 0),
12017 TREE_VEC_ELT (list, 1),
12018 OMP_CLAUSE_DECL (c), placeholder);
12019 OMP_CLAUSE_REDUCTION_MERGE (c)
12020 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12021 void_type_node, NULL_TREE,
12022 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12023 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12024 if (TREE_VEC_LENGTH (list) == 6)
12025 {
12026 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12027 c_mark_addressable (OMP_CLAUSE_DECL (c));
12028 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12029 c_mark_addressable (placeholder);
12030 tree init = TREE_VEC_ELT (list, 5);
12031 if (init == error_mark_node)
12032 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12033 OMP_CLAUSE_REDUCTION_INIT (c)
12034 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12035 TREE_VEC_ELT (list, 3),
12036 OMP_CLAUSE_DECL (c), placeholder);
12037 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12038 OMP_CLAUSE_REDUCTION_INIT (c)
12039 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12040 OMP_CLAUSE_REDUCTION_INIT (c));
12041 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12042 c_find_omp_placeholder_r,
12043 placeholder, NULL))
12044 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12045 }
12046 else
12047 {
12048 tree init;
12049 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12050 init = build_constructor (TREE_TYPE (t), NULL);
12051 else
12052 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12053 OMP_CLAUSE_REDUCTION_INIT (c)
12054 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12055 }
12056 OMP_CLAUSE_REDUCTION_INIT (c)
12057 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12058 void_type_node, NULL_TREE,
12059 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12060 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12061 }
953ff289
DN
12062 goto check_dup_generic;
12063
12064 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12065 copyprivate_seen = true;
12066 if (nowait_clause)
12067 {
12068 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12069 "%<nowait%> clause must not be used together "
12070 "with %<copyprivate%>");
12071 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12072 nowait_clause = NULL;
12073 }
953ff289
DN
12074 goto check_dup_generic;
12075
12076 case OMP_CLAUSE_COPYIN:
953ff289
DN
12077 t = OMP_CLAUSE_DECL (c);
12078 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12079 {
c2255bc4
AH
12080 error_at (OMP_CLAUSE_LOCATION (c),
12081 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12082 remove = true;
ee1d5a02 12083 break;
953ff289
DN
12084 }
12085 goto check_dup_generic;
12086
acf0174b
JJ
12087 case OMP_CLAUSE_LINEAR:
12088 t = OMP_CLAUSE_DECL (c);
12089 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12090 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12091 {
12092 error_at (OMP_CLAUSE_LOCATION (c),
12093 "linear clause applied to non-integral non-pointer "
12094 "variable with type %qT", TREE_TYPE (t));
12095 remove = true;
12096 break;
12097 }
12098 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12099 {
12100 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12101 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12102 OMP_CLAUSE_DECL (c), s);
12103 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12104 sizetype, s, OMP_CLAUSE_DECL (c));
12105 if (s == error_mark_node)
12106 s = size_one_node;
12107 OMP_CLAUSE_LINEAR_STEP (c) = s;
12108 }
da6f124d
JJ
12109 else
12110 OMP_CLAUSE_LINEAR_STEP (c)
12111 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12112 goto check_dup_generic;
12113
953ff289
DN
12114 check_dup_generic:
12115 t = OMP_CLAUSE_DECL (c);
12116 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12117 {
c2255bc4 12118 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12119 "%qE is not a variable in clause %qs", t,
12120 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12121 remove = true;
12122 }
12123 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12124 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12125 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12126 {
c2255bc4
AH
12127 error_at (OMP_CLAUSE_LOCATION (c),
12128 "%qE appears more than once in data clauses", t);
953ff289
DN
12129 remove = true;
12130 }
12131 else
12132 bitmap_set_bit (&generic_head, DECL_UID (t));
12133 break;
12134
12135 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12136 t = OMP_CLAUSE_DECL (c);
12137 need_complete = true;
12138 need_implicitly_determined = true;
12139 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12140 {
c2255bc4
AH
12141 error_at (OMP_CLAUSE_LOCATION (c),
12142 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12143 remove = true;
12144 }
12145 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12146 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12147 {
c2255bc4
AH
12148 error_at (OMP_CLAUSE_LOCATION (c),
12149 "%qE appears more than once in data clauses", t);
953ff289
DN
12150 remove = true;
12151 }
12152 else
12153 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12154 break;
12155
12156 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12157 t = OMP_CLAUSE_DECL (c);
12158 need_complete = true;
12159 need_implicitly_determined = true;
12160 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12161 {
c2255bc4
AH
12162 error_at (OMP_CLAUSE_LOCATION (c),
12163 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12164 remove = true;
12165 }
12166 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12167 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12168 {
c2255bc4
AH
12169 error_at (OMP_CLAUSE_LOCATION (c),
12170 "%qE appears more than once in data clauses", t);
953ff289
DN
12171 remove = true;
12172 }
12173 else
12174 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12175 break;
12176
acf0174b
JJ
12177 case OMP_CLAUSE_ALIGNED:
12178 t = OMP_CLAUSE_DECL (c);
12179 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12180 {
12181 error_at (OMP_CLAUSE_LOCATION (c),
12182 "%qE is not a variable in %<aligned%> clause", t);
12183 remove = true;
12184 }
5a9785fb
JJ
12185 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12186 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12187 {
12188 error_at (OMP_CLAUSE_LOCATION (c),
12189 "%qE in %<aligned%> clause is neither a pointer nor "
12190 "an array", t);
12191 remove = true;
12192 }
acf0174b
JJ
12193 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12194 {
12195 error_at (OMP_CLAUSE_LOCATION (c),
12196 "%qE appears more than once in %<aligned%> clauses",
12197 t);
12198 remove = true;
12199 }
12200 else
12201 bitmap_set_bit (&aligned_head, DECL_UID (t));
12202 break;
12203
12204 case OMP_CLAUSE_DEPEND:
12205 t = OMP_CLAUSE_DECL (c);
12206 if (TREE_CODE (t) == TREE_LIST)
12207 {
12208 if (handle_omp_array_sections (c))
12209 remove = true;
12210 break;
12211 }
12212 if (t == error_mark_node)
12213 remove = true;
12214 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12215 {
12216 error_at (OMP_CLAUSE_LOCATION (c),
12217 "%qE is not a variable in %<depend%> clause", t);
12218 remove = true;
12219 }
12220 else if (!c_mark_addressable (t))
12221 remove = true;
12222 break;
12223
12224 case OMP_CLAUSE_MAP:
12225 case OMP_CLAUSE_TO:
12226 case OMP_CLAUSE_FROM:
12227 t = OMP_CLAUSE_DECL (c);
12228 if (TREE_CODE (t) == TREE_LIST)
12229 {
12230 if (handle_omp_array_sections (c))
12231 remove = true;
12232 else
12233 {
12234 t = OMP_CLAUSE_DECL (c);
b17a8b07 12235 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12236 {
12237 error_at (OMP_CLAUSE_LOCATION (c),
12238 "array section does not have mappable type "
12239 "in %qs clause",
12240 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12241 remove = true;
12242 }
12243 }
12244 break;
12245 }
12246 if (t == error_mark_node)
12247 remove = true;
12248 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12249 {
12250 error_at (OMP_CLAUSE_LOCATION (c),
12251 "%qE is not a variable in %qs clause", t,
12252 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12253 remove = true;
12254 }
12255 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12256 {
12257 error_at (OMP_CLAUSE_LOCATION (c),
12258 "%qD is threadprivate variable in %qs clause", t,
12259 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12260 remove = true;
12261 }
12262 else if (!c_mark_addressable (t))
12263 remove = true;
b17a8b07
TS
12264 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12265 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12266 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12267 {
12268 error_at (OMP_CLAUSE_LOCATION (c),
12269 "%qD does not have a mappable type in %qs clause", t,
12270 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12271 remove = true;
12272 }
12273 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12274 {
12275 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12276 error ("%qD appears more than once in motion clauses", t);
12277 else
12278 error ("%qD appears more than once in map clauses", t);
12279 remove = true;
12280 }
12281 else
12282 bitmap_set_bit (&generic_head, DECL_UID (t));
12283 break;
12284
12285 case OMP_CLAUSE_UNIFORM:
12286 t = OMP_CLAUSE_DECL (c);
12287 if (TREE_CODE (t) != PARM_DECL)
12288 {
12289 if (DECL_P (t))
12290 error_at (OMP_CLAUSE_LOCATION (c),
12291 "%qD is not an argument in %<uniform%> clause", t);
12292 else
12293 error_at (OMP_CLAUSE_LOCATION (c),
12294 "%qE is not an argument in %<uniform%> clause", t);
12295 remove = true;
ee1d5a02 12296 break;
acf0174b 12297 }
ee1d5a02 12298 goto check_dup_generic;
acf0174b
JJ
12299
12300 case OMP_CLAUSE_NOWAIT:
12301 if (copyprivate_seen)
12302 {
12303 error_at (OMP_CLAUSE_LOCATION (c),
12304 "%<nowait%> clause must not be used together "
12305 "with %<copyprivate%>");
12306 remove = true;
12307 break;
12308 }
12309 nowait_clause = pc;
12310 pc = &OMP_CLAUSE_CHAIN (c);
12311 continue;
12312
953ff289
DN
12313 case OMP_CLAUSE_IF:
12314 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12315 case OMP_CLAUSE_NUM_TEAMS:
12316 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12317 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12318 case OMP_CLAUSE_ORDERED:
12319 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12320 case OMP_CLAUSE_UNTIED:
12321 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12322 case OMP_CLAUSE_FINAL:
12323 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12324 case OMP_CLAUSE_SAFELEN:
12325 case OMP_CLAUSE_SIMDLEN:
12326 case OMP_CLAUSE_DEVICE:
12327 case OMP_CLAUSE_DIST_SCHEDULE:
12328 case OMP_CLAUSE_PARALLEL:
12329 case OMP_CLAUSE_FOR:
12330 case OMP_CLAUSE_SECTIONS:
12331 case OMP_CLAUSE_TASKGROUP:
12332 case OMP_CLAUSE_PROC_BIND:
9a771876 12333 case OMP_CLAUSE__CILK_FOR_COUNT_:
acf0174b
JJ
12334 pc = &OMP_CLAUSE_CHAIN (c);
12335 continue;
12336
12337 case OMP_CLAUSE_INBRANCH:
12338 case OMP_CLAUSE_NOTINBRANCH:
12339 if (branch_seen)
12340 {
12341 error_at (OMP_CLAUSE_LOCATION (c),
12342 "%<inbranch%> clause is incompatible with "
12343 "%<notinbranch%>");
12344 remove = true;
12345 break;
12346 }
12347 branch_seen = true;
953ff289
DN
12348 pc = &OMP_CLAUSE_CHAIN (c);
12349 continue;
12350
12351 default:
12352 gcc_unreachable ();
12353 }
12354
12355 if (!remove)
12356 {
12357 t = OMP_CLAUSE_DECL (c);
12358
12359 if (need_complete)
12360 {
12361 t = require_complete_type (t);
12362 if (t == error_mark_node)
12363 remove = true;
12364 }
12365
12366 if (need_implicitly_determined)
12367 {
12368 const char *share_name = NULL;
12369
12370 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12371 share_name = "threadprivate";
12372 else switch (c_omp_predetermined_sharing (t))
12373 {
12374 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12375 break;
12376 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12377 /* const vars may be specified in firstprivate clause. */
12378 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12379 && TREE_READONLY (t))
12380 break;
953ff289
DN
12381 share_name = "shared";
12382 break;
12383 case OMP_CLAUSE_DEFAULT_PRIVATE:
12384 share_name = "private";
12385 break;
12386 default:
12387 gcc_unreachable ();
12388 }
12389 if (share_name)
12390 {
c2255bc4
AH
12391 error_at (OMP_CLAUSE_LOCATION (c),
12392 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12393 t, share_name,
12394 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12395 remove = true;
12396 }
12397 }
12398 }
12399
12400 if (remove)
12401 *pc = OMP_CLAUSE_CHAIN (c);
12402 else
12403 pc = &OMP_CLAUSE_CHAIN (c);
12404 }
12405
12406 bitmap_obstack_release (NULL);
12407 return clauses;
12408}
9ae165a0 12409
0a35513e
AH
12410/* Create a transaction node. */
12411
12412tree
12413c_finish_transaction (location_t loc, tree block, int flags)
12414{
12415 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12416 if (flags & TM_STMT_ATTR_OUTER)
12417 TRANSACTION_EXPR_OUTER (stmt) = 1;
12418 if (flags & TM_STMT_ATTR_RELAXED)
12419 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12420 return add_stmt (stmt);
12421}
12422
9ae165a0
DG
12423/* Make a variant type in the proper way for C/C++, propagating qualifiers
12424 down to the element type of an array. */
12425
12426tree
12427c_build_qualified_type (tree type, int type_quals)
12428{
12429 if (type == error_mark_node)
12430 return type;
12431
12432 if (TREE_CODE (type) == ARRAY_TYPE)
12433 {
12434 tree t;
12435 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12436 type_quals);
12437
12438 /* See if we already have an identically qualified type. */
12439 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12440 {
12441 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12442 && TYPE_NAME (t) == TYPE_NAME (type)
12443 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12444 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12445 TYPE_ATTRIBUTES (type)))
12446 break;
12447 }
12448 if (!t)
12449 {
12450 tree domain = TYPE_DOMAIN (type);
12451
12452 t = build_variant_type_copy (type);
12453 TREE_TYPE (t) = element_type;
12454
12455 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12456 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12457 SET_TYPE_STRUCTURAL_EQUALITY (t);
12458 else if (TYPE_CANONICAL (element_type) != element_type
12459 || (domain && TYPE_CANONICAL (domain) != domain))
12460 {
b8698a0f 12461 tree unqualified_canon
9ae165a0 12462 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12463 domain? TYPE_CANONICAL (domain)
9ae165a0 12464 : NULL_TREE);
b8698a0f 12465 TYPE_CANONICAL (t)
9ae165a0
DG
12466 = c_build_qualified_type (unqualified_canon, type_quals);
12467 }
12468 else
12469 TYPE_CANONICAL (t) = t;
12470 }
12471 return t;
12472 }
12473
12474 /* A restrict-qualified pointer type must be a pointer to object or
12475 incomplete type. Note that the use of POINTER_TYPE_P also allows
12476 REFERENCE_TYPEs, which is appropriate for C++. */
12477 if ((type_quals & TYPE_QUAL_RESTRICT)
12478 && (!POINTER_TYPE_P (type)
12479 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12480 {
12481 error ("invalid use of %<restrict%>");
12482 type_quals &= ~TYPE_QUAL_RESTRICT;
12483 }
12484
12485 return build_qualified_type (type, type_quals);
12486}
72b5577d
ILT
12487
12488/* Build a VA_ARG_EXPR for the C parser. */
12489
12490tree
c2255bc4 12491c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d
ILT
12492{
12493 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12494 warning_at (loc, OPT_Wc___compat,
12495 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12496 return build_va_arg (loc, expr, type);
72b5577d 12497}
acf0174b
JJ
12498
12499/* Return truthvalue of whether T1 is the same tree structure as T2.
12500 Return 1 if they are the same. Return 0 if they are different. */
12501
12502bool
12503c_tree_equal (tree t1, tree t2)
12504{
12505 enum tree_code code1, code2;
12506
12507 if (t1 == t2)
12508 return true;
12509 if (!t1 || !t2)
12510 return false;
12511
12512 for (code1 = TREE_CODE (t1);
12513 CONVERT_EXPR_CODE_P (code1)
12514 || code1 == NON_LVALUE_EXPR;
12515 code1 = TREE_CODE (t1))
12516 t1 = TREE_OPERAND (t1, 0);
12517 for (code2 = TREE_CODE (t2);
12518 CONVERT_EXPR_CODE_P (code2)
12519 || code2 == NON_LVALUE_EXPR;
12520 code2 = TREE_CODE (t2))
12521 t2 = TREE_OPERAND (t2, 0);
12522
12523 /* They might have become equal now. */
12524 if (t1 == t2)
12525 return true;
12526
12527 if (code1 != code2)
12528 return false;
12529
12530 switch (code1)
12531 {
12532 case INTEGER_CST:
807e902e 12533 return wi::eq_p (t1, t2);
acf0174b
JJ
12534
12535 case REAL_CST:
12536 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12537
12538 case STRING_CST:
12539 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12540 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12541 TREE_STRING_LENGTH (t1));
12542
12543 case FIXED_CST:
12544 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12545 TREE_FIXED_CST (t2));
12546
12547 case COMPLEX_CST:
12548 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12549 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12550
12551 case VECTOR_CST:
12552 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12553
12554 case CONSTRUCTOR:
12555 /* We need to do this when determining whether or not two
12556 non-type pointer to member function template arguments
12557 are the same. */
12558 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12559 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12560 return false;
12561 {
12562 tree field, value;
12563 unsigned int i;
12564 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12565 {
12566 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12567 if (!c_tree_equal (field, elt2->index)
12568 || !c_tree_equal (value, elt2->value))
12569 return false;
12570 }
12571 }
12572 return true;
12573
12574 case TREE_LIST:
12575 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12576 return false;
12577 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12578 return false;
12579 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12580
12581 case SAVE_EXPR:
12582 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12583
12584 case CALL_EXPR:
12585 {
12586 tree arg1, arg2;
12587 call_expr_arg_iterator iter1, iter2;
12588 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12589 return false;
12590 for (arg1 = first_call_expr_arg (t1, &iter1),
12591 arg2 = first_call_expr_arg (t2, &iter2);
12592 arg1 && arg2;
12593 arg1 = next_call_expr_arg (&iter1),
12594 arg2 = next_call_expr_arg (&iter2))
12595 if (!c_tree_equal (arg1, arg2))
12596 return false;
12597 if (arg1 || arg2)
12598 return false;
12599 return true;
12600 }
12601
12602 case TARGET_EXPR:
12603 {
12604 tree o1 = TREE_OPERAND (t1, 0);
12605 tree o2 = TREE_OPERAND (t2, 0);
12606
12607 /* Special case: if either target is an unallocated VAR_DECL,
12608 it means that it's going to be unified with whatever the
12609 TARGET_EXPR is really supposed to initialize, so treat it
12610 as being equivalent to anything. */
12611 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12612 && !DECL_RTL_SET_P (o1))
12613 /*Nop*/;
12614 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12615 && !DECL_RTL_SET_P (o2))
12616 /*Nop*/;
12617 else if (!c_tree_equal (o1, o2))
12618 return false;
12619
12620 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12621 }
12622
12623 case COMPONENT_REF:
12624 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12625 return false;
12626 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12627
12628 case PARM_DECL:
12629 case VAR_DECL:
12630 case CONST_DECL:
12631 case FIELD_DECL:
12632 case FUNCTION_DECL:
12633 case IDENTIFIER_NODE:
12634 case SSA_NAME:
12635 return false;
12636
12637 case TREE_VEC:
12638 {
12639 unsigned ix;
12640 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12641 return false;
12642 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12643 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12644 TREE_VEC_ELT (t2, ix)))
12645 return false;
12646 return true;
12647 }
12648
12649 default:
12650 break;
12651 }
12652
12653 switch (TREE_CODE_CLASS (code1))
12654 {
12655 case tcc_unary:
12656 case tcc_binary:
12657 case tcc_comparison:
12658 case tcc_expression:
12659 case tcc_vl_exp:
12660 case tcc_reference:
12661 case tcc_statement:
12662 {
12663 int i, n = TREE_OPERAND_LENGTH (t1);
12664
12665 switch (code1)
12666 {
12667 case PREINCREMENT_EXPR:
12668 case PREDECREMENT_EXPR:
12669 case POSTINCREMENT_EXPR:
12670 case POSTDECREMENT_EXPR:
12671 n = 1;
12672 break;
12673 case ARRAY_REF:
12674 n = 2;
12675 break;
12676 default:
12677 break;
12678 }
12679
12680 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12681 && n != TREE_OPERAND_LENGTH (t2))
12682 return false;
12683
12684 for (i = 0; i < n; ++i)
12685 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12686 return false;
12687
12688 return true;
12689 }
12690
12691 case tcc_type:
12692 return comptypes (t1, t2);
12693 default:
12694 gcc_unreachable ();
12695 }
12696 /* We can get here with --disable-checking. */
12697 return false;
12698}
12893402
BI
12699
12700/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12701 spawn-helper and BODY is the newly created body for FNDECL. */
12702
12703void
12704cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12705{
12706 tree list = alloc_stmt_list ();
12707 tree frame = make_cilk_frame (fndecl);
12708 tree dtor = create_cilk_function_exit (frame, false, true);
12709 add_local_decl (cfun, frame);
12710
12711 DECL_SAVED_TREE (fndecl) = list;
12712 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12713 frame);
12714 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12715 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12716
12717 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12718 append_to_statement_list (detach_expr, &body_list);
12719
12720 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12721 body = fold_build_cleanup_point_expr (void_type_node, body);
12722
12723 append_to_statement_list (body, &body_list);
12724 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12725 body_list, dtor), &list);
12726}