]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
plugin.def: Improve comment for PLUGIN_INCLUDE_FILE.
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
23a5b65a 2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6
ZW
28#include "coretypes.h"
29#include "tm.h"
400fbf9f 30#include "tree.h"
d8a2d370
DN
31#include "stor-layout.h"
32#include "trans-mem.h"
33#include "varasm.h"
34#include "stmt.h"
e57e265b 35#include "langhooks.h"
400fbf9f 36#include "c-tree.h"
29cc57cf 37#include "c-lang.h"
400fbf9f 38#include "flags.h"
ab87f8c8 39#include "intl.h"
672a6f42 40#include "target.h"
325c3691 41#include "tree-iterator.h"
6662d794 42#include "bitmap.h"
2fb9a547
AM
43#include "pointer-set.h"
44#include "basic-block.h"
45#include "gimple-expr.h"
45b0be94 46#include "gimplify.h"
acf0174b 47#include "tree-inline.h"
0645c1a2 48#include "omp-low.h"
61d3ce20 49#include "c-family/c-objc.h"
a212e43f 50#include "c-family/c-common.h"
de5a5fa1 51#include "c-family/c-ubsan.h"
12893402 52#include "cilk.h"
325c3691 53
2ac2f164
JM
54/* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
56enum impl_conv {
57 ic_argpass,
58 ic_assign,
59 ic_init,
60 ic_return
61};
62
bc4b653b
JM
63/* The level of nesting inside "__alignof__". */
64int in_alignof;
65
66/* The level of nesting inside "sizeof". */
67int in_sizeof;
68
69/* The level of nesting inside "typeof". */
70int in_typeof;
400fbf9f 71
1a4049e7
JJ
72/* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74tree c_last_sizeof_arg;
75
b71c7f8a 76/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 77 message within this initializer. */
b71c7f8a 78static int missing_braces_mentioned;
103b7b17 79
bf730f15
RS
80static int require_constant_value;
81static int require_constant_elements;
82
58f9752a 83static bool null_pointer_constant_p (const_tree);
f55ade6e 84static tree qualify_type (tree, tree);
dc5027f4
JM
85static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
86 bool *);
744aa42f 87static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
88static int function_types_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 91static tree lookup_field (tree, tree);
81e5eca8
MP
92static int convert_arguments (location_t, vec<location_t>, tree,
93 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
94 tree);
db3927fb 95static tree pointer_diff (location_t, tree, tree);
68fca595 96static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 97 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
98static tree valid_compound_expr_initializer (tree, tree);
99static void push_string (const char *);
100static void push_member_name (tree);
f55ade6e
AJ
101static int spelling_length (void);
102static char *print_spelling (char *);
683d6ff9 103static void warning_init (int, const char *);
c2255bc4 104static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
a1e3b3d9
LB
105static void output_init_element (tree, tree, bool, tree, tree, int, bool,
106 struct obstack *);
107static void output_pending_init_elements (int, struct obstack *);
108static int set_designator (int, struct obstack *);
109static void push_range_stack (tree, struct obstack *);
110static void add_pending_init (tree, tree, tree, bool, struct obstack *);
111static void set_nonincremental_init (struct obstack *);
112static void set_nonincremental_init_from_string (tree, struct obstack *);
113static tree find_init_member (tree, struct obstack *);
f37acdf9 114static void readonly_warning (tree, enum lvalue_use);
7bd11157 115static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 116static void record_maybe_used_decl (tree);
dc5027f4 117static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
118\f
119/* Return true if EXP is a null pointer constant, false otherwise. */
120
121static bool
58f9752a 122null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
123{
124 /* This should really operate on c_expr structures, but they aren't
125 yet available everywhere required. */
126 tree type = TREE_TYPE (expr);
127 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 128 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
129 && integer_zerop (expr)
130 && (INTEGRAL_TYPE_P (type)
131 || (TREE_CODE (type) == POINTER_TYPE
132 && VOID_TYPE_P (TREE_TYPE (type))
133 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
134}
928c19bb
JM
135
136/* EXPR may appear in an unevaluated part of an integer constant
137 expression, but not in an evaluated part. Wrap it in a
138 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
139 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
140
141static tree
142note_integer_operands (tree expr)
143{
144 tree ret;
145 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
146 {
147 ret = copy_node (expr);
148 TREE_OVERFLOW (ret) = 1;
149 }
150 else
151 {
152 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
153 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
154 }
155 return ret;
156}
157
4d84fe7c
JM
158/* Having checked whether EXPR may appear in an unevaluated part of an
159 integer constant expression and found that it may, remove any
160 C_MAYBE_CONST_EXPR noting this fact and return the resulting
161 expression. */
162
163static inline tree
164remove_c_maybe_const_expr (tree expr)
165{
166 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
167 return C_MAYBE_CONST_EXPR_EXPR (expr);
168 else
169 return expr;
170}
171
f13c9b2c
AP
172\f/* This is a cache to hold if two types are compatible or not. */
173
174struct tagged_tu_seen_cache {
175 const struct tagged_tu_seen_cache * next;
58f9752a
KG
176 const_tree t1;
177 const_tree t2;
f13c9b2c
AP
178 /* The return value of tagged_types_tu_compatible_p if we had seen
179 these two types already. */
180 int val;
181};
182
183static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
184static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
185
400fbf9f
JW
186/* Do `exp = require_complete_type (exp);' to make sure exp
187 does not have an incomplete type. (That includes void types.) */
188
189tree
2f6e4e97 190require_complete_type (tree value)
400fbf9f
JW
191{
192 tree type = TREE_TYPE (value);
193
c3d5c3fa 194 if (value == error_mark_node || type == error_mark_node)
ea0f786b
CB
195 return error_mark_node;
196
400fbf9f 197 /* First, detect a valid value with a complete type. */
d0f062fb 198 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
199 return value;
200
7a228918 201 c_incomplete_type_error (value, type);
400fbf9f
JW
202 return error_mark_node;
203}
204
205/* Print an error message for invalid use of an incomplete type.
206 VALUE is the expression that was used (or 0 if that isn't known)
207 and TYPE is the type that was invalid. */
208
209void
ac7d7749 210c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 211{
5d5993dd 212 const char *type_code_string;
400fbf9f
JW
213
214 /* Avoid duplicate error message. */
215 if (TREE_CODE (type) == ERROR_MARK)
216 return;
217
218 if (value != 0 && (TREE_CODE (value) == VAR_DECL
219 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 220 error ("%qD has an incomplete type", value);
400fbf9f
JW
221 else
222 {
223 retry:
224 /* We must print an error message. Be clever about what it says. */
225
226 switch (TREE_CODE (type))
227 {
228 case RECORD_TYPE:
ab87f8c8 229 type_code_string = "struct";
400fbf9f
JW
230 break;
231
232 case UNION_TYPE:
ab87f8c8 233 type_code_string = "union";
400fbf9f
JW
234 break;
235
236 case ENUMERAL_TYPE:
ab87f8c8 237 type_code_string = "enum";
400fbf9f
JW
238 break;
239
240 case VOID_TYPE:
241 error ("invalid use of void expression");
242 return;
243
244 case ARRAY_TYPE:
245 if (TYPE_DOMAIN (type))
246 {
fba78abb
RH
247 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
248 {
249 error ("invalid use of flexible array member");
250 return;
251 }
400fbf9f
JW
252 type = TREE_TYPE (type);
253 goto retry;
254 }
255 error ("invalid use of array with unspecified bounds");
256 return;
257
258 default:
366de0ce 259 gcc_unreachable ();
400fbf9f
JW
260 }
261
262 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
263 error ("invalid use of undefined type %<%s %E%>",
264 type_code_string, TYPE_NAME (type));
400fbf9f
JW
265 else
266 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 267 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
268 }
269}
270
ab393bf1
NB
271/* Given a type, apply default promotions wrt unnamed function
272 arguments and return the new type. */
273
274tree
2f6e4e97 275c_type_promotes_to (tree type)
ab393bf1 276{
267bac10 277 tree ret = NULL_TREE;
ab393bf1 278
267bac10
JM
279 if (TYPE_MAIN_VARIANT (type) == float_type_node)
280 ret = double_type_node;
281 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
282 {
283 /* Preserve unsignedness if not really getting any wider. */
8df83eae 284 if (TYPE_UNSIGNED (type)
c22cacf3 285 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
286 ret = unsigned_type_node;
287 else
288 ret = integer_type_node;
ab393bf1
NB
289 }
290
267bac10
JM
291 if (ret != NULL_TREE)
292 return (TYPE_ATOMIC (type)
293 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
294 : ret);
295
ab393bf1
NB
296 return type;
297}
298
36c5e70a
BE
299/* Return true if between two named address spaces, whether there is a superset
300 named address space that encompasses both address spaces. If there is a
301 superset, return which address space is the superset. */
302
303static bool
304addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
305{
306 if (as1 == as2)
307 {
308 *common = as1;
309 return true;
310 }
311 else if (targetm.addr_space.subset_p (as1, as2))
312 {
313 *common = as2;
314 return true;
315 }
316 else if (targetm.addr_space.subset_p (as2, as1))
317 {
318 *common = as1;
319 return true;
320 }
321 else
322 return false;
323}
324
400fbf9f
JW
325/* Return a variant of TYPE which has all the type qualifiers of LIKE
326 as well as those of TYPE. */
327
328static tree
2f6e4e97 329qualify_type (tree type, tree like)
400fbf9f 330{
36c5e70a
BE
331 addr_space_t as_type = TYPE_ADDR_SPACE (type);
332 addr_space_t as_like = TYPE_ADDR_SPACE (like);
333 addr_space_t as_common;
334
335 /* If the two named address spaces are different, determine the common
336 superset address space. If there isn't one, raise an error. */
337 if (!addr_space_superset (as_type, as_like, &as_common))
338 {
339 as_common = as_type;
340 error ("%qT and %qT are in disjoint named address spaces",
341 type, like);
342 }
343
2f6e4e97 344 return c_build_qualified_type (type,
36c5e70a 345 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 346 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 347 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 348}
52ffd86e
MS
349
350/* Return true iff the given tree T is a variable length array. */
351
352bool
ac7d7749 353c_vla_type_p (const_tree t)
52ffd86e
MS
354{
355 if (TREE_CODE (t) == ARRAY_TYPE
356 && C_TYPE_VARIABLE_SIZE (t))
357 return true;
358 return false;
359}
400fbf9f 360\f
10bc1b1b 361/* Return the composite type of two compatible types.
5305f6d7 362
10bc1b1b
JM
363 We assume that comptypes has already been done and returned
364 nonzero; if that isn't so, this may crash. In particular, we
365 assume that qualifiers match. */
400fbf9f
JW
366
367tree
10bc1b1b 368composite_type (tree t1, tree t2)
400fbf9f 369{
b3694847
SS
370 enum tree_code code1;
371 enum tree_code code2;
4b027d16 372 tree attributes;
400fbf9f
JW
373
374 /* Save time if the two types are the same. */
375
376 if (t1 == t2) return t1;
377
378 /* If one type is nonsense, use the other. */
379 if (t1 == error_mark_node)
380 return t2;
381 if (t2 == error_mark_node)
382 return t1;
383
10bc1b1b
JM
384 code1 = TREE_CODE (t1);
385 code2 = TREE_CODE (t2);
386
d9525bec 387 /* Merge the attributes. */
5fd9b178 388 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 389
10bc1b1b
JM
390 /* If one is an enumerated type and the other is the compatible
391 integer type, the composite type might be either of the two
392 (DR#013 question 3). For consistency, use the enumerated type as
393 the composite type. */
400fbf9f 394
10bc1b1b
JM
395 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
396 return t1;
397 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
398 return t2;
75326e8c 399
366de0ce 400 gcc_assert (code1 == code2);
b6a10c9f 401
400fbf9f
JW
402 switch (code1)
403 {
400fbf9f 404 case POINTER_TYPE:
10bc1b1b 405 /* For two pointers, do this recursively on the target type. */
400fbf9f 406 {
3932261a
MM
407 tree pointed_to_1 = TREE_TYPE (t1);
408 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 409 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 410 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
411 t1 = build_type_attribute_variant (t1, attributes);
412 return qualify_type (t1, t2);
400fbf9f 413 }
400fbf9f
JW
414
415 case ARRAY_TYPE:
416 {
10bc1b1b 417 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
418 int quals;
419 tree unqual_elt;
ca8bdb78
JM
420 tree d1 = TYPE_DOMAIN (t1);
421 tree d2 = TYPE_DOMAIN (t2);
422 bool d1_variable, d2_variable;
423 bool d1_zero, d2_zero;
f6294de7 424 bool t1_complete, t2_complete;
46df2823 425
de46b2fe 426 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
427 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
428 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 429
f6294de7
JM
430 t1_complete = COMPLETE_TYPE_P (t1);
431 t2_complete = COMPLETE_TYPE_P (t2);
432
ca8bdb78
JM
433 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
434 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
435
436 d1_variable = (!d1_zero
437 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
438 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
439 d2_variable = (!d2_zero
440 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
441 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
442 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
443 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 444
400fbf9f 445 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
446 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
447 && (d2_variable || d2_zero || !d1_variable))
4b027d16 448 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
449 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
450 && (d1_variable || d1_zero || !d2_variable))
4b027d16 451 return build_type_attribute_variant (t2, attributes);
c22cacf3 452
de46b2fe
AP
453 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
454 return build_type_attribute_variant (t1, attributes);
455 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
456 return build_type_attribute_variant (t2, attributes);
c22cacf3 457
46df2823
JM
458 /* Merge the element types, and have a size if either arg has
459 one. We may have qualifiers on the element types. To set
460 up TYPE_MAIN_VARIANT correctly, we need to form the
461 composite of the unqualified types and add the qualifiers
462 back at the end. */
463 quals = TYPE_QUALS (strip_array_types (elt));
464 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
465 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
466 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
467 && (d2_variable
468 || d2_zero
469 || !d1_variable))
470 ? t1
471 : t2));
f6294de7
JM
472 /* Ensure a composite type involving a zero-length array type
473 is a zero-length type not an incomplete type. */
474 if (d1_zero && d2_zero
475 && (t1_complete || t2_complete)
476 && !COMPLETE_TYPE_P (t1))
477 {
478 TYPE_SIZE (t1) = bitsize_zero_node;
479 TYPE_SIZE_UNIT (t1) = size_zero_node;
480 }
46df2823 481 t1 = c_build_qualified_type (t1, quals);
de46b2fe 482 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
483 }
484
fcb99e7b
JJ
485 case ENUMERAL_TYPE:
486 case RECORD_TYPE:
487 case UNION_TYPE:
488 if (attributes != NULL)
489 {
490 /* Try harder not to create a new aggregate type. */
491 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
492 return t1;
493 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
494 return t2;
495 }
496 return build_type_attribute_variant (t1, attributes);
497
400fbf9f
JW
498 case FUNCTION_TYPE:
499 /* Function types: prefer the one that specified arg types.
500 If both do, merge the arg types. Also merge the return types. */
501 {
10bc1b1b 502 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
503 tree p1 = TYPE_ARG_TYPES (t1);
504 tree p2 = TYPE_ARG_TYPES (t2);
505 int len;
506 tree newargs, n;
507 int i;
508
509 /* Save space: see if the result is identical to one of the args. */
3f75a254 510 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 511 return build_type_attribute_variant (t1, attributes);
3f75a254 512 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 513 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
514
515 /* Simple way if one arg fails to specify argument types. */
516 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 517 {
fe7080d2
AP
518 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
519 t1 = build_type_attribute_variant (t1, attributes);
520 return qualify_type (t1, t2);
4b027d16 521 }
400fbf9f 522 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
523 {
524 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
525 t1 = build_type_attribute_variant (t1, attributes);
526 return qualify_type (t1, t2);
4b027d16 527 }
400fbf9f
JW
528
529 /* If both args specify argument types, we must merge the two
530 lists, argument by argument. */
2f4e8f2b 531
400fbf9f
JW
532 len = list_length (p1);
533 newargs = 0;
534
535 for (i = 0; i < len; i++)
8d9bfdc5 536 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
537
538 n = newargs;
539
540 for (; p1;
541 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
542 {
543 /* A null type means arg type is not specified.
544 Take whatever the other function type has. */
545 if (TREE_VALUE (p1) == 0)
546 {
547 TREE_VALUE (n) = TREE_VALUE (p2);
548 goto parm_done;
549 }
550 if (TREE_VALUE (p2) == 0)
551 {
552 TREE_VALUE (n) = TREE_VALUE (p1);
553 goto parm_done;
554 }
2f6e4e97 555
400fbf9f
JW
556 /* Given wait (union {union wait *u; int *i} *)
557 and wait (union wait *),
558 prefer union wait * as type of parm. */
559 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
560 && TREE_VALUE (p1) != TREE_VALUE (p2))
561 {
562 tree memb;
58cb41e6
JJ
563 tree mv2 = TREE_VALUE (p2);
564 if (mv2 && mv2 != error_mark_node
565 && TREE_CODE (mv2) != ARRAY_TYPE)
566 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 567 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 568 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
569 {
570 tree mv3 = TREE_TYPE (memb);
571 if (mv3 && mv3 != error_mark_node
572 && TREE_CODE (mv3) != ARRAY_TYPE)
573 mv3 = TYPE_MAIN_VARIANT (mv3);
574 if (comptypes (mv3, mv2))
575 {
576 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
577 TREE_VALUE (p2));
c1771a20 578 pedwarn (input_location, OPT_Wpedantic,
fcf73884 579 "function types not truly compatible in ISO C");
58cb41e6
JJ
580 goto parm_done;
581 }
582 }
400fbf9f
JW
583 }
584 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
585 && TREE_VALUE (p2) != TREE_VALUE (p1))
586 {
587 tree memb;
58cb41e6
JJ
588 tree mv1 = TREE_VALUE (p1);
589 if (mv1 && mv1 != error_mark_node
590 && TREE_CODE (mv1) != ARRAY_TYPE)
591 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 592 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 593 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
594 {
595 tree mv3 = TREE_TYPE (memb);
596 if (mv3 && mv3 != error_mark_node
597 && TREE_CODE (mv3) != ARRAY_TYPE)
598 mv3 = TYPE_MAIN_VARIANT (mv3);
599 if (comptypes (mv3, mv1))
600 {
601 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
602 TREE_VALUE (p1));
c1771a20 603 pedwarn (input_location, OPT_Wpedantic,
fcf73884 604 "function types not truly compatible in ISO C");
58cb41e6
JJ
605 goto parm_done;
606 }
607 }
400fbf9f 608 }
10bc1b1b 609 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
610 parm_done: ;
611 }
612
4b027d16 613 t1 = build_function_type (valtype, newargs);
fe7080d2 614 t1 = qualify_type (t1, t2);
0f41302f 615 /* ... falls through ... */
400fbf9f
JW
616 }
617
618 default:
4b027d16 619 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
620 }
621
622}
10bc1b1b
JM
623
624/* Return the type of a conditional expression between pointers to
625 possibly differently qualified versions of compatible types.
626
627 We assume that comp_target_types has already been done and returned
628 nonzero; if that isn't so, this may crash. */
629
630static tree
631common_pointer_type (tree t1, tree t2)
632{
633 tree attributes;
46df2823
JM
634 tree pointed_to_1, mv1;
635 tree pointed_to_2, mv2;
10bc1b1b 636 tree target;
eb1387a0 637 unsigned target_quals;
36c5e70a
BE
638 addr_space_t as1, as2, as_common;
639 int quals1, quals2;
10bc1b1b
JM
640
641 /* Save time if the two types are the same. */
642
643 if (t1 == t2) return t1;
644
645 /* If one type is nonsense, use the other. */
646 if (t1 == error_mark_node)
647 return t2;
648 if (t2 == error_mark_node)
649 return t1;
650
366de0ce 651 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 652 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
653
654 /* Merge the attributes. */
655 attributes = targetm.merge_type_attributes (t1, t2);
656
657 /* Find the composite type of the target types, and combine the
46df2823
JM
658 qualifiers of the two types' targets. Do not lose qualifiers on
659 array element types by taking the TYPE_MAIN_VARIANT. */
660 mv1 = pointed_to_1 = TREE_TYPE (t1);
661 mv2 = pointed_to_2 = TREE_TYPE (t2);
662 if (TREE_CODE (mv1) != ARRAY_TYPE)
663 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
664 if (TREE_CODE (mv2) != ARRAY_TYPE)
665 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
666 target = composite_type (mv1, mv2);
eb1387a0
RG
667
668 /* For function types do not merge const qualifiers, but drop them
669 if used inconsistently. The middle-end uses these to mark const
670 and noreturn functions. */
36c5e70a
BE
671 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
672 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
673
eb1387a0 674 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 675 target_quals = (quals1 & quals2);
eb1387a0 676 else
36c5e70a
BE
677 target_quals = (quals1 | quals2);
678
679 /* If the two named address spaces are different, determine the common
680 superset address space. This is guaranteed to exist due to the
681 assumption that comp_target_type returned non-zero. */
682 as1 = TYPE_ADDR_SPACE (pointed_to_1);
683 as2 = TYPE_ADDR_SPACE (pointed_to_2);
684 if (!addr_space_superset (as1, as2, &as_common))
685 gcc_unreachable ();
686
687 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
688
eb1387a0 689 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
690 return build_type_attribute_variant (t1, attributes);
691}
692
693/* Return the common type for two arithmetic types under the usual
694 arithmetic conversions. The default conversions have already been
695 applied, and enumerated types converted to their compatible integer
696 types. The resulting type is unqualified and has no attributes.
697
698 This is the type for the result of most arithmetic operations
699 if the operands have the given two types. */
700
ccf7f880
JJ
701static tree
702c_common_type (tree t1, tree t2)
10bc1b1b
JM
703{
704 enum tree_code code1;
705 enum tree_code code2;
706
707 /* If one type is nonsense, use the other. */
708 if (t1 == error_mark_node)
709 return t2;
710 if (t2 == error_mark_node)
711 return t1;
712
713 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
714 t1 = TYPE_MAIN_VARIANT (t1);
715
716 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
717 t2 = TYPE_MAIN_VARIANT (t2);
718
719 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
720 t1 = build_type_attribute_variant (t1, NULL_TREE);
721
722 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
723 t2 = build_type_attribute_variant (t2, NULL_TREE);
724
725 /* Save time if the two types are the same. */
726
727 if (t1 == t2) return t1;
728
729 code1 = TREE_CODE (t1);
730 code2 = TREE_CODE (t2);
731
366de0ce 732 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
733 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
734 || code1 == INTEGER_TYPE);
366de0ce 735 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
736 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
737 || code2 == INTEGER_TYPE);
10bc1b1b 738
5fc89bfd
JJ
739 /* When one operand is a decimal float type, the other operand cannot be
740 a generic float type or a complex type. We also disallow vector types
741 here. */
742 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
743 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
744 {
745 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
746 {
747 error ("can%'t mix operands of decimal float and vector types");
748 return error_mark_node;
749 }
750 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
751 {
752 error ("can%'t mix operands of decimal float and complex types");
753 return error_mark_node;
754 }
755 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
756 {
757 error ("can%'t mix operands of decimal float and other float types");
758 return error_mark_node;
759 }
760 }
761
10bc1b1b
JM
762 /* If one type is a vector type, return that type. (How the usual
763 arithmetic conversions apply to the vector types extension is not
764 precisely specified.) */
765 if (code1 == VECTOR_TYPE)
766 return t1;
767
768 if (code2 == VECTOR_TYPE)
769 return t2;
770
771 /* If one type is complex, form the common type of the non-complex
772 components, then make that complex. Use T1 or T2 if it is the
773 required type. */
774 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
775 {
776 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
777 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 778 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
779
780 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
781 return t1;
782 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
783 return t2;
784 else
785 return build_complex_type (subtype);
786 }
787
788 /* If only one is real, use it as the result. */
789
790 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
791 return t1;
792
793 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
794 return t2;
795
9a8ce21f
JG
796 /* If both are real and either are decimal floating point types, use
797 the decimal floating point type with the greater precision. */
798
799 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
800 {
801 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
802 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
803 return dfloat128_type_node;
804 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
805 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
806 return dfloat64_type_node;
807 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
808 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
809 return dfloat32_type_node;
810 }
811
ab22c1fa
CF
812 /* Deal with fixed-point types. */
813 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
814 {
815 unsigned int unsignedp = 0, satp = 0;
816 enum machine_mode m1, m2;
817 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
818
819 m1 = TYPE_MODE (t1);
820 m2 = TYPE_MODE (t2);
821
822 /* If one input type is saturating, the result type is saturating. */
823 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
824 satp = 1;
825
826 /* If both fixed-point types are unsigned, the result type is unsigned.
827 When mixing fixed-point and integer types, follow the sign of the
828 fixed-point type.
829 Otherwise, the result type is signed. */
830 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
831 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
832 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t1))
834 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
835 && TYPE_UNSIGNED (t2)))
836 unsignedp = 1;
837
838 /* The result type is signed. */
839 if (unsignedp == 0)
840 {
841 /* If the input type is unsigned, we need to convert to the
842 signed type. */
843 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
844 {
d75d71e0 845 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
846 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
847 mclass = MODE_FRACT;
848 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
849 mclass = MODE_ACCUM;
850 else
851 gcc_unreachable ();
852 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
853 }
854 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
855 {
d75d71e0 856 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
857 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
858 mclass = MODE_FRACT;
859 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
860 mclass = MODE_ACCUM;
861 else
862 gcc_unreachable ();
863 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
864 }
865 }
866
867 if (code1 == FIXED_POINT_TYPE)
868 {
869 fbit1 = GET_MODE_FBIT (m1);
870 ibit1 = GET_MODE_IBIT (m1);
871 }
872 else
873 {
874 fbit1 = 0;
875 /* Signed integers need to subtract one sign bit. */
876 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
877 }
878
879 if (code2 == FIXED_POINT_TYPE)
880 {
881 fbit2 = GET_MODE_FBIT (m2);
882 ibit2 = GET_MODE_IBIT (m2);
883 }
884 else
885 {
886 fbit2 = 0;
887 /* Signed integers need to subtract one sign bit. */
888 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
889 }
890
891 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
892 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
893 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
894 satp);
895 }
896
10bc1b1b
JM
897 /* Both real or both integers; use the one with greater precision. */
898
899 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
900 return t1;
901 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
902 return t2;
903
904 /* Same precision. Prefer long longs to longs to ints when the
905 same precision, following the C99 rules on integer type rank
906 (which are equivalent to the C90 rules for C90 types). */
907
908 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
910 return long_long_unsigned_type_node;
911
912 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
914 {
915 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
916 return long_long_unsigned_type_node;
917 else
c22cacf3 918 return long_long_integer_type_node;
10bc1b1b
JM
919 }
920
921 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
923 return long_unsigned_type_node;
924
925 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
926 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
927 {
928 /* But preserve unsignedness from the other type,
929 since long cannot hold all the values of an unsigned int. */
930 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
931 return long_unsigned_type_node;
932 else
933 return long_integer_type_node;
934 }
935
936 /* Likewise, prefer long double to double even if same size. */
937 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
938 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
939 return long_double_type_node;
940
2531a1d9
JR
941 /* Likewise, prefer double to float even if same size.
942 We got a couple of embedded targets with 32 bit doubles, and the
943 pdp11 might have 64 bit floats. */
944 if (TYPE_MAIN_VARIANT (t1) == double_type_node
945 || TYPE_MAIN_VARIANT (t2) == double_type_node)
946 return double_type_node;
947
10bc1b1b
JM
948 /* Otherwise prefer the unsigned one. */
949
950 if (TYPE_UNSIGNED (t1))
951 return t1;
952 else
953 return t2;
954}
400fbf9f 955\f
5922c215
JM
956/* Wrapper around c_common_type that is used by c-common.c and other
957 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
958 are allowed here and are converted to their compatible integer types.
959 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
960 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
961tree
962common_type (tree t1, tree t2)
963{
964 if (TREE_CODE (t1) == ENUMERAL_TYPE)
965 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
966 if (TREE_CODE (t2) == ENUMERAL_TYPE)
967 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
968
969 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
970 if (TREE_CODE (t1) == BOOLEAN_TYPE
971 && TREE_CODE (t2) == BOOLEAN_TYPE)
972 return boolean_type_node;
973
974 /* If either type is BOOLEAN_TYPE, then return the other. */
975 if (TREE_CODE (t1) == BOOLEAN_TYPE)
976 return t2;
977 if (TREE_CODE (t2) == BOOLEAN_TYPE)
978 return t1;
979
ccf7f880
JJ
980 return c_common_type (t1, t2);
981}
f13c9b2c 982
400fbf9f
JW
983/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
984 or various other operations. Return 2 if they are compatible
985 but a warning may be needed if you use them together. */
986
987int
132da1a5 988comptypes (tree type1, tree type2)
f13c9b2c
AP
989{
990 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
991 int val;
992
dc5027f4 993 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
994 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
995
996 return val;
997}
998
999/* Like comptypes, but if it returns non-zero because enum and int are
1000 compatible, it sets *ENUM_AND_INT_P to true. */
1001
1002static int
1003comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1004{
1005 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1006 int val;
1007
dc5027f4
JM
1008 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1009 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1010
1011 return val;
1012}
1013
1014/* Like comptypes, but if it returns nonzero for different types, it
1015 sets *DIFFERENT_TYPES_P to true. */
1016
1017int
1018comptypes_check_different_types (tree type1, tree type2,
1019 bool *different_types_p)
1020{
1021 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1022 int val;
1023
1024 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1025 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1026
f13c9b2c 1027 return val;
c22cacf3
MS
1028}
1029\f
f13c9b2c
AP
1030/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1031 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1032 but a warning may be needed if you use them together. If
1033 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1034 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1035 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1036 NULL, and the types are compatible but different enough not to be
48b0b196 1037 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1038 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1039 false, but may or may not be set if the types are incompatible.
1040 This differs from comptypes, in that we don't free the seen
1041 types. */
f13c9b2c
AP
1042
1043static int
dc5027f4
JM
1044comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1045 bool *different_types_p)
400fbf9f 1046{
58f9752a
KG
1047 const_tree t1 = type1;
1048 const_tree t2 = type2;
4b027d16 1049 int attrval, val;
400fbf9f
JW
1050
1051 /* Suppress errors caused by previously reported errors. */
1052
8d47dfc5
RH
1053 if (t1 == t2 || !t1 || !t2
1054 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1055 return 1;
1056
bca63328
JM
1057 /* Enumerated types are compatible with integer types, but this is
1058 not transitive: two enumerated types in the same translation unit
1059 are compatible with each other only if they are the same type. */
400fbf9f 1060
bca63328 1061 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1062 {
1063 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1064 if (TREE_CODE (t2) != VOID_TYPE)
1065 {
1066 if (enum_and_int_p != NULL)
1067 *enum_and_int_p = true;
1068 if (different_types_p != NULL)
1069 *different_types_p = true;
1070 }
744aa42f 1071 }
bca63328 1072 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1073 {
1074 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1075 if (TREE_CODE (t1) != VOID_TYPE)
1076 {
1077 if (enum_and_int_p != NULL)
1078 *enum_and_int_p = true;
1079 if (different_types_p != NULL)
1080 *different_types_p = true;
1081 }
744aa42f 1082 }
400fbf9f
JW
1083
1084 if (t1 == t2)
1085 return 1;
1086
1087 /* Different classes of types can't be compatible. */
1088
3aeb3655
EC
1089 if (TREE_CODE (t1) != TREE_CODE (t2))
1090 return 0;
400fbf9f 1091
118a3a8b 1092 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1093
3932261a 1094 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1095 return 0;
1096
08632da2
RS
1097 /* Allow for two different type nodes which have essentially the same
1098 definition. Note that we already checked for equality of the type
38e01259 1099 qualifiers (just above). */
400fbf9f 1100
46df2823
JM
1101 if (TREE_CODE (t1) != ARRAY_TYPE
1102 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1103 return 1;
1104
4b027d16 1105 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1106 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1107 return 0;
1108
1109 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1110 val = 0;
1111
400fbf9f
JW
1112 switch (TREE_CODE (t1))
1113 {
1114 case POINTER_TYPE:
106f5de5
UW
1115 /* Do not remove mode or aliasing information. */
1116 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1117 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1118 break;
4b027d16 1119 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1120 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1121 enum_and_int_p, different_types_p));
4b027d16 1122 break;
400fbf9f
JW
1123
1124 case FUNCTION_TYPE:
dc5027f4
JM
1125 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1126 different_types_p);
4b027d16 1127 break;
400fbf9f
JW
1128
1129 case ARRAY_TYPE:
1130 {
400fbf9f
JW
1131 tree d1 = TYPE_DOMAIN (t1);
1132 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1133 bool d1_variable, d2_variable;
1134 bool d1_zero, d2_zero;
4b027d16 1135 val = 1;
400fbf9f
JW
1136
1137 /* Target types must match incl. qualifiers. */
1138 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1139 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1140 enum_and_int_p,
1141 different_types_p)))
400fbf9f
JW
1142 return 0;
1143
dc5027f4
JM
1144 if (different_types_p != NULL
1145 && (d1 == 0) != (d2 == 0))
1146 *different_types_p = true;
400fbf9f 1147 /* Sizes must match unless one is missing or variable. */
3f85558f 1148 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1149 break;
400fbf9f 1150
3f75a254
JM
1151 d1_zero = !TYPE_MAX_VALUE (d1);
1152 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1153
3f75a254 1154 d1_variable = (!d1_zero
3f85558f
RH
1155 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1156 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1157 d2_variable = (!d2_zero
3f85558f
RH
1158 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1159 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1160 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1161 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1162
dc5027f4
JM
1163 if (different_types_p != NULL
1164 && d1_variable != d2_variable)
1165 *different_types_p = true;
3f85558f
RH
1166 if (d1_variable || d2_variable)
1167 break;
1168 if (d1_zero && d2_zero)
1169 break;
1170 if (d1_zero || d2_zero
3f75a254
JM
1171 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1172 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1173 val = 0;
1174
c22cacf3 1175 break;
400fbf9f
JW
1176 }
1177
d1bd0ded 1178 case ENUMERAL_TYPE:
58393038 1179 case RECORD_TYPE:
d1bd0ded 1180 case UNION_TYPE:
766beae1 1181 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1182 {
fcb99e7b
JJ
1183 tree a1 = TYPE_ATTRIBUTES (t1);
1184 tree a2 = TYPE_ATTRIBUTES (t2);
1185
1186 if (! attribute_list_contained (a1, a2)
1187 && ! attribute_list_contained (a2, a1))
1188 break;
1189
f13c9b2c 1190 if (attrval != 2)
dc5027f4
JM
1191 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1192 different_types_p);
1193 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1194 different_types_p);
f13c9b2c 1195 }
4b027d16 1196 break;
e9a25f70 1197
62e1dfcf 1198 case VECTOR_TYPE:
744aa42f
ILT
1199 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1200 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1201 enum_and_int_p, different_types_p));
62e1dfcf
NC
1202 break;
1203
e9a25f70
JL
1204 default:
1205 break;
400fbf9f 1206 }
4b027d16 1207 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1208}
1209
36c5e70a
BE
1210/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1211 their qualifiers, except for named address spaces. If the pointers point to
1212 different named addresses, then we must determine if one address space is a
1213 subset of the other. */
400fbf9f
JW
1214
1215static int
744aa42f 1216comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1217{
392202b0 1218 int val;
36c5e70a
BE
1219 tree mvl = TREE_TYPE (ttl);
1220 tree mvr = TREE_TYPE (ttr);
1221 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1222 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1223 addr_space_t as_common;
744aa42f 1224 bool enum_and_int_p;
8b40563c 1225
36c5e70a
BE
1226 /* Fail if pointers point to incompatible address spaces. */
1227 if (!addr_space_superset (asl, asr, &as_common))
1228 return 0;
1229
46df2823
JM
1230 /* Do not lose qualifiers on element types of array types that are
1231 pointer targets by taking their TYPE_MAIN_VARIANT. */
46df2823 1232 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
1233 mvl = (TYPE_ATOMIC (mvl)
1234 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1235 : TYPE_MAIN_VARIANT (mvl));
46df2823 1236 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
1237 mvr = (TYPE_ATOMIC (mvr)
1238 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1239 : TYPE_MAIN_VARIANT (mvr));
744aa42f
ILT
1240 enum_and_int_p = false;
1241 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1242
fcf73884 1243 if (val == 2)
c1771a20 1244 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1245
1246 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1247 warning_at (location, OPT_Wc___compat,
1248 "pointer target types incompatible in C++");
1249
400fbf9f
JW
1250 return val;
1251}
1252\f
1253/* Subroutines of `comptypes'. */
1254
f75fbaf7
ZW
1255/* Determine whether two trees derive from the same translation unit.
1256 If the CONTEXT chain ends in a null, that tree's context is still
1257 being parsed, so if two trees have context chains ending in null,
766beae1 1258 they're in the same translation unit. */
f75fbaf7 1259int
58f9752a 1260same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1261{
1262 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1263 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1264 {
6615c446
JO
1265 case tcc_declaration:
1266 t1 = DECL_CONTEXT (t1); break;
1267 case tcc_type:
1268 t1 = TYPE_CONTEXT (t1); break;
1269 case tcc_exceptional:
1270 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1271 default: gcc_unreachable ();
766beae1
ZW
1272 }
1273
1274 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1275 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1276 {
6615c446
JO
1277 case tcc_declaration:
1278 t2 = DECL_CONTEXT (t2); break;
1279 case tcc_type:
1280 t2 = TYPE_CONTEXT (t2); break;
1281 case tcc_exceptional:
1282 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1283 default: gcc_unreachable ();
766beae1
ZW
1284 }
1285
1286 return t1 == t2;
1287}
1288
f13c9b2c 1289/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1290
f13c9b2c 1291static struct tagged_tu_seen_cache *
58f9752a 1292alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1293{
cceb1885 1294 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1295 tu->next = tagged_tu_seen_base;
1296 tu->t1 = t1;
1297 tu->t2 = t2;
c22cacf3 1298
f13c9b2c 1299 tagged_tu_seen_base = tu;
c22cacf3 1300
f13c9b2c
AP
1301 /* The C standard says that two structures in different translation
1302 units are compatible with each other only if the types of their
1303 fields are compatible (among other things). We assume that they
1304 are compatible until proven otherwise when building the cache.
1305 An example where this can occur is:
1306 struct a
1307 {
1308 struct a *next;
1309 };
1310 If we are comparing this against a similar struct in another TU,
c83eecad 1311 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1312 loop. */
1313 tu->val = 1;
1314 return tu;
1315}
d1bd0ded 1316
f13c9b2c 1317/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1318
f13c9b2c
AP
1319static void
1320free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1321{
1322 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1323 while (tu != tu_til)
1324 {
741ac903
KG
1325 const struct tagged_tu_seen_cache *const tu1
1326 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1327 tu = tu1->next;
b1d5455a 1328 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1329 }
1330 tagged_tu_seen_base = tu_til;
1331}
d1bd0ded
GK
1332
1333/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1334 compatible. If the two types are not the same (which has been
1335 checked earlier), this can only happen when multiple translation
1336 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1337 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1338 comptypes_internal. */
d1bd0ded
GK
1339
1340static int
744aa42f 1341tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1342 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1343{
1344 tree s1, s2;
1345 bool needs_warning = false;
3aeb3655 1346
d1bd0ded
GK
1347 /* We have to verify that the tags of the types are the same. This
1348 is harder than it looks because this may be a typedef, so we have
1349 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1350 typedef...
1351 In the case of compiler-created builtin structs the TYPE_DECL
1352 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1353 while (TYPE_NAME (t1)
1354 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1355 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1356 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1357
dea984dc
ILT
1358 while (TYPE_NAME (t2)
1359 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1360 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1361 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1362
1363 /* C90 didn't have the requirement that the two tags be the same. */
1364 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1365 return 0;
3aeb3655 1366
d1bd0ded
GK
1367 /* C90 didn't say what happened if one or both of the types were
1368 incomplete; we choose to follow C99 rules here, which is that they
1369 are compatible. */
1370 if (TYPE_SIZE (t1) == NULL
1371 || TYPE_SIZE (t2) == NULL)
1372 return 1;
3aeb3655 1373
d1bd0ded 1374 {
f13c9b2c 1375 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1376 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1377 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1378 return tts_i->val;
d1bd0ded 1379 }
3aeb3655 1380
d1bd0ded
GK
1381 switch (TREE_CODE (t1))
1382 {
1383 case ENUMERAL_TYPE:
1384 {
f13c9b2c 1385 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1386 /* Speed up the case where the type values are in the same order. */
1387 tree tv1 = TYPE_VALUES (t1);
1388 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1389
c22cacf3 1390 if (tv1 == tv2)
f13c9b2c
AP
1391 {
1392 return 1;
1393 }
3aeb3655 1394
c22cacf3
MS
1395 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1396 {
1397 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1398 break;
1399 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1400 {
c22cacf3 1401 tu->val = 0;
f13c9b2c
AP
1402 return 0;
1403 }
c22cacf3 1404 }
3aeb3655 1405
c22cacf3 1406 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1407 {
1408 return 1;
1409 }
c22cacf3 1410 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1411 {
1412 tu->val = 0;
1413 return 0;
1414 }
3aeb3655 1415
d1bd0ded 1416 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1417 {
1418 tu->val = 0;
1419 return 0;
1420 }
3aeb3655 1421
d1bd0ded
GK
1422 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1423 {
1424 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1425 if (s2 == NULL
1426 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1427 {
1428 tu->val = 0;
1429 return 0;
1430 }
d1bd0ded
GK
1431 }
1432 return 1;
1433 }
1434
1435 case UNION_TYPE:
1436 {
f13c9b2c 1437 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1438 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1439 {
1440 tu->val = 0;
1441 return 0;
1442 }
c22cacf3 1443
f13c9b2c
AP
1444 /* Speed up the common case where the fields are in the same order. */
1445 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1446 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1447 {
1448 int result;
c22cacf3 1449
3ae4d3cc 1450 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1451 break;
744aa42f 1452 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1453 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1454
1455 if (result != 1 && !DECL_NAME (s1))
1456 break;
f13c9b2c
AP
1457 if (result == 0)
1458 {
1459 tu->val = 0;
1460 return 0;
1461 }
1462 if (result == 2)
1463 needs_warning = true;
1464
1465 if (TREE_CODE (s1) == FIELD_DECL
1466 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1467 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1468 {
1469 tu->val = 0;
1470 return 0;
1471 }
1472 }
1473 if (!s1 && !s2)
1474 {
1475 tu->val = needs_warning ? 2 : 1;
1476 return tu->val;
1477 }
d1bd0ded 1478
910ad8de 1479 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1480 {
1481 bool ok = false;
3aeb3655 1482
910ad8de 1483 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1484 if (DECL_NAME (s1) == DECL_NAME (s2))
1485 {
1486 int result;
1487
744aa42f 1488 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1489 enum_and_int_p,
1490 different_types_p);
3ae4d3cc
AO
1491
1492 if (result != 1 && !DECL_NAME (s1))
1493 continue;
1494 if (result == 0)
1495 {
1496 tu->val = 0;
1497 return 0;
1498 }
1499 if (result == 2)
1500 needs_warning = true;
1501
1502 if (TREE_CODE (s1) == FIELD_DECL
1503 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1504 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1505 break;
3ae4d3cc
AO
1506
1507 ok = true;
1508 break;
1509 }
3f75a254 1510 if (!ok)
f13c9b2c
AP
1511 {
1512 tu->val = 0;
1513 return 0;
1514 }
d1bd0ded 1515 }
f13c9b2c
AP
1516 tu->val = needs_warning ? 2 : 10;
1517 return tu->val;
d1bd0ded
GK
1518 }
1519
1520 case RECORD_TYPE:
1521 {
c22cacf3 1522 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1523
1524 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1525 s1 && s2;
910ad8de 1526 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1527 {
1528 int result;
1529 if (TREE_CODE (s1) != TREE_CODE (s2)
1530 || DECL_NAME (s1) != DECL_NAME (s2))
1531 break;
744aa42f 1532 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1533 enum_and_int_p, different_types_p);
d1bd0ded
GK
1534 if (result == 0)
1535 break;
1536 if (result == 2)
1537 needs_warning = true;
3aeb3655 1538
d1bd0ded
GK
1539 if (TREE_CODE (s1) == FIELD_DECL
1540 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1541 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1542 break;
1543 }
d1bd0ded 1544 if (s1 && s2)
f13c9b2c
AP
1545 tu->val = 0;
1546 else
1547 tu->val = needs_warning ? 2 : 1;
1548 return tu->val;
d1bd0ded
GK
1549 }
1550
1551 default:
366de0ce 1552 gcc_unreachable ();
d1bd0ded
GK
1553 }
1554}
1555
400fbf9f
JW
1556/* Return 1 if two function types F1 and F2 are compatible.
1557 If either type specifies no argument types,
1558 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1559 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1560 the other must specify that number of self-promoting arg types.
744aa42f 1561 Otherwise, the argument types must match.
dc5027f4 1562 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1563
1564static int
744aa42f 1565function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1566 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1567{
1568 tree args1, args2;
1569 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1570 int val = 1;
1571 int val1;
a6fdc086
GK
1572 tree ret1, ret2;
1573
1574 ret1 = TREE_TYPE (f1);
1575 ret2 = TREE_TYPE (f2);
1576
e508a019
JM
1577 /* 'volatile' qualifiers on a function's return type used to mean
1578 the function is noreturn. */
1579 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1580 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1581 if (TYPE_VOLATILE (ret1))
1582 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1583 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1584 if (TYPE_VOLATILE (ret2))
1585 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1586 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1587 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1588 if (val == 0)
400fbf9f
JW
1589 return 0;
1590
1591 args1 = TYPE_ARG_TYPES (f1);
1592 args2 = TYPE_ARG_TYPES (f2);
1593
dc5027f4
JM
1594 if (different_types_p != NULL
1595 && (args1 == 0) != (args2 == 0))
1596 *different_types_p = true;
1597
400fbf9f
JW
1598 /* An unspecified parmlist matches any specified parmlist
1599 whose argument types don't need default promotions. */
1600
1601 if (args1 == 0)
1602 {
1603 if (!self_promoting_args_p (args2))
1604 return 0;
1605 /* If one of these types comes from a non-prototype fn definition,
1606 compare that with the other type's arglist.
3176a0c2 1607 If they don't match, ask for a warning (but no error). */
400fbf9f 1608 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1609 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1610 enum_and_int_p, different_types_p))
400fbf9f
JW
1611 val = 2;
1612 return val;
1613 }
1614 if (args2 == 0)
1615 {
1616 if (!self_promoting_args_p (args1))
1617 return 0;
1618 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1619 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1620 enum_and_int_p, different_types_p))
400fbf9f
JW
1621 val = 2;
1622 return val;
1623 }
1624
1625 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1626 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1627 different_types_p);
400fbf9f
JW
1628 return val1 != 1 ? val1 : val;
1629}
1630
744aa42f
ILT
1631/* Check two lists of types for compatibility, returning 0 for
1632 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1633 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1634 comptypes_internal. */
400fbf9f
JW
1635
1636static int
744aa42f 1637type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1638 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1639{
1640 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1641 int val = 1;
9d5f3e49 1642 int newval = 0;
400fbf9f
JW
1643
1644 while (1)
1645 {
46df2823 1646 tree a1, mv1, a2, mv2;
400fbf9f
JW
1647 if (args1 == 0 && args2 == 0)
1648 return val;
1649 /* If one list is shorter than the other,
1650 they fail to match. */
1651 if (args1 == 0 || args2 == 0)
1652 return 0;
46df2823
JM
1653 mv1 = a1 = TREE_VALUE (args1);
1654 mv2 = a2 = TREE_VALUE (args2);
1655 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1656 mv1 = (TYPE_ATOMIC (mv1)
1657 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1658 TYPE_QUAL_ATOMIC)
1659 : TYPE_MAIN_VARIANT (mv1));
46df2823 1660 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1661 mv2 = (TYPE_ATOMIC (mv2)
1662 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1663 TYPE_QUAL_ATOMIC)
1664 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1665 /* A null pointer instead of a type
1666 means there is supposed to be an argument
1667 but nothing is specified about what type it has.
1668 So match anything that self-promotes. */
dc5027f4
JM
1669 if (different_types_p != NULL
1670 && (a1 == 0) != (a2 == 0))
1671 *different_types_p = true;
46df2823 1672 if (a1 == 0)
400fbf9f 1673 {
46df2823 1674 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1675 return 0;
1676 }
46df2823 1677 else if (a2 == 0)
400fbf9f 1678 {
46df2823 1679 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1680 return 0;
1681 }
8f5b6d29 1682 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1683 else if (TREE_CODE (a1) == ERROR_MARK
1684 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1685 ;
dc5027f4
JM
1686 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1687 different_types_p)))
400fbf9f 1688 {
dc5027f4
JM
1689 if (different_types_p != NULL)
1690 *different_types_p = true;
400fbf9f
JW
1691 /* Allow wait (union {union wait *u; int *i} *)
1692 and wait (union wait *) to be compatible. */
46df2823
JM
1693 if (TREE_CODE (a1) == UNION_TYPE
1694 && (TYPE_NAME (a1) == 0
ebf0bf7f 1695 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1696 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1697 && tree_int_cst_equal (TYPE_SIZE (a1),
1698 TYPE_SIZE (a2)))
400fbf9f
JW
1699 {
1700 tree memb;
46df2823 1701 for (memb = TYPE_FIELDS (a1);
910ad8de 1702 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1703 {
1704 tree mv3 = TREE_TYPE (memb);
1705 if (mv3 && mv3 != error_mark_node
1706 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1707 mv3 = (TYPE_ATOMIC (mv3)
1708 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1709 TYPE_QUAL_ATOMIC)
1710 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1711 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1712 different_types_p))
58cb41e6
JJ
1713 break;
1714 }
400fbf9f
JW
1715 if (memb == 0)
1716 return 0;
1717 }
46df2823
JM
1718 else if (TREE_CODE (a2) == UNION_TYPE
1719 && (TYPE_NAME (a2) == 0
ebf0bf7f 1720 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1721 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1722 && tree_int_cst_equal (TYPE_SIZE (a2),
1723 TYPE_SIZE (a1)))
400fbf9f
JW
1724 {
1725 tree memb;
46df2823 1726 for (memb = TYPE_FIELDS (a2);
910ad8de 1727 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1728 {
1729 tree mv3 = TREE_TYPE (memb);
1730 if (mv3 && mv3 != error_mark_node
1731 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1732 mv3 = (TYPE_ATOMIC (mv3)
1733 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1734 TYPE_QUAL_ATOMIC)
1735 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1736 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1737 different_types_p))
58cb41e6
JJ
1738 break;
1739 }
400fbf9f
JW
1740 if (memb == 0)
1741 return 0;
1742 }
1743 else
1744 return 0;
1745 }
1746
1747 /* comptypes said ok, but record if it said to warn. */
1748 if (newval > val)
1749 val = newval;
1750
1751 args1 = TREE_CHAIN (args1);
1752 args2 = TREE_CHAIN (args2);
1753 }
1754}
400fbf9f 1755\f
400fbf9f
JW
1756/* Compute the size to increment a pointer by. */
1757
4e2fb7de 1758static tree
58f9752a 1759c_size_in_bytes (const_tree type)
400fbf9f
JW
1760{
1761 enum tree_code code = TREE_CODE (type);
1762
fed3cef0
RK
1763 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1764 return size_one_node;
1765
d0f062fb 1766 if (!COMPLETE_OR_VOID_TYPE_P (type))
400fbf9f
JW
1767 {
1768 error ("arithmetic on pointer to an incomplete type");
fed3cef0 1769 return size_one_node;
400fbf9f
JW
1770 }
1771
1772 /* Convert in case a char is more than one unit. */
db3927fb
AH
1773 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1774 size_int (TYPE_PRECISION (char_type_node)
1775 / BITS_PER_UNIT));
400fbf9f 1776}
400fbf9f 1777\f
400fbf9f
JW
1778/* Return either DECL or its known constant value (if it has one). */
1779
56cb9733 1780tree
2f6e4e97 1781decl_constant_value (tree decl)
400fbf9f 1782{
a7c1916a 1783 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1784 in a place where a variable is invalid. Note that DECL_INITIAL
1785 isn't valid for a PARM_DECL. */
a7c1916a 1786 current_function_decl != 0
4f976745 1787 && TREE_CODE (decl) != PARM_DECL
3f75a254 1788 && !TREE_THIS_VOLATILE (decl)
83bab8db 1789 && TREE_READONLY (decl)
400fbf9f
JW
1790 && DECL_INITIAL (decl) != 0
1791 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1792 /* This is invalid if initial value is not constant.
1793 If it has either a function call, a memory reference,
1794 or a variable, then re-evaluating it could give different results. */
1795 && TREE_CONSTANT (DECL_INITIAL (decl))
1796 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1797 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1798 return DECL_INITIAL (decl);
1799 return decl;
1800}
1801
f2a71bbc
JM
1802/* Convert the array expression EXP to a pointer. */
1803static tree
c2255bc4 1804array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1805{
f2a71bbc 1806 tree orig_exp = exp;
207bf485 1807 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1808 tree adr;
1809 tree restype = TREE_TYPE (type);
1810 tree ptrtype;
207bf485 1811
f2a71bbc 1812 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1813
f2a71bbc 1814 STRIP_TYPE_NOPS (exp);
207bf485 1815
487a92fe
JM
1816 if (TREE_NO_WARNING (orig_exp))
1817 TREE_NO_WARNING (exp) = 1;
207bf485 1818
f2a71bbc
JM
1819 ptrtype = build_pointer_type (restype);
1820
1821 if (TREE_CODE (exp) == INDIRECT_REF)
1822 return convert (ptrtype, TREE_OPERAND (exp, 0));
1823
1f37c583
JM
1824 /* In C++ array compound literals are temporary objects unless they are
1825 const or appear in namespace scope, so they are destroyed too soon
1826 to use them for much of anything (c++/53220). */
1827 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1828 {
1829 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1830 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1831 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1832 "converting an array compound literal to a pointer "
1833 "is ill-formed in C++");
1834 }
1835
c2255bc4 1836 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1837 return convert (ptrtype, adr);
1838}
207bf485 1839
f2a71bbc
JM
1840/* Convert the function expression EXP to a pointer. */
1841static tree
c2255bc4 1842function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1843{
1844 tree orig_exp = exp;
207bf485 1845
f2a71bbc 1846 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1847
f2a71bbc 1848 STRIP_TYPE_NOPS (exp);
207bf485 1849
f2a71bbc
JM
1850 if (TREE_NO_WARNING (orig_exp))
1851 TREE_NO_WARNING (exp) = 1;
207bf485 1852
c2255bc4 1853 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1854}
207bf485 1855
ebfbbdc5
JJ
1856/* Mark EXP as read, not just set, for set but not used -Wunused
1857 warning purposes. */
1858
1859void
1860mark_exp_read (tree exp)
1861{
1862 switch (TREE_CODE (exp))
1863 {
1864 case VAR_DECL:
1865 case PARM_DECL:
1866 DECL_READ_P (exp) = 1;
1867 break;
1868 case ARRAY_REF:
1869 case COMPONENT_REF:
1870 case MODIFY_EXPR:
1871 case REALPART_EXPR:
1872 case IMAGPART_EXPR:
1873 CASE_CONVERT:
1874 case ADDR_EXPR:
1875 mark_exp_read (TREE_OPERAND (exp, 0));
1876 break;
1877 case COMPOUND_EXPR:
82c3c067 1878 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1879 mark_exp_read (TREE_OPERAND (exp, 1));
1880 break;
1881 default:
1882 break;
1883 }
1884}
1885
f2a71bbc
JM
1886/* Perform the default conversion of arrays and functions to pointers.
1887 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1888 return EXP.
1889
1890 LOC is the location of the expression. */
f2a71bbc
JM
1891
1892struct c_expr
c2255bc4 1893default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1894{
1895 tree orig_exp = exp.value;
1896 tree type = TREE_TYPE (exp.value);
1897 enum tree_code code = TREE_CODE (type);
1898
1899 switch (code)
1900 {
1901 case ARRAY_TYPE:
1902 {
1903 bool not_lvalue = false;
1904 bool lvalue_array_p;
1905
1906 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1907 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1908 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1909 {
1910 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1911 not_lvalue = true;
1912 exp.value = TREE_OPERAND (exp.value, 0);
1913 }
1914
1915 if (TREE_NO_WARNING (orig_exp))
1916 TREE_NO_WARNING (exp.value) = 1;
1917
1918 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1919 if (!flag_isoc99 && !lvalue_array_p)
1920 {
1921 /* Before C99, non-lvalue arrays do not decay to pointers.
1922 Normally, using such an array would be invalid; but it can
1923 be used correctly inside sizeof or as a statement expression.
1924 Thus, do not give an error here; an error will result later. */
1925 return exp;
1926 }
1927
c2255bc4 1928 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1929 }
1930 break;
1931 case FUNCTION_TYPE:
c2255bc4 1932 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1933 break;
1934 default:
f2a71bbc 1935 break;
207bf485 1936 }
f2a71bbc 1937
207bf485
JM
1938 return exp;
1939}
1940
ebfbbdc5
JJ
1941struct c_expr
1942default_function_array_read_conversion (location_t loc, struct c_expr exp)
1943{
1944 mark_exp_read (exp.value);
1945 return default_function_array_conversion (loc, exp);
1946}
522ddfa2 1947
267bac10
JM
1948/* Return whether EXPR should be treated as an atomic lvalue for the
1949 purposes of load and store handling. */
1950
1951static bool
1952really_atomic_lvalue (tree expr)
1953{
1954 if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node)
1955 return false;
1956 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1957 return false;
1958 if (!lvalue_p (expr))
1959 return false;
1960
1961 /* Ignore _Atomic on register variables, since their addresses can't
1962 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1963 sequences wouldn't work. Ignore _Atomic on structures containing
1964 bit-fields, since accessing elements of atomic structures or
1965 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1966 it's undefined at translation time or execution time, and the
1967 normal atomic sequences again wouldn't work. */
1968 while (handled_component_p (expr))
1969 {
1970 if (TREE_CODE (expr) == COMPONENT_REF
1971 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1972 return false;
1973 expr = TREE_OPERAND (expr, 0);
1974 }
1975 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1976 return false;
1977 return true;
1978}
1979
1980/* Convert expression EXP (location LOC) from lvalue to rvalue,
1981 including converting functions and arrays to pointers if CONVERT_P.
1982 If READ_P, also mark the expression as having been read. */
1983
1984struct c_expr
1985convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1986 bool convert_p, bool read_p)
1987{
1988 if (read_p)
1989 mark_exp_read (exp.value);
1990 if (convert_p)
1991 exp = default_function_array_conversion (loc, exp);
1992 if (really_atomic_lvalue (exp.value))
1993 {
1994 vec<tree, va_gc> *params;
1995 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1996 tree expr_type = TREE_TYPE (exp.value);
1997 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1998 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
1999
2000 gcc_assert (TYPE_ATOMIC (expr_type));
2001
2002 /* Expansion of a generic atomic load may require an addition
2003 element, so allocate enough to prevent a resize. */
2004 vec_alloc (params, 4);
2005
2006 /* Remove the qualifiers for the rest of the expressions and
2007 create the VAL temp variable to hold the RHS. */
2008 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2009 tmp = create_tmp_var (nonatomic_type, NULL);
2010 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2011 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2012 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2013
2014 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2015 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2016 params->quick_push (expr_addr);
2017 params->quick_push (tmp_addr);
2018 params->quick_push (seq_cst);
81e5eca8 2019 func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2020
cc28fc7f
MP
2021 /* EXPR is always read. */
2022 mark_exp_read (exp.value);
2023
267bac10
JM
2024 /* Return tmp which contains the value loaded. */
2025 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2026 }
2027 return exp;
2028}
2029
522ddfa2
JM
2030/* EXP is an expression of integer type. Apply the integer promotions
2031 to it and return the promoted value. */
400fbf9f
JW
2032
2033tree
522ddfa2 2034perform_integral_promotions (tree exp)
400fbf9f 2035{
b3694847
SS
2036 tree type = TREE_TYPE (exp);
2037 enum tree_code code = TREE_CODE (type);
400fbf9f 2038
522ddfa2 2039 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2040
400fbf9f
JW
2041 /* Normally convert enums to int,
2042 but convert wide enums to something wider. */
2043 if (code == ENUMERAL_TYPE)
2044 {
b0c48229
NB
2045 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2046 TYPE_PRECISION (integer_type_node)),
2047 ((TYPE_PRECISION (type)
2048 >= TYPE_PRECISION (integer_type_node))
8df83eae 2049 && TYPE_UNSIGNED (type)));
05bccae2 2050
400fbf9f
JW
2051 return convert (type, exp);
2052 }
2053
522ddfa2
JM
2054 /* ??? This should no longer be needed now bit-fields have their
2055 proper types. */
9753f113 2056 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2057 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2058 /* If it's thinner than an int, promote it like a
d72040f5 2059 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2060 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2061 TYPE_PRECISION (integer_type_node)))
f458d1d5 2062 return convert (integer_type_node, exp);
9753f113 2063
d72040f5 2064 if (c_promoting_integer_type_p (type))
400fbf9f 2065 {
f458d1d5 2066 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2067 if (TYPE_UNSIGNED (type)
f458d1d5 2068 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2069 return convert (unsigned_type_node, exp);
05bccae2 2070
400fbf9f
JW
2071 return convert (integer_type_node, exp);
2072 }
05bccae2 2073
522ddfa2
JM
2074 return exp;
2075}
2076
2077
2078/* Perform default promotions for C data used in expressions.
46bdb9cf 2079 Enumeral types or short or char are converted to int.
522ddfa2
JM
2080 In addition, manifest constants symbols are replaced by their values. */
2081
2082tree
2083default_conversion (tree exp)
2084{
2085 tree orig_exp;
2086 tree type = TREE_TYPE (exp);
2087 enum tree_code code = TREE_CODE (type);
40449a90 2088 tree promoted_type;
522ddfa2 2089
ebfbbdc5
JJ
2090 mark_exp_read (exp);
2091
46bdb9cf
JM
2092 /* Functions and arrays have been converted during parsing. */
2093 gcc_assert (code != FUNCTION_TYPE);
2094 if (code == ARRAY_TYPE)
2095 return exp;
522ddfa2
JM
2096
2097 /* Constants can be used directly unless they're not loadable. */
2098 if (TREE_CODE (exp) == CONST_DECL)
2099 exp = DECL_INITIAL (exp);
2100
522ddfa2
JM
2101 /* Strip no-op conversions. */
2102 orig_exp = exp;
2103 STRIP_TYPE_NOPS (exp);
2104
2105 if (TREE_NO_WARNING (orig_exp))
2106 TREE_NO_WARNING (exp) = 1;
2107
400fbf9f
JW
2108 if (code == VOID_TYPE)
2109 {
2110 error ("void value not ignored as it ought to be");
2111 return error_mark_node;
2112 }
808d6eaa
JM
2113
2114 exp = require_complete_type (exp);
2115 if (exp == error_mark_node)
2116 return error_mark_node;
2117
40449a90
SL
2118 promoted_type = targetm.promoted_type (type);
2119 if (promoted_type)
2120 return convert (promoted_type, exp);
2121
808d6eaa
JM
2122 if (INTEGRAL_TYPE_P (type))
2123 return perform_integral_promotions (exp);
2124
400fbf9f
JW
2125 return exp;
2126}
2127\f
0fb96aa4 2128/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2129
2130 If the component name is not found, returns NULL_TREE. Otherwise,
2131 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2132 stepping down the chain to the component, which is in the last
2133 TREE_VALUE of the list. Normally the list is of length one, but if
2134 the component is embedded within (nested) anonymous structures or
2135 unions, the list steps down the chain to the component. */
2f6e4e97 2136
2f2d13da 2137static tree
0fb96aa4 2138lookup_field (tree type, tree component)
2f2d13da
DE
2139{
2140 tree field;
2141
2142 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2143 to the field elements. Use a binary search on this array to quickly
2144 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2145 will always be set for structures which have many elements. */
2146
22a0b85f 2147 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2148 {
2149 int bot, top, half;
d07605f5 2150 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2151
2152 field = TYPE_FIELDS (type);
2153 bot = 0;
d07605f5 2154 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2155 while (top - bot > 1)
2156 {
2f2d13da
DE
2157 half = (top - bot + 1) >> 1;
2158 field = field_array[bot+half];
2159
2160 if (DECL_NAME (field) == NULL_TREE)
2161 {
2162 /* Step through all anon unions in linear fashion. */
2163 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2164 {
2f2d13da 2165 field = field_array[bot++];
a68b98cf
RK
2166 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2167 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2168 {
0fb96aa4 2169 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2170
2171 if (anon)
2172 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2173
2174 /* The Plan 9 compiler permits referring
2175 directly to an anonymous struct/union field
2176 using a typedef name. */
2177 if (flag_plan9_extensions
2178 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2179 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2180 == TYPE_DECL)
2181 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2182 == component))
2183 break;
2f6e4e97 2184 }
2f2d13da
DE
2185 }
2186
2187 /* Entire record is only anon unions. */
2188 if (bot > top)
2189 return NULL_TREE;
2190
2191 /* Restart the binary search, with new lower bound. */
2192 continue;
2193 }
2194
e8b87aac 2195 if (DECL_NAME (field) == component)
2f2d13da 2196 break;
e8b87aac 2197 if (DECL_NAME (field) < component)
2f2d13da
DE
2198 bot += half;
2199 else
2200 top = bot + half;
2201 }
2202
2203 if (DECL_NAME (field_array[bot]) == component)
2204 field = field_array[bot];
2205 else if (DECL_NAME (field) != component)
e9b2c823 2206 return NULL_TREE;
2f2d13da
DE
2207 }
2208 else
2209 {
910ad8de 2210 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2211 {
e9b2c823
NB
2212 if (DECL_NAME (field) == NULL_TREE
2213 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2214 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2215 {
0fb96aa4 2216 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2217
e9b2c823
NB
2218 if (anon)
2219 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2220
2221 /* The Plan 9 compiler permits referring directly to an
2222 anonymous struct/union field using a typedef
2223 name. */
2224 if (flag_plan9_extensions
2225 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2226 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2227 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2228 == component))
2229 break;
2f2d13da
DE
2230 }
2231
2232 if (DECL_NAME (field) == component)
2233 break;
2234 }
e9b2c823
NB
2235
2236 if (field == NULL_TREE)
2237 return NULL_TREE;
2f2d13da
DE
2238 }
2239
e9b2c823 2240 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2241}
2242
c2255bc4
AH
2243/* Make an expression to refer to the COMPONENT field of structure or
2244 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2245 location of the COMPONENT_REF. */
400fbf9f
JW
2246
2247tree
c2255bc4 2248build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2249{
b3694847
SS
2250 tree type = TREE_TYPE (datum);
2251 enum tree_code code = TREE_CODE (type);
2252 tree field = NULL;
2253 tree ref;
1e57bf47 2254 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2255
7a3ea201
RH
2256 if (!objc_is_public (datum, component))
2257 return error_mark_node;
2258
46a88c12 2259 /* Detect Objective-C property syntax object.property. */
668ea4b1 2260 if (c_dialect_objc ()
46a88c12 2261 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2262 return ref;
2263
400fbf9f
JW
2264 /* See if there is a field or component with name COMPONENT. */
2265
2266 if (code == RECORD_TYPE || code == UNION_TYPE)
2267 {
d0f062fb 2268 if (!COMPLETE_TYPE_P (type))
400fbf9f 2269 {
7a228918 2270 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2271 return error_mark_node;
2272 }
2273
0fb96aa4 2274 field = lookup_field (type, component);
400fbf9f
JW
2275
2276 if (!field)
2277 {
c2255bc4 2278 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2279 return error_mark_node;
2280 }
400fbf9f 2281
e9b2c823
NB
2282 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2283 This might be better solved in future the way the C++ front
2284 end does it - by giving the anonymous entities each a
2285 separate name and type, and then have build_component_ref
2286 recursively call itself. We can't do that here. */
46ea50cb 2287 do
19d76e60 2288 {
e9b2c823 2289 tree subdatum = TREE_VALUE (field);
efed193e
JM
2290 int quals;
2291 tree subtype;
1e57bf47 2292 bool use_datum_quals;
e9b2c823
NB
2293
2294 if (TREE_TYPE (subdatum) == error_mark_node)
2295 return error_mark_node;
2296
1e57bf47
JM
2297 /* If this is an rvalue, it does not have qualifiers in C
2298 standard terms and we must avoid propagating such
2299 qualifiers down to a non-lvalue array that is then
2300 converted to a pointer. */
2301 use_datum_quals = (datum_lvalue
2302 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2303
efed193e 2304 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2305 if (use_datum_quals)
2306 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2307 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2308
2309 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2310 NULL_TREE);
c2255bc4 2311 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2312 if (TREE_READONLY (subdatum)
2313 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2314 TREE_READONLY (ref) = 1;
1e57bf47
JM
2315 if (TREE_THIS_VOLATILE (subdatum)
2316 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2317 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2318
2319 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2320 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2321
19d76e60 2322 datum = ref;
46ea50cb
RS
2323
2324 field = TREE_CHAIN (field);
19d76e60 2325 }
46ea50cb 2326 while (field);
19d76e60 2327
400fbf9f
JW
2328 return ref;
2329 }
2330 else if (code != ERROR_MARK)
c2255bc4
AH
2331 error_at (loc,
2332 "request for member %qE in something not a structure or union",
2333 component);
400fbf9f
JW
2334
2335 return error_mark_node;
2336}
2337\f
2338/* Given an expression PTR for a pointer, return an expression
2339 for the value pointed to.
6a3799eb
AH
2340 ERRORSTRING is the name of the operator to appear in error messages.
2341
2342 LOC is the location to use for the generated tree. */
400fbf9f
JW
2343
2344tree
dd865ef6 2345build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2346{
b3694847
SS
2347 tree pointer = default_conversion (ptr);
2348 tree type = TREE_TYPE (pointer);
6a3799eb 2349 tree ref;
400fbf9f
JW
2350
2351 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2352 {
1043771b 2353 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2354 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2355 {
2356 /* If a warning is issued, mark it to avoid duplicates from
2357 the backend. This only needs to be done at
2358 warn_strict_aliasing > 2. */
2359 if (warn_strict_aliasing > 2)
2360 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2361 type, TREE_OPERAND (pointer, 0)))
2362 TREE_NO_WARNING (pointer) = 1;
2363 }
2364
870cc33b 2365 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2366 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2367 == TREE_TYPE (type)))
6a3799eb
AH
2368 {
2369 ref = TREE_OPERAND (pointer, 0);
2370 protected_set_expr_location (ref, loc);
2371 return ref;
2372 }
870cc33b
RS
2373 else
2374 {
2375 tree t = TREE_TYPE (type);
46df2823 2376
984dfd8c 2377 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2378
baae9b65 2379 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2380 {
c9f9eb5d 2381 error_at (loc, "dereferencing pointer to incomplete type");
870cc33b
RS
2382 return error_mark_node;
2383 }
7d882b83 2384 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2385 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2386
2387 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2388 so that we get the proper error message if the result is used
2389 to assign to. Also, &* is supposed to be a no-op.
2390 And ANSI C seems to specify that the type of the result
2391 should be the const type. */
2392 /* A de-reference of a pointer to const is not a const. It is valid
2393 to change it via some other pointer. */
2394 TREE_READONLY (ref) = TYPE_READONLY (t);
2395 TREE_SIDE_EFFECTS (ref)
271bd540 2396 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2397 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2398 protected_set_expr_location (ref, loc);
870cc33b
RS
2399 return ref;
2400 }
2401 }
400fbf9f 2402 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2403 invalid_indirection_error (loc, type, errstring);
2404
400fbf9f
JW
2405 return error_mark_node;
2406}
2407
2408/* This handles expressions of the form "a[i]", which denotes
2409 an array reference.
2410
2411 This is logically equivalent in C to *(a+i), but we may do it differently.
2412 If A is a variable or a member, we generate a primitive ARRAY_REF.
2413 This avoids forcing the array out of registers, and can work on
2414 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2415 by functions).
2416
30cd1c5d
AS
2417 For vector types, allow vector[i] but not i[vector], and create
2418 *(((type*)&vectortype) + i) for the expression.
2419
6a3799eb 2420 LOC is the location to use for the returned expression. */
400fbf9f
JW
2421
2422tree
c2255bc4 2423build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2424{
6a3799eb 2425 tree ret;
a4ab7973 2426 bool swapped = false;
400fbf9f
JW
2427 if (TREE_TYPE (array) == error_mark_node
2428 || TREE_TYPE (index) == error_mark_node)
2429 return error_mark_node;
2430
b72271b9 2431 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2432 {
2433 size_t rank = 0;
2434 if (!find_rank (loc, index, index, true, &rank))
2435 return error_mark_node;
2436 if (rank > 1)
2437 {
2438 error_at (loc, "rank of the array's index is greater than 1");
2439 return error_mark_node;
2440 }
2441 }
a4ab7973 2442 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2443 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2444 /* Allow vector[index] but not index[vector]. */
2445 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2446 {
a4ab7973
JM
2447 tree temp;
2448 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2449 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2450 {
f90e8e2e 2451 error_at (loc,
30cd1c5d
AS
2452 "subscripted value is neither array nor pointer nor vector");
2453
fdeefd49
RS
2454 return error_mark_node;
2455 }
a4ab7973
JM
2456 temp = array;
2457 array = index;
2458 index = temp;
2459 swapped = true;
2460 }
2461
2462 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2463 {
a63068b6 2464 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2465 return error_mark_node;
2466 }
2467
2468 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2469 {
a63068b6 2470 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2471 return error_mark_node;
2472 }
2473
ff6b6641
GDR
2474 /* ??? Existing practice has been to warn only when the char
2475 index is syntactically the index, not for char[array]. */
2476 if (!swapped)
2477 warn_array_subscript_with_type_char (index);
a4ab7973
JM
2478
2479 /* Apply default promotions *after* noticing character types. */
2480 index = default_conversion (index);
2481
2482 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2483
7edaa4d2 2484 convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2485
2486 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2487 {
e4d35515 2488 tree rval, type;
fdeefd49 2489
400fbf9f
JW
2490 /* An array that is indexed by a non-constant
2491 cannot be stored in a register; we must be able to do
2492 address arithmetic on its address.
2493 Likewise an array of elements of variable size. */
2494 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2495 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2496 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2497 {
dffd7eb6 2498 if (!c_mark_addressable (array))
400fbf9f
JW
2499 return error_mark_node;
2500 }
e6d52559
JW
2501 /* An array that is indexed by a constant value which is not within
2502 the array bounds cannot be stored in a register either; because we
2503 would get a crash in store_bit_field/extract_bit_field when trying
2504 to access a non-existent part of the register. */
2505 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2506 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2507 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2508 {
dffd7eb6 2509 if (!c_mark_addressable (array))
e6d52559
JW
2510 return error_mark_node;
2511 }
400fbf9f 2512
400fbf9f
JW
2513 if (pedantic)
2514 {
2515 tree foo = array;
2516 while (TREE_CODE (foo) == COMPONENT_REF)
2517 foo = TREE_OPERAND (foo, 0);
5baeaac0 2518 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2519 pedwarn (loc, OPT_Wpedantic,
fcf73884 2520 "ISO C forbids subscripting %<register%> array");
3f75a254 2521 else if (!flag_isoc99 && !lvalue_p (foo))
c1771a20 2522 pedwarn (loc, OPT_Wpedantic,
fcf73884 2523 "ISO C90 forbids subscripting non-lvalue array");
400fbf9f
JW
2524 }
2525
46df2823 2526 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2527 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2528 /* Array ref is const/volatile if the array elements are
c22cacf3 2529 or if the array is. */
400fbf9f
JW
2530 TREE_READONLY (rval)
2531 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2532 | TREE_READONLY (array));
2533 TREE_SIDE_EFFECTS (rval)
2534 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2535 | TREE_SIDE_EFFECTS (array));
2536 TREE_THIS_VOLATILE (rval)
2537 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2538 /* This was added by rms on 16 Nov 91.
2f6e4e97 2539 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2540 in an inline function.
2541 Hope it doesn't break something else. */
2542 | TREE_THIS_VOLATILE (array));
928c19bb 2543 ret = require_complete_type (rval);
6a3799eb
AH
2544 protected_set_expr_location (ret, loc);
2545 return ret;
400fbf9f 2546 }
a4ab7973
JM
2547 else
2548 {
2549 tree ar = default_conversion (array);
400fbf9f 2550
a4ab7973
JM
2551 if (ar == error_mark_node)
2552 return ar;
400fbf9f 2553
a4ab7973
JM
2554 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2555 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2556
ba47d38d 2557 return build_indirect_ref
c9f9eb5d 2558 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
dd865ef6 2559 RO_ARRAY_INDEXING);
a4ab7973 2560 }
400fbf9f
JW
2561}
2562\f
7e585d16 2563/* Build an external reference to identifier ID. FUN indicates
766beb40 2564 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2565 location of the identifier. This sets *TYPE to the type of the
2566 identifier, which is not the same as the type of the returned value
2567 for CONST_DECLs defined as enum constants. If the type of the
2568 identifier is not available, *TYPE is set to NULL. */
7e585d16 2569tree
c2255bc4 2570build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2571{
2572 tree ref;
2573 tree decl = lookup_name (id);
16b34ad6
ZL
2574
2575 /* In Objective-C, an instance variable (ivar) may be preferred to
2576 whatever lookup_name() found. */
2577 decl = objc_lookup_ivar (decl, id);
7e585d16 2578
6866c6e8 2579 *type = NULL;
339a28b9 2580 if (decl && decl != error_mark_node)
6866c6e8
ILT
2581 {
2582 ref = decl;
2583 *type = TREE_TYPE (ref);
2584 }
339a28b9
ZW
2585 else if (fun)
2586 /* Implicit function declaration. */
c2255bc4 2587 ref = implicitly_declare (loc, id);
339a28b9
ZW
2588 else if (decl == error_mark_node)
2589 /* Don't complain about something that's already been
2590 complained about. */
2591 return error_mark_node;
2592 else
2593 {
c2255bc4 2594 undeclared_variable (loc, id);
339a28b9
ZW
2595 return error_mark_node;
2596 }
7e585d16
ZW
2597
2598 if (TREE_TYPE (ref) == error_mark_node)
2599 return error_mark_node;
2600
339a28b9 2601 if (TREE_DEPRECATED (ref))
9b86d6bb 2602 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2603
ad960f56 2604 /* Recursive call does not count as usage. */
b8698a0f 2605 if (ref != current_function_decl)
ad960f56 2606 {
ad960f56
MLI
2607 TREE_USED (ref) = 1;
2608 }
7e585d16 2609
bc4b653b
JM
2610 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2611 {
2612 if (!in_sizeof && !in_typeof)
2613 C_DECL_USED (ref) = 1;
2614 else if (DECL_INITIAL (ref) == 0
2615 && DECL_EXTERNAL (ref)
2616 && !TREE_PUBLIC (ref))
2617 record_maybe_used_decl (ref);
2618 }
2619
7e585d16
ZW
2620 if (TREE_CODE (ref) == CONST_DECL)
2621 {
6193b8b7 2622 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2623
2624 if (warn_cxx_compat
2625 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2626 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2627 {
2628 warning_at (loc, OPT_Wc___compat,
2629 ("enum constant defined in struct or union "
2630 "is not visible in C++"));
2631 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2632 }
2633
7e585d16
ZW
2634 ref = DECL_INITIAL (ref);
2635 TREE_CONSTANT (ref) = 1;
2636 }
6a29edea 2637 else if (current_function_decl != 0
4b1e44be 2638 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2639 && (TREE_CODE (ref) == VAR_DECL
2640 || TREE_CODE (ref) == PARM_DECL
2641 || TREE_CODE (ref) == FUNCTION_DECL))
2642 {
2643 tree context = decl_function_context (ref);
2f6e4e97 2644
6a29edea
EB
2645 if (context != 0 && context != current_function_decl)
2646 DECL_NONLOCAL (ref) = 1;
2647 }
71113fcd
GK
2648 /* C99 6.7.4p3: An inline definition of a function with external
2649 linkage ... shall not contain a reference to an identifier with
2650 internal linkage. */
2651 else if (current_function_decl != 0
2652 && DECL_DECLARED_INLINE_P (current_function_decl)
2653 && DECL_EXTERNAL (current_function_decl)
2654 && VAR_OR_FUNCTION_DECL_P (ref)
2655 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2656 && ! TREE_PUBLIC (ref)
2657 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2658 record_inline_static (loc, current_function_decl, ref,
2659 csi_internal);
7e585d16
ZW
2660
2661 return ref;
2662}
2663
bc4b653b
JM
2664/* Record details of decls possibly used inside sizeof or typeof. */
2665struct maybe_used_decl
2666{
2667 /* The decl. */
2668 tree decl;
2669 /* The level seen at (in_sizeof + in_typeof). */
2670 int level;
2671 /* The next one at this level or above, or NULL. */
2672 struct maybe_used_decl *next;
2673};
2674
2675static struct maybe_used_decl *maybe_used_decls;
2676
2677/* Record that DECL, an undefined static function reference seen
2678 inside sizeof or typeof, might be used if the operand of sizeof is
2679 a VLA type or the operand of typeof is a variably modified
2680 type. */
2681
4e2fb7de 2682static void
bc4b653b
JM
2683record_maybe_used_decl (tree decl)
2684{
2685 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2686 t->decl = decl;
2687 t->level = in_sizeof + in_typeof;
2688 t->next = maybe_used_decls;
2689 maybe_used_decls = t;
2690}
2691
2692/* Pop the stack of decls possibly used inside sizeof or typeof. If
2693 USED is false, just discard them. If it is true, mark them used
2694 (if no longer inside sizeof or typeof) or move them to the next
2695 level up (if still inside sizeof or typeof). */
2696
2697void
2698pop_maybe_used (bool used)
2699{
2700 struct maybe_used_decl *p = maybe_used_decls;
2701 int cur_level = in_sizeof + in_typeof;
2702 while (p && p->level > cur_level)
2703 {
2704 if (used)
2705 {
2706 if (cur_level == 0)
2707 C_DECL_USED (p->decl) = 1;
2708 else
2709 p->level = cur_level;
2710 }
2711 p = p->next;
2712 }
2713 if (!used || cur_level == 0)
2714 maybe_used_decls = p;
2715}
2716
2717/* Return the result of sizeof applied to EXPR. */
2718
2719struct c_expr
c2255bc4 2720c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2721{
2722 struct c_expr ret;
ad97f4be
JM
2723 if (expr.value == error_mark_node)
2724 {
2725 ret.value = error_mark_node;
2726 ret.original_code = ERROR_MARK;
6866c6e8 2727 ret.original_type = NULL;
ad97f4be
JM
2728 pop_maybe_used (false);
2729 }
2730 else
2731 {
928c19bb
JM
2732 bool expr_const_operands = true;
2733 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2734 &expr_const_operands);
c2255bc4 2735 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2736 c_last_sizeof_arg = expr.value;
2737 ret.original_code = SIZEOF_EXPR;
6866c6e8 2738 ret.original_type = NULL;
928c19bb 2739 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2740 {
2741 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2742 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2743 folded_expr, ret.value);
2744 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2745 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2746 }
928c19bb 2747 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2748 }
bc4b653b
JM
2749 return ret;
2750}
2751
2752/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2753 name passed to sizeof (rather than the type itself). LOC is the
2754 location of the original expression. */
bc4b653b
JM
2755
2756struct c_expr
c2255bc4 2757c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2758{
2759 tree type;
2760 struct c_expr ret;
928c19bb
JM
2761 tree type_expr = NULL_TREE;
2762 bool type_expr_const = true;
2763 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2764 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2765 c_last_sizeof_arg = type;
2766 ret.original_code = SIZEOF_EXPR;
6866c6e8 2767 ret.original_type = NULL;
24070fcb
JM
2768 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2769 && c_vla_type_p (type))
928c19bb 2770 {
24070fcb
JM
2771 /* If the type is a [*] array, it is a VLA but is represented as
2772 having a size of zero. In such a case we must ensure that
2773 the result of sizeof does not get folded to a constant by
2774 c_fully_fold, because if the size is evaluated the result is
2775 not constant and so constraints on zero or negative size
2776 arrays must not be applied when this sizeof call is inside
2777 another array declarator. */
2778 if (!type_expr)
2779 type_expr = integer_zero_node;
928c19bb
JM
2780 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2781 type_expr, ret.value);
2782 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2783 }
16464cc1
VR
2784 pop_maybe_used (type != error_mark_node
2785 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2786 return ret;
2787}
2788
400fbf9f 2789/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2790 The function call is at LOC.
400fbf9f
JW
2791 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2792 TREE_VALUE of each node is a parameter-expression.
2793 FUNCTION's data type may be a function type or a pointer-to-function. */
2794
2795tree
c2255bc4 2796build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2797{
9771b263 2798 vec<tree, va_gc> *v;
bbbbb16a
ILT
2799 tree ret;
2800
9771b263 2801 vec_alloc (v, list_length (params));
bbbbb16a 2802 for (; params; params = TREE_CHAIN (params))
9771b263 2803 v->quick_push (TREE_VALUE (params));
81e5eca8 2804 ret = build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2805 vec_free (v);
bbbbb16a
ILT
2806 return ret;
2807}
2808
ae52741c
MLI
2809/* Give a note about the location of the declaration of DECL. */
2810
2811static void inform_declaration (tree decl)
2812{
2813 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2814 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2815}
2816
bbbbb16a
ILT
2817/* Build a function call to function FUNCTION with parameters PARAMS.
2818 ORIGTYPES, if not NULL, is a vector of types; each element is
2819 either NULL or the original type of the corresponding element in
2820 PARAMS. The original type may differ from TREE_TYPE of the
2821 parameter for enums. FUNCTION's data type may be a function type
2822 or pointer-to-function. This function changes the elements of
2823 PARAMS. */
2824
2825tree
81e5eca8
MP
2826build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2827 tree function, vec<tree, va_gc> *params,
9771b263 2828 vec<tree, va_gc> *origtypes)
400fbf9f 2829{
b3694847 2830 tree fntype, fundecl = 0;
4977bab6 2831 tree name = NULL_TREE, result;
c96f4f73 2832 tree tem;
94a0dd7b
SL
2833 int nargs;
2834 tree *argarray;
b8698a0f 2835
400fbf9f 2836
fc76e425 2837 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2838 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2839
2840 /* Convert anything with function type to a pointer-to-function. */
2841 if (TREE_CODE (function) == FUNCTION_DECL)
2842 {
58646b77
PB
2843 /* Implement type-directed function overloading for builtins.
2844 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2845 handle all the type checking. The result is a complete expression
2846 that implements this function call. */
c2255bc4 2847 tem = resolve_overloaded_builtin (loc, function, params);
58646b77
PB
2848 if (tem)
2849 return tem;
48ae6c13 2850
400fbf9f 2851 name = DECL_NAME (function);
0a35513e
AH
2852
2853 if (flag_tm)
2854 tm_malloc_replacement (function);
a5eadacc 2855 fundecl = function;
86951993
AM
2856 /* Atomic functions have type checking/casting already done. They are
2857 often rewritten and don't match the original parameter list. */
2858 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2859 origtypes = NULL;
36536d79 2860
b72271b9 2861 if (flag_cilkplus
36536d79
BI
2862 && is_cilkplus_reduce_builtin (function))
2863 origtypes = NULL;
400fbf9f 2864 }
f2a71bbc 2865 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2866 function = function_to_pointer_conversion (loc, function);
400fbf9f 2867
6e955430
ZL
2868 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2869 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2870 if (params && !params->is_empty ())
2871 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2872
928c19bb
JM
2873 function = c_fully_fold (function, false, NULL);
2874
400fbf9f
JW
2875 fntype = TREE_TYPE (function);
2876
2877 if (TREE_CODE (fntype) == ERROR_MARK)
2878 return error_mark_node;
2879
2880 if (!(TREE_CODE (fntype) == POINTER_TYPE
2881 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2882 {
ae52741c
MLI
2883 if (!flag_diagnostics_show_caret)
2884 error_at (loc,
2885 "called object %qE is not a function or function pointer",
2886 function);
2887 else if (DECL_P (function))
2888 {
2889 error_at (loc,
2890 "called object %qD is not a function or function pointer",
2891 function);
2892 inform_declaration (function);
2893 }
2894 else
2895 error_at (loc,
2896 "called object is not a function or function pointer");
400fbf9f
JW
2897 return error_mark_node;
2898 }
2899
5ce89b2e
JM
2900 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2901 current_function_returns_abnormally = 1;
2902
400fbf9f
JW
2903 /* fntype now gets the type of function pointed to. */
2904 fntype = TREE_TYPE (fntype);
2905
ab4194da
JM
2906 /* Convert the parameters to the types declared in the
2907 function prototype, or apply default promotions. */
2908
81e5eca8
MP
2909 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2910 origtypes, function, fundecl);
ab4194da
JM
2911 if (nargs < 0)
2912 return error_mark_node;
2913
c96f4f73 2914 /* Check that the function is called through a compatible prototype.
fa337f3a 2915 If it is not, warn. */
1043771b 2916 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2917 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2918 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2919 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2920 {
2921 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
2922
2923 /* This situation leads to run-time undefined behavior. We can't,
2924 therefore, simply error unless we can prove that all possible
2925 executions of the program must execute the code. */
fa337f3a 2926 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 2927
fa337f3a
RB
2928 if (VOID_TYPE_P (return_type)
2929 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2930 pedwarn (loc, 0,
2931 "function with qualified void return type called");
2932 }
c96f4f73 2933
9771b263 2934 argarray = vec_safe_address (params);
bbbbb16a 2935
83322951
RG
2936 /* Check that arguments to builtin functions match the expectations. */
2937 if (fundecl
2938 && DECL_BUILT_IN (fundecl)
2939 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2940 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2941 return error_mark_node;
400fbf9f 2942
83322951 2943 /* Check that the arguments to the function are valid. */
dde05067 2944 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2945
928c19bb
JM
2946 if (name != NULL_TREE
2947 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2948 {
928c19bb 2949 if (require_constant_value)
b8698a0f 2950 result =
db3927fb
AH
2951 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2952 function, nargs, argarray);
928c19bb 2953 else
db3927fb
AH
2954 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2955 function, nargs, argarray);
928c19bb
JM
2956 if (TREE_CODE (result) == NOP_EXPR
2957 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2958 STRIP_TYPE_NOPS (result);
bf730f15
RS
2959 }
2960 else
db3927fb
AH
2961 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2962 function, nargs, argarray);
b0b3afb2 2963
71653180 2964 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2965 {
2966 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2967 pedwarn (loc, 0,
3ce62965
JM
2968 "function with qualified void return type called");
2969 return result;
2970 }
1eb8759b 2971 return require_complete_type (result);
400fbf9f
JW
2972}
2973\f
bbbbb16a
ILT
2974/* Convert the argument expressions in the vector VALUES
2975 to the types in the list TYPELIST.
400fbf9f
JW
2976
2977 If TYPELIST is exhausted, or when an element has NULL as its type,
2978 perform the default conversions.
2979
bbbbb16a
ILT
2980 ORIGTYPES is the original types of the expressions in VALUES. This
2981 holds the type of enum values which have been converted to integral
2982 types. It may be NULL.
400fbf9f 2983
03dafa61
JM
2984 FUNCTION is a tree for the called function. It is used only for
2985 error messages, where it is formatted with %qE.
400fbf9f
JW
2986
2987 This is also where warnings about wrong number of args are generated.
2988
81e5eca8
MP
2989 ARG_LOC are locations of function arguments (if any).
2990
94a0dd7b 2991 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
2992 than the length of VALUES in some error situations), or -1 on
2993 failure. */
94a0dd7b
SL
2994
2995static int
81e5eca8
MP
2996convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
2997 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
2998 tree function, tree fundecl)
400fbf9f 2999{
bbbbb16a
ILT
3000 tree typetail, val;
3001 unsigned int parmnum;
06302a02 3002 bool error_args = false;
b5d32c25 3003 const bool type_generic = fundecl
81e5eca8 3004 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3005 bool type_generic_remove_excess_precision = false;
03dafa61 3006 tree selector;
03dafa61 3007
2ac2f164
JM
3008 /* Change pointer to function to the function itself for
3009 diagnostics. */
03dafa61
JM
3010 if (TREE_CODE (function) == ADDR_EXPR
3011 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3012 function = TREE_OPERAND (function, 0);
03dafa61
JM
3013
3014 /* Handle an ObjC selector specially for diagnostics. */
3015 selector = objc_message_selector ();
400fbf9f 3016
8ce94e44
JM
3017 /* For type-generic built-in functions, determine whether excess
3018 precision should be removed (classification) or not
3019 (comparison). */
3020 if (type_generic
3021 && DECL_BUILT_IN (fundecl)
3022 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3023 {
3024 switch (DECL_FUNCTION_CODE (fundecl))
3025 {
3026 case BUILT_IN_ISFINITE:
3027 case BUILT_IN_ISINF:
3028 case BUILT_IN_ISINF_SIGN:
3029 case BUILT_IN_ISNAN:
3030 case BUILT_IN_ISNORMAL:
3031 case BUILT_IN_FPCLASSIFY:
3032 type_generic_remove_excess_precision = true;
3033 break;
3034
3035 default:
3036 type_generic_remove_excess_precision = false;
3037 break;
3038 }
3039 }
b72271b9 3040 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3041 return vec_safe_length (values);
8ce94e44 3042
400fbf9f 3043 /* Scan the given expressions and types, producing individual
bbbbb16a 3044 converted arguments. */
400fbf9f 3045
bbbbb16a 3046 for (typetail = typelist, parmnum = 0;
9771b263 3047 values && values->iterate (parmnum, &val);
bbbbb16a 3048 ++parmnum)
400fbf9f 3049 {
b3694847 3050 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3051 tree valtype = TREE_TYPE (val);
03dafa61
JM
3052 tree rname = function;
3053 int argnum = parmnum + 1;
4d3e6fae 3054 const char *invalid_func_diag;
8ce94e44 3055 bool excess_precision = false;
928c19bb 3056 bool npc;
bbbbb16a 3057 tree parmval;
400fbf9f
JW
3058
3059 if (type == void_type_node)
3060 {
19dc6d01 3061 if (selector)
68fca595 3062 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3063 else
68fca595 3064 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3065 inform_declaration (fundecl);
94a0dd7b 3066 return parmnum;
400fbf9f
JW
3067 }
3068
03dafa61
JM
3069 if (selector && argnum > 2)
3070 {
3071 rname = selector;
3072 argnum -= 2;
3073 }
3074
928c19bb 3075 npc = null_pointer_constant_p (val);
8ce94e44
JM
3076
3077 /* If there is excess precision and a prototype, convert once to
3078 the required type rather than converting via the semantic
3079 type. Likewise without a prototype a float value represented
3080 as long double should be converted once to double. But for
3081 type-generic classification functions excess precision must
3082 be removed here. */
3083 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3084 && (type || !type_generic || !type_generic_remove_excess_precision))
3085 {
3086 val = TREE_OPERAND (val, 0);
3087 excess_precision = true;
3088 }
928c19bb 3089 val = c_fully_fold (val, false, NULL);
ed248cf7 3090 STRIP_TYPE_NOPS (val);
400fbf9f 3091
400fbf9f
JW
3092 val = require_complete_type (val);
3093
3094 if (type != 0)
3095 {
3096 /* Formal parm type is specified by a function prototype. */
400fbf9f 3097
20913689 3098 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f
JW
3099 {
3100 error ("type of formal parameter %d is incomplete", parmnum + 1);
3101 parmval = val;
3102 }
3103 else
3104 {
bbbbb16a
ILT
3105 tree origtype;
3106
d45cf215
RS
3107 /* Optionally warn about conversions that
3108 differ from the default conversions. */
05170031 3109 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3110 {
e3a64162 3111 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3112
aae43c5f 3113 if (INTEGRAL_TYPE_P (type)
8ce94e44 3114 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3115 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3116 "rather than floating due to prototype",
3117 argnum, rname);
03829ad2 3118 if (INTEGRAL_TYPE_P (type)
8ce94e44 3119 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3120 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3121 "rather than complex due to prototype",
3122 argnum, rname);
aae43c5f 3123 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3124 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3125 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3126 "rather than floating due to prototype",
3127 argnum, rname);
400fbf9f 3128 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3129 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3130 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3131 "rather than integer due to prototype",
3132 argnum, rname);
03829ad2 3133 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3134 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3135 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3136 "rather than integer due to prototype",
3137 argnum, rname);
aae43c5f 3138 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3139 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3140 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3141 "rather than complex due to prototype",
3142 argnum, rname);
aae43c5f
RK
3143 /* ??? At some point, messages should be written about
3144 conversions between complex types, but that's too messy
3145 to do now. */
d45cf215 3146 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3147 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3148 {
3149 /* Warn if any argument is passed as `float',
047de90b 3150 since without a prototype it would be `double'. */
9a8ce21f
JG
3151 if (formal_prec == TYPE_PRECISION (float_type_node)
3152 && type != dfloat32_type_node)
d4ee4d25 3153 warning (0, "passing argument %d of %qE as %<float%> "
03dafa61
JM
3154 "rather than %<double%> due to prototype",
3155 argnum, rname);
9a8ce21f
JG
3156
3157 /* Warn if mismatch between argument and prototype
3158 for decimal float types. Warn of conversions with
3159 binary float types and of precision narrowing due to
3160 prototype. */
8ce94e44 3161 else if (type != valtype
9a8ce21f
JG
3162 && (type == dfloat32_type_node
3163 || type == dfloat64_type_node
c22cacf3 3164 || type == dfloat128_type_node
8ce94e44
JM
3165 || valtype == dfloat32_type_node
3166 || valtype == dfloat64_type_node
3167 || valtype == dfloat128_type_node)
c22cacf3 3168 && (formal_prec
8ce94e44 3169 <= TYPE_PRECISION (valtype)
9a8ce21f 3170 || (type == dfloat128_type_node
8ce94e44 3171 && (valtype
c22cacf3 3172 != dfloat64_type_node
8ce94e44 3173 && (valtype
9a8ce21f
JG
3174 != dfloat32_type_node)))
3175 || (type == dfloat64_type_node
8ce94e44 3176 && (valtype
9a8ce21f
JG
3177 != dfloat32_type_node))))
3178 warning (0, "passing argument %d of %qE as %qT "
3179 "rather than %qT due to prototype",
8ce94e44 3180 argnum, rname, type, valtype);
9a8ce21f 3181
d45cf215 3182 }
3ed56f8a
KG
3183 /* Detect integer changing in width or signedness.
3184 These warnings are only activated with
05170031
MLI
3185 -Wtraditional-conversion, not with -Wtraditional. */
3186 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3187 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3188 {
d45cf215
RS
3189 tree would_have_been = default_conversion (val);
3190 tree type1 = TREE_TYPE (would_have_been);
3191
754a4d82 3192 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3193 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3194 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3195 /* No warning if function asks for enum
3196 and the actual arg is that enum type. */
3197 ;
3198 else if (formal_prec != TYPE_PRECISION (type1))
c2255bc4
AH
3199 warning (OPT_Wtraditional_conversion,
3200 "passing argument %d of %qE "
3176a0c2
DD
3201 "with different width due to prototype",
3202 argnum, rname);
8df83eae 3203 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3204 ;
800cd3b9
RS
3205 /* Don't complain if the formal parameter type
3206 is an enum, because we can't tell now whether
3207 the value was an enum--even the same enum. */
3208 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3209 ;
400fbf9f
JW
3210 else if (TREE_CODE (val) == INTEGER_CST
3211 && int_fits_type_p (val, type))
3212 /* Change in signedness doesn't matter
3213 if a constant value is unaffected. */
3214 ;
ce9895ae
RS
3215 /* If the value is extended from a narrower
3216 unsigned type, it doesn't matter whether we
3217 pass it as signed or unsigned; the value
3218 certainly is the same either way. */
8ce94e44
JM
3219 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3220 && TYPE_UNSIGNED (valtype))
ce9895ae 3221 ;
8df83eae 3222 else if (TYPE_UNSIGNED (type))
c2255bc4
AH
3223 warning (OPT_Wtraditional_conversion,
3224 "passing argument %d of %qE "
3176a0c2
DD
3225 "as unsigned due to prototype",
3226 argnum, rname);
3ed56f8a 3227 else
c2255bc4
AH
3228 warning (OPT_Wtraditional_conversion,
3229 "passing argument %d of %qE "
3176a0c2 3230 "as signed due to prototype", argnum, rname);
400fbf9f
JW
3231 }
3232 }
3233
8ce94e44
JM
3234 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3235 sake of better warnings from convert_and_check. */
3236 if (excess_precision)
3237 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3238 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
81e5eca8
MP
3239 bool arg_loc_ok = !arg_loc.is_empty ()
3240 /* Some __atomic_* builtins have additional
3241 hidden argument at position 0. */
3242 && values->length () == arg_loc.length ();
3243 parmval = convert_for_assignment (loc,
3244 arg_loc_ok ? arg_loc[parmnum]
3245 : UNKNOWN_LOCATION, type,
68fca595
MP
3246 val, origtype, ic_argpass,
3247 npc, fundecl, function,
2ac2f164 3248 parmnum + 1);
2f6e4e97 3249
61f71b34 3250 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3251 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3252 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3253 parmval = default_conversion (parmval);
400fbf9f 3254 }
400fbf9f 3255 }
8ce94e44
JM
3256 else if (TREE_CODE (valtype) == REAL_TYPE
3257 && (TYPE_PRECISION (valtype)
2531a1d9 3258 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3259 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3260 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3261 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3262 {
3263 if (type_generic)
bbbbb16a 3264 parmval = val;
b5d32c25 3265 else
0a0b3574
MM
3266 {
3267 /* Convert `float' to `double'. */
3268 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
81e5eca8 3269 warning_at (arg_loc[parmnum], OPT_Wdouble_promotion,
0a0b3574
MM
3270 "implicit conversion from %qT to %qT when passing "
3271 "argument to function",
3272 valtype, double_type_node);
3273 parmval = convert (double_type_node, val);
3274 }
b5d32c25 3275 }
8ce94e44
JM
3276 else if (excess_precision && !type_generic)
3277 /* A "double" argument with excess precision being passed
3278 without a prototype or in variable arguments. */
bbbbb16a 3279 parmval = convert (valtype, val);
c22cacf3
MS
3280 else if ((invalid_func_diag =
3281 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3282 {
3283 error (invalid_func_diag);
94a0dd7b 3284 return -1;
4d3e6fae 3285 }
400fbf9f
JW
3286 else
3287 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3288 parmval = default_conversion (val);
3289
9771b263 3290 (*values)[parmnum] = parmval;
06302a02
JJ
3291 if (parmval == error_mark_node)
3292 error_args = true;
400fbf9f
JW
3293
3294 if (typetail)
3295 typetail = TREE_CHAIN (typetail);
3296 }
3297
9771b263 3298 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3299
400fbf9f 3300 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3301 {
68fca595 3302 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3303 inform_declaration (fundecl);
3304 return -1;
3789b316 3305 }
400fbf9f 3306
06302a02 3307 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3308}
3309\f
43f6dfd3
RS
3310/* This is the entry point used by the parser to build unary operators
3311 in the input. CODE, a tree_code, specifies the unary operator, and
3312 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3313 CONVERT_EXPR for code.
3314
3315 LOC is the location to use for the tree generated.
3316*/
43f6dfd3
RS
3317
3318struct c_expr
c2255bc4 3319parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3320{
3321 struct c_expr result;
3322
c9f9eb5d 3323 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3324 result.original_code = code;
6866c6e8
ILT
3325 result.original_type = NULL;
3326
59c0753d 3327 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3328 overflow_warning (loc, result.value);
59c0753d 3329
43f6dfd3
RS
3330 return result;
3331}
3332
3333/* This is the entry point used by the parser to build binary operators
3334 in the input. CODE, a tree_code, specifies the binary operator, and
3335 ARG1 and ARG2 are the operands. In addition to constructing the
3336 expression, we check for operands that were written with other binary
ba47d38d
AH
3337 operators in a way that is likely to confuse the user.
3338
3339 LOCATION is the location of the binary operator. */
edc7c4ec 3340
487a92fe 3341struct c_expr
ba47d38d
AH
3342parser_build_binary_op (location_t location, enum tree_code code,
3343 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3344{
487a92fe 3345 struct c_expr result;
400fbf9f 3346
487a92fe
JM
3347 enum tree_code code1 = arg1.original_code;
3348 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3349 tree type1 = (arg1.original_type
3350 ? arg1.original_type
3351 : TREE_TYPE (arg1.value));
3352 tree type2 = (arg2.original_type
3353 ? arg2.original_type
3354 : TREE_TYPE (arg2.value));
400fbf9f 3355
ba47d38d
AH
3356 result.value = build_binary_op (location, code,
3357 arg1.value, arg2.value, 1);
487a92fe 3358 result.original_code = code;
6866c6e8 3359 result.original_type = NULL;
58bf601b 3360
487a92fe
JM
3361 if (TREE_CODE (result.value) == ERROR_MARK)
3362 return result;
400fbf9f 3363
ba47d38d
AH
3364 if (location != UNKNOWN_LOCATION)
3365 protected_set_expr_location (result.value, location);
3366
400fbf9f 3367 /* Check for cases such as x+y<<z which users are likely
487a92fe 3368 to misinterpret. */
400fbf9f 3369 if (warn_parentheses)
393e8e8b
MP
3370 warn_about_parentheses (location, code, code1, arg1.value, code2,
3371 arg2.value);
001af587 3372
ca409efd 3373 if (warn_logical_op)
393e8e8b 3374 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3375 code1, arg1.value, code2, arg2.value);
63a08740 3376
e994a705
RS
3377 /* Warn about comparisons against string literals, with the exception
3378 of testing for equality or inequality of a string literal with NULL. */
3379 if (code == EQ_EXPR || code == NE_EXPR)
3380 {
3381 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3382 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3383 warning_at (location, OPT_Waddress,
3384 "comparison with string literal results in unspecified behavior");
e994a705
RS
3385 }
3386 else if (TREE_CODE_CLASS (code) == tcc_comparison
3387 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3388 warning_at (location, OPT_Waddress,
3389 "comparison with string literal results in unspecified behavior");
e994a705 3390
b8698a0f
L
3391 if (TREE_OVERFLOW_P (result.value)
3392 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3393 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3394 overflow_warning (location, result.value);
400fbf9f 3395
6866c6e8
ILT
3396 /* Warn about comparisons of different enum types. */
3397 if (warn_enum_compare
3398 && TREE_CODE_CLASS (code) == tcc_comparison
3399 && TREE_CODE (type1) == ENUMERAL_TYPE
3400 && TREE_CODE (type2) == ENUMERAL_TYPE
3401 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3402 warning_at (location, OPT_Wenum_compare,
3403 "comparison between %qT and %qT",
3404 type1, type2);
3405
400fbf9f
JW
3406 return result;
3407}
3e4093b6 3408\f
3e4093b6
RS
3409/* Return a tree for the difference of pointers OP0 and OP1.
3410 The resulting tree has type int. */
293c9fdd 3411
3e4093b6 3412static tree
db3927fb 3413pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3414{
3e4093b6 3415 tree restype = ptrdiff_type_node;
36c5e70a 3416 tree result, inttype;
400fbf9f 3417
36c5e70a
BE
3418 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3419 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6
RS
3420 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3421 tree con0, con1, lit0, lit1;
3422 tree orig_op1 = op1;
400fbf9f 3423
36c5e70a
BE
3424 /* If the operands point into different address spaces, we need to
3425 explicitly convert them to pointers into the common address space
3426 before we can subtract the numerical address values. */
3427 if (as0 != as1)
3428 {
3429 addr_space_t as_common;
3430 tree common_type;
3431
3432 /* Determine the common superset address space. This is guaranteed
3433 to exist because the caller verified that comp_target_types
3434 returned non-zero. */
3435 if (!addr_space_superset (as0, as1, &as_common))
3436 gcc_unreachable ();
3437
3438 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3439 op0 = convert (common_type, op0);
3440 op1 = convert (common_type, op1);
3441 }
3442
3443 /* Determine integer type to perform computations in. This will usually
3444 be the same as the result type (ptrdiff_t), but may need to be a wider
3445 type if pointers for the address space are wider than ptrdiff_t. */
3446 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3447 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3448 else
3449 inttype = restype;
3450
3451
fcf73884 3452 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3453 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3454 "pointer of type %<void *%> used in subtraction");
3455 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3456 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3457 "pointer to a function used in subtraction");
400fbf9f 3458
3e4093b6
RS
3459 /* If the conversion to ptrdiff_type does anything like widening or
3460 converting a partial to an integral mode, we get a convert_expression
3461 that is in the way to do any simplifications.
3462 (fold-const.c doesn't know that the extra bits won't be needed.
3463 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3464 different mode in place.)
3465 So first try to find a common term here 'by hand'; we want to cover
3466 at least the cases that occur in legal static initializers. */
1043771b 3467 if (CONVERT_EXPR_P (op0)
1344f9a3
JM
3468 && (TYPE_PRECISION (TREE_TYPE (op0))
3469 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3470 con0 = TREE_OPERAND (op0, 0);
3471 else
3472 con0 = op0;
1043771b 3473 if (CONVERT_EXPR_P (op1)
1344f9a3
JM
3474 && (TYPE_PRECISION (TREE_TYPE (op1))
3475 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3476 con1 = TREE_OPERAND (op1, 0);
3477 else
3478 con1 = op1;
400fbf9f 3479
5de72424 3480 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3e4093b6
RS
3481 {
3482 lit0 = TREE_OPERAND (con0, 1);
3483 con0 = TREE_OPERAND (con0, 0);
3484 }
3485 else
3486 lit0 = integer_zero_node;
400fbf9f 3487
5de72424 3488 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
400fbf9f 3489 {
3e4093b6
RS
3490 lit1 = TREE_OPERAND (con1, 1);
3491 con1 = TREE_OPERAND (con1, 0);
400fbf9f
JW
3492 }
3493 else
3e4093b6
RS
3494 lit1 = integer_zero_node;
3495
3496 if (operand_equal_p (con0, con1, 0))
400fbf9f 3497 {
3e4093b6
RS
3498 op0 = lit0;
3499 op1 = lit1;
400fbf9f
JW
3500 }
3501
400fbf9f 3502
3e4093b6
RS
3503 /* First do the subtraction as integers;
3504 then drop through to build the divide operator.
3505 Do not do default conversions on the minus operator
3506 in case restype is a short type. */
400fbf9f 3507
db3927fb 3508 op0 = build_binary_op (loc,
36c5e70a
BE
3509 MINUS_EXPR, convert (inttype, op0),
3510 convert (inttype, op1), 0);
3e4093b6
RS
3511 /* This generates an error if op1 is pointer to incomplete type. */
3512 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3513 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3514
3e4093b6
RS
3515 /* This generates an error if op0 is pointer to incomplete type. */
3516 op1 = c_size_in_bytes (target_type);
400fbf9f 3517
f04dda30
MP
3518 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3519 error_at (loc, "arithmetic on pointer to an empty aggregate");
3520
3e4093b6 3521 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3522 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3523 op0, convert (inttype, op1));
3524
3525 /* Convert to final result type if necessary. */
3526 return convert (restype, result);
3e4093b6
RS
3527}
3528\f
267bac10
JM
3529/* Expand atomic compound assignments into an approriate sequence as
3530 specified by the C11 standard section 6.5.16.2.
3531 given
3532 _Atomic T1 E1
3533 T2 E2
3534 E1 op= E2
3535
3536 This sequence is used for all types for which these operations are
3537 supported.
3538
3539 In addition, built-in versions of the 'fe' prefixed routines may
3540 need to be invoked for floating point (real, complex or vector) when
3541 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3542
3543 T1 newval;
3544 T1 old;
3545 T1 *addr
3546 T2 val
3547 fenv_t fenv
3548
3549 addr = &E1;
3550 val = (E2);
3551 __atomic_load (addr, &old, SEQ_CST);
3552 feholdexcept (&fenv);
3553loop:
3554 newval = old op val;
3555 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3556 SEQ_CST))
3557 goto done;
3558 feclearexcept (FE_ALL_EXCEPT);
3559 goto loop:
3560done:
3561 feupdateenv (&fenv);
3562
3563 Also note that the compiler is simply issuing the generic form of
3564 the atomic operations. This requires temp(s) and has their address
3565 taken. The atomic processing is smart enough to figure out when the
3566 size of an object can utilize a lock-free version, and convert the
3567 built-in call to the appropriate lock-free routine. The optimizers
3568 will then dispose of any temps that are no longer required, and
3569 lock-free implementations are utilized as long as there is target
3570 support for the required size.
3571
3572 If the operator is NOP_EXPR, then this is a simple assignment, and
3573 an __atomic_store is issued to perform the assignment rather than
3574 the above loop.
3575
3576*/
3577
3578/* Build an atomic assignment at LOC, expanding into the proper
3579 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3580 the result of the operation, unless RETURN_OLD_P in which case
3581 return the old value of LHS (this is only for postincrement and
3582 postdecrement). */
3583static tree
3584build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3585 tree rhs, bool return_old_p)
3586{
3587 tree fndecl, func_call;
3588 vec<tree, va_gc> *params;
3589 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3590 tree old, old_addr;
3591 tree compound_stmt;
3592 tree stmt, goto_stmt;
3593 tree loop_label, loop_decl, done_label, done_decl;
3594
3595 tree lhs_type = TREE_TYPE (lhs);
3596 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3597 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3598 tree rhs_type = TREE_TYPE (rhs);
3599
3600 gcc_assert (TYPE_ATOMIC (lhs_type));
3601
3602 if (return_old_p)
3603 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3604
3605 /* Allocate enough vector items for a compare_exchange. */
3606 vec_alloc (params, 6);
3607
3608 /* Create a compound statement to hold the sequence of statements
3609 with a loop. */
3610 compound_stmt = c_begin_compound_stmt (false);
3611
3612 /* Fold the RHS if it hasn't already been folded. */
3613 if (modifycode != NOP_EXPR)
3614 rhs = c_fully_fold (rhs, false, NULL);
3615
3616 /* Remove the qualifiers for the rest of the expressions and create
3617 the VAL temp variable to hold the RHS. */
3618 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3619 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3620 val = create_tmp_var (nonatomic_rhs_type, NULL);
3621 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3622 TREE_NO_WARNING (val) = 1;
267bac10
JM
3623 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3624 SET_EXPR_LOCATION (rhs, loc);
3625 add_stmt (rhs);
3626
3627 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3628 an atomic_store. */
3629 if (modifycode == NOP_EXPR)
3630 {
3631 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3632 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3633 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3634 params->quick_push (lhs_addr);
3635 params->quick_push (rhs);
3636 params->quick_push (seq_cst);
81e5eca8 3637 func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3638 add_stmt (func_call);
3639
3640 /* Finish the compound statement. */
3641 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3642
3643 /* VAL is the value which was stored, return a COMPOUND_STMT of
3644 the statement and that value. */
3645 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3646 }
3647
3648 /* Create the variables and labels required for the op= form. */
3649 old = create_tmp_var (nonatomic_lhs_type, NULL);
3650 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3651 TREE_ADDRESSABLE (old) = 1;
3652 TREE_NO_WARNING (old) = 1;
267bac10
JM
3653
3654 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3655 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3656 TREE_ADDRESSABLE (newval) = 1;
3657
3658 loop_decl = create_artificial_label (loc);
3659 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3660
3661 done_decl = create_artificial_label (loc);
3662 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3663
3664 /* __atomic_load (addr, &old, SEQ_CST). */
3665 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3666 params->quick_push (lhs_addr);
3667 params->quick_push (old_addr);
3668 params->quick_push (seq_cst);
81e5eca8 3669 func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3670 add_stmt (func_call);
3671 params->truncate (0);
3672
3673 /* Create the expressions for floating-point environment
3674 manipulation, if required. */
3675 bool need_fenv = (flag_trapping_math
3676 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3677 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3678 if (need_fenv)
3679 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3680
3681 if (hold_call)
3682 add_stmt (hold_call);
3683
3684 /* loop: */
3685 add_stmt (loop_label);
3686
3687 /* newval = old + val; */
3688 rhs = build_binary_op (loc, modifycode, old, val, 1);
68fca595
MP
3689 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3690 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3691 NULL_TREE, 0);
3692 if (rhs != error_mark_node)
3693 {
3694 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3695 SET_EXPR_LOCATION (rhs, loc);
3696 add_stmt (rhs);
3697 }
3698
3699 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3700 goto done; */
3701 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3702 params->quick_push (lhs_addr);
3703 params->quick_push (old_addr);
3704 params->quick_push (newval_addr);
3705 params->quick_push (integer_zero_node);
3706 params->quick_push (seq_cst);
3707 params->quick_push (seq_cst);
81e5eca8 3708 func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3709
3710 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3711 SET_EXPR_LOCATION (goto_stmt, loc);
3712
3713 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3714 SET_EXPR_LOCATION (stmt, loc);
3715 add_stmt (stmt);
3716
3717 if (clear_call)
3718 add_stmt (clear_call);
3719
3720 /* goto loop; */
3721 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3722 SET_EXPR_LOCATION (goto_stmt, loc);
3723 add_stmt (goto_stmt);
3724
3725 /* done: */
3726 add_stmt (done_label);
3727
3728 if (update_call)
3729 add_stmt (update_call);
3730
3731 /* Finish the compound statement. */
3732 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3733
3734 /* NEWVAL is the value that was successfully stored, return a
3735 COMPOUND_EXPR of the statement and the appropriate value. */
3736 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3737 return_old_p ? old : newval);
3738}
3739
3e4093b6
RS
3740/* Construct and perhaps optimize a tree representation
3741 for a unary operation. CODE, a tree_code, specifies the operation
3742 and XARG is the operand.
3743 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3744 the default promotions (such as from short to int).
3745 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3746 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3747 arrays to pointers in C99.
3748
3749 LOCATION is the location of the operator. */
400fbf9f 3750
3e4093b6 3751tree
c9f9eb5d
AH
3752build_unary_op (location_t location,
3753 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3754{
3755 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3756 tree arg = xarg;
3757 tree argtype = 0;
808d6eaa 3758 enum tree_code typecode;
3e4093b6 3759 tree val;
c9f9eb5d 3760 tree ret = error_mark_node;
8ce94e44 3761 tree eptype = NULL_TREE;
3e4093b6 3762 int noconvert = flag;
4de67c26 3763 const char *invalid_op_diag;
928c19bb
JM
3764 bool int_operands;
3765
3766 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3767 if (int_operands)
3768 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3769
808d6eaa
JM
3770 if (code != ADDR_EXPR)
3771 arg = require_complete_type (arg);
3772
3773 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3774 if (typecode == ERROR_MARK)
3775 return error_mark_node;
3776 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3777 typecode = INTEGER_TYPE;
6c36d76b 3778
4de67c26
JM
3779 if ((invalid_op_diag
3780 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3781 {
c9f9eb5d 3782 error_at (location, invalid_op_diag);
4de67c26
JM
3783 return error_mark_node;
3784 }
3785
8ce94e44
JM
3786 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3787 {
3788 eptype = TREE_TYPE (arg);
3789 arg = TREE_OPERAND (arg, 0);
3790 }
3791
3e4093b6
RS
3792 switch (code)
3793 {
3794 case CONVERT_EXPR:
3795 /* This is used for unary plus, because a CONVERT_EXPR
3796 is enough to prevent anybody from looking inside for
3797 associativity, but won't generate any code. */
3798 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3799 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3800 || typecode == VECTOR_TYPE))
400fbf9f 3801 {
c9f9eb5d 3802 error_at (location, "wrong type argument to unary plus");
3e4093b6 3803 return error_mark_node;
400fbf9f 3804 }
3e4093b6
RS
3805 else if (!noconvert)
3806 arg = default_conversion (arg);
db3927fb 3807 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3808 break;
3809
3e4093b6
RS
3810 case NEGATE_EXPR:
3811 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3812 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3813 || typecode == VECTOR_TYPE))
3814 {
c9f9eb5d 3815 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3816 return error_mark_node;
3817 }
3818 else if (!noconvert)
3819 arg = default_conversion (arg);
400fbf9f
JW
3820 break;
3821
3e4093b6 3822 case BIT_NOT_EXPR:
462643f0
AP
3823 /* ~ works on integer types and non float vectors. */
3824 if (typecode == INTEGER_TYPE
3825 || (typecode == VECTOR_TYPE
3826 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3827 {
3e4093b6
RS
3828 if (!noconvert)
3829 arg = default_conversion (arg);
03d5b1f5 3830 }
3e4093b6 3831 else if (typecode == COMPLEX_TYPE)
400fbf9f 3832 {
3e4093b6 3833 code = CONJ_EXPR;
c1771a20 3834 pedwarn (location, OPT_Wpedantic,
fcf73884 3835 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3836 if (!noconvert)
3837 arg = default_conversion (arg);
3838 }
3839 else
3840 {
c9f9eb5d 3841 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3842 return error_mark_node;
400fbf9f
JW
3843 }
3844 break;
3845
3e4093b6 3846 case ABS_EXPR:
11017cc7 3847 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3848 {
c9f9eb5d 3849 error_at (location, "wrong type argument to abs");
3e4093b6 3850 return error_mark_node;
400fbf9f 3851 }
3e4093b6
RS
3852 else if (!noconvert)
3853 arg = default_conversion (arg);
400fbf9f
JW
3854 break;
3855
3e4093b6
RS
3856 case CONJ_EXPR:
3857 /* Conjugating a real value is a no-op, but allow it anyway. */
3858 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3859 || typecode == COMPLEX_TYPE))
400fbf9f 3860 {
c9f9eb5d 3861 error_at (location, "wrong type argument to conjugation");
3e4093b6 3862 return error_mark_node;
400fbf9f 3863 }
3e4093b6
RS
3864 else if (!noconvert)
3865 arg = default_conversion (arg);
400fbf9f
JW
3866 break;
3867
3e4093b6 3868 case TRUTH_NOT_EXPR:
ab22c1fa 3869 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3870 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3871 && typecode != COMPLEX_TYPE)
400fbf9f 3872 {
c9f9eb5d
AH
3873 error_at (location,
3874 "wrong type argument to unary exclamation mark");
3e4093b6 3875 return error_mark_node;
400fbf9f 3876 }
a27d595d
JM
3877 if (int_operands)
3878 {
3879 arg = c_objc_common_truthvalue_conversion (location, xarg);
3880 arg = remove_c_maybe_const_expr (arg);
3881 }
3882 else
3883 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3884 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3885 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3886 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3887 location = EXPR_LOCATION (ret);
c9f9eb5d 3888 goto return_build_unary_op;
3e4093b6 3889
3e4093b6 3890 case REALPART_EXPR:
3e4093b6 3891 case IMAGPART_EXPR:
fb52b50a
NF
3892 ret = build_real_imag_expr (location, code, arg);
3893 if (ret == error_mark_node)
3894 return error_mark_node;
8ce94e44
JM
3895 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3896 eptype = TREE_TYPE (eptype);
c9f9eb5d 3897 goto return_build_unary_op;
3e4093b6
RS
3898
3899 case PREINCREMENT_EXPR:
3900 case POSTINCREMENT_EXPR:
3901 case PREDECREMENT_EXPR:
3902 case POSTDECREMENT_EXPR:
3e4093b6 3903
928c19bb
JM
3904 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3905 {
3906 tree inner = build_unary_op (location, code,
3907 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3908 if (inner == error_mark_node)
3909 return error_mark_node;
3910 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3911 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3912 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3913 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3914 goto return_build_unary_op;
3915 }
3916
925e8657
NP
3917 /* Complain about anything that is not a true lvalue. In
3918 Objective-C, skip this check for property_refs. */
f90e8e2e 3919 if (!objc_is_property_ref (arg)
7bd11157
TT
3920 && !lvalue_or_else (location,
3921 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3922 || code == POSTINCREMENT_EXPR)
3923 ? lv_increment
3924 : lv_decrement)))
928c19bb
JM
3925 return error_mark_node;
3926
09639a83
ILT
3927 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3928 {
3929 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3930 warning_at (location, OPT_Wc___compat,
3931 "increment of enumeration value is invalid in C++");
3932 else
3933 warning_at (location, OPT_Wc___compat,
3934 "decrement of enumeration value is invalid in C++");
3935 }
3936
928c19bb
JM
3937 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3938 arg = c_fully_fold (arg, false, NULL);
3939
267bac10
JM
3940 bool atomic_op;
3941 atomic_op = really_atomic_lvalue (arg);
3942
3e4093b6
RS
3943 /* Increment or decrement the real part of the value,
3944 and don't change the imaginary part. */
3945 if (typecode == COMPLEX_TYPE)
400fbf9f 3946 {
3e4093b6
RS
3947 tree real, imag;
3948
c1771a20 3949 pedwarn (location, OPT_Wpedantic,
509c9d60 3950 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3951
267bac10
JM
3952 if (!atomic_op)
3953 {
3954 arg = stabilize_reference (arg);
3955 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3956 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3957 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3958 if (real == error_mark_node || imag == error_mark_node)
3959 return error_mark_node;
3960 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3961 real, imag);
3962 goto return_build_unary_op;
3963 }
400fbf9f 3964 }
3e4093b6
RS
3965
3966 /* Report invalid types. */
3967
ab22c1fa 3968 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 3969 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 3970 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 3971 {
3e4093b6 3972 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 3973 error_at (location, "wrong type argument to increment");
c22cacf3 3974 else
c9f9eb5d 3975 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
3976
3977 return error_mark_node;
400fbf9f 3978 }
400fbf9f 3979
3e4093b6
RS
3980 {
3981 tree inc;
400fbf9f 3982
3e4093b6
RS
3983 argtype = TREE_TYPE (arg);
3984
3985 /* Compute the increment. */
3986
3987 if (typecode == POINTER_TYPE)
3988 {
3989 /* If pointer target is an undefined struct,
3990 we just cannot know how to do the arithmetic. */
b70cef5d 3991 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
3992 {
3993 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d
AH
3994 error_at (location,
3995 "increment of pointer to unknown structure");
3e4093b6 3996 else
c9f9eb5d
AH
3997 error_at (location,
3998 "decrement of pointer to unknown structure");
3e4093b6 3999 }
b70cef5d
JJ
4000 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4001 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4002 {
3e4093b6 4003 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4004 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4005 "wrong type argument to increment");
3e4093b6 4006 else
44d90fe1 4007 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4008 "wrong type argument to decrement");
3e4093b6
RS
4009 }
4010
b70cef5d 4011 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4012 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4013 }
b70cef5d 4014 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4015 {
4016 /* For signed fract types, we invert ++ to -- or
4017 -- to ++, and change inc from 1 to -1, because
4018 it is not possible to represent 1 in signed fract constants.
4019 For unsigned fract types, the result always overflows and
4020 we get an undefined (original) or the maximum value. */
4021 if (code == PREINCREMENT_EXPR)
4022 code = PREDECREMENT_EXPR;
4023 else if (code == PREDECREMENT_EXPR)
4024 code = PREINCREMENT_EXPR;
4025 else if (code == POSTINCREMENT_EXPR)
4026 code = POSTDECREMENT_EXPR;
4027 else /* code == POSTDECREMENT_EXPR */
4028 code = POSTINCREMENT_EXPR;
4029
4030 inc = integer_minus_one_node;
4031 inc = convert (argtype, inc);
4032 }
3e4093b6 4033 else
5be014d5 4034 {
241b71bb
TV
4035 inc = VECTOR_TYPE_P (argtype)
4036 ? build_one_cst (argtype)
4037 : integer_one_node;
5be014d5
AP
4038 inc = convert (argtype, inc);
4039 }
3e4093b6 4040
925e8657
NP
4041 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4042 need to ask Objective-C to build the increment or decrement
4043 expression for it. */
4044 if (objc_is_property_ref (arg))
f90e8e2e 4045 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4046 arg, inc);
4047
3e4093b6 4048 /* Report a read-only lvalue. */
f37acdf9 4049 if (TYPE_READONLY (argtype))
953ff289 4050 {
c02065fc 4051 readonly_error (location, arg,
953ff289
DN
4052 ((code == PREINCREMENT_EXPR
4053 || code == POSTINCREMENT_EXPR)
4054 ? lv_increment : lv_decrement));
4055 return error_mark_node;
4056 }
f37acdf9
JM
4057 else if (TREE_READONLY (arg))
4058 readonly_warning (arg,
4059 ((code == PREINCREMENT_EXPR
4060 || code == POSTINCREMENT_EXPR)
4061 ? lv_increment : lv_decrement));
3e4093b6 4062
267bac10
JM
4063 /* If the argument is atomic, use the special code sequences for
4064 atomic compound assignment. */
4065 if (atomic_op)
4066 {
4067 arg = stabilize_reference (arg);
4068 ret = build_atomic_assign (location, arg,
4069 ((code == PREINCREMENT_EXPR
4070 || code == POSTINCREMENT_EXPR)
4071 ? PLUS_EXPR
4072 : MINUS_EXPR),
4073 (FRACT_MODE_P (TYPE_MODE (argtype))
4074 ? inc
4075 : integer_one_node),
4076 (code == POSTINCREMENT_EXPR
4077 || code == POSTDECREMENT_EXPR));
4078 goto return_build_unary_op;
4079 }
4080
3e4093b6
RS
4081 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4082 val = boolean_increment (code, arg);
4083 else
53fb4de3 4084 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4085 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4086 if (TREE_CODE (val) != code)
6de9cd9a 4087 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4088 ret = val;
4089 goto return_build_unary_op;
3e4093b6
RS
4090 }
4091
4092 case ADDR_EXPR:
4093 /* Note that this operation never does default_conversion. */
4094
2b4b7036
JM
4095 /* The operand of unary '&' must be an lvalue (which excludes
4096 expressions of type void), or, in C99, the result of a [] or
4097 unary '*' operator. */
4098 if (VOID_TYPE_P (TREE_TYPE (arg))
4099 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4100 && (TREE_CODE (arg) != INDIRECT_REF
4101 || !flag_isoc99))
4102 pedwarn (location, 0, "taking address of expression of type %<void%>");
4103
3e4093b6
RS
4104 /* Let &* cancel out to simplify resulting code. */
4105 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 4106 {
3e4093b6
RS
4107 /* Don't let this be an lvalue. */
4108 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4109 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4110 ret = TREE_OPERAND (arg, 0);
4111 goto return_build_unary_op;
400fbf9f 4112 }
1eb8759b 4113
7c672dfc 4114 /* For &x[y], return x+y */
3e4093b6 4115 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4116 {
f2a71bbc
JM
4117 tree op0 = TREE_OPERAND (arg, 0);
4118 if (!c_mark_addressable (op0))
3e4093b6 4119 return error_mark_node;
1eb8759b 4120 }
1eb8759b 4121
3e4093b6
RS
4122 /* Anything not already handled and not a true memory reference
4123 or a non-lvalue array is an error. */
4124 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4125 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4126 return error_mark_node;
b6a10c9f 4127
928c19bb
JM
4128 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4129 folding later. */
4130 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4131 {
4132 tree inner = build_unary_op (location, code,
4133 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4134 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4135 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4136 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4137 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4138 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4139 goto return_build_unary_op;
4140 }
4141
3e4093b6
RS
4142 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4143 argtype = TREE_TYPE (arg);
400fbf9f 4144
3e4093b6 4145 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4146 to which the address will point. This is only needed
f2c1da78 4147 for function types. */
6615c446 4148 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4149 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4150 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4151 {
4152 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4153 int quals = orig_quals;
4154
4155 if (TREE_READONLY (arg))
4156 quals |= TYPE_QUAL_CONST;
4157 if (TREE_THIS_VOLATILE (arg))
4158 quals |= TYPE_QUAL_VOLATILE;
4159
f2c1da78
JM
4160 argtype = c_build_qualified_type (argtype, quals);
4161 }
400fbf9f 4162
3e4093b6
RS
4163 if (!c_mark_addressable (arg))
4164 return error_mark_node;
400fbf9f 4165
abb54d14
JM
4166 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4167 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4168
5cc200fc 4169 argtype = build_pointer_type (argtype);
5e55f99d
RH
4170
4171 /* ??? Cope with user tricks that amount to offsetof. Delete this
4172 when we have proper support for integer constant expressions. */
4173 val = get_base_address (arg);
4174 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
4175 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4176 {
cf9e9959 4177 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4178 goto return_build_unary_op;
3aa2ddb8 4179 }
5e55f99d 4180
5cc200fc 4181 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4182
c9f9eb5d
AH
4183 ret = val;
4184 goto return_build_unary_op;
400fbf9f 4185
3e4093b6 4186 default:
1344f9a3 4187 gcc_unreachable ();
3e4093b6 4188 }
400fbf9f 4189
3e4093b6
RS
4190 if (argtype == 0)
4191 argtype = TREE_TYPE (arg);
928c19bb
JM
4192 if (TREE_CODE (arg) == INTEGER_CST)
4193 ret = (require_constant_value
db3927fb
AH
4194 ? fold_build1_initializer_loc (location, code, argtype, arg)
4195 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4196 else
4197 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4198 return_build_unary_op:
4199 gcc_assert (ret != error_mark_node);
928c19bb
JM
4200 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4201 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4202 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4203 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4204 ret = note_integer_operands (ret);
8ce94e44
JM
4205 if (eptype)
4206 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4207 protected_set_expr_location (ret, location);
4208 return ret;
3e4093b6 4209}
400fbf9f 4210
3e4093b6
RS
4211/* Return nonzero if REF is an lvalue valid for this language.
4212 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4213 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4214
1e4ae551 4215bool
58f9752a 4216lvalue_p (const_tree ref)
3e4093b6 4217{
58f9752a 4218 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4219
3e4093b6
RS
4220 switch (code)
4221 {
4222 case REALPART_EXPR:
4223 case IMAGPART_EXPR:
4224 case COMPONENT_REF:
4225 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4226
928c19bb
JM
4227 case C_MAYBE_CONST_EXPR:
4228 return lvalue_p (TREE_OPERAND (ref, 1));
4229
3e4093b6
RS
4230 case COMPOUND_LITERAL_EXPR:
4231 case STRING_CST:
4232 return 1;
400fbf9f 4233
3e4093b6
RS
4234 case INDIRECT_REF:
4235 case ARRAY_REF:
36536d79 4236 case ARRAY_NOTATION_REF:
3e4093b6
RS
4237 case VAR_DECL:
4238 case PARM_DECL:
4239 case RESULT_DECL:
4240 case ERROR_MARK:
4241 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4242 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4243
3e4093b6 4244 case BIND_EXPR:
3e4093b6 4245 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4246
3e4093b6
RS
4247 default:
4248 return 0;
4249 }
4250}
400fbf9f 4251\f
f37acdf9
JM
4252/* Give a warning for storing in something that is read-only in GCC
4253 terms but not const in ISO C terms. */
4254
4255static void
4256readonly_warning (tree arg, enum lvalue_use use)
4257{
4258 switch (use)
4259 {
4260 case lv_assign:
4261 warning (0, "assignment of read-only location %qE", arg);
4262 break;
4263 case lv_increment:
4264 warning (0, "increment of read-only location %qE", arg);
4265 break;
4266 case lv_decrement:
4267 warning (0, "decrement of read-only location %qE", arg);
4268 break;
4269 default:
4270 gcc_unreachable ();
4271 }
4272 return;
4273}
4274
37dc0d8d
JM
4275
4276/* Return nonzero if REF is an lvalue valid for this language;
4277 otherwise, print an error message and return zero. USE says
7bd11157
TT
4278 how the lvalue is being used and so selects the error message.
4279 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4280
4281static int
7bd11157 4282lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4283{
4284 int win = lvalue_p (ref);
4285
4286 if (!win)
7bd11157 4287 lvalue_error (loc, use);
37dc0d8d
JM
4288
4289 return win;
4290}
3e4093b6
RS
4291\f
4292/* Mark EXP saying that we need to be able to take the
4293 address of it; it should not be allocated in a register.
4294 Returns true if successful. */
54c93c30 4295
3e4093b6
RS
4296bool
4297c_mark_addressable (tree exp)
400fbf9f 4298{
3e4093b6 4299 tree x = exp;
95602da1 4300
3e4093b6
RS
4301 while (1)
4302 switch (TREE_CODE (x))
4303 {
4304 case COMPONENT_REF:
4305 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4306 {
0039fa55
AN
4307 error
4308 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4309 return false;
4310 }
95602da1 4311
3e4093b6 4312 /* ... fall through ... */
95602da1 4313
3e4093b6
RS
4314 case ADDR_EXPR:
4315 case ARRAY_REF:
4316 case REALPART_EXPR:
4317 case IMAGPART_EXPR:
4318 x = TREE_OPERAND (x, 0);
4319 break;
95602da1 4320
3e4093b6
RS
4321 case COMPOUND_LITERAL_EXPR:
4322 case CONSTRUCTOR:
4323 TREE_ADDRESSABLE (x) = 1;
4324 return true;
95602da1 4325
3e4093b6
RS
4326 case VAR_DECL:
4327 case CONST_DECL:
4328 case PARM_DECL:
4329 case RESULT_DECL:
5baeaac0 4330 if (C_DECL_REGISTER (x)
3e4093b6
RS
4331 && DECL_NONLOCAL (x))
4332 {
e697b20f 4333 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4334 {
0039fa55
AN
4335 error
4336 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4337 return false;
4338 }
509c9d60 4339 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4340 }
5baeaac0 4341 else if (C_DECL_REGISTER (x))
3e4093b6 4342 {
e697b20f 4343 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4344 error ("address of global register variable %qD requested", x);
4345 else
4346 error ("address of register variable %qD requested", x);
4347 return false;
3e4093b6 4348 }
400fbf9f 4349
3e4093b6
RS
4350 /* drops in */
4351 case FUNCTION_DECL:
4352 TREE_ADDRESSABLE (x) = 1;
4353 /* drops out */
4354 default:
4355 return true;
4356 }
4357}
4358\f
2d2e923f
MLI
4359/* Convert EXPR to TYPE, warning about conversion problems with
4360 constants. SEMANTIC_TYPE is the type this conversion would use
4361 without excess precision. If SEMANTIC_TYPE is NULL, this function
4362 is equivalent to convert_and_check. This function is a wrapper that
4363 handles conversions that may be different than
4364 the usual ones because of excess precision. */
4365
4366static tree
68fca595
MP
4367ep_convert_and_check (location_t loc, tree type, tree expr,
4368 tree semantic_type)
2d2e923f
MLI
4369{
4370 if (TREE_TYPE (expr) == type)
4371 return expr;
4372
4373 if (!semantic_type)
68fca595 4374 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4375
4376 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4377 && TREE_TYPE (expr) != semantic_type)
4378 {
4379 /* For integers, we need to check the real conversion, not
4380 the conversion to the excess precision type. */
68fca595 4381 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4382 }
4383 /* Result type is the excess precision type, which should be
4384 large enough, so do not check. */
4385 return convert (type, expr);
4386}
4387
928c19bb
JM
4388/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4389 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4390 if folded to an integer constant then the unselected half may
4391 contain arbitrary operations not normally permitted in constant
c2255bc4 4392 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4393
4394tree
744aa42f 4395build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4396 tree op1, tree op1_original_type, tree op2,
4397 tree op2_original_type)
400fbf9f 4398{
3e4093b6
RS
4399 tree type1;
4400 tree type2;
4401 enum tree_code code1;
4402 enum tree_code code2;
4403 tree result_type = NULL;
2d2e923f 4404 tree semantic_result_type = NULL;
3e4093b6 4405 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4406 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4407 bool ifexp_int_operands;
928c19bb 4408 tree ret;
400fbf9f 4409
4d84fe7c
JM
4410 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4411 if (op1_int_operands)
4412 op1 = remove_c_maybe_const_expr (op1);
4413 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4414 if (op2_int_operands)
4415 op2 = remove_c_maybe_const_expr (op2);
4416 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4417 if (ifexp_int_operands)
4418 ifexp = remove_c_maybe_const_expr (ifexp);
4419
3e4093b6
RS
4420 /* Promote both alternatives. */
4421
4422 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4423 op1 = default_conversion (op1);
4424 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4425 op2 = default_conversion (op2);
4426
4427 if (TREE_CODE (ifexp) == ERROR_MARK
4428 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4429 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4430 return error_mark_node;
400fbf9f 4431
3e4093b6
RS
4432 type1 = TREE_TYPE (op1);
4433 code1 = TREE_CODE (type1);
4434 type2 = TREE_TYPE (op2);
4435 code2 = TREE_CODE (type2);
4436
b1adf557
JM
4437 /* C90 does not permit non-lvalue arrays in conditional expressions.
4438 In C99 they will be pointers by now. */
4439 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4440 {
744aa42f 4441 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4442 return error_mark_node;
4443 }
4444
8ce94e44
JM
4445 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4446 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4447 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4448 || code1 == COMPLEX_TYPE)
4449 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4450 || code2 == COMPLEX_TYPE))
4451 {
2d2e923f 4452 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4453 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4454 {
4455 op1 = TREE_OPERAND (op1, 0);
4456 type1 = TREE_TYPE (op1);
4457 gcc_assert (TREE_CODE (type1) == code1);
4458 }
4459 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4460 {
4461 op2 = TREE_OPERAND (op2, 0);
4462 type2 = TREE_TYPE (op2);
4463 gcc_assert (TREE_CODE (type2) == code2);
4464 }
4465 }
4466
d130ae11
ILT
4467 if (warn_cxx_compat)
4468 {
4469 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4470 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4471
4472 if (TREE_CODE (t1) == ENUMERAL_TYPE
4473 && TREE_CODE (t2) == ENUMERAL_TYPE
4474 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4475 warning_at (colon_loc, OPT_Wc___compat,
4476 ("different enum types in conditional is "
4477 "invalid in C++: %qT vs %qT"),
4478 t1, t2);
4479 }
4480
3e4093b6
RS
4481 /* Quickly detect the usual case where op1 and op2 have the same type
4482 after promotion. */
4483 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4484 {
3e4093b6
RS
4485 if (type1 == type2)
4486 result_type = type1;
4487 else
4488 result_type = TYPE_MAIN_VARIANT (type1);
4489 }
4490 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4491 || code1 == COMPLEX_TYPE)
4492 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4493 || code2 == COMPLEX_TYPE))
3e4093b6 4494 {
ccf7f880 4495 result_type = c_common_type (type1, type2);
0a0b3574
MM
4496 do_warn_double_promotion (result_type, type1, type2,
4497 "implicit conversion from %qT to %qT to "
4498 "match other result of conditional",
4499 colon_loc);
400fbf9f 4500
3e4093b6
RS
4501 /* If -Wsign-compare, warn here if type1 and type2 have
4502 different signedness. We'll promote the signed to unsigned
4503 and later code won't know it used to be different.
4504 Do this check on the original types, so that explicit casts
4505 will be considered, but default promotions won't. */
7d882b83 4506 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4507 {
8df83eae
RK
4508 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4509 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4510
3e4093b6
RS
4511 if (unsigned_op1 ^ unsigned_op2)
4512 {
6ac01510
ILT
4513 bool ovf;
4514
3e4093b6
RS
4515 /* Do not warn if the result type is signed, since the
4516 signed type will only be chosen if it can represent
4517 all the values of the unsigned type. */
3f75a254 4518 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4519 /* OK */;
3e4093b6 4520 else
928c19bb
JM
4521 {
4522 bool op1_maybe_const = true;
4523 bool op2_maybe_const = true;
4524
4525 /* Do not warn if the signed quantity is an
4526 unsuffixed integer literal (or some static
4527 constant expression involving such literals) and
4528 it is non-negative. This warning requires the
4529 operands to be folded for best results, so do
4530 that folding in this case even without
4531 warn_sign_compare to avoid warning options
4532 possibly affecting code generation. */
f5178456
RS
4533 c_inhibit_evaluation_warnings
4534 += (ifexp == truthvalue_false_node);
928c19bb
JM
4535 op1 = c_fully_fold (op1, require_constant_value,
4536 &op1_maybe_const);
f5178456
RS
4537 c_inhibit_evaluation_warnings
4538 -= (ifexp == truthvalue_false_node);
4539
4540 c_inhibit_evaluation_warnings
4541 += (ifexp == truthvalue_true_node);
928c19bb
JM
4542 op2 = c_fully_fold (op2, require_constant_value,
4543 &op2_maybe_const);
f5178456
RS
4544 c_inhibit_evaluation_warnings
4545 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4546
4547 if (warn_sign_compare)
4548 {
4549 if ((unsigned_op2
4550 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4551 || (unsigned_op1
4552 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4553 /* OK */;
4554 else
744aa42f
ILT
4555 warning_at (colon_loc, OPT_Wsign_compare,
4556 ("signed and unsigned type in "
4557 "conditional expression"));
928c19bb
JM
4558 }
4559 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4560 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4561 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4562 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4563 }
3e4093b6
RS
4564 }
4565 }
4566 }
4567 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4568 {
fcf73884 4569 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4570 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4571 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4572 result_type = void_type_node;
4573 }
4574 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4575 {
36c5e70a
BE
4576 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4577 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4578 addr_space_t as_common;
4579
744aa42f 4580 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4581 result_type = common_pointer_type (type1, type2);
6aa3c60d 4582 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4583 result_type = type2;
6aa3c60d 4584 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4585 result_type = type1;
4586 else if (!addr_space_superset (as1, as2, &as_common))
4587 {
4588 error_at (colon_loc, "pointers to disjoint address spaces "
4589 "used in conditional expression");
4590 return error_mark_node;
4591 }
267bac10
JM
4592 else if (VOID_TYPE_P (TREE_TYPE (type1))
4593 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4594 {
fcf73884 4595 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4596 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4597 "ISO C forbids conditional expr between "
bda67431 4598 "%<void *%> and function pointer");
3e4093b6
RS
4599 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4600 TREE_TYPE (type2)));
34a80643 4601 }
267bac10
JM
4602 else if (VOID_TYPE_P (TREE_TYPE (type2))
4603 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4604 {
fcf73884 4605 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4606 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4607 "ISO C forbids conditional expr between "
bda67431 4608 "%<void *%> and function pointer");
3e4093b6
RS
4609 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4610 TREE_TYPE (type1)));
1c2a9b35 4611 }
b581b85b
NP
4612 /* Objective-C pointer comparisons are a bit more lenient. */
4613 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4614 result_type = objc_common_type (type1, type2);
34a80643 4615 else
ab87f8c8 4616 {
36c5e70a
BE
4617 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4618
b581b85b
NP
4619 pedwarn (colon_loc, 0,
4620 "pointer type mismatch in conditional expression");
36c5e70a
BE
4621 result_type = build_pointer_type
4622 (build_qualified_type (void_type_node, qual));
ab87f8c8 4623 }
3e4093b6
RS
4624 }
4625 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4626 {
6aa3c60d 4627 if (!null_pointer_constant_p (orig_op2))
744aa42f 4628 pedwarn (colon_loc, 0,
509c9d60 4629 "pointer/integer type mismatch in conditional expression");
3e4093b6 4630 else
ab87f8c8 4631 {
3e4093b6 4632 op2 = null_pointer_node;
ab87f8c8 4633 }
3e4093b6
RS
4634 result_type = type1;
4635 }
4636 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4637 {
6aa3c60d 4638 if (!null_pointer_constant_p (orig_op1))
744aa42f 4639 pedwarn (colon_loc, 0,
509c9d60 4640 "pointer/integer type mismatch in conditional expression");
3e4093b6 4641 else
ab87f8c8 4642 {
3e4093b6 4643 op1 = null_pointer_node;
ab87f8c8 4644 }
3e4093b6
RS
4645 result_type = type2;
4646 }
1c2a9b35 4647
3e4093b6
RS
4648 if (!result_type)
4649 {
4650 if (flag_cond_mismatch)
4651 result_type = void_type_node;
4652 else
400fbf9f 4653 {
c2255bc4 4654 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4655 return error_mark_node;
400fbf9f 4656 }
3e4093b6 4657 }
400fbf9f 4658
3e4093b6
RS
4659 /* Merge const and volatile flags of the incoming types. */
4660 result_type
4661 = build_type_variant (result_type,
afbd0665
AS
4662 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4663 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4664
68fca595
MP
4665 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4666 semantic_result_type);
4667 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4668 semantic_result_type);
b6a10c9f 4669
928c19bb
JM
4670 if (ifexp_bcp && ifexp == truthvalue_true_node)
4671 {
4672 op2_int_operands = true;
4673 op1 = c_fully_fold (op1, require_constant_value, NULL);
4674 }
4675 if (ifexp_bcp && ifexp == truthvalue_false_node)
4676 {
4677 op1_int_operands = true;
4678 op2 = c_fully_fold (op2, require_constant_value, NULL);
4679 }
4d84fe7c 4680 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4681 && op1_int_operands
4682 && op2_int_operands);
4683 if (int_operands)
4684 {
4685 int_const = ((ifexp == truthvalue_true_node
4686 && TREE_CODE (orig_op1) == INTEGER_CST
4687 && !TREE_OVERFLOW (orig_op1))
4688 || (ifexp == truthvalue_false_node
4689 && TREE_CODE (orig_op2) == INTEGER_CST
4690 && !TREE_OVERFLOW (orig_op2)));
4691 }
4692 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4693 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4694 else
4695 {
01c7ccbb
JM
4696 if (int_operands)
4697 {
f34f1c87
MP
4698 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4699 nested inside of the expression. */
4700 op1 = c_fully_fold (op1, false, NULL);
4701 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4702 }
928c19bb
JM
4703 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4704 if (int_operands)
4705 ret = note_integer_operands (ret);
4706 }
2d2e923f
MLI
4707 if (semantic_result_type)
4708 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4709
c2255bc4 4710 protected_set_expr_location (ret, colon_loc);
928c19bb 4711 return ret;
3e4093b6
RS
4712}
4713\f
487a92fe 4714/* Return a compound expression that performs two expressions and
c2255bc4
AH
4715 returns the value of the second of them.
4716
4717 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4718
3e4093b6 4719tree
c2255bc4 4720build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4721{
4d84fe7c 4722 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4723 tree eptype = NULL_TREE;
928c19bb
JM
4724 tree ret;
4725
b72271b9 4726 if (flag_cilkplus
939b37da
BI
4727 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4728 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4729 {
4730 error_at (loc,
4731 "spawned function call cannot be part of a comma expression");
4732 return error_mark_node;
4733 }
4d84fe7c
JM
4734 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4735 if (expr1_int_operands)
4736 expr1 = remove_c_maybe_const_expr (expr1);
4737 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4738 if (expr2_int_operands)
4739 expr2 = remove_c_maybe_const_expr (expr2);
4740
8ce94e44
JM
4741 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4742 expr1 = TREE_OPERAND (expr1, 0);
4743 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4744 {
4745 eptype = TREE_TYPE (expr2);
4746 expr2 = TREE_OPERAND (expr2, 0);
4747 }
4748
3f75a254 4749 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4750 {
4751 /* The left-hand operand of a comma expression is like an expression
c5409249 4752 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4753 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4754 if (warn_unused_value)
47aecf47 4755 {
e14a6540 4756 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4757 && CONVERT_EXPR_P (expr1))
47aecf47 4758 ; /* (void) a, b */
e14a6540
JM
4759 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4760 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4761 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4762 ; /* (void) a, (void) b, c */
4763 else
b8698a0f 4764 warning_at (loc, OPT_Wunused_value,
c2255bc4 4765 "left-hand operand of comma expression has no effect");
47aecf47 4766 }
3e4093b6 4767 }
789eadcd
MP
4768 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4769 && warn_unused_value)
4770 {
4771 tree r = expr1;
4772 location_t cloc = loc;
4773 while (TREE_CODE (r) == COMPOUND_EXPR)
4774 {
4775 if (EXPR_HAS_LOCATION (r))
4776 cloc = EXPR_LOCATION (r);
4777 r = TREE_OPERAND (r, 1);
4778 }
4779 if (!TREE_SIDE_EFFECTS (r)
4780 && !VOID_TYPE_P (TREE_TYPE (r))
4781 && !CONVERT_EXPR_P (r))
4782 warning_at (cloc, OPT_Wunused_value,
4783 "right-hand operand of comma expression has no effect");
4784 }
400fbf9f 4785
3e4093b6
RS
4786 /* With -Wunused, we should also warn if the left-hand operand does have
4787 side-effects, but computes a value which is not used. For example, in
4788 `foo() + bar(), baz()' the result of the `+' operator is not used,
4789 so we should issue a warning. */
4790 else if (warn_unused_value)
c2255bc4 4791 warn_if_unused_value (expr1, loc);
400fbf9f 4792
e63d6886
AP
4793 if (expr2 == error_mark_node)
4794 return error_mark_node;
4795
928c19bb
JM
4796 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4797
4798 if (flag_isoc99
4d84fe7c
JM
4799 && expr1_int_operands
4800 && expr2_int_operands)
928c19bb
JM
4801 ret = note_integer_operands (ret);
4802
8ce94e44
JM
4803 if (eptype)
4804 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4805
c2255bc4 4806 protected_set_expr_location (ret, loc);
928c19bb 4807 return ret;
3e4093b6 4808}
400fbf9f 4809
67165eb3
ILT
4810/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4811 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4812 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4813 of the cast. -Wcast-qual appeared on the command line. Named
4814 address space qualifiers are not handled here, because they result
4815 in different warnings. */
67165eb3
ILT
4816
4817static void
2ee3cb35 4818handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4819{
4820 tree in_type = type;
4821 tree in_otype = otype;
4822 int added = 0;
4823 int discarded = 0;
4824 bool is_const;
4825
4826 /* Check that the qualifiers on IN_TYPE are a superset of the
4827 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4828 nodes is uninteresting and we stop as soon as we hit a
4829 non-POINTER_TYPE node on either type. */
4830 do
4831 {
4832 in_otype = TREE_TYPE (in_otype);
4833 in_type = TREE_TYPE (in_type);
4834
4835 /* GNU C allows cv-qualified function types. 'const' means the
4836 function is very pure, 'volatile' means it can't return. We
4837 need to warn when such qualifiers are added, not when they're
4838 taken away. */
4839 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4840 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4841 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4842 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4843 else
36c5e70a
BE
4844 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4845 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4846 }
4847 while (TREE_CODE (in_type) == POINTER_TYPE
4848 && TREE_CODE (in_otype) == POINTER_TYPE);
4849
4850 if (added)
2ee3cb35
MLI
4851 warning_at (loc, OPT_Wcast_qual,
4852 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4853
4854 if (discarded)
4855 /* There are qualifiers present in IN_OTYPE that are not present
4856 in IN_TYPE. */
2ee3cb35
MLI
4857 warning_at (loc, OPT_Wcast_qual,
4858 "cast discards %q#v qualifier from pointer target type",
4859 discarded);
67165eb3
ILT
4860
4861 if (added || discarded)
4862 return;
4863
4864 /* A cast from **T to const **T is unsafe, because it can cause a
4865 const value to be changed with no additional warning. We only
4866 issue this warning if T is the same on both sides, and we only
4867 issue the warning if there are the same number of pointers on
4868 both sides, as otherwise the cast is clearly unsafe anyhow. A
4869 cast is unsafe when a qualifier is added at one level and const
4870 is not present at all outer levels.
4871
4872 To issue this warning, we check at each level whether the cast
4873 adds new qualifiers not already seen. We don't need to special
4874 case function types, as they won't have the same
4875 TYPE_MAIN_VARIANT. */
4876
4877 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4878 return;
4879 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4880 return;
4881
4882 in_type = type;
4883 in_otype = otype;
4884 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4885 do
4886 {
4887 in_type = TREE_TYPE (in_type);
4888 in_otype = TREE_TYPE (in_otype);
4889 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4890 && !is_const)
4891 {
2ee3cb35
MLI
4892 warning_at (loc, OPT_Wcast_qual,
4893 "to be safe all intermediate pointers in cast from "
4894 "%qT to %qT must be %<const%> qualified",
4895 otype, type);
67165eb3
ILT
4896 break;
4897 }
4898 if (is_const)
4899 is_const = TYPE_READONLY (in_type);
4900 }
4901 while (TREE_CODE (in_type) == POINTER_TYPE);
4902}
4903
b8698a0f 4904/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4905 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4906
3e4093b6 4907tree
c2255bc4 4908build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4909{
8ce94e44
JM
4910 tree value;
4911
4912 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4913 expr = TREE_OPERAND (expr, 0);
4914
4915 value = expr;
400fbf9f 4916
3e4093b6
RS
4917 if (type == error_mark_node || expr == error_mark_node)
4918 return error_mark_node;
400fbf9f 4919
3e4093b6
RS
4920 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4921 only in <protocol> qualifications. But when constructing cast expressions,
4922 the protocols do matter and must be kept around. */
700686fa
ZL
4923 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4924 return build1 (NOP_EXPR, type, expr);
4925
4926 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4927
3e4093b6
RS
4928 if (TREE_CODE (type) == ARRAY_TYPE)
4929 {
c2255bc4 4930 error_at (loc, "cast specifies array type");
3e4093b6
RS
4931 return error_mark_node;
4932 }
400fbf9f 4933
3e4093b6
RS
4934 if (TREE_CODE (type) == FUNCTION_TYPE)
4935 {
c2255bc4 4936 error_at (loc, "cast specifies function type");
3e4093b6
RS
4937 return error_mark_node;
4938 }
400fbf9f 4939
808d6eaa
JM
4940 if (!VOID_TYPE_P (type))
4941 {
4942 value = require_complete_type (value);
4943 if (value == error_mark_node)
4944 return error_mark_node;
4945 }
4946
3e4093b6
RS
4947 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4948 {
fcf73884
MLI
4949 if (TREE_CODE (type) == RECORD_TYPE
4950 || TREE_CODE (type) == UNION_TYPE)
c1771a20 4951 pedwarn (loc, OPT_Wpedantic,
fcf73884 4952 "ISO C forbids casting nonscalar to the same type");
3e4093b6
RS
4953 }
4954 else if (TREE_CODE (type) == UNION_TYPE)
4955 {
4956 tree field;
400fbf9f 4957
910ad8de 4958 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
4959 if (TREE_TYPE (field) != error_mark_node
4960 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4961 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
4962 break;
4963
4964 if (field)
400fbf9f 4965 {
3e4093b6 4966 tree t;
e616f54d 4967 bool maybe_const = true;
3e4093b6 4968
c1771a20 4969 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
4970 t = c_fully_fold (value, false, &maybe_const);
4971 t = build_constructor_single (type, field, t);
4972 if (!maybe_const)
4973 t = c_wrap_maybe_const (t, true);
4974 t = digest_init (loc, type, t,
bbbbb16a 4975 NULL_TREE, false, true, 0);
3e4093b6
RS
4976 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4977 return t;
400fbf9f 4978 }
c2255bc4 4979 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
4980 return error_mark_node;
4981 }
4982 else
4983 {
4984 tree otype, ovalue;
400fbf9f 4985
3e4093b6 4986 if (type == void_type_node)
c2255bc4
AH
4987 {
4988 tree t = build1 (CONVERT_EXPR, type, value);
4989 SET_EXPR_LOCATION (t, loc);
4990 return t;
4991 }
400fbf9f 4992
3e4093b6 4993 otype = TREE_TYPE (value);
400fbf9f 4994
3e4093b6 4995 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
4996 if (warn_cast_qual
4997 && TREE_CODE (type) == POINTER_TYPE
4998 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 4999 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5000
36c5e70a
BE
5001 /* Warn about conversions between pointers to disjoint
5002 address spaces. */
5003 if (TREE_CODE (type) == POINTER_TYPE
5004 && TREE_CODE (otype) == POINTER_TYPE
5005 && !null_pointer_constant_p (value))
5006 {
5007 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5008 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5009 addr_space_t as_common;
5010
5011 if (!addr_space_superset (as_to, as_from, &as_common))
5012 {
5013 if (ADDR_SPACE_GENERIC_P (as_from))
5014 warning_at (loc, 0, "cast to %s address space pointer "
5015 "from disjoint generic address space pointer",
5016 c_addr_space_name (as_to));
5017
5018 else if (ADDR_SPACE_GENERIC_P (as_to))
5019 warning_at (loc, 0, "cast to generic address space pointer "
5020 "from disjoint %s address space pointer",
5021 c_addr_space_name (as_from));
5022
5023 else
5024 warning_at (loc, 0, "cast to %s address space pointer "
5025 "from disjoint %s address space pointer",
5026 c_addr_space_name (as_to),
5027 c_addr_space_name (as_from));
5028 }
5029 }
5030
3e4093b6 5031 /* Warn about possible alignment problems. */
3176a0c2 5032 if (STRICT_ALIGNMENT
3e4093b6
RS
5033 && TREE_CODE (type) == POINTER_TYPE
5034 && TREE_CODE (otype) == POINTER_TYPE
5035 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5036 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5037 /* Don't warn about opaque types, where the actual alignment
5038 restriction is unknown. */
5039 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5040 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5041 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5042 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5043 warning_at (loc, OPT_Wcast_align,
5044 "cast increases required alignment of target type");
e9a25f70 5045
3176a0c2 5046 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5047 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5048 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5049 /* Unlike conversion of integers to pointers, where the
5050 warning is disabled for converting constants because
5051 of cases such as SIG_*, warn about converting constant
5052 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5053 sign extension, and a warning is appropriate. */
c2255bc4
AH
5054 warning_at (loc, OPT_Wpointer_to_int_cast,
5055 "cast from pointer to integer of different size");
400fbf9f 5056
3176a0c2 5057 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5058 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5059 warning_at (loc, OPT_Wbad_function_cast,
5060 "cast from function call of type %qT "
5061 "to non-matching type %qT", otype, type);
400fbf9f 5062
3176a0c2 5063 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5064 && TREE_CODE (otype) == INTEGER_TYPE
5065 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5066 /* Don't warn about converting any constant. */
5067 && !TREE_CONSTANT (value))
c2255bc4
AH
5068 warning_at (loc,
5069 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5070 "of different size");
400fbf9f 5071
79bedddc
SR
5072 if (warn_strict_aliasing <= 2)
5073 strict_aliasing_warning (otype, type, expr);
400fbf9f 5074
3897f229
JM
5075 /* If pedantic, warn for conversions between function and object
5076 pointer types, except for converting a null pointer constant
5077 to function pointer type. */
5078 if (pedantic
5079 && TREE_CODE (type) == POINTER_TYPE
5080 && TREE_CODE (otype) == POINTER_TYPE
5081 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5082 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5083 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5084 "conversion of function pointer to object pointer type");
3897f229
JM
5085
5086 if (pedantic
5087 && TREE_CODE (type) == POINTER_TYPE
5088 && TREE_CODE (otype) == POINTER_TYPE
5089 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5090 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5091 && !null_pointer_constant_p (value))
c1771a20 5092 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5093 "conversion of object pointer to function pointer type");
3897f229 5094
3e4093b6 5095 ovalue = value;
3e4093b6 5096 value = convert (type, value);
400fbf9f 5097
3e4093b6 5098 /* Ignore any integer overflow caused by the cast. */
928c19bb 5099 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5100 {
8bcd6380 5101 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5102 {
8bcd6380
RS
5103 if (!TREE_OVERFLOW (value))
5104 {
5105 /* Avoid clobbering a shared constant. */
5106 value = copy_node (value);
5107 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5108 }
6414bad6 5109 }
8bcd6380 5110 else if (TREE_OVERFLOW (value))
d8e1f97b
PB
5111 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5112 value = build_int_cst_wide (TREE_TYPE (value),
5113 TREE_INT_CST_LOW (value),
5114 TREE_INT_CST_HIGH (value));
3e4093b6
RS
5115 }
5116 }
400fbf9f 5117
53cd18ec
JM
5118 /* Don't let a cast be an lvalue. */
5119 if (value == expr)
db3927fb 5120 value = non_lvalue_loc (loc, value);
e9a25f70 5121
928c19bb
JM
5122 /* Don't allow the results of casting to floating-point or complex
5123 types be confused with actual constants, or casts involving
5124 integer and pointer types other than direct integer-to-integer
5125 and integer-to-pointer be confused with integer constant
5126 expressions and null pointer constants. */
5127 if (TREE_CODE (value) == REAL_CST
5128 || TREE_CODE (value) == COMPLEX_CST
5129 || (TREE_CODE (value) == INTEGER_CST
5130 && !((TREE_CODE (expr) == INTEGER_CST
5131 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5132 || TREE_CODE (expr) == REAL_CST
5133 || TREE_CODE (expr) == COMPLEX_CST)))
5134 value = build1 (NOP_EXPR, type, value);
5135
c2255bc4
AH
5136 if (CAN_HAVE_LOCATION_P (value))
5137 SET_EXPR_LOCATION (value, loc);
3e4093b6 5138 return value;
400fbf9f
JW
5139}
5140
c2255bc4
AH
5141/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5142 location of the open paren of the cast, or the position of the cast
5143 expr. */
3e4093b6 5144tree
c2255bc4 5145c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5146{
f8893e47 5147 tree type;
928c19bb
JM
5148 tree type_expr = NULL_TREE;
5149 bool type_expr_const = true;
5150 tree ret;
3e4093b6 5151 int saved_wsp = warn_strict_prototypes;
c5c76735 5152
3e4093b6
RS
5153 /* This avoids warnings about unprototyped casts on
5154 integers. E.g. "#define SIG_DFL (void(*)())0". */
5155 if (TREE_CODE (expr) == INTEGER_CST)
5156 warn_strict_prototypes = 0;
928c19bb 5157 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5158 warn_strict_prototypes = saved_wsp;
c5c76735 5159
c2255bc4 5160 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5161 if (type_expr)
5162 {
9f33203d
JM
5163 bool inner_expr_const = true;
5164 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5165 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5166 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5167 && inner_expr_const);
c2255bc4 5168 SET_EXPR_LOCATION (ret, loc);
928c19bb 5169 }
24b97832
ILT
5170
5171 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5172 SET_EXPR_LOCATION (ret, loc);
5173
9e5b2115
PB
5174 /* C++ does not permits types to be defined in a cast, but it
5175 allows references to incomplete types. */
5176 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5177 warning_at (loc, OPT_Wc___compat,
5178 "defining a type in a cast is invalid in C++");
5179
928c19bb 5180 return ret;
400fbf9f 5181}
3e4093b6
RS
5182\f
5183/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5184 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5185 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5186 MODIFYCODE is the code for a binary operator that we use
5187 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5188 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5189 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5190 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5191
c2255bc4
AH
5192 LOCATION is the location of the MODIFYCODE operator.
5193 RHS_LOC is the location of the RHS. */
2f6e4e97 5194
3e4093b6 5195tree
32e8bb8e 5196build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5197 enum tree_code modifycode,
c2255bc4 5198 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5199{
3e4093b6
RS
5200 tree result;
5201 tree newrhs;
241f845a 5202 tree rhseval = NULL_TREE;
8ce94e44 5203 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5204 tree lhstype = TREE_TYPE (lhs);
5205 tree olhstype = lhstype;
928c19bb 5206 bool npc;
267bac10 5207 bool is_atomic_op;
e9a25f70 5208
3e4093b6
RS
5209 /* Types that aren't fully specified cannot be used in assignments. */
5210 lhs = require_complete_type (lhs);
e9a25f70 5211
3e4093b6
RS
5212 /* Avoid duplicate error messages from operands that had errors. */
5213 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5214 return error_mark_node;
400fbf9f 5215
4c2ecab0
JM
5216 /* Ensure an error for assigning a non-lvalue array to an array in
5217 C90. */
5218 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5219 {
5220 error_at (location, "assignment to expression with array type");
5221 return error_mark_node;
5222 }
5223
46a88c12 5224 /* For ObjC properties, defer this check. */
7bd11157 5225 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5226 return error_mark_node;
5227
267bac10
JM
5228 is_atomic_op = really_atomic_lvalue (lhs);
5229
8ce94e44
JM
5230 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5231 {
5232 rhs_semantic_type = TREE_TYPE (rhs);
5233 rhs = TREE_OPERAND (rhs, 0);
5234 }
5235
3e4093b6 5236 newrhs = rhs;
400fbf9f 5237
928c19bb
JM
5238 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5239 {
5240 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5241 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5242 rhs_origtype);
928c19bb
JM
5243 if (inner == error_mark_node)
5244 return error_mark_node;
5245 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5246 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5247 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5248 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5249 protected_set_expr_location (result, location);
5250 return result;
5251 }
5252
3e4093b6
RS
5253 /* If a binary op has been requested, combine the old LHS value with the RHS
5254 producing the value we should actually store into the LHS. */
5255
5256 if (modifycode != NOP_EXPR)
400fbf9f 5257 {
928c19bb 5258 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5259 lhs = stabilize_reference (lhs);
bbbbb16a 5260
267bac10
JM
5261 /* Construct the RHS for any non-atomic compound assignemnt. */
5262 if (!is_atomic_op)
5263 {
241f845a
JJ
5264 /* If in LHS op= RHS the RHS has side-effects, ensure they
5265 are preevaluated before the rest of the assignment expression's
5266 side-effects, because RHS could contain e.g. function calls
5267 that modify LHS. */
5268 if (TREE_SIDE_EFFECTS (rhs))
5269 {
5270 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5271 rhseval = newrhs;
5272 }
267bac10 5273 newrhs = build_binary_op (location,
241f845a 5274 modifycode, lhs, newrhs, 1);
267bac10
JM
5275
5276 /* The original type of the right hand side is no longer
5277 meaningful. */
5278 rhs_origtype = NULL_TREE;
5279 }
400fbf9f 5280 }
400fbf9f 5281
668ea4b1
IS
5282 if (c_dialect_objc ())
5283 {
46a88c12
NP
5284 /* Check if we are modifying an Objective-C property reference;
5285 if so, we need to generate setter calls. */
5286 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5287 if (result)
241f845a 5288 goto return_result;
46a88c12
NP
5289
5290 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5291 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5292 return error_mark_node;
5293 }
5294
9bf24266 5295 /* Give an error for storing in something that is 'const'. */
bbbd6700 5296
f37acdf9 5297 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5298 || ((TREE_CODE (lhstype) == RECORD_TYPE
5299 || TREE_CODE (lhstype) == UNION_TYPE)
5300 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5301 {
c02065fc 5302 readonly_error (location, lhs, lv_assign);
953ff289
DN
5303 return error_mark_node;
5304 }
f37acdf9
JM
5305 else if (TREE_READONLY (lhs))
5306 readonly_warning (lhs, lv_assign);
bbbd6700 5307
3e4093b6
RS
5308 /* If storing into a structure or union member,
5309 it has probably been given type `int'.
5310 Compute the type that would go with
5311 the actual amount of storage the member occupies. */
bbbd6700 5312
3e4093b6
RS
5313 if (TREE_CODE (lhs) == COMPONENT_REF
5314 && (TREE_CODE (lhstype) == INTEGER_TYPE
5315 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5316 || TREE_CODE (lhstype) == REAL_TYPE
5317 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5318 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5319
3e4093b6
RS
5320 /* If storing in a field that is in actuality a short or narrower than one,
5321 we must store in the field in its actual type. */
5322
5323 if (lhstype != TREE_TYPE (lhs))
5324 {
5325 lhs = copy_node (lhs);
5326 TREE_TYPE (lhs) = lhstype;
400fbf9f 5327 }
400fbf9f 5328
32e8bb8e
ILT
5329 /* Issue -Wc++-compat warnings about an assignment to an enum type
5330 when LHS does not have its original type. This happens for,
5331 e.g., an enum bitfield in a struct. */
5332 if (warn_cxx_compat
5333 && lhs_origtype != NULL_TREE
5334 && lhs_origtype != lhstype
5335 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5336 {
5337 tree checktype = (rhs_origtype != NULL_TREE
5338 ? rhs_origtype
5339 : TREE_TYPE (rhs));
5340 if (checktype != error_mark_node
267bac10
JM
5341 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5342 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5343 warning_at (location, OPT_Wc___compat,
5344 "enum conversion in assignment is invalid in C++");
5345 }
5346
267bac10
JM
5347 /* If the lhs is atomic, remove that qualifier. */
5348 if (is_atomic_op)
5349 {
5350 lhstype = build_qualified_type (lhstype,
5351 (TYPE_QUALS (lhstype)
5352 & ~TYPE_QUAL_ATOMIC));
5353 olhstype = build_qualified_type (olhstype,
5354 (TYPE_QUALS (lhstype)
5355 & ~TYPE_QUAL_ATOMIC));
5356 }
5357
8ce94e44
JM
5358 /* Convert new value to destination type. Fold it first, then
5359 restore any excess precision information, for the sake of
5360 conversion warnings. */
400fbf9f 5361
267bac10
JM
5362 if (!(is_atomic_op && modifycode != NOP_EXPR))
5363 {
5364 npc = null_pointer_constant_p (newrhs);
5365 newrhs = c_fully_fold (newrhs, false, NULL);
5366 if (rhs_semantic_type)
5367 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5368 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5369 rhs_origtype, ic_assign, npc,
5370 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5371 if (TREE_CODE (newrhs) == ERROR_MARK)
5372 return error_mark_node;
5373 }
400fbf9f 5374
6e955430
ZL
5375 /* Emit ObjC write barrier, if necessary. */
5376 if (c_dialect_objc () && flag_objc_gc)
5377 {
5378 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5379 if (result)
c9f9eb5d
AH
5380 {
5381 protected_set_expr_location (result, location);
241f845a 5382 goto return_result;
c9f9eb5d 5383 }
6e955430
ZL
5384 }
5385
ea4b7848 5386 /* Scan operands. */
400fbf9f 5387
267bac10
JM
5388 if (is_atomic_op)
5389 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5390 else
5391 {
5392 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5393 TREE_SIDE_EFFECTS (result) = 1;
5394 protected_set_expr_location (result, location);
5395 }
400fbf9f 5396
3e4093b6
RS
5397 /* If we got the LHS in a different type for storing in,
5398 convert the result back to the nominal type of LHS
5399 so that the value we return always has the same type
5400 as the LHS argument. */
e855c5ce 5401
3e4093b6 5402 if (olhstype == TREE_TYPE (result))
241f845a 5403 goto return_result;
c9f9eb5d 5404
68fca595
MP
5405 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5406 rhs_origtype, ic_assign, false, NULL_TREE,
5407 NULL_TREE, 0);
c9f9eb5d 5408 protected_set_expr_location (result, location);
241f845a
JJ
5409
5410return_result:
5411 if (rhseval)
5412 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5413 return result;
3e4093b6
RS
5414}
5415\f
478a1c5b
ILT
5416/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5417 This is used to implement -fplan9-extensions. */
5418
5419static bool
5420find_anonymous_field_with_type (tree struct_type, tree type)
5421{
5422 tree field;
5423 bool found;
5424
5425 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5426 || TREE_CODE (struct_type) == UNION_TYPE);
5427 found = false;
5428 for (field = TYPE_FIELDS (struct_type);
5429 field != NULL_TREE;
5430 field = TREE_CHAIN (field))
5431 {
267bac10
JM
5432 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5433 ? c_build_qualified_type (TREE_TYPE (field),
5434 TYPE_QUAL_ATOMIC)
5435 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5436 if (DECL_NAME (field) == NULL
267bac10 5437 && comptypes (type, fieldtype))
478a1c5b
ILT
5438 {
5439 if (found)
5440 return false;
5441 found = true;
5442 }
5443 else if (DECL_NAME (field) == NULL
5444 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5445 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5446 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5447 {
5448 if (found)
5449 return false;
5450 found = true;
5451 }
5452 }
5453 return found;
5454}
5455
5456/* RHS is an expression whose type is pointer to struct. If there is
5457 an anonymous field in RHS with type TYPE, then return a pointer to
5458 that field in RHS. This is used with -fplan9-extensions. This
5459 returns NULL if no conversion could be found. */
5460
5461static tree
5462convert_to_anonymous_field (location_t location, tree type, tree rhs)
5463{
5464 tree rhs_struct_type, lhs_main_type;
5465 tree field, found_field;
5466 bool found_sub_field;
5467 tree ret;
5468
5469 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5470 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5471 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5472 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5473
5474 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5475 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5476 ? c_build_qualified_type (TREE_TYPE (type),
5477 TYPE_QUAL_ATOMIC)
5478 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5479
5480 found_field = NULL_TREE;
5481 found_sub_field = false;
5482 for (field = TYPE_FIELDS (rhs_struct_type);
5483 field != NULL_TREE;
5484 field = TREE_CHAIN (field))
5485 {
5486 if (DECL_NAME (field) != NULL_TREE
5487 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5488 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5489 continue;
267bac10
JM
5490 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5491 ? c_build_qualified_type (TREE_TYPE (field),
5492 TYPE_QUAL_ATOMIC)
5493 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5494 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5495 {
5496 if (found_field != NULL_TREE)
5497 return NULL_TREE;
5498 found_field = field;
5499 }
5500 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5501 lhs_main_type))
5502 {
5503 if (found_field != NULL_TREE)
5504 return NULL_TREE;
5505 found_field = field;
5506 found_sub_field = true;
5507 }
5508 }
5509
5510 if (found_field == NULL_TREE)
5511 return NULL_TREE;
5512
5513 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5514 build_fold_indirect_ref (rhs), found_field,
5515 NULL_TREE);
5516 ret = build_fold_addr_expr_loc (location, ret);
5517
5518 if (found_sub_field)
5519 {
5520 ret = convert_to_anonymous_field (location, type, ret);
5521 gcc_assert (ret != NULL_TREE);
5522 }
5523
5524 return ret;
5525}
5526
bbbbb16a
ILT
5527/* Convert value RHS to type TYPE as preparation for an assignment to
5528 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5529 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5530 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5531 constant before any folding.
3e4093b6
RS
5532 The real work of conversion is done by `convert'.
5533 The purpose of this function is to generate error messages
5534 for assignments that are not allowed in C.
2ac2f164
JM
5535 ERRTYPE says whether it is argument passing, assignment,
5536 initialization or return.
2f6e4e97 5537
81e5eca8
MP
5538 LOCATION is the location of the assignment, EXPR_LOC is the location of
5539 the RHS or, for a function, location of an argument.
2ac2f164 5540 FUNCTION is a tree for the function being called.
3e4093b6 5541 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5542
3e4093b6 5543static tree
81e5eca8 5544convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5545 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5546 bool null_pointer_constant, tree fundecl,
5547 tree function, int parmnum)
3e4093b6
RS
5548{
5549 enum tree_code codel = TREE_CODE (type);
8ce94e44 5550 tree orig_rhs = rhs;
3e4093b6
RS
5551 tree rhstype;
5552 enum tree_code coder;
2ac2f164 5553 tree rname = NULL_TREE;
58393038 5554 bool objc_ok = false;
2ac2f164 5555
6b4ef5c1 5556 if (errtype == ic_argpass)
2ac2f164
JM
5557 {
5558 tree selector;
5559 /* Change pointer to function to the function itself for
5560 diagnostics. */
5561 if (TREE_CODE (function) == ADDR_EXPR
5562 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5563 function = TREE_OPERAND (function, 0);
5564
5565 /* Handle an ObjC selector specially for diagnostics. */
5566 selector = objc_message_selector ();
5567 rname = function;
5568 if (selector && parmnum > 2)
5569 {
5570 rname = selector;
5571 parmnum -= 2;
5572 }
5573 }
5574
5575 /* This macro is used to emit diagnostics to ensure that all format
5576 strings are complete sentences, visible to gettext and checked at
5577 compile time. */
c2255bc4 5578#define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5579 do { \
5580 switch (errtype) \
5581 { \
5582 case ic_argpass: \
5583 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
c2255bc4
AH
5584 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5585 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
1e053dfe
MLI
5586 "expected %qT but argument is of type %qT", \
5587 type, rhstype); \
5588 break; \
1e053dfe
MLI
5589 case ic_assign: \
5590 pedwarn (LOCATION, OPT, AS); \
5591 break; \
5592 case ic_init: \
6a8f4e12 5593 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5594 break; \
5595 case ic_return: \
c2255bc4 5596 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5597 break; \
5598 default: \
5599 gcc_unreachable (); \
5600 } \
2ac2f164 5601 } while (0)
cb3ca04e 5602
49706e39
MLI
5603 /* This macro is used to emit diagnostics to ensure that all format
5604 strings are complete sentences, visible to gettext and checked at
5605 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5606 extra parameter to enumerate qualifiers. */
5607
5608#define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5609 do { \
5610 switch (errtype) \
5611 { \
5612 case ic_argpass: \
5613 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5614 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5615 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5616 "expected %qT but argument is of type %qT", \
5617 type, rhstype); \
5618 break; \
5619 case ic_assign: \
5620 pedwarn (LOCATION, OPT, AS, QUALS); \
5621 break; \
5622 case ic_init: \
5623 pedwarn (LOCATION, OPT, IN, QUALS); \
5624 break; \
5625 case ic_return: \
5626 pedwarn (LOCATION, OPT, RE, QUALS); \
5627 break; \
5628 default: \
5629 gcc_unreachable (); \
5630 } \
5631 } while (0)
5632
8ce94e44
JM
5633 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5634 rhs = TREE_OPERAND (rhs, 0);
5635
3e4093b6
RS
5636 rhstype = TREE_TYPE (rhs);
5637 coder = TREE_CODE (rhstype);
5638
5639 if (coder == ERROR_MARK)
5640 return error_mark_node;
5641
58393038
ZL
5642 if (c_dialect_objc ())
5643 {
5644 int parmno;
5645
5646 switch (errtype)
5647 {
5648 case ic_return:
5649 parmno = 0;
5650 break;
5651
5652 case ic_assign:
5653 parmno = -1;
5654 break;
5655
5656 case ic_init:
5657 parmno = -2;
5658 break;
5659
5660 default:
5661 parmno = parmnum;
5662 break;
5663 }
5664
5665 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5666 }
5667
bbbbb16a
ILT
5668 if (warn_cxx_compat)
5669 {
5670 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5671 if (checktype != error_mark_node
5672 && TREE_CODE (type) == ENUMERAL_TYPE
5673 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5674 {
81f40b79
ILT
5675 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5676 G_("enum conversion when passing argument "
5677 "%d of %qE is invalid in C++"),
5678 G_("enum conversion in assignment is "
5679 "invalid in C++"),
5680 G_("enum conversion in initialization is "
5681 "invalid in C++"),
5682 G_("enum conversion in return is "
5683 "invalid in C++"));
bbbbb16a
ILT
5684 }
5685 }
5686
3e4093b6 5687 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5688 return rhs;
3e4093b6
RS
5689
5690 if (coder == VOID_TYPE)
400fbf9f 5691 {
6dcc04b0
JM
5692 /* Except for passing an argument to an unprototyped function,
5693 this is a constraint violation. When passing an argument to
5694 an unprototyped function, it is compile-time undefined;
5695 making it a constraint in that case was rejected in
5696 DR#252. */
c2255bc4 5697 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5698 return error_mark_node;
400fbf9f 5699 }
808d6eaa
JM
5700 rhs = require_complete_type (rhs);
5701 if (rhs == error_mark_node)
5702 return error_mark_node;
cd192ccc
MS
5703 /* A non-reference type can convert to a reference. This handles
5704 va_start, va_copy and possibly port built-ins. */
5705 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5706 {
3e4093b6 5707 if (!lvalue_p (rhs))
400fbf9f 5708 {
c2255bc4 5709 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5710 return error_mark_node;
400fbf9f 5711 }
3e4093b6
RS
5712 if (!c_mark_addressable (rhs))
5713 return error_mark_node;
5714 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5715 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5716
81e5eca8 5717 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
5718 build_pointer_type (TREE_TYPE (type)),
5719 rhs, origtype, errtype,
5720 null_pointer_constant, fundecl, function,
5721 parmnum);
cd192ccc
MS
5722 if (rhs == error_mark_node)
5723 return error_mark_node;
3e4093b6
RS
5724
5725 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5726 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5727 return rhs;
400fbf9f 5728 }
3e4093b6 5729 /* Some types can interconvert without explicit casts. */
3274deff 5730 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5731 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5732 return convert (type, rhs);
5733 /* Arithmetic types all interconvert, and enum is treated like int. */
5734 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5735 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5736 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5737 || codel == BOOLEAN_TYPE)
5738 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5739 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5740 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5741 || coder == BOOLEAN_TYPE))
928c19bb
JM
5742 {
5743 tree ret;
5744 bool save = in_late_binary_op;
dfe776dd 5745 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb 5746 in_late_binary_op = true;
81e5eca8
MP
5747 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5748 ? expr_loc : location, type, orig_rhs);
dfe776dd 5749 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb
JM
5750 in_late_binary_op = save;
5751 return ret;
5752 }
400fbf9f 5753
79077aea
JJ
5754 /* Aggregates in different TUs might need conversion. */
5755 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5756 && codel == coder
5757 && comptypes (type, rhstype))
81e5eca8
MP
5758 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5759 ? expr_loc : location, type, rhs);
79077aea 5760
ebf0bf7f 5761 /* Conversion to a transparent union or record from its member types.
3e4093b6 5762 This applies only to function arguments. */
ebf0bf7f
JJ
5763 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5764 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5765 && errtype == ic_argpass)
400fbf9f 5766 {
0257e383 5767 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5768
910ad8de 5769 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5770 {
0257e383 5771 tree memb_type = TREE_TYPE (memb);
400fbf9f 5772
3e4093b6 5773 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5774 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5775 break;
e58cd767 5776
3e4093b6
RS
5777 if (TREE_CODE (memb_type) != POINTER_TYPE)
5778 continue;
2f6e4e97 5779
3e4093b6
RS
5780 if (coder == POINTER_TYPE)
5781 {
5782 tree ttl = TREE_TYPE (memb_type);
5783 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5784
3e4093b6
RS
5785 /* Any non-function converts to a [const][volatile] void *
5786 and vice versa; otherwise, targets must be the same.
5787 Meanwhile, the lhs target must have all the qualifiers of
5788 the rhs. */
267bac10
JM
5789 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5790 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5791 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5792 {
267bac10
JM
5793 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5794 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5795 /* If this type won't generate any warnings, use it. */
267bac10 5796 if (lquals == rquals
3e4093b6
RS
5797 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5798 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5799 ? ((lquals | rquals) == rquals)
5800 : ((lquals | rquals) == lquals)))
3e4093b6 5801 break;
400fbf9f 5802
3e4093b6 5803 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5804 if (!marginal_memb)
5805 marginal_memb = memb;
3e4093b6
RS
5806 }
5807 }
82bde854 5808
3e4093b6 5809 /* Can convert integer zero to any pointer type. */
928c19bb 5810 if (null_pointer_constant)
3e4093b6
RS
5811 {
5812 rhs = null_pointer_node;
5813 break;
5814 }
5815 }
400fbf9f 5816
0257e383 5817 if (memb || marginal_memb)
3e4093b6 5818 {
0257e383 5819 if (!memb)
3e4093b6
RS
5820 {
5821 /* We have only a marginally acceptable member type;
5822 it needs a warning. */
0257e383 5823 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5824 tree ttr = TREE_TYPE (rhstype);
714a0864 5825
3e4093b6
RS
5826 /* Const and volatile mean something different for function
5827 types, so the usual warnings are not appropriate. */
5828 if (TREE_CODE (ttr) == FUNCTION_TYPE
5829 && TREE_CODE (ttl) == FUNCTION_TYPE)
5830 {
5831 /* Because const and volatile on functions are
5832 restrictions that say the function will not do
5833 certain things, it is okay to use a const or volatile
5834 function where an ordinary one is wanted, but not
5835 vice-versa. */
36c5e70a
BE
5836 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5837 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 5838 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5839 G_("passing argument %d of %qE "
49706e39 5840 "makes %q#v qualified function "
2ac2f164 5841 "pointer from unqualified"),
49706e39 5842 G_("assignment makes %q#v qualified "
2ac2f164
JM
5843 "function pointer from "
5844 "unqualified"),
49706e39 5845 G_("initialization makes %q#v qualified "
2ac2f164
JM
5846 "function pointer from "
5847 "unqualified"),
49706e39
MLI
5848 G_("return makes %q#v qualified function "
5849 "pointer from unqualified"),
5850 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 5851 }
36c5e70a
BE
5852 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5853 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
49706e39 5854 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5855 G_("passing argument %d of %qE discards "
49706e39
MLI
5856 "%qv qualifier from pointer target type"),
5857 G_("assignment discards %qv qualifier "
2ac2f164 5858 "from pointer target type"),
49706e39 5859 G_("initialization discards %qv qualifier "
2ac2f164 5860 "from pointer target type"),
49706e39
MLI
5861 G_("return discards %qv qualifier from "
5862 "pointer target type"),
5863 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
5864
5865 memb = marginal_memb;
3e4093b6 5866 }
400fbf9f 5867
fcf73884 5868 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 5869 pedwarn (location, OPT_Wpedantic,
fcf73884 5870 "ISO C prohibits argument conversion to union type");
0e7c47fa 5871
db3927fb 5872 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 5873 return build_constructor_single (type, memb, rhs);
3e4093b6 5874 }
0e7c47fa
RK
5875 }
5876
3e4093b6
RS
5877 /* Conversions among pointers */
5878 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5879 && (coder == codel))
400fbf9f 5880 {
3e4093b6
RS
5881 tree ttl = TREE_TYPE (type);
5882 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
5883 tree mvl = ttl;
5884 tree mvr = ttr;
3e4093b6 5885 bool is_opaque_pointer;
264fa2db 5886 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
5887 addr_space_t asl;
5888 addr_space_t asr;
400fbf9f 5889
46df2823 5890 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
5891 mvl = (TYPE_ATOMIC (mvl)
5892 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5893 TYPE_QUAL_ATOMIC)
5894 : TYPE_MAIN_VARIANT (mvl));
46df2823 5895 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
5896 mvr = (TYPE_ATOMIC (mvr)
5897 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5898 TYPE_QUAL_ATOMIC)
5899 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 5900 /* Opaque pointers are treated like void pointers. */
f83c7f63 5901 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 5902
478a1c5b
ILT
5903 /* The Plan 9 compiler permits a pointer to a struct to be
5904 automatically converted into a pointer to an anonymous field
5905 within the struct. */
5906 if (flag_plan9_extensions
5907 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5908 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5909 && mvl != mvr)
5910 {
5911 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5912 if (new_rhs != NULL_TREE)
5913 {
5914 rhs = new_rhs;
5915 rhstype = TREE_TYPE (rhs);
5916 coder = TREE_CODE (rhstype);
5917 ttr = TREE_TYPE (rhstype);
5918 mvr = TYPE_MAIN_VARIANT (ttr);
5919 }
5920 }
5921
b7e20b53 5922 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
5923 for the purpose of reducing the number of false positives, we
5924 tolerate the special case of
b7e20b53 5925
c22cacf3 5926 int *p = NULL;
b7e20b53 5927
c22cacf3 5928 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 5929 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
c2255bc4
AH
5930 warning_at (location, OPT_Wc___compat,
5931 "request for implicit conversion "
5932 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 5933
36c5e70a
BE
5934 /* See if the pointers point to incompatible address spaces. */
5935 asl = TYPE_ADDR_SPACE (ttl);
5936 asr = TYPE_ADDR_SPACE (ttr);
5937 if (!null_pointer_constant_p (rhs)
5938 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5939 {
5940 switch (errtype)
5941 {
5942 case ic_argpass:
5943 error_at (location, "passing argument %d of %qE from pointer to "
5944 "non-enclosed address space", parmnum, rname);
5945 break;
5946 case ic_assign:
5947 error_at (location, "assignment from pointer to "
5948 "non-enclosed address space");
5949 break;
5950 case ic_init:
5951 error_at (location, "initialization from pointer to "
5952 "non-enclosed address space");
5953 break;
5954 case ic_return:
5955 error_at (location, "return from pointer to "
5956 "non-enclosed address space");
5957 break;
5958 default:
5959 gcc_unreachable ();
5960 }
5961 return error_mark_node;
5962 }
5963
7876a414
KG
5964 /* Check if the right-hand side has a format attribute but the
5965 left-hand side doesn't. */
90137d8f 5966 if (warn_suggest_attribute_format
104f8784 5967 && check_missing_format_attribute (type, rhstype))
c22cacf3 5968 {
104f8784
KG
5969 switch (errtype)
5970 {
5971 case ic_argpass:
90137d8f 5972 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5973 "argument %d of %qE might be "
5974 "a candidate for a format attribute",
5975 parmnum, rname);
104f8784
KG
5976 break;
5977 case ic_assign:
90137d8f 5978 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5979 "assignment left-hand side might be "
5980 "a candidate for a format attribute");
104f8784
KG
5981 break;
5982 case ic_init:
90137d8f 5983 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5984 "initialization left-hand side might be "
5985 "a candidate for a format attribute");
104f8784
KG
5986 break;
5987 case ic_return:
90137d8f 5988 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5989 "return type might be "
5990 "a candidate for a format attribute");
104f8784
KG
5991 break;
5992 default:
5993 gcc_unreachable ();
5994 }
7876a414 5995 }
c22cacf3 5996
3e4093b6
RS
5997 /* Any non-function converts to a [const][volatile] void *
5998 and vice versa; otherwise, targets must be the same.
5999 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6000 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6001 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6002 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6003 || is_opaque_pointer
f8a93a2e
JJ
6004 || ((c_common_unsigned_type (mvl)
6005 == c_common_unsigned_type (mvr))
267bac10
JM
6006 && (c_common_signed_type (mvl)
6007 == c_common_signed_type (mvr))
6008 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6
RS
6009 {
6010 if (pedantic
6011 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6012 ||
6013 (VOID_TYPE_P (ttr)
928c19bb 6014 && !null_pointer_constant
3e4093b6 6015 && TREE_CODE (ttl) == FUNCTION_TYPE)))
c1771a20 6016 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
509c9d60 6017 G_("ISO C forbids passing argument %d of "
2ac2f164
JM
6018 "%qE between function pointer "
6019 "and %<void *%>"),
4b794eaf 6020 G_("ISO C forbids assignment between "
2ac2f164 6021 "function pointer and %<void *%>"),
4b794eaf 6022 G_("ISO C forbids initialization between "
2ac2f164 6023 "function pointer and %<void *%>"),
4b794eaf 6024 G_("ISO C forbids return between function "
2ac2f164 6025 "pointer and %<void *%>"));
3e4093b6
RS
6026 /* Const and volatile mean something different for function types,
6027 so the usual warnings are not appropriate. */
6028 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6029 && TREE_CODE (ttl) != FUNCTION_TYPE)
6030 {
267bac10
JM
6031 /* Assignments between atomic and non-atomic objects are OK. */
6032 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6033 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6034 {
6c39e757
NP
6035 WARN_FOR_QUALIFIERS (location, 0,
6036 G_("passing argument %d of %qE discards "
6037 "%qv qualifier from pointer target type"),
6038 G_("assignment discards %qv qualifier "
6039 "from pointer target type"),
6040 G_("initialization discards %qv qualifier "
6041 "from pointer target type"),
6042 G_("return discards %qv qualifier from "
6043 "pointer target type"),
6044 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6045 }
3e4093b6
RS
6046 /* If this is not a case of ignoring a mismatch in signedness,
6047 no warning. */
6048 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6049 || target_cmp)
3e4093b6
RS
6050 ;
6051 /* If there is a mismatch, do warn. */
f2fd3821 6052 else if (warn_pointer_sign)
c2255bc4 6053 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
509c9d60 6054 G_("pointer targets in passing argument "
2ac2f164 6055 "%d of %qE differ in signedness"),
4b794eaf 6056 G_("pointer targets in assignment "
2ac2f164 6057 "differ in signedness"),
4b794eaf 6058 G_("pointer targets in initialization "
2ac2f164 6059 "differ in signedness"),
4b794eaf 6060 G_("pointer targets in return differ "
2ac2f164 6061 "in signedness"));
3e4093b6
RS
6062 }
6063 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6064 && TREE_CODE (ttr) == FUNCTION_TYPE)
6065 {
6066 /* Because const and volatile on functions are restrictions
6067 that say the function will not do certain things,
6068 it is okay to use a const or volatile function
6069 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6070 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6071 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 6072 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 6073 G_("passing argument %d of %qE makes "
49706e39 6074 "%q#v qualified function pointer "
2ac2f164 6075 "from unqualified"),
49706e39 6076 G_("assignment makes %q#v qualified function "
2ac2f164 6077 "pointer from unqualified"),
49706e39 6078 G_("initialization makes %q#v qualified "
2ac2f164 6079 "function pointer from unqualified"),
49706e39
MLI
6080 G_("return makes %q#v qualified function "
6081 "pointer from unqualified"),
6082 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6083 }
6084 }
6085 else
58393038
ZL
6086 /* Avoid warning about the volatile ObjC EH puts on decls. */
6087 if (!objc_ok)
c2255bc4 6088 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6089 G_("passing argument %d of %qE from "
58393038 6090 "incompatible pointer type"),
4b794eaf
JJ
6091 G_("assignment from incompatible pointer type"),
6092 G_("initialization from incompatible "
58393038 6093 "pointer type"),
4b794eaf 6094 G_("return from incompatible pointer type"));
58393038 6095
3e4093b6
RS
6096 return convert (type, rhs);
6097 }
b494fd98
EB
6098 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6099 {
6dcc04b0
JM
6100 /* ??? This should not be an error when inlining calls to
6101 unprototyped functions. */
c2255bc4 6102 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6103 return error_mark_node;
6104 }
3e4093b6 6105 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6106 {
3e4093b6
RS
6107 /* An explicit constant 0 can convert to a pointer,
6108 or one that results from arithmetic, even including
6109 a cast to integer type. */
928c19bb 6110 if (!null_pointer_constant)
c2255bc4 6111 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6112 G_("passing argument %d of %qE makes "
2ac2f164 6113 "pointer from integer without a cast"),
4b794eaf 6114 G_("assignment makes pointer from integer "
2ac2f164 6115 "without a cast"),
4b794eaf 6116 G_("initialization makes pointer from "
2ac2f164 6117 "integer without a cast"),
4b794eaf 6118 G_("return makes pointer from integer "
2ac2f164 6119 "without a cast"));
b3006337
EB
6120
6121 return convert (type, rhs);
400fbf9f 6122 }
3e4093b6 6123 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6124 {
c2255bc4 6125 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6126 G_("passing argument %d of %qE makes integer "
2ac2f164 6127 "from pointer without a cast"),
4b794eaf 6128 G_("assignment makes integer from pointer "
2ac2f164 6129 "without a cast"),
4b794eaf 6130 G_("initialization makes integer from pointer "
2ac2f164 6131 "without a cast"),
4b794eaf 6132 G_("return makes integer from pointer "
2ac2f164 6133 "without a cast"));
3e4093b6
RS
6134 return convert (type, rhs);
6135 }
6136 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6137 {
6138 tree ret;
6139 bool save = in_late_binary_op;
6140 in_late_binary_op = true;
6141 ret = convert (type, rhs);
6142 in_late_binary_op = save;
6143 return ret;
6144 }
400fbf9f 6145
2ac2f164 6146 switch (errtype)
3e4093b6 6147 {
2ac2f164 6148 case ic_argpass:
c2255bc4 6149 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
a7da8b42
MLI
6150 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6151 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
6152 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6153 break;
6154 case ic_assign:
c2255bc4
AH
6155 error_at (location, "incompatible types when assigning to type %qT from "
6156 "type %qT", type, rhstype);
2ac2f164
JM
6157 break;
6158 case ic_init:
c2255bc4
AH
6159 error_at (location,
6160 "incompatible types when initializing type %qT using type %qT",
6161 type, rhstype);
2ac2f164
JM
6162 break;
6163 case ic_return:
c2255bc4
AH
6164 error_at (location,
6165 "incompatible types when returning type %qT but %qT was "
6166 "expected", rhstype, type);
2ac2f164
JM
6167 break;
6168 default:
6169 gcc_unreachable ();
400fbf9f 6170 }
53b01f59 6171
3e4093b6
RS
6172 return error_mark_node;
6173}
3e4093b6
RS
6174\f
6175/* If VALUE is a compound expr all of whose expressions are constant, then
6176 return its value. Otherwise, return error_mark_node.
15b732b2 6177
3e4093b6
RS
6178 This is for handling COMPOUND_EXPRs as initializer elements
6179 which is allowed with a warning when -pedantic is specified. */
15b732b2 6180
3e4093b6
RS
6181static tree
6182valid_compound_expr_initializer (tree value, tree endtype)
6183{
6184 if (TREE_CODE (value) == COMPOUND_EXPR)
6185 {
6186 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6187 == error_mark_node)
6188 return error_mark_node;
6189 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6190 endtype);
6191 }
116df786 6192 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6193 return error_mark_node;
6194 else
6195 return value;
15b732b2 6196}
400fbf9f 6197\f
3e4093b6
RS
6198/* Perform appropriate conversions on the initial value of a variable,
6199 store it in the declaration DECL,
6200 and print any error messages that are appropriate.
bbbbb16a 6201 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6202 If the init is invalid, store an ERROR_MARK.
6203
6204 INIT_LOC is the location of the initial value. */
400fbf9f 6205
3e4093b6 6206void
c2255bc4 6207store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6208{
3e4093b6 6209 tree value, type;
928c19bb 6210 bool npc = false;
400fbf9f 6211
3e4093b6 6212 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6213
3e4093b6
RS
6214 type = TREE_TYPE (decl);
6215 if (TREE_CODE (type) == ERROR_MARK)
6216 return;
400fbf9f 6217
3e4093b6 6218 /* Digest the specified initializer into an expression. */
400fbf9f 6219
928c19bb
JM
6220 if (init)
6221 npc = null_pointer_constant_p (init);
c2255bc4
AH
6222 value = digest_init (init_loc, type, init, origtype, npc,
6223 true, TREE_STATIC (decl));
400fbf9f 6224
3e4093b6 6225 /* Store the expression if valid; else report error. */
400fbf9f 6226
8400e75e 6227 if (!in_system_header_at (input_location)
3f75a254 6228 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6229 warning (OPT_Wtraditional, "traditional C rejects automatic "
6230 "aggregate initialization");
2f6e4e97 6231
3e4093b6 6232 DECL_INITIAL (decl) = value;
400fbf9f 6233
3e4093b6
RS
6234 /* ANSI wants warnings about out-of-range constant initializers. */
6235 STRIP_TYPE_NOPS (value);
b8698a0f 6236 if (TREE_STATIC (decl))
c2658540 6237 constant_expression_warning (value);
400fbf9f 6238
3e4093b6
RS
6239 /* Check if we need to set array size from compound literal size. */
6240 if (TREE_CODE (type) == ARRAY_TYPE
6241 && TYPE_DOMAIN (type) == 0
6242 && value != error_mark_node)
400fbf9f 6243 {
3e4093b6
RS
6244 tree inside_init = init;
6245
ed248cf7 6246 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6247 inside_init = fold (inside_init);
6248
6249 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6250 {
8d9f82d5 6251 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6252
8d9f82d5 6253 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6254 {
6255 /* For int foo[] = (int [3]){1}; we need to set array size
6256 now since later on array initializer will be just the
6257 brace enclosed list of the compound literal. */
e30ecc5d 6258 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6259 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6260 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6261 layout_type (type);
8d9f82d5 6262 layout_decl (cldecl, 0);
e30ecc5d
JJ
6263 TREE_TYPE (decl)
6264 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6265 }
6266 }
400fbf9f 6267 }
3e4093b6
RS
6268}
6269\f
6270/* Methods for storing and printing names for error messages. */
400fbf9f 6271
3e4093b6
RS
6272/* Implement a spelling stack that allows components of a name to be pushed
6273 and popped. Each element on the stack is this structure. */
400fbf9f 6274
3e4093b6
RS
6275struct spelling
6276{
6277 int kind;
6278 union
400fbf9f 6279 {
a0f0ab9f 6280 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6281 const char *s;
6282 } u;
6283};
2f6e4e97 6284
3e4093b6
RS
6285#define SPELLING_STRING 1
6286#define SPELLING_MEMBER 2
6287#define SPELLING_BOUNDS 3
400fbf9f 6288
3e4093b6
RS
6289static struct spelling *spelling; /* Next stack element (unused). */
6290static struct spelling *spelling_base; /* Spelling stack base. */
6291static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6292
3e4093b6
RS
6293/* Macros to save and restore the spelling stack around push_... functions.
6294 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6295
3e4093b6
RS
6296#define SPELLING_DEPTH() (spelling - spelling_base)
6297#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6298
3e4093b6
RS
6299/* Push an element on the spelling stack with type KIND and assign VALUE
6300 to MEMBER. */
400fbf9f 6301
3e4093b6
RS
6302#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6303{ \
6304 int depth = SPELLING_DEPTH (); \
6305 \
6306 if (depth >= spelling_size) \
6307 { \
6308 spelling_size += 10; \
cca8ead2
BI
6309 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6310 spelling_size); \
3e4093b6
RS
6311 RESTORE_SPELLING_DEPTH (depth); \
6312 } \
6313 \
6314 spelling->kind = (KIND); \
6315 spelling->MEMBER = (VALUE); \
6316 spelling++; \
6317}
400fbf9f 6318
3e4093b6 6319/* Push STRING on the stack. Printed literally. */
400fbf9f 6320
3e4093b6
RS
6321static void
6322push_string (const char *string)
6323{
6324 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6325}
400fbf9f 6326
3e4093b6 6327/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6328
3e4093b6
RS
6329static void
6330push_member_name (tree decl)
6331{
6332 const char *const string
88388a52
JM
6333 = (DECL_NAME (decl)
6334 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6335 : _("<anonymous>"));
3e4093b6
RS
6336 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6337}
400fbf9f 6338
3e4093b6 6339/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6340
3e4093b6 6341static void
a0f0ab9f 6342push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6343{
6344 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6345}
bb58bec5 6346
3e4093b6 6347/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6348
3e4093b6
RS
6349static int
6350spelling_length (void)
6351{
6352 int size = 0;
6353 struct spelling *p;
400fbf9f 6354
3e4093b6
RS
6355 for (p = spelling_base; p < spelling; p++)
6356 {
6357 if (p->kind == SPELLING_BOUNDS)
6358 size += 25;
6359 else
6360 size += strlen (p->u.s) + 1;
6361 }
6362
6363 return size;
400fbf9f 6364}
400fbf9f 6365
3e4093b6 6366/* Print the spelling to BUFFER and return it. */
400fbf9f 6367
3e4093b6
RS
6368static char *
6369print_spelling (char *buffer)
400fbf9f 6370{
3e4093b6
RS
6371 char *d = buffer;
6372 struct spelling *p;
400fbf9f 6373
3e4093b6
RS
6374 for (p = spelling_base; p < spelling; p++)
6375 if (p->kind == SPELLING_BOUNDS)
6376 {
a0f0ab9f 6377 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6378 d += strlen (d);
6379 }
6380 else
6381 {
6382 const char *s;
6383 if (p->kind == SPELLING_MEMBER)
6384 *d++ = '.';
6385 for (s = p->u.s; (*d = *s++); d++)
6386 ;
6387 }
6388 *d++ = '\0';
6389 return buffer;
6390}
400fbf9f 6391
3e4093b6 6392/* Issue an error message for a bad initializer component.
6a8f4e12 6393 GMSGID identifies the message.
3e4093b6 6394 The component name is taken from the spelling stack. */
400fbf9f 6395
3e4093b6 6396void
6a8f4e12 6397error_init (const char *gmsgid)
3e4093b6
RS
6398{
6399 char *ofwhat;
400fbf9f 6400
6a8f4e12
AP
6401 /* The gmsgid may be a format string with %< and %>. */
6402 error (gmsgid);
28dab132 6403 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6404 if (*ofwhat)
bda67431 6405 error ("(near initialization for %qs)", ofwhat);
3e4093b6 6406}
400fbf9f 6407
fcf73884
MLI
6408/* Issue a pedantic warning for a bad initializer component. OPT is
6409 the option OPT_* (from options.h) controlling this warning or 0 if
6a8f4e12 6410 it is unconditionally given. GMSGID identifies the message. The
fcf73884 6411 component name is taken from the spelling stack. */
400fbf9f 6412
3e4093b6 6413void
6a8f4e12 6414pedwarn_init (location_t location, int opt, const char *gmsgid)
3e4093b6
RS
6415{
6416 char *ofwhat;
f90e8e2e 6417
6a8f4e12
AP
6418 /* The gmsgid may be a format string with %< and %>. */
6419 pedwarn (location, opt, gmsgid);
28dab132 6420 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6421 if (*ofwhat)
509c9d60 6422 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
3e4093b6 6423}
9f720c3e 6424
b8698a0f 6425/* Issue a warning for a bad initializer component.
683d6ff9
MLI
6426
6427 OPT is the OPT_W* value corresponding to the warning option that
6a8f4e12 6428 controls this warning. GMSGID identifies the message. The
683d6ff9 6429 component name is taken from the spelling stack. */
61179109 6430
3e4093b6 6431static void
6a8f4e12 6432warning_init (int opt, const char *gmsgid)
3e4093b6
RS
6433{
6434 char *ofwhat;
7e842ef8 6435
6a8f4e12
AP
6436 /* The gmsgid may be a format string with %< and %>. */
6437 warning (opt, gmsgid);
28dab132 6438 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6439 if (*ofwhat)
683d6ff9 6440 warning (opt, "(near initialization for %qs)", ofwhat);
3e4093b6
RS
6441}
6442\f
916c5919
JM
6443/* If TYPE is an array type and EXPR is a parenthesized string
6444 constant, warn if pedantic that EXPR is being used to initialize an
6445 object of type TYPE. */
6446
6447void
6448maybe_warn_string_init (tree type, struct c_expr expr)
6449{
6450 if (pedantic
6451 && TREE_CODE (type) == ARRAY_TYPE
6452 && TREE_CODE (expr.value) == STRING_CST
6453 && expr.original_code != STRING_CST)
c1771a20 6454 pedwarn_init (input_location, OPT_Wpedantic,
fcf73884 6455 "array initialized from parenthesized string constant");
916c5919
JM
6456}
6457
3e4093b6
RS
6458/* Digest the parser output INIT as an initializer for type TYPE.
6459 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6460
bbbbb16a
ILT
6461 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6462
928c19bb
JM
6463 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6464
916c5919
JM
6465 If INIT is a string constant, STRICT_STRING is true if it is
6466 unparenthesized or we should not warn here for it being parenthesized.
6467 For other types of INIT, STRICT_STRING is not used.
6468
c2255bc4
AH
6469 INIT_LOC is the location of the INIT.
6470
3e4093b6
RS
6471 REQUIRE_CONSTANT requests an error if non-constant initializers or
6472 elements are seen. */
7e842ef8 6473
3e4093b6 6474static tree
c2255bc4
AH
6475digest_init (location_t init_loc, tree type, tree init, tree origtype,
6476 bool null_pointer_constant, bool strict_string,
6477 int require_constant)
3e4093b6
RS
6478{
6479 enum tree_code code = TREE_CODE (type);
6480 tree inside_init = init;
8ce94e44 6481 tree semantic_type = NULL_TREE;
928c19bb 6482 bool maybe_const = true;
7e842ef8 6483
3e4093b6 6484 if (type == error_mark_node
f01da1a5 6485 || !init
3e4093b6
RS
6486 || init == error_mark_node
6487 || TREE_TYPE (init) == error_mark_node)
6488 return error_mark_node;
7e842ef8 6489
ed248cf7 6490 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6491
8ce94e44
JM
6492 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6493 {
6494 semantic_type = TREE_TYPE (inside_init);
6495 inside_init = TREE_OPERAND (inside_init, 0);
6496 }
928c19bb
JM
6497 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6498 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6499
3e4093b6
RS
6500 /* Initialization of an array of chars from a string constant
6501 optionally enclosed in braces. */
7e842ef8 6502
197463ae
JM
6503 if (code == ARRAY_TYPE && inside_init
6504 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6505 {
267bac10
JM
6506 tree typ1
6507 = (TYPE_ATOMIC (TREE_TYPE (type))
6508 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6509 TYPE_QUAL_ATOMIC)
6510 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6511 /* Note that an array could be both an array of character type
6512 and an array of wchar_t if wchar_t is signed char or unsigned
6513 char. */
6514 bool char_array = (typ1 == char_type_node
6515 || typ1 == signed_char_type_node
6516 || typ1 == unsigned_char_type_node);
6517 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6518 bool char16_array = !!comptypes (typ1, char16_type_node);
6519 bool char32_array = !!comptypes (typ1, char32_type_node);
6520
6521 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6522 {
916c5919 6523 struct c_expr expr;
c466b2cd 6524 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6525 expr.value = inside_init;
6526 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6527 expr.original_type = NULL;
916c5919
JM
6528 maybe_warn_string_init (type, expr);
6529
a45e580b 6530 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6531 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6532 "initialization of a flexible array member");
6533
3e4093b6 6534 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6535 TYPE_MAIN_VARIANT (type)))
3e4093b6 6536 return inside_init;
7e842ef8 6537
c466b2cd 6538 if (char_array)
3e4093b6 6539 {
c466b2cd
KVH
6540 if (typ2 != char_type_node)
6541 {
6542 error_init ("char-array initialized from wide string");
6543 return error_mark_node;
6544 }
3e4093b6 6545 }
c466b2cd 6546 else
3e4093b6 6547 {
c466b2cd
KVH
6548 if (typ2 == char_type_node)
6549 {
6550 error_init ("wide character array initialized from non-wide "
6551 "string");
6552 return error_mark_node;
6553 }
6554 else if (!comptypes(typ1, typ2))
6555 {
6556 error_init ("wide character array initialized from "
6557 "incompatible wide string");
6558 return error_mark_node;
6559 }
7e842ef8 6560 }
2f6e4e97 6561
3e4093b6
RS
6562 TREE_TYPE (inside_init) = type;
6563 if (TYPE_DOMAIN (type) != 0
6564 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6565 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6566 {
6567 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6568
c466b2cd 6569 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6570 because it's ok to ignore the terminating null char
6571 that is counted in the length of the constant. */
5eb4df45
ILT
6572 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6573 (len
6574 - (TYPE_PRECISION (typ1)
6575 / BITS_PER_UNIT))))
6576 pedwarn_init (init_loc, 0,
6577 ("initializer-string for array of chars "
6578 "is too long"));
6579 else if (warn_cxx_compat
6580 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6581 warning_at (init_loc, OPT_Wc___compat,
6582 ("initializer-string for array chars "
6583 "is too long for C++"));
6584 }
7e842ef8 6585
3e4093b6 6586 return inside_init;
7e842ef8 6587 }
197463ae
JM
6588 else if (INTEGRAL_TYPE_P (typ1))
6589 {
6590 error_init ("array of inappropriate type initialized "
6591 "from string constant");
6592 return error_mark_node;
6593 }
7e842ef8
PE
6594 }
6595
3e4093b6
RS
6596 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6597 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6598 below and handle as a constructor. */
e89be13b
JJ
6599 if (code == VECTOR_TYPE
6600 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6601 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6602 && TREE_CONSTANT (inside_init))
6603 {
6604 if (TREE_CODE (inside_init) == VECTOR_CST
6605 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6606 TYPE_MAIN_VARIANT (type)))
6607 return inside_init;
6608
6609 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6610 {
4038c495
GB
6611 unsigned HOST_WIDE_INT ix;
6612 tree value;
6613 bool constant_p = true;
e89be13b
JJ
6614
6615 /* Iterate through elements and check if all constructor
6616 elements are *_CSTs. */
4038c495
GB
6617 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6618 if (!CONSTANT_CLASS_P (value))
6619 {
6620 constant_p = false;
6621 break;
6622 }
e89be13b 6623
4038c495
GB
6624 if (constant_p)
6625 return build_vector_from_ctor (type,
6626 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6627 }
6628 }
6035d635 6629
ca085fd7
MLI
6630 if (warn_sequence_point)
6631 verify_sequence_points (inside_init);
6632
3e4093b6
RS
6633 /* Any type can be initialized
6634 from an expression of the same type, optionally with braces. */
400fbf9f 6635
3e4093b6
RS
6636 if (inside_init && TREE_TYPE (inside_init) != 0
6637 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6638 TYPE_MAIN_VARIANT (type))
3e4093b6 6639 || (code == ARRAY_TYPE
132da1a5 6640 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6641 || (code == VECTOR_TYPE
132da1a5 6642 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6643 || (code == POINTER_TYPE
3897f229 6644 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6645 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6646 TREE_TYPE (type)))))
3e4093b6
RS
6647 {
6648 if (code == POINTER_TYPE)
b494fd98 6649 {
b494fd98
EB
6650 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6651 {
f2a71bbc
JM
6652 if (TREE_CODE (inside_init) == STRING_CST
6653 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6654 inside_init = array_to_pointer_conversion
6655 (init_loc, inside_init);
f2a71bbc
JM
6656 else
6657 {
6658 error_init ("invalid use of non-lvalue array");
6659 return error_mark_node;
6660 }
b494fd98 6661 }
f2a71bbc 6662 }
b494fd98 6663
bae39a73
NS
6664 if (code == VECTOR_TYPE)
6665 /* Although the types are compatible, we may require a
6666 conversion. */
6667 inside_init = convert (type, inside_init);
3e4093b6 6668
ca58211b
PB
6669 if (require_constant
6670 && (code == VECTOR_TYPE || !flag_isoc99)
3e4093b6 6671 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6672 {
3e4093b6
RS
6673 /* As an extension, allow initializing objects with static storage
6674 duration with compound literals (which are then treated just as
ca58211b
PB
6675 the brace enclosed list they contain). Also allow this for
6676 vectors, as we can only assign them with compound literals. */
3e4093b6
RS
6677 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6678 inside_init = DECL_INITIAL (decl);
400fbf9f 6679 }
3e4093b6
RS
6680
6681 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6682 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6683 {
3e4093b6
RS
6684 error_init ("array initialized from non-constant array expression");
6685 return error_mark_node;
400fbf9f 6686 }
400fbf9f 6687
c1771a20 6688 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6689 -pedantic-errors is specified. In the later case, we always want
6690 an error. In the former case, we simply want a warning. */
6691 if (require_constant && pedantic
6692 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6693 {
6694 inside_init
6695 = valid_compound_expr_initializer (inside_init,
6696 TREE_TYPE (inside_init));
6697 if (inside_init == error_mark_node)
6698 error_init ("initializer element is not constant");
2f6e4e97 6699 else
c1771a20 6700 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6701 "initializer element is not constant");
3e4093b6
RS
6702 if (flag_pedantic_errors)
6703 inside_init = error_mark_node;
6704 }
6705 else if (require_constant
116df786
RH
6706 && !initializer_constant_valid_p (inside_init,
6707 TREE_TYPE (inside_init)))
3e4093b6
RS
6708 {
6709 error_init ("initializer element is not constant");
6710 inside_init = error_mark_node;
8b40563c 6711 }
928c19bb 6712 else if (require_constant && !maybe_const)
c2255bc4 6713 pedwarn_init (init_loc, 0,
928c19bb 6714 "initializer element is not a constant expression");
f735a153 6715
90137d8f 6716 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6717 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
6718 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6719 type, inside_init, origtype,
bbbbb16a 6720 ic_init, null_pointer_constant,
928c19bb 6721 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6722 return inside_init;
6723 }
f735a153 6724
3e4093b6 6725 /* Handle scalar types, including conversions. */
400fbf9f 6726
ab22c1fa
CF
6727 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6728 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6729 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6730 {
f2a71bbc
JM
6731 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6732 && (TREE_CODE (init) == STRING_CST
6733 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6734 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6735 if (semantic_type)
6736 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6737 inside_init);
3e4093b6 6738 inside_init
68fca595
MP
6739 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6740 inside_init, origtype, ic_init,
6741 null_pointer_constant, NULL_TREE, NULL_TREE,
6742 0);
2f6e4e97 6743
3274deff
JW
6744 /* Check to see if we have already given an error message. */
6745 if (inside_init == error_mark_node)
6746 ;
3f75a254 6747 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6748 {
3e4093b6
RS
6749 error_init ("initializer element is not constant");
6750 inside_init = error_mark_node;
400fbf9f 6751 }
3e4093b6 6752 else if (require_constant
116df786
RH
6753 && !initializer_constant_valid_p (inside_init,
6754 TREE_TYPE (inside_init)))
400fbf9f 6755 {
3e4093b6
RS
6756 error_init ("initializer element is not computable at load time");
6757 inside_init = error_mark_node;
400fbf9f 6758 }
928c19bb 6759 else if (require_constant && !maybe_const)
c2255bc4 6760 pedwarn_init (init_loc, 0,
928c19bb 6761 "initializer element is not a constant expression");
3e4093b6
RS
6762
6763 return inside_init;
400fbf9f 6764 }
d9fc6069 6765
3e4093b6 6766 /* Come here only for records and arrays. */
d9fc6069 6767
3e4093b6 6768 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6769 {
3e4093b6
RS
6770 error_init ("variable-sized object may not be initialized");
6771 return error_mark_node;
d9fc6069 6772 }
3e4093b6
RS
6773
6774 error_init ("invalid initializer");
6775 return error_mark_node;
d9fc6069 6776}
400fbf9f 6777\f
3e4093b6 6778/* Handle initializers that use braces. */
400fbf9f 6779
3e4093b6
RS
6780/* Type of object we are accumulating a constructor for.
6781 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6782static tree constructor_type;
400fbf9f 6783
3e4093b6
RS
6784/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6785 left to fill. */
6786static tree constructor_fields;
400fbf9f 6787
3e4093b6
RS
6788/* For an ARRAY_TYPE, this is the specified index
6789 at which to store the next element we get. */
6790static tree constructor_index;
400fbf9f 6791
3e4093b6
RS
6792/* For an ARRAY_TYPE, this is the maximum index. */
6793static tree constructor_max_index;
400fbf9f 6794
3e4093b6
RS
6795/* For a RECORD_TYPE, this is the first field not yet written out. */
6796static tree constructor_unfilled_fields;
400fbf9f 6797
3e4093b6
RS
6798/* For an ARRAY_TYPE, this is the index of the first element
6799 not yet written out. */
6800static tree constructor_unfilled_index;
895ea614 6801
3e4093b6
RS
6802/* In a RECORD_TYPE, the byte index of the next consecutive field.
6803 This is so we can generate gaps between fields, when appropriate. */
6804static tree constructor_bit_index;
10d5caec 6805
3e4093b6
RS
6806/* If we are saving up the elements rather than allocating them,
6807 this is the list of elements so far (in reverse order,
6808 most recent first). */
9771b263 6809static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6810
3e4093b6
RS
6811/* 1 if constructor should be incrementally stored into a constructor chain,
6812 0 if all the elements should be kept in AVL tree. */
6813static int constructor_incremental;
ad47f1e5 6814
3e4093b6
RS
6815/* 1 if so far this constructor's elements are all compile-time constants. */
6816static int constructor_constant;
ad47f1e5 6817
3e4093b6
RS
6818/* 1 if so far this constructor's elements are all valid address constants. */
6819static int constructor_simple;
ad47f1e5 6820
928c19bb
JM
6821/* 1 if this constructor has an element that cannot be part of a
6822 constant expression. */
6823static int constructor_nonconst;
6824
3e4093b6
RS
6825/* 1 if this constructor is erroneous so far. */
6826static int constructor_erroneous;
d45cf215 6827
3e4093b6
RS
6828/* Structure for managing pending initializer elements, organized as an
6829 AVL tree. */
d45cf215 6830
3e4093b6 6831struct init_node
d45cf215 6832{
3e4093b6
RS
6833 struct init_node *left, *right;
6834 struct init_node *parent;
6835 int balance;
6836 tree purpose;
6837 tree value;
bbbbb16a 6838 tree origtype;
d45cf215
RS
6839};
6840
3e4093b6
RS
6841/* Tree of pending elements at this constructor level.
6842 These are elements encountered out of order
6843 which belong at places we haven't reached yet in actually
6844 writing the output.
6845 Will never hold tree nodes across GC runs. */
6846static struct init_node *constructor_pending_elts;
d45cf215 6847
3e4093b6
RS
6848/* The SPELLING_DEPTH of this constructor. */
6849static int constructor_depth;
d45cf215 6850
3e4093b6
RS
6851/* DECL node for which an initializer is being read.
6852 0 means we are reading a constructor expression
6853 such as (struct foo) {...}. */
6854static tree constructor_decl;
d45cf215 6855
3e4093b6
RS
6856/* Nonzero if this is an initializer for a top-level decl. */
6857static int constructor_top_level;
d45cf215 6858
3e4093b6
RS
6859/* Nonzero if there were any member designators in this initializer. */
6860static int constructor_designated;
d45cf215 6861
3e4093b6
RS
6862/* Nesting depth of designator list. */
6863static int designator_depth;
d45cf215 6864
3e4093b6 6865/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 6866static int designator_erroneous;
d45cf215 6867
3e4093b6
RS
6868\f
6869/* This stack has a level for each implicit or explicit level of
6870 structuring in the initializer, including the outermost one. It
6871 saves the values of most of the variables above. */
d45cf215 6872
3e4093b6
RS
6873struct constructor_range_stack;
6874
6875struct constructor_stack
d45cf215 6876{
3e4093b6
RS
6877 struct constructor_stack *next;
6878 tree type;
6879 tree fields;
6880 tree index;
6881 tree max_index;
6882 tree unfilled_index;
6883 tree unfilled_fields;
6884 tree bit_index;
9771b263 6885 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6886 struct init_node *pending_elts;
6887 int offset;
6888 int depth;
916c5919 6889 /* If value nonzero, this value should replace the entire
3e4093b6 6890 constructor at this level. */
916c5919 6891 struct c_expr replacement_value;
3e4093b6
RS
6892 struct constructor_range_stack *range_stack;
6893 char constant;
6894 char simple;
928c19bb 6895 char nonconst;
3e4093b6
RS
6896 char implicit;
6897 char erroneous;
6898 char outer;
6899 char incremental;
6900 char designated;
6901};
d45cf215 6902
802415d1 6903static struct constructor_stack *constructor_stack;
d45cf215 6904
3e4093b6
RS
6905/* This stack represents designators from some range designator up to
6906 the last designator in the list. */
d45cf215 6907
3e4093b6
RS
6908struct constructor_range_stack
6909{
6910 struct constructor_range_stack *next, *prev;
6911 struct constructor_stack *stack;
6912 tree range_start;
6913 tree index;
6914 tree range_end;
6915 tree fields;
6916};
d45cf215 6917
802415d1 6918static struct constructor_range_stack *constructor_range_stack;
d45cf215 6919
3e4093b6
RS
6920/* This stack records separate initializers that are nested.
6921 Nested initializers can't happen in ANSI C, but GNU C allows them
6922 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 6923
3e4093b6 6924struct initializer_stack
d45cf215 6925{
3e4093b6
RS
6926 struct initializer_stack *next;
6927 tree decl;
3e4093b6
RS
6928 struct constructor_stack *constructor_stack;
6929 struct constructor_range_stack *constructor_range_stack;
9771b263 6930 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6931 struct spelling *spelling;
6932 struct spelling *spelling_base;
6933 int spelling_size;
6934 char top_level;
6935 char require_constant_value;
6936 char require_constant_elements;
6937};
d45cf215 6938
802415d1 6939static struct initializer_stack *initializer_stack;
3e4093b6
RS
6940\f
6941/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
6942
6943void
a396f8ae 6944start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 6945{
3e4093b6 6946 const char *locus;
cceb1885 6947 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 6948
3e4093b6 6949 p->decl = constructor_decl;
3e4093b6
RS
6950 p->require_constant_value = require_constant_value;
6951 p->require_constant_elements = require_constant_elements;
6952 p->constructor_stack = constructor_stack;
6953 p->constructor_range_stack = constructor_range_stack;
6954 p->elements = constructor_elements;
6955 p->spelling = spelling;
6956 p->spelling_base = spelling_base;
6957 p->spelling_size = spelling_size;
6958 p->top_level = constructor_top_level;
6959 p->next = initializer_stack;
6960 initializer_stack = p;
400fbf9f 6961
3e4093b6 6962 constructor_decl = decl;
3e4093b6
RS
6963 constructor_designated = 0;
6964 constructor_top_level = top_level;
400fbf9f 6965
6f17bbcf 6966 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
6967 {
6968 require_constant_value = TREE_STATIC (decl);
6969 require_constant_elements
6970 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6971 /* For a scalar, you can always use any value to initialize,
6972 even within braces. */
6973 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6974 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6975 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6976 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 6977 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
6978 }
6979 else
6980 {
6981 require_constant_value = 0;
6982 require_constant_elements = 0;
88388a52 6983 locus = _("(anonymous)");
3e4093b6 6984 }
b71c7f8a 6985
3e4093b6
RS
6986 constructor_stack = 0;
6987 constructor_range_stack = 0;
b71c7f8a 6988
3e4093b6
RS
6989 missing_braces_mentioned = 0;
6990
6991 spelling_base = 0;
6992 spelling_size = 0;
6993 RESTORE_SPELLING_DEPTH (0);
6994
6995 if (locus)
6996 push_string (locus);
6997}
6998
6999void
7000finish_init (void)
b71c7f8a 7001{
3e4093b6 7002 struct initializer_stack *p = initializer_stack;
b71c7f8a 7003
3e4093b6
RS
7004 /* Free the whole constructor stack of this initializer. */
7005 while (constructor_stack)
7006 {
7007 struct constructor_stack *q = constructor_stack;
7008 constructor_stack = q->next;
7009 free (q);
7010 }
7011
366de0ce 7012 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7013
7014 /* Pop back to the data of the outer initializer (if any). */
36579663 7015 free (spelling_base);
3aeb3655 7016
3e4093b6 7017 constructor_decl = p->decl;
3e4093b6
RS
7018 require_constant_value = p->require_constant_value;
7019 require_constant_elements = p->require_constant_elements;
7020 constructor_stack = p->constructor_stack;
7021 constructor_range_stack = p->constructor_range_stack;
7022 constructor_elements = p->elements;
7023 spelling = p->spelling;
7024 spelling_base = p->spelling_base;
7025 spelling_size = p->spelling_size;
7026 constructor_top_level = p->top_level;
7027 initializer_stack = p->next;
7028 free (p);
b71c7f8a 7029}
400fbf9f 7030\f
3e4093b6
RS
7031/* Call here when we see the initializer is surrounded by braces.
7032 This is instead of a call to push_init_level;
7033 it is matched by a call to pop_init_level.
400fbf9f 7034
3e4093b6
RS
7035 TYPE is the type to initialize, for a constructor expression.
7036 For an initializer for a decl, TYPE is zero. */
400fbf9f 7037
3e4093b6
RS
7038void
7039really_start_incremental_init (tree type)
400fbf9f 7040{
5d038c4c 7041 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7042
3e4093b6
RS
7043 if (type == 0)
7044 type = TREE_TYPE (constructor_decl);
400fbf9f 7045
b6fc2cdb
PB
7046 if (TREE_CODE (type) == VECTOR_TYPE
7047 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7048 error ("opaque vector types cannot be initialized");
400fbf9f 7049
3e4093b6
RS
7050 p->type = constructor_type;
7051 p->fields = constructor_fields;
7052 p->index = constructor_index;
7053 p->max_index = constructor_max_index;
7054 p->unfilled_index = constructor_unfilled_index;
7055 p->unfilled_fields = constructor_unfilled_fields;
7056 p->bit_index = constructor_bit_index;
7057 p->elements = constructor_elements;
7058 p->constant = constructor_constant;
7059 p->simple = constructor_simple;
928c19bb 7060 p->nonconst = constructor_nonconst;
3e4093b6
RS
7061 p->erroneous = constructor_erroneous;
7062 p->pending_elts = constructor_pending_elts;
7063 p->depth = constructor_depth;
916c5919
JM
7064 p->replacement_value.value = 0;
7065 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7066 p->replacement_value.original_type = NULL;
3e4093b6
RS
7067 p->implicit = 0;
7068 p->range_stack = 0;
7069 p->outer = 0;
7070 p->incremental = constructor_incremental;
7071 p->designated = constructor_designated;
7072 p->next = 0;
7073 constructor_stack = p;
b13aca19 7074
3e4093b6
RS
7075 constructor_constant = 1;
7076 constructor_simple = 1;
928c19bb 7077 constructor_nonconst = 0;
3e4093b6 7078 constructor_depth = SPELLING_DEPTH ();
9771b263 7079 constructor_elements = NULL;
3e4093b6
RS
7080 constructor_pending_elts = 0;
7081 constructor_type = type;
7082 constructor_incremental = 1;
7083 constructor_designated = 0;
7084 designator_depth = 0;
b06df647 7085 designator_erroneous = 0;
400fbf9f 7086
3e4093b6
RS
7087 if (TREE_CODE (constructor_type) == RECORD_TYPE
7088 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7089 {
3e4093b6
RS
7090 constructor_fields = TYPE_FIELDS (constructor_type);
7091 /* Skip any nameless bit fields at the beginning. */
7092 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7093 && DECL_NAME (constructor_fields) == 0)
910ad8de 7094 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7095
3e4093b6
RS
7096 constructor_unfilled_fields = constructor_fields;
7097 constructor_bit_index = bitsize_zero_node;
400fbf9f 7098 }
3e4093b6
RS
7099 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7100 {
7101 if (TYPE_DOMAIN (constructor_type))
7102 {
7103 constructor_max_index
7104 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7105
3e4093b6
RS
7106 /* Detect non-empty initializations of zero-length arrays. */
7107 if (constructor_max_index == NULL_TREE
7108 && TYPE_SIZE (constructor_type))
9a9d280e 7109 constructor_max_index = integer_minus_one_node;
400fbf9f 7110
3e4093b6
RS
7111 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7112 to initialize VLAs will cause a proper error; avoid tree
7113 checking errors as well by setting a safe value. */
7114 if (constructor_max_index
7115 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7116 constructor_max_index = integer_minus_one_node;
59c83dbf 7117
3e4093b6
RS
7118 constructor_index
7119 = convert (bitsizetype,
7120 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7121 }
3e4093b6 7122 else
493179da
JM
7123 {
7124 constructor_index = bitsize_zero_node;
7125 constructor_max_index = NULL_TREE;
7126 }
59c83dbf 7127
3e4093b6
RS
7128 constructor_unfilled_index = constructor_index;
7129 }
7130 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7131 {
7132 /* Vectors are like simple fixed-size arrays. */
7133 constructor_max_index =
c62c040f 7134 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7135 constructor_index = bitsize_zero_node;
3e4093b6
RS
7136 constructor_unfilled_index = constructor_index;
7137 }
7138 else
7139 {
7140 /* Handle the case of int x = {5}; */
7141 constructor_fields = constructor_type;
7142 constructor_unfilled_fields = constructor_type;
7143 }
7144}
7145\f
7146/* Push down into a subobject, for initialization.
7147 If this is for an explicit set of braces, IMPLICIT is 0.
7148 If it is because the next element belongs at a lower level,
7149 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7150
3e4093b6 7151void
a1e3b3d9 7152push_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
7153{
7154 struct constructor_stack *p;
7155 tree value = NULL_TREE;
400fbf9f 7156
3e4093b6 7157 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7158 pop them now. If implicit == 1, this will have been done in
7159 process_init_element; do not repeat it here because in the case
7160 of excess initializers for an empty aggregate this leads to an
7161 infinite cycle of popping a level and immediately recreating
7162 it. */
7163 if (implicit != 1)
3e4093b6 7164 {
472d98b4
JM
7165 while (constructor_stack->implicit)
7166 {
7167 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7168 || TREE_CODE (constructor_type) == UNION_TYPE)
7169 && constructor_fields == 0)
a1e3b3d9
LB
7170 process_init_element (pop_init_level (1, braced_init_obstack),
7171 true, braced_init_obstack);
472d98b4
JM
7172 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7173 && constructor_max_index
7174 && tree_int_cst_lt (constructor_max_index,
7175 constructor_index))
a1e3b3d9
LB
7176 process_init_element (pop_init_level (1, braced_init_obstack),
7177 true, braced_init_obstack);
472d98b4
JM
7178 else
7179 break;
7180 }
3e4093b6 7181 }
400fbf9f 7182
3e4093b6
RS
7183 /* Unless this is an explicit brace, we need to preserve previous
7184 content if any. */
7185 if (implicit)
7186 {
7187 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7188 || TREE_CODE (constructor_type) == UNION_TYPE)
7189 && constructor_fields)
a1e3b3d9 7190 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7191 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7192 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7193 }
7194
5d038c4c 7195 p = XNEW (struct constructor_stack);
3e4093b6
RS
7196 p->type = constructor_type;
7197 p->fields = constructor_fields;
7198 p->index = constructor_index;
7199 p->max_index = constructor_max_index;
7200 p->unfilled_index = constructor_unfilled_index;
7201 p->unfilled_fields = constructor_unfilled_fields;
7202 p->bit_index = constructor_bit_index;
7203 p->elements = constructor_elements;
7204 p->constant = constructor_constant;
7205 p->simple = constructor_simple;
928c19bb 7206 p->nonconst = constructor_nonconst;
3e4093b6
RS
7207 p->erroneous = constructor_erroneous;
7208 p->pending_elts = constructor_pending_elts;
7209 p->depth = constructor_depth;
916c5919
JM
7210 p->replacement_value.value = 0;
7211 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7212 p->replacement_value.original_type = NULL;
3e4093b6
RS
7213 p->implicit = implicit;
7214 p->outer = 0;
7215 p->incremental = constructor_incremental;
7216 p->designated = constructor_designated;
7217 p->next = constructor_stack;
7218 p->range_stack = 0;
7219 constructor_stack = p;
400fbf9f 7220
3e4093b6
RS
7221 constructor_constant = 1;
7222 constructor_simple = 1;
928c19bb 7223 constructor_nonconst = 0;
3e4093b6 7224 constructor_depth = SPELLING_DEPTH ();
9771b263 7225 constructor_elements = NULL;
3e4093b6
RS
7226 constructor_incremental = 1;
7227 constructor_designated = 0;
7228 constructor_pending_elts = 0;
7229 if (!implicit)
400fbf9f 7230 {
3e4093b6
RS
7231 p->range_stack = constructor_range_stack;
7232 constructor_range_stack = 0;
7233 designator_depth = 0;
b06df647 7234 designator_erroneous = 0;
3e4093b6 7235 }
400fbf9f 7236
3e4093b6
RS
7237 /* Don't die if an entire brace-pair level is superfluous
7238 in the containing level. */
7239 if (constructor_type == 0)
7240 ;
7241 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7242 || TREE_CODE (constructor_type) == UNION_TYPE)
7243 {
7244 /* Don't die if there are extra init elts at the end. */
7245 if (constructor_fields == 0)
7246 constructor_type = 0;
7247 else
400fbf9f 7248 {
3e4093b6
RS
7249 constructor_type = TREE_TYPE (constructor_fields);
7250 push_member_name (constructor_fields);
7251 constructor_depth++;
400fbf9f 7252 }
3e4093b6
RS
7253 }
7254 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7255 {
7256 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7257 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7258 constructor_depth++;
400fbf9f
JW
7259 }
7260
3e4093b6 7261 if (constructor_type == 0)
400fbf9f 7262 {
3e4093b6
RS
7263 error_init ("extra brace group at end of initializer");
7264 constructor_fields = 0;
7265 constructor_unfilled_fields = 0;
7266 return;
400fbf9f
JW
7267 }
7268
3e4093b6
RS
7269 if (value && TREE_CODE (value) == CONSTRUCTOR)
7270 {
7271 constructor_constant = TREE_CONSTANT (value);
7272 constructor_simple = TREE_STATIC (value);
928c19bb 7273 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7274 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7275 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7276 && (TREE_CODE (constructor_type) == RECORD_TYPE
7277 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7278 set_nonincremental_init (braced_init_obstack);
3e4093b6 7279 }
400fbf9f 7280
3e4093b6
RS
7281 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
7282 {
7283 missing_braces_mentioned = 1;
683d6ff9 7284 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
3e4093b6 7285 }
400fbf9f 7286
3e4093b6
RS
7287 if (TREE_CODE (constructor_type) == RECORD_TYPE
7288 || TREE_CODE (constructor_type) == UNION_TYPE)
7289 {
7290 constructor_fields = TYPE_FIELDS (constructor_type);
7291 /* Skip any nameless bit fields at the beginning. */
7292 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7293 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7294 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7295
3e4093b6
RS
7296 constructor_unfilled_fields = constructor_fields;
7297 constructor_bit_index = bitsize_zero_node;
7298 }
7299 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7300 {
7301 /* Vectors are like simple fixed-size arrays. */
7302 constructor_max_index =
c62c040f
RG
7303 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7304 constructor_index = bitsize_int (0);
3e4093b6
RS
7305 constructor_unfilled_index = constructor_index;
7306 }
7307 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7308 {
7309 if (TYPE_DOMAIN (constructor_type))
7310 {
7311 constructor_max_index
7312 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7313
3e4093b6
RS
7314 /* Detect non-empty initializations of zero-length arrays. */
7315 if (constructor_max_index == NULL_TREE
7316 && TYPE_SIZE (constructor_type))
9a9d280e 7317 constructor_max_index = integer_minus_one_node;
de520661 7318
3e4093b6
RS
7319 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7320 to initialize VLAs will cause a proper error; avoid tree
7321 checking errors as well by setting a safe value. */
7322 if (constructor_max_index
7323 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7324 constructor_max_index = integer_minus_one_node;
b62acd60 7325
3e4093b6
RS
7326 constructor_index
7327 = convert (bitsizetype,
7328 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7329 }
7330 else
7331 constructor_index = bitsize_zero_node;
de520661 7332
3e4093b6
RS
7333 constructor_unfilled_index = constructor_index;
7334 if (value && TREE_CODE (value) == STRING_CST)
7335 {
7336 /* We need to split the char/wchar array into individual
7337 characters, so that we don't have to special case it
7338 everywhere. */
a1e3b3d9 7339 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7340 }
7341 }
7342 else
7343 {
b4519d39 7344 if (constructor_type != error_mark_node)
683d6ff9 7345 warning_init (0, "braces around scalar initializer");
3e4093b6
RS
7346 constructor_fields = constructor_type;
7347 constructor_unfilled_fields = constructor_type;
7348 }
7349}
8b6a5902 7350
3e4093b6 7351/* At the end of an implicit or explicit brace level,
916c5919
JM
7352 finish up that level of constructor. If a single expression
7353 with redundant braces initialized that level, return the
7354 c_expr structure for that expression. Otherwise, the original_code
7355 element is set to ERROR_MARK.
7356 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7357 from inner levels (process_init_element ignores that),
916c5919 7358 but return error_mark_node as the value from the outermost level
3e4093b6 7359 (that's what we want to put in DECL_INITIAL).
916c5919 7360 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7361
916c5919 7362struct c_expr
a1e3b3d9 7363pop_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
7364{
7365 struct constructor_stack *p;
916c5919
JM
7366 struct c_expr ret;
7367 ret.value = 0;
7368 ret.original_code = ERROR_MARK;
6866c6e8 7369 ret.original_type = NULL;
de520661 7370
3e4093b6
RS
7371 if (implicit == 0)
7372 {
7373 /* When we come to an explicit close brace,
7374 pop any inner levels that didn't have explicit braces. */
7375 while (constructor_stack->implicit)
a1e3b3d9
LB
7376 {
7377 process_init_element (pop_init_level (1, braced_init_obstack),
7378 true, braced_init_obstack);
7379 }
366de0ce 7380 gcc_assert (!constructor_range_stack);
3e4093b6 7381 }
e5e809f4 7382
0066ef9c
RH
7383 /* Now output all pending elements. */
7384 constructor_incremental = 1;
a1e3b3d9 7385 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7386
3e4093b6 7387 p = constructor_stack;
e5e809f4 7388
3e4093b6
RS
7389 /* Error for initializing a flexible array member, or a zero-length
7390 array member in an inappropriate context. */
7391 if (constructor_type && constructor_fields
7392 && TREE_CODE (constructor_type) == ARRAY_TYPE
7393 && TYPE_DOMAIN (constructor_type)
3f75a254 7394 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7395 {
7396 /* Silently discard empty initializations. The parser will
7397 already have pedwarned for empty brackets. */
7398 if (integer_zerop (constructor_unfilled_index))
7399 constructor_type = NULL_TREE;
366de0ce 7400 else
3e4093b6 7401 {
366de0ce 7402 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7403
3e4093b6
RS
7404 if (constructor_depth > 2)
7405 error_init ("initialization of flexible array member in a nested context");
fcf73884 7406 else
c1771a20 7407 pedwarn_init (input_location, OPT_Wpedantic,
509c9d60 7408 "initialization of a flexible array member");
de520661 7409
3e4093b6
RS
7410 /* We have already issued an error message for the existence
7411 of a flexible array member not at the end of the structure.
535a42b1 7412 Discard the initializer so that we do not die later. */
910ad8de 7413 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7414 constructor_type = NULL_TREE;
7415 }
3e4093b6 7416 }
de520661 7417
3e4093b6 7418 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7419 if (warn_missing_field_initializers
3e4093b6
RS
7420 && constructor_type
7421 && TREE_CODE (constructor_type) == RECORD_TYPE
7422 && constructor_unfilled_fields)
7423 {
49819fef 7424 bool constructor_zeroinit =
9771b263
DN
7425 (vec_safe_length (constructor_elements) == 1
7426 && integer_zerop ((*constructor_elements)[0].value));
49819fef 7427
3e4093b6
RS
7428 /* Do not warn for flexible array members or zero-length arrays. */
7429 while (constructor_unfilled_fields
3f75a254 7430 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7431 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7432 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7433
49819fef
AM
7434 if (constructor_unfilled_fields
7435 /* Do not warn if this level of the initializer uses member
7436 designators; it is likely to be deliberate. */
7437 && !constructor_designated
7438 /* Do not warn about initializing with ` = {0}'. */
7439 && !constructor_zeroinit)
3e4093b6 7440 {
32397f22
MLI
7441 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7442 "missing initializer for field %qD of %qT",
7443 constructor_unfilled_fields,
7444 constructor_type))
7445 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7446 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7447 }
7448 }
de520661 7449
3e4093b6 7450 /* Pad out the end of the structure. */
916c5919 7451 if (p->replacement_value.value)
3e4093b6
RS
7452 /* If this closes a superfluous brace pair,
7453 just pass out the element between them. */
916c5919 7454 ret = p->replacement_value;
3e4093b6
RS
7455 else if (constructor_type == 0)
7456 ;
7457 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7458 && TREE_CODE (constructor_type) != UNION_TYPE
7459 && TREE_CODE (constructor_type) != ARRAY_TYPE
7460 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7461 {
7462 /* A nonincremental scalar initializer--just return
7463 the element, after verifying there is just one. */
9771b263 7464 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7465 {
7466 if (!constructor_erroneous)
7467 error_init ("empty scalar initializer");
916c5919 7468 ret.value = error_mark_node;
3e4093b6 7469 }
9771b263 7470 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6
RS
7471 {
7472 error_init ("extra elements in scalar initializer");
9771b263 7473 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7474 }
7475 else
9771b263 7476 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7477 }
7478 else
7479 {
7480 if (constructor_erroneous)
916c5919 7481 ret.value = error_mark_node;
3e4093b6
RS
7482 else
7483 {
916c5919 7484 ret.value = build_constructor (constructor_type,
4038c495 7485 constructor_elements);
3e4093b6 7486 if (constructor_constant)
51eed280 7487 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7488 if (constructor_constant && constructor_simple)
916c5919 7489 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7490 if (constructor_nonconst)
7491 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7492 }
7493 }
de520661 7494
928c19bb
JM
7495 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7496 {
7497 if (constructor_nonconst)
7498 ret.original_code = C_MAYBE_CONST_EXPR;
7499 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7500 ret.original_code = ERROR_MARK;
7501 }
7502
3e4093b6
RS
7503 constructor_type = p->type;
7504 constructor_fields = p->fields;
7505 constructor_index = p->index;
7506 constructor_max_index = p->max_index;
7507 constructor_unfilled_index = p->unfilled_index;
7508 constructor_unfilled_fields = p->unfilled_fields;
7509 constructor_bit_index = p->bit_index;
7510 constructor_elements = p->elements;
7511 constructor_constant = p->constant;
7512 constructor_simple = p->simple;
928c19bb 7513 constructor_nonconst = p->nonconst;
3e4093b6
RS
7514 constructor_erroneous = p->erroneous;
7515 constructor_incremental = p->incremental;
7516 constructor_designated = p->designated;
7517 constructor_pending_elts = p->pending_elts;
7518 constructor_depth = p->depth;
7519 if (!p->implicit)
7520 constructor_range_stack = p->range_stack;
7521 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7522
3e4093b6
RS
7523 constructor_stack = p->next;
7524 free (p);
b621a4dd 7525
5d5e98dc
VR
7526 if (ret.value == 0 && constructor_stack == 0)
7527 ret.value = error_mark_node;
916c5919 7528 return ret;
3e4093b6 7529}
8b6a5902 7530
3e4093b6
RS
7531/* Common handling for both array range and field name designators.
7532 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7533
3e4093b6 7534static int
a1e3b3d9 7535set_designator (int array, struct obstack * braced_init_obstack)
de520661 7536{
3e4093b6
RS
7537 tree subtype;
7538 enum tree_code subcode;
de520661 7539
3e4093b6
RS
7540 /* Don't die if an entire brace-pair level is superfluous
7541 in the containing level. */
7542 if (constructor_type == 0)
7543 return 1;
de520661 7544
366de0ce
NS
7545 /* If there were errors in this designator list already, bail out
7546 silently. */
b06df647 7547 if (designator_erroneous)
3e4093b6 7548 return 1;
e28cae4f 7549
3e4093b6
RS
7550 if (!designator_depth)
7551 {
366de0ce 7552 gcc_assert (!constructor_range_stack);
de520661 7553
3e4093b6
RS
7554 /* Designator list starts at the level of closest explicit
7555 braces. */
7556 while (constructor_stack->implicit)
a1e3b3d9
LB
7557 {
7558 process_init_element (pop_init_level (1, braced_init_obstack),
7559 true, braced_init_obstack);
7560 }
3e4093b6
RS
7561 constructor_designated = 1;
7562 return 0;
7563 }
de520661 7564
366de0ce 7565 switch (TREE_CODE (constructor_type))
3c3fa147 7566 {
366de0ce
NS
7567 case RECORD_TYPE:
7568 case UNION_TYPE:
3e4093b6
RS
7569 subtype = TREE_TYPE (constructor_fields);
7570 if (subtype != error_mark_node)
7571 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7572 break;
7573 case ARRAY_TYPE:
3e4093b6 7574 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7575 break;
7576 default:
7577 gcc_unreachable ();
de520661 7578 }
400fbf9f 7579
3e4093b6
RS
7580 subcode = TREE_CODE (subtype);
7581 if (array && subcode != ARRAY_TYPE)
7582 {
7583 error_init ("array index in non-array initializer");
7584 return 1;
7585 }
7586 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7587 {
7588 error_init ("field name not in record or union initializer");
7589 return 1;
7590 }
d45cf215 7591
3e4093b6 7592 constructor_designated = 1;
a1e3b3d9 7593 push_init_level (2, braced_init_obstack);
3e4093b6 7594 return 0;
de520661 7595}
400fbf9f 7596
3e4093b6
RS
7597/* If there are range designators in designator list, push a new designator
7598 to constructor_range_stack. RANGE_END is end of such stack range or
7599 NULL_TREE if there is no range designator at this level. */
400fbf9f 7600
3e4093b6 7601static void
a1e3b3d9 7602push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7603{
7604 struct constructor_range_stack *p;
400fbf9f 7605
a1e3b3d9
LB
7606 p = (struct constructor_range_stack *)
7607 obstack_alloc (braced_init_obstack,
7608 sizeof (struct constructor_range_stack));
3e4093b6
RS
7609 p->prev = constructor_range_stack;
7610 p->next = 0;
7611 p->fields = constructor_fields;
7612 p->range_start = constructor_index;
7613 p->index = constructor_index;
7614 p->stack = constructor_stack;
7615 p->range_end = range_end;
8b6a5902 7616 if (constructor_range_stack)
3e4093b6
RS
7617 constructor_range_stack->next = p;
7618 constructor_range_stack = p;
de520661 7619}
400fbf9f 7620
3e4093b6
RS
7621/* Within an array initializer, specify the next index to be initialized.
7622 FIRST is that index. If LAST is nonzero, then initialize a range
7623 of indices, running from FIRST through LAST. */
5a7ec9d9 7624
de520661 7625void
a1e3b3d9
LB
7626set_init_index (tree first, tree last,
7627 struct obstack * braced_init_obstack)
de520661 7628{
a1e3b3d9 7629 if (set_designator (1, braced_init_obstack))
3e4093b6 7630 return;
de520661 7631
b06df647 7632 designator_erroneous = 1;
de520661 7633
3ea8cd06
JM
7634 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7635 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7636 {
7637 error_init ("array index in initializer not of integer type");
7638 return;
7639 }
7640
2b6da65c
JM
7641 if (TREE_CODE (first) != INTEGER_CST)
7642 {
7643 first = c_fully_fold (first, false, NULL);
7644 if (TREE_CODE (first) == INTEGER_CST)
c1771a20 7645 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7646 "array index in initializer is not "
7647 "an integer constant expression");
7648 }
7649
7650 if (last && TREE_CODE (last) != INTEGER_CST)
7651 {
7652 last = c_fully_fold (last, false, NULL);
7653 if (TREE_CODE (last) == INTEGER_CST)
c1771a20 7654 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7655 "array index in initializer is not "
7656 "an integer constant expression");
7657 }
7658
3e4093b6
RS
7659 if (TREE_CODE (first) != INTEGER_CST)
7660 error_init ("nonconstant array index in initializer");
7661 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7662 error_init ("nonconstant array index in initializer");
7663 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7664 error_init ("array index in non-array initializer");
622adc7e
MK
7665 else if (tree_int_cst_sgn (first) == -1)
7666 error_init ("array index in initializer exceeds array bounds");
3e4093b6
RS
7667 else if (constructor_max_index
7668 && tree_int_cst_lt (constructor_max_index, first))
7669 error_init ("array index in initializer exceeds array bounds");
7670 else
de520661 7671 {
928c19bb
JM
7672 constant_expression_warning (first);
7673 if (last)
7674 constant_expression_warning (last);
3e4093b6 7675 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7676 if (tree_int_cst_lt (constructor_index, first))
7677 {
7678 constructor_index = copy_node (constructor_index);
7679 TREE_OVERFLOW (constructor_index) = 1;
7680 }
665f2503 7681
3e4093b6 7682 if (last)
2bede729 7683 {
3e4093b6
RS
7684 if (tree_int_cst_equal (first, last))
7685 last = 0;
7686 else if (tree_int_cst_lt (last, first))
7687 {
7688 error_init ("empty index range in initializer");
7689 last = 0;
7690 }
7691 else
7692 {
7693 last = convert (bitsizetype, last);
7694 if (constructor_max_index != 0
7695 && tree_int_cst_lt (constructor_max_index, last))
7696 {
7697 error_init ("array index range in initializer exceeds array bounds");
7698 last = 0;
7699 }
7700 }
2bede729 7701 }
fed3cef0 7702
3e4093b6 7703 designator_depth++;
b06df647 7704 designator_erroneous = 0;
3e4093b6 7705 if (constructor_range_stack || last)
a1e3b3d9 7706 push_range_stack (last, braced_init_obstack);
de520661 7707 }
de520661 7708}
3e4093b6
RS
7709
7710/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7711
de520661 7712void
a1e3b3d9 7713set_init_label (tree fieldname, struct obstack * braced_init_obstack)
de520661 7714{
0fb96aa4 7715 tree field;
94ba5069 7716
a1e3b3d9 7717 if (set_designator (0, braced_init_obstack))
3e4093b6
RS
7718 return;
7719
b06df647 7720 designator_erroneous = 1;
3e4093b6
RS
7721
7722 if (TREE_CODE (constructor_type) != RECORD_TYPE
7723 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7724 {
3e4093b6
RS
7725 error_init ("field name not in record or union initializer");
7726 return;
94ba5069
RS
7727 }
7728
0fb96aa4 7729 field = lookup_field (constructor_type, fieldname);
8b6a5902 7730
0fb96aa4 7731 if (field == 0)
c51a1ba9 7732 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 7733 else
0fb96aa4
JM
7734 do
7735 {
7736 constructor_fields = TREE_VALUE (field);
7737 designator_depth++;
7738 designator_erroneous = 0;
7739 if (constructor_range_stack)
7740 push_range_stack (NULL_TREE, braced_init_obstack);
7741 field = TREE_CHAIN (field);
7742 if (field)
7743 {
7744 if (set_designator (0, braced_init_obstack))
7745 return;
7746 }
7747 }
7748 while (field != NULL_TREE);
3e4093b6
RS
7749}
7750\f
7751/* Add a new initializer to the tree of pending initializers. PURPOSE
7752 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7753 VALUE is the value of that index or field. If ORIGTYPE is not
7754 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7755
7756 IMPLICIT is true if value comes from pop_init_level (1),
7757 the new initializer has been merged with the existing one
7758 and thus no warnings should be emitted about overriding an
7759 existing initializer. */
de520661 7760
3e4093b6 7761static void
a1e3b3d9
LB
7762add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7763 struct obstack * braced_init_obstack)
3e4093b6
RS
7764{
7765 struct init_node *p, **q, *r;
7766
7767 q = &constructor_pending_elts;
7768 p = 0;
7769
7770 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7771 {
3e4093b6 7772 while (*q != 0)
91fa3c30 7773 {
3e4093b6
RS
7774 p = *q;
7775 if (tree_int_cst_lt (purpose, p->purpose))
7776 q = &p->left;
7777 else if (tree_int_cst_lt (p->purpose, purpose))
7778 q = &p->right;
7779 else
7780 {
b295aee2
JJ
7781 if (!implicit)
7782 {
7783 if (TREE_SIDE_EFFECTS (p->value))
7784 warning_init (0, "initialized field with side-effects overwritten");
7785 else if (warn_override_init)
7786 warning_init (OPT_Woverride_init, "initialized field overwritten");
7787 }
3e4093b6 7788 p->value = value;
bbbbb16a 7789 p->origtype = origtype;
3e4093b6
RS
7790 return;
7791 }
91fa3c30 7792 }
de520661 7793 }
3e4093b6 7794 else
de520661 7795 {
3e4093b6 7796 tree bitpos;
400fbf9f 7797
3e4093b6
RS
7798 bitpos = bit_position (purpose);
7799 while (*q != NULL)
7800 {
7801 p = *q;
7802 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7803 q = &p->left;
7804 else if (p->purpose != purpose)
7805 q = &p->right;
7806 else
7807 {
b295aee2
JJ
7808 if (!implicit)
7809 {
7810 if (TREE_SIDE_EFFECTS (p->value))
7811 warning_init (0, "initialized field with side-effects overwritten");
7812 else if (warn_override_init)
7813 warning_init (OPT_Woverride_init, "initialized field overwritten");
7814 }
3e4093b6 7815 p->value = value;
bbbbb16a 7816 p->origtype = origtype;
3e4093b6
RS
7817 return;
7818 }
7819 }
91fa3c30 7820 }
b71c7f8a 7821
a1e3b3d9
LB
7822 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7823 sizeof (struct init_node));
3e4093b6
RS
7824 r->purpose = purpose;
7825 r->value = value;
bbbbb16a 7826 r->origtype = origtype;
8b6a5902 7827
3e4093b6
RS
7828 *q = r;
7829 r->parent = p;
7830 r->left = 0;
7831 r->right = 0;
7832 r->balance = 0;
b71c7f8a 7833
3e4093b6 7834 while (p)
de520661 7835 {
3e4093b6 7836 struct init_node *s;
665f2503 7837
3e4093b6 7838 if (r == p->left)
2bede729 7839 {
3e4093b6
RS
7840 if (p->balance == 0)
7841 p->balance = -1;
7842 else if (p->balance < 0)
7843 {
7844 if (r->balance < 0)
7845 {
7846 /* L rotation. */
7847 p->left = r->right;
7848 if (p->left)
7849 p->left->parent = p;
7850 r->right = p;
e7b6a0ee 7851
3e4093b6
RS
7852 p->balance = 0;
7853 r->balance = 0;
39bc99c2 7854
3e4093b6
RS
7855 s = p->parent;
7856 p->parent = r;
7857 r->parent = s;
7858 if (s)
7859 {
7860 if (s->left == p)
7861 s->left = r;
7862 else
7863 s->right = r;
7864 }
7865 else
7866 constructor_pending_elts = r;
7867 }
7868 else
7869 {
7870 /* LR rotation. */
7871 struct init_node *t = r->right;
e7b6a0ee 7872
3e4093b6
RS
7873 r->right = t->left;
7874 if (r->right)
7875 r->right->parent = r;
7876 t->left = r;
7877
7878 p->left = t->right;
7879 if (p->left)
7880 p->left->parent = p;
7881 t->right = p;
7882
7883 p->balance = t->balance < 0;
7884 r->balance = -(t->balance > 0);
7885 t->balance = 0;
7886
7887 s = p->parent;
7888 p->parent = t;
7889 r->parent = t;
7890 t->parent = s;
7891 if (s)
7892 {
7893 if (s->left == p)
7894 s->left = t;
7895 else
7896 s->right = t;
7897 }
7898 else
7899 constructor_pending_elts = t;
7900 }
7901 break;
7902 }
7903 else
7904 {
7905 /* p->balance == +1; growth of left side balances the node. */
7906 p->balance = 0;
7907 break;
7908 }
2bede729 7909 }
3e4093b6
RS
7910 else /* r == p->right */
7911 {
7912 if (p->balance == 0)
7913 /* Growth propagation from right side. */
7914 p->balance++;
7915 else if (p->balance > 0)
7916 {
7917 if (r->balance > 0)
7918 {
7919 /* R rotation. */
7920 p->right = r->left;
7921 if (p->right)
7922 p->right->parent = p;
7923 r->left = p;
7924
7925 p->balance = 0;
7926 r->balance = 0;
7927
7928 s = p->parent;
7929 p->parent = r;
7930 r->parent = s;
7931 if (s)
7932 {
7933 if (s->left == p)
7934 s->left = r;
7935 else
7936 s->right = r;
7937 }
7938 else
7939 constructor_pending_elts = r;
7940 }
7941 else /* r->balance == -1 */
7942 {
7943 /* RL rotation */
7944 struct init_node *t = r->left;
7945
7946 r->left = t->right;
7947 if (r->left)
7948 r->left->parent = r;
7949 t->right = r;
7950
7951 p->right = t->left;
7952 if (p->right)
7953 p->right->parent = p;
7954 t->left = p;
7955
7956 r->balance = (t->balance < 0);
7957 p->balance = -(t->balance > 0);
7958 t->balance = 0;
7959
7960 s = p->parent;
7961 p->parent = t;
7962 r->parent = t;
7963 t->parent = s;
7964 if (s)
7965 {
7966 if (s->left == p)
7967 s->left = t;
7968 else
7969 s->right = t;
7970 }
7971 else
7972 constructor_pending_elts = t;
7973 }
7974 break;
7975 }
7976 else
7977 {
7978 /* p->balance == -1; growth of right side balances the node. */
7979 p->balance = 0;
7980 break;
7981 }
7982 }
7983
7984 r = p;
7985 p = p->parent;
7986 }
7987}
7988
7989/* Build AVL tree from a sorted chain. */
7990
7991static void
a1e3b3d9 7992set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 7993{
4038c495
GB
7994 unsigned HOST_WIDE_INT ix;
7995 tree index, value;
3e4093b6
RS
7996
7997 if (TREE_CODE (constructor_type) != RECORD_TYPE
7998 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7999 return;
8000
4038c495 8001 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
a1e3b3d9 8002 {
fdce1719 8003 add_pending_init (index, value, NULL_TREE, true,
a1e3b3d9
LB
8004 braced_init_obstack);
8005 }
9771b263 8006 constructor_elements = NULL;
3e4093b6
RS
8007 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8008 {
8009 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8010 /* Skip any nameless bit fields at the beginning. */
8011 while (constructor_unfilled_fields != 0
8012 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8013 && DECL_NAME (constructor_unfilled_fields) == 0)
8014 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8015
de520661 8016 }
3e4093b6 8017 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8018 {
3e4093b6
RS
8019 if (TYPE_DOMAIN (constructor_type))
8020 constructor_unfilled_index
8021 = convert (bitsizetype,
8022 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8023 else
8024 constructor_unfilled_index = bitsize_zero_node;
de520661 8025 }
3e4093b6 8026 constructor_incremental = 0;
de520661 8027}
400fbf9f 8028
3e4093b6 8029/* Build AVL tree from a string constant. */
de520661 8030
3e4093b6 8031static void
a1e3b3d9
LB
8032set_nonincremental_init_from_string (tree str,
8033 struct obstack * braced_init_obstack)
de520661 8034{
3e4093b6
RS
8035 tree value, purpose, type;
8036 HOST_WIDE_INT val[2];
8037 const char *p, *end;
8038 int byte, wchar_bytes, charwidth, bitpos;
de520661 8039
366de0ce 8040 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8041
c466b2cd 8042 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8043 charwidth = TYPE_PRECISION (char_type_node);
8044 type = TREE_TYPE (constructor_type);
8045 p = TREE_STRING_POINTER (str);
8046 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8047
3e4093b6 8048 for (purpose = bitsize_zero_node;
8824edff
JJ
8049 p < end
8050 && !(constructor_max_index
8051 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8052 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8053 {
3e4093b6 8054 if (wchar_bytes == 1)
ffc5c6a9 8055 {
3e4093b6
RS
8056 val[1] = (unsigned char) *p++;
8057 val[0] = 0;
ffc5c6a9
RH
8058 }
8059 else
3e4093b6
RS
8060 {
8061 val[0] = 0;
8062 val[1] = 0;
8063 for (byte = 0; byte < wchar_bytes; byte++)
8064 {
8065 if (BYTES_BIG_ENDIAN)
8066 bitpos = (wchar_bytes - byte - 1) * charwidth;
8067 else
8068 bitpos = byte * charwidth;
8069 val[bitpos < HOST_BITS_PER_WIDE_INT]
8070 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8071 << (bitpos % HOST_BITS_PER_WIDE_INT);
8072 }
8073 }
584ef5fe 8074
8df83eae 8075 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8076 {
8077 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8078 if (bitpos < HOST_BITS_PER_WIDE_INT)
8079 {
8080 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8081 {
8082 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
8083 val[0] = -1;
8084 }
8085 }
8086 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8087 {
8088 if (val[1] < 0)
c22cacf3 8089 val[0] = -1;
3e4093b6
RS
8090 }
8091 else if (val[0] & (((HOST_WIDE_INT) 1)
8092 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8093 val[0] |= ((HOST_WIDE_INT) -1)
8094 << (bitpos - HOST_BITS_PER_WIDE_INT);
8095 }
ffc5c6a9 8096
7d60be94 8097 value = build_int_cst_wide (type, val[1], val[0]);
fdce1719 8098 add_pending_init (purpose, value, NULL_TREE, true,
a1e3b3d9 8099 braced_init_obstack);
9dfcc8db
BH
8100 }
8101
3e4093b6
RS
8102 constructor_incremental = 0;
8103}
de520661 8104
3e4093b6
RS
8105/* Return value of FIELD in pending initializer or zero if the field was
8106 not initialized yet. */
8107
8108static tree
a1e3b3d9 8109find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8110{
8111 struct init_node *p;
8112
8113 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8114 {
3e4093b6
RS
8115 if (constructor_incremental
8116 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8117 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8118
8119 p = constructor_pending_elts;
8120 while (p)
19d76e60 8121 {
3e4093b6
RS
8122 if (tree_int_cst_lt (field, p->purpose))
8123 p = p->left;
8124 else if (tree_int_cst_lt (p->purpose, field))
8125 p = p->right;
8126 else
8127 return p->value;
19d76e60 8128 }
19d76e60 8129 }
3e4093b6 8130 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8131 {
3e4093b6 8132 tree bitpos = bit_position (field);
de520661 8133
3e4093b6
RS
8134 if (constructor_incremental
8135 && (!constructor_unfilled_fields
8136 || tree_int_cst_lt (bitpos,
8137 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8138 set_nonincremental_init (braced_init_obstack);
de520661 8139
3e4093b6
RS
8140 p = constructor_pending_elts;
8141 while (p)
8142 {
8143 if (field == p->purpose)
8144 return p->value;
8145 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8146 p = p->left;
8147 else
8148 p = p->right;
8149 }
8150 }
8151 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8152 {
9771b263
DN
8153 if (!vec_safe_is_empty (constructor_elements)
8154 && (constructor_elements->last ().index == field))
8155 return constructor_elements->last ().value;
de520661 8156 }
3e4093b6 8157 return 0;
de520661
RS
8158}
8159
3e4093b6
RS
8160/* "Output" the next constructor element.
8161 At top level, really output it to assembler code now.
8162 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8163 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8164 TYPE is the data type that the containing data type wants here.
8165 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8166 If VALUE is a string constant, STRICT_STRING is true if it is
8167 unparenthesized or we should not warn here for it being parenthesized.
8168 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8169
3e4093b6
RS
8170 PENDING if non-nil means output pending elements that belong
8171 right after this element. (PENDING is normally 1;
b295aee2
JJ
8172 it is 0 while outputting pending elements, to avoid recursion.)
8173
8174 IMPLICIT is true if value comes from pop_init_level (1),
8175 the new initializer has been merged with the existing one
8176 and thus no warnings should be emitted about overriding an
8177 existing initializer. */
8b6a5902 8178
3e4093b6 8179static void
bbbbb16a 8180output_init_element (tree value, tree origtype, bool strict_string, tree type,
a1e3b3d9
LB
8181 tree field, int pending, bool implicit,
8182 struct obstack * braced_init_obstack)
3e4093b6 8183{
8ce94e44 8184 tree semantic_type = NULL_TREE;
928c19bb
JM
8185 bool maybe_const = true;
8186 bool npc;
4038c495 8187
0a880880 8188 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8189 {
3e4093b6
RS
8190 constructor_erroneous = 1;
8191 return;
8b6a5902 8192 }
46bdb9cf
JM
8193 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8194 && (TREE_CODE (value) == STRING_CST
8195 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8196 && !(TREE_CODE (value) == STRING_CST
8197 && TREE_CODE (type) == ARRAY_TYPE
8198 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8199 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8200 TYPE_MAIN_VARIANT (type)))
c2255bc4 8201 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8202
3e4093b6
RS
8203 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8204 && require_constant_value && !flag_isoc99 && pending)
8b6a5902 8205 {
3e4093b6
RS
8206 /* As an extension, allow initializing objects with static storage
8207 duration with compound literals (which are then treated just as
8208 the brace enclosed list they contain). */
8209 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8210 value = DECL_INITIAL (decl);
8b6a5902
JJ
8211 }
8212
928c19bb 8213 npc = null_pointer_constant_p (value);
8ce94e44
JM
8214 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8215 {
8216 semantic_type = TREE_TYPE (value);
8217 value = TREE_OPERAND (value, 0);
8218 }
928c19bb
JM
8219 value = c_fully_fold (value, require_constant_value, &maybe_const);
8220
3e4093b6
RS
8221 if (value == error_mark_node)
8222 constructor_erroneous = 1;
8223 else if (!TREE_CONSTANT (value))
8224 constructor_constant = 0;
116df786 8225 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8226 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8227 || TREE_CODE (constructor_type) == UNION_TYPE)
8228 && DECL_C_BIT_FIELD (field)
8229 && TREE_CODE (value) != INTEGER_CST))
8230 constructor_simple = 0;
928c19bb
JM
8231 if (!maybe_const)
8232 constructor_nonconst = 1;
3e4093b6 8233
116df786 8234 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8235 {
116df786
RH
8236 if (require_constant_value)
8237 {
8238 error_init ("initializer element is not constant");
8239 value = error_mark_node;
8240 }
8241 else if (require_constant_elements)
509c9d60
MLI
8242 pedwarn (input_location, 0,
8243 "initializer element is not computable at load time");
8b6a5902 8244 }
928c19bb
JM
8245 else if (!maybe_const
8246 && (require_constant_value || require_constant_elements))
8247 pedwarn_init (input_location, 0,
8248 "initializer element is not a constant expression");
3e4093b6 8249
81f40b79
ILT
8250 /* Issue -Wc++-compat warnings about initializing a bitfield with
8251 enum type. */
8252 if (warn_cxx_compat
8253 && field != NULL_TREE
8254 && TREE_CODE (field) == FIELD_DECL
8255 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8256 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8257 != TYPE_MAIN_VARIANT (type))
8258 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8259 {
8260 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8261 if (checktype != error_mark_node
8262 && (TYPE_MAIN_VARIANT (checktype)
8263 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8264 warning_init (OPT_Wc___compat,
8265 "enum conversion in initialization is invalid in C++");
8266 }
8267
3e4093b6
RS
8268 /* If this field is empty (and not at the end of structure),
8269 don't do anything other than checking the initializer. */
8270 if (field
8271 && (TREE_TYPE (field) == error_mark_node
8272 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8273 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8274 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8275 || DECL_CHAIN (field)))))
3e4093b6
RS
8276 return;
8277
8ce94e44
JM
8278 if (semantic_type)
8279 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
c2255bc4
AH
8280 value = digest_init (input_location, type, value, origtype, npc,
8281 strict_string, require_constant_value);
3e4093b6 8282 if (value == error_mark_node)
8b6a5902 8283 {
3e4093b6
RS
8284 constructor_erroneous = 1;
8285 return;
8b6a5902 8286 }
928c19bb
JM
8287 if (require_constant_value || require_constant_elements)
8288 constant_expression_warning (value);
8b6a5902 8289
3e4093b6
RS
8290 /* If this element doesn't come next in sequence,
8291 put it on constructor_pending_elts. */
8292 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8293 && (!constructor_incremental
8294 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8295 {
3e4093b6
RS
8296 if (constructor_incremental
8297 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8298 set_nonincremental_init (braced_init_obstack);
3e4093b6 8299
a1e3b3d9
LB
8300 add_pending_init (field, value, origtype, implicit,
8301 braced_init_obstack);
3e4093b6 8302 return;
8b6a5902 8303 }
3e4093b6
RS
8304 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8305 && (!constructor_incremental
8306 || field != constructor_unfilled_fields))
8b6a5902 8307 {
3e4093b6
RS
8308 /* We do this for records but not for unions. In a union,
8309 no matter which field is specified, it can be initialized
8310 right away since it starts at the beginning of the union. */
8311 if (constructor_incremental)
8312 {
8313 if (!constructor_unfilled_fields)
a1e3b3d9 8314 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8315 else
8316 {
8317 tree bitpos, unfillpos;
8318
8319 bitpos = bit_position (field);
8320 unfillpos = bit_position (constructor_unfilled_fields);
8321
8322 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8323 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8324 }
8325 }
8326
a1e3b3d9
LB
8327 add_pending_init (field, value, origtype, implicit,
8328 braced_init_obstack);
3e4093b6 8329 return;
8b6a5902 8330 }
3e4093b6 8331 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8332 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8333 {
b295aee2
JJ
8334 if (!implicit)
8335 {
9771b263 8336 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
b295aee2
JJ
8337 warning_init (0,
8338 "initialized field with side-effects overwritten");
8339 else if (warn_override_init)
8340 warning_init (OPT_Woverride_init, "initialized field overwritten");
8341 }
8b6a5902 8342
3e4093b6 8343 /* We can have just one union field set. */
9771b263 8344 constructor_elements = NULL;
3e4093b6 8345 }
8b6a5902 8346
3e4093b6
RS
8347 /* Otherwise, output this element either to
8348 constructor_elements or to the assembler file. */
8b6a5902 8349
f32682ca 8350 constructor_elt celt = {field, value};
9771b263 8351 vec_safe_push (constructor_elements, celt);
8b6a5902 8352
3e4093b6
RS
8353 /* Advance the variable that indicates sequential elements output. */
8354 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8355 constructor_unfilled_index
db3927fb
AH
8356 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8357 bitsize_one_node);
3e4093b6
RS
8358 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8359 {
8360 constructor_unfilled_fields
910ad8de 8361 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8362
3e4093b6
RS
8363 /* Skip any nameless bit fields. */
8364 while (constructor_unfilled_fields != 0
8365 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8366 && DECL_NAME (constructor_unfilled_fields) == 0)
8367 constructor_unfilled_fields =
910ad8de 8368 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8369 }
8370 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8371 constructor_unfilled_fields = 0;
de520661 8372
3e4093b6
RS
8373 /* Now output any pending elements which have become next. */
8374 if (pending)
a1e3b3d9 8375 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8376}
8b6a5902 8377
3e4093b6
RS
8378/* Output any pending elements which have become next.
8379 As we output elements, constructor_unfilled_{fields,index}
8380 advances, which may cause other elements to become next;
8381 if so, they too are output.
8b6a5902 8382
3e4093b6
RS
8383 If ALL is 0, we return when there are
8384 no more pending elements to output now.
665f2503 8385
3e4093b6
RS
8386 If ALL is 1, we output space as necessary so that
8387 we can output all the pending elements. */
3e4093b6 8388static void
a1e3b3d9 8389output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8390{
8391 struct init_node *elt = constructor_pending_elts;
8392 tree next;
de520661 8393
3e4093b6
RS
8394 retry:
8395
ba228239 8396 /* Look through the whole pending tree.
3e4093b6
RS
8397 If we find an element that should be output now,
8398 output it. Otherwise, set NEXT to the element
8399 that comes first among those still pending. */
8400
8401 next = 0;
8402 while (elt)
8403 {
8404 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8405 {
3e4093b6
RS
8406 if (tree_int_cst_equal (elt->purpose,
8407 constructor_unfilled_index))
bbbbb16a 8408 output_init_element (elt->value, elt->origtype, true,
3e4093b6 8409 TREE_TYPE (constructor_type),
a1e3b3d9
LB
8410 constructor_unfilled_index, 0, false,
8411 braced_init_obstack);
3e4093b6
RS
8412 else if (tree_int_cst_lt (constructor_unfilled_index,
8413 elt->purpose))
8b6a5902 8414 {
3e4093b6
RS
8415 /* Advance to the next smaller node. */
8416 if (elt->left)
8417 elt = elt->left;
8418 else
8419 {
8420 /* We have reached the smallest node bigger than the
8421 current unfilled index. Fill the space first. */
8422 next = elt->purpose;
8423 break;
8424 }
8b6a5902 8425 }
ce662d4c
JJ
8426 else
8427 {
3e4093b6
RS
8428 /* Advance to the next bigger node. */
8429 if (elt->right)
8430 elt = elt->right;
8431 else
ce662d4c 8432 {
3e4093b6
RS
8433 /* We have reached the biggest node in a subtree. Find
8434 the parent of it, which is the next bigger node. */
8435 while (elt->parent && elt->parent->right == elt)
8436 elt = elt->parent;
8437 elt = elt->parent;
8438 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8439 elt->purpose))
8440 {
8441 next = elt->purpose;
8442 break;
8443 }
ce662d4c
JJ
8444 }
8445 }
8b6a5902 8446 }
3e4093b6
RS
8447 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8448 || TREE_CODE (constructor_type) == UNION_TYPE)
8449 {
8450 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8451
3e4093b6
RS
8452 /* If the current record is complete we are done. */
8453 if (constructor_unfilled_fields == 0)
8454 break;
de520661 8455
3e4093b6
RS
8456 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8457 elt_bitpos = bit_position (elt->purpose);
8458 /* We can't compare fields here because there might be empty
8459 fields in between. */
8460 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8461 {
8462 constructor_unfilled_fields = elt->purpose;
bbbbb16a
ILT
8463 output_init_element (elt->value, elt->origtype, true,
8464 TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8465 elt->purpose, 0, false,
8466 braced_init_obstack);
3e4093b6
RS
8467 }
8468 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8469 {
8470 /* Advance to the next smaller node. */
8471 if (elt->left)
8472 elt = elt->left;
8473 else
8474 {
8475 /* We have reached the smallest node bigger than the
8476 current unfilled field. Fill the space first. */
8477 next = elt->purpose;
8478 break;
8479 }
8480 }
8481 else
8482 {
8483 /* Advance to the next bigger node. */
8484 if (elt->right)
8485 elt = elt->right;
8486 else
8487 {
8488 /* We have reached the biggest node in a subtree. Find
8489 the parent of it, which is the next bigger node. */
8490 while (elt->parent && elt->parent->right == elt)
8491 elt = elt->parent;
8492 elt = elt->parent;
8493 if (elt
8494 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8495 bit_position (elt->purpose))))
8496 {
8497 next = elt->purpose;
8498 break;
8499 }
8500 }
8501 }
8502 }
8503 }
de520661 8504
3e4093b6
RS
8505 /* Ordinarily return, but not if we want to output all
8506 and there are elements left. */
3f75a254 8507 if (!(all && next != 0))
e5cfb88f
RK
8508 return;
8509
3e4093b6
RS
8510 /* If it's not incremental, just skip over the gap, so that after
8511 jumping to retry we will output the next successive element. */
8512 if (TREE_CODE (constructor_type) == RECORD_TYPE
8513 || TREE_CODE (constructor_type) == UNION_TYPE)
8514 constructor_unfilled_fields = next;
8515 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8516 constructor_unfilled_index = next;
de520661 8517
3e4093b6
RS
8518 /* ELT now points to the node in the pending tree with the next
8519 initializer to output. */
8520 goto retry;
de520661
RS
8521}
8522\f
3e4093b6
RS
8523/* Add one non-braced element to the current constructor level.
8524 This adjusts the current position within the constructor's type.
8525 This may also start or terminate implicit levels
8526 to handle a partly-braced initializer.
e5e809f4 8527
3e4093b6 8528 Once this has found the correct level for the new element,
b295aee2
JJ
8529 it calls output_init_element.
8530
8531 IMPLICIT is true if value comes from pop_init_level (1),
8532 the new initializer has been merged with the existing one
8533 and thus no warnings should be emitted about overriding an
8534 existing initializer. */
3e4093b6
RS
8535
8536void
a1e3b3d9
LB
8537process_init_element (struct c_expr value, bool implicit,
8538 struct obstack * braced_init_obstack)
e5e809f4 8539{
916c5919
JM
8540 tree orig_value = value.value;
8541 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8542 bool strict_string = value.original_code == STRING_CST;
340baef7 8543 bool was_designated = designator_depth != 0;
e5e809f4 8544
3e4093b6 8545 designator_depth = 0;
b06df647 8546 designator_erroneous = 0;
e5e809f4 8547
3e4093b6
RS
8548 /* Handle superfluous braces around string cst as in
8549 char x[] = {"foo"}; */
8550 if (string_flag
8551 && constructor_type
340baef7 8552 && !was_designated
3e4093b6 8553 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8554 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8555 && integer_zerop (constructor_unfilled_index))
e5e809f4 8556 {
916c5919 8557 if (constructor_stack->replacement_value.value)
c22cacf3 8558 error_init ("excess elements in char array initializer");
3e4093b6
RS
8559 constructor_stack->replacement_value = value;
8560 return;
e5e809f4 8561 }
8b6a5902 8562
916c5919 8563 if (constructor_stack->replacement_value.value != 0)
3e4093b6
RS
8564 {
8565 error_init ("excess elements in struct initializer");
8566 return;
e5e809f4
JL
8567 }
8568
3e4093b6
RS
8569 /* Ignore elements of a brace group if it is entirely superfluous
8570 and has already been diagnosed. */
8571 if (constructor_type == 0)
8572 return;
e5e809f4 8573
3e4093b6
RS
8574 /* If we've exhausted any levels that didn't have braces,
8575 pop them now. */
8576 while (constructor_stack->implicit)
8577 {
8578 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8579 || TREE_CODE (constructor_type) == UNION_TYPE)
8580 && constructor_fields == 0)
a1e3b3d9
LB
8581 process_init_element (pop_init_level (1, braced_init_obstack),
8582 true, braced_init_obstack);
53650abe
AP
8583 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8584 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8585 && constructor_max_index
8586 && tree_int_cst_lt (constructor_max_index,
8587 constructor_index))
a1e3b3d9
LB
8588 process_init_element (pop_init_level (1, braced_init_obstack),
8589 true, braced_init_obstack);
3e4093b6
RS
8590 else
8591 break;
8592 }
e5e809f4 8593
3e4093b6
RS
8594 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8595 if (constructor_range_stack)
e5e809f4 8596 {
3e4093b6
RS
8597 /* If value is a compound literal and we'll be just using its
8598 content, don't put it into a SAVE_EXPR. */
916c5919 8599 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
8600 || !require_constant_value
8601 || flag_isoc99)
8ce94e44
JM
8602 {
8603 tree semantic_type = NULL_TREE;
8604 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8605 {
8606 semantic_type = TREE_TYPE (value.value);
8607 value.value = TREE_OPERAND (value.value, 0);
8608 }
8609 value.value = c_save_expr (value.value);
8610 if (semantic_type)
8611 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8612 value.value);
8613 }
3e4093b6 8614 }
e5e809f4 8615
3e4093b6
RS
8616 while (1)
8617 {
8618 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8619 {
3e4093b6
RS
8620 tree fieldtype;
8621 enum tree_code fieldcode;
e5e809f4 8622
3e4093b6
RS
8623 if (constructor_fields == 0)
8624 {
509c9d60
MLI
8625 pedwarn_init (input_location, 0,
8626 "excess elements in struct initializer");
3e4093b6
RS
8627 break;
8628 }
e5e809f4 8629
3e4093b6
RS
8630 fieldtype = TREE_TYPE (constructor_fields);
8631 if (fieldtype != error_mark_node)
8632 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8633 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8634
3e4093b6
RS
8635 /* Error for non-static initialization of a flexible array member. */
8636 if (fieldcode == ARRAY_TYPE
8637 && !require_constant_value
8638 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8639 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6
RS
8640 {
8641 error_init ("non-static initialization of a flexible array member");
8642 break;
8643 }
e5e809f4 8644
3e4093b6 8645 /* Accept a string constant to initialize a subarray. */
916c5919 8646 if (value.value != 0
3e4093b6 8647 && fieldcode == ARRAY_TYPE
197463ae 8648 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8649 && string_flag)
916c5919 8650 value.value = orig_value;
3e4093b6
RS
8651 /* Otherwise, if we have come to a subaggregate,
8652 and we don't have an element of its type, push into it. */
0953878d 8653 else if (value.value != 0
916c5919
JM
8654 && value.value != error_mark_node
8655 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8656 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8657 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8658 {
a1e3b3d9 8659 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8660 continue;
8661 }
e5e809f4 8662
916c5919 8663 if (value.value)
3e4093b6
RS
8664 {
8665 push_member_name (constructor_fields);
bbbbb16a
ILT
8666 output_init_element (value.value, value.original_type,
8667 strict_string, fieldtype,
a1e3b3d9
LB
8668 constructor_fields, 1, implicit,
8669 braced_init_obstack);
3e4093b6 8670 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8671 }
8672 else
3e4093b6
RS
8673 /* Do the bookkeeping for an element that was
8674 directly output as a constructor. */
e5e809f4 8675 {
3e4093b6
RS
8676 /* For a record, keep track of end position of last field. */
8677 if (DECL_SIZE (constructor_fields))
c22cacf3 8678 constructor_bit_index
db3927fb
AH
8679 = size_binop_loc (input_location, PLUS_EXPR,
8680 bit_position (constructor_fields),
8681 DECL_SIZE (constructor_fields));
3e4093b6
RS
8682
8683 /* If the current field was the first one not yet written out,
8684 it isn't now, so update. */
8685 if (constructor_unfilled_fields == constructor_fields)
8686 {
910ad8de 8687 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8688 /* Skip any nameless bit fields. */
8689 while (constructor_unfilled_fields != 0
8690 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8691 && DECL_NAME (constructor_unfilled_fields) == 0)
8692 constructor_unfilled_fields =
910ad8de 8693 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8694 }
e5e809f4 8695 }
3e4093b6 8696
910ad8de 8697 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8698 /* Skip any nameless bit fields at the beginning. */
8699 while (constructor_fields != 0
8700 && DECL_C_BIT_FIELD (constructor_fields)
8701 && DECL_NAME (constructor_fields) == 0)
910ad8de 8702 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8703 }
3e4093b6 8704 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8705 {
3e4093b6
RS
8706 tree fieldtype;
8707 enum tree_code fieldcode;
e5e809f4 8708
3e4093b6
RS
8709 if (constructor_fields == 0)
8710 {
509c9d60
MLI
8711 pedwarn_init (input_location, 0,
8712 "excess elements in union initializer");
3e4093b6
RS
8713 break;
8714 }
e5e809f4 8715
3e4093b6
RS
8716 fieldtype = TREE_TYPE (constructor_fields);
8717 if (fieldtype != error_mark_node)
8718 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8719 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8720
3e4093b6
RS
8721 /* Warn that traditional C rejects initialization of unions.
8722 We skip the warning if the value is zero. This is done
8723 under the assumption that the zero initializer in user
8724 code appears conditioned on e.g. __STDC__ to avoid
8725 "missing initializer" warnings and relies on default
8726 initialization to zero in the traditional C case.
8727 We also skip the warning if the initializer is designated,
8728 again on the assumption that this must be conditional on
8729 __STDC__ anyway (and we've already complained about the
8730 member-designator already). */
8400e75e 8731 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8732 && !(value.value && (integer_zerop (value.value)
8733 || real_zerop (value.value))))
3176a0c2
DD
8734 warning (OPT_Wtraditional, "traditional C rejects initialization "
8735 "of unions");
e5e809f4 8736
3e4093b6 8737 /* Accept a string constant to initialize a subarray. */
916c5919 8738 if (value.value != 0
3e4093b6 8739 && fieldcode == ARRAY_TYPE
197463ae 8740 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8741 && string_flag)
916c5919 8742 value.value = orig_value;
3e4093b6
RS
8743 /* Otherwise, if we have come to a subaggregate,
8744 and we don't have an element of its type, push into it. */
0953878d 8745 else if (value.value != 0
916c5919
JM
8746 && value.value != error_mark_node
8747 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8748 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8749 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8750 {
a1e3b3d9 8751 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8752 continue;
8753 }
e5e809f4 8754
916c5919 8755 if (value.value)
3e4093b6
RS
8756 {
8757 push_member_name (constructor_fields);
bbbbb16a
ILT
8758 output_init_element (value.value, value.original_type,
8759 strict_string, fieldtype,
a1e3b3d9
LB
8760 constructor_fields, 1, implicit,
8761 braced_init_obstack);
3e4093b6 8762 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8763 }
8764 else
3e4093b6
RS
8765 /* Do the bookkeeping for an element that was
8766 directly output as a constructor. */
e5e809f4 8767 {
3e4093b6 8768 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8769 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8770 }
e5e809f4 8771
3e4093b6
RS
8772 constructor_fields = 0;
8773 }
8774 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8775 {
8776 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8777 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8778
3e4093b6 8779 /* Accept a string constant to initialize a subarray. */
916c5919 8780 if (value.value != 0
3e4093b6 8781 && eltcode == ARRAY_TYPE
197463ae 8782 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8783 && string_flag)
916c5919 8784 value.value = orig_value;
3e4093b6
RS
8785 /* Otherwise, if we have come to a subaggregate,
8786 and we don't have an element of its type, push into it. */
0953878d 8787 else if (value.value != 0
916c5919
JM
8788 && value.value != error_mark_node
8789 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 8790 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 8791 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 8792 {
a1e3b3d9 8793 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8794 continue;
8795 }
8b6a5902 8796
3e4093b6
RS
8797 if (constructor_max_index != 0
8798 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8799 || integer_all_onesp (constructor_max_index)))
8800 {
509c9d60
MLI
8801 pedwarn_init (input_location, 0,
8802 "excess elements in array initializer");
3e4093b6
RS
8803 break;
8804 }
8b6a5902 8805
3e4093b6 8806 /* Now output the actual element. */
916c5919 8807 if (value.value)
3e4093b6 8808 {
ae7e9ddd 8809 push_array_bounds (tree_to_uhwi (constructor_index));
bbbbb16a
ILT
8810 output_init_element (value.value, value.original_type,
8811 strict_string, elttype,
a1e3b3d9
LB
8812 constructor_index, 1, implicit,
8813 braced_init_obstack);
3e4093b6
RS
8814 RESTORE_SPELLING_DEPTH (constructor_depth);
8815 }
2f6e4e97 8816
3e4093b6 8817 constructor_index
db3927fb
AH
8818 = size_binop_loc (input_location, PLUS_EXPR,
8819 constructor_index, bitsize_one_node);
8b6a5902 8820
916c5919 8821 if (!value.value)
3e4093b6
RS
8822 /* If we are doing the bookkeeping for an element that was
8823 directly output as a constructor, we must update
8824 constructor_unfilled_index. */
8825 constructor_unfilled_index = constructor_index;
8826 }
8827 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8828 {
8829 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 8830
c22cacf3
MS
8831 /* Do a basic check of initializer size. Note that vectors
8832 always have a fixed size derived from their type. */
3e4093b6
RS
8833 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8834 {
509c9d60
MLI
8835 pedwarn_init (input_location, 0,
8836 "excess elements in vector initializer");
3e4093b6
RS
8837 break;
8838 }
8b6a5902 8839
3e4093b6 8840 /* Now output the actual element. */
916c5919 8841 if (value.value)
53650abe
AP
8842 {
8843 if (TREE_CODE (value.value) == VECTOR_CST)
8844 elttype = TYPE_MAIN_VARIANT (constructor_type);
8845 output_init_element (value.value, value.original_type,
8846 strict_string, elttype,
a1e3b3d9
LB
8847 constructor_index, 1, implicit,
8848 braced_init_obstack);
53650abe 8849 }
8b6a5902 8850
3e4093b6 8851 constructor_index
db3927fb
AH
8852 = size_binop_loc (input_location,
8853 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 8854
916c5919 8855 if (!value.value)
3e4093b6
RS
8856 /* If we are doing the bookkeeping for an element that was
8857 directly output as a constructor, we must update
8858 constructor_unfilled_index. */
8859 constructor_unfilled_index = constructor_index;
8860 }
8b6a5902 8861
3e4093b6
RS
8862 /* Handle the sole element allowed in a braced initializer
8863 for a scalar variable. */
b4519d39
SB
8864 else if (constructor_type != error_mark_node
8865 && constructor_fields == 0)
8b6a5902 8866 {
509c9d60
MLI
8867 pedwarn_init (input_location, 0,
8868 "excess elements in scalar initializer");
3e4093b6 8869 break;
8b6a5902
JJ
8870 }
8871 else
8872 {
916c5919 8873 if (value.value)
bbbbb16a
ILT
8874 output_init_element (value.value, value.original_type,
8875 strict_string, constructor_type,
a1e3b3d9
LB
8876 NULL_TREE, 1, implicit,
8877 braced_init_obstack);
3e4093b6 8878 constructor_fields = 0;
8b6a5902
JJ
8879 }
8880
3e4093b6
RS
8881 /* Handle range initializers either at this level or anywhere higher
8882 in the designator stack. */
8883 if (constructor_range_stack)
8b6a5902 8884 {
3e4093b6
RS
8885 struct constructor_range_stack *p, *range_stack;
8886 int finish = 0;
8887
8888 range_stack = constructor_range_stack;
8889 constructor_range_stack = 0;
8890 while (constructor_stack != range_stack->stack)
8b6a5902 8891 {
366de0ce 8892 gcc_assert (constructor_stack->implicit);
a1e3b3d9
LB
8893 process_init_element (pop_init_level (1,
8894 braced_init_obstack),
8895 true, braced_init_obstack);
8b6a5902 8896 }
3e4093b6
RS
8897 for (p = range_stack;
8898 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8899 p = p->prev)
8b6a5902 8900 {
366de0ce 8901 gcc_assert (constructor_stack->implicit);
a1e3b3d9
LB
8902 process_init_element (pop_init_level (1, braced_init_obstack),
8903 true, braced_init_obstack);
8b6a5902 8904 }
3e4093b6 8905
db3927fb
AH
8906 p->index = size_binop_loc (input_location,
8907 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
8908 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8909 finish = 1;
8910
8911 while (1)
8912 {
8913 constructor_index = p->index;
8914 constructor_fields = p->fields;
8915 if (finish && p->range_end && p->index == p->range_start)
8916 {
8917 finish = 0;
8918 p->prev = 0;
8919 }
8920 p = p->next;
8921 if (!p)
8922 break;
a1e3b3d9 8923 push_init_level (2, braced_init_obstack);
3e4093b6
RS
8924 p->stack = constructor_stack;
8925 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8926 p->index = p->range_start;
8927 }
8928
8929 if (!finish)
8930 constructor_range_stack = range_stack;
8931 continue;
8b6a5902
JJ
8932 }
8933
3e4093b6 8934 break;
8b6a5902
JJ
8935 }
8936
3e4093b6
RS
8937 constructor_range_stack = 0;
8938}
8939\f
9f0e2d86
ZW
8940/* Build a complete asm-statement, whose components are a CV_QUALIFIER
8941 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 8942 an ASM_EXPR node). */
3e4093b6 8943tree
9f0e2d86 8944build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 8945{
6de9cd9a
DN
8946 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8947 ASM_VOLATILE_P (args) = 1;
9f0e2d86 8948 return add_stmt (args);
8b6a5902
JJ
8949}
8950
9f0e2d86
ZW
8951/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8952 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8953 SIMPLE indicates whether there was anything at all after the
8954 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 8955 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 8956tree
c2255bc4 8957build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 8958 tree clobbers, tree labels, bool simple)
e5e809f4 8959{
3e4093b6 8960 tree tail;
9f0e2d86 8961 tree args;
6de9cd9a
DN
8962 int i;
8963 const char *constraint;
74f0c611 8964 const char **oconstraints;
6de9cd9a 8965 bool allows_mem, allows_reg, is_inout;
74f0c611 8966 int ninputs, noutputs;
6de9cd9a
DN
8967
8968 ninputs = list_length (inputs);
8969 noutputs = list_length (outputs);
74f0c611
RH
8970 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8971
1c384bf1 8972 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 8973
6de9cd9a
DN
8974 /* Remove output conversions that change the type but not the mode. */
8975 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 8976 {
3e4093b6 8977 tree output = TREE_VALUE (tail);
74f0c611 8978
eadd3d0d
JJ
8979 output = c_fully_fold (output, false, NULL);
8980
74f0c611
RH
8981 /* ??? Really, this should not be here. Users should be using a
8982 proper lvalue, dammit. But there's a long history of using casts
8983 in the output operands. In cases like longlong.h, this becomes a
8984 primitive form of typechecking -- if the cast can be removed, then
8985 the output operand had a type of the proper width; otherwise we'll
8986 get an error. Gross, but ... */
3e4093b6 8987 STRIP_NOPS (output);
74f0c611 8988
7bd11157 8989 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 8990 output = error_mark_node;
8b6a5902 8991
5544530a
PB
8992 if (output != error_mark_node
8993 && (TREE_READONLY (output)
8994 || TYPE_READONLY (TREE_TYPE (output))
8995 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8996 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8997 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 8998 readonly_error (loc, output, lv_asm);
5544530a 8999
6de9cd9a 9000 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9001 oconstraints[i] = constraint;
9002
9003 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9004 &allows_mem, &allows_reg, &is_inout))
9005 {
9006 /* If the operand is going to end up in memory,
9007 mark it addressable. */
9008 if (!allows_reg && !c_mark_addressable (output))
9009 output = error_mark_node;
bae5cddf
JJ
9010 if (!(!allows_reg && allows_mem)
9011 && output != error_mark_node
9012 && VOID_TYPE_P (TREE_TYPE (output)))
9013 {
9014 error_at (loc, "invalid use of void expression");
9015 output = error_mark_node;
9016 }
74f0c611
RH
9017 }
9018 else
c22cacf3 9019 output = error_mark_node;
3e4093b6 9020
74f0c611 9021 TREE_VALUE (tail) = output;
8b6a5902 9022 }
3e4093b6 9023
74f0c611
RH
9024 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9025 {
9026 tree input;
9027
9028 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9029 input = TREE_VALUE (tail);
9030
74f0c611
RH
9031 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9032 oconstraints, &allows_mem, &allows_reg))
9033 {
9034 /* If the operand is going to end up in memory,
9035 mark it addressable. */
b4c33883
AP
9036 if (!allows_reg && allows_mem)
9037 {
eadd3d0d
JJ
9038 input = c_fully_fold (input, false, NULL);
9039
b4c33883
AP
9040 /* Strip the nops as we allow this case. FIXME, this really
9041 should be rejected or made deprecated. */
9042 STRIP_NOPS (input);
9043 if (!c_mark_addressable (input))
9044 input = error_mark_node;
bae5cddf 9045 }
eadd3d0d 9046 else
bae5cddf 9047 {
eadd3d0d
JJ
9048 struct c_expr expr;
9049 memset (&expr, 0, sizeof (expr));
9050 expr.value = input;
267bac10 9051 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9052 input = c_fully_fold (expr.value, false, NULL);
9053
9054 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9055 {
9056 error_at (loc, "invalid use of void expression");
9057 input = error_mark_node;
9058 }
bae5cddf 9059 }
74f0c611
RH
9060 }
9061 else
9062 input = error_mark_node;
9063
9064 TREE_VALUE (tail) = input;
9065 }
3e4093b6 9066
1c384bf1
RH
9067 /* ASMs with labels cannot have outputs. This should have been
9068 enforced by the parser. */
9069 gcc_assert (outputs == NULL || labels == NULL);
9070
9071 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9072
5544530a
PB
9073 /* asm statements without outputs, including simple ones, are treated
9074 as volatile. */
9075 ASM_INPUT_P (args) = simple;
9076 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9077
9f0e2d86 9078 return args;
e5e809f4 9079}
3e4093b6 9080\f
c2255bc4
AH
9081/* Generate a goto statement to LABEL. LOC is the location of the
9082 GOTO. */
506e2710
RH
9083
9084tree
c2255bc4 9085c_finish_goto_label (location_t loc, tree label)
506e2710 9086{
e1b7793c 9087 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9088 if (!decl)
9089 return NULL_TREE;
506e2710 9090 TREE_USED (decl) = 1;
c2255bc4
AH
9091 {
9092 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9093 SET_EXPR_LOCATION (t, loc);
9094 return add_stmt (t);
9095 }
506e2710
RH
9096}
9097
c2255bc4
AH
9098/* Generate a computed goto statement to EXPR. LOC is the location of
9099 the GOTO. */
506e2710
RH
9100
9101tree
c2255bc4 9102c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9103{
c2255bc4 9104 tree t;
c1771a20 9105 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9106 expr = c_fully_fold (expr, false, NULL);
506e2710 9107 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9108 t = build1 (GOTO_EXPR, void_type_node, expr);
9109 SET_EXPR_LOCATION (t, loc);
9110 return add_stmt (t);
506e2710
RH
9111}
9112
5088b058 9113/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4
AH
9114 to return, or a null pointer for `return;' with no value. LOC is
9115 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
9116 is the original type of RETVAL. */
de520661 9117
506e2710 9118tree
c2255bc4 9119c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9120{
0c9b182b
JJ
9121 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9122 bool no_warning = false;
928c19bb 9123 bool npc = false;
36536d79 9124 size_t rank = 0;
3e4093b6
RS
9125
9126 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9127 warning_at (loc, 0,
9128 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9129
b72271b9 9130 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9131 {
9132 /* Array notations are allowed in a return statement if it is inside a
9133 built-in array notation reduction function. */
9134 if (!find_rank (loc, retval, retval, false, &rank))
9135 return error_mark_node;
9136 if (rank >= 1)
9137 {
9138 error_at (loc, "array notation expression cannot be used as a "
9139 "return value");
9140 return error_mark_node;
9141 }
9142 }
3af9c5e9 9143 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9144 {
9145 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9146 "allowed");
9147 return error_mark_node;
9148 }
928c19bb
JM
9149 if (retval)
9150 {
8ce94e44 9151 tree semantic_type = NULL_TREE;
928c19bb 9152 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9153 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9154 {
9155 semantic_type = TREE_TYPE (retval);
9156 retval = TREE_OPERAND (retval, 0);
9157 }
928c19bb 9158 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9159 if (semantic_type)
9160 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9161 }
9162
3e4093b6 9163 if (!retval)
de520661 9164 {
3e4093b6
RS
9165 current_function_returns_null = 1;
9166 if ((warn_return_type || flag_isoc99)
9167 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9168 {
b8698a0f 9169 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
fcf73884 9170 "%<return%> with no value, in "
0c9b182b
JJ
9171 "function returning non-void");
9172 no_warning = true;
9173 }
400fbf9f 9174 }
3e4093b6 9175 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9176 {
3e4093b6 9177 current_function_returns_null = 1;
2397c575 9178 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9179 pedwarn (loc, 0,
509c9d60 9180 "%<return%> with a value, in function returning void");
b8698a0f 9181 else
c1771a20 9182 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9183 "%<return%> with expression, in function returning void");
de520661 9184 }
3e4093b6 9185 else
de520661 9186 {
68fca595
MP
9187 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9188 retval, origtype, ic_return,
c2255bc4 9189 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9190 tree res = DECL_RESULT (current_function_decl);
9191 tree inner;
9feb29df 9192 bool save;
3e4093b6
RS
9193
9194 current_function_returns_value = 1;
9195 if (t == error_mark_node)
506e2710 9196 return NULL_TREE;
3e4093b6 9197
9feb29df
JJ
9198 save = in_late_binary_op;
9199 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9200 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9201 in_late_binary_op = true;
3e4093b6 9202 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9203 in_late_binary_op = save;
3e4093b6
RS
9204
9205 /* Strip any conversions, additions, and subtractions, and see if
9206 we are returning the address of a local variable. Warn if so. */
9207 while (1)
8b6a5902 9208 {
3e4093b6 9209 switch (TREE_CODE (inner))
8b6a5902 9210 {
849421a3
JJ
9211 CASE_CONVERT:
9212 case NON_LVALUE_EXPR:
3e4093b6 9213 case PLUS_EXPR:
849421a3 9214 case POINTER_PLUS_EXPR:
3e4093b6
RS
9215 inner = TREE_OPERAND (inner, 0);
9216 continue;
9217
9218 case MINUS_EXPR:
9219 /* If the second operand of the MINUS_EXPR has a pointer
9220 type (or is converted from it), this may be valid, so
9221 don't give a warning. */
9222 {
9223 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9224
3f75a254 9225 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9226 && (CONVERT_EXPR_P (op1)
9227 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9228 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9229
3e4093b6
RS
9230 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9231 break;
8b6a5902 9232
3e4093b6
RS
9233 inner = TREE_OPERAND (inner, 0);
9234 continue;
9235 }
400fbf9f 9236
3e4093b6
RS
9237 case ADDR_EXPR:
9238 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9239
6615c446 9240 while (REFERENCE_CLASS_P (inner)
c22cacf3 9241 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 9242 inner = TREE_OPERAND (inner, 0);
8b6a5902 9243
a2f1f4c3 9244 if (DECL_P (inner)
3f75a254
JM
9245 && !DECL_EXTERNAL (inner)
9246 && !TREE_STATIC (inner)
3e4093b6 9247 && DECL_CONTEXT (inner) == current_function_decl)
c2255bc4 9248 warning_at (loc,
880661a4
JW
9249 OPT_Wreturn_local_addr, "function returns address "
9250 "of local variable");
3e4093b6 9251 break;
8b6a5902 9252
3e4093b6
RS
9253 default:
9254 break;
9255 }
de520661 9256
3e4093b6
RS
9257 break;
9258 }
9259
53fb4de3 9260 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9261 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9262
9263 if (warn_sequence_point)
9264 verify_sequence_points (retval);
de520661 9265 }
8b6a5902 9266
c2255bc4 9267 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9268 TREE_NO_WARNING (ret_stmt) |= no_warning;
9269 return add_stmt (ret_stmt);
de520661 9270}
3e4093b6
RS
9271\f
9272struct c_switch {
604f5adf
ILT
9273 /* The SWITCH_EXPR being built. */
9274 tree switch_expr;
a6c0a76c 9275
89dbed81 9276 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9277 default conversion is applied. */
9278 tree orig_type;
9279
3e4093b6
RS
9280 /* A splay-tree mapping the low element of a case range to the high
9281 element, or NULL_TREE if there is no high element. Used to
9282 determine whether or not a new case label duplicates an old case
9283 label. We need a tree, rather than simply a hash table, because
9284 of the GNU case range extension. */
9285 splay_tree cases;
a6c0a76c 9286
e1b7793c
ILT
9287 /* The bindings at the point of the switch. This is used for
9288 warnings crossing decls when branching to a case label. */
9289 struct c_spot_bindings *bindings;
187230a7 9290
3e4093b6
RS
9291 /* The next node on the stack. */
9292 struct c_switch *next;
9293};
400fbf9f 9294
3e4093b6
RS
9295/* A stack of the currently active switch statements. The innermost
9296 switch statement is on the top of the stack. There is no need to
9297 mark the stack for garbage collection because it is only active
9298 during the processing of the body of a function, and we never
9299 collect at that point. */
de520661 9300
506e2710 9301struct c_switch *c_switch_stack;
de520661 9302
3e4093b6 9303/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4
AH
9304 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9305 SWITCH_COND_LOC is the location of the switch's condition. */
de520661 9306
3e4093b6 9307tree
c2255bc4
AH
9308c_start_case (location_t switch_loc,
9309 location_t switch_cond_loc,
9310 tree exp)
de520661 9311{
c58e8676 9312 tree orig_type = error_mark_node;
3e4093b6 9313 struct c_switch *cs;
2f6e4e97 9314
3e4093b6 9315 if (exp != error_mark_node)
de520661 9316 {
3e4093b6
RS
9317 orig_type = TREE_TYPE (exp);
9318
c58e8676 9319 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9320 {
c58e8676
VR
9321 if (orig_type != error_mark_node)
9322 {
c2255bc4 9323 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9324 orig_type = error_mark_node;
9325 }
3e4093b6 9326 exp = integer_zero_node;
de520661 9327 }
3e4093b6 9328 else
de520661 9329 {
c58e8676 9330 tree type = TYPE_MAIN_VARIANT (orig_type);
8b6a5902 9331
8400e75e 9332 if (!in_system_header_at (input_location)
3e4093b6
RS
9333 && (type == long_integer_type_node
9334 || type == long_unsigned_type_node))
c2255bc4
AH
9335 warning_at (switch_cond_loc,
9336 OPT_Wtraditional, "%<long%> switch expression not "
9337 "converted to %<int%> in ISO C");
8b6a5902 9338
928c19bb 9339 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9340 exp = default_conversion (exp);
ca085fd7
MLI
9341
9342 if (warn_sequence_point)
9343 verify_sequence_points (exp);
3e4093b6
RS
9344 }
9345 }
9346
604f5adf 9347 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9348 cs = XNEW (struct c_switch);
604f5adf 9349 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9350 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9351 cs->orig_type = orig_type;
3e4093b6 9352 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9353 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9354 cs->next = c_switch_stack;
9355 c_switch_stack = cs;
3e4093b6 9356
604f5adf 9357 return add_stmt (cs->switch_expr);
3e4093b6
RS
9358}
9359
c2255bc4 9360/* Process a case label at location LOC. */
3e4093b6
RS
9361
9362tree
c2255bc4 9363do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9364{
9365 tree label = NULL_TREE;
9366
17cede2e
JM
9367 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9368 {
9369 low_value = c_fully_fold (low_value, false, NULL);
9370 if (TREE_CODE (low_value) == INTEGER_CST)
c1771a20 9371 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9372 "case label is not an integer constant expression");
9373 }
9374
9375 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9376 {
9377 high_value = c_fully_fold (high_value, false, NULL);
9378 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9379 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9380 "case label is not an integer constant expression");
9381 }
9382
e1b7793c 9383 if (c_switch_stack == NULL)
187230a7
JM
9384 {
9385 if (low_value)
e1b7793c 9386 error_at (loc, "case label not within a switch statement");
187230a7 9387 else
e1b7793c
ILT
9388 error_at (loc, "%<default%> label not within a switch statement");
9389 return NULL_TREE;
187230a7 9390 }
de520661 9391
e1b7793c
ILT
9392 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9393 EXPR_LOCATION (c_switch_stack->switch_expr),
9394 loc))
9395 return NULL_TREE;
9396
9397 label = c_add_case_label (loc, c_switch_stack->cases,
9398 SWITCH_COND (c_switch_stack->switch_expr),
9399 c_switch_stack->orig_type,
9400 low_value, high_value);
9401 if (label == error_mark_node)
9402 label = NULL_TREE;
3e4093b6
RS
9403 return label;
9404}
de520661 9405
3e4093b6 9406/* Finish the switch statement. */
de520661 9407
3e4093b6 9408void
325c3691 9409c_finish_case (tree body)
3e4093b6 9410{
506e2710 9411 struct c_switch *cs = c_switch_stack;
fbc315db 9412 location_t switch_location;
3e4093b6 9413
604f5adf 9414 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9415
6de9cd9a 9416 /* Emit warnings as needed. */
c2255bc4 9417 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db
ILT
9418 c_do_switch_warnings (cs->cases, switch_location,
9419 TREE_TYPE (cs->switch_expr),
9420 SWITCH_COND (cs->switch_expr));
6de9cd9a 9421
3e4093b6 9422 /* Pop the stack. */
506e2710 9423 c_switch_stack = cs->next;
3e4093b6 9424 splay_tree_delete (cs->cases);
e1b7793c 9425 c_release_switch_bindings (cs->bindings);
5d038c4c 9426 XDELETE (cs);
de520661 9427}
325c3691 9428\f
506e2710
RH
9429/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9430 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9431 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9432 statement, and was not surrounded with parenthesis. */
325c3691 9433
9e51cf9d 9434void
506e2710
RH
9435c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9436 tree else_block, bool nested_if)
325c3691 9437{
506e2710 9438 tree stmt;
325c3691 9439
25c22937
BI
9440 /* If the condition has array notations, then the rank of the then_block and
9441 else_block must be either 0 or be equal to the rank of the condition. If
9442 the condition does not have array notations then break them up as it is
9443 broken up in a normal expression. */
b72271b9 9444 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9445 {
9446 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9447 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9448 return;
9449 if (then_block
9450 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9451 return;
9452 if (else_block
9453 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9454 return;
9455 if (cond_rank != then_rank && then_rank != 0)
9456 {
9457 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9458 " and the then-block");
9459 return;
9460 }
9461 else if (cond_rank != else_rank && else_rank != 0)
9462 {
9463 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9464 " and the else-block");
9465 return;
9466 }
9467 }
506e2710
RH
9468 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9469 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9470 {
506e2710 9471 tree inner_if = then_block;
16865eaa 9472
61ada8ae 9473 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9474 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9475 it might not be exactly THEN_BLOCK, but should be the last
9476 non-container statement within. */
9477 while (1)
9478 switch (TREE_CODE (inner_if))
9479 {
9480 case COND_EXPR:
9481 goto found;
9482 case BIND_EXPR:
9483 inner_if = BIND_EXPR_BODY (inner_if);
9484 break;
9485 case STATEMENT_LIST:
9486 inner_if = expr_last (then_block);
9487 break;
9488 case TRY_FINALLY_EXPR:
9489 case TRY_CATCH_EXPR:
9490 inner_if = TREE_OPERAND (inner_if, 0);
9491 break;
9492 default:
366de0ce 9493 gcc_unreachable ();
506e2710
RH
9494 }
9495 found:
16865eaa 9496
506e2710 9497 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9498 warning_at (if_locus, OPT_Wparentheses,
9499 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9500 }
16865eaa 9501
2214de30 9502 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9503 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9504 add_stmt (stmt);
325c3691
RH
9505}
9506
506e2710
RH
9507/* Emit a general-purpose loop construct. START_LOCUS is the location of
9508 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9509 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9510 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9511 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9512
9513void
506e2710
RH
9514c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9515 tree blab, tree clab, bool cond_is_first)
325c3691 9516{
506e2710
RH
9517 tree entry = NULL, exit = NULL, t;
9518
b72271b9 9519 if (flag_cilkplus && contains_array_notation_expr (cond))
36536d79
BI
9520 {
9521 error_at (start_locus, "array notation expression cannot be used in a "
9522 "loop%'s condition");
9523 return;
9524 }
9525
28af952a
RS
9526 /* If the condition is zero don't generate a loop construct. */
9527 if (cond && integer_zerop (cond))
9528 {
9529 if (cond_is_first)
9530 {
9531 t = build_and_jump (&blab);
9532 SET_EXPR_LOCATION (t, start_locus);
9533 add_stmt (t);
9534 }
9535 }
9536 else
506e2710
RH
9537 {
9538 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9539
506e2710 9540 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9541 out of the loop, or to the top of it. If there's no exit condition,
9542 then we just build a jump back to the top. */
506e2710 9543 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9544
28af952a 9545 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9546 {
9547 /* Canonicalize the loop condition to the end. This means
9548 generating a branch to the loop condition. Reuse the
9549 continue label, if possible. */
9550 if (cond_is_first)
9551 {
9552 if (incr || !clab)
9553 {
9554 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9555 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9556 }
9557 else
9558 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9559 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9560 add_stmt (t);
9561 }
9562
506e2710 9563 t = build_and_jump (&blab);
506e2710 9564 if (cond_is_first)
db3927fb
AH
9565 exit = fold_build3_loc (start_locus,
9566 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9567 else
db3927fb
AH
9568 exit = fold_build3_loc (input_location,
9569 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9570 }
9571
506e2710
RH
9572 add_stmt (top);
9573 }
c22cacf3 9574
506e2710
RH
9575 if (body)
9576 add_stmt (body);
9577 if (clab)
9578 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9579 if (incr)
9580 add_stmt (incr);
9581 if (entry)
9582 add_stmt (entry);
9583 if (exit)
9584 add_stmt (exit);
9585 if (blab)
9586 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9587}
325c3691
RH
9588
9589tree
c2255bc4 9590c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9591{
089efaa4 9592 bool skip;
506e2710 9593 tree label = *label_p;
325c3691 9594
089efaa4
ILT
9595 /* In switch statements break is sometimes stylistically used after
9596 a return statement. This can lead to spurious warnings about
9597 control reaching the end of a non-void function when it is
9598 inlined. Note that we are calling block_may_fallthru with
9599 language specific tree nodes; this works because
9600 block_may_fallthru returns true when given something it does not
9601 understand. */
9602 skip = !block_may_fallthru (cur_stmt_list);
9603
506e2710 9604 if (!label)
089efaa4
ILT
9605 {
9606 if (!skip)
c2255bc4 9607 *label_p = label = create_artificial_label (loc);
089efaa4 9608 }
953ff289
DN
9609 else if (TREE_CODE (label) == LABEL_DECL)
9610 ;
9611 else switch (TREE_INT_CST_LOW (label))
506e2710 9612 {
953ff289 9613 case 0:
506e2710 9614 if (is_break)
c2255bc4 9615 error_at (loc, "break statement not within loop or switch");
506e2710 9616 else
c2255bc4 9617 error_at (loc, "continue statement not within a loop");
506e2710 9618 return NULL_TREE;
953ff289
DN
9619
9620 case 1:
9621 gcc_assert (is_break);
c2255bc4 9622 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9623 return NULL_TREE;
9624
c02065fc
AH
9625 case 2:
9626 if (is_break)
9627 error ("break statement within %<#pragma simd%> loop body");
9628 else
9629 error ("continue statement within %<#pragma simd%> loop body");
9630 return NULL_TREE;
9631
953ff289
DN
9632 default:
9633 gcc_unreachable ();
506e2710 9634 }
325c3691 9635
089efaa4
ILT
9636 if (skip)
9637 return NULL_TREE;
9638
2e28e797
JH
9639 if (!is_break)
9640 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9641
53fb4de3 9642 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9643}
9644
506e2710 9645/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9646
9647static void
c2255bc4 9648emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9649{
e6b5a630
RH
9650 if (expr == error_mark_node)
9651 ;
9652 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9653 {
9654 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9655 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9656 }
789eadcd
MP
9657 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9658 {
9659 tree r = expr;
9660 location_t cloc = loc;
9661 while (TREE_CODE (r) == COMPOUND_EXPR)
9662 {
9663 if (EXPR_HAS_LOCATION (r))
9664 cloc = EXPR_LOCATION (r);
9665 r = TREE_OPERAND (r, 1);
9666 }
9667 if (!TREE_SIDE_EFFECTS (r)
9668 && !VOID_TYPE_P (TREE_TYPE (r))
9669 && !CONVERT_EXPR_P (r)
cc28fc7f 9670 && !TREE_NO_WARNING (r)
789eadcd
MP
9671 && !TREE_NO_WARNING (expr))
9672 warning_at (cloc, OPT_Wunused_value,
9673 "right-hand operand of comma expression has no effect");
9674 }
27f33b15 9675 else
c2255bc4 9676 warn_if_unused_value (expr, loc);
3a5b9284
RH
9677}
9678
506e2710 9679/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9680 diagnostics, but do not call ADD_STMT. LOC is the location of the
9681 statement. */
3a5b9284 9682
506e2710 9683tree
c2255bc4 9684c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9685{
056928b2
JJ
9686 tree exprv;
9687
3a5b9284 9688 if (!expr)
506e2710 9689 return NULL_TREE;
3a5b9284 9690
928c19bb
JM
9691 expr = c_fully_fold (expr, false, NULL);
9692
3a5b9284
RH
9693 if (warn_sequence_point)
9694 verify_sequence_points (expr);
9695
9696 if (TREE_TYPE (expr) != error_mark_node
9697 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9698 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9699 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9700
9701 /* If we're not processing a statement expression, warn about unused values.
9702 Warnings for statement expressions will be emitted later, once we figure
9703 out which is the result. */
9704 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9705 && warn_unused_value)
c2255bc4 9706 emit_side_effect_warnings (loc, expr);
3a5b9284 9707
056928b2
JJ
9708 exprv = expr;
9709 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9710 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9711 while (CONVERT_EXPR_P (exprv))
9712 exprv = TREE_OPERAND (exprv, 0);
9713 if (DECL_P (exprv)
9714 || handled_component_p (exprv)
9715 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9716 mark_exp_read (exprv);
fa8351f8 9717
3a5b9284
RH
9718 /* If the expression is not of a type to which we cannot assign a line
9719 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9720 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9721 {
9722 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9723 SET_EXPR_LOCATION (expr, loc);
9724 }
506e2710
RH
9725
9726 return expr;
9727}
9728
c2255bc4
AH
9729/* Emit an expression as a statement. LOC is the location of the
9730 expression. */
506e2710
RH
9731
9732tree
c2255bc4 9733c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9734{
9735 if (expr)
c2255bc4 9736 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9737 else
9738 return NULL;
3a5b9284
RH
9739}
9740
9741/* Do the opposite and emit a statement as an expression. To begin,
9742 create a new binding level and return it. */
325c3691
RH
9743
9744tree
9745c_begin_stmt_expr (void)
9746{
9747 tree ret;
9748
9749 /* We must force a BLOCK for this level so that, if it is not expanded
9750 later, there is a way to turn off the entire subtree of blocks that
9751 are contained in it. */
9752 keep_next_level ();
9753 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
9754
9755 c_bindings_start_stmt_expr (c_switch_stack == NULL
9756 ? NULL
9757 : c_switch_stack->bindings);
325c3691
RH
9758
9759 /* Mark the current statement list as belonging to a statement list. */
9760 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9761
9762 return ret;
9763}
9764
c2255bc4
AH
9765/* LOC is the location of the compound statement to which this body
9766 belongs. */
9767
325c3691 9768tree
c2255bc4 9769c_finish_stmt_expr (location_t loc, tree body)
325c3691 9770{
3a5b9284 9771 tree last, type, tmp, val;
325c3691
RH
9772 tree *last_p;
9773
c2255bc4 9774 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
9775
9776 c_bindings_end_stmt_expr (c_switch_stack == NULL
9777 ? NULL
9778 : c_switch_stack->bindings);
325c3691 9779
3a5b9284
RH
9780 /* Locate the last statement in BODY. See c_end_compound_stmt
9781 about always returning a BIND_EXPR. */
9782 last_p = &BIND_EXPR_BODY (body);
9783 last = BIND_EXPR_BODY (body);
9784
9785 continue_searching:
325c3691
RH
9786 if (TREE_CODE (last) == STATEMENT_LIST)
9787 {
3a5b9284
RH
9788 tree_stmt_iterator i;
9789
9790 /* This can happen with degenerate cases like ({ }). No value. */
9791 if (!TREE_SIDE_EFFECTS (last))
9792 return body;
9793
9794 /* If we're supposed to generate side effects warnings, process
9795 all of the statements except the last. */
27f33b15 9796 if (warn_unused_value)
325c3691 9797 {
3a5b9284 9798 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
9799 {
9800 location_t tloc;
9801 tree t = tsi_stmt (i);
9802
9803 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9804 emit_side_effect_warnings (tloc, t);
9805 }
325c3691
RH
9806 }
9807 else
3a5b9284
RH
9808 i = tsi_last (last);
9809 last_p = tsi_stmt_ptr (i);
9810 last = *last_p;
325c3691
RH
9811 }
9812
3a5b9284
RH
9813 /* If the end of the list is exception related, then the list was split
9814 by a call to push_cleanup. Continue searching. */
9815 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9816 || TREE_CODE (last) == TRY_CATCH_EXPR)
9817 {
9818 last_p = &TREE_OPERAND (last, 0);
9819 last = *last_p;
9820 goto continue_searching;
9821 }
9822
26d8af35
JM
9823 if (last == error_mark_node)
9824 return last;
9825
3a5b9284
RH
9826 /* In the case that the BIND_EXPR is not necessary, return the
9827 expression out from inside it. */
26d8af35
JM
9828 if (last == BIND_EXPR_BODY (body)
9829 && BIND_EXPR_VARS (body) == NULL)
591baeb0 9830 {
928c19bb
JM
9831 /* Even if this looks constant, do not allow it in a constant
9832 expression. */
e5a94231 9833 last = c_wrap_maybe_const (last, true);
591baeb0
JM
9834 /* Do not warn if the return value of a statement expression is
9835 unused. */
928c19bb 9836 TREE_NO_WARNING (last) = 1;
591baeb0
JM
9837 return last;
9838 }
325c3691
RH
9839
9840 /* Extract the type of said expression. */
9841 type = TREE_TYPE (last);
325c3691 9842
3a5b9284
RH
9843 /* If we're not returning a value at all, then the BIND_EXPR that
9844 we already have is a fine expression to return. */
9845 if (!type || VOID_TYPE_P (type))
9846 return body;
9847
9848 /* Now that we've located the expression containing the value, it seems
9849 silly to make voidify_wrapper_expr repeat the process. Create a
9850 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9851 tmp = create_tmp_var_raw (type, NULL);
9852
9853 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9854 tree_expr_nonnegative_p giving up immediately. */
9855 val = last;
9856 if (TREE_CODE (val) == NOP_EXPR
9857 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9858 val = TREE_OPERAND (val, 0);
9859
53fb4de3 9860 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 9861 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 9862
c2255bc4
AH
9863 {
9864 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9865 SET_EXPR_LOCATION (t, loc);
9866 return t;
9867 }
325c3691
RH
9868}
9869\f
9870/* Begin and end compound statements. This is as simple as pushing
9871 and popping new statement lists from the tree. */
9872
9873tree
9874c_begin_compound_stmt (bool do_scope)
9875{
9876 tree stmt = push_stmt_list ();
9877 if (do_scope)
4dfa0342 9878 push_scope ();
325c3691
RH
9879 return stmt;
9880}
9881
c2255bc4
AH
9882/* End a compound statement. STMT is the statement. LOC is the
9883 location of the compound statement-- this is usually the location
9884 of the opening brace. */
9885
325c3691 9886tree
c2255bc4 9887c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
9888{
9889 tree block = NULL;
9890
9891 if (do_scope)
9892 {
9893 if (c_dialect_objc ())
9894 objc_clear_super_receiver ();
9895 block = pop_scope ();
9896 }
9897
9898 stmt = pop_stmt_list (stmt);
c2255bc4 9899 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
9900
9901 /* If this compound statement is nested immediately inside a statement
9902 expression, then force a BIND_EXPR to be created. Otherwise we'll
9903 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9904 STATEMENT_LISTs merge, and thus we can lose track of what statement
9905 was really last. */
38e01f9e 9906 if (building_stmt_list_p ()
325c3691
RH
9907 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9908 && TREE_CODE (stmt) != BIND_EXPR)
9909 {
53fb4de3 9910 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 9911 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 9912 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
9913 }
9914
9915 return stmt;
9916}
5a508662
RH
9917
9918/* Queue a cleanup. CLEANUP is an expression/statement to be executed
9919 when the current scope is exited. EH_ONLY is true when this is not
9920 meant to apply to normal control flow transfer. */
9921
9922void
c2255bc4 9923push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 9924{
3a5b9284
RH
9925 enum tree_code code;
9926 tree stmt, list;
9927 bool stmt_expr;
9928
9929 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 9930 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 9931 add_stmt (stmt);
3a5b9284
RH
9932 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9933 list = push_stmt_list ();
9934 TREE_OPERAND (stmt, 0) = list;
9935 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 9936}
325c3691 9937\f
3e4093b6
RS
9938/* Build a binary-operation expression without default conversions.
9939 CODE is the kind of expression to build.
ba47d38d 9940 LOCATION is the operator's location.
3e4093b6
RS
9941 This function differs from `build' in several ways:
9942 the data type of the result is computed and recorded in it,
9943 warnings are generated if arg data types are invalid,
9944 special handling for addition and subtraction of pointers is known,
9945 and some optimization is done (operations on narrow ints
9946 are done in the narrower type when that gives the same result).
9947 Constant folding is also done before the result is returned.
de520661 9948
3e4093b6
RS
9949 Note that the operands will never have enumeral types, or function
9950 or array types, because either they will have the default conversions
9951 performed or they have both just been converted to some other type in which
9952 the arithmetic is to be done. */
9953
9954tree
ba47d38d
AH
9955build_binary_op (location_t location, enum tree_code code,
9956 tree orig_op0, tree orig_op1, int convert_p)
de520661 9957{
8ce94e44
JM
9958 tree type0, type1, orig_type0, orig_type1;
9959 tree eptype;
3e4093b6
RS
9960 enum tree_code code0, code1;
9961 tree op0, op1;
c9f9eb5d 9962 tree ret = error_mark_node;
4de67c26 9963 const char *invalid_op_diag;
4d84fe7c 9964 bool op0_int_operands, op1_int_operands;
928c19bb 9965 bool int_const, int_const_or_overflow, int_operands;
b62acd60 9966
3e4093b6
RS
9967 /* Expression code to give to the expression when it is built.
9968 Normally this is CODE, which is what the caller asked for,
9969 but in some special cases we change it. */
9970 enum tree_code resultcode = code;
8b6a5902 9971
3e4093b6
RS
9972 /* Data type in which the computation is to be performed.
9973 In the simplest cases this is the common type of the arguments. */
9974 tree result_type = NULL;
9975
8ce94e44
JM
9976 /* When the computation is in excess precision, the type of the
9977 final EXCESS_PRECISION_EXPR. */
2d2e923f 9978 tree semantic_result_type = NULL;
8ce94e44 9979
3e4093b6
RS
9980 /* Nonzero means operands have already been type-converted
9981 in whatever way is necessary.
9982 Zero means they need to be converted to RESULT_TYPE. */
9983 int converted = 0;
9984
9985 /* Nonzero means create the expression with this type, rather than
9986 RESULT_TYPE. */
9987 tree build_type = 0;
9988
9989 /* Nonzero means after finally constructing the expression
9990 convert it to this type. */
9991 tree final_type = 0;
9992
9993 /* Nonzero if this is an operation like MIN or MAX which can
9994 safely be computed in short if both args are promoted shorts.
9995 Also implies COMMON.
9996 -1 indicates a bitwise operation; this makes a difference
9997 in the exact conditions for when it is safe to do the operation
9998 in a narrower mode. */
9999 int shorten = 0;
10000
10001 /* Nonzero if this is a comparison operation;
10002 if both args are promoted shorts, compare the original shorts.
10003 Also implies COMMON. */
10004 int short_compare = 0;
10005
10006 /* Nonzero if this is a right-shift operation, which can be computed on the
10007 original short and then promoted if the operand is a promoted short. */
10008 int short_shift = 0;
10009
10010 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10011 int common = 0;
10012
58393038
ZL
10013 /* True means types are compatible as far as ObjC is concerned. */
10014 bool objc_ok;
10015
8ce94e44
JM
10016 /* True means this is an arithmetic operation that may need excess
10017 precision. */
10018 bool may_need_excess_precision;
10019
180f8dbb
JM
10020 /* True means this is a boolean operation that converts both its
10021 operands to truth-values. */
10022 bool boolean_op = false;
10023
de5a5fa1
MP
10024 /* Remember whether we're doing / or %. */
10025 bool doing_div_or_mod = false;
10026
10027 /* Remember whether we're doing << or >>. */
10028 bool doing_shift = false;
10029
10030 /* Tree holding instrumentation expression. */
10031 tree instrument_expr = NULL;
10032
ba47d38d
AH
10033 if (location == UNKNOWN_LOCATION)
10034 location = input_location;
10035
4d84fe7c
JM
10036 op0 = orig_op0;
10037 op1 = orig_op1;
10038
10039 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10040 if (op0_int_operands)
10041 op0 = remove_c_maybe_const_expr (op0);
10042 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10043 if (op1_int_operands)
10044 op1 = remove_c_maybe_const_expr (op1);
10045 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10046 if (int_operands)
10047 {
10048 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10049 && TREE_CODE (orig_op1) == INTEGER_CST);
10050 int_const = (int_const_or_overflow
10051 && !TREE_OVERFLOW (orig_op0)
10052 && !TREE_OVERFLOW (orig_op1));
10053 }
10054 else
10055 int_const = int_const_or_overflow = false;
10056
0e3a99ae 10057 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
10058 if (convert_p
10059 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 10060 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 10061 {
4d84fe7c
JM
10062 op0 = default_conversion (op0);
10063 op1 = default_conversion (op1);
790e9490
RS
10064 }
10065
36536d79
BI
10066 /* When Cilk Plus is enabled and there are array notations inside op0, then
10067 we check to see if there are builtin array notation functions. If
10068 so, then we take on the type of the array notation inside it. */
b72271b9 10069 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10070 orig_type0 = type0 = find_correct_array_notation_type (op0);
10071 else
10072 orig_type0 = type0 = TREE_TYPE (op0);
10073
b72271b9 10074 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10075 orig_type1 = type1 = find_correct_array_notation_type (op1);
10076 else
10077 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10078
3e4093b6
RS
10079 /* The expression codes of the data types of the arguments tell us
10080 whether the arguments are integers, floating, pointers, etc. */
10081 code0 = TREE_CODE (type0);
10082 code1 = TREE_CODE (type1);
10083
10084 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10085 STRIP_TYPE_NOPS (op0);
10086 STRIP_TYPE_NOPS (op1);
10087
10088 /* If an error was already reported for one of the arguments,
10089 avoid reporting another error. */
10090
10091 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10092 return error_mark_node;
10093
4de67c26
JM
10094 if ((invalid_op_diag
10095 = targetm.invalid_binary_op (code, type0, type1)))
10096 {
ba47d38d 10097 error_at (location, invalid_op_diag);
4de67c26
JM
10098 return error_mark_node;
10099 }
10100
8ce94e44
JM
10101 switch (code)
10102 {
10103 case PLUS_EXPR:
10104 case MINUS_EXPR:
10105 case MULT_EXPR:
10106 case TRUNC_DIV_EXPR:
10107 case CEIL_DIV_EXPR:
10108 case FLOOR_DIV_EXPR:
10109 case ROUND_DIV_EXPR:
10110 case EXACT_DIV_EXPR:
10111 may_need_excess_precision = true;
10112 break;
10113 default:
10114 may_need_excess_precision = false;
10115 break;
10116 }
10117 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10118 {
10119 op0 = TREE_OPERAND (op0, 0);
10120 type0 = TREE_TYPE (op0);
10121 }
10122 else if (may_need_excess_precision
10123 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10124 {
10125 type0 = eptype;
10126 op0 = convert (eptype, op0);
10127 }
10128 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10129 {
10130 op1 = TREE_OPERAND (op1, 0);
10131 type1 = TREE_TYPE (op1);
10132 }
10133 else if (may_need_excess_precision
10134 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10135 {
10136 type1 = eptype;
10137 op1 = convert (eptype, op1);
10138 }
10139
58393038
ZL
10140 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10141
0e3a99ae
AS
10142 /* In case when one of the operands of the binary operation is
10143 a vector and another is a scalar -- convert scalar to vector. */
10144 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10145 {
a212e43f
MG
10146 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10147 true);
f90e8e2e 10148
0e3a99ae
AS
10149 switch (convert_flag)
10150 {
10151 case stv_error:
10152 return error_mark_node;
10153 case stv_firstarg:
10154 {
10155 bool maybe_const = true;
10156 tree sc;
10157 sc = c_fully_fold (op0, false, &maybe_const);
10158 sc = save_expr (sc);
10159 sc = convert (TREE_TYPE (type1), sc);
10160 op0 = build_vector_from_val (type1, sc);
10161 if (!maybe_const)
10162 op0 = c_wrap_maybe_const (op0, true);
10163 orig_type0 = type0 = TREE_TYPE (op0);
10164 code0 = TREE_CODE (type0);
10165 converted = 1;
10166 break;
10167 }
10168 case stv_secondarg:
10169 {
10170 bool maybe_const = true;
10171 tree sc;
10172 sc = c_fully_fold (op1, false, &maybe_const);
10173 sc = save_expr (sc);
10174 sc = convert (TREE_TYPE (type0), sc);
10175 op1 = build_vector_from_val (type0, sc);
10176 if (!maybe_const)
54b9f838 10177 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10178 orig_type1 = type1 = TREE_TYPE (op1);
10179 code1 = TREE_CODE (type1);
10180 converted = 1;
10181 break;
10182 }
10183 default:
10184 break;
10185 }
10186 }
10187
3e4093b6 10188 switch (code)
de520661 10189 {
3e4093b6
RS
10190 case PLUS_EXPR:
10191 /* Handle the pointer + int case. */
10192 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10193 {
db3927fb 10194 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10195 goto return_build_binary_op;
10196 }
3e4093b6 10197 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10198 {
db3927fb 10199 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10200 goto return_build_binary_op;
10201 }
fe67cf58 10202 else
3e4093b6
RS
10203 common = 1;
10204 break;
400fbf9f 10205
3e4093b6
RS
10206 case MINUS_EXPR:
10207 /* Subtraction of two similar pointers.
10208 We must subtract them as integers, then divide by object size. */
10209 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10210 && comp_target_types (location, type0, type1))
c9f9eb5d 10211 {
db3927fb 10212 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10213 goto return_build_binary_op;
10214 }
3e4093b6
RS
10215 /* Handle pointer minus int. Just like pointer plus int. */
10216 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10217 {
db3927fb 10218 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10219 goto return_build_binary_op;
10220 }
3e4093b6
RS
10221 else
10222 common = 1;
10223 break;
8b6a5902 10224
3e4093b6
RS
10225 case MULT_EXPR:
10226 common = 1;
10227 break;
10228
10229 case TRUNC_DIV_EXPR:
10230 case CEIL_DIV_EXPR:
10231 case FLOOR_DIV_EXPR:
10232 case ROUND_DIV_EXPR:
10233 case EXACT_DIV_EXPR:
de5a5fa1 10234 doing_div_or_mod = true;
c9f9eb5d 10235 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10236
10237 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10238 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10239 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10240 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10241 || code1 == FIXED_POINT_TYPE
3e4093b6 10242 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10243 {
5bed876a
AH
10244 enum tree_code tcode0 = code0, tcode1 = code1;
10245
3a021db2 10246 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10247 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10248 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10249 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10250
ab22c1fa
CF
10251 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10252 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10253 resultcode = RDIV_EXPR;
10254 else
10255 /* Although it would be tempting to shorten always here, that
10256 loses on some targets, since the modulo instruction is
10257 undefined if the quotient can't be represented in the
10258 computation mode. We shorten only if unsigned or if
10259 dividing by something we know != -1. */
8df83eae 10260 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10261 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10262 && !integer_all_onesp (op1)));
3e4093b6
RS
10263 common = 1;
10264 }
10265 break;
de520661 10266
3e4093b6 10267 case BIT_AND_EXPR:
3e4093b6
RS
10268 case BIT_IOR_EXPR:
10269 case BIT_XOR_EXPR:
10270 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10271 shorten = -1;
9ef0c8d9
AP
10272 /* Allow vector types which are not floating point types. */
10273 else if (code0 == VECTOR_TYPE
10274 && code1 == VECTOR_TYPE
10275 && !VECTOR_FLOAT_TYPE_P (type0)
10276 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10277 common = 1;
10278 break;
10279
10280 case TRUNC_MOD_EXPR:
10281 case FLOOR_MOD_EXPR:
de5a5fa1 10282 doing_div_or_mod = true;
c9f9eb5d 10283 warn_for_div_by_zero (location, op1);
de520661 10284
5cfd5d9b
AP
10285 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10286 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10287 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10288 common = 1;
10289 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10290 {
10291 /* Although it would be tempting to shorten always here, that loses
10292 on some targets, since the modulo instruction is undefined if the
10293 quotient can't be represented in the computation mode. We shorten
10294 only if unsigned or if dividing by something we know != -1. */
8df83eae 10295 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10296 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10297 && !integer_all_onesp (op1)));
3e4093b6
RS
10298 common = 1;
10299 }
10300 break;
de520661 10301
3e4093b6
RS
10302 case TRUTH_ANDIF_EXPR:
10303 case TRUTH_ORIF_EXPR:
10304 case TRUTH_AND_EXPR:
10305 case TRUTH_OR_EXPR:
10306 case TRUTH_XOR_EXPR:
10307 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10308 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10309 || code0 == FIXED_POINT_TYPE)
3e4093b6 10310 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10311 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10312 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10313 {
10314 /* Result of these operations is always an int,
10315 but that does not mean the operands should be
10316 converted to ints! */
10317 result_type = integer_type_node;
a27d595d
JM
10318 if (op0_int_operands)
10319 {
10320 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10321 op0 = remove_c_maybe_const_expr (op0);
10322 }
10323 else
10324 op0 = c_objc_common_truthvalue_conversion (location, op0);
10325 if (op1_int_operands)
10326 {
10327 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10328 op1 = remove_c_maybe_const_expr (op1);
10329 }
10330 else
10331 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10332 converted = 1;
180f8dbb 10333 boolean_op = true;
3e4093b6 10334 }
928c19bb
JM
10335 if (code == TRUTH_ANDIF_EXPR)
10336 {
10337 int_const_or_overflow = (int_operands
10338 && TREE_CODE (orig_op0) == INTEGER_CST
10339 && (op0 == truthvalue_false_node
10340 || TREE_CODE (orig_op1) == INTEGER_CST));
10341 int_const = (int_const_or_overflow
10342 && !TREE_OVERFLOW (orig_op0)
10343 && (op0 == truthvalue_false_node
10344 || !TREE_OVERFLOW (orig_op1)));
10345 }
10346 else if (code == TRUTH_ORIF_EXPR)
10347 {
10348 int_const_or_overflow = (int_operands
10349 && TREE_CODE (orig_op0) == INTEGER_CST
10350 && (op0 == truthvalue_true_node
10351 || TREE_CODE (orig_op1) == INTEGER_CST));
10352 int_const = (int_const_or_overflow
10353 && !TREE_OVERFLOW (orig_op0)
10354 && (op0 == truthvalue_true_node
10355 || !TREE_OVERFLOW (orig_op1)));
10356 }
3e4093b6 10357 break;
eba80994 10358
3e4093b6
RS
10359 /* Shift operations: result has same type as first operand;
10360 always convert second operand to int.
10361 Also set SHORT_SHIFT if shifting rightward. */
de520661 10362
3e4093b6 10363 case RSHIFT_EXPR:
f87bd04b
AS
10364 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10365 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10366 {
10367 result_type = type0;
10368 converted = 1;
10369 }
10370 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10371 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10372 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10373 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10374 {
10375 result_type = type0;
10376 converted = 1;
10377 }
10378 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10379 && code1 == INTEGER_TYPE)
3e4093b6 10380 {
de5a5fa1 10381 doing_shift = true;
928c19bb 10382 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10383 {
3e4093b6 10384 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10385 {
10386 int_const = false;
7d882b83 10387 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10388 warning (0, "right shift count is negative");
10389 }
3e4093b6 10390 else
bbb818c6 10391 {
3f75a254 10392 if (!integer_zerop (op1))
3e4093b6
RS
10393 short_shift = 1;
10394
10395 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10396 {
10397 int_const = false;
7d882b83 10398 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10399 warning (0, "right shift count >= width of type");
10400 }
bbb818c6 10401 }
b62acd60 10402 }
de520661 10403
3e4093b6
RS
10404 /* Use the type of the value to be shifted. */
10405 result_type = type0;
f87bd04b
AS
10406 /* Convert the non vector shift-count to an integer, regardless
10407 of size of value being shifted. */
10408 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10409 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10410 op1 = convert (integer_type_node, op1);
10411 /* Avoid converting op1 to result_type later. */
10412 converted = 1;
400fbf9f 10413 }
3e4093b6 10414 break;
253b6b82 10415
3e4093b6 10416 case LSHIFT_EXPR:
f87bd04b
AS
10417 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10418 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10419 {
10420 result_type = type0;
10421 converted = 1;
10422 }
10423 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10424 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10425 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10426 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10427 {
10428 result_type = type0;
10429 converted = 1;
10430 }
10431 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10432 && code1 == INTEGER_TYPE)
3e4093b6 10433 {
de5a5fa1 10434 doing_shift = true;
928c19bb 10435 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10436 {
3e4093b6 10437 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10438 {
10439 int_const = false;
7d882b83 10440 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10441 warning (0, "left shift count is negative");
10442 }
de520661 10443
3e4093b6 10444 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10445 {
10446 int_const = false;
7d882b83 10447 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10448 warning (0, "left shift count >= width of type");
10449 }
94ba5069 10450 }
de520661 10451
3e4093b6
RS
10452 /* Use the type of the value to be shifted. */
10453 result_type = type0;
f87bd04b
AS
10454 /* Convert the non vector shift-count to an integer, regardless
10455 of size of value being shifted. */
10456 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10457 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10458 op1 = convert (integer_type_node, op1);
10459 /* Avoid converting op1 to result_type later. */
10460 converted = 1;
400fbf9f 10461 }
3e4093b6 10462 break;
de520661 10463
3e4093b6
RS
10464 case EQ_EXPR:
10465 case NE_EXPR:
d246ab4f
AS
10466 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10467 {
10468 tree intt;
0af94e6f 10469 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10470 {
10471 error_at (location, "comparing vectors with different "
10472 "element types");
10473 return error_mark_node;
10474 }
10475
10476 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10477 {
10478 error_at (location, "comparing vectors with different "
10479 "number of elements");
10480 return error_mark_node;
10481 }
10482
10483 /* Always construct signed integer vector type. */
10484 intt = c_common_type_for_size (GET_MODE_BITSIZE
10485 (TYPE_MODE (TREE_TYPE (type0))), 0);
10486 result_type = build_opaque_vector_type (intt,
10487 TYPE_VECTOR_SUBPARTS (type0));
10488 converted = 1;
10489 break;
10490 }
ae311566 10491 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10492 warning_at (location,
10493 OPT_Wfloat_equal,
10494 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10495 /* Result of comparison is always int,
10496 but don't convert the args to int! */
10497 build_type = integer_type_node;
10498 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10499 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10500 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10501 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10502 short_compare = 1;
637f1455
SZ
10503 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10504 {
10505 if (TREE_CODE (op0) == ADDR_EXPR
10506 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10507 {
10508 if (code == EQ_EXPR)
10509 warning_at (location,
10510 OPT_Waddress,
10511 "the comparison will always evaluate as %<false%> "
10512 "for the address of %qD will never be NULL",
10513 TREE_OPERAND (op0, 0));
10514 else
10515 warning_at (location,
10516 OPT_Waddress,
10517 "the comparison will always evaluate as %<true%> "
10518 "for the address of %qD will never be NULL",
10519 TREE_OPERAND (op0, 0));
10520 }
10521 result_type = type0;
10522 }
10523 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10524 {
10525 if (TREE_CODE (op1) == ADDR_EXPR
10526 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10527 {
10528 if (code == EQ_EXPR)
10529 warning_at (location,
f90e8e2e 10530 OPT_Waddress,
637f1455
SZ
10531 "the comparison will always evaluate as %<false%> "
10532 "for the address of %qD will never be NULL",
10533 TREE_OPERAND (op1, 0));
10534 else
10535 warning_at (location,
10536 OPT_Waddress,
10537 "the comparison will always evaluate as %<true%> "
10538 "for the address of %qD will never be NULL",
10539 TREE_OPERAND (op1, 0));
10540 }
10541 result_type = type1;
10542 }
3e4093b6
RS
10543 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10544 {
10545 tree tt0 = TREE_TYPE (type0);
10546 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10547 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10548 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10549 addr_space_t as_common = ADDR_SPACE_GENERIC;
10550
3e4093b6
RS
10551 /* Anything compares with void *. void * compares with anything.
10552 Otherwise, the targets must be compatible
10553 and both must be object or both incomplete. */
744aa42f 10554 if (comp_target_types (location, type0, type1))
10bc1b1b 10555 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10556 else if (!addr_space_superset (as0, as1, &as_common))
10557 {
10558 error_at (location, "comparison of pointers to "
10559 "disjoint address spaces");
10560 return error_mark_node;
10561 }
267bac10 10562 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10563 {
36c5e70a 10564 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10565 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10566 "comparison of %<void *%> with function pointer");
ee2990e7 10567 }
267bac10 10568 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10569 {
36c5e70a 10570 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10571 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10572 "comparison of %<void *%> with function pointer");
e6834654 10573 }
3e4093b6 10574 else
58393038
ZL
10575 /* Avoid warning about the volatile ObjC EH puts on decls. */
10576 if (!objc_ok)
ba47d38d 10577 pedwarn (location, 0,
509c9d60 10578 "comparison of distinct pointer types lacks a cast");
e6834654 10579
3e4093b6 10580 if (result_type == NULL_TREE)
36c5e70a
BE
10581 {
10582 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10583 result_type = build_pointer_type
10584 (build_qualified_type (void_type_node, qual));
10585 }
e6834654 10586 }
3e4093b6 10587 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10588 {
3e4093b6 10589 result_type = type0;
ba47d38d 10590 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10591 }
3e4093b6 10592 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10593 {
3e4093b6 10594 result_type = type1;
ba47d38d 10595 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10596 }
3e4093b6 10597 break;
8b6a5902 10598
3e4093b6
RS
10599 case LE_EXPR:
10600 case GE_EXPR:
10601 case LT_EXPR:
10602 case GT_EXPR:
d246ab4f
AS
10603 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10604 {
10605 tree intt;
0af94e6f 10606 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10607 {
10608 error_at (location, "comparing vectors with different "
10609 "element types");
10610 return error_mark_node;
10611 }
10612
10613 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10614 {
10615 error_at (location, "comparing vectors with different "
10616 "number of elements");
10617 return error_mark_node;
10618 }
10619
10620 /* Always construct signed integer vector type. */
10621 intt = c_common_type_for_size (GET_MODE_BITSIZE
10622 (TYPE_MODE (TREE_TYPE (type0))), 0);
10623 result_type = build_opaque_vector_type (intt,
10624 TYPE_VECTOR_SUBPARTS (type0));
10625 converted = 1;
10626 break;
10627 }
3e4093b6 10628 build_type = integer_type_node;
ab22c1fa
CF
10629 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10630 || code0 == FIXED_POINT_TYPE)
10631 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10632 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10633 short_compare = 1;
10634 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10635 {
36c5e70a
BE
10636 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10637 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10638 addr_space_t as_common;
10639
744aa42f 10640 if (comp_target_types (location, type0, type1))
3e4093b6 10641 {
10bc1b1b 10642 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10643 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10644 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10645 pedwarn (location, 0,
509c9d60 10646 "comparison of complete and incomplete pointers");
fcf73884 10647 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10648 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10649 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10650 else if (null_pointer_constant_p (orig_op0)
10651 || null_pointer_constant_p (orig_op1))
10652 warning_at (location, OPT_Wextra,
10653 "ordered comparison of pointer with null pointer");
10654
3e4093b6 10655 }
36c5e70a
BE
10656 else if (!addr_space_superset (as0, as1, &as_common))
10657 {
10658 error_at (location, "comparison of pointers to "
10659 "disjoint address spaces");
10660 return error_mark_node;
10661 }
3e4093b6
RS
10662 else
10663 {
36c5e70a
BE
10664 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10665 result_type = build_pointer_type
10666 (build_qualified_type (void_type_node, qual));
ba47d38d 10667 pedwarn (location, 0,
509c9d60 10668 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10669 }
10670 }
6aa3c60d 10671 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10672 {
10673 result_type = type0;
fcf73884 10674 if (pedantic)
c1771a20 10675 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10676 "ordered comparison of pointer with integer zero");
10677 else if (extra_warnings)
ba47d38d 10678 warning_at (location, OPT_Wextra,
d42ba3b8 10679 "ordered comparison of pointer with integer zero");
3e4093b6 10680 }
6aa3c60d 10681 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10682 {
10683 result_type = type1;
d42ba3b8 10684 if (pedantic)
c1771a20 10685 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10686 "ordered comparison of pointer with integer zero");
10687 else if (extra_warnings)
10688 warning_at (location, OPT_Wextra,
10689 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10690 }
10691 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10692 {
10693 result_type = type0;
ba47d38d 10694 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10695 }
10696 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10697 {
10698 result_type = type1;
ba47d38d 10699 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10700 }
10701 break;
64094f6a 10702
3e4093b6 10703 default:
37b2f290 10704 gcc_unreachable ();
c9fe6f9f 10705 }
8f17b5c5 10706
e57e265b
PB
10707 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10708 return error_mark_node;
10709
5bed876a
AH
10710 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10711 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10712 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10713 {
ba47d38d 10714 binary_op_error (location, code, type0, type1);
5bed876a
AH
10715 return error_mark_node;
10716 }
10717
3e4093b6 10718 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10719 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10720 &&
10721 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10722 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10723 {
2ca862e9
JM
10724 bool first_complex = (code0 == COMPLEX_TYPE);
10725 bool second_complex = (code1 == COMPLEX_TYPE);
10726 int none_complex = (!first_complex && !second_complex);
39b726dd 10727
3e4093b6 10728 if (shorten || common || short_compare)
3bf6bfcc
JJ
10729 {
10730 result_type = c_common_type (type0, type1);
0a0b3574
MM
10731 do_warn_double_promotion (result_type, type0, type1,
10732 "implicit conversion from %qT to %qT "
10733 "to match other operand of binary "
10734 "expression",
10735 location);
3bf6bfcc
JJ
10736 if (result_type == error_mark_node)
10737 return error_mark_node;
10738 }
400fbf9f 10739
2ca862e9
JM
10740 if (first_complex != second_complex
10741 && (code == PLUS_EXPR
10742 || code == MINUS_EXPR
10743 || code == MULT_EXPR
10744 || (code == TRUNC_DIV_EXPR && first_complex))
10745 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10746 && flag_signed_zeros)
10747 {
10748 /* An operation on mixed real/complex operands must be
10749 handled specially, but the language-independent code can
10750 more easily optimize the plain complex arithmetic if
10751 -fno-signed-zeros. */
10752 tree real_type = TREE_TYPE (result_type);
10753 tree real, imag;
10754 if (type0 != orig_type0 || type1 != orig_type1)
10755 {
10756 gcc_assert (may_need_excess_precision && common);
2d2e923f 10757 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
10758 }
10759 if (first_complex)
10760 {
10761 if (TREE_TYPE (op0) != result_type)
68fca595 10762 op0 = convert_and_check (location, result_type, op0);
2ca862e9 10763 if (TREE_TYPE (op1) != real_type)
68fca595 10764 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
10765 }
10766 else
10767 {
10768 if (TREE_TYPE (op0) != real_type)
68fca595 10769 op0 = convert_and_check (location, real_type, op0);
2ca862e9 10770 if (TREE_TYPE (op1) != result_type)
68fca595 10771 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
10772 }
10773 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10774 return error_mark_node;
10775 if (first_complex)
10776 {
10777 op0 = c_save_expr (op0);
10778 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10779 op0, 1);
10780 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10781 op0, 1);
10782 switch (code)
10783 {
10784 case MULT_EXPR:
10785 case TRUNC_DIV_EXPR:
c4603e7c 10786 op1 = c_save_expr (op1);
2ca862e9
JM
10787 imag = build2 (resultcode, real_type, imag, op1);
10788 /* Fall through. */
10789 case PLUS_EXPR:
10790 case MINUS_EXPR:
10791 real = build2 (resultcode, real_type, real, op1);
10792 break;
10793 default:
10794 gcc_unreachable();
10795 }
10796 }
10797 else
10798 {
10799 op1 = c_save_expr (op1);
10800 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10801 op1, 1);
10802 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10803 op1, 1);
10804 switch (code)
10805 {
10806 case MULT_EXPR:
c4603e7c 10807 op0 = c_save_expr (op0);
2ca862e9
JM
10808 imag = build2 (resultcode, real_type, op0, imag);
10809 /* Fall through. */
10810 case PLUS_EXPR:
10811 real = build2 (resultcode, real_type, op0, real);
10812 break;
10813 case MINUS_EXPR:
10814 real = build2 (resultcode, real_type, op0, real);
10815 imag = build1 (NEGATE_EXPR, real_type, imag);
10816 break;
10817 default:
10818 gcc_unreachable();
10819 }
10820 }
10821 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10822 goto return_build_binary_op;
10823 }
10824
3e4093b6
RS
10825 /* For certain operations (which identify themselves by shorten != 0)
10826 if both args were extended from the same smaller type,
10827 do the arithmetic in that type and then extend.
400fbf9f 10828
3e4093b6
RS
10829 shorten !=0 and !=1 indicates a bitwise operation.
10830 For them, this optimization is safe only if
10831 both args are zero-extended or both are sign-extended.
10832 Otherwise, we might change the result.
10833 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10834 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 10835
3e4093b6
RS
10836 if (shorten && none_complex)
10837 {
3e4093b6 10838 final_type = result_type;
b8698a0f 10839 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 10840 shorten == -1);
3e4093b6 10841 }
88a3dbc1 10842
3e4093b6 10843 /* Shifts can be shortened if shifting right. */
2f6e4e97 10844
3e4093b6
RS
10845 if (short_shift)
10846 {
10847 int unsigned_arg;
10848 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 10849
3e4093b6 10850 final_type = result_type;
abe80e6d 10851
3e4093b6 10852 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 10853 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 10854
3e4093b6 10855 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 10856 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
10857 /* We can shorten only if the shift count is less than the
10858 number of bits in the smaller type size. */
10859 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10860 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 10861 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
10862 {
10863 /* Do an unsigned shift if the operand was zero-extended. */
10864 result_type
10865 = c_common_signed_or_unsigned_type (unsigned_arg,
10866 TREE_TYPE (arg0));
10867 /* Convert value-to-be-shifted to that type. */
10868 if (TREE_TYPE (op0) != result_type)
10869 op0 = convert (result_type, op0);
10870 converted = 1;
abe80e6d 10871 }
88a3dbc1
RK
10872 }
10873
3e4093b6
RS
10874 /* Comparison operations are shortened too but differently.
10875 They identify themselves by setting short_compare = 1. */
56cb9733 10876
3e4093b6
RS
10877 if (short_compare)
10878 {
10879 /* Don't write &op0, etc., because that would prevent op0
10880 from being kept in a register.
10881 Instead, make copies of the our local variables and
10882 pass the copies by reference, then copy them back afterward. */
10883 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10884 enum tree_code xresultcode = resultcode;
10885 tree val
393e8e8b
MP
10886 = shorten_compare (location, &xop0, &xop1, &xresult_type,
10887 &xresultcode);
8f17b5c5 10888
3e4093b6 10889 if (val != 0)
c9f9eb5d
AH
10890 {
10891 ret = val;
10892 goto return_build_binary_op;
10893 }
8f17b5c5 10894
3e4093b6
RS
10895 op0 = xop0, op1 = xop1;
10896 converted = 1;
10897 resultcode = xresultcode;
8f17b5c5 10898
7d882b83 10899 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10900 {
10901 bool op0_maybe_const = true;
10902 bool op1_maybe_const = true;
10903 tree orig_op0_folded, orig_op1_folded;
10904
10905 if (in_late_binary_op)
10906 {
10907 orig_op0_folded = orig_op0;
10908 orig_op1_folded = orig_op1;
10909 }
10910 else
10911 {
10912 /* Fold for the sake of possible warnings, as in
10913 build_conditional_expr. This requires the
10914 "original" values to be folded, not just op0 and
10915 op1. */
f5178456 10916 c_inhibit_evaluation_warnings++;
928c19bb
JM
10917 op0 = c_fully_fold (op0, require_constant_value,
10918 &op0_maybe_const);
10919 op1 = c_fully_fold (op1, require_constant_value,
10920 &op1_maybe_const);
f5178456 10921 c_inhibit_evaluation_warnings--;
928c19bb
JM
10922 orig_op0_folded = c_fully_fold (orig_op0,
10923 require_constant_value,
10924 NULL);
10925 orig_op1_folded = c_fully_fold (orig_op1,
10926 require_constant_value,
10927 NULL);
10928 }
10929
10930 if (warn_sign_compare)
10931 warn_for_sign_compare (location, orig_op0_folded,
10932 orig_op1_folded, op0, op1,
10933 result_type, resultcode);
5c2f94b4 10934 if (!in_late_binary_op && !int_operands)
928c19bb
JM
10935 {
10936 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 10937 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 10938 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 10939 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 10940 }
3e4093b6 10941 }
2ad1815d 10942 }
64094f6a 10943 }
64094f6a 10944
3e4093b6
RS
10945 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10946 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10947 Then the expression will be built.
10948 It will be given type FINAL_TYPE if that is nonzero;
10949 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 10950
3e4093b6
RS
10951 if (!result_type)
10952 {
ba47d38d 10953 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
10954 return error_mark_node;
10955 }
400fbf9f 10956
3e4093b6 10957 if (build_type == NULL_TREE)
8ce94e44
JM
10958 {
10959 build_type = result_type;
180f8dbb
JM
10960 if ((type0 != orig_type0 || type1 != orig_type1)
10961 && !boolean_op)
8ce94e44
JM
10962 {
10963 gcc_assert (may_need_excess_precision && common);
2d2e923f 10964 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
10965 }
10966 }
400fbf9f 10967
2d2e923f
MLI
10968 if (!converted)
10969 {
68fca595
MP
10970 op0 = ep_convert_and_check (location, result_type, op0,
10971 semantic_result_type);
10972 op1 = ep_convert_and_check (location, result_type, op1,
10973 semantic_result_type);
2d2e923f
MLI
10974
10975 /* This can happen if one operand has a vector type, and the other
10976 has a different type. */
10977 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10978 return error_mark_node;
10979 }
10980
a24d975c 10981 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
de5a5fa1 10982 && current_function_decl != 0
ce6923c5
MP
10983 && !lookup_attribute ("no_sanitize_undefined",
10984 DECL_ATTRIBUTES (current_function_decl))
de5a5fa1
MP
10985 && (doing_div_or_mod || doing_shift))
10986 {
10987 /* OP0 and/or OP1 might have side-effects. */
10988 op0 = c_save_expr (op0);
10989 op1 = c_save_expr (op1);
10990 op0 = c_fully_fold (op0, false, NULL);
10991 op1 = c_fully_fold (op1, false, NULL);
a24d975c 10992 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
de5a5fa1 10993 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 10994 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
10995 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
10996 }
10997
c9f9eb5d 10998 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
10999 if (int_const_or_overflow)
11000 ret = (require_constant_value
db3927fb
AH
11001 ? fold_build2_initializer_loc (location, resultcode, build_type,
11002 op0, op1)
11003 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11004 else
11005 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11006 if (final_type != 0)
11007 ret = convert (final_type, ret);
11008
11009 return_build_binary_op:
11010 gcc_assert (ret != error_mark_node);
928c19bb
JM
11011 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11012 ret = (int_operands
11013 ? note_integer_operands (ret)
11014 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11015 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11016 && !in_late_binary_op)
11017 ret = note_integer_operands (ret);
2d2e923f
MLI
11018 if (semantic_result_type)
11019 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11020 protected_set_expr_location (ret, location);
de5a5fa1 11021
a24d975c 11022 if (instrument_expr != NULL)
de5a5fa1
MP
11023 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11024 instrument_expr, ret);
11025
c9f9eb5d 11026 return ret;
400fbf9f 11027}
85498824
JM
11028
11029
11030/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11031 purpose. LOCATION is the source location for the expression. */
85498824
JM
11032
11033tree
ba47d38d 11034c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11035{
928c19bb
JM
11036 bool int_const, int_operands;
11037
85498824
JM
11038 switch (TREE_CODE (TREE_TYPE (expr)))
11039 {
11040 case ARRAY_TYPE:
ba47d38d 11041 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11042 return error_mark_node;
11043
11044 case RECORD_TYPE:
ba47d38d 11045 error_at (location, "used struct type value where scalar is required");
85498824
JM
11046 return error_mark_node;
11047
11048 case UNION_TYPE:
ba47d38d 11049 error_at (location, "used union type value where scalar is required");
85498824
JM
11050 return error_mark_node;
11051
04af8788
NP
11052 case VOID_TYPE:
11053 error_at (location, "void value not ignored as it ought to be");
11054 return error_mark_node;
11055
46bdb9cf
JM
11056 case FUNCTION_TYPE:
11057 gcc_unreachable ();
11058
d246ab4f
AS
11059 case VECTOR_TYPE:
11060 error_at (location, "used vector type where scalar is required");
11061 return error_mark_node;
11062
85498824
JM
11063 default:
11064 break;
11065 }
11066
928c19bb
JM
11067 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11068 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11069 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11070 {
11071 expr = remove_c_maybe_const_expr (expr);
11072 expr = build2 (NE_EXPR, integer_type_node, expr,
11073 convert (TREE_TYPE (expr), integer_zero_node));
11074 expr = note_integer_operands (expr);
11075 }
11076 else
11077 /* ??? Should we also give an error for vectors rather than leaving
11078 those to give errors later? */
11079 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11080
11081 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11082 {
11083 if (TREE_OVERFLOW (expr))
11084 return expr;
11085 else
11086 return note_integer_operands (expr);
11087 }
11088 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11089 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11090 return expr;
85498824 11091}
73f397d4
JM
11092\f
11093
11094/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11095 required. */
11096
11097tree
51eed280 11098c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11099{
11100 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11101 {
11102 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11103 /* Executing a compound literal inside a function reinitializes
11104 it. */
11105 if (!TREE_STATIC (decl))
11106 *se = true;
11107 return decl;
11108 }
11109 else
11110 return expr;
11111}
953ff289 11112\f
c0220ea4 11113/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11114
11115tree
11116c_begin_omp_parallel (void)
11117{
11118 tree block;
11119
11120 keep_next_level ();
11121 block = c_begin_compound_stmt (true);
11122
11123 return block;
11124}
11125
c2255bc4
AH
11126/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11127 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11128
953ff289 11129tree
c2255bc4 11130c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11131{
11132 tree stmt;
11133
c2255bc4 11134 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11135
11136 stmt = make_node (OMP_PARALLEL);
11137 TREE_TYPE (stmt) = void_type_node;
11138 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11139 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11140 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11141
11142 return add_stmt (stmt);
11143}
11144
a68ab351
JJ
11145/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11146
11147tree
11148c_begin_omp_task (void)
11149{
11150 tree block;
11151
11152 keep_next_level ();
11153 block = c_begin_compound_stmt (true);
11154
11155 return block;
11156}
11157
c2255bc4
AH
11158/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11159 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11160
11161tree
c2255bc4 11162c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11163{
11164 tree stmt;
11165
c2255bc4 11166 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11167
11168 stmt = make_node (OMP_TASK);
11169 TREE_TYPE (stmt) = void_type_node;
11170 OMP_TASK_CLAUSES (stmt) = clauses;
11171 OMP_TASK_BODY (stmt) = block;
c2255bc4 11172 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11173
11174 return add_stmt (stmt);
11175}
11176
acf0174b
JJ
11177/* Generate GOMP_cancel call for #pragma omp cancel. */
11178
11179void
11180c_finish_omp_cancel (location_t loc, tree clauses)
11181{
11182 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11183 int mask = 0;
11184 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11185 mask = 1;
11186 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11187 mask = 2;
11188 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11189 mask = 4;
11190 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11191 mask = 8;
11192 else
11193 {
11194 error_at (loc, "%<#pragma omp cancel must specify one of "
11195 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11196 "clauses");
11197 return;
11198 }
11199 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11200 if (ifc != NULL_TREE)
11201 {
11202 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11203 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11204 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11205 build_zero_cst (type));
11206 }
11207 else
11208 ifc = boolean_true_node;
11209 tree stmt = build_call_expr_loc (loc, fn, 2,
11210 build_int_cst (integer_type_node, mask),
11211 ifc);
11212 add_stmt (stmt);
11213}
11214
11215/* Generate GOMP_cancellation_point call for
11216 #pragma omp cancellation point. */
11217
11218void
11219c_finish_omp_cancellation_point (location_t loc, tree clauses)
11220{
11221 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11222 int mask = 0;
11223 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11224 mask = 1;
11225 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11226 mask = 2;
11227 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11228 mask = 4;
11229 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11230 mask = 8;
11231 else
11232 {
11233 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11234 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11235 "clauses");
11236 return;
11237 }
11238 tree stmt = build_call_expr_loc (loc, fn, 1,
11239 build_int_cst (integer_type_node, mask));
11240 add_stmt (stmt);
11241}
11242
11243/* Helper function for handle_omp_array_sections. Called recursively
11244 to handle multiple array-section-subscripts. C is the clause,
11245 T current expression (initially OMP_CLAUSE_DECL), which is either
11246 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11247 expression if specified, TREE_VALUE length expression if specified,
11248 TREE_CHAIN is what it has been specified after, or some decl.
11249 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11250 set to true if any of the array-section-subscript could have length
11251 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11252 first array-section-subscript which is known not to have length
11253 of one. Given say:
11254 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11255 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11256 all are or may have length of 1, array-section-subscript [:2] is the
11257 first one knonwn not to have length 1. For array-section-subscript
11258 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11259 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11260 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11261 case though, as some lengths could be zero. */
11262
11263static tree
11264handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11265 bool &maybe_zero_len, unsigned int &first_non_one)
11266{
11267 tree ret, low_bound, length, type;
11268 if (TREE_CODE (t) != TREE_LIST)
11269 {
11270 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11271 return error_mark_node;
11272 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11273 {
11274 if (DECL_P (t))
11275 error_at (OMP_CLAUSE_LOCATION (c),
11276 "%qD is not a variable in %qs clause", t,
11277 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11278 else
11279 error_at (OMP_CLAUSE_LOCATION (c),
11280 "%qE is not a variable in %qs clause", t,
11281 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11282 return error_mark_node;
11283 }
11284 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11285 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11286 {
11287 error_at (OMP_CLAUSE_LOCATION (c),
11288 "%qD is threadprivate variable in %qs clause", t,
11289 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11290 return error_mark_node;
11291 }
11292 return t;
11293 }
11294
11295 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11296 maybe_zero_len, first_non_one);
11297 if (ret == error_mark_node || ret == NULL_TREE)
11298 return ret;
11299
11300 type = TREE_TYPE (ret);
11301 low_bound = TREE_PURPOSE (t);
11302 length = TREE_VALUE (t);
11303
11304 if (low_bound == error_mark_node || length == error_mark_node)
11305 return error_mark_node;
11306
11307 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11308 {
11309 error_at (OMP_CLAUSE_LOCATION (c),
11310 "low bound %qE of array section does not have integral type",
11311 low_bound);
11312 return error_mark_node;
11313 }
11314 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11315 {
11316 error_at (OMP_CLAUSE_LOCATION (c),
11317 "length %qE of array section does not have integral type",
11318 length);
11319 return error_mark_node;
11320 }
11321 if (low_bound
11322 && TREE_CODE (low_bound) == INTEGER_CST
11323 && TYPE_PRECISION (TREE_TYPE (low_bound))
11324 > TYPE_PRECISION (sizetype))
11325 low_bound = fold_convert (sizetype, low_bound);
11326 if (length
11327 && TREE_CODE (length) == INTEGER_CST
11328 && TYPE_PRECISION (TREE_TYPE (length))
11329 > TYPE_PRECISION (sizetype))
11330 length = fold_convert (sizetype, length);
11331 if (low_bound == NULL_TREE)
11332 low_bound = integer_zero_node;
11333
11334 if (length != NULL_TREE)
11335 {
11336 if (!integer_nonzerop (length))
11337 maybe_zero_len = true;
11338 if (first_non_one == types.length ()
11339 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11340 first_non_one++;
11341 }
11342 if (TREE_CODE (type) == ARRAY_TYPE)
11343 {
11344 if (length == NULL_TREE
11345 && (TYPE_DOMAIN (type) == NULL_TREE
11346 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11347 {
11348 error_at (OMP_CLAUSE_LOCATION (c),
11349 "for unknown bound array type length expression must "
11350 "be specified");
11351 return error_mark_node;
11352 }
11353 if (TREE_CODE (low_bound) == INTEGER_CST
11354 && tree_int_cst_sgn (low_bound) == -1)
11355 {
11356 error_at (OMP_CLAUSE_LOCATION (c),
11357 "negative low bound in array section in %qs clause",
11358 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11359 return error_mark_node;
11360 }
11361 if (length != NULL_TREE
11362 && TREE_CODE (length) == INTEGER_CST
11363 && tree_int_cst_sgn (length) == -1)
11364 {
11365 error_at (OMP_CLAUSE_LOCATION (c),
11366 "negative length in array section in %qs clause",
11367 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11368 return error_mark_node;
11369 }
11370 if (TYPE_DOMAIN (type)
11371 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11372 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11373 == INTEGER_CST)
11374 {
11375 tree size = size_binop (PLUS_EXPR,
11376 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11377 size_one_node);
11378 if (TREE_CODE (low_bound) == INTEGER_CST)
11379 {
11380 if (tree_int_cst_lt (size, low_bound))
11381 {
11382 error_at (OMP_CLAUSE_LOCATION (c),
11383 "low bound %qE above array section size "
11384 "in %qs clause", low_bound,
11385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11386 return error_mark_node;
11387 }
11388 if (tree_int_cst_equal (size, low_bound))
11389 maybe_zero_len = true;
11390 else if (length == NULL_TREE
11391 && first_non_one == types.length ()
11392 && tree_int_cst_equal
11393 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11394 low_bound))
11395 first_non_one++;
11396 }
11397 else if (length == NULL_TREE)
11398 {
11399 maybe_zero_len = true;
11400 if (first_non_one == types.length ())
11401 first_non_one++;
11402 }
11403 if (length && TREE_CODE (length) == INTEGER_CST)
11404 {
11405 if (tree_int_cst_lt (size, length))
11406 {
11407 error_at (OMP_CLAUSE_LOCATION (c),
11408 "length %qE above array section size "
11409 "in %qs clause", length,
11410 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11411 return error_mark_node;
11412 }
11413 if (TREE_CODE (low_bound) == INTEGER_CST)
11414 {
11415 tree lbpluslen
11416 = size_binop (PLUS_EXPR,
11417 fold_convert (sizetype, low_bound),
11418 fold_convert (sizetype, length));
11419 if (TREE_CODE (lbpluslen) == INTEGER_CST
11420 && tree_int_cst_lt (size, lbpluslen))
11421 {
11422 error_at (OMP_CLAUSE_LOCATION (c),
11423 "high bound %qE above array section size "
11424 "in %qs clause", lbpluslen,
11425 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11426 return error_mark_node;
11427 }
11428 }
11429 }
11430 }
11431 else if (length == NULL_TREE)
11432 {
11433 maybe_zero_len = true;
11434 if (first_non_one == types.length ())
11435 first_non_one++;
11436 }
11437
11438 /* For [lb:] we will need to evaluate lb more than once. */
11439 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11440 {
11441 tree lb = c_save_expr (low_bound);
11442 if (lb != low_bound)
11443 {
11444 TREE_PURPOSE (t) = lb;
11445 low_bound = lb;
11446 }
11447 }
11448 }
11449 else if (TREE_CODE (type) == POINTER_TYPE)
11450 {
11451 if (length == NULL_TREE)
11452 {
11453 error_at (OMP_CLAUSE_LOCATION (c),
11454 "for pointer type length expression must be specified");
11455 return error_mark_node;
11456 }
11457 /* If there is a pointer type anywhere but in the very first
11458 array-section-subscript, the array section can't be contiguous. */
11459 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11460 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11461 {
11462 error_at (OMP_CLAUSE_LOCATION (c),
11463 "array section is not contiguous in %qs clause",
11464 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11465 return error_mark_node;
11466 }
11467 }
11468 else
11469 {
11470 error_at (OMP_CLAUSE_LOCATION (c),
11471 "%qE does not have pointer or array type", ret);
11472 return error_mark_node;
11473 }
11474 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11475 types.safe_push (TREE_TYPE (ret));
11476 /* We will need to evaluate lb more than once. */
11477 tree lb = c_save_expr (low_bound);
11478 if (lb != low_bound)
11479 {
11480 TREE_PURPOSE (t) = lb;
11481 low_bound = lb;
11482 }
11483 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11484 return ret;
11485}
11486
11487/* Handle array sections for clause C. */
11488
11489static bool
11490handle_omp_array_sections (tree c)
11491{
11492 bool maybe_zero_len = false;
11493 unsigned int first_non_one = 0;
11494 vec<tree> types = vNULL;
11495 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11496 maybe_zero_len, first_non_one);
11497 if (first == error_mark_node)
11498 {
11499 types.release ();
11500 return true;
11501 }
11502 if (first == NULL_TREE)
11503 {
11504 types.release ();
11505 return false;
11506 }
11507 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11508 {
11509 tree t = OMP_CLAUSE_DECL (c);
11510 tree tem = NULL_TREE;
11511 types.release ();
11512 /* Need to evaluate side effects in the length expressions
11513 if any. */
11514 while (TREE_CODE (t) == TREE_LIST)
11515 {
11516 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11517 {
11518 if (tem == NULL_TREE)
11519 tem = TREE_VALUE (t);
11520 else
11521 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11522 TREE_VALUE (t), tem);
11523 }
11524 t = TREE_CHAIN (t);
11525 }
11526 if (tem)
11527 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11528 first = c_fully_fold (first, false, NULL);
11529 OMP_CLAUSE_DECL (c) = first;
11530 }
11531 else
11532 {
11533 unsigned int num = types.length (), i;
11534 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11535 tree condition = NULL_TREE;
11536
11537 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11538 maybe_zero_len = true;
11539
11540 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11541 t = TREE_CHAIN (t))
11542 {
11543 tree low_bound = TREE_PURPOSE (t);
11544 tree length = TREE_VALUE (t);
11545
11546 i--;
11547 if (low_bound
11548 && TREE_CODE (low_bound) == INTEGER_CST
11549 && TYPE_PRECISION (TREE_TYPE (low_bound))
11550 > TYPE_PRECISION (sizetype))
11551 low_bound = fold_convert (sizetype, low_bound);
11552 if (length
11553 && TREE_CODE (length) == INTEGER_CST
11554 && TYPE_PRECISION (TREE_TYPE (length))
11555 > TYPE_PRECISION (sizetype))
11556 length = fold_convert (sizetype, length);
11557 if (low_bound == NULL_TREE)
11558 low_bound = integer_zero_node;
11559 if (!maybe_zero_len && i > first_non_one)
11560 {
11561 if (integer_nonzerop (low_bound))
11562 goto do_warn_noncontiguous;
11563 if (length != NULL_TREE
11564 && TREE_CODE (length) == INTEGER_CST
11565 && TYPE_DOMAIN (types[i])
11566 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11567 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11568 == INTEGER_CST)
11569 {
11570 tree size;
11571 size = size_binop (PLUS_EXPR,
11572 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11573 size_one_node);
11574 if (!tree_int_cst_equal (length, size))
11575 {
11576 do_warn_noncontiguous:
11577 error_at (OMP_CLAUSE_LOCATION (c),
11578 "array section is not contiguous in %qs "
11579 "clause",
11580 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11581 types.release ();
11582 return true;
11583 }
11584 }
11585 if (length != NULL_TREE
11586 && TREE_SIDE_EFFECTS (length))
11587 {
11588 if (side_effects == NULL_TREE)
11589 side_effects = length;
11590 else
11591 side_effects = build2 (COMPOUND_EXPR,
11592 TREE_TYPE (side_effects),
11593 length, side_effects);
11594 }
11595 }
11596 else
11597 {
11598 tree l;
11599
11600 if (i > first_non_one && length && integer_nonzerop (length))
11601 continue;
11602 if (length)
11603 l = fold_convert (sizetype, length);
11604 else
11605 {
11606 l = size_binop (PLUS_EXPR,
11607 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11608 size_one_node);
11609 l = size_binop (MINUS_EXPR, l,
11610 fold_convert (sizetype, low_bound));
11611 }
11612 if (i > first_non_one)
11613 {
11614 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11615 size_zero_node);
11616 if (condition == NULL_TREE)
11617 condition = l;
11618 else
11619 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11620 l, condition);
11621 }
11622 else if (size == NULL_TREE)
11623 {
11624 size = size_in_bytes (TREE_TYPE (types[i]));
11625 size = size_binop (MULT_EXPR, size, l);
11626 if (condition)
11627 size = fold_build3 (COND_EXPR, sizetype, condition,
11628 size, size_zero_node);
11629 }
11630 else
11631 size = size_binop (MULT_EXPR, size, l);
11632 }
11633 }
11634 types.release ();
11635 if (side_effects)
11636 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11637 first = c_fully_fold (first, false, NULL);
11638 OMP_CLAUSE_DECL (c) = first;
11639 if (size)
11640 size = c_fully_fold (size, false, NULL);
11641 OMP_CLAUSE_SIZE (c) = size;
11642 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11643 return false;
11644 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11645 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11646 if (!c_mark_addressable (t))
11647 return false;
11648 OMP_CLAUSE_DECL (c2) = t;
11649 t = build_fold_addr_expr (first);
11650 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11651 tree ptr = OMP_CLAUSE_DECL (c2);
11652 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11653 ptr = build_fold_addr_expr (ptr);
11654 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11655 ptrdiff_type_node, t,
11656 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11657 ptrdiff_type_node, ptr));
11658 t = c_fully_fold (t, false, NULL);
11659 OMP_CLAUSE_SIZE (c2) = t;
11660 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11661 OMP_CLAUSE_CHAIN (c) = c2;
11662 }
11663 return false;
11664}
11665
11666/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11667 an inline call. But, remap
11668 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11669 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11670
11671static tree
11672c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11673 tree decl, tree placeholder)
11674{
11675 copy_body_data id;
11676 struct pointer_map_t *decl_map = pointer_map_create ();
11677
11678 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
11679 *pointer_map_insert (decl_map, omp_decl2) = decl;
11680 memset (&id, 0, sizeof (id));
11681 id.src_fn = DECL_CONTEXT (omp_decl1);
11682 id.dst_fn = current_function_decl;
11683 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11684 id.decl_map = decl_map;
11685
11686 id.copy_decl = copy_decl_no_change;
11687 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11688 id.transform_new_cfg = true;
11689 id.transform_return_to_modify = false;
11690 id.transform_lang_insert_block = NULL;
11691 id.eh_lp_nr = 0;
11692 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11693 pointer_map_destroy (decl_map);
11694 return stmt;
11695}
11696
11697/* Helper function of c_finish_omp_clauses, called via walk_tree.
11698 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11699
11700static tree
11701c_find_omp_placeholder_r (tree *tp, int *, void *data)
11702{
11703 if (*tp == (tree) data)
11704 return *tp;
11705 return NULL_TREE;
11706}
11707
953ff289
DN
11708/* For all elements of CLAUSES, validate them vs OpenMP constraints.
11709 Remove any elements from the list that are invalid. */
11710
11711tree
11712c_finish_omp_clauses (tree clauses)
11713{
11714 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 11715 bitmap_head aligned_head;
953ff289 11716 tree c, t, *pc = &clauses;
acf0174b
JJ
11717 bool branch_seen = false;
11718 bool copyprivate_seen = false;
11719 tree *nowait_clause = NULL;
953ff289
DN
11720
11721 bitmap_obstack_initialize (NULL);
11722 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11723 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11724 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 11725 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
11726
11727 for (pc = &clauses, c = clauses; c ; c = *pc)
11728 {
11729 bool remove = false;
11730 bool need_complete = false;
11731 bool need_implicitly_determined = false;
11732
aaf46ef9 11733 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
11734 {
11735 case OMP_CLAUSE_SHARED:
953ff289
DN
11736 need_implicitly_determined = true;
11737 goto check_dup_generic;
11738
11739 case OMP_CLAUSE_PRIVATE:
953ff289
DN
11740 need_complete = true;
11741 need_implicitly_determined = true;
11742 goto check_dup_generic;
11743
11744 case OMP_CLAUSE_REDUCTION:
953ff289
DN
11745 need_implicitly_determined = true;
11746 t = OMP_CLAUSE_DECL (c);
acf0174b 11747 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
652fea39
JJ
11748 && (FLOAT_TYPE_P (TREE_TYPE (t))
11749 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
953ff289
DN
11750 {
11751 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11752 const char *r_name = NULL;
11753
11754 switch (r_code)
11755 {
11756 case PLUS_EXPR:
11757 case MULT_EXPR:
11758 case MINUS_EXPR:
652fea39 11759 break;
20906c66 11760 case MIN_EXPR:
652fea39
JJ
11761 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11762 r_name = "min";
11763 break;
20906c66 11764 case MAX_EXPR:
652fea39
JJ
11765 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11766 r_name = "max";
953ff289
DN
11767 break;
11768 case BIT_AND_EXPR:
11769 r_name = "&";
11770 break;
11771 case BIT_XOR_EXPR:
11772 r_name = "^";
11773 break;
11774 case BIT_IOR_EXPR:
11775 r_name = "|";
11776 break;
11777 case TRUTH_ANDIF_EXPR:
652fea39
JJ
11778 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11779 r_name = "&&";
953ff289
DN
11780 break;
11781 case TRUTH_ORIF_EXPR:
652fea39
JJ
11782 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11783 r_name = "||";
953ff289
DN
11784 break;
11785 default:
11786 gcc_unreachable ();
11787 }
11788 if (r_name)
11789 {
c2255bc4
AH
11790 error_at (OMP_CLAUSE_LOCATION (c),
11791 "%qE has invalid type for %<reduction(%s)%>",
11792 t, r_name);
953ff289 11793 remove = true;
ee1d5a02 11794 break;
953ff289
DN
11795 }
11796 }
acf0174b
JJ
11797 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11798 {
11799 error_at (OMP_CLAUSE_LOCATION (c),
11800 "user defined reduction not found for %qD", t);
11801 remove = true;
ee1d5a02 11802 break;
acf0174b
JJ
11803 }
11804 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11805 {
11806 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11807 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11808 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11809 VAR_DECL, NULL_TREE, type);
11810 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11811 DECL_ARTIFICIAL (placeholder) = 1;
11812 DECL_IGNORED_P (placeholder) = 1;
11813 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11814 c_mark_addressable (placeholder);
11815 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11816 c_mark_addressable (OMP_CLAUSE_DECL (c));
11817 OMP_CLAUSE_REDUCTION_MERGE (c)
11818 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11819 TREE_VEC_ELT (list, 0),
11820 TREE_VEC_ELT (list, 1),
11821 OMP_CLAUSE_DECL (c), placeholder);
11822 OMP_CLAUSE_REDUCTION_MERGE (c)
11823 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11824 void_type_node, NULL_TREE,
11825 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11826 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11827 if (TREE_VEC_LENGTH (list) == 6)
11828 {
11829 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11830 c_mark_addressable (OMP_CLAUSE_DECL (c));
11831 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11832 c_mark_addressable (placeholder);
11833 tree init = TREE_VEC_ELT (list, 5);
11834 if (init == error_mark_node)
11835 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11836 OMP_CLAUSE_REDUCTION_INIT (c)
11837 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11838 TREE_VEC_ELT (list, 3),
11839 OMP_CLAUSE_DECL (c), placeholder);
11840 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11841 OMP_CLAUSE_REDUCTION_INIT (c)
11842 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11843 OMP_CLAUSE_REDUCTION_INIT (c));
11844 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11845 c_find_omp_placeholder_r,
11846 placeholder, NULL))
11847 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11848 }
11849 else
11850 {
11851 tree init;
11852 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11853 init = build_constructor (TREE_TYPE (t), NULL);
11854 else
11855 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11856 OMP_CLAUSE_REDUCTION_INIT (c)
11857 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11858 }
11859 OMP_CLAUSE_REDUCTION_INIT (c)
11860 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11861 void_type_node, NULL_TREE,
11862 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11863 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11864 }
953ff289
DN
11865 goto check_dup_generic;
11866
11867 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
11868 copyprivate_seen = true;
11869 if (nowait_clause)
11870 {
11871 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11872 "%<nowait%> clause must not be used together "
11873 "with %<copyprivate%>");
11874 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11875 nowait_clause = NULL;
11876 }
953ff289
DN
11877 goto check_dup_generic;
11878
11879 case OMP_CLAUSE_COPYIN:
953ff289
DN
11880 t = OMP_CLAUSE_DECL (c);
11881 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11882 {
c2255bc4
AH
11883 error_at (OMP_CLAUSE_LOCATION (c),
11884 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 11885 remove = true;
ee1d5a02 11886 break;
953ff289
DN
11887 }
11888 goto check_dup_generic;
11889
acf0174b
JJ
11890 case OMP_CLAUSE_LINEAR:
11891 t = OMP_CLAUSE_DECL (c);
11892 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11893 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11894 {
11895 error_at (OMP_CLAUSE_LOCATION (c),
11896 "linear clause applied to non-integral non-pointer "
11897 "variable with type %qT", TREE_TYPE (t));
11898 remove = true;
11899 break;
11900 }
11901 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11902 {
11903 tree s = OMP_CLAUSE_LINEAR_STEP (c);
11904 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
11905 OMP_CLAUSE_DECL (c), s);
11906 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11907 sizetype, s, OMP_CLAUSE_DECL (c));
11908 if (s == error_mark_node)
11909 s = size_one_node;
11910 OMP_CLAUSE_LINEAR_STEP (c) = s;
11911 }
11912 goto check_dup_generic;
11913
953ff289
DN
11914 check_dup_generic:
11915 t = OMP_CLAUSE_DECL (c);
11916 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11917 {
c2255bc4 11918 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
11919 "%qE is not a variable in clause %qs", t,
11920 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
11921 remove = true;
11922 }
11923 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11924 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
11925 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11926 {
c2255bc4
AH
11927 error_at (OMP_CLAUSE_LOCATION (c),
11928 "%qE appears more than once in data clauses", t);
953ff289
DN
11929 remove = true;
11930 }
11931 else
11932 bitmap_set_bit (&generic_head, DECL_UID (t));
11933 break;
11934
11935 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
11936 t = OMP_CLAUSE_DECL (c);
11937 need_complete = true;
11938 need_implicitly_determined = true;
11939 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11940 {
c2255bc4
AH
11941 error_at (OMP_CLAUSE_LOCATION (c),
11942 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
11943 remove = true;
11944 }
11945 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11946 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
11947 {
c2255bc4
AH
11948 error_at (OMP_CLAUSE_LOCATION (c),
11949 "%qE appears more than once in data clauses", t);
953ff289
DN
11950 remove = true;
11951 }
11952 else
11953 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
11954 break;
11955
11956 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
11957 t = OMP_CLAUSE_DECL (c);
11958 need_complete = true;
11959 need_implicitly_determined = true;
11960 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11961 {
c2255bc4
AH
11962 error_at (OMP_CLAUSE_LOCATION (c),
11963 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
11964 remove = true;
11965 }
11966 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11967 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11968 {
c2255bc4
AH
11969 error_at (OMP_CLAUSE_LOCATION (c),
11970 "%qE appears more than once in data clauses", t);
953ff289
DN
11971 remove = true;
11972 }
11973 else
11974 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
11975 break;
11976
acf0174b
JJ
11977 case OMP_CLAUSE_ALIGNED:
11978 t = OMP_CLAUSE_DECL (c);
11979 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11980 {
11981 error_at (OMP_CLAUSE_LOCATION (c),
11982 "%qE is not a variable in %<aligned%> clause", t);
11983 remove = true;
11984 }
5a9785fb
JJ
11985 else if (!POINTER_TYPE_P (TREE_TYPE (t))
11986 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
11987 {
11988 error_at (OMP_CLAUSE_LOCATION (c),
11989 "%qE in %<aligned%> clause is neither a pointer nor "
11990 "an array", t);
11991 remove = true;
11992 }
acf0174b
JJ
11993 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
11994 {
11995 error_at (OMP_CLAUSE_LOCATION (c),
11996 "%qE appears more than once in %<aligned%> clauses",
11997 t);
11998 remove = true;
11999 }
12000 else
12001 bitmap_set_bit (&aligned_head, DECL_UID (t));
12002 break;
12003
12004 case OMP_CLAUSE_DEPEND:
12005 t = OMP_CLAUSE_DECL (c);
12006 if (TREE_CODE (t) == TREE_LIST)
12007 {
12008 if (handle_omp_array_sections (c))
12009 remove = true;
12010 break;
12011 }
12012 if (t == error_mark_node)
12013 remove = true;
12014 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12015 {
12016 error_at (OMP_CLAUSE_LOCATION (c),
12017 "%qE is not a variable in %<depend%> clause", t);
12018 remove = true;
12019 }
12020 else if (!c_mark_addressable (t))
12021 remove = true;
12022 break;
12023
12024 case OMP_CLAUSE_MAP:
12025 case OMP_CLAUSE_TO:
12026 case OMP_CLAUSE_FROM:
12027 t = OMP_CLAUSE_DECL (c);
12028 if (TREE_CODE (t) == TREE_LIST)
12029 {
12030 if (handle_omp_array_sections (c))
12031 remove = true;
12032 else
12033 {
12034 t = OMP_CLAUSE_DECL (c);
b17a8b07 12035 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12036 {
12037 error_at (OMP_CLAUSE_LOCATION (c),
12038 "array section does not have mappable type "
12039 "in %qs clause",
12040 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12041 remove = true;
12042 }
12043 }
12044 break;
12045 }
12046 if (t == error_mark_node)
12047 remove = true;
12048 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12049 {
12050 error_at (OMP_CLAUSE_LOCATION (c),
12051 "%qE is not a variable in %qs clause", t,
12052 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12053 remove = true;
12054 }
12055 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12056 {
12057 error_at (OMP_CLAUSE_LOCATION (c),
12058 "%qD is threadprivate variable in %qs clause", t,
12059 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12060 remove = true;
12061 }
12062 else if (!c_mark_addressable (t))
12063 remove = true;
b17a8b07
TS
12064 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12065 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12066 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12067 {
12068 error_at (OMP_CLAUSE_LOCATION (c),
12069 "%qD does not have a mappable type in %qs clause", t,
12070 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12071 remove = true;
12072 }
12073 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12074 {
12075 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12076 error ("%qD appears more than once in motion clauses", t);
12077 else
12078 error ("%qD appears more than once in map clauses", t);
12079 remove = true;
12080 }
12081 else
12082 bitmap_set_bit (&generic_head, DECL_UID (t));
12083 break;
12084
12085 case OMP_CLAUSE_UNIFORM:
12086 t = OMP_CLAUSE_DECL (c);
12087 if (TREE_CODE (t) != PARM_DECL)
12088 {
12089 if (DECL_P (t))
12090 error_at (OMP_CLAUSE_LOCATION (c),
12091 "%qD is not an argument in %<uniform%> clause", t);
12092 else
12093 error_at (OMP_CLAUSE_LOCATION (c),
12094 "%qE is not an argument in %<uniform%> clause", t);
12095 remove = true;
ee1d5a02 12096 break;
acf0174b 12097 }
ee1d5a02 12098 goto check_dup_generic;
acf0174b
JJ
12099
12100 case OMP_CLAUSE_NOWAIT:
12101 if (copyprivate_seen)
12102 {
12103 error_at (OMP_CLAUSE_LOCATION (c),
12104 "%<nowait%> clause must not be used together "
12105 "with %<copyprivate%>");
12106 remove = true;
12107 break;
12108 }
12109 nowait_clause = pc;
12110 pc = &OMP_CLAUSE_CHAIN (c);
12111 continue;
12112
953ff289
DN
12113 case OMP_CLAUSE_IF:
12114 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12115 case OMP_CLAUSE_NUM_TEAMS:
12116 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12117 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12118 case OMP_CLAUSE_ORDERED:
12119 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12120 case OMP_CLAUSE_UNTIED:
12121 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12122 case OMP_CLAUSE_FINAL:
12123 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12124 case OMP_CLAUSE_SAFELEN:
12125 case OMP_CLAUSE_SIMDLEN:
12126 case OMP_CLAUSE_DEVICE:
12127 case OMP_CLAUSE_DIST_SCHEDULE:
12128 case OMP_CLAUSE_PARALLEL:
12129 case OMP_CLAUSE_FOR:
12130 case OMP_CLAUSE_SECTIONS:
12131 case OMP_CLAUSE_TASKGROUP:
12132 case OMP_CLAUSE_PROC_BIND:
12133 pc = &OMP_CLAUSE_CHAIN (c);
12134 continue;
12135
12136 case OMP_CLAUSE_INBRANCH:
12137 case OMP_CLAUSE_NOTINBRANCH:
12138 if (branch_seen)
12139 {
12140 error_at (OMP_CLAUSE_LOCATION (c),
12141 "%<inbranch%> clause is incompatible with "
12142 "%<notinbranch%>");
12143 remove = true;
12144 break;
12145 }
12146 branch_seen = true;
953ff289
DN
12147 pc = &OMP_CLAUSE_CHAIN (c);
12148 continue;
12149
12150 default:
12151 gcc_unreachable ();
12152 }
12153
12154 if (!remove)
12155 {
12156 t = OMP_CLAUSE_DECL (c);
12157
12158 if (need_complete)
12159 {
12160 t = require_complete_type (t);
12161 if (t == error_mark_node)
12162 remove = true;
12163 }
12164
12165 if (need_implicitly_determined)
12166 {
12167 const char *share_name = NULL;
12168
12169 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12170 share_name = "threadprivate";
12171 else switch (c_omp_predetermined_sharing (t))
12172 {
12173 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12174 break;
12175 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12176 /* const vars may be specified in firstprivate clause. */
12177 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12178 && TREE_READONLY (t))
12179 break;
953ff289
DN
12180 share_name = "shared";
12181 break;
12182 case OMP_CLAUSE_DEFAULT_PRIVATE:
12183 share_name = "private";
12184 break;
12185 default:
12186 gcc_unreachable ();
12187 }
12188 if (share_name)
12189 {
c2255bc4
AH
12190 error_at (OMP_CLAUSE_LOCATION (c),
12191 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12192 t, share_name,
12193 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12194 remove = true;
12195 }
12196 }
12197 }
12198
12199 if (remove)
12200 *pc = OMP_CLAUSE_CHAIN (c);
12201 else
12202 pc = &OMP_CLAUSE_CHAIN (c);
12203 }
12204
12205 bitmap_obstack_release (NULL);
12206 return clauses;
12207}
9ae165a0 12208
0a35513e
AH
12209/* Create a transaction node. */
12210
12211tree
12212c_finish_transaction (location_t loc, tree block, int flags)
12213{
12214 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12215 if (flags & TM_STMT_ATTR_OUTER)
12216 TRANSACTION_EXPR_OUTER (stmt) = 1;
12217 if (flags & TM_STMT_ATTR_RELAXED)
12218 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12219 return add_stmt (stmt);
12220}
12221
9ae165a0
DG
12222/* Make a variant type in the proper way for C/C++, propagating qualifiers
12223 down to the element type of an array. */
12224
12225tree
12226c_build_qualified_type (tree type, int type_quals)
12227{
12228 if (type == error_mark_node)
12229 return type;
12230
12231 if (TREE_CODE (type) == ARRAY_TYPE)
12232 {
12233 tree t;
12234 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12235 type_quals);
12236
12237 /* See if we already have an identically qualified type. */
12238 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12239 {
12240 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12241 && TYPE_NAME (t) == TYPE_NAME (type)
12242 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12243 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12244 TYPE_ATTRIBUTES (type)))
12245 break;
12246 }
12247 if (!t)
12248 {
12249 tree domain = TYPE_DOMAIN (type);
12250
12251 t = build_variant_type_copy (type);
12252 TREE_TYPE (t) = element_type;
12253
12254 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12255 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12256 SET_TYPE_STRUCTURAL_EQUALITY (t);
12257 else if (TYPE_CANONICAL (element_type) != element_type
12258 || (domain && TYPE_CANONICAL (domain) != domain))
12259 {
b8698a0f 12260 tree unqualified_canon
9ae165a0 12261 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12262 domain? TYPE_CANONICAL (domain)
9ae165a0 12263 : NULL_TREE);
b8698a0f 12264 TYPE_CANONICAL (t)
9ae165a0
DG
12265 = c_build_qualified_type (unqualified_canon, type_quals);
12266 }
12267 else
12268 TYPE_CANONICAL (t) = t;
12269 }
12270 return t;
12271 }
12272
12273 /* A restrict-qualified pointer type must be a pointer to object or
12274 incomplete type. Note that the use of POINTER_TYPE_P also allows
12275 REFERENCE_TYPEs, which is appropriate for C++. */
12276 if ((type_quals & TYPE_QUAL_RESTRICT)
12277 && (!POINTER_TYPE_P (type)
12278 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12279 {
12280 error ("invalid use of %<restrict%>");
12281 type_quals &= ~TYPE_QUAL_RESTRICT;
12282 }
12283
12284 return build_qualified_type (type, type_quals);
12285}
72b5577d
ILT
12286
12287/* Build a VA_ARG_EXPR for the C parser. */
12288
12289tree
c2255bc4 12290c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d
ILT
12291{
12292 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12293 warning_at (loc, OPT_Wc___compat,
12294 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12295 return build_va_arg (loc, expr, type);
72b5577d 12296}
acf0174b
JJ
12297
12298/* Return truthvalue of whether T1 is the same tree structure as T2.
12299 Return 1 if they are the same. Return 0 if they are different. */
12300
12301bool
12302c_tree_equal (tree t1, tree t2)
12303{
12304 enum tree_code code1, code2;
12305
12306 if (t1 == t2)
12307 return true;
12308 if (!t1 || !t2)
12309 return false;
12310
12311 for (code1 = TREE_CODE (t1);
12312 CONVERT_EXPR_CODE_P (code1)
12313 || code1 == NON_LVALUE_EXPR;
12314 code1 = TREE_CODE (t1))
12315 t1 = TREE_OPERAND (t1, 0);
12316 for (code2 = TREE_CODE (t2);
12317 CONVERT_EXPR_CODE_P (code2)
12318 || code2 == NON_LVALUE_EXPR;
12319 code2 = TREE_CODE (t2))
12320 t2 = TREE_OPERAND (t2, 0);
12321
12322 /* They might have become equal now. */
12323 if (t1 == t2)
12324 return true;
12325
12326 if (code1 != code2)
12327 return false;
12328
12329 switch (code1)
12330 {
12331 case INTEGER_CST:
12332 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
12333 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
12334
12335 case REAL_CST:
12336 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12337
12338 case STRING_CST:
12339 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12340 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12341 TREE_STRING_LENGTH (t1));
12342
12343 case FIXED_CST:
12344 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12345 TREE_FIXED_CST (t2));
12346
12347 case COMPLEX_CST:
12348 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12349 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12350
12351 case VECTOR_CST:
12352 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12353
12354 case CONSTRUCTOR:
12355 /* We need to do this when determining whether or not two
12356 non-type pointer to member function template arguments
12357 are the same. */
12358 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12359 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12360 return false;
12361 {
12362 tree field, value;
12363 unsigned int i;
12364 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12365 {
12366 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12367 if (!c_tree_equal (field, elt2->index)
12368 || !c_tree_equal (value, elt2->value))
12369 return false;
12370 }
12371 }
12372 return true;
12373
12374 case TREE_LIST:
12375 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12376 return false;
12377 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12378 return false;
12379 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12380
12381 case SAVE_EXPR:
12382 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12383
12384 case CALL_EXPR:
12385 {
12386 tree arg1, arg2;
12387 call_expr_arg_iterator iter1, iter2;
12388 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12389 return false;
12390 for (arg1 = first_call_expr_arg (t1, &iter1),
12391 arg2 = first_call_expr_arg (t2, &iter2);
12392 arg1 && arg2;
12393 arg1 = next_call_expr_arg (&iter1),
12394 arg2 = next_call_expr_arg (&iter2))
12395 if (!c_tree_equal (arg1, arg2))
12396 return false;
12397 if (arg1 || arg2)
12398 return false;
12399 return true;
12400 }
12401
12402 case TARGET_EXPR:
12403 {
12404 tree o1 = TREE_OPERAND (t1, 0);
12405 tree o2 = TREE_OPERAND (t2, 0);
12406
12407 /* Special case: if either target is an unallocated VAR_DECL,
12408 it means that it's going to be unified with whatever the
12409 TARGET_EXPR is really supposed to initialize, so treat it
12410 as being equivalent to anything. */
12411 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12412 && !DECL_RTL_SET_P (o1))
12413 /*Nop*/;
12414 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12415 && !DECL_RTL_SET_P (o2))
12416 /*Nop*/;
12417 else if (!c_tree_equal (o1, o2))
12418 return false;
12419
12420 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12421 }
12422
12423 case COMPONENT_REF:
12424 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12425 return false;
12426 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12427
12428 case PARM_DECL:
12429 case VAR_DECL:
12430 case CONST_DECL:
12431 case FIELD_DECL:
12432 case FUNCTION_DECL:
12433 case IDENTIFIER_NODE:
12434 case SSA_NAME:
12435 return false;
12436
12437 case TREE_VEC:
12438 {
12439 unsigned ix;
12440 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12441 return false;
12442 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12443 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12444 TREE_VEC_ELT (t2, ix)))
12445 return false;
12446 return true;
12447 }
12448
12449 default:
12450 break;
12451 }
12452
12453 switch (TREE_CODE_CLASS (code1))
12454 {
12455 case tcc_unary:
12456 case tcc_binary:
12457 case tcc_comparison:
12458 case tcc_expression:
12459 case tcc_vl_exp:
12460 case tcc_reference:
12461 case tcc_statement:
12462 {
12463 int i, n = TREE_OPERAND_LENGTH (t1);
12464
12465 switch (code1)
12466 {
12467 case PREINCREMENT_EXPR:
12468 case PREDECREMENT_EXPR:
12469 case POSTINCREMENT_EXPR:
12470 case POSTDECREMENT_EXPR:
12471 n = 1;
12472 break;
12473 case ARRAY_REF:
12474 n = 2;
12475 break;
12476 default:
12477 break;
12478 }
12479
12480 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12481 && n != TREE_OPERAND_LENGTH (t2))
12482 return false;
12483
12484 for (i = 0; i < n; ++i)
12485 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12486 return false;
12487
12488 return true;
12489 }
12490
12491 case tcc_type:
12492 return comptypes (t1, t2);
12493 default:
12494 gcc_unreachable ();
12495 }
12496 /* We can get here with --disable-checking. */
12497 return false;
12498}
12893402
BI
12499
12500/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12501 spawn-helper and BODY is the newly created body for FNDECL. */
12502
12503void
12504cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12505{
12506 tree list = alloc_stmt_list ();
12507 tree frame = make_cilk_frame (fndecl);
12508 tree dtor = create_cilk_function_exit (frame, false, true);
12509 add_local_decl (cfun, frame);
12510
12511 DECL_SAVED_TREE (fndecl) = list;
12512 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12513 frame);
12514 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12515 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12516
12517 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12518 append_to_statement_list (detach_expr, &body_list);
12519
12520 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12521 body = fold_build_cleanup_point_expr (void_type_node, body);
12522
12523 append_to_statement_list (body, &body_list);
12524 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12525 body_list, dtor), &list);
12526}