]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
common.opt (Wcast-align=strict): New warning option.
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
cbe34bb5 2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6 28#include "coretypes.h"
4d0cdd0c 29#include "memmodel.h"
2adfab87
AM
30#include "target.h"
31#include "function.h"
32#include "bitmap.h"
2adfab87
AM
33#include "c-tree.h"
34#include "gimple-expr.h"
35#include "predict.h"
d8a2d370
DN
36#include "stor-layout.h"
37#include "trans-mem.h"
38#include "varasm.h"
39#include "stmt.h"
e57e265b 40#include "langhooks.h"
29cc57cf 41#include "c-lang.h"
ab87f8c8 42#include "intl.h"
325c3691 43#include "tree-iterator.h"
45b0be94 44#include "gimplify.h"
acf0174b 45#include "tree-inline.h"
629b3d75 46#include "omp-general.h"
61d3ce20 47#include "c-family/c-objc.h"
de5a5fa1 48#include "c-family/c-ubsan.h"
12893402 49#include "cilk.h"
41dbbb37 50#include "gomp-constants.h"
6a3f203c 51#include "spellcheck-tree.h"
745e411d 52#include "gcc-rich-location.h"
314e6352
ML
53#include "stringpool.h"
54#include "attribs.h"
45b2222a 55#include "asan.h"
325c3691 56
2ac2f164
JM
57/* Possible cases of implicit bad conversions. Used to select
58 diagnostic messages in convert_for_assignment. */
59enum impl_conv {
60 ic_argpass,
61 ic_assign,
62 ic_init,
63 ic_return
64};
65
bc4b653b
JM
66/* The level of nesting inside "__alignof__". */
67int in_alignof;
68
69/* The level of nesting inside "sizeof". */
70int in_sizeof;
71
72/* The level of nesting inside "typeof". */
73int in_typeof;
400fbf9f 74
1a4049e7
JJ
75/* The argument of last parsed sizeof expression, only to be tested
76 if expr.original_code == SIZEOF_EXPR. */
77tree c_last_sizeof_arg;
40ffd95f 78location_t c_last_sizeof_loc;
1a4049e7 79
9bac5cbb
G
80/* Nonzero if we might need to print a "missing braces around
81 initializer" message within this initializer. */
82static int found_missing_braces;
103b7b17 83
bf730f15
RS
84static int require_constant_value;
85static int require_constant_elements;
86
58f9752a 87static bool null_pointer_constant_p (const_tree);
f55ade6e 88static tree qualify_type (tree, tree);
dc5027f4
JM
89static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
90 bool *);
744aa42f 91static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
92static int function_types_compatible_p (const_tree, const_tree, bool *,
93 bool *);
94static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 95static tree lookup_field (tree, tree);
81e5eca8
MP
96static int convert_arguments (location_t, vec<location_t>, tree,
97 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
98 tree);
db3927fb 99static tree pointer_diff (location_t, tree, tree);
68fca595 100static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 101 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
102static tree valid_compound_expr_initializer (tree, tree);
103static void push_string (const char *);
104static void push_member_name (tree);
f55ade6e
AJ
105static int spelling_length (void);
106static char *print_spelling (char *);
96b40f8d 107static void warning_init (location_t, int, const char *);
c2255bc4 108static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
30af3a2b 109static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
34cf811f 110 bool, struct obstack *);
a1e3b3d9 111static void output_pending_init_elements (int, struct obstack *);
30af3a2b 112static bool set_designator (location_t, bool, struct obstack *);
a1e3b3d9 113static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
114static void add_pending_init (location_t, tree, tree, tree, bool,
115 struct obstack *);
a1e3b3d9
LB
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 *);
f37acdf9 119static void readonly_warning (tree, enum lvalue_use);
7bd11157 120static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 121static void record_maybe_used_decl (tree);
dc5027f4 122static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
123\f
124/* Return true if EXP is a null pointer constant, false otherwise. */
125
126static bool
58f9752a 127null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
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
8bcd6380 133 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
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}
928c19bb
JM
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
4d84fe7c
JM
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
f13c9b2c
AP
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;
58f9752a
KG
181 const_tree t1;
182 const_tree t2;
f13c9b2c
AP
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
4f2e1536
MP
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. */
400fbf9f
JW
194
195tree
4f2e1536 196require_complete_type (location_t loc, tree value)
400fbf9f
JW
197{
198 tree type = TREE_TYPE (value);
199
7a0ca710 200 if (error_operand_p (value))
ea0f786b
CB
201 return error_mark_node;
202
400fbf9f 203 /* First, detect a valid value with a complete type. */
d0f062fb 204 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
205 return value;
206
4f2e1536 207 c_incomplete_type_error (loc, value, type);
400fbf9f
JW
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)
4f2e1536
MP
213 and TYPE is the type that was invalid. LOC is the location for
214 the error. */
400fbf9f
JW
215
216void
4f2e1536 217c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
400fbf9f 218{
400fbf9f
JW
219 /* Avoid duplicate error message. */
220 if (TREE_CODE (type) == ERROR_MARK)
221 return;
222
3fa8871b 223 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
4f2e1536 224 error_at (loc, "%qD has an incomplete type %qT", value, type);
400fbf9f
JW
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:
400fbf9f 233 case UNION_TYPE:
400fbf9f 234 case ENUMERAL_TYPE:
400fbf9f
JW
235 break;
236
237 case VOID_TYPE:
4f2e1536 238 error_at (loc, "invalid use of void expression");
400fbf9f
JW
239 return;
240
241 case ARRAY_TYPE:
242 if (TYPE_DOMAIN (type))
243 {
fba78abb
RH
244 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
245 {
4f2e1536 246 error_at (loc, "invalid use of flexible array member");
fba78abb
RH
247 return;
248 }
400fbf9f
JW
249 type = TREE_TYPE (type);
250 goto retry;
251 }
4f2e1536 252 error_at (loc, "invalid use of array with unspecified bounds");
400fbf9f
JW
253 return;
254
255 default:
366de0ce 256 gcc_unreachable ();
400fbf9f
JW
257 }
258
259 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
4f2e1536 260 error_at (loc, "invalid use of undefined type %qT", type);
400fbf9f
JW
261 else
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
4f2e1536 263 error_at (loc, "invalid use of incomplete typedef %qT", type);
400fbf9f
JW
264 }
265}
266
ab393bf1
NB
267/* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
269
270tree
2f6e4e97 271c_type_promotes_to (tree type)
ab393bf1 272{
267bac10 273 tree ret = NULL_TREE;
ab393bf1 274
267bac10
JM
275 if (TYPE_MAIN_VARIANT (type) == float_type_node)
276 ret = double_type_node;
277 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
278 {
279 /* Preserve unsignedness if not really getting any wider. */
8df83eae 280 if (TYPE_UNSIGNED (type)
c22cacf3 281 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
282 ret = unsigned_type_node;
283 else
284 ret = integer_type_node;
ab393bf1
NB
285 }
286
267bac10
JM
287 if (ret != NULL_TREE)
288 return (TYPE_ATOMIC (type)
289 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
290 : ret);
291
ab393bf1
NB
292 return type;
293}
294
36c5e70a
BE
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
400fbf9f
JW
321/* Return a variant of TYPE which has all the type qualifiers of LIKE
322 as well as those of TYPE. */
323
324static tree
2f6e4e97 325qualify_type (tree type, tree like)
400fbf9f 326{
36c5e70a
BE
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
2f6e4e97 340 return c_build_qualified_type (type,
36c5e70a 341 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 342 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 343 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 344}
52ffd86e
MS
345
346/* Return true iff the given tree T is a variable length array. */
347
348bool
ac7d7749 349c_vla_type_p (const_tree t)
52ffd86e
MS
350{
351 if (TREE_CODE (t) == ARRAY_TYPE
352 && C_TYPE_VARIABLE_SIZE (t))
353 return true;
354 return false;
355}
400fbf9f 356\f
10bc1b1b 357/* Return the composite type of two compatible types.
5305f6d7 358
10bc1b1b
JM
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. */
400fbf9f
JW
362
363tree
10bc1b1b 364composite_type (tree t1, tree t2)
400fbf9f 365{
b3694847
SS
366 enum tree_code code1;
367 enum tree_code code2;
4b027d16 368 tree attributes;
400fbf9f
JW
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
10bc1b1b
JM
380 code1 = TREE_CODE (t1);
381 code2 = TREE_CODE (t2);
382
d9525bec 383 /* Merge the attributes. */
5fd9b178 384 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 385
10bc1b1b
JM
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. */
400fbf9f 390
10bc1b1b
JM
391 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
392 return t1;
393 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
394 return t2;
75326e8c 395
366de0ce 396 gcc_assert (code1 == code2);
b6a10c9f 397
400fbf9f
JW
398 switch (code1)
399 {
400fbf9f 400 case POINTER_TYPE:
10bc1b1b 401 /* For two pointers, do this recursively on the target type. */
400fbf9f 402 {
3932261a
MM
403 tree pointed_to_1 = TREE_TYPE (t1);
404 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 405 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 406 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
407 t1 = build_type_attribute_variant (t1, attributes);
408 return qualify_type (t1, t2);
400fbf9f 409 }
400fbf9f
JW
410
411 case ARRAY_TYPE:
412 {
10bc1b1b 413 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
414 int quals;
415 tree unqual_elt;
ca8bdb78
JM
416 tree d1 = TYPE_DOMAIN (t1);
417 tree d2 = TYPE_DOMAIN (t2);
418 bool d1_variable, d2_variable;
419 bool d1_zero, d2_zero;
f6294de7 420 bool t1_complete, t2_complete;
46df2823 421
de46b2fe 422 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
423 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
424 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 425
f6294de7
JM
426 t1_complete = COMPLETE_TYPE_P (t1);
427 t2_complete = COMPLETE_TYPE_P (t2);
428
3fa8871b
MP
429 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
430 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
ca8bdb78
JM
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));
52ffd86e
MS
438 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
439 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 440
400fbf9f 441 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
442 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
443 && (d2_variable || d2_zero || !d1_variable))
4b027d16 444 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
445 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
446 && (d1_variable || d1_zero || !d2_variable))
4b027d16 447 return build_type_attribute_variant (t2, attributes);
c22cacf3 448
de46b2fe
AP
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);
c22cacf3 453
46df2823
JM
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,
ca8bdb78
JM
462 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
463 && (d2_variable
464 || d2_zero
465 || !d1_variable))
466 ? t1
467 : t2));
f6294de7
JM
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 }
46df2823 477 t1 = c_build_qualified_type (t1, quals);
de46b2fe 478 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
479 }
480
fcb99e7b
JJ
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
400fbf9f
JW
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 {
10bc1b1b 498 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
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. */
3f75a254 506 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 507 return build_type_attribute_variant (t1, attributes);
3f75a254 508 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 509 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
510
511 /* Simple way if one arg fails to specify argument types. */
3fa8871b 512 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
4b027d16 513 {
fe7080d2
AP
514 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
515 t1 = build_type_attribute_variant (t1, attributes);
516 return qualify_type (t1, t2);
4b027d16 517 }
3fa8871b 518 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
4b027d16
RK
519 {
520 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
521 t1 = build_type_attribute_variant (t1, attributes);
522 return qualify_type (t1, t2);
4b027d16 523 }
400fbf9f
JW
524
525 /* If both args specify argument types, we must merge the two
526 lists, argument by argument. */
2f4e8f2b 527
fcc2b74f
JJ
528 for (len = 0, newargs = p1;
529 newargs && newargs != void_list_node;
530 len++, newargs = TREE_CHAIN (newargs))
531 ;
400fbf9f
JW
532
533 for (i = 0; i < len; i++)
8d9bfdc5 534 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
535
536 n = newargs;
537
fcc2b74f 538 for (; p1 && p1 != void_list_node;
400fbf9f
JW
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. */
3fa8871b 543 if (TREE_VALUE (p1) == NULL_TREE)
400fbf9f
JW
544 {
545 TREE_VALUE (n) = TREE_VALUE (p2);
546 goto parm_done;
547 }
3fa8871b 548 if (TREE_VALUE (p2) == NULL_TREE)
400fbf9f
JW
549 {
550 TREE_VALUE (n) = TREE_VALUE (p1);
551 goto parm_done;
552 }
2f6e4e97 553
400fbf9f
JW
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;
58cb41e6
JJ
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);
400fbf9f 565 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 566 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
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));
c1771a20 576 pedwarn (input_location, OPT_Wpedantic,
fcf73884 577 "function types not truly compatible in ISO C");
58cb41e6
JJ
578 goto parm_done;
579 }
580 }
400fbf9f
JW
581 }
582 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
583 && TREE_VALUE (p2) != TREE_VALUE (p1))
584 {
585 tree memb;
58cb41e6
JJ
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);
400fbf9f 590 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 591 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
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));
c1771a20 601 pedwarn (input_location, OPT_Wpedantic,
fcf73884 602 "function types not truly compatible in ISO C");
58cb41e6
JJ
603 goto parm_done;
604 }
605 }
400fbf9f 606 }
10bc1b1b 607 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
608 parm_done: ;
609 }
610
4b027d16 611 t1 = build_function_type (valtype, newargs);
fe7080d2 612 t1 = qualify_type (t1, t2);
400fbf9f 613 }
295844f6 614 /* FALLTHRU */
400fbf9f
JW
615
616 default:
4b027d16 617 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
618 }
619
620}
10bc1b1b
JM
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;
46df2823
JM
632 tree pointed_to_1, mv1;
633 tree pointed_to_2, mv2;
10bc1b1b 634 tree target;
eb1387a0 635 unsigned target_quals;
36c5e70a
BE
636 addr_space_t as1, as2, as_common;
637 int quals1, quals2;
10bc1b1b
JM
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
366de0ce 649 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 650 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
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
46df2823
JM
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);
eb1387a0 665
768952be
MU
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
eb1387a0
RG
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)
36c5e70a 674 target_quals = (quals1 & quals2);
eb1387a0 675 else
36c5e70a
BE
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
eb1387a0 688 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
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
ccf7f880
JJ
700static tree
701c_common_type (tree t1, tree t2)
10bc1b1b
JM
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
366de0ce 731 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
732 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
733 || code1 == INTEGER_TYPE);
366de0ce 734 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
735 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
736 || code2 == INTEGER_TYPE);
10bc1b1b 737
5fc89bfd
JJ
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
10bc1b1b
JM
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;
ccf7f880 777 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
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
9a8ce21f
JG
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
ab22c1fa
CF
811 /* Deal with fixed-point types. */
812 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
813 {
814 unsigned int unsignedp = 0, satp = 0;
b397965c 815 scalar_mode m1, m2;
ab22c1fa
CF
816 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
817
b397965c
RS
818 m1 = SCALAR_TYPE_MODE (t1);
819 m2 = SCALAR_TYPE_MODE (t2);
ab22c1fa
CF
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 {
d75d71e0 844 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
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 ();
b397965c
RS
851 m1 = as_a <scalar_mode>
852 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
ab22c1fa
CF
853 }
854 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
855 {
d75d71e0 856 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
857 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
858 mclass = MODE_FRACT;
859 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
860 mclass = MODE_ACCUM;
861 else
862 gcc_unreachable ();
b397965c
RS
863 m2 = as_a <scalar_mode>
864 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
ab22c1fa
CF
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
10bc1b1b
JM
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
c22cacf3 919 return long_long_integer_type_node;
10bc1b1b
JM
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
c65699ef
JM
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
10bc1b1b 955 /* Likewise, prefer long double to double even if same size. */
c65699ef 956 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
10bc1b1b
JM
957 return long_double_type_node;
958
2531a1d9
JR
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. */
c65699ef 962 if (mv1 == double_type_node || mv2 == double_type_node)
2531a1d9
JR
963 return double_type_node;
964
c65699ef
JM
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
10bc1b1b
JM
972 /* Otherwise prefer the unsigned one. */
973
974 if (TYPE_UNSIGNED (t1))
975 return t1;
976 else
977 return t2;
978}
400fbf9f 979\f
5922c215
JM
980/* Wrapper around c_common_type that is used by c-common.c and other
981 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
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. */
ccf7f880
JJ
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);
b2232745
RS
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
ccf7f880
JJ
1004 return c_common_type (t1, t2);
1005}
f13c9b2c 1006
400fbf9f
JW
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
132da1a5 1012comptypes (tree type1, tree type2)
f13c9b2c
AP
1013{
1014 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1015 int val;
1016
dc5027f4 1017 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
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
dc5027f4
JM
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);
f13c9b2c 1049 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1050
f13c9b2c 1051 return val;
c22cacf3
MS
1052}
1053\f
f13c9b2c
AP
1054/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1055 or various other operations. Return 2 if they are compatible
744aa42f
ILT
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;
dc5027f4
JM
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
48b0b196 1061 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
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. */
f13c9b2c
AP
1066
1067static int
dc5027f4
JM
1068comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1069 bool *different_types_p)
400fbf9f 1070{
58f9752a
KG
1071 const_tree t1 = type1;
1072 const_tree t2 = type2;
4b027d16 1073 int attrval, val;
400fbf9f
JW
1074
1075 /* Suppress errors caused by previously reported errors. */
1076
8d47dfc5
RH
1077 if (t1 == t2 || !t1 || !t2
1078 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1079 return 1;
1080
bca63328
JM
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. */
400fbf9f 1084
bca63328 1085 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1086 {
1087 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
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 }
744aa42f 1095 }
bca63328 1096 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1097 {
1098 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
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 }
744aa42f 1106 }
400fbf9f
JW
1107
1108 if (t1 == t2)
1109 return 1;
1110
1111 /* Different classes of types can't be compatible. */
1112
3aeb3655
EC
1113 if (TREE_CODE (t1) != TREE_CODE (t2))
1114 return 0;
400fbf9f 1115
118a3a8b 1116 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1117
3932261a 1118 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1119 return 0;
1120
08632da2
RS
1121 /* Allow for two different type nodes which have essentially the same
1122 definition. Note that we already checked for equality of the type
38e01259 1123 qualifiers (just above). */
400fbf9f 1124
46df2823
JM
1125 if (TREE_CODE (t1) != ARRAY_TYPE
1126 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1127 return 1;
1128
4b027d16 1129 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1130 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1131 return 0;
1132
1133 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1134 val = 0;
1135
400fbf9f
JW
1136 switch (TREE_CODE (t1))
1137 {
5545a907
MP
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
400fbf9f 1157 case POINTER_TYPE:
5545a907
MP
1158 /* Do not remove mode information. */
1159 if (TYPE_MODE (t1) != TYPE_MODE (t2))
106f5de5 1160 break;
4b027d16 1161 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1162 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1163 enum_and_int_p, different_types_p));
4b027d16 1164 break;
400fbf9f
JW
1165
1166 case FUNCTION_TYPE:
dc5027f4
JM
1167 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1168 different_types_p);
4b027d16 1169 break;
400fbf9f
JW
1170
1171 case ARRAY_TYPE:
1172 {
400fbf9f
JW
1173 tree d1 = TYPE_DOMAIN (t1);
1174 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1175 bool d1_variable, d2_variable;
1176 bool d1_zero, d2_zero;
4b027d16 1177 val = 1;
400fbf9f
JW
1178
1179 /* Target types must match incl. qualifiers. */
1180 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1181 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1182 enum_and_int_p,
1183 different_types_p)))
400fbf9f
JW
1184 return 0;
1185
dc5027f4 1186 if (different_types_p != NULL
3fa8871b 1187 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
dc5027f4 1188 *different_types_p = true;
400fbf9f 1189 /* Sizes must match unless one is missing or variable. */
3fa8871b 1190 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
4b027d16 1191 break;
400fbf9f 1192
3f75a254
JM
1193 d1_zero = !TYPE_MAX_VALUE (d1);
1194 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1195
3f75a254 1196 d1_variable = (!d1_zero
3f85558f
RH
1197 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1198 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1199 d2_variable = (!d2_zero
3f85558f
RH
1200 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1201 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1202 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1203 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1204
dc5027f4
JM
1205 if (different_types_p != NULL
1206 && d1_variable != d2_variable)
1207 *different_types_p = true;
3f85558f
RH
1208 if (d1_variable || d2_variable)
1209 break;
1210 if (d1_zero && d2_zero)
1211 break;
1212 if (d1_zero || d2_zero
3f75a254
JM
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)))
05bccae2
RK
1215 val = 0;
1216
c22cacf3 1217 break;
400fbf9f
JW
1218 }
1219
d1bd0ded 1220 case ENUMERAL_TYPE:
58393038 1221 case RECORD_TYPE:
d1bd0ded 1222 case UNION_TYPE:
766beae1 1223 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1224 {
fcb99e7b
JJ
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
f13c9b2c 1232 if (attrval != 2)
dc5027f4
JM
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);
f13c9b2c 1237 }
4b027d16 1238 break;
e9a25f70 1239
62e1dfcf 1240 case VECTOR_TYPE:
744aa42f
ILT
1241 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1242 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1243 enum_and_int_p, different_types_p));
62e1dfcf
NC
1244 break;
1245
e9a25f70
JL
1246 default:
1247 break;
400fbf9f 1248 }
4b027d16 1249 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1250}
1251
36c5e70a
BE
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. */
400fbf9f
JW
1256
1257static int
744aa42f 1258comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1259{
392202b0 1260 int val;
768952be 1261 int val_ped;
36c5e70a
BE
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;
744aa42f 1267 bool enum_and_int_p;
8b40563c 1268
36c5e70a
BE
1269 /* Fail if pointers point to incompatible address spaces. */
1270 if (!addr_space_superset (asl, asr, &as_common))
1271 return 0;
1272
768952be
MU
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
744aa42f
ILT
1292 enum_and_int_p = false;
1293 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1294
768952be
MU
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
fcf73884 1299 if (val == 2)
c1771a20 1300 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
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
400fbf9f
JW
1306 return val;
1307}
1308\f
1309/* Subroutines of `comptypes'. */
1310
f75fbaf7
ZW
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,
766beae1 1314 they're in the same translation unit. */
e3455240
MP
1315
1316bool
58f9752a 1317same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1318{
1319 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1320 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1321 {
6615c446
JO
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 */
366de0ce 1328 default: gcc_unreachable ();
766beae1
ZW
1329 }
1330
1331 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1332 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1333 {
6615c446
JO
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 */
366de0ce 1340 default: gcc_unreachable ();
766beae1
ZW
1341 }
1342
1343 return t1 == t2;
1344}
1345
f13c9b2c 1346/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1347
f13c9b2c 1348static struct tagged_tu_seen_cache *
58f9752a 1349alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1350{
cceb1885 1351 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1352 tu->next = tagged_tu_seen_base;
1353 tu->t1 = t1;
1354 tu->t2 = t2;
c22cacf3 1355
f13c9b2c 1356 tagged_tu_seen_base = tu;
c22cacf3 1357
f13c9b2c
AP
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,
c83eecad 1368 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1369 loop. */
1370 tu->val = 1;
1371 return tu;
1372}
d1bd0ded 1373
f13c9b2c 1374/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1375
f13c9b2c
AP
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 {
741ac903
KG
1382 const struct tagged_tu_seen_cache *const tu1
1383 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1384 tu = tu1->next;
b1d5455a 1385 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1386 }
1387 tagged_tu_seen_base = tu_til;
1388}
d1bd0ded
GK
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
dc5027f4
JM
1394 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1395 comptypes_internal. */
d1bd0ded
GK
1396
1397static int
744aa42f 1398tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1399 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1400{
1401 tree s1, s2;
1402 bool needs_warning = false;
3aeb3655 1403
d1bd0ded
GK
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
6de9cd9a
DN
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. */
dea984dc
ILT
1410 while (TYPE_NAME (t1)
1411 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1412 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1413 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1414
dea984dc
ILT
1415 while (TYPE_NAME (t2)
1416 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1417 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
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;
3aeb3655 1423
d1bd0ded
GK
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;
3aeb3655 1430
d1bd0ded 1431 {
f13c9b2c 1432 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
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)
f13c9b2c 1435 return tts_i->val;
d1bd0ded 1436 }
3aeb3655 1437
d1bd0ded
GK
1438 switch (TREE_CODE (t1))
1439 {
1440 case ENUMERAL_TYPE:
1441 {
f13c9b2c 1442 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
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);
3aeb3655 1446
c22cacf3 1447 if (tv1 == tv2)
f13c9b2c
AP
1448 {
1449 return 1;
1450 }
3aeb3655 1451
c22cacf3
MS
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)
f13c9b2c 1457 {
c22cacf3 1458 tu->val = 0;
f13c9b2c
AP
1459 return 0;
1460 }
c22cacf3 1461 }
3aeb3655 1462
c22cacf3 1463 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1464 {
1465 return 1;
1466 }
c22cacf3 1467 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1468 {
1469 tu->val = 0;
1470 return 0;
1471 }
3aeb3655 1472
d1bd0ded 1473 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1474 {
1475 tu->val = 0;
1476 return 0;
1477 }
3aeb3655 1478
d1bd0ded
GK
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)
f13c9b2c
AP
1484 {
1485 tu->val = 0;
1486 return 0;
1487 }
d1bd0ded
GK
1488 }
1489 return 1;
1490 }
1491
1492 case UNION_TYPE:
1493 {
f13c9b2c 1494 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1495 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1496 {
1497 tu->val = 0;
1498 return 0;
1499 }
c22cacf3 1500
f13c9b2c
AP
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;
910ad8de 1503 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1504 {
1505 int result;
c22cacf3 1506
3ae4d3cc 1507 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1508 break;
744aa42f 1509 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1510 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1511
1512 if (result != 1 && !DECL_NAME (s1))
1513 break;
f13c9b2c
AP
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 }
d1bd0ded 1535
910ad8de 1536 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1537 {
1538 bool ok = false;
3aeb3655 1539
910ad8de 1540 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1541 if (DECL_NAME (s1) == DECL_NAME (s2))
1542 {
1543 int result;
1544
744aa42f 1545 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1546 enum_and_int_p,
1547 different_types_p);
3ae4d3cc
AO
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)
d1bd0ded 1562 break;
3ae4d3cc
AO
1563
1564 ok = true;
1565 break;
1566 }
3f75a254 1567 if (!ok)
f13c9b2c
AP
1568 {
1569 tu->val = 0;
1570 return 0;
1571 }
d1bd0ded 1572 }
f13c9b2c
AP
1573 tu->val = needs_warning ? 2 : 10;
1574 return tu->val;
d1bd0ded
GK
1575 }
1576
1577 case RECORD_TYPE:
1578 {
c22cacf3 1579 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1580
1581 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1582 s1 && s2;
910ad8de 1583 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1584 {
1585 int result;
1586 if (TREE_CODE (s1) != TREE_CODE (s2)
1587 || DECL_NAME (s1) != DECL_NAME (s2))
1588 break;
744aa42f 1589 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1590 enum_and_int_p, different_types_p);
d1bd0ded
GK
1591 if (result == 0)
1592 break;
1593 if (result == 2)
1594 needs_warning = true;
3aeb3655 1595
d1bd0ded
GK
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 }
d1bd0ded 1601 if (s1 && s2)
f13c9b2c
AP
1602 tu->val = 0;
1603 else
1604 tu->val = needs_warning ? 2 : 1;
1605 return tu->val;
d1bd0ded
GK
1606 }
1607
1608 default:
366de0ce 1609 gcc_unreachable ();
d1bd0ded
GK
1610 }
1611}
1612
400fbf9f
JW
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.
2f6e4e97 1616 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1617 the other must specify that number of self-promoting arg types.
744aa42f 1618 Otherwise, the argument types must match.
dc5027f4 1619 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1620
1621static int
744aa42f 1622function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1623 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
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;
a6fdc086
GK
1629 tree ret1, ret2;
1630
1631 ret1 = TREE_TYPE (f1);
1632 ret2 = TREE_TYPE (f2);
1633
e508a019
JM
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))
509c9d60 1637 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
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);
dc5027f4 1644 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1645 if (val == 0)
400fbf9f
JW
1646 return 0;
1647
1648 args1 = TYPE_ARG_TYPES (f1);
1649 args2 = TYPE_ARG_TYPES (f2);
1650
dc5027f4 1651 if (different_types_p != NULL
3fa8871b 1652 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
dc5027f4
JM
1653 *different_types_p = true;
1654
400fbf9f
JW
1655 /* An unspecified parmlist matches any specified parmlist
1656 whose argument types don't need default promotions. */
1657
3fa8871b 1658 if (args1 == NULL_TREE)
400fbf9f
JW
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.
3176a0c2 1664 If they don't match, ask for a warning (but no error). */
400fbf9f 1665 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1666 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1667 enum_and_int_p, different_types_p))
400fbf9f
JW
1668 val = 2;
1669 return val;
1670 }
3fa8871b 1671 if (args2 == NULL_TREE)
400fbf9f
JW
1672 {
1673 if (!self_promoting_args_p (args1))
1674 return 0;
1675 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1676 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1677 enum_and_int_p, different_types_p))
400fbf9f
JW
1678 val = 2;
1679 return val;
1680 }
1681
1682 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1683 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1684 different_types_p);
400fbf9f
JW
1685 return val1 != 1 ? val1 : val;
1686}
1687
744aa42f
ILT
1688/* Check two lists of types for compatibility, returning 0 for
1689 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1690 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1691 comptypes_internal. */
400fbf9f
JW
1692
1693static int
744aa42f 1694type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1695 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1696{
1697 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1698 int val = 1;
9d5f3e49 1699 int newval = 0;
400fbf9f
JW
1700
1701 while (1)
1702 {
46df2823 1703 tree a1, mv1, a2, mv2;
3fa8871b 1704 if (args1 == NULL_TREE && args2 == NULL_TREE)
400fbf9f
JW
1705 return val;
1706 /* If one list is shorter than the other,
1707 they fail to match. */
3fa8871b 1708 if (args1 == NULL_TREE || args2 == NULL_TREE)
400fbf9f 1709 return 0;
46df2823
JM
1710 mv1 = a1 = TREE_VALUE (args1);
1711 mv2 = a2 = TREE_VALUE (args2);
1712 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1713 mv1 = (TYPE_ATOMIC (mv1)
1714 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1715 TYPE_QUAL_ATOMIC)
1716 : TYPE_MAIN_VARIANT (mv1));
46df2823 1717 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1718 mv2 = (TYPE_ATOMIC (mv2)
1719 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1720 TYPE_QUAL_ATOMIC)
1721 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
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. */
dc5027f4 1726 if (different_types_p != NULL
3fa8871b 1727 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
dc5027f4 1728 *different_types_p = true;
3fa8871b 1729 if (a1 == NULL_TREE)
400fbf9f 1730 {
46df2823 1731 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1732 return 0;
1733 }
3fa8871b 1734 else if (a2 == NULL_TREE)
400fbf9f 1735 {
46df2823 1736 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1737 return 0;
1738 }
8f5b6d29 1739 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1740 else if (TREE_CODE (a1) == ERROR_MARK
1741 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1742 ;
dc5027f4
JM
1743 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1744 different_types_p)))
400fbf9f 1745 {
dc5027f4
JM
1746 if (different_types_p != NULL)
1747 *different_types_p = true;
400fbf9f
JW
1748 /* Allow wait (union {union wait *u; int *i} *)
1749 and wait (union wait *) to be compatible. */
46df2823 1750 if (TREE_CODE (a1) == UNION_TYPE
3fa8871b 1751 && (TYPE_NAME (a1) == NULL_TREE
ebf0bf7f 1752 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1753 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1754 && tree_int_cst_equal (TYPE_SIZE (a1),
1755 TYPE_SIZE (a2)))
400fbf9f
JW
1756 {
1757 tree memb;
46df2823 1758 for (memb = TYPE_FIELDS (a1);
910ad8de 1759 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1760 {
1761 tree mv3 = TREE_TYPE (memb);
1762 if (mv3 && mv3 != error_mark_node
1763 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1764 mv3 = (TYPE_ATOMIC (mv3)
1765 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1766 TYPE_QUAL_ATOMIC)
1767 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1768 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1769 different_types_p))
58cb41e6
JJ
1770 break;
1771 }
3fa8871b 1772 if (memb == NULL_TREE)
400fbf9f
JW
1773 return 0;
1774 }
46df2823 1775 else if (TREE_CODE (a2) == UNION_TYPE
3fa8871b 1776 && (TYPE_NAME (a2) == NULL_TREE
ebf0bf7f 1777 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1778 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1779 && tree_int_cst_equal (TYPE_SIZE (a2),
1780 TYPE_SIZE (a1)))
400fbf9f
JW
1781 {
1782 tree memb;
46df2823 1783 for (memb = TYPE_FIELDS (a2);
910ad8de 1784 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1785 {
1786 tree mv3 = TREE_TYPE (memb);
1787 if (mv3 && mv3 != error_mark_node
1788 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1789 mv3 = (TYPE_ATOMIC (mv3)
1790 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1791 TYPE_QUAL_ATOMIC)
1792 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1793 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1794 different_types_p))
58cb41e6
JJ
1795 break;
1796 }
3fa8871b 1797 if (memb == NULL_TREE)
400fbf9f
JW
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}
400fbf9f 1812\f
a0e24419
MP
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. */
400fbf9f 1817
4e2fb7de 1818static tree
58f9752a 1819c_size_in_bytes (const_tree type)
400fbf9f
JW
1820{
1821 enum tree_code code = TREE_CODE (type);
1822
a0e24419
MP
1823 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1824 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1825 return size_one_node;
1826
400fbf9f 1827 /* Convert in case a char is more than one unit. */
db3927fb
AH
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));
400fbf9f 1831}
400fbf9f 1832\f
400fbf9f
JW
1833/* Return either DECL or its known constant value (if it has one). */
1834
56cb9733 1835tree
2f6e4e97 1836decl_constant_value (tree decl)
400fbf9f 1837{
a7c1916a 1838 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1839 in a place where a variable is invalid. Note that DECL_INITIAL
1840 isn't valid for a PARM_DECL. */
3fa8871b 1841 current_function_decl != NULL_TREE
4f976745 1842 && TREE_CODE (decl) != PARM_DECL
3f75a254 1843 && !TREE_THIS_VOLATILE (decl)
83bab8db 1844 && TREE_READONLY (decl)
3fa8871b 1845 && DECL_INITIAL (decl) != NULL_TREE
400fbf9f
JW
1846 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1847 /* This is invalid if initial value is not constant.
1848 If it has either a function call, a memory reference,
1849 or a variable, then re-evaluating it could give different results. */
1850 && TREE_CONSTANT (DECL_INITIAL (decl))
1851 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1852 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1853 return DECL_INITIAL (decl);
1854 return decl;
1855}
1856
f2a71bbc
JM
1857/* Convert the array expression EXP to a pointer. */
1858static tree
c2255bc4 1859array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1860{
f2a71bbc 1861 tree orig_exp = exp;
207bf485 1862 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1863 tree adr;
1864 tree restype = TREE_TYPE (type);
1865 tree ptrtype;
207bf485 1866
f2a71bbc 1867 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1868
f2a71bbc 1869 STRIP_TYPE_NOPS (exp);
207bf485 1870
487a92fe
JM
1871 if (TREE_NO_WARNING (orig_exp))
1872 TREE_NO_WARNING (exp) = 1;
207bf485 1873
f2a71bbc
JM
1874 ptrtype = build_pointer_type (restype);
1875
22d03525 1876 if (INDIRECT_REF_P (exp))
f2a71bbc
JM
1877 return convert (ptrtype, TREE_OPERAND (exp, 0));
1878
1f37c583
JM
1879 /* In C++ array compound literals are temporary objects unless they are
1880 const or appear in namespace scope, so they are destroyed too soon
1881 to use them for much of anything (c++/53220). */
1882 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1883 {
1884 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1885 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1886 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1887 "converting an array compound literal to a pointer "
1888 "is ill-formed in C++");
1889 }
1890
e51fbec3 1891 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
f2a71bbc
JM
1892 return convert (ptrtype, adr);
1893}
207bf485 1894
f2a71bbc
JM
1895/* Convert the function expression EXP to a pointer. */
1896static tree
c2255bc4 1897function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1898{
1899 tree orig_exp = exp;
207bf485 1900
f2a71bbc 1901 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1902
f2a71bbc 1903 STRIP_TYPE_NOPS (exp);
207bf485 1904
f2a71bbc
JM
1905 if (TREE_NO_WARNING (orig_exp))
1906 TREE_NO_WARNING (exp) = 1;
207bf485 1907
e51fbec3 1908 return build_unary_op (loc, ADDR_EXPR, exp, false);
f2a71bbc 1909}
207bf485 1910
ebfbbdc5
JJ
1911/* Mark EXP as read, not just set, for set but not used -Wunused
1912 warning purposes. */
1913
1914void
1915mark_exp_read (tree exp)
1916{
1917 switch (TREE_CODE (exp))
1918 {
1919 case VAR_DECL:
1920 case PARM_DECL:
1921 DECL_READ_P (exp) = 1;
1922 break;
1923 case ARRAY_REF:
1924 case COMPONENT_REF:
1925 case MODIFY_EXPR:
1926 case REALPART_EXPR:
1927 case IMAGPART_EXPR:
1928 CASE_CONVERT:
1929 case ADDR_EXPR:
557e8c49 1930 case VIEW_CONVERT_EXPR:
ebfbbdc5
JJ
1931 mark_exp_read (TREE_OPERAND (exp, 0));
1932 break;
1933 case COMPOUND_EXPR:
82c3c067 1934 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1935 mark_exp_read (TREE_OPERAND (exp, 1));
1936 break;
1937 default:
1938 break;
1939 }
1940}
1941
f2a71bbc
JM
1942/* Perform the default conversion of arrays and functions to pointers.
1943 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1944 return EXP.
1945
1946 LOC is the location of the expression. */
f2a71bbc
JM
1947
1948struct c_expr
c2255bc4 1949default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1950{
1951 tree orig_exp = exp.value;
1952 tree type = TREE_TYPE (exp.value);
1953 enum tree_code code = TREE_CODE (type);
1954
1955 switch (code)
1956 {
1957 case ARRAY_TYPE:
1958 {
1959 bool not_lvalue = false;
1960 bool lvalue_array_p;
1961
1962 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1963 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1964 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1965 {
1966 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1967 not_lvalue = true;
1968 exp.value = TREE_OPERAND (exp.value, 0);
1969 }
1970
1971 if (TREE_NO_WARNING (orig_exp))
1972 TREE_NO_WARNING (exp.value) = 1;
1973
1974 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1975 if (!flag_isoc99 && !lvalue_array_p)
1976 {
1977 /* Before C99, non-lvalue arrays do not decay to pointers.
1978 Normally, using such an array would be invalid; but it can
1979 be used correctly inside sizeof or as a statement expression.
1980 Thus, do not give an error here; an error will result later. */
1981 return exp;
1982 }
1983
c2255bc4 1984 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1985 }
1986 break;
1987 case FUNCTION_TYPE:
c2255bc4 1988 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1989 break;
1990 default:
f2a71bbc 1991 break;
207bf485 1992 }
f2a71bbc 1993
207bf485
JM
1994 return exp;
1995}
1996
ebfbbdc5
JJ
1997struct c_expr
1998default_function_array_read_conversion (location_t loc, struct c_expr exp)
1999{
2000 mark_exp_read (exp.value);
2001 return default_function_array_conversion (loc, exp);
2002}
522ddfa2 2003
267bac10
JM
2004/* Return whether EXPR should be treated as an atomic lvalue for the
2005 purposes of load and store handling. */
2006
2007static bool
2008really_atomic_lvalue (tree expr)
2009{
7a0ca710 2010 if (error_operand_p (expr))
267bac10
JM
2011 return false;
2012 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2013 return false;
2014 if (!lvalue_p (expr))
2015 return false;
2016
2017 /* Ignore _Atomic on register variables, since their addresses can't
2018 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2019 sequences wouldn't work. Ignore _Atomic on structures containing
2020 bit-fields, since accessing elements of atomic structures or
2021 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2022 it's undefined at translation time or execution time, and the
2023 normal atomic sequences again wouldn't work. */
2024 while (handled_component_p (expr))
2025 {
2026 if (TREE_CODE (expr) == COMPONENT_REF
2027 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2028 return false;
2029 expr = TREE_OPERAND (expr, 0);
2030 }
2031 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2032 return false;
2033 return true;
2034}
2035
2036/* Convert expression EXP (location LOC) from lvalue to rvalue,
2037 including converting functions and arrays to pointers if CONVERT_P.
2038 If READ_P, also mark the expression as having been read. */
2039
2040struct c_expr
2041convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2042 bool convert_p, bool read_p)
2043{
2044 if (read_p)
2045 mark_exp_read (exp.value);
2046 if (convert_p)
2047 exp = default_function_array_conversion (loc, exp);
2048 if (really_atomic_lvalue (exp.value))
2049 {
2050 vec<tree, va_gc> *params;
2051 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2052 tree expr_type = TREE_TYPE (exp.value);
e51fbec3 2053 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
267bac10
JM
2054 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2055
2056 gcc_assert (TYPE_ATOMIC (expr_type));
2057
2058 /* Expansion of a generic atomic load may require an addition
2059 element, so allocate enough to prevent a resize. */
2060 vec_alloc (params, 4);
2061
2062 /* Remove the qualifiers for the rest of the expressions and
2063 create the VAL temp variable to hold the RHS. */
2064 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2065 tmp = create_tmp_var_raw (nonatomic_type);
e51fbec3 2066 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
267bac10 2067 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2068 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2069
2070 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2071 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2072 params->quick_push (expr_addr);
2073 params->quick_push (tmp_addr);
2074 params->quick_push (seq_cst);
8edbfaa6 2075 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2076
cc28fc7f
MP
2077 /* EXPR is always read. */
2078 mark_exp_read (exp.value);
2079
267bac10 2080 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2081 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2082 NULL_TREE, NULL_TREE);
267bac10
JM
2083 }
2084 return exp;
2085}
2086
522ddfa2
JM
2087/* EXP is an expression of integer type. Apply the integer promotions
2088 to it and return the promoted value. */
400fbf9f
JW
2089
2090tree
522ddfa2 2091perform_integral_promotions (tree exp)
400fbf9f 2092{
b3694847
SS
2093 tree type = TREE_TYPE (exp);
2094 enum tree_code code = TREE_CODE (type);
400fbf9f 2095
522ddfa2 2096 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2097
400fbf9f
JW
2098 /* Normally convert enums to int,
2099 but convert wide enums to something wider. */
2100 if (code == ENUMERAL_TYPE)
2101 {
b0c48229
NB
2102 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2103 TYPE_PRECISION (integer_type_node)),
2104 ((TYPE_PRECISION (type)
2105 >= TYPE_PRECISION (integer_type_node))
8df83eae 2106 && TYPE_UNSIGNED (type)));
05bccae2 2107
400fbf9f
JW
2108 return convert (type, exp);
2109 }
2110
522ddfa2
JM
2111 /* ??? This should no longer be needed now bit-fields have their
2112 proper types. */
9753f113 2113 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2114 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2115 /* If it's thinner than an int, promote it like a
d72040f5 2116 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2117 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2118 TYPE_PRECISION (integer_type_node)))
f458d1d5 2119 return convert (integer_type_node, exp);
9753f113 2120
d72040f5 2121 if (c_promoting_integer_type_p (type))
400fbf9f 2122 {
f458d1d5 2123 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2124 if (TYPE_UNSIGNED (type)
f458d1d5 2125 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2126 return convert (unsigned_type_node, exp);
05bccae2 2127
400fbf9f
JW
2128 return convert (integer_type_node, exp);
2129 }
05bccae2 2130
522ddfa2
JM
2131 return exp;
2132}
2133
2134
2135/* Perform default promotions for C data used in expressions.
46bdb9cf 2136 Enumeral types or short or char are converted to int.
522ddfa2
JM
2137 In addition, manifest constants symbols are replaced by their values. */
2138
2139tree
2140default_conversion (tree exp)
2141{
2142 tree orig_exp;
2143 tree type = TREE_TYPE (exp);
2144 enum tree_code code = TREE_CODE (type);
40449a90 2145 tree promoted_type;
522ddfa2 2146
ebfbbdc5
JJ
2147 mark_exp_read (exp);
2148
46bdb9cf
JM
2149 /* Functions and arrays have been converted during parsing. */
2150 gcc_assert (code != FUNCTION_TYPE);
2151 if (code == ARRAY_TYPE)
2152 return exp;
522ddfa2
JM
2153
2154 /* Constants can be used directly unless they're not loadable. */
2155 if (TREE_CODE (exp) == CONST_DECL)
2156 exp = DECL_INITIAL (exp);
2157
522ddfa2
JM
2158 /* Strip no-op conversions. */
2159 orig_exp = exp;
2160 STRIP_TYPE_NOPS (exp);
2161
2162 if (TREE_NO_WARNING (orig_exp))
2163 TREE_NO_WARNING (exp) = 1;
2164
400fbf9f
JW
2165 if (code == VOID_TYPE)
2166 {
5436fa2e
MP
2167 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2168 "void value not ignored as it ought to be");
400fbf9f
JW
2169 return error_mark_node;
2170 }
808d6eaa 2171
4f2e1536 2172 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
808d6eaa
JM
2173 if (exp == error_mark_node)
2174 return error_mark_node;
2175
40449a90
SL
2176 promoted_type = targetm.promoted_type (type);
2177 if (promoted_type)
2178 return convert (promoted_type, exp);
2179
808d6eaa
JM
2180 if (INTEGRAL_TYPE_P (type))
2181 return perform_integral_promotions (exp);
2182
400fbf9f
JW
2183 return exp;
2184}
2185\f
0fb96aa4 2186/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2187
2188 If the component name is not found, returns NULL_TREE. Otherwise,
2189 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2190 stepping down the chain to the component, which is in the last
2191 TREE_VALUE of the list. Normally the list is of length one, but if
2192 the component is embedded within (nested) anonymous structures or
2193 unions, the list steps down the chain to the component. */
2f6e4e97 2194
2f2d13da 2195static tree
0fb96aa4 2196lookup_field (tree type, tree component)
2f2d13da
DE
2197{
2198 tree field;
2199
2200 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2201 to the field elements. Use a binary search on this array to quickly
2202 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2203 will always be set for structures which have many elements. */
2204
22a0b85f 2205 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2206 {
2207 int bot, top, half;
d07605f5 2208 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2209
2210 field = TYPE_FIELDS (type);
2211 bot = 0;
d07605f5 2212 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2213 while (top - bot > 1)
2214 {
2f2d13da
DE
2215 half = (top - bot + 1) >> 1;
2216 field = field_array[bot+half];
2217
2218 if (DECL_NAME (field) == NULL_TREE)
2219 {
2220 /* Step through all anon unions in linear fashion. */
2221 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2222 {
2f2d13da 2223 field = field_array[bot++];
3e636daf 2224 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
19d76e60 2225 {
0fb96aa4 2226 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2227
2228 if (anon)
2229 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2230
2231 /* The Plan 9 compiler permits referring
2232 directly to an anonymous struct/union field
2233 using a typedef name. */
2234 if (flag_plan9_extensions
2235 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2236 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2237 == TYPE_DECL)
2238 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2239 == component))
2240 break;
2f6e4e97 2241 }
2f2d13da
DE
2242 }
2243
2244 /* Entire record is only anon unions. */
2245 if (bot > top)
2246 return NULL_TREE;
2247
2248 /* Restart the binary search, with new lower bound. */
2249 continue;
2250 }
2251
e8b87aac 2252 if (DECL_NAME (field) == component)
2f2d13da 2253 break;
e8b87aac 2254 if (DECL_NAME (field) < component)
2f2d13da
DE
2255 bot += half;
2256 else
2257 top = bot + half;
2258 }
2259
2260 if (DECL_NAME (field_array[bot]) == component)
2261 field = field_array[bot];
2262 else if (DECL_NAME (field) != component)
e9b2c823 2263 return NULL_TREE;
2f2d13da
DE
2264 }
2265 else
2266 {
910ad8de 2267 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2268 {
e9b2c823 2269 if (DECL_NAME (field) == NULL_TREE
3e636daf 2270 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2f2d13da 2271 {
0fb96aa4 2272 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2273
e9b2c823
NB
2274 if (anon)
2275 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2276
2277 /* The Plan 9 compiler permits referring directly to an
2278 anonymous struct/union field using a typedef
2279 name. */
2280 if (flag_plan9_extensions
2281 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2282 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2283 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2284 == component))
2285 break;
2f2d13da
DE
2286 }
2287
2288 if (DECL_NAME (field) == component)
2289 break;
2290 }
e9b2c823
NB
2291
2292 if (field == NULL_TREE)
2293 return NULL_TREE;
2f2d13da
DE
2294 }
2295
e9b2c823 2296 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2297}
2298
277fe616
DM
2299/* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2300
2301static void
2302lookup_field_fuzzy_find_candidates (tree type, tree component,
2303 vec<tree> *candidates)
2304{
2305 tree field;
2306 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2307 {
2308 if (DECL_NAME (field) == NULL_TREE
3e636daf
MP
2309 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2310 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2311 candidates);
277fe616
DM
2312
2313 if (DECL_NAME (field))
2314 candidates->safe_push (DECL_NAME (field));
2315 }
2316}
2317
2318/* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2319 rather than returning a TREE_LIST for an exact match. */
2320
2321static tree
2322lookup_field_fuzzy (tree type, tree component)
2323{
2324 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2325
2326 /* First, gather a list of candidates. */
2327 auto_vec <tree> candidates;
2328
2329 lookup_field_fuzzy_find_candidates (type, component,
2330 &candidates);
2331
8ece8dfb 2332 return find_closest_identifier (component, &candidates);
277fe616
DM
2333}
2334
a87a86e1
DM
2335/* Support function for build_component_ref's error-handling.
2336
2337 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2338 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2339
2340static bool
2341should_suggest_deref_p (tree datum_type)
2342{
2343 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2344 allows "." for ptrs; we could be handling a failed attempt
2345 to access a property. */
2346 if (c_dialect_objc ())
2347 return false;
2348
2349 /* Only suggest it for pointers... */
2350 if (TREE_CODE (datum_type) != POINTER_TYPE)
2351 return false;
2352
2353 /* ...to structs/unions. */
2354 tree underlying_type = TREE_TYPE (datum_type);
2355 enum tree_code code = TREE_CODE (underlying_type);
2356 if (code == RECORD_TYPE || code == UNION_TYPE)
2357 return true;
2358 else
2359 return false;
2360}
2361
c2255bc4
AH
2362/* Make an expression to refer to the COMPONENT field of structure or
2363 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
6ffd47b7
DM
2364 location of the COMPONENT_REF. COMPONENT_LOC is the location
2365 of COMPONENT. */
400fbf9f
JW
2366
2367tree
6ffd47b7
DM
2368build_component_ref (location_t loc, tree datum, tree component,
2369 location_t component_loc)
400fbf9f 2370{
b3694847
SS
2371 tree type = TREE_TYPE (datum);
2372 enum tree_code code = TREE_CODE (type);
2373 tree field = NULL;
2374 tree ref;
1e57bf47 2375 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2376
7a3ea201
RH
2377 if (!objc_is_public (datum, component))
2378 return error_mark_node;
2379
46a88c12 2380 /* Detect Objective-C property syntax object.property. */
668ea4b1 2381 if (c_dialect_objc ()
46a88c12 2382 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2383 return ref;
2384
400fbf9f
JW
2385 /* See if there is a field or component with name COMPONENT. */
2386
2387 if (code == RECORD_TYPE || code == UNION_TYPE)
2388 {
d0f062fb 2389 if (!COMPLETE_TYPE_P (type))
400fbf9f 2390 {
4f2e1536 2391 c_incomplete_type_error (loc, NULL_TREE, type);
400fbf9f
JW
2392 return error_mark_node;
2393 }
2394
0fb96aa4 2395 field = lookup_field (type, component);
400fbf9f
JW
2396
2397 if (!field)
2398 {
277fe616
DM
2399 tree guessed_id = lookup_field_fuzzy (type, component);
2400 if (guessed_id)
6ffd47b7
DM
2401 {
2402 /* Attempt to provide a fixit replacement hint, if
2403 we have a valid range for the component. */
2404 location_t reported_loc
2405 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
264757fb 2406 gcc_rich_location rich_loc (reported_loc);
6ffd47b7 2407 if (component_loc != UNKNOWN_LOCATION)
264757fb 2408 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
6ffd47b7
DM
2409 error_at_rich_loc
2410 (&rich_loc,
2411 "%qT has no member named %qE; did you mean %qE?",
2412 type, component, guessed_id);
2413 }
277fe616
DM
2414 else
2415 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2416 return error_mark_node;
2417 }
400fbf9f 2418
fda5652f
MP
2419 /* Accessing elements of atomic structures or unions is undefined
2420 behavior (C11 6.5.2.3#5). */
2421 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2422 {
2423 if (code == RECORD_TYPE)
2424 warning_at (loc, 0, "accessing a member %qE of an atomic "
2425 "structure %qE", component, datum);
2426 else
2427 warning_at (loc, 0, "accessing a member %qE of an atomic "
2428 "union %qE", component, datum);
2429 }
2430
e9b2c823
NB
2431 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2432 This might be better solved in future the way the C++ front
2433 end does it - by giving the anonymous entities each a
2434 separate name and type, and then have build_component_ref
2435 recursively call itself. We can't do that here. */
46ea50cb 2436 do
19d76e60 2437 {
e9b2c823 2438 tree subdatum = TREE_VALUE (field);
efed193e
JM
2439 int quals;
2440 tree subtype;
1e57bf47 2441 bool use_datum_quals;
e9b2c823
NB
2442
2443 if (TREE_TYPE (subdatum) == error_mark_node)
2444 return error_mark_node;
2445
1e57bf47
JM
2446 /* If this is an rvalue, it does not have qualifiers in C
2447 standard terms and we must avoid propagating such
2448 qualifiers down to a non-lvalue array that is then
2449 converted to a pointer. */
2450 use_datum_quals = (datum_lvalue
2451 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2452
efed193e 2453 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2454 if (use_datum_quals)
2455 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2456 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2457
2458 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2459 NULL_TREE);
c2255bc4 2460 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2461 if (TREE_READONLY (subdatum)
2462 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2463 TREE_READONLY (ref) = 1;
1e57bf47
JM
2464 if (TREE_THIS_VOLATILE (subdatum)
2465 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2466 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2467
2468 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2469 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2470
19d76e60 2471 datum = ref;
46ea50cb
RS
2472
2473 field = TREE_CHAIN (field);
19d76e60 2474 }
46ea50cb 2475 while (field);
19d76e60 2476
400fbf9f
JW
2477 return ref;
2478 }
a87a86e1
DM
2479 else if (should_suggest_deref_p (type))
2480 {
2481 /* Special-case the error message for "ptr.field" for the case
2482 where the user has confused "." vs "->". */
2483 rich_location richloc (line_table, loc);
2484 /* "loc" should be the "." token. */
f9087798 2485 richloc.add_fixit_replace ("->");
a87a86e1
DM
2486 error_at_rich_loc (&richloc,
2487 "%qE is a pointer; did you mean to use %<->%>?",
2488 datum);
2489 return error_mark_node;
2490 }
400fbf9f 2491 else if (code != ERROR_MARK)
c2255bc4
AH
2492 error_at (loc,
2493 "request for member %qE in something not a structure or union",
2494 component);
400fbf9f
JW
2495
2496 return error_mark_node;
2497}
2498\f
2499/* Given an expression PTR for a pointer, return an expression
2500 for the value pointed to.
6a3799eb
AH
2501 ERRORSTRING is the name of the operator to appear in error messages.
2502
2503 LOC is the location to use for the generated tree. */
400fbf9f
JW
2504
2505tree
dd865ef6 2506build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2507{
b3694847
SS
2508 tree pointer = default_conversion (ptr);
2509 tree type = TREE_TYPE (pointer);
6a3799eb 2510 tree ref;
400fbf9f
JW
2511
2512 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2513 {
1043771b 2514 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2515 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2516 {
2517 /* If a warning is issued, mark it to avoid duplicates from
2518 the backend. This only needs to be done at
2519 warn_strict_aliasing > 2. */
2520 if (warn_strict_aliasing > 2)
2521 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2522 type, TREE_OPERAND (pointer, 0)))
2523 TREE_NO_WARNING (pointer) = 1;
2524 }
2525
870cc33b 2526 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2527 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2528 == TREE_TYPE (type)))
6a3799eb
AH
2529 {
2530 ref = TREE_OPERAND (pointer, 0);
2531 protected_set_expr_location (ref, loc);
2532 return ref;
2533 }
870cc33b
RS
2534 else
2535 {
2536 tree t = TREE_TYPE (type);
46df2823 2537
984dfd8c 2538 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2539
baae9b65 2540 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2541 {
d9b7be2e
MP
2542 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2543 {
2544 error_at (loc, "dereferencing pointer to incomplete type "
2545 "%qT", t);
2546 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2547 }
870cc33b
RS
2548 return error_mark_node;
2549 }
7d882b83 2550 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2551 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2552
2553 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2554 so that we get the proper error message if the result is used
2555 to assign to. Also, &* is supposed to be a no-op.
2556 And ANSI C seems to specify that the type of the result
2557 should be the const type. */
2558 /* A de-reference of a pointer to const is not a const. It is valid
2559 to change it via some other pointer. */
2560 TREE_READONLY (ref) = TYPE_READONLY (t);
2561 TREE_SIDE_EFFECTS (ref)
271bd540 2562 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2563 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2564 protected_set_expr_location (ref, loc);
870cc33b
RS
2565 return ref;
2566 }
2567 }
400fbf9f 2568 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2569 invalid_indirection_error (loc, type, errstring);
2570
400fbf9f
JW
2571 return error_mark_node;
2572}
2573
2574/* This handles expressions of the form "a[i]", which denotes
2575 an array reference.
2576
2577 This is logically equivalent in C to *(a+i), but we may do it differently.
2578 If A is a variable or a member, we generate a primitive ARRAY_REF.
2579 This avoids forcing the array out of registers, and can work on
2580 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2581 by functions).
2582
30cd1c5d
AS
2583 For vector types, allow vector[i] but not i[vector], and create
2584 *(((type*)&vectortype) + i) for the expression.
2585
6a3799eb 2586 LOC is the location to use for the returned expression. */
400fbf9f
JW
2587
2588tree
c2255bc4 2589build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2590{
6a3799eb 2591 tree ret;
a4ab7973 2592 bool swapped = false;
400fbf9f
JW
2593 if (TREE_TYPE (array) == error_mark_node
2594 || TREE_TYPE (index) == error_mark_node)
2595 return error_mark_node;
2596
b72271b9 2597 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2598 {
2599 size_t rank = 0;
2600 if (!find_rank (loc, index, index, true, &rank))
2601 return error_mark_node;
2602 if (rank > 1)
2603 {
2604 error_at (loc, "rank of the array's index is greater than 1");
2605 return error_mark_node;
2606 }
2607 }
a4ab7973 2608 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2609 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2610 /* Allow vector[index] but not index[vector]. */
31521951 2611 && !VECTOR_TYPE_P (TREE_TYPE (array)))
400fbf9f 2612 {
a4ab7973
JM
2613 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2614 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2615 {
f90e8e2e 2616 error_at (loc,
30cd1c5d
AS
2617 "subscripted value is neither array nor pointer nor vector");
2618
fdeefd49
RS
2619 return error_mark_node;
2620 }
fab27f52 2621 std::swap (array, index);
a4ab7973
JM
2622 swapped = true;
2623 }
2624
2625 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2626 {
a63068b6 2627 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2628 return error_mark_node;
2629 }
2630
2631 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2632 {
a63068b6 2633 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2634 return error_mark_node;
2635 }
2636
ff6b6641
GDR
2637 /* ??? Existing practice has been to warn only when the char
2638 index is syntactically the index, not for char[array]. */
2639 if (!swapped)
5bd012f8 2640 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2641
2642 /* Apply default promotions *after* noticing character types. */
2643 index = default_conversion (index);
f406ae1f
MP
2644 if (index == error_mark_node)
2645 return error_mark_node;
a4ab7973
JM
2646
2647 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2648
f17a223d
RB
2649 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2650 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
a4ab7973
JM
2651
2652 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2653 {
e4d35515 2654 tree rval, type;
fdeefd49 2655
400fbf9f
JW
2656 /* An array that is indexed by a non-constant
2657 cannot be stored in a register; we must be able to do
2658 address arithmetic on its address.
2659 Likewise an array of elements of variable size. */
2660 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2661 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2662 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2663 {
a9e4a1a5 2664 if (!c_mark_addressable (array, true))
400fbf9f
JW
2665 return error_mark_node;
2666 }
e6d52559
JW
2667 /* An array that is indexed by a constant value which is not within
2668 the array bounds cannot be stored in a register either; because we
2669 would get a crash in store_bit_field/extract_bit_field when trying
2670 to access a non-existent part of the register. */
2671 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2672 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2673 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2674 {
dffd7eb6 2675 if (!c_mark_addressable (array))
e6d52559
JW
2676 return error_mark_node;
2677 }
400fbf9f 2678
f17a223d
RB
2679 if ((pedantic || warn_c90_c99_compat)
2680 && ! was_vector)
400fbf9f
JW
2681 {
2682 tree foo = array;
2683 while (TREE_CODE (foo) == COMPONENT_REF)
2684 foo = TREE_OPERAND (foo, 0);
0ae9bd27 2685 if (VAR_P (foo) && C_DECL_REGISTER (foo))
c1771a20 2686 pedwarn (loc, OPT_Wpedantic,
fcf73884 2687 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2688 else if (!lvalue_p (foo))
2689 pedwarn_c90 (loc, OPT_Wpedantic,
2690 "ISO C90 forbids subscripting non-lvalue "
2691 "array");
400fbf9f
JW
2692 }
2693
46df2823 2694 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2695 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2696 /* Array ref is const/volatile if the array elements are
c22cacf3 2697 or if the array is. */
400fbf9f
JW
2698 TREE_READONLY (rval)
2699 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2700 | TREE_READONLY (array));
2701 TREE_SIDE_EFFECTS (rval)
2702 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2703 | TREE_SIDE_EFFECTS (array));
2704 TREE_THIS_VOLATILE (rval)
2705 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2706 /* This was added by rms on 16 Nov 91.
2f6e4e97 2707 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2708 in an inline function.
2709 Hope it doesn't break something else. */
2710 | TREE_THIS_VOLATILE (array));
4f2e1536 2711 ret = require_complete_type (loc, rval);
6a3799eb 2712 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2713 if (non_lvalue)
2714 ret = non_lvalue_loc (loc, ret);
6a3799eb 2715 return ret;
400fbf9f 2716 }
a4ab7973
JM
2717 else
2718 {
2719 tree ar = default_conversion (array);
400fbf9f 2720
a4ab7973
JM
2721 if (ar == error_mark_node)
2722 return ar;
400fbf9f 2723
a4ab7973
JM
2724 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2725 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2726
aa7da51a 2727 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
30af3a2b 2728 index, false),
aa7da51a
JJ
2729 RO_ARRAY_INDEXING);
2730 if (non_lvalue)
2731 ret = non_lvalue_loc (loc, ret);
2732 return ret;
a4ab7973 2733 }
400fbf9f
JW
2734}
2735\f
7e585d16 2736/* Build an external reference to identifier ID. FUN indicates
766beb40 2737 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2738 location of the identifier. This sets *TYPE to the type of the
2739 identifier, which is not the same as the type of the returned value
2740 for CONST_DECLs defined as enum constants. If the type of the
2741 identifier is not available, *TYPE is set to NULL. */
7e585d16 2742tree
30af3a2b 2743build_external_ref (location_t loc, tree id, bool fun, tree *type)
7e585d16
ZW
2744{
2745 tree ref;
2746 tree decl = lookup_name (id);
16b34ad6
ZL
2747
2748 /* In Objective-C, an instance variable (ivar) may be preferred to
2749 whatever lookup_name() found. */
2750 decl = objc_lookup_ivar (decl, id);
7e585d16 2751
6866c6e8 2752 *type = NULL;
339a28b9 2753 if (decl && decl != error_mark_node)
6866c6e8
ILT
2754 {
2755 ref = decl;
2756 *type = TREE_TYPE (ref);
2757 }
339a28b9
ZW
2758 else if (fun)
2759 /* Implicit function declaration. */
c2255bc4 2760 ref = implicitly_declare (loc, id);
339a28b9
ZW
2761 else if (decl == error_mark_node)
2762 /* Don't complain about something that's already been
2763 complained about. */
2764 return error_mark_node;
2765 else
2766 {
c2255bc4 2767 undeclared_variable (loc, id);
339a28b9
ZW
2768 return error_mark_node;
2769 }
7e585d16
ZW
2770
2771 if (TREE_TYPE (ref) == error_mark_node)
2772 return error_mark_node;
2773
339a28b9 2774 if (TREE_DEPRECATED (ref))
9b86d6bb 2775 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2776
ad960f56 2777 /* Recursive call does not count as usage. */
b8698a0f 2778 if (ref != current_function_decl)
ad960f56 2779 {
ad960f56
MLI
2780 TREE_USED (ref) = 1;
2781 }
7e585d16 2782
bc4b653b
JM
2783 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2784 {
2785 if (!in_sizeof && !in_typeof)
2786 C_DECL_USED (ref) = 1;
3fa8871b 2787 else if (DECL_INITIAL (ref) == NULL_TREE
bc4b653b
JM
2788 && DECL_EXTERNAL (ref)
2789 && !TREE_PUBLIC (ref))
2790 record_maybe_used_decl (ref);
2791 }
2792
7e585d16
ZW
2793 if (TREE_CODE (ref) == CONST_DECL)
2794 {
6193b8b7 2795 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2796
2797 if (warn_cxx_compat
2798 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2799 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2800 {
2801 warning_at (loc, OPT_Wc___compat,
2802 ("enum constant defined in struct or union "
2803 "is not visible in C++"));
2804 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2805 }
2806
7e585d16
ZW
2807 ref = DECL_INITIAL (ref);
2808 TREE_CONSTANT (ref) = 1;
2809 }
3fa8871b 2810 else if (current_function_decl != NULL_TREE
4b1e44be 2811 && !DECL_FILE_SCOPE_P (current_function_decl)
21b634ae
MP
2812 && (VAR_OR_FUNCTION_DECL_P (ref)
2813 || TREE_CODE (ref) == PARM_DECL))
6a29edea
EB
2814 {
2815 tree context = decl_function_context (ref);
2f6e4e97 2816
3fa8871b 2817 if (context != NULL_TREE && context != current_function_decl)
6a29edea
EB
2818 DECL_NONLOCAL (ref) = 1;
2819 }
71113fcd
GK
2820 /* C99 6.7.4p3: An inline definition of a function with external
2821 linkage ... shall not contain a reference to an identifier with
2822 internal linkage. */
3fa8871b 2823 else if (current_function_decl != NULL_TREE
71113fcd
GK
2824 && DECL_DECLARED_INLINE_P (current_function_decl)
2825 && DECL_EXTERNAL (current_function_decl)
2826 && VAR_OR_FUNCTION_DECL_P (ref)
0ae9bd27 2827 && (!VAR_P (ref) || TREE_STATIC (ref))
1033ffa8
JJ
2828 && ! TREE_PUBLIC (ref)
2829 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2830 record_inline_static (loc, current_function_decl, ref,
2831 csi_internal);
7e585d16
ZW
2832
2833 return ref;
2834}
2835
bc4b653b
JM
2836/* Record details of decls possibly used inside sizeof or typeof. */
2837struct maybe_used_decl
2838{
2839 /* The decl. */
2840 tree decl;
2841 /* The level seen at (in_sizeof + in_typeof). */
2842 int level;
2843 /* The next one at this level or above, or NULL. */
2844 struct maybe_used_decl *next;
2845};
2846
2847static struct maybe_used_decl *maybe_used_decls;
2848
2849/* Record that DECL, an undefined static function reference seen
2850 inside sizeof or typeof, might be used if the operand of sizeof is
2851 a VLA type or the operand of typeof is a variably modified
2852 type. */
2853
4e2fb7de 2854static void
bc4b653b
JM
2855record_maybe_used_decl (tree decl)
2856{
2857 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2858 t->decl = decl;
2859 t->level = in_sizeof + in_typeof;
2860 t->next = maybe_used_decls;
2861 maybe_used_decls = t;
2862}
2863
2864/* Pop the stack of decls possibly used inside sizeof or typeof. If
2865 USED is false, just discard them. If it is true, mark them used
2866 (if no longer inside sizeof or typeof) or move them to the next
2867 level up (if still inside sizeof or typeof). */
2868
2869void
2870pop_maybe_used (bool used)
2871{
2872 struct maybe_used_decl *p = maybe_used_decls;
2873 int cur_level = in_sizeof + in_typeof;
2874 while (p && p->level > cur_level)
2875 {
2876 if (used)
2877 {
2878 if (cur_level == 0)
2879 C_DECL_USED (p->decl) = 1;
2880 else
2881 p->level = cur_level;
2882 }
2883 p = p->next;
2884 }
2885 if (!used || cur_level == 0)
2886 maybe_used_decls = p;
2887}
2888
2889/* Return the result of sizeof applied to EXPR. */
2890
2891struct c_expr
c2255bc4 2892c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2893{
2894 struct c_expr ret;
ad97f4be
JM
2895 if (expr.value == error_mark_node)
2896 {
2897 ret.value = error_mark_node;
2898 ret.original_code = ERROR_MARK;
6866c6e8 2899 ret.original_type = NULL;
ad97f4be
JM
2900 pop_maybe_used (false);
2901 }
2902 else
2903 {
928c19bb 2904 bool expr_const_operands = true;
773ec47f
MP
2905
2906 if (TREE_CODE (expr.value) == PARM_DECL
2907 && C_ARRAY_PARAMETER (expr.value))
2908 {
2909 if (warning_at (loc, OPT_Wsizeof_array_argument,
2910 "%<sizeof%> on array function parameter %qE will "
2911 "return size of %qT", expr.value,
267bbb6f 2912 TREE_TYPE (expr.value)))
773ec47f
MP
2913 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2914 }
928c19bb
JM
2915 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2916 &expr_const_operands);
c2255bc4 2917 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7 2918 c_last_sizeof_arg = expr.value;
40ffd95f 2919 c_last_sizeof_loc = loc;
1a4049e7 2920 ret.original_code = SIZEOF_EXPR;
6866c6e8 2921 ret.original_type = NULL;
928c19bb 2922 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2923 {
2924 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2925 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2926 folded_expr, ret.value);
2927 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2928 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2929 }
928c19bb 2930 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2931 }
bc4b653b
JM
2932 return ret;
2933}
2934
2935/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2936 name passed to sizeof (rather than the type itself). LOC is the
2937 location of the original expression. */
bc4b653b
JM
2938
2939struct c_expr
c2255bc4 2940c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2941{
2942 tree type;
2943 struct c_expr ret;
928c19bb
JM
2944 tree type_expr = NULL_TREE;
2945 bool type_expr_const = true;
2946 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2947 ret.value = c_sizeof (loc, type);
1a4049e7 2948 c_last_sizeof_arg = type;
40ffd95f 2949 c_last_sizeof_loc = loc;
1a4049e7 2950 ret.original_code = SIZEOF_EXPR;
6866c6e8 2951 ret.original_type = NULL;
24070fcb
JM
2952 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2953 && c_vla_type_p (type))
928c19bb 2954 {
24070fcb
JM
2955 /* If the type is a [*] array, it is a VLA but is represented as
2956 having a size of zero. In such a case we must ensure that
2957 the result of sizeof does not get folded to a constant by
2958 c_fully_fold, because if the size is evaluated the result is
2959 not constant and so constraints on zero or negative size
2960 arrays must not be applied when this sizeof call is inside
2961 another array declarator. */
2962 if (!type_expr)
2963 type_expr = integer_zero_node;
928c19bb
JM
2964 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2965 type_expr, ret.value);
2966 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2967 }
16464cc1
VR
2968 pop_maybe_used (type != error_mark_node
2969 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2970 return ret;
2971}
2972
400fbf9f 2973/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2974 The function call is at LOC.
400fbf9f
JW
2975 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2976 TREE_VALUE of each node is a parameter-expression.
2977 FUNCTION's data type may be a function type or a pointer-to-function. */
2978
2979tree
c2255bc4 2980build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2981{
9771b263 2982 vec<tree, va_gc> *v;
bbbbb16a
ILT
2983 tree ret;
2984
9771b263 2985 vec_alloc (v, list_length (params));
bbbbb16a 2986 for (; params; params = TREE_CHAIN (params))
9771b263 2987 v->quick_push (TREE_VALUE (params));
8edbfaa6 2988 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2989 vec_free (v);
bbbbb16a
ILT
2990 return ret;
2991}
2992
ae52741c
MLI
2993/* Give a note about the location of the declaration of DECL. */
2994
c7b70a3c
MP
2995static void
2996inform_declaration (tree decl)
ae52741c 2997{
c7b70a3c 2998 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
ae52741c
MLI
2999 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3000}
3001
bbbbb16a
ILT
3002/* Build a function call to function FUNCTION with parameters PARAMS.
3003 ORIGTYPES, if not NULL, is a vector of types; each element is
3004 either NULL or the original type of the corresponding element in
3005 PARAMS. The original type may differ from TREE_TYPE of the
3006 parameter for enums. FUNCTION's data type may be a function type
3007 or pointer-to-function. This function changes the elements of
3008 PARAMS. */
3009
3010tree
81e5eca8
MP
3011build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3012 tree function, vec<tree, va_gc> *params,
9771b263 3013 vec<tree, va_gc> *origtypes)
400fbf9f 3014{
3fa8871b 3015 tree fntype, fundecl = NULL_TREE;
4977bab6 3016 tree name = NULL_TREE, result;
c96f4f73 3017 tree tem;
94a0dd7b
SL
3018 int nargs;
3019 tree *argarray;
b8698a0f 3020
400fbf9f 3021
fc76e425 3022 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 3023 STRIP_TYPE_NOPS (function);
400fbf9f
JW
3024
3025 /* Convert anything with function type to a pointer-to-function. */
3026 if (TREE_CODE (function) == FUNCTION_DECL)
3027 {
3028 name = DECL_NAME (function);
0a35513e
AH
3029
3030 if (flag_tm)
3031 tm_malloc_replacement (function);
a5eadacc 3032 fundecl = function;
86951993
AM
3033 /* Atomic functions have type checking/casting already done. They are
3034 often rewritten and don't match the original parameter list. */
3035 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3036 origtypes = NULL;
36536d79 3037
b72271b9 3038 if (flag_cilkplus
36536d79
BI
3039 && is_cilkplus_reduce_builtin (function))
3040 origtypes = NULL;
400fbf9f 3041 }
f2a71bbc 3042 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 3043 function = function_to_pointer_conversion (loc, function);
400fbf9f 3044
6e955430
ZL
3045 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3046 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
3047 if (params && !params->is_empty ())
3048 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 3049
928c19bb
JM
3050 function = c_fully_fold (function, false, NULL);
3051
400fbf9f
JW
3052 fntype = TREE_TYPE (function);
3053
3054 if (TREE_CODE (fntype) == ERROR_MARK)
3055 return error_mark_node;
3056
3057 if (!(TREE_CODE (fntype) == POINTER_TYPE
3058 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3059 {
ae52741c
MLI
3060 if (!flag_diagnostics_show_caret)
3061 error_at (loc,
3062 "called object %qE is not a function or function pointer",
3063 function);
3064 else if (DECL_P (function))
3065 {
3066 error_at (loc,
3067 "called object %qD is not a function or function pointer",
3068 function);
3069 inform_declaration (function);
3070 }
3071 else
3072 error_at (loc,
3073 "called object is not a function or function pointer");
400fbf9f
JW
3074 return error_mark_node;
3075 }
3076
5ce89b2e
JM
3077 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3078 current_function_returns_abnormally = 1;
3079
400fbf9f
JW
3080 /* fntype now gets the type of function pointed to. */
3081 fntype = TREE_TYPE (fntype);
3082
ab4194da
JM
3083 /* Convert the parameters to the types declared in the
3084 function prototype, or apply default promotions. */
3085
81e5eca8
MP
3086 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3087 origtypes, function, fundecl);
ab4194da
JM
3088 if (nargs < 0)
3089 return error_mark_node;
3090
c96f4f73 3091 /* Check that the function is called through a compatible prototype.
fa337f3a 3092 If it is not, warn. */
1043771b 3093 if (CONVERT_EXPR_P (function)
c96f4f73
EB
3094 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3095 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 3096 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
3097 {
3098 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
3099
3100 /* This situation leads to run-time undefined behavior. We can't,
3101 therefore, simply error unless we can prove that all possible
3102 executions of the program must execute the code. */
fa337f3a 3103 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 3104
fa337f3a
RB
3105 if (VOID_TYPE_P (return_type)
3106 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3107 pedwarn (loc, 0,
3108 "function with qualified void return type called");
3109 }
c96f4f73 3110
9771b263 3111 argarray = vec_safe_address (params);
bbbbb16a 3112
83322951
RG
3113 /* Check that arguments to builtin functions match the expectations. */
3114 if (fundecl
3115 && DECL_BUILT_IN (fundecl)
3116 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
79ce98bc
MP
3117 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3118 argarray))
83322951 3119 return error_mark_node;
400fbf9f 3120
83322951 3121 /* Check that the arguments to the function are valid. */
4227c9ad 3122 bool warned_p = check_function_arguments (loc, fundecl, fntype,
2f687306 3123 nargs, argarray, &arg_loc);
400fbf9f 3124
928c19bb
JM
3125 if (name != NULL_TREE
3126 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 3127 {
928c19bb 3128 if (require_constant_value)
0dba7960
JJ
3129 result
3130 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3131 function, nargs, argarray);
928c19bb 3132 else
db3927fb
AH
3133 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3134 function, nargs, argarray);
928c19bb
JM
3135 if (TREE_CODE (result) == NOP_EXPR
3136 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3137 STRIP_TYPE_NOPS (result);
bf730f15
RS
3138 }
3139 else
db3927fb
AH
3140 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3141 function, nargs, argarray);
0dba7960
JJ
3142 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3143 later. */
3144 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3145 TREE_NO_WARNING (result) = 1;
b0b3afb2 3146
ab4c578f
MP
3147 /* In this improbable scenario, a nested function returns a VM type.
3148 Create a TARGET_EXPR so that the call always has a LHS, much as
3149 what the C++ FE does for functions returning non-PODs. */
3150 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3151 {
3152 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3153 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3154 NULL_TREE, NULL_TREE);
3155 }
3156
71653180 3157 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
3158 {
3159 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 3160 pedwarn (loc, 0,
3ce62965
JM
3161 "function with qualified void return type called");
3162 return result;
3163 }
4f2e1536 3164 return require_complete_type (loc, result);
400fbf9f 3165}
8edbfaa6
JJ
3166
3167/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3168
3169tree
3170c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3171 tree function, vec<tree, va_gc> *params,
3172 vec<tree, va_gc> *origtypes)
3173{
3174 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3175 STRIP_TYPE_NOPS (function);
3176
3177 /* Convert anything with function type to a pointer-to-function. */
3178 if (TREE_CODE (function) == FUNCTION_DECL)
3179 {
3180 /* Implement type-directed function overloading for builtins.
3181 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3182 handle all the type checking. The result is a complete expression
3183 that implements this function call. */
3184 tree tem = resolve_overloaded_builtin (loc, function, params);
3185 if (tem)
3186 return tem;
3187 }
3188 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3189}
400fbf9f 3190\f
bbbbb16a
ILT
3191/* Convert the argument expressions in the vector VALUES
3192 to the types in the list TYPELIST.
400fbf9f
JW
3193
3194 If TYPELIST is exhausted, or when an element has NULL as its type,
3195 perform the default conversions.
3196
bbbbb16a
ILT
3197 ORIGTYPES is the original types of the expressions in VALUES. This
3198 holds the type of enum values which have been converted to integral
3199 types. It may be NULL.
400fbf9f 3200
03dafa61
JM
3201 FUNCTION is a tree for the called function. It is used only for
3202 error messages, where it is formatted with %qE.
400fbf9f
JW
3203
3204 This is also where warnings about wrong number of args are generated.
3205
81e5eca8
MP
3206 ARG_LOC are locations of function arguments (if any).
3207
94a0dd7b 3208 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3209 than the length of VALUES in some error situations), or -1 on
3210 failure. */
94a0dd7b
SL
3211
3212static int
81e5eca8
MP
3213convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3214 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3215 tree function, tree fundecl)
400fbf9f 3216{
bbbbb16a
ILT
3217 tree typetail, val;
3218 unsigned int parmnum;
06302a02 3219 bool error_args = false;
b5d32c25 3220 const bool type_generic = fundecl
81e5eca8 3221 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3222 bool type_generic_remove_excess_precision = false;
44a845ca 3223 bool type_generic_overflow_p = false;
03dafa61 3224 tree selector;
03dafa61 3225
2ac2f164
JM
3226 /* Change pointer to function to the function itself for
3227 diagnostics. */
03dafa61
JM
3228 if (TREE_CODE (function) == ADDR_EXPR
3229 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3230 function = TREE_OPERAND (function, 0);
03dafa61
JM
3231
3232 /* Handle an ObjC selector specially for diagnostics. */
3233 selector = objc_message_selector ();
400fbf9f 3234
8ce94e44
JM
3235 /* For type-generic built-in functions, determine whether excess
3236 precision should be removed (classification) or not
3237 (comparison). */
3238 if (type_generic
3239 && DECL_BUILT_IN (fundecl)
3240 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3241 {
3242 switch (DECL_FUNCTION_CODE (fundecl))
3243 {
3244 case BUILT_IN_ISFINITE:
3245 case BUILT_IN_ISINF:
3246 case BUILT_IN_ISINF_SIGN:
3247 case BUILT_IN_ISNAN:
3248 case BUILT_IN_ISNORMAL:
3249 case BUILT_IN_FPCLASSIFY:
3250 type_generic_remove_excess_precision = true;
3251 break;
3252
44a845ca
MS
3253 case BUILT_IN_ADD_OVERFLOW_P:
3254 case BUILT_IN_SUB_OVERFLOW_P:
3255 case BUILT_IN_MUL_OVERFLOW_P:
3256 /* The last argument of these type-generic builtins
3257 should not be promoted. */
3258 type_generic_overflow_p = true;
3259 break;
3260
8ce94e44 3261 default:
8ce94e44
JM
3262 break;
3263 }
3264 }
b72271b9 3265 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3266 return vec_safe_length (values);
8ce94e44 3267
400fbf9f 3268 /* Scan the given expressions and types, producing individual
bbbbb16a 3269 converted arguments. */
400fbf9f 3270
bbbbb16a 3271 for (typetail = typelist, parmnum = 0;
9771b263 3272 values && values->iterate (parmnum, &val);
bbbbb16a 3273 ++parmnum)
400fbf9f 3274 {
b3694847 3275 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3276 tree valtype = TREE_TYPE (val);
03dafa61
JM
3277 tree rname = function;
3278 int argnum = parmnum + 1;
4d3e6fae 3279 const char *invalid_func_diag;
8ce94e44 3280 bool excess_precision = false;
928c19bb 3281 bool npc;
bbbbb16a 3282 tree parmval;
5c1bc275
MP
3283 /* Some __atomic_* builtins have additional hidden argument at
3284 position 0. */
3285 location_t ploc
3286 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3287 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3288 : input_location;
400fbf9f
JW
3289
3290 if (type == void_type_node)
3291 {
19dc6d01 3292 if (selector)
68fca595 3293 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3294 else
68fca595 3295 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3296 inform_declaration (fundecl);
d38f7dce 3297 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3298 }
3299
03dafa61
JM
3300 if (selector && argnum > 2)
3301 {
3302 rname = selector;
3303 argnum -= 2;
3304 }
3305
928c19bb 3306 npc = null_pointer_constant_p (val);
8ce94e44
JM
3307
3308 /* If there is excess precision and a prototype, convert once to
3309 the required type rather than converting via the semantic
3310 type. Likewise without a prototype a float value represented
3311 as long double should be converted once to double. But for
3312 type-generic classification functions excess precision must
3313 be removed here. */
3314 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3315 && (type || !type_generic || !type_generic_remove_excess_precision))
3316 {
3317 val = TREE_OPERAND (val, 0);
3318 excess_precision = true;
3319 }
928c19bb 3320 val = c_fully_fold (val, false, NULL);
ed248cf7 3321 STRIP_TYPE_NOPS (val);
400fbf9f 3322
4f2e1536 3323 val = require_complete_type (ploc, val);
400fbf9f 3324
c65699ef
JM
3325 /* Some floating-point arguments must be promoted to double when
3326 no type is specified by a prototype. This applies to
3327 arguments of type float, and to architecture-specific types
3328 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3329 bool promote_float_arg = false;
3330 if (type == NULL_TREE
3331 && TREE_CODE (valtype) == REAL_TYPE
3332 && (TYPE_PRECISION (valtype)
3333 <= TYPE_PRECISION (double_type_node))
3334 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3335 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3336 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3337 {
3338 /* Promote this argument, unless it has a _FloatN or
3339 _FloatNx type. */
3340 promote_float_arg = true;
3341 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3342 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3343 {
3344 promote_float_arg = false;
3345 break;
3346 }
3347 }
3348
3fa8871b 3349 if (type != NULL_TREE)
400fbf9f
JW
3350 {
3351 /* Formal parm type is specified by a function prototype. */
400fbf9f 3352
20913689 3353 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3354 {
5c1bc275
MP
3355 error_at (ploc, "type of formal parameter %d is incomplete",
3356 parmnum + 1);
400fbf9f
JW
3357 parmval = val;
3358 }
3359 else
3360 {
bbbbb16a
ILT
3361 tree origtype;
3362
d45cf215
RS
3363 /* Optionally warn about conversions that
3364 differ from the default conversions. */
05170031 3365 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3366 {
e3a64162 3367 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3368
aae43c5f 3369 if (INTEGRAL_TYPE_P (type)
8ce94e44 3370 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3371 warning_at (ploc, OPT_Wtraditional_conversion,
3372 "passing argument %d of %qE as integer rather "
3373 "than floating due to prototype",
3374 argnum, rname);
03829ad2 3375 if (INTEGRAL_TYPE_P (type)
8ce94e44 3376 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3377 warning_at (ploc, OPT_Wtraditional_conversion,
3378 "passing argument %d of %qE as integer rather "
3379 "than complex due to prototype",
3380 argnum, rname);
aae43c5f 3381 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3382 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3383 warning_at (ploc, OPT_Wtraditional_conversion,
3384 "passing argument %d of %qE as complex rather "
3385 "than floating due to prototype",
3386 argnum, rname);
400fbf9f 3387 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3388 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3389 warning_at (ploc, OPT_Wtraditional_conversion,
3390 "passing argument %d of %qE as floating rather "
3391 "than integer due to prototype",
3392 argnum, rname);
03829ad2 3393 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3394 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3395 warning_at (ploc, OPT_Wtraditional_conversion,
3396 "passing argument %d of %qE as complex rather "
3397 "than integer due to prototype",
3398 argnum, rname);
aae43c5f 3399 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3400 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3401 warning_at (ploc, OPT_Wtraditional_conversion,
3402 "passing argument %d of %qE as floating rather "
3403 "than complex due to prototype",
3404 argnum, rname);
aae43c5f
RK
3405 /* ??? At some point, messages should be written about
3406 conversions between complex types, but that's too messy
3407 to do now. */
d45cf215 3408 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3409 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3410 {
3411 /* Warn if any argument is passed as `float',
047de90b 3412 since without a prototype it would be `double'. */
9a8ce21f
JG
3413 if (formal_prec == TYPE_PRECISION (float_type_node)
3414 && type != dfloat32_type_node)
5c1bc275
MP
3415 warning_at (ploc, 0,
3416 "passing argument %d of %qE as %<float%> "
3417 "rather than %<double%> due to prototype",
3418 argnum, rname);
9a8ce21f
JG
3419
3420 /* Warn if mismatch between argument and prototype
3421 for decimal float types. Warn of conversions with
3422 binary float types and of precision narrowing due to
3423 prototype. */
8ce94e44 3424 else if (type != valtype
9a8ce21f
JG
3425 && (type == dfloat32_type_node
3426 || type == dfloat64_type_node
c22cacf3 3427 || type == dfloat128_type_node
8ce94e44
JM
3428 || valtype == dfloat32_type_node
3429 || valtype == dfloat64_type_node
3430 || valtype == dfloat128_type_node)
c22cacf3 3431 && (formal_prec
8ce94e44 3432 <= TYPE_PRECISION (valtype)
9a8ce21f 3433 || (type == dfloat128_type_node
8ce94e44 3434 && (valtype
c22cacf3 3435 != dfloat64_type_node
8ce94e44 3436 && (valtype
9a8ce21f
JG
3437 != dfloat32_type_node)))
3438 || (type == dfloat64_type_node
8ce94e44 3439 && (valtype
9a8ce21f 3440 != dfloat32_type_node))))
5c1bc275
MP
3441 warning_at (ploc, 0,
3442 "passing argument %d of %qE as %qT "
3443 "rather than %qT due to prototype",
3444 argnum, rname, type, valtype);
9a8ce21f 3445
d45cf215 3446 }
3ed56f8a
KG
3447 /* Detect integer changing in width or signedness.
3448 These warnings are only activated with
05170031 3449 -Wtraditional-conversion, not with -Wtraditional. */
61ac5ebe
MP
3450 else if (warn_traditional_conversion
3451 && INTEGRAL_TYPE_P (type)
8ce94e44 3452 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3453 {
d45cf215
RS
3454 tree would_have_been = default_conversion (val);
3455 tree type1 = TREE_TYPE (would_have_been);
3456
61ac5ebe
MP
3457 if (val == error_mark_node)
3458 /* VAL could have been of incomplete type. */;
3459 else if (TREE_CODE (type) == ENUMERAL_TYPE
3460 && (TYPE_MAIN_VARIANT (type)
3461 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3462 /* No warning if function asks for enum
3463 and the actual arg is that enum type. */
3464 ;
3465 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3466 warning_at (ploc, OPT_Wtraditional_conversion,
3467 "passing argument %d of %qE "
3468 "with different width due to prototype",
3469 argnum, rname);
8df83eae 3470 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3471 ;
800cd3b9
RS
3472 /* Don't complain if the formal parameter type
3473 is an enum, because we can't tell now whether
3474 the value was an enum--even the same enum. */
3475 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3476 ;
400fbf9f
JW
3477 else if (TREE_CODE (val) == INTEGER_CST
3478 && int_fits_type_p (val, type))
3479 /* Change in signedness doesn't matter
3480 if a constant value is unaffected. */
3481 ;
ce9895ae
RS
3482 /* If the value is extended from a narrower
3483 unsigned type, it doesn't matter whether we
3484 pass it as signed or unsigned; the value
3485 certainly is the same either way. */
8ce94e44
JM
3486 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3487 && TYPE_UNSIGNED (valtype))
ce9895ae 3488 ;
8df83eae 3489 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3490 warning_at (ploc, OPT_Wtraditional_conversion,
3491 "passing argument %d of %qE "
3492 "as unsigned due to prototype",
3493 argnum, rname);
3ed56f8a 3494 else
5c1bc275
MP
3495 warning_at (ploc, OPT_Wtraditional_conversion,
3496 "passing argument %d of %qE "
3497 "as signed due to prototype",
3498 argnum, rname);
400fbf9f
JW
3499 }
3500 }
3501
8ce94e44
JM
3502 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3503 sake of better warnings from convert_and_check. */
3504 if (excess_precision)
3505 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3506 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3507 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3508 val, origtype, ic_argpass,
3509 npc, fundecl, function,
2ac2f164 3510 parmnum + 1);
2f6e4e97 3511
61f71b34 3512 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3513 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3514 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3515 parmval = default_conversion (parmval);
400fbf9f 3516 }
400fbf9f 3517 }
c65699ef 3518 else if (promote_float_arg)
b5d32c25
KG
3519 {
3520 if (type_generic)
bbbbb16a 3521 parmval = val;
b5d32c25 3522 else
0a0b3574
MM
3523 {
3524 /* Convert `float' to `double'. */
3525 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3526 warning_at (ploc, OPT_Wdouble_promotion,
3527 "implicit conversion from %qT to %qT when passing "
3528 "argument to function",
3529 valtype, double_type_node);
0a0b3574
MM
3530 parmval = convert (double_type_node, val);
3531 }
b5d32c25 3532 }
44a845ca
MS
3533 else if ((excess_precision && !type_generic)
3534 || (type_generic_overflow_p && parmnum == 2))
8ce94e44 3535 /* A "double" argument with excess precision being passed
44a845ca
MS
3536 without a prototype or in variable arguments.
3537 The last argument of __builtin_*_overflow_p should not be
3538 promoted. */
bbbbb16a 3539 parmval = convert (valtype, val);
c22cacf3
MS
3540 else if ((invalid_func_diag =
3541 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3542 {
3543 error (invalid_func_diag);
94a0dd7b 3544 return -1;
4d3e6fae 3545 }
1807ffc1
MS
3546 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3547 {
3548 return -1;
3549 }
400fbf9f
JW
3550 else
3551 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3552 parmval = default_conversion (val);
3553
9771b263 3554 (*values)[parmnum] = parmval;
06302a02
JJ
3555 if (parmval == error_mark_node)
3556 error_args = true;
400fbf9f
JW
3557
3558 if (typetail)
3559 typetail = TREE_CHAIN (typetail);
3560 }
3561
9771b263 3562 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3563
3fa8871b 3564 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3789b316 3565 {
68fca595 3566 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3567 inform_declaration (fundecl);
3568 return -1;
3789b316 3569 }
400fbf9f 3570
06302a02 3571 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3572}
3573\f
43f6dfd3
RS
3574/* This is the entry point used by the parser to build unary operators
3575 in the input. CODE, a tree_code, specifies the unary operator, and
3576 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3577 CONVERT_EXPR for code.
3578
3579 LOC is the location to use for the tree generated.
3580*/
43f6dfd3
RS
3581
3582struct c_expr
c2255bc4 3583parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3584{
3585 struct c_expr result;
3586
100d537d 3587 result.original_code = code;
6866c6e8
ILT
3588 result.original_type = NULL;
3589
1807ffc1
MS
3590 if (reject_gcc_builtin (arg.value))
3591 {
3592 result.value = error_mark_node;
3593 }
3594 else
3595 {
e51fbec3 3596 result.value = build_unary_op (loc, code, arg.value, false);
1807ffc1 3597
e9892350 3598 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3cd211af 3599 overflow_warning (loc, result.value, arg.value);
1807ffc1 3600 }
59c0753d 3601
ebedc9a3
DM
3602 /* We are typically called when parsing a prefix token at LOC acting on
3603 ARG. Reflect this by updating the source range of the result to
3604 start at LOC and end at the end of ARG. */
3605 set_c_expr_source_range (&result,
3606 loc, arg.get_finish ());
3607
43f6dfd3
RS
3608 return result;
3609}
3610
a9342885
MP
3611/* Returns true if TYPE is a character type, *not* including wchar_t. */
3612
3613static bool
3614char_type_p (tree type)
3615{
3616 return (type == char_type_node
3617 || type == unsigned_char_type_node
3618 || type == signed_char_type_node
3619 || type == char16_type_node
3620 || type == char32_type_node);
3621}
3622
43f6dfd3
RS
3623/* This is the entry point used by the parser to build binary operators
3624 in the input. CODE, a tree_code, specifies the binary operator, and
3625 ARG1 and ARG2 are the operands. In addition to constructing the
3626 expression, we check for operands that were written with other binary
ba47d38d
AH
3627 operators in a way that is likely to confuse the user.
3628
3629 LOCATION is the location of the binary operator. */
edc7c4ec 3630
487a92fe 3631struct c_expr
ba47d38d
AH
3632parser_build_binary_op (location_t location, enum tree_code code,
3633 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3634{
487a92fe 3635 struct c_expr result;
400fbf9f 3636
487a92fe
JM
3637 enum tree_code code1 = arg1.original_code;
3638 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3639 tree type1 = (arg1.original_type
3640 ? arg1.original_type
3641 : TREE_TYPE (arg1.value));
3642 tree type2 = (arg2.original_type
3643 ? arg2.original_type
3644 : TREE_TYPE (arg2.value));
400fbf9f 3645
ba47d38d 3646 result.value = build_binary_op (location, code,
30af3a2b 3647 arg1.value, arg2.value, true);
487a92fe 3648 result.original_code = code;
6866c6e8 3649 result.original_type = NULL;
58bf601b 3650
487a92fe 3651 if (TREE_CODE (result.value) == ERROR_MARK)
8a40fef3
DM
3652 {
3653 set_c_expr_source_range (&result,
3654 arg1.get_start (),
3655 arg2.get_finish ());
3656 return result;
3657 }
400fbf9f 3658
ba47d38d
AH
3659 if (location != UNKNOWN_LOCATION)
3660 protected_set_expr_location (result.value, location);
3661
ebedc9a3
DM
3662 set_c_expr_source_range (&result,
3663 arg1.get_start (),
3664 arg2.get_finish ());
3665
400fbf9f 3666 /* Check for cases such as x+y<<z which users are likely
487a92fe 3667 to misinterpret. */
400fbf9f 3668 if (warn_parentheses)
393e8e8b
MP
3669 warn_about_parentheses (location, code, code1, arg1.value, code2,
3670 arg2.value);
001af587 3671
ca409efd 3672 if (warn_logical_op)
393e8e8b 3673 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3674 code1, arg1.value, code2, arg2.value);
63a08740 3675
05b28fd6 3676 if (warn_tautological_compare)
433068cc
MP
3677 {
3678 tree lhs = arg1.value;
3679 tree rhs = arg2.value;
3680 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3681 {
3682 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3683 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3684 lhs = NULL_TREE;
3685 else
3686 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3687 }
3688 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3689 {
3690 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3691 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3692 rhs = NULL_TREE;
3693 else
3694 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3695 }
3696 if (lhs != NULL_TREE && rhs != NULL_TREE)
3697 warn_tautological_cmp (location, code, lhs, rhs);
3698 }
05b28fd6 3699
742938c9 3700 if (warn_logical_not_paren
7ccb1a11 3701 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3702 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3703 && code2 != TRUTH_NOT_EXPR
3704 /* Avoid warning for !!x == y. */
3705 && (TREE_CODE (arg1.value) != NE_EXPR
3706 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3707 {
3708 /* Avoid warning for !b == y where b has _Bool type. */
3709 tree t = integer_zero_node;
3710 if (TREE_CODE (arg1.value) == EQ_EXPR
3711 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3712 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3713 {
3714 t = TREE_OPERAND (arg1.value, 0);
3715 do
3716 {
3717 if (TREE_TYPE (t) != integer_type_node)
3718 break;
3719 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3720 t = C_MAYBE_CONST_EXPR_EXPR (t);
3721 else if (CONVERT_EXPR_P (t))
3722 t = TREE_OPERAND (t, 0);
3723 else
3724 break;
3725 }
3726 while (1);
3727 }
3728 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
ebef225f 3729 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
01177669 3730 }
742938c9 3731
e994a705
RS
3732 /* Warn about comparisons against string literals, with the exception
3733 of testing for equality or inequality of a string literal with NULL. */
3734 if (code == EQ_EXPR || code == NE_EXPR)
3735 {
66756373
JJ
3736 if ((code1 == STRING_CST
3737 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3738 || (code2 == STRING_CST
3739 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
c2255bc4
AH
3740 warning_at (location, OPT_Waddress,
3741 "comparison with string literal results in unspecified behavior");
a9342885
MP
3742 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3743 if (POINTER_TYPE_P (type1)
3744 && null_pointer_constant_p (arg2.value)
3745 && char_type_p (type2)
3746 && warning_at (location, OPT_Wpointer_compare,
3747 "comparison between pointer and zero character "
3748 "constant"))
3749 inform (arg1.get_start (), "did you mean to dereference the pointer?");
3750 else if (POINTER_TYPE_P (type2)
3751 && null_pointer_constant_p (arg1.value)
3752 && char_type_p (type1)
3753 && warning_at (location, OPT_Wpointer_compare,
3754 "comparison between pointer and zero character "
3755 "constant"))
3756 inform (arg2.get_start (), "did you mean to dereference the pointer?");
e994a705
RS
3757 }
3758 else if (TREE_CODE_CLASS (code) == tcc_comparison
3759 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3760 warning_at (location, OPT_Waddress,
3761 "comparison with string literal results in unspecified behavior");
e994a705 3762
b8698a0f
L
3763 if (TREE_OVERFLOW_P (result.value)
3764 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3765 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3766 overflow_warning (location, result.value);
400fbf9f 3767
6866c6e8
ILT
3768 /* Warn about comparisons of different enum types. */
3769 if (warn_enum_compare
3770 && TREE_CODE_CLASS (code) == tcc_comparison
3771 && TREE_CODE (type1) == ENUMERAL_TYPE
3772 && TREE_CODE (type2) == ENUMERAL_TYPE
3773 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3774 warning_at (location, OPT_Wenum_compare,
3775 "comparison between %qT and %qT",
3776 type1, type2);
3777
400fbf9f
JW
3778 return result;
3779}
3e4093b6 3780\f
3e4093b6
RS
3781/* Return a tree for the difference of pointers OP0 and OP1.
3782 The resulting tree has type int. */
293c9fdd 3783
3e4093b6 3784static tree
db3927fb 3785pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3786{
3e4093b6 3787 tree restype = ptrdiff_type_node;
36c5e70a 3788 tree result, inttype;
400fbf9f 3789
36c5e70a
BE
3790 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3791 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3792 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3793 tree orig_op1 = op1;
400fbf9f 3794
36c5e70a
BE
3795 /* If the operands point into different address spaces, we need to
3796 explicitly convert them to pointers into the common address space
3797 before we can subtract the numerical address values. */
3798 if (as0 != as1)
3799 {
3800 addr_space_t as_common;
3801 tree common_type;
3802
3803 /* Determine the common superset address space. This is guaranteed
3804 to exist because the caller verified that comp_target_types
3805 returned non-zero. */
3806 if (!addr_space_superset (as0, as1, &as_common))
3807 gcc_unreachable ();
3808
3809 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3810 op0 = convert (common_type, op0);
3811 op1 = convert (common_type, op1);
3812 }
3813
3814 /* Determine integer type to perform computations in. This will usually
3815 be the same as the result type (ptrdiff_t), but may need to be a wider
3816 type if pointers for the address space are wider than ptrdiff_t. */
3817 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3818 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3819 else
3820 inttype = restype;
3821
fcf73884 3822 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3823 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3824 "pointer of type %<void *%> used in subtraction");
3825 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3826 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3827 "pointer to a function used in subtraction");
400fbf9f 3828
3e4093b6
RS
3829 /* First do the subtraction as integers;
3830 then drop through to build the divide operator.
3831 Do not do default conversions on the minus operator
3832 in case restype is a short type. */
400fbf9f 3833
db3927fb 3834 op0 = build_binary_op (loc,
36c5e70a 3835 MINUS_EXPR, convert (inttype, op0),
30af3a2b 3836 convert (inttype, op1), false);
3e4093b6
RS
3837 /* This generates an error if op1 is pointer to incomplete type. */
3838 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3839 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3840
3e4093b6 3841 op1 = c_size_in_bytes (target_type);
400fbf9f 3842
f04dda30
MP
3843 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3844 error_at (loc, "arithmetic on pointer to an empty aggregate");
3845
3e4093b6 3846 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3847 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3848 op0, convert (inttype, op1));
3849
3850 /* Convert to final result type if necessary. */
3851 return convert (restype, result);
3e4093b6
RS
3852}
3853\f
2fe0a208
MP
3854/* Expand atomic compound assignments into an appropriate sequence as
3855 specified by the C11 standard section 6.5.16.2.
3856
267bac10
JM
3857 _Atomic T1 E1
3858 T2 E2
3859 E1 op= E2
3860
3861 This sequence is used for all types for which these operations are
3862 supported.
3863
3864 In addition, built-in versions of the 'fe' prefixed routines may
3865 need to be invoked for floating point (real, complex or vector) when
3866 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3867
3868 T1 newval;
3869 T1 old;
3870 T1 *addr
3871 T2 val
3872 fenv_t fenv
3873
3874 addr = &E1;
3875 val = (E2);
3876 __atomic_load (addr, &old, SEQ_CST);
3877 feholdexcept (&fenv);
3878loop:
3879 newval = old op val;
3880 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3881 SEQ_CST))
3882 goto done;
3883 feclearexcept (FE_ALL_EXCEPT);
3884 goto loop:
3885done:
3886 feupdateenv (&fenv);
3887
2fe0a208
MP
3888 The compiler will issue the __atomic_fetch_* built-in when possible,
3889 otherwise it will generate the generic form of the atomic operations.
3890 This requires temp(s) and has their address taken. The atomic processing
3891 is smart enough to figure out when the size of an object can utilize
3892 a lock-free version, and convert the built-in call to the appropriate
3893 lock-free routine. The optimizers will then dispose of any temps that
3894 are no longer required, and lock-free implementations are utilized as
3895 long as there is target support for the required size.
267bac10
JM
3896
3897 If the operator is NOP_EXPR, then this is a simple assignment, and
3898 an __atomic_store is issued to perform the assignment rather than
2fe0a208 3899 the above loop. */
267bac10
JM
3900
3901/* Build an atomic assignment at LOC, expanding into the proper
3902 sequence to store LHS MODIFYCODE= RHS. Return a value representing
2fe0a208 3903 the result of the operation, unless RETURN_OLD_P, in which case
267bac10
JM
3904 return the old value of LHS (this is only for postincrement and
3905 postdecrement). */
2fe0a208 3906
267bac10
JM
3907static tree
3908build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3909 tree rhs, bool return_old_p)
3910{
3911 tree fndecl, func_call;
3912 vec<tree, va_gc> *params;
3913 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3914 tree old, old_addr;
3915 tree compound_stmt;
3916 tree stmt, goto_stmt;
3917 tree loop_label, loop_decl, done_label, done_decl;
3918
3919 tree lhs_type = TREE_TYPE (lhs);
e51fbec3 3920 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
267bac10 3921 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
e035be33
JM
3922 tree rhs_semantic_type = TREE_TYPE (rhs);
3923 tree nonatomic_rhs_semantic_type;
3924 tree rhs_type;
267bac10
JM
3925
3926 gcc_assert (TYPE_ATOMIC (lhs_type));
3927
3928 if (return_old_p)
3929 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3930
3931 /* Allocate enough vector items for a compare_exchange. */
3932 vec_alloc (params, 6);
3933
3934 /* Create a compound statement to hold the sequence of statements
3935 with a loop. */
3936 compound_stmt = c_begin_compound_stmt (false);
3937
e035be33
JM
3938 /* Remove any excess precision (which is only present here in the
3939 case of compound assignments). */
3940 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
3941 {
3942 gcc_assert (modifycode != NOP_EXPR);
3943 rhs = TREE_OPERAND (rhs, 0);
3944 }
3945 rhs_type = TREE_TYPE (rhs);
3946
267bac10
JM
3947 /* Fold the RHS if it hasn't already been folded. */
3948 if (modifycode != NOP_EXPR)
3949 rhs = c_fully_fold (rhs, false, NULL);
3950
3951 /* Remove the qualifiers for the rest of the expressions and create
3952 the VAL temp variable to hold the RHS. */
3953 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3954 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
e035be33
JM
3955 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
3956 TYPE_UNQUALIFIED);
5c4abbb8 3957 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3958 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3959 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3960 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3961 NULL_TREE);
267bac10
JM
3962 SET_EXPR_LOCATION (rhs, loc);
3963 add_stmt (rhs);
3964
3965 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3966 an atomic_store. */
3967 if (modifycode == NOP_EXPR)
3968 {
3969 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
e51fbec3 3970 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
267bac10
JM
3971 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3972 params->quick_push (lhs_addr);
3973 params->quick_push (rhs);
3974 params->quick_push (seq_cst);
8edbfaa6 3975 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3976 add_stmt (func_call);
3977
3978 /* Finish the compound statement. */
3979 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3980
3981 /* VAL is the value which was stored, return a COMPOUND_STMT of
3982 the statement and that value. */
3983 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3984 }
3985
2fe0a208
MP
3986 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3987 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3988 isn't applicable for such builtins. ??? Do we want to handle enums? */
3989 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3990 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3991 {
3992 built_in_function fncode;
3993 switch (modifycode)
3994 {
3995 case PLUS_EXPR:
3996 case POINTER_PLUS_EXPR:
3997 fncode = (return_old_p
3998 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3999 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4000 break;
4001 case MINUS_EXPR:
4002 fncode = (return_old_p
4003 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4004 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4005 break;
4006 case BIT_AND_EXPR:
4007 fncode = (return_old_p
4008 ? BUILT_IN_ATOMIC_FETCH_AND_N
4009 : BUILT_IN_ATOMIC_AND_FETCH_N);
4010 break;
4011 case BIT_IOR_EXPR:
4012 fncode = (return_old_p
4013 ? BUILT_IN_ATOMIC_FETCH_OR_N
4014 : BUILT_IN_ATOMIC_OR_FETCH_N);
4015 break;
4016 case BIT_XOR_EXPR:
4017 fncode = (return_old_p
4018 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4019 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4020 break;
4021 default:
4022 goto cas_loop;
4023 }
4024
4025 /* We can only use "_1" through "_16" variants of the atomic fetch
4026 built-ins. */
4027 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4028 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4029 goto cas_loop;
4030
4031 /* If this is a pointer type, we need to multiply by the size of
4032 the pointer target type. */
4033 if (POINTER_TYPE_P (lhs_type))
4034 {
4035 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4036 /* ??? This would introduce -Wdiscarded-qualifiers
4037 warning: __atomic_fetch_* expect volatile void *
4038 type as the first argument. (Assignments between
4039 atomic and non-atomic objects are OK.) */
4040 || TYPE_RESTRICT (lhs_type))
4041 goto cas_loop;
4042 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4043 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4044 convert (ptrdiff_type_node, rhs),
4045 convert (ptrdiff_type_node, sz));
4046 }
4047
4048 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4049 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4050 fndecl = builtin_decl_explicit (fncode);
4051 params->quick_push (lhs_addr);
4052 params->quick_push (rhs);
4053 params->quick_push (seq_cst);
4054 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4055
4056 newval = create_tmp_var_raw (nonatomic_lhs_type);
4057 TREE_ADDRESSABLE (newval) = 1;
4058 TREE_NO_WARNING (newval) = 1;
4059 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4060 NULL_TREE, NULL_TREE);
4061 SET_EXPR_LOCATION (rhs, loc);
4062 add_stmt (rhs);
4063
4064 /* Finish the compound statement. */
4065 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4066
4067 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4068 the statement and that value. */
4069 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4070 }
4071
4072cas_loop:
267bac10 4073 /* Create the variables and labels required for the op= form. */
5c4abbb8 4074 old = create_tmp_var_raw (nonatomic_lhs_type);
e51fbec3 4075 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
cc28fc7f
MP
4076 TREE_ADDRESSABLE (old) = 1;
4077 TREE_NO_WARNING (old) = 1;
267bac10 4078
5c4abbb8 4079 newval = create_tmp_var_raw (nonatomic_lhs_type);
e51fbec3 4080 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
267bac10 4081 TREE_ADDRESSABLE (newval) = 1;
2994fb91 4082 TREE_NO_WARNING (newval) = 1;
267bac10
JM
4083
4084 loop_decl = create_artificial_label (loc);
4085 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4086
4087 done_decl = create_artificial_label (loc);
4088 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4089
4090 /* __atomic_load (addr, &old, SEQ_CST). */
4091 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4092 params->quick_push (lhs_addr);
4093 params->quick_push (old_addr);
4094 params->quick_push (seq_cst);
8edbfaa6 4095 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
4096 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4097 NULL_TREE);
4098 add_stmt (old);
267bac10
JM
4099 params->truncate (0);
4100
4101 /* Create the expressions for floating-point environment
4102 manipulation, if required. */
4103 bool need_fenv = (flag_trapping_math
4104 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4105 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4106 if (need_fenv)
4107 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4108
4109 if (hold_call)
4110 add_stmt (hold_call);
4111
4112 /* loop: */
4113 add_stmt (loop_label);
4114
4115 /* newval = old + val; */
e035be33
JM
4116 if (rhs_type != rhs_semantic_type)
4117 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
30af3a2b 4118 rhs = build_binary_op (loc, modifycode, old, val, true);
e035be33
JM
4119 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4120 {
4121 tree eptype = TREE_TYPE (rhs);
4122 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4123 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4124 }
4125 else
4126 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
4127 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4128 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
4129 NULL_TREE, 0);
4130 if (rhs != error_mark_node)
4131 {
5c4abbb8
MP
4132 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4133 NULL_TREE);
267bac10
JM
4134 SET_EXPR_LOCATION (rhs, loc);
4135 add_stmt (rhs);
4136 }
4137
4138 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4139 goto done; */
4140 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4141 params->quick_push (lhs_addr);
4142 params->quick_push (old_addr);
4143 params->quick_push (newval_addr);
4144 params->quick_push (integer_zero_node);
4145 params->quick_push (seq_cst);
4146 params->quick_push (seq_cst);
8edbfaa6 4147 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
4148
4149 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4150 SET_EXPR_LOCATION (goto_stmt, loc);
4151
4152 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4153 SET_EXPR_LOCATION (stmt, loc);
4154 add_stmt (stmt);
5c4abbb8 4155
267bac10
JM
4156 if (clear_call)
4157 add_stmt (clear_call);
4158
4159 /* goto loop; */
4160 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4161 SET_EXPR_LOCATION (goto_stmt, loc);
4162 add_stmt (goto_stmt);
5c4abbb8 4163
267bac10
JM
4164 /* done: */
4165 add_stmt (done_label);
4166
4167 if (update_call)
4168 add_stmt (update_call);
4169
4170 /* Finish the compound statement. */
4171 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4172
4173 /* NEWVAL is the value that was successfully stored, return a
4174 COMPOUND_EXPR of the statement and the appropriate value. */
4175 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4176 return_old_p ? old : newval);
4177}
4178
3e4093b6
RS
4179/* Construct and perhaps optimize a tree representation
4180 for a unary operation. CODE, a tree_code, specifies the operation
4181 and XARG is the operand.
e51fbec3
MP
4182 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4183 promotions (such as from short to int).
4184 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4185 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4186 to pointers in C99.
c9f9eb5d
AH
4187
4188 LOCATION is the location of the operator. */
400fbf9f 4189
3e4093b6 4190tree
4c13ba17 4191build_unary_op (location_t location, enum tree_code code, tree xarg,
e51fbec3 4192 bool noconvert)
3e4093b6
RS
4193{
4194 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4195 tree arg = xarg;
3fa8871b 4196 tree argtype = NULL_TREE;
808d6eaa 4197 enum tree_code typecode;
3e4093b6 4198 tree val;
c9f9eb5d 4199 tree ret = error_mark_node;
8ce94e44 4200 tree eptype = NULL_TREE;
4de67c26 4201 const char *invalid_op_diag;
928c19bb
JM
4202 bool int_operands;
4203
4204 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
4205 if (int_operands)
4206 arg = remove_c_maybe_const_expr (arg);
400fbf9f 4207
808d6eaa 4208 if (code != ADDR_EXPR)
4f2e1536 4209 arg = require_complete_type (location, arg);
808d6eaa
JM
4210
4211 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
4212 if (typecode == ERROR_MARK)
4213 return error_mark_node;
4214 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4215 typecode = INTEGER_TYPE;
6c36d76b 4216
4de67c26
JM
4217 if ((invalid_op_diag
4218 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4219 {
c9f9eb5d 4220 error_at (location, invalid_op_diag);
4de67c26
JM
4221 return error_mark_node;
4222 }
4223
8ce94e44
JM
4224 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4225 {
4226 eptype = TREE_TYPE (arg);
4227 arg = TREE_OPERAND (arg, 0);
4228 }
4229
3e4093b6
RS
4230 switch (code)
4231 {
4232 case CONVERT_EXPR:
4233 /* This is used for unary plus, because a CONVERT_EXPR
4234 is enough to prevent anybody from looking inside for
4235 associativity, but won't generate any code. */
4236 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 4237 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 4238 || typecode == VECTOR_TYPE))
400fbf9f 4239 {
c9f9eb5d 4240 error_at (location, "wrong type argument to unary plus");
3e4093b6 4241 return error_mark_node;
400fbf9f 4242 }
3e4093b6
RS
4243 else if (!noconvert)
4244 arg = default_conversion (arg);
db3927fb 4245 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
4246 break;
4247
3e4093b6
RS
4248 case NEGATE_EXPR:
4249 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 4250 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
4251 || typecode == VECTOR_TYPE))
4252 {
c9f9eb5d 4253 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
4254 return error_mark_node;
4255 }
4256 else if (!noconvert)
4257 arg = default_conversion (arg);
400fbf9f
JW
4258 break;
4259
3e4093b6 4260 case BIT_NOT_EXPR:
462643f0
AP
4261 /* ~ works on integer types and non float vectors. */
4262 if (typecode == INTEGER_TYPE
4263 || (typecode == VECTOR_TYPE
4264 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 4265 {
9a2300e9
MP
4266 tree e = arg;
4267
4268 /* Warn if the expression has boolean value. */
4269 while (TREE_CODE (e) == COMPOUND_EXPR)
4270 e = TREE_OPERAND (e, 1);
4271
4272 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4273 || truth_value_p (TREE_CODE (e)))
4274 && warning_at (location, OPT_Wbool_operation,
4275 "%<~%> on a boolean expression"))
4276 {
4277 gcc_rich_location richloc (location);
4278 richloc.add_fixit_insert_before (location, "!");
4279 inform_at_rich_loc (&richloc, "did you mean to use logical "
4280 "not?");
4281 }
3e4093b6
RS
4282 if (!noconvert)
4283 arg = default_conversion (arg);
03d5b1f5 4284 }
3e4093b6 4285 else if (typecode == COMPLEX_TYPE)
400fbf9f 4286 {
3e4093b6 4287 code = CONJ_EXPR;
c1771a20 4288 pedwarn (location, OPT_Wpedantic,
fcf73884 4289 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
4290 if (!noconvert)
4291 arg = default_conversion (arg);
4292 }
4293 else
4294 {
c9f9eb5d 4295 error_at (location, "wrong type argument to bit-complement");
3e4093b6 4296 return error_mark_node;
400fbf9f
JW
4297 }
4298 break;
4299
3e4093b6 4300 case ABS_EXPR:
11017cc7 4301 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 4302 {
c9f9eb5d 4303 error_at (location, "wrong type argument to abs");
3e4093b6 4304 return error_mark_node;
400fbf9f 4305 }
3e4093b6
RS
4306 else if (!noconvert)
4307 arg = default_conversion (arg);
400fbf9f
JW
4308 break;
4309
3e4093b6
RS
4310 case CONJ_EXPR:
4311 /* Conjugating a real value is a no-op, but allow it anyway. */
4312 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4313 || typecode == COMPLEX_TYPE))
400fbf9f 4314 {
c9f9eb5d 4315 error_at (location, "wrong type argument to conjugation");
3e4093b6 4316 return error_mark_node;
400fbf9f 4317 }
3e4093b6
RS
4318 else if (!noconvert)
4319 arg = default_conversion (arg);
400fbf9f
JW
4320 break;
4321
3e4093b6 4322 case TRUTH_NOT_EXPR:
ab22c1fa 4323 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 4324 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 4325 && typecode != COMPLEX_TYPE)
400fbf9f 4326 {
c9f9eb5d
AH
4327 error_at (location,
4328 "wrong type argument to unary exclamation mark");
3e4093b6 4329 return error_mark_node;
400fbf9f 4330 }
a27d595d
JM
4331 if (int_operands)
4332 {
4333 arg = c_objc_common_truthvalue_conversion (location, xarg);
4334 arg = remove_c_maybe_const_expr (arg);
4335 }
4336 else
4337 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 4338 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
4339 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4340 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4341 location = EXPR_LOCATION (ret);
c9f9eb5d 4342 goto return_build_unary_op;
3e4093b6 4343
3e4093b6 4344 case REALPART_EXPR:
3e4093b6 4345 case IMAGPART_EXPR:
fb52b50a
NF
4346 ret = build_real_imag_expr (location, code, arg);
4347 if (ret == error_mark_node)
4348 return error_mark_node;
8ce94e44
JM
4349 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4350 eptype = TREE_TYPE (eptype);
c9f9eb5d 4351 goto return_build_unary_op;
3e4093b6
RS
4352
4353 case PREINCREMENT_EXPR:
4354 case POSTINCREMENT_EXPR:
4355 case PREDECREMENT_EXPR:
4356 case POSTDECREMENT_EXPR:
3e4093b6 4357
928c19bb
JM
4358 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4359 {
4360 tree inner = build_unary_op (location, code,
4c13ba17
MP
4361 C_MAYBE_CONST_EXPR_EXPR (arg),
4362 noconvert);
928c19bb
JM
4363 if (inner == error_mark_node)
4364 return error_mark_node;
4365 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4366 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4367 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4368 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4369 goto return_build_unary_op;
4370 }
4371
925e8657
NP
4372 /* Complain about anything that is not a true lvalue. In
4373 Objective-C, skip this check for property_refs. */
f90e8e2e 4374 if (!objc_is_property_ref (arg)
7bd11157
TT
4375 && !lvalue_or_else (location,
4376 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
4377 || code == POSTINCREMENT_EXPR)
4378 ? lv_increment
4379 : lv_decrement)))
928c19bb
JM
4380 return error_mark_node;
4381
09639a83
ILT
4382 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4383 {
4384 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4385 warning_at (location, OPT_Wc___compat,
4386 "increment of enumeration value is invalid in C++");
4387 else
4388 warning_at (location, OPT_Wc___compat,
4389 "decrement of enumeration value is invalid in C++");
4390 }
4391
9a2300e9
MP
4392 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4393 {
4394 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4395 warning_at (location, OPT_Wbool_operation,
4396 "increment of a boolean expression");
4397 else
4398 warning_at (location, OPT_Wbool_operation,
4399 "decrement of a boolean expression");
4400 }
4401
928c19bb
JM
4402 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4403 arg = c_fully_fold (arg, false, NULL);
4404
267bac10
JM
4405 bool atomic_op;
4406 atomic_op = really_atomic_lvalue (arg);
4407
3e4093b6
RS
4408 /* Increment or decrement the real part of the value,
4409 and don't change the imaginary part. */
4410 if (typecode == COMPLEX_TYPE)
400fbf9f 4411 {
3e4093b6
RS
4412 tree real, imag;
4413
c1771a20 4414 pedwarn (location, OPT_Wpedantic,
509c9d60 4415 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 4416
267bac10
JM
4417 if (!atomic_op)
4418 {
4419 arg = stabilize_reference (arg);
e51fbec3
MP
4420 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4421 true);
4422 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4423 true);
4424 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
267bac10
JM
4425 if (real == error_mark_node || imag == error_mark_node)
4426 return error_mark_node;
4427 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4428 real, imag);
4429 goto return_build_unary_op;
4430 }
400fbf9f 4431 }
3e4093b6
RS
4432
4433 /* Report invalid types. */
4434
ab22c1fa 4435 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4436 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4437 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4438 {
3e4093b6 4439 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4440 error_at (location, "wrong type argument to increment");
c22cacf3 4441 else
c9f9eb5d 4442 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4443
4444 return error_mark_node;
400fbf9f 4445 }
400fbf9f 4446
3e4093b6
RS
4447 {
4448 tree inc;
400fbf9f 4449
3e4093b6
RS
4450 argtype = TREE_TYPE (arg);
4451
4452 /* Compute the increment. */
4453
4454 if (typecode == POINTER_TYPE)
4455 {
a0e24419 4456 /* If pointer target is an incomplete type,
3e4093b6 4457 we just cannot know how to do the arithmetic. */
b70cef5d 4458 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4459 {
4460 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4461 error_at (location,
a0e24419
MP
4462 "increment of pointer to an incomplete type %qT",
4463 TREE_TYPE (argtype));
3e4093b6 4464 else
c9f9eb5d 4465 error_at (location,
a0e24419
MP
4466 "decrement of pointer to an incomplete type %qT",
4467 TREE_TYPE (argtype));
3e4093b6 4468 }
b70cef5d
JJ
4469 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4470 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4471 {
3e4093b6 4472 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4473 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4474 "wrong type argument to increment");
3e4093b6 4475 else
44d90fe1 4476 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4477 "wrong type argument to decrement");
3e4093b6
RS
4478 }
4479
b70cef5d 4480 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4481 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4482 }
b70cef5d 4483 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4484 {
4485 /* For signed fract types, we invert ++ to -- or
4486 -- to ++, and change inc from 1 to -1, because
4487 it is not possible to represent 1 in signed fract constants.
4488 For unsigned fract types, the result always overflows and
4489 we get an undefined (original) or the maximum value. */
4490 if (code == PREINCREMENT_EXPR)
4491 code = PREDECREMENT_EXPR;
4492 else if (code == PREDECREMENT_EXPR)
4493 code = PREINCREMENT_EXPR;
4494 else if (code == POSTINCREMENT_EXPR)
4495 code = POSTDECREMENT_EXPR;
4496 else /* code == POSTDECREMENT_EXPR */
4497 code = POSTINCREMENT_EXPR;
4498
4499 inc = integer_minus_one_node;
4500 inc = convert (argtype, inc);
4501 }
3e4093b6 4502 else
5be014d5 4503 {
241b71bb
TV
4504 inc = VECTOR_TYPE_P (argtype)
4505 ? build_one_cst (argtype)
4506 : integer_one_node;
5be014d5
AP
4507 inc = convert (argtype, inc);
4508 }
3e4093b6 4509
925e8657
NP
4510 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4511 need to ask Objective-C to build the increment or decrement
4512 expression for it. */
4513 if (objc_is_property_ref (arg))
f90e8e2e 4514 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4515 arg, inc);
4516
3e4093b6 4517 /* Report a read-only lvalue. */
f37acdf9 4518 if (TYPE_READONLY (argtype))
953ff289 4519 {
c02065fc 4520 readonly_error (location, arg,
953ff289
DN
4521 ((code == PREINCREMENT_EXPR
4522 || code == POSTINCREMENT_EXPR)
4523 ? lv_increment : lv_decrement));
4524 return error_mark_node;
4525 }
f37acdf9
JM
4526 else if (TREE_READONLY (arg))
4527 readonly_warning (arg,
4528 ((code == PREINCREMENT_EXPR
4529 || code == POSTINCREMENT_EXPR)
4530 ? lv_increment : lv_decrement));
3e4093b6 4531
267bac10
JM
4532 /* If the argument is atomic, use the special code sequences for
4533 atomic compound assignment. */
4534 if (atomic_op)
4535 {
4536 arg = stabilize_reference (arg);
4537 ret = build_atomic_assign (location, arg,
4538 ((code == PREINCREMENT_EXPR
4539 || code == POSTINCREMENT_EXPR)
4540 ? PLUS_EXPR
4541 : MINUS_EXPR),
4542 (FRACT_MODE_P (TYPE_MODE (argtype))
4543 ? inc
4544 : integer_one_node),
4545 (code == POSTINCREMENT_EXPR
4546 || code == POSTDECREMENT_EXPR));
4547 goto return_build_unary_op;
4548 }
4549
3e4093b6
RS
4550 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4551 val = boolean_increment (code, arg);
4552 else
53fb4de3 4553 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4554 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4555 if (TREE_CODE (val) != code)
6de9cd9a 4556 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4557 ret = val;
4558 goto return_build_unary_op;
3e4093b6
RS
4559 }
4560
4561 case ADDR_EXPR:
4562 /* Note that this operation never does default_conversion. */
4563
2b4b7036
JM
4564 /* The operand of unary '&' must be an lvalue (which excludes
4565 expressions of type void), or, in C99, the result of a [] or
4566 unary '*' operator. */
4567 if (VOID_TYPE_P (TREE_TYPE (arg))
4568 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
22d03525 4569 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
2b4b7036
JM
4570 pedwarn (location, 0, "taking address of expression of type %<void%>");
4571
3e4093b6 4572 /* Let &* cancel out to simplify resulting code. */
22d03525 4573 if (INDIRECT_REF_P (arg))
400fbf9f 4574 {
3e4093b6
RS
4575 /* Don't let this be an lvalue. */
4576 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4577 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4578 ret = TREE_OPERAND (arg, 0);
4579 goto return_build_unary_op;
400fbf9f 4580 }
1eb8759b 4581
3e4093b6
RS
4582 /* Anything not already handled and not a true memory reference
4583 or a non-lvalue array is an error. */
4c13ba17 4584 if (typecode != FUNCTION_TYPE && !noconvert
ee45a32d 4585 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4586 return error_mark_node;
b6a10c9f 4587
928c19bb
JM
4588 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4589 folding later. */
4590 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4591 {
4592 tree inner = build_unary_op (location, code,
4c13ba17
MP
4593 C_MAYBE_CONST_EXPR_EXPR (arg),
4594 noconvert);
928c19bb
JM
4595 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4596 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4597 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4598 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4599 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4600 goto return_build_unary_op;
4601 }
4602
3e4093b6
RS
4603 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4604 argtype = TREE_TYPE (arg);
400fbf9f 4605
3e4093b6 4606 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4607 to which the address will point. This is only needed
f2c1da78 4608 for function types. */
6615c446 4609 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4610 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4611 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4612 {
4613 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4614 int quals = orig_quals;
4615
4616 if (TREE_READONLY (arg))
4617 quals |= TYPE_QUAL_CONST;
4618 if (TREE_THIS_VOLATILE (arg))
4619 quals |= TYPE_QUAL_VOLATILE;
4620
f2c1da78
JM
4621 argtype = c_build_qualified_type (argtype, quals);
4622 }
400fbf9f 4623
ee45a32d
EB
4624 switch (TREE_CODE (arg))
4625 {
4626 case COMPONENT_REF:
4627 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4628 {
949505a9
MP
4629 error_at (location, "cannot take address of bit-field %qD",
4630 TREE_OPERAND (arg, 1));
ee45a32d
EB
4631 return error_mark_node;
4632 }
4633
191816a3 4634 /* fall through */
ee45a32d
EB
4635
4636 case ARRAY_REF:
4637 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4638 {
4639 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4640 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4641 {
949505a9
MP
4642 error_at (location, "cannot take address of scalar with "
4643 "reverse storage order");
ee45a32d
EB
4644 return error_mark_node;
4645 }
4646
4647 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4648 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
949505a9
MP
4649 warning_at (location, OPT_Wscalar_storage_order,
4650 "address of array with reverse scalar storage "
4651 "order requested");
ee45a32d
EB
4652 }
4653
4654 default:
4655 break;
4656 }
4657
3e4093b6
RS
4658 if (!c_mark_addressable (arg))
4659 return error_mark_node;
400fbf9f 4660
abb54d14
JM
4661 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4662 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4663
5cc200fc 4664 argtype = build_pointer_type (argtype);
5e55f99d
RH
4665
4666 /* ??? Cope with user tricks that amount to offsetof. Delete this
4667 when we have proper support for integer constant expressions. */
4668 val = get_base_address (arg);
22d03525 4669 if (val && INDIRECT_REF_P (val)
3aa2ddb8
JJ
4670 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4671 {
cf9e9959 4672 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4673 goto return_build_unary_op;
3aa2ddb8 4674 }
5e55f99d 4675
5cc200fc 4676 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4677
c9f9eb5d
AH
4678 ret = val;
4679 goto return_build_unary_op;
400fbf9f 4680
3e4093b6 4681 default:
1344f9a3 4682 gcc_unreachable ();
3e4093b6 4683 }
400fbf9f 4684
3fa8871b 4685 if (argtype == NULL_TREE)
3e4093b6 4686 argtype = TREE_TYPE (arg);
928c19bb
JM
4687 if (TREE_CODE (arg) == INTEGER_CST)
4688 ret = (require_constant_value
db3927fb
AH
4689 ? fold_build1_initializer_loc (location, code, argtype, arg)
4690 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4691 else
4692 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4693 return_build_unary_op:
4694 gcc_assert (ret != error_mark_node);
928c19bb
JM
4695 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4696 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4697 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4698 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4699 ret = note_integer_operands (ret);
8ce94e44
JM
4700 if (eptype)
4701 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4702 protected_set_expr_location (ret, location);
4703 return ret;
3e4093b6 4704}
400fbf9f 4705
3e4093b6
RS
4706/* Return nonzero if REF is an lvalue valid for this language.
4707 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4708 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4709
1e4ae551 4710bool
58f9752a 4711lvalue_p (const_tree ref)
3e4093b6 4712{
58f9752a 4713 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4714
3e4093b6
RS
4715 switch (code)
4716 {
4717 case REALPART_EXPR:
4718 case IMAGPART_EXPR:
4719 case COMPONENT_REF:
4720 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4721
928c19bb
JM
4722 case C_MAYBE_CONST_EXPR:
4723 return lvalue_p (TREE_OPERAND (ref, 1));
4724
3e4093b6
RS
4725 case COMPOUND_LITERAL_EXPR:
4726 case STRING_CST:
5b73d2ab 4727 return true;
400fbf9f 4728
3e4093b6
RS
4729 case INDIRECT_REF:
4730 case ARRAY_REF:
36536d79 4731 case ARRAY_NOTATION_REF:
3e4093b6
RS
4732 case VAR_DECL:
4733 case PARM_DECL:
4734 case RESULT_DECL:
4735 case ERROR_MARK:
4736 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4737 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4738
3e4093b6 4739 case BIND_EXPR:
3e4093b6 4740 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4741
3e4093b6 4742 default:
5b73d2ab 4743 return false;
3e4093b6
RS
4744 }
4745}
400fbf9f 4746\f
f37acdf9
JM
4747/* Give a warning for storing in something that is read-only in GCC
4748 terms but not const in ISO C terms. */
4749
4750static void
4751readonly_warning (tree arg, enum lvalue_use use)
4752{
4753 switch (use)
4754 {
4755 case lv_assign:
4756 warning (0, "assignment of read-only location %qE", arg);
4757 break;
4758 case lv_increment:
4759 warning (0, "increment of read-only location %qE", arg);
4760 break;
4761 case lv_decrement:
4762 warning (0, "decrement of read-only location %qE", arg);
4763 break;
4764 default:
4765 gcc_unreachable ();
4766 }
4767 return;
4768}
4769
37dc0d8d
JM
4770
4771/* Return nonzero if REF is an lvalue valid for this language;
4772 otherwise, print an error message and return zero. USE says
7bd11157
TT
4773 how the lvalue is being used and so selects the error message.
4774 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4775
4776static int
7bd11157 4777lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4778{
4779 int win = lvalue_p (ref);
4780
4781 if (!win)
7bd11157 4782 lvalue_error (loc, use);
37dc0d8d
JM
4783
4784 return win;
4785}
3e4093b6
RS
4786\f
4787/* Mark EXP saying that we need to be able to take the
4788 address of it; it should not be allocated in a register.
a9e4a1a5
JJ
4789 Returns true if successful. ARRAY_REF_P is true if this
4790 is for ARRAY_REF construction - in that case we don't want
4791 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4792 it is fine to use ARRAY_REFs for vector subscripts on vector
4793 register variables. */
54c93c30 4794
3e4093b6 4795bool
a9e4a1a5 4796c_mark_addressable (tree exp, bool array_ref_p)
400fbf9f 4797{
3e4093b6 4798 tree x = exp;
95602da1 4799
3e4093b6
RS
4800 while (1)
4801 switch (TREE_CODE (x))
4802 {
a9e4a1a5
JJ
4803 case VIEW_CONVERT_EXPR:
4804 if (array_ref_p
4805 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4806 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4807 return true;
4808 /* FALLTHRU */
3e4093b6 4809 case COMPONENT_REF:
3e4093b6
RS
4810 case ADDR_EXPR:
4811 case ARRAY_REF:
4812 case REALPART_EXPR:
4813 case IMAGPART_EXPR:
4814 x = TREE_OPERAND (x, 0);
4815 break;
95602da1 4816
3e4093b6
RS
4817 case COMPOUND_LITERAL_EXPR:
4818 case CONSTRUCTOR:
4819 TREE_ADDRESSABLE (x) = 1;
4820 return true;
95602da1 4821
3e4093b6
RS
4822 case VAR_DECL:
4823 case CONST_DECL:
4824 case PARM_DECL:
4825 case RESULT_DECL:
5baeaac0 4826 if (C_DECL_REGISTER (x)
3e4093b6
RS
4827 && DECL_NONLOCAL (x))
4828 {
62f9079a 4829 if (TREE_PUBLIC (x) || is_global_var (x))
3e4093b6 4830 {
0039fa55
AN
4831 error
4832 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4833 return false;
4834 }
509c9d60 4835 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4836 }
5baeaac0 4837 else if (C_DECL_REGISTER (x))
3e4093b6 4838 {
62f9079a 4839 if (TREE_PUBLIC (x) || is_global_var (x))
0039fa55
AN
4840 error ("address of global register variable %qD requested", x);
4841 else
4842 error ("address of register variable %qD requested", x);
4843 return false;
3e4093b6 4844 }
400fbf9f 4845
191816a3 4846 /* FALLTHRU */
3e4093b6
RS
4847 case FUNCTION_DECL:
4848 TREE_ADDRESSABLE (x) = 1;
191816a3 4849 /* FALLTHRU */
3e4093b6
RS
4850 default:
4851 return true;
4852 }
4853}
4854\f
2d2e923f
MLI
4855/* Convert EXPR to TYPE, warning about conversion problems with
4856 constants. SEMANTIC_TYPE is the type this conversion would use
4857 without excess precision. If SEMANTIC_TYPE is NULL, this function
4858 is equivalent to convert_and_check. This function is a wrapper that
4859 handles conversions that may be different than
4860 the usual ones because of excess precision. */
4861
4862static tree
68fca595
MP
4863ep_convert_and_check (location_t loc, tree type, tree expr,
4864 tree semantic_type)
2d2e923f
MLI
4865{
4866 if (TREE_TYPE (expr) == type)
4867 return expr;
4868
4869 if (!semantic_type)
68fca595 4870 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4871
4872 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4873 && TREE_TYPE (expr) != semantic_type)
4874 {
4875 /* For integers, we need to check the real conversion, not
4876 the conversion to the excess precision type. */
68fca595 4877 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4878 }
4879 /* Result type is the excess precision type, which should be
4880 large enough, so do not check. */
4881 return convert (type, expr);
4882}
4883
928c19bb
JM
4884/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4885 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4886 if folded to an integer constant then the unselected half may
4887 contain arbitrary operations not normally permitted in constant
c2255bc4 4888 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4889
4890tree
744aa42f 4891build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
a32c8316
MP
4892 tree op1, tree op1_original_type, location_t op1_loc,
4893 tree op2, tree op2_original_type, location_t op2_loc)
400fbf9f 4894{
3e4093b6
RS
4895 tree type1;
4896 tree type2;
4897 enum tree_code code1;
4898 enum tree_code code2;
4899 tree result_type = NULL;
2d2e923f 4900 tree semantic_result_type = NULL;
3e4093b6 4901 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4902 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4903 bool ifexp_int_operands;
928c19bb 4904 tree ret;
400fbf9f 4905
4d84fe7c
JM
4906 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4907 if (op1_int_operands)
4908 op1 = remove_c_maybe_const_expr (op1);
4909 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4910 if (op2_int_operands)
4911 op2 = remove_c_maybe_const_expr (op2);
4912 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4913 if (ifexp_int_operands)
4914 ifexp = remove_c_maybe_const_expr (ifexp);
4915
3e4093b6
RS
4916 /* Promote both alternatives. */
4917
4918 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4919 op1 = default_conversion (op1);
4920 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4921 op2 = default_conversion (op2);
4922
4923 if (TREE_CODE (ifexp) == ERROR_MARK
4924 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4925 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4926 return error_mark_node;
400fbf9f 4927
3e4093b6
RS
4928 type1 = TREE_TYPE (op1);
4929 code1 = TREE_CODE (type1);
4930 type2 = TREE_TYPE (op2);
4931 code2 = TREE_CODE (type2);
4932
1807ffc1
MS
4933 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4934 return error_mark_node;
4935
4936 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4937 return error_mark_node;
4938
b1adf557
JM
4939 /* C90 does not permit non-lvalue arrays in conditional expressions.
4940 In C99 they will be pointers by now. */
4941 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4942 {
744aa42f 4943 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4944 return error_mark_node;
4945 }
4946
8ce94e44
JM
4947 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4948 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4949 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4950 || code1 == COMPLEX_TYPE)
4951 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4952 || code2 == COMPLEX_TYPE))
4953 {
2d2e923f 4954 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4955 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4956 {
4957 op1 = TREE_OPERAND (op1, 0);
4958 type1 = TREE_TYPE (op1);
4959 gcc_assert (TREE_CODE (type1) == code1);
4960 }
4961 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4962 {
4963 op2 = TREE_OPERAND (op2, 0);
4964 type2 = TREE_TYPE (op2);
4965 gcc_assert (TREE_CODE (type2) == code2);
4966 }
4967 }
4968
d130ae11
ILT
4969 if (warn_cxx_compat)
4970 {
4971 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4972 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4973
4974 if (TREE_CODE (t1) == ENUMERAL_TYPE
4975 && TREE_CODE (t2) == ENUMERAL_TYPE
4976 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4977 warning_at (colon_loc, OPT_Wc___compat,
4978 ("different enum types in conditional is "
4979 "invalid in C++: %qT vs %qT"),
4980 t1, t2);
4981 }
4982
3e4093b6
RS
4983 /* Quickly detect the usual case where op1 and op2 have the same type
4984 after promotion. */
4985 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4986 {
3e4093b6
RS
4987 if (type1 == type2)
4988 result_type = type1;
4989 else
4990 result_type = TYPE_MAIN_VARIANT (type1);
4991 }
4992 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4993 || code1 == COMPLEX_TYPE)
4994 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4995 || code2 == COMPLEX_TYPE))
3e4093b6 4996 {
ccf7f880 4997 result_type = c_common_type (type1, type2);
5a578671
JM
4998 if (result_type == error_mark_node)
4999 return error_mark_node;
0a0b3574
MM
5000 do_warn_double_promotion (result_type, type1, type2,
5001 "implicit conversion from %qT to %qT to "
5002 "match other result of conditional",
5003 colon_loc);
400fbf9f 5004
3e4093b6
RS
5005 /* If -Wsign-compare, warn here if type1 and type2 have
5006 different signedness. We'll promote the signed to unsigned
5007 and later code won't know it used to be different.
5008 Do this check on the original types, so that explicit casts
5009 will be considered, but default promotions won't. */
7d882b83 5010 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 5011 {
8df83eae
RK
5012 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5013 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 5014
3e4093b6
RS
5015 if (unsigned_op1 ^ unsigned_op2)
5016 {
6ac01510
ILT
5017 bool ovf;
5018
3e4093b6
RS
5019 /* Do not warn if the result type is signed, since the
5020 signed type will only be chosen if it can represent
5021 all the values of the unsigned type. */
3f75a254 5022 if (!TYPE_UNSIGNED (result_type))
3e4093b6 5023 /* OK */;
3e4093b6 5024 else
928c19bb
JM
5025 {
5026 bool op1_maybe_const = true;
5027 bool op2_maybe_const = true;
5028
5029 /* Do not warn if the signed quantity is an
5030 unsuffixed integer literal (or some static
5031 constant expression involving such literals) and
5032 it is non-negative. This warning requires the
5033 operands to be folded for best results, so do
5034 that folding in this case even without
5035 warn_sign_compare to avoid warning options
5036 possibly affecting code generation. */
f5178456
RS
5037 c_inhibit_evaluation_warnings
5038 += (ifexp == truthvalue_false_node);
928c19bb
JM
5039 op1 = c_fully_fold (op1, require_constant_value,
5040 &op1_maybe_const);
f5178456
RS
5041 c_inhibit_evaluation_warnings
5042 -= (ifexp == truthvalue_false_node);
5043
5044 c_inhibit_evaluation_warnings
5045 += (ifexp == truthvalue_true_node);
928c19bb
JM
5046 op2 = c_fully_fold (op2, require_constant_value,
5047 &op2_maybe_const);
f5178456
RS
5048 c_inhibit_evaluation_warnings
5049 -= (ifexp == truthvalue_true_node);
928c19bb
JM
5050
5051 if (warn_sign_compare)
5052 {
5053 if ((unsigned_op2
5054 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5055 || (unsigned_op1
5056 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5057 /* OK */;
a32c8316
MP
5058 else if (unsigned_op2)
5059 warning_at (op1_loc, OPT_Wsign_compare,
5060 "operand of ?: changes signedness from "
5061 "%qT to %qT due to unsignedness of other "
5062 "operand", TREE_TYPE (orig_op1),
5063 TREE_TYPE (orig_op2));
928c19bb 5064 else
a32c8316
MP
5065 warning_at (op2_loc, OPT_Wsign_compare,
5066 "operand of ?: changes signedness from "
5067 "%qT to %qT due to unsignedness of other "
5068 "operand", TREE_TYPE (orig_op2),
5069 TREE_TYPE (orig_op1));
928c19bb
JM
5070 }
5071 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 5072 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 5073 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 5074 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 5075 }
3e4093b6
RS
5076 }
5077 }
5078 }
5079 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5080 {
fcf73884 5081 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 5082 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 5083 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
5084 result_type = void_type_node;
5085 }
5086 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5087 {
36c5e70a
BE
5088 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5089 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5090 addr_space_t as_common;
5091
744aa42f 5092 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 5093 result_type = common_pointer_type (type1, type2);
6aa3c60d 5094 else if (null_pointer_constant_p (orig_op1))
36c5e70a 5095 result_type = type2;
6aa3c60d 5096 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
5097 result_type = type1;
5098 else if (!addr_space_superset (as1, as2, &as_common))
5099 {
5100 error_at (colon_loc, "pointers to disjoint address spaces "
5101 "used in conditional expression");
5102 return error_mark_node;
5103 }
267bac10
JM
5104 else if (VOID_TYPE_P (TREE_TYPE (type1))
5105 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 5106 {
768952be
MU
5107 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5108 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5109 & ~TYPE_QUALS (TREE_TYPE (type1))))
5110 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5111 "pointer to array loses qualifier "
5112 "in conditional expression");
5113
fcf73884 5114 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 5115 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 5116 "ISO C forbids conditional expr between "
bda67431 5117 "%<void *%> and function pointer");
3e4093b6
RS
5118 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5119 TREE_TYPE (type2)));
34a80643 5120 }
267bac10
JM
5121 else if (VOID_TYPE_P (TREE_TYPE (type2))
5122 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 5123 {
768952be
MU
5124 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5125 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5126 & ~TYPE_QUALS (TREE_TYPE (type2))))
5127 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5128 "pointer to array loses qualifier "
5129 "in conditional expression");
5130
fcf73884 5131 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 5132 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 5133 "ISO C forbids conditional expr between "
bda67431 5134 "%<void *%> and function pointer");
3e4093b6
RS
5135 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5136 TREE_TYPE (type1)));
1c2a9b35 5137 }
b581b85b
NP
5138 /* Objective-C pointer comparisons are a bit more lenient. */
5139 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5140 result_type = objc_common_type (type1, type2);
34a80643 5141 else
ab87f8c8 5142 {
36c5e70a
BE
5143 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5144
b581b85b
NP
5145 pedwarn (colon_loc, 0,
5146 "pointer type mismatch in conditional expression");
36c5e70a
BE
5147 result_type = build_pointer_type
5148 (build_qualified_type (void_type_node, qual));
ab87f8c8 5149 }
3e4093b6
RS
5150 }
5151 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5152 {
6aa3c60d 5153 if (!null_pointer_constant_p (orig_op2))
744aa42f 5154 pedwarn (colon_loc, 0,
509c9d60 5155 "pointer/integer type mismatch in conditional expression");
3e4093b6 5156 else
ab87f8c8 5157 {
3e4093b6 5158 op2 = null_pointer_node;
ab87f8c8 5159 }
3e4093b6
RS
5160 result_type = type1;
5161 }
5162 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5163 {
6aa3c60d 5164 if (!null_pointer_constant_p (orig_op1))
744aa42f 5165 pedwarn (colon_loc, 0,
509c9d60 5166 "pointer/integer type mismatch in conditional expression");
3e4093b6 5167 else
ab87f8c8 5168 {
3e4093b6 5169 op1 = null_pointer_node;
ab87f8c8 5170 }
3e4093b6
RS
5171 result_type = type2;
5172 }
1c2a9b35 5173
3e4093b6
RS
5174 if (!result_type)
5175 {
5176 if (flag_cond_mismatch)
5177 result_type = void_type_node;
5178 else
400fbf9f 5179 {
c2255bc4 5180 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 5181 return error_mark_node;
400fbf9f 5182 }
3e4093b6 5183 }
400fbf9f 5184
3e4093b6
RS
5185 /* Merge const and volatile flags of the incoming types. */
5186 result_type
5187 = build_type_variant (result_type,
afbd0665
AS
5188 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5189 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 5190
68fca595
MP
5191 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5192 semantic_result_type);
5193 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5194 semantic_result_type);
b6a10c9f 5195
928c19bb
JM
5196 if (ifexp_bcp && ifexp == truthvalue_true_node)
5197 {
5198 op2_int_operands = true;
5199 op1 = c_fully_fold (op1, require_constant_value, NULL);
5200 }
5201 if (ifexp_bcp && ifexp == truthvalue_false_node)
5202 {
5203 op1_int_operands = true;
5204 op2 = c_fully_fold (op2, require_constant_value, NULL);
5205 }
4d84fe7c 5206 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
5207 && op1_int_operands
5208 && op2_int_operands);
5209 if (int_operands)
5210 {
5211 int_const = ((ifexp == truthvalue_true_node
5212 && TREE_CODE (orig_op1) == INTEGER_CST
5213 && !TREE_OVERFLOW (orig_op1))
5214 || (ifexp == truthvalue_false_node
5215 && TREE_CODE (orig_op2) == INTEGER_CST
5216 && !TREE_OVERFLOW (orig_op2)));
5217 }
9f47c7e5
IE
5218
5219 /* Need to convert condition operand into a vector mask. */
5220 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5221 {
5222 tree vectype = TREE_TYPE (ifexp);
5223 tree elem_type = TREE_TYPE (vectype);
5224 tree zero = build_int_cst (elem_type, 0);
5225 tree zero_vec = build_vector_from_val (vectype, zero);
5226 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5227 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5228 }
5229
928c19bb 5230 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 5231 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
5232 else
5233 {
01c7ccbb
JM
5234 if (int_operands)
5235 {
f34f1c87
MP
5236 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5237 nested inside of the expression. */
5238 op1 = c_fully_fold (op1, false, NULL);
5239 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 5240 }
928c19bb
JM
5241 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5242 if (int_operands)
5243 ret = note_integer_operands (ret);
5244 }
2d2e923f
MLI
5245 if (semantic_result_type)
5246 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 5247
c2255bc4 5248 protected_set_expr_location (ret, colon_loc);
2ebd93e1
MP
5249
5250 /* If the OP1 and OP2 are the same and don't have side-effects,
5251 warn here, because the COND_EXPR will be turned into OP1. */
5252 if (warn_duplicated_branches
5253 && TREE_CODE (ret) == COND_EXPR
5254 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5255 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5256 "this condition has identical branches");
5257
928c19bb 5258 return ret;
3e4093b6
RS
5259}
5260\f
487a92fe 5261/* Return a compound expression that performs two expressions and
c2255bc4
AH
5262 returns the value of the second of them.
5263
5264 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 5265
3e4093b6 5266tree
c2255bc4 5267build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 5268{
4d84fe7c 5269 bool expr1_int_operands, expr2_int_operands;
8ce94e44 5270 tree eptype = NULL_TREE;
928c19bb
JM
5271 tree ret;
5272
b72271b9 5273 if (flag_cilkplus
939b37da
BI
5274 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5275 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5276 {
5277 error_at (loc,
5278 "spawned function call cannot be part of a comma expression");
5279 return error_mark_node;
5280 }
4d84fe7c
JM
5281 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5282 if (expr1_int_operands)
5283 expr1 = remove_c_maybe_const_expr (expr1);
5284 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5285 if (expr2_int_operands)
5286 expr2 = remove_c_maybe_const_expr (expr2);
5287
8ce94e44
JM
5288 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5289 expr1 = TREE_OPERAND (expr1, 0);
5290 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5291 {
5292 eptype = TREE_TYPE (expr2);
5293 expr2 = TREE_OPERAND (expr2, 0);
5294 }
5295
3f75a254 5296 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
5297 {
5298 /* The left-hand operand of a comma expression is like an expression
c5409249 5299 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 5300 any side-effects, unless it was explicitly cast to (void). */
e14a6540 5301 if (warn_unused_value)
47aecf47 5302 {
e14a6540 5303 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 5304 && CONVERT_EXPR_P (expr1))
47aecf47 5305 ; /* (void) a, b */
e14a6540
JM
5306 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5307 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 5308 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
5309 ; /* (void) a, (void) b, c */
5310 else
b8698a0f 5311 warning_at (loc, OPT_Wunused_value,
c2255bc4 5312 "left-hand operand of comma expression has no effect");
47aecf47 5313 }
3e4093b6 5314 }
789eadcd
MP
5315 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5316 && warn_unused_value)
5317 {
5318 tree r = expr1;
5319 location_t cloc = loc;
5320 while (TREE_CODE (r) == COMPOUND_EXPR)
5321 {
5322 if (EXPR_HAS_LOCATION (r))
5323 cloc = EXPR_LOCATION (r);
5324 r = TREE_OPERAND (r, 1);
5325 }
5326 if (!TREE_SIDE_EFFECTS (r)
5327 && !VOID_TYPE_P (TREE_TYPE (r))
5328 && !CONVERT_EXPR_P (r))
5329 warning_at (cloc, OPT_Wunused_value,
5330 "right-hand operand of comma expression has no effect");
5331 }
400fbf9f 5332
3e4093b6
RS
5333 /* With -Wunused, we should also warn if the left-hand operand does have
5334 side-effects, but computes a value which is not used. For example, in
5335 `foo() + bar(), baz()' the result of the `+' operator is not used,
5336 so we should issue a warning. */
5337 else if (warn_unused_value)
c2255bc4 5338 warn_if_unused_value (expr1, loc);
400fbf9f 5339
e63d6886
AP
5340 if (expr2 == error_mark_node)
5341 return error_mark_node;
5342
928c19bb
JM
5343 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5344
5345 if (flag_isoc99
4d84fe7c
JM
5346 && expr1_int_operands
5347 && expr2_int_operands)
928c19bb
JM
5348 ret = note_integer_operands (ret);
5349
8ce94e44
JM
5350 if (eptype)
5351 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5352
c2255bc4 5353 protected_set_expr_location (ret, loc);
928c19bb 5354 return ret;
3e4093b6 5355}
400fbf9f 5356
67165eb3
ILT
5357/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5358 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
5359 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5360 of the cast. -Wcast-qual appeared on the command line. Named
5361 address space qualifiers are not handled here, because they result
5362 in different warnings. */
67165eb3
ILT
5363
5364static void
2ee3cb35 5365handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
5366{
5367 tree in_type = type;
5368 tree in_otype = otype;
5369 int added = 0;
5370 int discarded = 0;
5371 bool is_const;
5372
5373 /* Check that the qualifiers on IN_TYPE are a superset of the
5374 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5375 nodes is uninteresting and we stop as soon as we hit a
5376 non-POINTER_TYPE node on either type. */
5377 do
5378 {
5379 in_otype = TREE_TYPE (in_otype);
5380 in_type = TREE_TYPE (in_type);
5381
5382 /* GNU C allows cv-qualified function types. 'const' means the
5383 function is very pure, 'volatile' means it can't return. We
5384 need to warn when such qualifiers are added, not when they're
5385 taken away. */
5386 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5387 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
5388 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5389 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 5390 else
36c5e70a
BE
5391 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5392 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
5393 }
5394 while (TREE_CODE (in_type) == POINTER_TYPE
5395 && TREE_CODE (in_otype) == POINTER_TYPE);
5396
5397 if (added)
2ee3cb35
MLI
5398 warning_at (loc, OPT_Wcast_qual,
5399 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
5400
5401 if (discarded)
5402 /* There are qualifiers present in IN_OTYPE that are not present
5403 in IN_TYPE. */
2ee3cb35 5404 warning_at (loc, OPT_Wcast_qual,
7485aeea 5405 "cast discards %qv qualifier from pointer target type",
2ee3cb35 5406 discarded);
67165eb3
ILT
5407
5408 if (added || discarded)
5409 return;
5410
5411 /* A cast from **T to const **T is unsafe, because it can cause a
5412 const value to be changed with no additional warning. We only
5413 issue this warning if T is the same on both sides, and we only
5414 issue the warning if there are the same number of pointers on
5415 both sides, as otherwise the cast is clearly unsafe anyhow. A
5416 cast is unsafe when a qualifier is added at one level and const
5417 is not present at all outer levels.
5418
5419 To issue this warning, we check at each level whether the cast
5420 adds new qualifiers not already seen. We don't need to special
5421 case function types, as they won't have the same
5422 TYPE_MAIN_VARIANT. */
5423
5424 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5425 return;
5426 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5427 return;
5428
5429 in_type = type;
5430 in_otype = otype;
5431 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5432 do
5433 {
5434 in_type = TREE_TYPE (in_type);
5435 in_otype = TREE_TYPE (in_otype);
5436 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5437 && !is_const)
5438 {
2ee3cb35
MLI
5439 warning_at (loc, OPT_Wcast_qual,
5440 "to be safe all intermediate pointers in cast from "
5441 "%qT to %qT must be %<const%> qualified",
5442 otype, type);
67165eb3
ILT
5443 break;
5444 }
5445 if (is_const)
5446 is_const = TYPE_READONLY (in_type);
5447 }
5448 while (TREE_CODE (in_type) == POINTER_TYPE);
5449}
5450
b8698a0f 5451/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 5452 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 5453
3e4093b6 5454tree
c2255bc4 5455build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 5456{
8ce94e44
JM
5457 tree value;
5458
5459 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5460 expr = TREE_OPERAND (expr, 0);
5461
5462 value = expr;
400fbf9f 5463
3e4093b6
RS
5464 if (type == error_mark_node || expr == error_mark_node)
5465 return error_mark_node;
400fbf9f 5466
3e4093b6
RS
5467 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5468 only in <protocol> qualifications. But when constructing cast expressions,
5469 the protocols do matter and must be kept around. */
700686fa
ZL
5470 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5471 return build1 (NOP_EXPR, type, expr);
5472
5473 type = TYPE_MAIN_VARIANT (type);
400fbf9f 5474
3e4093b6
RS
5475 if (TREE_CODE (type) == ARRAY_TYPE)
5476 {
c2255bc4 5477 error_at (loc, "cast specifies array type");
3e4093b6
RS
5478 return error_mark_node;
5479 }
400fbf9f 5480
3e4093b6
RS
5481 if (TREE_CODE (type) == FUNCTION_TYPE)
5482 {
c2255bc4 5483 error_at (loc, "cast specifies function type");
3e4093b6
RS
5484 return error_mark_node;
5485 }
400fbf9f 5486
808d6eaa
JM
5487 if (!VOID_TYPE_P (type))
5488 {
4f2e1536 5489 value = require_complete_type (loc, value);
808d6eaa
JM
5490 if (value == error_mark_node)
5491 return error_mark_node;
5492 }
5493
3e4093b6
RS
5494 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5495 {
3e636daf 5496 if (RECORD_OR_UNION_TYPE_P (type))
c1771a20 5497 pedwarn (loc, OPT_Wpedantic,
fcf73884 5498 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5499
5500 /* Convert to remove any qualifiers from VALUE's type. */
5501 value = convert (type, value);
3e4093b6
RS
5502 }
5503 else if (TREE_CODE (type) == UNION_TYPE)
5504 {
5505 tree field;
400fbf9f 5506
910ad8de 5507 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5508 if (TREE_TYPE (field) != error_mark_node
5509 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5510 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5511 break;
5512
5513 if (field)
400fbf9f 5514 {
3e4093b6 5515 tree t;
e616f54d 5516 bool maybe_const = true;
3e4093b6 5517
c1771a20 5518 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5519 t = c_fully_fold (value, false, &maybe_const);
5520 t = build_constructor_single (type, field, t);
5521 if (!maybe_const)
5522 t = c_wrap_maybe_const (t, true);
5523 t = digest_init (loc, type, t,
bbbbb16a 5524 NULL_TREE, false, true, 0);
3e4093b6
RS
5525 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5526 return t;
400fbf9f 5527 }
c2255bc4 5528 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5529 return error_mark_node;
5530 }
5531 else
5532 {
5533 tree otype, ovalue;
400fbf9f 5534
3e4093b6 5535 if (type == void_type_node)
c2255bc4
AH
5536 {
5537 tree t = build1 (CONVERT_EXPR, type, value);
5538 SET_EXPR_LOCATION (t, loc);
5539 return t;
5540 }
400fbf9f 5541
3e4093b6 5542 otype = TREE_TYPE (value);
400fbf9f 5543
3e4093b6 5544 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5545 if (warn_cast_qual
5546 && TREE_CODE (type) == POINTER_TYPE
5547 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5548 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5549
36c5e70a
BE
5550 /* Warn about conversions between pointers to disjoint
5551 address spaces. */
5552 if (TREE_CODE (type) == POINTER_TYPE
5553 && TREE_CODE (otype) == POINTER_TYPE
5554 && !null_pointer_constant_p (value))
5555 {
5556 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5557 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5558 addr_space_t as_common;
5559
5560 if (!addr_space_superset (as_to, as_from, &as_common))
5561 {
5562 if (ADDR_SPACE_GENERIC_P (as_from))
5563 warning_at (loc, 0, "cast to %s address space pointer "
5564 "from disjoint generic address space pointer",
5565 c_addr_space_name (as_to));
5566
5567 else if (ADDR_SPACE_GENERIC_P (as_to))
5568 warning_at (loc, 0, "cast to generic address space pointer "
5569 "from disjoint %s address space pointer",
5570 c_addr_space_name (as_from));
5571
5572 else
5573 warning_at (loc, 0, "cast to %s address space pointer "
5574 "from disjoint %s address space pointer",
5575 c_addr_space_name (as_to),
5576 c_addr_space_name (as_from));
5577 }
5578 }
5579
3e4093b6 5580 /* Warn about possible alignment problems. */
1d933576 5581 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
3e4093b6
RS
5582 && TREE_CODE (type) == POINTER_TYPE
5583 && TREE_CODE (otype) == POINTER_TYPE
5584 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5585 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5586 /* Don't warn about opaque types, where the actual alignment
5587 restriction is unknown. */
3e636daf 5588 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
3e4093b6 5589 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
1d933576
BE
5590 && min_align_of_type (TREE_TYPE (type))
5591 > min_align_of_type (TREE_TYPE (otype)))
c2255bc4
AH
5592 warning_at (loc, OPT_Wcast_align,
5593 "cast increases required alignment of target type");
e9a25f70 5594
3176a0c2 5595 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5596 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5597 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5598 /* Unlike conversion of integers to pointers, where the
5599 warning is disabled for converting constants because
5600 of cases such as SIG_*, warn about converting constant
5601 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5602 sign extension, and a warning is appropriate. */
c2255bc4
AH
5603 warning_at (loc, OPT_Wpointer_to_int_cast,
5604 "cast from pointer to integer of different size");
400fbf9f 5605
3176a0c2 5606 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5607 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5608 warning_at (loc, OPT_Wbad_function_cast,
5609 "cast from function call of type %qT "
5610 "to non-matching type %qT", otype, type);
400fbf9f 5611
3176a0c2 5612 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5613 && TREE_CODE (otype) == INTEGER_TYPE
5614 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5615 /* Don't warn about converting any constant. */
5616 && !TREE_CONSTANT (value))
c2255bc4
AH
5617 warning_at (loc,
5618 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5619 "of different size");
400fbf9f 5620
79bedddc
SR
5621 if (warn_strict_aliasing <= 2)
5622 strict_aliasing_warning (otype, type, expr);
400fbf9f 5623
3897f229
JM
5624 /* If pedantic, warn for conversions between function and object
5625 pointer types, except for converting a null pointer constant
5626 to function pointer type. */
5627 if (pedantic
5628 && TREE_CODE (type) == POINTER_TYPE
5629 && TREE_CODE (otype) == POINTER_TYPE
5630 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5631 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5632 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5633 "conversion of function pointer to object pointer type");
3897f229
JM
5634
5635 if (pedantic
5636 && TREE_CODE (type) == POINTER_TYPE
5637 && TREE_CODE (otype) == POINTER_TYPE
5638 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5639 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5640 && !null_pointer_constant_p (value))
c1771a20 5641 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5642 "conversion of object pointer to function pointer type");
3897f229 5643
3e4093b6 5644 ovalue = value;
3e4093b6 5645 value = convert (type, value);
400fbf9f 5646
3e4093b6 5647 /* Ignore any integer overflow caused by the cast. */
928c19bb 5648 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5649 {
8bcd6380 5650 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5651 {
8bcd6380
RS
5652 if (!TREE_OVERFLOW (value))
5653 {
5654 /* Avoid clobbering a shared constant. */
5655 value = copy_node (value);
5656 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5657 }
6414bad6 5658 }
8bcd6380 5659 else if (TREE_OVERFLOW (value))
d8e1f97b 5660 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5661 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5662 }
5663 }
400fbf9f 5664
53cd18ec 5665 /* Don't let a cast be an lvalue. */
9482b620 5666 if (lvalue_p (value))
db3927fb 5667 value = non_lvalue_loc (loc, value);
e9a25f70 5668
928c19bb
JM
5669 /* Don't allow the results of casting to floating-point or complex
5670 types be confused with actual constants, or casts involving
5671 integer and pointer types other than direct integer-to-integer
5672 and integer-to-pointer be confused with integer constant
5673 expressions and null pointer constants. */
5674 if (TREE_CODE (value) == REAL_CST
5675 || TREE_CODE (value) == COMPLEX_CST
5676 || (TREE_CODE (value) == INTEGER_CST
5677 && !((TREE_CODE (expr) == INTEGER_CST
5678 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5679 || TREE_CODE (expr) == REAL_CST
5680 || TREE_CODE (expr) == COMPLEX_CST)))
5681 value = build1 (NOP_EXPR, type, value);
5682
21ba0cea 5683 protected_set_expr_location (value, loc);
3e4093b6 5684 return value;
400fbf9f
JW
5685}
5686
c2255bc4
AH
5687/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5688 location of the open paren of the cast, or the position of the cast
5689 expr. */
3e4093b6 5690tree
c2255bc4 5691c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5692{
f8893e47 5693 tree type;
928c19bb
JM
5694 tree type_expr = NULL_TREE;
5695 bool type_expr_const = true;
5696 tree ret;
3e4093b6 5697 int saved_wsp = warn_strict_prototypes;
c5c76735 5698
3e4093b6
RS
5699 /* This avoids warnings about unprototyped casts on
5700 integers. E.g. "#define SIG_DFL (void(*)())0". */
5701 if (TREE_CODE (expr) == INTEGER_CST)
5702 warn_strict_prototypes = 0;
928c19bb 5703 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5704 warn_strict_prototypes = saved_wsp;
c5c76735 5705
1807ffc1
MS
5706 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5707 && reject_gcc_builtin (expr))
5708 return error_mark_node;
5709
c2255bc4 5710 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5711 if (type_expr)
5712 {
9f33203d
JM
5713 bool inner_expr_const = true;
5714 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5715 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5716 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5717 && inner_expr_const);
c2255bc4 5718 SET_EXPR_LOCATION (ret, loc);
928c19bb 5719 }
24b97832 5720
21ba0cea
MP
5721 if (!EXPR_HAS_LOCATION (ret))
5722 protected_set_expr_location (ret, loc);
24b97832 5723
9e5b2115
PB
5724 /* C++ does not permits types to be defined in a cast, but it
5725 allows references to incomplete types. */
5726 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5727 warning_at (loc, OPT_Wc___compat,
5728 "defining a type in a cast is invalid in C++");
5729
928c19bb 5730 return ret;
400fbf9f 5731}
3e4093b6
RS
5732\f
5733/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5734 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5735 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5736 MODIFYCODE is the code for a binary operator that we use
5737 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5738 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5739 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5740 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5741
c2255bc4
AH
5742 LOCATION is the location of the MODIFYCODE operator.
5743 RHS_LOC is the location of the RHS. */
2f6e4e97 5744
3e4093b6 5745tree
32e8bb8e 5746build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5747 enum tree_code modifycode,
c2255bc4 5748 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5749{
3e4093b6
RS
5750 tree result;
5751 tree newrhs;
241f845a 5752 tree rhseval = NULL_TREE;
3e4093b6
RS
5753 tree lhstype = TREE_TYPE (lhs);
5754 tree olhstype = lhstype;
928c19bb 5755 bool npc;
267bac10 5756 bool is_atomic_op;
e9a25f70 5757
3e4093b6 5758 /* Types that aren't fully specified cannot be used in assignments. */
4f2e1536 5759 lhs = require_complete_type (location, lhs);
e9a25f70 5760
3e4093b6
RS
5761 /* Avoid duplicate error messages from operands that had errors. */
5762 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5763 return error_mark_node;
400fbf9f 5764
4c2ecab0
JM
5765 /* Ensure an error for assigning a non-lvalue array to an array in
5766 C90. */
5767 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5768 {
5769 error_at (location, "assignment to expression with array type");
5770 return error_mark_node;
5771 }
5772
46a88c12 5773 /* For ObjC properties, defer this check. */
7bd11157 5774 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5775 return error_mark_node;
5776
267bac10
JM
5777 is_atomic_op = really_atomic_lvalue (lhs);
5778
3e4093b6 5779 newrhs = rhs;
400fbf9f 5780
928c19bb
JM
5781 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5782 {
5783 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5784 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5785 rhs_origtype);
928c19bb
JM
5786 if (inner == error_mark_node)
5787 return error_mark_node;
5788 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5789 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5790 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5791 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5792 protected_set_expr_location (result, location);
5793 return result;
5794 }
5795
3e4093b6
RS
5796 /* If a binary op has been requested, combine the old LHS value with the RHS
5797 producing the value we should actually store into the LHS. */
5798
5799 if (modifycode != NOP_EXPR)
400fbf9f 5800 {
928c19bb 5801 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5802 lhs = stabilize_reference (lhs);
bbbbb16a 5803
267bac10
JM
5804 /* Construct the RHS for any non-atomic compound assignemnt. */
5805 if (!is_atomic_op)
5806 {
241f845a
JJ
5807 /* If in LHS op= RHS the RHS has side-effects, ensure they
5808 are preevaluated before the rest of the assignment expression's
5809 side-effects, because RHS could contain e.g. function calls
5810 that modify LHS. */
5811 if (TREE_SIDE_EFFECTS (rhs))
5812 {
e035be33
JM
5813 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5814 newrhs = save_expr (TREE_OPERAND (rhs, 0));
5815 else
5816 newrhs = save_expr (rhs);
241f845a 5817 rhseval = newrhs;
e035be33
JM
5818 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5819 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
5820 newrhs);
241f845a 5821 }
267bac10 5822 newrhs = build_binary_op (location,
30af3a2b 5823 modifycode, lhs, newrhs, true);
267bac10
JM
5824
5825 /* The original type of the right hand side is no longer
5826 meaningful. */
5827 rhs_origtype = NULL_TREE;
5828 }
400fbf9f 5829 }
400fbf9f 5830
668ea4b1
IS
5831 if (c_dialect_objc ())
5832 {
46a88c12
NP
5833 /* Check if we are modifying an Objective-C property reference;
5834 if so, we need to generate setter calls. */
e035be33
JM
5835 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5836 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
5837 else
5838 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5839 if (result)
241f845a 5840 goto return_result;
46a88c12
NP
5841
5842 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5843 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5844 return error_mark_node;
5845 }
5846
9bf24266 5847 /* Give an error for storing in something that is 'const'. */
bbbd6700 5848
f37acdf9 5849 if (TYPE_READONLY (lhstype)
3e636daf 5850 || (RECORD_OR_UNION_TYPE_P (lhstype)
3e4093b6 5851 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5852 {
c02065fc 5853 readonly_error (location, lhs, lv_assign);
953ff289
DN
5854 return error_mark_node;
5855 }
f37acdf9
JM
5856 else if (TREE_READONLY (lhs))
5857 readonly_warning (lhs, lv_assign);
bbbd6700 5858
3e4093b6
RS
5859 /* If storing into a structure or union member,
5860 it has probably been given type `int'.
5861 Compute the type that would go with
5862 the actual amount of storage the member occupies. */
bbbd6700 5863
3e4093b6
RS
5864 if (TREE_CODE (lhs) == COMPONENT_REF
5865 && (TREE_CODE (lhstype) == INTEGER_TYPE
5866 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5867 || TREE_CODE (lhstype) == REAL_TYPE
5868 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5869 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5870
3e4093b6
RS
5871 /* If storing in a field that is in actuality a short or narrower than one,
5872 we must store in the field in its actual type. */
5873
5874 if (lhstype != TREE_TYPE (lhs))
5875 {
5876 lhs = copy_node (lhs);
5877 TREE_TYPE (lhs) = lhstype;
400fbf9f 5878 }
400fbf9f 5879
32e8bb8e
ILT
5880 /* Issue -Wc++-compat warnings about an assignment to an enum type
5881 when LHS does not have its original type. This happens for,
5882 e.g., an enum bitfield in a struct. */
5883 if (warn_cxx_compat
5884 && lhs_origtype != NULL_TREE
5885 && lhs_origtype != lhstype
5886 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5887 {
5888 tree checktype = (rhs_origtype != NULL_TREE
5889 ? rhs_origtype
5890 : TREE_TYPE (rhs));
5891 if (checktype != error_mark_node
267bac10
JM
5892 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5893 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5894 warning_at (location, OPT_Wc___compat,
5895 "enum conversion in assignment is invalid in C++");
5896 }
5897
267bac10
JM
5898 /* If the lhs is atomic, remove that qualifier. */
5899 if (is_atomic_op)
5900 {
5901 lhstype = build_qualified_type (lhstype,
5902 (TYPE_QUALS (lhstype)
5903 & ~TYPE_QUAL_ATOMIC));
5904 olhstype = build_qualified_type (olhstype,
5905 (TYPE_QUALS (lhstype)
5906 & ~TYPE_QUAL_ATOMIC));
5907 }
5908
8ce94e44
JM
5909 /* Convert new value to destination type. Fold it first, then
5910 restore any excess precision information, for the sake of
5911 conversion warnings. */
400fbf9f 5912
267bac10
JM
5913 if (!(is_atomic_op && modifycode != NOP_EXPR))
5914 {
e035be33
JM
5915 tree rhs_semantic_type = NULL_TREE;
5916 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5917 {
5918 rhs_semantic_type = TREE_TYPE (newrhs);
5919 newrhs = TREE_OPERAND (newrhs, 0);
5920 }
267bac10
JM
5921 npc = null_pointer_constant_p (newrhs);
5922 newrhs = c_fully_fold (newrhs, false, NULL);
5923 if (rhs_semantic_type)
5924 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5925 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5926 rhs_origtype, ic_assign, npc,
5927 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5928 if (TREE_CODE (newrhs) == ERROR_MARK)
5929 return error_mark_node;
5930 }
400fbf9f 5931
6e955430
ZL
5932 /* Emit ObjC write barrier, if necessary. */
5933 if (c_dialect_objc () && flag_objc_gc)
5934 {
5935 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5936 if (result)
c9f9eb5d
AH
5937 {
5938 protected_set_expr_location (result, location);
241f845a 5939 goto return_result;
c9f9eb5d 5940 }
6e955430
ZL
5941 }
5942
ea4b7848 5943 /* Scan operands. */
400fbf9f 5944
267bac10
JM
5945 if (is_atomic_op)
5946 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5947 else
5948 {
5949 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5950 TREE_SIDE_EFFECTS (result) = 1;
5951 protected_set_expr_location (result, location);
5952 }
400fbf9f 5953
3e4093b6
RS
5954 /* If we got the LHS in a different type for storing in,
5955 convert the result back to the nominal type of LHS
5956 so that the value we return always has the same type
5957 as the LHS argument. */
e855c5ce 5958
3e4093b6 5959 if (olhstype == TREE_TYPE (result))
241f845a 5960 goto return_result;
c9f9eb5d 5961
68fca595
MP
5962 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5963 rhs_origtype, ic_assign, false, NULL_TREE,
5964 NULL_TREE, 0);
c9f9eb5d 5965 protected_set_expr_location (result, location);
241f845a
JJ
5966
5967return_result:
5968 if (rhseval)
5969 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5970 return result;
3e4093b6
RS
5971}
5972\f
478a1c5b
ILT
5973/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5974 This is used to implement -fplan9-extensions. */
5975
5976static bool
5977find_anonymous_field_with_type (tree struct_type, tree type)
5978{
5979 tree field;
5980 bool found;
5981
3e636daf 5982 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
478a1c5b
ILT
5983 found = false;
5984 for (field = TYPE_FIELDS (struct_type);
5985 field != NULL_TREE;
5986 field = TREE_CHAIN (field))
5987 {
267bac10
JM
5988 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5989 ? c_build_qualified_type (TREE_TYPE (field),
5990 TYPE_QUAL_ATOMIC)
5991 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5992 if (DECL_NAME (field) == NULL
267bac10 5993 && comptypes (type, fieldtype))
478a1c5b
ILT
5994 {
5995 if (found)
5996 return false;
5997 found = true;
5998 }
5999 else if (DECL_NAME (field) == NULL
3e636daf 6000 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
478a1c5b
ILT
6001 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6002 {
6003 if (found)
6004 return false;
6005 found = true;
6006 }
6007 }
6008 return found;
6009}
6010
6011/* RHS is an expression whose type is pointer to struct. If there is
6012 an anonymous field in RHS with type TYPE, then return a pointer to
6013 that field in RHS. This is used with -fplan9-extensions. This
6014 returns NULL if no conversion could be found. */
6015
6016static tree
6017convert_to_anonymous_field (location_t location, tree type, tree rhs)
6018{
6019 tree rhs_struct_type, lhs_main_type;
6020 tree field, found_field;
6021 bool found_sub_field;
6022 tree ret;
6023
6024 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6025 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
3e636daf 6026 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
478a1c5b
ILT
6027
6028 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
6029 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6030 ? c_build_qualified_type (TREE_TYPE (type),
6031 TYPE_QUAL_ATOMIC)
6032 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
6033
6034 found_field = NULL_TREE;
6035 found_sub_field = false;
6036 for (field = TYPE_FIELDS (rhs_struct_type);
6037 field != NULL_TREE;
6038 field = TREE_CHAIN (field))
6039 {
6040 if (DECL_NAME (field) != NULL_TREE
3e636daf 6041 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
478a1c5b 6042 continue;
267bac10
JM
6043 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6044 ? c_build_qualified_type (TREE_TYPE (field),
6045 TYPE_QUAL_ATOMIC)
6046 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6047 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
6048 {
6049 if (found_field != NULL_TREE)
6050 return NULL_TREE;
6051 found_field = field;
6052 }
6053 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6054 lhs_main_type))
6055 {
6056 if (found_field != NULL_TREE)
6057 return NULL_TREE;
6058 found_field = field;
6059 found_sub_field = true;
6060 }
6061 }
6062
6063 if (found_field == NULL_TREE)
6064 return NULL_TREE;
6065
6066 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6067 build_fold_indirect_ref (rhs), found_field,
6068 NULL_TREE);
6069 ret = build_fold_addr_expr_loc (location, ret);
6070
6071 if (found_sub_field)
6072 {
6073 ret = convert_to_anonymous_field (location, type, ret);
6074 gcc_assert (ret != NULL_TREE);
6075 }
6076
6077 return ret;
6078}
6079
63bc4e87
MP
6080/* Issue an error message for a bad initializer component.
6081 GMSGID identifies the message.
6082 The component name is taken from the spelling stack. */
6083
6084static void
ea58ef42 6085error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
6086{
6087 char *ofwhat;
6088
6089 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 6090 error_at (loc, gmsgid);
63bc4e87
MP
6091 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6092 if (*ofwhat)
d7ff7ae5 6093 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6094}
6095
6096/* Issue a pedantic warning for a bad initializer component. OPT is
6097 the option OPT_* (from options.h) controlling this warning or 0 if
6098 it is unconditionally given. GMSGID identifies the message. The
6099 component name is taken from the spelling stack. */
6100
296c53ac
MP
6101static void ATTRIBUTE_GCC_DIAG (3,0)
6102pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
63bc4e87 6103{
79063edd
MS
6104 /* Use the location where a macro was expanded rather than where
6105 it was defined to make sure macros defined in system headers
6106 but used incorrectly elsewhere are diagnosed. */
6107 source_location exploc = expansion_point_location_if_in_system_header (loc);
6108
296c53ac
MP
6109 va_list ap;
6110 va_start (ap, gmsgid);
6111 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6112 va_end (ap);
6113 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 6114 if (*ofwhat && warned)
79063edd 6115 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6116}
6117
6118/* Issue a warning for a bad initializer component.
6119
6120 OPT is the OPT_W* value corresponding to the warning option that
6121 controls this warning. GMSGID identifies the message. The
6122 component name is taken from the spelling stack. */
6123
6124static void
6125warning_init (location_t loc, int opt, const char *gmsgid)
6126{
6127 char *ofwhat;
d7ff7ae5 6128 bool warned;
63bc4e87 6129
79063edd
MS
6130 /* Use the location where a macro was expanded rather than where
6131 it was defined to make sure macros defined in system headers
6132 but used incorrectly elsewhere are diagnosed. */
6133 source_location exploc = expansion_point_location_if_in_system_header (loc);
6134
63bc4e87 6135 /* The gmsgid may be a format string with %< and %>. */
79063edd 6136 warned = warning_at (exploc, opt, gmsgid);
63bc4e87 6137 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 6138 if (*ofwhat && warned)
79063edd 6139 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6140}
6141\f
6142/* If TYPE is an array type and EXPR is a parenthesized string
6143 constant, warn if pedantic that EXPR is being used to initialize an
6144 object of type TYPE. */
6145
6146void
d033409e 6147maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
6148{
6149 if (pedantic
6150 && TREE_CODE (type) == ARRAY_TYPE
6151 && TREE_CODE (expr.value) == STRING_CST
6152 && expr.original_code != STRING_CST)
d033409e 6153 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
6154 "array initialized from parenthesized string constant");
6155}
6156
bbbbb16a
ILT
6157/* Convert value RHS to type TYPE as preparation for an assignment to
6158 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6159 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6160 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6161 constant before any folding.
3e4093b6
RS
6162 The real work of conversion is done by `convert'.
6163 The purpose of this function is to generate error messages
6164 for assignments that are not allowed in C.
2ac2f164
JM
6165 ERRTYPE says whether it is argument passing, assignment,
6166 initialization or return.
2f6e4e97 6167
aabef2de
MP
6168 In the following example, '~' denotes where EXPR_LOC and '^' where
6169 LOCATION point to:
6170
6171 f (var); [ic_argpass]
6172 ^ ~~~
6173 x = var; [ic_assign]
6174 ^ ~~~;
6175 int x = var; [ic_init]
6176 ^^^
6177 return x; [ic_return]
6178 ^
6179
2ac2f164 6180 FUNCTION is a tree for the function being called.
3e4093b6 6181 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 6182
3e4093b6 6183static tree
81e5eca8 6184convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 6185 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
6186 bool null_pointer_constant, tree fundecl,
6187 tree function, int parmnum)
3e4093b6
RS
6188{
6189 enum tree_code codel = TREE_CODE (type);
8ce94e44 6190 tree orig_rhs = rhs;
3e4093b6
RS
6191 tree rhstype;
6192 enum tree_code coder;
2ac2f164 6193 tree rname = NULL_TREE;
58393038 6194 bool objc_ok = false;
2ac2f164 6195
1c7485af
MP
6196 /* Use the expansion point location to handle cases such as user's
6197 function returning a wrong-type macro defined in a system header. */
6198 location = expansion_point_location_if_in_system_header (location);
6199
6b4ef5c1 6200 if (errtype == ic_argpass)
2ac2f164
JM
6201 {
6202 tree selector;
6203 /* Change pointer to function to the function itself for
6204 diagnostics. */
6205 if (TREE_CODE (function) == ADDR_EXPR
6206 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6207 function = TREE_OPERAND (function, 0);
6208
6209 /* Handle an ObjC selector specially for diagnostics. */
6210 selector = objc_message_selector ();
6211 rname = function;
6212 if (selector && parmnum > 2)
6213 {
6214 rname = selector;
6215 parmnum -= 2;
6216 }
6217 }
6218
6219 /* This macro is used to emit diagnostics to ensure that all format
6220 strings are complete sentences, visible to gettext and checked at
6221 compile time. */
768952be 6222#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
6223 do { \
6224 switch (errtype) \
6225 { \
6226 case ic_argpass: \
5c1bc275 6227 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 6228 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 6229 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
6230 "expected %qT but argument is of type %qT", \
6231 type, rhstype); \
6232 break; \
1e053dfe
MLI
6233 case ic_assign: \
6234 pedwarn (LOCATION, OPT, AS); \
6235 break; \
6236 case ic_init: \
6a8f4e12 6237 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
6238 break; \
6239 case ic_return: \
d033409e 6240 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
6241 break; \
6242 default: \
6243 gcc_unreachable (); \
6244 } \
2ac2f164 6245 } while (0)
cb3ca04e 6246
49706e39
MLI
6247 /* This macro is used to emit diagnostics to ensure that all format
6248 strings are complete sentences, visible to gettext and checked at
768952be 6249 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 6250 extra parameter to enumerate qualifiers. */
768952be 6251#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
6252 do { \
6253 switch (errtype) \
6254 { \
6255 case ic_argpass: \
5c1bc275 6256 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 6257 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 6258 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
6259 "expected %qT but argument is of type %qT", \
6260 type, rhstype); \
6261 break; \
6262 case ic_assign: \
5c1bc275 6263 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
6264 break; \
6265 case ic_init: \
5c1bc275 6266 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
6267 break; \
6268 case ic_return: \
5c1bc275 6269 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
6270 break; \
6271 default: \
6272 gcc_unreachable (); \
6273 } \
6274 } while (0)
6275
768952be
MU
6276 /* This macro is used to emit diagnostics to ensure that all format
6277 strings are complete sentences, visible to gettext and checked at
6278 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6279 warning_at instead of pedwarn. */
6280#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6281 do { \
6282 switch (errtype) \
6283 { \
6284 case ic_argpass: \
6285 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6286 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6287 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6288 "expected %qT but argument is of type %qT", \
6289 type, rhstype); \
6290 break; \
6291 case ic_assign: \
6292 warning_at (LOCATION, OPT, AS, QUALS); \
6293 break; \
6294 case ic_init: \
6295 warning_at (LOCATION, OPT, IN, QUALS); \
6296 break; \
6297 case ic_return: \
6298 warning_at (LOCATION, OPT, RE, QUALS); \
6299 break; \
6300 default: \
6301 gcc_unreachable (); \
6302 } \
6303 } while (0)
6304
8ce94e44
JM
6305 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6306 rhs = TREE_OPERAND (rhs, 0);
6307
3e4093b6
RS
6308 rhstype = TREE_TYPE (rhs);
6309 coder = TREE_CODE (rhstype);
6310
6311 if (coder == ERROR_MARK)
6312 return error_mark_node;
6313
58393038
ZL
6314 if (c_dialect_objc ())
6315 {
6316 int parmno;
6317
6318 switch (errtype)
6319 {
6320 case ic_return:
6321 parmno = 0;
6322 break;
6323
6324 case ic_assign:
6325 parmno = -1;
6326 break;
6327
6328 case ic_init:
6329 parmno = -2;
6330 break;
6331
6332 default:
6333 parmno = parmnum;
6334 break;
6335 }
6336
6337 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6338 }
6339
bbbbb16a
ILT
6340 if (warn_cxx_compat)
6341 {
6342 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6343 if (checktype != error_mark_node
6344 && TREE_CODE (type) == ENUMERAL_TYPE
6345 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
296c53ac
MP
6346 switch (errtype)
6347 {
6348 case ic_argpass:
6349 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6350 "passing argument %d of %qE is invalid in C++",
6351 parmnum, rname))
6352 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6353 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6354 "expected %qT but argument is of type %qT",
6355 type, rhstype);
6356 break;
6357 case ic_assign:
6358 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6359 "%qT in assignment is invalid in C++", rhstype, type);
6360 break;
6361 case ic_init:
6362 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6363 "%qT to %qT in initialization is invalid in C++",
6364 rhstype, type);
6365 break;
6366 case ic_return:
6367 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6368 "%qT in return is invalid in C++", rhstype, type);
6369 break;
6370 default:
6371 gcc_unreachable ();
6372 }
bbbbb16a
ILT
6373 }
6374
3e4093b6 6375 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 6376 return rhs;
3e4093b6
RS
6377
6378 if (coder == VOID_TYPE)
400fbf9f 6379 {
6dcc04b0
JM
6380 /* Except for passing an argument to an unprototyped function,
6381 this is a constraint violation. When passing an argument to
6382 an unprototyped function, it is compile-time undefined;
6383 making it a constraint in that case was rejected in
6384 DR#252. */
c2255bc4 6385 error_at (location, "void value not ignored as it ought to be");
3e4093b6 6386 return error_mark_node;
400fbf9f 6387 }
4f2e1536 6388 rhs = require_complete_type (location, rhs);
808d6eaa
JM
6389 if (rhs == error_mark_node)
6390 return error_mark_node;
1807ffc1
MS
6391
6392 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6393 return error_mark_node;
6394
cd192ccc
MS
6395 /* A non-reference type can convert to a reference. This handles
6396 va_start, va_copy and possibly port built-ins. */
6397 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 6398 {
3e4093b6 6399 if (!lvalue_p (rhs))
400fbf9f 6400 {
c2255bc4 6401 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 6402 return error_mark_node;
400fbf9f 6403 }
3e4093b6
RS
6404 if (!c_mark_addressable (rhs))
6405 return error_mark_node;
6406 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 6407 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6408
81e5eca8 6409 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
6410 build_pointer_type (TREE_TYPE (type)),
6411 rhs, origtype, errtype,
6412 null_pointer_constant, fundecl, function,
6413 parmnum);
cd192ccc
MS
6414 if (rhs == error_mark_node)
6415 return error_mark_node;
3e4093b6
RS
6416
6417 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 6418 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6419 return rhs;
400fbf9f 6420 }
3e4093b6 6421 /* Some types can interconvert without explicit casts. */
3274deff 6422 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 6423 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
6424 return convert (type, rhs);
6425 /* Arithmetic types all interconvert, and enum is treated like int. */
6426 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 6427 || codel == FIXED_POINT_TYPE
3e4093b6
RS
6428 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6429 || codel == BOOLEAN_TYPE)
6430 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 6431 || coder == FIXED_POINT_TYPE
3e4093b6
RS
6432 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6433 || coder == BOOLEAN_TYPE))
928c19bb
JM
6434 {
6435 tree ret;
6436 bool save = in_late_binary_op;
e5341100
JJ
6437 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6438 || (coder == REAL_TYPE
6439 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
45b2222a 6440 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
928c19bb 6441 in_late_binary_op = true;
81e5eca8
MP
6442 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6443 ? expr_loc : location, type, orig_rhs);
e5341100 6444 in_late_binary_op = save;
928c19bb
JM
6445 return ret;
6446 }
400fbf9f 6447
79077aea
JJ
6448 /* Aggregates in different TUs might need conversion. */
6449 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6450 && codel == coder
6451 && comptypes (type, rhstype))
81e5eca8
MP
6452 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6453 ? expr_loc : location, type, rhs);
79077aea 6454
ebf0bf7f 6455 /* Conversion to a transparent union or record from its member types.
3e4093b6 6456 This applies only to function arguments. */
ebf0bf7f
JJ
6457 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6458 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 6459 && errtype == ic_argpass)
400fbf9f 6460 {
0257e383 6461 tree memb, marginal_memb = NULL_TREE;
3e4093b6 6462
910ad8de 6463 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 6464 {
0257e383 6465 tree memb_type = TREE_TYPE (memb);
400fbf9f 6466
3e4093b6 6467 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 6468 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 6469 break;
e58cd767 6470
3e4093b6
RS
6471 if (TREE_CODE (memb_type) != POINTER_TYPE)
6472 continue;
2f6e4e97 6473
3e4093b6
RS
6474 if (coder == POINTER_TYPE)
6475 {
6476 tree ttl = TREE_TYPE (memb_type);
6477 tree ttr = TREE_TYPE (rhstype);
400fbf9f 6478
3e4093b6
RS
6479 /* Any non-function converts to a [const][volatile] void *
6480 and vice versa; otherwise, targets must be the same.
6481 Meanwhile, the lhs target must have all the qualifiers of
6482 the rhs. */
267bac10
JM
6483 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6484 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6485 || comp_target_types (location, memb_type, rhstype))
3e4093b6 6486 {
267bac10
JM
6487 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6488 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 6489 /* If this type won't generate any warnings, use it. */
267bac10 6490 if (lquals == rquals
3e4093b6
RS
6491 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6492 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
6493 ? ((lquals | rquals) == rquals)
6494 : ((lquals | rquals) == lquals)))
3e4093b6 6495 break;
400fbf9f 6496
3e4093b6 6497 /* Keep looking for a better type, but remember this one. */
0257e383
RH
6498 if (!marginal_memb)
6499 marginal_memb = memb;
3e4093b6
RS
6500 }
6501 }
82bde854 6502
3e4093b6 6503 /* Can convert integer zero to any pointer type. */
928c19bb 6504 if (null_pointer_constant)
3e4093b6
RS
6505 {
6506 rhs = null_pointer_node;
6507 break;
6508 }
6509 }
400fbf9f 6510
0257e383 6511 if (memb || marginal_memb)
3e4093b6 6512 {
0257e383 6513 if (!memb)
3e4093b6
RS
6514 {
6515 /* We have only a marginally acceptable member type;
6516 it needs a warning. */
0257e383 6517 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 6518 tree ttr = TREE_TYPE (rhstype);
714a0864 6519
3e4093b6
RS
6520 /* Const and volatile mean something different for function
6521 types, so the usual warnings are not appropriate. */
6522 if (TREE_CODE (ttr) == FUNCTION_TYPE
6523 && TREE_CODE (ttl) == FUNCTION_TYPE)
6524 {
6525 /* Because const and volatile on functions are
6526 restrictions that say the function will not do
6527 certain things, it is okay to use a const or volatile
6528 function where an ordinary one is wanted, but not
6529 vice-versa. */
36c5e70a
BE
6530 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6531 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6532 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6533 OPT_Wdiscarded_qualifiers,
6534 G_("passing argument %d of %qE "
6535 "makes %q#v qualified function "
6536 "pointer from unqualified"),
6537 G_("assignment makes %q#v qualified "
6538 "function pointer from "
6539 "unqualified"),
6540 G_("initialization makes %q#v qualified "
6541 "function pointer from "
6542 "unqualified"),
6543 G_("return makes %q#v qualified function "
6544 "pointer from unqualified"),
6545 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6546 }
36c5e70a
BE
6547 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6548 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6549 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6550 OPT_Wdiscarded_qualifiers,
6551 G_("passing argument %d of %qE discards "
6552 "%qv qualifier from pointer target type"),
6553 G_("assignment discards %qv qualifier "
6554 "from pointer target type"),
6555 G_("initialization discards %qv qualifier "
6556 "from pointer target type"),
6557 G_("return discards %qv qualifier from "
6558 "pointer target type"),
6559 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6560
6561 memb = marginal_memb;
3e4093b6 6562 }
400fbf9f 6563
fcf73884 6564 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6565 pedwarn (location, OPT_Wpedantic,
fcf73884 6566 "ISO C prohibits argument conversion to union type");
0e7c47fa 6567
db3927fb 6568 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6569 return build_constructor_single (type, memb, rhs);
3e4093b6 6570 }
0e7c47fa
RK
6571 }
6572
3e4093b6
RS
6573 /* Conversions among pointers */
6574 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6575 && (coder == codel))
400fbf9f 6576 {
3e4093b6
RS
6577 tree ttl = TREE_TYPE (type);
6578 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6579 tree mvl = ttl;
6580 tree mvr = ttr;
3e4093b6 6581 bool is_opaque_pointer;
264fa2db 6582 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6583 addr_space_t asl;
6584 addr_space_t asr;
400fbf9f 6585
46df2823 6586 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6587 mvl = (TYPE_ATOMIC (mvl)
6588 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6589 TYPE_QUAL_ATOMIC)
6590 : TYPE_MAIN_VARIANT (mvl));
46df2823 6591 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6592 mvr = (TYPE_ATOMIC (mvr)
6593 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6594 TYPE_QUAL_ATOMIC)
6595 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6596 /* Opaque pointers are treated like void pointers. */
f83c7f63 6597 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6598
478a1c5b
ILT
6599 /* The Plan 9 compiler permits a pointer to a struct to be
6600 automatically converted into a pointer to an anonymous field
6601 within the struct. */
6602 if (flag_plan9_extensions
3e636daf
MP
6603 && RECORD_OR_UNION_TYPE_P (mvl)
6604 && RECORD_OR_UNION_TYPE_P (mvr)
478a1c5b
ILT
6605 && mvl != mvr)
6606 {
6607 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6608 if (new_rhs != NULL_TREE)
6609 {
6610 rhs = new_rhs;
6611 rhstype = TREE_TYPE (rhs);
6612 coder = TREE_CODE (rhstype);
6613 ttr = TREE_TYPE (rhstype);
6614 mvr = TYPE_MAIN_VARIANT (ttr);
6615 }
6616 }
6617
b7e20b53 6618 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6619 for the purpose of reducing the number of false positives, we
6620 tolerate the special case of
b7e20b53 6621
c22cacf3 6622 int *p = NULL;
b7e20b53 6623
c22cacf3 6624 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6625 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6626 warning_at (errtype == ic_argpass ? expr_loc : location,
6627 OPT_Wc___compat,
6628 "request for implicit conversion "
c2255bc4 6629 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6630
36c5e70a
BE
6631 /* See if the pointers point to incompatible address spaces. */
6632 asl = TYPE_ADDR_SPACE (ttl);
6633 asr = TYPE_ADDR_SPACE (ttr);
6634 if (!null_pointer_constant_p (rhs)
6635 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6636 {
6637 switch (errtype)
6638 {
6639 case ic_argpass:
8ffcdea8 6640 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6641 "non-enclosed address space", parmnum, rname);
6642 break;
6643 case ic_assign:
6644 error_at (location, "assignment from pointer to "
6645 "non-enclosed address space");
6646 break;
6647 case ic_init:
6648 error_at (location, "initialization from pointer to "
6649 "non-enclosed address space");
6650 break;
6651 case ic_return:
6652 error_at (location, "return from pointer to "
6653 "non-enclosed address space");
6654 break;
6655 default:
6656 gcc_unreachable ();
6657 }
6658 return error_mark_node;
6659 }
6660
7876a414
KG
6661 /* Check if the right-hand side has a format attribute but the
6662 left-hand side doesn't. */
90137d8f 6663 if (warn_suggest_attribute_format
104f8784 6664 && check_missing_format_attribute (type, rhstype))
c22cacf3 6665 {
104f8784
KG
6666 switch (errtype)
6667 {
6668 case ic_argpass:
8ffcdea8 6669 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6670 "argument %d of %qE might be "
6671 "a candidate for a format attribute",
6672 parmnum, rname);
104f8784
KG
6673 break;
6674 case ic_assign:
90137d8f 6675 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6676 "assignment left-hand side might be "
6677 "a candidate for a format attribute");
104f8784
KG
6678 break;
6679 case ic_init:
90137d8f 6680 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6681 "initialization left-hand side might be "
6682 "a candidate for a format attribute");
104f8784
KG
6683 break;
6684 case ic_return:
90137d8f 6685 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6686 "return type might be "
6687 "a candidate for a format attribute");
104f8784
KG
6688 break;
6689 default:
6690 gcc_unreachable ();
6691 }
7876a414 6692 }
c22cacf3 6693
3e4093b6
RS
6694 /* Any non-function converts to a [const][volatile] void *
6695 and vice versa; otherwise, targets must be the same.
6696 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6697 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6698 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6699 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6700 || is_opaque_pointer
f8a93a2e
JJ
6701 || ((c_common_unsigned_type (mvl)
6702 == c_common_unsigned_type (mvr))
267bac10
JM
6703 && (c_common_signed_type (mvl)
6704 == c_common_signed_type (mvr))
6705 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6706 {
768952be
MU
6707 /* Warn about loss of qualifers from pointers to arrays with
6708 qualifiers on the element type. */
6709 if (TREE_CODE (ttr) == ARRAY_TYPE)
6710 {
6711 ttr = strip_array_types (ttr);
6712 ttl = strip_array_types (ttl);
6713
6714 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6715 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6716 WARNING_FOR_QUALIFIERS (location, expr_loc,
6717 OPT_Wdiscarded_array_qualifiers,
6718 G_("passing argument %d of %qE discards "
6719 "%qv qualifier from pointer target type"),
6720 G_("assignment discards %qv qualifier "
6721 "from pointer target type"),
6722 G_("initialization discards %qv qualifier "
6723 "from pointer target type"),
6724 G_("return discards %qv qualifier from "
6725 "pointer target type"),
6726 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6727 }
6728 else if (pedantic
3e4093b6
RS
6729 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6730 ||
6731 (VOID_TYPE_P (ttr)
928c19bb 6732 && !null_pointer_constant
3e4093b6 6733 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6734 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6735 G_("ISO C forbids passing argument %d of "
6736 "%qE between function pointer "
6737 "and %<void *%>"),
6738 G_("ISO C forbids assignment between "
6739 "function pointer and %<void *%>"),
6740 G_("ISO C forbids initialization between "
6741 "function pointer and %<void *%>"),
6742 G_("ISO C forbids return between function "
6743 "pointer and %<void *%>"));
3e4093b6
RS
6744 /* Const and volatile mean something different for function types,
6745 so the usual warnings are not appropriate. */
6746 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6747 && TREE_CODE (ttl) != FUNCTION_TYPE)
6748 {
768952be
MU
6749 /* Don't warn about loss of qualifier for conversions from
6750 qualified void* to pointers to arrays with corresponding
6751 qualifier on the element type. */
6752 if (!pedantic)
6753 ttl = strip_array_types (ttl);
6754
267bac10
JM
6755 /* Assignments between atomic and non-atomic objects are OK. */
6756 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6757 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6758 {
768952be
MU
6759 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6760 OPT_Wdiscarded_qualifiers,
6761 G_("passing argument %d of %qE discards "
6762 "%qv qualifier from pointer target type"),
6763 G_("assignment discards %qv qualifier "
6764 "from pointer target type"),
6765 G_("initialization discards %qv qualifier "
6766 "from pointer target type"),
6767 G_("return discards %qv qualifier from "
6768 "pointer target type"),
6769 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6770 }
3e4093b6
RS
6771 /* If this is not a case of ignoring a mismatch in signedness,
6772 no warning. */
6773 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6774 || target_cmp)
3e4093b6
RS
6775 ;
6776 /* If there is a mismatch, do warn. */
f2fd3821 6777 else if (warn_pointer_sign)
296c53ac
MP
6778 switch (errtype)
6779 {
6780 case ic_argpass:
6781 if (pedwarn (expr_loc, OPT_Wpointer_sign,
6782 "pointer targets in passing argument %d of "
6783 "%qE differ in signedness", parmnum, rname))
6784 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6785 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6786 "expected %qT but argument is of type %qT",
6787 type, rhstype);
6788 break;
6789 case ic_assign:
6790 pedwarn (location, OPT_Wpointer_sign,
6791 "pointer targets in assignment from %qT to %qT "
6792 "differ in signedness", rhstype, type);
6793 break;
6794 case ic_init:
6795 pedwarn_init (location, OPT_Wpointer_sign,
6796 "pointer targets in initialization of %qT "
6797 "from %qT differ in signedness", type,
6798 rhstype);
6799 break;
6800 case ic_return:
6801 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
6802 "returning %qT from a function with return type "
6803 "%qT differ in signedness", rhstype, type);
6804 break;
6805 default:
6806 gcc_unreachable ();
6807 }
3e4093b6
RS
6808 }
6809 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6810 && TREE_CODE (ttr) == FUNCTION_TYPE)
6811 {
6812 /* Because const and volatile on functions are restrictions
6813 that say the function will not do certain things,
6814 it is okay to use a const or volatile function
6815 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6816 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6817 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6818 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6819 OPT_Wdiscarded_qualifiers,
6820 G_("passing argument %d of %qE makes "
6821 "%q#v qualified function pointer "
6822 "from unqualified"),
6823 G_("assignment makes %q#v qualified function "
6824 "pointer from unqualified"),
6825 G_("initialization makes %q#v qualified "
6826 "function pointer from unqualified"),
6827 G_("return makes %q#v qualified function "
6828 "pointer from unqualified"),
6829 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6830 }
6831 }
296c53ac
MP
6832 /* Avoid warning about the volatile ObjC EH puts on decls. */
6833 else if (!objc_ok)
6834 {
6835 switch (errtype)
6836 {
6837 case ic_argpass:
6838 if (pedwarn (expr_loc, OPT_Wincompatible_pointer_types,
6839 "passing argument %d of %qE from incompatible "
6840 "pointer type", parmnum, rname))
6841 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6842 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6843 "expected %qT but argument is of type %qT",
6844 type, rhstype);
6845 break;
6846 case ic_assign:
6847 pedwarn (location, OPT_Wincompatible_pointer_types,
6848 "assignment to %qT from incompatible pointer type %qT",
6849 type, rhstype);
6850 break;
6851 case ic_init:
6852 pedwarn_init (location, OPT_Wincompatible_pointer_types,
6853 "initialization of %qT from incompatible pointer "
6854 "type %qT", type, rhstype);
6855 break;
6856 case ic_return:
6857 pedwarn (location, OPT_Wincompatible_pointer_types,
6858 "returning %qT from a function with incompatible "
6859 "return type %qT", rhstype, type);
6860 break;
6861 default:
6862 gcc_unreachable ();
6863 }
6864 }
58393038 6865
3e4093b6
RS
6866 return convert (type, rhs);
6867 }
b494fd98
EB
6868 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6869 {
6dcc04b0
JM
6870 /* ??? This should not be an error when inlining calls to
6871 unprototyped functions. */
c2255bc4 6872 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6873 return error_mark_node;
6874 }
3e4093b6 6875 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6876 {
3e4093b6
RS
6877 /* An explicit constant 0 can convert to a pointer,
6878 or one that results from arithmetic, even including
6879 a cast to integer type. */
928c19bb 6880 if (!null_pointer_constant)
296c53ac
MP
6881 switch (errtype)
6882 {
6883 case ic_argpass:
6884 if (pedwarn (expr_loc, OPT_Wint_conversion,
6885 "passing argument %d of %qE makes pointer from "
6886 "integer without a cast", parmnum, rname))
6887 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6888 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6889 "expected %qT but argument is of type %qT",
6890 type, rhstype);
6891 break;
6892 case ic_assign:
6893 pedwarn (location, OPT_Wint_conversion,
6894 "assignment to %qT from %qT makes pointer from integer "
6895 "without a cast", type, rhstype);
6896 break;
6897 case ic_init:
6898 pedwarn_init (location, OPT_Wint_conversion,
6899 "initialization of %qT from %qT makes pointer from "
6900 "integer without a cast", type, rhstype);
6901 break;
6902 case ic_return:
6903 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6904 "function with return type %qT makes pointer from "
6905 "integer without a cast", rhstype, type);
6906 break;
6907 default:
6908 gcc_unreachable ();
6909 }
b3006337
EB
6910
6911 return convert (type, rhs);
400fbf9f 6912 }
3e4093b6 6913 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6914 {
296c53ac
MP
6915 switch (errtype)
6916 {
6917 case ic_argpass:
6918 if (pedwarn (expr_loc, OPT_Wint_conversion,
6919 "passing argument %d of %qE makes integer from "
6920 "pointer without a cast", parmnum, rname))
6921 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6922 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6923 "expected %qT but argument is of type %qT",
6924 type, rhstype);
6925 break;
6926 case ic_assign:
6927 pedwarn (location, OPT_Wint_conversion,
6928 "assignment to %qT from %qT makes integer from pointer "
6929 "without a cast", type, rhstype);
6930 break;
6931 case ic_init:
6932 pedwarn_init (location, OPT_Wint_conversion,
6933 "initialization of %qT from %qT makes integer from "
6934 "pointer without a cast", type, rhstype);
6935 break;
6936 case ic_return:
6937 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
6938 "function with return type %qT makes integer from "
6939 "pointer without a cast", rhstype, type);
6940 break;
6941 default:
6942 gcc_unreachable ();
6943 }
6944
3e4093b6
RS
6945 return convert (type, rhs);
6946 }
6947 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6948 {
6949 tree ret;
6950 bool save = in_late_binary_op;
6951 in_late_binary_op = true;
6952 ret = convert (type, rhs);
6953 in_late_binary_op = save;
6954 return ret;
6955 }
400fbf9f 6956
2ac2f164 6957 switch (errtype)
3e4093b6 6958 {
2ac2f164 6959 case ic_argpass:
8ffcdea8
MP
6960 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6961 rname);
a7da8b42 6962 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6963 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6964 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6965 break;
6966 case ic_assign:
c2255bc4
AH
6967 error_at (location, "incompatible types when assigning to type %qT from "
6968 "type %qT", type, rhstype);
2ac2f164
JM
6969 break;
6970 case ic_init:
c2255bc4 6971 error_at (location,
8ffcdea8 6972 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6973 type, rhstype);
2ac2f164
JM
6974 break;
6975 case ic_return:
c2255bc4 6976 error_at (location,
8ffcdea8 6977 "incompatible types when returning type %qT but %qT was "
c2255bc4 6978 "expected", rhstype, type);
2ac2f164
JM
6979 break;
6980 default:
6981 gcc_unreachable ();
400fbf9f 6982 }
53b01f59 6983
3e4093b6
RS
6984 return error_mark_node;
6985}
3e4093b6
RS
6986\f
6987/* If VALUE is a compound expr all of whose expressions are constant, then
6988 return its value. Otherwise, return error_mark_node.
15b732b2 6989
3e4093b6
RS
6990 This is for handling COMPOUND_EXPRs as initializer elements
6991 which is allowed with a warning when -pedantic is specified. */
15b732b2 6992
3e4093b6
RS
6993static tree
6994valid_compound_expr_initializer (tree value, tree endtype)
6995{
6996 if (TREE_CODE (value) == COMPOUND_EXPR)
6997 {
6998 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6999 == error_mark_node)
7000 return error_mark_node;
7001 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7002 endtype);
7003 }
116df786 7004 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
7005 return error_mark_node;
7006 else
7007 return value;
15b732b2 7008}
400fbf9f 7009\f
3e4093b6
RS
7010/* Perform appropriate conversions on the initial value of a variable,
7011 store it in the declaration DECL,
7012 and print any error messages that are appropriate.
bbbbb16a 7013 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
7014 If the init is invalid, store an ERROR_MARK.
7015
7016 INIT_LOC is the location of the initial value. */
400fbf9f 7017
3e4093b6 7018void
c2255bc4 7019store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 7020{
3e4093b6 7021 tree value, type;
928c19bb 7022 bool npc = false;
400fbf9f 7023
3e4093b6 7024 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 7025
3e4093b6
RS
7026 type = TREE_TYPE (decl);
7027 if (TREE_CODE (type) == ERROR_MARK)
7028 return;
400fbf9f 7029
3e4093b6 7030 /* Digest the specified initializer into an expression. */
400fbf9f 7031
928c19bb
JM
7032 if (init)
7033 npc = null_pointer_constant_p (init);
c2255bc4
AH
7034 value = digest_init (init_loc, type, init, origtype, npc,
7035 true, TREE_STATIC (decl));
400fbf9f 7036
3e4093b6 7037 /* Store the expression if valid; else report error. */
400fbf9f 7038
8400e75e 7039 if (!in_system_header_at (input_location)
3f75a254 7040 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
7041 warning (OPT_Wtraditional, "traditional C rejects automatic "
7042 "aggregate initialization");
2f6e4e97 7043
dea63e49
JJ
7044 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7045 DECL_INITIAL (decl) = value;
400fbf9f 7046
3e4093b6
RS
7047 /* ANSI wants warnings about out-of-range constant initializers. */
7048 STRIP_TYPE_NOPS (value);
b8698a0f 7049 if (TREE_STATIC (decl))
c2658540 7050 constant_expression_warning (value);
400fbf9f 7051
3e4093b6
RS
7052 /* Check if we need to set array size from compound literal size. */
7053 if (TREE_CODE (type) == ARRAY_TYPE
3fa8871b 7054 && TYPE_DOMAIN (type) == NULL_TREE
3e4093b6 7055 && value != error_mark_node)
400fbf9f 7056 {
3e4093b6
RS
7057 tree inside_init = init;
7058
ed248cf7 7059 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
7060 inside_init = fold (inside_init);
7061
7062 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7063 {
8d9f82d5 7064 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 7065
8d9f82d5 7066 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
7067 {
7068 /* For int foo[] = (int [3]){1}; we need to set array size
7069 now since later on array initializer will be just the
7070 brace enclosed list of the compound literal. */
e30ecc5d 7071 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 7072 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 7073 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 7074 layout_type (type);
8d9f82d5 7075 layout_decl (cldecl, 0);
e30ecc5d
JJ
7076 TREE_TYPE (decl)
7077 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
7078 }
7079 }
400fbf9f 7080 }
3e4093b6
RS
7081}
7082\f
7083/* Methods for storing and printing names for error messages. */
400fbf9f 7084
3e4093b6
RS
7085/* Implement a spelling stack that allows components of a name to be pushed
7086 and popped. Each element on the stack is this structure. */
400fbf9f 7087
3e4093b6
RS
7088struct spelling
7089{
7090 int kind;
7091 union
400fbf9f 7092 {
a0f0ab9f 7093 unsigned HOST_WIDE_INT i;
3e4093b6
RS
7094 const char *s;
7095 } u;
7096};
2f6e4e97 7097
3e4093b6
RS
7098#define SPELLING_STRING 1
7099#define SPELLING_MEMBER 2
7100#define SPELLING_BOUNDS 3
400fbf9f 7101
3e4093b6
RS
7102static struct spelling *spelling; /* Next stack element (unused). */
7103static struct spelling *spelling_base; /* Spelling stack base. */
7104static int spelling_size; /* Size of the spelling stack. */
400fbf9f 7105
3e4093b6
RS
7106/* Macros to save and restore the spelling stack around push_... functions.
7107 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 7108
3e4093b6
RS
7109#define SPELLING_DEPTH() (spelling - spelling_base)
7110#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 7111
3e4093b6
RS
7112/* Push an element on the spelling stack with type KIND and assign VALUE
7113 to MEMBER. */
400fbf9f 7114
3e4093b6
RS
7115#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7116{ \
7117 int depth = SPELLING_DEPTH (); \
7118 \
7119 if (depth >= spelling_size) \
7120 { \
7121 spelling_size += 10; \
cca8ead2
BI
7122 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7123 spelling_size); \
3e4093b6
RS
7124 RESTORE_SPELLING_DEPTH (depth); \
7125 } \
7126 \
7127 spelling->kind = (KIND); \
7128 spelling->MEMBER = (VALUE); \
7129 spelling++; \
7130}
400fbf9f 7131
3e4093b6 7132/* Push STRING on the stack. Printed literally. */
400fbf9f 7133
3e4093b6
RS
7134static void
7135push_string (const char *string)
7136{
7137 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7138}
400fbf9f 7139
3e4093b6 7140/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 7141
3e4093b6
RS
7142static void
7143push_member_name (tree decl)
7144{
7145 const char *const string
88388a52
JM
7146 = (DECL_NAME (decl)
7147 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7148 : _("<anonymous>"));
3e4093b6
RS
7149 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7150}
400fbf9f 7151
3e4093b6 7152/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 7153
3e4093b6 7154static void
a0f0ab9f 7155push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
7156{
7157 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7158}
bb58bec5 7159
3e4093b6 7160/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 7161
3e4093b6
RS
7162static int
7163spelling_length (void)
7164{
7165 int size = 0;
7166 struct spelling *p;
400fbf9f 7167
3e4093b6
RS
7168 for (p = spelling_base; p < spelling; p++)
7169 {
7170 if (p->kind == SPELLING_BOUNDS)
7171 size += 25;
7172 else
7173 size += strlen (p->u.s) + 1;
7174 }
7175
7176 return size;
400fbf9f 7177}
400fbf9f 7178
3e4093b6 7179/* Print the spelling to BUFFER and return it. */
400fbf9f 7180
3e4093b6
RS
7181static char *
7182print_spelling (char *buffer)
400fbf9f 7183{
3e4093b6
RS
7184 char *d = buffer;
7185 struct spelling *p;
400fbf9f 7186
3e4093b6
RS
7187 for (p = spelling_base; p < spelling; p++)
7188 if (p->kind == SPELLING_BOUNDS)
7189 {
a0f0ab9f 7190 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
7191 d += strlen (d);
7192 }
7193 else
7194 {
7195 const char *s;
7196 if (p->kind == SPELLING_MEMBER)
7197 *d++ = '.';
7198 for (s = p->u.s; (*d = *s++); d++)
7199 ;
7200 }
7201 *d++ = '\0';
7202 return buffer;
7203}
400fbf9f 7204
3e4093b6
RS
7205/* Digest the parser output INIT as an initializer for type TYPE.
7206 Return a C expression of type TYPE to represent the initial value.
7e842ef8 7207
bbbbb16a
ILT
7208 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7209
928c19bb
JM
7210 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7211
916c5919
JM
7212 If INIT is a string constant, STRICT_STRING is true if it is
7213 unparenthesized or we should not warn here for it being parenthesized.
7214 For other types of INIT, STRICT_STRING is not used.
7215
c2255bc4
AH
7216 INIT_LOC is the location of the INIT.
7217
3e4093b6
RS
7218 REQUIRE_CONSTANT requests an error if non-constant initializers or
7219 elements are seen. */
7e842ef8 7220
3e4093b6 7221static tree
c2255bc4
AH
7222digest_init (location_t init_loc, tree type, tree init, tree origtype,
7223 bool null_pointer_constant, bool strict_string,
7224 int require_constant)
3e4093b6
RS
7225{
7226 enum tree_code code = TREE_CODE (type);
7227 tree inside_init = init;
8ce94e44 7228 tree semantic_type = NULL_TREE;
928c19bb 7229 bool maybe_const = true;
7e842ef8 7230
3e4093b6 7231 if (type == error_mark_node
f01da1a5 7232 || !init
7a0ca710 7233 || error_operand_p (init))
3e4093b6 7234 return error_mark_node;
7e842ef8 7235
ed248cf7 7236 STRIP_TYPE_NOPS (inside_init);
7e842ef8 7237
8ce94e44
JM
7238 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7239 {
7240 semantic_type = TREE_TYPE (inside_init);
7241 inside_init = TREE_OPERAND (inside_init, 0);
7242 }
928c19bb
JM
7243 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7244 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 7245
3e4093b6
RS
7246 /* Initialization of an array of chars from a string constant
7247 optionally enclosed in braces. */
7e842ef8 7248
197463ae
JM
7249 if (code == ARRAY_TYPE && inside_init
7250 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 7251 {
267bac10
JM
7252 tree typ1
7253 = (TYPE_ATOMIC (TREE_TYPE (type))
7254 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7255 TYPE_QUAL_ATOMIC)
7256 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
7257 /* Note that an array could be both an array of character type
7258 and an array of wchar_t if wchar_t is signed char or unsigned
7259 char. */
7260 bool char_array = (typ1 == char_type_node
7261 || typ1 == signed_char_type_node
7262 || typ1 == unsigned_char_type_node);
7263 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
7264 bool char16_array = !!comptypes (typ1, char16_type_node);
7265 bool char32_array = !!comptypes (typ1, char32_type_node);
7266
7267 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 7268 {
916c5919 7269 struct c_expr expr;
c466b2cd 7270 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
7271 expr.value = inside_init;
7272 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 7273 expr.original_type = NULL;
d033409e 7274 maybe_warn_string_init (init_loc, type, expr);
916c5919 7275
a45e580b 7276 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 7277 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
7278 "initialization of a flexible array member");
7279
3e4093b6 7280 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7281 TYPE_MAIN_VARIANT (type)))
3e4093b6 7282 return inside_init;
7e842ef8 7283
c466b2cd 7284 if (char_array)
3e4093b6 7285 {
c466b2cd
KVH
7286 if (typ2 != char_type_node)
7287 {
ea58ef42
MP
7288 error_init (init_loc, "char-array initialized from wide "
7289 "string");
c466b2cd
KVH
7290 return error_mark_node;
7291 }
3e4093b6 7292 }
c466b2cd 7293 else
3e4093b6 7294 {
c466b2cd
KVH
7295 if (typ2 == char_type_node)
7296 {
ea58ef42
MP
7297 error_init (init_loc, "wide character array initialized "
7298 "from non-wide string");
c466b2cd
KVH
7299 return error_mark_node;
7300 }
7301 else if (!comptypes(typ1, typ2))
7302 {
ea58ef42
MP
7303 error_init (init_loc, "wide character array initialized "
7304 "from incompatible wide string");
c466b2cd
KVH
7305 return error_mark_node;
7306 }
7e842ef8 7307 }
2f6e4e97 7308
3e4093b6 7309 TREE_TYPE (inside_init) = type;
3fa8871b
MP
7310 if (TYPE_DOMAIN (type) != NULL_TREE
7311 && TYPE_SIZE (type) != NULL_TREE
5eb4df45
ILT
7312 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7313 {
7314 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7315
c466b2cd 7316 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
7317 because it's ok to ignore the terminating null char
7318 that is counted in the length of the constant. */
5eb4df45
ILT
7319 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7320 (len
7321 - (TYPE_PRECISION (typ1)
7322 / BITS_PER_UNIT))))
7323 pedwarn_init (init_loc, 0,
7324 ("initializer-string for array of chars "
7325 "is too long"));
7326 else if (warn_cxx_compat
7327 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7328 warning_at (init_loc, OPT_Wc___compat,
7329 ("initializer-string for array chars "
7330 "is too long for C++"));
7331 }
7e842ef8 7332
3e4093b6 7333 return inside_init;
7e842ef8 7334 }
197463ae
JM
7335 else if (INTEGRAL_TYPE_P (typ1))
7336 {
ea58ef42 7337 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
7338 "from string constant");
7339 return error_mark_node;
7340 }
7e842ef8
PE
7341 }
7342
3e4093b6
RS
7343 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7344 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7345 below and handle as a constructor. */
e89be13b 7346 if (code == VECTOR_TYPE
31521951 7347 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
00c8e9f6 7348 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
7349 && TREE_CONSTANT (inside_init))
7350 {
7351 if (TREE_CODE (inside_init) == VECTOR_CST
7352 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7353 TYPE_MAIN_VARIANT (type)))
7354 return inside_init;
7355
7356 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7357 {
4038c495
GB
7358 unsigned HOST_WIDE_INT ix;
7359 tree value;
7360 bool constant_p = true;
e89be13b
JJ
7361
7362 /* Iterate through elements and check if all constructor
7363 elements are *_CSTs. */
4038c495
GB
7364 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7365 if (!CONSTANT_CLASS_P (value))
7366 {
7367 constant_p = false;
7368 break;
7369 }
e89be13b 7370
4038c495
GB
7371 if (constant_p)
7372 return build_vector_from_ctor (type,
7373 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
7374 }
7375 }
6035d635 7376
ca085fd7
MLI
7377 if (warn_sequence_point)
7378 verify_sequence_points (inside_init);
7379
3e4093b6
RS
7380 /* Any type can be initialized
7381 from an expression of the same type, optionally with braces. */
400fbf9f 7382
3fa8871b 7383 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
3e4093b6 7384 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7385 TYPE_MAIN_VARIANT (type))
3e4093b6 7386 || (code == ARRAY_TYPE
132da1a5 7387 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7388 || (code == VECTOR_TYPE
132da1a5 7389 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7390 || (code == POINTER_TYPE
3897f229 7391 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 7392 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 7393 TREE_TYPE (type)))))
3e4093b6
RS
7394 {
7395 if (code == POINTER_TYPE)
b494fd98 7396 {
b494fd98
EB
7397 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7398 {
f2a71bbc
JM
7399 if (TREE_CODE (inside_init) == STRING_CST
7400 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
7401 inside_init = array_to_pointer_conversion
7402 (init_loc, inside_init);
f2a71bbc
JM
7403 else
7404 {
ea58ef42 7405 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
7406 return error_mark_node;
7407 }
b494fd98 7408 }
f2a71bbc 7409 }
b494fd98 7410
bae39a73
NS
7411 if (code == VECTOR_TYPE)
7412 /* Although the types are compatible, we may require a
7413 conversion. */
7414 inside_init = convert (type, inside_init);
3e4093b6 7415
ca58211b 7416 if (require_constant
3e4093b6 7417 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 7418 {
3e4093b6
RS
7419 /* As an extension, allow initializing objects with static storage
7420 duration with compound literals (which are then treated just as
ca58211b
PB
7421 the brace enclosed list they contain). Also allow this for
7422 vectors, as we can only assign them with compound literals. */
7278465e
MP
7423 if (flag_isoc99 && code != VECTOR_TYPE)
7424 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7425 "is not constant");
3e4093b6
RS
7426 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7427 inside_init = DECL_INITIAL (decl);
400fbf9f 7428 }
3e4093b6
RS
7429
7430 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7431 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 7432 {
ea58ef42
MP
7433 error_init (init_loc, "array initialized from non-constant array "
7434 "expression");
3e4093b6 7435 return error_mark_node;
400fbf9f 7436 }
400fbf9f 7437
c1771a20 7438 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
7439 -pedantic-errors is specified. In the later case, we always want
7440 an error. In the former case, we simply want a warning. */
7441 if (require_constant && pedantic
7442 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7443 {
7444 inside_init
7445 = valid_compound_expr_initializer (inside_init,
7446 TREE_TYPE (inside_init));
7447 if (inside_init == error_mark_node)
ea58ef42 7448 error_init (init_loc, "initializer element is not constant");
2f6e4e97 7449 else
c1771a20 7450 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 7451 "initializer element is not constant");
3e4093b6
RS
7452 if (flag_pedantic_errors)
7453 inside_init = error_mark_node;
7454 }
7455 else if (require_constant
116df786
RH
7456 && !initializer_constant_valid_p (inside_init,
7457 TREE_TYPE (inside_init)))
3e4093b6 7458 {
ea58ef42 7459 error_init (init_loc, "initializer element is not constant");
3e4093b6 7460 inside_init = error_mark_node;
8b40563c 7461 }
928c19bb 7462 else if (require_constant && !maybe_const)
3aa3c9fc 7463 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7464 "initializer element is not a constant expression");
f735a153 7465
90137d8f 7466 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 7467 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
7468 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7469 type, inside_init, origtype,
bbbbb16a 7470 ic_init, null_pointer_constant,
928c19bb 7471 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
7472 return inside_init;
7473 }
f735a153 7474
3e4093b6 7475 /* Handle scalar types, including conversions. */
400fbf9f 7476
ab22c1fa
CF
7477 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7478 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7479 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 7480 {
f2a71bbc
JM
7481 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7482 && (TREE_CODE (init) == STRING_CST
7483 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 7484 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
7485 if (semantic_type)
7486 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7487 inside_init);
3e4093b6 7488 inside_init
68fca595
MP
7489 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7490 inside_init, origtype, ic_init,
7491 null_pointer_constant, NULL_TREE, NULL_TREE,
7492 0);
2f6e4e97 7493
3274deff
JW
7494 /* Check to see if we have already given an error message. */
7495 if (inside_init == error_mark_node)
7496 ;
3f75a254 7497 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 7498 {
ea58ef42 7499 error_init (init_loc, "initializer element is not constant");
3e4093b6 7500 inside_init = error_mark_node;
400fbf9f 7501 }
3e4093b6 7502 else if (require_constant
116df786
RH
7503 && !initializer_constant_valid_p (inside_init,
7504 TREE_TYPE (inside_init)))
400fbf9f 7505 {
ea58ef42
MP
7506 error_init (init_loc, "initializer element is not computable at "
7507 "load time");
3e4093b6 7508 inside_init = error_mark_node;
400fbf9f 7509 }
928c19bb 7510 else if (require_constant && !maybe_const)
451b5e48 7511 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7512 "initializer element is not a constant expression");
3e4093b6
RS
7513
7514 return inside_init;
400fbf9f 7515 }
d9fc6069 7516
3e4093b6 7517 /* Come here only for records and arrays. */
d9fc6069 7518
3e4093b6 7519 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 7520 {
ea58ef42 7521 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 7522 return error_mark_node;
d9fc6069 7523 }
3e4093b6 7524
ea58ef42 7525 error_init (init_loc, "invalid initializer");
3e4093b6 7526 return error_mark_node;
d9fc6069 7527}
400fbf9f 7528\f
3e4093b6 7529/* Handle initializers that use braces. */
400fbf9f 7530
3e4093b6
RS
7531/* Type of object we are accumulating a constructor for.
7532 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7533static tree constructor_type;
400fbf9f 7534
3e4093b6
RS
7535/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7536 left to fill. */
7537static tree constructor_fields;
400fbf9f 7538
3e4093b6
RS
7539/* For an ARRAY_TYPE, this is the specified index
7540 at which to store the next element we get. */
7541static tree constructor_index;
400fbf9f 7542
3e4093b6
RS
7543/* For an ARRAY_TYPE, this is the maximum index. */
7544static tree constructor_max_index;
400fbf9f 7545
3e4093b6
RS
7546/* For a RECORD_TYPE, this is the first field not yet written out. */
7547static tree constructor_unfilled_fields;
400fbf9f 7548
3e4093b6
RS
7549/* For an ARRAY_TYPE, this is the index of the first element
7550 not yet written out. */
7551static tree constructor_unfilled_index;
895ea614 7552
3e4093b6
RS
7553/* In a RECORD_TYPE, the byte index of the next consecutive field.
7554 This is so we can generate gaps between fields, when appropriate. */
7555static tree constructor_bit_index;
10d5caec 7556
3e4093b6
RS
7557/* If we are saving up the elements rather than allocating them,
7558 this is the list of elements so far (in reverse order,
7559 most recent first). */
9771b263 7560static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 7561
3e4093b6
RS
7562/* 1 if constructor should be incrementally stored into a constructor chain,
7563 0 if all the elements should be kept in AVL tree. */
7564static int constructor_incremental;
ad47f1e5 7565
3e4093b6
RS
7566/* 1 if so far this constructor's elements are all compile-time constants. */
7567static int constructor_constant;
ad47f1e5 7568
3e4093b6
RS
7569/* 1 if so far this constructor's elements are all valid address constants. */
7570static int constructor_simple;
ad47f1e5 7571
928c19bb
JM
7572/* 1 if this constructor has an element that cannot be part of a
7573 constant expression. */
7574static int constructor_nonconst;
7575
3e4093b6
RS
7576/* 1 if this constructor is erroneous so far. */
7577static int constructor_erroneous;
d45cf215 7578
9bac5cbb
G
7579/* 1 if this constructor is the universal zero initializer { 0 }. */
7580static int constructor_zeroinit;
7581
3e4093b6
RS
7582/* Structure for managing pending initializer elements, organized as an
7583 AVL tree. */
d45cf215 7584
3e4093b6 7585struct init_node
d45cf215 7586{
3e4093b6
RS
7587 struct init_node *left, *right;
7588 struct init_node *parent;
7589 int balance;
7590 tree purpose;
7591 tree value;
bbbbb16a 7592 tree origtype;
d45cf215
RS
7593};
7594
3e4093b6
RS
7595/* Tree of pending elements at this constructor level.
7596 These are elements encountered out of order
7597 which belong at places we haven't reached yet in actually
7598 writing the output.
7599 Will never hold tree nodes across GC runs. */
7600static struct init_node *constructor_pending_elts;
d45cf215 7601
3e4093b6
RS
7602/* The SPELLING_DEPTH of this constructor. */
7603static int constructor_depth;
d45cf215 7604
3e4093b6
RS
7605/* DECL node for which an initializer is being read.
7606 0 means we are reading a constructor expression
7607 such as (struct foo) {...}. */
7608static tree constructor_decl;
d45cf215 7609
3e4093b6
RS
7610/* Nonzero if this is an initializer for a top-level decl. */
7611static int constructor_top_level;
d45cf215 7612
3e4093b6
RS
7613/* Nonzero if there were any member designators in this initializer. */
7614static int constructor_designated;
d45cf215 7615
3e4093b6
RS
7616/* Nesting depth of designator list. */
7617static int designator_depth;
d45cf215 7618
3e4093b6 7619/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7620static int designator_erroneous;
d45cf215 7621
3e4093b6
RS
7622\f
7623/* This stack has a level for each implicit or explicit level of
7624 structuring in the initializer, including the outermost one. It
7625 saves the values of most of the variables above. */
d45cf215 7626
3e4093b6
RS
7627struct constructor_range_stack;
7628
7629struct constructor_stack
d45cf215 7630{
3e4093b6
RS
7631 struct constructor_stack *next;
7632 tree type;
7633 tree fields;
7634 tree index;
7635 tree max_index;
7636 tree unfilled_index;
7637 tree unfilled_fields;
7638 tree bit_index;
9771b263 7639 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7640 struct init_node *pending_elts;
7641 int offset;
7642 int depth;
916c5919 7643 /* If value nonzero, this value should replace the entire
3e4093b6 7644 constructor at this level. */
916c5919 7645 struct c_expr replacement_value;
3e4093b6
RS
7646 struct constructor_range_stack *range_stack;
7647 char constant;
7648 char simple;
928c19bb 7649 char nonconst;
3e4093b6
RS
7650 char implicit;
7651 char erroneous;
7652 char outer;
7653 char incremental;
7654 char designated;
976d5a22 7655 int designator_depth;
3e4093b6 7656};
d45cf215 7657
802415d1 7658static struct constructor_stack *constructor_stack;
d45cf215 7659
3e4093b6
RS
7660/* This stack represents designators from some range designator up to
7661 the last designator in the list. */
d45cf215 7662
3e4093b6
RS
7663struct constructor_range_stack
7664{
7665 struct constructor_range_stack *next, *prev;
7666 struct constructor_stack *stack;
7667 tree range_start;
7668 tree index;
7669 tree range_end;
7670 tree fields;
7671};
d45cf215 7672
802415d1 7673static struct constructor_range_stack *constructor_range_stack;
d45cf215 7674
3e4093b6
RS
7675/* This stack records separate initializers that are nested.
7676 Nested initializers can't happen in ANSI C, but GNU C allows them
7677 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7678
3e4093b6 7679struct initializer_stack
d45cf215 7680{
3e4093b6
RS
7681 struct initializer_stack *next;
7682 tree decl;
3e4093b6
RS
7683 struct constructor_stack *constructor_stack;
7684 struct constructor_range_stack *constructor_range_stack;
9771b263 7685 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7686 struct spelling *spelling;
7687 struct spelling *spelling_base;
7688 int spelling_size;
7689 char top_level;
7690 char require_constant_value;
7691 char require_constant_elements;
5dd9a9d0 7692 rich_location *missing_brace_richloc;
3e4093b6 7693};
d45cf215 7694
802415d1 7695static struct initializer_stack *initializer_stack;
3e4093b6
RS
7696\f
7697/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7698
7699void
5dd9a9d0
DM
7700start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7701 rich_location *richloc)
400fbf9f 7702{
3e4093b6 7703 const char *locus;
cceb1885 7704 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7705
3e4093b6 7706 p->decl = constructor_decl;
3e4093b6
RS
7707 p->require_constant_value = require_constant_value;
7708 p->require_constant_elements = require_constant_elements;
7709 p->constructor_stack = constructor_stack;
7710 p->constructor_range_stack = constructor_range_stack;
7711 p->elements = constructor_elements;
7712 p->spelling = spelling;
7713 p->spelling_base = spelling_base;
7714 p->spelling_size = spelling_size;
7715 p->top_level = constructor_top_level;
7716 p->next = initializer_stack;
5dd9a9d0 7717 p->missing_brace_richloc = richloc;
3e4093b6 7718 initializer_stack = p;
400fbf9f 7719
3e4093b6 7720 constructor_decl = decl;
3e4093b6
RS
7721 constructor_designated = 0;
7722 constructor_top_level = top_level;
400fbf9f 7723
3fa8871b 7724 if (decl != NULL_TREE && decl != error_mark_node)
3e4093b6
RS
7725 {
7726 require_constant_value = TREE_STATIC (decl);
7727 require_constant_elements
7728 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7729 /* For a scalar, you can always use any value to initialize,
7730 even within braces. */
296a8c2f 7731 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7732 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7733 }
7734 else
7735 {
7736 require_constant_value = 0;
7737 require_constant_elements = 0;
88388a52 7738 locus = _("(anonymous)");
3e4093b6 7739 }
b71c7f8a 7740
3e4093b6
RS
7741 constructor_stack = 0;
7742 constructor_range_stack = 0;
b71c7f8a 7743
9bac5cbb 7744 found_missing_braces = 0;
3e4093b6
RS
7745
7746 spelling_base = 0;
7747 spelling_size = 0;
7748 RESTORE_SPELLING_DEPTH (0);
7749
7750 if (locus)
7751 push_string (locus);
7752}
7753
7754void
7755finish_init (void)
b71c7f8a 7756{
3e4093b6 7757 struct initializer_stack *p = initializer_stack;
b71c7f8a 7758
3e4093b6
RS
7759 /* Free the whole constructor stack of this initializer. */
7760 while (constructor_stack)
7761 {
7762 struct constructor_stack *q = constructor_stack;
7763 constructor_stack = q->next;
7764 free (q);
7765 }
7766
366de0ce 7767 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7768
7769 /* Pop back to the data of the outer initializer (if any). */
36579663 7770 free (spelling_base);
3aeb3655 7771
3e4093b6 7772 constructor_decl = p->decl;
3e4093b6
RS
7773 require_constant_value = p->require_constant_value;
7774 require_constant_elements = p->require_constant_elements;
7775 constructor_stack = p->constructor_stack;
7776 constructor_range_stack = p->constructor_range_stack;
7777 constructor_elements = p->elements;
7778 spelling = p->spelling;
7779 spelling_base = p->spelling_base;
7780 spelling_size = p->spelling_size;
7781 constructor_top_level = p->top_level;
7782 initializer_stack = p->next;
7783 free (p);
b71c7f8a 7784}
400fbf9f 7785\f
3e4093b6
RS
7786/* Call here when we see the initializer is surrounded by braces.
7787 This is instead of a call to push_init_level;
7788 it is matched by a call to pop_init_level.
400fbf9f 7789
3e4093b6
RS
7790 TYPE is the type to initialize, for a constructor expression.
7791 For an initializer for a decl, TYPE is zero. */
400fbf9f 7792
3e4093b6
RS
7793void
7794really_start_incremental_init (tree type)
400fbf9f 7795{
5d038c4c 7796 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7797
3fa8871b 7798 if (type == NULL_TREE)
3e4093b6 7799 type = TREE_TYPE (constructor_decl);
400fbf9f 7800
31521951 7801 if (VECTOR_TYPE_P (type)
b6fc2cdb 7802 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7803 error ("opaque vector types cannot be initialized");
400fbf9f 7804
3e4093b6
RS
7805 p->type = constructor_type;
7806 p->fields = constructor_fields;
7807 p->index = constructor_index;
7808 p->max_index = constructor_max_index;
7809 p->unfilled_index = constructor_unfilled_index;
7810 p->unfilled_fields = constructor_unfilled_fields;
7811 p->bit_index = constructor_bit_index;
7812 p->elements = constructor_elements;
7813 p->constant = constructor_constant;
7814 p->simple = constructor_simple;
928c19bb 7815 p->nonconst = constructor_nonconst;
3e4093b6
RS
7816 p->erroneous = constructor_erroneous;
7817 p->pending_elts = constructor_pending_elts;
7818 p->depth = constructor_depth;
916c5919
JM
7819 p->replacement_value.value = 0;
7820 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7821 p->replacement_value.original_type = NULL;
3e4093b6
RS
7822 p->implicit = 0;
7823 p->range_stack = 0;
7824 p->outer = 0;
7825 p->incremental = constructor_incremental;
7826 p->designated = constructor_designated;
976d5a22 7827 p->designator_depth = designator_depth;
3e4093b6
RS
7828 p->next = 0;
7829 constructor_stack = p;
b13aca19 7830
3e4093b6
RS
7831 constructor_constant = 1;
7832 constructor_simple = 1;
928c19bb 7833 constructor_nonconst = 0;
3e4093b6 7834 constructor_depth = SPELLING_DEPTH ();
9771b263 7835 constructor_elements = NULL;
3e4093b6
RS
7836 constructor_pending_elts = 0;
7837 constructor_type = type;
7838 constructor_incremental = 1;
7839 constructor_designated = 0;
9bac5cbb 7840 constructor_zeroinit = 1;
3e4093b6 7841 designator_depth = 0;
b06df647 7842 designator_erroneous = 0;
400fbf9f 7843
3e636daf 7844 if (RECORD_OR_UNION_TYPE_P (constructor_type))
400fbf9f 7845 {
3e4093b6
RS
7846 constructor_fields = TYPE_FIELDS (constructor_type);
7847 /* Skip any nameless bit fields at the beginning. */
3fa8871b
MP
7848 while (constructor_fields != NULL_TREE
7849 && DECL_C_BIT_FIELD (constructor_fields)
7850 && DECL_NAME (constructor_fields) == NULL_TREE)
910ad8de 7851 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7852
3e4093b6
RS
7853 constructor_unfilled_fields = constructor_fields;
7854 constructor_bit_index = bitsize_zero_node;
400fbf9f 7855 }
3e4093b6
RS
7856 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7857 {
7858 if (TYPE_DOMAIN (constructor_type))
7859 {
7860 constructor_max_index
7861 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7862
3e4093b6
RS
7863 /* Detect non-empty initializations of zero-length arrays. */
7864 if (constructor_max_index == NULL_TREE
7865 && TYPE_SIZE (constructor_type))
9a9d280e 7866 constructor_max_index = integer_minus_one_node;
400fbf9f 7867
3e4093b6
RS
7868 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7869 to initialize VLAs will cause a proper error; avoid tree
7870 checking errors as well by setting a safe value. */
7871 if (constructor_max_index
7872 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7873 constructor_max_index = integer_minus_one_node;
59c83dbf 7874
3e4093b6
RS
7875 constructor_index
7876 = convert (bitsizetype,
7877 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7878 }
3e4093b6 7879 else
493179da
JM
7880 {
7881 constructor_index = bitsize_zero_node;
7882 constructor_max_index = NULL_TREE;
7883 }
59c83dbf 7884
3e4093b6
RS
7885 constructor_unfilled_index = constructor_index;
7886 }
31521951 7887 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7888 {
7889 /* Vectors are like simple fixed-size arrays. */
7890 constructor_max_index =
c62c040f 7891 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7892 constructor_index = bitsize_zero_node;
3e4093b6
RS
7893 constructor_unfilled_index = constructor_index;
7894 }
7895 else
7896 {
7897 /* Handle the case of int x = {5}; */
7898 constructor_fields = constructor_type;
7899 constructor_unfilled_fields = constructor_type;
7900 }
7901}
7902\f
5dd9a9d0
DM
7903extern location_t last_init_list_comma;
7904
16595a1f
BS
7905/* Called when we see an open brace for a nested initializer. Finish
7906 off any pending levels with implicit braces. */
7907void
7908finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7909{
7910 while (constructor_stack->implicit)
7911 {
7912 if (RECORD_OR_UNION_TYPE_P (constructor_type)
3fa8871b 7913 && constructor_fields == NULL_TREE)
16595a1f 7914 process_init_element (input_location,
5dd9a9d0
DM
7915 pop_init_level (loc, 1, braced_init_obstack,
7916 last_init_list_comma),
16595a1f
BS
7917 true, braced_init_obstack);
7918 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7919 && constructor_max_index
7920 && tree_int_cst_lt (constructor_max_index,
7921 constructor_index))
7922 process_init_element (input_location,
5dd9a9d0
DM
7923 pop_init_level (loc, 1, braced_init_obstack,
7924 last_init_list_comma),
16595a1f
BS
7925 true, braced_init_obstack);
7926 else
7927 break;
7928 }
7929}
7930
3e4093b6
RS
7931/* Push down into a subobject, for initialization.
7932 If this is for an explicit set of braces, IMPLICIT is 0.
7933 If it is because the next element belongs at a lower level,
7934 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7935
3e4093b6 7936void
ea58ef42
MP
7937push_init_level (location_t loc, int implicit,
7938 struct obstack *braced_init_obstack)
3e4093b6
RS
7939{
7940 struct constructor_stack *p;
7941 tree value = NULL_TREE;
400fbf9f 7942
3e4093b6
RS
7943 /* Unless this is an explicit brace, we need to preserve previous
7944 content if any. */
7945 if (implicit)
7946 {
3e636daf 7947 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
a1e3b3d9 7948 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7949 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7950 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7951 }
7952
5d038c4c 7953 p = XNEW (struct constructor_stack);
3e4093b6
RS
7954 p->type = constructor_type;
7955 p->fields = constructor_fields;
7956 p->index = constructor_index;
7957 p->max_index = constructor_max_index;
7958 p->unfilled_index = constructor_unfilled_index;
7959 p->unfilled_fields = constructor_unfilled_fields;
7960 p->bit_index = constructor_bit_index;
7961 p->elements = constructor_elements;
7962 p->constant = constructor_constant;
7963 p->simple = constructor_simple;
928c19bb 7964 p->nonconst = constructor_nonconst;
3e4093b6
RS
7965 p->erroneous = constructor_erroneous;
7966 p->pending_elts = constructor_pending_elts;
7967 p->depth = constructor_depth;
3fa8871b 7968 p->replacement_value.value = NULL_TREE;
916c5919 7969 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7970 p->replacement_value.original_type = NULL;
3e4093b6
RS
7971 p->implicit = implicit;
7972 p->outer = 0;
7973 p->incremental = constructor_incremental;
7974 p->designated = constructor_designated;
976d5a22 7975 p->designator_depth = designator_depth;
3e4093b6
RS
7976 p->next = constructor_stack;
7977 p->range_stack = 0;
7978 constructor_stack = p;
400fbf9f 7979
3e4093b6
RS
7980 constructor_constant = 1;
7981 constructor_simple = 1;
928c19bb 7982 constructor_nonconst = 0;
3e4093b6 7983 constructor_depth = SPELLING_DEPTH ();
9771b263 7984 constructor_elements = NULL;
3e4093b6
RS
7985 constructor_incremental = 1;
7986 constructor_designated = 0;
7987 constructor_pending_elts = 0;
7988 if (!implicit)
400fbf9f 7989 {
3e4093b6
RS
7990 p->range_stack = constructor_range_stack;
7991 constructor_range_stack = 0;
7992 designator_depth = 0;
b06df647 7993 designator_erroneous = 0;
3e4093b6 7994 }
400fbf9f 7995
3e4093b6
RS
7996 /* Don't die if an entire brace-pair level is superfluous
7997 in the containing level. */
3fa8871b 7998 if (constructor_type == NULL_TREE)
3e4093b6 7999 ;
3e636daf 8000 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8001 {
8002 /* Don't die if there are extra init elts at the end. */
3fa8871b
MP
8003 if (constructor_fields == NULL_TREE)
8004 constructor_type = NULL_TREE;
3e4093b6 8005 else
400fbf9f 8006 {
3e4093b6
RS
8007 constructor_type = TREE_TYPE (constructor_fields);
8008 push_member_name (constructor_fields);
8009 constructor_depth++;
400fbf9f 8010 }
6a358dcb
MP
8011 /* If upper initializer is designated, then mark this as
8012 designated too to prevent bogus warnings. */
8013 constructor_designated = p->designated;
3e4093b6
RS
8014 }
8015 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8016 {
8017 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 8018 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 8019 constructor_depth++;
400fbf9f
JW
8020 }
8021
3fa8871b 8022 if (constructor_type == NULL_TREE)
400fbf9f 8023 {
ea58ef42 8024 error_init (loc, "extra brace group at end of initializer");
3fa8871b
MP
8025 constructor_fields = NULL_TREE;
8026 constructor_unfilled_fields = NULL_TREE;
3e4093b6 8027 return;
400fbf9f
JW
8028 }
8029
3e4093b6
RS
8030 if (value && TREE_CODE (value) == CONSTRUCTOR)
8031 {
8032 constructor_constant = TREE_CONSTANT (value);
8033 constructor_simple = TREE_STATIC (value);
928c19bb 8034 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 8035 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 8036 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
8037 && (TREE_CODE (constructor_type) == RECORD_TYPE
8038 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 8039 set_nonincremental_init (braced_init_obstack);
3e4093b6 8040 }
400fbf9f 8041
9bac5cbb 8042 if (implicit == 1)
5dd9a9d0
DM
8043 {
8044 found_missing_braces = 1;
8045 if (initializer_stack->missing_brace_richloc)
8046 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8047 (loc, "{");
8048 }
400fbf9f 8049
3e636daf 8050 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8051 {
8052 constructor_fields = TYPE_FIELDS (constructor_type);
8053 /* Skip any nameless bit fields at the beginning. */
3fa8871b
MP
8054 while (constructor_fields != NULL_TREE
8055 && DECL_C_BIT_FIELD (constructor_fields)
8056 && DECL_NAME (constructor_fields) == NULL_TREE)
f7587ed0 8057 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 8058
3e4093b6
RS
8059 constructor_unfilled_fields = constructor_fields;
8060 constructor_bit_index = bitsize_zero_node;
8061 }
31521951 8062 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
8063 {
8064 /* Vectors are like simple fixed-size arrays. */
8065 constructor_max_index =
c62c040f
RG
8066 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8067 constructor_index = bitsize_int (0);
3e4093b6
RS
8068 constructor_unfilled_index = constructor_index;
8069 }
8070 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8071 {
8072 if (TYPE_DOMAIN (constructor_type))
8073 {
8074 constructor_max_index
8075 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 8076
3e4093b6
RS
8077 /* Detect non-empty initializations of zero-length arrays. */
8078 if (constructor_max_index == NULL_TREE
8079 && TYPE_SIZE (constructor_type))
9a9d280e 8080 constructor_max_index = integer_minus_one_node;
de520661 8081
3e4093b6
RS
8082 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8083 to initialize VLAs will cause a proper error; avoid tree
8084 checking errors as well by setting a safe value. */
8085 if (constructor_max_index
8086 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 8087 constructor_max_index = integer_minus_one_node;
b62acd60 8088
3e4093b6
RS
8089 constructor_index
8090 = convert (bitsizetype,
8091 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8092 }
8093 else
8094 constructor_index = bitsize_zero_node;
de520661 8095
3e4093b6
RS
8096 constructor_unfilled_index = constructor_index;
8097 if (value && TREE_CODE (value) == STRING_CST)
8098 {
8099 /* We need to split the char/wchar array into individual
8100 characters, so that we don't have to special case it
8101 everywhere. */
a1e3b3d9 8102 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
8103 }
8104 }
8105 else
8106 {
b4519d39 8107 if (constructor_type != error_mark_node)
96b40f8d 8108 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
8109 constructor_fields = constructor_type;
8110 constructor_unfilled_fields = constructor_type;
8111 }
8112}
8b6a5902 8113
3e4093b6 8114/* At the end of an implicit or explicit brace level,
916c5919
JM
8115 finish up that level of constructor. If a single expression
8116 with redundant braces initialized that level, return the
8117 c_expr structure for that expression. Otherwise, the original_code
8118 element is set to ERROR_MARK.
8119 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 8120 from inner levels (process_init_element ignores that),
916c5919 8121 but return error_mark_node as the value from the outermost level
3e4093b6 8122 (that's what we want to put in DECL_INITIAL).
916c5919 8123 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 8124
916c5919 8125struct c_expr
ea58ef42 8126pop_init_level (location_t loc, int implicit,
5dd9a9d0
DM
8127 struct obstack *braced_init_obstack,
8128 location_t insert_before)
3e4093b6
RS
8129{
8130 struct constructor_stack *p;
916c5919 8131 struct c_expr ret;
3fa8871b 8132 ret.value = NULL_TREE;
916c5919 8133 ret.original_code = ERROR_MARK;
6866c6e8 8134 ret.original_type = NULL;
de520661 8135
3e4093b6
RS
8136 if (implicit == 0)
8137 {
8138 /* When we come to an explicit close brace,
8139 pop any inner levels that didn't have explicit braces. */
8140 while (constructor_stack->implicit)
34cf811f 8141 process_init_element (input_location,
5dd9a9d0
DM
8142 pop_init_level (loc, 1, braced_init_obstack,
8143 insert_before),
34cf811f 8144 true, braced_init_obstack);
366de0ce 8145 gcc_assert (!constructor_range_stack);
3e4093b6 8146 }
5dd9a9d0
DM
8147 else
8148 if (initializer_stack->missing_brace_richloc)
8149 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8150 (insert_before, "}");
e5e809f4 8151
0066ef9c
RH
8152 /* Now output all pending elements. */
8153 constructor_incremental = 1;
a1e3b3d9 8154 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 8155
3e4093b6 8156 p = constructor_stack;
e5e809f4 8157
3e4093b6
RS
8158 /* Error for initializing a flexible array member, or a zero-length
8159 array member in an inappropriate context. */
8160 if (constructor_type && constructor_fields
8161 && TREE_CODE (constructor_type) == ARRAY_TYPE
8162 && TYPE_DOMAIN (constructor_type)
3f75a254 8163 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
8164 {
8165 /* Silently discard empty initializations. The parser will
8166 already have pedwarned for empty brackets. */
8167 if (integer_zerop (constructor_unfilled_index))
8168 constructor_type = NULL_TREE;
366de0ce 8169 else
3e4093b6 8170 {
366de0ce 8171 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 8172
3e4093b6 8173 if (constructor_depth > 2)
ea58ef42 8174 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 8175 else
d033409e 8176 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 8177 "initialization of a flexible array member");
de520661 8178
3e4093b6
RS
8179 /* We have already issued an error message for the existence
8180 of a flexible array member not at the end of the structure.
535a42b1 8181 Discard the initializer so that we do not die later. */
910ad8de 8182 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
8183 constructor_type = NULL_TREE;
8184 }
3e4093b6 8185 }
de520661 8186
7b33f0c8
MP
8187 switch (vec_safe_length (constructor_elements))
8188 {
8189 case 0:
8190 /* Initialization with { } counts as zeroinit. */
8191 constructor_zeroinit = 1;
8192 break;
8193 case 1:
8194 /* This might be zeroinit as well. */
8195 if (integer_zerop ((*constructor_elements)[0].value))
8196 constructor_zeroinit = 1;
8197 break;
8198 default:
8199 /* If the constructor has more than one element, it can't be { 0 }. */
8200 constructor_zeroinit = 0;
8201 break;
8202 }
9bac5cbb
G
8203
8204 /* Warn when some structs are initialized with direct aggregation. */
8205 if (!implicit && found_missing_braces && warn_missing_braces
8206 && !constructor_zeroinit)
5dd9a9d0
DM
8207 {
8208 gcc_assert (initializer_stack->missing_brace_richloc);
8209 warning_at_rich_loc (initializer_stack->missing_brace_richloc,
8210 OPT_Wmissing_braces,
8211 "missing braces around initializer");
8212 }
9bac5cbb 8213
3e4093b6 8214 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 8215 if (warn_missing_field_initializers
3e4093b6
RS
8216 && constructor_type
8217 && TREE_CODE (constructor_type) == RECORD_TYPE
8218 && constructor_unfilled_fields)
8219 {
8220 /* Do not warn for flexible array members or zero-length arrays. */
8221 while (constructor_unfilled_fields
3f75a254 8222 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 8223 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 8224 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 8225
49819fef
AM
8226 if (constructor_unfilled_fields
8227 /* Do not warn if this level of the initializer uses member
8228 designators; it is likely to be deliberate. */
8229 && !constructor_designated
84937de2 8230 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 8231 && !constructor_zeroinit)
3e4093b6 8232 {
32397f22
MLI
8233 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8234 "missing initializer for field %qD of %qT",
8235 constructor_unfilled_fields,
8236 constructor_type))
8237 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 8238 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
8239 }
8240 }
de520661 8241
3e4093b6 8242 /* Pad out the end of the structure. */
916c5919 8243 if (p->replacement_value.value)
3e4093b6
RS
8244 /* If this closes a superfluous brace pair,
8245 just pass out the element between them. */
916c5919 8246 ret = p->replacement_value;
3fa8871b 8247 else if (constructor_type == NULL_TREE)
3e4093b6 8248 ;
3e636daf 8249 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 8250 && TREE_CODE (constructor_type) != ARRAY_TYPE
31521951 8251 && !VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
8252 {
8253 /* A nonincremental scalar initializer--just return
8254 the element, after verifying there is just one. */
9771b263 8255 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
8256 {
8257 if (!constructor_erroneous)
ea58ef42 8258 error_init (loc, "empty scalar initializer");
916c5919 8259 ret.value = error_mark_node;
3e4093b6 8260 }
9771b263 8261 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 8262 {
ea58ef42 8263 error_init (loc, "extra elements in scalar initializer");
9771b263 8264 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
8265 }
8266 else
9771b263 8267 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
8268 }
8269 else
8270 {
8271 if (constructor_erroneous)
916c5919 8272 ret.value = error_mark_node;
3e4093b6
RS
8273 else
8274 {
916c5919 8275 ret.value = build_constructor (constructor_type,
4038c495 8276 constructor_elements);
3e4093b6 8277 if (constructor_constant)
51eed280 8278 TREE_CONSTANT (ret.value) = 1;
3e4093b6 8279 if (constructor_constant && constructor_simple)
916c5919 8280 TREE_STATIC (ret.value) = 1;
928c19bb
JM
8281 if (constructor_nonconst)
8282 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
8283 }
8284 }
de520661 8285
928c19bb
JM
8286 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8287 {
8288 if (constructor_nonconst)
8289 ret.original_code = C_MAYBE_CONST_EXPR;
8290 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8291 ret.original_code = ERROR_MARK;
8292 }
8293
3e4093b6
RS
8294 constructor_type = p->type;
8295 constructor_fields = p->fields;
8296 constructor_index = p->index;
8297 constructor_max_index = p->max_index;
8298 constructor_unfilled_index = p->unfilled_index;
8299 constructor_unfilled_fields = p->unfilled_fields;
8300 constructor_bit_index = p->bit_index;
8301 constructor_elements = p->elements;
8302 constructor_constant = p->constant;
8303 constructor_simple = p->simple;
928c19bb 8304 constructor_nonconst = p->nonconst;
3e4093b6
RS
8305 constructor_erroneous = p->erroneous;
8306 constructor_incremental = p->incremental;
8307 constructor_designated = p->designated;
976d5a22 8308 designator_depth = p->designator_depth;
3e4093b6
RS
8309 constructor_pending_elts = p->pending_elts;
8310 constructor_depth = p->depth;
8311 if (!p->implicit)
8312 constructor_range_stack = p->range_stack;
8313 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 8314
3e4093b6
RS
8315 constructor_stack = p->next;
8316 free (p);
b621a4dd 8317
3fa8871b 8318 if (ret.value == NULL_TREE && constructor_stack == 0)
5d5e98dc 8319 ret.value = error_mark_node;
916c5919 8320 return ret;
3e4093b6 8321}
8b6a5902 8322
3e4093b6 8323/* Common handling for both array range and field name designators.
e3455240 8324 ARRAY argument is nonzero for array ranges. Returns false for success. */
400fbf9f 8325
e3455240 8326static bool
30af3a2b 8327set_designator (location_t loc, bool array,
ea58ef42 8328 struct obstack *braced_init_obstack)
de520661 8329{
3e4093b6
RS
8330 tree subtype;
8331 enum tree_code subcode;
de520661 8332
3e4093b6
RS
8333 /* Don't die if an entire brace-pair level is superfluous
8334 in the containing level. */
3fa8871b 8335 if (constructor_type == NULL_TREE)
e3455240 8336 return true;
de520661 8337
366de0ce
NS
8338 /* If there were errors in this designator list already, bail out
8339 silently. */
b06df647 8340 if (designator_erroneous)
e3455240 8341 return true;
e28cae4f 8342
3e4093b6
RS
8343 if (!designator_depth)
8344 {
366de0ce 8345 gcc_assert (!constructor_range_stack);
de520661 8346
3e4093b6
RS
8347 /* Designator list starts at the level of closest explicit
8348 braces. */
8349 while (constructor_stack->implicit)
34cf811f 8350 process_init_element (input_location,
5dd9a9d0
DM
8351 pop_init_level (loc, 1, braced_init_obstack,
8352 last_init_list_comma),
34cf811f 8353 true, braced_init_obstack);
3e4093b6 8354 constructor_designated = 1;
e3455240 8355 return false;
3e4093b6 8356 }
de520661 8357
366de0ce 8358 switch (TREE_CODE (constructor_type))
3c3fa147 8359 {
366de0ce
NS
8360 case RECORD_TYPE:
8361 case UNION_TYPE:
3e4093b6
RS
8362 subtype = TREE_TYPE (constructor_fields);
8363 if (subtype != error_mark_node)
8364 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
8365 break;
8366 case ARRAY_TYPE:
3e4093b6 8367 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
8368 break;
8369 default:
8370 gcc_unreachable ();
de520661 8371 }
400fbf9f 8372
3e4093b6
RS
8373 subcode = TREE_CODE (subtype);
8374 if (array && subcode != ARRAY_TYPE)
8375 {
ea58ef42 8376 error_init (loc, "array index in non-array initializer");
e3455240 8377 return true;
3e4093b6
RS
8378 }
8379 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8380 {
ea58ef42 8381 error_init (loc, "field name not in record or union initializer");
e3455240 8382 return true;
3e4093b6 8383 }
d45cf215 8384
3e4093b6 8385 constructor_designated = 1;
16595a1f 8386 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 8387 push_init_level (loc, 2, braced_init_obstack);
e3455240 8388 return false;
de520661 8389}
400fbf9f 8390
3e4093b6
RS
8391/* If there are range designators in designator list, push a new designator
8392 to constructor_range_stack. RANGE_END is end of such stack range or
8393 NULL_TREE if there is no range designator at this level. */
400fbf9f 8394
3e4093b6 8395static void
a1e3b3d9 8396push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
8397{
8398 struct constructor_range_stack *p;
400fbf9f 8399
a1e3b3d9
LB
8400 p = (struct constructor_range_stack *)
8401 obstack_alloc (braced_init_obstack,
8402 sizeof (struct constructor_range_stack));
3e4093b6
RS
8403 p->prev = constructor_range_stack;
8404 p->next = 0;
8405 p->fields = constructor_fields;
8406 p->range_start = constructor_index;
8407 p->index = constructor_index;
8408 p->stack = constructor_stack;
8409 p->range_end = range_end;
8b6a5902 8410 if (constructor_range_stack)
3e4093b6
RS
8411 constructor_range_stack->next = p;
8412 constructor_range_stack = p;
de520661 8413}
400fbf9f 8414
3e4093b6
RS
8415/* Within an array initializer, specify the next index to be initialized.
8416 FIRST is that index. If LAST is nonzero, then initialize a range
8417 of indices, running from FIRST through LAST. */
5a7ec9d9 8418
de520661 8419void
ea58ef42 8420set_init_index (location_t loc, tree first, tree last,
d033409e 8421 struct obstack *braced_init_obstack)
de520661 8422{
30af3a2b 8423 if (set_designator (loc, true, braced_init_obstack))
3e4093b6 8424 return;
de520661 8425
b06df647 8426 designator_erroneous = 1;
de520661 8427
3ea8cd06
JM
8428 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8429 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8430 {
ea58ef42 8431 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
8432 return;
8433 }
8434
2b6da65c
JM
8435 if (TREE_CODE (first) != INTEGER_CST)
8436 {
8437 first = c_fully_fold (first, false, NULL);
8438 if (TREE_CODE (first) == INTEGER_CST)
d033409e 8439 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8440 "array index in initializer is not "
8441 "an integer constant expression");
8442 }
8443
8444 if (last && TREE_CODE (last) != INTEGER_CST)
8445 {
8446 last = c_fully_fold (last, false, NULL);
8447 if (TREE_CODE (last) == INTEGER_CST)
d033409e 8448 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8449 "array index in initializer is not "
8450 "an integer constant expression");
8451 }
8452
3e4093b6 8453 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 8454 error_init (loc, "nonconstant array index in initializer");
3fa8871b 8455 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
ea58ef42 8456 error_init (loc, "nonconstant array index in initializer");
3e4093b6 8457 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 8458 error_init (loc, "array index in non-array initializer");
622adc7e 8459 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 8460 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
8461 else if (constructor_max_index
8462 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 8463 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 8464 else
de520661 8465 {
928c19bb
JM
8466 constant_expression_warning (first);
8467 if (last)
8468 constant_expression_warning (last);
3e4093b6 8469 constructor_index = convert (bitsizetype, first);
40d3d530
JR
8470 if (tree_int_cst_lt (constructor_index, first))
8471 {
8472 constructor_index = copy_node (constructor_index);
8473 TREE_OVERFLOW (constructor_index) = 1;
8474 }
665f2503 8475
3e4093b6 8476 if (last)
2bede729 8477 {
3e4093b6 8478 if (tree_int_cst_equal (first, last))
3fa8871b 8479 last = NULL_TREE;
3e4093b6
RS
8480 else if (tree_int_cst_lt (last, first))
8481 {
ea58ef42 8482 error_init (loc, "empty index range in initializer");
3fa8871b 8483 last = NULL_TREE;
3e4093b6
RS
8484 }
8485 else
8486 {
8487 last = convert (bitsizetype, last);
3fa8871b 8488 if (constructor_max_index != NULL_TREE
3e4093b6
RS
8489 && tree_int_cst_lt (constructor_max_index, last))
8490 {
ea58ef42
MP
8491 error_init (loc, "array index range in initializer exceeds "
8492 "array bounds");
3fa8871b 8493 last = NULL_TREE;
3e4093b6
RS
8494 }
8495 }
2bede729 8496 }
fed3cef0 8497
3e4093b6 8498 designator_depth++;
b06df647 8499 designator_erroneous = 0;
3e4093b6 8500 if (constructor_range_stack || last)
a1e3b3d9 8501 push_range_stack (last, braced_init_obstack);
de520661 8502 }
de520661 8503}
3e4093b6
RS
8504
8505/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 8506
de520661 8507void
f7e4f2e3 8508set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
ea58ef42 8509 struct obstack *braced_init_obstack)
de520661 8510{
0fb96aa4 8511 tree field;
94ba5069 8512
30af3a2b 8513 if (set_designator (loc, false, braced_init_obstack))
3e4093b6
RS
8514 return;
8515
b06df647 8516 designator_erroneous = 1;
3e4093b6 8517
3e636daf 8518 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
94ba5069 8519 {
ea58ef42 8520 error_init (loc, "field name not in record or union initializer");
3e4093b6 8521 return;
94ba5069
RS
8522 }
8523
0fb96aa4 8524 field = lookup_field (constructor_type, fieldname);
8b6a5902 8525
3fa8871b 8526 if (field == NULL_TREE)
f7e4f2e3
DM
8527 {
8528 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8529 if (guessed_id)
8530 {
264757fb
DM
8531 gcc_rich_location rich_loc (fieldname_loc);
8532 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
f7e4f2e3
DM
8533 error_at_rich_loc
8534 (&rich_loc,
8535 "%qT has no member named %qE; did you mean %qE?",
8536 constructor_type, fieldname, guessed_id);
8537 }
8538 else
8539 error_at (fieldname_loc, "%qT has no member named %qE",
8540 constructor_type, fieldname);
8541 }
3e4093b6 8542 else
0fb96aa4
JM
8543 do
8544 {
8545 constructor_fields = TREE_VALUE (field);
8546 designator_depth++;
8547 designator_erroneous = 0;
8548 if (constructor_range_stack)
8549 push_range_stack (NULL_TREE, braced_init_obstack);
8550 field = TREE_CHAIN (field);
8551 if (field)
8552 {
30af3a2b 8553 if (set_designator (loc, false, braced_init_obstack))
0fb96aa4
JM
8554 return;
8555 }
8556 }
8557 while (field != NULL_TREE);
3e4093b6
RS
8558}
8559\f
8560/* Add a new initializer to the tree of pending initializers. PURPOSE
8561 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
8562 VALUE is the value of that index or field. If ORIGTYPE is not
8563 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
8564
8565 IMPLICIT is true if value comes from pop_init_level (1),
8566 the new initializer has been merged with the existing one
8567 and thus no warnings should be emitted about overriding an
8568 existing initializer. */
de520661 8569
3e4093b6 8570static void
96b40f8d
MP
8571add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8572 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
8573{
8574 struct init_node *p, **q, *r;
8575
8576 q = &constructor_pending_elts;
8577 p = 0;
8578
8579 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8580 {
3e4093b6 8581 while (*q != 0)
91fa3c30 8582 {
3e4093b6
RS
8583 p = *q;
8584 if (tree_int_cst_lt (purpose, p->purpose))
8585 q = &p->left;
8586 else if (tree_int_cst_lt (p->purpose, purpose))
8587 q = &p->right;
8588 else
8589 {
b295aee2
JJ
8590 if (!implicit)
8591 {
8592 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8593 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8594 "initialized field with side-effects "
8595 "overwritten");
b295aee2 8596 else if (warn_override_init)
96b40f8d
MP
8597 warning_init (loc, OPT_Woverride_init,
8598 "initialized field overwritten");
b295aee2 8599 }
3e4093b6 8600 p->value = value;
bbbbb16a 8601 p->origtype = origtype;
3e4093b6
RS
8602 return;
8603 }
91fa3c30 8604 }
de520661 8605 }
3e4093b6 8606 else
de520661 8607 {
3e4093b6 8608 tree bitpos;
400fbf9f 8609
3e4093b6
RS
8610 bitpos = bit_position (purpose);
8611 while (*q != NULL)
8612 {
8613 p = *q;
8614 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8615 q = &p->left;
8616 else if (p->purpose != purpose)
8617 q = &p->right;
8618 else
8619 {
b295aee2
JJ
8620 if (!implicit)
8621 {
8622 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8623 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8624 "initialized field with side-effects "
8625 "overwritten");
b295aee2 8626 else if (warn_override_init)
96b40f8d
MP
8627 warning_init (loc, OPT_Woverride_init,
8628 "initialized field overwritten");
b295aee2 8629 }
3e4093b6 8630 p->value = value;
bbbbb16a 8631 p->origtype = origtype;
3e4093b6
RS
8632 return;
8633 }
8634 }
91fa3c30 8635 }
b71c7f8a 8636
a1e3b3d9
LB
8637 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8638 sizeof (struct init_node));
3e4093b6
RS
8639 r->purpose = purpose;
8640 r->value = value;
bbbbb16a 8641 r->origtype = origtype;
8b6a5902 8642
3e4093b6
RS
8643 *q = r;
8644 r->parent = p;
8645 r->left = 0;
8646 r->right = 0;
8647 r->balance = 0;
b71c7f8a 8648
3e4093b6 8649 while (p)
de520661 8650 {
3e4093b6 8651 struct init_node *s;
665f2503 8652
3e4093b6 8653 if (r == p->left)
2bede729 8654 {
3e4093b6
RS
8655 if (p->balance == 0)
8656 p->balance = -1;
8657 else if (p->balance < 0)
8658 {
8659 if (r->balance < 0)
8660 {
8661 /* L rotation. */
8662 p->left = r->right;
8663 if (p->left)
8664 p->left->parent = p;
8665 r->right = p;
e7b6a0ee 8666
3e4093b6
RS
8667 p->balance = 0;
8668 r->balance = 0;
39bc99c2 8669
3e4093b6
RS
8670 s = p->parent;
8671 p->parent = r;
8672 r->parent = s;
8673 if (s)
8674 {
8675 if (s->left == p)
8676 s->left = r;
8677 else
8678 s->right = r;
8679 }
8680 else
8681 constructor_pending_elts = r;
8682 }
8683 else
8684 {
8685 /* LR rotation. */
8686 struct init_node *t = r->right;
e7b6a0ee 8687
3e4093b6
RS
8688 r->right = t->left;
8689 if (r->right)
8690 r->right->parent = r;
8691 t->left = r;
8692
8693 p->left = t->right;
8694 if (p->left)
8695 p->left->parent = p;
8696 t->right = p;
8697
8698 p->balance = t->balance < 0;
8699 r->balance = -(t->balance > 0);
8700 t->balance = 0;
8701
8702 s = p->parent;
8703 p->parent = t;
8704 r->parent = t;
8705 t->parent = s;
8706 if (s)
8707 {
8708 if (s->left == p)
8709 s->left = t;
8710 else
8711 s->right = t;
8712 }
8713 else
8714 constructor_pending_elts = t;
8715 }
8716 break;
8717 }
8718 else
8719 {
8720 /* p->balance == +1; growth of left side balances the node. */
8721 p->balance = 0;
8722 break;
8723 }
2bede729 8724 }
3e4093b6
RS
8725 else /* r == p->right */
8726 {
8727 if (p->balance == 0)
8728 /* Growth propagation from right side. */
8729 p->balance++;
8730 else if (p->balance > 0)
8731 {
8732 if (r->balance > 0)
8733 {
8734 /* R rotation. */
8735 p->right = r->left;
8736 if (p->right)
8737 p->right->parent = p;
8738 r->left = p;
8739
8740 p->balance = 0;
8741 r->balance = 0;
8742
8743 s = p->parent;
8744 p->parent = r;
8745 r->parent = s;
8746 if (s)
8747 {
8748 if (s->left == p)
8749 s->left = r;
8750 else
8751 s->right = r;
8752 }
8753 else
8754 constructor_pending_elts = r;
8755 }
8756 else /* r->balance == -1 */
8757 {
8758 /* RL rotation */
8759 struct init_node *t = r->left;
8760
8761 r->left = t->right;
8762 if (r->left)
8763 r->left->parent = r;
8764 t->right = r;
8765
8766 p->right = t->left;
8767 if (p->right)
8768 p->right->parent = p;
8769 t->left = p;
8770
8771 r->balance = (t->balance < 0);
8772 p->balance = -(t->balance > 0);
8773 t->balance = 0;
8774
8775 s = p->parent;
8776 p->parent = t;
8777 r->parent = t;
8778 t->parent = s;
8779 if (s)
8780 {
8781 if (s->left == p)
8782 s->left = t;
8783 else
8784 s->right = t;
8785 }
8786 else
8787 constructor_pending_elts = t;
8788 }
8789 break;
8790 }
8791 else
8792 {
8793 /* p->balance == -1; growth of right side balances the node. */
8794 p->balance = 0;
8795 break;
8796 }
8797 }
8798
8799 r = p;
8800 p = p->parent;
8801 }
8802}
8803
8804/* Build AVL tree from a sorted chain. */
8805
8806static void
a1e3b3d9 8807set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8808{
4038c495
GB
8809 unsigned HOST_WIDE_INT ix;
8810 tree index, value;
3e4093b6
RS
8811
8812 if (TREE_CODE (constructor_type) != RECORD_TYPE
8813 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8814 return;
8815
4038c495 8816 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8817 add_pending_init (input_location, index, value, NULL_TREE, true,
8818 braced_init_obstack);
9771b263 8819 constructor_elements = NULL;
3e4093b6
RS
8820 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8821 {
8822 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8823 /* Skip any nameless bit fields at the beginning. */
3fa8871b 8824 while (constructor_unfilled_fields != NULL_TREE
3e4093b6 8825 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
3fa8871b 8826 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
3e4093b6 8827 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8828
de520661 8829 }
3e4093b6 8830 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8831 {
3e4093b6
RS
8832 if (TYPE_DOMAIN (constructor_type))
8833 constructor_unfilled_index
8834 = convert (bitsizetype,
8835 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8836 else
8837 constructor_unfilled_index = bitsize_zero_node;
de520661 8838 }
3e4093b6 8839 constructor_incremental = 0;
de520661 8840}
400fbf9f 8841
3e4093b6 8842/* Build AVL tree from a string constant. */
de520661 8843
3e4093b6 8844static void
a1e3b3d9
LB
8845set_nonincremental_init_from_string (tree str,
8846 struct obstack * braced_init_obstack)
de520661 8847{
3e4093b6
RS
8848 tree value, purpose, type;
8849 HOST_WIDE_INT val[2];
8850 const char *p, *end;
8851 int byte, wchar_bytes, charwidth, bitpos;
de520661 8852
366de0ce 8853 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8854
c466b2cd 8855 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6 8856 charwidth = TYPE_PRECISION (char_type_node);
2f1364c2
JJ
8857 gcc_assert ((size_t) wchar_bytes * charwidth
8858 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
3e4093b6
RS
8859 type = TREE_TYPE (constructor_type);
8860 p = TREE_STRING_POINTER (str);
8861 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8862
3e4093b6 8863 for (purpose = bitsize_zero_node;
8824edff
JJ
8864 p < end
8865 && !(constructor_max_index
8866 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8867 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8868 {
3e4093b6 8869 if (wchar_bytes == 1)
ffc5c6a9 8870 {
807e902e
KZ
8871 val[0] = (unsigned char) *p++;
8872 val[1] = 0;
ffc5c6a9
RH
8873 }
8874 else
3e4093b6 8875 {
3e4093b6 8876 val[1] = 0;
807e902e 8877 val[0] = 0;
3e4093b6
RS
8878 for (byte = 0; byte < wchar_bytes; byte++)
8879 {
8880 if (BYTES_BIG_ENDIAN)
8881 bitpos = (wchar_bytes - byte - 1) * charwidth;
8882 else
8883 bitpos = byte * charwidth;
2f1364c2 8884 val[bitpos / HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8885 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8886 << (bitpos % HOST_BITS_PER_WIDE_INT);
8887 }
8888 }
584ef5fe 8889
8df83eae 8890 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8891 {
8892 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8893 if (bitpos < HOST_BITS_PER_WIDE_INT)
8894 {
e3fe09c1 8895 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
3e4093b6 8896 {
aa256c4a 8897 val[0] |= HOST_WIDE_INT_M1U << bitpos;
807e902e 8898 val[1] = -1;
3e4093b6
RS
8899 }
8900 }
8901 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8902 {
807e902e
KZ
8903 if (val[0] < 0)
8904 val[1] = -1;
3e4093b6 8905 }
e3fe09c1 8906 else if (val[1] & (HOST_WIDE_INT_1
3e4093b6 8907 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
aa256c4a 8908 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
3e4093b6 8909 }
ffc5c6a9 8910
807e902e
KZ
8911 value = wide_int_to_tree (type,
8912 wide_int::from_array (val, 2,
8913 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8914 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8915 braced_init_obstack);
9dfcc8db
BH
8916 }
8917
3e4093b6
RS
8918 constructor_incremental = 0;
8919}
de520661 8920
6574d78e 8921/* Return value of FIELD in pending initializer or NULL_TREE if the field was
3e4093b6
RS
8922 not initialized yet. */
8923
8924static tree
a1e3b3d9 8925find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8926{
8927 struct init_node *p;
8928
8929 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8930 {
3e4093b6
RS
8931 if (constructor_incremental
8932 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8933 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8934
8935 p = constructor_pending_elts;
8936 while (p)
19d76e60 8937 {
3e4093b6
RS
8938 if (tree_int_cst_lt (field, p->purpose))
8939 p = p->left;
8940 else if (tree_int_cst_lt (p->purpose, field))
8941 p = p->right;
8942 else
8943 return p->value;
19d76e60 8944 }
19d76e60 8945 }
3e4093b6 8946 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8947 {
3e4093b6 8948 tree bitpos = bit_position (field);
de520661 8949
3e4093b6
RS
8950 if (constructor_incremental
8951 && (!constructor_unfilled_fields
8952 || tree_int_cst_lt (bitpos,
8953 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8954 set_nonincremental_init (braced_init_obstack);
de520661 8955
3e4093b6
RS
8956 p = constructor_pending_elts;
8957 while (p)
8958 {
8959 if (field == p->purpose)
8960 return p->value;
8961 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8962 p = p->left;
8963 else
8964 p = p->right;
8965 }
8966 }
8967 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8968 {
9771b263
DN
8969 if (!vec_safe_is_empty (constructor_elements)
8970 && (constructor_elements->last ().index == field))
8971 return constructor_elements->last ().value;
de520661 8972 }
6574d78e 8973 return NULL_TREE;
de520661
RS
8974}
8975
3e4093b6
RS
8976/* "Output" the next constructor element.
8977 At top level, really output it to assembler code now.
8978 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8979 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8980 TYPE is the data type that the containing data type wants here.
8981 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8982 If VALUE is a string constant, STRICT_STRING is true if it is
8983 unparenthesized or we should not warn here for it being parenthesized.
8984 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8985
30af3a2b
MP
8986 PENDING if true means output pending elements that belong
8987 right after this element. (PENDING is normally true;
8988 it is false while outputting pending elements, to avoid recursion.)
b295aee2
JJ
8989
8990 IMPLICIT is true if value comes from pop_init_level (1),
8991 the new initializer has been merged with the existing one
8992 and thus no warnings should be emitted about overriding an
8993 existing initializer. */
8b6a5902 8994
3e4093b6 8995static void
34cf811f 8996output_init_element (location_t loc, tree value, tree origtype,
30af3a2b 8997 bool strict_string, tree type, tree field, bool pending,
34cf811f 8998 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8999{
8ce94e44 9000 tree semantic_type = NULL_TREE;
928c19bb
JM
9001 bool maybe_const = true;
9002 bool npc;
4038c495 9003
0a880880 9004 if (type == error_mark_node || value == error_mark_node)
8b6a5902 9005 {
3e4093b6
RS
9006 constructor_erroneous = 1;
9007 return;
8b6a5902 9008 }
46bdb9cf
JM
9009 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9010 && (TREE_CODE (value) == STRING_CST
9011 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9012 && !(TREE_CODE (value) == STRING_CST
9013 && TREE_CODE (type) == ARRAY_TYPE
9014 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9015 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9016 TYPE_MAIN_VARIANT (type)))
c2255bc4 9017 value = array_to_pointer_conversion (input_location, value);
8b6a5902 9018
3e4093b6 9019 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 9020 && require_constant_value && pending)
8b6a5902 9021 {
3e4093b6
RS
9022 /* As an extension, allow initializing objects with static storage
9023 duration with compound literals (which are then treated just as
9024 the brace enclosed list they contain). */
4435bb92
MP
9025 if (flag_isoc99)
9026 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9027 "constant");
3e4093b6
RS
9028 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9029 value = DECL_INITIAL (decl);
8b6a5902
JJ
9030 }
9031
928c19bb 9032 npc = null_pointer_constant_p (value);
8ce94e44
JM
9033 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9034 {
9035 semantic_type = TREE_TYPE (value);
9036 value = TREE_OPERAND (value, 0);
9037 }
928c19bb
JM
9038 value = c_fully_fold (value, require_constant_value, &maybe_const);
9039
3e4093b6
RS
9040 if (value == error_mark_node)
9041 constructor_erroneous = 1;
9042 else if (!TREE_CONSTANT (value))
9043 constructor_constant = 0;
ee45a32d
EB
9044 else if (!initializer_constant_valid_p (value,
9045 TREE_TYPE (value),
9046 AGGREGATE_TYPE_P (constructor_type)
9047 && TYPE_REVERSE_STORAGE_ORDER
9048 (constructor_type))
3e636daf 9049 || (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6
RS
9050 && DECL_C_BIT_FIELD (field)
9051 && TREE_CODE (value) != INTEGER_CST))
9052 constructor_simple = 0;
928c19bb
JM
9053 if (!maybe_const)
9054 constructor_nonconst = 1;
3e4093b6 9055
4378d117
MS
9056 /* Digest the initializer and issue any errors about incompatible
9057 types before issuing errors about non-constant initializers. */
9058 tree new_value = value;
9059 if (semantic_type)
9060 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9061 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9062 require_constant_value);
9063 if (new_value == error_mark_node)
9064 {
9065 constructor_erroneous = 1;
9066 return;
9067 }
9068 if (require_constant_value || require_constant_elements)
9069 constant_expression_warning (new_value);
9070
9071 /* Proceed to check the constness of the original initializer. */
116df786 9072 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 9073 {
116df786
RH
9074 if (require_constant_value)
9075 {
ea58ef42 9076 error_init (loc, "initializer element is not constant");
116df786
RH
9077 value = error_mark_node;
9078 }
9079 else if (require_constant_elements)
8337d1db 9080 pedwarn (loc, OPT_Wpedantic,
509c9d60 9081 "initializer element is not computable at load time");
8b6a5902 9082 }
928c19bb
JM
9083 else if (!maybe_const
9084 && (require_constant_value || require_constant_elements))
8337d1db 9085 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 9086 "initializer element is not a constant expression");
3e4093b6 9087
81f40b79
ILT
9088 /* Issue -Wc++-compat warnings about initializing a bitfield with
9089 enum type. */
9090 if (warn_cxx_compat
9091 && field != NULL_TREE
9092 && TREE_CODE (field) == FIELD_DECL
9093 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9094 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9095 != TYPE_MAIN_VARIANT (type))
9096 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9097 {
9098 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9099 if (checktype != error_mark_node
9100 && (TYPE_MAIN_VARIANT (checktype)
9101 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 9102 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
9103 "enum conversion in initialization is invalid in C++");
9104 }
9105
3e4093b6
RS
9106 /* If this field is empty (and not at the end of structure),
9107 don't do anything other than checking the initializer. */
9108 if (field
9109 && (TREE_TYPE (field) == error_mark_node
9110 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9111 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9112 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 9113 || DECL_CHAIN (field)))))
3e4093b6
RS
9114 return;
9115
4378d117
MS
9116 /* Finally, set VALUE to the initializer value digested above. */
9117 value = new_value;
8b6a5902 9118
3e4093b6
RS
9119 /* If this element doesn't come next in sequence,
9120 put it on constructor_pending_elts. */
9121 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9122 && (!constructor_incremental
9123 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 9124 {
3e4093b6
RS
9125 if (constructor_incremental
9126 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 9127 set_nonincremental_init (braced_init_obstack);
3e4093b6 9128
96b40f8d 9129 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 9130 braced_init_obstack);
3e4093b6 9131 return;
8b6a5902 9132 }
3e4093b6
RS
9133 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9134 && (!constructor_incremental
9135 || field != constructor_unfilled_fields))
8b6a5902 9136 {
3e4093b6
RS
9137 /* We do this for records but not for unions. In a union,
9138 no matter which field is specified, it can be initialized
9139 right away since it starts at the beginning of the union. */
9140 if (constructor_incremental)
9141 {
9142 if (!constructor_unfilled_fields)
a1e3b3d9 9143 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
9144 else
9145 {
9146 tree bitpos, unfillpos;
9147
9148 bitpos = bit_position (field);
9149 unfillpos = bit_position (constructor_unfilled_fields);
9150
9151 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 9152 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
9153 }
9154 }
9155
96b40f8d 9156 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 9157 braced_init_obstack);
3e4093b6 9158 return;
8b6a5902 9159 }
3e4093b6 9160 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 9161 && !vec_safe_is_empty (constructor_elements))
3e4093b6 9162 {
b295aee2
JJ
9163 if (!implicit)
9164 {
9771b263 9165 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 9166 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
9167 "initialized field with side-effects overwritten");
9168 else if (warn_override_init)
96b40f8d
MP
9169 warning_init (loc, OPT_Woverride_init,
9170 "initialized field overwritten");
b295aee2 9171 }
8b6a5902 9172
3e4093b6 9173 /* We can have just one union field set. */
9771b263 9174 constructor_elements = NULL;
3e4093b6 9175 }
8b6a5902 9176
3e4093b6
RS
9177 /* Otherwise, output this element either to
9178 constructor_elements or to the assembler file. */
8b6a5902 9179
f32682ca 9180 constructor_elt celt = {field, value};
9771b263 9181 vec_safe_push (constructor_elements, celt);
8b6a5902 9182
3e4093b6
RS
9183 /* Advance the variable that indicates sequential elements output. */
9184 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9185 constructor_unfilled_index
db3927fb
AH
9186 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9187 bitsize_one_node);
3e4093b6
RS
9188 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9189 {
9190 constructor_unfilled_fields
910ad8de 9191 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 9192
3e4093b6 9193 /* Skip any nameless bit fields. */
3fa8871b 9194 while (constructor_unfilled_fields != NULL_TREE
3e4093b6 9195 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
3fa8871b 9196 && DECL_NAME (constructor_unfilled_fields) == NULL_TREE)
3e4093b6 9197 constructor_unfilled_fields =
910ad8de 9198 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
9199 }
9200 else if (TREE_CODE (constructor_type) == UNION_TYPE)
3fa8871b 9201 constructor_unfilled_fields = NULL_TREE;
de520661 9202
3e4093b6
RS
9203 /* Now output any pending elements which have become next. */
9204 if (pending)
a1e3b3d9 9205 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 9206}
8b6a5902 9207
3e4093b6
RS
9208/* Output any pending elements which have become next.
9209 As we output elements, constructor_unfilled_{fields,index}
9210 advances, which may cause other elements to become next;
9211 if so, they too are output.
8b6a5902 9212
3e4093b6
RS
9213 If ALL is 0, we return when there are
9214 no more pending elements to output now.
665f2503 9215
3e4093b6
RS
9216 If ALL is 1, we output space as necessary so that
9217 we can output all the pending elements. */
3e4093b6 9218static void
a1e3b3d9 9219output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
9220{
9221 struct init_node *elt = constructor_pending_elts;
9222 tree next;
de520661 9223
3e4093b6
RS
9224 retry:
9225
ba228239 9226 /* Look through the whole pending tree.
3e4093b6
RS
9227 If we find an element that should be output now,
9228 output it. Otherwise, set NEXT to the element
9229 that comes first among those still pending. */
9230
3fa8871b 9231 next = NULL_TREE;
3e4093b6
RS
9232 while (elt)
9233 {
9234 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 9235 {
3e4093b6
RS
9236 if (tree_int_cst_equal (elt->purpose,
9237 constructor_unfilled_index))
34cf811f
MP
9238 output_init_element (input_location, elt->value, elt->origtype,
9239 true, TREE_TYPE (constructor_type),
30af3a2b 9240 constructor_unfilled_index, false, false,
a1e3b3d9 9241 braced_init_obstack);
3e4093b6
RS
9242 else if (tree_int_cst_lt (constructor_unfilled_index,
9243 elt->purpose))
8b6a5902 9244 {
3e4093b6
RS
9245 /* Advance to the next smaller node. */
9246 if (elt->left)
9247 elt = elt->left;
9248 else
9249 {
9250 /* We have reached the smallest node bigger than the
9251 current unfilled index. Fill the space first. */
9252 next = elt->purpose;
9253 break;
9254 }
8b6a5902 9255 }
ce662d4c
JJ
9256 else
9257 {
3e4093b6
RS
9258 /* Advance to the next bigger node. */
9259 if (elt->right)
9260 elt = elt->right;
9261 else
ce662d4c 9262 {
3e4093b6
RS
9263 /* We have reached the biggest node in a subtree. Find
9264 the parent of it, which is the next bigger node. */
9265 while (elt->parent && elt->parent->right == elt)
9266 elt = elt->parent;
9267 elt = elt->parent;
9268 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9269 elt->purpose))
9270 {
9271 next = elt->purpose;
9272 break;
9273 }
ce662d4c
JJ
9274 }
9275 }
8b6a5902 9276 }
3e636daf 9277 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
9278 {
9279 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 9280
3e4093b6 9281 /* If the current record is complete we are done. */
3fa8871b 9282 if (constructor_unfilled_fields == NULL_TREE)
3e4093b6 9283 break;
de520661 9284
3e4093b6
RS
9285 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9286 elt_bitpos = bit_position (elt->purpose);
9287 /* We can't compare fields here because there might be empty
9288 fields in between. */
9289 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9290 {
9291 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
9292 output_init_element (input_location, elt->value, elt->origtype,
9293 true, TREE_TYPE (elt->purpose),
30af3a2b 9294 elt->purpose, false, false,
a1e3b3d9 9295 braced_init_obstack);
3e4093b6
RS
9296 }
9297 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9298 {
9299 /* Advance to the next smaller node. */
9300 if (elt->left)
9301 elt = elt->left;
9302 else
9303 {
9304 /* We have reached the smallest node bigger than the
9305 current unfilled field. Fill the space first. */
9306 next = elt->purpose;
9307 break;
9308 }
9309 }
9310 else
9311 {
9312 /* Advance to the next bigger node. */
9313 if (elt->right)
9314 elt = elt->right;
9315 else
9316 {
9317 /* We have reached the biggest node in a subtree. Find
9318 the parent of it, which is the next bigger node. */
9319 while (elt->parent && elt->parent->right == elt)
9320 elt = elt->parent;
9321 elt = elt->parent;
9322 if (elt
9323 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9324 bit_position (elt->purpose))))
9325 {
9326 next = elt->purpose;
9327 break;
9328 }
9329 }
9330 }
9331 }
9332 }
de520661 9333
3e4093b6
RS
9334 /* Ordinarily return, but not if we want to output all
9335 and there are elements left. */
3fa8871b 9336 if (!(all && next != NULL_TREE))
e5cfb88f
RK
9337 return;
9338
3e4093b6
RS
9339 /* If it's not incremental, just skip over the gap, so that after
9340 jumping to retry we will output the next successive element. */
3e636daf 9341 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
9342 constructor_unfilled_fields = next;
9343 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9344 constructor_unfilled_index = next;
de520661 9345
3e4093b6
RS
9346 /* ELT now points to the node in the pending tree with the next
9347 initializer to output. */
9348 goto retry;
de520661
RS
9349}
9350\f
3e4093b6
RS
9351/* Add one non-braced element to the current constructor level.
9352 This adjusts the current position within the constructor's type.
9353 This may also start or terminate implicit levels
9354 to handle a partly-braced initializer.
e5e809f4 9355
3e4093b6 9356 Once this has found the correct level for the new element,
b295aee2
JJ
9357 it calls output_init_element.
9358
9359 IMPLICIT is true if value comes from pop_init_level (1),
9360 the new initializer has been merged with the existing one
9361 and thus no warnings should be emitted about overriding an
9362 existing initializer. */
3e4093b6
RS
9363
9364void
34cf811f 9365process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 9366 struct obstack * braced_init_obstack)
e5e809f4 9367{
916c5919 9368 tree orig_value = value.value;
3fa8871b
MP
9369 int string_flag
9370 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
916c5919 9371 bool strict_string = value.original_code == STRING_CST;
340baef7 9372 bool was_designated = designator_depth != 0;
e5e809f4 9373
3e4093b6 9374 designator_depth = 0;
b06df647 9375 designator_erroneous = 0;
e5e809f4 9376
9bac5cbb
G
9377 if (!implicit && value.value && !integer_zerop (value.value))
9378 constructor_zeroinit = 0;
9379
3e4093b6
RS
9380 /* Handle superfluous braces around string cst as in
9381 char x[] = {"foo"}; */
9382 if (string_flag
9383 && constructor_type
340baef7 9384 && !was_designated
3e4093b6 9385 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 9386 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 9387 && integer_zerop (constructor_unfilled_index))
e5e809f4 9388 {
916c5919 9389 if (constructor_stack->replacement_value.value)
ea58ef42 9390 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
9391 constructor_stack->replacement_value = value;
9392 return;
e5e809f4 9393 }
8b6a5902 9394
3fa8871b 9395 if (constructor_stack->replacement_value.value != NULL_TREE)
3e4093b6 9396 {
ea58ef42 9397 error_init (loc, "excess elements in struct initializer");
3e4093b6 9398 return;
e5e809f4
JL
9399 }
9400
3e4093b6
RS
9401 /* Ignore elements of a brace group if it is entirely superfluous
9402 and has already been diagnosed. */
3fa8871b 9403 if (constructor_type == NULL_TREE)
3e4093b6 9404 return;
e5e809f4 9405
976d5a22
TT
9406 if (!implicit && warn_designated_init && !was_designated
9407 && TREE_CODE (constructor_type) == RECORD_TYPE
9408 && lookup_attribute ("designated_init",
9409 TYPE_ATTRIBUTES (constructor_type)))
9410 warning_init (loc,
9411 OPT_Wdesignated_init,
9412 "positional initialization of field "
9413 "in %<struct%> declared with %<designated_init%> attribute");
9414
3e4093b6
RS
9415 /* If we've exhausted any levels that didn't have braces,
9416 pop them now. */
9417 while (constructor_stack->implicit)
9418 {
3e636daf 9419 if (RECORD_OR_UNION_TYPE_P (constructor_type)
3fa8871b 9420 && constructor_fields == NULL_TREE)
ea58ef42 9421 process_init_element (loc,
5dd9a9d0
DM
9422 pop_init_level (loc, 1, braced_init_obstack,
9423 last_init_list_comma),
a1e3b3d9 9424 true, braced_init_obstack);
53650abe 9425 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
31521951 9426 || VECTOR_TYPE_P (constructor_type))
8824edff
JJ
9427 && constructor_max_index
9428 && tree_int_cst_lt (constructor_max_index,
9429 constructor_index))
ea58ef42 9430 process_init_element (loc,
5dd9a9d0
DM
9431 pop_init_level (loc, 1, braced_init_obstack,
9432 last_init_list_comma),
a1e3b3d9 9433 true, braced_init_obstack);
3e4093b6
RS
9434 else
9435 break;
9436 }
e5e809f4 9437
3e4093b6
RS
9438 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9439 if (constructor_range_stack)
e5e809f4 9440 {
3e4093b6
RS
9441 /* If value is a compound literal and we'll be just using its
9442 content, don't put it into a SAVE_EXPR. */
916c5919 9443 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 9444 || !require_constant_value)
8ce94e44
JM
9445 {
9446 tree semantic_type = NULL_TREE;
9447 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9448 {
9449 semantic_type = TREE_TYPE (value.value);
9450 value.value = TREE_OPERAND (value.value, 0);
9451 }
b2fa0a8b 9452 value.value = save_expr (value.value);
8ce94e44
JM
9453 if (semantic_type)
9454 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9455 value.value);
9456 }
3e4093b6 9457 }
e5e809f4 9458
3e4093b6
RS
9459 while (1)
9460 {
9461 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 9462 {
3e4093b6
RS
9463 tree fieldtype;
9464 enum tree_code fieldcode;
e5e809f4 9465
3fa8871b 9466 if (constructor_fields == NULL_TREE)
3e4093b6 9467 {
ea58ef42 9468 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
9469 break;
9470 }
e5e809f4 9471
3e4093b6
RS
9472 fieldtype = TREE_TYPE (constructor_fields);
9473 if (fieldtype != error_mark_node)
9474 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9475 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9476
3e4093b6
RS
9477 /* Error for non-static initialization of a flexible array member. */
9478 if (fieldcode == ARRAY_TYPE
9479 && !require_constant_value
9480 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 9481 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 9482 {
ea58ef42
MP
9483 error_init (loc, "non-static initialization of a flexible "
9484 "array member");
3e4093b6
RS
9485 break;
9486 }
e5e809f4 9487
2cc901dc
MP
9488 /* Error for initialization of a flexible array member with
9489 a string constant if the structure is in an array. E.g.:
9490 struct S { int x; char y[]; };
9491 struct S s[] = { { 1, "foo" } };
9492 is invalid. */
9493 if (string_flag
9494 && fieldcode == ARRAY_TYPE
9495 && constructor_depth > 1
9496 && TYPE_SIZE (fieldtype) == NULL_TREE
9497 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9498 {
9499 bool in_array_p = false;
9500 for (struct constructor_stack *p = constructor_stack;
9501 p && p->type; p = p->next)
9502 if (TREE_CODE (p->type) == ARRAY_TYPE)
9503 {
9504 in_array_p = true;
9505 break;
9506 }
9507 if (in_array_p)
9508 {
9509 error_init (loc, "initialization of flexible array "
9510 "member in a nested context");
9511 break;
9512 }
9513 }
9514
3e4093b6 9515 /* Accept a string constant to initialize a subarray. */
3fa8871b 9516 if (value.value != NULL_TREE
3e4093b6 9517 && fieldcode == ARRAY_TYPE
197463ae 9518 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9519 && string_flag)
916c5919 9520 value.value = orig_value;
3e4093b6
RS
9521 /* Otherwise, if we have come to a subaggregate,
9522 and we don't have an element of its type, push into it. */
3fa8871b 9523 else if (value.value != NULL_TREE
916c5919
JM
9524 && value.value != error_mark_node
9525 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9526 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9527 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9528 {
ea58ef42 9529 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9530 continue;
9531 }
e5e809f4 9532
916c5919 9533 if (value.value)
3e4093b6
RS
9534 {
9535 push_member_name (constructor_fields);
34cf811f 9536 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9537 strict_string, fieldtype,
30af3a2b 9538 constructor_fields, true, implicit,
a1e3b3d9 9539 braced_init_obstack);
3e4093b6 9540 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9541 }
9542 else
3e4093b6
RS
9543 /* Do the bookkeeping for an element that was
9544 directly output as a constructor. */
e5e809f4 9545 {
3e4093b6
RS
9546 /* For a record, keep track of end position of last field. */
9547 if (DECL_SIZE (constructor_fields))
c22cacf3 9548 constructor_bit_index
db3927fb
AH
9549 = size_binop_loc (input_location, PLUS_EXPR,
9550 bit_position (constructor_fields),
9551 DECL_SIZE (constructor_fields));
3e4093b6
RS
9552
9553 /* If the current field was the first one not yet written out,
9554 it isn't now, so update. */
9555 if (constructor_unfilled_fields == constructor_fields)
9556 {
910ad8de 9557 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9558 /* Skip any nameless bit fields. */
9559 while (constructor_unfilled_fields != 0
9560 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9561 && DECL_NAME (constructor_unfilled_fields) == 0)
9562 constructor_unfilled_fields =
910ad8de 9563 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 9564 }
e5e809f4 9565 }
3e4093b6 9566
910ad8de 9567 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6 9568 /* Skip any nameless bit fields at the beginning. */
3fa8871b 9569 while (constructor_fields != NULL_TREE
3e4093b6 9570 && DECL_C_BIT_FIELD (constructor_fields)
3fa8871b 9571 && DECL_NAME (constructor_fields) == NULL_TREE)
910ad8de 9572 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9573 }
3e4093b6 9574 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 9575 {
3e4093b6
RS
9576 tree fieldtype;
9577 enum tree_code fieldcode;
e5e809f4 9578
3fa8871b 9579 if (constructor_fields == NULL_TREE)
3e4093b6 9580 {
d033409e 9581 pedwarn_init (loc, 0,
509c9d60 9582 "excess elements in union initializer");
3e4093b6
RS
9583 break;
9584 }
e5e809f4 9585
3e4093b6
RS
9586 fieldtype = TREE_TYPE (constructor_fields);
9587 if (fieldtype != error_mark_node)
9588 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9589 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9590
3e4093b6
RS
9591 /* Warn that traditional C rejects initialization of unions.
9592 We skip the warning if the value is zero. This is done
9593 under the assumption that the zero initializer in user
9594 code appears conditioned on e.g. __STDC__ to avoid
9595 "missing initializer" warnings and relies on default
9596 initialization to zero in the traditional C case.
9597 We also skip the warning if the initializer is designated,
9598 again on the assumption that this must be conditional on
9599 __STDC__ anyway (and we've already complained about the
9600 member-designator already). */
8400e75e 9601 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
9602 && !(value.value && (integer_zerop (value.value)
9603 || real_zerop (value.value))))
3176a0c2
DD
9604 warning (OPT_Wtraditional, "traditional C rejects initialization "
9605 "of unions");
e5e809f4 9606
3e4093b6 9607 /* Accept a string constant to initialize a subarray. */
3fa8871b 9608 if (value.value != NULL_TREE
3e4093b6 9609 && fieldcode == ARRAY_TYPE
197463ae 9610 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9611 && string_flag)
916c5919 9612 value.value = orig_value;
3e4093b6
RS
9613 /* Otherwise, if we have come to a subaggregate,
9614 and we don't have an element of its type, push into it. */
3fa8871b 9615 else if (value.value != NULL_TREE
916c5919
JM
9616 && value.value != error_mark_node
9617 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9618 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9619 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9620 {
ea58ef42 9621 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9622 continue;
9623 }
e5e809f4 9624
916c5919 9625 if (value.value)
3e4093b6
RS
9626 {
9627 push_member_name (constructor_fields);
34cf811f 9628 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9629 strict_string, fieldtype,
30af3a2b 9630 constructor_fields, true, implicit,
a1e3b3d9 9631 braced_init_obstack);
3e4093b6 9632 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9633 }
9634 else
3e4093b6
RS
9635 /* Do the bookkeeping for an element that was
9636 directly output as a constructor. */
e5e809f4 9637 {
3e4093b6 9638 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 9639 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9640 }
e5e809f4 9641
3fa8871b 9642 constructor_fields = NULL_TREE;
3e4093b6
RS
9643 }
9644 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9645 {
9646 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9647 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 9648
3e4093b6 9649 /* Accept a string constant to initialize a subarray. */
3fa8871b 9650 if (value.value != NULL_TREE
3e4093b6 9651 && eltcode == ARRAY_TYPE
197463ae 9652 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 9653 && string_flag)
916c5919 9654 value.value = orig_value;
3e4093b6
RS
9655 /* Otherwise, if we have come to a subaggregate,
9656 and we don't have an element of its type, push into it. */
3fa8871b 9657 else if (value.value != NULL_TREE
916c5919
JM
9658 && value.value != error_mark_node
9659 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9660 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9661 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9662 {
ea58ef42 9663 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9664 continue;
9665 }
8b6a5902 9666
3fa8871b 9667 if (constructor_max_index != NULL_TREE
3e4093b6
RS
9668 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9669 || integer_all_onesp (constructor_max_index)))
9670 {
d033409e 9671 pedwarn_init (loc, 0,
509c9d60 9672 "excess elements in array initializer");
3e4093b6
RS
9673 break;
9674 }
8b6a5902 9675
3e4093b6 9676 /* Now output the actual element. */
916c5919 9677 if (value.value)
3e4093b6 9678 {
ae7e9ddd 9679 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9680 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9681 strict_string, elttype,
30af3a2b 9682 constructor_index, true, implicit,
a1e3b3d9 9683 braced_init_obstack);
3e4093b6
RS
9684 RESTORE_SPELLING_DEPTH (constructor_depth);
9685 }
2f6e4e97 9686
3e4093b6 9687 constructor_index
db3927fb
AH
9688 = size_binop_loc (input_location, PLUS_EXPR,
9689 constructor_index, bitsize_one_node);
8b6a5902 9690
916c5919 9691 if (!value.value)
3e4093b6
RS
9692 /* If we are doing the bookkeeping for an element that was
9693 directly output as a constructor, we must update
9694 constructor_unfilled_index. */
9695 constructor_unfilled_index = constructor_index;
9696 }
31521951 9697 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
9698 {
9699 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9700
c22cacf3
MS
9701 /* Do a basic check of initializer size. Note that vectors
9702 always have a fixed size derived from their type. */
3e4093b6
RS
9703 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9704 {
d033409e 9705 pedwarn_init (loc, 0,
509c9d60 9706 "excess elements in vector initializer");
3e4093b6
RS
9707 break;
9708 }
8b6a5902 9709
3e4093b6 9710 /* Now output the actual element. */
916c5919 9711 if (value.value)
53650abe
AP
9712 {
9713 if (TREE_CODE (value.value) == VECTOR_CST)
9714 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9715 output_init_element (loc, value.value, value.original_type,
53650abe 9716 strict_string, elttype,
30af3a2b 9717 constructor_index, true, implicit,
a1e3b3d9 9718 braced_init_obstack);
53650abe 9719 }
8b6a5902 9720
3e4093b6 9721 constructor_index
db3927fb
AH
9722 = size_binop_loc (input_location,
9723 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9724
916c5919 9725 if (!value.value)
3e4093b6
RS
9726 /* If we are doing the bookkeeping for an element that was
9727 directly output as a constructor, we must update
9728 constructor_unfilled_index. */
9729 constructor_unfilled_index = constructor_index;
9730 }
8b6a5902 9731
3e4093b6
RS
9732 /* Handle the sole element allowed in a braced initializer
9733 for a scalar variable. */
b4519d39 9734 else if (constructor_type != error_mark_node
3fa8871b 9735 && constructor_fields == NULL_TREE)
8b6a5902 9736 {
d033409e 9737 pedwarn_init (loc, 0,
509c9d60 9738 "excess elements in scalar initializer");
3e4093b6 9739 break;
8b6a5902
JJ
9740 }
9741 else
9742 {
916c5919 9743 if (value.value)
34cf811f 9744 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9745 strict_string, constructor_type,
30af3a2b 9746 NULL_TREE, true, implicit,
a1e3b3d9 9747 braced_init_obstack);
3fa8871b 9748 constructor_fields = NULL_TREE;
8b6a5902
JJ
9749 }
9750
3e4093b6
RS
9751 /* Handle range initializers either at this level or anywhere higher
9752 in the designator stack. */
9753 if (constructor_range_stack)
8b6a5902 9754 {
3e4093b6
RS
9755 struct constructor_range_stack *p, *range_stack;
9756 int finish = 0;
9757
9758 range_stack = constructor_range_stack;
9759 constructor_range_stack = 0;
9760 while (constructor_stack != range_stack->stack)
8b6a5902 9761 {
366de0ce 9762 gcc_assert (constructor_stack->implicit);
34cf811f 9763 process_init_element (loc,
ea58ef42 9764 pop_init_level (loc, 1,
5dd9a9d0
DM
9765 braced_init_obstack,
9766 last_init_list_comma),
a1e3b3d9 9767 true, braced_init_obstack);
8b6a5902 9768 }
3e4093b6
RS
9769 for (p = range_stack;
9770 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9771 p = p->prev)
8b6a5902 9772 {
366de0ce 9773 gcc_assert (constructor_stack->implicit);
34cf811f 9774 process_init_element (loc,
ea58ef42 9775 pop_init_level (loc, 1,
5dd9a9d0
DM
9776 braced_init_obstack,
9777 last_init_list_comma),
a1e3b3d9 9778 true, braced_init_obstack);
8b6a5902 9779 }
3e4093b6 9780
db3927fb
AH
9781 p->index = size_binop_loc (input_location,
9782 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9783 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9784 finish = 1;
9785
9786 while (1)
9787 {
9788 constructor_index = p->index;
9789 constructor_fields = p->fields;
9790 if (finish && p->range_end && p->index == p->range_start)
9791 {
9792 finish = 0;
9793 p->prev = 0;
9794 }
9795 p = p->next;
9796 if (!p)
9797 break;
16595a1f 9798 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 9799 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9800 p->stack = constructor_stack;
9801 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9802 p->index = p->range_start;
9803 }
9804
9805 if (!finish)
9806 constructor_range_stack = range_stack;
9807 continue;
8b6a5902
JJ
9808 }
9809
3e4093b6 9810 break;
8b6a5902
JJ
9811 }
9812
3e4093b6
RS
9813 constructor_range_stack = 0;
9814}
9815\f
9f0e2d86
ZW
9816/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9817 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9818 an ASM_EXPR node). */
3e4093b6 9819tree
9f0e2d86 9820build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9821{
6de9cd9a
DN
9822 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9823 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9824 return add_stmt (args);
8b6a5902
JJ
9825}
9826
9f0e2d86
ZW
9827/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9828 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9829 SIMPLE indicates whether there was anything at all after the
9830 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9831 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9832tree
c2255bc4 9833build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9834 tree clobbers, tree labels, bool simple)
e5e809f4 9835{
3e4093b6 9836 tree tail;
9f0e2d86 9837 tree args;
6de9cd9a
DN
9838 int i;
9839 const char *constraint;
74f0c611 9840 const char **oconstraints;
6de9cd9a 9841 bool allows_mem, allows_reg, is_inout;
74f0c611 9842 int ninputs, noutputs;
6de9cd9a
DN
9843
9844 ninputs = list_length (inputs);
9845 noutputs = list_length (outputs);
74f0c611
RH
9846 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9847
1c384bf1 9848 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9849
6de9cd9a
DN
9850 /* Remove output conversions that change the type but not the mode. */
9851 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9852 {
3e4093b6 9853 tree output = TREE_VALUE (tail);
74f0c611 9854
eadd3d0d
JJ
9855 output = c_fully_fold (output, false, NULL);
9856
74f0c611
RH
9857 /* ??? Really, this should not be here. Users should be using a
9858 proper lvalue, dammit. But there's a long history of using casts
9859 in the output operands. In cases like longlong.h, this becomes a
9860 primitive form of typechecking -- if the cast can be removed, then
9861 the output operand had a type of the proper width; otherwise we'll
9862 get an error. Gross, but ... */
3e4093b6 9863 STRIP_NOPS (output);
74f0c611 9864
7bd11157 9865 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9866 output = error_mark_node;
8b6a5902 9867
5544530a
PB
9868 if (output != error_mark_node
9869 && (TREE_READONLY (output)
9870 || TYPE_READONLY (TREE_TYPE (output))
3e636daf 9871 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
5544530a 9872 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9873 readonly_error (loc, output, lv_asm);
5544530a 9874
6de9cd9a 9875 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9876 oconstraints[i] = constraint;
9877
9878 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9879 &allows_mem, &allows_reg, &is_inout))
9880 {
9881 /* If the operand is going to end up in memory,
9882 mark it addressable. */
9883 if (!allows_reg && !c_mark_addressable (output))
9884 output = error_mark_node;
bae5cddf
JJ
9885 if (!(!allows_reg && allows_mem)
9886 && output != error_mark_node
9887 && VOID_TYPE_P (TREE_TYPE (output)))
9888 {
9889 error_at (loc, "invalid use of void expression");
9890 output = error_mark_node;
9891 }
74f0c611
RH
9892 }
9893 else
c22cacf3 9894 output = error_mark_node;
3e4093b6 9895
74f0c611 9896 TREE_VALUE (tail) = output;
8b6a5902 9897 }
3e4093b6 9898
74f0c611
RH
9899 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9900 {
9901 tree input;
9902
9903 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9904 input = TREE_VALUE (tail);
9905
74f0c611
RH
9906 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9907 oconstraints, &allows_mem, &allows_reg))
9908 {
9909 /* If the operand is going to end up in memory,
9910 mark it addressable. */
b4c33883
AP
9911 if (!allows_reg && allows_mem)
9912 {
eadd3d0d
JJ
9913 input = c_fully_fold (input, false, NULL);
9914
b4c33883
AP
9915 /* Strip the nops as we allow this case. FIXME, this really
9916 should be rejected or made deprecated. */
9917 STRIP_NOPS (input);
9918 if (!c_mark_addressable (input))
9919 input = error_mark_node;
bae5cddf 9920 }
eadd3d0d 9921 else
bae5cddf 9922 {
eadd3d0d
JJ
9923 struct c_expr expr;
9924 memset (&expr, 0, sizeof (expr));
9925 expr.value = input;
267bac10 9926 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9927 input = c_fully_fold (expr.value, false, NULL);
9928
9929 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9930 {
9931 error_at (loc, "invalid use of void expression");
9932 input = error_mark_node;
9933 }
bae5cddf 9934 }
74f0c611
RH
9935 }
9936 else
9937 input = error_mark_node;
9938
9939 TREE_VALUE (tail) = input;
9940 }
3e4093b6 9941
1c384bf1
RH
9942 /* ASMs with labels cannot have outputs. This should have been
9943 enforced by the parser. */
9944 gcc_assert (outputs == NULL || labels == NULL);
9945
9946 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9947
5544530a
PB
9948 /* asm statements without outputs, including simple ones, are treated
9949 as volatile. */
9950 ASM_INPUT_P (args) = simple;
9951 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9952
9f0e2d86 9953 return args;
e5e809f4 9954}
3e4093b6 9955\f
c2255bc4
AH
9956/* Generate a goto statement to LABEL. LOC is the location of the
9957 GOTO. */
506e2710
RH
9958
9959tree
c2255bc4 9960c_finish_goto_label (location_t loc, tree label)
506e2710 9961{
e1b7793c 9962 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9963 if (!decl)
9964 return NULL_TREE;
506e2710 9965 TREE_USED (decl) = 1;
c2255bc4 9966 {
7fef86d3 9967 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
c2255bc4
AH
9968 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9969 SET_EXPR_LOCATION (t, loc);
9970 return add_stmt (t);
9971 }
506e2710
RH
9972}
9973
c2255bc4
AH
9974/* Generate a computed goto statement to EXPR. LOC is the location of
9975 the GOTO. */
506e2710
RH
9976
9977tree
c2255bc4 9978c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9979{
c2255bc4 9980 tree t;
c1771a20 9981 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9982 expr = c_fully_fold (expr, false, NULL);
506e2710 9983 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9984 t = build1 (GOTO_EXPR, void_type_node, expr);
9985 SET_EXPR_LOCATION (t, loc);
9986 return add_stmt (t);
506e2710
RH
9987}
9988
5088b058 9989/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9990 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9991 the location of the return statement, or the location of the expression,
9992 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9993 is the original type of RETVAL. */
de520661 9994
506e2710 9995tree
c2255bc4 9996c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9997{
0c9b182b
JJ
9998 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9999 bool no_warning = false;
928c19bb 10000 bool npc = false;
36536d79 10001 size_t rank = 0;
3e4093b6 10002
de8ddd5f
MP
10003 /* Use the expansion point to handle cases such as returning NULL
10004 in a function returning void. */
10005 source_location xloc = expansion_point_location_if_in_system_header (loc);
10006
3e4093b6 10007 if (TREE_THIS_VOLATILE (current_function_decl))
de8ddd5f 10008 warning_at (xloc, 0,
c2255bc4 10009 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 10010
b72271b9 10011 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
10012 {
10013 /* Array notations are allowed in a return statement if it is inside a
10014 built-in array notation reduction function. */
10015 if (!find_rank (loc, retval, retval, false, &rank))
10016 return error_mark_node;
10017 if (rank >= 1)
10018 {
10019 error_at (loc, "array notation expression cannot be used as a "
10020 "return value");
10021 return error_mark_node;
10022 }
10023 }
3af9c5e9 10024 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
10025 {
10026 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
10027 "allowed");
10028 return error_mark_node;
10029 }
928c19bb
JM
10030 if (retval)
10031 {
8ce94e44 10032 tree semantic_type = NULL_TREE;
928c19bb 10033 npc = null_pointer_constant_p (retval);
8ce94e44
JM
10034 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10035 {
10036 semantic_type = TREE_TYPE (retval);
10037 retval = TREE_OPERAND (retval, 0);
10038 }
928c19bb 10039 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
10040 if (semantic_type)
10041 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
10042 }
10043
3e4093b6 10044 if (!retval)
de520661 10045 {
3e4093b6
RS
10046 current_function_returns_null = 1;
10047 if ((warn_return_type || flag_isoc99)
3fa8871b 10048 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 10049 {
94c40e19 10050 bool warned_here;
35aff4fb 10051 if (flag_isoc99)
94c40e19
DM
10052 warned_here = pedwarn
10053 (loc, 0,
10054 "%<return%> with no value, in function returning non-void");
35aff4fb 10055 else
94c40e19
DM
10056 warned_here = warning_at
10057 (loc, OPT_Wreturn_type,
10058 "%<return%> with no value, in function returning non-void");
0c9b182b 10059 no_warning = true;
94c40e19
DM
10060 if (warned_here)
10061 inform (DECL_SOURCE_LOCATION (current_function_decl),
10062 "declared here");
0c9b182b 10063 }
400fbf9f 10064 }
3fa8871b 10065 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
de520661 10066 {
3e4093b6 10067 current_function_returns_null = 1;
94c40e19 10068 bool warned_here;
2397c575 10069 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
94c40e19
DM
10070 warned_here = pedwarn
10071 (xloc, 0,
10072 "%<return%> with a value, in function returning void");
b8698a0f 10073 else
94c40e19
DM
10074 warned_here = pedwarn
10075 (xloc, OPT_Wpedantic, "ISO C forbids "
10076 "%<return%> with expression, in function returning void");
10077 if (warned_here)
10078 inform (DECL_SOURCE_LOCATION (current_function_decl),
10079 "declared here");
de520661 10080 }
3e4093b6 10081 else
de520661 10082 {
68fca595
MP
10083 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10084 retval, origtype, ic_return,
c2255bc4 10085 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
10086 tree res = DECL_RESULT (current_function_decl);
10087 tree inner;
9feb29df 10088 bool save;
3e4093b6
RS
10089
10090 current_function_returns_value = 1;
10091 if (t == error_mark_node)
506e2710 10092 return NULL_TREE;
3e4093b6 10093
9feb29df
JJ
10094 save = in_late_binary_op;
10095 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
10096 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10097 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10098 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10099 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
45b2222a 10100 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
9feb29df 10101 in_late_binary_op = true;
3e4093b6 10102 inner = t = convert (TREE_TYPE (res), t);
9feb29df 10103 in_late_binary_op = save;
3e4093b6
RS
10104
10105 /* Strip any conversions, additions, and subtractions, and see if
10106 we are returning the address of a local variable. Warn if so. */
10107 while (1)
8b6a5902 10108 {
3e4093b6 10109 switch (TREE_CODE (inner))
8b6a5902 10110 {
849421a3
JJ
10111 CASE_CONVERT:
10112 case NON_LVALUE_EXPR:
3e4093b6 10113 case PLUS_EXPR:
849421a3 10114 case POINTER_PLUS_EXPR:
3e4093b6
RS
10115 inner = TREE_OPERAND (inner, 0);
10116 continue;
10117
10118 case MINUS_EXPR:
10119 /* If the second operand of the MINUS_EXPR has a pointer
10120 type (or is converted from it), this may be valid, so
10121 don't give a warning. */
10122 {
10123 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 10124
3f75a254 10125 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
10126 && (CONVERT_EXPR_P (op1)
10127 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 10128 op1 = TREE_OPERAND (op1, 0);
8b6a5902 10129
3e4093b6
RS
10130 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10131 break;
8b6a5902 10132
3e4093b6
RS
10133 inner = TREE_OPERAND (inner, 0);
10134 continue;
10135 }
400fbf9f 10136
3e4093b6
RS
10137 case ADDR_EXPR:
10138 inner = TREE_OPERAND (inner, 0);
c2f4acb7 10139
6615c446 10140 while (REFERENCE_CLASS_P (inner)
22d03525 10141 && !INDIRECT_REF_P (inner))
3e4093b6 10142 inner = TREE_OPERAND (inner, 0);
8b6a5902 10143
a2f1f4c3 10144 if (DECL_P (inner)
3f75a254
JM
10145 && !DECL_EXTERNAL (inner)
10146 && !TREE_STATIC (inner)
3e4093b6 10147 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
10148 {
10149 if (TREE_CODE (inner) == LABEL_DECL)
10150 warning_at (loc, OPT_Wreturn_local_addr,
10151 "function returns address of label");
10152 else
b4dfdc11
MG
10153 {
10154 warning_at (loc, OPT_Wreturn_local_addr,
10155 "function returns address of local variable");
10156 tree zero = build_zero_cst (TREE_TYPE (res));
10157 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10158 }
19fc9faa 10159 }
3e4093b6 10160 break;
8b6a5902 10161
3e4093b6
RS
10162 default:
10163 break;
10164 }
de520661 10165
3e4093b6
RS
10166 break;
10167 }
10168
53fb4de3 10169 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 10170 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
10171
10172 if (warn_sequence_point)
10173 verify_sequence_points (retval);
de520661 10174 }
8b6a5902 10175
c2255bc4 10176 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
10177 TREE_NO_WARNING (ret_stmt) |= no_warning;
10178 return add_stmt (ret_stmt);
de520661 10179}
3e4093b6
RS
10180\f
10181struct c_switch {
604f5adf
ILT
10182 /* The SWITCH_EXPR being built. */
10183 tree switch_expr;
a6c0a76c 10184
89dbed81 10185 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
10186 default conversion is applied. */
10187 tree orig_type;
10188
3e4093b6
RS
10189 /* A splay-tree mapping the low element of a case range to the high
10190 element, or NULL_TREE if there is no high element. Used to
10191 determine whether or not a new case label duplicates an old case
10192 label. We need a tree, rather than simply a hash table, because
10193 of the GNU case range extension. */
10194 splay_tree cases;
a6c0a76c 10195
e1b7793c
ILT
10196 /* The bindings at the point of the switch. This is used for
10197 warnings crossing decls when branching to a case label. */
10198 struct c_spot_bindings *bindings;
187230a7 10199
3e4093b6
RS
10200 /* The next node on the stack. */
10201 struct c_switch *next;
b155cfd9
MP
10202
10203 /* Remember whether the controlling expression had boolean type
10204 before integer promotions for the sake of -Wswitch-bool. */
10205 bool bool_cond_p;
10206
10207 /* Remember whether there was a case value that is outside the
10208 range of the ORIG_TYPE. */
10209 bool outside_range_p;
3e4093b6 10210};
400fbf9f 10211
3e4093b6
RS
10212/* A stack of the currently active switch statements. The innermost
10213 switch statement is on the top of the stack. There is no need to
10214 mark the stack for garbage collection because it is only active
10215 during the processing of the body of a function, and we never
10216 collect at that point. */
de520661 10217
506e2710 10218struct c_switch *c_switch_stack;
de520661 10219
3e4093b6 10220/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 10221 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef 10222 SWITCH_COND_LOC is the location of the switch's condition.
b155cfd9 10223 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
de520661 10224
3e4093b6 10225tree
c2255bc4
AH
10226c_start_case (location_t switch_loc,
10227 location_t switch_cond_loc,
fedfecef 10228 tree exp, bool explicit_cast_p)
de520661 10229{
c58e8676 10230 tree orig_type = error_mark_node;
b155cfd9 10231 bool bool_cond_p = false;
3e4093b6 10232 struct c_switch *cs;
2f6e4e97 10233
3e4093b6 10234 if (exp != error_mark_node)
de520661 10235 {
3e4093b6
RS
10236 orig_type = TREE_TYPE (exp);
10237
c58e8676 10238 if (!INTEGRAL_TYPE_P (orig_type))
de520661 10239 {
c58e8676
VR
10240 if (orig_type != error_mark_node)
10241 {
c2255bc4 10242 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
10243 orig_type = error_mark_node;
10244 }
3e4093b6 10245 exp = integer_zero_node;
de520661 10246 }
3e4093b6 10247 else
de520661 10248 {
c58e8676 10249 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
10250 tree e = exp;
10251
10252 /* Warn if the condition has boolean value. */
10253 while (TREE_CODE (e) == COMPOUND_EXPR)
10254 e = TREE_OPERAND (e, 1);
10255
10256 if ((TREE_CODE (type) == BOOLEAN_TYPE
10257 || truth_value_p (TREE_CODE (e)))
10258 /* Explicit cast to int suppresses this warning. */
10259 && !(TREE_CODE (type) == INTEGER_TYPE
10260 && explicit_cast_p))
b155cfd9 10261 bool_cond_p = true;
8b6a5902 10262
8400e75e 10263 if (!in_system_header_at (input_location)
3e4093b6
RS
10264 && (type == long_integer_type_node
10265 || type == long_unsigned_type_node))
c2255bc4
AH
10266 warning_at (switch_cond_loc,
10267 OPT_Wtraditional, "%<long%> switch expression not "
10268 "converted to %<int%> in ISO C");
8b6a5902 10269
928c19bb 10270 exp = c_fully_fold (exp, false, NULL);
3e4093b6 10271 exp = default_conversion (exp);
ca085fd7
MLI
10272
10273 if (warn_sequence_point)
10274 verify_sequence_points (exp);
3e4093b6
RS
10275 }
10276 }
10277
604f5adf 10278 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 10279 cs = XNEW (struct c_switch);
604f5adf 10280 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 10281 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 10282 cs->orig_type = orig_type;
3e4093b6 10283 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 10284 cs->bindings = c_get_switch_bindings ();
b155cfd9
MP
10285 cs->bool_cond_p = bool_cond_p;
10286 cs->outside_range_p = false;
506e2710
RH
10287 cs->next = c_switch_stack;
10288 c_switch_stack = cs;
3e4093b6 10289
604f5adf 10290 return add_stmt (cs->switch_expr);
3e4093b6
RS
10291}
10292
c2255bc4 10293/* Process a case label at location LOC. */
3e4093b6
RS
10294
10295tree
c2255bc4 10296do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
10297{
10298 tree label = NULL_TREE;
10299
17cede2e
JM
10300 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10301 {
10302 low_value = c_fully_fold (low_value, false, NULL);
10303 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 10304 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
10305 "case label is not an integer constant expression");
10306 }
10307
10308 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10309 {
10310 high_value = c_fully_fold (high_value, false, NULL);
10311 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 10312 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
10313 "case label is not an integer constant expression");
10314 }
10315
e1b7793c 10316 if (c_switch_stack == NULL)
187230a7
JM
10317 {
10318 if (low_value)
e1b7793c 10319 error_at (loc, "case label not within a switch statement");
187230a7 10320 else
e1b7793c
ILT
10321 error_at (loc, "%<default%> label not within a switch statement");
10322 return NULL_TREE;
187230a7 10323 }
de520661 10324
e1b7793c
ILT
10325 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10326 EXPR_LOCATION (c_switch_stack->switch_expr),
10327 loc))
10328 return NULL_TREE;
10329
10330 label = c_add_case_label (loc, c_switch_stack->cases,
10331 SWITCH_COND (c_switch_stack->switch_expr),
10332 c_switch_stack->orig_type,
b155cfd9
MP
10333 low_value, high_value,
10334 &c_switch_stack->outside_range_p);
e1b7793c
ILT
10335 if (label == error_mark_node)
10336 label = NULL_TREE;
3e4093b6
RS
10337 return label;
10338}
de520661 10339
083e891e
MP
10340/* Finish the switch statement. TYPE is the original type of the
10341 controlling expression of the switch, or NULL_TREE. */
de520661 10342
3e4093b6 10343void
083e891e 10344c_finish_case (tree body, tree type)
3e4093b6 10345{
506e2710 10346 struct c_switch *cs = c_switch_stack;
fbc315db 10347 location_t switch_location;
3e4093b6 10348
604f5adf 10349 SWITCH_BODY (cs->switch_expr) = body;
325c3691 10350
6de9cd9a 10351 /* Emit warnings as needed. */
c2255bc4 10352 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 10353 c_do_switch_warnings (cs->cases, switch_location,
083e891e 10354 type ? type : TREE_TYPE (cs->switch_expr),
b155cfd9
MP
10355 SWITCH_COND (cs->switch_expr),
10356 cs->bool_cond_p, cs->outside_range_p);
6de9cd9a 10357
3e4093b6 10358 /* Pop the stack. */
506e2710 10359 c_switch_stack = cs->next;
3e4093b6 10360 splay_tree_delete (cs->cases);
e1b7793c 10361 c_release_switch_bindings (cs->bindings);
5d038c4c 10362 XDELETE (cs);
de520661 10363}
325c3691 10364\f
506e2710
RH
10365/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10366 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
99cd9857 10367 may be null. */
325c3691 10368
9e51cf9d 10369void
506e2710 10370c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
99cd9857 10371 tree else_block)
325c3691 10372{
506e2710 10373 tree stmt;
325c3691 10374
25c22937
BI
10375 /* If the condition has array notations, then the rank of the then_block and
10376 else_block must be either 0 or be equal to the rank of the condition. If
10377 the condition does not have array notations then break them up as it is
10378 broken up in a normal expression. */
b72271b9 10379 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
10380 {
10381 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10382 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10383 return;
10384 if (then_block
10385 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10386 return;
10387 if (else_block
10388 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10389 return;
10390 if (cond_rank != then_rank && then_rank != 0)
10391 {
10392 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10393 " and the then-block");
10394 return;
10395 }
10396 else if (cond_rank != else_rank && else_rank != 0)
10397 {
10398 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10399 " and the else-block");
10400 return;
10401 }
10402 }
16865eaa 10403
2214de30 10404 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 10405 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 10406 add_stmt (stmt);
325c3691
RH
10407}
10408
506e2710
RH
10409/* Emit a general-purpose loop construct. START_LOCUS is the location of
10410 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10411 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 10412 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 10413 the continue label. Everything is allowed to be NULL. */
325c3691
RH
10414
10415void
506e2710
RH
10416c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10417 tree blab, tree clab, bool cond_is_first)
325c3691 10418{
506e2710
RH
10419 tree entry = NULL, exit = NULL, t;
10420
e5e44252
AK
10421 /* In theory could forbid cilk spawn for loop increment expression,
10422 but it should work just fine. */
36536d79 10423
28af952a
RS
10424 /* If the condition is zero don't generate a loop construct. */
10425 if (cond && integer_zerop (cond))
10426 {
10427 if (cond_is_first)
10428 {
10429 t = build_and_jump (&blab);
10430 SET_EXPR_LOCATION (t, start_locus);
10431 add_stmt (t);
10432 }
10433 }
10434 else
506e2710
RH
10435 {
10436 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 10437
506e2710 10438 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
10439 out of the loop, or to the top of it. If there's no exit condition,
10440 then we just build a jump back to the top. */
506e2710 10441 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 10442
28af952a 10443 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
10444 {
10445 /* Canonicalize the loop condition to the end. This means
10446 generating a branch to the loop condition. Reuse the
10447 continue label, if possible. */
10448 if (cond_is_first)
10449 {
10450 if (incr || !clab)
10451 {
10452 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10453 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10454 }
10455 else
10456 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 10457 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
10458 add_stmt (t);
10459 }
10460
506e2710 10461 t = build_and_jump (&blab);
506e2710 10462 if (cond_is_first)
db3927fb
AH
10463 exit = fold_build3_loc (start_locus,
10464 COND_EXPR, void_type_node, cond, exit, t);
506e2710 10465 else
db3927fb
AH
10466 exit = fold_build3_loc (input_location,
10467 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3 10468 }
fc402eec
AA
10469 else
10470 {
10471 /* For the backward-goto's location of an unconditional loop
10472 use the beginning of the body, or, if there is none, the
10473 top of the loop. */
10474 location_t loc = EXPR_LOCATION (expr_first (body));
10475 if (loc == UNKNOWN_LOCATION)
10476 loc = start_locus;
10477 SET_EXPR_LOCATION (exit, loc);
10478 }
c22cacf3 10479
506e2710
RH
10480 add_stmt (top);
10481 }
c22cacf3 10482
506e2710
RH
10483 if (body)
10484 add_stmt (body);
10485 if (clab)
10486 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10487 if (incr)
10488 add_stmt (incr);
10489 if (entry)
10490 add_stmt (entry);
10491 if (exit)
10492 add_stmt (exit);
10493 if (blab)
10494 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 10495}
325c3691
RH
10496
10497tree
c2255bc4 10498c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 10499{
089efaa4 10500 bool skip;
506e2710 10501 tree label = *label_p;
325c3691 10502
089efaa4
ILT
10503 /* In switch statements break is sometimes stylistically used after
10504 a return statement. This can lead to spurious warnings about
10505 control reaching the end of a non-void function when it is
10506 inlined. Note that we are calling block_may_fallthru with
10507 language specific tree nodes; this works because
10508 block_may_fallthru returns true when given something it does not
10509 understand. */
10510 skip = !block_may_fallthru (cur_stmt_list);
10511
506e2710 10512 if (!label)
089efaa4
ILT
10513 {
10514 if (!skip)
c2255bc4 10515 *label_p = label = create_artificial_label (loc);
089efaa4 10516 }
953ff289
DN
10517 else if (TREE_CODE (label) == LABEL_DECL)
10518 ;
10519 else switch (TREE_INT_CST_LOW (label))
506e2710 10520 {
953ff289 10521 case 0:
506e2710 10522 if (is_break)
c2255bc4 10523 error_at (loc, "break statement not within loop or switch");
506e2710 10524 else
c2255bc4 10525 error_at (loc, "continue statement not within a loop");
506e2710 10526 return NULL_TREE;
953ff289
DN
10527
10528 case 1:
10529 gcc_assert (is_break);
c2255bc4 10530 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
10531 return NULL_TREE;
10532
c02065fc
AH
10533 case 2:
10534 if (is_break)
10535 error ("break statement within %<#pragma simd%> loop body");
10536 else
10537 error ("continue statement within %<#pragma simd%> loop body");
10538 return NULL_TREE;
10539
953ff289
DN
10540 default:
10541 gcc_unreachable ();
506e2710 10542 }
325c3691 10543
089efaa4
ILT
10544 if (skip)
10545 return NULL_TREE;
10546
2e28e797
JH
10547 if (!is_break)
10548 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10549
53fb4de3 10550 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
10551}
10552
506e2710 10553/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
10554
10555static void
c2255bc4 10556emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 10557{
e6b5a630
RH
10558 if (expr == error_mark_node)
10559 ;
10560 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
10561 {
10562 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 10563 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 10564 }
789eadcd
MP
10565 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10566 {
10567 tree r = expr;
10568 location_t cloc = loc;
10569 while (TREE_CODE (r) == COMPOUND_EXPR)
10570 {
10571 if (EXPR_HAS_LOCATION (r))
10572 cloc = EXPR_LOCATION (r);
10573 r = TREE_OPERAND (r, 1);
10574 }
10575 if (!TREE_SIDE_EFFECTS (r)
10576 && !VOID_TYPE_P (TREE_TYPE (r))
10577 && !CONVERT_EXPR_P (r)
cc28fc7f 10578 && !TREE_NO_WARNING (r)
789eadcd
MP
10579 && !TREE_NO_WARNING (expr))
10580 warning_at (cloc, OPT_Wunused_value,
10581 "right-hand operand of comma expression has no effect");
10582 }
27f33b15 10583 else
c2255bc4 10584 warn_if_unused_value (expr, loc);
3a5b9284
RH
10585}
10586
506e2710 10587/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
10588 diagnostics, but do not call ADD_STMT. LOC is the location of the
10589 statement. */
3a5b9284 10590
506e2710 10591tree
c2255bc4 10592c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 10593{
056928b2
JJ
10594 tree exprv;
10595
3a5b9284 10596 if (!expr)
506e2710 10597 return NULL_TREE;
3a5b9284 10598
928c19bb
JM
10599 expr = c_fully_fold (expr, false, NULL);
10600
3a5b9284
RH
10601 if (warn_sequence_point)
10602 verify_sequence_points (expr);
10603
10604 if (TREE_TYPE (expr) != error_mark_node
10605 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10606 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 10607 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
10608
10609 /* If we're not processing a statement expression, warn about unused values.
10610 Warnings for statement expressions will be emitted later, once we figure
10611 out which is the result. */
10612 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 10613 && warn_unused_value)
d06f8b75 10614 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
3a5b9284 10615
056928b2
JJ
10616 exprv = expr;
10617 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10618 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
10619 while (CONVERT_EXPR_P (exprv))
10620 exprv = TREE_OPERAND (exprv, 0);
10621 if (DECL_P (exprv)
10622 || handled_component_p (exprv)
10623 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 10624 mark_exp_read (exprv);
fa8351f8 10625
3a5b9284
RH
10626 /* If the expression is not of a type to which we cannot assign a line
10627 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 10628 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
10629 {
10630 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10631 SET_EXPR_LOCATION (expr, loc);
10632 }
506e2710
RH
10633
10634 return expr;
10635}
10636
c2255bc4
AH
10637/* Emit an expression as a statement. LOC is the location of the
10638 expression. */
506e2710
RH
10639
10640tree
c2255bc4 10641c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
10642{
10643 if (expr)
c2255bc4 10644 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
10645 else
10646 return NULL;
3a5b9284
RH
10647}
10648
10649/* Do the opposite and emit a statement as an expression. To begin,
10650 create a new binding level and return it. */
325c3691
RH
10651
10652tree
10653c_begin_stmt_expr (void)
10654{
10655 tree ret;
10656
10657 /* We must force a BLOCK for this level so that, if it is not expanded
10658 later, there is a way to turn off the entire subtree of blocks that
10659 are contained in it. */
10660 keep_next_level ();
10661 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10662
10663 c_bindings_start_stmt_expr (c_switch_stack == NULL
10664 ? NULL
10665 : c_switch_stack->bindings);
325c3691
RH
10666
10667 /* Mark the current statement list as belonging to a statement list. */
10668 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10669
10670 return ret;
10671}
10672
c2255bc4
AH
10673/* LOC is the location of the compound statement to which this body
10674 belongs. */
10675
325c3691 10676tree
c2255bc4 10677c_finish_stmt_expr (location_t loc, tree body)
325c3691 10678{
3a5b9284 10679 tree last, type, tmp, val;
325c3691
RH
10680 tree *last_p;
10681
c2255bc4 10682 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10683
10684 c_bindings_end_stmt_expr (c_switch_stack == NULL
10685 ? NULL
10686 : c_switch_stack->bindings);
325c3691 10687
3a5b9284
RH
10688 /* Locate the last statement in BODY. See c_end_compound_stmt
10689 about always returning a BIND_EXPR. */
10690 last_p = &BIND_EXPR_BODY (body);
10691 last = BIND_EXPR_BODY (body);
10692
10693 continue_searching:
325c3691
RH
10694 if (TREE_CODE (last) == STATEMENT_LIST)
10695 {
3a5b9284
RH
10696 tree_stmt_iterator i;
10697
10698 /* This can happen with degenerate cases like ({ }). No value. */
10699 if (!TREE_SIDE_EFFECTS (last))
10700 return body;
10701
10702 /* If we're supposed to generate side effects warnings, process
10703 all of the statements except the last. */
27f33b15 10704 if (warn_unused_value)
325c3691 10705 {
3a5b9284 10706 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10707 {
10708 location_t tloc;
10709 tree t = tsi_stmt (i);
10710
10711 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10712 emit_side_effect_warnings (tloc, t);
10713 }
325c3691
RH
10714 }
10715 else
3a5b9284
RH
10716 i = tsi_last (last);
10717 last_p = tsi_stmt_ptr (i);
10718 last = *last_p;
325c3691
RH
10719 }
10720
3a5b9284
RH
10721 /* If the end of the list is exception related, then the list was split
10722 by a call to push_cleanup. Continue searching. */
10723 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10724 || TREE_CODE (last) == TRY_CATCH_EXPR)
10725 {
10726 last_p = &TREE_OPERAND (last, 0);
10727 last = *last_p;
10728 goto continue_searching;
10729 }
10730
26d8af35
JM
10731 if (last == error_mark_node)
10732 return last;
10733
3a5b9284
RH
10734 /* In the case that the BIND_EXPR is not necessary, return the
10735 expression out from inside it. */
26d8af35
JM
10736 if (last == BIND_EXPR_BODY (body)
10737 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10738 {
928c19bb
JM
10739 /* Even if this looks constant, do not allow it in a constant
10740 expression. */
e5a94231 10741 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10742 /* Do not warn if the return value of a statement expression is
10743 unused. */
928c19bb 10744 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10745 return last;
10746 }
325c3691
RH
10747
10748 /* Extract the type of said expression. */
10749 type = TREE_TYPE (last);
325c3691 10750
3a5b9284
RH
10751 /* If we're not returning a value at all, then the BIND_EXPR that
10752 we already have is a fine expression to return. */
10753 if (!type || VOID_TYPE_P (type))
10754 return body;
10755
10756 /* Now that we've located the expression containing the value, it seems
10757 silly to make voidify_wrapper_expr repeat the process. Create a
10758 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10759 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10760
10761 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10762 tree_expr_nonnegative_p giving up immediately. */
10763 val = last;
10764 if (TREE_CODE (val) == NOP_EXPR
10765 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10766 val = TREE_OPERAND (val, 0);
10767
53fb4de3 10768 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10769 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10770
c2255bc4
AH
10771 {
10772 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10773 SET_EXPR_LOCATION (t, loc);
10774 return t;
10775 }
325c3691
RH
10776}
10777\f
10778/* Begin and end compound statements. This is as simple as pushing
10779 and popping new statement lists from the tree. */
10780
10781tree
10782c_begin_compound_stmt (bool do_scope)
10783{
10784 tree stmt = push_stmt_list ();
10785 if (do_scope)
4dfa0342 10786 push_scope ();
325c3691
RH
10787 return stmt;
10788}
10789
c2255bc4
AH
10790/* End a compound statement. STMT is the statement. LOC is the
10791 location of the compound statement-- this is usually the location
10792 of the opening brace. */
10793
325c3691 10794tree
c2255bc4 10795c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10796{
10797 tree block = NULL;
10798
10799 if (do_scope)
10800 {
10801 if (c_dialect_objc ())
10802 objc_clear_super_receiver ();
10803 block = pop_scope ();
10804 }
10805
10806 stmt = pop_stmt_list (stmt);
c2255bc4 10807 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10808
10809 /* If this compound statement is nested immediately inside a statement
10810 expression, then force a BIND_EXPR to be created. Otherwise we'll
10811 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10812 STATEMENT_LISTs merge, and thus we can lose track of what statement
10813 was really last. */
38e01f9e 10814 if (building_stmt_list_p ()
325c3691
RH
10815 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10816 && TREE_CODE (stmt) != BIND_EXPR)
10817 {
53fb4de3 10818 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10819 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10820 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10821 }
10822
10823 return stmt;
10824}
5a508662
RH
10825
10826/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10827 when the current scope is exited. EH_ONLY is true when this is not
10828 meant to apply to normal control flow transfer. */
10829
10830void
c2255bc4 10831push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10832{
3a5b9284
RH
10833 enum tree_code code;
10834 tree stmt, list;
10835 bool stmt_expr;
10836
10837 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10838 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10839 add_stmt (stmt);
3a5b9284
RH
10840 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10841 list = push_stmt_list ();
10842 TREE_OPERAND (stmt, 0) = list;
10843 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10844}
325c3691 10845\f
9f47c7e5
IE
10846/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10847 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10848
10849static tree
10850build_vec_cmp (tree_code code, tree type,
10851 tree arg0, tree arg1)
10852{
10853 tree zero_vec = build_zero_cst (type);
10854 tree minus_one_vec = build_minus_one_cst (type);
10855 tree cmp_type = build_same_sized_truth_vector_type (type);
10856 tree cmp = build2 (code, cmp_type, arg0, arg1);
10857 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10858}
10859
3e4093b6
RS
10860/* Build a binary-operation expression without default conversions.
10861 CODE is the kind of expression to build.
ba47d38d 10862 LOCATION is the operator's location.
3e4093b6
RS
10863 This function differs from `build' in several ways:
10864 the data type of the result is computed and recorded in it,
10865 warnings are generated if arg data types are invalid,
10866 special handling for addition and subtraction of pointers is known,
10867 and some optimization is done (operations on narrow ints
10868 are done in the narrower type when that gives the same result).
10869 Constant folding is also done before the result is returned.
de520661 10870
3e4093b6
RS
10871 Note that the operands will never have enumeral types, or function
10872 or array types, because either they will have the default conversions
10873 performed or they have both just been converted to some other type in which
10874 the arithmetic is to be done. */
10875
10876tree
ba47d38d 10877build_binary_op (location_t location, enum tree_code code,
30af3a2b 10878 tree orig_op0, tree orig_op1, bool convert_p)
de520661 10879{
8ce94e44
JM
10880 tree type0, type1, orig_type0, orig_type1;
10881 tree eptype;
3e4093b6
RS
10882 enum tree_code code0, code1;
10883 tree op0, op1;
c9f9eb5d 10884 tree ret = error_mark_node;
4de67c26 10885 const char *invalid_op_diag;
4d84fe7c 10886 bool op0_int_operands, op1_int_operands;
928c19bb 10887 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10888
3e4093b6
RS
10889 /* Expression code to give to the expression when it is built.
10890 Normally this is CODE, which is what the caller asked for,
10891 but in some special cases we change it. */
10892 enum tree_code resultcode = code;
8b6a5902 10893
3e4093b6
RS
10894 /* Data type in which the computation is to be performed.
10895 In the simplest cases this is the common type of the arguments. */
10896 tree result_type = NULL;
10897
8ce94e44
JM
10898 /* When the computation is in excess precision, the type of the
10899 final EXCESS_PRECISION_EXPR. */
2d2e923f 10900 tree semantic_result_type = NULL;
8ce94e44 10901
3e4093b6
RS
10902 /* Nonzero means operands have already been type-converted
10903 in whatever way is necessary.
10904 Zero means they need to be converted to RESULT_TYPE. */
10905 int converted = 0;
10906
10907 /* Nonzero means create the expression with this type, rather than
10908 RESULT_TYPE. */
3fa8871b 10909 tree build_type = NULL_TREE;
3e4093b6
RS
10910
10911 /* Nonzero means after finally constructing the expression
10912 convert it to this type. */
3fa8871b 10913 tree final_type = NULL_TREE;
3e4093b6
RS
10914
10915 /* Nonzero if this is an operation like MIN or MAX which can
10916 safely be computed in short if both args are promoted shorts.
10917 Also implies COMMON.
10918 -1 indicates a bitwise operation; this makes a difference
10919 in the exact conditions for when it is safe to do the operation
10920 in a narrower mode. */
10921 int shorten = 0;
10922
10923 /* Nonzero if this is a comparison operation;
10924 if both args are promoted shorts, compare the original shorts.
10925 Also implies COMMON. */
10926 int short_compare = 0;
10927
10928 /* Nonzero if this is a right-shift operation, which can be computed on the
10929 original short and then promoted if the operand is a promoted short. */
10930 int short_shift = 0;
10931
10932 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10933 int common = 0;
10934
58393038
ZL
10935 /* True means types are compatible as far as ObjC is concerned. */
10936 bool objc_ok;
10937
8ce94e44
JM
10938 /* True means this is an arithmetic operation that may need excess
10939 precision. */
10940 bool may_need_excess_precision;
10941
180f8dbb
JM
10942 /* True means this is a boolean operation that converts both its
10943 operands to truth-values. */
10944 bool boolean_op = false;
10945
de5a5fa1
MP
10946 /* Remember whether we're doing / or %. */
10947 bool doing_div_or_mod = false;
10948
10949 /* Remember whether we're doing << or >>. */
10950 bool doing_shift = false;
10951
10952 /* Tree holding instrumentation expression. */
10953 tree instrument_expr = NULL;
10954
ba47d38d
AH
10955 if (location == UNKNOWN_LOCATION)
10956 location = input_location;
10957
4d84fe7c
JM
10958 op0 = orig_op0;
10959 op1 = orig_op1;
10960
10961 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10962 if (op0_int_operands)
10963 op0 = remove_c_maybe_const_expr (op0);
10964 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10965 if (op1_int_operands)
10966 op1 = remove_c_maybe_const_expr (op1);
10967 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10968 if (int_operands)
10969 {
10970 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10971 && TREE_CODE (orig_op1) == INTEGER_CST);
10972 int_const = (int_const_or_overflow
10973 && !TREE_OVERFLOW (orig_op0)
10974 && !TREE_OVERFLOW (orig_op1));
10975 }
10976 else
10977 int_const = int_const_or_overflow = false;
10978
0e3a99ae 10979 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e 10980 if (convert_p
31521951 10981 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
790e9490 10982 {
4d84fe7c
JM
10983 op0 = default_conversion (op0);
10984 op1 = default_conversion (op1);
790e9490
RS
10985 }
10986
36536d79
BI
10987 /* When Cilk Plus is enabled and there are array notations inside op0, then
10988 we check to see if there are builtin array notation functions. If
10989 so, then we take on the type of the array notation inside it. */
b72271b9 10990 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10991 orig_type0 = type0 = find_correct_array_notation_type (op0);
10992 else
10993 orig_type0 = type0 = TREE_TYPE (op0);
10994
b72271b9 10995 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10996 orig_type1 = type1 = find_correct_array_notation_type (op1);
10997 else
10998 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10999
3e4093b6
RS
11000 /* The expression codes of the data types of the arguments tell us
11001 whether the arguments are integers, floating, pointers, etc. */
11002 code0 = TREE_CODE (type0);
11003 code1 = TREE_CODE (type1);
11004
11005 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11006 STRIP_TYPE_NOPS (op0);
11007 STRIP_TYPE_NOPS (op1);
11008
11009 /* If an error was already reported for one of the arguments,
11010 avoid reporting another error. */
11011
11012 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11013 return error_mark_node;
11014
1807ffc1
MS
11015 if (code0 == POINTER_TYPE
11016 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11017 return error_mark_node;
11018
11019 if (code1 == POINTER_TYPE
11020 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11021 return error_mark_node;
11022
4de67c26
JM
11023 if ((invalid_op_diag
11024 = targetm.invalid_binary_op (code, type0, type1)))
11025 {
ba47d38d 11026 error_at (location, invalid_op_diag);
4de67c26
JM
11027 return error_mark_node;
11028 }
11029
8ce94e44
JM
11030 switch (code)
11031 {
11032 case PLUS_EXPR:
11033 case MINUS_EXPR:
11034 case MULT_EXPR:
11035 case TRUNC_DIV_EXPR:
11036 case CEIL_DIV_EXPR:
11037 case FLOOR_DIV_EXPR:
11038 case ROUND_DIV_EXPR:
11039 case EXACT_DIV_EXPR:
11040 may_need_excess_precision = true;
11041 break;
11042 default:
11043 may_need_excess_precision = false;
11044 break;
11045 }
11046 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11047 {
11048 op0 = TREE_OPERAND (op0, 0);
11049 type0 = TREE_TYPE (op0);
11050 }
11051 else if (may_need_excess_precision
11052 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11053 {
11054 type0 = eptype;
11055 op0 = convert (eptype, op0);
11056 }
11057 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11058 {
11059 op1 = TREE_OPERAND (op1, 0);
11060 type1 = TREE_TYPE (op1);
11061 }
11062 else if (may_need_excess_precision
11063 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11064 {
11065 type1 = eptype;
11066 op1 = convert (eptype, op1);
11067 }
11068
58393038
ZL
11069 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11070
0e3a99ae
AS
11071 /* In case when one of the operands of the binary operation is
11072 a vector and another is a scalar -- convert scalar to vector. */
11073 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11074 {
a212e43f
MG
11075 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11076 true);
f90e8e2e 11077
0e3a99ae
AS
11078 switch (convert_flag)
11079 {
11080 case stv_error:
11081 return error_mark_node;
11082 case stv_firstarg:
11083 {
11084 bool maybe_const = true;
11085 tree sc;
11086 sc = c_fully_fold (op0, false, &maybe_const);
11087 sc = save_expr (sc);
11088 sc = convert (TREE_TYPE (type1), sc);
11089 op0 = build_vector_from_val (type1, sc);
11090 if (!maybe_const)
11091 op0 = c_wrap_maybe_const (op0, true);
11092 orig_type0 = type0 = TREE_TYPE (op0);
11093 code0 = TREE_CODE (type0);
11094 converted = 1;
11095 break;
11096 }
11097 case stv_secondarg:
11098 {
11099 bool maybe_const = true;
11100 tree sc;
11101 sc = c_fully_fold (op1, false, &maybe_const);
11102 sc = save_expr (sc);
11103 sc = convert (TREE_TYPE (type0), sc);
11104 op1 = build_vector_from_val (type0, sc);
11105 if (!maybe_const)
54b9f838 11106 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
11107 orig_type1 = type1 = TREE_TYPE (op1);
11108 code1 = TREE_CODE (type1);
11109 converted = 1;
11110 break;
11111 }
11112 default:
11113 break;
11114 }
11115 }
11116
3e4093b6 11117 switch (code)
de520661 11118 {
3e4093b6
RS
11119 case PLUS_EXPR:
11120 /* Handle the pointer + int case. */
11121 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 11122 {
db3927fb 11123 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
11124 goto return_build_binary_op;
11125 }
3e4093b6 11126 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 11127 {
db3927fb 11128 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
11129 goto return_build_binary_op;
11130 }
fe67cf58 11131 else
3e4093b6
RS
11132 common = 1;
11133 break;
400fbf9f 11134
3e4093b6
RS
11135 case MINUS_EXPR:
11136 /* Subtraction of two similar pointers.
11137 We must subtract them as integers, then divide by object size. */
11138 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 11139 && comp_target_types (location, type0, type1))
c9f9eb5d 11140 {
db3927fb 11141 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
11142 goto return_build_binary_op;
11143 }
3e4093b6
RS
11144 /* Handle pointer minus int. Just like pointer plus int. */
11145 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 11146 {
db3927fb 11147 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
11148 goto return_build_binary_op;
11149 }
3e4093b6
RS
11150 else
11151 common = 1;
11152 break;
8b6a5902 11153
3e4093b6
RS
11154 case MULT_EXPR:
11155 common = 1;
11156 break;
11157
11158 case TRUNC_DIV_EXPR:
11159 case CEIL_DIV_EXPR:
11160 case FLOOR_DIV_EXPR:
11161 case ROUND_DIV_EXPR:
11162 case EXACT_DIV_EXPR:
de5a5fa1 11163 doing_div_or_mod = true;
c9f9eb5d 11164 warn_for_div_by_zero (location, op1);
3e4093b6
RS
11165
11166 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 11167 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
11168 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11169 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 11170 || code1 == FIXED_POINT_TYPE
3e4093b6 11171 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11172 {
5bed876a
AH
11173 enum tree_code tcode0 = code0, tcode1 = code1;
11174
3a021db2 11175 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 11176 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 11177 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 11178 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 11179
ab22c1fa
CF
11180 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11181 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
11182 resultcode = RDIV_EXPR;
11183 else
11184 /* Although it would be tempting to shorten always here, that
11185 loses on some targets, since the modulo instruction is
11186 undefined if the quotient can't be represented in the
11187 computation mode. We shorten only if unsigned or if
11188 dividing by something we know != -1. */
8df83eae 11189 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 11190 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 11191 && !integer_all_onesp (op1)));
3e4093b6
RS
11192 common = 1;
11193 }
11194 break;
de520661 11195
3e4093b6 11196 case BIT_AND_EXPR:
3e4093b6
RS
11197 case BIT_IOR_EXPR:
11198 case BIT_XOR_EXPR:
11199 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11200 shorten = -1;
9ef0c8d9
AP
11201 /* Allow vector types which are not floating point types. */
11202 else if (code0 == VECTOR_TYPE
11203 && code1 == VECTOR_TYPE
11204 && !VECTOR_FLOAT_TYPE_P (type0)
11205 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
11206 common = 1;
11207 break;
11208
11209 case TRUNC_MOD_EXPR:
11210 case FLOOR_MOD_EXPR:
de5a5fa1 11211 doing_div_or_mod = true;
c9f9eb5d 11212 warn_for_div_by_zero (location, op1);
de520661 11213
5cfd5d9b
AP
11214 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11215 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11216 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11217 common = 1;
11218 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
11219 {
11220 /* Although it would be tempting to shorten always here, that loses
11221 on some targets, since the modulo instruction is undefined if the
11222 quotient can't be represented in the computation mode. We shorten
11223 only if unsigned or if dividing by something we know != -1. */
8df83eae 11224 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 11225 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 11226 && !integer_all_onesp (op1)));
3e4093b6
RS
11227 common = 1;
11228 }
11229 break;
de520661 11230
3e4093b6
RS
11231 case TRUTH_ANDIF_EXPR:
11232 case TRUTH_ORIF_EXPR:
11233 case TRUTH_AND_EXPR:
11234 case TRUTH_OR_EXPR:
11235 case TRUTH_XOR_EXPR:
11236 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
11237 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11238 || code0 == FIXED_POINT_TYPE)
3e4093b6 11239 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
11240 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11241 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11242 {
11243 /* Result of these operations is always an int,
11244 but that does not mean the operands should be
11245 converted to ints! */
11246 result_type = integer_type_node;
a27d595d
JM
11247 if (op0_int_operands)
11248 {
11249 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11250 op0 = remove_c_maybe_const_expr (op0);
11251 }
11252 else
11253 op0 = c_objc_common_truthvalue_conversion (location, op0);
11254 if (op1_int_operands)
11255 {
11256 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11257 op1 = remove_c_maybe_const_expr (op1);
11258 }
11259 else
11260 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 11261 converted = 1;
180f8dbb 11262 boolean_op = true;
3e4093b6 11263 }
928c19bb
JM
11264 if (code == TRUTH_ANDIF_EXPR)
11265 {
11266 int_const_or_overflow = (int_operands
11267 && TREE_CODE (orig_op0) == INTEGER_CST
11268 && (op0 == truthvalue_false_node
11269 || TREE_CODE (orig_op1) == INTEGER_CST));
11270 int_const = (int_const_or_overflow
11271 && !TREE_OVERFLOW (orig_op0)
11272 && (op0 == truthvalue_false_node
11273 || !TREE_OVERFLOW (orig_op1)));
11274 }
11275 else if (code == TRUTH_ORIF_EXPR)
11276 {
11277 int_const_or_overflow = (int_operands
11278 && TREE_CODE (orig_op0) == INTEGER_CST
11279 && (op0 == truthvalue_true_node
11280 || TREE_CODE (orig_op1) == INTEGER_CST));
11281 int_const = (int_const_or_overflow
11282 && !TREE_OVERFLOW (orig_op0)
11283 && (op0 == truthvalue_true_node
11284 || !TREE_OVERFLOW (orig_op1)));
11285 }
3e4093b6 11286 break;
eba80994 11287
3e4093b6
RS
11288 /* Shift operations: result has same type as first operand;
11289 always convert second operand to int.
11290 Also set SHORT_SHIFT if shifting rightward. */
de520661 11291
3e4093b6 11292 case RSHIFT_EXPR:
76e2c821
JB
11293 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11294 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11295 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11296 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
11297 {
11298 result_type = type0;
11299 converted = 1;
11300 }
76e2c821
JB
11301 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11302 || code0 == VECTOR_TYPE)
451b5e48 11303 && code1 == INTEGER_TYPE)
3e4093b6 11304 {
de5a5fa1 11305 doing_shift = true;
928c19bb 11306 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 11307 {
3e4093b6 11308 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11309 {
11310 int_const = false;
7d882b83 11311 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11312 warning_at (location, OPT_Wshift_count_negative,
11313 "right shift count is negative");
928c19bb 11314 }
76e2c821
JB
11315 else if (code0 == VECTOR_TYPE)
11316 {
11317 if (compare_tree_int (op1,
11318 TYPE_PRECISION (TREE_TYPE (type0)))
11319 >= 0)
11320 {
11321 int_const = false;
11322 if (c_inhibit_evaluation_warnings == 0)
11323 warning_at (location, OPT_Wshift_count_overflow,
11324 "right shift count >= width of vector element");
11325 }
11326 }
3e4093b6 11327 else
bbb818c6 11328 {
3f75a254 11329 if (!integer_zerop (op1))
3e4093b6
RS
11330 short_shift = 1;
11331
11332 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11333 {
11334 int_const = false;
7d882b83 11335 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11336 warning_at (location, OPT_Wshift_count_overflow,
11337 "right shift count >= width of type");
928c19bb 11338 }
bbb818c6 11339 }
b62acd60 11340 }
de520661 11341
3e4093b6
RS
11342 /* Use the type of the value to be shifted. */
11343 result_type = type0;
3e4093b6
RS
11344 /* Avoid converting op1 to result_type later. */
11345 converted = 1;
400fbf9f 11346 }
3e4093b6 11347 break;
253b6b82 11348
3e4093b6 11349 case LSHIFT_EXPR:
76e2c821
JB
11350 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11351 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11352 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11353 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
11354 {
11355 result_type = type0;
11356 converted = 1;
11357 }
76e2c821
JB
11358 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11359 || code0 == VECTOR_TYPE)
451b5e48 11360 && code1 == INTEGER_TYPE)
3e4093b6 11361 {
de5a5fa1 11362 doing_shift = true;
0173bd2a
MP
11363 if (TREE_CODE (op0) == INTEGER_CST
11364 && tree_int_cst_sgn (op0) < 0)
11365 {
11366 /* Don't reject a left shift of a negative value in a context
11367 where a constant expression is needed in C90. */
11368 if (flag_isoc99)
11369 int_const = false;
11370 if (c_inhibit_evaluation_warnings == 0)
11371 warning_at (location, OPT_Wshift_negative_value,
11372 "left shift of negative value");
11373 }
928c19bb 11374 if (TREE_CODE (op1) == INTEGER_CST)
de520661 11375 {
3e4093b6 11376 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11377 {
11378 int_const = false;
7d882b83 11379 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11380 warning_at (location, OPT_Wshift_count_negative,
11381 "left shift count is negative");
928c19bb 11382 }
76e2c821
JB
11383 else if (code0 == VECTOR_TYPE)
11384 {
11385 if (compare_tree_int (op1,
11386 TYPE_PRECISION (TREE_TYPE (type0)))
11387 >= 0)
11388 {
11389 int_const = false;
11390 if (c_inhibit_evaluation_warnings == 0)
11391 warning_at (location, OPT_Wshift_count_overflow,
11392 "left shift count >= width of vector element");
11393 }
11394 }
3e4093b6 11395 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11396 {
11397 int_const = false;
7d882b83 11398 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11399 warning_at (location, OPT_Wshift_count_overflow,
11400 "left shift count >= width of type");
928c19bb 11401 }
451b5e48
MP
11402 else if (TREE_CODE (op0) == INTEGER_CST
11403 && maybe_warn_shift_overflow (location, op0, op1)
11404 && flag_isoc99)
11405 int_const = false;
94ba5069 11406 }
de520661 11407
3e4093b6
RS
11408 /* Use the type of the value to be shifted. */
11409 result_type = type0;
3e4093b6
RS
11410 /* Avoid converting op1 to result_type later. */
11411 converted = 1;
400fbf9f 11412 }
3e4093b6 11413 break;
de520661 11414
3e4093b6
RS
11415 case EQ_EXPR:
11416 case NE_EXPR:
d246ab4f
AS
11417 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11418 {
11419 tree intt;
0af94e6f 11420 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11421 {
11422 error_at (location, "comparing vectors with different "
11423 "element types");
11424 return error_mark_node;
11425 }
11426
11427 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11428 {
11429 error_at (location, "comparing vectors with different "
11430 "number of elements");
11431 return error_mark_node;
11432 }
11433
fa74a4bc
MP
11434 /* It's not precisely specified how the usual arithmetic
11435 conversions apply to the vector types. Here, we use
11436 the unsigned type if one of the operands is signed and
11437 the other one is unsigned. */
11438 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11439 {
11440 if (!TYPE_UNSIGNED (type0))
11441 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11442 else
11443 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11444 warning_at (location, OPT_Wsign_compare, "comparison between "
11445 "types %qT and %qT", type0, type1);
11446 }
11447
d246ab4f
AS
11448 /* Always construct signed integer vector type. */
11449 intt = c_common_type_for_size (GET_MODE_BITSIZE
b397965c
RS
11450 (SCALAR_TYPE_MODE
11451 (TREE_TYPE (type0))), 0);
d246ab4f
AS
11452 result_type = build_opaque_vector_type (intt,
11453 TYPE_VECTOR_SUBPARTS (type0));
11454 converted = 1;
9f47c7e5
IE
11455 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11456 goto return_build_binary_op;
d246ab4f 11457 }
ae311566 11458 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
11459 warning_at (location,
11460 OPT_Wfloat_equal,
11461 "comparing floating point with == or != is unsafe");
3e4093b6
RS
11462 /* Result of comparison is always int,
11463 but don't convert the args to int! */
11464 build_type = integer_type_node;
11465 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 11466 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 11467 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 11468 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 11469 short_compare = 1;
637f1455
SZ
11470 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11471 {
11472 if (TREE_CODE (op0) == ADDR_EXPR
de55efd5
MP
11473 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11474 && !from_macro_expansion_at (location))
637f1455
SZ
11475 {
11476 if (code == EQ_EXPR)
11477 warning_at (location,
11478 OPT_Waddress,
11479 "the comparison will always evaluate as %<false%> "
11480 "for the address of %qD will never be NULL",
11481 TREE_OPERAND (op0, 0));
11482 else
11483 warning_at (location,
11484 OPT_Waddress,
11485 "the comparison will always evaluate as %<true%> "
11486 "for the address of %qD will never be NULL",
11487 TREE_OPERAND (op0, 0));
11488 }
11489 result_type = type0;
11490 }
11491 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11492 {
11493 if (TREE_CODE (op1) == ADDR_EXPR
de55efd5
MP
11494 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11495 && !from_macro_expansion_at (location))
637f1455
SZ
11496 {
11497 if (code == EQ_EXPR)
11498 warning_at (location,
f90e8e2e 11499 OPT_Waddress,
637f1455
SZ
11500 "the comparison will always evaluate as %<false%> "
11501 "for the address of %qD will never be NULL",
11502 TREE_OPERAND (op1, 0));
11503 else
11504 warning_at (location,
11505 OPT_Waddress,
11506 "the comparison will always evaluate as %<true%> "
11507 "for the address of %qD will never be NULL",
11508 TREE_OPERAND (op1, 0));
11509 }
11510 result_type = type1;
11511 }
3e4093b6
RS
11512 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11513 {
11514 tree tt0 = TREE_TYPE (type0);
11515 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
11516 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11517 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11518 addr_space_t as_common = ADDR_SPACE_GENERIC;
11519
3e4093b6
RS
11520 /* Anything compares with void *. void * compares with anything.
11521 Otherwise, the targets must be compatible
11522 and both must be object or both incomplete. */
744aa42f 11523 if (comp_target_types (location, type0, type1))
10bc1b1b 11524 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
11525 else if (!addr_space_superset (as0, as1, &as_common))
11526 {
11527 error_at (location, "comparison of pointers to "
11528 "disjoint address spaces");
11529 return error_mark_node;
11530 }
267bac10 11531 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 11532 {
36c5e70a 11533 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 11534 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11535 "comparison of %<void *%> with function pointer");
ee2990e7 11536 }
267bac10 11537 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 11538 {
36c5e70a 11539 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 11540 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11541 "comparison of %<void *%> with function pointer");
e6834654 11542 }
3e4093b6 11543 else
58393038
ZL
11544 /* Avoid warning about the volatile ObjC EH puts on decls. */
11545 if (!objc_ok)
ba47d38d 11546 pedwarn (location, 0,
509c9d60 11547 "comparison of distinct pointer types lacks a cast");
e6834654 11548
3e4093b6 11549 if (result_type == NULL_TREE)
36c5e70a
BE
11550 {
11551 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11552 result_type = build_pointer_type
11553 (build_qualified_type (void_type_node, qual));
11554 }
e6834654 11555 }
3e4093b6 11556 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 11557 {
3e4093b6 11558 result_type = type0;
ba47d38d 11559 pedwarn (location, 0, "comparison between pointer and integer");
de520661 11560 }
3e4093b6 11561 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 11562 {
3e4093b6 11563 result_type = type1;
ba47d38d 11564 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 11565 }
04159acf
MP
11566 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11567 || truth_value_p (TREE_CODE (orig_op0)))
11568 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11569 || truth_value_p (TREE_CODE (orig_op1))))
11570 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11571 break;
8b6a5902 11572
3e4093b6
RS
11573 case LE_EXPR:
11574 case GE_EXPR:
11575 case LT_EXPR:
11576 case GT_EXPR:
d246ab4f
AS
11577 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11578 {
11579 tree intt;
0af94e6f 11580 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11581 {
11582 error_at (location, "comparing vectors with different "
11583 "element types");
11584 return error_mark_node;
11585 }
11586
11587 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11588 {
11589 error_at (location, "comparing vectors with different "
11590 "number of elements");
11591 return error_mark_node;
11592 }
11593
fa74a4bc
MP
11594 /* It's not precisely specified how the usual arithmetic
11595 conversions apply to the vector types. Here, we use
11596 the unsigned type if one of the operands is signed and
11597 the other one is unsigned. */
11598 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11599 {
11600 if (!TYPE_UNSIGNED (type0))
11601 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11602 else
11603 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11604 warning_at (location, OPT_Wsign_compare, "comparison between "
11605 "types %qT and %qT", type0, type1);
11606 }
11607
d246ab4f
AS
11608 /* Always construct signed integer vector type. */
11609 intt = c_common_type_for_size (GET_MODE_BITSIZE
b397965c
RS
11610 (SCALAR_TYPE_MODE
11611 (TREE_TYPE (type0))), 0);
d246ab4f
AS
11612 result_type = build_opaque_vector_type (intt,
11613 TYPE_VECTOR_SUBPARTS (type0));
11614 converted = 1;
9f47c7e5
IE
11615 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11616 goto return_build_binary_op;
d246ab4f 11617 }
3e4093b6 11618 build_type = integer_type_node;
ab22c1fa
CF
11619 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11620 || code0 == FIXED_POINT_TYPE)
11621 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11622 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11623 short_compare = 1;
11624 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11625 {
36c5e70a
BE
11626 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11627 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11628 addr_space_t as_common;
11629
744aa42f 11630 if (comp_target_types (location, type0, type1))
3e4093b6 11631 {
10bc1b1b 11632 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
11633 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11634 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 11635 pedwarn (location, 0,
509c9d60 11636 "comparison of complete and incomplete pointers");
fcf73884 11637 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 11638 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11639 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
11640 else if (null_pointer_constant_p (orig_op0)
11641 || null_pointer_constant_p (orig_op1))
11642 warning_at (location, OPT_Wextra,
11643 "ordered comparison of pointer with null pointer");
11644
3e4093b6 11645 }
36c5e70a
BE
11646 else if (!addr_space_superset (as0, as1, &as_common))
11647 {
11648 error_at (location, "comparison of pointers to "
11649 "disjoint address spaces");
11650 return error_mark_node;
11651 }
3e4093b6
RS
11652 else
11653 {
36c5e70a
BE
11654 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11655 result_type = build_pointer_type
11656 (build_qualified_type (void_type_node, qual));
ba47d38d 11657 pedwarn (location, 0,
509c9d60 11658 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
11659 }
11660 }
6aa3c60d 11661 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
11662 {
11663 result_type = type0;
fcf73884 11664 if (pedantic)
c1771a20 11665 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
11666 "ordered comparison of pointer with integer zero");
11667 else if (extra_warnings)
ba47d38d 11668 warning_at (location, OPT_Wextra,
d42ba3b8 11669 "ordered comparison of pointer with integer zero");
3e4093b6 11670 }
6aa3c60d 11671 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
11672 {
11673 result_type = type1;
d42ba3b8 11674 if (pedantic)
c1771a20 11675 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
11676 "ordered comparison of pointer with integer zero");
11677 else if (extra_warnings)
11678 warning_at (location, OPT_Wextra,
11679 "ordered comparison of pointer with integer zero");
3e4093b6
RS
11680 }
11681 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11682 {
11683 result_type = type0;
ba47d38d 11684 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
11685 }
11686 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11687 {
11688 result_type = type1;
ba47d38d 11689 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 11690 }
04159acf
MP
11691 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11692 || truth_value_p (TREE_CODE (orig_op0)))
11693 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11694 || truth_value_p (TREE_CODE (orig_op1))))
11695 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11696 break;
64094f6a 11697
3e4093b6 11698 default:
37b2f290 11699 gcc_unreachable ();
c9fe6f9f 11700 }
8f17b5c5 11701
e57e265b
PB
11702 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11703 return error_mark_node;
11704
5bed876a
AH
11705 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11706 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 11707 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 11708 {
745e411d
DM
11709 gcc_rich_location richloc (location);
11710 richloc.maybe_add_expr (orig_op0);
11711 richloc.maybe_add_expr (orig_op1);
11712 binary_op_error (&richloc, code, type0, type1);
5bed876a
AH
11713 return error_mark_node;
11714 }
11715
3e4093b6 11716 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 11717 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
11718 &&
11719 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 11720 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11721 {
2ca862e9
JM
11722 bool first_complex = (code0 == COMPLEX_TYPE);
11723 bool second_complex = (code1 == COMPLEX_TYPE);
11724 int none_complex = (!first_complex && !second_complex);
39b726dd 11725
3e4093b6 11726 if (shorten || common || short_compare)
3bf6bfcc
JJ
11727 {
11728 result_type = c_common_type (type0, type1);
0a0b3574
MM
11729 do_warn_double_promotion (result_type, type0, type1,
11730 "implicit conversion from %qT to %qT "
11731 "to match other operand of binary "
11732 "expression",
11733 location);
3bf6bfcc
JJ
11734 if (result_type == error_mark_node)
11735 return error_mark_node;
11736 }
400fbf9f 11737
2ca862e9
JM
11738 if (first_complex != second_complex
11739 && (code == PLUS_EXPR
11740 || code == MINUS_EXPR
11741 || code == MULT_EXPR
11742 || (code == TRUNC_DIV_EXPR && first_complex))
11743 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11744 && flag_signed_zeros)
11745 {
11746 /* An operation on mixed real/complex operands must be
11747 handled specially, but the language-independent code can
11748 more easily optimize the plain complex arithmetic if
11749 -fno-signed-zeros. */
11750 tree real_type = TREE_TYPE (result_type);
11751 tree real, imag;
11752 if (type0 != orig_type0 || type1 != orig_type1)
11753 {
11754 gcc_assert (may_need_excess_precision && common);
2d2e923f 11755 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11756 }
11757 if (first_complex)
11758 {
11759 if (TREE_TYPE (op0) != result_type)
68fca595 11760 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11761 if (TREE_TYPE (op1) != real_type)
68fca595 11762 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11763 }
11764 else
11765 {
11766 if (TREE_TYPE (op0) != real_type)
68fca595 11767 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11768 if (TREE_TYPE (op1) != result_type)
68fca595 11769 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11770 }
11771 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11772 return error_mark_node;
11773 if (first_complex)
11774 {
b2fa0a8b 11775 op0 = save_expr (op0);
2ca862e9 11776 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
e51fbec3 11777 op0, true);
2ca862e9 11778 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
e51fbec3 11779 op0, true);
2ca862e9
JM
11780 switch (code)
11781 {
11782 case MULT_EXPR:
11783 case TRUNC_DIV_EXPR:
b2fa0a8b 11784 op1 = save_expr (op1);
2ca862e9
JM
11785 imag = build2 (resultcode, real_type, imag, op1);
11786 /* Fall through. */
11787 case PLUS_EXPR:
11788 case MINUS_EXPR:
11789 real = build2 (resultcode, real_type, real, op1);
11790 break;
11791 default:
11792 gcc_unreachable();
11793 }
11794 }
11795 else
11796 {
b2fa0a8b 11797 op1 = save_expr (op1);
2ca862e9 11798 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
e51fbec3 11799 op1, true);
2ca862e9 11800 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
e51fbec3 11801 op1, true);
2ca862e9
JM
11802 switch (code)
11803 {
11804 case MULT_EXPR:
b2fa0a8b 11805 op0 = save_expr (op0);
2ca862e9
JM
11806 imag = build2 (resultcode, real_type, op0, imag);
11807 /* Fall through. */
11808 case PLUS_EXPR:
11809 real = build2 (resultcode, real_type, op0, real);
11810 break;
11811 case MINUS_EXPR:
11812 real = build2 (resultcode, real_type, op0, real);
11813 imag = build1 (NEGATE_EXPR, real_type, imag);
11814 break;
11815 default:
11816 gcc_unreachable();
11817 }
11818 }
11819 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11820 goto return_build_binary_op;
11821 }
11822
3e4093b6
RS
11823 /* For certain operations (which identify themselves by shorten != 0)
11824 if both args were extended from the same smaller type,
11825 do the arithmetic in that type and then extend.
400fbf9f 11826
3e4093b6
RS
11827 shorten !=0 and !=1 indicates a bitwise operation.
11828 For them, this optimization is safe only if
11829 both args are zero-extended or both are sign-extended.
11830 Otherwise, we might change the result.
11831 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11832 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11833
3e4093b6
RS
11834 if (shorten && none_complex)
11835 {
3e4093b6 11836 final_type = result_type;
b8698a0f 11837 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11838 shorten == -1);
3e4093b6 11839 }
88a3dbc1 11840
3e4093b6 11841 /* Shifts can be shortened if shifting right. */
2f6e4e97 11842
3e4093b6
RS
11843 if (short_shift)
11844 {
11845 int unsigned_arg;
11846 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11847
3e4093b6 11848 final_type = result_type;
abe80e6d 11849
3e4093b6 11850 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11851 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11852
3e4093b6 11853 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11854 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11855 /* We can shorten only if the shift count is less than the
11856 number of bits in the smaller type size. */
11857 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11858 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11859 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11860 {
11861 /* Do an unsigned shift if the operand was zero-extended. */
11862 result_type
11863 = c_common_signed_or_unsigned_type (unsigned_arg,
11864 TREE_TYPE (arg0));
11865 /* Convert value-to-be-shifted to that type. */
11866 if (TREE_TYPE (op0) != result_type)
11867 op0 = convert (result_type, op0);
11868 converted = 1;
abe80e6d 11869 }
88a3dbc1
RK
11870 }
11871
3e4093b6
RS
11872 /* Comparison operations are shortened too but differently.
11873 They identify themselves by setting short_compare = 1. */
56cb9733 11874
3e4093b6
RS
11875 if (short_compare)
11876 {
11877 /* Don't write &op0, etc., because that would prevent op0
11878 from being kept in a register.
11879 Instead, make copies of the our local variables and
11880 pass the copies by reference, then copy them back afterward. */
11881 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11882 enum tree_code xresultcode = resultcode;
11883 tree val
393e8e8b
MP
11884 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11885 &xresultcode);
8f17b5c5 11886
3fa8871b 11887 if (val != NULL_TREE)
c9f9eb5d
AH
11888 {
11889 ret = val;
11890 goto return_build_binary_op;
11891 }
8f17b5c5 11892
3e4093b6
RS
11893 op0 = xop0, op1 = xop1;
11894 converted = 1;
11895 resultcode = xresultcode;
8f17b5c5 11896
7d882b83 11897 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11898 {
11899 bool op0_maybe_const = true;
11900 bool op1_maybe_const = true;
11901 tree orig_op0_folded, orig_op1_folded;
11902
11903 if (in_late_binary_op)
11904 {
11905 orig_op0_folded = orig_op0;
11906 orig_op1_folded = orig_op1;
11907 }
11908 else
11909 {
11910 /* Fold for the sake of possible warnings, as in
11911 build_conditional_expr. This requires the
11912 "original" values to be folded, not just op0 and
11913 op1. */
f5178456 11914 c_inhibit_evaluation_warnings++;
928c19bb
JM
11915 op0 = c_fully_fold (op0, require_constant_value,
11916 &op0_maybe_const);
11917 op1 = c_fully_fold (op1, require_constant_value,
11918 &op1_maybe_const);
f5178456 11919 c_inhibit_evaluation_warnings--;
928c19bb
JM
11920 orig_op0_folded = c_fully_fold (orig_op0,
11921 require_constant_value,
11922 NULL);
11923 orig_op1_folded = c_fully_fold (orig_op1,
11924 require_constant_value,
11925 NULL);
11926 }
11927
11928 if (warn_sign_compare)
11929 warn_for_sign_compare (location, orig_op0_folded,
11930 orig_op1_folded, op0, op1,
11931 result_type, resultcode);
5c2f94b4 11932 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11933 {
11934 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11935 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11936 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11937 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11938 }
3e4093b6 11939 }
2ad1815d 11940 }
64094f6a 11941 }
64094f6a 11942
3e4093b6
RS
11943 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11944 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11945 Then the expression will be built.
11946 It will be given type FINAL_TYPE if that is nonzero;
11947 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11948
3e4093b6
RS
11949 if (!result_type)
11950 {
745e411d
DM
11951 gcc_rich_location richloc (location);
11952 richloc.maybe_add_expr (orig_op0);
11953 richloc.maybe_add_expr (orig_op1);
11954 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11955 return error_mark_node;
11956 }
400fbf9f 11957
3e4093b6 11958 if (build_type == NULL_TREE)
8ce94e44
JM
11959 {
11960 build_type = result_type;
180f8dbb
JM
11961 if ((type0 != orig_type0 || type1 != orig_type1)
11962 && !boolean_op)
8ce94e44
JM
11963 {
11964 gcc_assert (may_need_excess_precision && common);
2d2e923f 11965 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11966 }
11967 }
400fbf9f 11968
2d2e923f
MLI
11969 if (!converted)
11970 {
68fca595
MP
11971 op0 = ep_convert_and_check (location, result_type, op0,
11972 semantic_result_type);
11973 op1 = ep_convert_and_check (location, result_type, op1,
11974 semantic_result_type);
2d2e923f
MLI
11975
11976 /* This can happen if one operand has a vector type, and the other
11977 has a different type. */
11978 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11979 return error_mark_node;
11980 }
11981
45b2222a
ML
11982 if (sanitize_flags_p ((SANITIZE_SHIFT
11983 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
f34ebeb2 11984 && current_function_decl != NULL_TREE
b2aaf235
MP
11985 && (doing_div_or_mod || doing_shift)
11986 && !require_constant_value)
de5a5fa1
MP
11987 {
11988 /* OP0 and/or OP1 might have side-effects. */
b2fa0a8b
MP
11989 op0 = save_expr (op0);
11990 op1 = save_expr (op1);
de5a5fa1
MP
11991 op0 = c_fully_fold (op0, false, NULL);
11992 op1 = c_fully_fold (op1, false, NULL);
45b2222a
ML
11993 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
11994 | SANITIZE_FLOAT_DIVIDE))))
de5a5fa1 11995 instrument_expr = ubsan_instrument_division (location, op0, op1);
45b2222a 11996 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
de5a5fa1
MP
11997 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11998 }
11999
c9f9eb5d 12000 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
12001 if (int_const_or_overflow)
12002 ret = (require_constant_value
db3927fb
AH
12003 ? fold_build2_initializer_loc (location, resultcode, build_type,
12004 op0, op1)
12005 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
12006 else
12007 ret = build2 (resultcode, build_type, op0, op1);
3fa8871b 12008 if (final_type != NULL_TREE)
c9f9eb5d
AH
12009 ret = convert (final_type, ret);
12010
12011 return_build_binary_op:
12012 gcc_assert (ret != error_mark_node);
928c19bb
JM
12013 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12014 ret = (int_operands
12015 ? note_integer_operands (ret)
12016 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12017 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12018 && !in_late_binary_op)
12019 ret = note_integer_operands (ret);
c9f9eb5d 12020 protected_set_expr_location (ret, location);
de5a5fa1 12021
a24d975c 12022 if (instrument_expr != NULL)
de5a5fa1
MP
12023 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12024 instrument_expr, ret);
12025
4d1b8e70
JJ
12026 if (semantic_result_type)
12027 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12028 semantic_result_type, ret);
12029
c9f9eb5d 12030 return ret;
400fbf9f 12031}
85498824
JM
12032
12033
12034/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 12035 purpose. LOCATION is the source location for the expression. */
85498824
JM
12036
12037tree
ba47d38d 12038c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 12039{
928c19bb
JM
12040 bool int_const, int_operands;
12041
85498824
JM
12042 switch (TREE_CODE (TREE_TYPE (expr)))
12043 {
12044 case ARRAY_TYPE:
ba47d38d 12045 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
12046 return error_mark_node;
12047
12048 case RECORD_TYPE:
ba47d38d 12049 error_at (location, "used struct type value where scalar is required");
85498824
JM
12050 return error_mark_node;
12051
12052 case UNION_TYPE:
ba47d38d 12053 error_at (location, "used union type value where scalar is required");
85498824
JM
12054 return error_mark_node;
12055
04af8788
NP
12056 case VOID_TYPE:
12057 error_at (location, "void value not ignored as it ought to be");
12058 return error_mark_node;
12059
1807ffc1
MS
12060 case POINTER_TYPE:
12061 if (reject_gcc_builtin (expr))
12062 return error_mark_node;
12063 break;
12064
46bdb9cf
JM
12065 case FUNCTION_TYPE:
12066 gcc_unreachable ();
12067
d246ab4f
AS
12068 case VECTOR_TYPE:
12069 error_at (location, "used vector type where scalar is required");
12070 return error_mark_node;
12071
85498824
JM
12072 default:
12073 break;
12074 }
12075
928c19bb
JM
12076 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12077 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
12078 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12079 {
12080 expr = remove_c_maybe_const_expr (expr);
12081 expr = build2 (NE_EXPR, integer_type_node, expr,
12082 convert (TREE_TYPE (expr), integer_zero_node));
12083 expr = note_integer_operands (expr);
12084 }
12085 else
12086 /* ??? Should we also give an error for vectors rather than leaving
12087 those to give errors later? */
12088 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
12089
12090 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12091 {
12092 if (TREE_OVERFLOW (expr))
12093 return expr;
12094 else
12095 return note_integer_operands (expr);
12096 }
12097 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12098 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12099 return expr;
85498824 12100}
73f397d4
JM
12101\f
12102
12103/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12104 required. */
12105
12106tree
51eed280 12107c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
12108{
12109 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12110 {
12111 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12112 /* Executing a compound literal inside a function reinitializes
12113 it. */
12114 if (!TREE_STATIC (decl))
12115 *se = true;
12116 return decl;
12117 }
12118 else
12119 return expr;
12120}
953ff289 12121\f
88bae6f4
TS
12122/* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12123 statement. LOC is the location of the construct. */
41dbbb37
TS
12124
12125tree
88bae6f4
TS
12126c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12127 tree clauses)
41dbbb37 12128{
88bae6f4 12129 body = c_end_compound_stmt (loc, body, true);
41dbbb37 12130
88bae6f4 12131 tree stmt = make_node (code);
41dbbb37 12132 TREE_TYPE (stmt) = void_type_node;
88bae6f4
TS
12133 OMP_BODY (stmt) = body;
12134 OMP_CLAUSES (stmt) = clauses;
41dbbb37
TS
12135 SET_EXPR_LOCATION (stmt, loc);
12136
12137 return add_stmt (stmt);
12138}
12139
12140/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12141 statement. LOC is the location of the OACC_DATA. */
12142
12143tree
12144c_finish_oacc_data (location_t loc, tree clauses, tree block)
12145{
12146 tree stmt;
12147
12148 block = c_end_compound_stmt (loc, block, true);
12149
12150 stmt = make_node (OACC_DATA);
12151 TREE_TYPE (stmt) = void_type_node;
12152 OACC_DATA_CLAUSES (stmt) = clauses;
12153 OACC_DATA_BODY (stmt) = block;
12154 SET_EXPR_LOCATION (stmt, loc);
12155
12156 return add_stmt (stmt);
12157}
12158
37d5ad46
JB
12159/* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12160 statement. LOC is the location of the OACC_HOST_DATA. */
12161
12162tree
12163c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12164{
12165 tree stmt;
12166
12167 block = c_end_compound_stmt (loc, block, true);
12168
12169 stmt = make_node (OACC_HOST_DATA);
12170 TREE_TYPE (stmt) = void_type_node;
12171 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12172 OACC_HOST_DATA_BODY (stmt) = block;
12173 SET_EXPR_LOCATION (stmt, loc);
12174
12175 return add_stmt (stmt);
12176}
12177
c0220ea4 12178/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
12179
12180tree
12181c_begin_omp_parallel (void)
12182{
12183 tree block;
12184
12185 keep_next_level ();
12186 block = c_begin_compound_stmt (true);
12187
12188 return block;
12189}
12190
c2255bc4
AH
12191/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12192 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 12193
953ff289 12194tree
c2255bc4 12195c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
12196{
12197 tree stmt;
12198
c2255bc4 12199 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
12200
12201 stmt = make_node (OMP_PARALLEL);
12202 TREE_TYPE (stmt) = void_type_node;
12203 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12204 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 12205 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
12206
12207 return add_stmt (stmt);
12208}
12209
a68ab351
JJ
12210/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12211
12212tree
12213c_begin_omp_task (void)
12214{
12215 tree block;
12216
12217 keep_next_level ();
12218 block = c_begin_compound_stmt (true);
12219
12220 return block;
12221}
12222
c2255bc4
AH
12223/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12224 statement. LOC is the location of the #pragma. */
a68ab351
JJ
12225
12226tree
c2255bc4 12227c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
12228{
12229 tree stmt;
12230
c2255bc4 12231 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
12232
12233 stmt = make_node (OMP_TASK);
12234 TREE_TYPE (stmt) = void_type_node;
12235 OMP_TASK_CLAUSES (stmt) = clauses;
12236 OMP_TASK_BODY (stmt) = block;
c2255bc4 12237 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
12238
12239 return add_stmt (stmt);
12240}
12241
acf0174b
JJ
12242/* Generate GOMP_cancel call for #pragma omp cancel. */
12243
12244void
12245c_finish_omp_cancel (location_t loc, tree clauses)
12246{
12247 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12248 int mask = 0;
629b3d75 12249 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
acf0174b 12250 mask = 1;
629b3d75 12251 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
acf0174b 12252 mask = 2;
629b3d75 12253 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
acf0174b 12254 mask = 4;
629b3d75 12255 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
acf0174b
JJ
12256 mask = 8;
12257 else
12258 {
54d19c3b 12259 error_at (loc, "%<#pragma omp cancel%> must specify one of "
acf0174b
JJ
12260 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12261 "clauses");
12262 return;
12263 }
629b3d75 12264 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
acf0174b
JJ
12265 if (ifc != NULL_TREE)
12266 {
12267 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12268 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12269 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12270 build_zero_cst (type));
12271 }
12272 else
12273 ifc = boolean_true_node;
12274 tree stmt = build_call_expr_loc (loc, fn, 2,
12275 build_int_cst (integer_type_node, mask),
12276 ifc);
12277 add_stmt (stmt);
12278}
12279
12280/* Generate GOMP_cancellation_point call for
12281 #pragma omp cancellation point. */
12282
12283void
12284c_finish_omp_cancellation_point (location_t loc, tree clauses)
12285{
12286 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12287 int mask = 0;
629b3d75 12288 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
acf0174b 12289 mask = 1;
629b3d75 12290 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
acf0174b 12291 mask = 2;
629b3d75 12292 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
acf0174b 12293 mask = 4;
629b3d75 12294 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
acf0174b
JJ
12295 mask = 8;
12296 else
12297 {
54d19c3b 12298 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
acf0174b
JJ
12299 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12300 "clauses");
12301 return;
12302 }
12303 tree stmt = build_call_expr_loc (loc, fn, 1,
12304 build_int_cst (integer_type_node, mask));
12305 add_stmt (stmt);
12306}
12307
12308/* Helper function for handle_omp_array_sections. Called recursively
12309 to handle multiple array-section-subscripts. C is the clause,
12310 T current expression (initially OMP_CLAUSE_DECL), which is either
12311 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12312 expression if specified, TREE_VALUE length expression if specified,
12313 TREE_CHAIN is what it has been specified after, or some decl.
12314 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12315 set to true if any of the array-section-subscript could have length
12316 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12317 first array-section-subscript which is known not to have length
12318 of one. Given say:
12319 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12320 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12321 all are or may have length of 1, array-section-subscript [:2] is the
d9a6bd32 12322 first one known not to have length 1. For array-section-subscript
acf0174b
JJ
12323 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12324 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12325 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12326 case though, as some lengths could be zero. */
12327
12328static tree
12329handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
d9a6bd32 12330 bool &maybe_zero_len, unsigned int &first_non_one,
e46c7770 12331 enum c_omp_region_type ort)
acf0174b
JJ
12332{
12333 tree ret, low_bound, length, type;
12334 if (TREE_CODE (t) != TREE_LIST)
12335 {
7a0ca710 12336 if (error_operand_p (t))
acf0174b 12337 return error_mark_node;
d9a6bd32 12338 ret = t;
9dc5773f
JJ
12339 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12340 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12341 {
12342 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12343 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12344 return error_mark_node;
12345 }
d9a6bd32 12346 if (TREE_CODE (t) == COMPONENT_REF
e46c7770 12347 && ort == C_ORT_OMP
d9a6bd32
JJ
12348 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12349 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12350 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12351 {
12352 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12353 {
12354 error_at (OMP_CLAUSE_LOCATION (c),
12355 "bit-field %qE in %qs clause",
12356 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12357 return error_mark_node;
12358 }
12359 while (TREE_CODE (t) == COMPONENT_REF)
12360 {
12361 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12362 {
12363 error_at (OMP_CLAUSE_LOCATION (c),
12364 "%qE is a member of a union", t);
12365 return error_mark_node;
12366 }
12367 t = TREE_OPERAND (t, 0);
12368 }
12369 }
0ae9bd27 12370 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12371 {
12372 if (DECL_P (t))
12373 error_at (OMP_CLAUSE_LOCATION (c),
12374 "%qD is not a variable in %qs clause", t,
12375 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12376 else
12377 error_at (OMP_CLAUSE_LOCATION (c),
12378 "%qE is not a variable in %qs clause", t,
12379 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12380 return error_mark_node;
12381 }
12382 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
9dc5773f
JJ
12383 && TYPE_ATOMIC (TREE_TYPE (t)))
12384 {
12385 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12386 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12387 return error_mark_node;
12388 }
12389 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12390 && VAR_P (t)
12391 && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
12392 {
12393 error_at (OMP_CLAUSE_LOCATION (c),
12394 "%qD is threadprivate variable in %qs clause", t,
12395 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12396 return error_mark_node;
12397 }
9dc5773f
JJ
12398 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12399 && TYPE_ATOMIC (TREE_TYPE (t))
12400 && POINTER_TYPE_P (TREE_TYPE (t)))
12401 {
12402 /* If the array section is pointer based and the pointer
12403 itself is _Atomic qualified, we need to atomically load
12404 the pointer. */
12405 c_expr expr;
12406 memset (&expr, 0, sizeof (expr));
12407 expr.value = ret;
12408 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12409 expr, false, false);
12410 ret = expr.value;
12411 }
d9a6bd32 12412 return ret;
acf0174b
JJ
12413 }
12414
12415 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
e46c7770 12416 maybe_zero_len, first_non_one, ort);
acf0174b
JJ
12417 if (ret == error_mark_node || ret == NULL_TREE)
12418 return ret;
12419
12420 type = TREE_TYPE (ret);
12421 low_bound = TREE_PURPOSE (t);
12422 length = TREE_VALUE (t);
12423
12424 if (low_bound == error_mark_node || length == error_mark_node)
12425 return error_mark_node;
12426
12427 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12428 {
12429 error_at (OMP_CLAUSE_LOCATION (c),
12430 "low bound %qE of array section does not have integral type",
12431 low_bound);
12432 return error_mark_node;
12433 }
12434 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12435 {
12436 error_at (OMP_CLAUSE_LOCATION (c),
12437 "length %qE of array section does not have integral type",
12438 length);
12439 return error_mark_node;
12440 }
12441 if (low_bound
12442 && TREE_CODE (low_bound) == INTEGER_CST
12443 && TYPE_PRECISION (TREE_TYPE (low_bound))
12444 > TYPE_PRECISION (sizetype))
12445 low_bound = fold_convert (sizetype, low_bound);
12446 if (length
12447 && TREE_CODE (length) == INTEGER_CST
12448 && TYPE_PRECISION (TREE_TYPE (length))
12449 > TYPE_PRECISION (sizetype))
12450 length = fold_convert (sizetype, length);
12451 if (low_bound == NULL_TREE)
12452 low_bound = integer_zero_node;
12453
12454 if (length != NULL_TREE)
12455 {
12456 if (!integer_nonzerop (length))
d9a6bd32
JJ
12457 {
12458 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12459 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12460 {
12461 if (integer_zerop (length))
12462 {
12463 error_at (OMP_CLAUSE_LOCATION (c),
12464 "zero length array section in %qs clause",
12465 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12466 return error_mark_node;
12467 }
12468 }
12469 else
12470 maybe_zero_len = true;
12471 }
acf0174b
JJ
12472 if (first_non_one == types.length ()
12473 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12474 first_non_one++;
12475 }
12476 if (TREE_CODE (type) == ARRAY_TYPE)
12477 {
12478 if (length == NULL_TREE
12479 && (TYPE_DOMAIN (type) == NULL_TREE
12480 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12481 {
12482 error_at (OMP_CLAUSE_LOCATION (c),
12483 "for unknown bound array type length expression must "
12484 "be specified");
12485 return error_mark_node;
12486 }
12487 if (TREE_CODE (low_bound) == INTEGER_CST
12488 && tree_int_cst_sgn (low_bound) == -1)
12489 {
12490 error_at (OMP_CLAUSE_LOCATION (c),
12491 "negative low bound in array section in %qs clause",
12492 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12493 return error_mark_node;
12494 }
12495 if (length != NULL_TREE
12496 && TREE_CODE (length) == INTEGER_CST
12497 && tree_int_cst_sgn (length) == -1)
12498 {
12499 error_at (OMP_CLAUSE_LOCATION (c),
12500 "negative length in array section in %qs clause",
12501 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12502 return error_mark_node;
12503 }
12504 if (TYPE_DOMAIN (type)
12505 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12506 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12507 == INTEGER_CST)
12508 {
8ab7005b
JJ
12509 tree size
12510 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12511 size = size_binop (PLUS_EXPR, size, size_one_node);
acf0174b
JJ
12512 if (TREE_CODE (low_bound) == INTEGER_CST)
12513 {
12514 if (tree_int_cst_lt (size, low_bound))
12515 {
12516 error_at (OMP_CLAUSE_LOCATION (c),
12517 "low bound %qE above array section size "
12518 "in %qs clause", low_bound,
12519 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12520 return error_mark_node;
12521 }
12522 if (tree_int_cst_equal (size, low_bound))
d9a6bd32
JJ
12523 {
12524 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12525 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12526 {
12527 error_at (OMP_CLAUSE_LOCATION (c),
12528 "zero length array section in %qs clause",
12529 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12530 return error_mark_node;
12531 }
12532 maybe_zero_len = true;
12533 }
acf0174b
JJ
12534 else if (length == NULL_TREE
12535 && first_non_one == types.length ()
12536 && tree_int_cst_equal
12537 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12538 low_bound))
12539 first_non_one++;
12540 }
12541 else if (length == NULL_TREE)
12542 {
d9a6bd32
JJ
12543 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12544 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12545 maybe_zero_len = true;
acf0174b
JJ
12546 if (first_non_one == types.length ())
12547 first_non_one++;
12548 }
12549 if (length && TREE_CODE (length) == INTEGER_CST)
12550 {
12551 if (tree_int_cst_lt (size, length))
12552 {
12553 error_at (OMP_CLAUSE_LOCATION (c),
12554 "length %qE above array section size "
12555 "in %qs clause", length,
12556 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12557 return error_mark_node;
12558 }
12559 if (TREE_CODE (low_bound) == INTEGER_CST)
12560 {
12561 tree lbpluslen
12562 = size_binop (PLUS_EXPR,
12563 fold_convert (sizetype, low_bound),
12564 fold_convert (sizetype, length));
12565 if (TREE_CODE (lbpluslen) == INTEGER_CST
12566 && tree_int_cst_lt (size, lbpluslen))
12567 {
12568 error_at (OMP_CLAUSE_LOCATION (c),
12569 "high bound %qE above array section size "
12570 "in %qs clause", lbpluslen,
12571 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12572 return error_mark_node;
12573 }
12574 }
12575 }
12576 }
12577 else if (length == NULL_TREE)
12578 {
d9a6bd32
JJ
12579 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12580 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12581 maybe_zero_len = true;
acf0174b
JJ
12582 if (first_non_one == types.length ())
12583 first_non_one++;
12584 }
12585
12586 /* For [lb:] we will need to evaluate lb more than once. */
12587 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12588 {
b2fa0a8b 12589 tree lb = save_expr (low_bound);
acf0174b
JJ
12590 if (lb != low_bound)
12591 {
12592 TREE_PURPOSE (t) = lb;
12593 low_bound = lb;
12594 }
12595 }
12596 }
12597 else if (TREE_CODE (type) == POINTER_TYPE)
12598 {
12599 if (length == NULL_TREE)
12600 {
12601 error_at (OMP_CLAUSE_LOCATION (c),
12602 "for pointer type length expression must be specified");
12603 return error_mark_node;
12604 }
d9a6bd32
JJ
12605 if (length != NULL_TREE
12606 && TREE_CODE (length) == INTEGER_CST
12607 && tree_int_cst_sgn (length) == -1)
12608 {
12609 error_at (OMP_CLAUSE_LOCATION (c),
12610 "negative length in array section in %qs clause",
12611 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12612 return error_mark_node;
12613 }
acf0174b
JJ
12614 /* If there is a pointer type anywhere but in the very first
12615 array-section-subscript, the array section can't be contiguous. */
12616 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12617 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12618 {
12619 error_at (OMP_CLAUSE_LOCATION (c),
12620 "array section is not contiguous in %qs clause",
12621 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12622 return error_mark_node;
12623 }
12624 }
12625 else
12626 {
12627 error_at (OMP_CLAUSE_LOCATION (c),
12628 "%qE does not have pointer or array type", ret);
12629 return error_mark_node;
12630 }
12631 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12632 types.safe_push (TREE_TYPE (ret));
12633 /* We will need to evaluate lb more than once. */
b2fa0a8b 12634 tree lb = save_expr (low_bound);
acf0174b
JJ
12635 if (lb != low_bound)
12636 {
12637 TREE_PURPOSE (t) = lb;
12638 low_bound = lb;
12639 }
12640 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12641 return ret;
12642}
12643
12644/* Handle array sections for clause C. */
12645
12646static bool
e46c7770 12647handle_omp_array_sections (tree c, enum c_omp_region_type ort)
acf0174b
JJ
12648{
12649 bool maybe_zero_len = false;
12650 unsigned int first_non_one = 0;
d9a6bd32 12651 auto_vec<tree, 10> types;
acf0174b 12652 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
d9a6bd32 12653 maybe_zero_len, first_non_one,
e46c7770 12654 ort);
acf0174b 12655 if (first == error_mark_node)
d9a6bd32 12656 return true;
acf0174b 12657 if (first == NULL_TREE)
d9a6bd32 12658 return false;
acf0174b
JJ
12659 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12660 {
12661 tree t = OMP_CLAUSE_DECL (c);
12662 tree tem = NULL_TREE;
acf0174b
JJ
12663 /* Need to evaluate side effects in the length expressions
12664 if any. */
12665 while (TREE_CODE (t) == TREE_LIST)
12666 {
12667 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12668 {
12669 if (tem == NULL_TREE)
12670 tem = TREE_VALUE (t);
12671 else
12672 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12673 TREE_VALUE (t), tem);
12674 }
12675 t = TREE_CHAIN (t);
12676 }
12677 if (tem)
12678 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12679 first = c_fully_fold (first, false, NULL);
12680 OMP_CLAUSE_DECL (c) = first;
12681 }
12682 else
12683 {
12684 unsigned int num = types.length (), i;
12685 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12686 tree condition = NULL_TREE;
12687
12688 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12689 maybe_zero_len = true;
12690
12691 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12692 t = TREE_CHAIN (t))
12693 {
12694 tree low_bound = TREE_PURPOSE (t);
12695 tree length = TREE_VALUE (t);
12696
12697 i--;
12698 if (low_bound
12699 && TREE_CODE (low_bound) == INTEGER_CST
12700 && TYPE_PRECISION (TREE_TYPE (low_bound))
12701 > TYPE_PRECISION (sizetype))
12702 low_bound = fold_convert (sizetype, low_bound);
12703 if (length
12704 && TREE_CODE (length) == INTEGER_CST
12705 && TYPE_PRECISION (TREE_TYPE (length))
12706 > TYPE_PRECISION (sizetype))
12707 length = fold_convert (sizetype, length);
12708 if (low_bound == NULL_TREE)
12709 low_bound = integer_zero_node;
12710 if (!maybe_zero_len && i > first_non_one)
12711 {
12712 if (integer_nonzerop (low_bound))
12713 goto do_warn_noncontiguous;
12714 if (length != NULL_TREE
12715 && TREE_CODE (length) == INTEGER_CST
12716 && TYPE_DOMAIN (types[i])
12717 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12718 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12719 == INTEGER_CST)
12720 {
12721 tree size;
12722 size = size_binop (PLUS_EXPR,
12723 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12724 size_one_node);
12725 if (!tree_int_cst_equal (length, size))
12726 {
12727 do_warn_noncontiguous:
12728 error_at (OMP_CLAUSE_LOCATION (c),
12729 "array section is not contiguous in %qs "
12730 "clause",
12731 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
acf0174b
JJ
12732 return true;
12733 }
12734 }
12735 if (length != NULL_TREE
12736 && TREE_SIDE_EFFECTS (length))
12737 {
12738 if (side_effects == NULL_TREE)
12739 side_effects = length;
12740 else
12741 side_effects = build2 (COMPOUND_EXPR,
12742 TREE_TYPE (side_effects),
12743 length, side_effects);
12744 }
12745 }
12746 else
12747 {
12748 tree l;
12749
d9a6bd32
JJ
12750 if (i > first_non_one
12751 && ((length && integer_nonzerop (length))
12752 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
acf0174b
JJ
12753 continue;
12754 if (length)
12755 l = fold_convert (sizetype, length);
12756 else
12757 {
12758 l = size_binop (PLUS_EXPR,
12759 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12760 size_one_node);
12761 l = size_binop (MINUS_EXPR, l,
12762 fold_convert (sizetype, low_bound));
12763 }
12764 if (i > first_non_one)
12765 {
12766 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12767 size_zero_node);
12768 if (condition == NULL_TREE)
12769 condition = l;
12770 else
12771 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12772 l, condition);
12773 }
12774 else if (size == NULL_TREE)
12775 {
12776 size = size_in_bytes (TREE_TYPE (types[i]));
d9a6bd32
JJ
12777 tree eltype = TREE_TYPE (types[num - 1]);
12778 while (TREE_CODE (eltype) == ARRAY_TYPE)
12779 eltype = TREE_TYPE (eltype);
12780 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12781 {
12782 if (integer_zerop (size)
12783 || integer_zerop (size_in_bytes (eltype)))
12784 {
12785 error_at (OMP_CLAUSE_LOCATION (c),
12786 "zero length array section in %qs clause",
12787 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12788 return error_mark_node;
12789 }
12790 size = size_binop (EXACT_DIV_EXPR, size,
12791 size_in_bytes (eltype));
12792 }
acf0174b
JJ
12793 size = size_binop (MULT_EXPR, size, l);
12794 if (condition)
12795 size = fold_build3 (COND_EXPR, sizetype, condition,
12796 size, size_zero_node);
12797 }
12798 else
12799 size = size_binop (MULT_EXPR, size, l);
12800 }
12801 }
acf0174b
JJ
12802 if (side_effects)
12803 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
d9a6bd32
JJ
12804 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12805 {
12806 size = size_binop (MINUS_EXPR, size, size_one_node);
12807 size = c_fully_fold (size, false, NULL);
12808 tree index_type = build_index_type (size);
12809 tree eltype = TREE_TYPE (first);
12810 while (TREE_CODE (eltype) == ARRAY_TYPE)
12811 eltype = TREE_TYPE (eltype);
12812 tree type = build_array_type (eltype, index_type);
12813 tree ptype = build_pointer_type (eltype);
12814 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12815 t = build_fold_addr_expr (t);
e01d41e5
JJ
12816 tree t2 = build_fold_addr_expr (first);
12817 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12818 ptrdiff_type_node, t2);
12819 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12820 ptrdiff_type_node, t2,
12821 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12822 ptrdiff_type_node, t));
12823 t2 = c_fully_fold (t2, false, NULL);
12824 if (tree_fits_shwi_p (t2))
12825 t = build2 (MEM_REF, type, t,
12826 build_int_cst (ptype, tree_to_shwi (t2)));
12827 else
12828 {
12829 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12830 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12831 TREE_TYPE (t), t, t2);
12832 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12833 }
d9a6bd32
JJ
12834 OMP_CLAUSE_DECL (c) = t;
12835 return false;
12836 }
acf0174b
JJ
12837 first = c_fully_fold (first, false, NULL);
12838 OMP_CLAUSE_DECL (c) = first;
12839 if (size)
12840 size = c_fully_fold (size, false, NULL);
12841 OMP_CLAUSE_SIZE (c) = size;
d9a6bd32
JJ
12842 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12843 || (TREE_CODE (t) == COMPONENT_REF
12844 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
acf0174b 12845 return false;
41dbbb37 12846 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
e46c7770 12847 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
d9a6bd32
JJ
12848 switch (OMP_CLAUSE_MAP_KIND (c))
12849 {
12850 case GOMP_MAP_ALLOC:
12851 case GOMP_MAP_TO:
12852 case GOMP_MAP_FROM:
12853 case GOMP_MAP_TOFROM:
12854 case GOMP_MAP_ALWAYS_TO:
12855 case GOMP_MAP_ALWAYS_FROM:
12856 case GOMP_MAP_ALWAYS_TOFROM:
12857 case GOMP_MAP_RELEASE:
12858 case GOMP_MAP_DELETE:
b1c9c068
CP
12859 case GOMP_MAP_FORCE_TO:
12860 case GOMP_MAP_FORCE_FROM:
12861 case GOMP_MAP_FORCE_TOFROM:
12862 case GOMP_MAP_FORCE_PRESENT:
d9a6bd32
JJ
12863 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12864 break;
12865 default:
12866 break;
12867 }
acf0174b 12868 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
e46c7770 12869 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
e01d41e5
JJ
12870 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12871 else if (TREE_CODE (t) == COMPONENT_REF)
12872 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12873 else
12874 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12875 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12876 && !c_mark_addressable (t))
acf0174b
JJ
12877 return false;
12878 OMP_CLAUSE_DECL (c2) = t;
12879 t = build_fold_addr_expr (first);
12880 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12881 tree ptr = OMP_CLAUSE_DECL (c2);
12882 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12883 ptr = build_fold_addr_expr (ptr);
12884 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12885 ptrdiff_type_node, t,
12886 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12887 ptrdiff_type_node, ptr));
12888 t = c_fully_fold (t, false, NULL);
12889 OMP_CLAUSE_SIZE (c2) = t;
12890 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12891 OMP_CLAUSE_CHAIN (c) = c2;
12892 }
12893 return false;
12894}
12895
12896/* Helper function of finish_omp_clauses. Clone STMT as if we were making
12897 an inline call. But, remap
12898 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12899 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12900
12901static tree
12902c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12903 tree decl, tree placeholder)
12904{
12905 copy_body_data id;
b787e7a2 12906 hash_map<tree, tree> decl_map;
acf0174b 12907
b787e7a2
TS
12908 decl_map.put (omp_decl1, placeholder);
12909 decl_map.put (omp_decl2, decl);
acf0174b
JJ
12910 memset (&id, 0, sizeof (id));
12911 id.src_fn = DECL_CONTEXT (omp_decl1);
12912 id.dst_fn = current_function_decl;
12913 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12914 id.decl_map = &decl_map;
acf0174b
JJ
12915
12916 id.copy_decl = copy_decl_no_change;
12917 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12918 id.transform_new_cfg = true;
12919 id.transform_return_to_modify = false;
12920 id.transform_lang_insert_block = NULL;
12921 id.eh_lp_nr = 0;
12922 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12923 return stmt;
12924}
12925
12926/* Helper function of c_finish_omp_clauses, called via walk_tree.
12927 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12928
12929static tree
12930c_find_omp_placeholder_r (tree *tp, int *, void *data)
12931{
12932 if (*tp == (tree) data)
12933 return *tp;
12934 return NULL_TREE;
12935}
12936
41dbbb37 12937/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12938 Remove any elements from the list that are invalid. */
12939
12940tree
77886428 12941c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
953ff289
DN
12942{
12943 bitmap_head generic_head, firstprivate_head, lastprivate_head;
e46c7770 12944 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
d9a6bd32
JJ
12945 tree c, t, type, *pc;
12946 tree simdlen = NULL_TREE, safelen = NULL_TREE;
acf0174b
JJ
12947 bool branch_seen = false;
12948 bool copyprivate_seen = false;
e01d41e5 12949 bool linear_variable_step_check = false;
acf0174b 12950 tree *nowait_clause = NULL;
e01d41e5
JJ
12951 bool ordered_seen = false;
12952 tree schedule_clause = NULL_TREE;
b605f663 12953 bool oacc_async = false;
953ff289
DN
12954
12955 bitmap_obstack_initialize (NULL);
12956 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12957 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12958 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12959 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
d9a6bd32
JJ
12960 bitmap_initialize (&map_head, &bitmap_default_obstack);
12961 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
e46c7770 12962 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
953ff289 12963
b605f663
CLT
12964 if (ort & C_ORT_ACC)
12965 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12966 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12967 {
12968 oacc_async = true;
12969 break;
12970 }
9dc5773f 12971
953ff289
DN
12972 for (pc = &clauses, c = clauses; c ; c = *pc)
12973 {
12974 bool remove = false;
12975 bool need_complete = false;
12976 bool need_implicitly_determined = false;
12977
aaf46ef9 12978 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12979 {
12980 case OMP_CLAUSE_SHARED:
953ff289
DN
12981 need_implicitly_determined = true;
12982 goto check_dup_generic;
12983
12984 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12985 need_complete = true;
12986 need_implicitly_determined = true;
12987 goto check_dup_generic;
12988
12989 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12990 need_implicitly_determined = true;
12991 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12992 if (TREE_CODE (t) == TREE_LIST)
12993 {
e46c7770 12994 if (handle_omp_array_sections (c, ort))
d9a6bd32
JJ
12995 {
12996 remove = true;
12997 break;
12998 }
12999
13000 t = OMP_CLAUSE_DECL (c);
13001 }
4f2e1536 13002 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
d9a6bd32
JJ
13003 if (t == error_mark_node)
13004 {
13005 remove = true;
13006 break;
13007 }
b605f663
CLT
13008 if (oacc_async)
13009 c_mark_addressable (t);
d9a6bd32
JJ
13010 type = TREE_TYPE (t);
13011 if (TREE_CODE (t) == MEM_REF)
13012 type = TREE_TYPE (type);
13013 if (TREE_CODE (type) == ARRAY_TYPE)
13014 {
13015 tree oatype = type;
13016 gcc_assert (TREE_CODE (t) != MEM_REF);
13017 while (TREE_CODE (type) == ARRAY_TYPE)
13018 type = TREE_TYPE (type);
13019 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13020 {
13021 error_at (OMP_CLAUSE_LOCATION (c),
13022 "%qD in %<reduction%> clause is a zero size array",
13023 t);
13024 remove = true;
13025 break;
13026 }
13027 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13028 TYPE_SIZE_UNIT (type));
13029 if (integer_zerop (size))
13030 {
13031 error_at (OMP_CLAUSE_LOCATION (c),
13032 "%qD in %<reduction%> clause is a zero size array",
13033 t);
13034 remove = true;
13035 break;
13036 }
13037 size = size_binop (MINUS_EXPR, size, size_one_node);
13038 tree index_type = build_index_type (size);
13039 tree atype = build_array_type (type, index_type);
13040 tree ptype = build_pointer_type (type);
13041 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13042 t = build_fold_addr_expr (t);
13043 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13044 OMP_CLAUSE_DECL (c) = t;
13045 }
9dc5773f
JJ
13046 if (TYPE_ATOMIC (type))
13047 {
13048 error_at (OMP_CLAUSE_LOCATION (c),
13049 "%<_Atomic%> %qE in %<reduction%> clause", t);
13050 remove = true;
13051 break;
13052 }
acf0174b 13053 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
d9a6bd32
JJ
13054 && (FLOAT_TYPE_P (type)
13055 || TREE_CODE (type) == COMPLEX_TYPE))
953ff289
DN
13056 {
13057 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13058 const char *r_name = NULL;
13059
13060 switch (r_code)
13061 {
13062 case PLUS_EXPR:
13063 case MULT_EXPR:
13064 case MINUS_EXPR:
652fea39 13065 break;
20906c66 13066 case MIN_EXPR:
d9a6bd32 13067 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39
JJ
13068 r_name = "min";
13069 break;
20906c66 13070 case MAX_EXPR:
d9a6bd32 13071 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39 13072 r_name = "max";
953ff289
DN
13073 break;
13074 case BIT_AND_EXPR:
13075 r_name = "&";
13076 break;
13077 case BIT_XOR_EXPR:
13078 r_name = "^";
13079 break;
13080 case BIT_IOR_EXPR:
13081 r_name = "|";
13082 break;
13083 case TRUTH_ANDIF_EXPR:
d9a6bd32 13084 if (FLOAT_TYPE_P (type))
652fea39 13085 r_name = "&&";
953ff289
DN
13086 break;
13087 case TRUTH_ORIF_EXPR:
d9a6bd32 13088 if (FLOAT_TYPE_P (type))
652fea39 13089 r_name = "||";
953ff289
DN
13090 break;
13091 default:
13092 gcc_unreachable ();
13093 }
13094 if (r_name)
13095 {
c2255bc4
AH
13096 error_at (OMP_CLAUSE_LOCATION (c),
13097 "%qE has invalid type for %<reduction(%s)%>",
13098 t, r_name);
953ff289 13099 remove = true;
ee1d5a02 13100 break;
953ff289
DN
13101 }
13102 }
acf0174b
JJ
13103 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13104 {
13105 error_at (OMP_CLAUSE_LOCATION (c),
d9a6bd32 13106 "user defined reduction not found for %qE", t);
acf0174b 13107 remove = true;
ee1d5a02 13108 break;
acf0174b
JJ
13109 }
13110 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13111 {
13112 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
d9a6bd32 13113 type = TYPE_MAIN_VARIANT (type);
acf0174b
JJ
13114 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13115 VAR_DECL, NULL_TREE, type);
d9a6bd32 13116 tree decl_placeholder = NULL_TREE;
acf0174b
JJ
13117 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13118 DECL_ARTIFICIAL (placeholder) = 1;
13119 DECL_IGNORED_P (placeholder) = 1;
d9a6bd32
JJ
13120 if (TREE_CODE (t) == MEM_REF)
13121 {
13122 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13123 VAR_DECL, NULL_TREE, type);
13124 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13125 DECL_ARTIFICIAL (decl_placeholder) = 1;
13126 DECL_IGNORED_P (decl_placeholder) = 1;
13127 }
acf0174b
JJ
13128 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13129 c_mark_addressable (placeholder);
13130 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
d9a6bd32
JJ
13131 c_mark_addressable (decl_placeholder ? decl_placeholder
13132 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
13133 OMP_CLAUSE_REDUCTION_MERGE (c)
13134 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13135 TREE_VEC_ELT (list, 0),
13136 TREE_VEC_ELT (list, 1),
d9a6bd32
JJ
13137 decl_placeholder ? decl_placeholder
13138 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b
JJ
13139 OMP_CLAUSE_REDUCTION_MERGE (c)
13140 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13141 void_type_node, NULL_TREE,
d9a6bd32 13142 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
acf0174b
JJ
13143 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13144 if (TREE_VEC_LENGTH (list) == 6)
13145 {
13146 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
d9a6bd32
JJ
13147 c_mark_addressable (decl_placeholder ? decl_placeholder
13148 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
13149 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13150 c_mark_addressable (placeholder);
13151 tree init = TREE_VEC_ELT (list, 5);
13152 if (init == error_mark_node)
13153 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13154 OMP_CLAUSE_REDUCTION_INIT (c)
13155 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13156 TREE_VEC_ELT (list, 3),
d9a6bd32
JJ
13157 decl_placeholder ? decl_placeholder
13158 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b 13159 if (TREE_VEC_ELT (list, 5) == error_mark_node)
d9a6bd32
JJ
13160 {
13161 tree v = decl_placeholder ? decl_placeholder : t;
13162 OMP_CLAUSE_REDUCTION_INIT (c)
13163 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13164 OMP_CLAUSE_REDUCTION_INIT (c));
13165 }
acf0174b
JJ
13166 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13167 c_find_omp_placeholder_r,
13168 placeholder, NULL))
13169 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13170 }
13171 else
13172 {
13173 tree init;
d9a6bd32
JJ
13174 tree v = decl_placeholder ? decl_placeholder : t;
13175 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13176 init = build_constructor (TREE_TYPE (v), NULL);
acf0174b 13177 else
d9a6bd32 13178 init = fold_convert (TREE_TYPE (v), integer_zero_node);
acf0174b 13179 OMP_CLAUSE_REDUCTION_INIT (c)
d9a6bd32 13180 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
acf0174b
JJ
13181 }
13182 OMP_CLAUSE_REDUCTION_INIT (c)
13183 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13184 void_type_node, NULL_TREE,
13185 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13186 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13187 }
d9a6bd32
JJ
13188 if (TREE_CODE (t) == MEM_REF)
13189 {
13190 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13191 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13192 != INTEGER_CST)
13193 {
13194 sorry ("variable length element type in array "
13195 "%<reduction%> clause");
13196 remove = true;
13197 break;
13198 }
13199 t = TREE_OPERAND (t, 0);
e01d41e5
JJ
13200 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13201 t = TREE_OPERAND (t, 0);
d9a6bd32
JJ
13202 if (TREE_CODE (t) == ADDR_EXPR)
13203 t = TREE_OPERAND (t, 0);
13204 }
13205 goto check_dup_generic_t;
953ff289
DN
13206
13207 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
13208 copyprivate_seen = true;
13209 if (nowait_clause)
13210 {
13211 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13212 "%<nowait%> clause must not be used together "
13213 "with %<copyprivate%>");
13214 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13215 nowait_clause = NULL;
13216 }
953ff289
DN
13217 goto check_dup_generic;
13218
13219 case OMP_CLAUSE_COPYIN:
953ff289 13220 t = OMP_CLAUSE_DECL (c);
0ae9bd27 13221 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
953ff289 13222 {
c2255bc4
AH
13223 error_at (OMP_CLAUSE_LOCATION (c),
13224 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 13225 remove = true;
ee1d5a02 13226 break;
953ff289
DN
13227 }
13228 goto check_dup_generic;
13229
acf0174b 13230 case OMP_CLAUSE_LINEAR:
77886428 13231 if (ort != C_ORT_OMP_DECLARE_SIMD)
d9a6bd32 13232 need_implicitly_determined = true;
acf0174b 13233 t = OMP_CLAUSE_DECL (c);
77886428 13234 if (ort != C_ORT_OMP_DECLARE_SIMD
d9a6bd32
JJ
13235 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13236 {
13237 error_at (OMP_CLAUSE_LOCATION (c),
13238 "modifier should not be specified in %<linear%> "
13239 "clause on %<simd%> or %<for%> constructs");
13240 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13241 }
77886428 13242 if (ort & C_ORT_CILK)
acf0174b 13243 {
477d4906
IV
13244 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13245 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13246 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13247 {
13248 error_at (OMP_CLAUSE_LOCATION (c),
13249 "linear clause applied to non-integral, "
13250 "non-floating, non-pointer variable with type %qT",
13251 TREE_TYPE (t));
13252 remove = true;
13253 break;
13254 }
13255 }
13256 else
13257 {
13258 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13259 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13260 {
13261 error_at (OMP_CLAUSE_LOCATION (c),
13262 "linear clause applied to non-integral non-pointer "
13263 "variable with type %qT", TREE_TYPE (t));
13264 remove = true;
13265 break;
13266 }
9dc5773f
JJ
13267 if (TYPE_ATOMIC (TREE_TYPE (t)))
13268 {
13269 error_at (OMP_CLAUSE_LOCATION (c),
13270 "%<_Atomic%> %qD in %<linear%> clause", t);
13271 remove = true;
13272 break;
13273 }
acf0174b 13274 }
77886428 13275 if (ort == C_ORT_OMP_DECLARE_SIMD)
e01d41e5
JJ
13276 {
13277 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13278 if (TREE_CODE (s) == PARM_DECL)
13279 {
13280 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13281 /* map_head bitmap is used as uniform_head if
13282 declare_simd. */
13283 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13284 linear_variable_step_check = true;
13285 goto check_dup_generic;
13286 }
13287 if (TREE_CODE (s) != INTEGER_CST)
13288 {
13289 error_at (OMP_CLAUSE_LOCATION (c),
13290 "%<linear%> clause step %qE is neither constant "
13291 "nor a parameter", s);
13292 remove = true;
13293 break;
13294 }
13295 }
acf0174b
JJ
13296 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13297 {
13298 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13299 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13300 OMP_CLAUSE_DECL (c), s);
13301 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
13302 sizetype, fold_convert (sizetype, s),
13303 fold_convert
13304 (sizetype, OMP_CLAUSE_DECL (c)));
acf0174b
JJ
13305 if (s == error_mark_node)
13306 s = size_one_node;
13307 OMP_CLAUSE_LINEAR_STEP (c) = s;
13308 }
da6f124d
JJ
13309 else
13310 OMP_CLAUSE_LINEAR_STEP (c)
13311 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
13312 goto check_dup_generic;
13313
953ff289
DN
13314 check_dup_generic:
13315 t = OMP_CLAUSE_DECL (c);
d9a6bd32 13316 check_dup_generic_t:
0ae9bd27 13317 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13318 {
c2255bc4 13319 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
13320 "%qE is not a variable in clause %qs", t,
13321 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13322 remove = true;
13323 }
e46c7770
CP
13324 else if (ort == C_ORT_ACC
13325 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13326 {
13327 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13328 {
13329 error ("%qD appears more than once in reduction clauses", t);
13330 remove = true;
13331 }
13332 else
13333 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13334 }
953ff289
DN
13335 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13336 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13337 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13338 {
c2255bc4
AH
13339 error_at (OMP_CLAUSE_LOCATION (c),
13340 "%qE appears more than once in data clauses", t);
953ff289
DN
13341 remove = true;
13342 }
e01d41e5
JJ
13343 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13344 && bitmap_bit_p (&map_head, DECL_UID (t)))
13345 {
e46c7770
CP
13346 if (ort == C_ORT_ACC)
13347 error ("%qD appears more than once in data clauses", t);
13348 else
13349 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13350 remove = true;
13351 }
953ff289
DN
13352 else
13353 bitmap_set_bit (&generic_head, DECL_UID (t));
13354 break;
13355
13356 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
13357 t = OMP_CLAUSE_DECL (c);
13358 need_complete = true;
13359 need_implicitly_determined = true;
0ae9bd27 13360 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13361 {
c2255bc4
AH
13362 error_at (OMP_CLAUSE_LOCATION (c),
13363 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
13364 remove = true;
13365 }
13366 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13367 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13368 {
c2255bc4
AH
13369 error_at (OMP_CLAUSE_LOCATION (c),
13370 "%qE appears more than once in data clauses", t);
953ff289
DN
13371 remove = true;
13372 }
e01d41e5
JJ
13373 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13374 {
e46c7770
CP
13375 if (ort == C_ORT_ACC)
13376 error ("%qD appears more than once in data clauses", t);
13377 else
13378 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13379 remove = true;
13380 }
953ff289
DN
13381 else
13382 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13383 break;
13384
13385 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
13386 t = OMP_CLAUSE_DECL (c);
13387 need_complete = true;
13388 need_implicitly_determined = true;
0ae9bd27 13389 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13390 {
c2255bc4
AH
13391 error_at (OMP_CLAUSE_LOCATION (c),
13392 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
13393 remove = true;
13394 }
13395 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13396 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13397 {
c2255bc4
AH
13398 error_at (OMP_CLAUSE_LOCATION (c),
13399 "%qE appears more than once in data clauses", t);
953ff289
DN
13400 remove = true;
13401 }
13402 else
13403 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13404 break;
13405
acf0174b
JJ
13406 case OMP_CLAUSE_ALIGNED:
13407 t = OMP_CLAUSE_DECL (c);
0ae9bd27 13408 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13409 {
13410 error_at (OMP_CLAUSE_LOCATION (c),
13411 "%qE is not a variable in %<aligned%> clause", t);
13412 remove = true;
13413 }
5a9785fb
JJ
13414 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13415 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13416 {
13417 error_at (OMP_CLAUSE_LOCATION (c),
13418 "%qE in %<aligned%> clause is neither a pointer nor "
13419 "an array", t);
13420 remove = true;
13421 }
9dc5773f
JJ
13422 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13423 {
13424 error_at (OMP_CLAUSE_LOCATION (c),
13425 "%<_Atomic%> %qD in %<aligned%> clause", t);
13426 remove = true;
13427 break;
13428 }
acf0174b
JJ
13429 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13430 {
13431 error_at (OMP_CLAUSE_LOCATION (c),
13432 "%qE appears more than once in %<aligned%> clauses",
13433 t);
13434 remove = true;
13435 }
13436 else
13437 bitmap_set_bit (&aligned_head, DECL_UID (t));
13438 break;
13439
13440 case OMP_CLAUSE_DEPEND:
13441 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
13442 if (t == NULL_TREE)
13443 {
13444 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13445 == OMP_CLAUSE_DEPEND_SOURCE);
13446 break;
13447 }
13448 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13449 {
13450 gcc_assert (TREE_CODE (t) == TREE_LIST);
13451 for (; t; t = TREE_CHAIN (t))
13452 {
13453 tree decl = TREE_VALUE (t);
13454 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13455 {
13456 tree offset = TREE_PURPOSE (t);
13457 bool neg = wi::neg_p ((wide_int) offset);
13458 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13459 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13460 neg ? MINUS_EXPR : PLUS_EXPR,
13461 decl, offset);
13462 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
13463 sizetype,
13464 fold_convert (sizetype, t2),
13465 fold_convert (sizetype, decl));
d9a6bd32
JJ
13466 if (t2 == error_mark_node)
13467 {
13468 remove = true;
13469 break;
13470 }
13471 TREE_PURPOSE (t) = t2;
13472 }
13473 }
13474 break;
13475 }
acf0174b
JJ
13476 if (TREE_CODE (t) == TREE_LIST)
13477 {
e46c7770 13478 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13479 remove = true;
13480 break;
13481 }
13482 if (t == error_mark_node)
13483 remove = true;
0ae9bd27 13484 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13485 {
13486 error_at (OMP_CLAUSE_LOCATION (c),
13487 "%qE is not a variable in %<depend%> clause", t);
13488 remove = true;
13489 }
13490 else if (!c_mark_addressable (t))
13491 remove = true;
13492 break;
13493
13494 case OMP_CLAUSE_MAP:
13495 case OMP_CLAUSE_TO:
13496 case OMP_CLAUSE_FROM:
41dbbb37 13497 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
13498 t = OMP_CLAUSE_DECL (c);
13499 if (TREE_CODE (t) == TREE_LIST)
13500 {
e46c7770 13501 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13502 remove = true;
13503 else
13504 {
13505 t = OMP_CLAUSE_DECL (c);
b17a8b07 13506 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13507 {
13508 error_at (OMP_CLAUSE_LOCATION (c),
13509 "array section does not have mappable type "
13510 "in %qs clause",
13511 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13512 remove = true;
13513 }
9dc5773f
JJ
13514 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13515 {
13516 error_at (OMP_CLAUSE_LOCATION (c),
13517 "%<_Atomic%> %qE in %qs clause", t,
13518 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13519 remove = true;
13520 }
d9a6bd32
JJ
13521 while (TREE_CODE (t) == ARRAY_REF)
13522 t = TREE_OPERAND (t, 0);
13523 if (TREE_CODE (t) == COMPONENT_REF
13524 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13525 {
13526 while (TREE_CODE (t) == COMPONENT_REF)
13527 t = TREE_OPERAND (t, 0);
13528 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13529 break;
13530 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13531 {
13532 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13533 error ("%qD appears more than once in motion"
13534 " clauses", t);
e46c7770
CP
13535 else if (ort == C_ORT_ACC)
13536 error ("%qD appears more than once in data"
13537 " clauses", t);
d9a6bd32
JJ
13538 else
13539 error ("%qD appears more than once in map"
13540 " clauses", t);
13541 remove = true;
13542 }
13543 else
13544 {
13545 bitmap_set_bit (&map_head, DECL_UID (t));
13546 bitmap_set_bit (&map_field_head, DECL_UID (t));
13547 }
13548 }
acf0174b
JJ
13549 }
13550 break;
13551 }
13552 if (t == error_mark_node)
d9a6bd32
JJ
13553 {
13554 remove = true;
13555 break;
13556 }
13557 if (TREE_CODE (t) == COMPONENT_REF
77886428 13558 && (ort & C_ORT_OMP)
d9a6bd32
JJ
13559 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13560 {
13561 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13562 {
13563 error_at (OMP_CLAUSE_LOCATION (c),
13564 "bit-field %qE in %qs clause",
13565 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13566 remove = true;
13567 }
13568 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13569 {
13570 error_at (OMP_CLAUSE_LOCATION (c),
13571 "%qE does not have a mappable type in %qs clause",
13572 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13573 remove = true;
13574 }
9dc5773f
JJ
13575 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13576 {
13577 error_at (OMP_CLAUSE_LOCATION (c),
13578 "%<_Atomic%> %qE in %qs clause", t,
13579 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13580 remove = true;
13581 }
d9a6bd32
JJ
13582 while (TREE_CODE (t) == COMPONENT_REF)
13583 {
13584 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13585 == UNION_TYPE)
13586 {
13587 error_at (OMP_CLAUSE_LOCATION (c),
13588 "%qE is a member of a union", t);
13589 remove = true;
13590 break;
13591 }
13592 t = TREE_OPERAND (t, 0);
13593 }
13594 if (remove)
13595 break;
13596 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13597 {
e01d41e5 13598 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
d9a6bd32
JJ
13599 break;
13600 }
13601 }
13602 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13603 {
13604 error_at (OMP_CLAUSE_LOCATION (c),
13605 "%qE is not a variable in %qs clause", t,
13606 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13607 remove = true;
13608 }
0ae9bd27 13609 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
13610 {
13611 error_at (OMP_CLAUSE_LOCATION (c),
13612 "%qD is threadprivate variable in %qs clause", t,
13613 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13614 remove = true;
13615 }
e4606348
JJ
13616 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13617 || (OMP_CLAUSE_MAP_KIND (c)
13618 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13619 && !c_mark_addressable (t))
acf0174b 13620 remove = true;
b17a8b07 13621 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37 13622 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
d9a6bd32
JJ
13623 || (OMP_CLAUSE_MAP_KIND (c)
13624 == GOMP_MAP_FIRSTPRIVATE_POINTER)
41dbbb37
TS
13625 || (OMP_CLAUSE_MAP_KIND (c)
13626 == GOMP_MAP_FORCE_DEVICEPTR)))
d9a6bd32 13627 && t == OMP_CLAUSE_DECL (c)
b17a8b07 13628 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13629 {
13630 error_at (OMP_CLAUSE_LOCATION (c),
13631 "%qD does not have a mappable type in %qs clause", t,
13632 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13633 remove = true;
13634 }
9dc5773f
JJ
13635 else if (TREE_TYPE (t) == error_mark_node)
13636 remove = true;
13637 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13638 {
13639 error_at (OMP_CLAUSE_LOCATION (c),
13640 "%<_Atomic%> %qE in %qs clause", t,
13641 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13642 remove = true;
13643 }
d9a6bd32
JJ
13644 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13645 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13646 {
13647 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13648 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13649 {
13650 error ("%qD appears more than once in data clauses", t);
13651 remove = true;
13652 }
e01d41e5 13653 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
d9a6bd32 13654 {
e46c7770
CP
13655 if (ort == C_ORT_ACC)
13656 error ("%qD appears more than once in data clauses", t);
13657 else
13658 error ("%qD appears both in data and map clauses", t);
e01d41e5 13659 remove = true;
d9a6bd32 13660 }
e01d41e5
JJ
13661 else
13662 bitmap_set_bit (&generic_head, DECL_UID (t));
d9a6bd32
JJ
13663 }
13664 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
acf0174b
JJ
13665 {
13666 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13667 error ("%qD appears more than once in motion clauses", t);
e46c7770
CP
13668 else if (ort == C_ORT_ACC)
13669 error ("%qD appears more than once in data clauses", t);
acf0174b
JJ
13670 else
13671 error ("%qD appears more than once in map clauses", t);
13672 remove = true;
13673 }
e01d41e5
JJ
13674 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13675 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13676 {
e46c7770
CP
13677 if (ort == C_ORT_ACC)
13678 error ("%qD appears more than once in data clauses", t);
13679 else
13680 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13681 remove = true;
13682 }
acf0174b 13683 else
d9a6bd32
JJ
13684 {
13685 bitmap_set_bit (&map_head, DECL_UID (t));
13686 if (t != OMP_CLAUSE_DECL (c)
13687 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13688 bitmap_set_bit (&map_field_head, DECL_UID (t));
13689 }
13690 break;
13691
13692 case OMP_CLAUSE_TO_DECLARE:
d9a6bd32
JJ
13693 case OMP_CLAUSE_LINK:
13694 t = OMP_CLAUSE_DECL (c);
e01d41e5
JJ
13695 if (TREE_CODE (t) == FUNCTION_DECL
13696 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13697 ;
13698 else if (!VAR_P (t))
d9a6bd32 13699 {
e01d41e5
JJ
13700 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13701 error_at (OMP_CLAUSE_LOCATION (c),
13702 "%qE is neither a variable nor a function name in "
13703 "clause %qs", t,
13704 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13705 else
13706 error_at (OMP_CLAUSE_LOCATION (c),
13707 "%qE is not a variable in clause %qs", t,
13708 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
d9a6bd32
JJ
13709 remove = true;
13710 }
13711 else if (DECL_THREAD_LOCAL_P (t))
13712 {
13713 error_at (OMP_CLAUSE_LOCATION (c),
13714 "%qD is threadprivate variable in %qs clause", t,
13715 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13716 remove = true;
13717 }
13718 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13719 {
13720 error_at (OMP_CLAUSE_LOCATION (c),
13721 "%qD does not have a mappable type in %qs clause", t,
13722 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13723 remove = true;
13724 }
e01d41e5
JJ
13725 if (remove)
13726 break;
13727 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13728 {
13729 error_at (OMP_CLAUSE_LOCATION (c),
13730 "%qE appears more than once on the same "
13731 "%<declare target%> directive", t);
13732 remove = true;
13733 }
13734 else
13735 bitmap_set_bit (&generic_head, DECL_UID (t));
acf0174b
JJ
13736 break;
13737
13738 case OMP_CLAUSE_UNIFORM:
13739 t = OMP_CLAUSE_DECL (c);
13740 if (TREE_CODE (t) != PARM_DECL)
13741 {
13742 if (DECL_P (t))
13743 error_at (OMP_CLAUSE_LOCATION (c),
13744 "%qD is not an argument in %<uniform%> clause", t);
13745 else
13746 error_at (OMP_CLAUSE_LOCATION (c),
13747 "%qE is not an argument in %<uniform%> clause", t);
13748 remove = true;
ee1d5a02 13749 break;
acf0174b 13750 }
e01d41e5
JJ
13751 /* map_head bitmap is used as uniform_head if declare_simd. */
13752 bitmap_set_bit (&map_head, DECL_UID (t));
ee1d5a02 13753 goto check_dup_generic;
acf0174b 13754
d9a6bd32
JJ
13755 case OMP_CLAUSE_IS_DEVICE_PTR:
13756 case OMP_CLAUSE_USE_DEVICE_PTR:
13757 t = OMP_CLAUSE_DECL (c);
13758 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13759 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13760 {
13761 error_at (OMP_CLAUSE_LOCATION (c),
13762 "%qs variable is neither a pointer nor an array",
13763 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13764 remove = true;
13765 }
13766 goto check_dup_generic;
13767
acf0174b
JJ
13768 case OMP_CLAUSE_NOWAIT:
13769 if (copyprivate_seen)
13770 {
13771 error_at (OMP_CLAUSE_LOCATION (c),
13772 "%<nowait%> clause must not be used together "
13773 "with %<copyprivate%>");
13774 remove = true;
13775 break;
13776 }
13777 nowait_clause = pc;
13778 pc = &OMP_CLAUSE_CHAIN (c);
13779 continue;
13780
953ff289
DN
13781 case OMP_CLAUSE_IF:
13782 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
13783 case OMP_CLAUSE_NUM_TEAMS:
13784 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 13785 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
13786 case OMP_CLAUSE_UNTIED:
13787 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
13788 case OMP_CLAUSE_FINAL:
13789 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
13790 case OMP_CLAUSE_DEVICE:
13791 case OMP_CLAUSE_DIST_SCHEDULE:
13792 case OMP_CLAUSE_PARALLEL:
13793 case OMP_CLAUSE_FOR:
13794 case OMP_CLAUSE_SECTIONS:
13795 case OMP_CLAUSE_TASKGROUP:
13796 case OMP_CLAUSE_PROC_BIND:
d9a6bd32
JJ
13797 case OMP_CLAUSE_PRIORITY:
13798 case OMP_CLAUSE_GRAINSIZE:
13799 case OMP_CLAUSE_NUM_TASKS:
13800 case OMP_CLAUSE_NOGROUP:
13801 case OMP_CLAUSE_THREADS:
13802 case OMP_CLAUSE_SIMD:
13803 case OMP_CLAUSE_HINT:
13804 case OMP_CLAUSE_DEFAULTMAP:
9a771876 13805 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
13806 case OMP_CLAUSE_NUM_GANGS:
13807 case OMP_CLAUSE_NUM_WORKERS:
13808 case OMP_CLAUSE_VECTOR_LENGTH:
13809 case OMP_CLAUSE_ASYNC:
13810 case OMP_CLAUSE_WAIT:
13811 case OMP_CLAUSE_AUTO:
7a5e4956 13812 case OMP_CLAUSE_INDEPENDENT:
41dbbb37
TS
13813 case OMP_CLAUSE_SEQ:
13814 case OMP_CLAUSE_GANG:
13815 case OMP_CLAUSE_WORKER:
13816 case OMP_CLAUSE_VECTOR:
7a5e4956 13817 case OMP_CLAUSE_TILE:
acf0174b
JJ
13818 pc = &OMP_CLAUSE_CHAIN (c);
13819 continue;
13820
e01d41e5
JJ
13821 case OMP_CLAUSE_SCHEDULE:
13822 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13823 {
13824 const char *p = NULL;
13825 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13826 {
13827 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13828 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13829 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13830 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13831 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13832 default: gcc_unreachable ();
13833 }
13834 if (p)
13835 {
13836 error_at (OMP_CLAUSE_LOCATION (c),
13837 "%<nonmonotonic%> modifier specified for %qs "
13838 "schedule kind", p);
13839 OMP_CLAUSE_SCHEDULE_KIND (c)
13840 = (enum omp_clause_schedule_kind)
13841 (OMP_CLAUSE_SCHEDULE_KIND (c)
13842 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13843 }
13844 }
13845 schedule_clause = c;
13846 pc = &OMP_CLAUSE_CHAIN (c);
13847 continue;
13848
13849 case OMP_CLAUSE_ORDERED:
13850 ordered_seen = true;
13851 pc = &OMP_CLAUSE_CHAIN (c);
13852 continue;
13853
d9a6bd32
JJ
13854 case OMP_CLAUSE_SAFELEN:
13855 safelen = c;
13856 pc = &OMP_CLAUSE_CHAIN (c);
13857 continue;
13858 case OMP_CLAUSE_SIMDLEN:
13859 simdlen = c;
13860 pc = &OMP_CLAUSE_CHAIN (c);
13861 continue;
13862
acf0174b
JJ
13863 case OMP_CLAUSE_INBRANCH:
13864 case OMP_CLAUSE_NOTINBRANCH:
13865 if (branch_seen)
13866 {
13867 error_at (OMP_CLAUSE_LOCATION (c),
13868 "%<inbranch%> clause is incompatible with "
13869 "%<notinbranch%>");
13870 remove = true;
13871 break;
13872 }
13873 branch_seen = true;
953ff289
DN
13874 pc = &OMP_CLAUSE_CHAIN (c);
13875 continue;
13876
13877 default:
13878 gcc_unreachable ();
13879 }
13880
13881 if (!remove)
13882 {
13883 t = OMP_CLAUSE_DECL (c);
13884
13885 if (need_complete)
13886 {
4f2e1536 13887 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
953ff289
DN
13888 if (t == error_mark_node)
13889 remove = true;
13890 }
13891
13892 if (need_implicitly_determined)
13893 {
13894 const char *share_name = NULL;
13895
0ae9bd27 13896 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
953ff289
DN
13897 share_name = "threadprivate";
13898 else switch (c_omp_predetermined_sharing (t))
13899 {
13900 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13901 break;
13902 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
13903 /* const vars may be specified in firstprivate clause. */
13904 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13905 && TREE_READONLY (t))
13906 break;
953ff289
DN
13907 share_name = "shared";
13908 break;
13909 case OMP_CLAUSE_DEFAULT_PRIVATE:
13910 share_name = "private";
13911 break;
13912 default:
13913 gcc_unreachable ();
13914 }
13915 if (share_name)
13916 {
c2255bc4
AH
13917 error_at (OMP_CLAUSE_LOCATION (c),
13918 "%qE is predetermined %qs for %qs",
acf0174b
JJ
13919 t, share_name,
13920 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13921 remove = true;
13922 }
13923 }
13924 }
13925
13926 if (remove)
13927 *pc = OMP_CLAUSE_CHAIN (c);
13928 else
13929 pc = &OMP_CLAUSE_CHAIN (c);
13930 }
13931
d9a6bd32
JJ
13932 if (simdlen
13933 && safelen
13934 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13935 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13936 {
13937 error_at (OMP_CLAUSE_LOCATION (simdlen),
13938 "%<simdlen%> clause value is bigger than "
13939 "%<safelen%> clause value");
13940 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13941 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13942 }
13943
e01d41e5
JJ
13944 if (ordered_seen
13945 && schedule_clause
13946 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13947 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13948 {
13949 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13950 "%<nonmonotonic%> schedule modifier specified together "
13951 "with %<ordered%> clause");
13952 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13953 = (enum omp_clause_schedule_kind)
13954 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13955 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13956 }
13957
13958 if (linear_variable_step_check)
13959 for (pc = &clauses, c = clauses; c ; c = *pc)
13960 {
13961 bool remove = false;
13962 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13963 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13964 && !bitmap_bit_p (&map_head,
13965 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13966 {
13967 error_at (OMP_CLAUSE_LOCATION (c),
13968 "%<linear%> clause step is a parameter %qD not "
13969 "specified in %<uniform%> clause",
13970 OMP_CLAUSE_LINEAR_STEP (c));
13971 remove = true;
13972 }
13973
13974 if (remove)
13975 *pc = OMP_CLAUSE_CHAIN (c);
13976 else
13977 pc = &OMP_CLAUSE_CHAIN (c);
13978 }
13979
953ff289
DN
13980 bitmap_obstack_release (NULL);
13981 return clauses;
13982}
9ae165a0 13983
9dc5773f
JJ
13984/* Return code to initialize DST with a copy constructor from SRC.
13985 C doesn't have copy constructors nor assignment operators, only for
13986 _Atomic vars we need to perform __atomic_load from src into a temporary
13987 followed by __atomic_store of the temporary to dst. */
13988
13989tree
13990c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13991{
13992 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13993 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13994
13995 location_t loc = OMP_CLAUSE_LOCATION (clause);
13996 tree type = TREE_TYPE (dst);
13997 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13998 tree tmp = create_tmp_var (nonatomic_type);
13999 tree tmp_addr = build_fold_addr_expr (tmp);
14000 TREE_ADDRESSABLE (tmp) = 1;
14001 TREE_NO_WARNING (tmp) = 1;
14002 tree src_addr = build_fold_addr_expr (src);
14003 tree dst_addr = build_fold_addr_expr (dst);
14004 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14005 vec<tree, va_gc> *params;
14006 /* Expansion of a generic atomic load may require an addition
14007 element, so allocate enough to prevent a resize. */
14008 vec_alloc (params, 4);
14009
14010 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14011 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14012 params->quick_push (src_addr);
14013 params->quick_push (tmp_addr);
14014 params->quick_push (seq_cst);
14015 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14016
14017 vec_alloc (params, 4);
14018
14019 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14020 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14021 params->quick_push (dst_addr);
14022 params->quick_push (tmp_addr);
14023 params->quick_push (seq_cst);
14024 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14025 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14026}
14027
0a35513e
AH
14028/* Create a transaction node. */
14029
14030tree
14031c_finish_transaction (location_t loc, tree block, int flags)
14032{
14033 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14034 if (flags & TM_STMT_ATTR_OUTER)
14035 TRANSACTION_EXPR_OUTER (stmt) = 1;
14036 if (flags & TM_STMT_ATTR_RELAXED)
14037 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14038 return add_stmt (stmt);
14039}
14040
9ae165a0 14041/* Make a variant type in the proper way for C/C++, propagating qualifiers
e9e32ee6
JM
14042 down to the element type of an array. If ORIG_QUAL_TYPE is not
14043 NULL, then it should be used as the qualified type
14044 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14045 preserve information about the typedef name from which an array
14046 type was derived). */
9ae165a0
DG
14047
14048tree
e9e32ee6
JM
14049c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14050 size_t orig_qual_indirect)
9ae165a0
DG
14051{
14052 if (type == error_mark_node)
14053 return type;
14054
14055 if (TREE_CODE (type) == ARRAY_TYPE)
14056 {
14057 tree t;
14058 tree element_type = c_build_qualified_type (TREE_TYPE (type),
e9e32ee6
JM
14059 type_quals, orig_qual_type,
14060 orig_qual_indirect - 1);
9ae165a0
DG
14061
14062 /* See if we already have an identically qualified type. */
e9e32ee6
JM
14063 if (orig_qual_type && orig_qual_indirect == 0)
14064 t = orig_qual_type;
14065 else
14066 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14067 {
14068 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14069 && TYPE_NAME (t) == TYPE_NAME (type)
14070 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14071 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14072 TYPE_ATTRIBUTES (type)))
14073 break;
14074 }
9ae165a0
DG
14075 if (!t)
14076 {
14077 tree domain = TYPE_DOMAIN (type);
14078
14079 t = build_variant_type_copy (type);
14080 TREE_TYPE (t) = element_type;
14081
14082 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14083 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14084 SET_TYPE_STRUCTURAL_EQUALITY (t);
14085 else if (TYPE_CANONICAL (element_type) != element_type
14086 || (domain && TYPE_CANONICAL (domain) != domain))
14087 {
b8698a0f 14088 tree unqualified_canon
9ae165a0 14089 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 14090 domain? TYPE_CANONICAL (domain)
9ae165a0 14091 : NULL_TREE);
ee45a32d
EB
14092 if (TYPE_REVERSE_STORAGE_ORDER (type))
14093 {
14094 unqualified_canon
14095 = build_distinct_type_copy (unqualified_canon);
14096 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14097 }
b8698a0f 14098 TYPE_CANONICAL (t)
9ae165a0
DG
14099 = c_build_qualified_type (unqualified_canon, type_quals);
14100 }
14101 else
14102 TYPE_CANONICAL (t) = t;
14103 }
14104 return t;
14105 }
14106
14107 /* A restrict-qualified pointer type must be a pointer to object or
14108 incomplete type. Note that the use of POINTER_TYPE_P also allows
14109 REFERENCE_TYPEs, which is appropriate for C++. */
14110 if ((type_quals & TYPE_QUAL_RESTRICT)
14111 && (!POINTER_TYPE_P (type)
14112 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14113 {
14114 error ("invalid use of %<restrict%>");
14115 type_quals &= ~TYPE_QUAL_RESTRICT;
14116 }
14117
e9e32ee6
JM
14118 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14119 ? orig_qual_type
14120 : build_qualified_type (type, type_quals));
e922069e
JW
14121 /* A variant type does not inherit the list of incomplete vars from the
14122 type main variant. */
152ef731
JJ
14123 if (RECORD_OR_UNION_TYPE_P (var_type)
14124 && TYPE_MAIN_VARIANT (var_type) != var_type)
e922069e
JW
14125 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14126 return var_type;
9ae165a0 14127}
72b5577d
ILT
14128
14129/* Build a VA_ARG_EXPR for the C parser. */
14130
14131tree
f187980b 14132c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
72b5577d 14133{
ec3fba51
MP
14134 if (error_operand_p (type))
14135 return error_mark_node;
f187980b
EB
14136 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14137 order because it takes the address of the expression. */
14138 else if (handled_component_p (expr)
14139 && reverse_storage_order_for_component_p (expr))
14140 {
14141 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14142 return error_mark_node;
14143 }
ec3fba51 14144 else if (!COMPLETE_TYPE_P (type))
4e81b788 14145 {
f187980b 14146 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
ec3fba51 14147 "type %qT", type);
4e81b788
MP
14148 return error_mark_node;
14149 }
ec3fba51 14150 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
f187980b 14151 warning_at (loc2, OPT_Wc___compat,
ec3fba51 14152 "C++ requires promoted type, not enum type, in %<va_arg%>");
f187980b 14153 return build_va_arg (loc2, expr, type);
72b5577d 14154}
acf0174b
JJ
14155
14156/* Return truthvalue of whether T1 is the same tree structure as T2.
6574d78e 14157 Return 1 if they are the same. Return false if they are different. */
acf0174b
JJ
14158
14159bool
14160c_tree_equal (tree t1, tree t2)
14161{
14162 enum tree_code code1, code2;
14163
14164 if (t1 == t2)
14165 return true;
14166 if (!t1 || !t2)
14167 return false;
14168
14169 for (code1 = TREE_CODE (t1);
14170 CONVERT_EXPR_CODE_P (code1)
14171 || code1 == NON_LVALUE_EXPR;
14172 code1 = TREE_CODE (t1))
14173 t1 = TREE_OPERAND (t1, 0);
14174 for (code2 = TREE_CODE (t2);
14175 CONVERT_EXPR_CODE_P (code2)
14176 || code2 == NON_LVALUE_EXPR;
14177 code2 = TREE_CODE (t2))
14178 t2 = TREE_OPERAND (t2, 0);
14179
14180 /* They might have become equal now. */
14181 if (t1 == t2)
14182 return true;
14183
14184 if (code1 != code2)
14185 return false;
14186
14187 switch (code1)
14188 {
14189 case INTEGER_CST:
807e902e 14190 return wi::eq_p (t1, t2);
acf0174b
JJ
14191
14192 case REAL_CST:
624d31fe 14193 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
acf0174b
JJ
14194
14195 case STRING_CST:
14196 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14197 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14198 TREE_STRING_LENGTH (t1));
14199
14200 case FIXED_CST:
14201 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14202 TREE_FIXED_CST (t2));
14203
14204 case COMPLEX_CST:
14205 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14206 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14207
14208 case VECTOR_CST:
14209 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14210
14211 case CONSTRUCTOR:
14212 /* We need to do this when determining whether or not two
14213 non-type pointer to member function template arguments
14214 are the same. */
14215 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14216 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14217 return false;
14218 {
14219 tree field, value;
14220 unsigned int i;
14221 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14222 {
14223 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14224 if (!c_tree_equal (field, elt2->index)
14225 || !c_tree_equal (value, elt2->value))
14226 return false;
14227 }
14228 }
14229 return true;
14230
14231 case TREE_LIST:
14232 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14233 return false;
14234 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14235 return false;
14236 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14237
14238 case SAVE_EXPR:
14239 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14240
14241 case CALL_EXPR:
14242 {
14243 tree arg1, arg2;
14244 call_expr_arg_iterator iter1, iter2;
14245 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14246 return false;
14247 for (arg1 = first_call_expr_arg (t1, &iter1),
14248 arg2 = first_call_expr_arg (t2, &iter2);
14249 arg1 && arg2;
14250 arg1 = next_call_expr_arg (&iter1),
14251 arg2 = next_call_expr_arg (&iter2))
14252 if (!c_tree_equal (arg1, arg2))
14253 return false;
14254 if (arg1 || arg2)
14255 return false;
14256 return true;
14257 }
14258
14259 case TARGET_EXPR:
14260 {
14261 tree o1 = TREE_OPERAND (t1, 0);
14262 tree o2 = TREE_OPERAND (t2, 0);
14263
14264 /* Special case: if either target is an unallocated VAR_DECL,
14265 it means that it's going to be unified with whatever the
14266 TARGET_EXPR is really supposed to initialize, so treat it
14267 as being equivalent to anything. */
0ae9bd27 14268 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
acf0174b
JJ
14269 && !DECL_RTL_SET_P (o1))
14270 /*Nop*/;
0ae9bd27 14271 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
acf0174b
JJ
14272 && !DECL_RTL_SET_P (o2))
14273 /*Nop*/;
14274 else if (!c_tree_equal (o1, o2))
14275 return false;
14276
14277 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14278 }
14279
14280 case COMPONENT_REF:
14281 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14282 return false;
14283 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14284
14285 case PARM_DECL:
14286 case VAR_DECL:
14287 case CONST_DECL:
14288 case FIELD_DECL:
14289 case FUNCTION_DECL:
14290 case IDENTIFIER_NODE:
14291 case SSA_NAME:
14292 return false;
14293
14294 case TREE_VEC:
14295 {
14296 unsigned ix;
14297 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14298 return false;
14299 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14300 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14301 TREE_VEC_ELT (t2, ix)))
14302 return false;
14303 return true;
14304 }
14305
14306 default:
14307 break;
14308 }
14309
14310 switch (TREE_CODE_CLASS (code1))
14311 {
14312 case tcc_unary:
14313 case tcc_binary:
14314 case tcc_comparison:
14315 case tcc_expression:
14316 case tcc_vl_exp:
14317 case tcc_reference:
14318 case tcc_statement:
14319 {
14320 int i, n = TREE_OPERAND_LENGTH (t1);
14321
14322 switch (code1)
14323 {
14324 case PREINCREMENT_EXPR:
14325 case PREDECREMENT_EXPR:
14326 case POSTINCREMENT_EXPR:
14327 case POSTDECREMENT_EXPR:
14328 n = 1;
14329 break;
14330 case ARRAY_REF:
14331 n = 2;
14332 break;
14333 default:
14334 break;
14335 }
14336
14337 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14338 && n != TREE_OPERAND_LENGTH (t2))
14339 return false;
14340
14341 for (i = 0; i < n; ++i)
14342 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14343 return false;
14344
14345 return true;
14346 }
14347
14348 case tcc_type:
14349 return comptypes (t1, t2);
14350 default:
14351 gcc_unreachable ();
14352 }
14353 /* We can get here with --disable-checking. */
14354 return false;
14355}
12893402
BI
14356
14357/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14358 spawn-helper and BODY is the newly created body for FNDECL. */
14359
14360void
14361cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14362{
14363 tree list = alloc_stmt_list ();
14364 tree frame = make_cilk_frame (fndecl);
14365 tree dtor = create_cilk_function_exit (frame, false, true);
14366 add_local_decl (cfun, frame);
14367
14368 DECL_SAVED_TREE (fndecl) = list;
12893402 14369
815d9cc6 14370 tree body_list = alloc_stmt_list ();
12893402
BI
14371 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14372 body = fold_build_cleanup_point_expr (void_type_node, body);
14373
14374 append_to_statement_list (body, &body_list);
14375 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14376 body_list, dtor), &list);
14377}
1807ffc1
MS
14378
14379/* Returns true when the function declaration FNDECL is implicit,
14380 introduced as a result of a call to an otherwise undeclared
14381 function, and false otherwise. */
14382
14383bool
14384c_decl_implicit (const_tree fndecl)
14385{
14386 return C_DECL_IMPLICIT (fndecl);
14387}