]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
PR c++/88976
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
628fa4f8 1/* Build expressions with type checking for C compiler.
fbd26352 2 Copyright (C) 1987-2019 Free Software Foundation, Inc.
628fa4f8 3
f12b58b3 4This file is part of GCC.
628fa4f8 5
f12b58b3 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
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
f12b58b3 9version.
628fa4f8 10
f12b58b3 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.
628fa4f8 15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
628fa4f8 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,
c857cd60 24 and some optimization. */
628fa4f8 25
26#include "config.h"
405711de 27#include "system.h"
805e22b2 28#include "coretypes.h"
ad7b10a2 29#include "memmodel.h"
4cba6f60 30#include "target.h"
31#include "function.h"
32#include "bitmap.h"
4cba6f60 33#include "c-tree.h"
34#include "gimple-expr.h"
35#include "predict.h"
9ed99284 36#include "stor-layout.h"
37#include "trans-mem.h"
38#include "varasm.h"
39#include "stmt.h"
d1b92730 40#include "langhooks.h"
874993a5 41#include "c-lang.h"
be2828ce 42#include "intl.h"
2363ef00 43#include "tree-iterator.h"
a8783bee 44#include "gimplify.h"
bc7bff74 45#include "tree-inline.h"
4954efd4 46#include "omp-general.h"
6c536c4f 47#include "c-family/c-objc.h"
9e46467d 48#include "c-family/c-ubsan.h"
ca4c3545 49#include "gomp-constants.h"
f0d77991 50#include "spellcheck-tree.h"
c0bf500c 51#include "gcc-rich-location.h"
30a86690 52#include "stringpool.h"
53#include "attribs.h"
9917317a 54#include "asan.h"
2363ef00 55
245d6740 56/* Possible cases of implicit bad conversions. Used to select
57 diagnostic messages in convert_for_assignment. */
58enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_return
63};
64
9823d3a9 65/* The level of nesting inside "__alignof__". */
66int in_alignof;
67
68/* The level of nesting inside "sizeof". */
69int in_sizeof;
70
71/* The level of nesting inside "typeof". */
72int in_typeof;
628fa4f8 73
f003f5dc 74/* The argument of last parsed sizeof expression, only to be tested
75 if expr.original_code == SIZEOF_EXPR. */
76tree c_last_sizeof_arg;
0ca70bfd 77location_t c_last_sizeof_loc;
f003f5dc 78
95cdf3fd 79/* Nonzero if we might need to print a "missing braces around
80 initializer" message within this initializer. */
81static int found_missing_braces;
7de1d3ba 82
16cb78b3 83static int require_constant_value;
84static int require_constant_elements;
85
9f627b1a 86static bool null_pointer_constant_p (const_tree);
5a2784f8 87static tree qualify_type (tree, tree);
ce3765bf 88static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
89 bool *);
d5b637fa 90static int comp_target_types (location_t, tree, tree);
ce3765bf 91static int function_types_compatible_p (const_tree, const_tree, bool *,
92 bool *);
93static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
5a2784f8 94static tree lookup_field (tree, tree);
ec761d5a 95static int convert_arguments (location_t, vec<location_t>, tree,
96 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
97 tree);
eabe2d94 98static tree pointer_diff (location_t, tree, tree, tree *);
22a75734 99static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
16edb1bf 100 enum impl_conv, bool, tree, tree, int,
101 int = 0);
5a2784f8 102static tree valid_compound_expr_initializer (tree, tree);
103static void push_string (const char *);
104static void push_member_name (tree);
5a2784f8 105static int spelling_length (void);
106static char *print_spelling (char *);
d44f2f7c 107static void warning_init (location_t, int, const char *);
e60a6f7b 108static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
623ee358 109static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
adc93f31 110 bool, struct obstack *);
759791ee 111static void output_pending_init_elements (int, struct obstack *);
623ee358 112static bool set_designator (location_t, bool, struct obstack *);
759791ee 113static void push_range_stack (tree, struct obstack *);
d44f2f7c 114static void add_pending_init (location_t, tree, tree, tree, bool,
115 struct obstack *);
759791ee 116static void set_nonincremental_init (struct obstack *);
117static void set_nonincremental_init_from_string (tree, struct obstack *);
118static tree find_init_member (tree, struct obstack *);
d4e60318 119static void readonly_warning (tree, enum lvalue_use);
fdd84b77 120static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
91ffb709 121static void record_maybe_used_decl (tree);
ce3765bf 122static int comptypes_internal (const_tree, const_tree, bool *, bool *);
79f925ed 123\f
124/* Return true if EXP is a null pointer constant, false otherwise. */
125
126static bool
9f627b1a 127null_pointer_constant_p (const_tree expr)
79f925ed 128{
129 /* This should really operate on c_expr structures, but they aren't
130 yet available everywhere required. */
131 tree type = TREE_TYPE (expr);
132 return (TREE_CODE (expr) == INTEGER_CST
ca24357c 133 && !TREE_OVERFLOW (expr)
79f925ed 134 && integer_zerop (expr)
135 && (INTEGRAL_TYPE_P (type)
136 || (TREE_CODE (type) == POINTER_TYPE
137 && VOID_TYPE_P (TREE_TYPE (type))
138 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
139}
a75b1c71 140
141/* EXPR may appear in an unevaluated part of an integer constant
142 expression, but not in an evaluated part. Wrap it in a
143 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
144 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
145
146static tree
147note_integer_operands (tree expr)
148{
149 tree ret;
150 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
151 {
152 ret = copy_node (expr);
153 TREE_OVERFLOW (ret) = 1;
154 }
155 else
156 {
157 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
158 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
159 }
160 return ret;
161}
162
9a8bed72 163/* Having checked whether EXPR may appear in an unevaluated part of an
164 integer constant expression and found that it may, remove any
165 C_MAYBE_CONST_EXPR noting this fact and return the resulting
166 expression. */
167
168static inline tree
169remove_c_maybe_const_expr (tree expr)
170{
171 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
172 return C_MAYBE_CONST_EXPR_EXPR (expr);
173 else
174 return expr;
175}
176
7e58afa1 177\f/* This is a cache to hold if two types are compatible or not. */
178
179struct tagged_tu_seen_cache {
180 const struct tagged_tu_seen_cache * next;
9f627b1a 181 const_tree t1;
182 const_tree t2;
7e58afa1 183 /* The return value of tagged_types_tu_compatible_p if we had seen
184 these two types already. */
185 int val;
186};
187
188static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
189static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
190
22a3f7bd 191/* Do `exp = require_complete_type (loc, exp);' to make sure exp
192 does not have an incomplete type. (That includes void types.)
193 LOC is the location of the use. */
628fa4f8 194
195tree
22a3f7bd 196require_complete_type (location_t loc, tree value)
628fa4f8 197{
198 tree type = TREE_TYPE (value);
199
224bdd51 200 if (error_operand_p (value))
1e4f99e8 201 return error_mark_node;
202
628fa4f8 203 /* First, detect a valid value with a complete type. */
4b72716d 204 if (COMPLETE_TYPE_P (type))
628fa4f8 205 return value;
206
22a3f7bd 207 c_incomplete_type_error (loc, value, type);
628fa4f8 208 return error_mark_node;
209}
210
211/* Print an error message for invalid use of an incomplete type.
212 VALUE is the expression that was used (or 0 if that isn't known)
22a3f7bd 213 and TYPE is the type that was invalid. LOC is the location for
214 the error. */
628fa4f8 215
216void
22a3f7bd 217c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
628fa4f8 218{
628fa4f8 219 /* Avoid duplicate error message. */
220 if (TREE_CODE (type) == ERROR_MARK)
221 return;
222
72749341 223 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
22a3f7bd 224 error_at (loc, "%qD has an incomplete type %qT", value, type);
628fa4f8 225 else
226 {
227 retry:
228 /* We must print an error message. Be clever about what it says. */
229
230 switch (TREE_CODE (type))
231 {
232 case RECORD_TYPE:
628fa4f8 233 case UNION_TYPE:
628fa4f8 234 case ENUMERAL_TYPE:
628fa4f8 235 break;
236
237 case VOID_TYPE:
22a3f7bd 238 error_at (loc, "invalid use of void expression");
628fa4f8 239 return;
240
241 case ARRAY_TYPE:
242 if (TYPE_DOMAIN (type))
243 {
e40a1d5c 244 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
245 {
22a3f7bd 246 error_at (loc, "invalid use of flexible array member");
e40a1d5c 247 return;
248 }
628fa4f8 249 type = TREE_TYPE (type);
250 goto retry;
251 }
22a3f7bd 252 error_at (loc, "invalid use of array with unspecified bounds");
628fa4f8 253 return;
254
255 default:
231bd014 256 gcc_unreachable ();
628fa4f8 257 }
258
259 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
22a3f7bd 260 error_at (loc, "invalid use of undefined type %qT", type);
628fa4f8 261 else
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
22a3f7bd 263 error_at (loc, "invalid use of incomplete typedef %qT", type);
628fa4f8 264 }
265}
266
63c62881 267/* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
269
270tree
2b30d46c 271c_type_promotes_to (tree type)
63c62881 272{
b560fabd 273 tree ret = NULL_TREE;
63c62881 274
b560fabd 275 if (TYPE_MAIN_VARIANT (type) == float_type_node)
276 ret = double_type_node;
277 else if (c_promoting_integer_type_p (type))
63c62881 278 {
279 /* Preserve unsignedness if not really getting any wider. */
78a8ed03 280 if (TYPE_UNSIGNED (type)
a0c938f0 281 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
b560fabd 282 ret = unsigned_type_node;
283 else
284 ret = integer_type_node;
63c62881 285 }
286
b560fabd 287 if (ret != NULL_TREE)
288 return (TYPE_ATOMIC (type)
289 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
290 : ret);
291
63c62881 292 return type;
293}
294
6d5d708e 295/* Return true if between two named address spaces, whether there is a superset
296 named address space that encompasses both address spaces. If there is a
297 superset, return which address space is the superset. */
298
299static bool
300addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
301{
302 if (as1 == as2)
303 {
304 *common = as1;
305 return true;
306 }
307 else if (targetm.addr_space.subset_p (as1, as2))
308 {
309 *common = as2;
310 return true;
311 }
312 else if (targetm.addr_space.subset_p (as2, as1))
313 {
314 *common = as1;
315 return true;
316 }
317 else
318 return false;
319}
320
628fa4f8 321/* Return a variant of TYPE which has all the type qualifiers of LIKE
322 as well as those of TYPE. */
323
324static tree
2b30d46c 325qualify_type (tree type, tree like)
628fa4f8 326{
6d5d708e 327 addr_space_t as_type = TYPE_ADDR_SPACE (type);
328 addr_space_t as_like = TYPE_ADDR_SPACE (like);
329 addr_space_t as_common;
330
331 /* If the two named address spaces are different, determine the common
332 superset address space. If there isn't one, raise an error. */
333 if (!addr_space_superset (as_type, as_like, &as_common))
334 {
335 as_common = as_type;
336 error ("%qT and %qT are in disjoint named address spaces",
337 type, like);
338 }
339
2b30d46c 340 return c_build_qualified_type (type,
6d5d708e 341 TYPE_QUALS_NO_ADDR_SPACE (type)
b560fabd 342 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
6d5d708e 343 | ENCODE_QUAL_ADDR_SPACE (as_common));
628fa4f8 344}
32d33ab2 345
346/* Return true iff the given tree T is a variable length array. */
347
348bool
f8fd23c0 349c_vla_type_p (const_tree t)
32d33ab2 350{
351 if (TREE_CODE (t) == ARRAY_TYPE
352 && C_TYPE_VARIABLE_SIZE (t))
353 return true;
354 return false;
355}
628fa4f8 356\f
1ea7fa60 357/* Return the composite type of two compatible types.
3ac2c7b8 358
1ea7fa60 359 We assume that comptypes has already been done and returned
360 nonzero; if that isn't so, this may crash. In particular, we
361 assume that qualifiers match. */
628fa4f8 362
363tree
1ea7fa60 364composite_type (tree t1, tree t2)
628fa4f8 365{
19cb6b50 366 enum tree_code code1;
367 enum tree_code code2;
8fc98180 368 tree attributes;
628fa4f8 369
370 /* Save time if the two types are the same. */
371
372 if (t1 == t2) return t1;
373
374 /* If one type is nonsense, use the other. */
375 if (t1 == error_mark_node)
376 return t2;
377 if (t2 == error_mark_node)
378 return t1;
379
1ea7fa60 380 code1 = TREE_CODE (t1);
381 code2 = TREE_CODE (t2);
382
0bf60c2b 383 /* Merge the attributes. */
883b2e73 384 attributes = targetm.merge_type_attributes (t1, t2);
8fc98180 385
1ea7fa60 386 /* If one is an enumerated type and the other is the compatible
387 integer type, the composite type might be either of the two
388 (DR#013 question 3). For consistency, use the enumerated type as
389 the composite type. */
628fa4f8 390
1ea7fa60 391 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
392 return t1;
393 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
394 return t2;
1d790105 395
231bd014 396 gcc_assert (code1 == code2);
6b854be5 397
628fa4f8 398 switch (code1)
399 {
628fa4f8 400 case POINTER_TYPE:
1ea7fa60 401 /* For two pointers, do this recursively on the target type. */
628fa4f8 402 {
a5b1863e 403 tree pointed_to_1 = TREE_TYPE (t1);
404 tree pointed_to_2 = TREE_TYPE (t2);
1ea7fa60 405 tree target = composite_type (pointed_to_1, pointed_to_2);
b589b3b4 406 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
5be41d48 407 t1 = build_type_attribute_variant (t1, attributes);
408 return qualify_type (t1, t2);
628fa4f8 409 }
628fa4f8 410
411 case ARRAY_TYPE:
412 {
1ea7fa60 413 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
aebc8537 414 int quals;
415 tree unqual_elt;
a2f96aea 416 tree d1 = TYPE_DOMAIN (t1);
417 tree d2 = TYPE_DOMAIN (t2);
418 bool d1_variable, d2_variable;
419 bool d1_zero, d2_zero;
e5d71db4 420 bool t1_complete, t2_complete;
aebc8537 421
4e7c4787 422 /* We should not have any type quals on arrays at all. */
6d5d708e 423 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
424 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
a0c938f0 425
e5d71db4 426 t1_complete = COMPLETE_TYPE_P (t1);
427 t2_complete = COMPLETE_TYPE_P (t2);
428
72749341 429 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
430 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
a2f96aea 431
432 d1_variable = (!d1_zero
433 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
434 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
435 d2_variable = (!d2_zero
436 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
437 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
32d33ab2 438 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
439 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
a2f96aea 440
628fa4f8 441 /* Save space: see if the result is identical to one of the args. */
a2f96aea 442 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
443 && (d2_variable || d2_zero || !d1_variable))
8fc98180 444 return build_type_attribute_variant (t1, attributes);
a2f96aea 445 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
446 && (d1_variable || d1_zero || !d2_variable))
8fc98180 447 return build_type_attribute_variant (t2, attributes);
a0c938f0 448
4e7c4787 449 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
450 return build_type_attribute_variant (t1, attributes);
451 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
452 return build_type_attribute_variant (t2, attributes);
a0c938f0 453
aebc8537 454 /* Merge the element types, and have a size if either arg has
455 one. We may have qualifiers on the element types. To set
456 up TYPE_MAIN_VARIANT correctly, we need to form the
457 composite of the unqualified types and add the qualifiers
458 back at the end. */
459 quals = TYPE_QUALS (strip_array_types (elt));
460 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
461 t1 = build_array_type (unqual_elt,
a2f96aea 462 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
463 && (d2_variable
464 || d2_zero
465 || !d1_variable))
466 ? t1
467 : t2));
e5d71db4 468 /* Ensure a composite type involving a zero-length array type
469 is a zero-length type not an incomplete type. */
470 if (d1_zero && d2_zero
471 && (t1_complete || t2_complete)
472 && !COMPLETE_TYPE_P (t1))
473 {
474 TYPE_SIZE (t1) = bitsize_zero_node;
475 TYPE_SIZE_UNIT (t1) = size_zero_node;
476 }
aebc8537 477 t1 = c_build_qualified_type (t1, quals);
4e7c4787 478 return build_type_attribute_variant (t1, attributes);
628fa4f8 479 }
480
9369a09f 481 case ENUMERAL_TYPE:
482 case RECORD_TYPE:
483 case UNION_TYPE:
484 if (attributes != NULL)
485 {
486 /* Try harder not to create a new aggregate type. */
487 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
488 return t1;
489 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
490 return t2;
491 }
492 return build_type_attribute_variant (t1, attributes);
493
628fa4f8 494 case FUNCTION_TYPE:
495 /* Function types: prefer the one that specified arg types.
496 If both do, merge the arg types. Also merge the return types. */
497 {
1ea7fa60 498 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
628fa4f8 499 tree p1 = TYPE_ARG_TYPES (t1);
500 tree p2 = TYPE_ARG_TYPES (t2);
501 int len;
502 tree newargs, n;
503 int i;
504
505 /* Save space: see if the result is identical to one of the args. */
84166705 506 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
8fc98180 507 return build_type_attribute_variant (t1, attributes);
84166705 508 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
8fc98180 509 return build_type_attribute_variant (t2, attributes);
628fa4f8 510
511 /* Simple way if one arg fails to specify argument types. */
72749341 512 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
8fc98180 513 {
5be41d48 514 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
515 t1 = build_type_attribute_variant (t1, attributes);
516 return qualify_type (t1, t2);
8fc98180 517 }
72749341 518 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
8fc98180 519 {
520 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
5be41d48 521 t1 = build_type_attribute_variant (t1, attributes);
522 return qualify_type (t1, t2);
8fc98180 523 }
628fa4f8 524
525 /* If both args specify argument types, we must merge the two
526 lists, argument by argument. */
6e11a41e 527
29a514fa 528 for (len = 0, newargs = p1;
529 newargs && newargs != void_list_node;
530 len++, newargs = TREE_CHAIN (newargs))
531 ;
628fa4f8 532
533 for (i = 0; i < len; i++)
7224cf20 534 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
628fa4f8 535
536 n = newargs;
537
29a514fa 538 for (; p1 && p1 != void_list_node;
628fa4f8 539 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
540 {
541 /* A null type means arg type is not specified.
542 Take whatever the other function type has. */
72749341 543 if (TREE_VALUE (p1) == NULL_TREE)
628fa4f8 544 {
545 TREE_VALUE (n) = TREE_VALUE (p2);
546 goto parm_done;
547 }
72749341 548 if (TREE_VALUE (p2) == NULL_TREE)
628fa4f8 549 {
550 TREE_VALUE (n) = TREE_VALUE (p1);
551 goto parm_done;
552 }
2b30d46c 553
628fa4f8 554 /* Given wait (union {union wait *u; int *i} *)
555 and wait (union wait *),
556 prefer union wait * as type of parm. */
557 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
558 && TREE_VALUE (p1) != TREE_VALUE (p2))
559 {
560 tree memb;
c75da5d5 561 tree mv2 = TREE_VALUE (p2);
562 if (mv2 && mv2 != error_mark_node
563 && TREE_CODE (mv2) != ARRAY_TYPE)
564 mv2 = TYPE_MAIN_VARIANT (mv2);
628fa4f8 565 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
1767a056 566 memb; memb = DECL_CHAIN (memb))
c75da5d5 567 {
568 tree mv3 = TREE_TYPE (memb);
569 if (mv3 && mv3 != error_mark_node
570 && TREE_CODE (mv3) != ARRAY_TYPE)
571 mv3 = TYPE_MAIN_VARIANT (mv3);
572 if (comptypes (mv3, mv2))
573 {
574 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
575 TREE_VALUE (p2));
29438999 576 pedwarn (input_location, OPT_Wpedantic,
8864917d 577 "function types not truly compatible in ISO C");
c75da5d5 578 goto parm_done;
579 }
580 }
628fa4f8 581 }
582 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
583 && TREE_VALUE (p2) != TREE_VALUE (p1))
584 {
585 tree memb;
c75da5d5 586 tree mv1 = TREE_VALUE (p1);
587 if (mv1 && mv1 != error_mark_node
588 && TREE_CODE (mv1) != ARRAY_TYPE)
589 mv1 = TYPE_MAIN_VARIANT (mv1);
628fa4f8 590 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
1767a056 591 memb; memb = DECL_CHAIN (memb))
c75da5d5 592 {
593 tree mv3 = TREE_TYPE (memb);
594 if (mv3 && mv3 != error_mark_node
595 && TREE_CODE (mv3) != ARRAY_TYPE)
596 mv3 = TYPE_MAIN_VARIANT (mv3);
597 if (comptypes (mv3, mv1))
598 {
599 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
600 TREE_VALUE (p1));
29438999 601 pedwarn (input_location, OPT_Wpedantic,
8864917d 602 "function types not truly compatible in ISO C");
c75da5d5 603 goto parm_done;
604 }
605 }
628fa4f8 606 }
1ea7fa60 607 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
628fa4f8 608 parm_done: ;
609 }
610
8fc98180 611 t1 = build_function_type (valtype, newargs);
5be41d48 612 t1 = qualify_type (t1, t2);
628fa4f8 613 }
c7afb782 614 /* FALLTHRU */
628fa4f8 615
616 default:
8fc98180 617 return build_type_attribute_variant (t1, attributes);
628fa4f8 618 }
619
620}
1ea7fa60 621
622/* Return the type of a conditional expression between pointers to
623 possibly differently qualified versions of compatible types.
624
625 We assume that comp_target_types has already been done and returned
626 nonzero; if that isn't so, this may crash. */
627
628static tree
629common_pointer_type (tree t1, tree t2)
630{
631 tree attributes;
aebc8537 632 tree pointed_to_1, mv1;
633 tree pointed_to_2, mv2;
1ea7fa60 634 tree target;
36d43c4a 635 unsigned target_quals;
6d5d708e 636 addr_space_t as1, as2, as_common;
637 int quals1, quals2;
1ea7fa60 638
639 /* Save time if the two types are the same. */
640
641 if (t1 == t2) return t1;
642
643 /* If one type is nonsense, use the other. */
644 if (t1 == error_mark_node)
645 return t2;
646 if (t2 == error_mark_node)
647 return t1;
648
231bd014 649 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
a0c938f0 650 && TREE_CODE (t2) == POINTER_TYPE);
1ea7fa60 651
652 /* Merge the attributes. */
653 attributes = targetm.merge_type_attributes (t1, t2);
654
655 /* Find the composite type of the target types, and combine the
aebc8537 656 qualifiers of the two types' targets. Do not lose qualifiers on
657 array element types by taking the TYPE_MAIN_VARIANT. */
658 mv1 = pointed_to_1 = TREE_TYPE (t1);
659 mv2 = pointed_to_2 = TREE_TYPE (t2);
660 if (TREE_CODE (mv1) != ARRAY_TYPE)
661 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
662 if (TREE_CODE (mv2) != ARRAY_TYPE)
663 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
664 target = composite_type (mv1, mv2);
36d43c4a 665
8a8211df 666 /* Strip array types to get correct qualifier for pointers to arrays */
667 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
668 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
669
36d43c4a 670 /* For function types do not merge const qualifiers, but drop them
671 if used inconsistently. The middle-end uses these to mark const
672 and noreturn functions. */
673 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
6d5d708e 674 target_quals = (quals1 & quals2);
36d43c4a 675 else
6d5d708e 676 target_quals = (quals1 | quals2);
677
678 /* If the two named address spaces are different, determine the common
679 superset address space. This is guaranteed to exist due to the
680 assumption that comp_target_type returned non-zero. */
681 as1 = TYPE_ADDR_SPACE (pointed_to_1);
682 as2 = TYPE_ADDR_SPACE (pointed_to_2);
683 if (!addr_space_superset (as1, as2, &as_common))
684 gcc_unreachable ();
685
686 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
687
36d43c4a 688 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
1ea7fa60 689 return build_type_attribute_variant (t1, attributes);
690}
691
692/* Return the common type for two arithmetic types under the usual
693 arithmetic conversions. The default conversions have already been
694 applied, and enumerated types converted to their compatible integer
695 types. The resulting type is unqualified and has no attributes.
696
697 This is the type for the result of most arithmetic operations
698 if the operands have the given two types. */
699
79d37242 700static tree
701c_common_type (tree t1, tree t2)
1ea7fa60 702{
703 enum tree_code code1;
704 enum tree_code code2;
705
706 /* If one type is nonsense, use the other. */
707 if (t1 == error_mark_node)
708 return t2;
709 if (t2 == error_mark_node)
710 return t1;
711
712 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
713 t1 = TYPE_MAIN_VARIANT (t1);
714
715 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
716 t2 = TYPE_MAIN_VARIANT (t2);
717
718 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
719 t1 = build_type_attribute_variant (t1, NULL_TREE);
720
721 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
722 t2 = build_type_attribute_variant (t2, NULL_TREE);
723
724 /* Save time if the two types are the same. */
725
726 if (t1 == t2) return t1;
727
728 code1 = TREE_CODE (t1);
729 code2 = TREE_CODE (t2);
730
231bd014 731 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
9421ebb9 732 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
733 || code1 == INTEGER_TYPE);
231bd014 734 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
9421ebb9 735 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
736 || code2 == INTEGER_TYPE);
1ea7fa60 737
e5d0ba06 738 /* When one operand is a decimal float type, the other operand cannot be
739 a generic float type or a complex type. We also disallow vector types
740 here. */
741 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
742 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
743 {
744 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
745 {
746 error ("can%'t mix operands of decimal float and vector types");
747 return error_mark_node;
748 }
749 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
750 {
751 error ("can%'t mix operands of decimal float and complex types");
752 return error_mark_node;
753 }
754 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
755 {
756 error ("can%'t mix operands of decimal float and other float types");
757 return error_mark_node;
758 }
759 }
760
1ea7fa60 761 /* If one type is a vector type, return that type. (How the usual
762 arithmetic conversions apply to the vector types extension is not
763 precisely specified.) */
764 if (code1 == VECTOR_TYPE)
765 return t1;
766
767 if (code2 == VECTOR_TYPE)
768 return t2;
769
770 /* If one type is complex, form the common type of the non-complex
771 components, then make that complex. Use T1 or T2 if it is the
772 required type. */
773 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
774 {
775 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
776 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
79d37242 777 tree subtype = c_common_type (subtype1, subtype2);
1ea7fa60 778
779 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
780 return t1;
781 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
782 return t2;
783 else
784 return build_complex_type (subtype);
785 }
786
787 /* If only one is real, use it as the result. */
788
789 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
790 return t1;
791
792 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
793 return t2;
794
c4503c0a 795 /* If both are real and either are decimal floating point types, use
796 the decimal floating point type with the greater precision. */
797
798 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
799 {
800 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
801 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
802 return dfloat128_type_node;
803 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
804 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
805 return dfloat64_type_node;
806 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
807 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
808 return dfloat32_type_node;
809 }
810
9421ebb9 811 /* Deal with fixed-point types. */
812 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
813 {
814 unsigned int unsignedp = 0, satp = 0;
3d2b0034 815 scalar_mode m1, m2;
9421ebb9 816 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
817
3d2b0034 818 m1 = SCALAR_TYPE_MODE (t1);
819 m2 = SCALAR_TYPE_MODE (t2);
9421ebb9 820
821 /* If one input type is saturating, the result type is saturating. */
822 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
823 satp = 1;
824
825 /* If both fixed-point types are unsigned, the result type is unsigned.
826 When mixing fixed-point and integer types, follow the sign of the
827 fixed-point type.
828 Otherwise, the result type is signed. */
829 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
830 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
831 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
832 && TYPE_UNSIGNED (t1))
833 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
834 && TYPE_UNSIGNED (t2)))
835 unsignedp = 1;
836
837 /* The result type is signed. */
838 if (unsignedp == 0)
839 {
840 /* If the input type is unsigned, we need to convert to the
841 signed type. */
842 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
843 {
7d339f93 844 enum mode_class mclass = (enum mode_class) 0;
9421ebb9 845 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
846 mclass = MODE_FRACT;
847 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
848 mclass = MODE_ACCUM;
849 else
850 gcc_unreachable ();
3d2b0034 851 m1 = as_a <scalar_mode>
852 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
9421ebb9 853 }
854 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
855 {
7d339f93 856 enum mode_class mclass = (enum mode_class) 0;
9421ebb9 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 ();
3d2b0034 863 m2 = as_a <scalar_mode>
864 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
9421ebb9 865 }
866 }
867
868 if (code1 == FIXED_POINT_TYPE)
869 {
870 fbit1 = GET_MODE_FBIT (m1);
871 ibit1 = GET_MODE_IBIT (m1);
872 }
873 else
874 {
875 fbit1 = 0;
876 /* Signed integers need to subtract one sign bit. */
877 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
878 }
879
880 if (code2 == FIXED_POINT_TYPE)
881 {
882 fbit2 = GET_MODE_FBIT (m2);
883 ibit2 = GET_MODE_IBIT (m2);
884 }
885 else
886 {
887 fbit2 = 0;
888 /* Signed integers need to subtract one sign bit. */
889 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
890 }
891
892 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
893 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
894 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
895 satp);
896 }
897
1ea7fa60 898 /* Both real or both integers; use the one with greater precision. */
899
900 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
901 return t1;
902 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
903 return t2;
904
905 /* Same precision. Prefer long longs to longs to ints when the
906 same precision, following the C99 rules on integer type rank
907 (which are equivalent to the C90 rules for C90 types). */
908
909 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
911 return long_long_unsigned_type_node;
912
913 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
914 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
915 {
916 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
917 return long_long_unsigned_type_node;
918 else
a0c938f0 919 return long_long_integer_type_node;
1ea7fa60 920 }
921
922 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
923 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
924 return long_unsigned_type_node;
925
926 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
927 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
928 {
929 /* But preserve unsignedness from the other type,
930 since long cannot hold all the values of an unsigned int. */
931 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
932 return long_unsigned_type_node;
933 else
934 return long_integer_type_node;
935 }
936
82c85aba 937 /* For floating types of the same TYPE_PRECISION (which we here
938 assume means either the same set of values, or sets of values
939 neither a subset of the other, with behavior being undefined in
940 the latter case), follow the rules from TS 18661-3: prefer
941 interchange types _FloatN, then standard types long double,
942 double, float, then extended types _FloatNx. For extended types,
943 check them starting with _Float128x as that seems most consistent
944 in spirit with preferring long double to double; for interchange
945 types, also check in that order for consistency although it's not
946 possible for more than one of them to have the same
947 precision. */
948 tree mv1 = TYPE_MAIN_VARIANT (t1);
949 tree mv2 = TYPE_MAIN_VARIANT (t2);
950
951 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
952 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
953 return FLOATN_TYPE_NODE (i);
954
1ea7fa60 955 /* Likewise, prefer long double to double even if same size. */
82c85aba 956 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1ea7fa60 957 return long_double_type_node;
958
f8234178 959 /* Likewise, prefer double to float even if same size.
960 We got a couple of embedded targets with 32 bit doubles, and the
961 pdp11 might have 64 bit floats. */
82c85aba 962 if (mv1 == double_type_node || mv2 == double_type_node)
f8234178 963 return double_type_node;
964
82c85aba 965 if (mv1 == float_type_node || mv2 == float_type_node)
966 return float_type_node;
967
968 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
969 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
970 return FLOATNX_TYPE_NODE (i);
971
1ea7fa60 972 /* Otherwise prefer the unsigned one. */
973
974 if (TYPE_UNSIGNED (t1))
975 return t1;
976 else
977 return t2;
978}
628fa4f8 979\f
7422cd7b 980/* Wrapper around c_common_type that is used by c-common.c and other
981 front end optimizations that remove promotions. ENUMERAL_TYPEs
c3a8afb5 982 are allowed here and are converted to their compatible integer types.
983 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
984 preferably a non-Boolean type as the common type. */
79d37242 985tree
986common_type (tree t1, tree t2)
987{
988 if (TREE_CODE (t1) == ENUMERAL_TYPE)
989 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
990 if (TREE_CODE (t2) == ENUMERAL_TYPE)
991 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
c3a8afb5 992
993 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
994 if (TREE_CODE (t1) == BOOLEAN_TYPE
995 && TREE_CODE (t2) == BOOLEAN_TYPE)
996 return boolean_type_node;
997
998 /* If either type is BOOLEAN_TYPE, then return the other. */
999 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1000 return t2;
1001 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1002 return t1;
1003
79d37242 1004 return c_common_type (t1, t2);
1005}
7e58afa1 1006
628fa4f8 1007/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1008 or various other operations. Return 2 if they are compatible
1009 but a warning may be needed if you use them together. */
1010
1011int
3ed275a6 1012comptypes (tree type1, tree type2)
7e58afa1 1013{
1014 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1015 int val;
1016
ce3765bf 1017 val = comptypes_internal (type1, type2, NULL, NULL);
d5b637fa 1018 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1019
1020 return val;
1021}
1022
1023/* Like comptypes, but if it returns non-zero because enum and int are
1024 compatible, it sets *ENUM_AND_INT_P to true. */
1025
1026static int
1027comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1028{
1029 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1030 int val;
1031
ce3765bf 1032 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1033 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1034
1035 return val;
1036}
1037
1038/* Like comptypes, but if it returns nonzero for different types, it
1039 sets *DIFFERENT_TYPES_P to true. */
1040
1041int
1042comptypes_check_different_types (tree type1, tree type2,
1043 bool *different_types_p)
1044{
1045 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1046 int val;
1047
1048 val = comptypes_internal (type1, type2, NULL, different_types_p);
7e58afa1 1049 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
a0c938f0 1050
7e58afa1 1051 return val;
a0c938f0 1052}
1053\f
7e58afa1 1054/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1055 or various other operations. Return 2 if they are compatible
d5b637fa 1056 but a warning may be needed if you use them together. If
1057 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1058 compatible integer type, then this sets *ENUM_AND_INT_P to true;
ce3765bf 1059 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1060 NULL, and the types are compatible but different enough not to be
32074525 1061 permitted in C11 typedef redeclarations, then this sets
ce3765bf 1062 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1063 false, but may or may not be set if the types are incompatible.
1064 This differs from comptypes, in that we don't free the seen
1065 types. */
7e58afa1 1066
1067static int
ce3765bf 1068comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1069 bool *different_types_p)
628fa4f8 1070{
9f627b1a 1071 const_tree t1 = type1;
1072 const_tree t2 = type2;
8fc98180 1073 int attrval, val;
628fa4f8 1074
1075 /* Suppress errors caused by previously reported errors. */
1076
6b4e4ff6 1077 if (t1 == t2 || !t1 || !t2
1078 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
628fa4f8 1079 return 1;
1080
09e0a2a1 1081 /* Enumerated types are compatible with integer types, but this is
1082 not transitive: two enumerated types in the same translation unit
1083 are compatible with each other only if they are the same type. */
628fa4f8 1084
09e0a2a1 1085 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
d5b637fa 1086 {
1087 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
ce3765bf 1088 if (TREE_CODE (t2) != VOID_TYPE)
1089 {
1090 if (enum_and_int_p != NULL)
1091 *enum_and_int_p = true;
1092 if (different_types_p != NULL)
1093 *different_types_p = true;
1094 }
d5b637fa 1095 }
09e0a2a1 1096 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
d5b637fa 1097 {
1098 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
ce3765bf 1099 if (TREE_CODE (t1) != VOID_TYPE)
1100 {
1101 if (enum_and_int_p != NULL)
1102 *enum_and_int_p = true;
1103 if (different_types_p != NULL)
1104 *different_types_p = true;
1105 }
d5b637fa 1106 }
628fa4f8 1107
1108 if (t1 == t2)
1109 return 1;
1110
1111 /* Different classes of types can't be compatible. */
1112
a5d9b222 1113 if (TREE_CODE (t1) != TREE_CODE (t2))
1114 return 0;
628fa4f8 1115
14338e9a 1116 /* Qualifiers must match. C99 6.7.3p9 */
628fa4f8 1117
a5b1863e 1118 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
628fa4f8 1119 return 0;
1120
2201c2d1 1121 /* Allow for two different type nodes which have essentially the same
1122 definition. Note that we already checked for equality of the type
3398e91d 1123 qualifiers (just above). */
628fa4f8 1124
aebc8537 1125 if (TREE_CODE (t1) != ARRAY_TYPE
1126 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
628fa4f8 1127 return 1;
1128
8fc98180 1129 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
309303cf 1130 if (!(attrval = comp_type_attributes (t1, t2)))
8fc98180 1131 return 0;
1132
1133 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1134 val = 0;
1135
628fa4f8 1136 switch (TREE_CODE (t1))
1137 {
e4b92538 1138 case INTEGER_TYPE:
1139 case FIXED_POINT_TYPE:
1140 case REAL_TYPE:
1141 /* With these nodes, we can't determine type equivalence by
1142 looking at what is stored in the nodes themselves, because
1143 two nodes might have different TYPE_MAIN_VARIANTs but still
1144 represent the same type. For example, wchar_t and int could
1145 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1146 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1147 and are distinct types. On the other hand, int and the
1148 following typedef
1149
1150 typedef int INT __attribute((may_alias));
1151
1152 have identical properties, different TYPE_MAIN_VARIANTs, but
1153 represent the same type. The canonical type system keeps
1154 track of equivalence in this case, so we fall back on it. */
1155 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1156
628fa4f8 1157 case POINTER_TYPE:
e4b92538 1158 /* Do not remove mode information. */
1159 if (TYPE_MODE (t1) != TYPE_MODE (t2))
4cb9d5c8 1160 break;
8fc98180 1161 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
d5b637fa 1162 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
ce3765bf 1163 enum_and_int_p, different_types_p));
8fc98180 1164 break;
628fa4f8 1165
1166 case FUNCTION_TYPE:
ce3765bf 1167 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1168 different_types_p);
8fc98180 1169 break;
628fa4f8 1170
1171 case ARRAY_TYPE:
1172 {
628fa4f8 1173 tree d1 = TYPE_DOMAIN (t1);
1174 tree d2 = TYPE_DOMAIN (t2);
2af9221e 1175 bool d1_variable, d2_variable;
1176 bool d1_zero, d2_zero;
8fc98180 1177 val = 1;
628fa4f8 1178
1179 /* Target types must match incl. qualifiers. */
1180 if (TREE_TYPE (t1) != TREE_TYPE (t2)
c9281ef8 1181 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1182 enum_and_int_p,
1183 different_types_p)) == 0)
628fa4f8 1184 return 0;
1185
ce3765bf 1186 if (different_types_p != NULL
72749341 1187 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
ce3765bf 1188 *different_types_p = true;
628fa4f8 1189 /* Sizes must match unless one is missing or variable. */
72749341 1190 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
8fc98180 1191 break;
628fa4f8 1192
84166705 1193 d1_zero = !TYPE_MAX_VALUE (d1);
1194 d2_zero = !TYPE_MAX_VALUE (d2);
2af9221e 1195
84166705 1196 d1_variable = (!d1_zero
2af9221e 1197 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1198 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
84166705 1199 d2_variable = (!d2_zero
2af9221e 1200 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1201 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
32d33ab2 1202 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1203 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
2af9221e 1204
ce3765bf 1205 if (different_types_p != NULL
1206 && d1_variable != d2_variable)
1207 *different_types_p = true;
2af9221e 1208 if (d1_variable || d2_variable)
1209 break;
1210 if (d1_zero && d2_zero)
1211 break;
1212 if (d1_zero || d2_zero
84166705 1213 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1214 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
a0c2c45b 1215 val = 0;
1216
a0c938f0 1217 break;
628fa4f8 1218 }
1219
40109983 1220 case ENUMERAL_TYPE:
34d3c5de 1221 case RECORD_TYPE:
40109983 1222 case UNION_TYPE:
76c16586 1223 if (val != 1 && !same_translation_unit_p (t1, t2))
a0c938f0 1224 {
9369a09f 1225 tree a1 = TYPE_ATTRIBUTES (t1);
1226 tree a2 = TYPE_ATTRIBUTES (t2);
1227
1228 if (! attribute_list_contained (a1, a2)
1229 && ! attribute_list_contained (a2, a1))
1230 break;
1231
7e58afa1 1232 if (attrval != 2)
ce3765bf 1233 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1234 different_types_p);
1235 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1236 different_types_p);
7e58afa1 1237 }
8fc98180 1238 break;
0dbd1c74 1239
5050b2f7 1240 case VECTOR_TYPE:
f08ee65f 1241 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
d5b637fa 1242 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
ce3765bf 1243 enum_and_int_p, different_types_p));
5050b2f7 1244 break;
1245
0dbd1c74 1246 default:
1247 break;
628fa4f8 1248 }
8fc98180 1249 return attrval == 2 && val == 1 ? 2 : val;
628fa4f8 1250}
1251
6d5d708e 1252/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1253 their qualifiers, except for named address spaces. If the pointers point to
1254 different named addresses, then we must determine if one address space is a
1255 subset of the other. */
628fa4f8 1256
1257static int
d5b637fa 1258comp_target_types (location_t location, tree ttl, tree ttr)
628fa4f8 1259{
fadbd30d 1260 int val;
8a8211df 1261 int val_ped;
6d5d708e 1262 tree mvl = TREE_TYPE (ttl);
1263 tree mvr = TREE_TYPE (ttr);
1264 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1265 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1266 addr_space_t as_common;
d5b637fa 1267 bool enum_and_int_p;
ea5fb8f9 1268
6d5d708e 1269 /* Fail if pointers point to incompatible address spaces. */
1270 if (!addr_space_superset (asl, asr, &as_common))
1271 return 0;
1272
8a8211df 1273 /* For pedantic record result of comptypes on arrays before losing
1274 qualifiers on the element type below. */
1275 val_ped = 1;
1276
1277 if (TREE_CODE (mvl) == ARRAY_TYPE
1278 && TREE_CODE (mvr) == ARRAY_TYPE)
1279 val_ped = comptypes (mvl, mvr);
1280
1281 /* Qualifiers on element types of array types that are
1282 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1283
1284 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1285 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1286 : TYPE_MAIN_VARIANT (mvl));
1287
1288 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1289 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1290 : TYPE_MAIN_VARIANT (mvr));
1291
d5b637fa 1292 enum_and_int_p = false;
1293 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
ea5fb8f9 1294
8a8211df 1295 if (val == 1 && val_ped != 1)
1296 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1297 "are incompatible in ISO C");
1298
8864917d 1299 if (val == 2)
29438999 1300 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
d5b637fa 1301
1302 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1303 warning_at (location, OPT_Wc___compat,
1304 "pointer target types incompatible in C++");
1305
628fa4f8 1306 return val;
1307}
1308\f
1309/* Subroutines of `comptypes'. */
1310
393b349a 1311/* Determine whether two trees derive from the same translation unit.
1312 If the CONTEXT chain ends in a null, that tree's context is still
1313 being parsed, so if two trees have context chains ending in null,
76c16586 1314 they're in the same translation unit. */
2823920b 1315
1316bool
9f627b1a 1317same_translation_unit_p (const_tree t1, const_tree t2)
76c16586 1318{
1319 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1320 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1321 {
ce45a448 1322 case tcc_declaration:
1323 t1 = DECL_CONTEXT (t1); break;
1324 case tcc_type:
1325 t1 = TYPE_CONTEXT (t1); break;
1326 case tcc_exceptional:
1327 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
231bd014 1328 default: gcc_unreachable ();
76c16586 1329 }
1330
1331 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1332 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1333 {
ce45a448 1334 case tcc_declaration:
1335 t2 = DECL_CONTEXT (t2); break;
1336 case tcc_type:
1337 t2 = TYPE_CONTEXT (t2); break;
1338 case tcc_exceptional:
1339 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
231bd014 1340 default: gcc_unreachable ();
76c16586 1341 }
1342
1343 return t1 == t2;
1344}
1345
7e58afa1 1346/* Allocate the seen two types, assuming that they are compatible. */
40109983 1347
7e58afa1 1348static struct tagged_tu_seen_cache *
9f627b1a 1349alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
7e58afa1 1350{
a9c6c0e3 1351 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
7e58afa1 1352 tu->next = tagged_tu_seen_base;
1353 tu->t1 = t1;
1354 tu->t2 = t2;
a0c938f0 1355
7e58afa1 1356 tagged_tu_seen_base = tu;
a0c938f0 1357
7e58afa1 1358 /* The C standard says that two structures in different translation
1359 units are compatible with each other only if the types of their
1360 fields are compatible (among other things). We assume that they
1361 are compatible until proven otherwise when building the cache.
1362 An example where this can occur is:
1363 struct a
1364 {
1365 struct a *next;
1366 };
1367 If we are comparing this against a similar struct in another TU,
7063afc3 1368 and did not assume they were compatible, we end up with an infinite
7e58afa1 1369 loop. */
1370 tu->val = 1;
1371 return tu;
1372}
40109983 1373
7e58afa1 1374/* Free the seen types until we get to TU_TIL. */
40109983 1375
7e58afa1 1376static void
1377free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1378{
1379 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1380 while (tu != tu_til)
1381 {
aae87fc3 1382 const struct tagged_tu_seen_cache *const tu1
1383 = (const struct tagged_tu_seen_cache *) tu;
7e58afa1 1384 tu = tu1->next;
e47a6f81 1385 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
7e58afa1 1386 }
1387 tagged_tu_seen_base = tu_til;
1388}
40109983 1389
1390/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1391 compatible. If the two types are not the same (which has been
1392 checked earlier), this can only happen when multiple translation
1393 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
ce3765bf 1394 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1395 comptypes_internal. */
40109983 1396
1397static int
d5b637fa 1398tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
ce3765bf 1399 bool *enum_and_int_p, bool *different_types_p)
40109983 1400{
1401 tree s1, s2;
1402 bool needs_warning = false;
a5d9b222 1403
40109983 1404 /* We have to verify that the tags of the types are the same. This
1405 is harder than it looks because this may be a typedef, so we have
1406 to go look at the original type. It may even be a typedef of a
4ee9c684 1407 typedef...
1408 In the case of compiler-created builtin structs the TYPE_DECL
1409 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dba94932 1410 while (TYPE_NAME (t1)
1411 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1412 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
40109983 1413 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1414
dba94932 1415 while (TYPE_NAME (t2)
1416 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1417 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
40109983 1418 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1419
1420 /* C90 didn't have the requirement that the two tags be the same. */
1421 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1422 return 0;
a5d9b222 1423
40109983 1424 /* C90 didn't say what happened if one or both of the types were
1425 incomplete; we choose to follow C99 rules here, which is that they
1426 are compatible. */
1427 if (TYPE_SIZE (t1) == NULL
1428 || TYPE_SIZE (t2) == NULL)
1429 return 1;
a5d9b222 1430
40109983 1431 {
7e58afa1 1432 const struct tagged_tu_seen_cache * tts_i;
40109983 1433 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1434 if (tts_i->t1 == t1 && tts_i->t2 == t2)
7e58afa1 1435 return tts_i->val;
40109983 1436 }
a5d9b222 1437
40109983 1438 switch (TREE_CODE (t1))
1439 {
1440 case ENUMERAL_TYPE:
1441 {
7e58afa1 1442 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
a0c938f0 1443 /* Speed up the case where the type values are in the same order. */
1444 tree tv1 = TYPE_VALUES (t1);
1445 tree tv2 = TYPE_VALUES (t2);
a5d9b222 1446
a0c938f0 1447 if (tv1 == tv2)
7e58afa1 1448 {
1449 return 1;
1450 }
a5d9b222 1451
a0c938f0 1452 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1453 {
1454 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1455 break;
1456 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
7e58afa1 1457 {
a0c938f0 1458 tu->val = 0;
7e58afa1 1459 return 0;
1460 }
a0c938f0 1461 }
a5d9b222 1462
a0c938f0 1463 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
7e58afa1 1464 {
1465 return 1;
1466 }
a0c938f0 1467 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
7e58afa1 1468 {
1469 tu->val = 0;
1470 return 0;
1471 }
a5d9b222 1472
40109983 1473 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
7e58afa1 1474 {
1475 tu->val = 0;
1476 return 0;
1477 }
a5d9b222 1478
40109983 1479 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1480 {
1481 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1482 if (s2 == NULL
1483 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
7e58afa1 1484 {
1485 tu->val = 0;
1486 return 0;
1487 }
40109983 1488 }
1489 return 1;
1490 }
1491
1492 case UNION_TYPE:
1493 {
7e58afa1 1494 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
40109983 1495 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
7e58afa1 1496 {
1497 tu->val = 0;
1498 return 0;
1499 }
a0c938f0 1500
7e58afa1 1501 /* Speed up the common case where the fields are in the same order. */
1502 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1767a056 1503 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
7e58afa1 1504 {
1505 int result;
a0c938f0 1506
7a07cc13 1507 if (DECL_NAME (s1) != DECL_NAME (s2))
7e58afa1 1508 break;
d5b637fa 1509 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
ce3765bf 1510 enum_and_int_p, different_types_p);
7a07cc13 1511
1512 if (result != 1 && !DECL_NAME (s1))
1513 break;
7e58afa1 1514 if (result == 0)
1515 {
1516 tu->val = 0;
1517 return 0;
1518 }
1519 if (result == 2)
1520 needs_warning = true;
1521
1522 if (TREE_CODE (s1) == FIELD_DECL
1523 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1524 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1525 {
1526 tu->val = 0;
1527 return 0;
1528 }
1529 }
1530 if (!s1 && !s2)
1531 {
1532 tu->val = needs_warning ? 2 : 1;
1533 return tu->val;
1534 }
40109983 1535
1767a056 1536 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
40109983 1537 {
1538 bool ok = false;
a5d9b222 1539
1767a056 1540 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
7a07cc13 1541 if (DECL_NAME (s1) == DECL_NAME (s2))
1542 {
1543 int result;
1544
d5b637fa 1545 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
ce3765bf 1546 enum_and_int_p,
1547 different_types_p);
7a07cc13 1548
1549 if (result != 1 && !DECL_NAME (s1))
1550 continue;
1551 if (result == 0)
1552 {
1553 tu->val = 0;
1554 return 0;
1555 }
1556 if (result == 2)
1557 needs_warning = true;
1558
1559 if (TREE_CODE (s1) == FIELD_DECL
1560 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1561 DECL_FIELD_BIT_OFFSET (s2)) != 1)
40109983 1562 break;
7a07cc13 1563
1564 ok = true;
1565 break;
1566 }
84166705 1567 if (!ok)
7e58afa1 1568 {
1569 tu->val = 0;
1570 return 0;
1571 }
40109983 1572 }
7e58afa1 1573 tu->val = needs_warning ? 2 : 10;
1574 return tu->val;
40109983 1575 }
1576
1577 case RECORD_TYPE:
1578 {
a0c938f0 1579 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
a5d9b222 1580
1581 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
40109983 1582 s1 && s2;
1767a056 1583 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
40109983 1584 {
1585 int result;
1586 if (TREE_CODE (s1) != TREE_CODE (s2)
1587 || DECL_NAME (s1) != DECL_NAME (s2))
1588 break;
d5b637fa 1589 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
ce3765bf 1590 enum_and_int_p, different_types_p);
40109983 1591 if (result == 0)
1592 break;
1593 if (result == 2)
1594 needs_warning = true;
a5d9b222 1595
40109983 1596 if (TREE_CODE (s1) == FIELD_DECL
1597 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1598 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1599 break;
1600 }
40109983 1601 if (s1 && s2)
7e58afa1 1602 tu->val = 0;
1603 else
1604 tu->val = needs_warning ? 2 : 1;
1605 return tu->val;
40109983 1606 }
1607
1608 default:
231bd014 1609 gcc_unreachable ();
40109983 1610 }
1611}
1612
628fa4f8 1613/* Return 1 if two function types F1 and F2 are compatible.
1614 If either type specifies no argument types,
1615 the other must specify a fixed number of self-promoting arg types.
2b30d46c 1616 Otherwise, if one type specifies only the number of arguments,
628fa4f8 1617 the other must specify that number of self-promoting arg types.
d5b637fa 1618 Otherwise, the argument types must match.
ce3765bf 1619 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
628fa4f8 1620
1621static int
d5b637fa 1622function_types_compatible_p (const_tree f1, const_tree f2,
ce3765bf 1623 bool *enum_and_int_p, bool *different_types_p)
628fa4f8 1624{
1625 tree args1, args2;
1626 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1627 int val = 1;
1628 int val1;
fd49c655 1629 tree ret1, ret2;
1630
1631 ret1 = TREE_TYPE (f1);
1632 ret2 = TREE_TYPE (f2);
1633
0bfe2579 1634 /* 'volatile' qualifiers on a function's return type used to mean
1635 the function is noreturn. */
1636 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
21ca8540 1637 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
fd49c655 1638 if (TYPE_VOLATILE (ret1))
1639 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1640 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1641 if (TYPE_VOLATILE (ret2))
1642 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1643 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
ce3765bf 1644 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
fd49c655 1645 if (val == 0)
628fa4f8 1646 return 0;
1647
1648 args1 = TYPE_ARG_TYPES (f1);
1649 args2 = TYPE_ARG_TYPES (f2);
1650
ce3765bf 1651 if (different_types_p != NULL
72749341 1652 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
ce3765bf 1653 *different_types_p = true;
1654
628fa4f8 1655 /* An unspecified parmlist matches any specified parmlist
1656 whose argument types don't need default promotions. */
1657
72749341 1658 if (args1 == NULL_TREE)
628fa4f8 1659 {
1660 if (!self_promoting_args_p (args2))
1661 return 0;
1662 /* If one of these types comes from a non-prototype fn definition,
1663 compare that with the other type's arglist.
6bf97f82 1664 If they don't match, ask for a warning (but no error). */
628fa4f8 1665 if (TYPE_ACTUAL_ARG_TYPES (f1)
c9281ef8 1666 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1667 enum_and_int_p, different_types_p) != 1)
628fa4f8 1668 val = 2;
1669 return val;
1670 }
72749341 1671 if (args2 == NULL_TREE)
628fa4f8 1672 {
1673 if (!self_promoting_args_p (args1))
1674 return 0;
1675 if (TYPE_ACTUAL_ARG_TYPES (f2)
c9281ef8 1676 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1677 enum_and_int_p, different_types_p) != 1)
628fa4f8 1678 val = 2;
1679 return val;
1680 }
1681
1682 /* Both types have argument lists: compare them and propagate results. */
ce3765bf 1683 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1684 different_types_p);
628fa4f8 1685 return val1 != 1 ? val1 : val;
1686}
1687
d5b637fa 1688/* Check two lists of types for compatibility, returning 0 for
1689 incompatible, 1 for compatible, or 2 for compatible with
ce3765bf 1690 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1691 comptypes_internal. */
628fa4f8 1692
1693static int
d5b637fa 1694type_lists_compatible_p (const_tree args1, const_tree args2,
ce3765bf 1695 bool *enum_and_int_p, bool *different_types_p)
628fa4f8 1696{
1697 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1698 int val = 1;
c2879dd7 1699 int newval = 0;
628fa4f8 1700
1701 while (1)
1702 {
aebc8537 1703 tree a1, mv1, a2, mv2;
72749341 1704 if (args1 == NULL_TREE && args2 == NULL_TREE)
628fa4f8 1705 return val;
1706 /* If one list is shorter than the other,
1707 they fail to match. */
72749341 1708 if (args1 == NULL_TREE || args2 == NULL_TREE)
628fa4f8 1709 return 0;
aebc8537 1710 mv1 = a1 = TREE_VALUE (args1);
1711 mv2 = a2 = TREE_VALUE (args2);
1712 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
b560fabd 1713 mv1 = (TYPE_ATOMIC (mv1)
1714 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1715 TYPE_QUAL_ATOMIC)
1716 : TYPE_MAIN_VARIANT (mv1));
aebc8537 1717 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
b560fabd 1718 mv2 = (TYPE_ATOMIC (mv2)
1719 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1720 TYPE_QUAL_ATOMIC)
1721 : TYPE_MAIN_VARIANT (mv2));
628fa4f8 1722 /* A null pointer instead of a type
1723 means there is supposed to be an argument
1724 but nothing is specified about what type it has.
1725 So match anything that self-promotes. */
ce3765bf 1726 if (different_types_p != NULL
72749341 1727 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
ce3765bf 1728 *different_types_p = true;
72749341 1729 if (a1 == NULL_TREE)
628fa4f8 1730 {
aebc8537 1731 if (c_type_promotes_to (a2) != a2)
628fa4f8 1732 return 0;
1733 }
72749341 1734 else if (a2 == NULL_TREE)
628fa4f8 1735 {
aebc8537 1736 if (c_type_promotes_to (a1) != a1)
628fa4f8 1737 return 0;
1738 }
aa76d997 1739 /* If one of the lists has an error marker, ignore this arg. */
aebc8537 1740 else if (TREE_CODE (a1) == ERROR_MARK
1741 || TREE_CODE (a2) == ERROR_MARK)
aa76d997 1742 ;
ce3765bf 1743 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1744 different_types_p)))
628fa4f8 1745 {
ce3765bf 1746 if (different_types_p != NULL)
1747 *different_types_p = true;
628fa4f8 1748 /* Allow wait (union {union wait *u; int *i} *)
1749 and wait (union wait *) to be compatible. */
aebc8537 1750 if (TREE_CODE (a1) == UNION_TYPE
72749341 1751 && (TYPE_NAME (a1) == NULL_TREE
8df5a43d 1752 || TYPE_TRANSPARENT_AGGR (a1))
aebc8537 1753 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1754 && tree_int_cst_equal (TYPE_SIZE (a1),
1755 TYPE_SIZE (a2)))
628fa4f8 1756 {
1757 tree memb;
aebc8537 1758 for (memb = TYPE_FIELDS (a1);
1767a056 1759 memb; memb = DECL_CHAIN (memb))
c75da5d5 1760 {
1761 tree mv3 = TREE_TYPE (memb);
1762 if (mv3 && mv3 != error_mark_node
1763 && TREE_CODE (mv3) != ARRAY_TYPE)
b560fabd 1764 mv3 = (TYPE_ATOMIC (mv3)
1765 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1766 TYPE_QUAL_ATOMIC)
1767 : TYPE_MAIN_VARIANT (mv3));
ce3765bf 1768 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1769 different_types_p))
c75da5d5 1770 break;
1771 }
72749341 1772 if (memb == NULL_TREE)
628fa4f8 1773 return 0;
1774 }
aebc8537 1775 else if (TREE_CODE (a2) == UNION_TYPE
72749341 1776 && (TYPE_NAME (a2) == NULL_TREE
8df5a43d 1777 || TYPE_TRANSPARENT_AGGR (a2))
aebc8537 1778 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1779 && tree_int_cst_equal (TYPE_SIZE (a2),
1780 TYPE_SIZE (a1)))
628fa4f8 1781 {
1782 tree memb;
aebc8537 1783 for (memb = TYPE_FIELDS (a2);
1767a056 1784 memb; memb = DECL_CHAIN (memb))
c75da5d5 1785 {
1786 tree mv3 = TREE_TYPE (memb);
1787 if (mv3 && mv3 != error_mark_node
1788 && TREE_CODE (mv3) != ARRAY_TYPE)
b560fabd 1789 mv3 = (TYPE_ATOMIC (mv3)
1790 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1791 TYPE_QUAL_ATOMIC)
1792 : TYPE_MAIN_VARIANT (mv3));
ce3765bf 1793 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1794 different_types_p))
c75da5d5 1795 break;
1796 }
72749341 1797 if (memb == NULL_TREE)
628fa4f8 1798 return 0;
1799 }
1800 else
1801 return 0;
1802 }
1803
1804 /* comptypes said ok, but record if it said to warn. */
1805 if (newval > val)
1806 val = newval;
1807
1808 args1 = TREE_CHAIN (args1);
1809 args2 = TREE_CHAIN (args2);
1810 }
1811}
628fa4f8 1812\f
d44f2f7c 1813/* Compute the size to increment a pointer by. When a function type or void
1814 type or incomplete type is passed, size_one_node is returned.
1815 This function does not emit any diagnostics; the caller is responsible
1816 for that. */
628fa4f8 1817
91ffb709 1818static tree
9f627b1a 1819c_size_in_bytes (const_tree type)
628fa4f8 1820{
1821 enum tree_code code = TREE_CODE (type);
1822
d44f2f7c 1823 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1824 || !COMPLETE_TYPE_P (type))
902de8ed 1825 return size_one_node;
1826
628fa4f8 1827 /* Convert in case a char is more than one unit. */
389dd41b 1828 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1829 size_int (TYPE_PRECISION (char_type_node)
1830 / BITS_PER_UNIT));
628fa4f8 1831}
628fa4f8 1832\f
628fa4f8 1833/* Return either DECL or its known constant value (if it has one). */
1834
225ec6aa 1835tree
eb2fdcf5 1836decl_constant_value_1 (tree decl, bool in_init)
628fa4f8 1837{
a8f15ac7 1838 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1839 TREE_CODE (decl) != PARM_DECL
84166705 1840 && !TREE_THIS_VOLATILE (decl)
57913890 1841 && TREE_READONLY (decl)
72749341 1842 && DECL_INITIAL (decl) != NULL_TREE
eb2fdcf5 1843 && !error_operand_p (DECL_INITIAL (decl))
628fa4f8 1844 /* This is invalid if initial value is not constant.
1845 If it has either a function call, a memory reference,
1846 or a variable, then re-evaluating it could give different results. */
1847 && TREE_CONSTANT (DECL_INITIAL (decl))
1848 /* Check for cases where this is sub-optimal, even though valid. */
eb2fdcf5 1849 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
628fa4f8 1850 return DECL_INITIAL (decl);
1851 return decl;
1852}
1853
a8f15ac7 1854/* Return either DECL or its known constant value (if it has one).
1855 Like the above, but always return decl outside of functions. */
1856
1857tree
1858decl_constant_value (tree decl)
1859{
1860 /* Don't change a variable array bound or initial value to a constant
1861 in a place where a variable is invalid. */
eb2fdcf5 1862 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
a8f15ac7 1863}
1864
e78cf9a7 1865/* Convert the array expression EXP to a pointer. */
1866static tree
e60a6f7b 1867array_to_pointer_conversion (location_t loc, tree exp)
62827f44 1868{
e78cf9a7 1869 tree orig_exp = exp;
62827f44 1870 tree type = TREE_TYPE (exp);
e78cf9a7 1871 tree adr;
1872 tree restype = TREE_TYPE (type);
1873 tree ptrtype;
62827f44 1874
e78cf9a7 1875 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
62827f44 1876
e78cf9a7 1877 STRIP_TYPE_NOPS (exp);
62827f44 1878
066f26bf 1879 if (TREE_NO_WARNING (orig_exp))
1880 TREE_NO_WARNING (exp) = 1;
62827f44 1881
e78cf9a7 1882 ptrtype = build_pointer_type (restype);
1883
aa3e402e 1884 if (INDIRECT_REF_P (exp))
e78cf9a7 1885 return convert (ptrtype, TREE_OPERAND (exp, 0));
1886
eb5d9b7b 1887 /* In C++ array compound literals are temporary objects unless they are
1888 const or appear in namespace scope, so they are destroyed too soon
1889 to use them for much of anything (c++/53220). */
1890 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1891 {
1892 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1893 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1894 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1895 "converting an array compound literal to a pointer "
1896 "is ill-formed in C++");
1897 }
1898
b99cc6da 1899 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
e78cf9a7 1900 return convert (ptrtype, adr);
1901}
62827f44 1902
e78cf9a7 1903/* Convert the function expression EXP to a pointer. */
1904static tree
e60a6f7b 1905function_to_pointer_conversion (location_t loc, tree exp)
e78cf9a7 1906{
1907 tree orig_exp = exp;
62827f44 1908
e78cf9a7 1909 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
62827f44 1910
e78cf9a7 1911 STRIP_TYPE_NOPS (exp);
62827f44 1912
e78cf9a7 1913 if (TREE_NO_WARNING (orig_exp))
1914 TREE_NO_WARNING (exp) = 1;
62827f44 1915
b99cc6da 1916 return build_unary_op (loc, ADDR_EXPR, exp, false);
e78cf9a7 1917}
62827f44 1918
abc6c64f 1919/* Mark EXP as read, not just set, for set but not used -Wunused
1920 warning purposes. */
1921
1922void
1923mark_exp_read (tree exp)
1924{
1925 switch (TREE_CODE (exp))
1926 {
1927 case VAR_DECL:
1928 case PARM_DECL:
1929 DECL_READ_P (exp) = 1;
1930 break;
1931 case ARRAY_REF:
1932 case COMPONENT_REF:
1933 case MODIFY_EXPR:
1934 case REALPART_EXPR:
1935 case IMAGPART_EXPR:
1936 CASE_CONVERT:
1937 case ADDR_EXPR:
c364d0f3 1938 case VIEW_CONVERT_EXPR:
abc6c64f 1939 mark_exp_read (TREE_OPERAND (exp, 0));
1940 break;
1941 case COMPOUND_EXPR:
062966c1 1942 case C_MAYBE_CONST_EXPR:
abc6c64f 1943 mark_exp_read (TREE_OPERAND (exp, 1));
1944 break;
1945 default:
1946 break;
1947 }
1948}
1949
e78cf9a7 1950/* Perform the default conversion of arrays and functions to pointers.
1951 Return the result of converting EXP. For any other expression, just
e60a6f7b 1952 return EXP.
1953
1954 LOC is the location of the expression. */
e78cf9a7 1955
1956struct c_expr
e60a6f7b 1957default_function_array_conversion (location_t loc, struct c_expr exp)
e78cf9a7 1958{
1959 tree orig_exp = exp.value;
1960 tree type = TREE_TYPE (exp.value);
1961 enum tree_code code = TREE_CODE (type);
1962
1963 switch (code)
1964 {
1965 case ARRAY_TYPE:
1966 {
1967 bool not_lvalue = false;
1968 bool lvalue_array_p;
1969
1970 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
72dd6141 1971 || CONVERT_EXPR_P (exp.value))
e78cf9a7 1972 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1973 {
1974 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1975 not_lvalue = true;
1976 exp.value = TREE_OPERAND (exp.value, 0);
1977 }
1978
1979 if (TREE_NO_WARNING (orig_exp))
1980 TREE_NO_WARNING (exp.value) = 1;
1981
1982 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1983 if (!flag_isoc99 && !lvalue_array_p)
1984 {
1985 /* Before C99, non-lvalue arrays do not decay to pointers.
1986 Normally, using such an array would be invalid; but it can
1987 be used correctly inside sizeof or as a statement expression.
1988 Thus, do not give an error here; an error will result later. */
1989 return exp;
1990 }
1991
e60a6f7b 1992 exp.value = array_to_pointer_conversion (loc, exp.value);
e78cf9a7 1993 }
1994 break;
1995 case FUNCTION_TYPE:
e60a6f7b 1996 exp.value = function_to_pointer_conversion (loc, exp.value);
e78cf9a7 1997 break;
1998 default:
e78cf9a7 1999 break;
62827f44 2000 }
e78cf9a7 2001
62827f44 2002 return exp;
2003}
2004
abc6c64f 2005struct c_expr
2006default_function_array_read_conversion (location_t loc, struct c_expr exp)
2007{
2008 mark_exp_read (exp.value);
2009 return default_function_array_conversion (loc, exp);
2010}
b96dc121 2011
b560fabd 2012/* Return whether EXPR should be treated as an atomic lvalue for the
2013 purposes of load and store handling. */
2014
2015static bool
2016really_atomic_lvalue (tree expr)
2017{
224bdd51 2018 if (error_operand_p (expr))
b560fabd 2019 return false;
2020 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2021 return false;
2022 if (!lvalue_p (expr))
2023 return false;
2024
2025 /* Ignore _Atomic on register variables, since their addresses can't
2026 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2027 sequences wouldn't work. Ignore _Atomic on structures containing
2028 bit-fields, since accessing elements of atomic structures or
2029 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2030 it's undefined at translation time or execution time, and the
2031 normal atomic sequences again wouldn't work. */
2032 while (handled_component_p (expr))
2033 {
2034 if (TREE_CODE (expr) == COMPONENT_REF
2035 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2036 return false;
2037 expr = TREE_OPERAND (expr, 0);
2038 }
2039 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2040 return false;
2041 return true;
2042}
2043
2044/* Convert expression EXP (location LOC) from lvalue to rvalue,
2045 including converting functions and arrays to pointers if CONVERT_P.
2046 If READ_P, also mark the expression as having been read. */
2047
2048struct c_expr
2049convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2050 bool convert_p, bool read_p)
2051{
2052 if (read_p)
2053 mark_exp_read (exp.value);
2054 if (convert_p)
2055 exp = default_function_array_conversion (loc, exp);
2056 if (really_atomic_lvalue (exp.value))
2057 {
2058 vec<tree, va_gc> *params;
2059 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2060 tree expr_type = TREE_TYPE (exp.value);
b99cc6da 2061 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
b560fabd 2062 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2063
2064 gcc_assert (TYPE_ATOMIC (expr_type));
2065
2066 /* Expansion of a generic atomic load may require an addition
2067 element, so allocate enough to prevent a resize. */
2068 vec_alloc (params, 4);
2069
2070 /* Remove the qualifiers for the rest of the expressions and
2071 create the VAL temp variable to hold the RHS. */
2072 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
0cd6b1b4 2073 tmp = create_tmp_var_raw (nonatomic_type);
b99cc6da 2074 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
b560fabd 2075 TREE_ADDRESSABLE (tmp) = 1;
977b1853 2076 TREE_NO_WARNING (tmp) = 1;
b560fabd 2077
2078 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2079 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2080 params->quick_push (expr_addr);
2081 params->quick_push (tmp_addr);
2082 params->quick_push (seq_cst);
5a672e62 2083 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
b560fabd 2084
977b1853 2085 /* EXPR is always read. */
2086 mark_exp_read (exp.value);
b560fabd 2087
2088 /* Return tmp which contains the value loaded. */
0cd6b1b4 2089 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2090 NULL_TREE, NULL_TREE);
b560fabd 2091 }
2092 return exp;
2093}
2094
b96dc121 2095/* EXP is an expression of integer type. Apply the integer promotions
2096 to it and return the promoted value. */
628fa4f8 2097
2098tree
b96dc121 2099perform_integral_promotions (tree exp)
628fa4f8 2100{
19cb6b50 2101 tree type = TREE_TYPE (exp);
2102 enum tree_code code = TREE_CODE (type);
628fa4f8 2103
b96dc121 2104 gcc_assert (INTEGRAL_TYPE_P (type));
98821063 2105
628fa4f8 2106 /* Normally convert enums to int,
2107 but convert wide enums to something wider. */
2108 if (code == ENUMERAL_TYPE)
2109 {
771d21fa 2110 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2111 TYPE_PRECISION (integer_type_node)),
2112 ((TYPE_PRECISION (type)
2113 >= TYPE_PRECISION (integer_type_node))
78a8ed03 2114 && TYPE_UNSIGNED (type)));
a0c2c45b 2115
628fa4f8 2116 return convert (type, exp);
2117 }
2118
b96dc121 2119 /* ??? This should no longer be needed now bit-fields have their
2120 proper types. */
3486c3bf 2121 if (TREE_CODE (exp) == COMPONENT_REF
a0c2c45b 2122 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
ff552418 2123 /* If it's thinner than an int, promote it like a
d7aeef06 2124 c_promoting_integer_type_p, otherwise leave it alone. */
c9281ef8 2125 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2126 TYPE_PRECISION (integer_type_node)) < 0)
455730ef 2127 return convert (integer_type_node, exp);
3486c3bf 2128
d7aeef06 2129 if (c_promoting_integer_type_p (type))
628fa4f8 2130 {
455730ef 2131 /* Preserve unsignedness if not really getting any wider. */
78a8ed03 2132 if (TYPE_UNSIGNED (type)
455730ef 2133 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
628fa4f8 2134 return convert (unsigned_type_node, exp);
a0c2c45b 2135
628fa4f8 2136 return convert (integer_type_node, exp);
2137 }
a0c2c45b 2138
b96dc121 2139 return exp;
2140}
2141
2142
2143/* Perform default promotions for C data used in expressions.
f14c8207 2144 Enumeral types or short or char are converted to int.
b96dc121 2145 In addition, manifest constants symbols are replaced by their values. */
2146
2147tree
2148default_conversion (tree exp)
2149{
2150 tree orig_exp;
2151 tree type = TREE_TYPE (exp);
2152 enum tree_code code = TREE_CODE (type);
2b3c93a3 2153 tree promoted_type;
b96dc121 2154
abc6c64f 2155 mark_exp_read (exp);
2156
f14c8207 2157 /* Functions and arrays have been converted during parsing. */
2158 gcc_assert (code != FUNCTION_TYPE);
2159 if (code == ARRAY_TYPE)
2160 return exp;
b96dc121 2161
2162 /* Constants can be used directly unless they're not loadable. */
2163 if (TREE_CODE (exp) == CONST_DECL)
2164 exp = DECL_INITIAL (exp);
2165
b96dc121 2166 /* Strip no-op conversions. */
2167 orig_exp = exp;
2168 STRIP_TYPE_NOPS (exp);
2169
2170 if (TREE_NO_WARNING (orig_exp))
2171 TREE_NO_WARNING (exp) = 1;
2172
628fa4f8 2173 if (code == VOID_TYPE)
2174 {
d44f2f7c 2175 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2176 "void value not ignored as it ought to be");
628fa4f8 2177 return error_mark_node;
2178 }
f831c98f 2179
22a3f7bd 2180 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
f831c98f 2181 if (exp == error_mark_node)
2182 return error_mark_node;
2183
2b3c93a3 2184 promoted_type = targetm.promoted_type (type);
2185 if (promoted_type)
2186 return convert (promoted_type, exp);
2187
f831c98f 2188 if (INTEGRAL_TYPE_P (type))
2189 return perform_integral_promotions (exp);
2190
628fa4f8 2191 return exp;
2192}
2193\f
10064fb9 2194/* Look up COMPONENT in a structure or union TYPE.
9303bbca 2195
2196 If the component name is not found, returns NULL_TREE. Otherwise,
2197 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2198 stepping down the chain to the component, which is in the last
2199 TREE_VALUE of the list. Normally the list is of length one, but if
2200 the component is embedded within (nested) anonymous structures or
2201 unions, the list steps down the chain to the component. */
2b30d46c 2202
bdbd2f6d 2203static tree
10064fb9 2204lookup_field (tree type, tree component)
bdbd2f6d 2205{
2206 tree field;
2207
2208 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2209 to the field elements. Use a binary search on this array to quickly
2210 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
40eafc9b 2211 will always be set for structures which have many elements.
bdbd2f6d 2212
40eafc9b 2213 Duplicate field checking replaces duplicates with NULL_TREE so
2214 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2215 case just iterate using DECL_CHAIN. */
2216
2217 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2218 && !seen_error ())
bdbd2f6d 2219 {
2220 int bot, top, half;
860251be 2221 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
bdbd2f6d 2222
2223 field = TYPE_FIELDS (type);
2224 bot = 0;
860251be 2225 top = TYPE_LANG_SPECIFIC (type)->s->len;
bdbd2f6d 2226 while (top - bot > 1)
2227 {
bdbd2f6d 2228 half = (top - bot + 1) >> 1;
2229 field = field_array[bot+half];
2230
2231 if (DECL_NAME (field) == NULL_TREE)
2232 {
2233 /* Step through all anon unions in linear fashion. */
2234 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2235 {
bdbd2f6d 2236 field = field_array[bot++];
ef61516b 2237 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
37c005bb 2238 {
10064fb9 2239 tree anon = lookup_field (TREE_TYPE (field), component);
9303bbca 2240
2241 if (anon)
2242 return tree_cons (NULL_TREE, field, anon);
2fdec027 2243
2244 /* The Plan 9 compiler permits referring
2245 directly to an anonymous struct/union field
2246 using a typedef name. */
2247 if (flag_plan9_extensions
2248 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2249 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2250 == TYPE_DECL)
2251 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2252 == component))
2253 break;
2b30d46c 2254 }
bdbd2f6d 2255 }
2256
2257 /* Entire record is only anon unions. */
2258 if (bot > top)
2259 return NULL_TREE;
2260
2261 /* Restart the binary search, with new lower bound. */
2262 continue;
2263 }
2264
d3016716 2265 if (DECL_NAME (field) == component)
bdbd2f6d 2266 break;
d3016716 2267 if (DECL_NAME (field) < component)
bdbd2f6d 2268 bot += half;
2269 else
2270 top = bot + half;
2271 }
2272
2273 if (DECL_NAME (field_array[bot]) == component)
2274 field = field_array[bot];
2275 else if (DECL_NAME (field) != component)
9303bbca 2276 return NULL_TREE;
bdbd2f6d 2277 }
2278 else
2279 {
1767a056 2280 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
bdbd2f6d 2281 {
9303bbca 2282 if (DECL_NAME (field) == NULL_TREE
ef61516b 2283 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
bdbd2f6d 2284 {
10064fb9 2285 tree anon = lookup_field (TREE_TYPE (field), component);
f9d98a50 2286
9303bbca 2287 if (anon)
2288 return tree_cons (NULL_TREE, field, anon);
2fdec027 2289
2290 /* The Plan 9 compiler permits referring directly to an
2291 anonymous struct/union field using a typedef
2292 name. */
2293 if (flag_plan9_extensions
2294 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2295 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2296 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2297 == component))
2298 break;
bdbd2f6d 2299 }
2300
2301 if (DECL_NAME (field) == component)
2302 break;
2303 }
9303bbca 2304
2305 if (field == NULL_TREE)
2306 return NULL_TREE;
bdbd2f6d 2307 }
2308
9303bbca 2309 return tree_cons (NULL_TREE, field, NULL_TREE);
bdbd2f6d 2310}
2311
b67f0560 2312/* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2313
2314static void
2315lookup_field_fuzzy_find_candidates (tree type, tree component,
2316 vec<tree> *candidates)
2317{
2318 tree field;
2319 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2320 {
2321 if (DECL_NAME (field) == NULL_TREE
ef61516b 2322 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2323 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2324 candidates);
b67f0560 2325
2326 if (DECL_NAME (field))
2327 candidates->safe_push (DECL_NAME (field));
2328 }
2329}
2330
2331/* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2332 rather than returning a TREE_LIST for an exact match. */
2333
2334static tree
2335lookup_field_fuzzy (tree type, tree component)
2336{
2337 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2338
2339 /* First, gather a list of candidates. */
2340 auto_vec <tree> candidates;
2341
2342 lookup_field_fuzzy_find_candidates (type, component,
2343 &candidates);
2344
11d8dd6f 2345 return find_closest_identifier (component, &candidates);
b67f0560 2346}
2347
734caf84 2348/* Support function for build_component_ref's error-handling.
2349
2350 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2351 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2352
2353static bool
2354should_suggest_deref_p (tree datum_type)
2355{
2356 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2357 allows "." for ptrs; we could be handling a failed attempt
2358 to access a property. */
2359 if (c_dialect_objc ())
2360 return false;
2361
2362 /* Only suggest it for pointers... */
2363 if (TREE_CODE (datum_type) != POINTER_TYPE)
2364 return false;
2365
2366 /* ...to structs/unions. */
2367 tree underlying_type = TREE_TYPE (datum_type);
2368 enum tree_code code = TREE_CODE (underlying_type);
2369 if (code == RECORD_TYPE || code == UNION_TYPE)
2370 return true;
2371 else
2372 return false;
2373}
2374
e60a6f7b 2375/* Make an expression to refer to the COMPONENT field of structure or
2376 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
7cfde92a 2377 location of the COMPONENT_REF. COMPONENT_LOC is the location
2378 of COMPONENT. */
628fa4f8 2379
2380tree
7cfde92a 2381build_component_ref (location_t loc, tree datum, tree component,
2382 location_t component_loc)
628fa4f8 2383{
19cb6b50 2384 tree type = TREE_TYPE (datum);
2385 enum tree_code code = TREE_CODE (type);
2386 tree field = NULL;
2387 tree ref;
7666d572 2388 bool datum_lvalue = lvalue_p (datum);
628fa4f8 2389
43bf5d72 2390 if (!objc_is_public (datum, component))
2391 return error_mark_node;
2392
9d9f5bb3 2393 /* Detect Objective-C property syntax object.property. */
86c110ac 2394 if (c_dialect_objc ()
9d9f5bb3 2395 && (ref = objc_maybe_build_component_ref (datum, component)))
86c110ac 2396 return ref;
2397
628fa4f8 2398 /* See if there is a field or component with name COMPONENT. */
2399
2400 if (code == RECORD_TYPE || code == UNION_TYPE)
2401 {
4b72716d 2402 if (!COMPLETE_TYPE_P (type))
628fa4f8 2403 {
22a3f7bd 2404 c_incomplete_type_error (loc, NULL_TREE, type);
628fa4f8 2405 return error_mark_node;
2406 }
2407
10064fb9 2408 field = lookup_field (type, component);
628fa4f8 2409
2410 if (!field)
2411 {
b67f0560 2412 tree guessed_id = lookup_field_fuzzy (type, component);
2413 if (guessed_id)
7cfde92a 2414 {
2415 /* Attempt to provide a fixit replacement hint, if
2416 we have a valid range for the component. */
2417 location_t reported_loc
2418 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2a990300 2419 gcc_rich_location rich_loc (reported_loc);
7cfde92a 2420 if (component_loc != UNKNOWN_LOCATION)
2a990300 2421 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
3b6578b3 2422 error_at (&rich_loc,
2423 "%qT has no member named %qE; did you mean %qE?",
2424 type, component, guessed_id);
7cfde92a 2425 }
b67f0560 2426 else
2427 error_at (loc, "%qT has no member named %qE", type, component);
628fa4f8 2428 return error_mark_node;
2429 }
628fa4f8 2430
37e2d32f 2431 /* Accessing elements of atomic structures or unions is undefined
2432 behavior (C11 6.5.2.3#5). */
2433 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2434 {
2435 if (code == RECORD_TYPE)
2436 warning_at (loc, 0, "accessing a member %qE of an atomic "
2437 "structure %qE", component, datum);
2438 else
2439 warning_at (loc, 0, "accessing a member %qE of an atomic "
2440 "union %qE", component, datum);
2441 }
2442
9303bbca 2443 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2444 This might be better solved in future the way the C++ front
2445 end does it - by giving the anonymous entities each a
2446 separate name and type, and then have build_component_ref
2447 recursively call itself. We can't do that here. */
04416275 2448 do
37c005bb 2449 {
9303bbca 2450 tree subdatum = TREE_VALUE (field);
62e58612 2451 int quals;
2452 tree subtype;
7666d572 2453 bool use_datum_quals;
9303bbca 2454
2455 if (TREE_TYPE (subdatum) == error_mark_node)
2456 return error_mark_node;
2457
7666d572 2458 /* If this is an rvalue, it does not have qualifiers in C
2459 standard terms and we must avoid propagating such
2460 qualifiers down to a non-lvalue array that is then
2461 converted to a pointer. */
2462 use_datum_quals = (datum_lvalue
2463 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2464
62e58612 2465 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
7666d572 2466 if (use_datum_quals)
2467 quals |= TYPE_QUALS (TREE_TYPE (datum));
62e58612 2468 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2469
2470 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
14ae0310 2471 NULL_TREE);
e60a6f7b 2472 SET_EXPR_LOCATION (ref, loc);
7666d572 2473 if (TREE_READONLY (subdatum)
2474 || (use_datum_quals && TREE_READONLY (datum)))
37c005bb 2475 TREE_READONLY (ref) = 1;
7666d572 2476 if (TREE_THIS_VOLATILE (subdatum)
2477 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
37c005bb 2478 TREE_THIS_VOLATILE (ref) = 1;
88da234d 2479
2480 if (TREE_DEPRECATED (subdatum))
45c4e798 2481 warn_deprecated_use (subdatum, NULL_TREE);
88da234d 2482
37c005bb 2483 datum = ref;
04416275 2484
2485 field = TREE_CHAIN (field);
37c005bb 2486 }
04416275 2487 while (field);
37c005bb 2488
628fa4f8 2489 return ref;
2490 }
734caf84 2491 else if (should_suggest_deref_p (type))
2492 {
2493 /* Special-case the error message for "ptr.field" for the case
2494 where the user has confused "." vs "->". */
2495 rich_location richloc (line_table, loc);
2496 /* "loc" should be the "." token. */
850c2009 2497 richloc.add_fixit_replace ("->");
3b6578b3 2498 error_at (&richloc,
2499 "%qE is a pointer; did you mean to use %<->%>?",
2500 datum);
734caf84 2501 return error_mark_node;
2502 }
628fa4f8 2503 else if (code != ERROR_MARK)
e60a6f7b 2504 error_at (loc,
2505 "request for member %qE in something not a structure or union",
2506 component);
628fa4f8 2507
2508 return error_mark_node;
2509}
2510\f
2511/* Given an expression PTR for a pointer, return an expression
2512 for the value pointed to.
dda49785 2513 ERRORSTRING is the name of the operator to appear in error messages.
2514
2515 LOC is the location to use for the generated tree. */
628fa4f8 2516
2517tree
f08923b3 2518build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
628fa4f8 2519{
19cb6b50 2520 tree pointer = default_conversion (ptr);
2521 tree type = TREE_TYPE (pointer);
dda49785 2522 tree ref;
628fa4f8 2523
2524 if (TREE_CODE (type) == POINTER_TYPE)
bfbc4d3a 2525 {
72dd6141 2526 if (CONVERT_EXPR_P (pointer)
e6fa0ea6 2527 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2528 {
2529 /* If a warning is issued, mark it to avoid duplicates from
2530 the backend. This only needs to be done at
2531 warn_strict_aliasing > 2. */
2532 if (warn_strict_aliasing > 2)
d9757bb6 2533 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
e6fa0ea6 2534 type, TREE_OPERAND (pointer, 0)))
2535 TREE_NO_WARNING (pointer) = 1;
2536 }
2537
bfbc4d3a 2538 if (TREE_CODE (pointer) == ADDR_EXPR
bfbc4d3a 2539 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2540 == TREE_TYPE (type)))
dda49785 2541 {
2542 ref = TREE_OPERAND (pointer, 0);
2543 protected_set_expr_location (ref, loc);
2544 return ref;
2545 }
bfbc4d3a 2546 else
2547 {
2548 tree t = TREE_TYPE (type);
aebc8537 2549
ca136233 2550 ref = build1 (INDIRECT_REF, t, pointer);
628fa4f8 2551
fe2c198f 2552 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
bfbc4d3a 2553 {
d9c77450 2554 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2555 {
2556 error_at (loc, "dereferencing pointer to incomplete type "
2557 "%qT", t);
2558 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2559 }
bfbc4d3a 2560 return error_mark_node;
2561 }
48d94ede 2562 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
b6889cb0 2563 warning_at (loc, 0, "dereferencing %<void *%> pointer");
bfbc4d3a 2564
2565 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2566 so that we get the proper error message if the result is used
2567 to assign to. Also, &* is supposed to be a no-op.
2568 And ANSI C seems to specify that the type of the result
2569 should be the const type. */
2570 /* A de-reference of a pointer to const is not a const. It is valid
2571 to change it via some other pointer. */
2572 TREE_READONLY (ref) = TYPE_READONLY (t);
2573 TREE_SIDE_EFFECTS (ref)
c1771f2c 2574 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
618c9975 2575 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
dda49785 2576 protected_set_expr_location (ref, loc);
bfbc4d3a 2577 return ref;
2578 }
2579 }
628fa4f8 2580 else if (TREE_CODE (pointer) != ERROR_MARK)
b1bbc8e5 2581 invalid_indirection_error (loc, type, errstring);
2582
628fa4f8 2583 return error_mark_node;
2584}
2585
2586/* This handles expressions of the form "a[i]", which denotes
2587 an array reference.
2588
2589 This is logically equivalent in C to *(a+i), but we may do it differently.
2590 If A is a variable or a member, we generate a primitive ARRAY_REF.
2591 This avoids forcing the array out of registers, and can work on
2592 arrays that are not lvalues (for example, members of structures returned
dda49785 2593 by functions).
2594
93426222 2595 For vector types, allow vector[i] but not i[vector], and create
2596 *(((type*)&vectortype) + i) for the expression.
2597
dda49785 2598 LOC is the location to use for the returned expression. */
628fa4f8 2599
2600tree
e60a6f7b 2601build_array_ref (location_t loc, tree array, tree index)
628fa4f8 2602{
dda49785 2603 tree ret;
20931c08 2604 bool swapped = false;
628fa4f8 2605 if (TREE_TYPE (array) == error_mark_node
2606 || TREE_TYPE (index) == error_mark_node)
2607 return error_mark_node;
2608
20931c08 2609 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
93426222 2610 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2611 /* Allow vector[index] but not index[vector]. */
6290f0db 2612 && !VECTOR_TYPE_P (TREE_TYPE (array)))
628fa4f8 2613 {
20931c08 2614 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2615 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
c4f6b7e5 2616 {
6cf89e04 2617 error_at (loc,
93426222 2618 "subscripted value is neither array nor pointer nor vector");
2619
c4f6b7e5 2620 return error_mark_node;
2621 }
dfcf26a5 2622 std::swap (array, index);
20931c08 2623 swapped = true;
2624 }
2625
2626 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2627 {
3bc1df45 2628 error_at (loc, "array subscript is not an integer");
20931c08 2629 return error_mark_node;
2630 }
2631
2632 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2633 {
3bc1df45 2634 error_at (loc, "subscripted value is pointer to function");
20931c08 2635 return error_mark_node;
2636 }
2637
2840aae4 2638 /* ??? Existing practice has been to warn only when the char
2639 index is syntactically the index, not for char[array]. */
2640 if (!swapped)
92b63884 2641 warn_array_subscript_with_type_char (loc, index);
20931c08 2642
2643 /* Apply default promotions *after* noticing character types. */
2644 index = default_conversion (index);
a2bbd0a7 2645 if (index == error_mark_node)
2646 return error_mark_node;
20931c08 2647
2648 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
6cf89e04 2649
7345b977 2650 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2651 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
20931c08 2652
2653 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2654 {
8e31b4b2 2655 tree rval, type;
c4f6b7e5 2656
628fa4f8 2657 /* An array that is indexed by a non-constant
2658 cannot be stored in a register; we must be able to do
2659 address arithmetic on its address.
2660 Likewise an array of elements of variable size. */
2661 if (TREE_CODE (index) != INTEGER_CST
4b72716d 2662 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
628fa4f8 2663 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2664 {
3385a573 2665 if (!c_mark_addressable (array, true))
628fa4f8 2666 return error_mark_node;
2667 }
754b3ff5 2668 /* An array that is indexed by a constant value which is not within
2669 the array bounds cannot be stored in a register either; because we
2670 would get a crash in store_bit_field/extract_bit_field when trying
2671 to access a non-existent part of the register. */
2672 if (TREE_CODE (index) == INTEGER_CST
82bb2115 2673 && TYPE_DOMAIN (TREE_TYPE (array))
84166705 2674 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
754b3ff5 2675 {
9b86eec0 2676 if (!c_mark_addressable (array))
754b3ff5 2677 return error_mark_node;
2678 }
628fa4f8 2679
7345b977 2680 if ((pedantic || warn_c90_c99_compat)
2681 && ! was_vector)
628fa4f8 2682 {
2683 tree foo = array;
2684 while (TREE_CODE (foo) == COMPONENT_REF)
2685 foo = TREE_OPERAND (foo, 0);
f48c7f4a 2686 if (VAR_P (foo) && C_DECL_REGISTER (foo))
29438999 2687 pedwarn (loc, OPT_Wpedantic,
8864917d 2688 "ISO C forbids subscripting %<register%> array");
890c2e2f 2689 else if (!lvalue_p (foo))
2690 pedwarn_c90 (loc, OPT_Wpedantic,
2691 "ISO C90 forbids subscripting non-lvalue "
2692 "array");
628fa4f8 2693 }
2694
aebc8537 2695 type = TREE_TYPE (TREE_TYPE (array));
14ae0310 2696 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
628fa4f8 2697 /* Array ref is const/volatile if the array elements are
a0c938f0 2698 or if the array is. */
628fa4f8 2699 TREE_READONLY (rval)
2700 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2701 | TREE_READONLY (array));
2702 TREE_SIDE_EFFECTS (rval)
2703 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2704 | TREE_SIDE_EFFECTS (array));
2705 TREE_THIS_VOLATILE (rval)
2706 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2707 /* This was added by rms on 16 Nov 91.
2b30d46c 2708 It fixes vol struct foo *a; a->elts[1]
628fa4f8 2709 in an inline function.
2710 Hope it doesn't break something else. */
2711 | TREE_THIS_VOLATILE (array));
22a3f7bd 2712 ret = require_complete_type (loc, rval);
dda49785 2713 protected_set_expr_location (ret, loc);
c61ef207 2714 if (non_lvalue)
2715 ret = non_lvalue_loc (loc, ret);
dda49785 2716 return ret;
628fa4f8 2717 }
20931c08 2718 else
2719 {
2720 tree ar = default_conversion (array);
628fa4f8 2721
20931c08 2722 if (ar == error_mark_node)
2723 return ar;
628fa4f8 2724
20931c08 2725 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2726 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
628fa4f8 2727
c61ef207 2728 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
623ee358 2729 index, false),
c61ef207 2730 RO_ARRAY_INDEXING);
2731 if (non_lvalue)
2732 ret = non_lvalue_loc (loc, ret);
2733 return ret;
20931c08 2734 }
628fa4f8 2735}
2736\f
1beb61f9 2737/* Build an external reference to identifier ID. FUN indicates
8dc938b9 2738 whether this will be used for a function call. LOC is the source
841fdaa6 2739 location of the identifier. This sets *TYPE to the type of the
2740 identifier, which is not the same as the type of the returned value
2741 for CONST_DECLs defined as enum constants. If the type of the
2742 identifier is not available, *TYPE is set to NULL. */
1beb61f9 2743tree
623ee358 2744build_external_ref (location_t loc, tree id, bool fun, tree *type)
1beb61f9 2745{
2746 tree ref;
2747 tree decl = lookup_name (id);
e913c82d 2748
2749 /* In Objective-C, an instance variable (ivar) may be preferred to
2750 whatever lookup_name() found. */
2751 decl = objc_lookup_ivar (decl, id);
1beb61f9 2752
841fdaa6 2753 *type = NULL;
31ddae9f 2754 if (decl && decl != error_mark_node)
841fdaa6 2755 {
2756 ref = decl;
2757 *type = TREE_TYPE (ref);
2758 }
31ddae9f 2759 else if (fun)
2760 /* Implicit function declaration. */
e60a6f7b 2761 ref = implicitly_declare (loc, id);
31ddae9f 2762 else if (decl == error_mark_node)
2763 /* Don't complain about something that's already been
2764 complained about. */
2765 return error_mark_node;
2766 else
2767 {
e60a6f7b 2768 undeclared_variable (loc, id);
31ddae9f 2769 return error_mark_node;
2770 }
1beb61f9 2771
2772 if (TREE_TYPE (ref) == error_mark_node)
2773 return error_mark_node;
2774
31ddae9f 2775 if (TREE_DEPRECATED (ref))
45c4e798 2776 warn_deprecated_use (ref, NULL_TREE);
31ddae9f 2777
3d053e06 2778 /* Recursive call does not count as usage. */
48e1416a 2779 if (ref != current_function_decl)
3d053e06 2780 {
3d053e06 2781 TREE_USED (ref) = 1;
2782 }
1beb61f9 2783
9823d3a9 2784 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2785 {
2786 if (!in_sizeof && !in_typeof)
2787 C_DECL_USED (ref) = 1;
72749341 2788 else if (DECL_INITIAL (ref) == NULL_TREE
9823d3a9 2789 && DECL_EXTERNAL (ref)
2790 && !TREE_PUBLIC (ref))
2791 record_maybe_used_decl (ref);
2792 }
2793
1beb61f9 2794 if (TREE_CODE (ref) == CONST_DECL)
2795 {
89c30811 2796 used_types_insert (TREE_TYPE (ref));
0b09525f 2797
2798 if (warn_cxx_compat
2799 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2800 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2801 {
2802 warning_at (loc, OPT_Wc___compat,
2803 ("enum constant defined in struct or union "
2804 "is not visible in C++"));
2805 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2806 }
2807
1beb61f9 2808 ref = DECL_INITIAL (ref);
2809 TREE_CONSTANT (ref) = 1;
2810 }
72749341 2811 else if (current_function_decl != NULL_TREE
3a2bf8d3 2812 && !DECL_FILE_SCOPE_P (current_function_decl)
b443c459 2813 && (VAR_OR_FUNCTION_DECL_P (ref)
2814 || TREE_CODE (ref) == PARM_DECL))
f3d3a8e2 2815 {
2816 tree context = decl_function_context (ref);
2b30d46c 2817
72749341 2818 if (context != NULL_TREE && context != current_function_decl)
f3d3a8e2 2819 DECL_NONLOCAL (ref) = 1;
2820 }
c42cc79e 2821 /* C99 6.7.4p3: An inline definition of a function with external
2822 linkage ... shall not contain a reference to an identifier with
2823 internal linkage. */
72749341 2824 else if (current_function_decl != NULL_TREE
c42cc79e 2825 && DECL_DECLARED_INLINE_P (current_function_decl)
2826 && DECL_EXTERNAL (current_function_decl)
2827 && VAR_OR_FUNCTION_DECL_P (ref)
f48c7f4a 2828 && (!VAR_P (ref) || TREE_STATIC (ref))
c55e2943 2829 && ! TREE_PUBLIC (ref)
2830 && DECL_CONTEXT (ref) != current_function_decl)
551ed71a 2831 record_inline_static (loc, current_function_decl, ref,
2832 csi_internal);
1beb61f9 2833
2834 return ref;
2835}
2836
9823d3a9 2837/* Record details of decls possibly used inside sizeof or typeof. */
2838struct maybe_used_decl
2839{
2840 /* The decl. */
2841 tree decl;
2842 /* The level seen at (in_sizeof + in_typeof). */
2843 int level;
2844 /* The next one at this level or above, or NULL. */
2845 struct maybe_used_decl *next;
2846};
2847
2848static struct maybe_used_decl *maybe_used_decls;
2849
2850/* Record that DECL, an undefined static function reference seen
2851 inside sizeof or typeof, might be used if the operand of sizeof is
2852 a VLA type or the operand of typeof is a variably modified
2853 type. */
2854
91ffb709 2855static void
9823d3a9 2856record_maybe_used_decl (tree decl)
2857{
2858 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2859 t->decl = decl;
2860 t->level = in_sizeof + in_typeof;
2861 t->next = maybe_used_decls;
2862 maybe_used_decls = t;
2863}
2864
2865/* Pop the stack of decls possibly used inside sizeof or typeof. If
2866 USED is false, just discard them. If it is true, mark them used
2867 (if no longer inside sizeof or typeof) or move them to the next
2868 level up (if still inside sizeof or typeof). */
2869
2870void
2871pop_maybe_used (bool used)
2872{
2873 struct maybe_used_decl *p = maybe_used_decls;
2874 int cur_level = in_sizeof + in_typeof;
2875 while (p && p->level > cur_level)
2876 {
2877 if (used)
2878 {
2879 if (cur_level == 0)
2880 C_DECL_USED (p->decl) = 1;
2881 else
2882 p->level = cur_level;
2883 }
2884 p = p->next;
2885 }
2886 if (!used || cur_level == 0)
2887 maybe_used_decls = p;
2888}
2889
2890/* Return the result of sizeof applied to EXPR. */
2891
2892struct c_expr
e60a6f7b 2893c_expr_sizeof_expr (location_t loc, struct c_expr expr)
9823d3a9 2894{
2895 struct c_expr ret;
7d32f35d 2896 if (expr.value == error_mark_node)
2897 {
2898 ret.value = error_mark_node;
2899 ret.original_code = ERROR_MARK;
841fdaa6 2900 ret.original_type = NULL;
7d32f35d 2901 pop_maybe_used (false);
2902 }
2903 else
2904 {
a75b1c71 2905 bool expr_const_operands = true;
87d59e72 2906
2907 if (TREE_CODE (expr.value) == PARM_DECL
2908 && C_ARRAY_PARAMETER (expr.value))
2909 {
bc35ef65 2910 auto_diagnostic_group d;
87d59e72 2911 if (warning_at (loc, OPT_Wsizeof_array_argument,
2912 "%<sizeof%> on array function parameter %qE will "
2913 "return size of %qT", expr.value,
2be90a05 2914 TREE_TYPE (expr.value)))
87d59e72 2915 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2916 }
a75b1c71 2917 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2918 &expr_const_operands);
e60a6f7b 2919 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
f003f5dc 2920 c_last_sizeof_arg = expr.value;
0ca70bfd 2921 c_last_sizeof_loc = loc;
f003f5dc 2922 ret.original_code = SIZEOF_EXPR;
841fdaa6 2923 ret.original_type = NULL;
a75b1c71 2924 if (c_vla_type_p (TREE_TYPE (folded_expr)))
32d33ab2 2925 {
2926 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
a75b1c71 2927 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2928 folded_expr, ret.value);
2929 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
e60a6f7b 2930 SET_EXPR_LOCATION (ret.value, loc);
32d33ab2 2931 }
a75b1c71 2932 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
7d32f35d 2933 }
9823d3a9 2934 return ret;
2935}
2936
2937/* Return the result of sizeof applied to T, a structure for the type
e60a6f7b 2938 name passed to sizeof (rather than the type itself). LOC is the
2939 location of the original expression. */
9823d3a9 2940
2941struct c_expr
e60a6f7b 2942c_expr_sizeof_type (location_t loc, struct c_type_name *t)
9823d3a9 2943{
2944 tree type;
2945 struct c_expr ret;
a75b1c71 2946 tree type_expr = NULL_TREE;
2947 bool type_expr_const = true;
2948 type = groktypename (t, &type_expr, &type_expr_const);
e60a6f7b 2949 ret.value = c_sizeof (loc, type);
f003f5dc 2950 c_last_sizeof_arg = type;
0ca70bfd 2951 c_last_sizeof_loc = loc;
f003f5dc 2952 ret.original_code = SIZEOF_EXPR;
841fdaa6 2953 ret.original_type = NULL;
e8074982 2954 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2955 && c_vla_type_p (type))
a75b1c71 2956 {
e8074982 2957 /* If the type is a [*] array, it is a VLA but is represented as
2958 having a size of zero. In such a case we must ensure that
2959 the result of sizeof does not get folded to a constant by
2960 c_fully_fold, because if the size is evaluated the result is
2961 not constant and so constraints on zero or negative size
2962 arrays must not be applied when this sizeof call is inside
2963 another array declarator. */
2964 if (!type_expr)
2965 type_expr = integer_zero_node;
a75b1c71 2966 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2967 type_expr, ret.value);
2968 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2969 }
8a98ec4a 2970 pop_maybe_used (type != error_mark_node
2971 ? C_TYPE_VARIABLE_SIZE (type) : false);
9823d3a9 2972 return ret;
2973}
2974
628fa4f8 2975/* Build a function call to function FUNCTION with parameters PARAMS.
e60a6f7b 2976 The function call is at LOC.
628fa4f8 2977 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2978 TREE_VALUE of each node is a parameter-expression.
2979 FUNCTION's data type may be a function type or a pointer-to-function. */
2980
2981tree
e60a6f7b 2982build_function_call (location_t loc, tree function, tree params)
b9c74b4d 2983{
f1f41a6c 2984 vec<tree, va_gc> *v;
b9c74b4d 2985 tree ret;
2986
f1f41a6c 2987 vec_alloc (v, list_length (params));
b9c74b4d 2988 for (; params; params = TREE_CHAIN (params))
f1f41a6c 2989 v->quick_push (TREE_VALUE (params));
5a672e62 2990 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
f1f41a6c 2991 vec_free (v);
b9c74b4d 2992 return ret;
2993}
2994
28738b20 2995/* Give a note about the location of the declaration of DECL. */
2996
5650b4ff 2997static void
2998inform_declaration (tree decl)
28738b20 2999{
5650b4ff 3000 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
28738b20 3001 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3002}
3003
b9c74b4d 3004/* Build a function call to function FUNCTION with parameters PARAMS.
3005 ORIGTYPES, if not NULL, is a vector of types; each element is
3006 either NULL or the original type of the corresponding element in
3007 PARAMS. The original type may differ from TREE_TYPE of the
3008 parameter for enums. FUNCTION's data type may be a function type
3009 or pointer-to-function. This function changes the elements of
3010 PARAMS. */
3011
3012tree
ec761d5a 3013build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3014 tree function, vec<tree, va_gc> *params,
f1f41a6c 3015 vec<tree, va_gc> *origtypes)
628fa4f8 3016{
72749341 3017 tree fntype, fundecl = NULL_TREE;
805e22b2 3018 tree name = NULL_TREE, result;
59798ded 3019 tree tem;
d01f58f9 3020 int nargs;
3021 tree *argarray;
48e1416a 3022
628fa4f8 3023
a390f7f2 3024 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
fe4d64a0 3025 STRIP_TYPE_NOPS (function);
628fa4f8 3026
3027 /* Convert anything with function type to a pointer-to-function. */
3028 if (TREE_CODE (function) == FUNCTION_DECL)
3029 {
3030 name = DECL_NAME (function);
4c0315d0 3031
3032 if (flag_tm)
3033 tm_malloc_replacement (function);
25fd8fda 3034 fundecl = function;
1cd6e20d 3035 /* Atomic functions have type checking/casting already done. They are
3036 often rewritten and don't match the original parameter list. */
3037 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3038 origtypes = NULL;
628fa4f8 3039 }
e78cf9a7 3040 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
e60a6f7b 3041 function = function_to_pointer_conversion (loc, function);
628fa4f8 3042
ddb5d39d 3043 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3044 expressions, like those used for ObjC messenger dispatches. */
f1f41a6c 3045 if (params && !params->is_empty ())
3046 function = objc_rewrite_function_call (function, (*params)[0]);
ddb5d39d 3047
a75b1c71 3048 function = c_fully_fold (function, false, NULL);
3049
628fa4f8 3050 fntype = TREE_TYPE (function);
3051
3052 if (TREE_CODE (fntype) == ERROR_MARK)
3053 return error_mark_node;
3054
3055 if (!(TREE_CODE (fntype) == POINTER_TYPE
3056 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3057 {
28738b20 3058 if (!flag_diagnostics_show_caret)
3059 error_at (loc,
3060 "called object %qE is not a function or function pointer",
3061 function);
3062 else if (DECL_P (function))
3063 {
3064 error_at (loc,
3065 "called object %qD is not a function or function pointer",
3066 function);
3067 inform_declaration (function);
3068 }
3069 else
3070 error_at (loc,
3071 "called object is not a function or function pointer");
628fa4f8 3072 return error_mark_node;
3073 }
3074
af857942 3075 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3076 current_function_returns_abnormally = 1;
3077
628fa4f8 3078 /* fntype now gets the type of function pointed to. */
3079 fntype = TREE_TYPE (fntype);
3080
ad0e7a06 3081 /* Convert the parameters to the types declared in the
3082 function prototype, or apply default promotions. */
3083
ec761d5a 3084 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3085 origtypes, function, fundecl);
ad0e7a06 3086 if (nargs < 0)
3087 return error_mark_node;
3088
59798ded 3089 /* Check that the function is called through a compatible prototype.
29f580ac 3090 If it is not, warn. */
72dd6141 3091 if (CONVERT_EXPR_P (function)
59798ded 3092 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3093 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
84166705 3094 && !comptypes (fntype, TREE_TYPE (tem)))
59798ded 3095 {
3096 tree return_type = TREE_TYPE (fntype);
59798ded 3097
3098 /* This situation leads to run-time undefined behavior. We can't,
3099 therefore, simply error unless we can prove that all possible
3100 executions of the program must execute the code. */
29f580ac 3101 warning_at (loc, 0, "function called through a non-compatible type");
59798ded 3102
29f580ac 3103 if (VOID_TYPE_P (return_type)
3104 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3105 pedwarn (loc, 0,
3106 "function with qualified void return type called");
3107 }
59798ded 3108
f1f41a6c 3109 argarray = vec_safe_address (params);
b9c74b4d 3110
d43cee80 3111 /* Check that arguments to builtin functions match the expectations. */
a0e9bfbb 3112 if (fundecl && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL)
5d4db8ef 3113 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3114 argarray))
d43cee80 3115 return error_mark_node;
628fa4f8 3116
d43cee80 3117 /* Check that the arguments to the function are valid. */
bbd5521e 3118 bool warned_p = check_function_arguments (loc, fundecl, fntype,
a115c319 3119 nargs, argarray, &arg_loc);
628fa4f8 3120
a75b1c71 3121 if (name != NULL_TREE
3122 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
16cb78b3 3123 {
a75b1c71 3124 if (require_constant_value)
184fac50 3125 result
3126 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3127 function, nargs, argarray);
a75b1c71 3128 else
389dd41b 3129 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3130 function, nargs, argarray);
a75b1c71 3131 if (TREE_CODE (result) == NOP_EXPR
3132 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3133 STRIP_TYPE_NOPS (result);
16cb78b3 3134 }
3135 else
389dd41b 3136 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3137 function, nargs, argarray);
184fac50 3138 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3139 later. */
3140 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3141 TREE_NO_WARNING (result) = 1;
650e4c94 3142
462ca422 3143 /* In this improbable scenario, a nested function returns a VM type.
3144 Create a TARGET_EXPR so that the call always has a LHS, much as
3145 what the C++ FE does for functions returning non-PODs. */
3146 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3147 {
3148 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3149 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3150 NULL_TREE, NULL_TREE);
3151 }
3152
5c803577 3153 if (VOID_TYPE_P (TREE_TYPE (result)))
8f24e4bd 3154 {
3155 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
389dd41b 3156 pedwarn (loc, 0,
8f24e4bd 3157 "function with qualified void return type called");
3158 return result;
3159 }
22a3f7bd 3160 return require_complete_type (loc, result);
628fa4f8 3161}
5a672e62 3162
3163/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3164
3165tree
3166c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3167 tree function, vec<tree, va_gc> *params,
3168 vec<tree, va_gc> *origtypes)
3169{
3170 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3171 STRIP_TYPE_NOPS (function);
3172
3173 /* Convert anything with function type to a pointer-to-function. */
3174 if (TREE_CODE (function) == FUNCTION_DECL)
3175 {
3176 /* Implement type-directed function overloading for builtins.
3177 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3178 handle all the type checking. The result is a complete expression
3179 that implements this function call. */
3180 tree tem = resolve_overloaded_builtin (loc, function, params);
3181 if (tem)
3182 return tem;
3183 }
3184 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3185}
628fa4f8 3186\f
16edb1bf 3187/* Helper for convert_arguments called to convert the VALue of argument
3188 number ARGNUM from ORIGTYPE to the corresponding parameter number
83ee3310 3189 PARMNUM and TYPE.
3190 PLOC is the location where the conversion is being performed.
3191 FUNCTION and FUNDECL are the same as in convert_arguments.
3192 VALTYPE is the original type of VAL before the conversion and,
3193 for EXCESS_PRECISION_EXPR, the operand of the expression.
3194 NPC is true if VAL represents the null pointer constant (VAL itself
3195 will have been folded to an integer constant).
3196 RNAME is the same as FUNCTION except in Objective C when it's
3197 the function selector.
3198 EXCESS_PRECISION is true when VAL was originally represented
3199 as EXCESS_PRECISION_EXPR.
3200 WARNOPT is the same as in convert_for_assignment. */
16edb1bf 3201
3202static tree
3203convert_argument (location_t ploc, tree function, tree fundecl,
83ee3310 3204 tree type, tree origtype, tree val, tree valtype,
3205 bool npc, tree rname, int parmnum, int argnum,
16edb1bf 3206 bool excess_precision, int warnopt)
3207{
16edb1bf 3208 /* Formal parm type is specified by a function prototype. */
3209
3210 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3211 {
3212 error_at (ploc, "type of formal parameter %d is incomplete",
3213 parmnum + 1);
83ee3310 3214 return val;
16edb1bf 3215 }
16edb1bf 3216
83ee3310 3217 /* Optionally warn about conversions that differ from the default
3218 conversions. */
3219 if (warn_traditional_conversion || warn_traditional)
3220 {
3221 unsigned int formal_prec = TYPE_PRECISION (type);
3222
3223 if (INTEGRAL_TYPE_P (type)
3224 && TREE_CODE (valtype) == REAL_TYPE)
3225 warning_at (ploc, OPT_Wtraditional_conversion,
3226 "passing argument %d of %qE as integer rather "
3227 "than floating due to prototype",
3228 argnum, rname);
3229 if (INTEGRAL_TYPE_P (type)
3230 && TREE_CODE (valtype) == COMPLEX_TYPE)
3231 warning_at (ploc, OPT_Wtraditional_conversion,
3232 "passing argument %d of %qE as integer rather "
3233 "than complex due to prototype",
3234 argnum, rname);
3235 else if (TREE_CODE (type) == COMPLEX_TYPE
3236 && TREE_CODE (valtype) == REAL_TYPE)
3237 warning_at (ploc, OPT_Wtraditional_conversion,
3238 "passing argument %d of %qE as complex rather "
3239 "than floating due to prototype",
3240 argnum, rname);
3241 else if (TREE_CODE (type) == REAL_TYPE
3242 && INTEGRAL_TYPE_P (valtype))
3243 warning_at (ploc, OPT_Wtraditional_conversion,
3244 "passing argument %d of %qE as floating rather "
3245 "than integer due to prototype",
3246 argnum, rname);
3247 else if (TREE_CODE (type) == COMPLEX_TYPE
3248 && INTEGRAL_TYPE_P (valtype))
3249 warning_at (ploc, OPT_Wtraditional_conversion,
3250 "passing argument %d of %qE as complex rather "
3251 "than integer due to prototype",
3252 argnum, rname);
3253 else if (TREE_CODE (type) == REAL_TYPE
3254 && TREE_CODE (valtype) == COMPLEX_TYPE)
3255 warning_at (ploc, OPT_Wtraditional_conversion,
3256 "passing argument %d of %qE as floating rather "
3257 "than complex due to prototype",
3258 argnum, rname);
3259 /* ??? At some point, messages should be written about
3260 conversions between complex types, but that's too messy
3261 to do now. */
3262 else if (TREE_CODE (type) == REAL_TYPE
3263 && TREE_CODE (valtype) == REAL_TYPE)
3264 {
3265 /* Warn if any argument is passed as `float',
3266 since without a prototype it would be `double'. */
3267 if (formal_prec == TYPE_PRECISION (float_type_node)
3268 && type != dfloat32_type_node)
3269 warning_at (ploc, 0,
3270 "passing argument %d of %qE as %<float%> "
3271 "rather than %<double%> due to prototype",
16edb1bf 3272 argnum, rname);
83ee3310 3273
3274 /* Warn if mismatch between argument and prototype
3275 for decimal float types. Warn of conversions with
3276 binary float types and of precision narrowing due to
3277 prototype. */
3278 else if (type != valtype
3279 && (type == dfloat32_type_node
3280 || type == dfloat64_type_node
3281 || type == dfloat128_type_node
3282 || valtype == dfloat32_type_node
3283 || valtype == dfloat64_type_node
3284 || valtype == dfloat128_type_node)
3285 && (formal_prec
3286 <= TYPE_PRECISION (valtype)
3287 || (type == dfloat128_type_node
3288 && (valtype
3289 != dfloat64_type_node
3290 && (valtype
3291 != dfloat32_type_node)))
3292 || (type == dfloat64_type_node
3293 && (valtype
3294 != dfloat32_type_node))))
3295 warning_at (ploc, 0,
3296 "passing argument %d of %qE as %qT "
3297 "rather than %qT due to prototype",
3298 argnum, rname, type, valtype);
3299
3300 }
3301 /* Detect integer changing in width or signedness.
3302 These warnings are only activated with
3303 -Wtraditional-conversion, not with -Wtraditional. */
3304 else if (warn_traditional_conversion
3305 && INTEGRAL_TYPE_P (type)
3306 && INTEGRAL_TYPE_P (valtype))
3307 {
3308 tree would_have_been = default_conversion (val);
3309 tree type1 = TREE_TYPE (would_have_been);
3310
3311 if (val == error_mark_node)
3312 /* VAL could have been of incomplete type. */;
3313 else if (TREE_CODE (type) == ENUMERAL_TYPE
3314 && (TYPE_MAIN_VARIANT (type)
3315 == TYPE_MAIN_VARIANT (valtype)))
3316 /* No warning if function asks for enum
3317 and the actual arg is that enum type. */
3318 ;
3319 else if (formal_prec != TYPE_PRECISION (type1))
16edb1bf 3320 warning_at (ploc, OPT_Wtraditional_conversion,
83ee3310 3321 "passing argument %d of %qE "
3322 "with different width due to prototype",
16edb1bf 3323 argnum, rname);
83ee3310 3324 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3325 ;
3326 /* Don't complain if the formal parameter type
3327 is an enum, because we can't tell now whether
3328 the value was an enum--even the same enum. */
3329 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3330 ;
3331 else if (TREE_CODE (val) == INTEGER_CST
3332 && int_fits_type_p (val, type))
3333 /* Change in signedness doesn't matter
3334 if a constant value is unaffected. */
3335 ;
3336 /* If the value is extended from a narrower
3337 unsigned type, it doesn't matter whether we
3338 pass it as signed or unsigned; the value
3339 certainly is the same either way. */
3340 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3341 && TYPE_UNSIGNED (valtype))
3342 ;
3343 else if (TYPE_UNSIGNED (type))
16edb1bf 3344 warning_at (ploc, OPT_Wtraditional_conversion,
83ee3310 3345 "passing argument %d of %qE "
3346 "as unsigned due to prototype",
16edb1bf 3347 argnum, rname);
83ee3310 3348 else
16edb1bf 3349 warning_at (ploc, OPT_Wtraditional_conversion,
83ee3310 3350 "passing argument %d of %qE "
3351 "as signed due to prototype",
16edb1bf 3352 argnum, rname);
16edb1bf 3353 }
83ee3310 3354 }
16edb1bf 3355
83ee3310 3356 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3357 sake of better warnings from convert_and_check. */
3358 if (excess_precision)
3359 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
16edb1bf 3360
83ee3310 3361 tree parmval = convert_for_assignment (ploc, ploc, type,
3362 val, origtype, ic_argpass,
3363 npc, fundecl, function,
3364 parmnum + 1, warnopt);
16edb1bf 3365
83ee3310 3366 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3367 && INTEGRAL_TYPE_P (type)
3368 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3369 parmval = default_conversion (parmval);
16edb1bf 3370
3371 return parmval;
3372}
3373
b9c74b4d 3374/* Convert the argument expressions in the vector VALUES
3375 to the types in the list TYPELIST.
628fa4f8 3376
3377 If TYPELIST is exhausted, or when an element has NULL as its type,
3378 perform the default conversions.
3379
b9c74b4d 3380 ORIGTYPES is the original types of the expressions in VALUES. This
3381 holds the type of enum values which have been converted to integral
3382 types. It may be NULL.
628fa4f8 3383
8ccb0952 3384 FUNCTION is a tree for the called function. It is used only for
3385 error messages, where it is formatted with %qE.
628fa4f8 3386
3387 This is also where warnings about wrong number of args are generated.
3388
ec761d5a 3389 ARG_LOC are locations of function arguments (if any).
3390
d01f58f9 3391 Returns the actual number of arguments processed (which may be less
b9c74b4d 3392 than the length of VALUES in some error situations), or -1 on
3393 failure. */
d01f58f9 3394
3395static int
ec761d5a 3396convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3397 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3398 tree function, tree fundecl)
628fa4f8 3399{
b9c74b4d 3400 unsigned int parmnum;
cb60919f 3401 bool error_args = false;
b5c26b42 3402 const bool type_generic = fundecl
ec761d5a 3403 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
c6418a4e 3404 bool type_generic_remove_excess_precision = false;
732905bb 3405 bool type_generic_overflow_p = false;
8ccb0952 3406 tree selector;
8ccb0952 3407
245d6740 3408 /* Change pointer to function to the function itself for
3409 diagnostics. */
8ccb0952 3410 if (TREE_CODE (function) == ADDR_EXPR
3411 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
245d6740 3412 function = TREE_OPERAND (function, 0);
8ccb0952 3413
3414 /* Handle an ObjC selector specially for diagnostics. */
3415 selector = objc_message_selector ();
628fa4f8 3416
16edb1bf 3417 /* For a call to a built-in function declared without a prototype,
3418 set to the built-in function's argument list. */
3419 tree builtin_typelist = NULL_TREE;
3420
c6418a4e 3421 /* For type-generic built-in functions, determine whether excess
3422 precision should be removed (classification) or not
3423 (comparison). */
16edb1bf 3424 if (fundecl
a0e9bfbb 3425 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
c6418a4e 3426 {
16edb1bf 3427 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3428 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
c6418a4e 3429 {
cf18b3e8 3430 /* For a call to a built-in function declared without a prototype
3431 use the types of the parameters of the internal built-in to
3432 match those of the arguments to. */
3433 if (tree bdecl = builtin_decl_explicit (code))
16edb1bf 3434 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3435 }
c6418a4e 3436
16edb1bf 3437 /* For type-generic built-in functions, determine whether excess
3438 precision should be removed (classification) or not
3439 (comparison). */
3440 if (type_generic)
3441 switch (code)
3442 {
3443 case BUILT_IN_ISFINITE:
3444 case BUILT_IN_ISINF:
3445 case BUILT_IN_ISINF_SIGN:
3446 case BUILT_IN_ISNAN:
3447 case BUILT_IN_ISNORMAL:
3448 case BUILT_IN_FPCLASSIFY:
3449 type_generic_remove_excess_precision = true;
3450 break;
732905bb 3451
16edb1bf 3452 case BUILT_IN_ADD_OVERFLOW_P:
3453 case BUILT_IN_SUB_OVERFLOW_P:
3454 case BUILT_IN_MUL_OVERFLOW_P:
3455 /* The last argument of these type-generic builtins
3456 should not be promoted. */
3457 type_generic_overflow_p = true;
3458 break;
3459
3460 default:
3461 break;
3462 }
c6418a4e 3463 }
3464
83ee3310 3465 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3466 individual converted arguments. */
628fa4f8 3467
16edb1bf 3468 tree typetail, builtin_typetail, val;
3469 for (typetail = typelist,
3470 builtin_typetail = builtin_typelist,
3471 parmnum = 0;
f1f41a6c 3472 values && values->iterate (parmnum, &val);
b9c74b4d 3473 ++parmnum)
628fa4f8 3474 {
83ee3310 3475 /* The type of the function parameter (if it was declared with one). */
16edb1bf 3476 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
83ee3310 3477 /* The type of the built-in function parameter (if the function
3478 is a built-in). Used to detect type incompatibilities in
3479 calls to built-ins declared without a prototype. */
16edb1bf 3480 tree builtin_type = (builtin_typetail
3481 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
83ee3310 3482 /* The original type of the argument being passed to the function. */
c6418a4e 3483 tree valtype = TREE_TYPE (val);
83ee3310 3484 /* The called function (or function selector in Objective C). */
8ccb0952 3485 tree rname = function;
3486 int argnum = parmnum + 1;
10fda9eb 3487 const char *invalid_func_diag;
83ee3310 3488 /* Set for EXCESS_PRECISION_EXPR arguments. */
c6418a4e 3489 bool excess_precision = false;
83ee3310 3490 /* The value of the argument after conversion to the type
3491 of the function parameter it is passed to. */
b9c74b4d 3492 tree parmval;
dcfff6f9 3493 /* Some __atomic_* builtins have additional hidden argument at
3494 position 0. */
3495 location_t ploc
3496 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3497 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3498 : input_location;
628fa4f8 3499
3500 if (type == void_type_node)
3501 {
1f3a51d6 3502 if (selector)
22a75734 3503 error_at (loc, "too many arguments to method %qE", selector);
1f3a51d6 3504 else
22a75734 3505 error_at (loc, "too many arguments to function %qE", function);
28738b20 3506 inform_declaration (fundecl);
87778dc4 3507 return error_args ? -1 : (int) parmnum;
628fa4f8 3508 }
3509
16edb1bf 3510 if (builtin_type == void_type_node)
3511 {
3512 warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3513 "too many arguments to built-in function %qE "
3514 "expecting %d",
3515 function, parmnum);
3516
3517 inform_declaration (fundecl);
3518 builtin_typetail = NULL_TREE;
3519 }
3520
8ccb0952 3521 if (selector && argnum > 2)
3522 {
3523 rname = selector;
3524 argnum -= 2;
3525 }
3526
16edb1bf 3527 /* Determine if VAL is a null pointer constant before folding it. */
3528 bool npc = null_pointer_constant_p (val);
c6418a4e 3529
3530 /* If there is excess precision and a prototype, convert once to
3531 the required type rather than converting via the semantic
3532 type. Likewise without a prototype a float value represented
3533 as long double should be converted once to double. But for
3534 type-generic classification functions excess precision must
3535 be removed here. */
3536 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3537 && (type || !type_generic || !type_generic_remove_excess_precision))
3538 {
3539 val = TREE_OPERAND (val, 0);
3540 excess_precision = true;
3541 }
a75b1c71 3542 val = c_fully_fold (val, false, NULL);
a97c952c 3543 STRIP_TYPE_NOPS (val);
628fa4f8 3544
22a3f7bd 3545 val = require_complete_type (ploc, val);
628fa4f8 3546
82c85aba 3547 /* Some floating-point arguments must be promoted to double when
3548 no type is specified by a prototype. This applies to
3549 arguments of type float, and to architecture-specific types
3550 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3551 bool promote_float_arg = false;
3552 if (type == NULL_TREE
3553 && TREE_CODE (valtype) == REAL_TYPE
3554 && (TYPE_PRECISION (valtype)
3555 <= TYPE_PRECISION (double_type_node))
3556 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3557 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3558 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3559 {
3560 /* Promote this argument, unless it has a _FloatN or
3561 _FloatNx type. */
3562 promote_float_arg = true;
3563 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3564 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3565 {
3566 promote_float_arg = false;
3567 break;
3568 }
3569 }
3570
72749341 3571 if (type != NULL_TREE)
628fa4f8 3572 {
16edb1bf 3573 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3574 parmval = convert_argument (ploc, function, fundecl, type, origtype,
83ee3310 3575 val, valtype, npc, rname, parmnum, argnum,
16edb1bf 3576 excess_precision, 0);
628fa4f8 3577 }
82c85aba 3578 else if (promote_float_arg)
b5c26b42 3579 {
3580 if (type_generic)
b9c74b4d 3581 parmval = val;
b5c26b42 3582 else
c920faa3 3583 {
3584 /* Convert `float' to `double'. */
3585 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
dcfff6f9 3586 warning_at (ploc, OPT_Wdouble_promotion,
3587 "implicit conversion from %qT to %qT when passing "
3588 "argument to function",
3589 valtype, double_type_node);
c920faa3 3590 parmval = convert (double_type_node, val);
3591 }
b5c26b42 3592 }
732905bb 3593 else if ((excess_precision && !type_generic)
3594 || (type_generic_overflow_p && parmnum == 2))
c6418a4e 3595 /* A "double" argument with excess precision being passed
732905bb 3596 without a prototype or in variable arguments.
3597 The last argument of __builtin_*_overflow_p should not be
3598 promoted. */
b9c74b4d 3599 parmval = convert (valtype, val);
a0c938f0 3600 else if ((invalid_func_diag =
3601 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
10fda9eb 3602 {
3603 error (invalid_func_diag);
d01f58f9 3604 return -1;
10fda9eb 3605 }
547c6b1f 3606 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3607 {
3608 return -1;
3609 }
628fa4f8 3610 else
3611 /* Convert `short' and `char' to full-size `int'. */
b9c74b4d 3612 parmval = default_conversion (val);
3613
f1f41a6c 3614 (*values)[parmnum] = parmval;
cb60919f 3615 if (parmval == error_mark_node)
3616 error_args = true;
628fa4f8 3617
16edb1bf 3618 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3619 {
3620 /* For a call to a built-in function declared without a prototype,
3621 perform the conversions from the argument to the expected type
3622 but issue warnings rather than errors for any mismatches.
3623 Ignore the converted argument and use the PARMVAL obtained
3624 above by applying default conversions instead. */
3625 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3626 convert_argument (ploc, function, fundecl, builtin_type, origtype,
83ee3310 3627 val, valtype, npc, rname, parmnum, argnum,
3628 excess_precision,
16edb1bf 3629 OPT_Wbuiltin_declaration_mismatch);
3630 }
3631
628fa4f8 3632 if (typetail)
3633 typetail = TREE_CHAIN (typetail);
16edb1bf 3634
3635 if (builtin_typetail)
3636 builtin_typetail = TREE_CHAIN (builtin_typetail);
628fa4f8 3637 }
3638
f1f41a6c 3639 gcc_assert (parmnum == vec_safe_length (values));
d01f58f9 3640
72749341 3641 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
7b2e6555 3642 {
22a75734 3643 error_at (loc, "too few arguments to function %qE", function);
6e13bc01 3644 inform_declaration (fundecl);
3645 return -1;
7b2e6555 3646 }
628fa4f8 3647
16edb1bf 3648 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3649 {
3650 unsigned nargs = parmnum;
3651 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3652 ++nargs;
3653
3654 warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3655 "too few arguments to built-in function %qE expecting %u",
3656 function, nargs - 1);
3657 inform_declaration (fundecl);
3658 }
3659
cb60919f 3660 return error_args ? -1 : (int) parmnum;
628fa4f8 3661}
3662\f
5381eb98 3663/* This is the entry point used by the parser to build unary operators
3664 in the input. CODE, a tree_code, specifies the unary operator, and
3665 ARG is the operand. For unary plus, the C parser currently uses
dda49785 3666 CONVERT_EXPR for code.
3667
3668 LOC is the location to use for the tree generated.
3669*/
5381eb98 3670
3671struct c_expr
e60a6f7b 3672parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
5381eb98 3673{
3674 struct c_expr result;
3675
82012ffe 3676 result.original_code = code;
841fdaa6 3677 result.original_type = NULL;
3678
547c6b1f 3679 if (reject_gcc_builtin (arg.value))
3680 {
3681 result.value = error_mark_node;
3682 }
3683 else
3684 {
b99cc6da 3685 result.value = build_unary_op (loc, code, arg.value, false);
547c6b1f 3686
e68d7b49 3687 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
645b0f8d 3688 overflow_warning (loc, result.value, arg.value);
547c6b1f 3689 }
f170d67f 3690
a96cefb2 3691 /* We are typically called when parsing a prefix token at LOC acting on
3692 ARG. Reflect this by updating the source range of the result to
3693 start at LOC and end at the end of ARG. */
3694 set_c_expr_source_range (&result,
3695 loc, arg.get_finish ());
3696
5381eb98 3697 return result;
3698}
3699
6124217c 3700/* Returns true if TYPE is a character type, *not* including wchar_t. */
3701
3702static bool
3703char_type_p (tree type)
3704{
3705 return (type == char_type_node
3706 || type == unsigned_char_type_node
3707 || type == signed_char_type_node
3708 || type == char16_type_node
3709 || type == char32_type_node);
3710}
3711
5381eb98 3712/* This is the entry point used by the parser to build binary operators
3713 in the input. CODE, a tree_code, specifies the binary operator, and
3714 ARG1 and ARG2 are the operands. In addition to constructing the
3715 expression, we check for operands that were written with other binary
8e70fb09 3716 operators in a way that is likely to confuse the user.
3717
3718 LOCATION is the location of the binary operator. */
a9ba5ac4 3719
066f26bf 3720struct c_expr
8e70fb09 3721parser_build_binary_op (location_t location, enum tree_code code,
3722 struct c_expr arg1, struct c_expr arg2)
628fa4f8 3723{
066f26bf 3724 struct c_expr result;
628fa4f8 3725
066f26bf 3726 enum tree_code code1 = arg1.original_code;
3727 enum tree_code code2 = arg2.original_code;
841fdaa6 3728 tree type1 = (arg1.original_type
3729 ? arg1.original_type
3730 : TREE_TYPE (arg1.value));
3731 tree type2 = (arg2.original_type
3732 ? arg2.original_type
3733 : TREE_TYPE (arg2.value));
628fa4f8 3734
8e70fb09 3735 result.value = build_binary_op (location, code,
623ee358 3736 arg1.value, arg2.value, true);
066f26bf 3737 result.original_code = code;
841fdaa6 3738 result.original_type = NULL;
5eaf60b3 3739
066f26bf 3740 if (TREE_CODE (result.value) == ERROR_MARK)
97c71d71 3741 {
3742 set_c_expr_source_range (&result,
3743 arg1.get_start (),
3744 arg2.get_finish ());
3745 return result;
3746 }
628fa4f8 3747
8e70fb09 3748 if (location != UNKNOWN_LOCATION)
3749 protected_set_expr_location (result.value, location);
3750
a96cefb2 3751 set_c_expr_source_range (&result,
3752 arg1.get_start (),
3753 arg2.get_finish ());
3754
628fa4f8 3755 /* Check for cases such as x+y<<z which users are likely
066f26bf 3756 to misinterpret. */
628fa4f8 3757 if (warn_parentheses)
2623625f 3758 warn_about_parentheses (location, code, code1, arg1.value, code2,
3759 arg2.value);
8ee1dd9f 3760
03033af4 3761 if (warn_logical_op)
2623625f 3762 warn_logical_operator (location, code, TREE_TYPE (result.value),
03033af4 3763 code1, arg1.value, code2, arg2.value);
b13d1547 3764
6784a472 3765 if (warn_tautological_compare)
62efaf6b 3766 {
3767 tree lhs = arg1.value;
3768 tree rhs = arg2.value;
3769 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3770 {
3771 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3772 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3773 lhs = NULL_TREE;
3774 else
3775 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3776 }
3777 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3778 {
3779 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3780 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3781 rhs = NULL_TREE;
3782 else
3783 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3784 }
3785 if (lhs != NULL_TREE && rhs != NULL_TREE)
3786 warn_tautological_cmp (location, code, lhs, rhs);
3787 }
6784a472 3788
32dc1512 3789 if (warn_logical_not_paren
16f958b3 3790 && TREE_CODE_CLASS (code) == tcc_comparison
499b7390 3791 && code1 == TRUTH_NOT_EXPR
6054103a 3792 && code2 != TRUTH_NOT_EXPR
3793 /* Avoid warning for !!x == y. */
3794 && (TREE_CODE (arg1.value) != NE_EXPR
3795 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3796 {
3797 /* Avoid warning for !b == y where b has _Bool type. */
3798 tree t = integer_zero_node;
3799 if (TREE_CODE (arg1.value) == EQ_EXPR
3800 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3801 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3802 {
3803 t = TREE_OPERAND (arg1.value, 0);
3804 do
3805 {
3806 if (TREE_TYPE (t) != integer_type_node)
3807 break;
3808 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3809 t = C_MAYBE_CONST_EXPR_EXPR (t);
3810 else if (CONVERT_EXPR_P (t))
3811 t = TREE_OPERAND (t, 0);
3812 else
3813 break;
3814 }
3815 while (1);
3816 }
3817 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
c4963714 3818 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
6054103a 3819 }
32dc1512 3820
19f0596c 3821 /* Warn about comparisons against string literals, with the exception
3822 of testing for equality or inequality of a string literal with NULL. */
3823 if (code == EQ_EXPR || code == NE_EXPR)
3824 {
40b38ace 3825 if ((code1 == STRING_CST
3826 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3827 || (code2 == STRING_CST
3828 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
e60a6f7b 3829 warning_at (location, OPT_Waddress,
3830 "comparison with string literal results in unspecified behavior");
6124217c 3831 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3832 if (POINTER_TYPE_P (type1)
bc35ef65 3833 && null_pointer_constant_p (arg2.value)
3834 && char_type_p (type2))
3835 {
3836 auto_diagnostic_group d;
3837 if (warning_at (location, OPT_Wpointer_compare,
3838 "comparison between pointer and zero character "
3839 "constant"))
3840 inform (arg1.get_start (),
3841 "did you mean to dereference the pointer?");
3842 }
6124217c 3843 else if (POINTER_TYPE_P (type2)
3844 && null_pointer_constant_p (arg1.value)
bc35ef65 3845 && char_type_p (type1))
3846 {
3847 auto_diagnostic_group d;
3848 if (warning_at (location, OPT_Wpointer_compare,
3849 "comparison between pointer and zero character "
3850 "constant"))
3851 inform (arg2.get_start (),
3852 "did you mean to dereference the pointer?");
3853 }
19f0596c 3854 }
3855 else if (TREE_CODE_CLASS (code) == tcc_comparison
3856 && (code1 == STRING_CST || code2 == STRING_CST))
e60a6f7b 3857 warning_at (location, OPT_Waddress,
3858 "comparison with string literal results in unspecified behavior");
19f0596c 3859
48e1416a 3860 if (TREE_OVERFLOW_P (result.value)
3861 && !TREE_OVERFLOW_P (arg1.value)
f170d67f 3862 && !TREE_OVERFLOW_P (arg2.value))
e60a6f7b 3863 overflow_warning (location, result.value);
628fa4f8 3864
841fdaa6 3865 /* Warn about comparisons of different enum types. */
3866 if (warn_enum_compare
3867 && TREE_CODE_CLASS (code) == tcc_comparison
3868 && TREE_CODE (type1) == ENUMERAL_TYPE
3869 && TREE_CODE (type2) == ENUMERAL_TYPE
3870 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3871 warning_at (location, OPT_Wenum_compare,
3872 "comparison between %qT and %qT",
3873 type1, type2);
3874
628fa4f8 3875 return result;
3876}
276beea2 3877\f
276beea2 3878/* Return a tree for the difference of pointers OP0 and OP1.
eabe2d94 3879 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3880 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
367d2866 3881
276beea2 3882static tree
eabe2d94 3883pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
276beea2 3884{
276beea2 3885 tree restype = ptrdiff_type_node;
6d5d708e 3886 tree result, inttype;
628fa4f8 3887
6d5d708e 3888 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3889 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
276beea2 3890 tree target_type = TREE_TYPE (TREE_TYPE (op0));
276beea2 3891 tree orig_op1 = op1;
628fa4f8 3892
6d5d708e 3893 /* If the operands point into different address spaces, we need to
3894 explicitly convert them to pointers into the common address space
3895 before we can subtract the numerical address values. */
3896 if (as0 != as1)
3897 {
3898 addr_space_t as_common;
3899 tree common_type;
3900
3901 /* Determine the common superset address space. This is guaranteed
3902 to exist because the caller verified that comp_target_types
3903 returned non-zero. */
3904 if (!addr_space_superset (as0, as1, &as_common))
3905 gcc_unreachable ();
3906
3907 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3908 op0 = convert (common_type, op0);
3909 op1 = convert (common_type, op1);
3910 }
3911
57e83b58 3912 /* Determine integer type result of the subtraction. This will usually
6d5d708e 3913 be the same as the result type (ptrdiff_t), but may need to be a wider
3914 type if pointers for the address space are wider than ptrdiff_t. */
3915 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3c9b85aa 3916 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6d5d708e 3917 else
3918 inttype = restype;
3919
8864917d 3920 if (TREE_CODE (target_type) == VOID_TYPE)
9205a6cc 3921 pedwarn (loc, OPT_Wpointer_arith,
8864917d 3922 "pointer of type %<void *%> used in subtraction");
3923 if (TREE_CODE (target_type) == FUNCTION_TYPE)
9205a6cc 3924 pedwarn (loc, OPT_Wpointer_arith,
8864917d 3925 "pointer to a function used in subtraction");
628fa4f8 3926
eabe2d94 3927 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3928 {
3929 gcc_assert (current_function_decl != NULL_TREE);
3930
3931 op0 = save_expr (op0);
3932 op1 = save_expr (op1);
3933
3934 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3935 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3936 }
3937
57e83b58 3938 /* First do the subtraction, then build the divide operator
3939 and only convert at the very end.
3940 Do not do default conversions in case restype is a short type. */
3941
3942 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3943 pointers. If some platform cannot provide that, or has a larger
3944 ptrdiff_type to support differences larger than half the address
3945 space, cast the pointers to some larger integer type and do the
3946 computations in that type. */
3947 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
eabe2d94 3948 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3949 convert (inttype, op1), false);
57e83b58 3950 else
0245ae71 3951 {
3952 /* Cast away qualifiers. */
3953 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
3954 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
3955 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3956 }
628fa4f8 3957
276beea2 3958 /* This generates an error if op1 is pointer to incomplete type. */
3959 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
389dd41b 3960 error_at (loc, "arithmetic on pointer to an incomplete type");
628fa4f8 3961
276beea2 3962 op1 = c_size_in_bytes (target_type);
628fa4f8 3963
46da3601 3964 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3965 error_at (loc, "arithmetic on pointer to an empty aggregate");
3966
276beea2 3967 /* Divide by the size, in easiest possible way. */
6d5d708e 3968 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3969 op0, convert (inttype, op1));
3970
3971 /* Convert to final result type if necessary. */
3972 return convert (restype, result);
276beea2 3973}
3974\f
d7d6a3ab 3975/* Expand atomic compound assignments into an appropriate sequence as
3976 specified by the C11 standard section 6.5.16.2.
3977
b560fabd 3978 _Atomic T1 E1
3979 T2 E2
3980 E1 op= E2
3981
3982 This sequence is used for all types for which these operations are
3983 supported.
3984
3985 In addition, built-in versions of the 'fe' prefixed routines may
3986 need to be invoked for floating point (real, complex or vector) when
3987 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3988
3989 T1 newval;
3990 T1 old;
3991 T1 *addr
3992 T2 val
3993 fenv_t fenv
3994
3995 addr = &E1;
3996 val = (E2);
3997 __atomic_load (addr, &old, SEQ_CST);
3998 feholdexcept (&fenv);
3999loop:
4000 newval = old op val;
4001 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4002 SEQ_CST))
4003 goto done;
4004 feclearexcept (FE_ALL_EXCEPT);
4005 goto loop:
4006done:
4007 feupdateenv (&fenv);
4008
d7d6a3ab 4009 The compiler will issue the __atomic_fetch_* built-in when possible,
4010 otherwise it will generate the generic form of the atomic operations.
4011 This requires temp(s) and has their address taken. The atomic processing
4012 is smart enough to figure out when the size of an object can utilize
4013 a lock-free version, and convert the built-in call to the appropriate
4014 lock-free routine. The optimizers will then dispose of any temps that
4015 are no longer required, and lock-free implementations are utilized as
4016 long as there is target support for the required size.
b560fabd 4017
4018 If the operator is NOP_EXPR, then this is a simple assignment, and
4019 an __atomic_store is issued to perform the assignment rather than
d7d6a3ab 4020 the above loop. */
b560fabd 4021
4022/* Build an atomic assignment at LOC, expanding into the proper
4023 sequence to store LHS MODIFYCODE= RHS. Return a value representing
d7d6a3ab 4024 the result of the operation, unless RETURN_OLD_P, in which case
b560fabd 4025 return the old value of LHS (this is only for postincrement and
4026 postdecrement). */
d7d6a3ab 4027
b560fabd 4028static tree
4029build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4030 tree rhs, bool return_old_p)
4031{
4032 tree fndecl, func_call;
4033 vec<tree, va_gc> *params;
4034 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4035 tree old, old_addr;
4036 tree compound_stmt;
4037 tree stmt, goto_stmt;
4038 tree loop_label, loop_decl, done_label, done_decl;
4039
4040 tree lhs_type = TREE_TYPE (lhs);
b99cc6da 4041 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
b560fabd 4042 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
de7757ed 4043 tree rhs_semantic_type = TREE_TYPE (rhs);
4044 tree nonatomic_rhs_semantic_type;
4045 tree rhs_type;
b560fabd 4046
4047 gcc_assert (TYPE_ATOMIC (lhs_type));
4048
4049 if (return_old_p)
4050 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4051
4052 /* Allocate enough vector items for a compare_exchange. */
4053 vec_alloc (params, 6);
4054
4055 /* Create a compound statement to hold the sequence of statements
4056 with a loop. */
4057 compound_stmt = c_begin_compound_stmt (false);
4058
de7757ed 4059 /* Remove any excess precision (which is only present here in the
4060 case of compound assignments). */
4061 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4062 {
4063 gcc_assert (modifycode != NOP_EXPR);
4064 rhs = TREE_OPERAND (rhs, 0);
4065 }
4066 rhs_type = TREE_TYPE (rhs);
4067
b560fabd 4068 /* Fold the RHS if it hasn't already been folded. */
4069 if (modifycode != NOP_EXPR)
4070 rhs = c_fully_fold (rhs, false, NULL);
4071
4072 /* Remove the qualifiers for the rest of the expressions and create
4073 the VAL temp variable to hold the RHS. */
4074 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4075 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
de7757ed 4076 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4077 TYPE_UNQUALIFIED);
0cd6b1b4 4078 val = create_tmp_var_raw (nonatomic_rhs_type);
b560fabd 4079 TREE_ADDRESSABLE (val) = 1;
977b1853 4080 TREE_NO_WARNING (val) = 1;
0cd6b1b4 4081 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4082 NULL_TREE);
b560fabd 4083 SET_EXPR_LOCATION (rhs, loc);
4084 add_stmt (rhs);
4085
4086 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4087 an atomic_store. */
4088 if (modifycode == NOP_EXPR)
4089 {
4090 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
b99cc6da 4091 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
b560fabd 4092 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4093 params->quick_push (lhs_addr);
4094 params->quick_push (rhs);
4095 params->quick_push (seq_cst);
5a672e62 4096 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
b560fabd 4097 add_stmt (func_call);
4098
4099 /* Finish the compound statement. */
4100 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4101
4102 /* VAL is the value which was stored, return a COMPOUND_STMT of
4103 the statement and that value. */
4104 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4105 }
4106
d7d6a3ab 4107 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4108 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4109 isn't applicable for such builtins. ??? Do we want to handle enums? */
4110 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4111 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4112 {
4113 built_in_function fncode;
4114 switch (modifycode)
4115 {
4116 case PLUS_EXPR:
4117 case POINTER_PLUS_EXPR:
4118 fncode = (return_old_p
4119 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4120 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4121 break;
4122 case MINUS_EXPR:
4123 fncode = (return_old_p
4124 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4125 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4126 break;
4127 case BIT_AND_EXPR:
4128 fncode = (return_old_p
4129 ? BUILT_IN_ATOMIC_FETCH_AND_N
4130 : BUILT_IN_ATOMIC_AND_FETCH_N);
4131 break;
4132 case BIT_IOR_EXPR:
4133 fncode = (return_old_p
4134 ? BUILT_IN_ATOMIC_FETCH_OR_N
4135 : BUILT_IN_ATOMIC_OR_FETCH_N);
4136 break;
4137 case BIT_XOR_EXPR:
4138 fncode = (return_old_p
4139 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4140 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4141 break;
4142 default:
4143 goto cas_loop;
4144 }
4145
4146 /* We can only use "_1" through "_16" variants of the atomic fetch
4147 built-ins. */
4148 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4149 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4150 goto cas_loop;
4151
4152 /* If this is a pointer type, we need to multiply by the size of
4153 the pointer target type. */
4154 if (POINTER_TYPE_P (lhs_type))
4155 {
4156 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4157 /* ??? This would introduce -Wdiscarded-qualifiers
4158 warning: __atomic_fetch_* expect volatile void *
4159 type as the first argument. (Assignments between
4160 atomic and non-atomic objects are OK.) */
4161 || TYPE_RESTRICT (lhs_type))
4162 goto cas_loop;
4163 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4164 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4165 convert (ptrdiff_type_node, rhs),
4166 convert (ptrdiff_type_node, sz));
4167 }
4168
4169 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4170 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4171 fndecl = builtin_decl_explicit (fncode);
4172 params->quick_push (lhs_addr);
4173 params->quick_push (rhs);
4174 params->quick_push (seq_cst);
4175 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4176
4177 newval = create_tmp_var_raw (nonatomic_lhs_type);
4178 TREE_ADDRESSABLE (newval) = 1;
4179 TREE_NO_WARNING (newval) = 1;
4180 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4181 NULL_TREE, NULL_TREE);
4182 SET_EXPR_LOCATION (rhs, loc);
4183 add_stmt (rhs);
4184
4185 /* Finish the compound statement. */
4186 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4187
4188 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4189 the statement and that value. */
4190 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4191 }
4192
4193cas_loop:
b560fabd 4194 /* Create the variables and labels required for the op= form. */
0cd6b1b4 4195 old = create_tmp_var_raw (nonatomic_lhs_type);
b99cc6da 4196 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
977b1853 4197 TREE_ADDRESSABLE (old) = 1;
4198 TREE_NO_WARNING (old) = 1;
b560fabd 4199
0cd6b1b4 4200 newval = create_tmp_var_raw (nonatomic_lhs_type);
b99cc6da 4201 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
b560fabd 4202 TREE_ADDRESSABLE (newval) = 1;
75e722a6 4203 TREE_NO_WARNING (newval) = 1;
b560fabd 4204
4205 loop_decl = create_artificial_label (loc);
4206 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4207
4208 done_decl = create_artificial_label (loc);
4209 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4210
4211 /* __atomic_load (addr, &old, SEQ_CST). */
4212 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4213 params->quick_push (lhs_addr);
4214 params->quick_push (old_addr);
4215 params->quick_push (seq_cst);
5a672e62 4216 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
0cd6b1b4 4217 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4218 NULL_TREE);
4219 add_stmt (old);
b560fabd 4220 params->truncate (0);
4221
4222 /* Create the expressions for floating-point environment
4223 manipulation, if required. */
4224 bool need_fenv = (flag_trapping_math
4225 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4226 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4227 if (need_fenv)
4228 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4229
4230 if (hold_call)
4231 add_stmt (hold_call);
4232
4233 /* loop: */
4234 add_stmt (loop_label);
4235
4236 /* newval = old + val; */
de7757ed 4237 if (rhs_type != rhs_semantic_type)
4238 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
623ee358 4239 rhs = build_binary_op (loc, modifycode, old, val, true);
de7757ed 4240 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4241 {
4242 tree eptype = TREE_TYPE (rhs);
4243 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4244 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4245 }
4246 else
4247 rhs = c_fully_fold (rhs, false, NULL);
22a75734 4248 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4249 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
b560fabd 4250 NULL_TREE, 0);
4251 if (rhs != error_mark_node)
4252 {
0cd6b1b4 4253 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4254 NULL_TREE);
b560fabd 4255 SET_EXPR_LOCATION (rhs, loc);
4256 add_stmt (rhs);
4257 }
4258
4259 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4260 goto done; */
4261 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4262 params->quick_push (lhs_addr);
4263 params->quick_push (old_addr);
4264 params->quick_push (newval_addr);
4265 params->quick_push (integer_zero_node);
4266 params->quick_push (seq_cst);
4267 params->quick_push (seq_cst);
5a672e62 4268 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
b560fabd 4269
4270 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4271 SET_EXPR_LOCATION (goto_stmt, loc);
4272
4273 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4274 SET_EXPR_LOCATION (stmt, loc);
4275 add_stmt (stmt);
0cd6b1b4 4276
b560fabd 4277 if (clear_call)
4278 add_stmt (clear_call);
4279
4280 /* goto loop; */
4281 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4282 SET_EXPR_LOCATION (goto_stmt, loc);
4283 add_stmt (goto_stmt);
0cd6b1b4 4284
b560fabd 4285 /* done: */
4286 add_stmt (done_label);
4287
4288 if (update_call)
4289 add_stmt (update_call);
4290
4291 /* Finish the compound statement. */
4292 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4293
4294 /* NEWVAL is the value that was successfully stored, return a
4295 COMPOUND_EXPR of the statement and the appropriate value. */
4296 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4297 return_old_p ? old : newval);
4298}
4299
276beea2 4300/* Construct and perhaps optimize a tree representation
4301 for a unary operation. CODE, a tree_code, specifies the operation
4302 and XARG is the operand.
b99cc6da 4303 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4304 promotions (such as from short to int).
4305 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4306 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4307 to pointers in C99.
b6889cb0 4308
4309 LOCATION is the location of the operator. */
628fa4f8 4310
276beea2 4311tree
89bcb5a3 4312build_unary_op (location_t location, enum tree_code code, tree xarg,
b99cc6da 4313 bool noconvert)
276beea2 4314{
4315 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4316 tree arg = xarg;
72749341 4317 tree argtype = NULL_TREE;
f831c98f 4318 enum tree_code typecode;
276beea2 4319 tree val;
b6889cb0 4320 tree ret = error_mark_node;
c6418a4e 4321 tree eptype = NULL_TREE;
7a979707 4322 const char *invalid_op_diag;
a75b1c71 4323 bool int_operands;
4324
4325 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
9a8bed72 4326 if (int_operands)
4327 arg = remove_c_maybe_const_expr (arg);
628fa4f8 4328
f831c98f 4329 if (code != ADDR_EXPR)
22a3f7bd 4330 arg = require_complete_type (location, arg);
f831c98f 4331
4332 typecode = TREE_CODE (TREE_TYPE (arg));
276beea2 4333 if (typecode == ERROR_MARK)
4334 return error_mark_node;
4335 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4336 typecode = INTEGER_TYPE;
bd08a4e6 4337
7a979707 4338 if ((invalid_op_diag
4339 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4340 {
b6889cb0 4341 error_at (location, invalid_op_diag);
7a979707 4342 return error_mark_node;
4343 }
4344
c6418a4e 4345 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4346 {
4347 eptype = TREE_TYPE (arg);
4348 arg = TREE_OPERAND (arg, 0);
4349 }
4350
276beea2 4351 switch (code)
4352 {
4353 case CONVERT_EXPR:
4354 /* This is used for unary plus, because a CONVERT_EXPR
4355 is enough to prevent anybody from looking inside for
4356 associativity, but won't generate any code. */
4357 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
9421ebb9 4358 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
71c777ee 4359 || typecode == VECTOR_TYPE))
628fa4f8 4360 {
b6889cb0 4361 error_at (location, "wrong type argument to unary plus");
276beea2 4362 return error_mark_node;
628fa4f8 4363 }
276beea2 4364 else if (!noconvert)
4365 arg = default_conversion (arg);
389dd41b 4366 arg = non_lvalue_loc (location, arg);
628fa4f8 4367 break;
4368
276beea2 4369 case NEGATE_EXPR:
4370 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
9421ebb9 4371 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
276beea2 4372 || typecode == VECTOR_TYPE))
4373 {
b6889cb0 4374 error_at (location, "wrong type argument to unary minus");
276beea2 4375 return error_mark_node;
4376 }
4377 else if (!noconvert)
4378 arg = default_conversion (arg);
628fa4f8 4379 break;
4380
276beea2 4381 case BIT_NOT_EXPR:
0eb14c3d 4382 /* ~ works on integer types and non float vectors. */
4383 if (typecode == INTEGER_TYPE
4384 || (typecode == VECTOR_TYPE
4385 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
d2c0af2b 4386 {
481ce481 4387 tree e = arg;
4388
4389 /* Warn if the expression has boolean value. */
4390 while (TREE_CODE (e) == COMPOUND_EXPR)
4391 e = TREE_OPERAND (e, 1);
4392
4393 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
bc35ef65 4394 || truth_value_p (TREE_CODE (e))))
481ce481 4395 {
bc35ef65 4396 auto_diagnostic_group d;
4397 if (warning_at (location, OPT_Wbool_operation,
4398 "%<~%> on a boolean expression"))
4399 {
4400 gcc_rich_location richloc (location);
4401 richloc.add_fixit_insert_before (location, "!");
4402 inform (&richloc, "did you mean to use logical not?");
4403 }
481ce481 4404 }
276beea2 4405 if (!noconvert)
4406 arg = default_conversion (arg);
d2c0af2b 4407 }
276beea2 4408 else if (typecode == COMPLEX_TYPE)
628fa4f8 4409 {
276beea2 4410 code = CONJ_EXPR;
29438999 4411 pedwarn (location, OPT_Wpedantic,
8864917d 4412 "ISO C does not support %<~%> for complex conjugation");
276beea2 4413 if (!noconvert)
4414 arg = default_conversion (arg);
4415 }
4416 else
4417 {
b6889cb0 4418 error_at (location, "wrong type argument to bit-complement");
276beea2 4419 return error_mark_node;
628fa4f8 4420 }
4421 break;
4422
276beea2 4423 case ABS_EXPR:
71d5a758 4424 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
628fa4f8 4425 {
b6889cb0 4426 error_at (location, "wrong type argument to abs");
276beea2 4427 return error_mark_node;
628fa4f8 4428 }
276beea2 4429 else if (!noconvert)
4430 arg = default_conversion (arg);
628fa4f8 4431 break;
4432
1c67942e 4433 case ABSU_EXPR:
4434 if (!(typecode == INTEGER_TYPE))
4435 {
4436 error_at (location, "wrong type argument to absu");
4437 return error_mark_node;
4438 }
4439 else if (!noconvert)
4440 arg = default_conversion (arg);
4441 break;
4442
276beea2 4443 case CONJ_EXPR:
4444 /* Conjugating a real value is a no-op, but allow it anyway. */
4445 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4446 || typecode == COMPLEX_TYPE))
628fa4f8 4447 {
b6889cb0 4448 error_at (location, "wrong type argument to conjugation");
276beea2 4449 return error_mark_node;
628fa4f8 4450 }
276beea2 4451 else if (!noconvert)
4452 arg = default_conversion (arg);
628fa4f8 4453 break;
4454
276beea2 4455 case TRUTH_NOT_EXPR:
9421ebb9 4456 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
276beea2 4457 && typecode != REAL_TYPE && typecode != POINTER_TYPE
f14c8207 4458 && typecode != COMPLEX_TYPE)
628fa4f8 4459 {
b6889cb0 4460 error_at (location,
4461 "wrong type argument to unary exclamation mark");
276beea2 4462 return error_mark_node;
628fa4f8 4463 }
bb554051 4464 if (int_operands)
4465 {
4466 arg = c_objc_common_truthvalue_conversion (location, xarg);
4467 arg = remove_c_maybe_const_expr (arg);
4468 }
4469 else
4470 arg = c_objc_common_truthvalue_conversion (location, arg);
389dd41b 4471 ret = invert_truthvalue_loc (location, arg);
43158006 4472 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4473 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4474 location = EXPR_LOCATION (ret);
b6889cb0 4475 goto return_build_unary_op;
276beea2 4476
276beea2 4477 case REALPART_EXPR:
276beea2 4478 case IMAGPART_EXPR:
0b5fc5d6 4479 ret = build_real_imag_expr (location, code, arg);
4480 if (ret == error_mark_node)
4481 return error_mark_node;
c6418a4e 4482 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4483 eptype = TREE_TYPE (eptype);
b6889cb0 4484 goto return_build_unary_op;
276beea2 4485
4486 case PREINCREMENT_EXPR:
4487 case POSTINCREMENT_EXPR:
4488 case PREDECREMENT_EXPR:
4489 case POSTDECREMENT_EXPR:
276beea2 4490
a75b1c71 4491 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4492 {
4493 tree inner = build_unary_op (location, code,
89bcb5a3 4494 C_MAYBE_CONST_EXPR_EXPR (arg),
4495 noconvert);
a75b1c71 4496 if (inner == error_mark_node)
4497 return error_mark_node;
4498 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4499 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4500 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4501 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4502 goto return_build_unary_op;
4503 }
4504
e4a7640a 4505 /* Complain about anything that is not a true lvalue. In
4506 Objective-C, skip this check for property_refs. */
6cf89e04 4507 if (!objc_is_property_ref (arg)
fdd84b77 4508 && !lvalue_or_else (location,
4509 arg, ((code == PREINCREMENT_EXPR
e4a7640a 4510 || code == POSTINCREMENT_EXPR)
4511 ? lv_increment
4512 : lv_decrement)))
a75b1c71 4513 return error_mark_node;
4514
9f1b7d17 4515 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4516 {
4517 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4518 warning_at (location, OPT_Wc___compat,
4519 "increment of enumeration value is invalid in C++");
4520 else
4521 warning_at (location, OPT_Wc___compat,
4522 "decrement of enumeration value is invalid in C++");
4523 }
4524
481ce481 4525 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4526 {
4527 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4528 warning_at (location, OPT_Wbool_operation,
4529 "increment of a boolean expression");
4530 else
4531 warning_at (location, OPT_Wbool_operation,
4532 "decrement of a boolean expression");
4533 }
4534
a75b1c71 4535 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
69cd03b2 4536 arg = c_fully_fold (arg, false, NULL, true);
a75b1c71 4537
b560fabd 4538 bool atomic_op;
4539 atomic_op = really_atomic_lvalue (arg);
4540
276beea2 4541 /* Increment or decrement the real part of the value,
4542 and don't change the imaginary part. */
4543 if (typecode == COMPLEX_TYPE)
628fa4f8 4544 {
276beea2 4545 tree real, imag;
4546
29438999 4547 pedwarn (location, OPT_Wpedantic,
21ca8540 4548 "ISO C does not support %<++%> and %<--%> on complex types");
276beea2 4549
b560fabd 4550 if (!atomic_op)
4551 {
4552 arg = stabilize_reference (arg);
b99cc6da 4553 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4554 true);
4555 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4556 true);
4557 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
b560fabd 4558 if (real == error_mark_node || imag == error_mark_node)
4559 return error_mark_node;
4560 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4561 real, imag);
4562 goto return_build_unary_op;
4563 }
628fa4f8 4564 }
276beea2 4565
4566 /* Report invalid types. */
4567
9421ebb9 4568 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
b560fabd 4569 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
73fd9cbe 4570 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
628fa4f8 4571 {
276beea2 4572 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
b6889cb0 4573 error_at (location, "wrong type argument to increment");
a0c938f0 4574 else
b6889cb0 4575 error_at (location, "wrong type argument to decrement");
276beea2 4576
4577 return error_mark_node;
628fa4f8 4578 }
628fa4f8 4579
276beea2 4580 {
4581 tree inc;
628fa4f8 4582
276beea2 4583 argtype = TREE_TYPE (arg);
4584
4585 /* Compute the increment. */
4586
4587 if (typecode == POINTER_TYPE)
4588 {
d44f2f7c 4589 /* If pointer target is an incomplete type,
276beea2 4590 we just cannot know how to do the arithmetic. */
20a44a69 4591 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
276beea2 4592 {
4593 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
b6889cb0 4594 error_at (location,
d44f2f7c 4595 "increment of pointer to an incomplete type %qT",
4596 TREE_TYPE (argtype));
276beea2 4597 else
b6889cb0 4598 error_at (location,
d44f2f7c 4599 "decrement of pointer to an incomplete type %qT",
4600 TREE_TYPE (argtype));
276beea2 4601 }
20a44a69 4602 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4603 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
a0c938f0 4604 {
276beea2 4605 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
9205a6cc 4606 pedwarn (location, OPT_Wpointer_arith,
8864917d 4607 "wrong type argument to increment");
276beea2 4608 else
9205a6cc 4609 pedwarn (location, OPT_Wpointer_arith,
8864917d 4610 "wrong type argument to decrement");
276beea2 4611 }
4612
20a44a69 4613 inc = c_size_in_bytes (TREE_TYPE (argtype));
a0553bff 4614 inc = convert_to_ptrofftype_loc (location, inc);
276beea2 4615 }
20a44a69 4616 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
9421ebb9 4617 {
4618 /* For signed fract types, we invert ++ to -- or
4619 -- to ++, and change inc from 1 to -1, because
4620 it is not possible to represent 1 in signed fract constants.
4621 For unsigned fract types, the result always overflows and
4622 we get an undefined (original) or the maximum value. */
4623 if (code == PREINCREMENT_EXPR)
4624 code = PREDECREMENT_EXPR;
4625 else if (code == PREDECREMENT_EXPR)
4626 code = PREINCREMENT_EXPR;
4627 else if (code == POSTINCREMENT_EXPR)
4628 code = POSTDECREMENT_EXPR;
4629 else /* code == POSTDECREMENT_EXPR */
4630 code = POSTINCREMENT_EXPR;
4631
4632 inc = integer_minus_one_node;
4633 inc = convert (argtype, inc);
4634 }
276beea2 4635 else
0de36bdb 4636 {
73fd9cbe 4637 inc = VECTOR_TYPE_P (argtype)
4638 ? build_one_cst (argtype)
4639 : integer_one_node;
0de36bdb 4640 inc = convert (argtype, inc);
4641 }
276beea2 4642
e4a7640a 4643 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4644 need to ask Objective-C to build the increment or decrement
4645 expression for it. */
4646 if (objc_is_property_ref (arg))
6cf89e04 4647 return objc_build_incr_expr_for_property_ref (location, code,
e4a7640a 4648 arg, inc);
4649
276beea2 4650 /* Report a read-only lvalue. */
d4e60318 4651 if (TYPE_READONLY (argtype))
1e8e9920 4652 {
f2697631 4653 readonly_error (location, arg,
1e8e9920 4654 ((code == PREINCREMENT_EXPR
4655 || code == POSTINCREMENT_EXPR)
4656 ? lv_increment : lv_decrement));
4657 return error_mark_node;
4658 }
d4e60318 4659 else if (TREE_READONLY (arg))
4660 readonly_warning (arg,
4661 ((code == PREINCREMENT_EXPR
4662 || code == POSTINCREMENT_EXPR)
4663 ? lv_increment : lv_decrement));
276beea2 4664
b560fabd 4665 /* If the argument is atomic, use the special code sequences for
4666 atomic compound assignment. */
4667 if (atomic_op)
4668 {
4669 arg = stabilize_reference (arg);
4670 ret = build_atomic_assign (location, arg,
4671 ((code == PREINCREMENT_EXPR
4672 || code == POSTINCREMENT_EXPR)
4673 ? PLUS_EXPR
4674 : MINUS_EXPR),
4675 (FRACT_MODE_P (TYPE_MODE (argtype))
4676 ? inc
4677 : integer_one_node),
4678 (code == POSTINCREMENT_EXPR
4679 || code == POSTDECREMENT_EXPR));
4680 goto return_build_unary_op;
4681 }
4682
276beea2 4683 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4684 val = boolean_increment (code, arg);
4685 else
14ae0310 4686 val = build2 (code, TREE_TYPE (arg), arg, inc);
276beea2 4687 TREE_SIDE_EFFECTS (val) = 1;
276beea2 4688 if (TREE_CODE (val) != code)
4ee9c684 4689 TREE_NO_WARNING (val) = 1;
b6889cb0 4690 ret = val;
4691 goto return_build_unary_op;
276beea2 4692 }
4693
4694 case ADDR_EXPR:
4695 /* Note that this operation never does default_conversion. */
4696
1b157e43 4697 /* The operand of unary '&' must be an lvalue (which excludes
4698 expressions of type void), or, in C99, the result of a [] or
4699 unary '*' operator. */
4700 if (VOID_TYPE_P (TREE_TYPE (arg))
4701 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
aa3e402e 4702 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
1b157e43 4703 pedwarn (location, 0, "taking address of expression of type %<void%>");
4704
276beea2 4705 /* Let &* cancel out to simplify resulting code. */
aa3e402e 4706 if (INDIRECT_REF_P (arg))
628fa4f8 4707 {
276beea2 4708 /* Don't let this be an lvalue. */
4709 if (lvalue_p (TREE_OPERAND (arg, 0)))
389dd41b 4710 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
b6889cb0 4711 ret = TREE_OPERAND (arg, 0);
4712 goto return_build_unary_op;
628fa4f8 4713 }
a4110d9a 4714
276beea2 4715 /* Anything not already handled and not a true memory reference
4716 or a non-lvalue array is an error. */
89bcb5a3 4717 if (typecode != FUNCTION_TYPE && !noconvert
292237f3 4718 && !lvalue_or_else (location, arg, lv_addressof))
276beea2 4719 return error_mark_node;
6b854be5 4720
a75b1c71 4721 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4722 folding later. */
4723 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4724 {
4725 tree inner = build_unary_op (location, code,
89bcb5a3 4726 C_MAYBE_CONST_EXPR_EXPR (arg),
4727 noconvert);
a75b1c71 4728 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4729 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4730 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4731 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4732 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4733 goto return_build_unary_op;
4734 }
4735
276beea2 4736 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4737 argtype = TREE_TYPE (arg);
628fa4f8 4738
276beea2 4739 /* If the lvalue is const or volatile, merge that into the type
8ae93b58 4740 to which the address will point. This is only needed
51e25433 4741 for function types. */
ce45a448 4742 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
8ae93b58 4743 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4744 && TREE_CODE (argtype) == FUNCTION_TYPE)
51e25433 4745 {
4746 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4747 int quals = orig_quals;
4748
4749 if (TREE_READONLY (arg))
4750 quals |= TYPE_QUAL_CONST;
4751 if (TREE_THIS_VOLATILE (arg))
4752 quals |= TYPE_QUAL_VOLATILE;
4753
51e25433 4754 argtype = c_build_qualified_type (argtype, quals);
4755 }
628fa4f8 4756
292237f3 4757 switch (TREE_CODE (arg))
4758 {
4759 case COMPONENT_REF:
4760 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4761 {
eac243e7 4762 error_at (location, "cannot take address of bit-field %qD",
4763 TREE_OPERAND (arg, 1));
292237f3 4764 return error_mark_node;
4765 }
4766
e3533433 4767 /* fall through */
292237f3 4768
4769 case ARRAY_REF:
4770 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4771 {
4772 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4773 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4774 {
eac243e7 4775 error_at (location, "cannot take address of scalar with "
4776 "reverse storage order");
292237f3 4777 return error_mark_node;
4778 }
4779
4780 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4781 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
eac243e7 4782 warning_at (location, OPT_Wscalar_storage_order,
4783 "address of array with reverse scalar storage "
4784 "order requested");
292237f3 4785 }
4786
4787 default:
4788 break;
4789 }
4790
276beea2 4791 if (!c_mark_addressable (arg))
4792 return error_mark_node;
628fa4f8 4793
43d08288 4794 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4795 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
628fa4f8 4796
7da1265c 4797 argtype = build_pointer_type (argtype);
3ce29533 4798
4799 /* ??? Cope with user tricks that amount to offsetof. Delete this
4800 when we have proper support for integer constant expressions. */
4801 val = get_base_address (arg);
aa3e402e 4802 if (val && INDIRECT_REF_P (val)
120c6697 4803 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4804 {
3c43ed34 4805 ret = fold_offsetof (arg, argtype);
b6889cb0 4806 goto return_build_unary_op;
120c6697 4807 }
3ce29533 4808
7da1265c 4809 val = build1 (ADDR_EXPR, argtype, arg);
628fa4f8 4810
b6889cb0 4811 ret = val;
4812 goto return_build_unary_op;
628fa4f8 4813
276beea2 4814 default:
c44afe23 4815 gcc_unreachable ();
276beea2 4816 }
628fa4f8 4817
72749341 4818 if (argtype == NULL_TREE)
276beea2 4819 argtype = TREE_TYPE (arg);
a75b1c71 4820 if (TREE_CODE (arg) == INTEGER_CST)
4821 ret = (require_constant_value
389dd41b 4822 ? fold_build1_initializer_loc (location, code, argtype, arg)
4823 : fold_build1_loc (location, code, argtype, arg));
a75b1c71 4824 else
4825 ret = build1 (code, argtype, arg);
b6889cb0 4826 return_build_unary_op:
4827 gcc_assert (ret != error_mark_node);
a75b1c71 4828 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4829 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4830 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4831 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4832 ret = note_integer_operands (ret);
c6418a4e 4833 if (eptype)
4834 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
b6889cb0 4835 protected_set_expr_location (ret, location);
4836 return ret;
276beea2 4837}
628fa4f8 4838
276beea2 4839/* Return nonzero if REF is an lvalue valid for this language.
4840 Lvalues can be assigned, unless their type has TYPE_READONLY.
7746224a 4841 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
628fa4f8 4842
79973b57 4843bool
9f627b1a 4844lvalue_p (const_tree ref)
276beea2 4845{
9f627b1a 4846 const enum tree_code code = TREE_CODE (ref);
628fa4f8 4847
276beea2 4848 switch (code)
4849 {
4850 case REALPART_EXPR:
4851 case IMAGPART_EXPR:
4852 case COMPONENT_REF:
4853 return lvalue_p (TREE_OPERAND (ref, 0));
628fa4f8 4854
a75b1c71 4855 case C_MAYBE_CONST_EXPR:
4856 return lvalue_p (TREE_OPERAND (ref, 1));
4857
276beea2 4858 case COMPOUND_LITERAL_EXPR:
4859 case STRING_CST:
d80c1c6c 4860 return true;
628fa4f8 4861
276beea2 4862 case INDIRECT_REF:
4863 case ARRAY_REF:
4864 case VAR_DECL:
4865 case PARM_DECL:
4866 case RESULT_DECL:
4867 case ERROR_MARK:
4868 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4869 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5d844ba2 4870
276beea2 4871 case BIND_EXPR:
276beea2 4872 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5d844ba2 4873
276beea2 4874 default:
d80c1c6c 4875 return false;
276beea2 4876 }
4877}
628fa4f8 4878\f
d4e60318 4879/* Give a warning for storing in something that is read-only in GCC
4880 terms but not const in ISO C terms. */
4881
4882static void
4883readonly_warning (tree arg, enum lvalue_use use)
4884{
4885 switch (use)
4886 {
4887 case lv_assign:
4888 warning (0, "assignment of read-only location %qE", arg);
4889 break;
4890 case lv_increment:
4891 warning (0, "increment of read-only location %qE", arg);
4892 break;
4893 case lv_decrement:
4894 warning (0, "decrement of read-only location %qE", arg);
4895 break;
4896 default:
4897 gcc_unreachable ();
4898 }
4899 return;
4900}
4901
e35976b1 4902
4903/* Return nonzero if REF is an lvalue valid for this language;
4904 otherwise, print an error message and return zero. USE says
fdd84b77 4905 how the lvalue is being used and so selects the error message.
4906 LOCATION is the location at which any error should be reported. */
e35976b1 4907
4908static int
fdd84b77 4909lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
e35976b1 4910{
4911 int win = lvalue_p (ref);
4912
4913 if (!win)
fdd84b77 4914 lvalue_error (loc, use);
e35976b1 4915
4916 return win;
4917}
276beea2 4918\f
4919/* Mark EXP saying that we need to be able to take the
4920 address of it; it should not be allocated in a register.
3385a573 4921 Returns true if successful. ARRAY_REF_P is true if this
4922 is for ARRAY_REF construction - in that case we don't want
4923 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4924 it is fine to use ARRAY_REFs for vector subscripts on vector
4925 register variables. */
e4d4d9ae 4926
276beea2 4927bool
3385a573 4928c_mark_addressable (tree exp, bool array_ref_p)
628fa4f8 4929{
276beea2 4930 tree x = exp;
277f8686 4931
276beea2 4932 while (1)
4933 switch (TREE_CODE (x))
4934 {
3385a573 4935 case VIEW_CONVERT_EXPR:
4936 if (array_ref_p
4937 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4938 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4939 return true;
4940 /* FALLTHRU */
276beea2 4941 case COMPONENT_REF:
276beea2 4942 case ADDR_EXPR:
4943 case ARRAY_REF:
4944 case REALPART_EXPR:
4945 case IMAGPART_EXPR:
4946 x = TREE_OPERAND (x, 0);
4947 break;
277f8686 4948
276beea2 4949 case COMPOUND_LITERAL_EXPR:
be4b81f3 4950 TREE_ADDRESSABLE (x) = 1;
4951 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
4952 return true;
4953
276beea2 4954 case CONSTRUCTOR:
4955 TREE_ADDRESSABLE (x) = 1;
4956 return true;
277f8686 4957
276beea2 4958 case VAR_DECL:
4959 case CONST_DECL:
4960 case PARM_DECL:
4961 case RESULT_DECL:
7746224a 4962 if (C_DECL_REGISTER (x)
276beea2 4963 && DECL_NONLOCAL (x))
4964 {
ce41e81a 4965 if (TREE_PUBLIC (x) || is_global_var (x))
276beea2 4966 {
e3ab5fb6 4967 error
4968 ("global register variable %qD used in nested function", x);
276beea2 4969 return false;
4970 }
21ca8540 4971 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
276beea2 4972 }
7746224a 4973 else if (C_DECL_REGISTER (x))
276beea2 4974 {
ce41e81a 4975 if (TREE_PUBLIC (x) || is_global_var (x))
e3ab5fb6 4976 error ("address of global register variable %qD requested", x);
4977 else
4978 error ("address of register variable %qD requested", x);
4979 return false;
276beea2 4980 }
628fa4f8 4981
e3533433 4982 /* FALLTHRU */
276beea2 4983 case FUNCTION_DECL:
4984 TREE_ADDRESSABLE (x) = 1;
e3533433 4985 /* FALLTHRU */
276beea2 4986 default:
4987 return true;
4988 }
4989}
4990\f
8f45ab4d 4991/* Convert EXPR to TYPE, warning about conversion problems with
4992 constants. SEMANTIC_TYPE is the type this conversion would use
4993 without excess precision. If SEMANTIC_TYPE is NULL, this function
4994 is equivalent to convert_and_check. This function is a wrapper that
4995 handles conversions that may be different than
4996 the usual ones because of excess precision. */
4997
4998static tree
22a75734 4999ep_convert_and_check (location_t loc, tree type, tree expr,
5000 tree semantic_type)
8f45ab4d 5001{
5002 if (TREE_TYPE (expr) == type)
5003 return expr;
5004
3507457a 5005 /* For C11, integer conversions may have results with excess
5006 precision. */
5007 if (flag_isoc11 || !semantic_type)
22a75734 5008 return convert_and_check (loc, type, expr);
8f45ab4d 5009
5010 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5011 && TREE_TYPE (expr) != semantic_type)
5012 {
5013 /* For integers, we need to check the real conversion, not
5014 the conversion to the excess precision type. */
22a75734 5015 expr = convert_and_check (loc, semantic_type, expr);
8f45ab4d 5016 }
5017 /* Result type is the excess precision type, which should be
5018 large enough, so do not check. */
5019 return convert (type, expr);
5020}
5021
16edb1bf 5022/* If EXPR refers to a built-in declared without a prototype returns
5023 the actual type of the built-in and, if non-null, set *BLTIN to
5024 a pointer to the built-in. Otherwise return the type of EXPR
5025 and clear *BLTIN if non-null. */
5026
5027static tree
5028type_or_builtin_type (tree expr, tree *bltin = NULL)
5029{
5030 tree dummy;
5031 if (!bltin)
5032 bltin = &dummy;
5033
5034 *bltin = NULL_TREE;
5035
5036 tree type = TREE_TYPE (expr);
5037 if (TREE_CODE (expr) != ADDR_EXPR)
5038 return type;
5039
5040 tree oper = TREE_OPERAND (expr, 0);
5041 if (!DECL_P (oper)
5042 || TREE_CODE (oper) != FUNCTION_DECL
5043 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5044 return type;
5045
5046 built_in_function code = DECL_FUNCTION_CODE (oper);
5047 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5048 return type;
5049
5050 if ((*bltin = builtin_decl_implicit (code)))
5051 type = build_pointer_type (TREE_TYPE (*bltin));
5052
5053 return type;
5054}
5055
a75b1c71 5056/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5057 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5058 if folded to an integer constant then the unselected half may
5059 contain arbitrary operations not normally permitted in constant
e60a6f7b 5060 expressions. Set the location of the expression to LOC. */
628fa4f8 5061
5062tree
d5b637fa 5063build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
cb4d9ee2 5064 tree op1, tree op1_original_type, location_t op1_loc,
5065 tree op2, tree op2_original_type, location_t op2_loc)
628fa4f8 5066{
276beea2 5067 tree type1;
5068 tree type2;
5069 enum tree_code code1;
5070 enum tree_code code2;
5071 tree result_type = NULL;
8f45ab4d 5072 tree semantic_result_type = NULL;
276beea2 5073 tree orig_op1 = op1, orig_op2 = op2;
a75b1c71 5074 bool int_const, op1_int_operands, op2_int_operands, int_operands;
9a8bed72 5075 bool ifexp_int_operands;
a75b1c71 5076 tree ret;
628fa4f8 5077
9a8bed72 5078 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5079 if (op1_int_operands)
5080 op1 = remove_c_maybe_const_expr (op1);
5081 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5082 if (op2_int_operands)
5083 op2 = remove_c_maybe_const_expr (op2);
5084 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5085 if (ifexp_int_operands)
5086 ifexp = remove_c_maybe_const_expr (ifexp);
5087
276beea2 5088 /* Promote both alternatives. */
5089
5090 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5091 op1 = default_conversion (op1);
5092 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5093 op2 = default_conversion (op2);
5094
5095 if (TREE_CODE (ifexp) == ERROR_MARK
5096 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5097 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
628fa4f8 5098 return error_mark_node;
628fa4f8 5099
16edb1bf 5100 tree bltin1 = NULL_TREE;
5101 tree bltin2 = NULL_TREE;
5102 type1 = type_or_builtin_type (op1, &bltin1);
276beea2 5103 code1 = TREE_CODE (type1);
16edb1bf 5104 type2 = type_or_builtin_type (op2, &bltin2);
276beea2 5105 code2 = TREE_CODE (type2);
5106
547c6b1f 5107 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5108 return error_mark_node;
5109
5110 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5111 return error_mark_node;
5112
9b73f131 5113 /* C90 does not permit non-lvalue arrays in conditional expressions.
5114 In C99 they will be pointers by now. */
5115 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5116 {
d5b637fa 5117 error_at (colon_loc, "non-lvalue array in conditional expression");
9b73f131 5118 return error_mark_node;
5119 }
5120
c6418a4e 5121 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5122 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5123 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5124 || code1 == COMPLEX_TYPE)
5125 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5126 || code2 == COMPLEX_TYPE))
5127 {
8f45ab4d 5128 semantic_result_type = c_common_type (type1, type2);
c6418a4e 5129 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5130 {
5131 op1 = TREE_OPERAND (op1, 0);
5132 type1 = TREE_TYPE (op1);
5133 gcc_assert (TREE_CODE (type1) == code1);
5134 }
5135 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5136 {
5137 op2 = TREE_OPERAND (op2, 0);
5138 type2 = TREE_TYPE (op2);
5139 gcc_assert (TREE_CODE (type2) == code2);
5140 }
5141 }
5142
6163a125 5143 if (warn_cxx_compat)
5144 {
5145 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5146 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5147
5148 if (TREE_CODE (t1) == ENUMERAL_TYPE
5149 && TREE_CODE (t2) == ENUMERAL_TYPE
5150 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5151 warning_at (colon_loc, OPT_Wc___compat,
5152 ("different enum types in conditional is "
5153 "invalid in C++: %qT vs %qT"),
5154 t1, t2);
5155 }
5156
276beea2 5157 /* Quickly detect the usual case where op1 and op2 have the same type
5158 after promotion. */
5159 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
628fa4f8 5160 {
276beea2 5161 if (type1 == type2)
5162 result_type = type1;
5163 else
5164 result_type = TYPE_MAIN_VARIANT (type1);
5165 }
5166 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
a0c938f0 5167 || code1 == COMPLEX_TYPE)
5168 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5169 || code2 == COMPLEX_TYPE))
276beea2 5170 {
3507457a 5171 /* In C11, a conditional expression between a floating-point
5172 type and an integer type should convert the integer type to
5173 the evaluation format of the floating-point type, with
5174 possible excess precision. */
5175 tree eptype1 = type1;
5176 tree eptype2 = type2;
5177 if (flag_isoc11)
5178 {
5179 tree eptype;
5180 if (ANY_INTEGRAL_TYPE_P (type1)
5181 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5182 {
5183 eptype2 = eptype;
5184 if (!semantic_result_type)
5185 semantic_result_type = c_common_type (type1, type2);
5186 }
5187 else if (ANY_INTEGRAL_TYPE_P (type2)
5188 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5189 {
5190 eptype1 = eptype;
5191 if (!semantic_result_type)
5192 semantic_result_type = c_common_type (type1, type2);
5193 }
5194 }
5195 result_type = c_common_type (eptype1, eptype2);
e218d07f 5196 if (result_type == error_mark_node)
5197 return error_mark_node;
c920faa3 5198 do_warn_double_promotion (result_type, type1, type2,
5199 "implicit conversion from %qT to %qT to "
5200 "match other result of conditional",
5201 colon_loc);
628fa4f8 5202
276beea2 5203 /* If -Wsign-compare, warn here if type1 and type2 have
5204 different signedness. We'll promote the signed to unsigned
5205 and later code won't know it used to be different.
5206 Do this check on the original types, so that explicit casts
5207 will be considered, but default promotions won't. */
48d94ede 5208 if (c_inhibit_evaluation_warnings == 0)
be2828ce 5209 {
78a8ed03 5210 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5211 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
628fa4f8 5212
276beea2 5213 if (unsigned_op1 ^ unsigned_op2)
5214 {
add6ee5e 5215 bool ovf;
5216
276beea2 5217 /* Do not warn if the result type is signed, since the
5218 signed type will only be chosen if it can represent
5219 all the values of the unsigned type. */
84166705 5220 if (!TYPE_UNSIGNED (result_type))
276beea2 5221 /* OK */;
276beea2 5222 else
a75b1c71 5223 {
5224 bool op1_maybe_const = true;
5225 bool op2_maybe_const = true;
5226
5227 /* Do not warn if the signed quantity is an
5228 unsuffixed integer literal (or some static
5229 constant expression involving such literals) and
5230 it is non-negative. This warning requires the
5231 operands to be folded for best results, so do
5232 that folding in this case even without
5233 warn_sign_compare to avoid warning options
5234 possibly affecting code generation. */
672d914b 5235 c_inhibit_evaluation_warnings
5236 += (ifexp == truthvalue_false_node);
a75b1c71 5237 op1 = c_fully_fold (op1, require_constant_value,
5238 &op1_maybe_const);
672d914b 5239 c_inhibit_evaluation_warnings
5240 -= (ifexp == truthvalue_false_node);
5241
5242 c_inhibit_evaluation_warnings
5243 += (ifexp == truthvalue_true_node);
a75b1c71 5244 op2 = c_fully_fold (op2, require_constant_value,
5245 &op2_maybe_const);
672d914b 5246 c_inhibit_evaluation_warnings
5247 -= (ifexp == truthvalue_true_node);
a75b1c71 5248
5249 if (warn_sign_compare)
5250 {
5251 if ((unsigned_op2
5252 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5253 || (unsigned_op1
5254 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5255 /* OK */;
cb4d9ee2 5256 else if (unsigned_op2)
5257 warning_at (op1_loc, OPT_Wsign_compare,
5258 "operand of ?: changes signedness from "
5259 "%qT to %qT due to unsignedness of other "
5260 "operand", TREE_TYPE (orig_op1),
5261 TREE_TYPE (orig_op2));
a75b1c71 5262 else
cb4d9ee2 5263 warning_at (op2_loc, OPT_Wsign_compare,
5264 "operand of ?: changes signedness from "
5265 "%qT to %qT due to unsignedness of other "
5266 "operand", TREE_TYPE (orig_op2),
5267 TREE_TYPE (orig_op1));
a75b1c71 5268 }
5269 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
f59e3889 5270 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
a75b1c71 5271 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
f59e3889 5272 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
a75b1c71 5273 }
276beea2 5274 }
5275 }
5276 }
5277 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5278 {
8864917d 5279 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
29438999 5280 pedwarn (colon_loc, OPT_Wpedantic,
8864917d 5281 "ISO C forbids conditional expr with only one void side");
276beea2 5282 result_type = void_type_node;
5283 }
5284 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5285 {
6d5d708e 5286 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5287 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5288 addr_space_t as_common;
5289
d5b637fa 5290 if (comp_target_types (colon_loc, type1, type2))
1ea7fa60 5291 result_type = common_pointer_type (type1, type2);
79f925ed 5292 else if (null_pointer_constant_p (orig_op1))
6d5d708e 5293 result_type = type2;
79f925ed 5294 else if (null_pointer_constant_p (orig_op2))
6d5d708e 5295 result_type = type1;
5296 else if (!addr_space_superset (as1, as2, &as_common))
5297 {
5298 error_at (colon_loc, "pointers to disjoint address spaces "
5299 "used in conditional expression");
5300 return error_mark_node;
5301 }
b560fabd 5302 else if (VOID_TYPE_P (TREE_TYPE (type1))
5303 && !TYPE_ATOMIC (TREE_TYPE (type1)))
20407c42 5304 {
8a8211df 5305 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5306 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5307 & ~TYPE_QUALS (TREE_TYPE (type1))))
5308 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5309 "pointer to array loses qualifier "
5310 "in conditional expression");
5311
8864917d 5312 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
29438999 5313 pedwarn (colon_loc, OPT_Wpedantic,
21ca8540 5314 "ISO C forbids conditional expr between "
b0b1af64 5315 "%<void *%> and function pointer");
276beea2 5316 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5317 TREE_TYPE (type2)));
20407c42 5318 }
b560fabd 5319 else if (VOID_TYPE_P (TREE_TYPE (type2))
5320 && !TYPE_ATOMIC (TREE_TYPE (type2)))
a5eb6189 5321 {
8a8211df 5322 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5323 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5324 & ~TYPE_QUALS (TREE_TYPE (type2))))
5325 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5326 "pointer to array loses qualifier "
5327 "in conditional expression");
5328
8864917d 5329 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
29438999 5330 pedwarn (colon_loc, OPT_Wpedantic,
21ca8540 5331 "ISO C forbids conditional expr between "
b0b1af64 5332 "%<void *%> and function pointer");
276beea2 5333 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5334 TREE_TYPE (type1)));
a5eb6189 5335 }
4abfc532 5336 /* Objective-C pointer comparisons are a bit more lenient. */
5337 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5338 result_type = objc_common_type (type1, type2);
20407c42 5339 else
be2828ce 5340 {
6d5d708e 5341 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
16edb1bf 5342 if (bltin1 && bltin2)
5343 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5344 "pointer type mismatch between %qT and %qT "
5345 "of %qD and %qD in conditional expression",
5346 type1, type2, bltin1, bltin2);
5347 else
5348 pedwarn (colon_loc, 0,
5349 "pointer type mismatch in conditional expression");
6d5d708e 5350 result_type = build_pointer_type
5351 (build_qualified_type (void_type_node, qual));
be2828ce 5352 }
276beea2 5353 }
5354 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5355 {
79f925ed 5356 if (!null_pointer_constant_p (orig_op2))
d5b637fa 5357 pedwarn (colon_loc, 0,
21ca8540 5358 "pointer/integer type mismatch in conditional expression");
276beea2 5359 else
be2828ce 5360 {
276beea2 5361 op2 = null_pointer_node;
be2828ce 5362 }
276beea2 5363 result_type = type1;
5364 }
5365 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5366 {
79f925ed 5367 if (!null_pointer_constant_p (orig_op1))
d5b637fa 5368 pedwarn (colon_loc, 0,
21ca8540 5369 "pointer/integer type mismatch in conditional expression");
276beea2 5370 else
be2828ce 5371 {
276beea2 5372 op1 = null_pointer_node;
be2828ce 5373 }
276beea2 5374 result_type = type2;
5375 }
a5eb6189 5376
276beea2 5377 if (!result_type)
5378 {
5379 if (flag_cond_mismatch)
5380 result_type = void_type_node;
5381 else
628fa4f8 5382 {
e60a6f7b 5383 error_at (colon_loc, "type mismatch in conditional expression");
be2828ce 5384 return error_mark_node;
628fa4f8 5385 }
276beea2 5386 }
628fa4f8 5387
276beea2 5388 /* Merge const and volatile flags of the incoming types. */
5389 result_type
5390 = build_type_variant (result_type,
a6b4313a 5391 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5392 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
6b854be5 5393
22a75734 5394 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5395 semantic_result_type);
5396 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5397 semantic_result_type);
6b854be5 5398
a75b1c71 5399 if (ifexp_bcp && ifexp == truthvalue_true_node)
5400 {
5401 op2_int_operands = true;
5402 op1 = c_fully_fold (op1, require_constant_value, NULL);
5403 }
5404 if (ifexp_bcp && ifexp == truthvalue_false_node)
5405 {
5406 op1_int_operands = true;
5407 op2 = c_fully_fold (op2, require_constant_value, NULL);
5408 }
9a8bed72 5409 int_const = int_operands = (ifexp_int_operands
a75b1c71 5410 && op1_int_operands
5411 && op2_int_operands);
5412 if (int_operands)
5413 {
5414 int_const = ((ifexp == truthvalue_true_node
5415 && TREE_CODE (orig_op1) == INTEGER_CST
5416 && !TREE_OVERFLOW (orig_op1))
5417 || (ifexp == truthvalue_false_node
5418 && TREE_CODE (orig_op2) == INTEGER_CST
5419 && !TREE_OVERFLOW (orig_op2)));
5420 }
1f137e6d 5421
5422 /* Need to convert condition operand into a vector mask. */
5423 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5424 {
5425 tree vectype = TREE_TYPE (ifexp);
5426 tree elem_type = TREE_TYPE (vectype);
5427 tree zero = build_int_cst (elem_type, 0);
5428 tree zero_vec = build_vector_from_val (vectype, zero);
5429 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5430 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5431 }
5432
a75b1c71 5433 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
389dd41b 5434 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
a75b1c71 5435 else
5436 {
7c2df6db 5437 if (int_operands)
5438 {
ec431042 5439 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5440 nested inside of the expression. */
5441 op1 = c_fully_fold (op1, false, NULL);
5442 op2 = c_fully_fold (op2, false, NULL);
7c2df6db 5443 }
a75b1c71 5444 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5445 if (int_operands)
5446 ret = note_integer_operands (ret);
5447 }
8f45ab4d 5448 if (semantic_result_type)
5449 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
a75b1c71 5450
e60a6f7b 5451 protected_set_expr_location (ret, colon_loc);
3ef7eab1 5452
5453 /* If the OP1 and OP2 are the same and don't have side-effects,
5454 warn here, because the COND_EXPR will be turned into OP1. */
5455 if (warn_duplicated_branches
5456 && TREE_CODE (ret) == COND_EXPR
5457 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5458 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5459 "this condition has identical branches");
5460
a75b1c71 5461 return ret;
276beea2 5462}
5463\f
066f26bf 5464/* Return a compound expression that performs two expressions and
e60a6f7b 5465 returns the value of the second of them.
5466
5467 LOC is the location of the COMPOUND_EXPR. */
628fa4f8 5468
276beea2 5469tree
e60a6f7b 5470build_compound_expr (location_t loc, tree expr1, tree expr2)
276beea2 5471{
9a8bed72 5472 bool expr1_int_operands, expr2_int_operands;
c6418a4e 5473 tree eptype = NULL_TREE;
a75b1c71 5474 tree ret;
5475
9a8bed72 5476 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5477 if (expr1_int_operands)
5478 expr1 = remove_c_maybe_const_expr (expr1);
5479 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5480 if (expr2_int_operands)
5481 expr2 = remove_c_maybe_const_expr (expr2);
5482
c6418a4e 5483 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5484 expr1 = TREE_OPERAND (expr1, 0);
5485 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5486 {
5487 eptype = TREE_TYPE (expr2);
5488 expr2 = TREE_OPERAND (expr2, 0);
5489 }
5490
84166705 5491 if (!TREE_SIDE_EFFECTS (expr1))
276beea2 5492 {
5493 /* The left-hand operand of a comma expression is like an expression
2006d7dc 5494 statement: with -Wunused, we should warn if it doesn't have
276beea2 5495 any side-effects, unless it was explicitly cast to (void). */
aaa8f689 5496 if (warn_unused_value)
778e3b56 5497 {
aaa8f689 5498 if (VOID_TYPE_P (TREE_TYPE (expr1))
72dd6141 5499 && CONVERT_EXPR_P (expr1))
778e3b56 5500 ; /* (void) a, b */
aaa8f689 5501 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5502 && TREE_CODE (expr1) == COMPOUND_EXPR
72dd6141 5503 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
778e3b56 5504 ; /* (void) a, (void) b, c */
5505 else
48e1416a 5506 warning_at (loc, OPT_Wunused_value,
e60a6f7b 5507 "left-hand operand of comma expression has no effect");
778e3b56 5508 }
276beea2 5509 }
9a9db3c1 5510 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5511 && warn_unused_value)
5512 {
5513 tree r = expr1;
5514 location_t cloc = loc;
5515 while (TREE_CODE (r) == COMPOUND_EXPR)
5516 {
5517 if (EXPR_HAS_LOCATION (r))
5518 cloc = EXPR_LOCATION (r);
5519 r = TREE_OPERAND (r, 1);
5520 }
5521 if (!TREE_SIDE_EFFECTS (r)
5522 && !VOID_TYPE_P (TREE_TYPE (r))
5523 && !CONVERT_EXPR_P (r))
5524 warning_at (cloc, OPT_Wunused_value,
5525 "right-hand operand of comma expression has no effect");
5526 }
628fa4f8 5527
276beea2 5528 /* With -Wunused, we should also warn if the left-hand operand does have
5529 side-effects, but computes a value which is not used. For example, in
5530 `foo() + bar(), baz()' the result of the `+' operator is not used,
5531 so we should issue a warning. */
5532 else if (warn_unused_value)
e60a6f7b 5533 warn_if_unused_value (expr1, loc);
628fa4f8 5534
47adf96b 5535 if (expr2 == error_mark_node)
5536 return error_mark_node;
5537
a75b1c71 5538 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5539
5540 if (flag_isoc99
9a8bed72 5541 && expr1_int_operands
5542 && expr2_int_operands)
a75b1c71 5543 ret = note_integer_operands (ret);
5544
c6418a4e 5545 if (eptype)
5546 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5547
e60a6f7b 5548 protected_set_expr_location (ret, loc);
a75b1c71 5549 return ret;
276beea2 5550}
628fa4f8 5551
402ba866 5552/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5553 which we are casting. OTYPE is the type of the expression being
80373d81 5554 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5555 of the cast. -Wcast-qual appeared on the command line. Named
5556 address space qualifiers are not handled here, because they result
5557 in different warnings. */
402ba866 5558
5559static void
80373d81 5560handle_warn_cast_qual (location_t loc, tree type, tree otype)
402ba866 5561{
5562 tree in_type = type;
5563 tree in_otype = otype;
5564 int added = 0;
5565 int discarded = 0;
5566 bool is_const;
5567
5568 /* Check that the qualifiers on IN_TYPE are a superset of the
5569 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5570 nodes is uninteresting and we stop as soon as we hit a
5571 non-POINTER_TYPE node on either type. */
5572 do
5573 {
5574 in_otype = TREE_TYPE (in_otype);
5575 in_type = TREE_TYPE (in_type);
5576
5577 /* GNU C allows cv-qualified function types. 'const' means the
5578 function is very pure, 'volatile' means it can't return. We
5579 need to warn when such qualifiers are added, not when they're
5580 taken away. */
5581 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5582 && TREE_CODE (in_type) == FUNCTION_TYPE)
6d5d708e 5583 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5584 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
402ba866 5585 else
6d5d708e 5586 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5587 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
402ba866 5588 }
5589 while (TREE_CODE (in_type) == POINTER_TYPE
5590 && TREE_CODE (in_otype) == POINTER_TYPE);
5591
5592 if (added)
80373d81 5593 warning_at (loc, OPT_Wcast_qual,
5594 "cast adds %q#v qualifier to function type", added);
402ba866 5595
5596 if (discarded)
5597 /* There are qualifiers present in IN_OTYPE that are not present
5598 in IN_TYPE. */
80373d81 5599 warning_at (loc, OPT_Wcast_qual,
f40f5e0e 5600 "cast discards %qv qualifier from pointer target type",
80373d81 5601 discarded);
402ba866 5602
5603 if (added || discarded)
5604 return;
5605
5606 /* A cast from **T to const **T is unsafe, because it can cause a
5607 const value to be changed with no additional warning. We only
5608 issue this warning if T is the same on both sides, and we only
5609 issue the warning if there are the same number of pointers on
5610 both sides, as otherwise the cast is clearly unsafe anyhow. A
5611 cast is unsafe when a qualifier is added at one level and const
5612 is not present at all outer levels.
5613
5614 To issue this warning, we check at each level whether the cast
5615 adds new qualifiers not already seen. We don't need to special
5616 case function types, as they won't have the same
5617 TYPE_MAIN_VARIANT. */
5618
5619 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5620 return;
5621 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5622 return;
5623
5624 in_type = type;
5625 in_otype = otype;
5626 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5627 do
5628 {
5629 in_type = TREE_TYPE (in_type);
5630 in_otype = TREE_TYPE (in_otype);
5631 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5632 && !is_const)
5633 {
80373d81 5634 warning_at (loc, OPT_Wcast_qual,
5635 "to be safe all intermediate pointers in cast from "
5636 "%qT to %qT must be %<const%> qualified",
5637 otype, type);
402ba866 5638 break;
5639 }
5640 if (is_const)
5641 is_const = TYPE_READONLY (in_type);
5642 }
5643 while (TREE_CODE (in_type) == POINTER_TYPE);
5644}
5645
d76c514d 5646/* Heuristic check if two parameter types can be considered ABI-equivalent. */
5647
5648static bool
5649c_safe_arg_type_equiv_p (tree t1, tree t2)
5650{
5651 t1 = TYPE_MAIN_VARIANT (t1);
5652 t2 = TYPE_MAIN_VARIANT (t2);
5653
5654 if (TREE_CODE (t1) == POINTER_TYPE
5655 && TREE_CODE (t2) == POINTER_TYPE)
5656 return true;
5657
5658 /* The signedness of the parameter matters only when an integral
5659 type smaller than int is promoted to int, otherwise only the
5660 precision of the parameter matters.
5661 This check should make sure that the callee does not see
5662 undefined values in argument registers. */
5663 if (INTEGRAL_TYPE_P (t1)
5664 && INTEGRAL_TYPE_P (t2)
5665 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5666 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5667 || !targetm.calls.promote_prototypes (NULL_TREE)
5668 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5669 return true;
5670
5671 return comptypes (t1, t2);
5672}
5673
5674/* Check if a type cast between two function types can be considered safe. */
5675
5676static bool
5677c_safe_function_type_cast_p (tree t1, tree t2)
5678{
5679 if (TREE_TYPE (t1) == void_type_node &&
5680 TYPE_ARG_TYPES (t1) == void_list_node)
5681 return true;
5682
5683 if (TREE_TYPE (t2) == void_type_node &&
5684 TYPE_ARG_TYPES (t2) == void_list_node)
5685 return true;
5686
5687 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5688 return false;
5689
5690 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5691 t1 && t2;
5692 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5693 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5694 return false;
5695
5696 return true;
5697}
5698
48e1416a 5699/* Build an expression representing a cast to type TYPE of expression EXPR.
e60a6f7b 5700 LOC is the location of the cast-- typically the open paren of the cast. */
628fa4f8 5701
276beea2 5702tree
e60a6f7b 5703build_c_cast (location_t loc, tree type, tree expr)
276beea2 5704{
c6418a4e 5705 tree value;
5706
5707 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5708 expr = TREE_OPERAND (expr, 0);
5709
5710 value = expr;
628fa4f8 5711
276beea2 5712 if (type == error_mark_node || expr == error_mark_node)
5713 return error_mark_node;
628fa4f8 5714
276beea2 5715 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5716 only in <protocol> qualifications. But when constructing cast expressions,
5717 the protocols do matter and must be kept around. */
e0d844be 5718 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5719 return build1 (NOP_EXPR, type, expr);
5720
5721 type = TYPE_MAIN_VARIANT (type);
628fa4f8 5722
276beea2 5723 if (TREE_CODE (type) == ARRAY_TYPE)
5724 {
e60a6f7b 5725 error_at (loc, "cast specifies array type");
276beea2 5726 return error_mark_node;
5727 }
628fa4f8 5728
276beea2 5729 if (TREE_CODE (type) == FUNCTION_TYPE)
5730 {
e60a6f7b 5731 error_at (loc, "cast specifies function type");
276beea2 5732 return error_mark_node;
5733 }
628fa4f8 5734
f831c98f 5735 if (!VOID_TYPE_P (type))
5736 {
22a3f7bd 5737 value = require_complete_type (loc, value);
f831c98f 5738 if (value == error_mark_node)
5739 return error_mark_node;
5740 }
5741
276beea2 5742 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5743 {
ef61516b 5744 if (RECORD_OR_UNION_TYPE_P (type))
29438999 5745 pedwarn (loc, OPT_Wpedantic,
8864917d 5746 "ISO C forbids casting nonscalar to the same type");
005b8fe7 5747
5748 /* Convert to remove any qualifiers from VALUE's type. */
5749 value = convert (type, value);
276beea2 5750 }
5751 else if (TREE_CODE (type) == UNION_TYPE)
5752 {
5753 tree field;
628fa4f8 5754
1767a056 5755 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
ae4400fc 5756 if (TREE_TYPE (field) != error_mark_node
5757 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5758 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
276beea2 5759 break;
5760
5761 if (field)
628fa4f8 5762 {
276beea2 5763 tree t;
8c212779 5764 bool maybe_const = true;
276beea2 5765
29438999 5766 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
8c212779 5767 t = c_fully_fold (value, false, &maybe_const);
5768 t = build_constructor_single (type, field, t);
5769 if (!maybe_const)
5770 t = c_wrap_maybe_const (t, true);
5771 t = digest_init (loc, type, t,
b9c74b4d 5772 NULL_TREE, false, true, 0);
276beea2 5773 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5774 return t;
628fa4f8 5775 }
e60a6f7b 5776 error_at (loc, "cast to union type from type not present in union");
276beea2 5777 return error_mark_node;
5778 }
5779 else
5780 {
5781 tree otype, ovalue;
628fa4f8 5782
276beea2 5783 if (type == void_type_node)
e60a6f7b 5784 {
5785 tree t = build1 (CONVERT_EXPR, type, value);
5786 SET_EXPR_LOCATION (t, loc);
5787 return t;
5788 }
628fa4f8 5789
276beea2 5790 otype = TREE_TYPE (value);
628fa4f8 5791
276beea2 5792 /* Optionally warn about potentially worrisome casts. */
276beea2 5793 if (warn_cast_qual
5794 && TREE_CODE (type) == POINTER_TYPE
5795 && TREE_CODE (otype) == POINTER_TYPE)
80373d81 5796 handle_warn_cast_qual (loc, type, otype);
628fa4f8 5797
6d5d708e 5798 /* Warn about conversions between pointers to disjoint
5799 address spaces. */
5800 if (TREE_CODE (type) == POINTER_TYPE
5801 && TREE_CODE (otype) == POINTER_TYPE
5802 && !null_pointer_constant_p (value))
5803 {
5804 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5805 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5806 addr_space_t as_common;
5807
5808 if (!addr_space_superset (as_to, as_from, &as_common))
5809 {
5810 if (ADDR_SPACE_GENERIC_P (as_from))
5811 warning_at (loc, 0, "cast to %s address space pointer "
5812 "from disjoint generic address space pointer",
5813 c_addr_space_name (as_to));
5814
5815 else if (ADDR_SPACE_GENERIC_P (as_to))
5816 warning_at (loc, 0, "cast to generic address space pointer "
5817 "from disjoint %s address space pointer",
5818 c_addr_space_name (as_from));
5819
5820 else
5821 warning_at (loc, 0, "cast to %s address space pointer "
5822 "from disjoint %s address space pointer",
5823 c_addr_space_name (as_to),
5824 c_addr_space_name (as_from));
5825 }
5826 }
5827
276beea2 5828 /* Warn about possible alignment problems. */
7ceee42d 5829 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
276beea2 5830 && TREE_CODE (type) == POINTER_TYPE
5831 && TREE_CODE (otype) == POINTER_TYPE
5832 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5833 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5834 /* Don't warn about opaque types, where the actual alignment
5835 restriction is unknown. */
ef61516b 5836 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
276beea2 5837 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
7ceee42d 5838 && min_align_of_type (TREE_TYPE (type))
5839 > min_align_of_type (TREE_TYPE (otype)))
e60a6f7b 5840 warning_at (loc, OPT_Wcast_align,
5841 "cast increases required alignment of target type");
0dbd1c74 5842
6bf97f82 5843 if (TREE_CODE (type) == INTEGER_TYPE
276beea2 5844 && TREE_CODE (otype) == POINTER_TYPE
b8656032 5845 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
a0c938f0 5846 /* Unlike conversion of integers to pointers, where the
5847 warning is disabled for converting constants because
5848 of cases such as SIG_*, warn about converting constant
5849 pointers to integers. In some cases it may cause unwanted
b8656032 5850 sign extension, and a warning is appropriate. */
e60a6f7b 5851 warning_at (loc, OPT_Wpointer_to_int_cast,
5852 "cast from pointer to integer of different size");
628fa4f8 5853
6bf97f82 5854 if (TREE_CODE (value) == CALL_EXPR
276beea2 5855 && TREE_CODE (type) != TREE_CODE (otype))
e60a6f7b 5856 warning_at (loc, OPT_Wbad_function_cast,
5857 "cast from function call of type %qT "
5858 "to non-matching type %qT", otype, type);
628fa4f8 5859
6bf97f82 5860 if (TREE_CODE (type) == POINTER_TYPE
276beea2 5861 && TREE_CODE (otype) == INTEGER_TYPE
5862 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5863 /* Don't warn about converting any constant. */
5864 && !TREE_CONSTANT (value))
e60a6f7b 5865 warning_at (loc,
5866 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5867 "of different size");
628fa4f8 5868
e6fa0ea6 5869 if (warn_strict_aliasing <= 2)
d9757bb6 5870 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
628fa4f8 5871
48b3d385 5872 /* If pedantic, warn for conversions between function and object
5873 pointer types, except for converting a null pointer constant
5874 to function pointer type. */
5875 if (pedantic
5876 && TREE_CODE (type) == POINTER_TYPE
5877 && TREE_CODE (otype) == POINTER_TYPE
5878 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5879 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
29438999 5880 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
8864917d 5881 "conversion of function pointer to object pointer type");
48b3d385 5882
5883 if (pedantic
5884 && TREE_CODE (type) == POINTER_TYPE
5885 && TREE_CODE (otype) == POINTER_TYPE
5886 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5887 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
79f925ed 5888 && !null_pointer_constant_p (value))
29438999 5889 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
8864917d 5890 "conversion of object pointer to function pointer type");
48b3d385 5891
d76c514d 5892 if (TREE_CODE (type) == POINTER_TYPE
5893 && TREE_CODE (otype) == POINTER_TYPE
5894 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5895 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5896 && !c_safe_function_type_cast_p (TREE_TYPE (type),
5897 TREE_TYPE (otype)))
5898 warning_at (loc, OPT_Wcast_function_type,
5899 "cast between incompatible function types"
5900 " from %qT to %qT", otype, type);
5901
276beea2 5902 ovalue = value;
276beea2 5903 value = convert (type, value);
628fa4f8 5904
276beea2 5905 /* Ignore any integer overflow caused by the cast. */
a75b1c71 5906 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
276beea2 5907 {
ca24357c 5908 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
b468918a 5909 {
ca24357c 5910 if (!TREE_OVERFLOW (value))
5911 {
5912 /* Avoid clobbering a shared constant. */
5913 value = copy_node (value);
5914 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5915 }
b468918a 5916 }
ca24357c 5917 else if (TREE_OVERFLOW (value))
84086eb0 5918 /* Reset VALUE's overflow flags, ensuring constant sharing. */
e3d0f65c 5919 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
276beea2 5920 }
5921 }
628fa4f8 5922
a7ea5e81 5923 /* Don't let a cast be an lvalue. */
b44134dc 5924 if (lvalue_p (value))
389dd41b 5925 value = non_lvalue_loc (loc, value);
0dbd1c74 5926
a75b1c71 5927 /* Don't allow the results of casting to floating-point or complex
5928 types be confused with actual constants, or casts involving
5929 integer and pointer types other than direct integer-to-integer
5930 and integer-to-pointer be confused with integer constant
5931 expressions and null pointer constants. */
5932 if (TREE_CODE (value) == REAL_CST
5933 || TREE_CODE (value) == COMPLEX_CST
5934 || (TREE_CODE (value) == INTEGER_CST
5935 && !((TREE_CODE (expr) == INTEGER_CST
5936 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5937 || TREE_CODE (expr) == REAL_CST
5938 || TREE_CODE (expr) == COMPLEX_CST)))
5939 value = build1 (NOP_EXPR, type, value);
5940
789fbb51 5941 protected_set_expr_location (value, loc);
276beea2 5942 return value;
628fa4f8 5943}
5944
e60a6f7b 5945/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5946 location of the open paren of the cast, or the position of the cast
5947 expr. */
276beea2 5948tree
e60a6f7b 5949c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
628fa4f8 5950{
ac206aff 5951 tree type;
a75b1c71 5952 tree type_expr = NULL_TREE;
5953 bool type_expr_const = true;
5954 tree ret;
276beea2 5955 int saved_wsp = warn_strict_prototypes;
7014838c 5956
276beea2 5957 /* This avoids warnings about unprototyped casts on
5958 integers. E.g. "#define SIG_DFL (void(*)())0". */
5959 if (TREE_CODE (expr) == INTEGER_CST)
5960 warn_strict_prototypes = 0;
a75b1c71 5961 type = groktypename (type_name, &type_expr, &type_expr_const);
276beea2 5962 warn_strict_prototypes = saved_wsp;
7014838c 5963
547c6b1f 5964 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5965 && reject_gcc_builtin (expr))
5966 return error_mark_node;
5967
e60a6f7b 5968 ret = build_c_cast (loc, type, expr);
a75b1c71 5969 if (type_expr)
5970 {
c4b1a13a 5971 bool inner_expr_const = true;
5972 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
a75b1c71 5973 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
c4b1a13a 5974 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5975 && inner_expr_const);
e60a6f7b 5976 SET_EXPR_LOCATION (ret, loc);
a75b1c71 5977 }
0b09525f 5978
789fbb51 5979 if (!EXPR_HAS_LOCATION (ret))
5980 protected_set_expr_location (ret, loc);
0b09525f 5981
ae5ead32 5982 /* C++ does not permits types to be defined in a cast, but it
5983 allows references to incomplete types. */
5984 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
0b09525f 5985 warning_at (loc, OPT_Wc___compat,
5986 "defining a type in a cast is invalid in C++");
5987
a75b1c71 5988 return ret;
628fa4f8 5989}
276beea2 5990\f
5991/* Build an assignment expression of lvalue LHS from value RHS.
8458f4ca 5992 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5993 may differ from TREE_TYPE (LHS) for an enum bitfield.
276beea2 5994 MODIFYCODE is the code for a binary operator that we use
5995 to combine the old value of LHS with RHS to get the new value.
b6889cb0 5996 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
b9c74b4d 5997 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5998 which may differ from TREE_TYPE (RHS) for an enum value.
b6889cb0 5999
e60a6f7b 6000 LOCATION is the location of the MODIFYCODE operator.
6001 RHS_LOC is the location of the RHS. */
2b30d46c 6002
276beea2 6003tree
8458f4ca 6004build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
48e1416a 6005 enum tree_code modifycode,
e60a6f7b 6006 location_t rhs_loc, tree rhs, tree rhs_origtype)
628fa4f8 6007{
276beea2 6008 tree result;
6009 tree newrhs;
1248c663 6010 tree rhseval = NULL_TREE;
276beea2 6011 tree lhstype = TREE_TYPE (lhs);
6012 tree olhstype = lhstype;
a75b1c71 6013 bool npc;
b560fabd 6014 bool is_atomic_op;
0dbd1c74 6015
276beea2 6016 /* Types that aren't fully specified cannot be used in assignments. */
22a3f7bd 6017 lhs = require_complete_type (location, lhs);
0dbd1c74 6018
276beea2 6019 /* Avoid duplicate error messages from operands that had errors. */
6020 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6021 return error_mark_node;
628fa4f8 6022
39f74d66 6023 /* Ensure an error for assigning a non-lvalue array to an array in
6024 C90. */
6025 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6026 {
6027 error_at (location, "assignment to expression with array type");
6028 return error_mark_node;
6029 }
6030
9d9f5bb3 6031 /* For ObjC properties, defer this check. */
fdd84b77 6032 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
ab774101 6033 return error_mark_node;
6034
b560fabd 6035 is_atomic_op = really_atomic_lvalue (lhs);
6036
276beea2 6037 newrhs = rhs;
628fa4f8 6038
a75b1c71 6039 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6040 {
6041 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
e60a6f7b 6042 lhs_origtype, modifycode, rhs_loc, rhs,
8458f4ca 6043 rhs_origtype);
a75b1c71 6044 if (inner == error_mark_node)
6045 return error_mark_node;
6046 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6047 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6048 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6049 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6050 protected_set_expr_location (result, location);
6051 return result;
6052 }
6053
276beea2 6054 /* If a binary op has been requested, combine the old LHS value with the RHS
6055 producing the value we should actually store into the LHS. */
6056
6057 if (modifycode != NOP_EXPR)
628fa4f8 6058 {
69cd03b2 6059 lhs = c_fully_fold (lhs, false, NULL, true);
276beea2 6060 lhs = stabilize_reference (lhs);
b9c74b4d 6061
b560fabd 6062 /* Construct the RHS for any non-atomic compound assignemnt. */
6063 if (!is_atomic_op)
6064 {
1248c663 6065 /* If in LHS op= RHS the RHS has side-effects, ensure they
6066 are preevaluated before the rest of the assignment expression's
6067 side-effects, because RHS could contain e.g. function calls
6068 that modify LHS. */
6069 if (TREE_SIDE_EFFECTS (rhs))
6070 {
de7757ed 6071 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6072 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6073 else
6074 newrhs = save_expr (rhs);
1248c663 6075 rhseval = newrhs;
de7757ed 6076 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6077 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6078 newrhs);
1248c663 6079 }
b560fabd 6080 newrhs = build_binary_op (location,
623ee358 6081 modifycode, lhs, newrhs, true);
b560fabd 6082
6083 /* The original type of the right hand side is no longer
6084 meaningful. */
6085 rhs_origtype = NULL_TREE;
6086 }
628fa4f8 6087 }
628fa4f8 6088
86c110ac 6089 if (c_dialect_objc ())
6090 {
9d9f5bb3 6091 /* Check if we are modifying an Objective-C property reference;
6092 if so, we need to generate setter calls. */
de7757ed 6093 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6094 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6095 else
6096 result = objc_maybe_build_modify_expr (lhs, newrhs);
86c110ac 6097 if (result)
1248c663 6098 goto return_result;
9d9f5bb3 6099
6100 /* Else, do the check that we postponed for Objective-C. */
fdd84b77 6101 if (!lvalue_or_else (location, lhs, lv_assign))
86c110ac 6102 return error_mark_node;
6103 }
6104
056a8ee6 6105 /* Give an error for storing in something that is 'const'. */
874d597d 6106
d4e60318 6107 if (TYPE_READONLY (lhstype)
ef61516b 6108 || (RECORD_OR_UNION_TYPE_P (lhstype)
276beea2 6109 && C_TYPE_FIELDS_READONLY (lhstype)))
1e8e9920 6110 {
f2697631 6111 readonly_error (location, lhs, lv_assign);
1e8e9920 6112 return error_mark_node;
6113 }
d4e60318 6114 else if (TREE_READONLY (lhs))
6115 readonly_warning (lhs, lv_assign);
874d597d 6116
276beea2 6117 /* If storing into a structure or union member,
6118 it has probably been given type `int'.
6119 Compute the type that would go with
6120 the actual amount of storage the member occupies. */
874d597d 6121
276beea2 6122 if (TREE_CODE (lhs) == COMPONENT_REF
6123 && (TREE_CODE (lhstype) == INTEGER_TYPE
6124 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6125 || TREE_CODE (lhstype) == REAL_TYPE
6126 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6127 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
628fa4f8 6128
276beea2 6129 /* If storing in a field that is in actuality a short or narrower than one,
6130 we must store in the field in its actual type. */
6131
6132 if (lhstype != TREE_TYPE (lhs))
6133 {
6134 lhs = copy_node (lhs);
6135 TREE_TYPE (lhs) = lhstype;
628fa4f8 6136 }
628fa4f8 6137
8458f4ca 6138 /* Issue -Wc++-compat warnings about an assignment to an enum type
6139 when LHS does not have its original type. This happens for,
6140 e.g., an enum bitfield in a struct. */
6141 if (warn_cxx_compat
6142 && lhs_origtype != NULL_TREE
6143 && lhs_origtype != lhstype
6144 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6145 {
6146 tree checktype = (rhs_origtype != NULL_TREE
6147 ? rhs_origtype
6148 : TREE_TYPE (rhs));
6149 if (checktype != error_mark_node
b560fabd 6150 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6151 || (is_atomic_op && modifycode != NOP_EXPR)))
8458f4ca 6152 warning_at (location, OPT_Wc___compat,
6153 "enum conversion in assignment is invalid in C++");
6154 }
6155
b560fabd 6156 /* If the lhs is atomic, remove that qualifier. */
6157 if (is_atomic_op)
6158 {
6159 lhstype = build_qualified_type (lhstype,
6160 (TYPE_QUALS (lhstype)
6161 & ~TYPE_QUAL_ATOMIC));
6162 olhstype = build_qualified_type (olhstype,
6163 (TYPE_QUALS (lhstype)
6164 & ~TYPE_QUAL_ATOMIC));
6165 }
6166
c6418a4e 6167 /* Convert new value to destination type. Fold it first, then
6168 restore any excess precision information, for the sake of
6169 conversion warnings. */
628fa4f8 6170
b560fabd 6171 if (!(is_atomic_op && modifycode != NOP_EXPR))
6172 {
de7757ed 6173 tree rhs_semantic_type = NULL_TREE;
6174 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6175 {
6176 rhs_semantic_type = TREE_TYPE (newrhs);
6177 newrhs = TREE_OPERAND (newrhs, 0);
6178 }
b560fabd 6179 npc = null_pointer_constant_p (newrhs);
6180 newrhs = c_fully_fold (newrhs, false, NULL);
6181 if (rhs_semantic_type)
6182 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
22a75734 6183 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6184 rhs_origtype, ic_assign, npc,
6185 NULL_TREE, NULL_TREE, 0);
b560fabd 6186 if (TREE_CODE (newrhs) == ERROR_MARK)
6187 return error_mark_node;
6188 }
628fa4f8 6189
ddb5d39d 6190 /* Emit ObjC write barrier, if necessary. */
6191 if (c_dialect_objc () && flag_objc_gc)
6192 {
6193 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6194 if (result)
b6889cb0 6195 {
6196 protected_set_expr_location (result, location);
1248c663 6197 goto return_result;
b6889cb0 6198 }
ddb5d39d 6199 }
6200
778ac06a 6201 /* Scan operands. */
628fa4f8 6202
b560fabd 6203 if (is_atomic_op)
6204 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6205 else
6206 {
6207 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6208 TREE_SIDE_EFFECTS (result) = 1;
6209 protected_set_expr_location (result, location);
6210 }
628fa4f8 6211
276beea2 6212 /* If we got the LHS in a different type for storing in,
6213 convert the result back to the nominal type of LHS
6214 so that the value we return always has the same type
6215 as the LHS argument. */
5ada9644 6216
276beea2 6217 if (olhstype == TREE_TYPE (result))
1248c663 6218 goto return_result;
b6889cb0 6219
22a75734 6220 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6221 rhs_origtype, ic_assign, false, NULL_TREE,
6222 NULL_TREE, 0);
b6889cb0 6223 protected_set_expr_location (result, location);
1248c663 6224
6225return_result:
6226 if (rhseval)
6227 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
b6889cb0 6228 return result;
276beea2 6229}
6230\f
2fdec027 6231/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6232 This is used to implement -fplan9-extensions. */
6233
6234static bool
6235find_anonymous_field_with_type (tree struct_type, tree type)
6236{
6237 tree field;
6238 bool found;
6239
ef61516b 6240 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
2fdec027 6241 found = false;
6242 for (field = TYPE_FIELDS (struct_type);
6243 field != NULL_TREE;
6244 field = TREE_CHAIN (field))
6245 {
b560fabd 6246 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6247 ? c_build_qualified_type (TREE_TYPE (field),
6248 TYPE_QUAL_ATOMIC)
6249 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
2fdec027 6250 if (DECL_NAME (field) == NULL
b560fabd 6251 && comptypes (type, fieldtype))
2fdec027 6252 {
6253 if (found)
6254 return false;
6255 found = true;
6256 }
6257 else if (DECL_NAME (field) == NULL
ef61516b 6258 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
2fdec027 6259 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6260 {
6261 if (found)
6262 return false;
6263 found = true;
6264 }
6265 }
6266 return found;
6267}
6268
6269/* RHS is an expression whose type is pointer to struct. If there is
6270 an anonymous field in RHS with type TYPE, then return a pointer to
6271 that field in RHS. This is used with -fplan9-extensions. This
6272 returns NULL if no conversion could be found. */
6273
6274static tree
6275convert_to_anonymous_field (location_t location, tree type, tree rhs)
6276{
6277 tree rhs_struct_type, lhs_main_type;
6278 tree field, found_field;
6279 bool found_sub_field;
6280 tree ret;
6281
6282 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6283 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
ef61516b 6284 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
2fdec027 6285
6286 gcc_assert (POINTER_TYPE_P (type));
b560fabd 6287 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6288 ? c_build_qualified_type (TREE_TYPE (type),
6289 TYPE_QUAL_ATOMIC)
6290 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
2fdec027 6291
6292 found_field = NULL_TREE;
6293 found_sub_field = false;
6294 for (field = TYPE_FIELDS (rhs_struct_type);
6295 field != NULL_TREE;
6296 field = TREE_CHAIN (field))
6297 {
6298 if (DECL_NAME (field) != NULL_TREE
ef61516b 6299 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2fdec027 6300 continue;
b560fabd 6301 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6302 ? c_build_qualified_type (TREE_TYPE (field),
6303 TYPE_QUAL_ATOMIC)
6304 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6305 if (comptypes (lhs_main_type, fieldtype))
2fdec027 6306 {
6307 if (found_field != NULL_TREE)
6308 return NULL_TREE;
6309 found_field = field;
6310 }
6311 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6312 lhs_main_type))
6313 {
6314 if (found_field != NULL_TREE)
6315 return NULL_TREE;
6316 found_field = field;
6317 found_sub_field = true;
6318 }
6319 }
6320
6321 if (found_field == NULL_TREE)
6322 return NULL_TREE;
6323
6324 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6325 build_fold_indirect_ref (rhs), found_field,
6326 NULL_TREE);
6327 ret = build_fold_addr_expr_loc (location, ret);
6328
6329 if (found_sub_field)
6330 {
6331 ret = convert_to_anonymous_field (location, type, ret);
6332 gcc_assert (ret != NULL_TREE);
6333 }
6334
6335 return ret;
6336}
6337
d44f2f7c 6338/* Issue an error message for a bad initializer component.
6339 GMSGID identifies the message.
6340 The component name is taken from the spelling stack. */
6341
7214f613 6342static void ATTRIBUTE_GCC_DIAG (2,0)
6343error_init (location_t loc, const char *gmsgid, ...)
d44f2f7c 6344{
6345 char *ofwhat;
6346
bc35ef65 6347 auto_diagnostic_group d;
6348
d44f2f7c 6349 /* The gmsgid may be a format string with %< and %>. */
7214f613 6350 va_list ap;
6351 va_start (ap, gmsgid);
6352 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6353 va_end (ap);
6354
d44f2f7c 6355 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
7214f613 6356 if (*ofwhat && warned)
064b0d02 6357 inform (loc, "(near initialization for %qs)", ofwhat);
d44f2f7c 6358}
6359
6360/* Issue a pedantic warning for a bad initializer component. OPT is
6361 the option OPT_* (from options.h) controlling this warning or 0 if
6362 it is unconditionally given. GMSGID identifies the message. The
6363 component name is taken from the spelling stack. */
6364
88539b96 6365static void ATTRIBUTE_GCC_DIAG (3,0)
6366pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
d44f2f7c 6367{
8bdfbc08 6368 /* Use the location where a macro was expanded rather than where
6369 it was defined to make sure macros defined in system headers
6370 but used incorrectly elsewhere are diagnosed. */
be1e7283 6371 location_t exploc = expansion_point_location_if_in_system_header (loc);
bc35ef65 6372 auto_diagnostic_group d;
88539b96 6373 va_list ap;
6374 va_start (ap, gmsgid);
6375 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6376 va_end (ap);
6377 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
064b0d02 6378 if (*ofwhat && warned)
8bdfbc08 6379 inform (exploc, "(near initialization for %qs)", ofwhat);
d44f2f7c 6380}
6381
6382/* Issue a warning for a bad initializer component.
6383
6384 OPT is the OPT_W* value corresponding to the warning option that
6385 controls this warning. GMSGID identifies the message. The
6386 component name is taken from the spelling stack. */
6387
6388static void
6389warning_init (location_t loc, int opt, const char *gmsgid)
6390{
6391 char *ofwhat;
064b0d02 6392 bool warned;
d44f2f7c 6393
bc35ef65 6394 auto_diagnostic_group d;
6395
8bdfbc08 6396 /* Use the location where a macro was expanded rather than where
6397 it was defined to make sure macros defined in system headers
6398 but used incorrectly elsewhere are diagnosed. */
be1e7283 6399 location_t exploc = expansion_point_location_if_in_system_header (loc);
8bdfbc08 6400
d44f2f7c 6401 /* The gmsgid may be a format string with %< and %>. */
8bdfbc08 6402 warned = warning_at (exploc, opt, gmsgid);
d44f2f7c 6403 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
064b0d02 6404 if (*ofwhat && warned)
8bdfbc08 6405 inform (exploc, "(near initialization for %qs)", ofwhat);
d44f2f7c 6406}
6407\f
6408/* If TYPE is an array type and EXPR is a parenthesized string
6409 constant, warn if pedantic that EXPR is being used to initialize an
6410 object of type TYPE. */
6411
6412void
c9e51d25 6413maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
d44f2f7c 6414{
6415 if (pedantic
6416 && TREE_CODE (type) == ARRAY_TYPE
6417 && TREE_CODE (expr.value) == STRING_CST
6418 && expr.original_code != STRING_CST)
c9e51d25 6419 pedwarn_init (loc, OPT_Wpedantic,
d44f2f7c 6420 "array initialized from parenthesized string constant");
6421}
6422
b6fde48e 6423/* Attempt to locate the parameter with the given index within FNDECL,
6424 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6425
6426static location_t
6427get_fndecl_argument_location (tree fndecl, int argnum)
6428{
6429 int i;
6430 tree param;
6431
6432 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6433 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6434 i < argnum && param;
6435 i++, param = TREE_CHAIN (param))
6436 ;
6437
6438 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6439 return DECL_SOURCE_LOCATION (FNDECL). */
6440 if (param == NULL)
6441 return DECL_SOURCE_LOCATION (fndecl);
6442
6443 return DECL_SOURCE_LOCATION (param);
6444}
6445
6446/* Issue a note about a mismatching argument for parameter PARMNUM
6447 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6448 Attempt to issue the note at the pertinent parameter of the decl;
6449 failing that issue it at the location of FUNDECL; failing that
6450 issue it at PLOC. */
6451
6452static void
6453inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6454 tree expected_type, tree actual_type)
6455{
6456 location_t loc;
6457 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6458 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6459 else
6460 loc = ploc;
6461
6462 inform (loc,
6463 "expected %qT but argument is of type %qT",
6464 expected_type, actual_type);
6465}
6466
16edb1bf 6467/* Issue a warning when an argument of ARGTYPE is passed to a built-in
6468 function FUNDECL declared without prototype to parameter PARMNUM of
6469 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6470
6471static void
6472maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6473 tree parmtype, tree argtype)
6474{
6475 tree_code parmcode = TREE_CODE (parmtype);
6476 tree_code argcode = TREE_CODE (argtype);
6477 tree promoted = c_type_promotes_to (argtype);
6478
6479 /* Avoid warning for enum arguments that promote to an integer type
6480 of the same size/mode. */
6481 if (parmcode == INTEGER_TYPE
6482 && argcode == ENUMERAL_TYPE
6483 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6484 return;
6485
cf18b3e8 6486 if ((parmcode == argcode
6487 || (parmcode == INTEGER_TYPE
6488 && argcode == ENUMERAL_TYPE))
16edb1bf 6489 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6490 return;
6491
6492 /* This diagnoses even signed/unsigned mismatches. Those might be
6493 safe in many cases but GCC may emit suboptimal code for them so
6494 warning on those cases drives efficiency improvements. */
6495 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6496 TYPE_MAIN_VARIANT (promoted) == argtype
6497 ? G_("%qD argument %d type is %qT where %qT is expected "
6498 "in a call to built-in function declared without "
6499 "prototype")
6500 : G_("%qD argument %d promotes to %qT where %qT is expected "
6501 "in a call to built-in function declared without "
6502 "prototype"),
6503 fundecl, parmnum, promoted, parmtype))
6504 inform (DECL_SOURCE_LOCATION (fundecl),
6505 "built-in %qD declared here",
6506 fundecl);
6507}
6508
b9c74b4d 6509/* Convert value RHS to type TYPE as preparation for an assignment to
6510 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6511 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6512 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6513 constant before any folding.
276beea2 6514 The real work of conversion is done by `convert'.
6515 The purpose of this function is to generate error messages
6516 for assignments that are not allowed in C.
245d6740 6517 ERRTYPE says whether it is argument passing, assignment,
6518 initialization or return.
2b30d46c 6519
7e93eb1a 6520 In the following example, '~' denotes where EXPR_LOC and '^' where
6521 LOCATION point to:
6522
6523 f (var); [ic_argpass]
6524 ^ ~~~
6525 x = var; [ic_assign]
6526 ^ ~~~;
6527 int x = var; [ic_init]
6528 ^^^
6529 return x; [ic_return]
6530 ^
6531
245d6740 6532 FUNCTION is a tree for the function being called.
16edb1bf 6533 PARMNUM is the number of the argument, for printing in error messages.
6534 WARNOPT may be set to a warning option to issue the corresponding warning
6535 rather than an error for invalid conversions. Used for calls to built-in
6536 functions declared without a prototype. */
dcf6eb8b 6537
276beea2 6538static tree
ec761d5a 6539convert_for_assignment (location_t location, location_t expr_loc, tree type,
22a75734 6540 tree rhs, tree origtype, enum impl_conv errtype,
d5b637fa 6541 bool null_pointer_constant, tree fundecl,
16edb1bf 6542 tree function, int parmnum, int warnopt /* = 0 */)
276beea2 6543{
6544 enum tree_code codel = TREE_CODE (type);
c6418a4e 6545 tree orig_rhs = rhs;
276beea2 6546 tree rhstype;
6547 enum tree_code coder;
245d6740 6548 tree rname = NULL_TREE;
34d3c5de 6549 bool objc_ok = false;
245d6740 6550
802c32cd 6551 /* Use the expansion point location to handle cases such as user's
6552 function returning a wrong-type macro defined in a system header. */
6553 location = expansion_point_location_if_in_system_header (location);
6554
5933982f 6555 if (errtype == ic_argpass)
245d6740 6556 {
6557 tree selector;
6558 /* Change pointer to function to the function itself for
6559 diagnostics. */
6560 if (TREE_CODE (function) == ADDR_EXPR
6561 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6562 function = TREE_OPERAND (function, 0);
6563
6564 /* Handle an ObjC selector specially for diagnostics. */
6565 selector = objc_message_selector ();
6566 rname = function;
6567 if (selector && parmnum > 2)
6568 {
6569 rname = selector;
6570 parmnum -= 2;
6571 }
6572 }
6573
6574 /* This macro is used to emit diagnostics to ensure that all format
6575 strings are complete sentences, visible to gettext and checked at
6576 compile time. */
8a8211df 6577#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
3230bfa8 6578 do { \
6579 switch (errtype) \
6580 { \
6581 case ic_argpass: \
bc35ef65 6582 { \
6583 auto_diagnostic_group d; \
6584 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6585 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6586 } \
3230bfa8 6587 break; \
3230bfa8 6588 case ic_assign: \
6589 pedwarn (LOCATION, OPT, AS); \
6590 break; \
6591 case ic_init: \
1183a806 6592 pedwarn_init (LOCATION, OPT, IN); \
3230bfa8 6593 break; \
6594 case ic_return: \
c9e51d25 6595 pedwarn (LOCATION, OPT, RE); \
3230bfa8 6596 break; \
6597 default: \
6598 gcc_unreachable (); \
6599 } \
245d6740 6600 } while (0)
dcf6eb8b 6601
754eb2f5 6602 /* This macro is used to emit diagnostics to ensure that all format
6603 strings are complete sentences, visible to gettext and checked at
8a8211df 6604 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
754eb2f5 6605 extra parameter to enumerate qualifiers. */
8a8211df 6606#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
754eb2f5 6607 do { \
6608 switch (errtype) \
6609 { \
6610 case ic_argpass: \
bc35ef65 6611 { \
6612 auto_diagnostic_group d; \
6613 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6614 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6615 } \
754eb2f5 6616 break; \
6617 case ic_assign: \
dcfff6f9 6618 pedwarn (LOCATION, OPT, AS, QUALS); \
754eb2f5 6619 break; \
6620 case ic_init: \
dcfff6f9 6621 pedwarn (LOCATION, OPT, IN, QUALS); \
754eb2f5 6622 break; \
6623 case ic_return: \
dcfff6f9 6624 pedwarn (LOCATION, OPT, RE, QUALS); \
754eb2f5 6625 break; \
6626 default: \
6627 gcc_unreachable (); \
6628 } \
6629 } while (0)
6630
8a8211df 6631 /* This macro is used to emit diagnostics to ensure that all format
6632 strings are complete sentences, visible to gettext and checked at
6633 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6634 warning_at instead of pedwarn. */
6635#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6636 do { \
6637 switch (errtype) \
6638 { \
6639 case ic_argpass: \
bc35ef65 6640 { \
6641 auto_diagnostic_group d; \
6642 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6643 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6644 } \
8a8211df 6645 break; \
6646 case ic_assign: \
6647 warning_at (LOCATION, OPT, AS, QUALS); \
6648 break; \
6649 case ic_init: \
6650 warning_at (LOCATION, OPT, IN, QUALS); \
6651 break; \
6652 case ic_return: \
6653 warning_at (LOCATION, OPT, RE, QUALS); \
6654 break; \
6655 default: \
6656 gcc_unreachable (); \
6657 } \
6658 } while (0)
6659
c6418a4e 6660 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6661 rhs = TREE_OPERAND (rhs, 0);
6662
276beea2 6663 rhstype = TREE_TYPE (rhs);
6664 coder = TREE_CODE (rhstype);
6665
6666 if (coder == ERROR_MARK)
6667 return error_mark_node;
6668
34d3c5de 6669 if (c_dialect_objc ())
6670 {
6671 int parmno;
6672
6673 switch (errtype)
6674 {
6675 case ic_return:
6676 parmno = 0;
6677 break;
6678
6679 case ic_assign:
6680 parmno = -1;
6681 break;
6682
6683 case ic_init:
6684 parmno = -2;
6685 break;
6686
6687 default:
6688 parmno = parmnum;
6689 break;
6690 }
6691
6692 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6693 }
6694
b9c74b4d 6695 if (warn_cxx_compat)
6696 {
6697 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6698 if (checktype != error_mark_node
6699 && TREE_CODE (type) == ENUMERAL_TYPE
6700 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
88539b96 6701 switch (errtype)
6702 {
6703 case ic_argpass:
6704 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6705 "passing argument %d of %qE is invalid in C++",
6706 parmnum, rname))
6707 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6708 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6709 "expected %qT but argument is of type %qT",
6710 type, rhstype);
6711 break;
6712 case ic_assign:
6713 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6714 "%qT in assignment is invalid in C++", rhstype, type);
6715 break;
6716 case ic_init:
6717 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6718 "%qT to %qT in initialization is invalid in C++",
6719 rhstype, type);
6720 break;
6721 case ic_return:
6722 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6723 "%qT in return is invalid in C++", rhstype, type);
6724 break;
6725 default:
6726 gcc_unreachable ();
6727 }
b9c74b4d 6728 }
6729
276beea2 6730 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5c934723 6731 {
6d4e8a95 6732 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
5c934723 6733 return rhs;
6734 }
276beea2 6735
6736 if (coder == VOID_TYPE)
628fa4f8 6737 {
050526fe 6738 /* Except for passing an argument to an unprototyped function,
6739 this is a constraint violation. When passing an argument to
6740 an unprototyped function, it is compile-time undefined;
6741 making it a constraint in that case was rejected in
6742 DR#252. */
16edb1bf 6743 const char msg[] = "void value not ignored as it ought to be";
6744 if (warnopt)
6745 warning_at (location, warnopt, msg);
6746 else
6747 error_at (location, msg);
276beea2 6748 return error_mark_node;
628fa4f8 6749 }
22a3f7bd 6750 rhs = require_complete_type (location, rhs);
f831c98f 6751 if (rhs == error_mark_node)
6752 return error_mark_node;
547c6b1f 6753
6754 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6755 return error_mark_node;
6756
2c1c8a2c 6757 /* A non-reference type can convert to a reference. This handles
6758 va_start, va_copy and possibly port built-ins. */
6759 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
628fa4f8 6760 {
276beea2 6761 if (!lvalue_p (rhs))
628fa4f8 6762 {
16edb1bf 6763 const char msg[] = "cannot pass rvalue to reference parameter";
6764 if (warnopt)
6765 warning_at (location, warnopt, msg);
6766 else
6767 error_at (location, msg);
276beea2 6768 return error_mark_node;
628fa4f8 6769 }
276beea2 6770 if (!c_mark_addressable (rhs))
6771 return error_mark_node;
6772 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
e60a6f7b 6773 SET_EXPR_LOCATION (rhs, location);
276beea2 6774
ec761d5a 6775 rhs = convert_for_assignment (location, expr_loc,
22a75734 6776 build_pointer_type (TREE_TYPE (type)),
6777 rhs, origtype, errtype,
6778 null_pointer_constant, fundecl, function,
16edb1bf 6779 parmnum, warnopt);
2c1c8a2c 6780 if (rhs == error_mark_node)
6781 return error_mark_node;
276beea2 6782
6783 rhs = build1 (NOP_EXPR, type, rhs);
e60a6f7b 6784 SET_EXPR_LOCATION (rhs, location);
276beea2 6785 return rhs;
628fa4f8 6786 }
276beea2 6787 /* Some types can interconvert without explicit casts. */
0d45e76f 6788 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
546c4794 6789 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
276beea2 6790 return convert (type, rhs);
6791 /* Arithmetic types all interconvert, and enum is treated like int. */
6792 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
9421ebb9 6793 || codel == FIXED_POINT_TYPE
276beea2 6794 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6795 || codel == BOOLEAN_TYPE)
6796 && (coder == INTEGER_TYPE || coder == REAL_TYPE
9421ebb9 6797 || coder == FIXED_POINT_TYPE
276beea2 6798 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6799 || coder == BOOLEAN_TYPE))
a75b1c71 6800 {
16edb1bf 6801 if (warnopt && errtype == ic_argpass)
6802 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
6803 rhstype);
6804
a75b1c71 6805 bool save = in_late_binary_op;
823a9dd4 6806 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6807 || (coder == REAL_TYPE
6808 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
9917317a 6809 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
a75b1c71 6810 in_late_binary_op = true;
16edb1bf 6811 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6812 ? expr_loc : location, type, orig_rhs);
823a9dd4 6813 in_late_binary_op = save;
a75b1c71 6814 return ret;
6815 }
628fa4f8 6816
aedd6e32 6817 /* Aggregates in different TUs might need conversion. */
6818 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6819 && codel == coder
6820 && comptypes (type, rhstype))
ec761d5a 6821 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6822 ? expr_loc : location, type, rhs);
aedd6e32 6823
8df5a43d 6824 /* Conversion to a transparent union or record from its member types.
276beea2 6825 This applies only to function arguments. */
8df5a43d 6826 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6827 && TYPE_TRANSPARENT_AGGR (type))
5933982f 6828 && errtype == ic_argpass)
628fa4f8 6829 {
0f7adc03 6830 tree memb, marginal_memb = NULL_TREE;
276beea2 6831
1767a056 6832 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
628fa4f8 6833 {
0f7adc03 6834 tree memb_type = TREE_TYPE (memb);
628fa4f8 6835
276beea2 6836 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3ed275a6 6837 TYPE_MAIN_VARIANT (rhstype)))
276beea2 6838 break;
8915b00e 6839
276beea2 6840 if (TREE_CODE (memb_type) != POINTER_TYPE)
6841 continue;
2b30d46c 6842
276beea2 6843 if (coder == POINTER_TYPE)
6844 {
6845 tree ttl = TREE_TYPE (memb_type);
6846 tree ttr = TREE_TYPE (rhstype);
628fa4f8 6847
276beea2 6848 /* Any non-function converts to a [const][volatile] void *
6849 and vice versa; otherwise, targets must be the same.
6850 Meanwhile, the lhs target must have all the qualifiers of
6851 the rhs. */
b560fabd 6852 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6853 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
d5b637fa 6854 || comp_target_types (location, memb_type, rhstype))
276beea2 6855 {
b560fabd 6856 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6857 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
276beea2 6858 /* If this type won't generate any warnings, use it. */
b560fabd 6859 if (lquals == rquals
276beea2 6860 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6861 && TREE_CODE (ttl) == FUNCTION_TYPE)
b560fabd 6862 ? ((lquals | rquals) == rquals)
6863 : ((lquals | rquals) == lquals)))
276beea2 6864 break;
628fa4f8 6865
276beea2 6866 /* Keep looking for a better type, but remember this one. */
0f7adc03 6867 if (!marginal_memb)
6868 marginal_memb = memb;
276beea2 6869 }
6870 }
448d3ee7 6871
276beea2 6872 /* Can convert integer zero to any pointer type. */
a75b1c71 6873 if (null_pointer_constant)
276beea2 6874 {
6875 rhs = null_pointer_node;
6876 break;
6877 }
6878 }
628fa4f8 6879
0f7adc03 6880 if (memb || marginal_memb)
276beea2 6881 {
0f7adc03 6882 if (!memb)
276beea2 6883 {
6884 /* We have only a marginally acceptable member type;
6885 it needs a warning. */
0f7adc03 6886 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
276beea2 6887 tree ttr = TREE_TYPE (rhstype);
c377133d 6888
276beea2 6889 /* Const and volatile mean something different for function
6890 types, so the usual warnings are not appropriate. */
6891 if (TREE_CODE (ttr) == FUNCTION_TYPE
6892 && TREE_CODE (ttl) == FUNCTION_TYPE)
6893 {
6894 /* Because const and volatile on functions are
6895 restrictions that say the function will not do
6896 certain things, it is okay to use a const or volatile
6897 function where an ordinary one is wanted, but not
6898 vice-versa. */
6d5d708e 6899 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6900 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8a8211df 6901 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6902 OPT_Wdiscarded_qualifiers,
6903 G_("passing argument %d of %qE "
6904 "makes %q#v qualified function "
6905 "pointer from unqualified"),
6906 G_("assignment makes %q#v qualified "
6907 "function pointer from "
6908 "unqualified"),
6909 G_("initialization makes %q#v qualified "
6910 "function pointer from "
6911 "unqualified"),
6912 G_("return makes %q#v qualified function "
6913 "pointer from unqualified"),
6914 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
276beea2 6915 }
6d5d708e 6916 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6917 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
8a8211df 6918 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6919 OPT_Wdiscarded_qualifiers,
6920 G_("passing argument %d of %qE discards "
6921 "%qv qualifier from pointer target type"),
6922 G_("assignment discards %qv qualifier "
6923 "from pointer target type"),
6924 G_("initialization discards %qv qualifier "
6925 "from pointer target type"),
6926 G_("return discards %qv qualifier from "
6927 "pointer target type"),
6928 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0f7adc03 6929
6930 memb = marginal_memb;
276beea2 6931 }
628fa4f8 6932
8864917d 6933 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
29438999 6934 pedwarn (location, OPT_Wpedantic,
8864917d 6935 "ISO C prohibits argument conversion to union type");
5fcbb750 6936
389dd41b 6937 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0f7adc03 6938 return build_constructor_single (type, memb, rhs);
276beea2 6939 }
5fcbb750 6940 }
6941
276beea2 6942 /* Conversions among pointers */
6943 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6944 && (coder == codel))
628fa4f8 6945 {
16edb1bf 6946 /* If RHS refers to a built-in declared without a prototype
6947 BLTIN is the declaration of the built-in with a prototype
6948 and RHSTYPE is set to the actual type of the built-in. */
6949 tree bltin;
6950 rhstype = type_or_builtin_type (rhs, &bltin);
6951
276beea2 6952 tree ttl = TREE_TYPE (type);
6953 tree ttr = TREE_TYPE (rhstype);
aebc8537 6954 tree mvl = ttl;
6955 tree mvr = ttr;
276beea2 6956 bool is_opaque_pointer;
c17b85ea 6957 int target_cmp = 0; /* Cache comp_target_types () result. */
6d5d708e 6958 addr_space_t asl;
6959 addr_space_t asr;
628fa4f8 6960
aebc8537 6961 if (TREE_CODE (mvl) != ARRAY_TYPE)
b560fabd 6962 mvl = (TYPE_ATOMIC (mvl)
6963 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6964 TYPE_QUAL_ATOMIC)
6965 : TYPE_MAIN_VARIANT (mvl));
aebc8537 6966 if (TREE_CODE (mvr) != ARRAY_TYPE)
b560fabd 6967 mvr = (TYPE_ATOMIC (mvr)
6968 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6969 TYPE_QUAL_ATOMIC)
6970 : TYPE_MAIN_VARIANT (mvr));
276beea2 6971 /* Opaque pointers are treated like void pointers. */
ed7c4e62 6972 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
a0c938f0 6973
2fdec027 6974 /* The Plan 9 compiler permits a pointer to a struct to be
6975 automatically converted into a pointer to an anonymous field
6976 within the struct. */
6977 if (flag_plan9_extensions
ef61516b 6978 && RECORD_OR_UNION_TYPE_P (mvl)
6979 && RECORD_OR_UNION_TYPE_P (mvr)
2fdec027 6980 && mvl != mvr)
6981 {
6982 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6983 if (new_rhs != NULL_TREE)
6984 {
6985 rhs = new_rhs;
6986 rhstype = TREE_TYPE (rhs);
6987 coder = TREE_CODE (rhstype);
6988 ttr = TREE_TYPE (rhstype);
6989 mvr = TYPE_MAIN_VARIANT (ttr);
6990 }
6991 }
6992
fa2eba0d 6993 /* C++ does not allow the implicit conversion void* -> T*. However,
a0c938f0 6994 for the purpose of reducing the number of false positives, we
6995 tolerate the special case of
fa2eba0d 6996
a0c938f0 6997 int *p = NULL;
fa2eba0d 6998
a0c938f0 6999 where NULL is typically defined in C to be '(void *) 0'. */
bcdafdcb 7000 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
a07f86a6 7001 warning_at (errtype == ic_argpass ? expr_loc : location,
7002 OPT_Wc___compat,
7003 "request for implicit conversion "
e60a6f7b 7004 "from %qT to %qT not permitted in C++", rhstype, type);
628fa4f8 7005
6d5d708e 7006 /* See if the pointers point to incompatible address spaces. */
7007 asl = TYPE_ADDR_SPACE (ttl);
7008 asr = TYPE_ADDR_SPACE (ttr);
7009 if (!null_pointer_constant_p (rhs)
7010 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7011 {
7012 switch (errtype)
7013 {
7014 case ic_argpass:
16edb1bf 7015 {
7016 const char msg[] = G_("passing argument %d of %qE from "
7017 "pointer to non-enclosed address space");
7018 if (warnopt)
7019 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7020 else
7021 error_at (expr_loc, msg, parmnum, rname);
6d5d708e 7022 break;
16edb1bf 7023 }
6d5d708e 7024 case ic_assign:
16edb1bf 7025 {
7026 const char msg[] = G_("assignment from pointer to "
7027 "non-enclosed address space");
7028 if (warnopt)
7029 warning_at (location, warnopt, msg);
7030 else
7031 error_at (location, msg);
7032 break;
7033 }
6d5d708e 7034 case ic_init:
16edb1bf 7035 {
7036 const char msg[] = G_("initialization from pointer to "
7037 "non-enclosed address space");
7038 if (warnopt)
7039 warning_at (location, warnopt, msg);
7040 else
7041 error_at (location, msg);
7042 break;
7043 }
6d5d708e 7044 case ic_return:
16edb1bf 7045 {
7046 const char msg[] = G_("return from pointer to "
7047 "non-enclosed address space");
7048 if (warnopt)
7049 warning_at (location, warnopt, msg);
7050 else
7051 error_at (location, msg);
7052 break;
7053 }
6d5d708e 7054 default:
7055 gcc_unreachable ();
7056 }
7057 return error_mark_node;
7058 }
7059
95c90e04 7060 /* Check if the right-hand side has a format attribute but the
7061 left-hand side doesn't. */
b86527d8 7062 if (warn_suggest_attribute_format
be7350e7 7063 && check_missing_format_attribute (type, rhstype))
a0c938f0 7064 {
be7350e7 7065 switch (errtype)
7066 {
7067 case ic_argpass:
a07f86a6 7068 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
e60a6f7b 7069 "argument %d of %qE might be "
7070 "a candidate for a format attribute",
7071 parmnum, rname);
be7350e7 7072 break;
7073 case ic_assign:
b86527d8 7074 warning_at (location, OPT_Wsuggest_attribute_format,
e60a6f7b 7075 "assignment left-hand side might be "
7076 "a candidate for a format attribute");
be7350e7 7077 break;
7078 case ic_init:
b86527d8 7079 warning_at (location, OPT_Wsuggest_attribute_format,
e60a6f7b 7080 "initialization left-hand side might be "
7081 "a candidate for a format attribute");
be7350e7 7082 break;
7083 case ic_return:
b86527d8 7084 warning_at (location, OPT_Wsuggest_attribute_format,
e60a6f7b 7085 "return type might be "
7086 "a candidate for a format attribute");
be7350e7 7087 break;
7088 default:
7089 gcc_unreachable ();
7090 }
95c90e04 7091 }
a0c938f0 7092
276beea2 7093 /* Any non-function converts to a [const][volatile] void *
7094 and vice versa; otherwise, targets must be the same.
7095 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
b560fabd 7096 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7097 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
d5b637fa 7098 || (target_cmp = comp_target_types (location, type, rhstype))
276beea2 7099 || is_opaque_pointer
7cbb7f1a 7100 || ((c_common_unsigned_type (mvl)
7101 == c_common_unsigned_type (mvr))
b560fabd 7102 && (c_common_signed_type (mvl)
7103 == c_common_signed_type (mvr))
7104 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
276beea2 7105 {
8a8211df 7106 /* Warn about loss of qualifers from pointers to arrays with
7107 qualifiers on the element type. */
7108 if (TREE_CODE (ttr) == ARRAY_TYPE)
7109 {
7110 ttr = strip_array_types (ttr);
7111 ttl = strip_array_types (ttl);
7112
7113 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7114 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7115 WARNING_FOR_QUALIFIERS (location, expr_loc,
7116 OPT_Wdiscarded_array_qualifiers,
7117 G_("passing argument %d of %qE discards "
7118 "%qv qualifier from pointer target type"),
7119 G_("assignment discards %qv qualifier "
7120 "from pointer target type"),
7121 G_("initialization discards %qv qualifier "
7122 "from pointer target type"),
7123 G_("return discards %qv qualifier from "
7124 "pointer target type"),
7125 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7126 }
7127 else if (pedantic
276beea2 7128 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7129 ||
7130 (VOID_TYPE_P (ttr)
a75b1c71 7131 && !null_pointer_constant
276beea2 7132 && TREE_CODE (ttl) == FUNCTION_TYPE)))
8a8211df 7133 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7134 G_("ISO C forbids passing argument %d of "
7135 "%qE between function pointer "
7136 "and %<void *%>"),
7137 G_("ISO C forbids assignment between "
7138 "function pointer and %<void *%>"),
7139 G_("ISO C forbids initialization between "
7140 "function pointer and %<void *%>"),
7141 G_("ISO C forbids return between function "
7142 "pointer and %<void *%>"));
276beea2 7143 /* Const and volatile mean something different for function types,
7144 so the usual warnings are not appropriate. */
7145 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7146 && TREE_CODE (ttl) != FUNCTION_TYPE)
7147 {
8a8211df 7148 /* Don't warn about loss of qualifier for conversions from
7149 qualified void* to pointers to arrays with corresponding
7150 qualifier on the element type. */
7151 if (!pedantic)
7152 ttl = strip_array_types (ttl);
7153
b560fabd 7154 /* Assignments between atomic and non-atomic objects are OK. */
7155 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7156 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
34d3c5de 7157 {
8a8211df 7158 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7159 OPT_Wdiscarded_qualifiers,
7160 G_("passing argument %d of %qE discards "
7161 "%qv qualifier from pointer target type"),
7162 G_("assignment discards %qv qualifier "
7163 "from pointer target type"),
7164 G_("initialization discards %qv qualifier "
7165 "from pointer target type"),
7166 G_("return discards %qv qualifier from "
7167 "pointer target type"),
7168 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
34d3c5de 7169 }
276beea2 7170 /* If this is not a case of ignoring a mismatch in signedness,
7171 no warning. */
7172 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
c17b85ea 7173 || target_cmp)
276beea2 7174 ;
7175 /* If there is a mismatch, do warn. */
380bf74b 7176 else if (warn_pointer_sign)
88539b96 7177 switch (errtype)
7178 {
7179 case ic_argpass:
b7bb5264 7180 {
bc35ef65 7181 auto_diagnostic_group d;
b7bb5264 7182 range_label_for_type_mismatch rhs_label (rhstype, type);
7183 gcc_rich_location richloc (expr_loc, &rhs_label);
7184 if (pedwarn (&richloc, OPT_Wpointer_sign,
7185 "pointer targets in passing argument %d of "
7186 "%qE differ in signedness", parmnum, rname))
7187 inform_for_arg (fundecl, expr_loc, parmnum, type,
7188 rhstype);
7189 }
88539b96 7190 break;
7191 case ic_assign:
7192 pedwarn (location, OPT_Wpointer_sign,
7193 "pointer targets in assignment from %qT to %qT "
7194 "differ in signedness", rhstype, type);
7195 break;
7196 case ic_init:
7197 pedwarn_init (location, OPT_Wpointer_sign,
7198 "pointer targets in initialization of %qT "
7199 "from %qT differ in signedness", type,
7200 rhstype);
7201 break;
7202 case ic_return:
7203 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7204 "returning %qT from a function with return type "
7205 "%qT differ in signedness", rhstype, type);
7206 break;
7207 default:
7208 gcc_unreachable ();
7209 }
276beea2 7210 }
7211 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7212 && TREE_CODE (ttr) == FUNCTION_TYPE)
7213 {
7214 /* Because const and volatile on functions are restrictions
7215 that say the function will not do certain things,
7216 it is okay to use a const or volatile function
7217 where an ordinary one is wanted, but not vice-versa. */
6d5d708e 7218 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7219 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8a8211df 7220 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7221 OPT_Wdiscarded_qualifiers,
7222 G_("passing argument %d of %qE makes "
7223 "%q#v qualified function pointer "
7224 "from unqualified"),
7225 G_("assignment makes %q#v qualified function "
7226 "pointer from unqualified"),
7227 G_("initialization makes %q#v qualified "
7228 "function pointer from unqualified"),
7229 G_("return makes %q#v qualified function "
7230 "pointer from unqualified"),
7231 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
276beea2 7232 }
7233 }
88539b96 7234 /* Avoid warning about the volatile ObjC EH puts on decls. */
7235 else if (!objc_ok)
7236 {
7237 switch (errtype)
7238 {
7239 case ic_argpass:
b7bb5264 7240 {
bc35ef65 7241 auto_diagnostic_group d;
b7bb5264 7242 range_label_for_type_mismatch rhs_label (rhstype, type);
7243 gcc_rich_location richloc (expr_loc, &rhs_label);
7244 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7245 "passing argument %d of %qE from incompatible "
7246 "pointer type", parmnum, rname))
7247 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7248 }
88539b96 7249 break;
7250 case ic_assign:
16edb1bf 7251 if (bltin)
7252 pedwarn (location, OPT_Wincompatible_pointer_types,
7253 "assignment to %qT from pointer to "
7254 "%qD with incompatible type %qT",
7255 type, bltin, rhstype);
7256 else
7257 pedwarn (location, OPT_Wincompatible_pointer_types,
7258 "assignment to %qT from incompatible pointer type %qT",
7259 type, rhstype);
88539b96 7260 break;
7261 case ic_init:
16edb1bf 7262 if (bltin)
7263 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7264 "initialization of %qT from pointer to "
7265 "%qD with incompatible type %qT",
7266 type, bltin, rhstype);
7267 else
7268 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7269 "initialization of %qT from incompatible "
7270 "pointer type %qT",
7271 type, rhstype);
88539b96 7272 break;
7273 case ic_return:
16edb1bf 7274 if (bltin)
7275 pedwarn (location, OPT_Wincompatible_pointer_types,
7276 "returning pointer to %qD of type %qT from "
7277 "a function with incompatible type %qT",
7278 bltin, rhstype, type);
7279 else
7280 pedwarn (location, OPT_Wincompatible_pointer_types,
7281 "returning %qT from a function with incompatible "
7282 "return type %qT", rhstype, type);
88539b96 7283 break;
7284 default:
7285 gcc_unreachable ();
7286 }
7287 }
34d3c5de 7288
6fd31ec4 7289 /* If RHS isn't an address, check pointer or array of packed
5c934723 7290 struct or union. */
6d4e8a95 7291 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
5c934723 7292
276beea2 7293 return convert (type, rhs);
7294 }
5cd106da 7295 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7296 {
050526fe 7297 /* ??? This should not be an error when inlining calls to
7298 unprototyped functions. */
16edb1bf 7299 const char msg[] = "invalid use of non-lvalue array";
7300 if (warnopt)
7301 warning_at (location, warnopt, msg);
7302 else
7303 error_at (location, msg);
5cd106da 7304 return error_mark_node;
7305 }
276beea2 7306 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
628fa4f8 7307 {
276beea2 7308 /* An explicit constant 0 can convert to a pointer,
7309 or one that results from arithmetic, even including
7310 a cast to integer type. */
a75b1c71 7311 if (!null_pointer_constant)
88539b96 7312 switch (errtype)
7313 {
7314 case ic_argpass:
b7bb5264 7315 {
bc35ef65 7316 auto_diagnostic_group d;
b7bb5264 7317 range_label_for_type_mismatch rhs_label (rhstype, type);
7318 gcc_rich_location richloc (expr_loc, &rhs_label);
7319 if (pedwarn (&richloc, OPT_Wint_conversion,
7320 "passing argument %d of %qE makes pointer from "
7321 "integer without a cast", parmnum, rname))
7322 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7323 }
88539b96 7324 break;
7325 case ic_assign:
7326 pedwarn (location, OPT_Wint_conversion,
7327 "assignment to %qT from %qT makes pointer from integer "
7328 "without a cast", type, rhstype);
7329 break;
7330 case ic_init:
7331 pedwarn_init (location, OPT_Wint_conversion,
7332 "initialization of %qT from %qT makes pointer from "
7333 "integer without a cast", type, rhstype);
7334 break;
7335 case ic_return:
7336 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7337 "function with return type %qT makes pointer from "
7338 "integer without a cast", rhstype, type);
7339 break;
7340 default:
7341 gcc_unreachable ();
7342 }
d318227b 7343
7344 return convert (type, rhs);
628fa4f8 7345 }
276beea2 7346 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
628fa4f8 7347 {
88539b96 7348 switch (errtype)
7349 {
7350 case ic_argpass:
b7bb5264 7351 {
bc35ef65 7352 auto_diagnostic_group d;
b7bb5264 7353 range_label_for_type_mismatch rhs_label (rhstype, type);
7354 gcc_rich_location richloc (expr_loc, &rhs_label);
7355 if (pedwarn (&richloc, OPT_Wint_conversion,
7356 "passing argument %d of %qE makes integer from "
7357 "pointer without a cast", parmnum, rname))
7358 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7359 }
88539b96 7360 break;
7361 case ic_assign:
7362 pedwarn (location, OPT_Wint_conversion,
7363 "assignment to %qT from %qT makes integer from pointer "
7364 "without a cast", type, rhstype);
7365 break;
7366 case ic_init:
7367 pedwarn_init (location, OPT_Wint_conversion,
7368 "initialization of %qT from %qT makes integer from "
7369 "pointer without a cast", type, rhstype);
7370 break;
7371 case ic_return:
7372 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7373 "function with return type %qT makes integer from "
7374 "pointer without a cast", rhstype, type);
7375 break;
7376 default:
7377 gcc_unreachable ();
7378 }
7379
276beea2 7380 return convert (type, rhs);
7381 }
7382 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
a75b1c71 7383 {
7384 tree ret;
7385 bool save = in_late_binary_op;
7386 in_late_binary_op = true;
7387 ret = convert (type, rhs);
7388 in_late_binary_op = save;
7389 return ret;
7390 }
628fa4f8 7391
245d6740 7392 switch (errtype)
276beea2 7393 {
245d6740 7394 case ic_argpass:
b7bb5264 7395 {
bc35ef65 7396 auto_diagnostic_group d;
b7bb5264 7397 range_label_for_type_mismatch rhs_label (rhstype, type);
7398 gcc_rich_location richloc (expr_loc, &rhs_label);
16edb1bf 7399 const char msg[] = G_("incompatible type for argument %d of %qE");
7400 if (warnopt)
7401 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7402 else
7403 error_at (&richloc, msg, parmnum, rname);
b7bb5264 7404 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7405 }
245d6740 7406 break;
7407 case ic_assign:
16edb1bf 7408 {
7409 const char msg[]
7410 = G_("incompatible types when assigning to type %qT from type %qT");
7411 if (warnopt)
7412 warning_at (expr_loc, 0, msg, type, rhstype);
7413 else
7414 error_at (expr_loc, msg, type, rhstype);
7415 break;
7416 }
245d6740 7417 case ic_init:
16edb1bf 7418 {
7419 const char msg[]
7420 = G_("incompatible types when initializing type %qT using type %qT");
7421 if (warnopt)
7422 warning_at (location, 0, msg, type, rhstype);
7423 else
7424 error_at (location, msg, type, rhstype);
7425 break;
7426 }
245d6740 7427 case ic_return:
16edb1bf 7428 {
7429 const char msg[]
7430 = G_("incompatible types when returning type %qT but %qT was expected");
7431 if (warnopt)
7432 warning_at (location, 0, msg, rhstype, type);
7433 else
7434 error_at (location, msg, rhstype, type);
7435 break;
7436 }
245d6740 7437 default:
7438 gcc_unreachable ();
628fa4f8 7439 }
8ba8da08 7440
276beea2 7441 return error_mark_node;
7442}
276beea2 7443\f
7444/* If VALUE is a compound expr all of whose expressions are constant, then
7445 return its value. Otherwise, return error_mark_node.
d0a47c8d 7446
276beea2 7447 This is for handling COMPOUND_EXPRs as initializer elements
7448 which is allowed with a warning when -pedantic is specified. */
d0a47c8d 7449
276beea2 7450static tree
7451valid_compound_expr_initializer (tree value, tree endtype)
7452{
7453 if (TREE_CODE (value) == COMPOUND_EXPR)
7454 {
7455 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7456 == error_mark_node)
7457 return error_mark_node;
7458 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7459 endtype);
7460 }
fdf95cc9 7461 else if (!initializer_constant_valid_p (value, endtype))
276beea2 7462 return error_mark_node;
7463 else
7464 return value;
d0a47c8d 7465}
628fa4f8 7466\f
276beea2 7467/* Perform appropriate conversions on the initial value of a variable,
7468 store it in the declaration DECL,
7469 and print any error messages that are appropriate.
b9c74b4d 7470 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
e60a6f7b 7471 If the init is invalid, store an ERROR_MARK.
7472
7473 INIT_LOC is the location of the initial value. */
628fa4f8 7474
276beea2 7475void
e60a6f7b 7476store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
628fa4f8 7477{
276beea2 7478 tree value, type;
a75b1c71 7479 bool npc = false;
628fa4f8 7480
276beea2 7481 /* If variable's type was invalidly declared, just ignore it. */
628fa4f8 7482
276beea2 7483 type = TREE_TYPE (decl);
7484 if (TREE_CODE (type) == ERROR_MARK)
7485 return;
628fa4f8 7486
276beea2 7487 /* Digest the specified initializer into an expression. */
628fa4f8 7488
a75b1c71 7489 if (init)
7490 npc = null_pointer_constant_p (init);
e60a6f7b 7491 value = digest_init (init_loc, type, init, origtype, npc,
7492 true, TREE_STATIC (decl));
628fa4f8 7493
276beea2 7494 /* Store the expression if valid; else report error. */
628fa4f8 7495
3df42822 7496 if (!in_system_header_at (input_location)
84166705 7497 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6bf97f82 7498 warning (OPT_Wtraditional, "traditional C rejects automatic "
7499 "aggregate initialization");
2b30d46c 7500
591970b4 7501 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7502 DECL_INITIAL (decl) = value;
628fa4f8 7503
276beea2 7504 /* ANSI wants warnings about out-of-range constant initializers. */
7505 STRIP_TYPE_NOPS (value);
48e1416a 7506 if (TREE_STATIC (decl))
67e05044 7507 constant_expression_warning (value);
628fa4f8 7508
276beea2 7509 /* Check if we need to set array size from compound literal size. */
7510 if (TREE_CODE (type) == ARRAY_TYPE
72749341 7511 && TYPE_DOMAIN (type) == NULL_TREE
276beea2 7512 && value != error_mark_node)
628fa4f8 7513 {
276beea2 7514 tree inside_init = init;
7515
a97c952c 7516 STRIP_TYPE_NOPS (inside_init);
276beea2 7517 inside_init = fold (inside_init);
7518
7519 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7520 {
ce675af0 7521 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
276beea2 7522
ce675af0 7523 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
276beea2 7524 {
7525 /* For int foo[] = (int [3]){1}; we need to set array size
7526 now since later on array initializer will be just the
7527 brace enclosed list of the compound literal. */
9e1f1fbf 7528 tree etype = strip_array_types (TREE_TYPE (decl));
ce675af0 7529 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
ce675af0 7530 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
276beea2 7531 layout_type (type);
ce675af0 7532 layout_decl (cldecl, 0);
9e1f1fbf 7533 TREE_TYPE (decl)
7534 = c_build_qualified_type (type, TYPE_QUALS (etype));
276beea2 7535 }
7536 }
628fa4f8 7537 }
276beea2 7538}
7539\f
7540/* Methods for storing and printing names for error messages. */
628fa4f8 7541
276beea2 7542/* Implement a spelling stack that allows components of a name to be pushed
7543 and popped. Each element on the stack is this structure. */
628fa4f8 7544
276beea2 7545struct spelling
7546{
7547 int kind;
7548 union
628fa4f8 7549 {
4aeeff51 7550 unsigned HOST_WIDE_INT i;
276beea2 7551 const char *s;
7552 } u;
7553};
2b30d46c 7554
276beea2 7555#define SPELLING_STRING 1
7556#define SPELLING_MEMBER 2
7557#define SPELLING_BOUNDS 3
628fa4f8 7558
276beea2 7559static struct spelling *spelling; /* Next stack element (unused). */
7560static struct spelling *spelling_base; /* Spelling stack base. */
7561static int spelling_size; /* Size of the spelling stack. */
628fa4f8 7562
276beea2 7563/* Macros to save and restore the spelling stack around push_... functions.
7564 Alternative to SAVE_SPELLING_STACK. */
628fa4f8 7565
276beea2 7566#define SPELLING_DEPTH() (spelling - spelling_base)
7567#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
628fa4f8 7568
276beea2 7569/* Push an element on the spelling stack with type KIND and assign VALUE
7570 to MEMBER. */
628fa4f8 7571
276beea2 7572#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7573{ \
7574 int depth = SPELLING_DEPTH (); \
7575 \
7576 if (depth >= spelling_size) \
7577 { \
7578 spelling_size += 10; \
63aa3db6 7579 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7580 spelling_size); \
276beea2 7581 RESTORE_SPELLING_DEPTH (depth); \
7582 } \
7583 \
7584 spelling->kind = (KIND); \
7585 spelling->MEMBER = (VALUE); \
7586 spelling++; \
7587}
628fa4f8 7588
276beea2 7589/* Push STRING on the stack. Printed literally. */
628fa4f8 7590
276beea2 7591static void
7592push_string (const char *string)
7593{
7594 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7595}
628fa4f8 7596
276beea2 7597/* Push a member name on the stack. Printed as '.' STRING. */
628fa4f8 7598
276beea2 7599static void
7600push_member_name (tree decl)
7601{
7602 const char *const string
d1dd9ac0 7603 = (DECL_NAME (decl)
7604 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7605 : _("<anonymous>"));
276beea2 7606 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7607}
628fa4f8 7608
276beea2 7609/* Push an array bounds on the stack. Printed as [BOUNDS]. */
628fa4f8 7610
276beea2 7611static void
4aeeff51 7612push_array_bounds (unsigned HOST_WIDE_INT bounds)
276beea2 7613{
7614 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7615}
cf18ae1f 7616
276beea2 7617/* Compute the maximum size in bytes of the printed spelling. */
628fa4f8 7618
276beea2 7619static int
7620spelling_length (void)
7621{
7622 int size = 0;
7623 struct spelling *p;
628fa4f8 7624
276beea2 7625 for (p = spelling_base; p < spelling; p++)
7626 {
7627 if (p->kind == SPELLING_BOUNDS)
7628 size += 25;
7629 else
7630 size += strlen (p->u.s) + 1;
7631 }
7632
7633 return size;
628fa4f8 7634}
628fa4f8 7635
276beea2 7636/* Print the spelling to BUFFER and return it. */
628fa4f8 7637
276beea2 7638static char *
7639print_spelling (char *buffer)
628fa4f8 7640{
276beea2 7641 char *d = buffer;
7642 struct spelling *p;
628fa4f8 7643
276beea2 7644 for (p = spelling_base; p < spelling; p++)
7645 if (p->kind == SPELLING_BOUNDS)
7646 {
4aeeff51 7647 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
276beea2 7648 d += strlen (d);
7649 }
7650 else
7651 {
7652 const char *s;
7653 if (p->kind == SPELLING_MEMBER)
7654 *d++ = '.';
7655 for (s = p->u.s; (*d = *s++); d++)
7656 ;
7657 }
7658 *d++ = '\0';
7659 return buffer;
7660}
628fa4f8 7661
276beea2 7662/* Digest the parser output INIT as an initializer for type TYPE.
7663 Return a C expression of type TYPE to represent the initial value.
fdcfba98 7664
b9c74b4d 7665 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7666
a75b1c71 7667 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7668
3ad5f027 7669 If INIT is a string constant, STRICT_STRING is true if it is
7670 unparenthesized or we should not warn here for it being parenthesized.
7671 For other types of INIT, STRICT_STRING is not used.
7672
e60a6f7b 7673 INIT_LOC is the location of the INIT.
7674
276beea2 7675 REQUIRE_CONSTANT requests an error if non-constant initializers or
7676 elements are seen. */
fdcfba98 7677
276beea2 7678static tree
e60a6f7b 7679digest_init (location_t init_loc, tree type, tree init, tree origtype,
7680 bool null_pointer_constant, bool strict_string,
7681 int require_constant)
276beea2 7682{
7683 enum tree_code code = TREE_CODE (type);
7684 tree inside_init = init;
c6418a4e 7685 tree semantic_type = NULL_TREE;
a75b1c71 7686 bool maybe_const = true;
fdcfba98 7687
276beea2 7688 if (type == error_mark_node
b5a68e6b 7689 || !init
224bdd51 7690 || error_operand_p (init))
276beea2 7691 return error_mark_node;
fdcfba98 7692
a97c952c 7693 STRIP_TYPE_NOPS (inside_init);
fdcfba98 7694
c6418a4e 7695 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7696 {
7697 semantic_type = TREE_TYPE (inside_init);
7698 inside_init = TREE_OPERAND (inside_init, 0);
7699 }
a75b1c71 7700 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
fdcfba98 7701
276beea2 7702 /* Initialization of an array of chars from a string constant
7703 optionally enclosed in braces. */
fdcfba98 7704
e91a0d39 7705 if (code == ARRAY_TYPE && inside_init
7706 && TREE_CODE (inside_init) == STRING_CST)
276beea2 7707 {
b560fabd 7708 tree typ1
7709 = (TYPE_ATOMIC (TREE_TYPE (type))
7710 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7711 TYPE_QUAL_ATOMIC)
7712 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
e91a0d39 7713 /* Note that an array could be both an array of character type
7714 and an array of wchar_t if wchar_t is signed char or unsigned
7715 char. */
7716 bool char_array = (typ1 == char_type_node
7717 || typ1 == signed_char_type_node
7718 || typ1 == unsigned_char_type_node);
7719 bool wchar_array = !!comptypes (typ1, wchar_type_node);
b0726616 7720 bool char16_array = !!comptypes (typ1, char16_type_node);
7721 bool char32_array = !!comptypes (typ1, char32_type_node);
7722
7723 if (char_array || wchar_array || char16_array || char32_array)
fdcfba98 7724 {
3ad5f027 7725 struct c_expr expr;
b0726616 7726 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7214f613 7727 bool incompat_string_cst = false;
3ad5f027 7728 expr.value = inside_init;
7729 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
841fdaa6 7730 expr.original_type = NULL;
c9e51d25 7731 maybe_warn_string_init (init_loc, type, expr);
3ad5f027 7732
fe49e588 7733 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
29438999 7734 pedwarn_init (init_loc, OPT_Wpedantic,
fe49e588 7735 "initialization of a flexible array member");
7736
276beea2 7737 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3ed275a6 7738 TYPE_MAIN_VARIANT (type)))
276beea2 7739 return inside_init;
fdcfba98 7740
b0726616 7741 if (char_array)
276beea2 7742 {
b0726616 7743 if (typ2 != char_type_node)
7214f613 7744 incompat_string_cst = true;
fdcfba98 7745 }
7214f613 7746 else if (!comptypes (typ1, typ2))
7747 incompat_string_cst = true;
7748
7749 if (incompat_string_cst)
7750 {
7751 error_init (init_loc, "cannot initialize array of %qT from "
7752 "a string literal with type array of %qT",
7753 typ1, typ2);
7754 return error_mark_node;
7755 }
2b30d46c 7756
72749341 7757 if (TYPE_DOMAIN (type) != NULL_TREE
7758 && TYPE_SIZE (type) != NULL_TREE
019c559b 7759 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7760 {
7761 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
abadc2f8 7762 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
019c559b 7763
b0726616 7764 /* Subtract the size of a single (possibly wide) character
276beea2 7765 because it's ok to ignore the terminating null char
7766 that is counted in the length of the constant. */
abadc2f8 7767 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
019c559b 7768 pedwarn_init (init_loc, 0,
7769 ("initializer-string for array of chars "
7770 "is too long"));
7771 else if (warn_cxx_compat
c9281ef8 7772 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
019c559b 7773 warning_at (init_loc, OPT_Wc___compat,
7774 ("initializer-string for array chars "
7775 "is too long for C++"));
abadc2f8 7776 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7777 {
7778 unsigned HOST_WIDE_INT size
7779 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7780 const char *p = TREE_STRING_POINTER (inside_init);
7781
7782 inside_init = build_string (size, p);
7783 }
019c559b 7784 }
fdcfba98 7785
abadc2f8 7786 TREE_TYPE (inside_init) = type;
276beea2 7787 return inside_init;
fdcfba98 7788 }
e91a0d39 7789 else if (INTEGRAL_TYPE_P (typ1))
7790 {
53de1631 7791 error_init (init_loc, "array of inappropriate type initialized "
e91a0d39 7792 "from string constant");
7793 return error_mark_node;
7794 }
fdcfba98 7795 }
7796
276beea2 7797 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7798 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7799 below and handle as a constructor. */
3a3c175c 7800 if (code == VECTOR_TYPE
6290f0db 7801 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
546c4794 7802 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
3a3c175c 7803 && TREE_CONSTANT (inside_init))
7804 {
7805 if (TREE_CODE (inside_init) == VECTOR_CST
7806 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7807 TYPE_MAIN_VARIANT (type)))
7808 return inside_init;
7809
7810 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7811 {
c75b4594 7812 unsigned HOST_WIDE_INT ix;
7813 tree value;
7814 bool constant_p = true;
3a3c175c 7815
7816 /* Iterate through elements and check if all constructor
7817 elements are *_CSTs. */
c75b4594 7818 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7819 if (!CONSTANT_CLASS_P (value))
7820 {
7821 constant_p = false;
7822 break;
7823 }
3a3c175c 7824
c75b4594 7825 if (constant_p)
7826 return build_vector_from_ctor (type,
7827 CONSTRUCTOR_ELTS (inside_init));
3a3c175c 7828 }
7829 }
4b4dcf9c 7830
6513b50d 7831 if (warn_sequence_point)
7832 verify_sequence_points (inside_init);
7833
276beea2 7834 /* Any type can be initialized
7835 from an expression of the same type, optionally with braces. */
628fa4f8 7836
72749341 7837 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
276beea2 7838 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3ed275a6 7839 TYPE_MAIN_VARIANT (type))
276beea2 7840 || (code == ARRAY_TYPE
3ed275a6 7841 && comptypes (TREE_TYPE (inside_init), type))
276beea2 7842 || (code == VECTOR_TYPE
3ed275a6 7843 && comptypes (TREE_TYPE (inside_init), type))
276beea2 7844 || (code == POINTER_TYPE
48b3d385 7845 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
276beea2 7846 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
3ed275a6 7847 TREE_TYPE (type)))))
276beea2 7848 {
7849 if (code == POINTER_TYPE)
5cd106da 7850 {
5cd106da 7851 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7852 {
e78cf9a7 7853 if (TREE_CODE (inside_init) == STRING_CST
7854 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
e60a6f7b 7855 inside_init = array_to_pointer_conversion
7856 (init_loc, inside_init);
e78cf9a7 7857 else
7858 {
53de1631 7859 error_init (init_loc, "invalid use of non-lvalue array");
e78cf9a7 7860 return error_mark_node;
7861 }
5cd106da 7862 }
e78cf9a7 7863 }
5cd106da 7864
cd81f692 7865 if (code == VECTOR_TYPE)
7866 /* Although the types are compatible, we may require a
7867 conversion. */
7868 inside_init = convert (type, inside_init);
276beea2 7869
b7d1b569 7870 if (require_constant
276beea2 7871 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
628fa4f8 7872 {
276beea2 7873 /* As an extension, allow initializing objects with static storage
7874 duration with compound literals (which are then treated just as
b7d1b569 7875 the brace enclosed list they contain). Also allow this for
7876 vectors, as we can only assign them with compound literals. */
bccf97da 7877 if (flag_isoc99 && code != VECTOR_TYPE)
7878 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7879 "is not constant");
276beea2 7880 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7881 inside_init = DECL_INITIAL (decl);
628fa4f8 7882 }
276beea2 7883
7884 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7885 && TREE_CODE (inside_init) != CONSTRUCTOR)
628fa4f8 7886 {
53de1631 7887 error_init (init_loc, "array initialized from non-constant array "
7888 "expression");
276beea2 7889 return error_mark_node;
628fa4f8 7890 }
628fa4f8 7891
29438999 7892 /* Compound expressions can only occur here if -Wpedantic or
276beea2 7893 -pedantic-errors is specified. In the later case, we always want
7894 an error. In the former case, we simply want a warning. */
7895 if (require_constant && pedantic
7896 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7897 {
7898 inside_init
7899 = valid_compound_expr_initializer (inside_init,
7900 TREE_TYPE (inside_init));
7901 if (inside_init == error_mark_node)
53de1631 7902 error_init (init_loc, "initializer element is not constant");
2b30d46c 7903 else
29438999 7904 pedwarn_init (init_loc, OPT_Wpedantic,
21ca8540 7905 "initializer element is not constant");
276beea2 7906 if (flag_pedantic_errors)
7907 inside_init = error_mark_node;
7908 }
7909 else if (require_constant
fdf95cc9 7910 && !initializer_constant_valid_p (inside_init,
7911 TREE_TYPE (inside_init)))
276beea2 7912 {
53de1631 7913 error_init (init_loc, "initializer element is not constant");
276beea2 7914 inside_init = error_mark_node;
ea5fb8f9 7915 }
a75b1c71 7916 else if (require_constant && !maybe_const)
aac24642 7917 pedwarn_init (init_loc, OPT_Wpedantic,
a75b1c71 7918 "initializer element is not a constant expression");
78b80426 7919
b86527d8 7920 /* Added to enable additional -Wsuggest-attribute=format warnings. */
c7eca986 7921 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
22a75734 7922 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7923 type, inside_init, origtype,
b9c74b4d 7924 ic_init, null_pointer_constant,
a75b1c71 7925 NULL_TREE, NULL_TREE, 0);
276beea2 7926 return inside_init;
7927 }
78b80426 7928
276beea2 7929 /* Handle scalar types, including conversions. */
628fa4f8 7930
9421ebb9 7931 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7932 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7933 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
628fa4f8 7934 {
e78cf9a7 7935 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7936 && (TREE_CODE (init) == STRING_CST
7937 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
e60a6f7b 7938 inside_init = init = array_to_pointer_conversion (init_loc, init);
c6418a4e 7939 if (semantic_type)
7940 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7941 inside_init);
276beea2 7942 inside_init
22a75734 7943 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7944 inside_init, origtype, ic_init,
7945 null_pointer_constant, NULL_TREE, NULL_TREE,
7946 0);
2b30d46c 7947
0d45e76f 7948 /* Check to see if we have already given an error message. */
7949 if (inside_init == error_mark_node)
7950 ;
84166705 7951 else if (require_constant && !TREE_CONSTANT (inside_init))
628fa4f8 7952 {
53de1631 7953 error_init (init_loc, "initializer element is not constant");
276beea2 7954 inside_init = error_mark_node;
628fa4f8 7955 }
276beea2 7956 else if (require_constant
fdf95cc9 7957 && !initializer_constant_valid_p (inside_init,
7958 TREE_TYPE (inside_init)))
628fa4f8 7959 {
53de1631 7960 error_init (init_loc, "initializer element is not computable at "
7961 "load time");
276beea2 7962 inside_init = error_mark_node;
628fa4f8 7963 }
a75b1c71 7964 else if (require_constant && !maybe_const)
8d669e79 7965 pedwarn_init (init_loc, OPT_Wpedantic,
a75b1c71 7966 "initializer element is not a constant expression");
276beea2 7967
7968 return inside_init;
628fa4f8 7969 }
bb94852b 7970
276beea2 7971 /* Come here only for records and arrays. */
bb94852b 7972
276beea2 7973 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
bb94852b 7974 {
53de1631 7975 error_init (init_loc, "variable-sized object may not be initialized");
276beea2 7976 return error_mark_node;
bb94852b 7977 }
276beea2 7978
53de1631 7979 error_init (init_loc, "invalid initializer");
276beea2 7980 return error_mark_node;
bb94852b 7981}
628fa4f8 7982\f
276beea2 7983/* Handle initializers that use braces. */
628fa4f8 7984
276beea2 7985/* Type of object we are accumulating a constructor for.
7986 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7987static tree constructor_type;
628fa4f8 7988
276beea2 7989/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7990 left to fill. */
7991static tree constructor_fields;
628fa4f8 7992
276beea2 7993/* For an ARRAY_TYPE, this is the specified index
7994 at which to store the next element we get. */
7995static tree constructor_index;
628fa4f8 7996
276beea2 7997/* For an ARRAY_TYPE, this is the maximum index. */
7998static tree constructor_max_index;
628fa4f8 7999
276beea2 8000/* For a RECORD_TYPE, this is the first field not yet written out. */
8001static tree constructor_unfilled_fields;
628fa4f8 8002
276beea2 8003/* For an ARRAY_TYPE, this is the index of the first element
8004 not yet written out. */
8005static tree constructor_unfilled_index;
3a77db2f 8006
276beea2 8007/* In a RECORD_TYPE, the byte index of the next consecutive field.
8008 This is so we can generate gaps between fields, when appropriate. */
8009static tree constructor_bit_index;
550e465e 8010
276beea2 8011/* If we are saving up the elements rather than allocating them,
8012 this is the list of elements so far (in reverse order,
8013 most recent first). */
f1f41a6c 8014static vec<constructor_elt, va_gc> *constructor_elements;
02643ebc 8015
276beea2 8016/* 1 if constructor should be incrementally stored into a constructor chain,
8017 0 if all the elements should be kept in AVL tree. */
8018static int constructor_incremental;
02643ebc 8019
276beea2 8020/* 1 if so far this constructor's elements are all compile-time constants. */
8021static int constructor_constant;
02643ebc 8022
276beea2 8023/* 1 if so far this constructor's elements are all valid address constants. */
8024static int constructor_simple;
02643ebc 8025
a75b1c71 8026/* 1 if this constructor has an element that cannot be part of a
8027 constant expression. */
8028static int constructor_nonconst;
8029
276beea2 8030/* 1 if this constructor is erroneous so far. */
8031static int constructor_erroneous;
f9e15121 8032
95cdf3fd 8033/* 1 if this constructor is the universal zero initializer { 0 }. */
8034static int constructor_zeroinit;
8035
276beea2 8036/* Structure for managing pending initializer elements, organized as an
8037 AVL tree. */
f9e15121 8038
276beea2 8039struct init_node
f9e15121 8040{
276beea2 8041 struct init_node *left, *right;
8042 struct init_node *parent;
8043 int balance;
8044 tree purpose;
8045 tree value;
b9c74b4d 8046 tree origtype;
f9e15121 8047};
8048
276beea2 8049/* Tree of pending elements at this constructor level.
8050 These are elements encountered out of order
8051 which belong at places we haven't reached yet in actually
8052 writing the output.
8053 Will never hold tree nodes across GC runs. */
8054static struct init_node *constructor_pending_elts;
f9e15121 8055
276beea2 8056/* The SPELLING_DEPTH of this constructor. */
8057static int constructor_depth;
f9e15121 8058
276beea2 8059/* DECL node for which an initializer is being read.
8060 0 means we are reading a constructor expression
8061 such as (struct foo) {...}. */
8062static tree constructor_decl;
f9e15121 8063
276beea2 8064/* Nonzero if this is an initializer for a top-level decl. */
8065static int constructor_top_level;
f9e15121 8066
276beea2 8067/* Nonzero if there were any member designators in this initializer. */
8068static int constructor_designated;
f9e15121 8069
276beea2 8070/* Nesting depth of designator list. */
8071static int designator_depth;
f9e15121 8072
276beea2 8073/* Nonzero if there were diagnosed errors in this designator list. */
ebe39827 8074static int designator_erroneous;
f9e15121 8075
276beea2 8076\f
8077/* This stack has a level for each implicit or explicit level of
8078 structuring in the initializer, including the outermost one. It
8079 saves the values of most of the variables above. */
f9e15121 8080
276beea2 8081struct constructor_range_stack;
8082
8083struct constructor_stack
f9e15121 8084{
276beea2 8085 struct constructor_stack *next;
8086 tree type;
8087 tree fields;
8088 tree index;
8089 tree max_index;
8090 tree unfilled_index;
8091 tree unfilled_fields;
8092 tree bit_index;
f1f41a6c 8093 vec<constructor_elt, va_gc> *elements;
276beea2 8094 struct init_node *pending_elts;
8095 int offset;
8096 int depth;
3ad5f027 8097 /* If value nonzero, this value should replace the entire
276beea2 8098 constructor at this level. */
3ad5f027 8099 struct c_expr replacement_value;
276beea2 8100 struct constructor_range_stack *range_stack;
8101 char constant;
8102 char simple;
a75b1c71 8103 char nonconst;
276beea2 8104 char implicit;
8105 char erroneous;
8106 char outer;
8107 char incremental;
8108 char designated;
74691f46 8109 int designator_depth;
276beea2 8110};
f9e15121 8111
4aa31b78 8112static struct constructor_stack *constructor_stack;
f9e15121 8113
276beea2 8114/* This stack represents designators from some range designator up to
8115 the last designator in the list. */
f9e15121 8116
276beea2 8117struct constructor_range_stack
8118{
8119 struct constructor_range_stack *next, *prev;
8120 struct constructor_stack *stack;
8121 tree range_start;
8122 tree index;
8123 tree range_end;
8124 tree fields;
8125};
f9e15121 8126
4aa31b78 8127static struct constructor_range_stack *constructor_range_stack;
f9e15121 8128
276beea2 8129/* This stack records separate initializers that are nested.
8130 Nested initializers can't happen in ANSI C, but GNU C allows them
8131 in cases like { ... (struct foo) { ... } ... }. */
f9e15121 8132
276beea2 8133struct initializer_stack
f9e15121 8134{
276beea2 8135 struct initializer_stack *next;
8136 tree decl;
276beea2 8137 struct constructor_stack *constructor_stack;
8138 struct constructor_range_stack *constructor_range_stack;
f1f41a6c 8139 vec<constructor_elt, va_gc> *elements;
276beea2 8140 struct spelling *spelling;
8141 struct spelling *spelling_base;
8142 int spelling_size;
8143 char top_level;
8144 char require_constant_value;
8145 char require_constant_elements;
8ccda24a 8146 rich_location *missing_brace_richloc;
276beea2 8147};
f9e15121 8148
4aa31b78 8149static struct initializer_stack *initializer_stack;
276beea2 8150\f
8151/* Prepare to parse and output the initializer for variable DECL. */
628fa4f8 8152
8153void
8ccda24a 8154start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8155 rich_location *richloc)
628fa4f8 8156{
276beea2 8157 const char *locus;
a9c6c0e3 8158 struct initializer_stack *p = XNEW (struct initializer_stack);
628fa4f8 8159
276beea2 8160 p->decl = constructor_decl;
276beea2 8161 p->require_constant_value = require_constant_value;
8162 p->require_constant_elements = require_constant_elements;
8163 p->constructor_stack = constructor_stack;
8164 p->constructor_range_stack = constructor_range_stack;
8165 p->elements = constructor_elements;
8166 p->spelling = spelling;
8167 p->spelling_base = spelling_base;
8168 p->spelling_size = spelling_size;
8169 p->top_level = constructor_top_level;
8170 p->next = initializer_stack;
8ccda24a 8171 p->missing_brace_richloc = richloc;
276beea2 8172 initializer_stack = p;
628fa4f8 8173
276beea2 8174 constructor_decl = decl;
276beea2 8175 constructor_designated = 0;
8176 constructor_top_level = top_level;
628fa4f8 8177
72749341 8178 if (decl != NULL_TREE && decl != error_mark_node)
276beea2 8179 {
8180 require_constant_value = TREE_STATIC (decl);
8181 require_constant_elements
8182 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8183 /* For a scalar, you can always use any value to initialize,
8184 even within braces. */
9e73638f 8185 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
d1dd9ac0 8186 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
276beea2 8187 }
8188 else
8189 {
8190 require_constant_value = 0;
8191 require_constant_elements = 0;
d1dd9ac0 8192 locus = _("(anonymous)");
276beea2 8193 }
89a5f6d1 8194
276beea2 8195 constructor_stack = 0;
8196 constructor_range_stack = 0;
89a5f6d1 8197
95cdf3fd 8198 found_missing_braces = 0;
276beea2 8199
8200 spelling_base = 0;
8201 spelling_size = 0;
8202 RESTORE_SPELLING_DEPTH (0);
8203
8204 if (locus)
8205 push_string (locus);
8206}
8207
8208void
8209finish_init (void)
89a5f6d1 8210{
276beea2 8211 struct initializer_stack *p = initializer_stack;
89a5f6d1 8212
276beea2 8213 /* Free the whole constructor stack of this initializer. */
8214 while (constructor_stack)
8215 {
8216 struct constructor_stack *q = constructor_stack;
8217 constructor_stack = q->next;
8218 free (q);
8219 }
8220
231bd014 8221 gcc_assert (!constructor_range_stack);
276beea2 8222
8223 /* Pop back to the data of the outer initializer (if any). */
dcd8fd01 8224 free (spelling_base);
a5d9b222 8225
276beea2 8226 constructor_decl = p->decl;
276beea2 8227 require_constant_value = p->require_constant_value;
8228 require_constant_elements = p->require_constant_elements;
8229 constructor_stack = p->constructor_stack;
8230 constructor_range_stack = p->constructor_range_stack;
8231 constructor_elements = p->elements;
8232 spelling = p->spelling;
8233 spelling_base = p->spelling_base;
8234 spelling_size = p->spelling_size;
8235 constructor_top_level = p->top_level;
8236 initializer_stack = p->next;
8237 free (p);
89a5f6d1 8238}
628fa4f8 8239\f
276beea2 8240/* Call here when we see the initializer is surrounded by braces.
8241 This is instead of a call to push_init_level;
8242 it is matched by a call to pop_init_level.
628fa4f8 8243
276beea2 8244 TYPE is the type to initialize, for a constructor expression.
8245 For an initializer for a decl, TYPE is zero. */
628fa4f8 8246
276beea2 8247void
8248really_start_incremental_init (tree type)
628fa4f8 8249{
9318f22c 8250 struct constructor_stack *p = XNEW (struct constructor_stack);
628fa4f8 8251
72749341 8252 if (type == NULL_TREE)
276beea2 8253 type = TREE_TYPE (constructor_decl);
628fa4f8 8254
6290f0db 8255 if (VECTOR_TYPE_P (type)
8d125f7d 8256 && TYPE_VECTOR_OPAQUE (type))
276beea2 8257 error ("opaque vector types cannot be initialized");
628fa4f8 8258
276beea2 8259 p->type = constructor_type;
8260 p->fields = constructor_fields;
8261 p->index = constructor_index;
8262 p->max_index = constructor_max_index;
8263 p->unfilled_index = constructor_unfilled_index;
8264 p->unfilled_fields = constructor_unfilled_fields;
8265 p->bit_index = constructor_bit_index;
8266 p->elements = constructor_elements;
8267 p->constant = constructor_constant;
8268 p->simple = constructor_simple;
a75b1c71 8269 p->nonconst = constructor_nonconst;
276beea2 8270 p->erroneous = constructor_erroneous;
8271 p->pending_elts = constructor_pending_elts;
8272 p->depth = constructor_depth;
3ad5f027 8273 p->replacement_value.value = 0;
8274 p->replacement_value.original_code = ERROR_MARK;
841fdaa6 8275 p->replacement_value.original_type = NULL;
276beea2 8276 p->implicit = 0;
8277 p->range_stack = 0;
8278 p->outer = 0;
8279 p->incremental = constructor_incremental;
8280 p->designated = constructor_designated;
74691f46 8281 p->designator_depth = designator_depth;
276beea2 8282 p->next = 0;
8283 constructor_stack = p;
189495e1 8284
276beea2 8285 constructor_constant = 1;
8286 constructor_simple = 1;
a75b1c71 8287 constructor_nonconst = 0;
276beea2 8288 constructor_depth = SPELLING_DEPTH ();
f1f41a6c 8289 constructor_elements = NULL;
276beea2 8290 constructor_pending_elts = 0;
8291 constructor_type = type;
8292 constructor_incremental = 1;
8293 constructor_designated = 0;
95cdf3fd 8294 constructor_zeroinit = 1;
276beea2 8295 designator_depth = 0;
ebe39827 8296 designator_erroneous = 0;
628fa4f8 8297
ef61516b 8298 if (RECORD_OR_UNION_TYPE_P (constructor_type))
628fa4f8 8299 {
276beea2 8300 constructor_fields = TYPE_FIELDS (constructor_type);
8301 /* Skip any nameless bit fields at the beginning. */
72749341 8302 while (constructor_fields != NULL_TREE
3cde3c29 8303 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
1767a056 8304 constructor_fields = DECL_CHAIN (constructor_fields);
a0c2c45b 8305
276beea2 8306 constructor_unfilled_fields = constructor_fields;
8307 constructor_bit_index = bitsize_zero_node;
628fa4f8 8308 }
276beea2 8309 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8310 {
8311 if (TYPE_DOMAIN (constructor_type))
8312 {
8313 constructor_max_index
8314 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
628fa4f8 8315
276beea2 8316 /* Detect non-empty initializations of zero-length arrays. */
8317 if (constructor_max_index == NULL_TREE
8318 && TYPE_SIZE (constructor_type))
2512209b 8319 constructor_max_index = integer_minus_one_node;
628fa4f8 8320
276beea2 8321 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8322 to initialize VLAs will cause a proper error; avoid tree
8323 checking errors as well by setting a safe value. */
8324 if (constructor_max_index
8325 && TREE_CODE (constructor_max_index) != INTEGER_CST)
2512209b 8326 constructor_max_index = integer_minus_one_node;
4619fc15 8327
276beea2 8328 constructor_index
8329 = convert (bitsizetype,
8330 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4619fc15 8331 }
276beea2 8332 else
fb213c85 8333 {
8334 constructor_index = bitsize_zero_node;
8335 constructor_max_index = NULL_TREE;
8336 }
4619fc15 8337
276beea2 8338 constructor_unfilled_index = constructor_index;
8339 }
6290f0db 8340 else if (VECTOR_TYPE_P (constructor_type))
276beea2 8341 {
8342 /* Vectors are like simple fixed-size arrays. */
8343 constructor_max_index =
ceb7b692 8344 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4aeeff51 8345 constructor_index = bitsize_zero_node;
276beea2 8346 constructor_unfilled_index = constructor_index;
8347 }
8348 else
8349 {
8350 /* Handle the case of int x = {5}; */
8351 constructor_fields = constructor_type;
8352 constructor_unfilled_fields = constructor_type;
8353 }
8354}
8355\f
8ccda24a 8356extern location_t last_init_list_comma;
8357
74a4140f 8358/* Called when we see an open brace for a nested initializer. Finish
8359 off any pending levels with implicit braces. */
8360void
8361finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8362{
8363 while (constructor_stack->implicit)
8364 {
8365 if (RECORD_OR_UNION_TYPE_P (constructor_type)
72749341 8366 && constructor_fields == NULL_TREE)
74a4140f 8367 process_init_element (input_location,
8ccda24a 8368 pop_init_level (loc, 1, braced_init_obstack,
8369 last_init_list_comma),
74a4140f 8370 true, braced_init_obstack);
8371 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8372 && constructor_max_index
8373 && tree_int_cst_lt (constructor_max_index,
8374 constructor_index))
8375 process_init_element (input_location,
8ccda24a 8376 pop_init_level (loc, 1, braced_init_obstack,
8377 last_init_list_comma),
74a4140f 8378 true, braced_init_obstack);
8379 else
8380 break;
8381 }
8382}
8383
276beea2 8384/* Push down into a subobject, for initialization.
8385 If this is for an explicit set of braces, IMPLICIT is 0.
8386 If it is because the next element belongs at a lower level,
8387 IMPLICIT is 1 (or 2 if the push is because of designator list). */
628fa4f8 8388
276beea2 8389void
53de1631 8390push_init_level (location_t loc, int implicit,
8391 struct obstack *braced_init_obstack)
276beea2 8392{
8393 struct constructor_stack *p;
8394 tree value = NULL_TREE;
628fa4f8 8395
276beea2 8396 /* Unless this is an explicit brace, we need to preserve previous
8397 content if any. */
8398 if (implicit)
8399 {
ef61516b 8400 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
759791ee 8401 value = find_init_member (constructor_fields, braced_init_obstack);
276beea2 8402 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
759791ee 8403 value = find_init_member (constructor_index, braced_init_obstack);
628fa4f8 8404 }
8405
9318f22c 8406 p = XNEW (struct constructor_stack);
276beea2 8407 p->type = constructor_type;
8408 p->fields = constructor_fields;
8409 p->index = constructor_index;
8410 p->max_index = constructor_max_index;
8411 p->unfilled_index = constructor_unfilled_index;
8412 p->unfilled_fields = constructor_unfilled_fields;
8413 p->bit_index = constructor_bit_index;
8414 p->elements = constructor_elements;
8415 p->constant = constructor_constant;
8416 p->simple = constructor_simple;
a75b1c71 8417 p->nonconst = constructor_nonconst;
276beea2 8418 p->erroneous = constructor_erroneous;
8419 p->pending_elts = constructor_pending_elts;
8420 p->depth = constructor_depth;
72749341 8421 p->replacement_value.value = NULL_TREE;
3ad5f027 8422 p->replacement_value.original_code = ERROR_MARK;
841fdaa6 8423 p->replacement_value.original_type = NULL;
276beea2 8424 p->implicit = implicit;
8425 p->outer = 0;
8426 p->incremental = constructor_incremental;
8427 p->designated = constructor_designated;
74691f46 8428 p->designator_depth = designator_depth;
276beea2 8429 p->next = constructor_stack;
8430 p->range_stack = 0;
8431 constructor_stack = p;
628fa4f8 8432
276beea2 8433 constructor_constant = 1;
8434 constructor_simple = 1;
a75b1c71 8435 constructor_nonconst = 0;
276beea2 8436 constructor_depth = SPELLING_DEPTH ();
f1f41a6c 8437 constructor_elements = NULL;
276beea2 8438 constructor_incremental = 1;
8439 constructor_designated = 0;
8440 constructor_pending_elts = 0;
8441 if (!implicit)
628fa4f8 8442 {
276beea2 8443 p->range_stack = constructor_range_stack;
8444 constructor_range_stack = 0;
8445 designator_depth = 0;
ebe39827 8446 designator_erroneous = 0;
276beea2 8447 }
628fa4f8 8448
276beea2 8449 /* Don't die if an entire brace-pair level is superfluous
8450 in the containing level. */
72749341 8451 if (constructor_type == NULL_TREE)
276beea2 8452 ;
ef61516b 8453 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
276beea2 8454 {
8455 /* Don't die if there are extra init elts at the end. */
72749341 8456 if (constructor_fields == NULL_TREE)
8457 constructor_type = NULL_TREE;
276beea2 8458 else
628fa4f8 8459 {
276beea2 8460 constructor_type = TREE_TYPE (constructor_fields);
8461 push_member_name (constructor_fields);
8462 constructor_depth++;
628fa4f8 8463 }
d44f2f7c 8464 /* If upper initializer is designated, then mark this as
8465 designated too to prevent bogus warnings. */
8466 constructor_designated = p->designated;
276beea2 8467 }
8468 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8469 {
8470 constructor_type = TREE_TYPE (constructor_type);
e913b5cd 8471 push_array_bounds (tree_to_uhwi (constructor_index));
276beea2 8472 constructor_depth++;
628fa4f8 8473 }
8474
72749341 8475 if (constructor_type == NULL_TREE)
628fa4f8 8476 {
53de1631 8477 error_init (loc, "extra brace group at end of initializer");
72749341 8478 constructor_fields = NULL_TREE;
8479 constructor_unfilled_fields = NULL_TREE;
276beea2 8480 return;
628fa4f8 8481 }
8482
276beea2 8483 if (value && TREE_CODE (value) == CONSTRUCTOR)
8484 {
8485 constructor_constant = TREE_CONSTANT (value);
8486 constructor_simple = TREE_STATIC (value);
a75b1c71 8487 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
276beea2 8488 constructor_elements = CONSTRUCTOR_ELTS (value);
f1f41a6c 8489 if (!vec_safe_is_empty (constructor_elements)
276beea2 8490 && (TREE_CODE (constructor_type) == RECORD_TYPE
8491 || TREE_CODE (constructor_type) == ARRAY_TYPE))
759791ee 8492 set_nonincremental_init (braced_init_obstack);
276beea2 8493 }
628fa4f8 8494
95cdf3fd 8495 if (implicit == 1)
8ccda24a 8496 {
8497 found_missing_braces = 1;
8498 if (initializer_stack->missing_brace_richloc)
8499 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8500 (loc, "{");
8501 }
628fa4f8 8502
ef61516b 8503 if (RECORD_OR_UNION_TYPE_P (constructor_type))
276beea2 8504 {
8505 constructor_fields = TYPE_FIELDS (constructor_type);
8506 /* Skip any nameless bit fields at the beginning. */
72749341 8507 while (constructor_fields != NULL_TREE
3cde3c29 8508 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
d059ef79 8509 constructor_fields = DECL_CHAIN (constructor_fields);
7de1d3ba 8510
276beea2 8511 constructor_unfilled_fields = constructor_fields;
8512 constructor_bit_index = bitsize_zero_node;
8513 }
6290f0db 8514 else if (VECTOR_TYPE_P (constructor_type))
276beea2 8515 {
8516 /* Vectors are like simple fixed-size arrays. */
8517 constructor_max_index =
ceb7b692 8518 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8519 constructor_index = bitsize_int (0);
276beea2 8520 constructor_unfilled_index = constructor_index;
8521 }
8522 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8523 {
8524 if (TYPE_DOMAIN (constructor_type))
8525 {
8526 constructor_max_index
8527 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
628fa4f8 8528
276beea2 8529 /* Detect non-empty initializations of zero-length arrays. */
8530 if (constructor_max_index == NULL_TREE
8531 && TYPE_SIZE (constructor_type))
2512209b 8532 constructor_max_index = integer_minus_one_node;
fc698c9d 8533
276beea2 8534 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8535 to initialize VLAs will cause a proper error; avoid tree
8536 checking errors as well by setting a safe value. */
8537 if (constructor_max_index
8538 && TREE_CODE (constructor_max_index) != INTEGER_CST)
2512209b 8539 constructor_max_index = integer_minus_one_node;
554732cd 8540
276beea2 8541 constructor_index
8542 = convert (bitsizetype,
8543 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8544 }
8545 else
8546 constructor_index = bitsize_zero_node;
fc698c9d 8547
276beea2 8548 constructor_unfilled_index = constructor_index;
8549 if (value && TREE_CODE (value) == STRING_CST)
8550 {
8551 /* We need to split the char/wchar array into individual
8552 characters, so that we don't have to special case it
8553 everywhere. */
759791ee 8554 set_nonincremental_init_from_string (value, braced_init_obstack);
276beea2 8555 }
8556 }
8557 else
8558 {
b765c734 8559 if (constructor_type != error_mark_node)
d44f2f7c 8560 warning_init (input_location, 0, "braces around scalar initializer");
276beea2 8561 constructor_fields = constructor_type;
8562 constructor_unfilled_fields = constructor_type;
8563 }
8564}
989bbcfc 8565
276beea2 8566/* At the end of an implicit or explicit brace level,
3ad5f027 8567 finish up that level of constructor. If a single expression
8568 with redundant braces initialized that level, return the
8569 c_expr structure for that expression. Otherwise, the original_code
8570 element is set to ERROR_MARK.
8571 If we were outputting the elements as they are read, return 0 as the value
276beea2 8572 from inner levels (process_init_element ignores that),
3ad5f027 8573 but return error_mark_node as the value from the outermost level
276beea2 8574 (that's what we want to put in DECL_INITIAL).
3ad5f027 8575 Otherwise, return a CONSTRUCTOR expression as the value. */
fc698c9d 8576
3ad5f027 8577struct c_expr
53de1631 8578pop_init_level (location_t loc, int implicit,
8ccda24a 8579 struct obstack *braced_init_obstack,
8580 location_t insert_before)
276beea2 8581{
8582 struct constructor_stack *p;
3ad5f027 8583 struct c_expr ret;
72749341 8584 ret.value = NULL_TREE;
3ad5f027 8585 ret.original_code = ERROR_MARK;
841fdaa6 8586 ret.original_type = NULL;
fc698c9d 8587
276beea2 8588 if (implicit == 0)
8589 {
8590 /* When we come to an explicit close brace,
8591 pop any inner levels that didn't have explicit braces. */
8592 while (constructor_stack->implicit)
adc93f31 8593 process_init_element (input_location,
8ccda24a 8594 pop_init_level (loc, 1, braced_init_obstack,
8595 insert_before),
adc93f31 8596 true, braced_init_obstack);
231bd014 8597 gcc_assert (!constructor_range_stack);
276beea2 8598 }
8ccda24a 8599 else
8600 if (initializer_stack->missing_brace_richloc)
8601 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8602 (insert_before, "}");
997d68fe 8603
36e0475c 8604 /* Now output all pending elements. */
8605 constructor_incremental = 1;
759791ee 8606 output_pending_init_elements (1, braced_init_obstack);
36e0475c 8607
276beea2 8608 p = constructor_stack;
997d68fe 8609
276beea2 8610 /* Error for initializing a flexible array member, or a zero-length
8611 array member in an inappropriate context. */
8612 if (constructor_type && constructor_fields
8613 && TREE_CODE (constructor_type) == ARRAY_TYPE
8614 && TYPE_DOMAIN (constructor_type)
84166705 8615 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
276beea2 8616 {
8617 /* Silently discard empty initializations. The parser will
8618 already have pedwarned for empty brackets. */
8619 if (integer_zerop (constructor_unfilled_index))
8620 constructor_type = NULL_TREE;
231bd014 8621 else
276beea2 8622 {
231bd014 8623 gcc_assert (!TYPE_SIZE (constructor_type));
a0c938f0 8624
276beea2 8625 if (constructor_depth > 2)
53de1631 8626 error_init (loc, "initialization of flexible array member in a nested context");
8864917d 8627 else
c9e51d25 8628 pedwarn_init (loc, OPT_Wpedantic,
21ca8540 8629 "initialization of a flexible array member");
fc698c9d 8630
276beea2 8631 /* We have already issued an error message for the existence
8632 of a flexible array member not at the end of the structure.
89f18f73 8633 Discard the initializer so that we do not die later. */
1767a056 8634 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
276beea2 8635 constructor_type = NULL_TREE;
8636 }
276beea2 8637 }
fc698c9d 8638
70279be9 8639 switch (vec_safe_length (constructor_elements))
8640 {
8641 case 0:
8642 /* Initialization with { } counts as zeroinit. */
8643 constructor_zeroinit = 1;
8644 break;
8645 case 1:
8646 /* This might be zeroinit as well. */
8647 if (integer_zerop ((*constructor_elements)[0].value))
8648 constructor_zeroinit = 1;
8649 break;
8650 default:
8651 /* If the constructor has more than one element, it can't be { 0 }. */
8652 constructor_zeroinit = 0;
8653 break;
8654 }
95cdf3fd 8655
8656 /* Warn when some structs are initialized with direct aggregation. */
8657 if (!implicit && found_missing_braces && warn_missing_braces
8658 && !constructor_zeroinit)
8ccda24a 8659 {
8660 gcc_assert (initializer_stack->missing_brace_richloc);
3b6578b3 8661 warning_at (initializer_stack->missing_brace_richloc,
8662 OPT_Wmissing_braces,
8663 "missing braces around initializer");
8ccda24a 8664 }
95cdf3fd 8665
276beea2 8666 /* Warn when some struct elements are implicitly initialized to zero. */
3304fb1c 8667 if (warn_missing_field_initializers
276beea2 8668 && constructor_type
8669 && TREE_CODE (constructor_type) == RECORD_TYPE
8670 && constructor_unfilled_fields)
8671 {
8672 /* Do not warn for flexible array members or zero-length arrays. */
8673 while (constructor_unfilled_fields
84166705 8674 && (!DECL_SIZE (constructor_unfilled_fields)
276beea2 8675 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
1767a056 8676 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
1955ae11 8677
ac4e675f 8678 if (constructor_unfilled_fields
8679 /* Do not warn if this level of the initializer uses member
8680 designators; it is likely to be deliberate. */
8681 && !constructor_designated
61a892dc 8682 /* Do not warn about initializing with { 0 } or with { }. */
ac4e675f 8683 && !constructor_zeroinit)
276beea2 8684 {
292bb106 8685 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8686 "missing initializer for field %qD of %qT",
8687 constructor_unfilled_fields,
8688 constructor_type))
8689 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
b0ec32b9 8690 "%qD declared here", constructor_unfilled_fields);
276beea2 8691 }
8692 }
fc698c9d 8693
276beea2 8694 /* Pad out the end of the structure. */
3ad5f027 8695 if (p->replacement_value.value)
276beea2 8696 /* If this closes a superfluous brace pair,
8697 just pass out the element between them. */
3ad5f027 8698 ret = p->replacement_value;
72749341 8699 else if (constructor_type == NULL_TREE)
276beea2 8700 ;
ef61516b 8701 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
276beea2 8702 && TREE_CODE (constructor_type) != ARRAY_TYPE
6290f0db 8703 && !VECTOR_TYPE_P (constructor_type))
276beea2 8704 {
8705 /* A nonincremental scalar initializer--just return
8706 the element, after verifying there is just one. */
f1f41a6c 8707 if (vec_safe_is_empty (constructor_elements))
276beea2 8708 {
8709 if (!constructor_erroneous)
53de1631 8710 error_init (loc, "empty scalar initializer");
3ad5f027 8711 ret.value = error_mark_node;
276beea2 8712 }
f1f41a6c 8713 else if (vec_safe_length (constructor_elements) != 1)
276beea2 8714 {
53de1631 8715 error_init (loc, "extra elements in scalar initializer");
f1f41a6c 8716 ret.value = (*constructor_elements)[0].value;
276beea2 8717 }
8718 else
f1f41a6c 8719 ret.value = (*constructor_elements)[0].value;
276beea2 8720 }
8721 else
8722 {
8723 if (constructor_erroneous)
3ad5f027 8724 ret.value = error_mark_node;
276beea2 8725 else
8726 {
3ad5f027 8727 ret.value = build_constructor (constructor_type,
c75b4594 8728 constructor_elements);
276beea2 8729 if (constructor_constant)
c7d4e749 8730 TREE_CONSTANT (ret.value) = 1;
276beea2 8731 if (constructor_constant && constructor_simple)
3ad5f027 8732 TREE_STATIC (ret.value) = 1;
a75b1c71 8733 if (constructor_nonconst)
8734 CONSTRUCTOR_NON_CONST (ret.value) = 1;
276beea2 8735 }
8736 }
fc698c9d 8737
a75b1c71 8738 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8739 {
8740 if (constructor_nonconst)
8741 ret.original_code = C_MAYBE_CONST_EXPR;
8742 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8743 ret.original_code = ERROR_MARK;
8744 }
8745
276beea2 8746 constructor_type = p->type;
8747 constructor_fields = p->fields;
8748 constructor_index = p->index;
8749 constructor_max_index = p->max_index;
8750 constructor_unfilled_index = p->unfilled_index;
8751 constructor_unfilled_fields = p->unfilled_fields;
8752 constructor_bit_index = p->bit_index;
8753 constructor_elements = p->elements;
8754 constructor_constant = p->constant;
8755 constructor_simple = p->simple;
a75b1c71 8756 constructor_nonconst = p->nonconst;
276beea2 8757 constructor_erroneous = p->erroneous;
8758 constructor_incremental = p->incremental;
8759 constructor_designated = p->designated;
74691f46 8760 designator_depth = p->designator_depth;
276beea2 8761 constructor_pending_elts = p->pending_elts;
8762 constructor_depth = p->depth;
8763 if (!p->implicit)
8764 constructor_range_stack = p->range_stack;
8765 RESTORE_SPELLING_DEPTH (constructor_depth);
fc698c9d 8766
276beea2 8767 constructor_stack = p->next;
8768 free (p);
52136990 8769
72749341 8770 if (ret.value == NULL_TREE && constructor_stack == 0)
09d2b71a 8771 ret.value = error_mark_node;
3ad5f027 8772 return ret;
276beea2 8773}
989bbcfc 8774
276beea2 8775/* Common handling for both array range and field name designators.
2823920b 8776 ARRAY argument is nonzero for array ranges. Returns false for success. */
628fa4f8 8777
2823920b 8778static bool
623ee358 8779set_designator (location_t loc, bool array,
53de1631 8780 struct obstack *braced_init_obstack)
fc698c9d 8781{
276beea2 8782 tree subtype;
8783 enum tree_code subcode;
fc698c9d 8784
276beea2 8785 /* Don't die if an entire brace-pair level is superfluous
8786 in the containing level. */
72749341 8787 if (constructor_type == NULL_TREE)
2823920b 8788 return true;
fc698c9d 8789
231bd014 8790 /* If there were errors in this designator list already, bail out
8791 silently. */
ebe39827 8792 if (designator_erroneous)
2823920b 8793 return true;
0583de9c 8794
276beea2 8795 if (!designator_depth)
8796 {
231bd014 8797 gcc_assert (!constructor_range_stack);
fc698c9d 8798
276beea2 8799 /* Designator list starts at the level of closest explicit
8800 braces. */
8801 while (constructor_stack->implicit)
adc93f31 8802 process_init_element (input_location,
8ccda24a 8803 pop_init_level (loc, 1, braced_init_obstack,
8804 last_init_list_comma),
adc93f31 8805 true, braced_init_obstack);
276beea2 8806 constructor_designated = 1;
2823920b 8807 return false;
276beea2 8808 }
fc698c9d 8809
231bd014 8810 switch (TREE_CODE (constructor_type))
f1c75cb5 8811 {
231bd014 8812 case RECORD_TYPE:
8813 case UNION_TYPE:
276beea2 8814 subtype = TREE_TYPE (constructor_fields);
8815 if (subtype != error_mark_node)
8816 subtype = TYPE_MAIN_VARIANT (subtype);
231bd014 8817 break;
8818 case ARRAY_TYPE:
276beea2 8819 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
231bd014 8820 break;
8821 default:
8822 gcc_unreachable ();
fc698c9d 8823 }
628fa4f8 8824
276beea2 8825 subcode = TREE_CODE (subtype);
8826 if (array && subcode != ARRAY_TYPE)
8827 {
53de1631 8828 error_init (loc, "array index in non-array initializer");
2823920b 8829 return true;
276beea2 8830 }
8831 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8832 {
53de1631 8833 error_init (loc, "field name not in record or union initializer");
2823920b 8834 return true;
276beea2 8835 }
f9e15121 8836
276beea2 8837 constructor_designated = 1;
74a4140f 8838 finish_implicit_inits (loc, braced_init_obstack);
53de1631 8839 push_init_level (loc, 2, braced_init_obstack);
2823920b 8840 return false;
fc698c9d 8841}
628fa4f8 8842
276beea2 8843/* If there are range designators in designator list, push a new designator
8844 to constructor_range_stack. RANGE_END is end of such stack range or
8845 NULL_TREE if there is no range designator at this level. */
628fa4f8 8846
276beea2 8847static void
759791ee 8848push_range_stack (tree range_end, struct obstack * braced_init_obstack)
276beea2 8849{
8850 struct constructor_range_stack *p;
628fa4f8 8851
759791ee 8852 p = (struct constructor_range_stack *)
8853 obstack_alloc (braced_init_obstack,
8854 sizeof (struct constructor_range_stack));
276beea2 8855 p->prev = constructor_range_stack;
8856 p->next = 0;
8857 p->fields = constructor_fields;
8858 p->range_start = constructor_index;
8859 p->index = constructor_index;
8860 p->stack = constructor_stack;
8861 p->range_end = range_end;
989bbcfc 8862 if (constructor_range_stack)
276beea2 8863 constructor_range_stack->next = p;
8864 constructor_range_stack = p;
fc698c9d 8865}
628fa4f8 8866
276beea2 8867/* Within an array initializer, specify the next index to be initialized.
8868 FIRST is that index. If LAST is nonzero, then initialize a range
8869 of indices, running from FIRST through LAST. */
ca9c4c86 8870
fc698c9d 8871void
53de1631 8872set_init_index (location_t loc, tree first, tree last,
c9e51d25 8873 struct obstack *braced_init_obstack)
fc698c9d 8874{
623ee358 8875 if (set_designator (loc, true, braced_init_obstack))
276beea2 8876 return;
fc698c9d 8877
ebe39827 8878 designator_erroneous = 1;
fc698c9d 8879
a250ed2e 8880 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8881 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8882 {
53de1631 8883 error_init (loc, "array index in initializer not of integer type");
a250ed2e 8884 return;
8885 }
8886
88c32270 8887 if (TREE_CODE (first) != INTEGER_CST)
8888 {
8889 first = c_fully_fold (first, false, NULL);
8890 if (TREE_CODE (first) == INTEGER_CST)
c9e51d25 8891 pedwarn_init (loc, OPT_Wpedantic,
88c32270 8892 "array index in initializer is not "
8893 "an integer constant expression");
8894 }
8895
8896 if (last && TREE_CODE (last) != INTEGER_CST)
8897 {
8898 last = c_fully_fold (last, false, NULL);
8899 if (TREE_CODE (last) == INTEGER_CST)
c9e51d25 8900 pedwarn_init (loc, OPT_Wpedantic,
88c32270 8901 "array index in initializer is not "
8902 "an integer constant expression");
8903 }
8904
276beea2 8905 if (TREE_CODE (first) != INTEGER_CST)
53de1631 8906 error_init (loc, "nonconstant array index in initializer");
72749341 8907 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
53de1631 8908 error_init (loc, "nonconstant array index in initializer");
276beea2 8909 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
53de1631 8910 error_init (loc, "array index in non-array initializer");
42b1a257 8911 else if (tree_int_cst_sgn (first) == -1)
53de1631 8912 error_init (loc, "array index in initializer exceeds array bounds");
276beea2 8913 else if (constructor_max_index
8914 && tree_int_cst_lt (constructor_max_index, first))
53de1631 8915 error_init (loc, "array index in initializer exceeds array bounds");
276beea2 8916 else
fc698c9d 8917 {
a75b1c71 8918 constant_expression_warning (first);
8919 if (last)
8920 constant_expression_warning (last);
276beea2 8921 constructor_index = convert (bitsizetype, first);
f5298614 8922 if (tree_int_cst_lt (constructor_index, first))
8923 {
8924 constructor_index = copy_node (constructor_index);
8925 TREE_OVERFLOW (constructor_index) = 1;
8926 }
5d844ba2 8927
276beea2 8928 if (last)
886de5de 8929 {
276beea2 8930 if (tree_int_cst_equal (first, last))
72749341 8931 last = NULL_TREE;
276beea2 8932 else if (tree_int_cst_lt (last, first))
8933 {
53de1631 8934 error_init (loc, "empty index range in initializer");
72749341 8935 last = NULL_TREE;
276beea2 8936 }
8937 else
8938 {
8939 last = convert (bitsizetype, last);
72749341 8940 if (constructor_max_index != NULL_TREE
276beea2 8941 && tree_int_cst_lt (constructor_max_index, last))
8942 {
53de1631 8943 error_init (loc, "array index range in initializer exceeds "
8944 "array bounds");
72749341 8945 last = NULL_TREE;
276beea2 8946 }
8947 }
886de5de 8948 }
902de8ed 8949
276beea2 8950 designator_depth++;
ebe39827 8951 designator_erroneous = 0;
276beea2 8952 if (constructor_range_stack || last)
759791ee 8953 push_range_stack (last, braced_init_obstack);
fc698c9d 8954 }
fc698c9d 8955}
276beea2 8956
8957/* Within a struct initializer, specify the next field to be initialized. */
628fa4f8 8958
fc698c9d 8959void
527790c1 8960set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
53de1631 8961 struct obstack *braced_init_obstack)
fc698c9d 8962{
10064fb9 8963 tree field;
d354d271 8964
623ee358 8965 if (set_designator (loc, false, braced_init_obstack))
276beea2 8966 return;
8967
ebe39827 8968 designator_erroneous = 1;
276beea2 8969
ef61516b 8970 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
d354d271 8971 {
53de1631 8972 error_init (loc, "field name not in record or union initializer");
276beea2 8973 return;
d354d271 8974 }
8975
10064fb9 8976 field = lookup_field (constructor_type, fieldname);
989bbcfc 8977
72749341 8978 if (field == NULL_TREE)
527790c1 8979 {
8980 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8981 if (guessed_id)
8982 {
2a990300 8983 gcc_rich_location rich_loc (fieldname_loc);
8984 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
3b6578b3 8985 error_at (&rich_loc,
8986 "%qT has no member named %qE; did you mean %qE?",
8987 constructor_type, fieldname, guessed_id);
527790c1 8988 }
8989 else
8990 error_at (fieldname_loc, "%qT has no member named %qE",
8991 constructor_type, fieldname);
8992 }
276beea2 8993 else
10064fb9 8994 do
8995 {
8996 constructor_fields = TREE_VALUE (field);
8997 designator_depth++;
8998 designator_erroneous = 0;
8999 if (constructor_range_stack)
9000 push_range_stack (NULL_TREE, braced_init_obstack);
9001 field = TREE_CHAIN (field);
9002 if (field)
9003 {
623ee358 9004 if (set_designator (loc, false, braced_init_obstack))
10064fb9 9005 return;
9006 }
9007 }
9008 while (field != NULL_TREE);
276beea2 9009}
9010\f
9011/* Add a new initializer to the tree of pending initializers. PURPOSE
9012 identifies the initializer, either array index or field in a structure.
b9c74b4d 9013 VALUE is the value of that index or field. If ORIGTYPE is not
9014 NULL_TREE, it is the original type of VALUE.
d241bf0d 9015
9016 IMPLICIT is true if value comes from pop_init_level (1),
9017 the new initializer has been merged with the existing one
9018 and thus no warnings should be emitted about overriding an
9019 existing initializer. */
fc698c9d 9020
276beea2 9021static void
d44f2f7c 9022add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9023 bool implicit, struct obstack *braced_init_obstack)
276beea2 9024{
9025 struct init_node *p, **q, *r;
9026
9027 q = &constructor_pending_elts;
9028 p = 0;
9029
9030 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
fc698c9d 9031 {
276beea2 9032 while (*q != 0)
812fff1a 9033 {
276beea2 9034 p = *q;
9035 if (tree_int_cst_lt (purpose, p->purpose))
9036 q = &p->left;
9037 else if (tree_int_cst_lt (p->purpose, purpose))
9038 q = &p->right;
9039 else
9040 {
d241bf0d 9041 if (!implicit)
9042 {
9043 if (TREE_SIDE_EFFECTS (p->value))
dc5945dc 9044 warning_init (loc, OPT_Woverride_init_side_effects,
d44f2f7c 9045 "initialized field with side-effects "
9046 "overwritten");
d241bf0d 9047 else if (warn_override_init)
d44f2f7c 9048 warning_init (loc, OPT_Woverride_init,
9049 "initialized field overwritten");
d241bf0d 9050 }
276beea2 9051 p->value = value;
b9c74b4d 9052 p->origtype = origtype;
276beea2 9053 return;
9054 }
812fff1a 9055 }
fc698c9d 9056 }
276beea2 9057 else
fc698c9d 9058 {
276beea2 9059 tree bitpos;
628fa4f8 9060
276beea2 9061 bitpos = bit_position (purpose);
9062 while (*q != NULL)
9063 {
9064 p = *q;
9065 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9066 q = &p->left;
9067 else if (p->purpose != purpose)
9068 q = &p->right;
9069 else
9070 {
d241bf0d 9071 if (!implicit)
9072 {
9073 if (TREE_SIDE_EFFECTS (p->value))
dc5945dc 9074 warning_init (loc, OPT_Woverride_init_side_effects,
d44f2f7c 9075 "initialized field with side-effects "
9076 "overwritten");
d241bf0d 9077 else if (warn_override_init)
d44f2f7c 9078 warning_init (loc, OPT_Woverride_init,
9079 "initialized field overwritten");
d241bf0d 9080 }
276beea2 9081 p->value = value;
b9c74b4d 9082 p->origtype = origtype;
276beea2 9083 return;
9084 }
9085 }
812fff1a 9086 }
89a5f6d1 9087
759791ee 9088 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9089 sizeof (struct init_node));
276beea2 9090 r->purpose = purpose;
9091 r->value = value;
b9c74b4d 9092 r->origtype = origtype;
989bbcfc 9093
276beea2 9094 *q = r;
9095 r->parent = p;
9096 r->left = 0;
9097 r->right = 0;
9098 r->balance = 0;
89a5f6d1 9099
276beea2 9100 while (p)
fc698c9d 9101 {
276beea2 9102 struct init_node *s;
5d844ba2 9103
276beea2 9104 if (r == p->left)
886de5de 9105 {
276beea2 9106 if (p->balance == 0)
9107 p->balance = -1;
9108 else if (p->balance < 0)
9109 {
9110 if (r->balance < 0)
9111 {
9112 /* L rotation. */
9113 p->left = r->right;
9114 if (p->left)
9115 p->left->parent = p;
9116 r->right = p;
26877a3f 9117
276beea2 9118 p->balance = 0;
9119 r->balance = 0;
c052c0ba 9120
276beea2 9121 s = p->parent;
9122 p->parent = r;
9123 r->parent = s;
9124 if (s)
9125 {
9126 if (s->left == p)
9127 s->left = r;
9128 else
9129 s->right = r;
9130 }
9131 else
9132 constructor_pending_elts = r;
9133 }
9134 else
9135 {
9136 /* LR rotation. */
9137 struct init_node *t = r->right;
26877a3f 9138
276beea2 9139 r->right = t->left;
9140 if (r->right)
9141 r->right->parent = r;
9142 t->left = r;
9143
9144 p->left = t->right;
9145 if (p->left)
9146 p->left->parent = p;
9147 t->right = p;
9148
9149 p->balance = t->balance < 0;
9150 r->balance = -(t->balance > 0);
9151 t->balance = 0;
9152
9153 s = p->parent;
9154 p->parent = t;
9155 r->parent = t;
9156 t->parent = s;
9157 if (s)
9158 {
9159 if (s->left == p)
9160 s->left = t;
9161 else
9162 s->right = t;
9163 }
9164 else
9165 constructor_pending_elts = t;
9166 }
9167 break;
9168 }
9169 else
9170 {
9171 /* p->balance == +1; growth of left side balances the node. */
9172 p->balance = 0;
9173 break;
9174 }
886de5de 9175 }
276beea2 9176 else /* r == p->right */
9177 {
9178 if (p->balance == 0)
9179 /* Growth propagation from right side. */
9180 p->balance++;
9181 else if (p->balance > 0)
9182 {
9183 if (r->balance > 0)
9184 {
9185 /* R rotation. */
9186 p->right = r->left;
9187 if (p->right)
9188 p->right->parent = p;
9189 r->left = p;
9190
9191 p->balance = 0;
9192 r->balance = 0;
9193
9194 s = p->parent;
9195 p->parent = r;
9196 r->parent = s;
9197 if (s)
9198 {
9199 if (s->left == p)
9200 s->left = r;
9201 else
9202 s->right = r;
9203 }
9204 else
9205 constructor_pending_elts = r;
9206 }
9207 else /* r->balance == -1 */
9208 {
9209 /* RL rotation */
9210 struct init_node *t = r->left;
9211
9212 r->left = t->right;
9213 if (r->left)
9214 r->left->parent = r;
9215 t->right = r;
9216
9217 p->right = t->left;
9218 if (p->right)
9219 p->right->parent = p;
9220 t->left = p;
9221
9222 r->balance = (t->balance < 0);
9223 p->balance = -(t->balance > 0);
9224 t->balance = 0;
9225
9226 s = p->parent;
9227 p->parent = t;
9228 r->parent = t;
9229 t->parent = s;
9230 if (s)
9231 {
9232 if (s->left == p)
9233 s->left = t;
9234 else
9235 s->right = t;
9236 }
9237 else
9238 constructor_pending_elts = t;
9239 }
9240 break;
9241 }
9242 else
9243 {
9244 /* p->balance == -1; growth of right side balances the node. */
9245 p->balance = 0;
9246 break;
9247 }
9248 }
9249
9250 r = p;
9251 p = p->parent;
9252 }
9253}
9254
9255/* Build AVL tree from a sorted chain. */
9256
9257static void
759791ee 9258set_nonincremental_init (struct obstack * braced_init_obstack)
276beea2 9259{
c75b4594 9260 unsigned HOST_WIDE_INT ix;
9261 tree index, value;
276beea2 9262
9263 if (TREE_CODE (constructor_type) != RECORD_TYPE
9264 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9265 return;
9266
c75b4594 9267 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
d44f2f7c 9268 add_pending_init (input_location, index, value, NULL_TREE, true,
9269 braced_init_obstack);
f1f41a6c 9270 constructor_elements = NULL;
276beea2 9271 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9272 {
9273 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9274 /* Skip any nameless bit fields at the beginning. */
72749341 9275 while (constructor_unfilled_fields != NULL_TREE
3cde3c29 9276 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
276beea2 9277 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
902de8ed 9278
fc698c9d 9279 }
276beea2 9280 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
fc698c9d 9281 {
276beea2 9282 if (TYPE_DOMAIN (constructor_type))
9283 constructor_unfilled_index
9284 = convert (bitsizetype,
9285 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9286 else
9287 constructor_unfilled_index = bitsize_zero_node;
fc698c9d 9288 }
276beea2 9289 constructor_incremental = 0;
fc698c9d 9290}
628fa4f8 9291
276beea2 9292/* Build AVL tree from a string constant. */
fc698c9d 9293
276beea2 9294static void
759791ee 9295set_nonincremental_init_from_string (tree str,
9296 struct obstack * braced_init_obstack)
fc698c9d 9297{
276beea2 9298 tree value, purpose, type;
9299 HOST_WIDE_INT val[2];
9300 const char *p, *end;
9301 int byte, wchar_bytes, charwidth, bitpos;
fc698c9d 9302
231bd014 9303 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
b9fc4354 9304
b0726616 9305 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
276beea2 9306 charwidth = TYPE_PRECISION (char_type_node);
7175bb2a 9307 gcc_assert ((size_t) wchar_bytes * charwidth
9308 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
276beea2 9309 type = TREE_TYPE (constructor_type);
9310 p = TREE_STRING_POINTER (str);
9311 end = p + TREE_STRING_LENGTH (str);
812fff1a 9312
276beea2 9313 for (purpose = bitsize_zero_node;
54550ecb 9314 p < end
9315 && !(constructor_max_index
9316 && tree_int_cst_lt (constructor_max_index, purpose));
276beea2 9317 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
15119d83 9318 {
276beea2 9319 if (wchar_bytes == 1)
21c8999d 9320 {
e913b5cd 9321 val[0] = (unsigned char) *p++;
9322 val[1] = 0;
21c8999d 9323 }
9324 else
276beea2 9325 {
276beea2 9326 val[1] = 0;
e913b5cd 9327 val[0] = 0;
276beea2 9328 for (byte = 0; byte < wchar_bytes; byte++)
9329 {
9330 if (BYTES_BIG_ENDIAN)
9331 bitpos = (wchar_bytes - byte - 1) * charwidth;
9332 else
9333 bitpos = byte * charwidth;
7175bb2a 9334 val[bitpos / HOST_BITS_PER_WIDE_INT]
276beea2 9335 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9336 << (bitpos % HOST_BITS_PER_WIDE_INT);
9337 }
9338 }
15119d83 9339
78a8ed03 9340 if (!TYPE_UNSIGNED (type))
276beea2 9341 {
9342 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9343 if (bitpos < HOST_BITS_PER_WIDE_INT)
9344 {
59c1507a 9345 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
276beea2 9346 {
85f5e2ee 9347 val[0] |= HOST_WIDE_INT_M1U << bitpos;
e913b5cd 9348 val[1] = -1;
276beea2 9349 }
9350 }
9351 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9352 {
e913b5cd 9353 if (val[0] < 0)
9354 val[1] = -1;
276beea2 9355 }
59c1507a 9356 else if (val[1] & (HOST_WIDE_INT_1
276beea2 9357 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
85f5e2ee 9358 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
276beea2 9359 }
21c8999d 9360
ddb1be65 9361 value = wide_int_to_tree (type,
9362 wide_int::from_array (val, 2,
e913b5cd 9363 HOST_BITS_PER_WIDE_INT * 2));
d44f2f7c 9364 add_pending_init (input_location, purpose, value, NULL_TREE, true,
759791ee 9365 braced_init_obstack);
03632119 9366 }
9367
276beea2 9368 constructor_incremental = 0;
9369}
fc698c9d 9370
78173203 9371/* Return value of FIELD in pending initializer or NULL_TREE if the field was
276beea2 9372 not initialized yet. */
9373
9374static tree
759791ee 9375find_init_member (tree field, struct obstack * braced_init_obstack)
276beea2 9376{
9377 struct init_node *p;
9378
9379 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
37c005bb 9380 {
276beea2 9381 if (constructor_incremental
9382 && tree_int_cst_lt (field, constructor_unfilled_index))
759791ee 9383 set_nonincremental_init (braced_init_obstack);
276beea2 9384
9385 p = constructor_pending_elts;
9386 while (p)
37c005bb 9387 {
276beea2 9388 if (tree_int_cst_lt (field, p->purpose))
9389 p = p->left;
9390 else if (tree_int_cst_lt (p->purpose, field))
9391 p = p->right;
9392 else
9393 return p->value;
37c005bb 9394 }
37c005bb 9395 }
276beea2 9396 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
fc698c9d 9397 {
276beea2 9398 tree bitpos = bit_position (field);
fc698c9d 9399
276beea2 9400 if (constructor_incremental
9401 && (!constructor_unfilled_fields
9402 || tree_int_cst_lt (bitpos,
9403 bit_position (constructor_unfilled_fields))))
759791ee 9404 set_nonincremental_init (braced_init_obstack);
fc698c9d 9405
276beea2 9406 p = constructor_pending_elts;
9407 while (p)
9408 {
9409 if (field == p->purpose)
9410 return p->value;
9411 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9412 p = p->left;
9413 else
9414 p = p->right;
9415 }
9416 }
9417 else if (TREE_CODE (constructor_type) == UNION_TYPE)
fc698c9d 9418 {
f1f41a6c 9419 if (!vec_safe_is_empty (constructor_elements)
9420 && (constructor_elements->last ().index == field))
9421 return constructor_elements->last ().value;
fc698c9d 9422 }
78173203 9423 return NULL_TREE;
fc698c9d 9424}
9425
276beea2 9426/* "Output" the next constructor element.
9427 At top level, really output it to assembler code now.
9428 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
b9c74b4d 9429 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
276beea2 9430 TYPE is the data type that the containing data type wants here.
9431 FIELD is the field (a FIELD_DECL) or the index that this element fills.
3ad5f027 9432 If VALUE is a string constant, STRICT_STRING is true if it is
9433 unparenthesized or we should not warn here for it being parenthesized.
9434 For other types of VALUE, STRICT_STRING is not used.
989bbcfc 9435
623ee358 9436 PENDING if true means output pending elements that belong
9437 right after this element. (PENDING is normally true;
9438 it is false while outputting pending elements, to avoid recursion.)
d241bf0d 9439
9440 IMPLICIT is true if value comes from pop_init_level (1),
9441 the new initializer has been merged with the existing one
9442 and thus no warnings should be emitted about overriding an
9443 existing initializer. */
989bbcfc 9444
276beea2 9445static void
adc93f31 9446output_init_element (location_t loc, tree value, tree origtype,
623ee358 9447 bool strict_string, tree type, tree field, bool pending,
adc93f31 9448 bool implicit, struct obstack * braced_init_obstack)
276beea2 9449{
c6418a4e 9450 tree semantic_type = NULL_TREE;
a75b1c71 9451 bool maybe_const = true;
9452 bool npc;
c75b4594 9453
081b8c23 9454 if (type == error_mark_node || value == error_mark_node)
989bbcfc 9455 {
276beea2 9456 constructor_erroneous = 1;
9457 return;
989bbcfc 9458 }
f14c8207 9459 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9460 && (TREE_CODE (value) == STRING_CST
9461 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9462 && !(TREE_CODE (value) == STRING_CST
9463 && TREE_CODE (type) == ARRAY_TYPE
9464 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9465 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9466 TYPE_MAIN_VARIANT (type)))
e60a6f7b 9467 value = array_to_pointer_conversion (input_location, value);
989bbcfc 9468
276beea2 9469 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
d303aeaf 9470 && require_constant_value && pending)
989bbcfc 9471 {
276beea2 9472 /* As an extension, allow initializing objects with static storage
9473 duration with compound literals (which are then treated just as
9474 the brace enclosed list they contain). */
d303aeaf 9475 if (flag_isoc99)
9476 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9477 "constant");
276beea2 9478 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9479 value = DECL_INITIAL (decl);
989bbcfc 9480 }
9481
a75b1c71 9482 npc = null_pointer_constant_p (value);
c6418a4e 9483 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9484 {
9485 semantic_type = TREE_TYPE (value);
9486 value = TREE_OPERAND (value, 0);
9487 }
a75b1c71 9488 value = c_fully_fold (value, require_constant_value, &maybe_const);
9489
276beea2 9490 if (value == error_mark_node)
9491 constructor_erroneous = 1;
9492 else if (!TREE_CONSTANT (value))
9493 constructor_constant = 0;
292237f3 9494 else if (!initializer_constant_valid_p (value,
9495 TREE_TYPE (value),
9496 AGGREGATE_TYPE_P (constructor_type)
9497 && TYPE_REVERSE_STORAGE_ORDER
9498 (constructor_type))
ef61516b 9499 || (RECORD_OR_UNION_TYPE_P (constructor_type)
276beea2 9500 && DECL_C_BIT_FIELD (field)
9501 && TREE_CODE (value) != INTEGER_CST))
9502 constructor_simple = 0;
a75b1c71 9503 if (!maybe_const)
9504 constructor_nonconst = 1;
276beea2 9505
9c3ffbcb 9506 /* Digest the initializer and issue any errors about incompatible
9507 types before issuing errors about non-constant initializers. */
9508 tree new_value = value;
9509 if (semantic_type)
9510 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9511 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9512 require_constant_value);
9513 if (new_value == error_mark_node)
9514 {
9515 constructor_erroneous = 1;
9516 return;
9517 }
9518 if (require_constant_value || require_constant_elements)
9519 constant_expression_warning (new_value);
9520
9521 /* Proceed to check the constness of the original initializer. */
fdf95cc9 9522 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
989bbcfc 9523 {
fdf95cc9 9524 if (require_constant_value)
9525 {
53de1631 9526 error_init (loc, "initializer element is not constant");
fdf95cc9 9527 value = error_mark_node;
9528 }
9529 else if (require_constant_elements)
af379dd3 9530 pedwarn (loc, OPT_Wpedantic,
21ca8540 9531 "initializer element is not computable at load time");
989bbcfc 9532 }
a75b1c71 9533 else if (!maybe_const
9534 && (require_constant_value || require_constant_elements))
af379dd3 9535 pedwarn_init (loc, OPT_Wpedantic,
a75b1c71 9536 "initializer element is not a constant expression");
276beea2 9537
bc620c5c 9538 /* Issue -Wc++-compat warnings about initializing a bitfield with
9539 enum type. */
9540 if (warn_cxx_compat
9541 && field != NULL_TREE
9542 && TREE_CODE (field) == FIELD_DECL
9543 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9544 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9545 != TYPE_MAIN_VARIANT (type))
9546 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9547 {
9548 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9549 if (checktype != error_mark_node
9550 && (TYPE_MAIN_VARIANT (checktype)
9551 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
d44f2f7c 9552 warning_init (loc, OPT_Wc___compat,
bc620c5c 9553 "enum conversion in initialization is invalid in C++");
9554 }
9555
1b2ff8ae 9556 /* If this field is empty and does not have side effects (and is not at
9557 the end of structure), don't do anything other than checking the
9558 initializer. */
276beea2 9559 if (field
9560 && (TREE_TYPE (field) == error_mark_node
9561 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9562 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
1b2ff8ae 9563 && !TREE_SIDE_EFFECTS (new_value)
276beea2 9564 && (TREE_CODE (constructor_type) == ARRAY_TYPE
1767a056 9565 || DECL_CHAIN (field)))))
276beea2 9566 return;
9567
9c3ffbcb 9568 /* Finally, set VALUE to the initializer value digested above. */
9569 value = new_value;
989bbcfc 9570
276beea2 9571 /* If this element doesn't come next in sequence,
9572 put it on constructor_pending_elts. */
9573 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9574 && (!constructor_incremental
9575 || !tree_int_cst_equal (field, constructor_unfilled_index)))
989bbcfc 9576 {
276beea2 9577 if (constructor_incremental
9578 && tree_int_cst_lt (field, constructor_unfilled_index))
759791ee 9579 set_nonincremental_init (braced_init_obstack);
276beea2 9580
d44f2f7c 9581 add_pending_init (loc, field, value, origtype, implicit,
759791ee 9582 braced_init_obstack);
276beea2 9583 return;
989bbcfc 9584 }
276beea2 9585 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9586 && (!constructor_incremental
9587 || field != constructor_unfilled_fields))
989bbcfc 9588 {
276beea2 9589 /* We do this for records but not for unions. In a union,
9590 no matter which field is specified, it can be initialized
9591 right away since it starts at the beginning of the union. */
9592 if (constructor_incremental)
9593 {
9594 if (!constructor_unfilled_fields)
759791ee 9595 set_nonincremental_init (braced_init_obstack);
276beea2 9596 else
9597 {
9598 tree bitpos, unfillpos;
9599
9600 bitpos = bit_position (field);
9601 unfillpos = bit_position (constructor_unfilled_fields);
9602
9603 if (tree_int_cst_lt (bitpos, unfillpos))
759791ee 9604 set_nonincremental_init (braced_init_obstack);
276beea2 9605 }
9606 }
9607
d44f2f7c 9608 add_pending_init (loc, field, value, origtype, implicit,
759791ee 9609 braced_init_obstack);
276beea2 9610 return;
989bbcfc 9611 }
276beea2 9612 else if (TREE_CODE (constructor_type) == UNION_TYPE
f1f41a6c 9613 && !vec_safe_is_empty (constructor_elements))
276beea2 9614 {
d241bf0d 9615 if (!implicit)
9616 {
f1f41a6c 9617 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
dc5945dc 9618 warning_init (loc, OPT_Woverride_init_side_effects,
d241bf0d 9619 "initialized field with side-effects overwritten");
9620 else if (warn_override_init)
d44f2f7c 9621 warning_init (loc, OPT_Woverride_init,
9622 "initialized field overwritten");
d241bf0d 9623 }
989bbcfc 9624
276beea2 9625 /* We can have just one union field set. */
f1f41a6c 9626 constructor_elements = NULL;
276beea2 9627 }
989bbcfc 9628
276beea2 9629 /* Otherwise, output this element either to
9630 constructor_elements or to the assembler file. */
989bbcfc 9631
e82e4eb5 9632 constructor_elt celt = {field, value};
f1f41a6c 9633 vec_safe_push (constructor_elements, celt);
989bbcfc 9634
276beea2 9635 /* Advance the variable that indicates sequential elements output. */
9636 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9637 constructor_unfilled_index
389dd41b 9638 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9639 bitsize_one_node);
276beea2 9640 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9641 {
9642 constructor_unfilled_fields
1767a056 9643 = DECL_CHAIN (constructor_unfilled_fields);
989bbcfc 9644
276beea2 9645 /* Skip any nameless bit fields. */
72749341 9646 while (constructor_unfilled_fields != NULL_TREE
3cde3c29 9647 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
276beea2 9648 constructor_unfilled_fields =
1767a056 9649 DECL_CHAIN (constructor_unfilled_fields);
276beea2 9650 }
9651 else if (TREE_CODE (constructor_type) == UNION_TYPE)
72749341 9652 constructor_unfilled_fields = NULL_TREE;
fc698c9d 9653
276beea2 9654 /* Now output any pending elements which have become next. */
9655 if (pending)
759791ee 9656 output_pending_init_elements (0, braced_init_obstack);
276beea2 9657}
989bbcfc 9658
c1e4a7a5 9659/* For two FIELD_DECLs in the same chain, return -1 if field1
9660 comes before field2, 1 if field1 comes after field2 and
9661 0 if field1 == field2. */
9662
9663static int
9664init_field_decl_cmp (tree field1, tree field2)
9665{
9666 if (field1 == field2)
9667 return 0;
9668
9669 tree bitpos1 = bit_position (field1);
9670 tree bitpos2 = bit_position (field2);
9671 if (tree_int_cst_equal (bitpos1, bitpos2))
9672 {
9673 /* If one of the fields has non-zero bitsize, then that
9674 field must be the last one in a sequence of zero
9675 sized fields, fields after it will have bigger
9676 bit_position. */
9677 if (TREE_TYPE (field1) != error_mark_node
9678 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9679 && integer_nonzerop (TREE_TYPE (field1)))
9680 return 1;
9681 if (TREE_TYPE (field2) != error_mark_node
9682 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9683 && integer_nonzerop (TREE_TYPE (field2)))
9684 return -1;
9685 /* Otherwise, fallback to DECL_CHAIN walk to find out
9686 which field comes earlier. Walk chains of both
9687 fields, so that if field1 and field2 are close to each
9688 other in either order, it is found soon even for large
9689 sequences of zero sized fields. */
9690 tree f1 = field1, f2 = field2;
9691 while (1)
9692 {
9693 f1 = DECL_CHAIN (f1);
9694 f2 = DECL_CHAIN (f2);
9695 if (f1 == NULL_TREE)
9696 {
9697 gcc_assert (f2);
9698 return 1;
9699 }
9700 if (f2 == NULL_TREE)
9701 return -1;
9702 if (f1 == field2)
9703 return -1;
9704 if (f2 == field1)
9705 return 1;
9706 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9707 return 1;
9708 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9709 return -1;
9710 }
9711 }
9712 else if (tree_int_cst_lt (bitpos1, bitpos2))
9713 return -1;
9714 else
9715 return 1;
9716}
9717
276beea2 9718/* Output any pending elements which have become next.
9719 As we output elements, constructor_unfilled_{fields,index}
9720 advances, which may cause other elements to become next;
9721 if so, they too are output.
989bbcfc 9722
276beea2 9723 If ALL is 0, we return when there are
9724 no more pending elements to output now.
5d844ba2 9725
276beea2 9726 If ALL is 1, we output space as necessary so that
9727 we can output all the pending elements. */
276beea2 9728static void
759791ee 9729output_pending_init_elements (int all, struct obstack * braced_init_obstack)
276beea2 9730{
9731 struct init_node *elt = constructor_pending_elts;
9732 tree next;
fc698c9d 9733
276beea2 9734 retry:
9735
c7bf1374 9736 /* Look through the whole pending tree.
276beea2 9737 If we find an element that should be output now,
9738 output it. Otherwise, set NEXT to the element
9739 that comes first among those still pending. */
9740
72749341 9741 next = NULL_TREE;
276beea2 9742 while (elt)
9743 {
9744 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
989bbcfc 9745 {
276beea2 9746 if (tree_int_cst_equal (elt->purpose,
9747 constructor_unfilled_index))
adc93f31 9748 output_init_element (input_location, elt->value, elt->origtype,
9749 true, TREE_TYPE (constructor_type),
623ee358 9750 constructor_unfilled_index, false, false,
759791ee 9751 braced_init_obstack);
276beea2 9752 else if (tree_int_cst_lt (constructor_unfilled_index,
9753 elt->purpose))
989bbcfc 9754 {
276beea2 9755 /* Advance to the next smaller node. */
9756 if (elt->left)
9757 elt = elt->left;
9758 else
9759 {
9760 /* We have reached the smallest node bigger than the
9761 current unfilled index. Fill the space first. */
9762 next = elt->purpose;
9763 break;
9764 }
989bbcfc 9765 }
b10b344f 9766 else
9767 {
276beea2 9768 /* Advance to the next bigger node. */
9769 if (elt->right)
9770 elt = elt->right;
9771 else
b10b344f 9772 {
276beea2 9773 /* We have reached the biggest node in a subtree. Find
9774 the parent of it, which is the next bigger node. */
9775 while (elt->parent && elt->parent->right == elt)
9776 elt = elt->parent;
9777 elt = elt->parent;
9778 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9779 elt->purpose))
9780 {
9781 next = elt->purpose;
9782 break;
9783 }
b10b344f 9784 }
9785 }
989bbcfc 9786 }
ef61516b 9787 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
276beea2 9788 {
276beea2 9789 /* If the current record is complete we are done. */
72749341 9790 if (constructor_unfilled_fields == NULL_TREE)
276beea2 9791 break;
fc698c9d 9792
c1e4a7a5 9793 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9794 elt->purpose);
9795 if (cmp == 0)
9796 output_init_element (input_location, elt->value, elt->origtype,
9797 true, TREE_TYPE (elt->purpose),
9798 elt->purpose, false, false,
9799 braced_init_obstack);
9800 else if (cmp < 0)
276beea2 9801 {
9802 /* Advance to the next smaller node. */
9803 if (elt->left)
9804 elt = elt->left;
9805 else
9806 {
9807 /* We have reached the smallest node bigger than the
9808 current unfilled field. Fill the space first. */
9809 next = elt->purpose;
9810 break;
9811 }
9812 }
9813 else
9814 {
9815 /* Advance to the next bigger node. */
9816 if (elt->right)
9817 elt = elt->right;
9818 else
9819 {
9820 /* We have reached the biggest node in a subtree. Find
9821 the parent of it, which is the next bigger node. */
9822 while (elt->parent && elt->parent->right == elt)
9823 elt = elt->parent;
9824 elt = elt->parent;
9825 if (elt
c1e4a7a5 9826 && init_field_decl_cmp (constructor_unfilled_fields,
9827 elt->purpose) < 0)
276beea2 9828 {
9829 next = elt->purpose;
9830 break;
9831 }
9832 }
9833 }
9834 }
9835 }
fc698c9d 9836
276beea2 9837 /* Ordinarily return, but not if we want to output all
9838 and there are elements left. */
72749341 9839 if (!(all && next != NULL_TREE))
c2e053d8 9840 return;
9841
276beea2 9842 /* If it's not incremental, just skip over the gap, so that after
9843 jumping to retry we will output the next successive element. */
ef61516b 9844 if (RECORD_OR_UNION_TYPE_P (constructor_type))
276beea2 9845 constructor_unfilled_fields = next;
9846 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9847 constructor_unfilled_index = next;
fc698c9d 9848
276beea2 9849 /* ELT now points to the node in the pending tree with the next
9850 initializer to output. */
9851 goto retry;
fc698c9d 9852}
9853\f
276beea2 9854/* Add one non-braced element to the current constructor level.
9855 This adjusts the current position within the constructor's type.
9856 This may also start or terminate implicit levels
9857 to handle a partly-braced initializer.
997d68fe 9858
276beea2 9859 Once this has found the correct level for the new element,
d241bf0d 9860 it calls output_init_element.
9861
9862 IMPLICIT is true if value comes from pop_init_level (1),
9863 the new initializer has been merged with the existing one
9864 and thus no warnings should be emitted about overriding an
9865 existing initializer. */
276beea2 9866
9867void
adc93f31 9868process_init_element (location_t loc, struct c_expr value, bool implicit,
759791ee 9869 struct obstack * braced_init_obstack)
997d68fe 9870{
3ad5f027 9871 tree orig_value = value.value;
72749341 9872 int string_flag
9873 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
3ad5f027 9874 bool strict_string = value.original_code == STRING_CST;
f221adca 9875 bool was_designated = designator_depth != 0;
997d68fe 9876
276beea2 9877 designator_depth = 0;
ebe39827 9878 designator_erroneous = 0;
997d68fe 9879
95cdf3fd 9880 if (!implicit && value.value && !integer_zerop (value.value))
9881 constructor_zeroinit = 0;
9882
276beea2 9883 /* Handle superfluous braces around string cst as in
9884 char x[] = {"foo"}; */
9885 if (string_flag
9886 && constructor_type
f221adca 9887 && !was_designated
276beea2 9888 && TREE_CODE (constructor_type) == ARRAY_TYPE
e91a0d39 9889 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
276beea2 9890 && integer_zerop (constructor_unfilled_index))
997d68fe 9891 {
3ad5f027 9892 if (constructor_stack->replacement_value.value)
53de1631 9893 error_init (loc, "excess elements in char array initializer");
276beea2 9894 constructor_stack->replacement_value = value;
9895 return;
997d68fe 9896 }
989bbcfc 9897
72749341 9898 if (constructor_stack->replacement_value.value != NULL_TREE)
276beea2 9899 {
53de1631 9900 error_init (loc, "excess elements in struct initializer");
276beea2 9901 return;
997d68fe 9902 }
9903
276beea2 9904 /* Ignore elements of a brace group if it is entirely superfluous
9905 and has already been diagnosed. */
72749341 9906 if (constructor_type == NULL_TREE)
276beea2 9907 return;
997d68fe 9908
74691f46 9909 if (!implicit && warn_designated_init && !was_designated
9910 && TREE_CODE (constructor_type) == RECORD_TYPE
9911 && lookup_attribute ("designated_init",
9912 TYPE_ATTRIBUTES (constructor_type)))
9913 warning_init (loc,
9914 OPT_Wdesignated_init,
9915 "positional initialization of field "
9916 "in %<struct%> declared with %<designated_init%> attribute");
9917
276beea2 9918 /* If we've exhausted any levels that didn't have braces,
9919 pop them now. */
9920 while (constructor_stack->implicit)
9921 {
ef61516b 9922 if (RECORD_OR_UNION_TYPE_P (constructor_type)
72749341 9923 && constructor_fields == NULL_TREE)
53de1631 9924 process_init_element (loc,
8ccda24a 9925 pop_init_level (loc, 1, braced_init_obstack,
9926 last_init_list_comma),
759791ee 9927 true, braced_init_obstack);
415528ef 9928 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
6290f0db 9929 || VECTOR_TYPE_P (constructor_type))
54550ecb 9930 && constructor_max_index
9931 && tree_int_cst_lt (constructor_max_index,
9932 constructor_index))
53de1631 9933 process_init_element (loc,
8ccda24a 9934 pop_init_level (loc, 1, braced_init_obstack,
9935 last_init_list_comma),
759791ee 9936 true, braced_init_obstack);
276beea2 9937 else
9938 break;
9939 }
997d68fe 9940
276beea2 9941 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9942 if (constructor_range_stack)
997d68fe 9943 {
276beea2 9944 /* If value is a compound literal and we'll be just using its
9945 content, don't put it into a SAVE_EXPR. */
3ad5f027 9946 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
46861609 9947 || !require_constant_value)
c6418a4e 9948 {
9949 tree semantic_type = NULL_TREE;
9950 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9951 {
9952 semantic_type = TREE_TYPE (value.value);
9953 value.value = TREE_OPERAND (value.value, 0);
9954 }
d0869ea4 9955 value.value = save_expr (value.value);
c6418a4e 9956 if (semantic_type)
9957 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9958 value.value);
9959 }
276beea2 9960 }
997d68fe 9961
276beea2 9962 while (1)
9963 {
9964 if (TREE_CODE (constructor_type) == RECORD_TYPE)
997d68fe 9965 {
276beea2 9966 tree fieldtype;
9967 enum tree_code fieldcode;
997d68fe 9968
72749341 9969 if (constructor_fields == NULL_TREE)
276beea2 9970 {
53de1631 9971 pedwarn_init (loc, 0, "excess elements in struct initializer");
276beea2 9972 break;
9973 }
997d68fe 9974
276beea2 9975 fieldtype = TREE_TYPE (constructor_fields);
9976 if (fieldtype != error_mark_node)
9977 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9978 fieldcode = TREE_CODE (fieldtype);
997d68fe 9979
276beea2 9980 /* Error for non-static initialization of a flexible array member. */
9981 if (fieldcode == ARRAY_TYPE
9982 && !require_constant_value
9983 && TYPE_SIZE (fieldtype) == NULL_TREE
d059ef79 9984 && DECL_CHAIN (constructor_fields) == NULL_TREE)
276beea2 9985 {
53de1631 9986 error_init (loc, "non-static initialization of a flexible "
9987 "array member");
276beea2 9988 break;
9989 }
997d68fe 9990
15e18a8c 9991 /* Error for initialization of a flexible array member with
9992 a string constant if the structure is in an array. E.g.:
9993 struct S { int x; char y[]; };
9994 struct S s[] = { { 1, "foo" } };
9995 is invalid. */
9996 if (string_flag
9997 && fieldcode == ARRAY_TYPE
9998 && constructor_depth > 1
9999 && TYPE_SIZE (fieldtype) == NULL_TREE
10000 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10001 {
10002 bool in_array_p = false;
10003 for (struct constructor_stack *p = constructor_stack;
10004 p && p->type; p = p->next)
10005 if (TREE_CODE (p->type) == ARRAY_TYPE)
10006 {
10007 in_array_p = true;
10008 break;
10009 }
10010 if (in_array_p)
10011 {
10012 error_init (loc, "initialization of flexible array "
10013 "member in a nested context");
10014 break;
10015 }
10016 }
10017
276beea2 10018 /* Accept a string constant to initialize a subarray. */
72749341 10019 if (value.value != NULL_TREE
276beea2 10020 && fieldcode == ARRAY_TYPE
e91a0d39 10021 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
276beea2 10022 && string_flag)
3ad5f027 10023 value.value = orig_value;
276beea2 10024 /* Otherwise, if we have come to a subaggregate,
10025 and we don't have an element of its type, push into it. */
72749341 10026 else if (value.value != NULL_TREE
3ad5f027 10027 && value.value != error_mark_node
10028 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
276beea2 10029 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
415528ef 10030 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
276beea2 10031 {
53de1631 10032 push_init_level (loc, 1, braced_init_obstack);
276beea2 10033 continue;
10034 }
997d68fe 10035
3ad5f027 10036 if (value.value)
276beea2 10037 {
10038 push_member_name (constructor_fields);
adc93f31 10039 output_init_element (loc, value.value, value.original_type,
b9c74b4d 10040 strict_string, fieldtype,
623ee358 10041 constructor_fields, true, implicit,
759791ee 10042 braced_init_obstack);
276beea2 10043 RESTORE_SPELLING_DEPTH (constructor_depth);
997d68fe 10044 }
10045 else
276beea2 10046 /* Do the bookkeeping for an element that was
10047 directly output as a constructor. */
997d68fe 10048 {
276beea2 10049 /* For a record, keep track of end position of last field. */
10050 if (DECL_SIZE (constructor_fields))
a0c938f0 10051 constructor_bit_index
389dd41b 10052 = size_binop_loc (input_location, PLUS_EXPR,
10053 bit_position (constructor_fields),
10054 DECL_SIZE (constructor_fields));
276beea2 10055
10056 /* If the current field was the first one not yet written out,
10057 it isn't now, so update. */
10058 if (constructor_unfilled_fields == constructor_fields)
10059 {
1767a056 10060 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
276beea2 10061 /* Skip any nameless bit fields. */
10062 while (constructor_unfilled_fields != 0
3cde3c29 10063 && (DECL_UNNAMED_BIT_FIELD
10064 (constructor_unfilled_fields)))
276beea2 10065 constructor_unfilled_fields =
1767a056 10066 DECL_CHAIN (constructor_unfilled_fields);
276beea2 10067 }
997d68fe 10068 }
276beea2 10069
1767a056 10070 constructor_fields = DECL_CHAIN (constructor_fields);
276beea2 10071 /* Skip any nameless bit fields at the beginning. */
72749341 10072 while (constructor_fields != NULL_TREE
3cde3c29 10073 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
1767a056 10074 constructor_fields = DECL_CHAIN (constructor_fields);
997d68fe 10075 }
276beea2 10076 else if (TREE_CODE (constructor_type) == UNION_TYPE)
997d68fe 10077 {
276beea2 10078 tree fieldtype;
10079 enum tree_code fieldcode;
997d68fe 10080
72749341 10081 if (constructor_fields == NULL_TREE)
276beea2 10082 {
c9e51d25 10083 pedwarn_init (loc, 0,
21ca8540 10084 "excess elements in union initializer");
276beea2 10085 break;
10086 }
997d68fe 10087
276beea2 10088 fieldtype = TREE_TYPE (constructor_fields);
10089 if (fieldtype != error_mark_node)
10090 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10091 fieldcode = TREE_CODE (fieldtype);
997d68fe 10092
276beea2 10093 /* Warn that traditional C rejects initialization of unions.
10094 We skip the warning if the value is zero. This is done
10095 under the assumption that the zero initializer in user
10096 code appears conditioned on e.g. __STDC__ to avoid
10097 "missing initializer" warnings and relies on default
10098 initialization to zero in the traditional C case.
10099 We also skip the warning if the initializer is designated,
10100 again on the assumption that this must be conditional on
10101 __STDC__ anyway (and we've already complained about the
10102 member-designator already). */
3df42822 10103 if (!in_system_header_at (input_location) && !constructor_designated
3ad5f027 10104 && !(value.value && (integer_zerop (value.value)
10105 || real_zerop (value.value))))
6bf97f82 10106 warning (OPT_Wtraditional, "traditional C rejects initialization "
10107 "of unions");
997d68fe 10108
276beea2 10109 /* Accept a string constant to initialize a subarray. */
72749341 10110 if (value.value != NULL_TREE
276beea2 10111 && fieldcode == ARRAY_TYPE
e91a0d39 10112 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
276beea2 10113 && string_flag)
3ad5f027 10114 value.value = orig_value;
276beea2 10115 /* Otherwise, if we have come to a subaggregate,
10116 and we don't have an element of its type, push into it. */
72749341 10117 else if (value.value != NULL_TREE
3ad5f027 10118 && value.value != error_mark_node
10119 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
276beea2 10120 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
415528ef 10121 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
276beea2 10122 {
53de1631 10123 push_init_level (loc, 1, braced_init_obstack);
276beea2 10124 continue;
10125 }
997d68fe 10126
3ad5f027 10127 if (value.value)
276beea2 10128 {
10129 push_member_name (constructor_fields);
adc93f31 10130 output_init_element (loc, value.value, value.original_type,
b9c74b4d 10131 strict_string, fieldtype,
623ee358 10132 constructor_fields, true, implicit,
759791ee 10133 braced_init_obstack);
276beea2 10134 RESTORE_SPELLING_DEPTH (constructor_depth);
997d68fe 10135 }
10136 else
276beea2 10137 /* Do the bookkeeping for an element that was
10138 directly output as a constructor. */
997d68fe 10139 {
276beea2 10140 constructor_bit_index = DECL_SIZE (constructor_fields);
d059ef79 10141 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
997d68fe 10142 }
997d68fe 10143
72749341 10144 constructor_fields = NULL_TREE;
276beea2 10145 }
10146 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10147 {
10148 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10149 enum tree_code eltcode = TREE_CODE (elttype);
997d68fe 10150
276beea2 10151 /* Accept a string constant to initialize a subarray. */
72749341 10152 if (value.value != NULL_TREE
276beea2 10153 && eltcode == ARRAY_TYPE
e91a0d39 10154 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
276beea2 10155 && string_flag)
3ad5f027 10156 value.value = orig_value;
276beea2 10157 /* Otherwise, if we have come to a subaggregate,
10158 and we don't have an element of its type, push into it. */
72749341 10159 else if (value.value != NULL_TREE
3ad5f027 10160 && value.value != error_mark_node
10161 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
276beea2 10162 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
415528ef 10163 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
276beea2 10164 {
53de1631 10165 push_init_level (loc, 1, braced_init_obstack);
276beea2 10166 continue;
10167 }
989bbcfc 10168
72749341 10169 if (constructor_max_index != NULL_TREE
276beea2 10170 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10171 || integer_all_onesp (constructor_max_index)))
10172 {
c9e51d25 10173 pedwarn_init (loc, 0,
21ca8540 10174 "excess elements in array initializer");
276beea2 10175 break;
10176 }
989bbcfc 10177
276beea2 10178 /* Now output the actual element. */
3ad5f027 10179 if (value.value)
276beea2 10180 {
e913b5cd 10181 push_array_bounds (tree_to_uhwi (constructor_index));
adc93f31 10182 output_init_element (loc, value.value, value.original_type,
b9c74b4d 10183 strict_string, elttype,
623ee358 10184 constructor_index, true, implicit,
759791ee 10185 braced_init_obstack);
276beea2 10186 RESTORE_SPELLING_DEPTH (constructor_depth);
10187 }
2b30d46c 10188
276beea2 10189 constructor_index
389dd41b 10190 = size_binop_loc (input_location, PLUS_EXPR,
10191 constructor_index, bitsize_one_node);
989bbcfc 10192
3ad5f027 10193 if (!value.value)
276beea2 10194 /* If we are doing the bookkeeping for an element that was
10195 directly output as a constructor, we must update
10196 constructor_unfilled_index. */
10197 constructor_unfilled_index = constructor_index;
10198 }
6290f0db 10199 else if (VECTOR_TYPE_P (constructor_type))
276beea2 10200 {
10201 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
989bbcfc 10202
a0c938f0 10203 /* Do a basic check of initializer size. Note that vectors
10204 always have a fixed size derived from their type. */
276beea2 10205 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10206 {
c9e51d25 10207 pedwarn_init (loc, 0,
21ca8540 10208 "excess elements in vector initializer");
276beea2 10209 break;
10210 }
989bbcfc 10211
276beea2 10212 /* Now output the actual element. */
3ad5f027 10213 if (value.value)
415528ef 10214 {
10215 if (TREE_CODE (value.value) == VECTOR_CST)
10216 elttype = TYPE_MAIN_VARIANT (constructor_type);
adc93f31 10217 output_init_element (loc, value.value, value.original_type,
415528ef 10218 strict_string, elttype,
623ee358 10219 constructor_index, true, implicit,
759791ee 10220 braced_init_obstack);
415528ef 10221 }
989bbcfc 10222
276beea2 10223 constructor_index
389dd41b 10224 = size_binop_loc (input_location,
10225 PLUS_EXPR, constructor_index, bitsize_one_node);
989bbcfc 10226
3ad5f027 10227 if (!value.value)
276beea2 10228 /* If we are doing the bookkeeping for an element that was
10229 directly output as a constructor, we must update
10230 constructor_unfilled_index. */
10231 constructor_unfilled_index = constructor_index;
10232 }
989bbcfc 10233
276beea2 10234 /* Handle the sole element allowed in a braced initializer
10235 for a scalar variable. */
b765c734 10236 else if (constructor_type != error_mark_node
72749341 10237 && constructor_fields == NULL_TREE)
989bbcfc 10238 {
c9e51d25 10239 pedwarn_init (loc, 0,
21ca8540 10240 "excess elements in scalar initializer");
276beea2 10241 break;
989bbcfc 10242 }
10243 else
10244 {
3ad5f027 10245 if (value.value)
adc93f31 10246 output_init_element (loc, value.value, value.original_type,
b9c74b4d 10247 strict_string, constructor_type,
623ee358 10248 NULL_TREE, true, implicit,
759791ee 10249 braced_init_obstack);
72749341 10250 constructor_fields = NULL_TREE;
989bbcfc 10251 }
10252
276beea2 10253 /* Handle range initializers either at this level or anywhere higher
10254 in the designator stack. */
10255 if (constructor_range_stack)
989bbcfc 10256 {
276beea2 10257 struct constructor_range_stack *p, *range_stack;
10258 int finish = 0;
10259
10260 range_stack = constructor_range_stack;
10261 constructor_range_stack = 0;
10262 while (constructor_stack != range_stack->stack)
989bbcfc 10263 {
231bd014 10264 gcc_assert (constructor_stack->implicit);
adc93f31 10265 process_init_element (loc,
53de1631 10266 pop_init_level (loc, 1,
8ccda24a 10267 braced_init_obstack,
10268 last_init_list_comma),
759791ee 10269 true, braced_init_obstack);
989bbcfc 10270 }
276beea2 10271 for (p = range_stack;
10272 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10273 p = p->prev)
989bbcfc 10274 {
231bd014 10275 gcc_assert (constructor_stack->implicit);
adc93f31 10276 process_init_element (loc,
53de1631 10277 pop_init_level (loc, 1,
8ccda24a 10278 braced_init_obstack,
10279 last_init_list_comma),
759791ee 10280 true, braced_init_obstack);
989bbcfc 10281 }
276beea2 10282
389dd41b 10283 p->index = size_binop_loc (input_location,
10284 PLUS_EXPR, p->index, bitsize_one_node);
276beea2 10285 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10286 finish = 1;
10287
10288 while (1)
10289 {
10290 constructor_index = p->index;
10291 constructor_fields = p->fields;
10292 if (finish && p->range_end && p->index == p->range_start)
10293 {
10294 finish = 0;
10295 p->prev = 0;
10296 }
10297 p = p->next;
10298 if (!p)
10299 break;
74a4140f 10300 finish_implicit_inits (loc, braced_init_obstack);
53de1631 10301 push_init_level (loc, 2, braced_init_obstack);
276beea2 10302 p->stack = constructor_stack;
10303 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10304 p->index = p->range_start;
10305 }
10306
10307 if (!finish)
10308 constructor_range_stack = range_stack;
10309 continue;
989bbcfc 10310 }
10311
276beea2 10312 break;
989bbcfc 10313 }
10314
276beea2 10315 constructor_range_stack = 0;
10316}
10317\f
fae5a040 10318/* Build a complete asm-statement, whose components are a CV_QUALIFIER
10319 (guaranteed to be 'volatile' or null) and ARGS (represented using
226c2452 10320 an ASM_EXPR node). */
276beea2 10321tree
d2a2e292 10322build_asm_stmt (bool is_volatile, tree args)
276beea2 10323{
d2a2e292 10324 if (is_volatile)
4ee9c684 10325 ASM_VOLATILE_P (args) = 1;
fae5a040 10326 return add_stmt (args);
989bbcfc 10327}
10328
fae5a040 10329/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10330 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10331 SIMPLE indicates whether there was anything at all after the
10332 string in the asm expression -- asm("blah") and asm("blah" : )
6de46ad5 10333 are subtly different. We use a ASM_EXPR node to represent this.
10334 LOC is the location of the asm, and IS_INLINE says whether this
10335 is asm inline. */
276beea2 10336tree
e60a6f7b 10337build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
6de46ad5 10338 tree clobbers, tree labels, bool simple, bool is_inline)
997d68fe 10339{
276beea2 10340 tree tail;
fae5a040 10341 tree args;
4ee9c684 10342 int i;
10343 const char *constraint;
8b0d0af6 10344 const char **oconstraints;
4ee9c684 10345 bool allows_mem, allows_reg, is_inout;
8b0d0af6 10346 int ninputs, noutputs;
4ee9c684 10347
10348 ninputs = list_length (inputs);
10349 noutputs = list_length (outputs);
8b0d0af6 10350 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10351
78f55ca8 10352 string = resolve_asm_operand_names (string, outputs, inputs, labels);
276beea2 10353
4ee9c684 10354 /* Remove output conversions that change the type but not the mode. */
10355 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
997d68fe 10356 {
276beea2 10357 tree output = TREE_VALUE (tail);
8b0d0af6 10358
69cd03b2 10359 output = c_fully_fold (output, false, NULL, true);
035b65e2 10360
8b0d0af6 10361 /* ??? Really, this should not be here. Users should be using a
10362 proper lvalue, dammit. But there's a long history of using casts
10363 in the output operands. In cases like longlong.h, this becomes a
10364 primitive form of typechecking -- if the cast can be removed, then
10365 the output operand had a type of the proper width; otherwise we'll
10366 get an error. Gross, but ... */
276beea2 10367 STRIP_NOPS (output);
8b0d0af6 10368
fdd84b77 10369 if (!lvalue_or_else (loc, output, lv_asm))
8b0d0af6 10370 output = error_mark_node;
989bbcfc 10371
89552023 10372 if (output != error_mark_node
10373 && (TREE_READONLY (output)
10374 || TYPE_READONLY (TREE_TYPE (output))
ef61516b 10375 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
89552023 10376 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
f2697631 10377 readonly_error (loc, output, lv_asm);
89552023 10378
4ee9c684 10379 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8b0d0af6 10380 oconstraints[i] = constraint;
10381
10382 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10383 &allows_mem, &allows_reg, &is_inout))
10384 {
10385 /* If the operand is going to end up in memory,
10386 mark it addressable. */
10387 if (!allows_reg && !c_mark_addressable (output))
10388 output = error_mark_node;
ef31075b 10389 if (!(!allows_reg && allows_mem)
10390 && output != error_mark_node
10391 && VOID_TYPE_P (TREE_TYPE (output)))
10392 {
10393 error_at (loc, "invalid use of void expression");
10394 output = error_mark_node;
10395 }
8b0d0af6 10396 }
10397 else
a0c938f0 10398 output = error_mark_node;
276beea2 10399
8b0d0af6 10400 TREE_VALUE (tail) = output;
989bbcfc 10401 }
276beea2 10402
8b0d0af6 10403 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10404 {
10405 tree input;
10406
10407 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10408 input = TREE_VALUE (tail);
10409
8b0d0af6 10410 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10411 oconstraints, &allows_mem, &allows_reg))
10412 {
10413 /* If the operand is going to end up in memory,
10414 mark it addressable. */
c6e95001 10415 if (!allows_reg && allows_mem)
10416 {
69cd03b2 10417 input = c_fully_fold (input, false, NULL, true);
035b65e2 10418
c6e95001 10419 /* Strip the nops as we allow this case. FIXME, this really
10420 should be rejected or made deprecated. */
10421 STRIP_NOPS (input);
10422 if (!c_mark_addressable (input))
10423 input = error_mark_node;
ef31075b 10424 }
035b65e2 10425 else
ef31075b 10426 {
035b65e2 10427 struct c_expr expr;
10428 memset (&expr, 0, sizeof (expr));
10429 expr.value = input;
b560fabd 10430 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
035b65e2 10431 input = c_fully_fold (expr.value, false, NULL);
10432
10433 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10434 {
10435 error_at (loc, "invalid use of void expression");
10436 input = error_mark_node;
10437 }
ef31075b 10438 }
8b0d0af6 10439 }
10440 else
10441 input = error_mark_node;
10442
10443 TREE_VALUE (tail) = input;
10444 }
276beea2 10445
78f55ca8 10446 /* ASMs with labels cannot have outputs. This should have been
10447 enforced by the parser. */
10448 gcc_assert (outputs == NULL || labels == NULL);
10449
10450 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
fae5a040 10451
89552023 10452 /* asm statements without outputs, including simple ones, are treated
10453 as volatile. */
10454 ASM_INPUT_P (args) = simple;
10455 ASM_VOLATILE_P (args) = (noutputs == 0);
6de46ad5 10456 ASM_INLINE_P (args) = is_inline;
8b0d0af6 10457
fae5a040 10458 return args;
997d68fe 10459}
276beea2 10460\f
e60a6f7b 10461/* Generate a goto statement to LABEL. LOC is the location of the
10462 GOTO. */
0375a275 10463
10464tree
e60a6f7b 10465c_finish_goto_label (location_t loc, tree label)
0375a275 10466{
f805d53d 10467 tree decl = lookup_label_for_goto (loc, label);
0375a275 10468 if (!decl)
10469 return NULL_TREE;
0375a275 10470 TREE_USED (decl) = 1;
e60a6f7b 10471 {
38ef3642 10472 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
e60a6f7b 10473 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10474 SET_EXPR_LOCATION (t, loc);
10475 return add_stmt (t);
10476 }
0375a275 10477}
10478
e60a6f7b 10479/* Generate a computed goto statement to EXPR. LOC is the location of
10480 the GOTO. */
0375a275 10481
10482tree
e60a6f7b 10483c_finish_goto_ptr (location_t loc, tree expr)
0375a275 10484{
e60a6f7b 10485 tree t;
29438999 10486 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
a75b1c71 10487 expr = c_fully_fold (expr, false, NULL);
0375a275 10488 expr = convert (ptr_type_node, expr);
e60a6f7b 10489 t = build1 (GOTO_EXPR, void_type_node, expr);
10490 SET_EXPR_LOCATION (t, loc);
10491 return add_stmt (t);
0375a275 10492}
10493
c857cd60 10494/* Generate a C `return' statement. RETVAL is the expression for what
e60a6f7b 10495 to return, or a null pointer for `return;' with no value. LOC is
430b73ee 10496 the location of the return statement, or the location of the expression,
10497 if the statement has any. If ORIGTYPE is not NULL_TREE, it
e60a6f7b 10498 is the original type of RETVAL. */
fc698c9d 10499
0375a275 10500tree
e60a6f7b 10501c_finish_return (location_t loc, tree retval, tree origtype)
276beea2 10502{
81a3c55b 10503 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10504 bool no_warning = false;
a75b1c71 10505 bool npc = false;
276beea2 10506
ab4c803c 10507 /* Use the expansion point to handle cases such as returning NULL
10508 in a function returning void. */
be1e7283 10509 location_t xloc = expansion_point_location_if_in_system_header (loc);
ab4c803c 10510
276beea2 10511 if (TREE_THIS_VOLATILE (current_function_decl))
ab4c803c 10512 warning_at (xloc, 0,
e60a6f7b 10513 "function declared %<noreturn%> has a %<return%> statement");
276beea2 10514
a75b1c71 10515 if (retval)
10516 {
c6418a4e 10517 tree semantic_type = NULL_TREE;
a75b1c71 10518 npc = null_pointer_constant_p (retval);
c6418a4e 10519 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10520 {
10521 semantic_type = TREE_TYPE (retval);
10522 retval = TREE_OPERAND (retval, 0);
10523 }
a75b1c71 10524 retval = c_fully_fold (retval, false, NULL);
c6418a4e 10525 if (semantic_type)
10526 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
a75b1c71 10527 }
10528
276beea2 10529 if (!retval)
fc698c9d 10530 {
276beea2 10531 current_function_returns_null = 1;
768cc20e 10532 if ((warn_return_type >= 0 || flag_isoc99)
72749341 10533 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
81a3c55b 10534 {
b9f9586a 10535 bool warned_here;
508ea33a 10536 if (flag_isoc99)
b9f9586a 10537 warned_here = pedwarn
768cc20e 10538 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
b9f9586a 10539 "%<return%> with no value, in function returning non-void");
508ea33a 10540 else
b9f9586a 10541 warned_here = warning_at
10542 (loc, OPT_Wreturn_type,
10543 "%<return%> with no value, in function returning non-void");
81a3c55b 10544 no_warning = true;
b9f9586a 10545 if (warned_here)
10546 inform (DECL_SOURCE_LOCATION (current_function_decl),
10547 "declared here");
81a3c55b 10548 }
628fa4f8 10549 }
72749341 10550 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
fc698c9d 10551 {
276beea2 10552 current_function_returns_null = 1;
b9f9586a 10553 bool warned_here;
6e4e3580 10554 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b9f9586a 10555 warned_here = pedwarn
768cc20e 10556 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
b9f9586a 10557 "%<return%> with a value, in function returning void");
48e1416a 10558 else
b9f9586a 10559 warned_here = pedwarn
10560 (xloc, OPT_Wpedantic, "ISO C forbids "
10561 "%<return%> with expression, in function returning void");
10562 if (warned_here)
10563 inform (DECL_SOURCE_LOCATION (current_function_decl),
10564 "declared here");
fc698c9d 10565 }
276beea2 10566 else
fc698c9d 10567 {
22a75734 10568 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10569 retval, origtype, ic_return,
e60a6f7b 10570 npc, NULL_TREE, NULL_TREE, 0);
276beea2 10571 tree res = DECL_RESULT (current_function_decl);
10572 tree inner;
c708c6ef 10573 bool save;
276beea2 10574
10575 current_function_returns_value = 1;
10576 if (t == error_mark_node)
0375a275 10577 return NULL_TREE;
276beea2 10578
c708c6ef 10579 save = in_late_binary_op;
10580 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
823a9dd4 10581 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10582 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10583 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10584 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9917317a 10585 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
c708c6ef 10586 in_late_binary_op = true;
276beea2 10587 inner = t = convert (TREE_TYPE (res), t);
c708c6ef 10588 in_late_binary_op = save;
276beea2 10589
10590 /* Strip any conversions, additions, and subtractions, and see if
10591 we are returning the address of a local variable. Warn if so. */
10592 while (1)
989bbcfc 10593 {
276beea2 10594 switch (TREE_CODE (inner))
989bbcfc 10595 {
e7e7c0fe 10596 CASE_CONVERT:
10597 case NON_LVALUE_EXPR:
276beea2 10598 case PLUS_EXPR:
e7e7c0fe 10599 case POINTER_PLUS_EXPR:
276beea2 10600 inner = TREE_OPERAND (inner, 0);
10601 continue;
10602
10603 case MINUS_EXPR:
10604 /* If the second operand of the MINUS_EXPR has a pointer
10605 type (or is converted from it), this may be valid, so
10606 don't give a warning. */
10607 {
10608 tree op1 = TREE_OPERAND (inner, 1);
989bbcfc 10609
84166705 10610 while (!POINTER_TYPE_P (TREE_TYPE (op1))
72dd6141 10611 && (CONVERT_EXPR_P (op1)
10612 || TREE_CODE (op1) == NON_LVALUE_EXPR))
276beea2 10613 op1 = TREE_OPERAND (op1, 0);
989bbcfc 10614
276beea2 10615 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10616 break;
989bbcfc 10617
276beea2 10618 inner = TREE_OPERAND (inner, 0);
10619 continue;
10620 }
628fa4f8 10621
276beea2 10622 case ADDR_EXPR:
10623 inner = TREE_OPERAND (inner, 0);
8ee1039f 10624
ce45a448 10625 while (REFERENCE_CLASS_P (inner)
aa3e402e 10626 && !INDIRECT_REF_P (inner))
276beea2 10627 inner = TREE_OPERAND (inner, 0);
989bbcfc 10628
aaadf915 10629 if (DECL_P (inner)
84166705 10630 && !DECL_EXTERNAL (inner)
10631 && !TREE_STATIC (inner)
276beea2 10632 && DECL_CONTEXT (inner) == current_function_decl)
d44f2f7c 10633 {
10634 if (TREE_CODE (inner) == LABEL_DECL)
10635 warning_at (loc, OPT_Wreturn_local_addr,
10636 "function returns address of label");
10637 else
f22a2cb7 10638 {
10639 warning_at (loc, OPT_Wreturn_local_addr,
10640 "function returns address of local variable");
10641 tree zero = build_zero_cst (TREE_TYPE (res));
10642 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10643 }
d44f2f7c 10644 }
276beea2 10645 break;
989bbcfc 10646
276beea2 10647 default:
10648 break;
10649 }
fc698c9d 10650
276beea2 10651 break;
10652 }
10653
14ae0310 10654 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
e60a6f7b 10655 SET_EXPR_LOCATION (retval, loc);
6513b50d 10656
10657 if (warn_sequence_point)
10658 verify_sequence_points (retval);
fc698c9d 10659 }
989bbcfc 10660
e60a6f7b 10661 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
81a3c55b 10662 TREE_NO_WARNING (ret_stmt) |= no_warning;
10663 return add_stmt (ret_stmt);
fc698c9d 10664}
276beea2 10665\f
10666struct c_switch {
197638d4 10667 /* The SWITCH_EXPR being built. */
10668 tree switch_expr;
2ca392fd 10669
5c9dae64 10670 /* The original type of the testing expression, i.e. before the
2ca392fd 10671 default conversion is applied. */
10672 tree orig_type;
10673
276beea2 10674 /* A splay-tree mapping the low element of a case range to the high
10675 element, or NULL_TREE if there is no high element. Used to
10676 determine whether or not a new case label duplicates an old case
10677 label. We need a tree, rather than simply a hash table, because
10678 of the GNU case range extension. */
10679 splay_tree cases;
2ca392fd 10680
f805d53d 10681 /* The bindings at the point of the switch. This is used for
10682 warnings crossing decls when branching to a case label. */
10683 struct c_spot_bindings *bindings;
f5d156fd 10684
276beea2 10685 /* The next node on the stack. */
10686 struct c_switch *next;
be23b16f 10687
10688 /* Remember whether the controlling expression had boolean type
10689 before integer promotions for the sake of -Wswitch-bool. */
10690 bool bool_cond_p;
10691
10692 /* Remember whether there was a case value that is outside the
10693 range of the ORIG_TYPE. */
10694 bool outside_range_p;
276beea2 10695};
628fa4f8 10696
276beea2 10697/* A stack of the currently active switch statements. The innermost
10698 switch statement is on the top of the stack. There is no need to
10699 mark the stack for garbage collection because it is only active
10700 during the processing of the body of a function, and we never
10701 collect at that point. */
fc698c9d 10702
0375a275 10703struct c_switch *c_switch_stack;
fc698c9d 10704
276beea2 10705/* Start a C switch statement, testing expression EXP. Return the new
e60a6f7b 10706 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
c69ec07d 10707 SWITCH_COND_LOC is the location of the switch's condition.
be23b16f 10708 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
fc698c9d 10709
276beea2 10710tree
e60a6f7b 10711c_start_case (location_t switch_loc,
10712 location_t switch_cond_loc,
c69ec07d 10713 tree exp, bool explicit_cast_p)
fc698c9d 10714{
55d7e60b 10715 tree orig_type = error_mark_node;
be23b16f 10716 bool bool_cond_p = false;
276beea2 10717 struct c_switch *cs;
2b30d46c 10718
276beea2 10719 if (exp != error_mark_node)
fc698c9d 10720 {
276beea2 10721 orig_type = TREE_TYPE (exp);
10722
55d7e60b 10723 if (!INTEGRAL_TYPE_P (orig_type))
fc698c9d 10724 {
55d7e60b 10725 if (orig_type != error_mark_node)
10726 {
e60a6f7b 10727 error_at (switch_cond_loc, "switch quantity not an integer");
55d7e60b 10728 orig_type = error_mark_node;
10729 }
276beea2 10730 exp = integer_zero_node;
fc698c9d 10731 }
276beea2 10732 else
fc698c9d 10733 {
55d7e60b 10734 tree type = TYPE_MAIN_VARIANT (orig_type);
c69ec07d 10735 tree e = exp;
10736
10737 /* Warn if the condition has boolean value. */
10738 while (TREE_CODE (e) == COMPOUND_EXPR)
10739 e = TREE_OPERAND (e, 1);
10740
10741 if ((TREE_CODE (type) == BOOLEAN_TYPE
10742 || truth_value_p (TREE_CODE (e)))
10743 /* Explicit cast to int suppresses this warning. */
10744 && !(TREE_CODE (type) == INTEGER_TYPE
10745 && explicit_cast_p))
be23b16f 10746 bool_cond_p = true;
989bbcfc 10747
3df42822 10748 if (!in_system_header_at (input_location)
276beea2 10749 && (type == long_integer_type_node
10750 || type == long_unsigned_type_node))
e60a6f7b 10751 warning_at (switch_cond_loc,
10752 OPT_Wtraditional, "%<long%> switch expression not "
10753 "converted to %<int%> in ISO C");
989bbcfc 10754
a75b1c71 10755 exp = c_fully_fold (exp, false, NULL);
276beea2 10756 exp = default_conversion (exp);
6513b50d 10757
10758 if (warn_sequence_point)
10759 verify_sequence_points (exp);
276beea2 10760 }
10761 }
10762
197638d4 10763 /* Add this new SWITCH_EXPR to the stack. */
9318f22c 10764 cs = XNEW (struct c_switch);
bd37ce3e 10765 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
e60a6f7b 10766 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
2ca392fd 10767 cs->orig_type = orig_type;
276beea2 10768 cs->cases = splay_tree_new (case_compare, NULL, NULL);
f805d53d 10769 cs->bindings = c_get_switch_bindings ();
be23b16f 10770 cs->bool_cond_p = bool_cond_p;
10771 cs->outside_range_p = false;
0375a275 10772 cs->next = c_switch_stack;
10773 c_switch_stack = cs;
276beea2 10774
197638d4 10775 return add_stmt (cs->switch_expr);
276beea2 10776}
10777
e60a6f7b 10778/* Process a case label at location LOC. */
276beea2 10779
10780tree
e60a6f7b 10781do_case (location_t loc, tree low_value, tree high_value)
276beea2 10782{
10783 tree label = NULL_TREE;
10784
482c1ea0 10785 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10786 {
10787 low_value = c_fully_fold (low_value, false, NULL);
10788 if (TREE_CODE (low_value) == INTEGER_CST)
c9e51d25 10789 pedwarn (loc, OPT_Wpedantic,
482c1ea0 10790 "case label is not an integer constant expression");
10791 }
10792
10793 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10794 {
10795 high_value = c_fully_fold (high_value, false, NULL);
10796 if (TREE_CODE (high_value) == INTEGER_CST)
29438999 10797 pedwarn (input_location, OPT_Wpedantic,
482c1ea0 10798 "case label is not an integer constant expression");
10799 }
10800
f805d53d 10801 if (c_switch_stack == NULL)
f5d156fd 10802 {
10803 if (low_value)
f805d53d 10804 error_at (loc, "case label not within a switch statement");
f5d156fd 10805 else
f805d53d 10806 error_at (loc, "%<default%> label not within a switch statement");
10807 return NULL_TREE;
f5d156fd 10808 }
fc698c9d 10809
f805d53d 10810 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10811 EXPR_LOCATION (c_switch_stack->switch_expr),
10812 loc))
10813 return NULL_TREE;
10814
10815 label = c_add_case_label (loc, c_switch_stack->cases,
10816 SWITCH_COND (c_switch_stack->switch_expr),
10817 c_switch_stack->orig_type,
be23b16f 10818 low_value, high_value,
10819 &c_switch_stack->outside_range_p);
f805d53d 10820 if (label == error_mark_node)
10821 label = NULL_TREE;
276beea2 10822 return label;
10823}
fc698c9d 10824
5213d6c9 10825/* Finish the switch statement. TYPE is the original type of the
10826 controlling expression of the switch, or NULL_TREE. */
fc698c9d 10827
276beea2 10828void
5213d6c9 10829c_finish_case (tree body, tree type)
276beea2 10830{
0375a275 10831 struct c_switch *cs = c_switch_stack;
e7911019 10832 location_t switch_location;
276beea2 10833
197638d4 10834 SWITCH_BODY (cs->switch_expr) = body;
2363ef00 10835
4ee9c684 10836 /* Emit warnings as needed. */
e60a6f7b 10837 switch_location = EXPR_LOCATION (cs->switch_expr);
e7911019 10838 c_do_switch_warnings (cs->cases, switch_location,
5213d6c9 10839 type ? type : TREE_TYPE (cs->switch_expr),
be23b16f 10840 SWITCH_COND (cs->switch_expr),
10841 cs->bool_cond_p, cs->outside_range_p);
dfa10f0d 10842 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10843 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
4ee9c684 10844
276beea2 10845 /* Pop the stack. */
0375a275 10846 c_switch_stack = cs->next;
276beea2 10847 splay_tree_delete (cs->cases);
f805d53d 10848 c_release_switch_bindings (cs->bindings);
9318f22c 10849 XDELETE (cs);
fc698c9d 10850}
2363ef00 10851\f
0375a275 10852/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10853 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
0241e4dc 10854 may be null. */
2363ef00 10855
b576c0c1 10856void
0375a275 10857c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
0241e4dc 10858 tree else_block)
2363ef00 10859{
0375a275 10860 tree stmt;
2363ef00 10861
fee9f7da 10862 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
2ed8b5d0 10863 SET_EXPR_LOCATION (stmt, if_locus);
0375a275 10864 add_stmt (stmt);
2363ef00 10865}
10866
0375a275 10867/* Emit a general-purpose loop construct. START_LOCUS is the location of
10868 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10869 is false for DO loops. INCR is the FOR increment expression. BODY is
2c763ed4 10870 the statement controlled by the loop. BLAB is the break label. CLAB is
0375a275 10871 the continue label. Everything is allowed to be NULL. */
2363ef00 10872
10873void
0375a275 10874c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10875 tree blab, tree clab, bool cond_is_first)
2363ef00 10876{
0375a275 10877 tree entry = NULL, exit = NULL, t;
10878
3959c2d9 10879 /* If the condition is zero don't generate a loop construct. */
10880 if (cond && integer_zerop (cond))
10881 {
10882 if (cond_is_first)
10883 {
10884 t = build_and_jump (&blab);
10885 SET_EXPR_LOCATION (t, start_locus);
10886 add_stmt (t);
10887 }
10888 }
10889 else
0375a275 10890 {
10891 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
a0c938f0 10892
0375a275 10893 /* If we have an exit condition, then we build an IF with gotos either
a0c938f0 10894 out of the loop, or to the top of it. If there's no exit condition,
10895 then we just build a jump back to the top. */
0375a275 10896 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
a0c938f0 10897
3959c2d9 10898 if (cond && !integer_nonzerop (cond))
a0c938f0 10899 {
10900 /* Canonicalize the loop condition to the end. This means
10901 generating a branch to the loop condition. Reuse the
10902 continue label, if possible. */
10903 if (cond_is_first)
10904 {
10905 if (incr || !clab)
10906 {
10907 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10908 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10909 }
10910 else
10911 t = build1 (GOTO_EXPR, void_type_node, clab);
2ed8b5d0 10912 SET_EXPR_LOCATION (t, start_locus);
a0c938f0 10913 add_stmt (t);
10914 }
10915
0375a275 10916 t = build_and_jump (&blab);
0375a275 10917 if (cond_is_first)
389dd41b 10918 exit = fold_build3_loc (start_locus,
10919 COND_EXPR, void_type_node, cond, exit, t);
0375a275 10920 else
389dd41b 10921 exit = fold_build3_loc (input_location,
10922 COND_EXPR, void_type_node, cond, exit, t);
a0c938f0 10923 }
7758d608 10924 else
10925 {
10926 /* For the backward-goto's location of an unconditional loop
10927 use the beginning of the body, or, if there is none, the
10928 top of the loop. */
10929 location_t loc = EXPR_LOCATION (expr_first (body));
10930 if (loc == UNKNOWN_LOCATION)
10931 loc = start_locus;
10932 SET_EXPR_LOCATION (exit, loc);
10933 }
a0c938f0 10934
0375a275 10935 add_stmt (top);
10936 }
a0c938f0 10937
0375a275 10938 if (body)
10939 add_stmt (body);
10940 if (clab)
10941 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10942 if (incr)
10943 add_stmt (incr);
10944 if (entry)
10945 add_stmt (entry);
10946 if (exit)
10947 add_stmt (exit);
10948 if (blab)
10949 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
2363ef00 10950}
2363ef00 10951
10952tree
e60a6f7b 10953c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
2363ef00 10954{
7f0f308d 10955 bool skip;
0375a275 10956 tree label = *label_p;
2363ef00 10957
7f0f308d 10958 /* In switch statements break is sometimes stylistically used after
10959 a return statement. This can lead to spurious warnings about
10960 control reaching the end of a non-void function when it is
10961 inlined. Note that we are calling block_may_fallthru with
10962 language specific tree nodes; this works because
10963 block_may_fallthru returns true when given something it does not
10964 understand. */
10965 skip = !block_may_fallthru (cur_stmt_list);
10966
0375a275 10967 if (!label)
7f0f308d 10968 {
10969 if (!skip)
e60a6f7b 10970 *label_p = label = create_artificial_label (loc);
7f0f308d 10971 }
1e8e9920 10972 else if (TREE_CODE (label) == LABEL_DECL)
10973 ;
f9ae6f95 10974 else switch (TREE_INT_CST_LOW (label))
0375a275 10975 {
1e8e9920 10976 case 0:
0375a275 10977 if (is_break)
e60a6f7b 10978 error_at (loc, "break statement not within loop or switch");
0375a275 10979 else
e60a6f7b 10980 error_at (loc, "continue statement not within a loop");
0375a275 10981 return NULL_TREE;
1e8e9920 10982
10983 case 1:
10984 gcc_assert (is_break);
e60a6f7b 10985 error_at (loc, "break statement used with OpenMP for loop");
1e8e9920 10986 return NULL_TREE;
10987
f2697631 10988 case 2:
10989 if (is_break)
10990 error ("break statement within %<#pragma simd%> loop body");
10991 else
10992 error ("continue statement within %<#pragma simd%> loop body");
10993 return NULL_TREE;
10994
1e8e9920 10995 default:
10996 gcc_unreachable ();
0375a275 10997 }
2363ef00 10998
7f0f308d 10999 if (skip)
11000 return NULL_TREE;
11001
4a1849e3 11002 if (!is_break)
11003 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
11004
14ae0310 11005 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
2363ef00 11006}
11007
0375a275 11008/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
2569a1be 11009
11010static void
e60a6f7b 11011emit_side_effect_warnings (location_t loc, tree expr)
2569a1be 11012{
29eda12a 11013 if (expr == error_mark_node)
11014 ;
11015 else if (!TREE_SIDE_EFFECTS (expr))
2569a1be 11016 {
11017 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
e60a6f7b 11018 warning_at (loc, OPT_Wunused_value, "statement with no effect");
2569a1be 11019 }
9a9db3c1 11020 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11021 {
11022 tree r = expr;
11023 location_t cloc = loc;
11024 while (TREE_CODE (r) == COMPOUND_EXPR)
11025 {
11026 if (EXPR_HAS_LOCATION (r))
11027 cloc = EXPR_LOCATION (r);
11028 r = TREE_OPERAND (r, 1);
11029 }
11030 if (!TREE_SIDE_EFFECTS (r)
11031 && !VOID_TYPE_P (TREE_TYPE (r))
11032 && !CONVERT_EXPR_P (r)
977b1853 11033 && !TREE_NO_WARNING (r)
9a9db3c1 11034 && !TREE_NO_WARNING (expr))
11035 warning_at (cloc, OPT_Wunused_value,
11036 "right-hand operand of comma expression has no effect");
11037 }
43667bd3 11038 else
e60a6f7b 11039 warn_if_unused_value (expr, loc);
2569a1be 11040}
11041
0375a275 11042/* Process an expression as if it were a complete statement. Emit
e60a6f7b 11043 diagnostics, but do not call ADD_STMT. LOC is the location of the
11044 statement. */
2569a1be 11045
0375a275 11046tree
e60a6f7b 11047c_process_expr_stmt (location_t loc, tree expr)
2569a1be 11048{
b2af2a93 11049 tree exprv;
11050
2569a1be 11051 if (!expr)
0375a275 11052 return NULL_TREE;
2569a1be 11053
a75b1c71 11054 expr = c_fully_fold (expr, false, NULL);
11055
2569a1be 11056 if (warn_sequence_point)
11057 verify_sequence_points (expr);
11058
11059 if (TREE_TYPE (expr) != error_mark_node
11060 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11061 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
e60a6f7b 11062 error_at (loc, "expression statement has incomplete type");
2569a1be 11063
11064 /* If we're not processing a statement expression, warn about unused values.
11065 Warnings for statement expressions will be emitted later, once we figure
11066 out which is the result. */
11067 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
43667bd3 11068 && warn_unused_value)
1d65bac2 11069 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
2569a1be 11070
b2af2a93 11071 exprv = expr;
11072 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11073 exprv = TREE_OPERAND (exprv, 1);
0902fd73 11074 while (CONVERT_EXPR_P (exprv))
11075 exprv = TREE_OPERAND (exprv, 0);
11076 if (DECL_P (exprv)
11077 || handled_component_p (exprv)
11078 || TREE_CODE (exprv) == ADDR_EXPR)
b2af2a93 11079 mark_exp_read (exprv);
7cb9c999 11080
2569a1be 11081 /* If the expression is not of a type to which we cannot assign a line
11082 number, wrap the thing in a no-op NOP_EXPR. */
ce45a448 11083 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
e60a6f7b 11084 {
11085 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11086 SET_EXPR_LOCATION (expr, loc);
11087 }
0375a275 11088
11089 return expr;
11090}
11091
e60a6f7b 11092/* Emit an expression as a statement. LOC is the location of the
11093 expression. */
0375a275 11094
11095tree
e60a6f7b 11096c_finish_expr_stmt (location_t loc, tree expr)
0375a275 11097{
11098 if (expr)
e60a6f7b 11099 return add_stmt (c_process_expr_stmt (loc, expr));
0375a275 11100 else
11101 return NULL;
2569a1be 11102}
11103
11104/* Do the opposite and emit a statement as an expression. To begin,
11105 create a new binding level and return it. */
2363ef00 11106
11107tree
11108c_begin_stmt_expr (void)
11109{
11110 tree ret;
11111
11112 /* We must force a BLOCK for this level so that, if it is not expanded
11113 later, there is a way to turn off the entire subtree of blocks that
11114 are contained in it. */
11115 keep_next_level ();
11116 ret = c_begin_compound_stmt (true);
f805d53d 11117
11118 c_bindings_start_stmt_expr (c_switch_stack == NULL
11119 ? NULL
11120 : c_switch_stack->bindings);
2363ef00 11121
11122 /* Mark the current statement list as belonging to a statement list. */
11123 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11124
11125 return ret;
11126}
11127
e60a6f7b 11128/* LOC is the location of the compound statement to which this body
11129 belongs. */
11130
2363ef00 11131tree
e60a6f7b 11132c_finish_stmt_expr (location_t loc, tree body)
2363ef00 11133{
2569a1be 11134 tree last, type, tmp, val;
2363ef00 11135 tree *last_p;
11136
e60a6f7b 11137 body = c_end_compound_stmt (loc, body, true);
f805d53d 11138
11139 c_bindings_end_stmt_expr (c_switch_stack == NULL
11140 ? NULL
11141 : c_switch_stack->bindings);
2363ef00 11142
2569a1be 11143 /* Locate the last statement in BODY. See c_end_compound_stmt
11144 about always returning a BIND_EXPR. */
11145 last_p = &BIND_EXPR_BODY (body);
11146 last = BIND_EXPR_BODY (body);
11147
11148 continue_searching:
2363ef00 11149 if (TREE_CODE (last) == STATEMENT_LIST)
11150 {
2ea33951 11151 tree_stmt_iterator l = tsi_last (last);
11152
11153 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11154 tsi_prev (&l);
2569a1be 11155
11156 /* This can happen with degenerate cases like ({ }). No value. */
2ea33951 11157 if (tsi_end_p (l))
2569a1be 11158 return body;
11159
11160 /* If we're supposed to generate side effects warnings, process
11161 all of the statements except the last. */
43667bd3 11162 if (warn_unused_value)
2363ef00 11163 {
2ea33951 11164 for (tree_stmt_iterator i = tsi_start (last);
11165 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
e60a6f7b 11166 {
11167 location_t tloc;
11168 tree t = tsi_stmt (i);
11169
11170 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11171 emit_side_effect_warnings (tloc, t);
11172 }
2363ef00 11173 }
2ea33951 11174 last_p = tsi_stmt_ptr (l);
2569a1be 11175 last = *last_p;
2363ef00 11176 }
11177
2569a1be 11178 /* If the end of the list is exception related, then the list was split
11179 by a call to push_cleanup. Continue searching. */
11180 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11181 || TREE_CODE (last) == TRY_CATCH_EXPR)
11182 {
11183 last_p = &TREE_OPERAND (last, 0);
11184 last = *last_p;
11185 goto continue_searching;
11186 }
11187
9fa2814c 11188 if (last == error_mark_node)
11189 return last;
11190
2569a1be 11191 /* In the case that the BIND_EXPR is not necessary, return the
11192 expression out from inside it. */
90567983 11193 if ((last == BIND_EXPR_BODY (body)
11194 /* Skip nested debug stmts. */
11195 || last == expr_first (BIND_EXPR_BODY (body)))
9fa2814c 11196 && BIND_EXPR_VARS (body) == NULL)
1c3d3e7c 11197 {
a75b1c71 11198 /* Even if this looks constant, do not allow it in a constant
11199 expression. */
f59e3889 11200 last = c_wrap_maybe_const (last, true);
1c3d3e7c 11201 /* Do not warn if the return value of a statement expression is
11202 unused. */
a75b1c71 11203 TREE_NO_WARNING (last) = 1;
1c3d3e7c 11204 return last;
11205 }
2363ef00 11206
11207 /* Extract the type of said expression. */
11208 type = TREE_TYPE (last);
2363ef00 11209
2569a1be 11210 /* If we're not returning a value at all, then the BIND_EXPR that
11211 we already have is a fine expression to return. */
11212 if (!type || VOID_TYPE_P (type))
11213 return body;
11214
11215 /* Now that we've located the expression containing the value, it seems
11216 silly to make voidify_wrapper_expr repeat the process. Create a
11217 temporary of the appropriate type and stick it in a TARGET_EXPR. */
f9e245b2 11218 tmp = create_tmp_var_raw (type);
2569a1be 11219
11220 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11221 tree_expr_nonnegative_p giving up immediately. */
11222 val = last;
11223 if (TREE_CODE (val) == NOP_EXPR
11224 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11225 val = TREE_OPERAND (val, 0);
11226
14ae0310 11227 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
1cf1742e 11228 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
2569a1be 11229
e60a6f7b 11230 {
11231 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11232 SET_EXPR_LOCATION (t, loc);
11233 return t;
11234 }
2363ef00 11235}
11236\f
11237/* Begin and end compound statements. This is as simple as pushing
11238 and popping new statement lists from the tree. */
11239
11240tree
11241c_begin_compound_stmt (bool do_scope)
11242{
11243 tree stmt = push_stmt_list ();
11244 if (do_scope)
735f4358 11245 push_scope ();
2363ef00 11246 return stmt;
11247}
11248
e60a6f7b 11249/* End a compound statement. STMT is the statement. LOC is the
11250 location of the compound statement-- this is usually the location
11251 of the opening brace. */
11252
2363ef00 11253tree
e60a6f7b 11254c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
2363ef00 11255{
11256 tree block = NULL;
11257
11258 if (do_scope)
11259 {
11260 if (c_dialect_objc ())
11261 objc_clear_super_receiver ();
11262 block = pop_scope ();
11263 }
11264
11265 stmt = pop_stmt_list (stmt);
e60a6f7b 11266 stmt = c_build_bind_expr (loc, block, stmt);
2363ef00 11267
11268 /* If this compound statement is nested immediately inside a statement
11269 expression, then force a BIND_EXPR to be created. Otherwise we'll
11270 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11271 STATEMENT_LISTs merge, and thus we can lose track of what statement
11272 was really last. */
cacfdc02 11273 if (building_stmt_list_p ()
2363ef00 11274 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11275 && TREE_CODE (stmt) != BIND_EXPR)
11276 {
14ae0310 11277 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
2363ef00 11278 TREE_SIDE_EFFECTS (stmt) = 1;
e60a6f7b 11279 SET_EXPR_LOCATION (stmt, loc);
2363ef00 11280 }
11281
11282 return stmt;
11283}
dddab69e 11284
11285/* Queue a cleanup. CLEANUP is an expression/statement to be executed
11286 when the current scope is exited. EH_ONLY is true when this is not
11287 meant to apply to normal control flow transfer. */
11288
11289void
e60a6f7b 11290push_cleanup (tree decl, tree cleanup, bool eh_only)
dddab69e 11291{
2569a1be 11292 enum tree_code code;
11293 tree stmt, list;
11294 bool stmt_expr;
11295
11296 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
e60a6f7b 11297 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
dddab69e 11298 add_stmt (stmt);
2569a1be 11299 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11300 list = push_stmt_list ();
11301 TREE_OPERAND (stmt, 0) = list;
11302 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
dddab69e 11303}
2363ef00 11304\f
1f137e6d 11305/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11306 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11307
11308static tree
11309build_vec_cmp (tree_code code, tree type,
11310 tree arg0, tree arg1)
11311{
11312 tree zero_vec = build_zero_cst (type);
11313 tree minus_one_vec = build_minus_one_cst (type);
11314 tree cmp_type = build_same_sized_truth_vector_type (type);
11315 tree cmp = build2 (code, cmp_type, arg0, arg1);
11316 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11317}
11318
276beea2 11319/* Build a binary-operation expression without default conversions.
11320 CODE is the kind of expression to build.
8e70fb09 11321 LOCATION is the operator's location.
276beea2 11322 This function differs from `build' in several ways:
11323 the data type of the result is computed and recorded in it,
11324 warnings are generated if arg data types are invalid,
11325 special handling for addition and subtraction of pointers is known,
11326 and some optimization is done (operations on narrow ints
11327 are done in the narrower type when that gives the same result).
11328 Constant folding is also done before the result is returned.
fc698c9d 11329
276beea2 11330 Note that the operands will never have enumeral types, or function
11331 or array types, because either they will have the default conversions
11332 performed or they have both just been converted to some other type in which
11333 the arithmetic is to be done. */
11334
11335tree
8e70fb09 11336build_binary_op (location_t location, enum tree_code code,
623ee358 11337 tree orig_op0, tree orig_op1, bool convert_p)
fc698c9d 11338{
c6418a4e 11339 tree type0, type1, orig_type0, orig_type1;
11340 tree eptype;
276beea2 11341 enum tree_code code0, code1;
11342 tree op0, op1;
b6889cb0 11343 tree ret = error_mark_node;
7a979707 11344 const char *invalid_op_diag;
9a8bed72 11345 bool op0_int_operands, op1_int_operands;
a75b1c71 11346 bool int_const, int_const_or_overflow, int_operands;
554732cd 11347
276beea2 11348 /* Expression code to give to the expression when it is built.
11349 Normally this is CODE, which is what the caller asked for,
11350 but in some special cases we change it. */
11351 enum tree_code resultcode = code;
989bbcfc 11352
276beea2 11353 /* Data type in which the computation is to be performed.
11354 In the simplest cases this is the common type of the arguments. */
11355 tree result_type = NULL;
11356
c6418a4e 11357 /* When the computation is in excess precision, the type of the
11358 final EXCESS_PRECISION_EXPR. */
8f45ab4d 11359 tree semantic_result_type = NULL;
c6418a4e 11360
276beea2 11361 /* Nonzero means operands have already been type-converted
11362 in whatever way is necessary.
11363 Zero means they need to be converted to RESULT_TYPE. */
11364 int converted = 0;
11365
11366 /* Nonzero means create the expression with this type, rather than
11367 RESULT_TYPE. */
72749341 11368 tree build_type = NULL_TREE;
276beea2 11369
11370 /* Nonzero means after finally constructing the expression
11371 convert it to this type. */
72749341 11372 tree final_type = NULL_TREE;
276beea2 11373
11374 /* Nonzero if this is an operation like MIN or MAX which can
11375 safely be computed in short if both args are promoted shorts.
11376 Also implies COMMON.
11377 -1 indicates a bitwise operation; this makes a difference
11378 in the exact conditions for when it is safe to do the operation
11379 in a narrower mode. */
11380 int shorten = 0;
11381
11382 /* Nonzero if this is a comparison operation;
11383 if both args are promoted shorts, compare the original shorts.
11384 Also implies COMMON. */
11385 int short_compare = 0;
11386
11387 /* Nonzero if this is a right-shift operation, which can be computed on the
11388 original short and then promoted if the operand is a promoted short. */
11389 int short_shift = 0;
11390
11391 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11392 int common = 0;
11393
34d3c5de 11394 /* True means types are compatible as far as ObjC is concerned. */
11395 bool objc_ok;
11396
c6418a4e 11397 /* True means this is an arithmetic operation that may need excess
11398 precision. */
11399 bool may_need_excess_precision;
11400
16a547ac 11401 /* True means this is a boolean operation that converts both its
11402 operands to truth-values. */
11403 bool boolean_op = false;
11404
9e46467d 11405 /* Remember whether we're doing / or %. */
11406 bool doing_div_or_mod = false;
11407
11408 /* Remember whether we're doing << or >>. */
11409 bool doing_shift = false;
11410
11411 /* Tree holding instrumentation expression. */
11412 tree instrument_expr = NULL;
11413
8e70fb09 11414 if (location == UNKNOWN_LOCATION)
11415 location = input_location;
11416
9a8bed72 11417 op0 = orig_op0;
11418 op1 = orig_op1;
11419
11420 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11421 if (op0_int_operands)
11422 op0 = remove_c_maybe_const_expr (op0);
11423 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11424 if (op1_int_operands)
11425 op1 = remove_c_maybe_const_expr (op1);
11426 int_operands = (op0_int_operands && op1_int_operands);
a75b1c71 11427 if (int_operands)
11428 {
11429 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11430 && TREE_CODE (orig_op1) == INTEGER_CST);
11431 int_const = (int_const_or_overflow
11432 && !TREE_OVERFLOW (orig_op0)
11433 && !TREE_OVERFLOW (orig_op1));
11434 }
11435 else
11436 int_const = int_const_or_overflow = false;
11437
7dfa155b 11438 /* Do not apply default conversion in mixed vector/scalar expression. */
6cf89e04 11439 if (convert_p
6290f0db 11440 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
fcacf125 11441 {
9a8bed72 11442 op0 = default_conversion (op0);
11443 op1 = default_conversion (op1);
fcacf125 11444 }
11445
efa02472 11446 orig_type0 = type0 = TREE_TYPE (op0);
3c6d4197 11447
efa02472 11448 orig_type1 = type1 = TREE_TYPE (op1);
812fff1a 11449
276beea2 11450 /* The expression codes of the data types of the arguments tell us
11451 whether the arguments are integers, floating, pointers, etc. */
11452 code0 = TREE_CODE (type0);
11453 code1 = TREE_CODE (type1);
11454
11455 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11456 STRIP_TYPE_NOPS (op0);
11457 STRIP_TYPE_NOPS (op1);
11458
11459 /* If an error was already reported for one of the arguments,
11460 avoid reporting another error. */
11461
11462 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11463 return error_mark_node;
11464
547c6b1f 11465 if (code0 == POINTER_TYPE
11466 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11467 return error_mark_node;
11468
11469 if (code1 == POINTER_TYPE
11470 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11471 return error_mark_node;
11472
7a979707 11473 if ((invalid_op_diag
11474 = targetm.invalid_binary_op (code, type0, type1)))
11475 {
8e70fb09 11476 error_at (location, invalid_op_diag);
7a979707 11477 return error_mark_node;
11478 }
11479
c6418a4e 11480 switch (code)
11481 {
11482 case PLUS_EXPR:
11483 case MINUS_EXPR:
11484 case MULT_EXPR:
11485 case TRUNC_DIV_EXPR:
11486 case CEIL_DIV_EXPR:
11487 case FLOOR_DIV_EXPR:
11488 case ROUND_DIV_EXPR:
11489 case EXACT_DIV_EXPR:
11490 may_need_excess_precision = true;
11491 break;
2923f790 11492
11493 case EQ_EXPR:
11494 case NE_EXPR:
11495 case LE_EXPR:
11496 case GE_EXPR:
11497 case LT_EXPR:
11498 case GT_EXPR:
11499 /* Excess precision for implicit conversions of integers to
11500 floating point in C11 and later. */
11501 may_need_excess_precision = (flag_isoc11
11502 && (ANY_INTEGRAL_TYPE_P (type0)
11503 || ANY_INTEGRAL_TYPE_P (type1)));
11504 break;
11505
c6418a4e 11506 default:
11507 may_need_excess_precision = false;
11508 break;
11509 }
11510 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11511 {
11512 op0 = TREE_OPERAND (op0, 0);
11513 type0 = TREE_TYPE (op0);
11514 }
11515 else if (may_need_excess_precision
11516 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11517 {
11518 type0 = eptype;
11519 op0 = convert (eptype, op0);
11520 }
11521 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11522 {
11523 op1 = TREE_OPERAND (op1, 0);
11524 type1 = TREE_TYPE (op1);
11525 }
11526 else if (may_need_excess_precision
11527 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11528 {
11529 type1 = eptype;
11530 op1 = convert (eptype, op1);
11531 }
11532
34d3c5de 11533 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11534
7dfa155b 11535 /* In case when one of the operands of the binary operation is
11536 a vector and another is a scalar -- convert scalar to vector. */
11537 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11538 {
41ed701a 11539 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11540 true);
6cf89e04 11541
7dfa155b 11542 switch (convert_flag)
11543 {
11544 case stv_error:
11545 return error_mark_node;
11546 case stv_firstarg:
11547 {
11548 bool maybe_const = true;
11549 tree sc;
11550 sc = c_fully_fold (op0, false, &maybe_const);
11551 sc = save_expr (sc);
11552 sc = convert (TREE_TYPE (type1), sc);
11553 op0 = build_vector_from_val (type1, sc);
11554 if (!maybe_const)
11555 op0 = c_wrap_maybe_const (op0, true);
11556 orig_type0 = type0 = TREE_TYPE (op0);
11557 code0 = TREE_CODE (type0);
11558 converted = 1;
11559 break;
11560 }
11561 case stv_secondarg:
11562 {
11563 bool maybe_const = true;
11564 tree sc;
11565 sc = c_fully_fold (op1, false, &maybe_const);
11566 sc = save_expr (sc);
11567 sc = convert (TREE_TYPE (type0), sc);
11568 op1 = build_vector_from_val (type0, sc);
11569 if (!maybe_const)
104619f5 11570 op1 = c_wrap_maybe_const (op1, true);
7dfa155b 11571 orig_type1 = type1 = TREE_TYPE (op1);
11572 code1 = TREE_CODE (type1);
11573 converted = 1;
11574 break;
11575 }
11576 default:
11577 break;
11578 }
11579 }
11580
276beea2 11581 switch (code)
fc698c9d 11582 {
276beea2 11583 case PLUS_EXPR:
11584 /* Handle the pointer + int case. */
11585 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
b6889cb0 11586 {
389dd41b 11587 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
b6889cb0 11588 goto return_build_binary_op;
11589 }
276beea2 11590 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
b6889cb0 11591 {
389dd41b 11592 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
b6889cb0 11593 goto return_build_binary_op;
11594 }
16d7533c 11595 else
276beea2 11596 common = 1;
11597 break;
628fa4f8 11598
276beea2 11599 case MINUS_EXPR:
11600 /* Subtraction of two similar pointers.
11601 We must subtract them as integers, then divide by object size. */
11602 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
d5b637fa 11603 && comp_target_types (location, type0, type1))
b6889cb0 11604 {
eabe2d94 11605 ret = pointer_diff (location, op0, op1, &instrument_expr);
b6889cb0 11606 goto return_build_binary_op;
11607 }
276beea2 11608 /* Handle pointer minus int. Just like pointer plus int. */
11609 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
b6889cb0 11610 {
389dd41b 11611 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
b6889cb0 11612 goto return_build_binary_op;
11613 }
276beea2 11614 else
11615 common = 1;
11616 break;
989bbcfc 11617
276beea2 11618 case MULT_EXPR:
11619 common = 1;
11620 break;
11621
11622 case TRUNC_DIV_EXPR:
11623 case CEIL_DIV_EXPR:
11624 case FLOOR_DIV_EXPR:
11625 case ROUND_DIV_EXPR:
11626 case EXACT_DIV_EXPR:
9e46467d 11627 doing_div_or_mod = true;
b6889cb0 11628 warn_for_div_by_zero (location, op1);
276beea2 11629
11630 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9421ebb9 11631 || code0 == FIXED_POINT_TYPE
276beea2 11632 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11633 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9421ebb9 11634 || code1 == FIXED_POINT_TYPE
276beea2 11635 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
628fa4f8 11636 {
491255f5 11637 enum tree_code tcode0 = code0, tcode1 = code1;
11638
4418a1d4 11639 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
491255f5 11640 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4418a1d4 11641 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
491255f5 11642 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4418a1d4 11643
9421ebb9 11644 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11645 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
276beea2 11646 resultcode = RDIV_EXPR;
11647 else
11648 /* Although it would be tempting to shorten always here, that
11649 loses on some targets, since the modulo instruction is
11650 undefined if the quotient can't be represented in the
11651 computation mode. We shorten only if unsigned or if
11652 dividing by something we know != -1. */
78a8ed03 11653 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
276beea2 11654 || (TREE_CODE (op1) == INTEGER_CST
84166705 11655 && !integer_all_onesp (op1)));
276beea2 11656 common = 1;
11657 }
11658 break;
fc698c9d 11659
276beea2 11660 case BIT_AND_EXPR:
276beea2 11661 case BIT_IOR_EXPR:
11662 case BIT_XOR_EXPR:
11663 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11664 shorten = -1;
f8629d35 11665 /* Allow vector types which are not floating point types. */
11666 else if (code0 == VECTOR_TYPE
11667 && code1 == VECTOR_TYPE
11668 && !VECTOR_FLOAT_TYPE_P (type0)
11669 && !VECTOR_FLOAT_TYPE_P (type1))
276beea2 11670 common = 1;
11671 break;
11672
11673 case TRUNC_MOD_EXPR:
11674 case FLOOR_MOD_EXPR:
9e46467d 11675 doing_div_or_mod = true;
b6889cb0 11676 warn_for_div_by_zero (location, op1);
fc698c9d 11677
b6d7101e 11678 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11679 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11680 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11681 common = 1;
11682 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
276beea2 11683 {
11684 /* Although it would be tempting to shorten always here, that loses
11685 on some targets, since the modulo instruction is undefined if the
11686 quotient can't be represented in the computation mode. We shorten
11687 only if unsigned or if dividing by something we know != -1. */
78a8ed03 11688 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
276beea2 11689 || (TREE_CODE (op1) == INTEGER_CST
84166705 11690 && !integer_all_onesp (op1)));
276beea2 11691 common = 1;
11692 }
11693 break;
fc698c9d 11694
276beea2 11695 case TRUTH_ANDIF_EXPR:
11696 case TRUTH_ORIF_EXPR:
11697 case TRUTH_AND_EXPR:
11698 case TRUTH_OR_EXPR:
11699 case TRUTH_XOR_EXPR:
11700 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9421ebb9 11701 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11702 || code0 == FIXED_POINT_TYPE)
276beea2 11703 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9421ebb9 11704 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11705 || code1 == FIXED_POINT_TYPE))
276beea2 11706 {
11707 /* Result of these operations is always an int,
11708 but that does not mean the operands should be
11709 converted to ints! */
11710 result_type = integer_type_node;
bb554051 11711 if (op0_int_operands)
11712 {
11713 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11714 op0 = remove_c_maybe_const_expr (op0);
11715 }
11716 else
11717 op0 = c_objc_common_truthvalue_conversion (location, op0);
11718 if (op1_int_operands)
11719 {
11720 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11721 op1 = remove_c_maybe_const_expr (op1);
11722 }
11723 else
11724 op1 = c_objc_common_truthvalue_conversion (location, op1);
276beea2 11725 converted = 1;
16a547ac 11726 boolean_op = true;
276beea2 11727 }
a75b1c71 11728 if (code == TRUTH_ANDIF_EXPR)
11729 {
11730 int_const_or_overflow = (int_operands
11731 && TREE_CODE (orig_op0) == INTEGER_CST
11732 && (op0 == truthvalue_false_node
11733 || TREE_CODE (orig_op1) == INTEGER_CST));
11734 int_const = (int_const_or_overflow
11735 && !TREE_OVERFLOW (orig_op0)
11736 && (op0 == truthvalue_false_node
11737 || !TREE_OVERFLOW (orig_op1)));
11738 }
11739 else if (code == TRUTH_ORIF_EXPR)
11740 {
11741 int_const_or_overflow = (int_operands
11742 && TREE_CODE (orig_op0) == INTEGER_CST
11743 && (op0 == truthvalue_true_node
11744 || TREE_CODE (orig_op1) == INTEGER_CST));
11745 int_const = (int_const_or_overflow
11746 && !TREE_OVERFLOW (orig_op0)
11747 && (op0 == truthvalue_true_node
11748 || !TREE_OVERFLOW (orig_op1)));
11749 }
276beea2 11750 break;
cad0b4b0 11751
276beea2 11752 /* Shift operations: result has same type as first operand;
11753 always convert second operand to int.
11754 Also set SHORT_SHIFT if shifting rightward. */
fc698c9d 11755
276beea2 11756 case RSHIFT_EXPR:
f3c032a4 11757 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11758 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11759 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
f08ee65f 11760 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11761 TYPE_VECTOR_SUBPARTS (type1)))
191b2c61 11762 {
11763 result_type = type0;
11764 converted = 1;
11765 }
f3c032a4 11766 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
9040bba5 11767 || (code0 == VECTOR_TYPE
11768 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
8d669e79 11769 && code1 == INTEGER_TYPE)
276beea2 11770 {
9e46467d 11771 doing_shift = true;
a75b1c71 11772 if (TREE_CODE (op1) == INTEGER_CST)
554732cd 11773 {
276beea2 11774 if (tree_int_cst_sgn (op1) < 0)
a75b1c71 11775 {
11776 int_const = false;
48d94ede 11777 if (c_inhibit_evaluation_warnings == 0)
0feb4de1 11778 warning_at (location, OPT_Wshift_count_negative,
11779 "right shift count is negative");
a75b1c71 11780 }
f3c032a4 11781 else if (code0 == VECTOR_TYPE)
11782 {
11783 if (compare_tree_int (op1,
11784 TYPE_PRECISION (TREE_TYPE (type0)))
11785 >= 0)
11786 {
11787 int_const = false;
11788 if (c_inhibit_evaluation_warnings == 0)
11789 warning_at (location, OPT_Wshift_count_overflow,
11790 "right shift count >= width of vector element");
11791 }
11792 }
276beea2 11793 else
66fb954a 11794 {
84166705 11795 if (!integer_zerop (op1))
276beea2 11796 short_shift = 1;
11797
11798 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
a75b1c71 11799 {
11800 int_const = false;
48d94ede 11801 if (c_inhibit_evaluation_warnings == 0)
0feb4de1 11802 warning_at (location, OPT_Wshift_count_overflow,
11803 "right shift count >= width of type");
a75b1c71 11804 }
66fb954a 11805 }
554732cd 11806 }
fc698c9d 11807
276beea2 11808 /* Use the type of the value to be shifted. */
11809 result_type = type0;
276beea2 11810 /* Avoid converting op1 to result_type later. */
11811 converted = 1;
628fa4f8 11812 }
276beea2 11813 break;
fa6ddf23 11814
276beea2 11815 case LSHIFT_EXPR:
f3c032a4 11816 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11817 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11818 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
f08ee65f 11819 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11820 TYPE_VECTOR_SUBPARTS (type1)))
191b2c61 11821 {
11822 result_type = type0;
11823 converted = 1;
11824 }
f3c032a4 11825 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
9040bba5 11826 || (code0 == VECTOR_TYPE
11827 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
8d669e79 11828 && code1 == INTEGER_TYPE)
276beea2 11829 {
9e46467d 11830 doing_shift = true;
cdc64059 11831 if (TREE_CODE (op0) == INTEGER_CST
11832 && tree_int_cst_sgn (op0) < 0)
11833 {
11834 /* Don't reject a left shift of a negative value in a context
11835 where a constant expression is needed in C90. */
11836 if (flag_isoc99)
11837 int_const = false;
11838 if (c_inhibit_evaluation_warnings == 0)
11839 warning_at (location, OPT_Wshift_negative_value,
11840 "left shift of negative value");
11841 }
a75b1c71 11842 if (TREE_CODE (op1) == INTEGER_CST)
fc698c9d 11843 {
276beea2 11844 if (tree_int_cst_sgn (op1) < 0)
a75b1c71 11845 {
11846 int_const = false;
48d94ede 11847 if (c_inhibit_evaluation_warnings == 0)
0feb4de1 11848 warning_at (location, OPT_Wshift_count_negative,
11849 "left shift count is negative");
a75b1c71 11850 }
f3c032a4 11851 else if (code0 == VECTOR_TYPE)
11852 {
11853 if (compare_tree_int (op1,
11854 TYPE_PRECISION (TREE_TYPE (type0)))
11855 >= 0)
11856 {
11857 int_const = false;
11858 if (c_inhibit_evaluation_warnings == 0)
11859 warning_at (location, OPT_Wshift_count_overflow,
11860 "left shift count >= width of vector element");
11861 }
11862 }
276beea2 11863 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
a75b1c71 11864 {
11865 int_const = false;
48d94ede 11866 if (c_inhibit_evaluation_warnings == 0)
0feb4de1 11867 warning_at (location, OPT_Wshift_count_overflow,
11868 "left shift count >= width of type");
a75b1c71 11869 }
8d669e79 11870 else if (TREE_CODE (op0) == INTEGER_CST
11871 && maybe_warn_shift_overflow (location, op0, op1)
11872 && flag_isoc99)
11873 int_const = false;
d354d271 11874 }
fc698c9d 11875
276beea2 11876 /* Use the type of the value to be shifted. */
11877 result_type = type0;
276beea2 11878 /* Avoid converting op1 to result_type later. */
11879 converted = 1;
628fa4f8 11880 }
276beea2 11881 break;
fc698c9d 11882
276beea2 11883 case EQ_EXPR:
11884 case NE_EXPR:
d7ad16c2 11885 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11886 {
11887 tree intt;
73437615 11888 if (!vector_types_compatible_elements_p (type0, type1))
d7ad16c2 11889 {
11890 error_at (location, "comparing vectors with different "
11891 "element types");
11892 return error_mark_node;
11893 }
11894
f08ee65f 11895 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11896 TYPE_VECTOR_SUBPARTS (type1)))
d7ad16c2 11897 {
11898 error_at (location, "comparing vectors with different "
11899 "number of elements");
11900 return error_mark_node;
11901 }
11902
fee5e491 11903 /* It's not precisely specified how the usual arithmetic
11904 conversions apply to the vector types. Here, we use
11905 the unsigned type if one of the operands is signed and
11906 the other one is unsigned. */
11907 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11908 {
11909 if (!TYPE_UNSIGNED (type0))
11910 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11911 else
11912 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11913 warning_at (location, OPT_Wsign_compare, "comparison between "
11914 "types %qT and %qT", type0, type1);
11915 }
11916
d7ad16c2 11917 /* Always construct signed integer vector type. */
11918 intt = c_common_type_for_size (GET_MODE_BITSIZE
3d2b0034 11919 (SCALAR_TYPE_MODE
11920 (TREE_TYPE (type0))), 0);
86f1f5d1 11921 if (!intt)
11922 {
11923 error_at (location, "could not find an integer type "
11924 "of the same size as %qT",
11925 TREE_TYPE (type0));
11926 return error_mark_node;
11927 }
d7ad16c2 11928 result_type = build_opaque_vector_type (intt,
11929 TYPE_VECTOR_SUBPARTS (type0));
11930 converted = 1;
1f137e6d 11931 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11932 goto return_build_binary_op;
d7ad16c2 11933 }
067d82a2 11934 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
8e70fb09 11935 warning_at (location,
11936 OPT_Wfloat_equal,
11937 "comparing floating point with == or != is unsafe");
276beea2 11938 /* Result of comparison is always int,
11939 but don't convert the args to int! */
11940 build_type = integer_type_node;
11941 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9421ebb9 11942 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
276beea2 11943 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9421ebb9 11944 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
276beea2 11945 short_compare = 1;
bf8949d1 11946 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11947 {
11948 if (TREE_CODE (op0) == ADDR_EXPR
a64030fe 11949 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11950 && !from_macro_expansion_at (location))
bf8949d1 11951 {
11952 if (code == EQ_EXPR)
11953 warning_at (location,
11954 OPT_Waddress,
11955 "the comparison will always evaluate as %<false%> "
11956 "for the address of %qD will never be NULL",
11957 TREE_OPERAND (op0, 0));
11958 else
11959 warning_at (location,
11960 OPT_Waddress,
11961 "the comparison will always evaluate as %<true%> "
11962 "for the address of %qD will never be NULL",
11963 TREE_OPERAND (op0, 0));
11964 }
11965 result_type = type0;
11966 }
11967 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11968 {
11969 if (TREE_CODE (op1) == ADDR_EXPR
a64030fe 11970 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11971 && !from_macro_expansion_at (location))
bf8949d1 11972 {
11973 if (code == EQ_EXPR)
11974 warning_at (location,
6cf89e04 11975 OPT_Waddress,
bf8949d1 11976 "the comparison will always evaluate as %<false%> "
11977 "for the address of %qD will never be NULL",
11978 TREE_OPERAND (op1, 0));
11979 else
11980 warning_at (location,
11981 OPT_Waddress,
11982 "the comparison will always evaluate as %<true%> "
11983 "for the address of %qD will never be NULL",
11984 TREE_OPERAND (op1, 0));
11985 }
11986 result_type = type1;
11987 }
276beea2 11988 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11989 {
11990 tree tt0 = TREE_TYPE (type0);
11991 tree tt1 = TREE_TYPE (type1);
6d5d708e 11992 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11993 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11994 addr_space_t as_common = ADDR_SPACE_GENERIC;
11995
276beea2 11996 /* Anything compares with void *. void * compares with anything.
11997 Otherwise, the targets must be compatible
11998 and both must be object or both incomplete. */
d5b637fa 11999 if (comp_target_types (location, type0, type1))
1ea7fa60 12000 result_type = common_pointer_type (type0, type1);
6d5d708e 12001 else if (!addr_space_superset (as0, as1, &as_common))
12002 {
12003 error_at (location, "comparison of pointers to "
12004 "disjoint address spaces");
12005 return error_mark_node;
12006 }
b560fabd 12007 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
5e4a4d15 12008 {
6d5d708e 12009 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
29438999 12010 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
8864917d 12011 "comparison of %<void *%> with function pointer");
5e4a4d15 12012 }
b560fabd 12013 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
0ad236c2 12014 {
6d5d708e 12015 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
29438999 12016 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
8864917d 12017 "comparison of %<void *%> with function pointer");
0ad236c2 12018 }
276beea2 12019 else
34d3c5de 12020 /* Avoid warning about the volatile ObjC EH puts on decls. */
12021 if (!objc_ok)
8e70fb09 12022 pedwarn (location, 0,
21ca8540 12023 "comparison of distinct pointer types lacks a cast");
0ad236c2 12024
276beea2 12025 if (result_type == NULL_TREE)
6d5d708e 12026 {
12027 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12028 result_type = build_pointer_type
12029 (build_qualified_type (void_type_node, qual));
12030 }
0ad236c2 12031 }
276beea2 12032 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
fc698c9d 12033 {
276beea2 12034 result_type = type0;
8e70fb09 12035 pedwarn (location, 0, "comparison between pointer and integer");
fc698c9d 12036 }
276beea2 12037 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
989bbcfc 12038 {
276beea2 12039 result_type = type1;
8e70fb09 12040 pedwarn (location, 0, "comparison between pointer and integer");
989bbcfc 12041 }
78bf4156 12042 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12043 || truth_value_p (TREE_CODE (orig_op0)))
12044 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12045 || truth_value_p (TREE_CODE (orig_op1))))
12046 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
276beea2 12047 break;
989bbcfc 12048
276beea2 12049 case LE_EXPR:
12050 case GE_EXPR:
12051 case LT_EXPR:
12052 case GT_EXPR:
d7ad16c2 12053 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
12054 {
12055 tree intt;
73437615 12056 if (!vector_types_compatible_elements_p (type0, type1))
d7ad16c2 12057 {
12058 error_at (location, "comparing vectors with different "
12059 "element types");
12060 return error_mark_node;
12061 }
12062
f08ee65f 12063 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12064 TYPE_VECTOR_SUBPARTS (type1)))
d7ad16c2 12065 {
12066 error_at (location, "comparing vectors with different "
12067 "number of elements");
12068 return error_mark_node;
12069 }
12070
fee5e491 12071 /* It's not precisely specified how the usual arithmetic
12072 conversions apply to the vector types. Here, we use
12073 the unsigned type if one of the operands is signed and
12074 the other one is unsigned. */
12075 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12076 {
12077 if (!TYPE_UNSIGNED (type0))
12078 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12079 else
12080 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12081 warning_at (location, OPT_Wsign_compare, "comparison between "
12082 "types %qT and %qT", type0, type1);
12083 }
12084
d7ad16c2 12085 /* Always construct signed integer vector type. */
12086 intt = c_common_type_for_size (GET_MODE_BITSIZE
3d2b0034 12087 (SCALAR_TYPE_MODE
12088 (TREE_TYPE (type0))), 0);
86f1f5d1 12089 if (!intt)
12090 {
12091 error_at (location, "could not find an integer type "
12092 "of the same size as %qT",
12093 TREE_TYPE (type0));
12094 return error_mark_node;
12095 }
d7ad16c2 12096 result_type = build_opaque_vector_type (intt,
12097 TYPE_VECTOR_SUBPARTS (type0));
12098 converted = 1;
1f137e6d 12099 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12100 goto return_build_binary_op;
d7ad16c2 12101 }
276beea2 12102 build_type = integer_type_node;
9421ebb9 12103 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12104 || code0 == FIXED_POINT_TYPE)
12105 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12106 || code1 == FIXED_POINT_TYPE))
276beea2 12107 short_compare = 1;
12108 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12109 {
6d5d708e 12110 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12111 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12112 addr_space_t as_common;
12113
d5b637fa 12114 if (comp_target_types (location, type0, type1))
276beea2 12115 {
1ea7fa60 12116 result_type = common_pointer_type (type0, type1);
276beea2 12117 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12118 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
8e70fb09 12119 pedwarn (location, 0,
21ca8540 12120 "comparison of complete and incomplete pointers");
8864917d 12121 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
29438999 12122 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
8864917d 12123 "ordered comparisons of pointers to functions");
cdd6b2f1 12124 else if (null_pointer_constant_p (orig_op0)
12125 || null_pointer_constant_p (orig_op1))
12126 warning_at (location, OPT_Wextra,
12127 "ordered comparison of pointer with null pointer");
12128
276beea2 12129 }
6d5d708e 12130 else if (!addr_space_superset (as0, as1, &as_common))
12131 {
12132 error_at (location, "comparison of pointers to "
12133 "disjoint address spaces");
12134 return error_mark_node;
12135 }
276beea2 12136 else
12137 {
6d5d708e 12138 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12139 result_type = build_pointer_type
12140 (build_qualified_type (void_type_node, qual));
8e70fb09 12141 pedwarn (location, 0,
21ca8540 12142 "comparison of distinct pointer types lacks a cast");
276beea2 12143 }
12144 }
79f925ed 12145 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
276beea2 12146 {
12147 result_type = type0;
8864917d 12148 if (pedantic)
29438999 12149 pedwarn (location, OPT_Wpedantic,
8864917d 12150 "ordered comparison of pointer with integer zero");
12151 else if (extra_warnings)
8e70fb09 12152 warning_at (location, OPT_Wextra,
cdd6b2f1 12153 "ordered comparison of pointer with integer zero");
276beea2 12154 }
79f925ed 12155 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
276beea2 12156 {
12157 result_type = type1;
cdd6b2f1 12158 if (pedantic)
29438999 12159 pedwarn (location, OPT_Wpedantic,
cdd6b2f1 12160 "ordered comparison of pointer with integer zero");
12161 else if (extra_warnings)
12162 warning_at (location, OPT_Wextra,
12163 "ordered comparison of pointer with integer zero");
276beea2 12164 }
12165 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12166 {
12167 result_type = type0;
8e70fb09 12168 pedwarn (location, 0, "comparison between pointer and integer");
276beea2 12169 }
12170 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12171 {
12172 result_type = type1;
8e70fb09 12173 pedwarn (location, 0, "comparison between pointer and integer");
276beea2 12174 }
eabe2d94 12175
12176 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12177 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12178 {
12179 op0 = save_expr (op0);
12180 op1 = save_expr (op1);
12181
12182 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12183 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12184 }
12185
78bf4156 12186 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12187 || truth_value_p (TREE_CODE (orig_op0)))
12188 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12189 || truth_value_p (TREE_CODE (orig_op1))))
12190 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
276beea2 12191 break;
7cad36f4 12192
276beea2 12193 default:
315ba355 12194 gcc_unreachable ();
f1929933 12195 }
e41f0d80 12196
d1b92730 12197 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12198 return error_mark_node;
12199
491255f5 12200 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
12201 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
73437615 12202 || !vector_types_compatible_elements_p (type0, type1)))
491255f5 12203 {
c0bf500c 12204 gcc_rich_location richloc (location);
b7bb5264 12205 maybe_range_label_for_tree_type_mismatch
12206 label_for_op0 (orig_op0, orig_op1),
12207 label_for_op1 (orig_op1, orig_op0);
12208 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12209 richloc.maybe_add_expr (orig_op1, &label_for_op1);
c0bf500c 12210 binary_op_error (&richloc, code, type0, type1);
491255f5 12211 return error_mark_node;
12212 }
12213
276beea2 12214 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9421ebb9 12215 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
276beea2 12216 &&
12217 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9421ebb9 12218 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
628fa4f8 12219 {
0f241d3f 12220 bool first_complex = (code0 == COMPLEX_TYPE);
12221 bool second_complex = (code1 == COMPLEX_TYPE);
12222 int none_complex = (!first_complex && !second_complex);
4ec54c99 12223
276beea2 12224 if (shorten || common || short_compare)
441df341 12225 {
12226 result_type = c_common_type (type0, type1);
c920faa3 12227 do_warn_double_promotion (result_type, type0, type1,
12228 "implicit conversion from %qT to %qT "
12229 "to match other operand of binary "
12230 "expression",
12231 location);
441df341 12232 if (result_type == error_mark_node)
12233 return error_mark_node;
12234 }
628fa4f8 12235
0f241d3f 12236 if (first_complex != second_complex
12237 && (code == PLUS_EXPR
12238 || code == MINUS_EXPR
12239 || code == MULT_EXPR
12240 || (code == TRUNC_DIV_EXPR && first_complex))
12241 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12242 && flag_signed_zeros)
12243 {
12244 /* An operation on mixed real/complex operands must be
12245 handled specially, but the language-independent code can
12246 more easily optimize the plain complex arithmetic if
12247 -fno-signed-zeros. */
12248 tree real_type = TREE_TYPE (result_type);
12249 tree real, imag;
12250 if (type0 != orig_type0 || type1 != orig_type1)
12251 {
12252 gcc_assert (may_need_excess_precision && common);
8f45ab4d 12253 semantic_result_type = c_common_type (orig_type0, orig_type1);
0f241d3f 12254 }
12255 if (first_complex)
12256 {
12257 if (TREE_TYPE (op0) != result_type)
22a75734 12258 op0 = convert_and_check (location, result_type, op0);
0f241d3f 12259 if (TREE_TYPE (op1) != real_type)
22a75734 12260 op1 = convert_and_check (location, real_type, op1);
0f241d3f 12261 }
12262 else
12263 {
12264 if (TREE_TYPE (op0) != real_type)
22a75734 12265 op0 = convert_and_check (location, real_type, op0);
0f241d3f 12266 if (TREE_TYPE (op1) != result_type)
22a75734 12267 op1 = convert_and_check (location, result_type, op1);
0f241d3f 12268 }
12269 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12270 return error_mark_node;
12271 if (first_complex)
12272 {
d0869ea4 12273 op0 = save_expr (op0);
0f241d3f 12274 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
b99cc6da 12275 op0, true);
0f241d3f 12276 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
b99cc6da 12277 op0, true);
0f241d3f 12278 switch (code)
12279 {
12280 case MULT_EXPR:
12281 case TRUNC_DIV_EXPR:
d0869ea4 12282 op1 = save_expr (op1);
0f241d3f 12283 imag = build2 (resultcode, real_type, imag, op1);
12284 /* Fall through. */
12285 case PLUS_EXPR:
12286 case MINUS_EXPR:
12287 real = build2 (resultcode, real_type, real, op1);
12288 break;
12289 default:
12290 gcc_unreachable();
12291 }
12292 }
12293 else
12294 {
d0869ea4 12295 op1 = save_expr (op1);
0f241d3f 12296 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
b99cc6da 12297 op1, true);
0f241d3f 12298 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
b99cc6da 12299 op1, true);
0f241d3f 12300 switch (code)
12301 {
12302 case MULT_EXPR:
d0869ea4 12303 op0 = save_expr (op0);
0f241d3f 12304 imag = build2 (resultcode, real_type, op0, imag);
12305 /* Fall through. */
12306 case PLUS_EXPR:
12307 real = build2 (resultcode, real_type, op0, real);
12308 break;
12309 case MINUS_EXPR:
12310 real = build2 (resultcode, real_type, op0, real);
12311 imag = build1 (NEGATE_EXPR, real_type, imag);
12312 break;
12313 default:
12314 gcc_unreachable();
12315 }
12316 }
12317 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12318 goto return_build_binary_op;
12319 }
12320
276beea2 12321 /* For certain operations (which identify themselves by shorten != 0)
12322 if both args were extended from the same smaller type,
12323 do the arithmetic in that type and then extend.
628fa4f8 12324
276beea2 12325 shorten !=0 and !=1 indicates a bitwise operation.
12326 For them, this optimization is safe only if
12327 both args are zero-extended or both are sign-extended.
12328 Otherwise, we might change the result.
12329 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12330 but calculated in (unsigned short) it would be (unsigned short)-1. */
628fa4f8 12331
276beea2 12332 if (shorten && none_complex)
12333 {
276beea2 12334 final_type = result_type;
48e1416a 12335 result_type = shorten_binary_op (result_type, op0, op1,
2561cea2 12336 shorten == -1);
276beea2 12337 }
30cd880f 12338
276beea2 12339 /* Shifts can be shortened if shifting right. */
2b30d46c 12340
276beea2 12341 if (short_shift)
12342 {
12343 int unsigned_arg;
12344 tree arg0 = get_narrower (op0, &unsigned_arg);
30cd880f 12345
276beea2 12346 final_type = result_type;
17677b84 12347
276beea2 12348 if (arg0 == op0 && final_type == TREE_TYPE (op0))
78a8ed03 12349 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
0dbd1c74 12350
276beea2 12351 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9e072631 12352 && tree_int_cst_sgn (op1) > 0
276beea2 12353 /* We can shorten only if the shift count is less than the
12354 number of bits in the smaller type size. */
12355 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12356 /* We cannot drop an unsigned shift after sign-extension. */
78a8ed03 12357 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
276beea2 12358 {
12359 /* Do an unsigned shift if the operand was zero-extended. */
12360 result_type
12361 = c_common_signed_or_unsigned_type (unsigned_arg,
12362 TREE_TYPE (arg0));
12363 /* Convert value-to-be-shifted to that type. */
12364 if (TREE_TYPE (op0) != result_type)
12365 op0 = convert (result_type, op0);
12366 converted = 1;
17677b84 12367 }
30cd880f 12368 }
12369
276beea2 12370 /* Comparison operations are shortened too but differently.
12371 They identify themselves by setting short_compare = 1. */
225ec6aa 12372
276beea2 12373 if (short_compare)
12374 {
12375 /* Don't write &op0, etc., because that would prevent op0
12376 from being kept in a register.
12377 Instead, make copies of the our local variables and
12378 pass the copies by reference, then copy them back afterward. */
12379 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12380 enum tree_code xresultcode = resultcode;
12381 tree val
2623625f 12382 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12383 &xresultcode);
e41f0d80 12384
72749341 12385 if (val != NULL_TREE)
b6889cb0 12386 {
12387 ret = val;
12388 goto return_build_binary_op;
12389 }
e41f0d80 12390
276beea2 12391 op0 = xop0, op1 = xop1;
12392 converted = 1;
12393 resultcode = xresultcode;
e41f0d80 12394
48d94ede 12395 if (c_inhibit_evaluation_warnings == 0)
a75b1c71 12396 {
12397 bool op0_maybe_const = true;
12398 bool op1_maybe_const = true;
12399 tree orig_op0_folded, orig_op1_folded;
12400
12401 if (in_late_binary_op)
12402 {
12403 orig_op0_folded = orig_op0;
12404 orig_op1_folded = orig_op1;
12405 }
12406 else
12407 {
12408 /* Fold for the sake of possible warnings, as in
12409 build_conditional_expr. This requires the
12410 "original" values to be folded, not just op0 and
12411 op1. */
672d914b 12412 c_inhibit_evaluation_warnings++;
a75b1c71 12413 op0 = c_fully_fold (op0, require_constant_value,
12414 &op0_maybe_const);
12415 op1 = c_fully_fold (op1, require_constant_value,
12416 &op1_maybe_const);
672d914b 12417 c_inhibit_evaluation_warnings--;
a75b1c71 12418 orig_op0_folded = c_fully_fold (orig_op0,
12419 require_constant_value,
12420 NULL);
12421 orig_op1_folded = c_fully_fold (orig_op1,
12422 require_constant_value,
12423 NULL);
12424 }
12425
12426 if (warn_sign_compare)
12427 warn_for_sign_compare (location, orig_op0_folded,
12428 orig_op1_folded, op0, op1,
12429 result_type, resultcode);
4095a8c8 12430 if (!in_late_binary_op && !int_operands)
a75b1c71 12431 {
12432 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
f59e3889 12433 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
a75b1c71 12434 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
f59e3889 12435 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
a75b1c71 12436 }
276beea2 12437 }
736393e5 12438 }
7cad36f4 12439 }
7cad36f4 12440
276beea2 12441 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12442 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12443 Then the expression will be built.
12444 It will be given type FINAL_TYPE if that is nonzero;
12445 otherwise, it will be given type RESULT_TYPE. */
628fa4f8 12446
276beea2 12447 if (!result_type)
12448 {
fbfc389d 12449 /* Favor showing any expression locations that are available. */
12450 op_location_t oploc (location, UNKNOWN_LOCATION);
12451 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
c0bf500c 12452 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
276beea2 12453 return error_mark_node;
12454 }
628fa4f8 12455
276beea2 12456 if (build_type == NULL_TREE)
c6418a4e 12457 {
12458 build_type = result_type;
16a547ac 12459 if ((type0 != orig_type0 || type1 != orig_type1)
12460 && !boolean_op)
c6418a4e 12461 {
12462 gcc_assert (may_need_excess_precision && common);
8f45ab4d 12463 semantic_result_type = c_common_type (orig_type0, orig_type1);
c6418a4e 12464 }
12465 }
628fa4f8 12466
8f45ab4d 12467 if (!converted)
12468 {
22a75734 12469 op0 = ep_convert_and_check (location, result_type, op0,
12470 semantic_result_type);
12471 op1 = ep_convert_and_check (location, result_type, op1,
12472 semantic_result_type);
8f45ab4d 12473
12474 /* This can happen if one operand has a vector type, and the other
12475 has a different type. */
12476 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12477 return error_mark_node;
12478 }
12479
9917317a 12480 if (sanitize_flags_p ((SANITIZE_SHIFT
12481 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
dadc219c 12482 && current_function_decl != NULL_TREE
e96292e9 12483 && (doing_div_or_mod || doing_shift)
12484 && !require_constant_value)
9e46467d 12485 {
12486 /* OP0 and/or OP1 might have side-effects. */
d0869ea4 12487 op0 = save_expr (op0);
12488 op1 = save_expr (op1);
9e46467d 12489 op0 = c_fully_fold (op0, false, NULL);
12490 op1 = c_fully_fold (op1, false, NULL);
9917317a 12491 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12492 | SANITIZE_FLOAT_DIVIDE))))
9e46467d 12493 instrument_expr = ubsan_instrument_division (location, op0, op1);
9917317a 12494 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
9e46467d 12495 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12496 }
12497
b6889cb0 12498 /* Treat expressions in initializers specially as they can't trap. */
a75b1c71 12499 if (int_const_or_overflow)
12500 ret = (require_constant_value
389dd41b 12501 ? fold_build2_initializer_loc (location, resultcode, build_type,
12502 op0, op1)
12503 : fold_build2_loc (location, resultcode, build_type, op0, op1));
a75b1c71 12504 else
12505 ret = build2 (resultcode, build_type, op0, op1);
72749341 12506 if (final_type != NULL_TREE)
b6889cb0 12507 ret = convert (final_type, ret);
12508
12509 return_build_binary_op:
12510 gcc_assert (ret != error_mark_node);
a75b1c71 12511 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12512 ret = (int_operands
12513 ? note_integer_operands (ret)
12514 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12515 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12516 && !in_late_binary_op)
12517 ret = note_integer_operands (ret);
b6889cb0 12518 protected_set_expr_location (ret, location);
9e46467d 12519
1b309377 12520 if (instrument_expr != NULL)
9e46467d 12521 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12522 instrument_expr, ret);
12523
048100df 12524 if (semantic_result_type)
12525 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12526 semantic_result_type, ret);
12527
b6889cb0 12528 return ret;
628fa4f8 12529}
45a78cc0 12530
12531
12532/* Convert EXPR to be a truth-value, validating its type for this
8e70fb09 12533 purpose. LOCATION is the source location for the expression. */
45a78cc0 12534
12535tree
8e70fb09 12536c_objc_common_truthvalue_conversion (location_t location, tree expr)
45a78cc0 12537{
a75b1c71 12538 bool int_const, int_operands;
12539
45a78cc0 12540 switch (TREE_CODE (TREE_TYPE (expr)))
12541 {
12542 case ARRAY_TYPE:
8e70fb09 12543 error_at (location, "used array that cannot be converted to pointer where scalar is required");
45a78cc0 12544 return error_mark_node;
12545
12546 case RECORD_TYPE:
8e70fb09 12547 error_at (location, "used struct type value where scalar is required");
45a78cc0 12548 return error_mark_node;
12549
12550 case UNION_TYPE:
8e70fb09 12551 error_at (location, "used union type value where scalar is required");
45a78cc0 12552 return error_mark_node;
12553
a43fc97e 12554 case VOID_TYPE:
12555 error_at (location, "void value not ignored as it ought to be");
12556 return error_mark_node;
12557
547c6b1f 12558 case POINTER_TYPE:
12559 if (reject_gcc_builtin (expr))
12560 return error_mark_node;
12561 break;
12562
f14c8207 12563 case FUNCTION_TYPE:
12564 gcc_unreachable ();
12565
d7ad16c2 12566 case VECTOR_TYPE:
12567 error_at (location, "used vector type where scalar is required");
12568 return error_mark_node;
12569
45a78cc0 12570 default:
12571 break;
12572 }
12573
a75b1c71 12574 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12575 int_operands = EXPR_INT_CONST_OPERANDS (expr);
bb554051 12576 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12577 {
12578 expr = remove_c_maybe_const_expr (expr);
12579 expr = build2 (NE_EXPR, integer_type_node, expr,
12580 convert (TREE_TYPE (expr), integer_zero_node));
12581 expr = note_integer_operands (expr);
12582 }
12583 else
12584 /* ??? Should we also give an error for vectors rather than leaving
12585 those to give errors later? */
12586 expr = c_common_truthvalue_conversion (location, expr);
a75b1c71 12587
12588 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12589 {
12590 if (TREE_OVERFLOW (expr))
12591 return expr;
12592 else
12593 return note_integer_operands (expr);
12594 }
12595 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12596 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12597 return expr;
45a78cc0 12598}
54d7165a 12599\f
12600
12601/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12602 required. */
12603
12604tree
c7d4e749 12605c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
54d7165a 12606{
12607 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12608 {
12609 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12610 /* Executing a compound literal inside a function reinitializes
12611 it. */
12612 if (!TREE_STATIC (decl))
12613 *se = true;
12614 return decl;
12615 }
12616 else
12617 return expr;
12618}
1e8e9920 12619\f
2c4c8725 12620/* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12621 statement. LOC is the location of the construct. */
ca4c3545 12622
12623tree
2c4c8725 12624c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12625 tree clauses)
ca4c3545 12626{
2c4c8725 12627 body = c_end_compound_stmt (loc, body, true);
ca4c3545 12628
2c4c8725 12629 tree stmt = make_node (code);
ca4c3545 12630 TREE_TYPE (stmt) = void_type_node;
2c4c8725 12631 OMP_BODY (stmt) = body;
12632 OMP_CLAUSES (stmt) = clauses;
ca4c3545 12633 SET_EXPR_LOCATION (stmt, loc);
12634
12635 return add_stmt (stmt);
12636}
12637
12638/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12639 statement. LOC is the location of the OACC_DATA. */
12640
12641tree
12642c_finish_oacc_data (location_t loc, tree clauses, tree block)
12643{
12644 tree stmt;
12645
12646 block = c_end_compound_stmt (loc, block, true);
12647
12648 stmt = make_node (OACC_DATA);
12649 TREE_TYPE (stmt) = void_type_node;
12650 OACC_DATA_CLAUSES (stmt) = clauses;
12651 OACC_DATA_BODY (stmt) = block;
12652 SET_EXPR_LOCATION (stmt, loc);
12653
12654 return add_stmt (stmt);
12655}
12656
571b3486 12657/* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12658 statement. LOC is the location of the OACC_HOST_DATA. */
12659
12660tree
12661c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12662{
12663 tree stmt;
12664
12665 block = c_end_compound_stmt (loc, block, true);
12666
12667 stmt = make_node (OACC_HOST_DATA);
12668 TREE_TYPE (stmt) = void_type_node;
12669 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12670 OACC_HOST_DATA_BODY (stmt) = block;
12671 SET_EXPR_LOCATION (stmt, loc);
12672
12673 return add_stmt (stmt);
12674}
12675
334ec2d8 12676/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
1e8e9920 12677
12678tree
12679c_begin_omp_parallel (void)
12680{
12681 tree block;
12682
12683 keep_next_level ();
12684 block = c_begin_compound_stmt (true);
12685
12686 return block;
12687}
12688
e60a6f7b 12689/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12690 statement. LOC is the location of the OMP_PARALLEL. */
fd6481cf 12691
1e8e9920 12692tree
e60a6f7b 12693c_finish_omp_parallel (location_t loc, tree clauses, tree block)
1e8e9920 12694{
12695 tree stmt;
12696
e60a6f7b 12697 block = c_end_compound_stmt (loc, block, true);
1e8e9920 12698
12699 stmt = make_node (OMP_PARALLEL);
12700 TREE_TYPE (stmt) = void_type_node;
12701 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12702 OMP_PARALLEL_BODY (stmt) = block;
e60a6f7b 12703 SET_EXPR_LOCATION (stmt, loc);
1e8e9920 12704
12705 return add_stmt (stmt);
12706}
12707
fd6481cf 12708/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12709
12710tree
12711c_begin_omp_task (void)
12712{
12713 tree block;
12714
12715 keep_next_level ();
12716 block = c_begin_compound_stmt (true);
12717
12718 return block;
12719}
12720
e60a6f7b 12721/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12722 statement. LOC is the location of the #pragma. */
fd6481cf 12723
12724tree
e60a6f7b 12725c_finish_omp_task (location_t loc, tree clauses, tree block)
fd6481cf 12726{
12727 tree stmt;
12728
e60a6f7b 12729 block = c_end_compound_stmt (loc, block, true);
fd6481cf 12730
12731 stmt = make_node (OMP_TASK);
12732 TREE_TYPE (stmt) = void_type_node;
12733 OMP_TASK_CLAUSES (stmt) = clauses;
12734 OMP_TASK_BODY (stmt) = block;
e60a6f7b 12735 SET_EXPR_LOCATION (stmt, loc);
fd6481cf 12736
12737 return add_stmt (stmt);
12738}
12739
bc7bff74 12740/* Generate GOMP_cancel call for #pragma omp cancel. */
12741
12742void
12743c_finish_omp_cancel (location_t loc, tree clauses)
12744{
12745 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12746 int mask = 0;
4954efd4 12747 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
bc7bff74 12748 mask = 1;
4954efd4 12749 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
bc7bff74 12750 mask = 2;
4954efd4 12751 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
bc7bff74 12752 mask = 4;
4954efd4 12753 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
bc7bff74 12754 mask = 8;
12755 else
12756 {
44e775d8 12757 error_at (loc, "%<#pragma omp cancel%> must specify one of "
bc7bff74 12758 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12759 "clauses");
12760 return;
12761 }
4954efd4 12762 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
bc7bff74 12763 if (ifc != NULL_TREE)
12764 {
7e5a76c8 12765 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
12766 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
12767 error_at (OMP_CLAUSE_LOCATION (ifc),
12768 "expected %<cancel%> %<if%> clause modifier");
b5e76680 12769 else
12770 {
12771 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
12772 if (ifc2 != NULL_TREE)
12773 {
12774 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
12775 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
12776 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
12777 error_at (OMP_CLAUSE_LOCATION (ifc2),
12778 "expected %<cancel%> %<if%> clause modifier");
12779 }
12780 }
7e5a76c8 12781
bc7bff74 12782 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12783 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12784 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12785 build_zero_cst (type));
12786 }
12787 else
12788 ifc = boolean_true_node;
12789 tree stmt = build_call_expr_loc (loc, fn, 2,
12790 build_int_cst (integer_type_node, mask),
12791 ifc);
12792 add_stmt (stmt);
12793}
12794
12795/* Generate GOMP_cancellation_point call for
12796 #pragma omp cancellation point. */
12797
12798void
12799c_finish_omp_cancellation_point (location_t loc, tree clauses)
12800{
12801 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12802 int mask = 0;
4954efd4 12803 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
bc7bff74 12804 mask = 1;
4954efd4 12805 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
bc7bff74 12806 mask = 2;
4954efd4 12807 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
bc7bff74 12808 mask = 4;
4954efd4 12809 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
bc7bff74 12810 mask = 8;
12811 else
12812 {
44e775d8 12813 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
bc7bff74 12814 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12815 "clauses");
12816 return;
12817 }
12818 tree stmt = build_call_expr_loc (loc, fn, 1,
12819 build_int_cst (integer_type_node, mask));
12820 add_stmt (stmt);
12821}
12822
12823/* Helper function for handle_omp_array_sections. Called recursively
12824 to handle multiple array-section-subscripts. C is the clause,
12825 T current expression (initially OMP_CLAUSE_DECL), which is either
12826 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12827 expression if specified, TREE_VALUE length expression if specified,
12828 TREE_CHAIN is what it has been specified after, or some decl.
12829 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12830 set to true if any of the array-section-subscript could have length
12831 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12832 first array-section-subscript which is known not to have length
12833 of one. Given say:
12834 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12835 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12836 all are or may have length of 1, array-section-subscript [:2] is the
43895be5 12837 first one known not to have length 1. For array-section-subscript
bc7bff74 12838 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12839 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12840 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12841 case though, as some lengths could be zero. */
12842
12843static tree
12844handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
43895be5 12845 bool &maybe_zero_len, unsigned int &first_non_one,
6d6a3fc3 12846 enum c_omp_region_type ort)
bc7bff74 12847{
12848 tree ret, low_bound, length, type;
12849 if (TREE_CODE (t) != TREE_LIST)
12850 {
224bdd51 12851 if (error_operand_p (t))
bc7bff74 12852 return error_mark_node;
43895be5 12853 ret = t;
0b80c4b2 12854 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12855 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12856 {
12857 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12858 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12859 return error_mark_node;
12860 }
43895be5 12861 if (TREE_CODE (t) == COMPONENT_REF
6d6a3fc3 12862 && ort == C_ORT_OMP
43895be5 12863 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12864 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12865 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12866 {
12867 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12868 {
12869 error_at (OMP_CLAUSE_LOCATION (c),
12870 "bit-field %qE in %qs clause",
12871 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12872 return error_mark_node;
12873 }
12874 while (TREE_CODE (t) == COMPONENT_REF)
12875 {
12876 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12877 {
12878 error_at (OMP_CLAUSE_LOCATION (c),
12879 "%qE is a member of a union", t);
12880 return error_mark_node;
12881 }
12882 t = TREE_OPERAND (t, 0);
12883 }
12884 }
f48c7f4a 12885 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
bc7bff74 12886 {
12887 if (DECL_P (t))
12888 error_at (OMP_CLAUSE_LOCATION (c),
12889 "%qD is not a variable in %qs clause", t,
12890 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12891 else
12892 error_at (OMP_CLAUSE_LOCATION (c),
12893 "%qE is not a variable in %qs clause", t,
12894 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12895 return error_mark_node;
12896 }
12897 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
0b80c4b2 12898 && TYPE_ATOMIC (TREE_TYPE (t)))
12899 {
12900 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12901 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12902 return error_mark_node;
12903 }
12904 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12905 && VAR_P (t)
12906 && DECL_THREAD_LOCAL_P (t))
bc7bff74 12907 {
12908 error_at (OMP_CLAUSE_LOCATION (c),
12909 "%qD is threadprivate variable in %qs clause", t,
12910 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12911 return error_mark_node;
12912 }
0b80c4b2 12913 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12914 && TYPE_ATOMIC (TREE_TYPE (t))
12915 && POINTER_TYPE_P (TREE_TYPE (t)))
12916 {
12917 /* If the array section is pointer based and the pointer
12918 itself is _Atomic qualified, we need to atomically load
12919 the pointer. */
12920 c_expr expr;
12921 memset (&expr, 0, sizeof (expr));
12922 expr.value = ret;
12923 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12924 expr, false, false);
12925 ret = expr.value;
12926 }
43895be5 12927 return ret;
bc7bff74 12928 }
12929
12930 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
6d6a3fc3 12931 maybe_zero_len, first_non_one, ort);
bc7bff74 12932 if (ret == error_mark_node || ret == NULL_TREE)
12933 return ret;
12934
12935 type = TREE_TYPE (ret);
12936 low_bound = TREE_PURPOSE (t);
12937 length = TREE_VALUE (t);
12938
12939 if (low_bound == error_mark_node || length == error_mark_node)
12940 return error_mark_node;
12941
12942 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12943 {
12944 error_at (OMP_CLAUSE_LOCATION (c),
12945 "low bound %qE of array section does not have integral type",
12946 low_bound);
12947 return error_mark_node;
12948 }
12949 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12950 {
12951 error_at (OMP_CLAUSE_LOCATION (c),
12952 "length %qE of array section does not have integral type",
12953 length);
12954 return error_mark_node;
12955 }
12956 if (low_bound
12957 && TREE_CODE (low_bound) == INTEGER_CST
12958 && TYPE_PRECISION (TREE_TYPE (low_bound))
12959 > TYPE_PRECISION (sizetype))
12960 low_bound = fold_convert (sizetype, low_bound);
12961 if (length
12962 && TREE_CODE (length) == INTEGER_CST
12963 && TYPE_PRECISION (TREE_TYPE (length))
12964 > TYPE_PRECISION (sizetype))
12965 length = fold_convert (sizetype, length);
12966 if (low_bound == NULL_TREE)
12967 low_bound = integer_zero_node;
12968
12969 if (length != NULL_TREE)
12970 {
12971 if (!integer_nonzerop (length))
43895be5 12972 {
12973 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7e5a76c8 12974 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12975 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
12976 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
43895be5 12977 {
12978 if (integer_zerop (length))
12979 {
12980 error_at (OMP_CLAUSE_LOCATION (c),
12981 "zero length array section in %qs clause",
12982 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12983 return error_mark_node;
12984 }
12985 }
12986 else
12987 maybe_zero_len = true;
12988 }
bc7bff74 12989 if (first_non_one == types.length ()
12990 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12991 first_non_one++;
12992 }
12993 if (TREE_CODE (type) == ARRAY_TYPE)
12994 {
12995 if (length == NULL_TREE
12996 && (TYPE_DOMAIN (type) == NULL_TREE
12997 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12998 {
12999 error_at (OMP_CLAUSE_LOCATION (c),
13000 "for unknown bound array type length expression must "
13001 "be specified");
13002 return error_mark_node;
13003 }
13004 if (TREE_CODE (low_bound) == INTEGER_CST
13005 && tree_int_cst_sgn (low_bound) == -1)
13006 {
13007 error_at (OMP_CLAUSE_LOCATION (c),
13008 "negative low bound in array section in %qs clause",
13009 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13010 return error_mark_node;
13011 }
13012 if (length != NULL_TREE
13013 && TREE_CODE (length) == INTEGER_CST
13014 && tree_int_cst_sgn (length) == -1)
13015 {
13016 error_at (OMP_CLAUSE_LOCATION (c),
13017 "negative length in array section in %qs clause",
13018 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13019 return error_mark_node;
13020 }
13021 if (TYPE_DOMAIN (type)
13022 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13023 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13024 == INTEGER_CST)
13025 {
37ce62dc 13026 tree size
13027 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13028 size = size_binop (PLUS_EXPR, size, size_one_node);
bc7bff74 13029 if (TREE_CODE (low_bound) == INTEGER_CST)
13030 {
13031 if (tree_int_cst_lt (size, low_bound))
13032 {
13033 error_at (OMP_CLAUSE_LOCATION (c),
13034 "low bound %qE above array section size "
13035 "in %qs clause", low_bound,
13036 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13037 return error_mark_node;
13038 }
13039 if (tree_int_cst_equal (size, low_bound))
43895be5 13040 {
13041 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7e5a76c8 13042 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13043 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13044 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
43895be5 13045 {
13046 error_at (OMP_CLAUSE_LOCATION (c),
13047 "zero length array section in %qs clause",
13048 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13049 return error_mark_node;
13050 }
13051 maybe_zero_len = true;
13052 }
bc7bff74 13053 else if (length == NULL_TREE
13054 && first_non_one == types.length ()
13055 && tree_int_cst_equal
13056 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13057 low_bound))
13058 first_non_one++;
13059 }
13060 else if (length == NULL_TREE)
13061 {
43895be5 13062 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
7e5a76c8 13063 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13064 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13065 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
43895be5 13066 maybe_zero_len = true;
bc7bff74 13067 if (first_non_one == types.length ())
13068 first_non_one++;
13069 }
13070 if (length && TREE_CODE (length) == INTEGER_CST)
13071 {
13072 if (tree_int_cst_lt (size, length))
13073 {
13074 error_at (OMP_CLAUSE_LOCATION (c),
13075 "length %qE above array section size "
13076 "in %qs clause", length,
13077 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13078 return error_mark_node;
13079 }
13080 if (TREE_CODE (low_bound) == INTEGER_CST)
13081 {
13082 tree lbpluslen
13083 = size_binop (PLUS_EXPR,
13084 fold_convert (sizetype, low_bound),
13085 fold_convert (sizetype, length));
13086 if (TREE_CODE (lbpluslen) == INTEGER_CST
13087 && tree_int_cst_lt (size, lbpluslen))
13088 {
13089 error_at (OMP_CLAUSE_LOCATION (c),
13090 "high bound %qE above array section size "
13091 "in %qs clause", lbpluslen,
13092 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13093 return error_mark_node;
13094 }
13095 }
13096 }
13097 }
13098 else if (length == NULL_TREE)
13099 {
43895be5 13100 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
7e5a76c8 13101 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13102 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13103 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
43895be5 13104 maybe_zero_len = true;
bc7bff74 13105 if (first_non_one == types.length ())
13106 first_non_one++;
13107 }
13108
13109 /* For [lb:] we will need to evaluate lb more than once. */
13110 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13111 {
d0869ea4 13112 tree lb = save_expr (low_bound);
bc7bff74 13113 if (lb != low_bound)
13114 {
13115 TREE_PURPOSE (t) = lb;
13116 low_bound = lb;
13117 }
13118 }
13119 }
13120 else if (TREE_CODE (type) == POINTER_TYPE)
13121 {
13122 if (length == NULL_TREE)
13123 {
13124 error_at (OMP_CLAUSE_LOCATION (c),
13125 "for pointer type length expression must be specified");
13126 return error_mark_node;
13127 }
43895be5 13128 if (length != NULL_TREE
13129 && TREE_CODE (length) == INTEGER_CST
13130 && tree_int_cst_sgn (length) == -1)
13131 {
13132 error_at (OMP_CLAUSE_LOCATION (c),
13133 "negative length in array section in %qs clause",
13134 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13135 return error_mark_node;
13136 }
bc7bff74 13137 /* If there is a pointer type anywhere but in the very first
13138 array-section-subscript, the array section can't be contiguous. */
13139 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13140 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13141 {
13142 error_at (OMP_CLAUSE_LOCATION (c),
13143 "array section is not contiguous in %qs clause",
13144 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13145 return error_mark_node;
13146 }
13147 }
13148 else
13149 {
13150 error_at (OMP_CLAUSE_LOCATION (c),
13151 "%qE does not have pointer or array type", ret);
13152 return error_mark_node;
13153 }
13154 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13155 types.safe_push (TREE_TYPE (ret));
13156 /* We will need to evaluate lb more than once. */
d0869ea4 13157 tree lb = save_expr (low_bound);
bc7bff74 13158 if (lb != low_bound)
13159 {
13160 TREE_PURPOSE (t) = lb;
13161 low_bound = lb;
13162 }
13163 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13164 return ret;
13165}
13166
13167/* Handle array sections for clause C. */
13168
13169static bool
6d6a3fc3 13170handle_omp_array_sections (tree c, enum c_omp_region_type ort)
bc7bff74 13171{
13172 bool maybe_zero_len = false;
13173 unsigned int first_non_one = 0;
43895be5 13174 auto_vec<tree, 10> types;
7e5a76c8 13175 tree *tp = &OMP_CLAUSE_DECL (c);
13176 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13177 && TREE_CODE (*tp) == TREE_LIST
13178 && TREE_PURPOSE (*tp)
13179 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13180 tp = &TREE_VALUE (*tp);
13181 tree first = handle_omp_array_sections_1 (c, *tp, types,
43895be5 13182 maybe_zero_len, first_non_one,
6d6a3fc3 13183 ort);
bc7bff74 13184 if (first == error_mark_node)
43895be5 13185 return true;
bc7bff74 13186 if (first == NULL_TREE)
43895be5 13187 return false;
bc7bff74 13188 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13189 {
7e5a76c8 13190 tree t = *tp;
bc7bff74 13191 tree tem = NULL_TREE;
bc7bff74 13192 /* Need to evaluate side effects in the length expressions
13193 if any. */
13194 while (TREE_CODE (t) == TREE_LIST)
13195 {
13196 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13197 {
13198 if (tem == NULL_TREE)
13199 tem = TREE_VALUE (t);
13200 else
13201 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13202 TREE_VALUE (t), tem);
13203 }
13204 t = TREE_CHAIN (t);
13205 }
13206 if (tem)
13207 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
69cd03b2 13208 first = c_fully_fold (first, false, NULL, true);
7e5a76c8 13209 *tp = first;
bc7bff74 13210 }
13211 else
13212 {
13213 unsigned int num = types.length (), i;
13214 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13215 tree condition = NULL_TREE;
13216
13217 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13218 maybe_zero_len = true;
13219
13220 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13221 t = TREE_CHAIN (t))
13222 {
13223 tree low_bound = TREE_PURPOSE (t);
13224 tree length = TREE_VALUE (t);
13225
13226 i--;
13227 if (low_bound
13228 && TREE_CODE (low_bound) == INTEGER_CST
13229 && TYPE_PRECISION (TREE_TYPE (low_bound))
13230 > TYPE_PRECISION (sizetype))
13231 low_bound = fold_convert (sizetype, low_bound);
13232 if (length
13233 && TREE_CODE (length) == INTEGER_CST
13234 && TYPE_PRECISION (TREE_TYPE (length))
13235 > TYPE_PRECISION (sizetype))
13236 length = fold_convert (sizetype, length);
13237 if (low_bound == NULL_TREE)
13238 low_bound = integer_zero_node;
13239 if (!maybe_zero_len && i > first_non_one)
13240 {
13241 if (integer_nonzerop (low_bound))
13242 goto do_warn_noncontiguous;
13243 if (length != NULL_TREE
13244 && TREE_CODE (length) == INTEGER_CST
13245 && TYPE_DOMAIN (types[i])
13246 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13247 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13248 == INTEGER_CST)
13249 {
13250 tree size;
13251 size = size_binop (PLUS_EXPR,
13252 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13253 size_one_node);
13254 if (!tree_int_cst_equal (length, size))
13255 {
13256 do_warn_noncontiguous:
13257 error_at (OMP_CLAUSE_LOCATION (c),
13258 "array section is not contiguous in %qs "
13259 "clause",
13260 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
bc7bff74 13261 return true;
13262 }
13263 }
13264 if (length != NULL_TREE
13265 && TREE_SIDE_EFFECTS (length))
13266 {
13267 if (side_effects == NULL_TREE)
13268 side_effects = length;
13269 else
13270 side_effects = build2 (COMPOUND_EXPR,
13271 TREE_TYPE (side_effects),
13272 length, side_effects);
13273 }
13274 }
13275 else
13276 {
13277 tree l;
13278
43895be5 13279 if (i > first_non_one
13280 && ((length && integer_nonzerop (length))
7e5a76c8 13281 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13282 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13283 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
bc7bff74 13284 continue;
13285 if (length)
13286 l = fold_convert (sizetype, length);
13287 else
13288 {
13289 l = size_binop (PLUS_EXPR,
13290 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13291 size_one_node);
13292 l = size_binop (MINUS_EXPR, l,
13293 fold_convert (sizetype, low_bound));
13294 }
13295 if (i > first_non_one)
13296 {
13297 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13298 size_zero_node);
13299 if (condition == NULL_TREE)
13300 condition = l;
13301 else
13302 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13303 l, condition);
13304 }
13305 else if (size == NULL_TREE)
13306 {
13307 size = size_in_bytes (TREE_TYPE (types[i]));
43895be5 13308 tree eltype = TREE_TYPE (types[num - 1]);
13309 while (TREE_CODE (eltype) == ARRAY_TYPE)
13310 eltype = TREE_TYPE (eltype);
7e5a76c8 13311 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13312 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13313 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
43895be5 13314 {
13315 if (integer_zerop (size)
13316 || integer_zerop (size_in_bytes (eltype)))
13317 {
13318 error_at (OMP_CLAUSE_LOCATION (c),
13319 "zero length array section in %qs clause",
13320 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13321 return error_mark_node;
13322 }
13323 size = size_binop (EXACT_DIV_EXPR, size,
13324 size_in_bytes (eltype));
13325 }
bc7bff74 13326 size = size_binop (MULT_EXPR, size, l);
13327 if (condition)
13328 size = fold_build3 (COND_EXPR, sizetype, condition,
13329 size, size_zero_node);
13330 }
13331 else
13332 size = size_binop (MULT_EXPR, size, l);
13333 }
13334 }
bc7bff74 13335 if (side_effects)
13336 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
7e5a76c8 13337 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13338 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13339 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
43895be5 13340 {
13341 size = size_binop (MINUS_EXPR, size, size_one_node);
13342 size = c_fully_fold (size, false, NULL);
7e5a76c8 13343 size = save_expr (size);
43895be5 13344 tree index_type = build_index_type (size);
13345 tree eltype = TREE_TYPE (first);
13346 while (TREE_CODE (eltype) == ARRAY_TYPE)
13347 eltype = TREE_TYPE (eltype);
13348 tree type = build_array_type (eltype, index_type);
13349 tree ptype = build_pointer_type (eltype);
13350 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13351 t = build_fold_addr_expr (t);
9561765e 13352 tree t2 = build_fold_addr_expr (first);
13353 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13354 ptrdiff_type_node, t2);
13355 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13356 ptrdiff_type_node, t2,
13357 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13358 ptrdiff_type_node, t));
13359 t2 = c_fully_fold (t2, false, NULL);
13360 if (tree_fits_shwi_p (t2))
13361 t = build2 (MEM_REF, type, t,
13362 build_int_cst (ptype, tree_to_shwi (t2)));
13363 else
13364 {
13365 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13366 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13367 TREE_TYPE (t), t, t2);
13368 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13369 }
43895be5 13370 OMP_CLAUSE_DECL (c) = t;
13371 return false;
13372 }
bc7bff74 13373 first = c_fully_fold (first, false, NULL);
13374 OMP_CLAUSE_DECL (c) = first;
13375 if (size)
13376 size = c_fully_fold (size, false, NULL);
13377 OMP_CLAUSE_SIZE (c) = size;
43895be5 13378 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13379 || (TREE_CODE (t) == COMPONENT_REF
13380 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
bc7bff74 13381 return false;
ca4c3545 13382 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
6d6a3fc3 13383 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
43895be5 13384 switch (OMP_CLAUSE_MAP_KIND (c))
13385 {
13386 case GOMP_MAP_ALLOC:
13387 case GOMP_MAP_TO:
13388 case GOMP_MAP_FROM:
13389 case GOMP_MAP_TOFROM:
13390 case GOMP_MAP_ALWAYS_TO:
13391 case GOMP_MAP_ALWAYS_FROM:
13392 case GOMP_MAP_ALWAYS_TOFROM:
13393 case GOMP_MAP_RELEASE:
13394 case GOMP_MAP_DELETE:
0ef9358d 13395 case GOMP_MAP_FORCE_TO:
13396 case GOMP_MAP_FORCE_FROM:
13397 case GOMP_MAP_FORCE_TOFROM:
13398 case GOMP_MAP_FORCE_PRESENT:
43895be5 13399 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13400 break;
13401 default:
13402 break;
13403 }
bc7bff74 13404 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
6d6a3fc3 13405 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
9561765e 13406 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13407 else if (TREE_CODE (t) == COMPONENT_REF)
13408 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
13409 else
13410 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13411 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13412 && !c_mark_addressable (t))
bc7bff74 13413 return false;
13414 OMP_CLAUSE_DECL (c2) = t;
13415 t = build_fold_addr_expr (first);
13416 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13417 tree ptr = OMP_CLAUSE_DECL (c2);
13418 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13419 ptr = build_fold_addr_expr (ptr);
13420 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13421 ptrdiff_type_node, t,
13422 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13423 ptrdiff_type_node, ptr));
13424 t = c_fully_fold (t, false, NULL);
13425 OMP_CLAUSE_SIZE (c2) = t;
13426 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13427 OMP_CLAUSE_CHAIN (c) = c2;
13428 }
13429 return false;
13430}
13431
13432/* Helper function of finish_omp_clauses. Clone STMT as if we were making
13433 an inline call. But, remap
13434 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13435 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13436
13437static tree
13438c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13439 tree decl, tree placeholder)
13440{
13441 copy_body_data id;
06ecf488 13442 hash_map<tree, tree> decl_map;
bc7bff74 13443
06ecf488 13444 decl_map.put (omp_decl1, placeholder);
13445 decl_map.put (omp_decl2, decl);
bc7bff74 13446 memset (&id, 0, sizeof (id));
13447 id.src_fn = DECL_CONTEXT (omp_decl1);
13448 id.dst_fn = current_function_decl;
13449 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
06ecf488 13450 id.decl_map = &decl_map;
bc7bff74 13451
13452 id.copy_decl = copy_decl_no_change;
13453 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13454 id.transform_new_cfg = true;
13455 id.transform_return_to_modify = false;
13456 id.transform_lang_insert_block = NULL;
13457 id.eh_lp_nr = 0;
13458 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
bc7bff74 13459 return stmt;
13460}
13461
13462/* Helper function of c_finish_omp_clauses, called via walk_tree.
13463 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13464
13465static tree
13466c_find_omp_placeholder_r (tree *tp, int *, void *data)
13467{
13468 if (*tp == (tree) data)
13469 return *tp;
13470 return NULL_TREE;
13471}
13472
7e5a76c8 13473/* Similarly, but also walk aggregate fields. */
13474
13475struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13476
13477static tree
13478c_find_omp_var_r (tree *tp, int *, void *data)
13479{
13480 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13481 return *tp;
13482 if (RECORD_OR_UNION_TYPE_P (*tp))
13483 {
13484 tree field;
13485 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13486
13487 for (field = TYPE_FIELDS (*tp); field;
13488 field = DECL_CHAIN (field))
13489 if (TREE_CODE (field) == FIELD_DECL)
13490 {
13491 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13492 c_find_omp_var_r, data, pset);
13493 if (ret)
13494 return ret;
13495 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13496 if (ret)
13497 return ret;
13498 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13499 pset);
13500 if (ret)
13501 return ret;
13502 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13503 if (ret)
13504 return ret;
13505 }
13506 }
13507 else if (INTEGRAL_TYPE_P (*tp))
13508 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13509 ((struct c_find_omp_var_s *) data)->pset);
13510 return NULL_TREE;
13511}
13512
13513/* Finish OpenMP iterators ITER. Return true if they are errorneous
13514 and clauses containing them should be removed. */
13515
13516static bool
13517c_omp_finish_iterators (tree iter)
13518{
13519 bool ret = false;
13520 for (tree it = iter; it; it = TREE_CHAIN (it))
13521 {
13522 tree var = TREE_VEC_ELT (it, 0);
13523 tree begin = TREE_VEC_ELT (it, 1);
13524 tree end = TREE_VEC_ELT (it, 2);
13525 tree step = TREE_VEC_ELT (it, 3);
13526 tree orig_step;
13527 tree type = TREE_TYPE (var);
13528 location_t loc = DECL_SOURCE_LOCATION (var);
13529 if (type == error_mark_node)
13530 {
13531 ret = true;
13532 continue;
13533 }
13534 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13535 {
13536 error_at (loc, "iterator %qD has neither integral nor pointer type",
13537 var);
13538 ret = true;
13539 continue;
13540 }
13541 else if (TYPE_ATOMIC (type))
13542 {
13543 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13544 ret = true;
13545 continue;
13546 }
13547 else if (TYPE_READONLY (type))
13548 {
13549 error_at (loc, "iterator %qD has const qualified type", var);
13550 ret = true;
13551 continue;
13552 }
13553 else if (step == error_mark_node
13554 || TREE_TYPE (step) == error_mark_node)
13555 {
13556 ret = true;
13557 continue;
13558 }
13559 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13560 {
13561 error_at (EXPR_LOC_OR_LOC (step, loc),
13562 "iterator step with non-integral type");
13563 ret = true;
13564 continue;
13565 }
13566 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13567 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13568 orig_step = save_expr (c_fully_fold (step, false, NULL));
13569 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13570 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13571 if (POINTER_TYPE_P (type))
13572 {
13573 begin = save_expr (begin);
13574 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13575 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13576 fold_convert (sizetype, step),
13577 fold_convert (sizetype, begin));
13578 step = fold_convert (ssizetype, step);
13579 }
13580 if (integer_zerop (step))
13581 {
13582 error_at (loc, "iterator %qD has zero step", var);
13583 ret = true;
13584 continue;
13585 }
13586
13587 if (begin == error_mark_node
13588 || end == error_mark_node
13589 || step == error_mark_node
13590 || orig_step == error_mark_node)
13591 {
13592 ret = true;
13593 continue;
13594 }
13595 hash_set<tree> pset;
13596 tree it2;
13597 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13598 {
13599 tree var2 = TREE_VEC_ELT (it2, 0);
13600 tree begin2 = TREE_VEC_ELT (it2, 1);
13601 tree end2 = TREE_VEC_ELT (it2, 2);
13602 tree step2 = TREE_VEC_ELT (it2, 3);
13603 tree type2 = TREE_TYPE (var2);
13604 location_t loc2 = DECL_SOURCE_LOCATION (var2);
13605 struct c_find_omp_var_s data = { var, &pset };
13606 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13607 {
13608 error_at (loc2,
13609 "type of iterator %qD refers to outer iterator %qD",
13610 var2, var);
13611 break;
13612 }
13613 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13614 {
13615 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13616 "begin expression refers to outer iterator %qD", var);
13617 break;
13618 }
13619 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13620 {
13621 error_at (EXPR_LOC_OR_LOC (end2, loc2),
13622 "end expression refers to outer iterator %qD", var);
13623 break;
13624 }
13625 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
13626 {
13627 error_at (EXPR_LOC_OR_LOC (step2, loc2),
13628 "step expression refers to outer iterator %qD", var);
13629 break;
13630 }
13631 }
13632 if (it2)
13633 {
13634 ret = true;
13635 continue;
13636 }
13637 TREE_VEC_ELT (it, 1) = begin;
13638 TREE_VEC_ELT (it, 2) = end;
13639 TREE_VEC_ELT (it, 3) = step;
13640 TREE_VEC_ELT (it, 4) = orig_step;
13641 }
13642 return ret;
13643}
13644
ca4c3545 13645/* For all elements of CLAUSES, validate them against their constraints.
1e8e9920 13646 Remove any elements from the list that are invalid. */
13647
13648tree
b5e88f74 13649c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
1e8e9920 13650{
13651 bitmap_head generic_head, firstprivate_head, lastprivate_head;
6d6a3fc3 13652 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
43895be5 13653 tree c, t, type, *pc;
13654 tree simdlen = NULL_TREE, safelen = NULL_TREE;
bc7bff74 13655 bool branch_seen = false;
13656 bool copyprivate_seen = false;
9561765e 13657 bool linear_variable_step_check = false;
bc7bff74 13658 tree *nowait_clause = NULL;
9561765e 13659 bool ordered_seen = false;
13660 tree schedule_clause = NULL_TREE;
2234363c 13661 bool oacc_async = false;
7e5a76c8 13662 tree last_iterators = NULL_TREE;
13663 bool last_iterators_remove = false;
13664 tree *nogroup_seen = NULL;
13665 bool reduction_seen = false;
1e8e9920 13666
13667 bitmap_obstack_initialize (NULL);
13668 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13669 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13670 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
bc7bff74 13671 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
7e5a76c8 13672 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
43895be5 13673 bitmap_initialize (&map_head, &bitmap_default_obstack);
13674 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
7e5a76c8 13675 /* If ort == C_ORT_OMP used as nontemporal_head instead. */
6d6a3fc3 13676 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
1e8e9920 13677
2234363c 13678 if (ort & C_ORT_ACC)
13679 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13680 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13681 {
13682 oacc_async = true;
13683 break;
13684 }
0b80c4b2 13685
1e8e9920 13686 for (pc = &clauses, c = clauses; c ; c = *pc)
13687 {
13688 bool remove = false;
13689 bool need_complete = false;
13690 bool need_implicitly_determined = false;
13691
55d6e7cd 13692 switch (OMP_CLAUSE_CODE (c))
1e8e9920 13693 {
13694 case OMP_CLAUSE_SHARED:
1e8e9920 13695 need_implicitly_determined = true;
13696 goto check_dup_generic;
13697
13698 case OMP_CLAUSE_PRIVATE:
1e8e9920 13699 need_complete = true;
13700 need_implicitly_determined = true;
13701 goto check_dup_generic;
13702
13703 case OMP_CLAUSE_REDUCTION:
7e5a76c8 13704 reduction_seen = true;
13705 /* FALLTHRU */
13706 case OMP_CLAUSE_IN_REDUCTION:
13707 case OMP_CLAUSE_TASK_REDUCTION:
1e8e9920 13708 need_implicitly_determined = true;
13709 t = OMP_CLAUSE_DECL (c);
43895be5 13710 if (TREE_CODE (t) == TREE_LIST)
13711 {
6d6a3fc3 13712 if (handle_omp_array_sections (c, ort))
43895be5 13713 {
13714 remove = true;
13715 break;
13716 }
13717
13718 t = OMP_CLAUSE_DECL (c);
13719 }
22a3f7bd 13720 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
43895be5 13721 if (t == error_mark_node)
13722 {
13723 remove = true;
13724 break;
13725 }
2234363c 13726 if (oacc_async)
13727 c_mark_addressable (t);
43895be5 13728 type = TREE_TYPE (t);
13729 if (TREE_CODE (t) == MEM_REF)
13730 type = TREE_TYPE (type);
13731 if (TREE_CODE (type) == ARRAY_TYPE)
13732 {
13733 tree oatype = type;
13734 gcc_assert (TREE_CODE (t) != MEM_REF);
13735 while (TREE_CODE (type) == ARRAY_TYPE)
13736 type = TREE_TYPE (type);
13737 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13738 {
13739 error_at (OMP_CLAUSE_LOCATION (c),
13740 "%qD in %<reduction%> clause is a zero size array",
13741 t);
13742 remove = true;
13743 break;
13744 }
13745 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13746 TYPE_SIZE_UNIT (type));
13747 if (integer_zerop (size))
13748 {
13749 error_at (OMP_CLAUSE_LOCATION (c),
13750 "%qD in %<reduction%> clause is a zero size array",
13751 t);
13752 remove = true;
13753 break;
13754 }
13755 size = size_binop (MINUS_EXPR, size, size_one_node);
7e5a76c8 13756 size = save_expr (size);
43895be5 13757 tree index_type = build_index_type (size);
13758 tree atype = build_array_type (type, index_type);
13759 tree ptype = build_pointer_type (type);
13760 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13761 t = build_fold_addr_expr (t);
13762 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13763 OMP_CLAUSE_DECL (c) = t;
13764 }
0b80c4b2 13765 if (TYPE_ATOMIC (type))
13766 {
13767 error_at (OMP_CLAUSE_LOCATION (c),
13768 "%<_Atomic%> %qE in %<reduction%> clause", t);
13769 remove = true;
13770 break;
13771 }
7e5a76c8 13772 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13773 || OMP_CLAUSE_REDUCTION_TASK (c))
13774 {
13775 /* Disallow zero sized or potentially zero sized task
13776 reductions. */
13777 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13778 {
13779 error_at (OMP_CLAUSE_LOCATION (c),
13780 "zero sized type %qT in %qs clause", type,
13781 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13782 remove = true;
13783 break;
13784 }
13785 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
13786 {
13787 error_at (OMP_CLAUSE_LOCATION (c),
13788 "variable sized type %qT in %qs clause", type,
13789 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13790 remove = true;
13791 break;
13792 }
13793 }
bc7bff74 13794 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
43895be5 13795 && (FLOAT_TYPE_P (type)
13796 || TREE_CODE (type) == COMPLEX_TYPE))
1e8e9920 13797 {
13798 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13799 const char *r_name = NULL;
13800
13801 switch (r_code)
13802 {
13803 case PLUS_EXPR:
13804 case MULT_EXPR:
13805 case MINUS_EXPR:
d6779b5f 13806 break;
2169f33b 13807 case MIN_EXPR:
43895be5 13808 if (TREE_CODE (type) == COMPLEX_TYPE)
d6779b5f 13809 r_name = "min";
13810 break;
2169f33b 13811 case MAX_EXPR:
43895be5 13812 if (TREE_CODE (type) == COMPLEX_TYPE)
d6779b5f 13813 r_name = "max";
1e8e9920 13814 break;
13815 case BIT_AND_EXPR:
13816 r_name = "&";
13817 break;
13818 case BIT_XOR_EXPR:
13819 r_name = "^";
13820 break;
13821 case BIT_IOR_EXPR:
13822 r_name = "|";
13823 break;
13824 case TRUTH_ANDIF_EXPR:
43895be5 13825 if (FLOAT_TYPE_P (type))
d6779b5f 13826 r_name = "&&";
1e8e9920 13827 break;
13828 case TRUTH_ORIF_EXPR:
43895be5 13829 if (FLOAT_TYPE_P (type))
d6779b5f 13830 r_name = "||";
1e8e9920 13831 break;
13832 default:
13833 gcc_unreachable ();
13834 }
13835 if (r_name)
13836 {
e60a6f7b 13837 error_at (OMP_CLAUSE_LOCATION (c),
13838 "%qE has invalid type for %<reduction(%s)%>",
13839 t, r_name);
1e8e9920 13840 remove = true;
df205251 13841 break;
1e8e9920 13842 }
13843 }
bc7bff74 13844 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13845 {
13846 error_at (OMP_CLAUSE_LOCATION (c),
43895be5 13847 "user defined reduction not found for %qE", t);
bc7bff74 13848 remove = true;
df205251 13849 break;
bc7bff74 13850 }
13851 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13852 {
13853 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
43895be5 13854 type = TYPE_MAIN_VARIANT (type);
bc7bff74 13855 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13856 VAR_DECL, NULL_TREE, type);
43895be5 13857 tree decl_placeholder = NULL_TREE;
bc7bff74 13858 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13859 DECL_ARTIFICIAL (placeholder) = 1;
13860 DECL_IGNORED_P (placeholder) = 1;
43895be5 13861 if (TREE_CODE (t) == MEM_REF)
13862 {
13863 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13864 VAR_DECL, NULL_TREE, type);
13865 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13866 DECL_ARTIFICIAL (decl_placeholder) = 1;
13867 DECL_IGNORED_P (decl_placeholder) = 1;
13868 }
bc7bff74 13869 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13870 c_mark_addressable (placeholder);
13871 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
43895be5 13872 c_mark_addressable (decl_placeholder ? decl_placeholder
13873 : OMP_CLAUSE_DECL (c));
bc7bff74 13874 OMP_CLAUSE_REDUCTION_MERGE (c)
13875 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13876 TREE_VEC_ELT (list, 0),
13877 TREE_VEC_ELT (list, 1),
43895be5 13878 decl_placeholder ? decl_placeholder
13879 : OMP_CLAUSE_DECL (c), placeholder);
bc7bff74 13880 OMP_CLAUSE_REDUCTION_MERGE (c)
13881 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13882 void_type_node, NULL_TREE,
43895be5 13883 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
bc7bff74 13884 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13885 if (TREE_VEC_LENGTH (list) == 6)
13886 {
13887 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
43895be5 13888 c_mark_addressable (decl_placeholder ? decl_placeholder
13889 : OMP_CLAUSE_DECL (c));
bc7bff74 13890 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13891 c_mark_addressable (placeholder);
13892 tree init = TREE_VEC_ELT (list, 5);
13893 if (init == error_mark_node)
13894 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13895 OMP_CLAUSE_REDUCTION_INIT (c)
13896 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13897 TREE_VEC_ELT (list, 3),
43895be5 13898 decl_placeholder ? decl_placeholder
13899 : OMP_CLAUSE_DECL (c), placeholder);
bc7bff74 13900 if (TREE_VEC_ELT (list, 5) == error_mark_node)
43895be5 13901 {
13902 tree v = decl_placeholder ? decl_placeholder : t;
13903 OMP_CLAUSE_REDUCTION_INIT (c)
13904 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13905 OMP_CLAUSE_REDUCTION_INIT (c));
13906 }
bc7bff74 13907 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13908 c_find_omp_placeholder_r,
13909 placeholder, NULL))
13910 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13911 }
13912 else
13913 {
13914 tree init;
43895be5 13915 tree v = decl_placeholder ? decl_placeholder : t;
13916 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13917 init = build_constructor (TREE_TYPE (v), NULL);
bc7bff74 13918 else
43895be5 13919 init = fold_convert (TREE_TYPE (v), integer_zero_node);
bc7bff74 13920 OMP_CLAUSE_REDUCTION_INIT (c)
43895be5 13921 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
bc7bff74 13922 }
13923 OMP_CLAUSE_REDUCTION_INIT (c)
13924 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13925 void_type_node, NULL_TREE,
13926 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13927 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13928 }
43895be5 13929 if (TREE_CODE (t) == MEM_REF)
13930 {
13931 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13932 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13933 != INTEGER_CST)
13934 {
13935 sorry ("variable length element type in array "
13936 "%<reduction%> clause");
13937 remove = true;
13938 break;
13939 }
13940 t = TREE_OPERAND (t, 0);
9561765e 13941 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13942 t = TREE_OPERAND (t, 0);
43895be5 13943 if (TREE_CODE (t) == ADDR_EXPR)
13944 t = TREE_OPERAND (t, 0);
13945 }
13946 goto check_dup_generic_t;
1e8e9920 13947
13948 case OMP_CLAUSE_COPYPRIVATE:
bc7bff74 13949 copyprivate_seen = true;
13950 if (nowait_clause)
13951 {
13952 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13953 "%<nowait%> clause must not be used together "
13954 "with %<copyprivate%>");
13955 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13956 nowait_clause = NULL;
13957 }
1e8e9920 13958 goto check_dup_generic;
13959
13960 case OMP_CLAUSE_COPYIN:
1e8e9920 13961 t = OMP_CLAUSE_DECL (c);
f48c7f4a 13962 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
1e8e9920 13963 {
e60a6f7b 13964 error_at (OMP_CLAUSE_LOCATION (c),
13965 "%qE must be %<threadprivate%> for %<copyin%>", t);
1e8e9920 13966 remove = true;
df205251 13967 break;
1e8e9920 13968 }
13969 goto check_dup_generic;
13970
bc7bff74 13971 case OMP_CLAUSE_LINEAR:
b5e88f74 13972 if (ort != C_ORT_OMP_DECLARE_SIMD)
43895be5 13973 need_implicitly_determined = true;
bc7bff74 13974 t = OMP_CLAUSE_DECL (c);
b5e88f74 13975 if (ort != C_ORT_OMP_DECLARE_SIMD
43895be5 13976 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13977 {
13978 error_at (OMP_CLAUSE_LOCATION (c),
13979 "modifier should not be specified in %<linear%> "
13980 "clause on %<simd%> or %<for%> constructs");
13981 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13982 }
efa02472 13983 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13984 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
bc7bff74 13985 {
efa02472 13986 error_at (OMP_CLAUSE_LOCATION (c),
13987 "linear clause applied to non-integral non-pointer "
13988 "variable with type %qT", TREE_TYPE (t));
13989 remove = true;
13990 break;
70574e60 13991 }
efa02472 13992 if (TYPE_ATOMIC (TREE_TYPE (t)))
70574e60 13993 {
efa02472 13994 error_at (OMP_CLAUSE_LOCATION (c),
7e5a76c8 13995 "%<_Atomic%> %qD in %<linear%> clause", t);
efa02472 13996 remove = true;
13997 break;
bc7bff74 13998 }
b5e88f74 13999 if (ort == C_ORT_OMP_DECLARE_SIMD)
9561765e 14000 {
14001 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14002 if (TREE_CODE (s) == PARM_DECL)
14003 {
14004 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14005 /* map_head bitmap is used as uniform_head if
14006 declare_simd. */
14007 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14008 linear_variable_step_check = true;
14009 goto check_dup_generic;
14010 }
14011 if (TREE_CODE (s) != INTEGER_CST)
14012 {
14013 error_at (OMP_CLAUSE_LOCATION (c),
14014 "%<linear%> clause step %qE is neither constant "
14015 "nor a parameter", s);
14016 remove = true;
14017 break;
14018 }
14019 }
bc7bff74 14020 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14021 {
14022 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14023 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14024 OMP_CLAUSE_DECL (c), s);
14025 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
844cece0 14026 sizetype, fold_convert (sizetype, s),
14027 fold_convert
14028 (sizetype, OMP_CLAUSE_DECL (c)));
bc7bff74 14029 if (s == error_mark_node)
14030 s = size_one_node;
14031 OMP_CLAUSE_LINEAR_STEP (c) = s;
14032 }
9580cb79 14033 else
14034 OMP_CLAUSE_LINEAR_STEP (c)
14035 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
bc7bff74 14036 goto check_dup_generic;
14037
1e8e9920 14038 check_dup_generic:
14039 t = OMP_CLAUSE_DECL (c);
43895be5 14040 check_dup_generic_t:
f48c7f4a 14041 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
1e8e9920 14042 {
e60a6f7b 14043 error_at (OMP_CLAUSE_LOCATION (c),
bc7bff74 14044 "%qE is not a variable in clause %qs", t,
14045 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
1e8e9920 14046 remove = true;
14047 }
6d6a3fc3 14048 else if (ort == C_ORT_ACC
14049 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14050 {
14051 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14052 {
7e5a76c8 14053 error_at (OMP_CLAUSE_LOCATION (c),
14054 "%qD appears more than once in reduction clauses",
14055 t);
6d6a3fc3 14056 remove = true;
14057 }
14058 else
14059 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14060 }
1e8e9920 14061 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14062 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14063 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14064 {
e60a6f7b 14065 error_at (OMP_CLAUSE_LOCATION (c),
14066 "%qE appears more than once in data clauses", t);
1e8e9920 14067 remove = true;
14068 }
9561765e 14069 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14070 && bitmap_bit_p (&map_head, DECL_UID (t)))
14071 {
6d6a3fc3 14072 if (ort == C_ORT_ACC)
7e5a76c8 14073 error_at (OMP_CLAUSE_LOCATION (c),
14074 "%qD appears more than once in data clauses", t);
6d6a3fc3 14075 else
7e5a76c8 14076 error_at (OMP_CLAUSE_LOCATION (c),
14077 "%qD appears both in data and map clauses", t);
9561765e 14078 remove = true;
14079 }
1e8e9920 14080 else
14081 bitmap_set_bit (&generic_head, DECL_UID (t));
14082 break;
14083
14084 case OMP_CLAUSE_FIRSTPRIVATE:
1e8e9920 14085 t = OMP_CLAUSE_DECL (c);
14086 need_complete = true;
14087 need_implicitly_determined = true;
f48c7f4a 14088 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
1e8e9920 14089 {
e60a6f7b 14090 error_at (OMP_CLAUSE_LOCATION (c),
14091 "%qE is not a variable in clause %<firstprivate%>", t);
1e8e9920 14092 remove = true;
14093 }
14094 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14095 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14096 {
e60a6f7b 14097 error_at (OMP_CLAUSE_LOCATION (c),
14098 "%qE appears more than once in data clauses", t);
1e8e9920 14099 remove = true;
14100 }
9561765e 14101 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14102 {
6d6a3fc3 14103 if (ort == C_ORT_ACC)
7e5a76c8 14104 error_at (OMP_CLAUSE_LOCATION (c),
14105 "%qD appears more than once in data clauses", t);
6d6a3fc3 14106 else
7e5a76c8 14107 error_at (OMP_CLAUSE_LOCATION (c),
14108 "%qD appears both in data and map clauses", t);
9561765e 14109 remove = true;
14110 }
1e8e9920 14111 else
14112 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14113 break;
14114
14115 case OMP_CLAUSE_LASTPRIVATE:
1e8e9920 14116 t = OMP_CLAUSE_DECL (c);
14117 need_complete = true;
14118 need_implicitly_determined = true;
f48c7f4a 14119 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
1e8e9920 14120 {
e60a6f7b 14121 error_at (OMP_CLAUSE_LOCATION (c),
14122 "%qE is not a variable in clause %<lastprivate%>", t);
1e8e9920 14123 remove = true;
14124 }
14125 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14126 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14127 {
e60a6f7b 14128 error_at (OMP_CLAUSE_LOCATION (c),
14129 "%qE appears more than once in data clauses", t);
1e8e9920 14130 remove = true;
14131 }
14132 else
14133 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14134 break;
14135
bc7bff74 14136 case OMP_CLAUSE_ALIGNED:
14137 t = OMP_CLAUSE_DECL (c);
f48c7f4a 14138 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
bc7bff74 14139 {
14140 error_at (OMP_CLAUSE_LOCATION (c),
14141 "%qE is not a variable in %<aligned%> clause", t);
14142 remove = true;
14143 }
23871d0c 14144 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14145 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14146 {
14147 error_at (OMP_CLAUSE_LOCATION (c),
14148 "%qE in %<aligned%> clause is neither a pointer nor "
14149 "an array", t);
14150 remove = true;
14151 }
0b80c4b2 14152 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14153 {
14154 error_at (OMP_CLAUSE_LOCATION (c),
14155 "%<_Atomic%> %qD in %<aligned%> clause", t);
14156 remove = true;
14157 break;
14158 }
bc7bff74 14159 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14160 {
14161 error_at (OMP_CLAUSE_LOCATION (c),
14162 "%qE appears more than once in %<aligned%> clauses",
14163 t);
14164 remove = true;
14165 }
14166 else
14167 bitmap_set_bit (&aligned_head, DECL_UID (t));
14168 break;
14169
7e5a76c8 14170 case OMP_CLAUSE_NONTEMPORAL:
14171 t = OMP_CLAUSE_DECL (c);
14172 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14173 {
14174 error_at (OMP_CLAUSE_LOCATION (c),
14175 "%qE is not a variable in %<nontemporal%> clause", t);
14176 remove = true;
14177 }
14178 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14179 {
14180 error_at (OMP_CLAUSE_LOCATION (c),
14181 "%qE appears more than once in %<nontemporal%> "
14182 "clauses", t);
14183 remove = true;
14184 }
14185 else
14186 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14187 break;
14188
bc7bff74 14189 case OMP_CLAUSE_DEPEND:
14190 t = OMP_CLAUSE_DECL (c);
43895be5 14191 if (t == NULL_TREE)
14192 {
14193 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14194 == OMP_CLAUSE_DEPEND_SOURCE);
14195 break;
14196 }
14197 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14198 {
14199 gcc_assert (TREE_CODE (t) == TREE_LIST);
14200 for (; t; t = TREE_CHAIN (t))
14201 {
14202 tree decl = TREE_VALUE (t);
14203 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14204 {
14205 tree offset = TREE_PURPOSE (t);
e3d0f65c 14206 bool neg = wi::neg_p (wi::to_wide (offset));
43895be5 14207 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14208 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14209 neg ? MINUS_EXPR : PLUS_EXPR,
14210 decl, offset);
14211 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
844cece0 14212 sizetype,
14213 fold_convert (sizetype, t2),
14214 fold_convert (sizetype, decl));
43895be5 14215 if (t2 == error_mark_node)
14216 {
14217 remove = true;
14218 break;
14219 }
14220 TREE_PURPOSE (t) = t2;
14221 }
14222 }
14223 break;
14224 }
7e5a76c8 14225 if (TREE_CODE (t) == TREE_LIST
14226 && TREE_PURPOSE (t)
14227 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14228 {
14229 if (TREE_PURPOSE (t) != last_iterators)
14230 last_iterators_remove
14231 = c_omp_finish_iterators (TREE_PURPOSE (t));
14232 last_iterators = TREE_PURPOSE (t);
14233 t = TREE_VALUE (t);
14234 if (last_iterators_remove)
14235 t = error_mark_node;
14236 }
14237 else
14238 last_iterators = NULL_TREE;
bc7bff74 14239 if (TREE_CODE (t) == TREE_LIST)
14240 {
6d6a3fc3 14241 if (handle_omp_array_sections (c, ort))
bc7bff74 14242 remove = true;
7e5a76c8 14243 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14244 {
14245 error_at (OMP_CLAUSE_LOCATION (c),
14246 "%<depend%> clause with %<depobj%> dependence "
14247 "type on array section");
14248 remove = true;
14249 }
bc7bff74 14250 break;
14251 }
14252 if (t == error_mark_node)
14253 remove = true;
7e5a76c8 14254 else if (!lvalue_p (t))
bc7bff74 14255 {
14256 error_at (OMP_CLAUSE_LOCATION (c),
7e5a76c8 14257 "%qE is not lvalue expression nor array section in "
14258 "%<depend%> clause", t);
bc7bff74 14259 remove = true;
14260 }
7e5a76c8 14261 else if (TREE_CODE (t) == COMPONENT_REF
14262 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14263 {
14264 error_at (OMP_CLAUSE_LOCATION (c),
14265 "bit-field %qE in %qs clause", t, "depend");
14266 remove = true;
14267 }
14268 else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14269 {
14270 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14271 {
14272 error_at (OMP_CLAUSE_LOCATION (c),
14273 "%qE does not have %<omp_depend_t%> type in "
14274 "%<depend%> clause with %<depobj%> dependence "
14275 "type", t);
14276 remove = true;
14277 }
14278 }
14279 else if (c_omp_depend_t_p (TREE_TYPE (t)))
14280 {
14281 error_at (OMP_CLAUSE_LOCATION (c),
14282 "%qE should not have %<omp_depend_t%> type in "
14283 "%<depend%> clause with dependence type other than "
14284 "%<depobj%>", t);
14285 remove = true;
14286 }
14287 if (!remove)
14288 {
14289 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14290 t, false);
14291 if (addr == error_mark_node)
14292 remove = true;
14293 else
14294 {
14295 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14296 RO_UNARY_STAR);
14297 if (t == error_mark_node)
14298 remove = true;
14299 else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14300 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14301 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14302 == TREE_VEC))
14303 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14304 else
14305 OMP_CLAUSE_DECL (c) = t;
14306 }
14307 }
bc7bff74 14308 break;
14309
14310 case OMP_CLAUSE_MAP:
14311 case OMP_CLAUSE_TO:
14312 case OMP_CLAUSE_FROM:
ca4c3545 14313 case OMP_CLAUSE__CACHE_:
bc7bff74 14314 t = OMP_CLAUSE_DECL (c);
14315 if (TREE_CODE (t) == TREE_LIST)
14316 {
6d6a3fc3 14317 if (handle_omp_array_sections (c, ort))
bc7bff74 14318 remove = true;
14319 else
14320 {
14321 t = OMP_CLAUSE_DECL (c);
9ec099a3 14322 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
bc7bff74 14323 {
14324 error_at (OMP_CLAUSE_LOCATION (c),
14325 "array section does not have mappable type "
14326 "in %qs clause",
14327 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14328 remove = true;
14329 }
0b80c4b2 14330 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14331 {
14332 error_at (OMP_CLAUSE_LOCATION (c),
14333 "%<_Atomic%> %qE in %qs clause", t,
14334 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14335 remove = true;
14336 }
43895be5 14337 while (TREE_CODE (t) == ARRAY_REF)
14338 t = TREE_OPERAND (t, 0);
14339 if (TREE_CODE (t) == COMPONENT_REF
14340 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14341 {
14342 while (TREE_CODE (t) == COMPONENT_REF)
14343 t = TREE_OPERAND (t, 0);
14344 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14345 break;
14346 if (bitmap_bit_p (&map_head, DECL_UID (t)))
14347 {
14348 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7e5a76c8 14349 error_at (OMP_CLAUSE_LOCATION (c),
14350 "%qD appears more than once in motion "
14351 "clauses", t);
6d6a3fc3 14352 else if (ort == C_ORT_ACC)
7e5a76c8 14353 error_at (OMP_CLAUSE_LOCATION (c),
14354 "%qD appears more than once in data "
14355 "clauses", t);
43895be5 14356 else
7e5a76c8 14357 error_at (OMP_CLAUSE_LOCATION (c),
14358 "%qD appears more than once in map "
14359 "clauses", t);
43895be5 14360 remove = true;
14361 }
14362 else
14363 {
14364 bitmap_set_bit (&map_head, DECL_UID (t));
14365 bitmap_set_bit (&map_field_head, DECL_UID (t));
14366 }
14367 }
bc7bff74 14368 }
14369 break;
14370 }
14371 if (t == error_mark_node)
43895be5 14372 {
14373 remove = true;
14374 break;
14375 }
14376 if (TREE_CODE (t) == COMPONENT_REF
b5e88f74 14377 && (ort & C_ORT_OMP)
43895be5 14378 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14379 {
14380 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14381 {
14382 error_at (OMP_CLAUSE_LOCATION (c),
14383 "bit-field %qE in %qs clause",
14384 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14385 remove = true;
14386 }
14387 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14388 {
14389 error_at (OMP_CLAUSE_LOCATION (c),
14390 "%qE does not have a mappable type in %qs clause",
14391 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14392 remove = true;
14393 }
0b80c4b2 14394 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14395 {
14396 error_at (OMP_CLAUSE_LOCATION (c),
14397 "%<_Atomic%> %qE in %qs clause", t,
14398 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14399 remove = true;
14400 }
43895be5 14401 while (TREE_CODE (t) == COMPONENT_REF)
14402 {
14403 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14404 == UNION_TYPE)
14405 {
14406 error_at (OMP_CLAUSE_LOCATION (c),
14407 "%qE is a member of a union", t);
14408 remove = true;
14409 break;
14410 }
14411 t = TREE_OPERAND (t, 0);
14412 }
14413 if (remove)
14414 break;
14415 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14416 {
9561765e 14417 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
43895be5 14418 break;
14419 }
14420 }
14421 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
bc7bff74 14422 {
14423 error_at (OMP_CLAUSE_LOCATION (c),
14424 "%qE is not a variable in %qs clause", t,
14425 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14426 remove = true;
14427 }
f48c7f4a 14428 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
bc7bff74 14429 {
14430 error_at (OMP_CLAUSE_LOCATION (c),
14431 "%qD is threadprivate variable in %qs clause", t,
14432 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14433 remove = true;
14434 }
a9833286 14435 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14436 || (OMP_CLAUSE_MAP_KIND (c)
14437 != GOMP_MAP_FIRSTPRIVATE_POINTER))
14438 && !c_mark_addressable (t))
bc7bff74 14439 remove = true;
9ec099a3 14440 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
ca4c3545 14441 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
43895be5 14442 || (OMP_CLAUSE_MAP_KIND (c)
14443 == GOMP_MAP_FIRSTPRIVATE_POINTER)
ca4c3545 14444 || (OMP_CLAUSE_MAP_KIND (c)
14445 == GOMP_MAP_FORCE_DEVICEPTR)))
43895be5 14446 && t == OMP_CLAUSE_DECL (c)
9ec099a3 14447 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
bc7bff74 14448 {
14449 error_at (OMP_CLAUSE_LOCATION (c),
14450 "%qD does not have a mappable type in %qs clause", t,
14451 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14452 remove = true;
14453 }
0b80c4b2 14454 else if (TREE_TYPE (t) == error_mark_node)
14455 remove = true;
14456 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
14457 {
14458 error_at (OMP_CLAUSE_LOCATION (c),
14459 "%<_Atomic%> %qE in %qs clause", t,
14460 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14461 remove = true;
14462 }
43895be5 14463 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14464 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14465 {
14466 if (bitmap_bit_p (&generic_head, DECL_UID (t))
14467 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14468 {
7e5a76c8 14469 error_at (OMP_CLAUSE_LOCATION (c),
14470 "%qD appears more than once in data clauses", t);
43895be5 14471 remove = true;
14472 }
9561765e 14473 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
43895be5 14474 {
6d6a3fc3 14475 if (ort == C_ORT_ACC)
7e5a76c8 14476 error_at (OMP_CLAUSE_LOCATION (c),
14477 "%qD appears more than once in data clauses", t);
6d6a3fc3 14478 else
7e5a76c8 14479 error_at (OMP_CLAUSE_LOCATION (c),
14480 "%qD appears both in data and map clauses", t);
9561765e 14481 remove = true;
43895be5 14482 }
9561765e 14483 else
14484 bitmap_set_bit (&generic_head, DECL_UID (t));
43895be5 14485 }
14486 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
bc7bff74 14487 {
14488 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7e5a76c8 14489 error_at (OMP_CLAUSE_LOCATION (c),
14490 "%qD appears more than once in motion clauses", t);
6d6a3fc3 14491 else if (ort == C_ORT_ACC)
7e5a76c8 14492 error_at (OMP_CLAUSE_LOCATION (c),
14493 "%qD appears more than once in data clauses", t);
bc7bff74 14494 else
7e5a76c8 14495 error_at (OMP_CLAUSE_LOCATION (c),
14496 "%qD appears more than once in map clauses", t);
bc7bff74 14497 remove = true;
14498 }
9561765e 14499 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14500 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14501 {
6d6a3fc3 14502 if (ort == C_ORT_ACC)
7e5a76c8 14503 error_at (OMP_CLAUSE_LOCATION (c),
14504 "%qD appears more than once in data clauses", t);
6d6a3fc3 14505 else
7e5a76c8 14506 error_at (OMP_CLAUSE_LOCATION (c),
14507 "%qD appears both in data and map clauses", t);
9561765e 14508 remove = true;
14509 }
bc7bff74 14510 else
43895be5 14511 {
14512 bitmap_set_bit (&map_head, DECL_UID (t));
14513 if (t != OMP_CLAUSE_DECL (c)
14514 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14515 bitmap_set_bit (&map_field_head, DECL_UID (t));
14516 }
14517 break;
14518
14519 case OMP_CLAUSE_TO_DECLARE:
43895be5 14520 case OMP_CLAUSE_LINK:
14521 t = OMP_CLAUSE_DECL (c);
9561765e 14522 if (TREE_CODE (t) == FUNCTION_DECL
14523 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14524 ;
14525 else if (!VAR_P (t))
43895be5 14526 {
9561765e 14527 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14528 error_at (OMP_CLAUSE_LOCATION (c),
14529 "%qE is neither a variable nor a function name in "
14530 "clause %qs", t,
14531 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14532 else
14533 error_at (OMP_CLAUSE_LOCATION (c),
14534 "%qE is not a variable in clause %qs", t,
14535 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
43895be5 14536 remove = true;
14537 }
14538 else if (DECL_THREAD_LOCAL_P (t))
14539 {
14540 error_at (OMP_CLAUSE_LOCATION (c),
14541 "%qD is threadprivate variable in %qs clause", t,
14542 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14543 remove = true;
14544 }
14545 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14546 {
14547 error_at (OMP_CLAUSE_LOCATION (c),
14548 "%qD does not have a mappable type in %qs clause", t,
14549 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14550 remove = true;
14551 }
9561765e 14552 if (remove)
14553 break;
14554 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
14555 {
14556 error_at (OMP_CLAUSE_LOCATION (c),
14557 "%qE appears more than once on the same "
14558 "%<declare target%> directive", t);
14559 remove = true;
14560 }
14561 else
14562 bitmap_set_bit (&generic_head, DECL_UID (t));
bc7bff74 14563 break;
14564
14565 case OMP_CLAUSE_UNIFORM:
14566 t = OMP_CLAUSE_DECL (c);
14567 if (TREE_CODE (t) != PARM_DECL)
14568 {
14569 if (DECL_P (t))
14570 error_at (OMP_CLAUSE_LOCATION (c),
14571 "%qD is not an argument in %<uniform%> clause", t);
14572 else
14573 error_at (OMP_CLAUSE_LOCATION (c),
14574 "%qE is not an argument in %<uniform%> clause", t);
14575 remove = true;
df205251 14576 break;
bc7bff74 14577 }
9561765e 14578 /* map_head bitmap is used as uniform_head if declare_simd. */
14579 bitmap_set_bit (&map_head, DECL_UID (t));
df205251 14580 goto check_dup_generic;
bc7bff74 14581
43895be5 14582 case OMP_CLAUSE_IS_DEVICE_PTR:
14583 case OMP_CLAUSE_USE_DEVICE_PTR:
14584 t = OMP_CLAUSE_DECL (c);
14585 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
14586 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14587 {
14588 error_at (OMP_CLAUSE_LOCATION (c),
14589 "%qs variable is neither a pointer nor an array",
14590 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14591 remove = true;
14592 }
14593 goto check_dup_generic;
14594
bc7bff74 14595 case OMP_CLAUSE_NOWAIT:
14596 if (copyprivate_seen)
14597 {
14598 error_at (OMP_CLAUSE_LOCATION (c),
14599 "%<nowait%> clause must not be used together "
14600 "with %<copyprivate%>");
14601 remove = true;
14602 break;
14603 }
14604 nowait_clause = pc;
14605 pc = &OMP_CLAUSE_CHAIN (c);
14606 continue;
14607
1e8e9920 14608 case OMP_CLAUSE_IF:
14609 case OMP_CLAUSE_NUM_THREADS:
bc7bff74 14610 case OMP_CLAUSE_NUM_TEAMS:
14611 case OMP_CLAUSE_THREAD_LIMIT:
1e8e9920 14612 case OMP_CLAUSE_DEFAULT:
fd6481cf 14613 case OMP_CLAUSE_UNTIED:
14614 case OMP_CLAUSE_COLLAPSE:
2169f33b 14615 case OMP_CLAUSE_FINAL:
14616 case OMP_CLAUSE_MERGEABLE:
bc7bff74 14617 case OMP_CLAUSE_DEVICE:
14618 case OMP_CLAUSE_DIST_SCHEDULE:
14619 case OMP_CLAUSE_PARALLEL:
14620 case OMP_CLAUSE_FOR:
14621 case OMP_CLAUSE_SECTIONS:
14622 case OMP_CLAUSE_TASKGROUP:
14623 case OMP_CLAUSE_PROC_BIND:
43895be5 14624 case OMP_CLAUSE_PRIORITY:
14625 case OMP_CLAUSE_GRAINSIZE:
14626 case OMP_CLAUSE_NUM_TASKS:
43895be5 14627 case OMP_CLAUSE_THREADS:
14628 case OMP_CLAUSE_SIMD:
14629 case OMP_CLAUSE_HINT:
14630 case OMP_CLAUSE_DEFAULTMAP:
ca4c3545 14631 case OMP_CLAUSE_NUM_GANGS:
14632 case OMP_CLAUSE_NUM_WORKERS:
14633 case OMP_CLAUSE_VECTOR_LENGTH:
14634 case OMP_CLAUSE_ASYNC:
14635 case OMP_CLAUSE_WAIT:
14636 case OMP_CLAUSE_AUTO:
ef014f95 14637 case OMP_CLAUSE_INDEPENDENT:
ca4c3545 14638 case OMP_CLAUSE_SEQ:
14639 case OMP_CLAUSE_GANG:
14640 case OMP_CLAUSE_WORKER:
14641 case OMP_CLAUSE_VECTOR:
ef014f95 14642 case OMP_CLAUSE_TILE:
737cc978 14643 case OMP_CLAUSE_IF_PRESENT:
14644 case OMP_CLAUSE_FINALIZE:
bc7bff74 14645 pc = &OMP_CLAUSE_CHAIN (c);
14646 continue;
14647
7e5a76c8 14648 case OMP_CLAUSE_NOGROUP:
14649 nogroup_seen = pc;
14650 pc = &OMP_CLAUSE_CHAIN (c);
14651 continue;
14652
9561765e 14653 case OMP_CLAUSE_SCHEDULE:
9561765e 14654 schedule_clause = c;
14655 pc = &OMP_CLAUSE_CHAIN (c);
14656 continue;
14657
14658 case OMP_CLAUSE_ORDERED:
14659 ordered_seen = true;
14660 pc = &OMP_CLAUSE_CHAIN (c);
14661 continue;
14662
43895be5 14663 case OMP_CLAUSE_SAFELEN:
14664 safelen = c;
14665 pc = &OMP_CLAUSE_CHAIN (c);
14666 continue;
14667 case OMP_CLAUSE_SIMDLEN:
14668 simdlen = c;
14669 pc = &OMP_CLAUSE_CHAIN (c);
14670 continue;
14671
bc7bff74 14672 case OMP_CLAUSE_INBRANCH:
14673 case OMP_CLAUSE_NOTINBRANCH:
14674 if (branch_seen)
14675 {
14676 error_at (OMP_CLAUSE_LOCATION (c),
14677 "%<inbranch%> clause is incompatible with "
14678 "%<notinbranch%>");
14679 remove = true;
14680 break;
14681 }
14682 branch_seen = true;
1e8e9920 14683 pc = &OMP_CLAUSE_CHAIN (c);
14684 continue;
14685
14686 default:
14687 gcc_unreachable ();
14688 }
14689
14690 if (!remove)
14691 {
14692 t = OMP_CLAUSE_DECL (c);
14693
14694 if (need_complete)
14695 {
22a3f7bd 14696 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
1e8e9920 14697 if (t == error_mark_node)
14698 remove = true;
14699 }
14700
14701 if (need_implicitly_determined)
14702 {
14703 const char *share_name = NULL;
14704
f48c7f4a 14705 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
1e8e9920 14706 share_name = "threadprivate";
14707 else switch (c_omp_predetermined_sharing (t))
14708 {
14709 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
14710 break;
14711 case OMP_CLAUSE_DEFAULT_SHARED:
14712 share_name = "shared";
14713 break;
14714 case OMP_CLAUSE_DEFAULT_PRIVATE:
14715 share_name = "private";
14716 break;
14717 default:
14718 gcc_unreachable ();
14719 }
14720 if (share_name)
14721 {
e60a6f7b 14722 error_at (OMP_CLAUSE_LOCATION (c),
14723 "%qE is predetermined %qs for %qs",
bc7bff74 14724 t, share_name,
14725 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
1e8e9920 14726 remove = true;
14727 }
7e5a76c8 14728 else if (TREE_READONLY (t)
14729 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
14730 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
14731 {
14732 error_at (OMP_CLAUSE_LOCATION (c),
14733 "%<const%> qualified %qE may appear only in "
14734 "%<shared%> or %<firstprivate%> clauses", t);
14735 remove = true;
14736 }
1e8e9920 14737 }
14738 }
14739
14740 if (remove)
14741 *pc = OMP_CLAUSE_CHAIN (c);
14742 else
14743 pc = &OMP_CLAUSE_CHAIN (c);
14744 }
14745
43895be5 14746 if (simdlen
14747 && safelen
14748 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
14749 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
14750 {
14751 error_at (OMP_CLAUSE_LOCATION (simdlen),
14752 "%<simdlen%> clause value is bigger than "
14753 "%<safelen%> clause value");
14754 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
14755 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
14756 }
14757
9561765e 14758 if (ordered_seen
14759 && schedule_clause
14760 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14761 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14762 {
14763 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
14764 "%<nonmonotonic%> schedule modifier specified together "
14765 "with %<ordered%> clause");
14766 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14767 = (enum omp_clause_schedule_kind)
14768 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14769 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
14770 }
14771
14772 if (linear_variable_step_check)
14773 for (pc = &clauses, c = clauses; c ; c = *pc)
14774 {
14775 bool remove = false;
14776 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14777 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
14778 && !bitmap_bit_p (&map_head,
14779 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
14780 {
14781 error_at (OMP_CLAUSE_LOCATION (c),
14782 "%<linear%> clause step is a parameter %qD not "
14783 "specified in %<uniform%> clause",
14784 OMP_CLAUSE_LINEAR_STEP (c));
14785 remove = true;
14786 }
14787
14788 if (remove)
14789 *pc = OMP_CLAUSE_CHAIN (c);
14790 else
14791 pc = &OMP_CLAUSE_CHAIN (c);
14792 }
14793
7e5a76c8 14794 if (nogroup_seen && reduction_seen)
14795 {
14796 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
14797 "%<nogroup%> clause must not be used together with "
14798 "%<reduction%> clause");
14799 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
14800 }
14801
1e8e9920 14802 bitmap_obstack_release (NULL);
14803 return clauses;
14804}
796735dc 14805
0b80c4b2 14806/* Return code to initialize DST with a copy constructor from SRC.
14807 C doesn't have copy constructors nor assignment operators, only for
14808 _Atomic vars we need to perform __atomic_load from src into a temporary
14809 followed by __atomic_store of the temporary to dst. */
14810
14811tree
14812c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
14813{
14814 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
14815 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
14816
14817 location_t loc = OMP_CLAUSE_LOCATION (clause);
14818 tree type = TREE_TYPE (dst);
14819 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
14820 tree tmp = create_tmp_var (nonatomic_type);
14821 tree tmp_addr = build_fold_addr_expr (tmp);
14822 TREE_ADDRESSABLE (tmp) = 1;
14823 TREE_NO_WARNING (tmp) = 1;
14824 tree src_addr = build_fold_addr_expr (src);
14825 tree dst_addr = build_fold_addr_expr (dst);
14826 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14827 vec<tree, va_gc> *params;
14828 /* Expansion of a generic atomic load may require an addition
14829 element, so allocate enough to prevent a resize. */
14830 vec_alloc (params, 4);
14831
14832 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14833 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14834 params->quick_push (src_addr);
14835 params->quick_push (tmp_addr);
14836 params->quick_push (seq_cst);
14837 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14838
14839 vec_alloc (params, 4);
14840
14841 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14842 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14843 params->quick_push (dst_addr);
14844 params->quick_push (tmp_addr);
14845 params->quick_push (seq_cst);
14846 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14847 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14848}
14849
4c0315d0 14850/* Create a transaction node. */
14851
14852tree
14853c_finish_transaction (location_t loc, tree block, int flags)
14854{
14855 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14856 if (flags & TM_STMT_ATTR_OUTER)
14857 TRANSACTION_EXPR_OUTER (stmt) = 1;
14858 if (flags & TM_STMT_ATTR_RELAXED)
14859 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14860 return add_stmt (stmt);
14861}
14862
796735dc 14863/* Make a variant type in the proper way for C/C++, propagating qualifiers
89f16034 14864 down to the element type of an array. If ORIG_QUAL_TYPE is not
14865 NULL, then it should be used as the qualified type
14866 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14867 preserve information about the typedef name from which an array
14868 type was derived). */
796735dc 14869
14870tree
89f16034 14871c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14872 size_t orig_qual_indirect)
796735dc 14873{
14874 if (type == error_mark_node)
14875 return type;
14876
14877 if (TREE_CODE (type) == ARRAY_TYPE)
14878 {
14879 tree t;
14880 tree element_type = c_build_qualified_type (TREE_TYPE (type),
89f16034 14881 type_quals, orig_qual_type,
14882 orig_qual_indirect - 1);
796735dc 14883
14884 /* See if we already have an identically qualified type. */
89f16034 14885 if (orig_qual_type && orig_qual_indirect == 0)
14886 t = orig_qual_type;
14887 else
14888 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14889 {
14890 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14891 && TYPE_NAME (t) == TYPE_NAME (type)
14892 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14893 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14894 TYPE_ATTRIBUTES (type)))
14895 break;
14896 }
796735dc 14897 if (!t)
14898 {
14899 tree domain = TYPE_DOMAIN (type);
14900
14901 t = build_variant_type_copy (type);
14902 TREE_TYPE (t) = element_type;
14903
14904 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14905 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14906 SET_TYPE_STRUCTURAL_EQUALITY (t);
14907 else if (TYPE_CANONICAL (element_type) != element_type
14908 || (domain && TYPE_CANONICAL (domain) != domain))
14909 {
48e1416a 14910 tree unqualified_canon
796735dc 14911 = build_array_type (TYPE_CANONICAL (element_type),
48e1416a 14912 domain? TYPE_CANONICAL (domain)
796735dc 14913 : NULL_TREE);
292237f3 14914 if (TYPE_REVERSE_STORAGE_ORDER (type))
14915 {
14916 unqualified_canon
14917 = build_distinct_type_copy (unqualified_canon);
14918 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14919 }
48e1416a 14920 TYPE_CANONICAL (t)
796735dc 14921 = c_build_qualified_type (unqualified_canon, type_quals);
14922 }
14923 else
14924 TYPE_CANONICAL (t) = t;
14925 }
14926 return t;
14927 }
14928
14929 /* A restrict-qualified pointer type must be a pointer to object or
14930 incomplete type. Note that the use of POINTER_TYPE_P also allows
14931 REFERENCE_TYPEs, which is appropriate for C++. */
14932 if ((type_quals & TYPE_QUAL_RESTRICT)
14933 && (!POINTER_TYPE_P (type)
14934 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14935 {
14936 error ("invalid use of %<restrict%>");
14937 type_quals &= ~TYPE_QUAL_RESTRICT;
14938 }
14939
89f16034 14940 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14941 ? orig_qual_type
14942 : build_qualified_type (type, type_quals));
7b15d957 14943 /* A variant type does not inherit the list of incomplete vars from the
14944 type main variant. */
1af10ce6 14945 if (RECORD_OR_UNION_TYPE_P (var_type)
14946 && TYPE_MAIN_VARIANT (var_type) != var_type)
7b15d957 14947 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14948 return var_type;
796735dc 14949}
d62e827b 14950
14951/* Build a VA_ARG_EXPR for the C parser. */
14952
14953tree
a4ab54ac 14954c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
d62e827b 14955{
2cd7caae 14956 if (error_operand_p (type))
14957 return error_mark_node;
a4ab54ac 14958 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14959 order because it takes the address of the expression. */
14960 else if (handled_component_p (expr)
14961 && reverse_storage_order_for_component_p (expr))
14962 {
14963 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14964 return error_mark_node;
14965 }
2cd7caae 14966 else if (!COMPLETE_TYPE_P (type))
5580bf42 14967 {
a4ab54ac 14968 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
2cd7caae 14969 "type %qT", type);
5580bf42 14970 return error_mark_node;
14971 }
2cd7caae 14972 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
a4ab54ac 14973 warning_at (loc2, OPT_Wc___compat,
2cd7caae 14974 "C++ requires promoted type, not enum type, in %<va_arg%>");
a4ab54ac 14975 return build_va_arg (loc2, expr, type);
d62e827b 14976}
bc7bff74 14977
14978/* Return truthvalue of whether T1 is the same tree structure as T2.
78173203 14979 Return 1 if they are the same. Return false if they are different. */
bc7bff74 14980
14981bool
14982c_tree_equal (tree t1, tree t2)
14983{
14984 enum tree_code code1, code2;
14985
14986 if (t1 == t2)
14987 return true;
14988 if (!t1 || !t2)
14989 return false;
14990
14991 for (code1 = TREE_CODE (t1);
14992 CONVERT_EXPR_CODE_P (code1)
14993 || code1 == NON_LVALUE_EXPR;
14994 code1 = TREE_CODE (t1))
14995 t1 = TREE_OPERAND (t1, 0);
14996 for (code2 = TREE_CODE (t2);
14997 CONVERT_EXPR_CODE_P (code2)
14998 || code2 == NON_LVALUE_EXPR;
14999 code2 = TREE_CODE (t2))
15000 t2 = TREE_OPERAND (t2, 0);
15001
15002 /* They might have become equal now. */
15003 if (t1 == t2)
15004 return true;
15005
15006 if (code1 != code2)
15007 return false;
15008
15009 switch (code1)
15010 {
15011 case INTEGER_CST:
e3d0f65c 15012 return wi::to_wide (t1) == wi::to_wide (t2);
bc7bff74 15013
15014 case REAL_CST:
20cb53c9 15015 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
bc7bff74 15016
15017 case STRING_CST:
15018 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15019 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15020 TREE_STRING_LENGTH (t1));
15021
15022 case FIXED_CST:
15023 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15024 TREE_FIXED_CST (t2));
15025
15026 case COMPLEX_CST:
15027 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15028 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15029
15030 case VECTOR_CST:
15031 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15032
15033 case CONSTRUCTOR:
15034 /* We need to do this when determining whether or not two
15035 non-type pointer to member function template arguments
15036 are the same. */
15037 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15038 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15039 return false;
15040 {
15041 tree field, value;
15042 unsigned int i;
15043 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15044 {
15045 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15046 if (!c_tree_equal (field, elt2->index)
15047 || !c_tree_equal (value, elt2->value))
15048 return false;
15049 }
15050 }
15051 return true;
15052
15053 case TREE_LIST:
15054 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15055 return false;
15056 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15057 return false;
15058 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15059
15060 case SAVE_EXPR:
15061 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15062
15063 case CALL_EXPR:
15064 {
15065 tree arg1, arg2;
15066 call_expr_arg_iterator iter1, iter2;
15067 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
15068 return false;
15069 for (arg1 = first_call_expr_arg (t1, &iter1),
15070 arg2 = first_call_expr_arg (t2, &iter2);
15071 arg1 && arg2;
15072 arg1 = next_call_expr_arg (&iter1),
15073 arg2 = next_call_expr_arg (&iter2))
15074 if (!c_tree_equal (arg1, arg2))
15075 return false;
15076 if (arg1 || arg2)
15077 return false;
15078 return true;
15079 }
15080
15081 case TARGET_EXPR:
15082 {
15083 tree o1 = TREE_OPERAND (t1, 0);
15084 tree o2 = TREE_OPERAND (t2, 0);
15085
15086 /* Special case: if either target is an unallocated VAR_DECL,
15087 it means that it's going to be unified with whatever the
15088 TARGET_EXPR is really supposed to initialize, so treat it
15089 as being equivalent to anything. */
f48c7f4a 15090 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
bc7bff74 15091 && !DECL_RTL_SET_P (o1))
15092 /*Nop*/;
f48c7f4a 15093 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
bc7bff74 15094 && !DECL_RTL_SET_P (o2))
15095 /*Nop*/;
15096 else if (!c_tree_equal (o1, o2))
15097 return false;
15098
15099 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
15100 }
15101
15102 case COMPONENT_REF:
15103 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
15104 return false;
15105 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15106
15107 case PARM_DECL:
15108 case VAR_DECL:
15109 case CONST_DECL:
15110 case FIELD_DECL:
15111 case FUNCTION_DECL:
15112 case IDENTIFIER_NODE:
15113 case SSA_NAME:
15114 return false;
15115
15116 case TREE_VEC:
15117 {
15118 unsigned ix;
15119 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
15120 return false;
15121 for (ix = TREE_VEC_LENGTH (t1); ix--;)
15122 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
15123 TREE_VEC_ELT (t2, ix)))
15124 return false;
15125 return true;
15126 }
15127
15128 default:
15129 break;
15130 }
15131
15132 switch (TREE_CODE_CLASS (code1))
15133 {
15134 case tcc_unary:
15135 case tcc_binary:
15136 case tcc_comparison:
15137 case tcc_expression:
15138 case tcc_vl_exp:
15139 case tcc_reference:
15140 case tcc_statement:
15141 {
15142 int i, n = TREE_OPERAND_LENGTH (t1);
15143
15144 switch (code1)
15145 {
15146 case PREINCREMENT_EXPR:
15147 case PREDECREMENT_EXPR:
15148 case POSTINCREMENT_EXPR:
15149 case POSTDECREMENT_EXPR:
15150 n = 1;
15151 break;
15152 case ARRAY_REF:
15153 n = 2;
15154 break;
15155 default:
15156 break;
15157 }
15158
15159 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
15160 && n != TREE_OPERAND_LENGTH (t2))
15161 return false;
15162
15163 for (i = 0; i < n; ++i)
15164 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
15165 return false;
15166
15167 return true;
15168 }
15169
15170 case tcc_type:
15171 return comptypes (t1, t2);
15172 default:
15173 gcc_unreachable ();
15174 }
15175 /* We can get here with --disable-checking. */
15176 return false;
15177}
433e804e 15178
547c6b1f 15179/* Returns true when the function declaration FNDECL is implicit,
15180 introduced as a result of a call to an otherwise undeclared
15181 function, and false otherwise. */
15182
15183bool
15184c_decl_implicit (const_tree fndecl)
15185{
15186 return C_DECL_IMPLICIT (fndecl);
15187}