]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
Add some generic lambda test cases.
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
d1e082c2 2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6
ZW
28#include "coretypes.h"
29#include "tm.h"
400fbf9f 30#include "tree.h"
e57e265b 31#include "langhooks.h"
400fbf9f 32#include "c-tree.h"
29cc57cf 33#include "c-lang.h"
400fbf9f 34#include "flags.h"
ab87f8c8 35#include "intl.h"
672a6f42 36#include "target.h"
325c3691 37#include "tree-iterator.h"
6662d794
MLI
38#include "bitmap.h"
39#include "gimple.h"
acf0174b 40#include "tree-inline.h"
0645c1a2 41#include "omp-low.h"
61d3ce20 42#include "c-family/c-objc.h"
a212e43f 43#include "c-family/c-common.h"
de5a5fa1 44#include "c-family/c-ubsan.h"
325c3691 45
2ac2f164
JM
46/* Possible cases of implicit bad conversions. Used to select
47 diagnostic messages in convert_for_assignment. */
48enum impl_conv {
49 ic_argpass,
50 ic_assign,
51 ic_init,
52 ic_return
53};
54
bc4b653b
JM
55/* The level of nesting inside "__alignof__". */
56int in_alignof;
57
58/* The level of nesting inside "sizeof". */
59int in_sizeof;
60
61/* The level of nesting inside "typeof". */
62int in_typeof;
400fbf9f 63
1a4049e7
JJ
64/* The argument of last parsed sizeof expression, only to be tested
65 if expr.original_code == SIZEOF_EXPR. */
66tree c_last_sizeof_arg;
67
b71c7f8a 68/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 69 message within this initializer. */
b71c7f8a 70static int missing_braces_mentioned;
103b7b17 71
bf730f15
RS
72static int require_constant_value;
73static int require_constant_elements;
74
58f9752a 75static bool null_pointer_constant_p (const_tree);
f55ade6e 76static tree qualify_type (tree, tree);
dc5027f4
JM
77static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
78 bool *);
744aa42f 79static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
80static int function_types_compatible_p (const_tree, const_tree, bool *,
81 bool *);
82static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 83static tree lookup_field (tree, tree);
9771b263
DN
84static int convert_arguments (tree, vec<tree, va_gc> *, vec<tree, va_gc> *,
85 tree, tree);
db3927fb 86static tree pointer_diff (location_t, tree, tree);
744aa42f
ILT
87static tree convert_for_assignment (location_t, tree, tree, tree,
88 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
89static tree valid_compound_expr_initializer (tree, tree);
90static void push_string (const char *);
91static void push_member_name (tree);
f55ade6e
AJ
92static int spelling_length (void);
93static char *print_spelling (char *);
683d6ff9 94static void warning_init (int, const char *);
c2255bc4 95static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
a1e3b3d9
LB
96static void output_init_element (tree, tree, bool, tree, tree, int, bool,
97 struct obstack *);
98static void output_pending_init_elements (int, struct obstack *);
99static int set_designator (int, struct obstack *);
100static void push_range_stack (tree, struct obstack *);
101static void add_pending_init (tree, tree, tree, bool, struct obstack *);
102static void set_nonincremental_init (struct obstack *);
103static void set_nonincremental_init_from_string (tree, struct obstack *);
104static tree find_init_member (tree, struct obstack *);
f37acdf9 105static void readonly_warning (tree, enum lvalue_use);
7bd11157 106static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 107static void record_maybe_used_decl (tree);
dc5027f4 108static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
109\f
110/* Return true if EXP is a null pointer constant, false otherwise. */
111
112static bool
58f9752a 113null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
114{
115 /* This should really operate on c_expr structures, but they aren't
116 yet available everywhere required. */
117 tree type = TREE_TYPE (expr);
118 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 119 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
120 && integer_zerop (expr)
121 && (INTEGRAL_TYPE_P (type)
122 || (TREE_CODE (type) == POINTER_TYPE
123 && VOID_TYPE_P (TREE_TYPE (type))
124 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
125}
928c19bb
JM
126
127/* EXPR may appear in an unevaluated part of an integer constant
128 expression, but not in an evaluated part. Wrap it in a
129 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
130 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
131
132static tree
133note_integer_operands (tree expr)
134{
135 tree ret;
136 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
137 {
138 ret = copy_node (expr);
139 TREE_OVERFLOW (ret) = 1;
140 }
141 else
142 {
143 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
144 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
145 }
146 return ret;
147}
148
4d84fe7c
JM
149/* Having checked whether EXPR may appear in an unevaluated part of an
150 integer constant expression and found that it may, remove any
151 C_MAYBE_CONST_EXPR noting this fact and return the resulting
152 expression. */
153
154static inline tree
155remove_c_maybe_const_expr (tree expr)
156{
157 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
158 return C_MAYBE_CONST_EXPR_EXPR (expr);
159 else
160 return expr;
161}
162
f13c9b2c
AP
163\f/* This is a cache to hold if two types are compatible or not. */
164
165struct tagged_tu_seen_cache {
166 const struct tagged_tu_seen_cache * next;
58f9752a
KG
167 const_tree t1;
168 const_tree t2;
f13c9b2c
AP
169 /* The return value of tagged_types_tu_compatible_p if we had seen
170 these two types already. */
171 int val;
172};
173
174static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
175static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
176
400fbf9f
JW
177/* Do `exp = require_complete_type (exp);' to make sure exp
178 does not have an incomplete type. (That includes void types.) */
179
180tree
2f6e4e97 181require_complete_type (tree value)
400fbf9f
JW
182{
183 tree type = TREE_TYPE (value);
184
c3d5c3fa 185 if (value == error_mark_node || type == error_mark_node)
ea0f786b
CB
186 return error_mark_node;
187
400fbf9f 188 /* First, detect a valid value with a complete type. */
d0f062fb 189 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
190 return value;
191
7a228918 192 c_incomplete_type_error (value, type);
400fbf9f
JW
193 return error_mark_node;
194}
195
196/* Print an error message for invalid use of an incomplete type.
197 VALUE is the expression that was used (or 0 if that isn't known)
198 and TYPE is the type that was invalid. */
199
200void
ac7d7749 201c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 202{
5d5993dd 203 const char *type_code_string;
400fbf9f
JW
204
205 /* Avoid duplicate error message. */
206 if (TREE_CODE (type) == ERROR_MARK)
207 return;
208
209 if (value != 0 && (TREE_CODE (value) == VAR_DECL
210 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 211 error ("%qD has an incomplete type", value);
400fbf9f
JW
212 else
213 {
214 retry:
215 /* We must print an error message. Be clever about what it says. */
216
217 switch (TREE_CODE (type))
218 {
219 case RECORD_TYPE:
ab87f8c8 220 type_code_string = "struct";
400fbf9f
JW
221 break;
222
223 case UNION_TYPE:
ab87f8c8 224 type_code_string = "union";
400fbf9f
JW
225 break;
226
227 case ENUMERAL_TYPE:
ab87f8c8 228 type_code_string = "enum";
400fbf9f
JW
229 break;
230
231 case VOID_TYPE:
232 error ("invalid use of void expression");
233 return;
234
235 case ARRAY_TYPE:
236 if (TYPE_DOMAIN (type))
237 {
fba78abb
RH
238 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
239 {
240 error ("invalid use of flexible array member");
241 return;
242 }
400fbf9f
JW
243 type = TREE_TYPE (type);
244 goto retry;
245 }
246 error ("invalid use of array with unspecified bounds");
247 return;
248
249 default:
366de0ce 250 gcc_unreachable ();
400fbf9f
JW
251 }
252
253 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
254 error ("invalid use of undefined type %<%s %E%>",
255 type_code_string, TYPE_NAME (type));
400fbf9f
JW
256 else
257 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 258 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
259 }
260}
261
ab393bf1
NB
262/* Given a type, apply default promotions wrt unnamed function
263 arguments and return the new type. */
264
265tree
2f6e4e97 266c_type_promotes_to (tree type)
ab393bf1 267{
267bac10 268 tree ret = NULL_TREE;
ab393bf1 269
267bac10
JM
270 if (TYPE_MAIN_VARIANT (type) == float_type_node)
271 ret = double_type_node;
272 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
273 {
274 /* Preserve unsignedness if not really getting any wider. */
8df83eae 275 if (TYPE_UNSIGNED (type)
c22cacf3 276 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
277 ret = unsigned_type_node;
278 else
279 ret = integer_type_node;
ab393bf1
NB
280 }
281
267bac10
JM
282 if (ret != NULL_TREE)
283 return (TYPE_ATOMIC (type)
284 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
285 : ret);
286
ab393bf1
NB
287 return type;
288}
289
36c5e70a
BE
290/* Return true if between two named address spaces, whether there is a superset
291 named address space that encompasses both address spaces. If there is a
292 superset, return which address space is the superset. */
293
294static bool
295addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
296{
297 if (as1 == as2)
298 {
299 *common = as1;
300 return true;
301 }
302 else if (targetm.addr_space.subset_p (as1, as2))
303 {
304 *common = as2;
305 return true;
306 }
307 else if (targetm.addr_space.subset_p (as2, as1))
308 {
309 *common = as1;
310 return true;
311 }
312 else
313 return false;
314}
315
400fbf9f
JW
316/* Return a variant of TYPE which has all the type qualifiers of LIKE
317 as well as those of TYPE. */
318
319static tree
2f6e4e97 320qualify_type (tree type, tree like)
400fbf9f 321{
36c5e70a
BE
322 addr_space_t as_type = TYPE_ADDR_SPACE (type);
323 addr_space_t as_like = TYPE_ADDR_SPACE (like);
324 addr_space_t as_common;
325
326 /* If the two named address spaces are different, determine the common
327 superset address space. If there isn't one, raise an error. */
328 if (!addr_space_superset (as_type, as_like, &as_common))
329 {
330 as_common = as_type;
331 error ("%qT and %qT are in disjoint named address spaces",
332 type, like);
333 }
334
2f6e4e97 335 return c_build_qualified_type (type,
36c5e70a 336 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 337 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 338 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 339}
52ffd86e
MS
340
341/* Return true iff the given tree T is a variable length array. */
342
343bool
ac7d7749 344c_vla_type_p (const_tree t)
52ffd86e
MS
345{
346 if (TREE_CODE (t) == ARRAY_TYPE
347 && C_TYPE_VARIABLE_SIZE (t))
348 return true;
349 return false;
350}
400fbf9f 351\f
10bc1b1b 352/* Return the composite type of two compatible types.
5305f6d7 353
10bc1b1b
JM
354 We assume that comptypes has already been done and returned
355 nonzero; if that isn't so, this may crash. In particular, we
356 assume that qualifiers match. */
400fbf9f
JW
357
358tree
10bc1b1b 359composite_type (tree t1, tree t2)
400fbf9f 360{
b3694847
SS
361 enum tree_code code1;
362 enum tree_code code2;
4b027d16 363 tree attributes;
400fbf9f
JW
364
365 /* Save time if the two types are the same. */
366
367 if (t1 == t2) return t1;
368
369 /* If one type is nonsense, use the other. */
370 if (t1 == error_mark_node)
371 return t2;
372 if (t2 == error_mark_node)
373 return t1;
374
10bc1b1b
JM
375 code1 = TREE_CODE (t1);
376 code2 = TREE_CODE (t2);
377
d9525bec 378 /* Merge the attributes. */
5fd9b178 379 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 380
10bc1b1b
JM
381 /* If one is an enumerated type and the other is the compatible
382 integer type, the composite type might be either of the two
383 (DR#013 question 3). For consistency, use the enumerated type as
384 the composite type. */
400fbf9f 385
10bc1b1b
JM
386 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
387 return t1;
388 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
389 return t2;
75326e8c 390
366de0ce 391 gcc_assert (code1 == code2);
b6a10c9f 392
400fbf9f
JW
393 switch (code1)
394 {
400fbf9f 395 case POINTER_TYPE:
10bc1b1b 396 /* For two pointers, do this recursively on the target type. */
400fbf9f 397 {
3932261a
MM
398 tree pointed_to_1 = TREE_TYPE (t1);
399 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 400 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 401 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
402 t1 = build_type_attribute_variant (t1, attributes);
403 return qualify_type (t1, t2);
400fbf9f 404 }
400fbf9f
JW
405
406 case ARRAY_TYPE:
407 {
10bc1b1b 408 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
409 int quals;
410 tree unqual_elt;
ca8bdb78
JM
411 tree d1 = TYPE_DOMAIN (t1);
412 tree d2 = TYPE_DOMAIN (t2);
413 bool d1_variable, d2_variable;
414 bool d1_zero, d2_zero;
f6294de7 415 bool t1_complete, t2_complete;
46df2823 416
de46b2fe 417 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
418 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
419 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 420
f6294de7
JM
421 t1_complete = COMPLETE_TYPE_P (t1);
422 t2_complete = COMPLETE_TYPE_P (t2);
423
ca8bdb78
JM
424 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
425 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
426
427 d1_variable = (!d1_zero
428 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
429 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
430 d2_variable = (!d2_zero
431 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
432 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
433 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
434 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 435
400fbf9f 436 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
437 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
438 && (d2_variable || d2_zero || !d1_variable))
4b027d16 439 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
440 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
441 && (d1_variable || d1_zero || !d2_variable))
4b027d16 442 return build_type_attribute_variant (t2, attributes);
c22cacf3 443
de46b2fe
AP
444 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
445 return build_type_attribute_variant (t1, attributes);
446 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
447 return build_type_attribute_variant (t2, attributes);
c22cacf3 448
46df2823
JM
449 /* Merge the element types, and have a size if either arg has
450 one. We may have qualifiers on the element types. To set
451 up TYPE_MAIN_VARIANT correctly, we need to form the
452 composite of the unqualified types and add the qualifiers
453 back at the end. */
454 quals = TYPE_QUALS (strip_array_types (elt));
455 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
456 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
457 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
458 && (d2_variable
459 || d2_zero
460 || !d1_variable))
461 ? t1
462 : t2));
f6294de7
JM
463 /* Ensure a composite type involving a zero-length array type
464 is a zero-length type not an incomplete type. */
465 if (d1_zero && d2_zero
466 && (t1_complete || t2_complete)
467 && !COMPLETE_TYPE_P (t1))
468 {
469 TYPE_SIZE (t1) = bitsize_zero_node;
470 TYPE_SIZE_UNIT (t1) = size_zero_node;
471 }
46df2823 472 t1 = c_build_qualified_type (t1, quals);
de46b2fe 473 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
474 }
475
fcb99e7b
JJ
476 case ENUMERAL_TYPE:
477 case RECORD_TYPE:
478 case UNION_TYPE:
479 if (attributes != NULL)
480 {
481 /* Try harder not to create a new aggregate type. */
482 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
483 return t1;
484 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
485 return t2;
486 }
487 return build_type_attribute_variant (t1, attributes);
488
400fbf9f
JW
489 case FUNCTION_TYPE:
490 /* Function types: prefer the one that specified arg types.
491 If both do, merge the arg types. Also merge the return types. */
492 {
10bc1b1b 493 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
494 tree p1 = TYPE_ARG_TYPES (t1);
495 tree p2 = TYPE_ARG_TYPES (t2);
496 int len;
497 tree newargs, n;
498 int i;
499
500 /* Save space: see if the result is identical to one of the args. */
3f75a254 501 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 502 return build_type_attribute_variant (t1, attributes);
3f75a254 503 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 504 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
505
506 /* Simple way if one arg fails to specify argument types. */
507 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 508 {
fe7080d2
AP
509 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
510 t1 = build_type_attribute_variant (t1, attributes);
511 return qualify_type (t1, t2);
4b027d16 512 }
400fbf9f 513 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
514 {
515 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
516 t1 = build_type_attribute_variant (t1, attributes);
517 return qualify_type (t1, t2);
4b027d16 518 }
400fbf9f
JW
519
520 /* If both args specify argument types, we must merge the two
521 lists, argument by argument. */
2f4e8f2b 522
400fbf9f
JW
523 len = list_length (p1);
524 newargs = 0;
525
526 for (i = 0; i < len; i++)
8d9bfdc5 527 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
528
529 n = newargs;
530
531 for (; p1;
532 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
533 {
534 /* A null type means arg type is not specified.
535 Take whatever the other function type has. */
536 if (TREE_VALUE (p1) == 0)
537 {
538 TREE_VALUE (n) = TREE_VALUE (p2);
539 goto parm_done;
540 }
541 if (TREE_VALUE (p2) == 0)
542 {
543 TREE_VALUE (n) = TREE_VALUE (p1);
544 goto parm_done;
545 }
2f6e4e97 546
400fbf9f
JW
547 /* Given wait (union {union wait *u; int *i} *)
548 and wait (union wait *),
549 prefer union wait * as type of parm. */
550 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
551 && TREE_VALUE (p1) != TREE_VALUE (p2))
552 {
553 tree memb;
58cb41e6
JJ
554 tree mv2 = TREE_VALUE (p2);
555 if (mv2 && mv2 != error_mark_node
556 && TREE_CODE (mv2) != ARRAY_TYPE)
557 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 558 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 559 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
560 {
561 tree mv3 = TREE_TYPE (memb);
562 if (mv3 && mv3 != error_mark_node
563 && TREE_CODE (mv3) != ARRAY_TYPE)
564 mv3 = TYPE_MAIN_VARIANT (mv3);
565 if (comptypes (mv3, mv2))
566 {
567 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
568 TREE_VALUE (p2));
c1771a20 569 pedwarn (input_location, OPT_Wpedantic,
fcf73884 570 "function types not truly compatible in ISO C");
58cb41e6
JJ
571 goto parm_done;
572 }
573 }
400fbf9f
JW
574 }
575 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
576 && TREE_VALUE (p2) != TREE_VALUE (p1))
577 {
578 tree memb;
58cb41e6
JJ
579 tree mv1 = TREE_VALUE (p1);
580 if (mv1 && mv1 != error_mark_node
581 && TREE_CODE (mv1) != ARRAY_TYPE)
582 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 583 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 584 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
585 {
586 tree mv3 = TREE_TYPE (memb);
587 if (mv3 && mv3 != error_mark_node
588 && TREE_CODE (mv3) != ARRAY_TYPE)
589 mv3 = TYPE_MAIN_VARIANT (mv3);
590 if (comptypes (mv3, mv1))
591 {
592 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
593 TREE_VALUE (p1));
c1771a20 594 pedwarn (input_location, OPT_Wpedantic,
fcf73884 595 "function types not truly compatible in ISO C");
58cb41e6
JJ
596 goto parm_done;
597 }
598 }
400fbf9f 599 }
10bc1b1b 600 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
601 parm_done: ;
602 }
603
4b027d16 604 t1 = build_function_type (valtype, newargs);
fe7080d2 605 t1 = qualify_type (t1, t2);
0f41302f 606 /* ... falls through ... */
400fbf9f
JW
607 }
608
609 default:
4b027d16 610 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
611 }
612
613}
10bc1b1b
JM
614
615/* Return the type of a conditional expression between pointers to
616 possibly differently qualified versions of compatible types.
617
618 We assume that comp_target_types has already been done and returned
619 nonzero; if that isn't so, this may crash. */
620
621static tree
622common_pointer_type (tree t1, tree t2)
623{
624 tree attributes;
46df2823
JM
625 tree pointed_to_1, mv1;
626 tree pointed_to_2, mv2;
10bc1b1b 627 tree target;
eb1387a0 628 unsigned target_quals;
36c5e70a
BE
629 addr_space_t as1, as2, as_common;
630 int quals1, quals2;
10bc1b1b
JM
631
632 /* Save time if the two types are the same. */
633
634 if (t1 == t2) return t1;
635
636 /* If one type is nonsense, use the other. */
637 if (t1 == error_mark_node)
638 return t2;
639 if (t2 == error_mark_node)
640 return t1;
641
366de0ce 642 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 643 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
644
645 /* Merge the attributes. */
646 attributes = targetm.merge_type_attributes (t1, t2);
647
648 /* Find the composite type of the target types, and combine the
46df2823
JM
649 qualifiers of the two types' targets. Do not lose qualifiers on
650 array element types by taking the TYPE_MAIN_VARIANT. */
651 mv1 = pointed_to_1 = TREE_TYPE (t1);
652 mv2 = pointed_to_2 = TREE_TYPE (t2);
653 if (TREE_CODE (mv1) != ARRAY_TYPE)
654 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
655 if (TREE_CODE (mv2) != ARRAY_TYPE)
656 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
657 target = composite_type (mv1, mv2);
eb1387a0
RG
658
659 /* For function types do not merge const qualifiers, but drop them
660 if used inconsistently. The middle-end uses these to mark const
661 and noreturn functions. */
36c5e70a
BE
662 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
663 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
664
eb1387a0 665 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 666 target_quals = (quals1 & quals2);
eb1387a0 667 else
36c5e70a
BE
668 target_quals = (quals1 | quals2);
669
670 /* If the two named address spaces are different, determine the common
671 superset address space. This is guaranteed to exist due to the
672 assumption that comp_target_type returned non-zero. */
673 as1 = TYPE_ADDR_SPACE (pointed_to_1);
674 as2 = TYPE_ADDR_SPACE (pointed_to_2);
675 if (!addr_space_superset (as1, as2, &as_common))
676 gcc_unreachable ();
677
678 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
679
eb1387a0 680 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
681 return build_type_attribute_variant (t1, attributes);
682}
683
684/* Return the common type for two arithmetic types under the usual
685 arithmetic conversions. The default conversions have already been
686 applied, and enumerated types converted to their compatible integer
687 types. The resulting type is unqualified and has no attributes.
688
689 This is the type for the result of most arithmetic operations
690 if the operands have the given two types. */
691
ccf7f880
JJ
692static tree
693c_common_type (tree t1, tree t2)
10bc1b1b
JM
694{
695 enum tree_code code1;
696 enum tree_code code2;
697
698 /* If one type is nonsense, use the other. */
699 if (t1 == error_mark_node)
700 return t2;
701 if (t2 == error_mark_node)
702 return t1;
703
704 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
705 t1 = TYPE_MAIN_VARIANT (t1);
706
707 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
708 t2 = TYPE_MAIN_VARIANT (t2);
709
710 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
711 t1 = build_type_attribute_variant (t1, NULL_TREE);
712
713 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
714 t2 = build_type_attribute_variant (t2, NULL_TREE);
715
716 /* Save time if the two types are the same. */
717
718 if (t1 == t2) return t1;
719
720 code1 = TREE_CODE (t1);
721 code2 = TREE_CODE (t2);
722
366de0ce 723 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
724 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
725 || code1 == INTEGER_TYPE);
366de0ce 726 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
727 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
728 || code2 == INTEGER_TYPE);
10bc1b1b 729
5fc89bfd
JJ
730 /* When one operand is a decimal float type, the other operand cannot be
731 a generic float type or a complex type. We also disallow vector types
732 here. */
733 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
734 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
735 {
736 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
737 {
738 error ("can%'t mix operands of decimal float and vector types");
739 return error_mark_node;
740 }
741 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
742 {
743 error ("can%'t mix operands of decimal float and complex types");
744 return error_mark_node;
745 }
746 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
747 {
748 error ("can%'t mix operands of decimal float and other float types");
749 return error_mark_node;
750 }
751 }
752
10bc1b1b
JM
753 /* If one type is a vector type, return that type. (How the usual
754 arithmetic conversions apply to the vector types extension is not
755 precisely specified.) */
756 if (code1 == VECTOR_TYPE)
757 return t1;
758
759 if (code2 == VECTOR_TYPE)
760 return t2;
761
762 /* If one type is complex, form the common type of the non-complex
763 components, then make that complex. Use T1 or T2 if it is the
764 required type. */
765 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
766 {
767 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
768 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 769 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
770
771 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
772 return t1;
773 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
774 return t2;
775 else
776 return build_complex_type (subtype);
777 }
778
779 /* If only one is real, use it as the result. */
780
781 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
782 return t1;
783
784 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
785 return t2;
786
9a8ce21f
JG
787 /* If both are real and either are decimal floating point types, use
788 the decimal floating point type with the greater precision. */
789
790 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
791 {
792 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
793 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
794 return dfloat128_type_node;
795 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
796 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
797 return dfloat64_type_node;
798 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
799 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
800 return dfloat32_type_node;
801 }
802
ab22c1fa
CF
803 /* Deal with fixed-point types. */
804 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
805 {
806 unsigned int unsignedp = 0, satp = 0;
807 enum machine_mode m1, m2;
808 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
809
810 m1 = TYPE_MODE (t1);
811 m2 = TYPE_MODE (t2);
812
813 /* If one input type is saturating, the result type is saturating. */
814 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
815 satp = 1;
816
817 /* If both fixed-point types are unsigned, the result type is unsigned.
818 When mixing fixed-point and integer types, follow the sign of the
819 fixed-point type.
820 Otherwise, the result type is signed. */
821 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
822 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
823 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
824 && TYPE_UNSIGNED (t1))
825 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
826 && TYPE_UNSIGNED (t2)))
827 unsignedp = 1;
828
829 /* The result type is signed. */
830 if (unsignedp == 0)
831 {
832 /* If the input type is unsigned, we need to convert to the
833 signed type. */
834 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
835 {
d75d71e0 836 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
837 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
838 mclass = MODE_FRACT;
839 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
840 mclass = MODE_ACCUM;
841 else
842 gcc_unreachable ();
843 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
844 }
845 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
846 {
d75d71e0 847 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
848 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
849 mclass = MODE_FRACT;
850 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
851 mclass = MODE_ACCUM;
852 else
853 gcc_unreachable ();
854 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
855 }
856 }
857
858 if (code1 == FIXED_POINT_TYPE)
859 {
860 fbit1 = GET_MODE_FBIT (m1);
861 ibit1 = GET_MODE_IBIT (m1);
862 }
863 else
864 {
865 fbit1 = 0;
866 /* Signed integers need to subtract one sign bit. */
867 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
868 }
869
870 if (code2 == FIXED_POINT_TYPE)
871 {
872 fbit2 = GET_MODE_FBIT (m2);
873 ibit2 = GET_MODE_IBIT (m2);
874 }
875 else
876 {
877 fbit2 = 0;
878 /* Signed integers need to subtract one sign bit. */
879 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
880 }
881
882 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
883 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
884 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
885 satp);
886 }
887
10bc1b1b
JM
888 /* Both real or both integers; use the one with greater precision. */
889
890 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
891 return t1;
892 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
893 return t2;
894
895 /* Same precision. Prefer long longs to longs to ints when the
896 same precision, following the C99 rules on integer type rank
897 (which are equivalent to the C90 rules for C90 types). */
898
899 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
900 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
901 return long_long_unsigned_type_node;
902
903 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
905 {
906 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
907 return long_long_unsigned_type_node;
908 else
c22cacf3 909 return long_long_integer_type_node;
10bc1b1b
JM
910 }
911
912 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
914 return long_unsigned_type_node;
915
916 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
918 {
919 /* But preserve unsignedness from the other type,
920 since long cannot hold all the values of an unsigned int. */
921 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
922 return long_unsigned_type_node;
923 else
924 return long_integer_type_node;
925 }
926
927 /* Likewise, prefer long double to double even if same size. */
928 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
929 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
930 return long_double_type_node;
931
2531a1d9
JR
932 /* Likewise, prefer double to float even if same size.
933 We got a couple of embedded targets with 32 bit doubles, and the
934 pdp11 might have 64 bit floats. */
935 if (TYPE_MAIN_VARIANT (t1) == double_type_node
936 || TYPE_MAIN_VARIANT (t2) == double_type_node)
937 return double_type_node;
938
10bc1b1b
JM
939 /* Otherwise prefer the unsigned one. */
940
941 if (TYPE_UNSIGNED (t1))
942 return t1;
943 else
944 return t2;
945}
400fbf9f 946\f
5922c215
JM
947/* Wrapper around c_common_type that is used by c-common.c and other
948 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
949 are allowed here and are converted to their compatible integer types.
950 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
951 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
952tree
953common_type (tree t1, tree t2)
954{
955 if (TREE_CODE (t1) == ENUMERAL_TYPE)
956 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
957 if (TREE_CODE (t2) == ENUMERAL_TYPE)
958 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
959
960 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
961 if (TREE_CODE (t1) == BOOLEAN_TYPE
962 && TREE_CODE (t2) == BOOLEAN_TYPE)
963 return boolean_type_node;
964
965 /* If either type is BOOLEAN_TYPE, then return the other. */
966 if (TREE_CODE (t1) == BOOLEAN_TYPE)
967 return t2;
968 if (TREE_CODE (t2) == BOOLEAN_TYPE)
969 return t1;
970
ccf7f880
JJ
971 return c_common_type (t1, t2);
972}
f13c9b2c 973
400fbf9f
JW
974/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
975 or various other operations. Return 2 if they are compatible
976 but a warning may be needed if you use them together. */
977
978int
132da1a5 979comptypes (tree type1, tree type2)
f13c9b2c
AP
980{
981 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
982 int val;
983
dc5027f4 984 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
985 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
986
987 return val;
988}
989
990/* Like comptypes, but if it returns non-zero because enum and int are
991 compatible, it sets *ENUM_AND_INT_P to true. */
992
993static int
994comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
995{
996 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
997 int val;
998
dc5027f4
JM
999 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1000 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1001
1002 return val;
1003}
1004
1005/* Like comptypes, but if it returns nonzero for different types, it
1006 sets *DIFFERENT_TYPES_P to true. */
1007
1008int
1009comptypes_check_different_types (tree type1, tree type2,
1010 bool *different_types_p)
1011{
1012 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1013 int val;
1014
1015 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1016 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1017
f13c9b2c 1018 return val;
c22cacf3
MS
1019}
1020\f
f13c9b2c
AP
1021/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1022 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1023 but a warning may be needed if you use them together. If
1024 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1025 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1026 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1027 NULL, and the types are compatible but different enough not to be
48b0b196 1028 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1029 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1030 false, but may or may not be set if the types are incompatible.
1031 This differs from comptypes, in that we don't free the seen
1032 types. */
f13c9b2c
AP
1033
1034static int
dc5027f4
JM
1035comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1036 bool *different_types_p)
400fbf9f 1037{
58f9752a
KG
1038 const_tree t1 = type1;
1039 const_tree t2 = type2;
4b027d16 1040 int attrval, val;
400fbf9f
JW
1041
1042 /* Suppress errors caused by previously reported errors. */
1043
8d47dfc5
RH
1044 if (t1 == t2 || !t1 || !t2
1045 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1046 return 1;
1047
bca63328
JM
1048 /* Enumerated types are compatible with integer types, but this is
1049 not transitive: two enumerated types in the same translation unit
1050 are compatible with each other only if they are the same type. */
400fbf9f 1051
bca63328 1052 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1053 {
1054 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1055 if (TREE_CODE (t2) != VOID_TYPE)
1056 {
1057 if (enum_and_int_p != NULL)
1058 *enum_and_int_p = true;
1059 if (different_types_p != NULL)
1060 *different_types_p = true;
1061 }
744aa42f 1062 }
bca63328 1063 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1064 {
1065 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1066 if (TREE_CODE (t1) != VOID_TYPE)
1067 {
1068 if (enum_and_int_p != NULL)
1069 *enum_and_int_p = true;
1070 if (different_types_p != NULL)
1071 *different_types_p = true;
1072 }
744aa42f 1073 }
400fbf9f
JW
1074
1075 if (t1 == t2)
1076 return 1;
1077
1078 /* Different classes of types can't be compatible. */
1079
3aeb3655
EC
1080 if (TREE_CODE (t1) != TREE_CODE (t2))
1081 return 0;
400fbf9f 1082
118a3a8b 1083 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1084
3932261a 1085 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1086 return 0;
1087
08632da2
RS
1088 /* Allow for two different type nodes which have essentially the same
1089 definition. Note that we already checked for equality of the type
38e01259 1090 qualifiers (just above). */
400fbf9f 1091
46df2823
JM
1092 if (TREE_CODE (t1) != ARRAY_TYPE
1093 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1094 return 1;
1095
4b027d16 1096 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1097 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1098 return 0;
1099
1100 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1101 val = 0;
1102
400fbf9f
JW
1103 switch (TREE_CODE (t1))
1104 {
1105 case POINTER_TYPE:
106f5de5
UW
1106 /* Do not remove mode or aliasing information. */
1107 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1108 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1109 break;
4b027d16 1110 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1111 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1112 enum_and_int_p, different_types_p));
4b027d16 1113 break;
400fbf9f
JW
1114
1115 case FUNCTION_TYPE:
dc5027f4
JM
1116 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1117 different_types_p);
4b027d16 1118 break;
400fbf9f
JW
1119
1120 case ARRAY_TYPE:
1121 {
400fbf9f
JW
1122 tree d1 = TYPE_DOMAIN (t1);
1123 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1124 bool d1_variable, d2_variable;
1125 bool d1_zero, d2_zero;
4b027d16 1126 val = 1;
400fbf9f
JW
1127
1128 /* Target types must match incl. qualifiers. */
1129 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1130 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1131 enum_and_int_p,
1132 different_types_p)))
400fbf9f
JW
1133 return 0;
1134
dc5027f4
JM
1135 if (different_types_p != NULL
1136 && (d1 == 0) != (d2 == 0))
1137 *different_types_p = true;
400fbf9f 1138 /* Sizes must match unless one is missing or variable. */
3f85558f 1139 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1140 break;
400fbf9f 1141
3f75a254
JM
1142 d1_zero = !TYPE_MAX_VALUE (d1);
1143 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1144
3f75a254 1145 d1_variable = (!d1_zero
3f85558f
RH
1146 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1147 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1148 d2_variable = (!d2_zero
3f85558f
RH
1149 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1150 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1151 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1152 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1153
dc5027f4
JM
1154 if (different_types_p != NULL
1155 && d1_variable != d2_variable)
1156 *different_types_p = true;
3f85558f
RH
1157 if (d1_variable || d2_variable)
1158 break;
1159 if (d1_zero && d2_zero)
1160 break;
1161 if (d1_zero || d2_zero
3f75a254
JM
1162 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1163 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1164 val = 0;
1165
c22cacf3 1166 break;
400fbf9f
JW
1167 }
1168
d1bd0ded 1169 case ENUMERAL_TYPE:
58393038 1170 case RECORD_TYPE:
d1bd0ded 1171 case UNION_TYPE:
766beae1 1172 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1173 {
fcb99e7b
JJ
1174 tree a1 = TYPE_ATTRIBUTES (t1);
1175 tree a2 = TYPE_ATTRIBUTES (t2);
1176
1177 if (! attribute_list_contained (a1, a2)
1178 && ! attribute_list_contained (a2, a1))
1179 break;
1180
f13c9b2c 1181 if (attrval != 2)
dc5027f4
JM
1182 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1183 different_types_p);
1184 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1185 different_types_p);
f13c9b2c 1186 }
4b027d16 1187 break;
e9a25f70 1188
62e1dfcf 1189 case VECTOR_TYPE:
744aa42f
ILT
1190 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1191 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1192 enum_and_int_p, different_types_p));
62e1dfcf
NC
1193 break;
1194
e9a25f70
JL
1195 default:
1196 break;
400fbf9f 1197 }
4b027d16 1198 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1199}
1200
36c5e70a
BE
1201/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1202 their qualifiers, except for named address spaces. If the pointers point to
1203 different named addresses, then we must determine if one address space is a
1204 subset of the other. */
400fbf9f
JW
1205
1206static int
744aa42f 1207comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1208{
392202b0 1209 int val;
36c5e70a
BE
1210 tree mvl = TREE_TYPE (ttl);
1211 tree mvr = TREE_TYPE (ttr);
1212 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1213 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1214 addr_space_t as_common;
744aa42f 1215 bool enum_and_int_p;
8b40563c 1216
36c5e70a
BE
1217 /* Fail if pointers point to incompatible address spaces. */
1218 if (!addr_space_superset (asl, asr, &as_common))
1219 return 0;
1220
46df2823
JM
1221 /* Do not lose qualifiers on element types of array types that are
1222 pointer targets by taking their TYPE_MAIN_VARIANT. */
46df2823 1223 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
1224 mvl = (TYPE_ATOMIC (mvl)
1225 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1226 : TYPE_MAIN_VARIANT (mvl));
46df2823 1227 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
1228 mvr = (TYPE_ATOMIC (mvr)
1229 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1230 : TYPE_MAIN_VARIANT (mvr));
744aa42f
ILT
1231 enum_and_int_p = false;
1232 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1233
fcf73884 1234 if (val == 2)
c1771a20 1235 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1236
1237 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1238 warning_at (location, OPT_Wc___compat,
1239 "pointer target types incompatible in C++");
1240
400fbf9f
JW
1241 return val;
1242}
1243\f
1244/* Subroutines of `comptypes'. */
1245
f75fbaf7
ZW
1246/* Determine whether two trees derive from the same translation unit.
1247 If the CONTEXT chain ends in a null, that tree's context is still
1248 being parsed, so if two trees have context chains ending in null,
766beae1 1249 they're in the same translation unit. */
f75fbaf7 1250int
58f9752a 1251same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1252{
1253 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1254 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1255 {
6615c446
JO
1256 case tcc_declaration:
1257 t1 = DECL_CONTEXT (t1); break;
1258 case tcc_type:
1259 t1 = TYPE_CONTEXT (t1); break;
1260 case tcc_exceptional:
1261 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1262 default: gcc_unreachable ();
766beae1
ZW
1263 }
1264
1265 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1266 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1267 {
6615c446
JO
1268 case tcc_declaration:
1269 t2 = DECL_CONTEXT (t2); break;
1270 case tcc_type:
1271 t2 = TYPE_CONTEXT (t2); break;
1272 case tcc_exceptional:
1273 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1274 default: gcc_unreachable ();
766beae1
ZW
1275 }
1276
1277 return t1 == t2;
1278}
1279
f13c9b2c 1280/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1281
f13c9b2c 1282static struct tagged_tu_seen_cache *
58f9752a 1283alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1284{
cceb1885 1285 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1286 tu->next = tagged_tu_seen_base;
1287 tu->t1 = t1;
1288 tu->t2 = t2;
c22cacf3 1289
f13c9b2c 1290 tagged_tu_seen_base = tu;
c22cacf3 1291
f13c9b2c
AP
1292 /* The C standard says that two structures in different translation
1293 units are compatible with each other only if the types of their
1294 fields are compatible (among other things). We assume that they
1295 are compatible until proven otherwise when building the cache.
1296 An example where this can occur is:
1297 struct a
1298 {
1299 struct a *next;
1300 };
1301 If we are comparing this against a similar struct in another TU,
c83eecad 1302 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1303 loop. */
1304 tu->val = 1;
1305 return tu;
1306}
d1bd0ded 1307
f13c9b2c 1308/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1309
f13c9b2c
AP
1310static void
1311free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1312{
1313 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1314 while (tu != tu_til)
1315 {
741ac903
KG
1316 const struct tagged_tu_seen_cache *const tu1
1317 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1318 tu = tu1->next;
b1d5455a 1319 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1320 }
1321 tagged_tu_seen_base = tu_til;
1322}
d1bd0ded
GK
1323
1324/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1325 compatible. If the two types are not the same (which has been
1326 checked earlier), this can only happen when multiple translation
1327 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1328 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1329 comptypes_internal. */
d1bd0ded
GK
1330
1331static int
744aa42f 1332tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1333 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1334{
1335 tree s1, s2;
1336 bool needs_warning = false;
3aeb3655 1337
d1bd0ded
GK
1338 /* We have to verify that the tags of the types are the same. This
1339 is harder than it looks because this may be a typedef, so we have
1340 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1341 typedef...
1342 In the case of compiler-created builtin structs the TYPE_DECL
1343 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1344 while (TYPE_NAME (t1)
1345 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1346 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1347 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1348
dea984dc
ILT
1349 while (TYPE_NAME (t2)
1350 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1351 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1352 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1353
1354 /* C90 didn't have the requirement that the two tags be the same. */
1355 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1356 return 0;
3aeb3655 1357
d1bd0ded
GK
1358 /* C90 didn't say what happened if one or both of the types were
1359 incomplete; we choose to follow C99 rules here, which is that they
1360 are compatible. */
1361 if (TYPE_SIZE (t1) == NULL
1362 || TYPE_SIZE (t2) == NULL)
1363 return 1;
3aeb3655 1364
d1bd0ded 1365 {
f13c9b2c 1366 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1367 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1368 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1369 return tts_i->val;
d1bd0ded 1370 }
3aeb3655 1371
d1bd0ded
GK
1372 switch (TREE_CODE (t1))
1373 {
1374 case ENUMERAL_TYPE:
1375 {
f13c9b2c 1376 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1377 /* Speed up the case where the type values are in the same order. */
1378 tree tv1 = TYPE_VALUES (t1);
1379 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1380
c22cacf3 1381 if (tv1 == tv2)
f13c9b2c
AP
1382 {
1383 return 1;
1384 }
3aeb3655 1385
c22cacf3
MS
1386 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1387 {
1388 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1389 break;
1390 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1391 {
c22cacf3 1392 tu->val = 0;
f13c9b2c
AP
1393 return 0;
1394 }
c22cacf3 1395 }
3aeb3655 1396
c22cacf3 1397 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1398 {
1399 return 1;
1400 }
c22cacf3 1401 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1402 {
1403 tu->val = 0;
1404 return 0;
1405 }
3aeb3655 1406
d1bd0ded 1407 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1408 {
1409 tu->val = 0;
1410 return 0;
1411 }
3aeb3655 1412
d1bd0ded
GK
1413 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1414 {
1415 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1416 if (s2 == NULL
1417 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1418 {
1419 tu->val = 0;
1420 return 0;
1421 }
d1bd0ded
GK
1422 }
1423 return 1;
1424 }
1425
1426 case UNION_TYPE:
1427 {
f13c9b2c 1428 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1429 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1430 {
1431 tu->val = 0;
1432 return 0;
1433 }
c22cacf3 1434
f13c9b2c
AP
1435 /* Speed up the common case where the fields are in the same order. */
1436 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1437 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1438 {
1439 int result;
c22cacf3 1440
3ae4d3cc 1441 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1442 break;
744aa42f 1443 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1444 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1445
1446 if (result != 1 && !DECL_NAME (s1))
1447 break;
f13c9b2c
AP
1448 if (result == 0)
1449 {
1450 tu->val = 0;
1451 return 0;
1452 }
1453 if (result == 2)
1454 needs_warning = true;
1455
1456 if (TREE_CODE (s1) == FIELD_DECL
1457 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1458 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1459 {
1460 tu->val = 0;
1461 return 0;
1462 }
1463 }
1464 if (!s1 && !s2)
1465 {
1466 tu->val = needs_warning ? 2 : 1;
1467 return tu->val;
1468 }
d1bd0ded 1469
910ad8de 1470 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1471 {
1472 bool ok = false;
3aeb3655 1473
910ad8de 1474 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1475 if (DECL_NAME (s1) == DECL_NAME (s2))
1476 {
1477 int result;
1478
744aa42f 1479 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1480 enum_and_int_p,
1481 different_types_p);
3ae4d3cc
AO
1482
1483 if (result != 1 && !DECL_NAME (s1))
1484 continue;
1485 if (result == 0)
1486 {
1487 tu->val = 0;
1488 return 0;
1489 }
1490 if (result == 2)
1491 needs_warning = true;
1492
1493 if (TREE_CODE (s1) == FIELD_DECL
1494 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1495 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1496 break;
3ae4d3cc
AO
1497
1498 ok = true;
1499 break;
1500 }
3f75a254 1501 if (!ok)
f13c9b2c
AP
1502 {
1503 tu->val = 0;
1504 return 0;
1505 }
d1bd0ded 1506 }
f13c9b2c
AP
1507 tu->val = needs_warning ? 2 : 10;
1508 return tu->val;
d1bd0ded
GK
1509 }
1510
1511 case RECORD_TYPE:
1512 {
c22cacf3 1513 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1514
1515 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1516 s1 && s2;
910ad8de 1517 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1518 {
1519 int result;
1520 if (TREE_CODE (s1) != TREE_CODE (s2)
1521 || DECL_NAME (s1) != DECL_NAME (s2))
1522 break;
744aa42f 1523 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1524 enum_and_int_p, different_types_p);
d1bd0ded
GK
1525 if (result == 0)
1526 break;
1527 if (result == 2)
1528 needs_warning = true;
3aeb3655 1529
d1bd0ded
GK
1530 if (TREE_CODE (s1) == FIELD_DECL
1531 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1532 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1533 break;
1534 }
d1bd0ded 1535 if (s1 && s2)
f13c9b2c
AP
1536 tu->val = 0;
1537 else
1538 tu->val = needs_warning ? 2 : 1;
1539 return tu->val;
d1bd0ded
GK
1540 }
1541
1542 default:
366de0ce 1543 gcc_unreachable ();
d1bd0ded
GK
1544 }
1545}
1546
400fbf9f
JW
1547/* Return 1 if two function types F1 and F2 are compatible.
1548 If either type specifies no argument types,
1549 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1550 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1551 the other must specify that number of self-promoting arg types.
744aa42f 1552 Otherwise, the argument types must match.
dc5027f4 1553 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1554
1555static int
744aa42f 1556function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1557 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1558{
1559 tree args1, args2;
1560 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1561 int val = 1;
1562 int val1;
a6fdc086
GK
1563 tree ret1, ret2;
1564
1565 ret1 = TREE_TYPE (f1);
1566 ret2 = TREE_TYPE (f2);
1567
e508a019
JM
1568 /* 'volatile' qualifiers on a function's return type used to mean
1569 the function is noreturn. */
1570 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1571 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1572 if (TYPE_VOLATILE (ret1))
1573 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1574 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1575 if (TYPE_VOLATILE (ret2))
1576 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1577 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1578 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1579 if (val == 0)
400fbf9f
JW
1580 return 0;
1581
1582 args1 = TYPE_ARG_TYPES (f1);
1583 args2 = TYPE_ARG_TYPES (f2);
1584
dc5027f4
JM
1585 if (different_types_p != NULL
1586 && (args1 == 0) != (args2 == 0))
1587 *different_types_p = true;
1588
400fbf9f
JW
1589 /* An unspecified parmlist matches any specified parmlist
1590 whose argument types don't need default promotions. */
1591
1592 if (args1 == 0)
1593 {
1594 if (!self_promoting_args_p (args2))
1595 return 0;
1596 /* If one of these types comes from a non-prototype fn definition,
1597 compare that with the other type's arglist.
3176a0c2 1598 If they don't match, ask for a warning (but no error). */
400fbf9f 1599 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1600 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1601 enum_and_int_p, different_types_p))
400fbf9f
JW
1602 val = 2;
1603 return val;
1604 }
1605 if (args2 == 0)
1606 {
1607 if (!self_promoting_args_p (args1))
1608 return 0;
1609 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1610 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1611 enum_and_int_p, different_types_p))
400fbf9f
JW
1612 val = 2;
1613 return val;
1614 }
1615
1616 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1617 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1618 different_types_p);
400fbf9f
JW
1619 return val1 != 1 ? val1 : val;
1620}
1621
744aa42f
ILT
1622/* Check two lists of types for compatibility, returning 0 for
1623 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1624 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1625 comptypes_internal. */
400fbf9f
JW
1626
1627static int
744aa42f 1628type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1629 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1630{
1631 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1632 int val = 1;
9d5f3e49 1633 int newval = 0;
400fbf9f
JW
1634
1635 while (1)
1636 {
46df2823 1637 tree a1, mv1, a2, mv2;
400fbf9f
JW
1638 if (args1 == 0 && args2 == 0)
1639 return val;
1640 /* If one list is shorter than the other,
1641 they fail to match. */
1642 if (args1 == 0 || args2 == 0)
1643 return 0;
46df2823
JM
1644 mv1 = a1 = TREE_VALUE (args1);
1645 mv2 = a2 = TREE_VALUE (args2);
1646 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1647 mv1 = (TYPE_ATOMIC (mv1)
1648 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1649 TYPE_QUAL_ATOMIC)
1650 : TYPE_MAIN_VARIANT (mv1));
46df2823 1651 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1652 mv2 = (TYPE_ATOMIC (mv2)
1653 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1654 TYPE_QUAL_ATOMIC)
1655 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1656 /* A null pointer instead of a type
1657 means there is supposed to be an argument
1658 but nothing is specified about what type it has.
1659 So match anything that self-promotes. */
dc5027f4
JM
1660 if (different_types_p != NULL
1661 && (a1 == 0) != (a2 == 0))
1662 *different_types_p = true;
46df2823 1663 if (a1 == 0)
400fbf9f 1664 {
46df2823 1665 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1666 return 0;
1667 }
46df2823 1668 else if (a2 == 0)
400fbf9f 1669 {
46df2823 1670 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1671 return 0;
1672 }
8f5b6d29 1673 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1674 else if (TREE_CODE (a1) == ERROR_MARK
1675 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1676 ;
dc5027f4
JM
1677 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1678 different_types_p)))
400fbf9f 1679 {
dc5027f4
JM
1680 if (different_types_p != NULL)
1681 *different_types_p = true;
400fbf9f
JW
1682 /* Allow wait (union {union wait *u; int *i} *)
1683 and wait (union wait *) to be compatible. */
46df2823
JM
1684 if (TREE_CODE (a1) == UNION_TYPE
1685 && (TYPE_NAME (a1) == 0
ebf0bf7f 1686 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1687 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1688 && tree_int_cst_equal (TYPE_SIZE (a1),
1689 TYPE_SIZE (a2)))
400fbf9f
JW
1690 {
1691 tree memb;
46df2823 1692 for (memb = TYPE_FIELDS (a1);
910ad8de 1693 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1694 {
1695 tree mv3 = TREE_TYPE (memb);
1696 if (mv3 && mv3 != error_mark_node
1697 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1698 mv3 = (TYPE_ATOMIC (mv3)
1699 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1700 TYPE_QUAL_ATOMIC)
1701 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1702 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1703 different_types_p))
58cb41e6
JJ
1704 break;
1705 }
400fbf9f
JW
1706 if (memb == 0)
1707 return 0;
1708 }
46df2823
JM
1709 else if (TREE_CODE (a2) == UNION_TYPE
1710 && (TYPE_NAME (a2) == 0
ebf0bf7f 1711 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1712 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1713 && tree_int_cst_equal (TYPE_SIZE (a2),
1714 TYPE_SIZE (a1)))
400fbf9f
JW
1715 {
1716 tree memb;
46df2823 1717 for (memb = TYPE_FIELDS (a2);
910ad8de 1718 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1719 {
1720 tree mv3 = TREE_TYPE (memb);
1721 if (mv3 && mv3 != error_mark_node
1722 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1723 mv3 = (TYPE_ATOMIC (mv3)
1724 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1725 TYPE_QUAL_ATOMIC)
1726 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1727 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1728 different_types_p))
58cb41e6
JJ
1729 break;
1730 }
400fbf9f
JW
1731 if (memb == 0)
1732 return 0;
1733 }
1734 else
1735 return 0;
1736 }
1737
1738 /* comptypes said ok, but record if it said to warn. */
1739 if (newval > val)
1740 val = newval;
1741
1742 args1 = TREE_CHAIN (args1);
1743 args2 = TREE_CHAIN (args2);
1744 }
1745}
400fbf9f 1746\f
400fbf9f
JW
1747/* Compute the size to increment a pointer by. */
1748
4e2fb7de 1749static tree
58f9752a 1750c_size_in_bytes (const_tree type)
400fbf9f
JW
1751{
1752 enum tree_code code = TREE_CODE (type);
1753
fed3cef0
RK
1754 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1755 return size_one_node;
1756
d0f062fb 1757 if (!COMPLETE_OR_VOID_TYPE_P (type))
400fbf9f
JW
1758 {
1759 error ("arithmetic on pointer to an incomplete type");
fed3cef0 1760 return size_one_node;
400fbf9f
JW
1761 }
1762
1763 /* Convert in case a char is more than one unit. */
db3927fb
AH
1764 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1765 size_int (TYPE_PRECISION (char_type_node)
1766 / BITS_PER_UNIT));
400fbf9f 1767}
400fbf9f 1768\f
400fbf9f
JW
1769/* Return either DECL or its known constant value (if it has one). */
1770
56cb9733 1771tree
2f6e4e97 1772decl_constant_value (tree decl)
400fbf9f 1773{
a7c1916a 1774 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1775 in a place where a variable is invalid. Note that DECL_INITIAL
1776 isn't valid for a PARM_DECL. */
a7c1916a 1777 current_function_decl != 0
4f976745 1778 && TREE_CODE (decl) != PARM_DECL
3f75a254 1779 && !TREE_THIS_VOLATILE (decl)
83bab8db 1780 && TREE_READONLY (decl)
400fbf9f
JW
1781 && DECL_INITIAL (decl) != 0
1782 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1783 /* This is invalid if initial value is not constant.
1784 If it has either a function call, a memory reference,
1785 or a variable, then re-evaluating it could give different results. */
1786 && TREE_CONSTANT (DECL_INITIAL (decl))
1787 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1788 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1789 return DECL_INITIAL (decl);
1790 return decl;
1791}
1792
f2a71bbc
JM
1793/* Convert the array expression EXP to a pointer. */
1794static tree
c2255bc4 1795array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1796{
f2a71bbc 1797 tree orig_exp = exp;
207bf485 1798 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1799 tree adr;
1800 tree restype = TREE_TYPE (type);
1801 tree ptrtype;
207bf485 1802
f2a71bbc 1803 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1804
f2a71bbc 1805 STRIP_TYPE_NOPS (exp);
207bf485 1806
487a92fe
JM
1807 if (TREE_NO_WARNING (orig_exp))
1808 TREE_NO_WARNING (exp) = 1;
207bf485 1809
f2a71bbc
JM
1810 ptrtype = build_pointer_type (restype);
1811
1812 if (TREE_CODE (exp) == INDIRECT_REF)
1813 return convert (ptrtype, TREE_OPERAND (exp, 0));
1814
1f37c583
JM
1815 /* In C++ array compound literals are temporary objects unless they are
1816 const or appear in namespace scope, so they are destroyed too soon
1817 to use them for much of anything (c++/53220). */
1818 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1819 {
1820 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1821 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1822 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1823 "converting an array compound literal to a pointer "
1824 "is ill-formed in C++");
1825 }
1826
c2255bc4 1827 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1828 return convert (ptrtype, adr);
1829}
207bf485 1830
f2a71bbc
JM
1831/* Convert the function expression EXP to a pointer. */
1832static tree
c2255bc4 1833function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1834{
1835 tree orig_exp = exp;
207bf485 1836
f2a71bbc 1837 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1838
f2a71bbc 1839 STRIP_TYPE_NOPS (exp);
207bf485 1840
f2a71bbc
JM
1841 if (TREE_NO_WARNING (orig_exp))
1842 TREE_NO_WARNING (exp) = 1;
207bf485 1843
c2255bc4 1844 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1845}
207bf485 1846
ebfbbdc5
JJ
1847/* Mark EXP as read, not just set, for set but not used -Wunused
1848 warning purposes. */
1849
1850void
1851mark_exp_read (tree exp)
1852{
1853 switch (TREE_CODE (exp))
1854 {
1855 case VAR_DECL:
1856 case PARM_DECL:
1857 DECL_READ_P (exp) = 1;
1858 break;
1859 case ARRAY_REF:
1860 case COMPONENT_REF:
1861 case MODIFY_EXPR:
1862 case REALPART_EXPR:
1863 case IMAGPART_EXPR:
1864 CASE_CONVERT:
1865 case ADDR_EXPR:
1866 mark_exp_read (TREE_OPERAND (exp, 0));
1867 break;
1868 case COMPOUND_EXPR:
82c3c067 1869 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1870 mark_exp_read (TREE_OPERAND (exp, 1));
1871 break;
1872 default:
1873 break;
1874 }
1875}
1876
f2a71bbc
JM
1877/* Perform the default conversion of arrays and functions to pointers.
1878 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1879 return EXP.
1880
1881 LOC is the location of the expression. */
f2a71bbc
JM
1882
1883struct c_expr
c2255bc4 1884default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1885{
1886 tree orig_exp = exp.value;
1887 tree type = TREE_TYPE (exp.value);
1888 enum tree_code code = TREE_CODE (type);
1889
1890 switch (code)
1891 {
1892 case ARRAY_TYPE:
1893 {
1894 bool not_lvalue = false;
1895 bool lvalue_array_p;
1896
1897 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1898 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1899 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1900 {
1901 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1902 not_lvalue = true;
1903 exp.value = TREE_OPERAND (exp.value, 0);
1904 }
1905
1906 if (TREE_NO_WARNING (orig_exp))
1907 TREE_NO_WARNING (exp.value) = 1;
1908
1909 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1910 if (!flag_isoc99 && !lvalue_array_p)
1911 {
1912 /* Before C99, non-lvalue arrays do not decay to pointers.
1913 Normally, using such an array would be invalid; but it can
1914 be used correctly inside sizeof or as a statement expression.
1915 Thus, do not give an error here; an error will result later. */
1916 return exp;
1917 }
1918
c2255bc4 1919 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1920 }
1921 break;
1922 case FUNCTION_TYPE:
c2255bc4 1923 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1924 break;
1925 default:
f2a71bbc 1926 break;
207bf485 1927 }
f2a71bbc 1928
207bf485
JM
1929 return exp;
1930}
1931
ebfbbdc5
JJ
1932struct c_expr
1933default_function_array_read_conversion (location_t loc, struct c_expr exp)
1934{
1935 mark_exp_read (exp.value);
1936 return default_function_array_conversion (loc, exp);
1937}
522ddfa2 1938
267bac10
JM
1939/* Return whether EXPR should be treated as an atomic lvalue for the
1940 purposes of load and store handling. */
1941
1942static bool
1943really_atomic_lvalue (tree expr)
1944{
1945 if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node)
1946 return false;
1947 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1948 return false;
1949 if (!lvalue_p (expr))
1950 return false;
1951
1952 /* Ignore _Atomic on register variables, since their addresses can't
1953 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1954 sequences wouldn't work. Ignore _Atomic on structures containing
1955 bit-fields, since accessing elements of atomic structures or
1956 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1957 it's undefined at translation time or execution time, and the
1958 normal atomic sequences again wouldn't work. */
1959 while (handled_component_p (expr))
1960 {
1961 if (TREE_CODE (expr) == COMPONENT_REF
1962 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1963 return false;
1964 expr = TREE_OPERAND (expr, 0);
1965 }
1966 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1967 return false;
1968 return true;
1969}
1970
1971/* Convert expression EXP (location LOC) from lvalue to rvalue,
1972 including converting functions and arrays to pointers if CONVERT_P.
1973 If READ_P, also mark the expression as having been read. */
1974
1975struct c_expr
1976convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1977 bool convert_p, bool read_p)
1978{
1979 if (read_p)
1980 mark_exp_read (exp.value);
1981 if (convert_p)
1982 exp = default_function_array_conversion (loc, exp);
1983 if (really_atomic_lvalue (exp.value))
1984 {
1985 vec<tree, va_gc> *params;
1986 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1987 tree expr_type = TREE_TYPE (exp.value);
1988 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1989 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
1990
1991 gcc_assert (TYPE_ATOMIC (expr_type));
1992
1993 /* Expansion of a generic atomic load may require an addition
1994 element, so allocate enough to prevent a resize. */
1995 vec_alloc (params, 4);
1996
1997 /* Remove the qualifiers for the rest of the expressions and
1998 create the VAL temp variable to hold the RHS. */
1999 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2000 tmp = create_tmp_var (nonatomic_type, NULL);
2001 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2002 TREE_ADDRESSABLE (tmp) = 1;
2003
2004 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2005 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2006 params->quick_push (expr_addr);
2007 params->quick_push (tmp_addr);
2008 params->quick_push (seq_cst);
2009 func_call = build_function_call_vec (loc, fndecl, params, NULL);
2010
2011 /* Return tmp which contains the value loaded. */
2012 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2013 }
2014 return exp;
2015}
2016
522ddfa2
JM
2017/* EXP is an expression of integer type. Apply the integer promotions
2018 to it and return the promoted value. */
400fbf9f
JW
2019
2020tree
522ddfa2 2021perform_integral_promotions (tree exp)
400fbf9f 2022{
b3694847
SS
2023 tree type = TREE_TYPE (exp);
2024 enum tree_code code = TREE_CODE (type);
400fbf9f 2025
522ddfa2 2026 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2027
400fbf9f
JW
2028 /* Normally convert enums to int,
2029 but convert wide enums to something wider. */
2030 if (code == ENUMERAL_TYPE)
2031 {
b0c48229
NB
2032 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2033 TYPE_PRECISION (integer_type_node)),
2034 ((TYPE_PRECISION (type)
2035 >= TYPE_PRECISION (integer_type_node))
8df83eae 2036 && TYPE_UNSIGNED (type)));
05bccae2 2037
400fbf9f
JW
2038 return convert (type, exp);
2039 }
2040
522ddfa2
JM
2041 /* ??? This should no longer be needed now bit-fields have their
2042 proper types. */
9753f113 2043 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2044 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2045 /* If it's thinner than an int, promote it like a
d72040f5 2046 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2047 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2048 TYPE_PRECISION (integer_type_node)))
f458d1d5 2049 return convert (integer_type_node, exp);
9753f113 2050
d72040f5 2051 if (c_promoting_integer_type_p (type))
400fbf9f 2052 {
f458d1d5 2053 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2054 if (TYPE_UNSIGNED (type)
f458d1d5 2055 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2056 return convert (unsigned_type_node, exp);
05bccae2 2057
400fbf9f
JW
2058 return convert (integer_type_node, exp);
2059 }
05bccae2 2060
522ddfa2
JM
2061 return exp;
2062}
2063
2064
2065/* Perform default promotions for C data used in expressions.
46bdb9cf 2066 Enumeral types or short or char are converted to int.
522ddfa2
JM
2067 In addition, manifest constants symbols are replaced by their values. */
2068
2069tree
2070default_conversion (tree exp)
2071{
2072 tree orig_exp;
2073 tree type = TREE_TYPE (exp);
2074 enum tree_code code = TREE_CODE (type);
40449a90 2075 tree promoted_type;
522ddfa2 2076
ebfbbdc5
JJ
2077 mark_exp_read (exp);
2078
46bdb9cf
JM
2079 /* Functions and arrays have been converted during parsing. */
2080 gcc_assert (code != FUNCTION_TYPE);
2081 if (code == ARRAY_TYPE)
2082 return exp;
522ddfa2
JM
2083
2084 /* Constants can be used directly unless they're not loadable. */
2085 if (TREE_CODE (exp) == CONST_DECL)
2086 exp = DECL_INITIAL (exp);
2087
522ddfa2
JM
2088 /* Strip no-op conversions. */
2089 orig_exp = exp;
2090 STRIP_TYPE_NOPS (exp);
2091
2092 if (TREE_NO_WARNING (orig_exp))
2093 TREE_NO_WARNING (exp) = 1;
2094
400fbf9f
JW
2095 if (code == VOID_TYPE)
2096 {
2097 error ("void value not ignored as it ought to be");
2098 return error_mark_node;
2099 }
808d6eaa
JM
2100
2101 exp = require_complete_type (exp);
2102 if (exp == error_mark_node)
2103 return error_mark_node;
2104
40449a90
SL
2105 promoted_type = targetm.promoted_type (type);
2106 if (promoted_type)
2107 return convert (promoted_type, exp);
2108
808d6eaa
JM
2109 if (INTEGRAL_TYPE_P (type))
2110 return perform_integral_promotions (exp);
2111
400fbf9f
JW
2112 return exp;
2113}
2114\f
0fb96aa4 2115/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2116
2117 If the component name is not found, returns NULL_TREE. Otherwise,
2118 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2119 stepping down the chain to the component, which is in the last
2120 TREE_VALUE of the list. Normally the list is of length one, but if
2121 the component is embedded within (nested) anonymous structures or
2122 unions, the list steps down the chain to the component. */
2f6e4e97 2123
2f2d13da 2124static tree
0fb96aa4 2125lookup_field (tree type, tree component)
2f2d13da
DE
2126{
2127 tree field;
2128
2129 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2130 to the field elements. Use a binary search on this array to quickly
2131 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2132 will always be set for structures which have many elements. */
2133
22a0b85f 2134 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2135 {
2136 int bot, top, half;
d07605f5 2137 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2138
2139 field = TYPE_FIELDS (type);
2140 bot = 0;
d07605f5 2141 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2142 while (top - bot > 1)
2143 {
2f2d13da
DE
2144 half = (top - bot + 1) >> 1;
2145 field = field_array[bot+half];
2146
2147 if (DECL_NAME (field) == NULL_TREE)
2148 {
2149 /* Step through all anon unions in linear fashion. */
2150 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2151 {
2f2d13da 2152 field = field_array[bot++];
a68b98cf
RK
2153 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2154 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2155 {
0fb96aa4 2156 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2157
2158 if (anon)
2159 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2160
2161 /* The Plan 9 compiler permits referring
2162 directly to an anonymous struct/union field
2163 using a typedef name. */
2164 if (flag_plan9_extensions
2165 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2166 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2167 == TYPE_DECL)
2168 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2169 == component))
2170 break;
2f6e4e97 2171 }
2f2d13da
DE
2172 }
2173
2174 /* Entire record is only anon unions. */
2175 if (bot > top)
2176 return NULL_TREE;
2177
2178 /* Restart the binary search, with new lower bound. */
2179 continue;
2180 }
2181
e8b87aac 2182 if (DECL_NAME (field) == component)
2f2d13da 2183 break;
e8b87aac 2184 if (DECL_NAME (field) < component)
2f2d13da
DE
2185 bot += half;
2186 else
2187 top = bot + half;
2188 }
2189
2190 if (DECL_NAME (field_array[bot]) == component)
2191 field = field_array[bot];
2192 else if (DECL_NAME (field) != component)
e9b2c823 2193 return NULL_TREE;
2f2d13da
DE
2194 }
2195 else
2196 {
910ad8de 2197 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2198 {
e9b2c823
NB
2199 if (DECL_NAME (field) == NULL_TREE
2200 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2201 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2202 {
0fb96aa4 2203 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2204
e9b2c823
NB
2205 if (anon)
2206 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2207
2208 /* The Plan 9 compiler permits referring directly to an
2209 anonymous struct/union field using a typedef
2210 name. */
2211 if (flag_plan9_extensions
2212 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2213 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2214 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2215 == component))
2216 break;
2f2d13da
DE
2217 }
2218
2219 if (DECL_NAME (field) == component)
2220 break;
2221 }
e9b2c823
NB
2222
2223 if (field == NULL_TREE)
2224 return NULL_TREE;
2f2d13da
DE
2225 }
2226
e9b2c823 2227 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2228}
2229
c2255bc4
AH
2230/* Make an expression to refer to the COMPONENT field of structure or
2231 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2232 location of the COMPONENT_REF. */
400fbf9f
JW
2233
2234tree
c2255bc4 2235build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2236{
b3694847
SS
2237 tree type = TREE_TYPE (datum);
2238 enum tree_code code = TREE_CODE (type);
2239 tree field = NULL;
2240 tree ref;
1e57bf47 2241 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2242
7a3ea201
RH
2243 if (!objc_is_public (datum, component))
2244 return error_mark_node;
2245
46a88c12 2246 /* Detect Objective-C property syntax object.property. */
668ea4b1 2247 if (c_dialect_objc ()
46a88c12 2248 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2249 return ref;
2250
400fbf9f
JW
2251 /* See if there is a field or component with name COMPONENT. */
2252
2253 if (code == RECORD_TYPE || code == UNION_TYPE)
2254 {
d0f062fb 2255 if (!COMPLETE_TYPE_P (type))
400fbf9f 2256 {
7a228918 2257 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2258 return error_mark_node;
2259 }
2260
0fb96aa4 2261 field = lookup_field (type, component);
400fbf9f
JW
2262
2263 if (!field)
2264 {
c2255bc4 2265 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2266 return error_mark_node;
2267 }
400fbf9f 2268
e9b2c823
NB
2269 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2270 This might be better solved in future the way the C++ front
2271 end does it - by giving the anonymous entities each a
2272 separate name and type, and then have build_component_ref
2273 recursively call itself. We can't do that here. */
46ea50cb 2274 do
19d76e60 2275 {
e9b2c823 2276 tree subdatum = TREE_VALUE (field);
efed193e
JM
2277 int quals;
2278 tree subtype;
1e57bf47 2279 bool use_datum_quals;
e9b2c823
NB
2280
2281 if (TREE_TYPE (subdatum) == error_mark_node)
2282 return error_mark_node;
2283
1e57bf47
JM
2284 /* If this is an rvalue, it does not have qualifiers in C
2285 standard terms and we must avoid propagating such
2286 qualifiers down to a non-lvalue array that is then
2287 converted to a pointer. */
2288 use_datum_quals = (datum_lvalue
2289 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2290
efed193e 2291 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2292 if (use_datum_quals)
2293 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2294 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2295
2296 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2297 NULL_TREE);
c2255bc4 2298 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2299 if (TREE_READONLY (subdatum)
2300 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2301 TREE_READONLY (ref) = 1;
1e57bf47
JM
2302 if (TREE_THIS_VOLATILE (subdatum)
2303 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2304 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2305
2306 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2307 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2308
19d76e60 2309 datum = ref;
46ea50cb
RS
2310
2311 field = TREE_CHAIN (field);
19d76e60 2312 }
46ea50cb 2313 while (field);
19d76e60 2314
400fbf9f
JW
2315 return ref;
2316 }
2317 else if (code != ERROR_MARK)
c2255bc4
AH
2318 error_at (loc,
2319 "request for member %qE in something not a structure or union",
2320 component);
400fbf9f
JW
2321
2322 return error_mark_node;
2323}
2324\f
2325/* Given an expression PTR for a pointer, return an expression
2326 for the value pointed to.
6a3799eb
AH
2327 ERRORSTRING is the name of the operator to appear in error messages.
2328
2329 LOC is the location to use for the generated tree. */
400fbf9f
JW
2330
2331tree
dd865ef6 2332build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2333{
b3694847
SS
2334 tree pointer = default_conversion (ptr);
2335 tree type = TREE_TYPE (pointer);
6a3799eb 2336 tree ref;
400fbf9f
JW
2337
2338 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2339 {
1043771b 2340 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2341 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2342 {
2343 /* If a warning is issued, mark it to avoid duplicates from
2344 the backend. This only needs to be done at
2345 warn_strict_aliasing > 2. */
2346 if (warn_strict_aliasing > 2)
2347 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2348 type, TREE_OPERAND (pointer, 0)))
2349 TREE_NO_WARNING (pointer) = 1;
2350 }
2351
870cc33b 2352 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2353 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2354 == TREE_TYPE (type)))
6a3799eb
AH
2355 {
2356 ref = TREE_OPERAND (pointer, 0);
2357 protected_set_expr_location (ref, loc);
2358 return ref;
2359 }
870cc33b
RS
2360 else
2361 {
2362 tree t = TREE_TYPE (type);
46df2823 2363
984dfd8c 2364 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2365
baae9b65 2366 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2367 {
c9f9eb5d 2368 error_at (loc, "dereferencing pointer to incomplete type");
870cc33b
RS
2369 return error_mark_node;
2370 }
7d882b83 2371 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2372 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2373
2374 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2375 so that we get the proper error message if the result is used
2376 to assign to. Also, &* is supposed to be a no-op.
2377 And ANSI C seems to specify that the type of the result
2378 should be the const type. */
2379 /* A de-reference of a pointer to const is not a const. It is valid
2380 to change it via some other pointer. */
2381 TREE_READONLY (ref) = TYPE_READONLY (t);
2382 TREE_SIDE_EFFECTS (ref)
271bd540 2383 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2384 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2385 protected_set_expr_location (ref, loc);
870cc33b
RS
2386 return ref;
2387 }
2388 }
400fbf9f 2389 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2390 invalid_indirection_error (loc, type, errstring);
2391
400fbf9f
JW
2392 return error_mark_node;
2393}
2394
2395/* This handles expressions of the form "a[i]", which denotes
2396 an array reference.
2397
2398 This is logically equivalent in C to *(a+i), but we may do it differently.
2399 If A is a variable or a member, we generate a primitive ARRAY_REF.
2400 This avoids forcing the array out of registers, and can work on
2401 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2402 by functions).
2403
30cd1c5d
AS
2404 For vector types, allow vector[i] but not i[vector], and create
2405 *(((type*)&vectortype) + i) for the expression.
2406
6a3799eb 2407 LOC is the location to use for the returned expression. */
400fbf9f
JW
2408
2409tree
c2255bc4 2410build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2411{
6a3799eb 2412 tree ret;
a4ab7973 2413 bool swapped = false;
400fbf9f
JW
2414 if (TREE_TYPE (array) == error_mark_node
2415 || TREE_TYPE (index) == error_mark_node)
2416 return error_mark_node;
2417
36536d79
BI
2418 if (flag_enable_cilkplus && contains_array_notation_expr (index))
2419 {
2420 size_t rank = 0;
2421 if (!find_rank (loc, index, index, true, &rank))
2422 return error_mark_node;
2423 if (rank > 1)
2424 {
2425 error_at (loc, "rank of the array's index is greater than 1");
2426 return error_mark_node;
2427 }
2428 }
a4ab7973 2429 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2430 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2431 /* Allow vector[index] but not index[vector]. */
2432 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2433 {
a4ab7973
JM
2434 tree temp;
2435 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2436 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2437 {
f90e8e2e 2438 error_at (loc,
30cd1c5d
AS
2439 "subscripted value is neither array nor pointer nor vector");
2440
fdeefd49
RS
2441 return error_mark_node;
2442 }
a4ab7973
JM
2443 temp = array;
2444 array = index;
2445 index = temp;
2446 swapped = true;
2447 }
2448
2449 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2450 {
a63068b6 2451 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2452 return error_mark_node;
2453 }
2454
2455 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2456 {
a63068b6 2457 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2458 return error_mark_node;
2459 }
2460
ff6b6641
GDR
2461 /* ??? Existing practice has been to warn only when the char
2462 index is syntactically the index, not for char[array]. */
2463 if (!swapped)
2464 warn_array_subscript_with_type_char (index);
a4ab7973
JM
2465
2466 /* Apply default promotions *after* noticing character types. */
2467 index = default_conversion (index);
2468
2469 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2470
7edaa4d2 2471 convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2472
2473 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2474 {
e4d35515 2475 tree rval, type;
fdeefd49 2476
400fbf9f
JW
2477 /* An array that is indexed by a non-constant
2478 cannot be stored in a register; we must be able to do
2479 address arithmetic on its address.
2480 Likewise an array of elements of variable size. */
2481 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2482 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2483 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2484 {
dffd7eb6 2485 if (!c_mark_addressable (array))
400fbf9f
JW
2486 return error_mark_node;
2487 }
e6d52559
JW
2488 /* An array that is indexed by a constant value which is not within
2489 the array bounds cannot be stored in a register either; because we
2490 would get a crash in store_bit_field/extract_bit_field when trying
2491 to access a non-existent part of the register. */
2492 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2493 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2494 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2495 {
dffd7eb6 2496 if (!c_mark_addressable (array))
e6d52559
JW
2497 return error_mark_node;
2498 }
400fbf9f 2499
400fbf9f
JW
2500 if (pedantic)
2501 {
2502 tree foo = array;
2503 while (TREE_CODE (foo) == COMPONENT_REF)
2504 foo = TREE_OPERAND (foo, 0);
5baeaac0 2505 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2506 pedwarn (loc, OPT_Wpedantic,
fcf73884 2507 "ISO C forbids subscripting %<register%> array");
3f75a254 2508 else if (!flag_isoc99 && !lvalue_p (foo))
c1771a20 2509 pedwarn (loc, OPT_Wpedantic,
fcf73884 2510 "ISO C90 forbids subscripting non-lvalue array");
400fbf9f
JW
2511 }
2512
46df2823 2513 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2514 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2515 /* Array ref is const/volatile if the array elements are
c22cacf3 2516 or if the array is. */
400fbf9f
JW
2517 TREE_READONLY (rval)
2518 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2519 | TREE_READONLY (array));
2520 TREE_SIDE_EFFECTS (rval)
2521 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2522 | TREE_SIDE_EFFECTS (array));
2523 TREE_THIS_VOLATILE (rval)
2524 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2525 /* This was added by rms on 16 Nov 91.
2f6e4e97 2526 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2527 in an inline function.
2528 Hope it doesn't break something else. */
2529 | TREE_THIS_VOLATILE (array));
928c19bb 2530 ret = require_complete_type (rval);
6a3799eb
AH
2531 protected_set_expr_location (ret, loc);
2532 return ret;
400fbf9f 2533 }
a4ab7973
JM
2534 else
2535 {
2536 tree ar = default_conversion (array);
400fbf9f 2537
a4ab7973
JM
2538 if (ar == error_mark_node)
2539 return ar;
400fbf9f 2540
a4ab7973
JM
2541 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2542 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2543
ba47d38d 2544 return build_indirect_ref
c9f9eb5d 2545 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
dd865ef6 2546 RO_ARRAY_INDEXING);
a4ab7973 2547 }
400fbf9f
JW
2548}
2549\f
7e585d16 2550/* Build an external reference to identifier ID. FUN indicates
766beb40 2551 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2552 location of the identifier. This sets *TYPE to the type of the
2553 identifier, which is not the same as the type of the returned value
2554 for CONST_DECLs defined as enum constants. If the type of the
2555 identifier is not available, *TYPE is set to NULL. */
7e585d16 2556tree
c2255bc4 2557build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2558{
2559 tree ref;
2560 tree decl = lookup_name (id);
16b34ad6
ZL
2561
2562 /* In Objective-C, an instance variable (ivar) may be preferred to
2563 whatever lookup_name() found. */
2564 decl = objc_lookup_ivar (decl, id);
7e585d16 2565
6866c6e8 2566 *type = NULL;
339a28b9 2567 if (decl && decl != error_mark_node)
6866c6e8
ILT
2568 {
2569 ref = decl;
2570 *type = TREE_TYPE (ref);
2571 }
339a28b9
ZW
2572 else if (fun)
2573 /* Implicit function declaration. */
c2255bc4 2574 ref = implicitly_declare (loc, id);
339a28b9
ZW
2575 else if (decl == error_mark_node)
2576 /* Don't complain about something that's already been
2577 complained about. */
2578 return error_mark_node;
2579 else
2580 {
c2255bc4 2581 undeclared_variable (loc, id);
339a28b9
ZW
2582 return error_mark_node;
2583 }
7e585d16
ZW
2584
2585 if (TREE_TYPE (ref) == error_mark_node)
2586 return error_mark_node;
2587
339a28b9 2588 if (TREE_DEPRECATED (ref))
9b86d6bb 2589 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2590
ad960f56 2591 /* Recursive call does not count as usage. */
b8698a0f 2592 if (ref != current_function_decl)
ad960f56 2593 {
ad960f56
MLI
2594 TREE_USED (ref) = 1;
2595 }
7e585d16 2596
bc4b653b
JM
2597 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2598 {
2599 if (!in_sizeof && !in_typeof)
2600 C_DECL_USED (ref) = 1;
2601 else if (DECL_INITIAL (ref) == 0
2602 && DECL_EXTERNAL (ref)
2603 && !TREE_PUBLIC (ref))
2604 record_maybe_used_decl (ref);
2605 }
2606
7e585d16
ZW
2607 if (TREE_CODE (ref) == CONST_DECL)
2608 {
6193b8b7 2609 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2610
2611 if (warn_cxx_compat
2612 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2613 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2614 {
2615 warning_at (loc, OPT_Wc___compat,
2616 ("enum constant defined in struct or union "
2617 "is not visible in C++"));
2618 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2619 }
2620
7e585d16
ZW
2621 ref = DECL_INITIAL (ref);
2622 TREE_CONSTANT (ref) = 1;
2623 }
6a29edea 2624 else if (current_function_decl != 0
4b1e44be 2625 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2626 && (TREE_CODE (ref) == VAR_DECL
2627 || TREE_CODE (ref) == PARM_DECL
2628 || TREE_CODE (ref) == FUNCTION_DECL))
2629 {
2630 tree context = decl_function_context (ref);
2f6e4e97 2631
6a29edea
EB
2632 if (context != 0 && context != current_function_decl)
2633 DECL_NONLOCAL (ref) = 1;
2634 }
71113fcd
GK
2635 /* C99 6.7.4p3: An inline definition of a function with external
2636 linkage ... shall not contain a reference to an identifier with
2637 internal linkage. */
2638 else if (current_function_decl != 0
2639 && DECL_DECLARED_INLINE_P (current_function_decl)
2640 && DECL_EXTERNAL (current_function_decl)
2641 && VAR_OR_FUNCTION_DECL_P (ref)
2642 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2643 && ! TREE_PUBLIC (ref)
2644 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2645 record_inline_static (loc, current_function_decl, ref,
2646 csi_internal);
7e585d16
ZW
2647
2648 return ref;
2649}
2650
bc4b653b
JM
2651/* Record details of decls possibly used inside sizeof or typeof. */
2652struct maybe_used_decl
2653{
2654 /* The decl. */
2655 tree decl;
2656 /* The level seen at (in_sizeof + in_typeof). */
2657 int level;
2658 /* The next one at this level or above, or NULL. */
2659 struct maybe_used_decl *next;
2660};
2661
2662static struct maybe_used_decl *maybe_used_decls;
2663
2664/* Record that DECL, an undefined static function reference seen
2665 inside sizeof or typeof, might be used if the operand of sizeof is
2666 a VLA type or the operand of typeof is a variably modified
2667 type. */
2668
4e2fb7de 2669static void
bc4b653b
JM
2670record_maybe_used_decl (tree decl)
2671{
2672 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2673 t->decl = decl;
2674 t->level = in_sizeof + in_typeof;
2675 t->next = maybe_used_decls;
2676 maybe_used_decls = t;
2677}
2678
2679/* Pop the stack of decls possibly used inside sizeof or typeof. If
2680 USED is false, just discard them. If it is true, mark them used
2681 (if no longer inside sizeof or typeof) or move them to the next
2682 level up (if still inside sizeof or typeof). */
2683
2684void
2685pop_maybe_used (bool used)
2686{
2687 struct maybe_used_decl *p = maybe_used_decls;
2688 int cur_level = in_sizeof + in_typeof;
2689 while (p && p->level > cur_level)
2690 {
2691 if (used)
2692 {
2693 if (cur_level == 0)
2694 C_DECL_USED (p->decl) = 1;
2695 else
2696 p->level = cur_level;
2697 }
2698 p = p->next;
2699 }
2700 if (!used || cur_level == 0)
2701 maybe_used_decls = p;
2702}
2703
2704/* Return the result of sizeof applied to EXPR. */
2705
2706struct c_expr
c2255bc4 2707c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2708{
2709 struct c_expr ret;
ad97f4be
JM
2710 if (expr.value == error_mark_node)
2711 {
2712 ret.value = error_mark_node;
2713 ret.original_code = ERROR_MARK;
6866c6e8 2714 ret.original_type = NULL;
ad97f4be
JM
2715 pop_maybe_used (false);
2716 }
2717 else
2718 {
928c19bb
JM
2719 bool expr_const_operands = true;
2720 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2721 &expr_const_operands);
c2255bc4 2722 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2723 c_last_sizeof_arg = expr.value;
2724 ret.original_code = SIZEOF_EXPR;
6866c6e8 2725 ret.original_type = NULL;
928c19bb 2726 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2727 {
2728 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2729 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2730 folded_expr, ret.value);
2731 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2732 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2733 }
928c19bb 2734 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2735 }
bc4b653b
JM
2736 return ret;
2737}
2738
2739/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2740 name passed to sizeof (rather than the type itself). LOC is the
2741 location of the original expression. */
bc4b653b
JM
2742
2743struct c_expr
c2255bc4 2744c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2745{
2746 tree type;
2747 struct c_expr ret;
928c19bb
JM
2748 tree type_expr = NULL_TREE;
2749 bool type_expr_const = true;
2750 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2751 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2752 c_last_sizeof_arg = type;
2753 ret.original_code = SIZEOF_EXPR;
6866c6e8 2754 ret.original_type = NULL;
24070fcb
JM
2755 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2756 && c_vla_type_p (type))
928c19bb 2757 {
24070fcb
JM
2758 /* If the type is a [*] array, it is a VLA but is represented as
2759 having a size of zero. In such a case we must ensure that
2760 the result of sizeof does not get folded to a constant by
2761 c_fully_fold, because if the size is evaluated the result is
2762 not constant and so constraints on zero or negative size
2763 arrays must not be applied when this sizeof call is inside
2764 another array declarator. */
2765 if (!type_expr)
2766 type_expr = integer_zero_node;
928c19bb
JM
2767 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2768 type_expr, ret.value);
2769 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2770 }
16464cc1
VR
2771 pop_maybe_used (type != error_mark_node
2772 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2773 return ret;
2774}
2775
400fbf9f 2776/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2777 The function call is at LOC.
400fbf9f
JW
2778 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2779 TREE_VALUE of each node is a parameter-expression.
2780 FUNCTION's data type may be a function type or a pointer-to-function. */
2781
2782tree
c2255bc4 2783build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2784{
9771b263 2785 vec<tree, va_gc> *v;
bbbbb16a
ILT
2786 tree ret;
2787
9771b263 2788 vec_alloc (v, list_length (params));
bbbbb16a 2789 for (; params; params = TREE_CHAIN (params))
9771b263
DN
2790 v->quick_push (TREE_VALUE (params));
2791 ret = build_function_call_vec (loc, function, v, NULL);
2792 vec_free (v);
bbbbb16a
ILT
2793 return ret;
2794}
2795
ae52741c
MLI
2796/* Give a note about the location of the declaration of DECL. */
2797
2798static void inform_declaration (tree decl)
2799{
2800 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2801 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2802}
2803
bbbbb16a
ILT
2804/* Build a function call to function FUNCTION with parameters PARAMS.
2805 ORIGTYPES, if not NULL, is a vector of types; each element is
2806 either NULL or the original type of the corresponding element in
2807 PARAMS. The original type may differ from TREE_TYPE of the
2808 parameter for enums. FUNCTION's data type may be a function type
2809 or pointer-to-function. This function changes the elements of
2810 PARAMS. */
2811
2812tree
9771b263
DN
2813build_function_call_vec (location_t loc, tree function,
2814 vec<tree, va_gc> *params,
2815 vec<tree, va_gc> *origtypes)
400fbf9f 2816{
b3694847 2817 tree fntype, fundecl = 0;
4977bab6 2818 tree name = NULL_TREE, result;
c96f4f73 2819 tree tem;
94a0dd7b
SL
2820 int nargs;
2821 tree *argarray;
b8698a0f 2822
400fbf9f 2823
fc76e425 2824 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2825 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2826
2827 /* Convert anything with function type to a pointer-to-function. */
2828 if (TREE_CODE (function) == FUNCTION_DECL)
2829 {
58646b77
PB
2830 /* Implement type-directed function overloading for builtins.
2831 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2832 handle all the type checking. The result is a complete expression
2833 that implements this function call. */
c2255bc4 2834 tem = resolve_overloaded_builtin (loc, function, params);
58646b77
PB
2835 if (tem)
2836 return tem;
48ae6c13 2837
400fbf9f 2838 name = DECL_NAME (function);
0a35513e
AH
2839
2840 if (flag_tm)
2841 tm_malloc_replacement (function);
a5eadacc 2842 fundecl = function;
86951993
AM
2843 /* Atomic functions have type checking/casting already done. They are
2844 often rewritten and don't match the original parameter list. */
2845 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2846 origtypes = NULL;
36536d79
BI
2847
2848 if (flag_enable_cilkplus
2849 && is_cilkplus_reduce_builtin (function))
2850 origtypes = NULL;
400fbf9f 2851 }
f2a71bbc 2852 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2853 function = function_to_pointer_conversion (loc, function);
400fbf9f 2854
6e955430
ZL
2855 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2856 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2857 if (params && !params->is_empty ())
2858 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2859
928c19bb
JM
2860 function = c_fully_fold (function, false, NULL);
2861
400fbf9f
JW
2862 fntype = TREE_TYPE (function);
2863
2864 if (TREE_CODE (fntype) == ERROR_MARK)
2865 return error_mark_node;
2866
2867 if (!(TREE_CODE (fntype) == POINTER_TYPE
2868 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2869 {
ae52741c
MLI
2870 if (!flag_diagnostics_show_caret)
2871 error_at (loc,
2872 "called object %qE is not a function or function pointer",
2873 function);
2874 else if (DECL_P (function))
2875 {
2876 error_at (loc,
2877 "called object %qD is not a function or function pointer",
2878 function);
2879 inform_declaration (function);
2880 }
2881 else
2882 error_at (loc,
2883 "called object is not a function or function pointer");
400fbf9f
JW
2884 return error_mark_node;
2885 }
2886
5ce89b2e
JM
2887 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2888 current_function_returns_abnormally = 1;
2889
400fbf9f
JW
2890 /* fntype now gets the type of function pointed to. */
2891 fntype = TREE_TYPE (fntype);
2892
ab4194da
JM
2893 /* Convert the parameters to the types declared in the
2894 function prototype, or apply default promotions. */
2895
bbbbb16a
ILT
2896 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2897 function, fundecl);
ab4194da
JM
2898 if (nargs < 0)
2899 return error_mark_node;
2900
c96f4f73
EB
2901 /* Check that the function is called through a compatible prototype.
2902 If it is not, replace the call by a trap, wrapped up in a compound
2903 expression if necessary. This has the nice side-effect to prevent
2904 the tree-inliner from generating invalid assignment trees which may
58393038 2905 blow up in the RTL expander later. */
1043771b 2906 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2907 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2908 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2909 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2910 {
2911 tree return_type = TREE_TYPE (fntype);
e79983f4
MM
2912 tree trap = build_function_call (loc,
2913 builtin_decl_explicit (BUILT_IN_TRAP),
c96f4f73 2914 NULL_TREE);
ab4194da 2915 int i;
c96f4f73
EB
2916
2917 /* This situation leads to run-time undefined behavior. We can't,
2918 therefore, simply error unless we can prove that all possible
2919 executions of the program must execute the code. */
c2255bc4 2920 if (warning_at (loc, 0, "function called through a non-compatible type"))
71205d17
MLI
2921 /* We can, however, treat "undefined" any way we please.
2922 Call abort to encourage the user to fix the program. */
c2255bc4 2923 inform (loc, "if this code is reached, the program will abort");
ab4194da
JM
2924 /* Before the abort, allow the function arguments to exit or
2925 call longjmp. */
2926 for (i = 0; i < nargs; i++)
9771b263 2927 trap = build2 (COMPOUND_EXPR, void_type_node, (*params)[i], trap);
bba745c1 2928
c96f4f73 2929 if (VOID_TYPE_P (return_type))
3ce62965
JM
2930 {
2931 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
db3927fb 2932 pedwarn (loc, 0,
3ce62965
JM
2933 "function with qualified void return type called");
2934 return trap;
2935 }
c96f4f73
EB
2936 else
2937 {
2938 tree rhs;
2939
2940 if (AGGREGATE_TYPE_P (return_type))
c2255bc4 2941 rhs = build_compound_literal (loc, return_type,
9771b263
DN
2942 build_constructor (return_type,
2943 NULL),
928c19bb 2944 false);
c96f4f73 2945 else
e8160c9a 2946 rhs = build_zero_cst (return_type);
c96f4f73 2947
3ce62965
JM
2948 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2949 trap, rhs));
c96f4f73
EB
2950 }
2951 }
2952
9771b263 2953 argarray = vec_safe_address (params);
bbbbb16a 2954
83322951
RG
2955 /* Check that arguments to builtin functions match the expectations. */
2956 if (fundecl
2957 && DECL_BUILT_IN (fundecl)
2958 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2959 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2960 return error_mark_node;
400fbf9f 2961
83322951 2962 /* Check that the arguments to the function are valid. */
dde05067 2963 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2964
928c19bb
JM
2965 if (name != NULL_TREE
2966 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2967 {
928c19bb 2968 if (require_constant_value)
b8698a0f 2969 result =
db3927fb
AH
2970 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2971 function, nargs, argarray);
928c19bb 2972 else
db3927fb
AH
2973 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2974 function, nargs, argarray);
928c19bb
JM
2975 if (TREE_CODE (result) == NOP_EXPR
2976 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2977 STRIP_TYPE_NOPS (result);
bf730f15
RS
2978 }
2979 else
db3927fb
AH
2980 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2981 function, nargs, argarray);
b0b3afb2 2982
71653180 2983 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2984 {
2985 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2986 pedwarn (loc, 0,
3ce62965
JM
2987 "function with qualified void return type called");
2988 return result;
2989 }
1eb8759b 2990 return require_complete_type (result);
400fbf9f
JW
2991}
2992\f
bbbbb16a
ILT
2993/* Convert the argument expressions in the vector VALUES
2994 to the types in the list TYPELIST.
400fbf9f
JW
2995
2996 If TYPELIST is exhausted, or when an element has NULL as its type,
2997 perform the default conversions.
2998
bbbbb16a
ILT
2999 ORIGTYPES is the original types of the expressions in VALUES. This
3000 holds the type of enum values which have been converted to integral
3001 types. It may be NULL.
400fbf9f 3002
03dafa61
JM
3003 FUNCTION is a tree for the called function. It is used only for
3004 error messages, where it is formatted with %qE.
400fbf9f
JW
3005
3006 This is also where warnings about wrong number of args are generated.
3007
94a0dd7b 3008 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3009 than the length of VALUES in some error situations), or -1 on
3010 failure. */
94a0dd7b
SL
3011
3012static int
9771b263
DN
3013convert_arguments (tree typelist, vec<tree, va_gc> *values,
3014 vec<tree, va_gc> *origtypes, tree function, tree fundecl)
400fbf9f 3015{
bbbbb16a
ILT
3016 tree typetail, val;
3017 unsigned int parmnum;
06302a02 3018 bool error_args = false;
b5d32c25
KG
3019 const bool type_generic = fundecl
3020 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
8ce94e44 3021 bool type_generic_remove_excess_precision = false;
03dafa61 3022 tree selector;
03dafa61 3023
2ac2f164
JM
3024 /* Change pointer to function to the function itself for
3025 diagnostics. */
03dafa61
JM
3026 if (TREE_CODE (function) == ADDR_EXPR
3027 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3028 function = TREE_OPERAND (function, 0);
03dafa61
JM
3029
3030 /* Handle an ObjC selector specially for diagnostics. */
3031 selector = objc_message_selector ();
400fbf9f 3032
8ce94e44
JM
3033 /* For type-generic built-in functions, determine whether excess
3034 precision should be removed (classification) or not
3035 (comparison). */
3036 if (type_generic
3037 && DECL_BUILT_IN (fundecl)
3038 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3039 {
3040 switch (DECL_FUNCTION_CODE (fundecl))
3041 {
3042 case BUILT_IN_ISFINITE:
3043 case BUILT_IN_ISINF:
3044 case BUILT_IN_ISINF_SIGN:
3045 case BUILT_IN_ISNAN:
3046 case BUILT_IN_ISNORMAL:
3047 case BUILT_IN_FPCLASSIFY:
3048 type_generic_remove_excess_precision = true;
3049 break;
3050
3051 default:
3052 type_generic_remove_excess_precision = false;
3053 break;
3054 }
3055 }
6d6efbb3
BI
3056 if (flag_enable_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3057 return vec_safe_length (values);
8ce94e44 3058
400fbf9f 3059 /* Scan the given expressions and types, producing individual
bbbbb16a 3060 converted arguments. */
400fbf9f 3061
bbbbb16a 3062 for (typetail = typelist, parmnum = 0;
9771b263 3063 values && values->iterate (parmnum, &val);
bbbbb16a 3064 ++parmnum)
400fbf9f 3065 {
b3694847 3066 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3067 tree valtype = TREE_TYPE (val);
03dafa61
JM
3068 tree rname = function;
3069 int argnum = parmnum + 1;
4d3e6fae 3070 const char *invalid_func_diag;
8ce94e44 3071 bool excess_precision = false;
928c19bb 3072 bool npc;
bbbbb16a 3073 tree parmval;
400fbf9f
JW
3074
3075 if (type == void_type_node)
3076 {
19dc6d01
NP
3077 if (selector)
3078 error_at (input_location,
3079 "too many arguments to method %qE", selector);
3080 else
3081 error_at (input_location,
3082 "too many arguments to function %qE", function);
ae52741c 3083 inform_declaration (fundecl);
94a0dd7b 3084 return parmnum;
400fbf9f
JW
3085 }
3086
03dafa61
JM
3087 if (selector && argnum > 2)
3088 {
3089 rname = selector;
3090 argnum -= 2;
3091 }
3092
928c19bb 3093 npc = null_pointer_constant_p (val);
8ce94e44
JM
3094
3095 /* If there is excess precision and a prototype, convert once to
3096 the required type rather than converting via the semantic
3097 type. Likewise without a prototype a float value represented
3098 as long double should be converted once to double. But for
3099 type-generic classification functions excess precision must
3100 be removed here. */
3101 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3102 && (type || !type_generic || !type_generic_remove_excess_precision))
3103 {
3104 val = TREE_OPERAND (val, 0);
3105 excess_precision = true;
3106 }
928c19bb 3107 val = c_fully_fold (val, false, NULL);
ed248cf7 3108 STRIP_TYPE_NOPS (val);
400fbf9f 3109
400fbf9f
JW
3110 val = require_complete_type (val);
3111
3112 if (type != 0)
3113 {
3114 /* Formal parm type is specified by a function prototype. */
400fbf9f 3115
20913689 3116 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f
JW
3117 {
3118 error ("type of formal parameter %d is incomplete", parmnum + 1);
3119 parmval = val;
3120 }
3121 else
3122 {
bbbbb16a
ILT
3123 tree origtype;
3124
d45cf215
RS
3125 /* Optionally warn about conversions that
3126 differ from the default conversions. */
05170031 3127 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3128 {
e3a64162 3129 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3130
aae43c5f 3131 if (INTEGRAL_TYPE_P (type)
8ce94e44 3132 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3133 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3134 "rather than floating due to prototype",
3135 argnum, rname);
03829ad2 3136 if (INTEGRAL_TYPE_P (type)
8ce94e44 3137 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3138 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3139 "rather than complex due to prototype",
3140 argnum, rname);
aae43c5f 3141 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3142 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3143 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3144 "rather than floating due to prototype",
3145 argnum, rname);
400fbf9f 3146 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3147 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3148 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3149 "rather than integer due to prototype",
3150 argnum, rname);
03829ad2 3151 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3152 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3153 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3154 "rather than integer due to prototype",
3155 argnum, rname);
aae43c5f 3156 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3157 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3158 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3159 "rather than complex due to prototype",
3160 argnum, rname);
aae43c5f
RK
3161 /* ??? At some point, messages should be written about
3162 conversions between complex types, but that's too messy
3163 to do now. */
d45cf215 3164 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3165 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3166 {
3167 /* Warn if any argument is passed as `float',
047de90b 3168 since without a prototype it would be `double'. */
9a8ce21f
JG
3169 if (formal_prec == TYPE_PRECISION (float_type_node)
3170 && type != dfloat32_type_node)
d4ee4d25 3171 warning (0, "passing argument %d of %qE as %<float%> "
03dafa61
JM
3172 "rather than %<double%> due to prototype",
3173 argnum, rname);
9a8ce21f
JG
3174
3175 /* Warn if mismatch between argument and prototype
3176 for decimal float types. Warn of conversions with
3177 binary float types and of precision narrowing due to
3178 prototype. */
8ce94e44 3179 else if (type != valtype
9a8ce21f
JG
3180 && (type == dfloat32_type_node
3181 || type == dfloat64_type_node
c22cacf3 3182 || type == dfloat128_type_node
8ce94e44
JM
3183 || valtype == dfloat32_type_node
3184 || valtype == dfloat64_type_node
3185 || valtype == dfloat128_type_node)
c22cacf3 3186 && (formal_prec
8ce94e44 3187 <= TYPE_PRECISION (valtype)
9a8ce21f 3188 || (type == dfloat128_type_node
8ce94e44 3189 && (valtype
c22cacf3 3190 != dfloat64_type_node
8ce94e44 3191 && (valtype
9a8ce21f
JG
3192 != dfloat32_type_node)))
3193 || (type == dfloat64_type_node
8ce94e44 3194 && (valtype
9a8ce21f
JG
3195 != dfloat32_type_node))))
3196 warning (0, "passing argument %d of %qE as %qT "
3197 "rather than %qT due to prototype",
8ce94e44 3198 argnum, rname, type, valtype);
9a8ce21f 3199
d45cf215 3200 }
3ed56f8a
KG
3201 /* Detect integer changing in width or signedness.
3202 These warnings are only activated with
05170031
MLI
3203 -Wtraditional-conversion, not with -Wtraditional. */
3204 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3205 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3206 {
d45cf215
RS
3207 tree would_have_been = default_conversion (val);
3208 tree type1 = TREE_TYPE (would_have_been);
3209
754a4d82 3210 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3211 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3212 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3213 /* No warning if function asks for enum
3214 and the actual arg is that enum type. */
3215 ;
3216 else if (formal_prec != TYPE_PRECISION (type1))
c2255bc4
AH
3217 warning (OPT_Wtraditional_conversion,
3218 "passing argument %d of %qE "
3176a0c2
DD
3219 "with different width due to prototype",
3220 argnum, rname);
8df83eae 3221 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3222 ;
800cd3b9
RS
3223 /* Don't complain if the formal parameter type
3224 is an enum, because we can't tell now whether
3225 the value was an enum--even the same enum. */
3226 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3227 ;
400fbf9f
JW
3228 else if (TREE_CODE (val) == INTEGER_CST
3229 && int_fits_type_p (val, type))
3230 /* Change in signedness doesn't matter
3231 if a constant value is unaffected. */
3232 ;
ce9895ae
RS
3233 /* If the value is extended from a narrower
3234 unsigned type, it doesn't matter whether we
3235 pass it as signed or unsigned; the value
3236 certainly is the same either way. */
8ce94e44
JM
3237 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3238 && TYPE_UNSIGNED (valtype))
ce9895ae 3239 ;
8df83eae 3240 else if (TYPE_UNSIGNED (type))
c2255bc4
AH
3241 warning (OPT_Wtraditional_conversion,
3242 "passing argument %d of %qE "
3176a0c2
DD
3243 "as unsigned due to prototype",
3244 argnum, rname);
3ed56f8a 3245 else
c2255bc4
AH
3246 warning (OPT_Wtraditional_conversion,
3247 "passing argument %d of %qE "
3176a0c2 3248 "as signed due to prototype", argnum, rname);
400fbf9f
JW
3249 }
3250 }
3251
8ce94e44
JM
3252 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3253 sake of better warnings from convert_and_check. */
3254 if (excess_precision)
3255 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3256 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
744aa42f
ILT
3257 parmval = convert_for_assignment (input_location, type, val,
3258 origtype, ic_argpass, npc,
2ac2f164
JM
3259 fundecl, function,
3260 parmnum + 1);
2f6e4e97 3261
61f71b34 3262 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3263 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3264 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3265 parmval = default_conversion (parmval);
400fbf9f 3266 }
400fbf9f 3267 }
8ce94e44
JM
3268 else if (TREE_CODE (valtype) == REAL_TYPE
3269 && (TYPE_PRECISION (valtype)
2531a1d9 3270 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3271 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3272 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3273 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3274 {
3275 if (type_generic)
bbbbb16a 3276 parmval = val;
b5d32c25 3277 else
0a0b3574
MM
3278 {
3279 /* Convert `float' to `double'. */
3280 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3281 warning (OPT_Wdouble_promotion,
3282 "implicit conversion from %qT to %qT when passing "
3283 "argument to function",
3284 valtype, double_type_node);
3285 parmval = convert (double_type_node, val);
3286 }
b5d32c25 3287 }
8ce94e44
JM
3288 else if (excess_precision && !type_generic)
3289 /* A "double" argument with excess precision being passed
3290 without a prototype or in variable arguments. */
bbbbb16a 3291 parmval = convert (valtype, val);
c22cacf3
MS
3292 else if ((invalid_func_diag =
3293 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3294 {
3295 error (invalid_func_diag);
94a0dd7b 3296 return -1;
4d3e6fae 3297 }
400fbf9f
JW
3298 else
3299 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3300 parmval = default_conversion (val);
3301
9771b263 3302 (*values)[parmnum] = parmval;
06302a02
JJ
3303 if (parmval == error_mark_node)
3304 error_args = true;
400fbf9f
JW
3305
3306 if (typetail)
3307 typetail = TREE_CHAIN (typetail);
3308 }
3309
9771b263 3310 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3311
400fbf9f 3312 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3313 {
6d6efbb3
BI
3314 error_at (input_location,
3315 "too few arguments to function %qE", function);
3316 inform_declaration (fundecl);
3317 return -1;
3789b316 3318 }
400fbf9f 3319
06302a02 3320 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3321}
3322\f
43f6dfd3
RS
3323/* This is the entry point used by the parser to build unary operators
3324 in the input. CODE, a tree_code, specifies the unary operator, and
3325 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3326 CONVERT_EXPR for code.
3327
3328 LOC is the location to use for the tree generated.
3329*/
43f6dfd3
RS
3330
3331struct c_expr
c2255bc4 3332parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3333{
3334 struct c_expr result;
3335
c9f9eb5d 3336 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3337 result.original_code = code;
6866c6e8
ILT
3338 result.original_type = NULL;
3339
59c0753d 3340 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3341 overflow_warning (loc, result.value);
59c0753d 3342
43f6dfd3
RS
3343 return result;
3344}
3345
3346/* This is the entry point used by the parser to build binary operators
3347 in the input. CODE, a tree_code, specifies the binary operator, and
3348 ARG1 and ARG2 are the operands. In addition to constructing the
3349 expression, we check for operands that were written with other binary
ba47d38d
AH
3350 operators in a way that is likely to confuse the user.
3351
3352 LOCATION is the location of the binary operator. */
edc7c4ec 3353
487a92fe 3354struct c_expr
ba47d38d
AH
3355parser_build_binary_op (location_t location, enum tree_code code,
3356 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3357{
487a92fe 3358 struct c_expr result;
400fbf9f 3359
487a92fe
JM
3360 enum tree_code code1 = arg1.original_code;
3361 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3362 tree type1 = (arg1.original_type
3363 ? arg1.original_type
3364 : TREE_TYPE (arg1.value));
3365 tree type2 = (arg2.original_type
3366 ? arg2.original_type
3367 : TREE_TYPE (arg2.value));
400fbf9f 3368
ba47d38d
AH
3369 result.value = build_binary_op (location, code,
3370 arg1.value, arg2.value, 1);
487a92fe 3371 result.original_code = code;
6866c6e8 3372 result.original_type = NULL;
58bf601b 3373
487a92fe
JM
3374 if (TREE_CODE (result.value) == ERROR_MARK)
3375 return result;
400fbf9f 3376
ba47d38d
AH
3377 if (location != UNKNOWN_LOCATION)
3378 protected_set_expr_location (result.value, location);
3379
400fbf9f 3380 /* Check for cases such as x+y<<z which users are likely
487a92fe 3381 to misinterpret. */
400fbf9f 3382 if (warn_parentheses)
5d9de0d0
PC
3383 warn_about_parentheses (input_location, code,
3384 code1, arg1.value, code2, arg2.value);
001af587 3385
ca409efd 3386 if (warn_logical_op)
a243fb4a 3387 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
ca409efd 3388 code1, arg1.value, code2, arg2.value);
63a08740 3389
e994a705
RS
3390 /* Warn about comparisons against string literals, with the exception
3391 of testing for equality or inequality of a string literal with NULL. */
3392 if (code == EQ_EXPR || code == NE_EXPR)
3393 {
3394 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3395 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3396 warning_at (location, OPT_Waddress,
3397 "comparison with string literal results in unspecified behavior");
e994a705
RS
3398 }
3399 else if (TREE_CODE_CLASS (code) == tcc_comparison
3400 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3401 warning_at (location, OPT_Waddress,
3402 "comparison with string literal results in unspecified behavior");
e994a705 3403
b8698a0f
L
3404 if (TREE_OVERFLOW_P (result.value)
3405 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3406 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3407 overflow_warning (location, result.value);
400fbf9f 3408
6866c6e8
ILT
3409 /* Warn about comparisons of different enum types. */
3410 if (warn_enum_compare
3411 && TREE_CODE_CLASS (code) == tcc_comparison
3412 && TREE_CODE (type1) == ENUMERAL_TYPE
3413 && TREE_CODE (type2) == ENUMERAL_TYPE
3414 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3415 warning_at (location, OPT_Wenum_compare,
3416 "comparison between %qT and %qT",
3417 type1, type2);
3418
400fbf9f
JW
3419 return result;
3420}
3e4093b6 3421\f
3e4093b6
RS
3422/* Return a tree for the difference of pointers OP0 and OP1.
3423 The resulting tree has type int. */
293c9fdd 3424
3e4093b6 3425static tree
db3927fb 3426pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3427{
3e4093b6 3428 tree restype = ptrdiff_type_node;
36c5e70a 3429 tree result, inttype;
400fbf9f 3430
36c5e70a
BE
3431 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3432 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6
RS
3433 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3434 tree con0, con1, lit0, lit1;
3435 tree orig_op1 = op1;
400fbf9f 3436
36c5e70a
BE
3437 /* If the operands point into different address spaces, we need to
3438 explicitly convert them to pointers into the common address space
3439 before we can subtract the numerical address values. */
3440 if (as0 != as1)
3441 {
3442 addr_space_t as_common;
3443 tree common_type;
3444
3445 /* Determine the common superset address space. This is guaranteed
3446 to exist because the caller verified that comp_target_types
3447 returned non-zero. */
3448 if (!addr_space_superset (as0, as1, &as_common))
3449 gcc_unreachable ();
3450
3451 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3452 op0 = convert (common_type, op0);
3453 op1 = convert (common_type, op1);
3454 }
3455
3456 /* Determine integer type to perform computations in. This will usually
3457 be the same as the result type (ptrdiff_t), but may need to be a wider
3458 type if pointers for the address space are wider than ptrdiff_t. */
3459 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3460 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3461 else
3462 inttype = restype;
3463
3464
fcf73884 3465 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3466 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3467 "pointer of type %<void *%> used in subtraction");
3468 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3469 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3470 "pointer to a function used in subtraction");
400fbf9f 3471
3e4093b6
RS
3472 /* If the conversion to ptrdiff_type does anything like widening or
3473 converting a partial to an integral mode, we get a convert_expression
3474 that is in the way to do any simplifications.
3475 (fold-const.c doesn't know that the extra bits won't be needed.
3476 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3477 different mode in place.)
3478 So first try to find a common term here 'by hand'; we want to cover
3479 at least the cases that occur in legal static initializers. */
1043771b 3480 if (CONVERT_EXPR_P (op0)
1344f9a3
JM
3481 && (TYPE_PRECISION (TREE_TYPE (op0))
3482 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3483 con0 = TREE_OPERAND (op0, 0);
3484 else
3485 con0 = op0;
1043771b 3486 if (CONVERT_EXPR_P (op1)
1344f9a3
JM
3487 && (TYPE_PRECISION (TREE_TYPE (op1))
3488 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3489 con1 = TREE_OPERAND (op1, 0);
3490 else
3491 con1 = op1;
400fbf9f 3492
5de72424 3493 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3e4093b6
RS
3494 {
3495 lit0 = TREE_OPERAND (con0, 1);
3496 con0 = TREE_OPERAND (con0, 0);
3497 }
3498 else
3499 lit0 = integer_zero_node;
400fbf9f 3500
5de72424 3501 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
400fbf9f 3502 {
3e4093b6
RS
3503 lit1 = TREE_OPERAND (con1, 1);
3504 con1 = TREE_OPERAND (con1, 0);
400fbf9f
JW
3505 }
3506 else
3e4093b6
RS
3507 lit1 = integer_zero_node;
3508
3509 if (operand_equal_p (con0, con1, 0))
400fbf9f 3510 {
3e4093b6
RS
3511 op0 = lit0;
3512 op1 = lit1;
400fbf9f
JW
3513 }
3514
400fbf9f 3515
3e4093b6
RS
3516 /* First do the subtraction as integers;
3517 then drop through to build the divide operator.
3518 Do not do default conversions on the minus operator
3519 in case restype is a short type. */
400fbf9f 3520
db3927fb 3521 op0 = build_binary_op (loc,
36c5e70a
BE
3522 MINUS_EXPR, convert (inttype, op0),
3523 convert (inttype, op1), 0);
3e4093b6
RS
3524 /* This generates an error if op1 is pointer to incomplete type. */
3525 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3526 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3527
3e4093b6
RS
3528 /* This generates an error if op0 is pointer to incomplete type. */
3529 op1 = c_size_in_bytes (target_type);
400fbf9f 3530
3e4093b6 3531 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3532 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3533 op0, convert (inttype, op1));
3534
3535 /* Convert to final result type if necessary. */
3536 return convert (restype, result);
3e4093b6
RS
3537}
3538\f
267bac10
JM
3539/* Expand atomic compound assignments into an approriate sequence as
3540 specified by the C11 standard section 6.5.16.2.
3541 given
3542 _Atomic T1 E1
3543 T2 E2
3544 E1 op= E2
3545
3546 This sequence is used for all types for which these operations are
3547 supported.
3548
3549 In addition, built-in versions of the 'fe' prefixed routines may
3550 need to be invoked for floating point (real, complex or vector) when
3551 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3552
3553 T1 newval;
3554 T1 old;
3555 T1 *addr
3556 T2 val
3557 fenv_t fenv
3558
3559 addr = &E1;
3560 val = (E2);
3561 __atomic_load (addr, &old, SEQ_CST);
3562 feholdexcept (&fenv);
3563loop:
3564 newval = old op val;
3565 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3566 SEQ_CST))
3567 goto done;
3568 feclearexcept (FE_ALL_EXCEPT);
3569 goto loop:
3570done:
3571 feupdateenv (&fenv);
3572
3573 Also note that the compiler is simply issuing the generic form of
3574 the atomic operations. This requires temp(s) and has their address
3575 taken. The atomic processing is smart enough to figure out when the
3576 size of an object can utilize a lock-free version, and convert the
3577 built-in call to the appropriate lock-free routine. The optimizers
3578 will then dispose of any temps that are no longer required, and
3579 lock-free implementations are utilized as long as there is target
3580 support for the required size.
3581
3582 If the operator is NOP_EXPR, then this is a simple assignment, and
3583 an __atomic_store is issued to perform the assignment rather than
3584 the above loop.
3585
3586*/
3587
3588/* Build an atomic assignment at LOC, expanding into the proper
3589 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3590 the result of the operation, unless RETURN_OLD_P in which case
3591 return the old value of LHS (this is only for postincrement and
3592 postdecrement). */
3593static tree
3594build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3595 tree rhs, bool return_old_p)
3596{
3597 tree fndecl, func_call;
3598 vec<tree, va_gc> *params;
3599 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3600 tree old, old_addr;
3601 tree compound_stmt;
3602 tree stmt, goto_stmt;
3603 tree loop_label, loop_decl, done_label, done_decl;
3604
3605 tree lhs_type = TREE_TYPE (lhs);
3606 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3607 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3608 tree rhs_type = TREE_TYPE (rhs);
3609
3610 gcc_assert (TYPE_ATOMIC (lhs_type));
3611
3612 if (return_old_p)
3613 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3614
3615 /* Allocate enough vector items for a compare_exchange. */
3616 vec_alloc (params, 6);
3617
3618 /* Create a compound statement to hold the sequence of statements
3619 with a loop. */
3620 compound_stmt = c_begin_compound_stmt (false);
3621
3622 /* Fold the RHS if it hasn't already been folded. */
3623 if (modifycode != NOP_EXPR)
3624 rhs = c_fully_fold (rhs, false, NULL);
3625
3626 /* Remove the qualifiers for the rest of the expressions and create
3627 the VAL temp variable to hold the RHS. */
3628 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3629 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3630 val = create_tmp_var (nonatomic_rhs_type, NULL);
3631 TREE_ADDRESSABLE (val) = 1;
3632 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3633 SET_EXPR_LOCATION (rhs, loc);
3634 add_stmt (rhs);
3635
3636 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3637 an atomic_store. */
3638 if (modifycode == NOP_EXPR)
3639 {
3640 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3641 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3642 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3643 params->quick_push (lhs_addr);
3644 params->quick_push (rhs);
3645 params->quick_push (seq_cst);
3646 func_call = build_function_call_vec (loc, fndecl, params, NULL);
3647 add_stmt (func_call);
3648
3649 /* Finish the compound statement. */
3650 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3651
3652 /* VAL is the value which was stored, return a COMPOUND_STMT of
3653 the statement and that value. */
3654 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3655 }
3656
3657 /* Create the variables and labels required for the op= form. */
3658 old = create_tmp_var (nonatomic_lhs_type, NULL);
3659 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3660 TREE_ADDRESSABLE (val) = 1;
3661
3662 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3663 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3664 TREE_ADDRESSABLE (newval) = 1;
3665
3666 loop_decl = create_artificial_label (loc);
3667 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3668
3669 done_decl = create_artificial_label (loc);
3670 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3671
3672 /* __atomic_load (addr, &old, SEQ_CST). */
3673 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3674 params->quick_push (lhs_addr);
3675 params->quick_push (old_addr);
3676 params->quick_push (seq_cst);
3677 func_call = build_function_call_vec (loc, fndecl, params, NULL);
3678 add_stmt (func_call);
3679 params->truncate (0);
3680
3681 /* Create the expressions for floating-point environment
3682 manipulation, if required. */
3683 bool need_fenv = (flag_trapping_math
3684 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3685 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3686 if (need_fenv)
3687 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3688
3689 if (hold_call)
3690 add_stmt (hold_call);
3691
3692 /* loop: */
3693 add_stmt (loop_label);
3694
3695 /* newval = old + val; */
3696 rhs = build_binary_op (loc, modifycode, old, val, 1);
3697 rhs = convert_for_assignment (loc, nonatomic_lhs_type, rhs, NULL_TREE,
3698 ic_assign, false, NULL_TREE,
3699 NULL_TREE, 0);
3700 if (rhs != error_mark_node)
3701 {
3702 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3703 SET_EXPR_LOCATION (rhs, loc);
3704 add_stmt (rhs);
3705 }
3706
3707 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3708 goto done; */
3709 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3710 params->quick_push (lhs_addr);
3711 params->quick_push (old_addr);
3712 params->quick_push (newval_addr);
3713 params->quick_push (integer_zero_node);
3714 params->quick_push (seq_cst);
3715 params->quick_push (seq_cst);
3716 func_call = build_function_call_vec (loc, fndecl, params, NULL);
3717
3718 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3719 SET_EXPR_LOCATION (goto_stmt, loc);
3720
3721 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3722 SET_EXPR_LOCATION (stmt, loc);
3723 add_stmt (stmt);
3724
3725 if (clear_call)
3726 add_stmt (clear_call);
3727
3728 /* goto loop; */
3729 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3730 SET_EXPR_LOCATION (goto_stmt, loc);
3731 add_stmt (goto_stmt);
3732
3733 /* done: */
3734 add_stmt (done_label);
3735
3736 if (update_call)
3737 add_stmt (update_call);
3738
3739 /* Finish the compound statement. */
3740 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3741
3742 /* NEWVAL is the value that was successfully stored, return a
3743 COMPOUND_EXPR of the statement and the appropriate value. */
3744 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3745 return_old_p ? old : newval);
3746}
3747
3e4093b6
RS
3748/* Construct and perhaps optimize a tree representation
3749 for a unary operation. CODE, a tree_code, specifies the operation
3750 and XARG is the operand.
3751 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3752 the default promotions (such as from short to int).
3753 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3754 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3755 arrays to pointers in C99.
3756
3757 LOCATION is the location of the operator. */
400fbf9f 3758
3e4093b6 3759tree
c9f9eb5d
AH
3760build_unary_op (location_t location,
3761 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3762{
3763 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3764 tree arg = xarg;
3765 tree argtype = 0;
808d6eaa 3766 enum tree_code typecode;
3e4093b6 3767 tree val;
c9f9eb5d 3768 tree ret = error_mark_node;
8ce94e44 3769 tree eptype = NULL_TREE;
3e4093b6 3770 int noconvert = flag;
4de67c26 3771 const char *invalid_op_diag;
928c19bb
JM
3772 bool int_operands;
3773
3774 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3775 if (int_operands)
3776 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3777
808d6eaa
JM
3778 if (code != ADDR_EXPR)
3779 arg = require_complete_type (arg);
3780
3781 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3782 if (typecode == ERROR_MARK)
3783 return error_mark_node;
3784 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3785 typecode = INTEGER_TYPE;
6c36d76b 3786
4de67c26
JM
3787 if ((invalid_op_diag
3788 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3789 {
c9f9eb5d 3790 error_at (location, invalid_op_diag);
4de67c26
JM
3791 return error_mark_node;
3792 }
3793
8ce94e44
JM
3794 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3795 {
3796 eptype = TREE_TYPE (arg);
3797 arg = TREE_OPERAND (arg, 0);
3798 }
3799
3e4093b6
RS
3800 switch (code)
3801 {
3802 case CONVERT_EXPR:
3803 /* This is used for unary plus, because a CONVERT_EXPR
3804 is enough to prevent anybody from looking inside for
3805 associativity, but won't generate any code. */
3806 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3807 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3808 || typecode == VECTOR_TYPE))
400fbf9f 3809 {
c9f9eb5d 3810 error_at (location, "wrong type argument to unary plus");
3e4093b6 3811 return error_mark_node;
400fbf9f 3812 }
3e4093b6
RS
3813 else if (!noconvert)
3814 arg = default_conversion (arg);
db3927fb 3815 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3816 break;
3817
3e4093b6
RS
3818 case NEGATE_EXPR:
3819 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3820 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3821 || typecode == VECTOR_TYPE))
3822 {
c9f9eb5d 3823 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3824 return error_mark_node;
3825 }
3826 else if (!noconvert)
3827 arg = default_conversion (arg);
400fbf9f
JW
3828 break;
3829
3e4093b6 3830 case BIT_NOT_EXPR:
462643f0
AP
3831 /* ~ works on integer types and non float vectors. */
3832 if (typecode == INTEGER_TYPE
3833 || (typecode == VECTOR_TYPE
3834 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3835 {
3e4093b6
RS
3836 if (!noconvert)
3837 arg = default_conversion (arg);
03d5b1f5 3838 }
3e4093b6 3839 else if (typecode == COMPLEX_TYPE)
400fbf9f 3840 {
3e4093b6 3841 code = CONJ_EXPR;
c1771a20 3842 pedwarn (location, OPT_Wpedantic,
fcf73884 3843 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3844 if (!noconvert)
3845 arg = default_conversion (arg);
3846 }
3847 else
3848 {
c9f9eb5d 3849 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3850 return error_mark_node;
400fbf9f
JW
3851 }
3852 break;
3853
3e4093b6 3854 case ABS_EXPR:
11017cc7 3855 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3856 {
c9f9eb5d 3857 error_at (location, "wrong type argument to abs");
3e4093b6 3858 return error_mark_node;
400fbf9f 3859 }
3e4093b6
RS
3860 else if (!noconvert)
3861 arg = default_conversion (arg);
400fbf9f
JW
3862 break;
3863
3e4093b6
RS
3864 case CONJ_EXPR:
3865 /* Conjugating a real value is a no-op, but allow it anyway. */
3866 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3867 || typecode == COMPLEX_TYPE))
400fbf9f 3868 {
c9f9eb5d 3869 error_at (location, "wrong type argument to conjugation");
3e4093b6 3870 return error_mark_node;
400fbf9f 3871 }
3e4093b6
RS
3872 else if (!noconvert)
3873 arg = default_conversion (arg);
400fbf9f
JW
3874 break;
3875
3e4093b6 3876 case TRUTH_NOT_EXPR:
ab22c1fa 3877 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3878 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3879 && typecode != COMPLEX_TYPE)
400fbf9f 3880 {
c9f9eb5d
AH
3881 error_at (location,
3882 "wrong type argument to unary exclamation mark");
3e4093b6 3883 return error_mark_node;
400fbf9f 3884 }
a27d595d
JM
3885 if (int_operands)
3886 {
3887 arg = c_objc_common_truthvalue_conversion (location, xarg);
3888 arg = remove_c_maybe_const_expr (arg);
3889 }
3890 else
3891 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3892 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3893 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3894 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3895 location = EXPR_LOCATION (ret);
c9f9eb5d 3896 goto return_build_unary_op;
3e4093b6 3897
3e4093b6 3898 case REALPART_EXPR:
3e4093b6 3899 case IMAGPART_EXPR:
fb52b50a
NF
3900 ret = build_real_imag_expr (location, code, arg);
3901 if (ret == error_mark_node)
3902 return error_mark_node;
8ce94e44
JM
3903 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3904 eptype = TREE_TYPE (eptype);
c9f9eb5d 3905 goto return_build_unary_op;
3e4093b6
RS
3906
3907 case PREINCREMENT_EXPR:
3908 case POSTINCREMENT_EXPR:
3909 case PREDECREMENT_EXPR:
3910 case POSTDECREMENT_EXPR:
3e4093b6 3911
928c19bb
JM
3912 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3913 {
3914 tree inner = build_unary_op (location, code,
3915 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3916 if (inner == error_mark_node)
3917 return error_mark_node;
3918 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3919 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3920 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3921 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3922 goto return_build_unary_op;
3923 }
3924
925e8657
NP
3925 /* Complain about anything that is not a true lvalue. In
3926 Objective-C, skip this check for property_refs. */
f90e8e2e 3927 if (!objc_is_property_ref (arg)
7bd11157
TT
3928 && !lvalue_or_else (location,
3929 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3930 || code == POSTINCREMENT_EXPR)
3931 ? lv_increment
3932 : lv_decrement)))
928c19bb
JM
3933 return error_mark_node;
3934
09639a83
ILT
3935 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3936 {
3937 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3938 warning_at (location, OPT_Wc___compat,
3939 "increment of enumeration value is invalid in C++");
3940 else
3941 warning_at (location, OPT_Wc___compat,
3942 "decrement of enumeration value is invalid in C++");
3943 }
3944
928c19bb
JM
3945 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3946 arg = c_fully_fold (arg, false, NULL);
3947
267bac10
JM
3948 bool atomic_op;
3949 atomic_op = really_atomic_lvalue (arg);
3950
3e4093b6
RS
3951 /* Increment or decrement the real part of the value,
3952 and don't change the imaginary part. */
3953 if (typecode == COMPLEX_TYPE)
400fbf9f 3954 {
3e4093b6
RS
3955 tree real, imag;
3956
c1771a20 3957 pedwarn (location, OPT_Wpedantic,
509c9d60 3958 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3959
267bac10
JM
3960 if (!atomic_op)
3961 {
3962 arg = stabilize_reference (arg);
3963 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3964 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3965 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3966 if (real == error_mark_node || imag == error_mark_node)
3967 return error_mark_node;
3968 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3969 real, imag);
3970 goto return_build_unary_op;
3971 }
400fbf9f 3972 }
3e4093b6
RS
3973
3974 /* Report invalid types. */
3975
ab22c1fa 3976 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10
JM
3977 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
3978 && typecode != COMPLEX_TYPE)
400fbf9f 3979 {
3e4093b6 3980 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 3981 error_at (location, "wrong type argument to increment");
c22cacf3 3982 else
c9f9eb5d 3983 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
3984
3985 return error_mark_node;
400fbf9f 3986 }
400fbf9f 3987
3e4093b6
RS
3988 {
3989 tree inc;
400fbf9f 3990
3e4093b6
RS
3991 argtype = TREE_TYPE (arg);
3992
3993 /* Compute the increment. */
3994
3995 if (typecode == POINTER_TYPE)
3996 {
3997 /* If pointer target is an undefined struct,
3998 we just cannot know how to do the arithmetic. */
b70cef5d 3999 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4000 {
4001 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d
AH
4002 error_at (location,
4003 "increment of pointer to unknown structure");
3e4093b6 4004 else
c9f9eb5d
AH
4005 error_at (location,
4006 "decrement of pointer to unknown structure");
3e4093b6 4007 }
b70cef5d
JJ
4008 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4009 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4010 {
3e4093b6 4011 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4012 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4013 "wrong type argument to increment");
3e4093b6 4014 else
44d90fe1 4015 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4016 "wrong type argument to decrement");
3e4093b6
RS
4017 }
4018
b70cef5d 4019 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4020 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4021 }
b70cef5d 4022 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4023 {
4024 /* For signed fract types, we invert ++ to -- or
4025 -- to ++, and change inc from 1 to -1, because
4026 it is not possible to represent 1 in signed fract constants.
4027 For unsigned fract types, the result always overflows and
4028 we get an undefined (original) or the maximum value. */
4029 if (code == PREINCREMENT_EXPR)
4030 code = PREDECREMENT_EXPR;
4031 else if (code == PREDECREMENT_EXPR)
4032 code = PREINCREMENT_EXPR;
4033 else if (code == POSTINCREMENT_EXPR)
4034 code = POSTDECREMENT_EXPR;
4035 else /* code == POSTDECREMENT_EXPR */
4036 code = POSTINCREMENT_EXPR;
4037
4038 inc = integer_minus_one_node;
4039 inc = convert (argtype, inc);
4040 }
3e4093b6 4041 else
5be014d5
AP
4042 {
4043 inc = integer_one_node;
4044 inc = convert (argtype, inc);
4045 }
3e4093b6 4046
925e8657
NP
4047 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4048 need to ask Objective-C to build the increment or decrement
4049 expression for it. */
4050 if (objc_is_property_ref (arg))
f90e8e2e 4051 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4052 arg, inc);
4053
3e4093b6 4054 /* Report a read-only lvalue. */
f37acdf9 4055 if (TYPE_READONLY (argtype))
953ff289
DN
4056 {
4057 readonly_error (arg,
4058 ((code == PREINCREMENT_EXPR
4059 || code == POSTINCREMENT_EXPR)
4060 ? lv_increment : lv_decrement));
4061 return error_mark_node;
4062 }
f37acdf9
JM
4063 else if (TREE_READONLY (arg))
4064 readonly_warning (arg,
4065 ((code == PREINCREMENT_EXPR
4066 || code == POSTINCREMENT_EXPR)
4067 ? lv_increment : lv_decrement));
3e4093b6 4068
267bac10
JM
4069 /* If the argument is atomic, use the special code sequences for
4070 atomic compound assignment. */
4071 if (atomic_op)
4072 {
4073 arg = stabilize_reference (arg);
4074 ret = build_atomic_assign (location, arg,
4075 ((code == PREINCREMENT_EXPR
4076 || code == POSTINCREMENT_EXPR)
4077 ? PLUS_EXPR
4078 : MINUS_EXPR),
4079 (FRACT_MODE_P (TYPE_MODE (argtype))
4080 ? inc
4081 : integer_one_node),
4082 (code == POSTINCREMENT_EXPR
4083 || code == POSTDECREMENT_EXPR));
4084 goto return_build_unary_op;
4085 }
4086
3e4093b6
RS
4087 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4088 val = boolean_increment (code, arg);
4089 else
53fb4de3 4090 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4091 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4092 if (TREE_CODE (val) != code)
6de9cd9a 4093 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4094 ret = val;
4095 goto return_build_unary_op;
3e4093b6
RS
4096 }
4097
4098 case ADDR_EXPR:
4099 /* Note that this operation never does default_conversion. */
4100
2b4b7036
JM
4101 /* The operand of unary '&' must be an lvalue (which excludes
4102 expressions of type void), or, in C99, the result of a [] or
4103 unary '*' operator. */
4104 if (VOID_TYPE_P (TREE_TYPE (arg))
4105 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4106 && (TREE_CODE (arg) != INDIRECT_REF
4107 || !flag_isoc99))
4108 pedwarn (location, 0, "taking address of expression of type %<void%>");
4109
3e4093b6
RS
4110 /* Let &* cancel out to simplify resulting code. */
4111 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 4112 {
3e4093b6
RS
4113 /* Don't let this be an lvalue. */
4114 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4115 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4116 ret = TREE_OPERAND (arg, 0);
4117 goto return_build_unary_op;
400fbf9f 4118 }
1eb8759b 4119
7c672dfc 4120 /* For &x[y], return x+y */
3e4093b6 4121 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4122 {
f2a71bbc
JM
4123 tree op0 = TREE_OPERAND (arg, 0);
4124 if (!c_mark_addressable (op0))
3e4093b6 4125 return error_mark_node;
1eb8759b 4126 }
1eb8759b 4127
3e4093b6
RS
4128 /* Anything not already handled and not a true memory reference
4129 or a non-lvalue array is an error. */
4130 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4131 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4132 return error_mark_node;
b6a10c9f 4133
928c19bb
JM
4134 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4135 folding later. */
4136 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4137 {
4138 tree inner = build_unary_op (location, code,
4139 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4140 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4141 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4142 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4143 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4144 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4145 goto return_build_unary_op;
4146 }
4147
3e4093b6
RS
4148 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4149 argtype = TREE_TYPE (arg);
400fbf9f 4150
3e4093b6 4151 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4152 to which the address will point. This is only needed
f2c1da78 4153 for function types. */
6615c446 4154 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4155 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4156 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4157 {
4158 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4159 int quals = orig_quals;
4160
4161 if (TREE_READONLY (arg))
4162 quals |= TYPE_QUAL_CONST;
4163 if (TREE_THIS_VOLATILE (arg))
4164 quals |= TYPE_QUAL_VOLATILE;
4165
f2c1da78
JM
4166 argtype = c_build_qualified_type (argtype, quals);
4167 }
400fbf9f 4168
3e4093b6
RS
4169 if (!c_mark_addressable (arg))
4170 return error_mark_node;
400fbf9f 4171
abb54d14
JM
4172 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4173 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4174
5cc200fc 4175 argtype = build_pointer_type (argtype);
5e55f99d
RH
4176
4177 /* ??? Cope with user tricks that amount to offsetof. Delete this
4178 when we have proper support for integer constant expressions. */
4179 val = get_base_address (arg);
4180 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
4181 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4182 {
cf9e9959 4183 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4184 goto return_build_unary_op;
3aa2ddb8 4185 }
5e55f99d 4186
5cc200fc 4187 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4188
c9f9eb5d
AH
4189 ret = val;
4190 goto return_build_unary_op;
400fbf9f 4191
3e4093b6 4192 default:
1344f9a3 4193 gcc_unreachable ();
3e4093b6 4194 }
400fbf9f 4195
3e4093b6
RS
4196 if (argtype == 0)
4197 argtype = TREE_TYPE (arg);
928c19bb
JM
4198 if (TREE_CODE (arg) == INTEGER_CST)
4199 ret = (require_constant_value
db3927fb
AH
4200 ? fold_build1_initializer_loc (location, code, argtype, arg)
4201 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4202 else
4203 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4204 return_build_unary_op:
4205 gcc_assert (ret != error_mark_node);
928c19bb
JM
4206 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4207 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4208 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4209 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4210 ret = note_integer_operands (ret);
8ce94e44
JM
4211 if (eptype)
4212 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4213 protected_set_expr_location (ret, location);
4214 return ret;
3e4093b6 4215}
400fbf9f 4216
3e4093b6
RS
4217/* Return nonzero if REF is an lvalue valid for this language.
4218 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4219 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4220
1e4ae551 4221bool
58f9752a 4222lvalue_p (const_tree ref)
3e4093b6 4223{
58f9752a 4224 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4225
3e4093b6
RS
4226 switch (code)
4227 {
4228 case REALPART_EXPR:
4229 case IMAGPART_EXPR:
4230 case COMPONENT_REF:
4231 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4232
928c19bb
JM
4233 case C_MAYBE_CONST_EXPR:
4234 return lvalue_p (TREE_OPERAND (ref, 1));
4235
3e4093b6
RS
4236 case COMPOUND_LITERAL_EXPR:
4237 case STRING_CST:
4238 return 1;
400fbf9f 4239
3e4093b6
RS
4240 case INDIRECT_REF:
4241 case ARRAY_REF:
36536d79 4242 case ARRAY_NOTATION_REF:
3e4093b6
RS
4243 case VAR_DECL:
4244 case PARM_DECL:
4245 case RESULT_DECL:
4246 case ERROR_MARK:
4247 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4248 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4249
3e4093b6 4250 case BIND_EXPR:
3e4093b6 4251 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4252
3e4093b6
RS
4253 default:
4254 return 0;
4255 }
4256}
400fbf9f 4257\f
f37acdf9
JM
4258/* Give a warning for storing in something that is read-only in GCC
4259 terms but not const in ISO C terms. */
4260
4261static void
4262readonly_warning (tree arg, enum lvalue_use use)
4263{
4264 switch (use)
4265 {
4266 case lv_assign:
4267 warning (0, "assignment of read-only location %qE", arg);
4268 break;
4269 case lv_increment:
4270 warning (0, "increment of read-only location %qE", arg);
4271 break;
4272 case lv_decrement:
4273 warning (0, "decrement of read-only location %qE", arg);
4274 break;
4275 default:
4276 gcc_unreachable ();
4277 }
4278 return;
4279}
4280
37dc0d8d
JM
4281
4282/* Return nonzero if REF is an lvalue valid for this language;
4283 otherwise, print an error message and return zero. USE says
7bd11157
TT
4284 how the lvalue is being used and so selects the error message.
4285 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4286
4287static int
7bd11157 4288lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4289{
4290 int win = lvalue_p (ref);
4291
4292 if (!win)
7bd11157 4293 lvalue_error (loc, use);
37dc0d8d
JM
4294
4295 return win;
4296}
3e4093b6
RS
4297\f
4298/* Mark EXP saying that we need to be able to take the
4299 address of it; it should not be allocated in a register.
4300 Returns true if successful. */
54c93c30 4301
3e4093b6
RS
4302bool
4303c_mark_addressable (tree exp)
400fbf9f 4304{
3e4093b6 4305 tree x = exp;
95602da1 4306
3e4093b6
RS
4307 while (1)
4308 switch (TREE_CODE (x))
4309 {
4310 case COMPONENT_REF:
4311 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4312 {
0039fa55
AN
4313 error
4314 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4315 return false;
4316 }
95602da1 4317
3e4093b6 4318 /* ... fall through ... */
95602da1 4319
3e4093b6
RS
4320 case ADDR_EXPR:
4321 case ARRAY_REF:
4322 case REALPART_EXPR:
4323 case IMAGPART_EXPR:
4324 x = TREE_OPERAND (x, 0);
4325 break;
95602da1 4326
3e4093b6
RS
4327 case COMPOUND_LITERAL_EXPR:
4328 case CONSTRUCTOR:
4329 TREE_ADDRESSABLE (x) = 1;
4330 return true;
95602da1 4331
3e4093b6
RS
4332 case VAR_DECL:
4333 case CONST_DECL:
4334 case PARM_DECL:
4335 case RESULT_DECL:
5baeaac0 4336 if (C_DECL_REGISTER (x)
3e4093b6
RS
4337 && DECL_NONLOCAL (x))
4338 {
e697b20f 4339 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4340 {
0039fa55
AN
4341 error
4342 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4343 return false;
4344 }
509c9d60 4345 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4346 }
5baeaac0 4347 else if (C_DECL_REGISTER (x))
3e4093b6 4348 {
e697b20f 4349 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4350 error ("address of global register variable %qD requested", x);
4351 else
4352 error ("address of register variable %qD requested", x);
4353 return false;
3e4093b6 4354 }
400fbf9f 4355
3e4093b6
RS
4356 /* drops in */
4357 case FUNCTION_DECL:
4358 TREE_ADDRESSABLE (x) = 1;
4359 /* drops out */
4360 default:
4361 return true;
4362 }
4363}
4364\f
2d2e923f
MLI
4365/* Convert EXPR to TYPE, warning about conversion problems with
4366 constants. SEMANTIC_TYPE is the type this conversion would use
4367 without excess precision. If SEMANTIC_TYPE is NULL, this function
4368 is equivalent to convert_and_check. This function is a wrapper that
4369 handles conversions that may be different than
4370 the usual ones because of excess precision. */
4371
4372static tree
4373ep_convert_and_check (tree type, tree expr, tree semantic_type)
4374{
4375 if (TREE_TYPE (expr) == type)
4376 return expr;
4377
4378 if (!semantic_type)
4379 return convert_and_check (type, expr);
4380
4381 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4382 && TREE_TYPE (expr) != semantic_type)
4383 {
4384 /* For integers, we need to check the real conversion, not
4385 the conversion to the excess precision type. */
4386 expr = convert_and_check (semantic_type, expr);
4387 }
4388 /* Result type is the excess precision type, which should be
4389 large enough, so do not check. */
4390 return convert (type, expr);
4391}
4392
928c19bb
JM
4393/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4394 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4395 if folded to an integer constant then the unselected half may
4396 contain arbitrary operations not normally permitted in constant
c2255bc4 4397 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4398
4399tree
744aa42f 4400build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4401 tree op1, tree op1_original_type, tree op2,
4402 tree op2_original_type)
400fbf9f 4403{
3e4093b6
RS
4404 tree type1;
4405 tree type2;
4406 enum tree_code code1;
4407 enum tree_code code2;
4408 tree result_type = NULL;
2d2e923f 4409 tree semantic_result_type = NULL;
3e4093b6 4410 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4411 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4412 bool ifexp_int_operands;
928c19bb 4413 tree ret;
400fbf9f 4414
4d84fe7c
JM
4415 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4416 if (op1_int_operands)
4417 op1 = remove_c_maybe_const_expr (op1);
4418 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4419 if (op2_int_operands)
4420 op2 = remove_c_maybe_const_expr (op2);
4421 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4422 if (ifexp_int_operands)
4423 ifexp = remove_c_maybe_const_expr (ifexp);
4424
3e4093b6
RS
4425 /* Promote both alternatives. */
4426
4427 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4428 op1 = default_conversion (op1);
4429 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4430 op2 = default_conversion (op2);
4431
4432 if (TREE_CODE (ifexp) == ERROR_MARK
4433 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4434 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4435 return error_mark_node;
400fbf9f 4436
3e4093b6
RS
4437 type1 = TREE_TYPE (op1);
4438 code1 = TREE_CODE (type1);
4439 type2 = TREE_TYPE (op2);
4440 code2 = TREE_CODE (type2);
4441
b1adf557
JM
4442 /* C90 does not permit non-lvalue arrays in conditional expressions.
4443 In C99 they will be pointers by now. */
4444 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4445 {
744aa42f 4446 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4447 return error_mark_node;
4448 }
4449
8ce94e44
JM
4450 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4451 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4452 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4453 || code1 == COMPLEX_TYPE)
4454 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4455 || code2 == COMPLEX_TYPE))
4456 {
2d2e923f 4457 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4458 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4459 {
4460 op1 = TREE_OPERAND (op1, 0);
4461 type1 = TREE_TYPE (op1);
4462 gcc_assert (TREE_CODE (type1) == code1);
4463 }
4464 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4465 {
4466 op2 = TREE_OPERAND (op2, 0);
4467 type2 = TREE_TYPE (op2);
4468 gcc_assert (TREE_CODE (type2) == code2);
4469 }
4470 }
4471
d130ae11
ILT
4472 if (warn_cxx_compat)
4473 {
4474 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4475 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4476
4477 if (TREE_CODE (t1) == ENUMERAL_TYPE
4478 && TREE_CODE (t2) == ENUMERAL_TYPE
4479 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4480 warning_at (colon_loc, OPT_Wc___compat,
4481 ("different enum types in conditional is "
4482 "invalid in C++: %qT vs %qT"),
4483 t1, t2);
4484 }
4485
3e4093b6
RS
4486 /* Quickly detect the usual case where op1 and op2 have the same type
4487 after promotion. */
4488 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4489 {
3e4093b6
RS
4490 if (type1 == type2)
4491 result_type = type1;
4492 else
4493 result_type = TYPE_MAIN_VARIANT (type1);
4494 }
4495 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4496 || code1 == COMPLEX_TYPE)
4497 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4498 || code2 == COMPLEX_TYPE))
3e4093b6 4499 {
ccf7f880 4500 result_type = c_common_type (type1, type2);
0a0b3574
MM
4501 do_warn_double_promotion (result_type, type1, type2,
4502 "implicit conversion from %qT to %qT to "
4503 "match other result of conditional",
4504 colon_loc);
400fbf9f 4505
3e4093b6
RS
4506 /* If -Wsign-compare, warn here if type1 and type2 have
4507 different signedness. We'll promote the signed to unsigned
4508 and later code won't know it used to be different.
4509 Do this check on the original types, so that explicit casts
4510 will be considered, but default promotions won't. */
7d882b83 4511 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4512 {
8df83eae
RK
4513 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4514 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4515
3e4093b6
RS
4516 if (unsigned_op1 ^ unsigned_op2)
4517 {
6ac01510
ILT
4518 bool ovf;
4519
3e4093b6
RS
4520 /* Do not warn if the result type is signed, since the
4521 signed type will only be chosen if it can represent
4522 all the values of the unsigned type. */
3f75a254 4523 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4524 /* OK */;
3e4093b6 4525 else
928c19bb
JM
4526 {
4527 bool op1_maybe_const = true;
4528 bool op2_maybe_const = true;
4529
4530 /* Do not warn if the signed quantity is an
4531 unsuffixed integer literal (or some static
4532 constant expression involving such literals) and
4533 it is non-negative. This warning requires the
4534 operands to be folded for best results, so do
4535 that folding in this case even without
4536 warn_sign_compare to avoid warning options
4537 possibly affecting code generation. */
f5178456
RS
4538 c_inhibit_evaluation_warnings
4539 += (ifexp == truthvalue_false_node);
928c19bb
JM
4540 op1 = c_fully_fold (op1, require_constant_value,
4541 &op1_maybe_const);
f5178456
RS
4542 c_inhibit_evaluation_warnings
4543 -= (ifexp == truthvalue_false_node);
4544
4545 c_inhibit_evaluation_warnings
4546 += (ifexp == truthvalue_true_node);
928c19bb
JM
4547 op2 = c_fully_fold (op2, require_constant_value,
4548 &op2_maybe_const);
f5178456
RS
4549 c_inhibit_evaluation_warnings
4550 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4551
4552 if (warn_sign_compare)
4553 {
4554 if ((unsigned_op2
4555 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4556 || (unsigned_op1
4557 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4558 /* OK */;
4559 else
744aa42f
ILT
4560 warning_at (colon_loc, OPT_Wsign_compare,
4561 ("signed and unsigned type in "
4562 "conditional expression"));
928c19bb
JM
4563 }
4564 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4565 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4566 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4567 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4568 }
3e4093b6
RS
4569 }
4570 }
4571 }
4572 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4573 {
fcf73884 4574 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4575 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4576 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4577 result_type = void_type_node;
4578 }
4579 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4580 {
36c5e70a
BE
4581 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4582 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4583 addr_space_t as_common;
4584
744aa42f 4585 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4586 result_type = common_pointer_type (type1, type2);
6aa3c60d 4587 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4588 result_type = type2;
6aa3c60d 4589 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4590 result_type = type1;
4591 else if (!addr_space_superset (as1, as2, &as_common))
4592 {
4593 error_at (colon_loc, "pointers to disjoint address spaces "
4594 "used in conditional expression");
4595 return error_mark_node;
4596 }
267bac10
JM
4597 else if (VOID_TYPE_P (TREE_TYPE (type1))
4598 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4599 {
fcf73884 4600 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4601 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4602 "ISO C forbids conditional expr between "
bda67431 4603 "%<void *%> and function pointer");
3e4093b6
RS
4604 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4605 TREE_TYPE (type2)));
34a80643 4606 }
267bac10
JM
4607 else if (VOID_TYPE_P (TREE_TYPE (type2))
4608 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4609 {
fcf73884 4610 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4611 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4612 "ISO C forbids conditional expr between "
bda67431 4613 "%<void *%> and function pointer");
3e4093b6
RS
4614 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4615 TREE_TYPE (type1)));
1c2a9b35 4616 }
b581b85b
NP
4617 /* Objective-C pointer comparisons are a bit more lenient. */
4618 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4619 result_type = objc_common_type (type1, type2);
34a80643 4620 else
ab87f8c8 4621 {
36c5e70a
BE
4622 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4623
b581b85b
NP
4624 pedwarn (colon_loc, 0,
4625 "pointer type mismatch in conditional expression");
36c5e70a
BE
4626 result_type = build_pointer_type
4627 (build_qualified_type (void_type_node, qual));
ab87f8c8 4628 }
3e4093b6
RS
4629 }
4630 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4631 {
6aa3c60d 4632 if (!null_pointer_constant_p (orig_op2))
744aa42f 4633 pedwarn (colon_loc, 0,
509c9d60 4634 "pointer/integer type mismatch in conditional expression");
3e4093b6 4635 else
ab87f8c8 4636 {
3e4093b6 4637 op2 = null_pointer_node;
ab87f8c8 4638 }
3e4093b6
RS
4639 result_type = type1;
4640 }
4641 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4642 {
6aa3c60d 4643 if (!null_pointer_constant_p (orig_op1))
744aa42f 4644 pedwarn (colon_loc, 0,
509c9d60 4645 "pointer/integer type mismatch in conditional expression");
3e4093b6 4646 else
ab87f8c8 4647 {
3e4093b6 4648 op1 = null_pointer_node;
ab87f8c8 4649 }
3e4093b6
RS
4650 result_type = type2;
4651 }
1c2a9b35 4652
3e4093b6
RS
4653 if (!result_type)
4654 {
4655 if (flag_cond_mismatch)
4656 result_type = void_type_node;
4657 else
400fbf9f 4658 {
c2255bc4 4659 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4660 return error_mark_node;
400fbf9f 4661 }
3e4093b6 4662 }
400fbf9f 4663
3e4093b6
RS
4664 /* Merge const and volatile flags of the incoming types. */
4665 result_type
4666 = build_type_variant (result_type,
afbd0665
AS
4667 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4668 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4669
2d2e923f
MLI
4670 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4671 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
b6a10c9f 4672
928c19bb
JM
4673 if (ifexp_bcp && ifexp == truthvalue_true_node)
4674 {
4675 op2_int_operands = true;
4676 op1 = c_fully_fold (op1, require_constant_value, NULL);
4677 }
4678 if (ifexp_bcp && ifexp == truthvalue_false_node)
4679 {
4680 op1_int_operands = true;
4681 op2 = c_fully_fold (op2, require_constant_value, NULL);
4682 }
4d84fe7c 4683 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4684 && op1_int_operands
4685 && op2_int_operands);
4686 if (int_operands)
4687 {
4688 int_const = ((ifexp == truthvalue_true_node
4689 && TREE_CODE (orig_op1) == INTEGER_CST
4690 && !TREE_OVERFLOW (orig_op1))
4691 || (ifexp == truthvalue_false_node
4692 && TREE_CODE (orig_op2) == INTEGER_CST
4693 && !TREE_OVERFLOW (orig_op2)));
4694 }
4695 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4696 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4697 else
4698 {
01c7ccbb
JM
4699 if (int_operands)
4700 {
4701 op1 = remove_c_maybe_const_expr (op1);
4702 op2 = remove_c_maybe_const_expr (op2);
4703 }
928c19bb
JM
4704 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4705 if (int_operands)
4706 ret = note_integer_operands (ret);
4707 }
2d2e923f
MLI
4708 if (semantic_result_type)
4709 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4710
c2255bc4 4711 protected_set_expr_location (ret, colon_loc);
928c19bb 4712 return ret;
3e4093b6
RS
4713}
4714\f
487a92fe 4715/* Return a compound expression that performs two expressions and
c2255bc4
AH
4716 returns the value of the second of them.
4717
4718 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4719
3e4093b6 4720tree
c2255bc4 4721build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4722{
4d84fe7c 4723 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4724 tree eptype = NULL_TREE;
928c19bb
JM
4725 tree ret;
4726
939b37da
BI
4727 if (flag_enable_cilkplus
4728 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4729 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4730 {
4731 error_at (loc,
4732 "spawned function call cannot be part of a comma expression");
4733 return error_mark_node;
4734 }
4d84fe7c
JM
4735 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4736 if (expr1_int_operands)
4737 expr1 = remove_c_maybe_const_expr (expr1);
4738 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4739 if (expr2_int_operands)
4740 expr2 = remove_c_maybe_const_expr (expr2);
4741
8ce94e44
JM
4742 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4743 expr1 = TREE_OPERAND (expr1, 0);
4744 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4745 {
4746 eptype = TREE_TYPE (expr2);
4747 expr2 = TREE_OPERAND (expr2, 0);
4748 }
4749
3f75a254 4750 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4751 {
4752 /* The left-hand operand of a comma expression is like an expression
c5409249 4753 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4754 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4755 if (warn_unused_value)
47aecf47 4756 {
e14a6540 4757 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4758 && CONVERT_EXPR_P (expr1))
47aecf47 4759 ; /* (void) a, b */
e14a6540
JM
4760 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4761 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4762 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4763 ; /* (void) a, (void) b, c */
4764 else
b8698a0f 4765 warning_at (loc, OPT_Wunused_value,
c2255bc4 4766 "left-hand operand of comma expression has no effect");
47aecf47 4767 }
3e4093b6 4768 }
400fbf9f 4769
3e4093b6
RS
4770 /* With -Wunused, we should also warn if the left-hand operand does have
4771 side-effects, but computes a value which is not used. For example, in
4772 `foo() + bar(), baz()' the result of the `+' operator is not used,
4773 so we should issue a warning. */
4774 else if (warn_unused_value)
c2255bc4 4775 warn_if_unused_value (expr1, loc);
400fbf9f 4776
e63d6886
AP
4777 if (expr2 == error_mark_node)
4778 return error_mark_node;
4779
928c19bb
JM
4780 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4781
4782 if (flag_isoc99
4d84fe7c
JM
4783 && expr1_int_operands
4784 && expr2_int_operands)
928c19bb
JM
4785 ret = note_integer_operands (ret);
4786
8ce94e44
JM
4787 if (eptype)
4788 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4789
c2255bc4 4790 protected_set_expr_location (ret, loc);
928c19bb 4791 return ret;
3e4093b6 4792}
400fbf9f 4793
67165eb3
ILT
4794/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4795 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4796 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4797 of the cast. -Wcast-qual appeared on the command line. Named
4798 address space qualifiers are not handled here, because they result
4799 in different warnings. */
67165eb3
ILT
4800
4801static void
2ee3cb35 4802handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4803{
4804 tree in_type = type;
4805 tree in_otype = otype;
4806 int added = 0;
4807 int discarded = 0;
4808 bool is_const;
4809
4810 /* Check that the qualifiers on IN_TYPE are a superset of the
4811 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4812 nodes is uninteresting and we stop as soon as we hit a
4813 non-POINTER_TYPE node on either type. */
4814 do
4815 {
4816 in_otype = TREE_TYPE (in_otype);
4817 in_type = TREE_TYPE (in_type);
4818
4819 /* GNU C allows cv-qualified function types. 'const' means the
4820 function is very pure, 'volatile' means it can't return. We
4821 need to warn when such qualifiers are added, not when they're
4822 taken away. */
4823 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4824 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4825 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4826 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4827 else
36c5e70a
BE
4828 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4829 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4830 }
4831 while (TREE_CODE (in_type) == POINTER_TYPE
4832 && TREE_CODE (in_otype) == POINTER_TYPE);
4833
4834 if (added)
2ee3cb35
MLI
4835 warning_at (loc, OPT_Wcast_qual,
4836 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4837
4838 if (discarded)
4839 /* There are qualifiers present in IN_OTYPE that are not present
4840 in IN_TYPE. */
2ee3cb35
MLI
4841 warning_at (loc, OPT_Wcast_qual,
4842 "cast discards %q#v qualifier from pointer target type",
4843 discarded);
67165eb3
ILT
4844
4845 if (added || discarded)
4846 return;
4847
4848 /* A cast from **T to const **T is unsafe, because it can cause a
4849 const value to be changed with no additional warning. We only
4850 issue this warning if T is the same on both sides, and we only
4851 issue the warning if there are the same number of pointers on
4852 both sides, as otherwise the cast is clearly unsafe anyhow. A
4853 cast is unsafe when a qualifier is added at one level and const
4854 is not present at all outer levels.
4855
4856 To issue this warning, we check at each level whether the cast
4857 adds new qualifiers not already seen. We don't need to special
4858 case function types, as they won't have the same
4859 TYPE_MAIN_VARIANT. */
4860
4861 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4862 return;
4863 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4864 return;
4865
4866 in_type = type;
4867 in_otype = otype;
4868 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4869 do
4870 {
4871 in_type = TREE_TYPE (in_type);
4872 in_otype = TREE_TYPE (in_otype);
4873 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4874 && !is_const)
4875 {
2ee3cb35
MLI
4876 warning_at (loc, OPT_Wcast_qual,
4877 "to be safe all intermediate pointers in cast from "
4878 "%qT to %qT must be %<const%> qualified",
4879 otype, type);
67165eb3
ILT
4880 break;
4881 }
4882 if (is_const)
4883 is_const = TYPE_READONLY (in_type);
4884 }
4885 while (TREE_CODE (in_type) == POINTER_TYPE);
4886}
4887
b8698a0f 4888/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4889 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4890
3e4093b6 4891tree
c2255bc4 4892build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4893{
8ce94e44
JM
4894 tree value;
4895
4896 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4897 expr = TREE_OPERAND (expr, 0);
4898
4899 value = expr;
400fbf9f 4900
3e4093b6
RS
4901 if (type == error_mark_node || expr == error_mark_node)
4902 return error_mark_node;
400fbf9f 4903
3e4093b6
RS
4904 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4905 only in <protocol> qualifications. But when constructing cast expressions,
4906 the protocols do matter and must be kept around. */
700686fa
ZL
4907 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4908 return build1 (NOP_EXPR, type, expr);
4909
4910 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4911
3e4093b6
RS
4912 if (TREE_CODE (type) == ARRAY_TYPE)
4913 {
c2255bc4 4914 error_at (loc, "cast specifies array type");
3e4093b6
RS
4915 return error_mark_node;
4916 }
400fbf9f 4917
3e4093b6
RS
4918 if (TREE_CODE (type) == FUNCTION_TYPE)
4919 {
c2255bc4 4920 error_at (loc, "cast specifies function type");
3e4093b6
RS
4921 return error_mark_node;
4922 }
400fbf9f 4923
808d6eaa
JM
4924 if (!VOID_TYPE_P (type))
4925 {
4926 value = require_complete_type (value);
4927 if (value == error_mark_node)
4928 return error_mark_node;
4929 }
4930
3e4093b6
RS
4931 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4932 {
fcf73884
MLI
4933 if (TREE_CODE (type) == RECORD_TYPE
4934 || TREE_CODE (type) == UNION_TYPE)
c1771a20 4935 pedwarn (loc, OPT_Wpedantic,
fcf73884 4936 "ISO C forbids casting nonscalar to the same type");
3e4093b6
RS
4937 }
4938 else if (TREE_CODE (type) == UNION_TYPE)
4939 {
4940 tree field;
400fbf9f 4941
910ad8de 4942 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
4943 if (TREE_TYPE (field) != error_mark_node
4944 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4945 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
4946 break;
4947
4948 if (field)
400fbf9f 4949 {
3e4093b6 4950 tree t;
e616f54d 4951 bool maybe_const = true;
3e4093b6 4952
c1771a20 4953 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
4954 t = c_fully_fold (value, false, &maybe_const);
4955 t = build_constructor_single (type, field, t);
4956 if (!maybe_const)
4957 t = c_wrap_maybe_const (t, true);
4958 t = digest_init (loc, type, t,
bbbbb16a 4959 NULL_TREE, false, true, 0);
3e4093b6
RS
4960 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4961 return t;
400fbf9f 4962 }
c2255bc4 4963 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
4964 return error_mark_node;
4965 }
4966 else
4967 {
4968 tree otype, ovalue;
400fbf9f 4969
3e4093b6 4970 if (type == void_type_node)
c2255bc4
AH
4971 {
4972 tree t = build1 (CONVERT_EXPR, type, value);
4973 SET_EXPR_LOCATION (t, loc);
4974 return t;
4975 }
400fbf9f 4976
3e4093b6 4977 otype = TREE_TYPE (value);
400fbf9f 4978
3e4093b6 4979 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
4980 if (warn_cast_qual
4981 && TREE_CODE (type) == POINTER_TYPE
4982 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 4983 handle_warn_cast_qual (loc, type, otype);
400fbf9f 4984
36c5e70a
BE
4985 /* Warn about conversions between pointers to disjoint
4986 address spaces. */
4987 if (TREE_CODE (type) == POINTER_TYPE
4988 && TREE_CODE (otype) == POINTER_TYPE
4989 && !null_pointer_constant_p (value))
4990 {
4991 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4992 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4993 addr_space_t as_common;
4994
4995 if (!addr_space_superset (as_to, as_from, &as_common))
4996 {
4997 if (ADDR_SPACE_GENERIC_P (as_from))
4998 warning_at (loc, 0, "cast to %s address space pointer "
4999 "from disjoint generic address space pointer",
5000 c_addr_space_name (as_to));
5001
5002 else if (ADDR_SPACE_GENERIC_P (as_to))
5003 warning_at (loc, 0, "cast to generic address space pointer "
5004 "from disjoint %s address space pointer",
5005 c_addr_space_name (as_from));
5006
5007 else
5008 warning_at (loc, 0, "cast to %s address space pointer "
5009 "from disjoint %s address space pointer",
5010 c_addr_space_name (as_to),
5011 c_addr_space_name (as_from));
5012 }
5013 }
5014
3e4093b6 5015 /* Warn about possible alignment problems. */
3176a0c2 5016 if (STRICT_ALIGNMENT
3e4093b6
RS
5017 && TREE_CODE (type) == POINTER_TYPE
5018 && TREE_CODE (otype) == POINTER_TYPE
5019 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5020 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5021 /* Don't warn about opaque types, where the actual alignment
5022 restriction is unknown. */
5023 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5024 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5025 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5026 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5027 warning_at (loc, OPT_Wcast_align,
5028 "cast increases required alignment of target type");
e9a25f70 5029
3176a0c2 5030 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5031 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5032 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5033 /* Unlike conversion of integers to pointers, where the
5034 warning is disabled for converting constants because
5035 of cases such as SIG_*, warn about converting constant
5036 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5037 sign extension, and a warning is appropriate. */
c2255bc4
AH
5038 warning_at (loc, OPT_Wpointer_to_int_cast,
5039 "cast from pointer to integer of different size");
400fbf9f 5040
3176a0c2 5041 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5042 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5043 warning_at (loc, OPT_Wbad_function_cast,
5044 "cast from function call of type %qT "
5045 "to non-matching type %qT", otype, type);
400fbf9f 5046
3176a0c2 5047 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5048 && TREE_CODE (otype) == INTEGER_TYPE
5049 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5050 /* Don't warn about converting any constant. */
5051 && !TREE_CONSTANT (value))
c2255bc4
AH
5052 warning_at (loc,
5053 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5054 "of different size");
400fbf9f 5055
79bedddc
SR
5056 if (warn_strict_aliasing <= 2)
5057 strict_aliasing_warning (otype, type, expr);
400fbf9f 5058
3897f229
JM
5059 /* If pedantic, warn for conversions between function and object
5060 pointer types, except for converting a null pointer constant
5061 to function pointer type. */
5062 if (pedantic
5063 && TREE_CODE (type) == POINTER_TYPE
5064 && TREE_CODE (otype) == POINTER_TYPE
5065 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5066 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5067 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5068 "conversion of function pointer to object pointer type");
3897f229
JM
5069
5070 if (pedantic
5071 && TREE_CODE (type) == POINTER_TYPE
5072 && TREE_CODE (otype) == POINTER_TYPE
5073 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5074 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5075 && !null_pointer_constant_p (value))
c1771a20 5076 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5077 "conversion of object pointer to function pointer type");
3897f229 5078
3e4093b6 5079 ovalue = value;
3e4093b6 5080 value = convert (type, value);
400fbf9f 5081
3e4093b6 5082 /* Ignore any integer overflow caused by the cast. */
928c19bb 5083 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5084 {
8bcd6380 5085 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5086 {
8bcd6380
RS
5087 if (!TREE_OVERFLOW (value))
5088 {
5089 /* Avoid clobbering a shared constant. */
5090 value = copy_node (value);
5091 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5092 }
6414bad6 5093 }
8bcd6380 5094 else if (TREE_OVERFLOW (value))
d8e1f97b
PB
5095 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5096 value = build_int_cst_wide (TREE_TYPE (value),
5097 TREE_INT_CST_LOW (value),
5098 TREE_INT_CST_HIGH (value));
3e4093b6
RS
5099 }
5100 }
400fbf9f 5101
53cd18ec
JM
5102 /* Don't let a cast be an lvalue. */
5103 if (value == expr)
db3927fb 5104 value = non_lvalue_loc (loc, value);
e9a25f70 5105
928c19bb
JM
5106 /* Don't allow the results of casting to floating-point or complex
5107 types be confused with actual constants, or casts involving
5108 integer and pointer types other than direct integer-to-integer
5109 and integer-to-pointer be confused with integer constant
5110 expressions and null pointer constants. */
5111 if (TREE_CODE (value) == REAL_CST
5112 || TREE_CODE (value) == COMPLEX_CST
5113 || (TREE_CODE (value) == INTEGER_CST
5114 && !((TREE_CODE (expr) == INTEGER_CST
5115 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5116 || TREE_CODE (expr) == REAL_CST
5117 || TREE_CODE (expr) == COMPLEX_CST)))
5118 value = build1 (NOP_EXPR, type, value);
5119
c2255bc4
AH
5120 if (CAN_HAVE_LOCATION_P (value))
5121 SET_EXPR_LOCATION (value, loc);
3e4093b6 5122 return value;
400fbf9f
JW
5123}
5124
c2255bc4
AH
5125/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5126 location of the open paren of the cast, or the position of the cast
5127 expr. */
3e4093b6 5128tree
c2255bc4 5129c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5130{
f8893e47 5131 tree type;
928c19bb
JM
5132 tree type_expr = NULL_TREE;
5133 bool type_expr_const = true;
5134 tree ret;
3e4093b6 5135 int saved_wsp = warn_strict_prototypes;
c5c76735 5136
3e4093b6
RS
5137 /* This avoids warnings about unprototyped casts on
5138 integers. E.g. "#define SIG_DFL (void(*)())0". */
5139 if (TREE_CODE (expr) == INTEGER_CST)
5140 warn_strict_prototypes = 0;
928c19bb 5141 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5142 warn_strict_prototypes = saved_wsp;
c5c76735 5143
c2255bc4 5144 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5145 if (type_expr)
5146 {
9f33203d
JM
5147 bool inner_expr_const = true;
5148 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5149 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5150 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5151 && inner_expr_const);
c2255bc4 5152 SET_EXPR_LOCATION (ret, loc);
928c19bb 5153 }
24b97832
ILT
5154
5155 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5156 SET_EXPR_LOCATION (ret, loc);
5157
9e5b2115
PB
5158 /* C++ does not permits types to be defined in a cast, but it
5159 allows references to incomplete types. */
5160 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5161 warning_at (loc, OPT_Wc___compat,
5162 "defining a type in a cast is invalid in C++");
5163
928c19bb 5164 return ret;
400fbf9f 5165}
3e4093b6
RS
5166\f
5167/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5168 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5169 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5170 MODIFYCODE is the code for a binary operator that we use
5171 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5172 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5173 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5174 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5175
c2255bc4
AH
5176 LOCATION is the location of the MODIFYCODE operator.
5177 RHS_LOC is the location of the RHS. */
2f6e4e97 5178
3e4093b6 5179tree
32e8bb8e 5180build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5181 enum tree_code modifycode,
c2255bc4 5182 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5183{
3e4093b6
RS
5184 tree result;
5185 tree newrhs;
8ce94e44 5186 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5187 tree lhstype = TREE_TYPE (lhs);
5188 tree olhstype = lhstype;
928c19bb 5189 bool npc;
267bac10 5190 bool is_atomic_op;
e9a25f70 5191
3e4093b6
RS
5192 /* Types that aren't fully specified cannot be used in assignments. */
5193 lhs = require_complete_type (lhs);
e9a25f70 5194
3e4093b6
RS
5195 /* Avoid duplicate error messages from operands that had errors. */
5196 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5197 return error_mark_node;
400fbf9f 5198
46a88c12 5199 /* For ObjC properties, defer this check. */
7bd11157 5200 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5201 return error_mark_node;
5202
267bac10
JM
5203 is_atomic_op = really_atomic_lvalue (lhs);
5204
8ce94e44
JM
5205 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5206 {
5207 rhs_semantic_type = TREE_TYPE (rhs);
5208 rhs = TREE_OPERAND (rhs, 0);
5209 }
5210
3e4093b6 5211 newrhs = rhs;
400fbf9f 5212
928c19bb
JM
5213 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5214 {
5215 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5216 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5217 rhs_origtype);
928c19bb
JM
5218 if (inner == error_mark_node)
5219 return error_mark_node;
5220 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5221 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5222 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5223 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5224 protected_set_expr_location (result, location);
5225 return result;
5226 }
5227
3e4093b6
RS
5228 /* If a binary op has been requested, combine the old LHS value with the RHS
5229 producing the value we should actually store into the LHS. */
5230
5231 if (modifycode != NOP_EXPR)
400fbf9f 5232 {
928c19bb 5233 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5234 lhs = stabilize_reference (lhs);
bbbbb16a 5235
267bac10
JM
5236 /* Construct the RHS for any non-atomic compound assignemnt. */
5237 if (!is_atomic_op)
5238 {
5239 newrhs = build_binary_op (location,
5240 modifycode, lhs, rhs, 1);
5241
5242 /* The original type of the right hand side is no longer
5243 meaningful. */
5244 rhs_origtype = NULL_TREE;
5245 }
400fbf9f 5246 }
400fbf9f 5247
668ea4b1
IS
5248 if (c_dialect_objc ())
5249 {
46a88c12
NP
5250 /* Check if we are modifying an Objective-C property reference;
5251 if so, we need to generate setter calls. */
5252 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1
IS
5253 if (result)
5254 return result;
46a88c12
NP
5255
5256 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5257 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5258 return error_mark_node;
5259 }
5260
9bf24266 5261 /* Give an error for storing in something that is 'const'. */
bbbd6700 5262
f37acdf9 5263 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5264 || ((TREE_CODE (lhstype) == RECORD_TYPE
5265 || TREE_CODE (lhstype) == UNION_TYPE)
5266 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289
DN
5267 {
5268 readonly_error (lhs, lv_assign);
5269 return error_mark_node;
5270 }
f37acdf9
JM
5271 else if (TREE_READONLY (lhs))
5272 readonly_warning (lhs, lv_assign);
bbbd6700 5273
3e4093b6
RS
5274 /* If storing into a structure or union member,
5275 it has probably been given type `int'.
5276 Compute the type that would go with
5277 the actual amount of storage the member occupies. */
bbbd6700 5278
3e4093b6
RS
5279 if (TREE_CODE (lhs) == COMPONENT_REF
5280 && (TREE_CODE (lhstype) == INTEGER_TYPE
5281 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5282 || TREE_CODE (lhstype) == REAL_TYPE
5283 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5284 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5285
3e4093b6
RS
5286 /* If storing in a field that is in actuality a short or narrower than one,
5287 we must store in the field in its actual type. */
5288
5289 if (lhstype != TREE_TYPE (lhs))
5290 {
5291 lhs = copy_node (lhs);
5292 TREE_TYPE (lhs) = lhstype;
400fbf9f 5293 }
400fbf9f 5294
32e8bb8e
ILT
5295 /* Issue -Wc++-compat warnings about an assignment to an enum type
5296 when LHS does not have its original type. This happens for,
5297 e.g., an enum bitfield in a struct. */
5298 if (warn_cxx_compat
5299 && lhs_origtype != NULL_TREE
5300 && lhs_origtype != lhstype
5301 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5302 {
5303 tree checktype = (rhs_origtype != NULL_TREE
5304 ? rhs_origtype
5305 : TREE_TYPE (rhs));
5306 if (checktype != error_mark_node
267bac10
JM
5307 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5308 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5309 warning_at (location, OPT_Wc___compat,
5310 "enum conversion in assignment is invalid in C++");
5311 }
5312
267bac10
JM
5313 /* If the lhs is atomic, remove that qualifier. */
5314 if (is_atomic_op)
5315 {
5316 lhstype = build_qualified_type (lhstype,
5317 (TYPE_QUALS (lhstype)
5318 & ~TYPE_QUAL_ATOMIC));
5319 olhstype = build_qualified_type (olhstype,
5320 (TYPE_QUALS (lhstype)
5321 & ~TYPE_QUAL_ATOMIC));
5322 }
5323
8ce94e44
JM
5324 /* Convert new value to destination type. Fold it first, then
5325 restore any excess precision information, for the sake of
5326 conversion warnings. */
400fbf9f 5327
267bac10
JM
5328 if (!(is_atomic_op && modifycode != NOP_EXPR))
5329 {
5330 npc = null_pointer_constant_p (newrhs);
5331 newrhs = c_fully_fold (newrhs, false, NULL);
5332 if (rhs_semantic_type)
5333 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5334 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
5335 ic_assign, npc, NULL_TREE,
5336 NULL_TREE, 0);
5337 if (TREE_CODE (newrhs) == ERROR_MARK)
5338 return error_mark_node;
5339 }
400fbf9f 5340
6e955430
ZL
5341 /* Emit ObjC write barrier, if necessary. */
5342 if (c_dialect_objc () && flag_objc_gc)
5343 {
5344 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5345 if (result)
c9f9eb5d
AH
5346 {
5347 protected_set_expr_location (result, location);
5348 return result;
5349 }
6e955430
ZL
5350 }
5351
ea4b7848 5352 /* Scan operands. */
400fbf9f 5353
267bac10
JM
5354 if (is_atomic_op)
5355 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5356 else
5357 {
5358 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5359 TREE_SIDE_EFFECTS (result) = 1;
5360 protected_set_expr_location (result, location);
5361 }
400fbf9f 5362
3e4093b6
RS
5363 /* If we got the LHS in a different type for storing in,
5364 convert the result back to the nominal type of LHS
5365 so that the value we return always has the same type
5366 as the LHS argument. */
e855c5ce 5367
3e4093b6
RS
5368 if (olhstype == TREE_TYPE (result))
5369 return result;
c9f9eb5d 5370
744aa42f
ILT
5371 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5372 ic_assign, false, NULL_TREE, NULL_TREE, 0);
c9f9eb5d
AH
5373 protected_set_expr_location (result, location);
5374 return result;
3e4093b6
RS
5375}
5376\f
478a1c5b
ILT
5377/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5378 This is used to implement -fplan9-extensions. */
5379
5380static bool
5381find_anonymous_field_with_type (tree struct_type, tree type)
5382{
5383 tree field;
5384 bool found;
5385
5386 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5387 || TREE_CODE (struct_type) == UNION_TYPE);
5388 found = false;
5389 for (field = TYPE_FIELDS (struct_type);
5390 field != NULL_TREE;
5391 field = TREE_CHAIN (field))
5392 {
267bac10
JM
5393 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5394 ? c_build_qualified_type (TREE_TYPE (field),
5395 TYPE_QUAL_ATOMIC)
5396 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5397 if (DECL_NAME (field) == NULL
267bac10 5398 && comptypes (type, fieldtype))
478a1c5b
ILT
5399 {
5400 if (found)
5401 return false;
5402 found = true;
5403 }
5404 else if (DECL_NAME (field) == NULL
5405 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5406 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5407 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5408 {
5409 if (found)
5410 return false;
5411 found = true;
5412 }
5413 }
5414 return found;
5415}
5416
5417/* RHS is an expression whose type is pointer to struct. If there is
5418 an anonymous field in RHS with type TYPE, then return a pointer to
5419 that field in RHS. This is used with -fplan9-extensions. This
5420 returns NULL if no conversion could be found. */
5421
5422static tree
5423convert_to_anonymous_field (location_t location, tree type, tree rhs)
5424{
5425 tree rhs_struct_type, lhs_main_type;
5426 tree field, found_field;
5427 bool found_sub_field;
5428 tree ret;
5429
5430 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5431 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5432 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5433 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5434
5435 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5436 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5437 ? c_build_qualified_type (TREE_TYPE (type),
5438 TYPE_QUAL_ATOMIC)
5439 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5440
5441 found_field = NULL_TREE;
5442 found_sub_field = false;
5443 for (field = TYPE_FIELDS (rhs_struct_type);
5444 field != NULL_TREE;
5445 field = TREE_CHAIN (field))
5446 {
5447 if (DECL_NAME (field) != NULL_TREE
5448 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5449 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5450 continue;
267bac10
JM
5451 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5452 ? c_build_qualified_type (TREE_TYPE (field),
5453 TYPE_QUAL_ATOMIC)
5454 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5455 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5456 {
5457 if (found_field != NULL_TREE)
5458 return NULL_TREE;
5459 found_field = field;
5460 }
5461 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5462 lhs_main_type))
5463 {
5464 if (found_field != NULL_TREE)
5465 return NULL_TREE;
5466 found_field = field;
5467 found_sub_field = true;
5468 }
5469 }
5470
5471 if (found_field == NULL_TREE)
5472 return NULL_TREE;
5473
5474 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5475 build_fold_indirect_ref (rhs), found_field,
5476 NULL_TREE);
5477 ret = build_fold_addr_expr_loc (location, ret);
5478
5479 if (found_sub_field)
5480 {
5481 ret = convert_to_anonymous_field (location, type, ret);
5482 gcc_assert (ret != NULL_TREE);
5483 }
5484
5485 return ret;
5486}
5487
bbbbb16a
ILT
5488/* Convert value RHS to type TYPE as preparation for an assignment to
5489 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5490 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5491 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5492 constant before any folding.
3e4093b6
RS
5493 The real work of conversion is done by `convert'.
5494 The purpose of this function is to generate error messages
5495 for assignments that are not allowed in C.
2ac2f164
JM
5496 ERRTYPE says whether it is argument passing, assignment,
5497 initialization or return.
2f6e4e97 5498
c2255bc4 5499 LOCATION is the location of the RHS.
2ac2f164 5500 FUNCTION is a tree for the function being called.
3e4093b6 5501 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5502
3e4093b6 5503static tree
744aa42f
ILT
5504convert_for_assignment (location_t location, tree type, tree rhs,
5505 tree origtype, enum impl_conv errtype,
5506 bool null_pointer_constant, tree fundecl,
5507 tree function, int parmnum)
3e4093b6
RS
5508{
5509 enum tree_code codel = TREE_CODE (type);
8ce94e44 5510 tree orig_rhs = rhs;
3e4093b6
RS
5511 tree rhstype;
5512 enum tree_code coder;
2ac2f164 5513 tree rname = NULL_TREE;
58393038 5514 bool objc_ok = false;
2ac2f164 5515
6b4ef5c1 5516 if (errtype == ic_argpass)
2ac2f164
JM
5517 {
5518 tree selector;
5519 /* Change pointer to function to the function itself for
5520 diagnostics. */
5521 if (TREE_CODE (function) == ADDR_EXPR
5522 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5523 function = TREE_OPERAND (function, 0);
5524
5525 /* Handle an ObjC selector specially for diagnostics. */
5526 selector = objc_message_selector ();
5527 rname = function;
5528 if (selector && parmnum > 2)
5529 {
5530 rname = selector;
5531 parmnum -= 2;
5532 }
5533 }
5534
5535 /* This macro is used to emit diagnostics to ensure that all format
5536 strings are complete sentences, visible to gettext and checked at
5537 compile time. */
c2255bc4 5538#define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5539 do { \
5540 switch (errtype) \
5541 { \
5542 case ic_argpass: \
5543 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
c2255bc4
AH
5544 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5545 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
1e053dfe
MLI
5546 "expected %qT but argument is of type %qT", \
5547 type, rhstype); \
5548 break; \
1e053dfe
MLI
5549 case ic_assign: \
5550 pedwarn (LOCATION, OPT, AS); \
5551 break; \
5552 case ic_init: \
6a8f4e12 5553 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5554 break; \
5555 case ic_return: \
c2255bc4 5556 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5557 break; \
5558 default: \
5559 gcc_unreachable (); \
5560 } \
2ac2f164 5561 } while (0)
cb3ca04e 5562
49706e39
MLI
5563 /* This macro is used to emit diagnostics to ensure that all format
5564 strings are complete sentences, visible to gettext and checked at
5565 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5566 extra parameter to enumerate qualifiers. */
5567
5568#define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5569 do { \
5570 switch (errtype) \
5571 { \
5572 case ic_argpass: \
5573 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5574 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5575 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5576 "expected %qT but argument is of type %qT", \
5577 type, rhstype); \
5578 break; \
5579 case ic_assign: \
5580 pedwarn (LOCATION, OPT, AS, QUALS); \
5581 break; \
5582 case ic_init: \
5583 pedwarn (LOCATION, OPT, IN, QUALS); \
5584 break; \
5585 case ic_return: \
5586 pedwarn (LOCATION, OPT, RE, QUALS); \
5587 break; \
5588 default: \
5589 gcc_unreachable (); \
5590 } \
5591 } while (0)
5592
8ce94e44
JM
5593 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5594 rhs = TREE_OPERAND (rhs, 0);
5595
3e4093b6
RS
5596 rhstype = TREE_TYPE (rhs);
5597 coder = TREE_CODE (rhstype);
5598
5599 if (coder == ERROR_MARK)
5600 return error_mark_node;
5601
58393038
ZL
5602 if (c_dialect_objc ())
5603 {
5604 int parmno;
5605
5606 switch (errtype)
5607 {
5608 case ic_return:
5609 parmno = 0;
5610 break;
5611
5612 case ic_assign:
5613 parmno = -1;
5614 break;
5615
5616 case ic_init:
5617 parmno = -2;
5618 break;
5619
5620 default:
5621 parmno = parmnum;
5622 break;
5623 }
5624
5625 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5626 }
5627
bbbbb16a
ILT
5628 if (warn_cxx_compat)
5629 {
5630 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5631 if (checktype != error_mark_node
5632 && TREE_CODE (type) == ENUMERAL_TYPE
5633 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5634 {
81f40b79
ILT
5635 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5636 G_("enum conversion when passing argument "
5637 "%d of %qE is invalid in C++"),
5638 G_("enum conversion in assignment is "
5639 "invalid in C++"),
5640 G_("enum conversion in initialization is "
5641 "invalid in C++"),
5642 G_("enum conversion in return is "
5643 "invalid in C++"));
bbbbb16a
ILT
5644 }
5645 }
5646
3e4093b6 5647 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5648 return rhs;
3e4093b6
RS
5649
5650 if (coder == VOID_TYPE)
400fbf9f 5651 {
6dcc04b0
JM
5652 /* Except for passing an argument to an unprototyped function,
5653 this is a constraint violation. When passing an argument to
5654 an unprototyped function, it is compile-time undefined;
5655 making it a constraint in that case was rejected in
5656 DR#252. */
c2255bc4 5657 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5658 return error_mark_node;
400fbf9f 5659 }
808d6eaa
JM
5660 rhs = require_complete_type (rhs);
5661 if (rhs == error_mark_node)
5662 return error_mark_node;
cd192ccc
MS
5663 /* A non-reference type can convert to a reference. This handles
5664 va_start, va_copy and possibly port built-ins. */
5665 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5666 {
3e4093b6 5667 if (!lvalue_p (rhs))
400fbf9f 5668 {
c2255bc4 5669 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5670 return error_mark_node;
400fbf9f 5671 }
3e4093b6
RS
5672 if (!c_mark_addressable (rhs))
5673 return error_mark_node;
5674 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5675 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5676
cd192ccc
MS
5677 rhs = convert_for_assignment (location, build_pointer_type (TREE_TYPE (type)),
5678 rhs, origtype, errtype, null_pointer_constant,
5679 fundecl, function, parmnum);
5680 if (rhs == error_mark_node)
5681 return error_mark_node;
3e4093b6
RS
5682
5683 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5684 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5685 return rhs;
400fbf9f 5686 }
3e4093b6 5687 /* Some types can interconvert without explicit casts. */
3274deff 5688 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5689 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5690 return convert (type, rhs);
5691 /* Arithmetic types all interconvert, and enum is treated like int. */
5692 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5693 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5694 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5695 || codel == BOOLEAN_TYPE)
5696 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5697 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5698 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5699 || coder == BOOLEAN_TYPE))
928c19bb
JM
5700 {
5701 tree ret;
5702 bool save = in_late_binary_op;
dfe776dd 5703 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb 5704 in_late_binary_op = true;
8ce94e44 5705 ret = convert_and_check (type, orig_rhs);
dfe776dd 5706 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb
JM
5707 in_late_binary_op = save;
5708 return ret;
5709 }
400fbf9f 5710
79077aea
JJ
5711 /* Aggregates in different TUs might need conversion. */
5712 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5713 && codel == coder
5714 && comptypes (type, rhstype))
5715 return convert_and_check (type, rhs);
5716
ebf0bf7f 5717 /* Conversion to a transparent union or record from its member types.
3e4093b6 5718 This applies only to function arguments. */
ebf0bf7f
JJ
5719 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5720 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5721 && errtype == ic_argpass)
400fbf9f 5722 {
0257e383 5723 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5724
910ad8de 5725 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5726 {
0257e383 5727 tree memb_type = TREE_TYPE (memb);
400fbf9f 5728
3e4093b6 5729 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5730 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5731 break;
e58cd767 5732
3e4093b6
RS
5733 if (TREE_CODE (memb_type) != POINTER_TYPE)
5734 continue;
2f6e4e97 5735
3e4093b6
RS
5736 if (coder == POINTER_TYPE)
5737 {
5738 tree ttl = TREE_TYPE (memb_type);
5739 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5740
3e4093b6
RS
5741 /* Any non-function converts to a [const][volatile] void *
5742 and vice versa; otherwise, targets must be the same.
5743 Meanwhile, the lhs target must have all the qualifiers of
5744 the rhs. */
267bac10
JM
5745 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5746 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5747 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5748 {
267bac10
JM
5749 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5750 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5751 /* If this type won't generate any warnings, use it. */
267bac10 5752 if (lquals == rquals
3e4093b6
RS
5753 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5754 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5755 ? ((lquals | rquals) == rquals)
5756 : ((lquals | rquals) == lquals)))
3e4093b6 5757 break;
400fbf9f 5758
3e4093b6 5759 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5760 if (!marginal_memb)
5761 marginal_memb = memb;
3e4093b6
RS
5762 }
5763 }
82bde854 5764
3e4093b6 5765 /* Can convert integer zero to any pointer type. */
928c19bb 5766 if (null_pointer_constant)
3e4093b6
RS
5767 {
5768 rhs = null_pointer_node;
5769 break;
5770 }
5771 }
400fbf9f 5772
0257e383 5773 if (memb || marginal_memb)
3e4093b6 5774 {
0257e383 5775 if (!memb)
3e4093b6
RS
5776 {
5777 /* We have only a marginally acceptable member type;
5778 it needs a warning. */
0257e383 5779 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5780 tree ttr = TREE_TYPE (rhstype);
714a0864 5781
3e4093b6
RS
5782 /* Const and volatile mean something different for function
5783 types, so the usual warnings are not appropriate. */
5784 if (TREE_CODE (ttr) == FUNCTION_TYPE
5785 && TREE_CODE (ttl) == FUNCTION_TYPE)
5786 {
5787 /* Because const and volatile on functions are
5788 restrictions that say the function will not do
5789 certain things, it is okay to use a const or volatile
5790 function where an ordinary one is wanted, but not
5791 vice-versa. */
36c5e70a
BE
5792 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5793 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 5794 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5795 G_("passing argument %d of %qE "
49706e39 5796 "makes %q#v qualified function "
2ac2f164 5797 "pointer from unqualified"),
49706e39 5798 G_("assignment makes %q#v qualified "
2ac2f164
JM
5799 "function pointer from "
5800 "unqualified"),
49706e39 5801 G_("initialization makes %q#v qualified "
2ac2f164
JM
5802 "function pointer from "
5803 "unqualified"),
49706e39
MLI
5804 G_("return makes %q#v qualified function "
5805 "pointer from unqualified"),
5806 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 5807 }
36c5e70a
BE
5808 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5809 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
49706e39 5810 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5811 G_("passing argument %d of %qE discards "
49706e39
MLI
5812 "%qv qualifier from pointer target type"),
5813 G_("assignment discards %qv qualifier "
2ac2f164 5814 "from pointer target type"),
49706e39 5815 G_("initialization discards %qv qualifier "
2ac2f164 5816 "from pointer target type"),
49706e39
MLI
5817 G_("return discards %qv qualifier from "
5818 "pointer target type"),
5819 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
5820
5821 memb = marginal_memb;
3e4093b6 5822 }
400fbf9f 5823
fcf73884 5824 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 5825 pedwarn (location, OPT_Wpedantic,
fcf73884 5826 "ISO C prohibits argument conversion to union type");
0e7c47fa 5827
db3927fb 5828 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 5829 return build_constructor_single (type, memb, rhs);
3e4093b6 5830 }
0e7c47fa
RK
5831 }
5832
3e4093b6
RS
5833 /* Conversions among pointers */
5834 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5835 && (coder == codel))
400fbf9f 5836 {
3e4093b6
RS
5837 tree ttl = TREE_TYPE (type);
5838 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
5839 tree mvl = ttl;
5840 tree mvr = ttr;
3e4093b6 5841 bool is_opaque_pointer;
264fa2db 5842 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
5843 addr_space_t asl;
5844 addr_space_t asr;
400fbf9f 5845
46df2823 5846 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
5847 mvl = (TYPE_ATOMIC (mvl)
5848 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5849 TYPE_QUAL_ATOMIC)
5850 : TYPE_MAIN_VARIANT (mvl));
46df2823 5851 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
5852 mvr = (TYPE_ATOMIC (mvr)
5853 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5854 TYPE_QUAL_ATOMIC)
5855 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 5856 /* Opaque pointers are treated like void pointers. */
f83c7f63 5857 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 5858
478a1c5b
ILT
5859 /* The Plan 9 compiler permits a pointer to a struct to be
5860 automatically converted into a pointer to an anonymous field
5861 within the struct. */
5862 if (flag_plan9_extensions
5863 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5864 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5865 && mvl != mvr)
5866 {
5867 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5868 if (new_rhs != NULL_TREE)
5869 {
5870 rhs = new_rhs;
5871 rhstype = TREE_TYPE (rhs);
5872 coder = TREE_CODE (rhstype);
5873 ttr = TREE_TYPE (rhstype);
5874 mvr = TYPE_MAIN_VARIANT (ttr);
5875 }
5876 }
5877
b7e20b53 5878 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
5879 for the purpose of reducing the number of false positives, we
5880 tolerate the special case of
b7e20b53 5881
c22cacf3 5882 int *p = NULL;
b7e20b53 5883
c22cacf3 5884 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 5885 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
c2255bc4
AH
5886 warning_at (location, OPT_Wc___compat,
5887 "request for implicit conversion "
5888 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 5889
36c5e70a
BE
5890 /* See if the pointers point to incompatible address spaces. */
5891 asl = TYPE_ADDR_SPACE (ttl);
5892 asr = TYPE_ADDR_SPACE (ttr);
5893 if (!null_pointer_constant_p (rhs)
5894 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5895 {
5896 switch (errtype)
5897 {
5898 case ic_argpass:
5899 error_at (location, "passing argument %d of %qE from pointer to "
5900 "non-enclosed address space", parmnum, rname);
5901 break;
5902 case ic_assign:
5903 error_at (location, "assignment from pointer to "
5904 "non-enclosed address space");
5905 break;
5906 case ic_init:
5907 error_at (location, "initialization from pointer to "
5908 "non-enclosed address space");
5909 break;
5910 case ic_return:
5911 error_at (location, "return from pointer to "
5912 "non-enclosed address space");
5913 break;
5914 default:
5915 gcc_unreachable ();
5916 }
5917 return error_mark_node;
5918 }
5919
7876a414
KG
5920 /* Check if the right-hand side has a format attribute but the
5921 left-hand side doesn't. */
90137d8f 5922 if (warn_suggest_attribute_format
104f8784 5923 && check_missing_format_attribute (type, rhstype))
c22cacf3 5924 {
104f8784
KG
5925 switch (errtype)
5926 {
5927 case ic_argpass:
90137d8f 5928 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5929 "argument %d of %qE might be "
5930 "a candidate for a format attribute",
5931 parmnum, rname);
104f8784
KG
5932 break;
5933 case ic_assign:
90137d8f 5934 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5935 "assignment left-hand side might be "
5936 "a candidate for a format attribute");
104f8784
KG
5937 break;
5938 case ic_init:
90137d8f 5939 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5940 "initialization left-hand side might be "
5941 "a candidate for a format attribute");
104f8784
KG
5942 break;
5943 case ic_return:
90137d8f 5944 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5945 "return type might be "
5946 "a candidate for a format attribute");
104f8784
KG
5947 break;
5948 default:
5949 gcc_unreachable ();
5950 }
7876a414 5951 }
c22cacf3 5952
3e4093b6
RS
5953 /* Any non-function converts to a [const][volatile] void *
5954 and vice versa; otherwise, targets must be the same.
5955 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
5956 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5957 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5958 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 5959 || is_opaque_pointer
f8a93a2e
JJ
5960 || ((c_common_unsigned_type (mvl)
5961 == c_common_unsigned_type (mvr))
267bac10
JM
5962 && (c_common_signed_type (mvl)
5963 == c_common_signed_type (mvr))
5964 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6
RS
5965 {
5966 if (pedantic
5967 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5968 ||
5969 (VOID_TYPE_P (ttr)
928c19bb 5970 && !null_pointer_constant
3e4093b6 5971 && TREE_CODE (ttl) == FUNCTION_TYPE)))
c1771a20 5972 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
509c9d60 5973 G_("ISO C forbids passing argument %d of "
2ac2f164
JM
5974 "%qE between function pointer "
5975 "and %<void *%>"),
4b794eaf 5976 G_("ISO C forbids assignment between "
2ac2f164 5977 "function pointer and %<void *%>"),
4b794eaf 5978 G_("ISO C forbids initialization between "
2ac2f164 5979 "function pointer and %<void *%>"),
4b794eaf 5980 G_("ISO C forbids return between function "
2ac2f164 5981 "pointer and %<void *%>"));
3e4093b6
RS
5982 /* Const and volatile mean something different for function types,
5983 so the usual warnings are not appropriate. */
5984 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5985 && TREE_CODE (ttl) != FUNCTION_TYPE)
5986 {
267bac10
JM
5987 /* Assignments between atomic and non-atomic objects are OK. */
5988 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
5989 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 5990 {
6c39e757
NP
5991 WARN_FOR_QUALIFIERS (location, 0,
5992 G_("passing argument %d of %qE discards "
5993 "%qv qualifier from pointer target type"),
5994 G_("assignment discards %qv qualifier "
5995 "from pointer target type"),
5996 G_("initialization discards %qv qualifier "
5997 "from pointer target type"),
5998 G_("return discards %qv qualifier from "
5999 "pointer target type"),
6000 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6001 }
3e4093b6
RS
6002 /* If this is not a case of ignoring a mismatch in signedness,
6003 no warning. */
6004 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6005 || target_cmp)
3e4093b6
RS
6006 ;
6007 /* If there is a mismatch, do warn. */
f2fd3821 6008 else if (warn_pointer_sign)
c2255bc4 6009 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
509c9d60 6010 G_("pointer targets in passing argument "
2ac2f164 6011 "%d of %qE differ in signedness"),
4b794eaf 6012 G_("pointer targets in assignment "
2ac2f164 6013 "differ in signedness"),
4b794eaf 6014 G_("pointer targets in initialization "
2ac2f164 6015 "differ in signedness"),
4b794eaf 6016 G_("pointer targets in return differ "
2ac2f164 6017 "in signedness"));
3e4093b6
RS
6018 }
6019 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6020 && TREE_CODE (ttr) == FUNCTION_TYPE)
6021 {
6022 /* Because const and volatile on functions are restrictions
6023 that say the function will not do certain things,
6024 it is okay to use a const or volatile function
6025 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6026 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6027 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 6028 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 6029 G_("passing argument %d of %qE makes "
49706e39 6030 "%q#v qualified function pointer "
2ac2f164 6031 "from unqualified"),
49706e39 6032 G_("assignment makes %q#v qualified function "
2ac2f164 6033 "pointer from unqualified"),
49706e39 6034 G_("initialization makes %q#v qualified "
2ac2f164 6035 "function pointer from unqualified"),
49706e39
MLI
6036 G_("return makes %q#v qualified function "
6037 "pointer from unqualified"),
6038 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6039 }
6040 }
6041 else
58393038
ZL
6042 /* Avoid warning about the volatile ObjC EH puts on decls. */
6043 if (!objc_ok)
c2255bc4 6044 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6045 G_("passing argument %d of %qE from "
58393038 6046 "incompatible pointer type"),
4b794eaf
JJ
6047 G_("assignment from incompatible pointer type"),
6048 G_("initialization from incompatible "
58393038 6049 "pointer type"),
4b794eaf 6050 G_("return from incompatible pointer type"));
58393038 6051
3e4093b6
RS
6052 return convert (type, rhs);
6053 }
b494fd98
EB
6054 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6055 {
6dcc04b0
JM
6056 /* ??? This should not be an error when inlining calls to
6057 unprototyped functions. */
c2255bc4 6058 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6059 return error_mark_node;
6060 }
3e4093b6 6061 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6062 {
3e4093b6
RS
6063 /* An explicit constant 0 can convert to a pointer,
6064 or one that results from arithmetic, even including
6065 a cast to integer type. */
928c19bb 6066 if (!null_pointer_constant)
c2255bc4 6067 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6068 G_("passing argument %d of %qE makes "
2ac2f164 6069 "pointer from integer without a cast"),
4b794eaf 6070 G_("assignment makes pointer from integer "
2ac2f164 6071 "without a cast"),
4b794eaf 6072 G_("initialization makes pointer from "
2ac2f164 6073 "integer without a cast"),
4b794eaf 6074 G_("return makes pointer from integer "
2ac2f164 6075 "without a cast"));
b3006337
EB
6076
6077 return convert (type, rhs);
400fbf9f 6078 }
3e4093b6 6079 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6080 {
c2255bc4 6081 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6082 G_("passing argument %d of %qE makes integer "
2ac2f164 6083 "from pointer without a cast"),
4b794eaf 6084 G_("assignment makes integer from pointer "
2ac2f164 6085 "without a cast"),
4b794eaf 6086 G_("initialization makes integer from pointer "
2ac2f164 6087 "without a cast"),
4b794eaf 6088 G_("return makes integer from pointer "
2ac2f164 6089 "without a cast"));
3e4093b6
RS
6090 return convert (type, rhs);
6091 }
6092 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6093 {
6094 tree ret;
6095 bool save = in_late_binary_op;
6096 in_late_binary_op = true;
6097 ret = convert (type, rhs);
6098 in_late_binary_op = save;
6099 return ret;
6100 }
400fbf9f 6101
2ac2f164 6102 switch (errtype)
3e4093b6 6103 {
2ac2f164 6104 case ic_argpass:
c2255bc4 6105 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
a7da8b42
MLI
6106 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6107 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
6108 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6109 break;
6110 case ic_assign:
c2255bc4
AH
6111 error_at (location, "incompatible types when assigning to type %qT from "
6112 "type %qT", type, rhstype);
2ac2f164
JM
6113 break;
6114 case ic_init:
c2255bc4
AH
6115 error_at (location,
6116 "incompatible types when initializing type %qT using type %qT",
6117 type, rhstype);
2ac2f164
JM
6118 break;
6119 case ic_return:
c2255bc4
AH
6120 error_at (location,
6121 "incompatible types when returning type %qT but %qT was "
6122 "expected", rhstype, type);
2ac2f164
JM
6123 break;
6124 default:
6125 gcc_unreachable ();
400fbf9f 6126 }
53b01f59 6127
3e4093b6
RS
6128 return error_mark_node;
6129}
3e4093b6
RS
6130\f
6131/* If VALUE is a compound expr all of whose expressions are constant, then
6132 return its value. Otherwise, return error_mark_node.
15b732b2 6133
3e4093b6
RS
6134 This is for handling COMPOUND_EXPRs as initializer elements
6135 which is allowed with a warning when -pedantic is specified. */
15b732b2 6136
3e4093b6
RS
6137static tree
6138valid_compound_expr_initializer (tree value, tree endtype)
6139{
6140 if (TREE_CODE (value) == COMPOUND_EXPR)
6141 {
6142 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6143 == error_mark_node)
6144 return error_mark_node;
6145 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6146 endtype);
6147 }
116df786 6148 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6149 return error_mark_node;
6150 else
6151 return value;
15b732b2 6152}
400fbf9f 6153\f
3e4093b6
RS
6154/* Perform appropriate conversions on the initial value of a variable,
6155 store it in the declaration DECL,
6156 and print any error messages that are appropriate.
bbbbb16a 6157 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6158 If the init is invalid, store an ERROR_MARK.
6159
6160 INIT_LOC is the location of the initial value. */
400fbf9f 6161
3e4093b6 6162void
c2255bc4 6163store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6164{
3e4093b6 6165 tree value, type;
928c19bb 6166 bool npc = false;
400fbf9f 6167
3e4093b6 6168 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6169
3e4093b6
RS
6170 type = TREE_TYPE (decl);
6171 if (TREE_CODE (type) == ERROR_MARK)
6172 return;
400fbf9f 6173
3e4093b6 6174 /* Digest the specified initializer into an expression. */
400fbf9f 6175
928c19bb
JM
6176 if (init)
6177 npc = null_pointer_constant_p (init);
c2255bc4
AH
6178 value = digest_init (init_loc, type, init, origtype, npc,
6179 true, TREE_STATIC (decl));
400fbf9f 6180
3e4093b6 6181 /* Store the expression if valid; else report error. */
400fbf9f 6182
3176a0c2 6183 if (!in_system_header
3f75a254 6184 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6185 warning (OPT_Wtraditional, "traditional C rejects automatic "
6186 "aggregate initialization");
2f6e4e97 6187
3e4093b6 6188 DECL_INITIAL (decl) = value;
400fbf9f 6189
3e4093b6
RS
6190 /* ANSI wants warnings about out-of-range constant initializers. */
6191 STRIP_TYPE_NOPS (value);
b8698a0f 6192 if (TREE_STATIC (decl))
c2658540 6193 constant_expression_warning (value);
400fbf9f 6194
3e4093b6
RS
6195 /* Check if we need to set array size from compound literal size. */
6196 if (TREE_CODE (type) == ARRAY_TYPE
6197 && TYPE_DOMAIN (type) == 0
6198 && value != error_mark_node)
400fbf9f 6199 {
3e4093b6
RS
6200 tree inside_init = init;
6201
ed248cf7 6202 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6203 inside_init = fold (inside_init);
6204
6205 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6206 {
8d9f82d5 6207 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6208
8d9f82d5 6209 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6210 {
6211 /* For int foo[] = (int [3]){1}; we need to set array size
6212 now since later on array initializer will be just the
6213 brace enclosed list of the compound literal. */
e30ecc5d 6214 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6215 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6216 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6217 layout_type (type);
8d9f82d5 6218 layout_decl (cldecl, 0);
e30ecc5d
JJ
6219 TREE_TYPE (decl)
6220 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6221 }
6222 }
400fbf9f 6223 }
3e4093b6
RS
6224}
6225\f
6226/* Methods for storing and printing names for error messages. */
400fbf9f 6227
3e4093b6
RS
6228/* Implement a spelling stack that allows components of a name to be pushed
6229 and popped. Each element on the stack is this structure. */
400fbf9f 6230
3e4093b6
RS
6231struct spelling
6232{
6233 int kind;
6234 union
400fbf9f 6235 {
a0f0ab9f 6236 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6237 const char *s;
6238 } u;
6239};
2f6e4e97 6240
3e4093b6
RS
6241#define SPELLING_STRING 1
6242#define SPELLING_MEMBER 2
6243#define SPELLING_BOUNDS 3
400fbf9f 6244
3e4093b6
RS
6245static struct spelling *spelling; /* Next stack element (unused). */
6246static struct spelling *spelling_base; /* Spelling stack base. */
6247static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6248
3e4093b6
RS
6249/* Macros to save and restore the spelling stack around push_... functions.
6250 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6251
3e4093b6
RS
6252#define SPELLING_DEPTH() (spelling - spelling_base)
6253#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6254
3e4093b6
RS
6255/* Push an element on the spelling stack with type KIND and assign VALUE
6256 to MEMBER. */
400fbf9f 6257
3e4093b6
RS
6258#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6259{ \
6260 int depth = SPELLING_DEPTH (); \
6261 \
6262 if (depth >= spelling_size) \
6263 { \
6264 spelling_size += 10; \
cca8ead2
BI
6265 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6266 spelling_size); \
3e4093b6
RS
6267 RESTORE_SPELLING_DEPTH (depth); \
6268 } \
6269 \
6270 spelling->kind = (KIND); \
6271 spelling->MEMBER = (VALUE); \
6272 spelling++; \
6273}
400fbf9f 6274
3e4093b6 6275/* Push STRING on the stack. Printed literally. */
400fbf9f 6276
3e4093b6
RS
6277static void
6278push_string (const char *string)
6279{
6280 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6281}
400fbf9f 6282
3e4093b6 6283/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6284
3e4093b6
RS
6285static void
6286push_member_name (tree decl)
6287{
6288 const char *const string
88388a52
JM
6289 = (DECL_NAME (decl)
6290 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6291 : _("<anonymous>"));
3e4093b6
RS
6292 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6293}
400fbf9f 6294
3e4093b6 6295/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6296
3e4093b6 6297static void
a0f0ab9f 6298push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6299{
6300 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6301}
bb58bec5 6302
3e4093b6 6303/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6304
3e4093b6
RS
6305static int
6306spelling_length (void)
6307{
6308 int size = 0;
6309 struct spelling *p;
400fbf9f 6310
3e4093b6
RS
6311 for (p = spelling_base; p < spelling; p++)
6312 {
6313 if (p->kind == SPELLING_BOUNDS)
6314 size += 25;
6315 else
6316 size += strlen (p->u.s) + 1;
6317 }
6318
6319 return size;
400fbf9f 6320}
400fbf9f 6321
3e4093b6 6322/* Print the spelling to BUFFER and return it. */
400fbf9f 6323
3e4093b6
RS
6324static char *
6325print_spelling (char *buffer)
400fbf9f 6326{
3e4093b6
RS
6327 char *d = buffer;
6328 struct spelling *p;
400fbf9f 6329
3e4093b6
RS
6330 for (p = spelling_base; p < spelling; p++)
6331 if (p->kind == SPELLING_BOUNDS)
6332 {
a0f0ab9f 6333 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6334 d += strlen (d);
6335 }
6336 else
6337 {
6338 const char *s;
6339 if (p->kind == SPELLING_MEMBER)
6340 *d++ = '.';
6341 for (s = p->u.s; (*d = *s++); d++)
6342 ;
6343 }
6344 *d++ = '\0';
6345 return buffer;
6346}
400fbf9f 6347
3e4093b6 6348/* Issue an error message for a bad initializer component.
6a8f4e12 6349 GMSGID identifies the message.
3e4093b6 6350 The component name is taken from the spelling stack. */
400fbf9f 6351
3e4093b6 6352void
6a8f4e12 6353error_init (const char *gmsgid)
3e4093b6
RS
6354{
6355 char *ofwhat;
400fbf9f 6356
6a8f4e12
AP
6357 /* The gmsgid may be a format string with %< and %>. */
6358 error (gmsgid);
28dab132 6359 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6360 if (*ofwhat)
bda67431 6361 error ("(near initialization for %qs)", ofwhat);
3e4093b6 6362}
400fbf9f 6363
fcf73884
MLI
6364/* Issue a pedantic warning for a bad initializer component. OPT is
6365 the option OPT_* (from options.h) controlling this warning or 0 if
6a8f4e12 6366 it is unconditionally given. GMSGID identifies the message. The
fcf73884 6367 component name is taken from the spelling stack. */
400fbf9f 6368
3e4093b6 6369void
6a8f4e12 6370pedwarn_init (location_t location, int opt, const char *gmsgid)
3e4093b6
RS
6371{
6372 char *ofwhat;
f90e8e2e 6373
6a8f4e12
AP
6374 /* The gmsgid may be a format string with %< and %>. */
6375 pedwarn (location, opt, gmsgid);
28dab132 6376 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6377 if (*ofwhat)
509c9d60 6378 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
3e4093b6 6379}
9f720c3e 6380
b8698a0f 6381/* Issue a warning for a bad initializer component.
683d6ff9
MLI
6382
6383 OPT is the OPT_W* value corresponding to the warning option that
6a8f4e12 6384 controls this warning. GMSGID identifies the message. The
683d6ff9 6385 component name is taken from the spelling stack. */
61179109 6386
3e4093b6 6387static void
6a8f4e12 6388warning_init (int opt, const char *gmsgid)
3e4093b6
RS
6389{
6390 char *ofwhat;
7e842ef8 6391
6a8f4e12
AP
6392 /* The gmsgid may be a format string with %< and %>. */
6393 warning (opt, gmsgid);
28dab132 6394 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6395 if (*ofwhat)
683d6ff9 6396 warning (opt, "(near initialization for %qs)", ofwhat);
3e4093b6
RS
6397}
6398\f
916c5919
JM
6399/* If TYPE is an array type and EXPR is a parenthesized string
6400 constant, warn if pedantic that EXPR is being used to initialize an
6401 object of type TYPE. */
6402
6403void
6404maybe_warn_string_init (tree type, struct c_expr expr)
6405{
6406 if (pedantic
6407 && TREE_CODE (type) == ARRAY_TYPE
6408 && TREE_CODE (expr.value) == STRING_CST
6409 && expr.original_code != STRING_CST)
c1771a20 6410 pedwarn_init (input_location, OPT_Wpedantic,
fcf73884 6411 "array initialized from parenthesized string constant");
916c5919
JM
6412}
6413
3e4093b6
RS
6414/* Digest the parser output INIT as an initializer for type TYPE.
6415 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6416
bbbbb16a
ILT
6417 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6418
928c19bb
JM
6419 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6420
916c5919
JM
6421 If INIT is a string constant, STRICT_STRING is true if it is
6422 unparenthesized or we should not warn here for it being parenthesized.
6423 For other types of INIT, STRICT_STRING is not used.
6424
c2255bc4
AH
6425 INIT_LOC is the location of the INIT.
6426
3e4093b6
RS
6427 REQUIRE_CONSTANT requests an error if non-constant initializers or
6428 elements are seen. */
7e842ef8 6429
3e4093b6 6430static tree
c2255bc4
AH
6431digest_init (location_t init_loc, tree type, tree init, tree origtype,
6432 bool null_pointer_constant, bool strict_string,
6433 int require_constant)
3e4093b6
RS
6434{
6435 enum tree_code code = TREE_CODE (type);
6436 tree inside_init = init;
8ce94e44 6437 tree semantic_type = NULL_TREE;
928c19bb 6438 bool maybe_const = true;
7e842ef8 6439
3e4093b6 6440 if (type == error_mark_node
f01da1a5 6441 || !init
3e4093b6
RS
6442 || init == error_mark_node
6443 || TREE_TYPE (init) == error_mark_node)
6444 return error_mark_node;
7e842ef8 6445
ed248cf7 6446 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6447
8ce94e44
JM
6448 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6449 {
6450 semantic_type = TREE_TYPE (inside_init);
6451 inside_init = TREE_OPERAND (inside_init, 0);
6452 }
928c19bb
JM
6453 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6454 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6455
3e4093b6
RS
6456 /* Initialization of an array of chars from a string constant
6457 optionally enclosed in braces. */
7e842ef8 6458
197463ae
JM
6459 if (code == ARRAY_TYPE && inside_init
6460 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6461 {
267bac10
JM
6462 tree typ1
6463 = (TYPE_ATOMIC (TREE_TYPE (type))
6464 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6465 TYPE_QUAL_ATOMIC)
6466 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6467 /* Note that an array could be both an array of character type
6468 and an array of wchar_t if wchar_t is signed char or unsigned
6469 char. */
6470 bool char_array = (typ1 == char_type_node
6471 || typ1 == signed_char_type_node
6472 || typ1 == unsigned_char_type_node);
6473 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6474 bool char16_array = !!comptypes (typ1, char16_type_node);
6475 bool char32_array = !!comptypes (typ1, char32_type_node);
6476
6477 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6478 {
916c5919 6479 struct c_expr expr;
c466b2cd 6480 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6481 expr.value = inside_init;
6482 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6483 expr.original_type = NULL;
916c5919
JM
6484 maybe_warn_string_init (type, expr);
6485
a45e580b 6486 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6487 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6488 "initialization of a flexible array member");
6489
3e4093b6 6490 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6491 TYPE_MAIN_VARIANT (type)))
3e4093b6 6492 return inside_init;
7e842ef8 6493
c466b2cd 6494 if (char_array)
3e4093b6 6495 {
c466b2cd
KVH
6496 if (typ2 != char_type_node)
6497 {
6498 error_init ("char-array initialized from wide string");
6499 return error_mark_node;
6500 }
3e4093b6 6501 }
c466b2cd 6502 else
3e4093b6 6503 {
c466b2cd
KVH
6504 if (typ2 == char_type_node)
6505 {
6506 error_init ("wide character array initialized from non-wide "
6507 "string");
6508 return error_mark_node;
6509 }
6510 else if (!comptypes(typ1, typ2))
6511 {
6512 error_init ("wide character array initialized from "
6513 "incompatible wide string");
6514 return error_mark_node;
6515 }
7e842ef8 6516 }
2f6e4e97 6517
3e4093b6
RS
6518 TREE_TYPE (inside_init) = type;
6519 if (TYPE_DOMAIN (type) != 0
6520 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6521 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6522 {
6523 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6524
c466b2cd 6525 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6526 because it's ok to ignore the terminating null char
6527 that is counted in the length of the constant. */
5eb4df45
ILT
6528 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6529 (len
6530 - (TYPE_PRECISION (typ1)
6531 / BITS_PER_UNIT))))
6532 pedwarn_init (init_loc, 0,
6533 ("initializer-string for array of chars "
6534 "is too long"));
6535 else if (warn_cxx_compat
6536 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6537 warning_at (init_loc, OPT_Wc___compat,
6538 ("initializer-string for array chars "
6539 "is too long for C++"));
6540 }
7e842ef8 6541
3e4093b6 6542 return inside_init;
7e842ef8 6543 }
197463ae
JM
6544 else if (INTEGRAL_TYPE_P (typ1))
6545 {
6546 error_init ("array of inappropriate type initialized "
6547 "from string constant");
6548 return error_mark_node;
6549 }
7e842ef8
PE
6550 }
6551
3e4093b6
RS
6552 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6553 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6554 below and handle as a constructor. */
e89be13b
JJ
6555 if (code == VECTOR_TYPE
6556 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6557 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6558 && TREE_CONSTANT (inside_init))
6559 {
6560 if (TREE_CODE (inside_init) == VECTOR_CST
6561 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6562 TYPE_MAIN_VARIANT (type)))
6563 return inside_init;
6564
6565 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6566 {
4038c495
GB
6567 unsigned HOST_WIDE_INT ix;
6568 tree value;
6569 bool constant_p = true;
e89be13b
JJ
6570
6571 /* Iterate through elements and check if all constructor
6572 elements are *_CSTs. */
4038c495
GB
6573 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6574 if (!CONSTANT_CLASS_P (value))
6575 {
6576 constant_p = false;
6577 break;
6578 }
e89be13b 6579
4038c495
GB
6580 if (constant_p)
6581 return build_vector_from_ctor (type,
6582 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6583 }
6584 }
6035d635 6585
ca085fd7
MLI
6586 if (warn_sequence_point)
6587 verify_sequence_points (inside_init);
6588
3e4093b6
RS
6589 /* Any type can be initialized
6590 from an expression of the same type, optionally with braces. */
400fbf9f 6591
3e4093b6
RS
6592 if (inside_init && TREE_TYPE (inside_init) != 0
6593 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6594 TYPE_MAIN_VARIANT (type))
3e4093b6 6595 || (code == ARRAY_TYPE
132da1a5 6596 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6597 || (code == VECTOR_TYPE
132da1a5 6598 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6599 || (code == POINTER_TYPE
3897f229 6600 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6601 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6602 TREE_TYPE (type)))))
3e4093b6
RS
6603 {
6604 if (code == POINTER_TYPE)
b494fd98 6605 {
b494fd98
EB
6606 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6607 {
f2a71bbc
JM
6608 if (TREE_CODE (inside_init) == STRING_CST
6609 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6610 inside_init = array_to_pointer_conversion
6611 (init_loc, inside_init);
f2a71bbc
JM
6612 else
6613 {
6614 error_init ("invalid use of non-lvalue array");
6615 return error_mark_node;
6616 }
b494fd98 6617 }
f2a71bbc 6618 }
b494fd98 6619
bae39a73
NS
6620 if (code == VECTOR_TYPE)
6621 /* Although the types are compatible, we may require a
6622 conversion. */
6623 inside_init = convert (type, inside_init);
3e4093b6 6624
ca58211b
PB
6625 if (require_constant
6626 && (code == VECTOR_TYPE || !flag_isoc99)
3e4093b6 6627 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6628 {
3e4093b6
RS
6629 /* As an extension, allow initializing objects with static storage
6630 duration with compound literals (which are then treated just as
ca58211b
PB
6631 the brace enclosed list they contain). Also allow this for
6632 vectors, as we can only assign them with compound literals. */
3e4093b6
RS
6633 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6634 inside_init = DECL_INITIAL (decl);
400fbf9f 6635 }
3e4093b6
RS
6636
6637 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6638 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6639 {
3e4093b6
RS
6640 error_init ("array initialized from non-constant array expression");
6641 return error_mark_node;
400fbf9f 6642 }
400fbf9f 6643
c1771a20 6644 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6645 -pedantic-errors is specified. In the later case, we always want
6646 an error. In the former case, we simply want a warning. */
6647 if (require_constant && pedantic
6648 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6649 {
6650 inside_init
6651 = valid_compound_expr_initializer (inside_init,
6652 TREE_TYPE (inside_init));
6653 if (inside_init == error_mark_node)
6654 error_init ("initializer element is not constant");
2f6e4e97 6655 else
c1771a20 6656 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6657 "initializer element is not constant");
3e4093b6
RS
6658 if (flag_pedantic_errors)
6659 inside_init = error_mark_node;
6660 }
6661 else if (require_constant
116df786
RH
6662 && !initializer_constant_valid_p (inside_init,
6663 TREE_TYPE (inside_init)))
3e4093b6
RS
6664 {
6665 error_init ("initializer element is not constant");
6666 inside_init = error_mark_node;
8b40563c 6667 }
928c19bb 6668 else if (require_constant && !maybe_const)
c2255bc4 6669 pedwarn_init (init_loc, 0,
928c19bb 6670 "initializer element is not a constant expression");
f735a153 6671
90137d8f 6672 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6673 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
c2255bc4
AH
6674 inside_init = convert_for_assignment (init_loc, type, inside_init,
6675 origtype,
bbbbb16a 6676 ic_init, null_pointer_constant,
928c19bb 6677 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6678 return inside_init;
6679 }
f735a153 6680
3e4093b6 6681 /* Handle scalar types, including conversions. */
400fbf9f 6682
ab22c1fa
CF
6683 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6684 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6685 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6686 {
f2a71bbc
JM
6687 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6688 && (TREE_CODE (init) == STRING_CST
6689 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6690 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6691 if (semantic_type)
6692 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6693 inside_init);
3e4093b6 6694 inside_init
c2255bc4
AH
6695 = convert_for_assignment (init_loc, type, inside_init, origtype,
6696 ic_init, null_pointer_constant,
3e4093b6 6697 NULL_TREE, NULL_TREE, 0);
2f6e4e97 6698
3274deff
JW
6699 /* Check to see if we have already given an error message. */
6700 if (inside_init == error_mark_node)
6701 ;
3f75a254 6702 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6703 {
3e4093b6
RS
6704 error_init ("initializer element is not constant");
6705 inside_init = error_mark_node;
400fbf9f 6706 }
3e4093b6 6707 else if (require_constant
116df786
RH
6708 && !initializer_constant_valid_p (inside_init,
6709 TREE_TYPE (inside_init)))
400fbf9f 6710 {
3e4093b6
RS
6711 error_init ("initializer element is not computable at load time");
6712 inside_init = error_mark_node;
400fbf9f 6713 }
928c19bb 6714 else if (require_constant && !maybe_const)
c2255bc4 6715 pedwarn_init (init_loc, 0,
928c19bb 6716 "initializer element is not a constant expression");
3e4093b6
RS
6717
6718 return inside_init;
400fbf9f 6719 }
d9fc6069 6720
3e4093b6 6721 /* Come here only for records and arrays. */
d9fc6069 6722
3e4093b6 6723 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6724 {
3e4093b6
RS
6725 error_init ("variable-sized object may not be initialized");
6726 return error_mark_node;
d9fc6069 6727 }
3e4093b6
RS
6728
6729 error_init ("invalid initializer");
6730 return error_mark_node;
d9fc6069 6731}
400fbf9f 6732\f
3e4093b6 6733/* Handle initializers that use braces. */
400fbf9f 6734
3e4093b6
RS
6735/* Type of object we are accumulating a constructor for.
6736 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6737static tree constructor_type;
400fbf9f 6738
3e4093b6
RS
6739/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6740 left to fill. */
6741static tree constructor_fields;
400fbf9f 6742
3e4093b6
RS
6743/* For an ARRAY_TYPE, this is the specified index
6744 at which to store the next element we get. */
6745static tree constructor_index;
400fbf9f 6746
3e4093b6
RS
6747/* For an ARRAY_TYPE, this is the maximum index. */
6748static tree constructor_max_index;
400fbf9f 6749
3e4093b6
RS
6750/* For a RECORD_TYPE, this is the first field not yet written out. */
6751static tree constructor_unfilled_fields;
400fbf9f 6752
3e4093b6
RS
6753/* For an ARRAY_TYPE, this is the index of the first element
6754 not yet written out. */
6755static tree constructor_unfilled_index;
895ea614 6756
3e4093b6
RS
6757/* In a RECORD_TYPE, the byte index of the next consecutive field.
6758 This is so we can generate gaps between fields, when appropriate. */
6759static tree constructor_bit_index;
10d5caec 6760
3e4093b6
RS
6761/* If we are saving up the elements rather than allocating them,
6762 this is the list of elements so far (in reverse order,
6763 most recent first). */
9771b263 6764static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6765
3e4093b6
RS
6766/* 1 if constructor should be incrementally stored into a constructor chain,
6767 0 if all the elements should be kept in AVL tree. */
6768static int constructor_incremental;
ad47f1e5 6769
3e4093b6
RS
6770/* 1 if so far this constructor's elements are all compile-time constants. */
6771static int constructor_constant;
ad47f1e5 6772
3e4093b6
RS
6773/* 1 if so far this constructor's elements are all valid address constants. */
6774static int constructor_simple;
ad47f1e5 6775
928c19bb
JM
6776/* 1 if this constructor has an element that cannot be part of a
6777 constant expression. */
6778static int constructor_nonconst;
6779
3e4093b6
RS
6780/* 1 if this constructor is erroneous so far. */
6781static int constructor_erroneous;
d45cf215 6782
3e4093b6
RS
6783/* Structure for managing pending initializer elements, organized as an
6784 AVL tree. */
d45cf215 6785
3e4093b6 6786struct init_node
d45cf215 6787{
3e4093b6
RS
6788 struct init_node *left, *right;
6789 struct init_node *parent;
6790 int balance;
6791 tree purpose;
6792 tree value;
bbbbb16a 6793 tree origtype;
d45cf215
RS
6794};
6795
3e4093b6
RS
6796/* Tree of pending elements at this constructor level.
6797 These are elements encountered out of order
6798 which belong at places we haven't reached yet in actually
6799 writing the output.
6800 Will never hold tree nodes across GC runs. */
6801static struct init_node *constructor_pending_elts;
d45cf215 6802
3e4093b6
RS
6803/* The SPELLING_DEPTH of this constructor. */
6804static int constructor_depth;
d45cf215 6805
3e4093b6
RS
6806/* DECL node for which an initializer is being read.
6807 0 means we are reading a constructor expression
6808 such as (struct foo) {...}. */
6809static tree constructor_decl;
d45cf215 6810
3e4093b6
RS
6811/* Nonzero if this is an initializer for a top-level decl. */
6812static int constructor_top_level;
d45cf215 6813
3e4093b6
RS
6814/* Nonzero if there were any member designators in this initializer. */
6815static int constructor_designated;
d45cf215 6816
3e4093b6
RS
6817/* Nesting depth of designator list. */
6818static int designator_depth;
d45cf215 6819
3e4093b6 6820/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 6821static int designator_erroneous;
d45cf215 6822
3e4093b6
RS
6823\f
6824/* This stack has a level for each implicit or explicit level of
6825 structuring in the initializer, including the outermost one. It
6826 saves the values of most of the variables above. */
d45cf215 6827
3e4093b6
RS
6828struct constructor_range_stack;
6829
6830struct constructor_stack
d45cf215 6831{
3e4093b6
RS
6832 struct constructor_stack *next;
6833 tree type;
6834 tree fields;
6835 tree index;
6836 tree max_index;
6837 tree unfilled_index;
6838 tree unfilled_fields;
6839 tree bit_index;
9771b263 6840 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6841 struct init_node *pending_elts;
6842 int offset;
6843 int depth;
916c5919 6844 /* If value nonzero, this value should replace the entire
3e4093b6 6845 constructor at this level. */
916c5919 6846 struct c_expr replacement_value;
3e4093b6
RS
6847 struct constructor_range_stack *range_stack;
6848 char constant;
6849 char simple;
928c19bb 6850 char nonconst;
3e4093b6
RS
6851 char implicit;
6852 char erroneous;
6853 char outer;
6854 char incremental;
6855 char designated;
6856};
d45cf215 6857
802415d1 6858static struct constructor_stack *constructor_stack;
d45cf215 6859
3e4093b6
RS
6860/* This stack represents designators from some range designator up to
6861 the last designator in the list. */
d45cf215 6862
3e4093b6
RS
6863struct constructor_range_stack
6864{
6865 struct constructor_range_stack *next, *prev;
6866 struct constructor_stack *stack;
6867 tree range_start;
6868 tree index;
6869 tree range_end;
6870 tree fields;
6871};
d45cf215 6872
802415d1 6873static struct constructor_range_stack *constructor_range_stack;
d45cf215 6874
3e4093b6
RS
6875/* This stack records separate initializers that are nested.
6876 Nested initializers can't happen in ANSI C, but GNU C allows them
6877 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 6878
3e4093b6 6879struct initializer_stack
d45cf215 6880{
3e4093b6
RS
6881 struct initializer_stack *next;
6882 tree decl;
3e4093b6
RS
6883 struct constructor_stack *constructor_stack;
6884 struct constructor_range_stack *constructor_range_stack;
9771b263 6885 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6886 struct spelling *spelling;
6887 struct spelling *spelling_base;
6888 int spelling_size;
6889 char top_level;
6890 char require_constant_value;
6891 char require_constant_elements;
6892};
d45cf215 6893
802415d1 6894static struct initializer_stack *initializer_stack;
3e4093b6
RS
6895\f
6896/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
6897
6898void
a396f8ae 6899start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 6900{
3e4093b6 6901 const char *locus;
cceb1885 6902 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 6903
3e4093b6 6904 p->decl = constructor_decl;
3e4093b6
RS
6905 p->require_constant_value = require_constant_value;
6906 p->require_constant_elements = require_constant_elements;
6907 p->constructor_stack = constructor_stack;
6908 p->constructor_range_stack = constructor_range_stack;
6909 p->elements = constructor_elements;
6910 p->spelling = spelling;
6911 p->spelling_base = spelling_base;
6912 p->spelling_size = spelling_size;
6913 p->top_level = constructor_top_level;
6914 p->next = initializer_stack;
6915 initializer_stack = p;
400fbf9f 6916
3e4093b6 6917 constructor_decl = decl;
3e4093b6
RS
6918 constructor_designated = 0;
6919 constructor_top_level = top_level;
400fbf9f 6920
6f17bbcf 6921 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
6922 {
6923 require_constant_value = TREE_STATIC (decl);
6924 require_constant_elements
6925 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6926 /* For a scalar, you can always use any value to initialize,
6927 even within braces. */
6928 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6929 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6930 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6931 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 6932 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
6933 }
6934 else
6935 {
6936 require_constant_value = 0;
6937 require_constant_elements = 0;
88388a52 6938 locus = _("(anonymous)");
3e4093b6 6939 }
b71c7f8a 6940
3e4093b6
RS
6941 constructor_stack = 0;
6942 constructor_range_stack = 0;
b71c7f8a 6943
3e4093b6
RS
6944 missing_braces_mentioned = 0;
6945
6946 spelling_base = 0;
6947 spelling_size = 0;
6948 RESTORE_SPELLING_DEPTH (0);
6949
6950 if (locus)
6951 push_string (locus);
6952}
6953
6954void
6955finish_init (void)
b71c7f8a 6956{
3e4093b6 6957 struct initializer_stack *p = initializer_stack;
b71c7f8a 6958
3e4093b6
RS
6959 /* Free the whole constructor stack of this initializer. */
6960 while (constructor_stack)
6961 {
6962 struct constructor_stack *q = constructor_stack;
6963 constructor_stack = q->next;
6964 free (q);
6965 }
6966
366de0ce 6967 gcc_assert (!constructor_range_stack);
3e4093b6
RS
6968
6969 /* Pop back to the data of the outer initializer (if any). */
36579663 6970 free (spelling_base);
3aeb3655 6971
3e4093b6 6972 constructor_decl = p->decl;
3e4093b6
RS
6973 require_constant_value = p->require_constant_value;
6974 require_constant_elements = p->require_constant_elements;
6975 constructor_stack = p->constructor_stack;
6976 constructor_range_stack = p->constructor_range_stack;
6977 constructor_elements = p->elements;
6978 spelling = p->spelling;
6979 spelling_base = p->spelling_base;
6980 spelling_size = p->spelling_size;
6981 constructor_top_level = p->top_level;
6982 initializer_stack = p->next;
6983 free (p);
b71c7f8a 6984}
400fbf9f 6985\f
3e4093b6
RS
6986/* Call here when we see the initializer is surrounded by braces.
6987 This is instead of a call to push_init_level;
6988 it is matched by a call to pop_init_level.
400fbf9f 6989
3e4093b6
RS
6990 TYPE is the type to initialize, for a constructor expression.
6991 For an initializer for a decl, TYPE is zero. */
400fbf9f 6992
3e4093b6
RS
6993void
6994really_start_incremental_init (tree type)
400fbf9f 6995{
5d038c4c 6996 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 6997
3e4093b6
RS
6998 if (type == 0)
6999 type = TREE_TYPE (constructor_decl);
400fbf9f 7000
b6fc2cdb
PB
7001 if (TREE_CODE (type) == VECTOR_TYPE
7002 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7003 error ("opaque vector types cannot be initialized");
400fbf9f 7004
3e4093b6
RS
7005 p->type = constructor_type;
7006 p->fields = constructor_fields;
7007 p->index = constructor_index;
7008 p->max_index = constructor_max_index;
7009 p->unfilled_index = constructor_unfilled_index;
7010 p->unfilled_fields = constructor_unfilled_fields;
7011 p->bit_index = constructor_bit_index;
7012 p->elements = constructor_elements;
7013 p->constant = constructor_constant;
7014 p->simple = constructor_simple;
928c19bb 7015 p->nonconst = constructor_nonconst;
3e4093b6
RS
7016 p->erroneous = constructor_erroneous;
7017 p->pending_elts = constructor_pending_elts;
7018 p->depth = constructor_depth;
916c5919
JM
7019 p->replacement_value.value = 0;
7020 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7021 p->replacement_value.original_type = NULL;
3e4093b6
RS
7022 p->implicit = 0;
7023 p->range_stack = 0;
7024 p->outer = 0;
7025 p->incremental = constructor_incremental;
7026 p->designated = constructor_designated;
7027 p->next = 0;
7028 constructor_stack = p;
b13aca19 7029
3e4093b6
RS
7030 constructor_constant = 1;
7031 constructor_simple = 1;
928c19bb 7032 constructor_nonconst = 0;
3e4093b6 7033 constructor_depth = SPELLING_DEPTH ();
9771b263 7034 constructor_elements = NULL;
3e4093b6
RS
7035 constructor_pending_elts = 0;
7036 constructor_type = type;
7037 constructor_incremental = 1;
7038 constructor_designated = 0;
7039 designator_depth = 0;
b06df647 7040 designator_erroneous = 0;
400fbf9f 7041
3e4093b6
RS
7042 if (TREE_CODE (constructor_type) == RECORD_TYPE
7043 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7044 {
3e4093b6
RS
7045 constructor_fields = TYPE_FIELDS (constructor_type);
7046 /* Skip any nameless bit fields at the beginning. */
7047 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7048 && DECL_NAME (constructor_fields) == 0)
910ad8de 7049 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7050
3e4093b6
RS
7051 constructor_unfilled_fields = constructor_fields;
7052 constructor_bit_index = bitsize_zero_node;
400fbf9f 7053 }
3e4093b6
RS
7054 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7055 {
7056 if (TYPE_DOMAIN (constructor_type))
7057 {
7058 constructor_max_index
7059 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7060
3e4093b6
RS
7061 /* Detect non-empty initializations of zero-length arrays. */
7062 if (constructor_max_index == NULL_TREE
7063 && TYPE_SIZE (constructor_type))
9a9d280e 7064 constructor_max_index = integer_minus_one_node;
400fbf9f 7065
3e4093b6
RS
7066 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7067 to initialize VLAs will cause a proper error; avoid tree
7068 checking errors as well by setting a safe value. */
7069 if (constructor_max_index
7070 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7071 constructor_max_index = integer_minus_one_node;
59c83dbf 7072
3e4093b6
RS
7073 constructor_index
7074 = convert (bitsizetype,
7075 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7076 }
3e4093b6 7077 else
493179da
JM
7078 {
7079 constructor_index = bitsize_zero_node;
7080 constructor_max_index = NULL_TREE;
7081 }
59c83dbf 7082
3e4093b6
RS
7083 constructor_unfilled_index = constructor_index;
7084 }
7085 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7086 {
7087 /* Vectors are like simple fixed-size arrays. */
7088 constructor_max_index =
c62c040f 7089 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7090 constructor_index = bitsize_zero_node;
3e4093b6
RS
7091 constructor_unfilled_index = constructor_index;
7092 }
7093 else
7094 {
7095 /* Handle the case of int x = {5}; */
7096 constructor_fields = constructor_type;
7097 constructor_unfilled_fields = constructor_type;
7098 }
7099}
7100\f
7101/* Push down into a subobject, for initialization.
7102 If this is for an explicit set of braces, IMPLICIT is 0.
7103 If it is because the next element belongs at a lower level,
7104 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7105
3e4093b6 7106void
a1e3b3d9 7107push_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
7108{
7109 struct constructor_stack *p;
7110 tree value = NULL_TREE;
400fbf9f 7111
3e4093b6 7112 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7113 pop them now. If implicit == 1, this will have been done in
7114 process_init_element; do not repeat it here because in the case
7115 of excess initializers for an empty aggregate this leads to an
7116 infinite cycle of popping a level and immediately recreating
7117 it. */
7118 if (implicit != 1)
3e4093b6 7119 {
472d98b4
JM
7120 while (constructor_stack->implicit)
7121 {
7122 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7123 || TREE_CODE (constructor_type) == UNION_TYPE)
7124 && constructor_fields == 0)
a1e3b3d9
LB
7125 process_init_element (pop_init_level (1, braced_init_obstack),
7126 true, braced_init_obstack);
472d98b4
JM
7127 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7128 && constructor_max_index
7129 && tree_int_cst_lt (constructor_max_index,
7130 constructor_index))
a1e3b3d9
LB
7131 process_init_element (pop_init_level (1, braced_init_obstack),
7132 true, braced_init_obstack);
472d98b4
JM
7133 else
7134 break;
7135 }
3e4093b6 7136 }
400fbf9f 7137
3e4093b6
RS
7138 /* Unless this is an explicit brace, we need to preserve previous
7139 content if any. */
7140 if (implicit)
7141 {
7142 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7143 || TREE_CODE (constructor_type) == UNION_TYPE)
7144 && constructor_fields)
a1e3b3d9 7145 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7146 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7147 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7148 }
7149
5d038c4c 7150 p = XNEW (struct constructor_stack);
3e4093b6
RS
7151 p->type = constructor_type;
7152 p->fields = constructor_fields;
7153 p->index = constructor_index;
7154 p->max_index = constructor_max_index;
7155 p->unfilled_index = constructor_unfilled_index;
7156 p->unfilled_fields = constructor_unfilled_fields;
7157 p->bit_index = constructor_bit_index;
7158 p->elements = constructor_elements;
7159 p->constant = constructor_constant;
7160 p->simple = constructor_simple;
928c19bb 7161 p->nonconst = constructor_nonconst;
3e4093b6
RS
7162 p->erroneous = constructor_erroneous;
7163 p->pending_elts = constructor_pending_elts;
7164 p->depth = constructor_depth;
916c5919
JM
7165 p->replacement_value.value = 0;
7166 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7167 p->replacement_value.original_type = NULL;
3e4093b6
RS
7168 p->implicit = implicit;
7169 p->outer = 0;
7170 p->incremental = constructor_incremental;
7171 p->designated = constructor_designated;
7172 p->next = constructor_stack;
7173 p->range_stack = 0;
7174 constructor_stack = p;
400fbf9f 7175
3e4093b6
RS
7176 constructor_constant = 1;
7177 constructor_simple = 1;
928c19bb 7178 constructor_nonconst = 0;
3e4093b6 7179 constructor_depth = SPELLING_DEPTH ();
9771b263 7180 constructor_elements = NULL;
3e4093b6
RS
7181 constructor_incremental = 1;
7182 constructor_designated = 0;
7183 constructor_pending_elts = 0;
7184 if (!implicit)
400fbf9f 7185 {
3e4093b6
RS
7186 p->range_stack = constructor_range_stack;
7187 constructor_range_stack = 0;
7188 designator_depth = 0;
b06df647 7189 designator_erroneous = 0;
3e4093b6 7190 }
400fbf9f 7191
3e4093b6
RS
7192 /* Don't die if an entire brace-pair level is superfluous
7193 in the containing level. */
7194 if (constructor_type == 0)
7195 ;
7196 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7197 || TREE_CODE (constructor_type) == UNION_TYPE)
7198 {
7199 /* Don't die if there are extra init elts at the end. */
7200 if (constructor_fields == 0)
7201 constructor_type = 0;
7202 else
400fbf9f 7203 {
3e4093b6
RS
7204 constructor_type = TREE_TYPE (constructor_fields);
7205 push_member_name (constructor_fields);
7206 constructor_depth++;
400fbf9f 7207 }
3e4093b6
RS
7208 }
7209 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7210 {
7211 constructor_type = TREE_TYPE (constructor_type);
a0f0ab9f 7212 push_array_bounds (tree_low_cst (constructor_index, 1));
3e4093b6 7213 constructor_depth++;
400fbf9f
JW
7214 }
7215
3e4093b6 7216 if (constructor_type == 0)
400fbf9f 7217 {
3e4093b6
RS
7218 error_init ("extra brace group at end of initializer");
7219 constructor_fields = 0;
7220 constructor_unfilled_fields = 0;
7221 return;
400fbf9f
JW
7222 }
7223
3e4093b6
RS
7224 if (value && TREE_CODE (value) == CONSTRUCTOR)
7225 {
7226 constructor_constant = TREE_CONSTANT (value);
7227 constructor_simple = TREE_STATIC (value);
928c19bb 7228 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7229 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7230 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7231 && (TREE_CODE (constructor_type) == RECORD_TYPE
7232 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7233 set_nonincremental_init (braced_init_obstack);
3e4093b6 7234 }
400fbf9f 7235
3e4093b6
RS
7236 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
7237 {
7238 missing_braces_mentioned = 1;
683d6ff9 7239 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
3e4093b6 7240 }
400fbf9f 7241
3e4093b6
RS
7242 if (TREE_CODE (constructor_type) == RECORD_TYPE
7243 || TREE_CODE (constructor_type) == UNION_TYPE)
7244 {
7245 constructor_fields = TYPE_FIELDS (constructor_type);
7246 /* Skip any nameless bit fields at the beginning. */
7247 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7248 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7249 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7250
3e4093b6
RS
7251 constructor_unfilled_fields = constructor_fields;
7252 constructor_bit_index = bitsize_zero_node;
7253 }
7254 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7255 {
7256 /* Vectors are like simple fixed-size arrays. */
7257 constructor_max_index =
c62c040f
RG
7258 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7259 constructor_index = bitsize_int (0);
3e4093b6
RS
7260 constructor_unfilled_index = constructor_index;
7261 }
7262 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7263 {
7264 if (TYPE_DOMAIN (constructor_type))
7265 {
7266 constructor_max_index
7267 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7268
3e4093b6
RS
7269 /* Detect non-empty initializations of zero-length arrays. */
7270 if (constructor_max_index == NULL_TREE
7271 && TYPE_SIZE (constructor_type))
9a9d280e 7272 constructor_max_index = integer_minus_one_node;
de520661 7273
3e4093b6
RS
7274 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7275 to initialize VLAs will cause a proper error; avoid tree
7276 checking errors as well by setting a safe value. */
7277 if (constructor_max_index
7278 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7279 constructor_max_index = integer_minus_one_node;
b62acd60 7280
3e4093b6
RS
7281 constructor_index
7282 = convert (bitsizetype,
7283 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7284 }
7285 else
7286 constructor_index = bitsize_zero_node;
de520661 7287
3e4093b6
RS
7288 constructor_unfilled_index = constructor_index;
7289 if (value && TREE_CODE (value) == STRING_CST)
7290 {
7291 /* We need to split the char/wchar array into individual
7292 characters, so that we don't have to special case it
7293 everywhere. */
a1e3b3d9 7294 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7295 }
7296 }
7297 else
7298 {
b4519d39 7299 if (constructor_type != error_mark_node)
683d6ff9 7300 warning_init (0, "braces around scalar initializer");
3e4093b6
RS
7301 constructor_fields = constructor_type;
7302 constructor_unfilled_fields = constructor_type;
7303 }
7304}
8b6a5902 7305
3e4093b6 7306/* At the end of an implicit or explicit brace level,
916c5919
JM
7307 finish up that level of constructor. If a single expression
7308 with redundant braces initialized that level, return the
7309 c_expr structure for that expression. Otherwise, the original_code
7310 element is set to ERROR_MARK.
7311 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7312 from inner levels (process_init_element ignores that),
916c5919 7313 but return error_mark_node as the value from the outermost level
3e4093b6 7314 (that's what we want to put in DECL_INITIAL).
916c5919 7315 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7316
916c5919 7317struct c_expr
a1e3b3d9 7318pop_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
7319{
7320 struct constructor_stack *p;
916c5919
JM
7321 struct c_expr ret;
7322 ret.value = 0;
7323 ret.original_code = ERROR_MARK;
6866c6e8 7324 ret.original_type = NULL;
de520661 7325
3e4093b6
RS
7326 if (implicit == 0)
7327 {
7328 /* When we come to an explicit close brace,
7329 pop any inner levels that didn't have explicit braces. */
7330 while (constructor_stack->implicit)
a1e3b3d9
LB
7331 {
7332 process_init_element (pop_init_level (1, braced_init_obstack),
7333 true, braced_init_obstack);
7334 }
366de0ce 7335 gcc_assert (!constructor_range_stack);
3e4093b6 7336 }
e5e809f4 7337
0066ef9c
RH
7338 /* Now output all pending elements. */
7339 constructor_incremental = 1;
a1e3b3d9 7340 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7341
3e4093b6 7342 p = constructor_stack;
e5e809f4 7343
3e4093b6
RS
7344 /* Error for initializing a flexible array member, or a zero-length
7345 array member in an inappropriate context. */
7346 if (constructor_type && constructor_fields
7347 && TREE_CODE (constructor_type) == ARRAY_TYPE
7348 && TYPE_DOMAIN (constructor_type)
3f75a254 7349 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7350 {
7351 /* Silently discard empty initializations. The parser will
7352 already have pedwarned for empty brackets. */
7353 if (integer_zerop (constructor_unfilled_index))
7354 constructor_type = NULL_TREE;
366de0ce 7355 else
3e4093b6 7356 {
366de0ce 7357 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7358
3e4093b6
RS
7359 if (constructor_depth > 2)
7360 error_init ("initialization of flexible array member in a nested context");
fcf73884 7361 else
c1771a20 7362 pedwarn_init (input_location, OPT_Wpedantic,
509c9d60 7363 "initialization of a flexible array member");
de520661 7364
3e4093b6
RS
7365 /* We have already issued an error message for the existence
7366 of a flexible array member not at the end of the structure.
535a42b1 7367 Discard the initializer so that we do not die later. */
910ad8de 7368 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7369 constructor_type = NULL_TREE;
7370 }
3e4093b6 7371 }
de520661 7372
3e4093b6 7373 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7374 if (warn_missing_field_initializers
3e4093b6
RS
7375 && constructor_type
7376 && TREE_CODE (constructor_type) == RECORD_TYPE
7377 && constructor_unfilled_fields)
7378 {
49819fef 7379 bool constructor_zeroinit =
9771b263
DN
7380 (vec_safe_length (constructor_elements) == 1
7381 && integer_zerop ((*constructor_elements)[0].value));
49819fef 7382
3e4093b6
RS
7383 /* Do not warn for flexible array members or zero-length arrays. */
7384 while (constructor_unfilled_fields
3f75a254 7385 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7386 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7387 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7388
49819fef
AM
7389 if (constructor_unfilled_fields
7390 /* Do not warn if this level of the initializer uses member
7391 designators; it is likely to be deliberate. */
7392 && !constructor_designated
7393 /* Do not warn about initializing with ` = {0}'. */
7394 && !constructor_zeroinit)
3e4093b6 7395 {
32397f22
MLI
7396 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7397 "missing initializer for field %qD of %qT",
7398 constructor_unfilled_fields,
7399 constructor_type))
7400 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7401 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7402 }
7403 }
de520661 7404
3e4093b6 7405 /* Pad out the end of the structure. */
916c5919 7406 if (p->replacement_value.value)
3e4093b6
RS
7407 /* If this closes a superfluous brace pair,
7408 just pass out the element between them. */
916c5919 7409 ret = p->replacement_value;
3e4093b6
RS
7410 else if (constructor_type == 0)
7411 ;
7412 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7413 && TREE_CODE (constructor_type) != UNION_TYPE
7414 && TREE_CODE (constructor_type) != ARRAY_TYPE
7415 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7416 {
7417 /* A nonincremental scalar initializer--just return
7418 the element, after verifying there is just one. */
9771b263 7419 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7420 {
7421 if (!constructor_erroneous)
7422 error_init ("empty scalar initializer");
916c5919 7423 ret.value = error_mark_node;
3e4093b6 7424 }
9771b263 7425 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6
RS
7426 {
7427 error_init ("extra elements in scalar initializer");
9771b263 7428 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7429 }
7430 else
9771b263 7431 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7432 }
7433 else
7434 {
7435 if (constructor_erroneous)
916c5919 7436 ret.value = error_mark_node;
3e4093b6
RS
7437 else
7438 {
916c5919 7439 ret.value = build_constructor (constructor_type,
4038c495 7440 constructor_elements);
3e4093b6 7441 if (constructor_constant)
51eed280 7442 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7443 if (constructor_constant && constructor_simple)
916c5919 7444 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7445 if (constructor_nonconst)
7446 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7447 }
7448 }
de520661 7449
928c19bb
JM
7450 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7451 {
7452 if (constructor_nonconst)
7453 ret.original_code = C_MAYBE_CONST_EXPR;
7454 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7455 ret.original_code = ERROR_MARK;
7456 }
7457
3e4093b6
RS
7458 constructor_type = p->type;
7459 constructor_fields = p->fields;
7460 constructor_index = p->index;
7461 constructor_max_index = p->max_index;
7462 constructor_unfilled_index = p->unfilled_index;
7463 constructor_unfilled_fields = p->unfilled_fields;
7464 constructor_bit_index = p->bit_index;
7465 constructor_elements = p->elements;
7466 constructor_constant = p->constant;
7467 constructor_simple = p->simple;
928c19bb 7468 constructor_nonconst = p->nonconst;
3e4093b6
RS
7469 constructor_erroneous = p->erroneous;
7470 constructor_incremental = p->incremental;
7471 constructor_designated = p->designated;
7472 constructor_pending_elts = p->pending_elts;
7473 constructor_depth = p->depth;
7474 if (!p->implicit)
7475 constructor_range_stack = p->range_stack;
7476 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7477
3e4093b6
RS
7478 constructor_stack = p->next;
7479 free (p);
b621a4dd 7480
5d5e98dc
VR
7481 if (ret.value == 0 && constructor_stack == 0)
7482 ret.value = error_mark_node;
916c5919 7483 return ret;
3e4093b6 7484}
8b6a5902 7485
3e4093b6
RS
7486/* Common handling for both array range and field name designators.
7487 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7488
3e4093b6 7489static int
a1e3b3d9 7490set_designator (int array, struct obstack * braced_init_obstack)
de520661 7491{
3e4093b6
RS
7492 tree subtype;
7493 enum tree_code subcode;
de520661 7494
3e4093b6
RS
7495 /* Don't die if an entire brace-pair level is superfluous
7496 in the containing level. */
7497 if (constructor_type == 0)
7498 return 1;
de520661 7499
366de0ce
NS
7500 /* If there were errors in this designator list already, bail out
7501 silently. */
b06df647 7502 if (designator_erroneous)
3e4093b6 7503 return 1;
e28cae4f 7504
3e4093b6
RS
7505 if (!designator_depth)
7506 {
366de0ce 7507 gcc_assert (!constructor_range_stack);
de520661 7508
3e4093b6
RS
7509 /* Designator list starts at the level of closest explicit
7510 braces. */
7511 while (constructor_stack->implicit)
a1e3b3d9
LB
7512 {
7513 process_init_element (pop_init_level (1, braced_init_obstack),
7514 true, braced_init_obstack);
7515 }
3e4093b6
RS
7516 constructor_designated = 1;
7517 return 0;
7518 }
de520661 7519
366de0ce 7520 switch (TREE_CODE (constructor_type))
3c3fa147 7521 {
366de0ce
NS
7522 case RECORD_TYPE:
7523 case UNION_TYPE:
3e4093b6
RS
7524 subtype = TREE_TYPE (constructor_fields);
7525 if (subtype != error_mark_node)
7526 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7527 break;
7528 case ARRAY_TYPE:
3e4093b6 7529 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7530 break;
7531 default:
7532 gcc_unreachable ();
de520661 7533 }
400fbf9f 7534
3e4093b6
RS
7535 subcode = TREE_CODE (subtype);
7536 if (array && subcode != ARRAY_TYPE)
7537 {
7538 error_init ("array index in non-array initializer");
7539 return 1;
7540 }
7541 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7542 {
7543 error_init ("field name not in record or union initializer");
7544 return 1;
7545 }
d45cf215 7546
3e4093b6 7547 constructor_designated = 1;
a1e3b3d9 7548 push_init_level (2, braced_init_obstack);
3e4093b6 7549 return 0;
de520661 7550}
400fbf9f 7551
3e4093b6
RS
7552/* If there are range designators in designator list, push a new designator
7553 to constructor_range_stack. RANGE_END is end of such stack range or
7554 NULL_TREE if there is no range designator at this level. */
400fbf9f 7555
3e4093b6 7556static void
a1e3b3d9 7557push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7558{
7559 struct constructor_range_stack *p;
400fbf9f 7560
a1e3b3d9
LB
7561 p = (struct constructor_range_stack *)
7562 obstack_alloc (braced_init_obstack,
7563 sizeof (struct constructor_range_stack));
3e4093b6
RS
7564 p->prev = constructor_range_stack;
7565 p->next = 0;
7566 p->fields = constructor_fields;
7567 p->range_start = constructor_index;
7568 p->index = constructor_index;
7569 p->stack = constructor_stack;
7570 p->range_end = range_end;
8b6a5902 7571 if (constructor_range_stack)
3e4093b6
RS
7572 constructor_range_stack->next = p;
7573 constructor_range_stack = p;
de520661 7574}
400fbf9f 7575
3e4093b6
RS
7576/* Within an array initializer, specify the next index to be initialized.
7577 FIRST is that index. If LAST is nonzero, then initialize a range
7578 of indices, running from FIRST through LAST. */
5a7ec9d9 7579
de520661 7580void
a1e3b3d9
LB
7581set_init_index (tree first, tree last,
7582 struct obstack * braced_init_obstack)
de520661 7583{
a1e3b3d9 7584 if (set_designator (1, braced_init_obstack))
3e4093b6 7585 return;
de520661 7586
b06df647 7587 designator_erroneous = 1;
de520661 7588
3ea8cd06
JM
7589 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7590 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7591 {
7592 error_init ("array index in initializer not of integer type");
7593 return;
7594 }
7595
2b6da65c
JM
7596 if (TREE_CODE (first) != INTEGER_CST)
7597 {
7598 first = c_fully_fold (first, false, NULL);
7599 if (TREE_CODE (first) == INTEGER_CST)
c1771a20 7600 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7601 "array index in initializer is not "
7602 "an integer constant expression");
7603 }
7604
7605 if (last && TREE_CODE (last) != INTEGER_CST)
7606 {
7607 last = c_fully_fold (last, false, NULL);
7608 if (TREE_CODE (last) == INTEGER_CST)
c1771a20 7609 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7610 "array index in initializer is not "
7611 "an integer constant expression");
7612 }
7613
3e4093b6
RS
7614 if (TREE_CODE (first) != INTEGER_CST)
7615 error_init ("nonconstant array index in initializer");
7616 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7617 error_init ("nonconstant array index in initializer");
7618 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7619 error_init ("array index in non-array initializer");
622adc7e
MK
7620 else if (tree_int_cst_sgn (first) == -1)
7621 error_init ("array index in initializer exceeds array bounds");
3e4093b6
RS
7622 else if (constructor_max_index
7623 && tree_int_cst_lt (constructor_max_index, first))
7624 error_init ("array index in initializer exceeds array bounds");
7625 else
de520661 7626 {
928c19bb
JM
7627 constant_expression_warning (first);
7628 if (last)
7629 constant_expression_warning (last);
3e4093b6 7630 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7631 if (tree_int_cst_lt (constructor_index, first))
7632 {
7633 constructor_index = copy_node (constructor_index);
7634 TREE_OVERFLOW (constructor_index) = 1;
7635 }
665f2503 7636
3e4093b6 7637 if (last)
2bede729 7638 {
3e4093b6
RS
7639 if (tree_int_cst_equal (first, last))
7640 last = 0;
7641 else if (tree_int_cst_lt (last, first))
7642 {
7643 error_init ("empty index range in initializer");
7644 last = 0;
7645 }
7646 else
7647 {
7648 last = convert (bitsizetype, last);
7649 if (constructor_max_index != 0
7650 && tree_int_cst_lt (constructor_max_index, last))
7651 {
7652 error_init ("array index range in initializer exceeds array bounds");
7653 last = 0;
7654 }
7655 }
2bede729 7656 }
fed3cef0 7657
3e4093b6 7658 designator_depth++;
b06df647 7659 designator_erroneous = 0;
3e4093b6 7660 if (constructor_range_stack || last)
a1e3b3d9 7661 push_range_stack (last, braced_init_obstack);
de520661 7662 }
de520661 7663}
3e4093b6
RS
7664
7665/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7666
de520661 7667void
a1e3b3d9 7668set_init_label (tree fieldname, struct obstack * braced_init_obstack)
de520661 7669{
0fb96aa4 7670 tree field;
94ba5069 7671
a1e3b3d9 7672 if (set_designator (0, braced_init_obstack))
3e4093b6
RS
7673 return;
7674
b06df647 7675 designator_erroneous = 1;
3e4093b6
RS
7676
7677 if (TREE_CODE (constructor_type) != RECORD_TYPE
7678 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7679 {
3e4093b6
RS
7680 error_init ("field name not in record or union initializer");
7681 return;
94ba5069
RS
7682 }
7683
0fb96aa4 7684 field = lookup_field (constructor_type, fieldname);
8b6a5902 7685
0fb96aa4 7686 if (field == 0)
c51a1ba9 7687 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 7688 else
0fb96aa4
JM
7689 do
7690 {
7691 constructor_fields = TREE_VALUE (field);
7692 designator_depth++;
7693 designator_erroneous = 0;
7694 if (constructor_range_stack)
7695 push_range_stack (NULL_TREE, braced_init_obstack);
7696 field = TREE_CHAIN (field);
7697 if (field)
7698 {
7699 if (set_designator (0, braced_init_obstack))
7700 return;
7701 }
7702 }
7703 while (field != NULL_TREE);
3e4093b6
RS
7704}
7705\f
7706/* Add a new initializer to the tree of pending initializers. PURPOSE
7707 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7708 VALUE is the value of that index or field. If ORIGTYPE is not
7709 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7710
7711 IMPLICIT is true if value comes from pop_init_level (1),
7712 the new initializer has been merged with the existing one
7713 and thus no warnings should be emitted about overriding an
7714 existing initializer. */
de520661 7715
3e4093b6 7716static void
a1e3b3d9
LB
7717add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7718 struct obstack * braced_init_obstack)
3e4093b6
RS
7719{
7720 struct init_node *p, **q, *r;
7721
7722 q = &constructor_pending_elts;
7723 p = 0;
7724
7725 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7726 {
3e4093b6 7727 while (*q != 0)
91fa3c30 7728 {
3e4093b6
RS
7729 p = *q;
7730 if (tree_int_cst_lt (purpose, p->purpose))
7731 q = &p->left;
7732 else if (tree_int_cst_lt (p->purpose, purpose))
7733 q = &p->right;
7734 else
7735 {
b295aee2
JJ
7736 if (!implicit)
7737 {
7738 if (TREE_SIDE_EFFECTS (p->value))
7739 warning_init (0, "initialized field with side-effects overwritten");
7740 else if (warn_override_init)
7741 warning_init (OPT_Woverride_init, "initialized field overwritten");
7742 }
3e4093b6 7743 p->value = value;
bbbbb16a 7744 p->origtype = origtype;
3e4093b6
RS
7745 return;
7746 }
91fa3c30 7747 }
de520661 7748 }
3e4093b6 7749 else
de520661 7750 {
3e4093b6 7751 tree bitpos;
400fbf9f 7752
3e4093b6
RS
7753 bitpos = bit_position (purpose);
7754 while (*q != NULL)
7755 {
7756 p = *q;
7757 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7758 q = &p->left;
7759 else if (p->purpose != purpose)
7760 q = &p->right;
7761 else
7762 {
b295aee2
JJ
7763 if (!implicit)
7764 {
7765 if (TREE_SIDE_EFFECTS (p->value))
7766 warning_init (0, "initialized field with side-effects overwritten");
7767 else if (warn_override_init)
7768 warning_init (OPT_Woverride_init, "initialized field overwritten");
7769 }
3e4093b6 7770 p->value = value;
bbbbb16a 7771 p->origtype = origtype;
3e4093b6
RS
7772 return;
7773 }
7774 }
91fa3c30 7775 }
b71c7f8a 7776
a1e3b3d9
LB
7777 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7778 sizeof (struct init_node));
3e4093b6
RS
7779 r->purpose = purpose;
7780 r->value = value;
bbbbb16a 7781 r->origtype = origtype;
8b6a5902 7782
3e4093b6
RS
7783 *q = r;
7784 r->parent = p;
7785 r->left = 0;
7786 r->right = 0;
7787 r->balance = 0;
b71c7f8a 7788
3e4093b6 7789 while (p)
de520661 7790 {
3e4093b6 7791 struct init_node *s;
665f2503 7792
3e4093b6 7793 if (r == p->left)
2bede729 7794 {
3e4093b6
RS
7795 if (p->balance == 0)
7796 p->balance = -1;
7797 else if (p->balance < 0)
7798 {
7799 if (r->balance < 0)
7800 {
7801 /* L rotation. */
7802 p->left = r->right;
7803 if (p->left)
7804 p->left->parent = p;
7805 r->right = p;
e7b6a0ee 7806
3e4093b6
RS
7807 p->balance = 0;
7808 r->balance = 0;
39bc99c2 7809
3e4093b6
RS
7810 s = p->parent;
7811 p->parent = r;
7812 r->parent = s;
7813 if (s)
7814 {
7815 if (s->left == p)
7816 s->left = r;
7817 else
7818 s->right = r;
7819 }
7820 else
7821 constructor_pending_elts = r;
7822 }
7823 else
7824 {
7825 /* LR rotation. */
7826 struct init_node *t = r->right;
e7b6a0ee 7827
3e4093b6
RS
7828 r->right = t->left;
7829 if (r->right)
7830 r->right->parent = r;
7831 t->left = r;
7832
7833 p->left = t->right;
7834 if (p->left)
7835 p->left->parent = p;
7836 t->right = p;
7837
7838 p->balance = t->balance < 0;
7839 r->balance = -(t->balance > 0);
7840 t->balance = 0;
7841
7842 s = p->parent;
7843 p->parent = t;
7844 r->parent = t;
7845 t->parent = s;
7846 if (s)
7847 {
7848 if (s->left == p)
7849 s->left = t;
7850 else
7851 s->right = t;
7852 }
7853 else
7854 constructor_pending_elts = t;
7855 }
7856 break;
7857 }
7858 else
7859 {
7860 /* p->balance == +1; growth of left side balances the node. */
7861 p->balance = 0;
7862 break;
7863 }
2bede729 7864 }
3e4093b6
RS
7865 else /* r == p->right */
7866 {
7867 if (p->balance == 0)
7868 /* Growth propagation from right side. */
7869 p->balance++;
7870 else if (p->balance > 0)
7871 {
7872 if (r->balance > 0)
7873 {
7874 /* R rotation. */
7875 p->right = r->left;
7876 if (p->right)
7877 p->right->parent = p;
7878 r->left = p;
7879
7880 p->balance = 0;
7881 r->balance = 0;
7882
7883 s = p->parent;
7884 p->parent = r;
7885 r->parent = s;
7886 if (s)
7887 {
7888 if (s->left == p)
7889 s->left = r;
7890 else
7891 s->right = r;
7892 }
7893 else
7894 constructor_pending_elts = r;
7895 }
7896 else /* r->balance == -1 */
7897 {
7898 /* RL rotation */
7899 struct init_node *t = r->left;
7900
7901 r->left = t->right;
7902 if (r->left)
7903 r->left->parent = r;
7904 t->right = r;
7905
7906 p->right = t->left;
7907 if (p->right)
7908 p->right->parent = p;
7909 t->left = p;
7910
7911 r->balance = (t->balance < 0);
7912 p->balance = -(t->balance > 0);
7913 t->balance = 0;
7914
7915 s = p->parent;
7916 p->parent = t;
7917 r->parent = t;
7918 t->parent = s;
7919 if (s)
7920 {
7921 if (s->left == p)
7922 s->left = t;
7923 else
7924 s->right = t;
7925 }
7926 else
7927 constructor_pending_elts = t;
7928 }
7929 break;
7930 }
7931 else
7932 {
7933 /* p->balance == -1; growth of right side balances the node. */
7934 p->balance = 0;
7935 break;
7936 }
7937 }
7938
7939 r = p;
7940 p = p->parent;
7941 }
7942}
7943
7944/* Build AVL tree from a sorted chain. */
7945
7946static void
a1e3b3d9 7947set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 7948{
4038c495
GB
7949 unsigned HOST_WIDE_INT ix;
7950 tree index, value;
3e4093b6
RS
7951
7952 if (TREE_CODE (constructor_type) != RECORD_TYPE
7953 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7954 return;
7955
4038c495 7956 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
a1e3b3d9 7957 {
fdce1719 7958 add_pending_init (index, value, NULL_TREE, true,
a1e3b3d9
LB
7959 braced_init_obstack);
7960 }
9771b263 7961 constructor_elements = NULL;
3e4093b6
RS
7962 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7963 {
7964 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7965 /* Skip any nameless bit fields at the beginning. */
7966 while (constructor_unfilled_fields != 0
7967 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7968 && DECL_NAME (constructor_unfilled_fields) == 0)
7969 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 7970
de520661 7971 }
3e4093b6 7972 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7973 {
3e4093b6
RS
7974 if (TYPE_DOMAIN (constructor_type))
7975 constructor_unfilled_index
7976 = convert (bitsizetype,
7977 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7978 else
7979 constructor_unfilled_index = bitsize_zero_node;
de520661 7980 }
3e4093b6 7981 constructor_incremental = 0;
de520661 7982}
400fbf9f 7983
3e4093b6 7984/* Build AVL tree from a string constant. */
de520661 7985
3e4093b6 7986static void
a1e3b3d9
LB
7987set_nonincremental_init_from_string (tree str,
7988 struct obstack * braced_init_obstack)
de520661 7989{
3e4093b6
RS
7990 tree value, purpose, type;
7991 HOST_WIDE_INT val[2];
7992 const char *p, *end;
7993 int byte, wchar_bytes, charwidth, bitpos;
de520661 7994
366de0ce 7995 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 7996
c466b2cd 7997 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
7998 charwidth = TYPE_PRECISION (char_type_node);
7999 type = TREE_TYPE (constructor_type);
8000 p = TREE_STRING_POINTER (str);
8001 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8002
3e4093b6 8003 for (purpose = bitsize_zero_node;
8824edff
JJ
8004 p < end
8005 && !(constructor_max_index
8006 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8007 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8008 {
3e4093b6 8009 if (wchar_bytes == 1)
ffc5c6a9 8010 {
3e4093b6
RS
8011 val[1] = (unsigned char) *p++;
8012 val[0] = 0;
ffc5c6a9
RH
8013 }
8014 else
3e4093b6
RS
8015 {
8016 val[0] = 0;
8017 val[1] = 0;
8018 for (byte = 0; byte < wchar_bytes; byte++)
8019 {
8020 if (BYTES_BIG_ENDIAN)
8021 bitpos = (wchar_bytes - byte - 1) * charwidth;
8022 else
8023 bitpos = byte * charwidth;
8024 val[bitpos < HOST_BITS_PER_WIDE_INT]
8025 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8026 << (bitpos % HOST_BITS_PER_WIDE_INT);
8027 }
8028 }
584ef5fe 8029
8df83eae 8030 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8031 {
8032 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8033 if (bitpos < HOST_BITS_PER_WIDE_INT)
8034 {
8035 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8036 {
8037 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
8038 val[0] = -1;
8039 }
8040 }
8041 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8042 {
8043 if (val[1] < 0)
c22cacf3 8044 val[0] = -1;
3e4093b6
RS
8045 }
8046 else if (val[0] & (((HOST_WIDE_INT) 1)
8047 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8048 val[0] |= ((HOST_WIDE_INT) -1)
8049 << (bitpos - HOST_BITS_PER_WIDE_INT);
8050 }
ffc5c6a9 8051
7d60be94 8052 value = build_int_cst_wide (type, val[1], val[0]);
fdce1719 8053 add_pending_init (purpose, value, NULL_TREE, true,
a1e3b3d9 8054 braced_init_obstack);
9dfcc8db
BH
8055 }
8056
3e4093b6
RS
8057 constructor_incremental = 0;
8058}
de520661 8059
3e4093b6
RS
8060/* Return value of FIELD in pending initializer or zero if the field was
8061 not initialized yet. */
8062
8063static tree
a1e3b3d9 8064find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8065{
8066 struct init_node *p;
8067
8068 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8069 {
3e4093b6
RS
8070 if (constructor_incremental
8071 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8072 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8073
8074 p = constructor_pending_elts;
8075 while (p)
19d76e60 8076 {
3e4093b6
RS
8077 if (tree_int_cst_lt (field, p->purpose))
8078 p = p->left;
8079 else if (tree_int_cst_lt (p->purpose, field))
8080 p = p->right;
8081 else
8082 return p->value;
19d76e60 8083 }
19d76e60 8084 }
3e4093b6 8085 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8086 {
3e4093b6 8087 tree bitpos = bit_position (field);
de520661 8088
3e4093b6
RS
8089 if (constructor_incremental
8090 && (!constructor_unfilled_fields
8091 || tree_int_cst_lt (bitpos,
8092 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8093 set_nonincremental_init (braced_init_obstack);
de520661 8094
3e4093b6
RS
8095 p = constructor_pending_elts;
8096 while (p)
8097 {
8098 if (field == p->purpose)
8099 return p->value;
8100 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8101 p = p->left;
8102 else
8103 p = p->right;
8104 }
8105 }
8106 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8107 {
9771b263
DN
8108 if (!vec_safe_is_empty (constructor_elements)
8109 && (constructor_elements->last ().index == field))
8110 return constructor_elements->last ().value;
de520661 8111 }
3e4093b6 8112 return 0;
de520661
RS
8113}
8114
3e4093b6
RS
8115/* "Output" the next constructor element.
8116 At top level, really output it to assembler code now.
8117 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8118 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8119 TYPE is the data type that the containing data type wants here.
8120 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8121 If VALUE is a string constant, STRICT_STRING is true if it is
8122 unparenthesized or we should not warn here for it being parenthesized.
8123 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8124
3e4093b6
RS
8125 PENDING if non-nil means output pending elements that belong
8126 right after this element. (PENDING is normally 1;
b295aee2
JJ
8127 it is 0 while outputting pending elements, to avoid recursion.)
8128
8129 IMPLICIT is true if value comes from pop_init_level (1),
8130 the new initializer has been merged with the existing one
8131 and thus no warnings should be emitted about overriding an
8132 existing initializer. */
8b6a5902 8133
3e4093b6 8134static void
bbbbb16a 8135output_init_element (tree value, tree origtype, bool strict_string, tree type,
a1e3b3d9
LB
8136 tree field, int pending, bool implicit,
8137 struct obstack * braced_init_obstack)
3e4093b6 8138{
8ce94e44 8139 tree semantic_type = NULL_TREE;
928c19bb
JM
8140 bool maybe_const = true;
8141 bool npc;
4038c495 8142
0a880880 8143 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8144 {
3e4093b6
RS
8145 constructor_erroneous = 1;
8146 return;
8b6a5902 8147 }
46bdb9cf
JM
8148 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8149 && (TREE_CODE (value) == STRING_CST
8150 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8151 && !(TREE_CODE (value) == STRING_CST
8152 && TREE_CODE (type) == ARRAY_TYPE
8153 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8154 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8155 TYPE_MAIN_VARIANT (type)))
c2255bc4 8156 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8157
3e4093b6
RS
8158 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8159 && require_constant_value && !flag_isoc99 && pending)
8b6a5902 8160 {
3e4093b6
RS
8161 /* As an extension, allow initializing objects with static storage
8162 duration with compound literals (which are then treated just as
8163 the brace enclosed list they contain). */
8164 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8165 value = DECL_INITIAL (decl);
8b6a5902
JJ
8166 }
8167
928c19bb 8168 npc = null_pointer_constant_p (value);
8ce94e44
JM
8169 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8170 {
8171 semantic_type = TREE_TYPE (value);
8172 value = TREE_OPERAND (value, 0);
8173 }
928c19bb
JM
8174 value = c_fully_fold (value, require_constant_value, &maybe_const);
8175
3e4093b6
RS
8176 if (value == error_mark_node)
8177 constructor_erroneous = 1;
8178 else if (!TREE_CONSTANT (value))
8179 constructor_constant = 0;
116df786 8180 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8181 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8182 || TREE_CODE (constructor_type) == UNION_TYPE)
8183 && DECL_C_BIT_FIELD (field)
8184 && TREE_CODE (value) != INTEGER_CST))
8185 constructor_simple = 0;
928c19bb
JM
8186 if (!maybe_const)
8187 constructor_nonconst = 1;
3e4093b6 8188
116df786 8189 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8190 {
116df786
RH
8191 if (require_constant_value)
8192 {
8193 error_init ("initializer element is not constant");
8194 value = error_mark_node;
8195 }
8196 else if (require_constant_elements)
509c9d60
MLI
8197 pedwarn (input_location, 0,
8198 "initializer element is not computable at load time");
8b6a5902 8199 }
928c19bb
JM
8200 else if (!maybe_const
8201 && (require_constant_value || require_constant_elements))
8202 pedwarn_init (input_location, 0,
8203 "initializer element is not a constant expression");
3e4093b6 8204
81f40b79
ILT
8205 /* Issue -Wc++-compat warnings about initializing a bitfield with
8206 enum type. */
8207 if (warn_cxx_compat
8208 && field != NULL_TREE
8209 && TREE_CODE (field) == FIELD_DECL
8210 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8211 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8212 != TYPE_MAIN_VARIANT (type))
8213 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8214 {
8215 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8216 if (checktype != error_mark_node
8217 && (TYPE_MAIN_VARIANT (checktype)
8218 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8219 warning_init (OPT_Wc___compat,
8220 "enum conversion in initialization is invalid in C++");
8221 }
8222
3e4093b6
RS
8223 /* If this field is empty (and not at the end of structure),
8224 don't do anything other than checking the initializer. */
8225 if (field
8226 && (TREE_TYPE (field) == error_mark_node
8227 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8228 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8229 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8230 || DECL_CHAIN (field)))))
3e4093b6
RS
8231 return;
8232
8ce94e44
JM
8233 if (semantic_type)
8234 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
c2255bc4
AH
8235 value = digest_init (input_location, type, value, origtype, npc,
8236 strict_string, require_constant_value);
3e4093b6 8237 if (value == error_mark_node)
8b6a5902 8238 {
3e4093b6
RS
8239 constructor_erroneous = 1;
8240 return;
8b6a5902 8241 }
928c19bb
JM
8242 if (require_constant_value || require_constant_elements)
8243 constant_expression_warning (value);
8b6a5902 8244
3e4093b6
RS
8245 /* If this element doesn't come next in sequence,
8246 put it on constructor_pending_elts. */
8247 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8248 && (!constructor_incremental
8249 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8250 {
3e4093b6
RS
8251 if (constructor_incremental
8252 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8253 set_nonincremental_init (braced_init_obstack);
3e4093b6 8254
a1e3b3d9
LB
8255 add_pending_init (field, value, origtype, implicit,
8256 braced_init_obstack);
3e4093b6 8257 return;
8b6a5902 8258 }
3e4093b6
RS
8259 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8260 && (!constructor_incremental
8261 || field != constructor_unfilled_fields))
8b6a5902 8262 {
3e4093b6
RS
8263 /* We do this for records but not for unions. In a union,
8264 no matter which field is specified, it can be initialized
8265 right away since it starts at the beginning of the union. */
8266 if (constructor_incremental)
8267 {
8268 if (!constructor_unfilled_fields)
a1e3b3d9 8269 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8270 else
8271 {
8272 tree bitpos, unfillpos;
8273
8274 bitpos = bit_position (field);
8275 unfillpos = bit_position (constructor_unfilled_fields);
8276
8277 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8278 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8279 }
8280 }
8281
a1e3b3d9
LB
8282 add_pending_init (field, value, origtype, implicit,
8283 braced_init_obstack);
3e4093b6 8284 return;
8b6a5902 8285 }
3e4093b6 8286 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8287 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8288 {
b295aee2
JJ
8289 if (!implicit)
8290 {
9771b263 8291 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
b295aee2
JJ
8292 warning_init (0,
8293 "initialized field with side-effects overwritten");
8294 else if (warn_override_init)
8295 warning_init (OPT_Woverride_init, "initialized field overwritten");
8296 }
8b6a5902 8297
3e4093b6 8298 /* We can have just one union field set. */
9771b263 8299 constructor_elements = NULL;
3e4093b6 8300 }
8b6a5902 8301
3e4093b6
RS
8302 /* Otherwise, output this element either to
8303 constructor_elements or to the assembler file. */
8b6a5902 8304
f32682ca 8305 constructor_elt celt = {field, value};
9771b263 8306 vec_safe_push (constructor_elements, celt);
8b6a5902 8307
3e4093b6
RS
8308 /* Advance the variable that indicates sequential elements output. */
8309 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8310 constructor_unfilled_index
db3927fb
AH
8311 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8312 bitsize_one_node);
3e4093b6
RS
8313 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8314 {
8315 constructor_unfilled_fields
910ad8de 8316 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8317
3e4093b6
RS
8318 /* Skip any nameless bit fields. */
8319 while (constructor_unfilled_fields != 0
8320 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8321 && DECL_NAME (constructor_unfilled_fields) == 0)
8322 constructor_unfilled_fields =
910ad8de 8323 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8324 }
8325 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8326 constructor_unfilled_fields = 0;
de520661 8327
3e4093b6
RS
8328 /* Now output any pending elements which have become next. */
8329 if (pending)
a1e3b3d9 8330 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8331}
8b6a5902 8332
3e4093b6
RS
8333/* Output any pending elements which have become next.
8334 As we output elements, constructor_unfilled_{fields,index}
8335 advances, which may cause other elements to become next;
8336 if so, they too are output.
8b6a5902 8337
3e4093b6
RS
8338 If ALL is 0, we return when there are
8339 no more pending elements to output now.
665f2503 8340
3e4093b6
RS
8341 If ALL is 1, we output space as necessary so that
8342 we can output all the pending elements. */
3e4093b6 8343static void
a1e3b3d9 8344output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8345{
8346 struct init_node *elt = constructor_pending_elts;
8347 tree next;
de520661 8348
3e4093b6
RS
8349 retry:
8350
ba228239 8351 /* Look through the whole pending tree.
3e4093b6
RS
8352 If we find an element that should be output now,
8353 output it. Otherwise, set NEXT to the element
8354 that comes first among those still pending. */
8355
8356 next = 0;
8357 while (elt)
8358 {
8359 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8360 {
3e4093b6
RS
8361 if (tree_int_cst_equal (elt->purpose,
8362 constructor_unfilled_index))
bbbbb16a 8363 output_init_element (elt->value, elt->origtype, true,
3e4093b6 8364 TREE_TYPE (constructor_type),
a1e3b3d9
LB
8365 constructor_unfilled_index, 0, false,
8366 braced_init_obstack);
3e4093b6
RS
8367 else if (tree_int_cst_lt (constructor_unfilled_index,
8368 elt->purpose))
8b6a5902 8369 {
3e4093b6
RS
8370 /* Advance to the next smaller node. */
8371 if (elt->left)
8372 elt = elt->left;
8373 else
8374 {
8375 /* We have reached the smallest node bigger than the
8376 current unfilled index. Fill the space first. */
8377 next = elt->purpose;
8378 break;
8379 }
8b6a5902 8380 }
ce662d4c
JJ
8381 else
8382 {
3e4093b6
RS
8383 /* Advance to the next bigger node. */
8384 if (elt->right)
8385 elt = elt->right;
8386 else
ce662d4c 8387 {
3e4093b6
RS
8388 /* We have reached the biggest node in a subtree. Find
8389 the parent of it, which is the next bigger node. */
8390 while (elt->parent && elt->parent->right == elt)
8391 elt = elt->parent;
8392 elt = elt->parent;
8393 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8394 elt->purpose))
8395 {
8396 next = elt->purpose;
8397 break;
8398 }
ce662d4c
JJ
8399 }
8400 }
8b6a5902 8401 }
3e4093b6
RS
8402 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8403 || TREE_CODE (constructor_type) == UNION_TYPE)
8404 {
8405 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8406
3e4093b6
RS
8407 /* If the current record is complete we are done. */
8408 if (constructor_unfilled_fields == 0)
8409 break;
de520661 8410
3e4093b6
RS
8411 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8412 elt_bitpos = bit_position (elt->purpose);
8413 /* We can't compare fields here because there might be empty
8414 fields in between. */
8415 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8416 {
8417 constructor_unfilled_fields = elt->purpose;
bbbbb16a
ILT
8418 output_init_element (elt->value, elt->origtype, true,
8419 TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8420 elt->purpose, 0, false,
8421 braced_init_obstack);
3e4093b6
RS
8422 }
8423 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8424 {
8425 /* Advance to the next smaller node. */
8426 if (elt->left)
8427 elt = elt->left;
8428 else
8429 {
8430 /* We have reached the smallest node bigger than the
8431 current unfilled field. Fill the space first. */
8432 next = elt->purpose;
8433 break;
8434 }
8435 }
8436 else
8437 {
8438 /* Advance to the next bigger node. */
8439 if (elt->right)
8440 elt = elt->right;
8441 else
8442 {
8443 /* We have reached the biggest node in a subtree. Find
8444 the parent of it, which is the next bigger node. */
8445 while (elt->parent && elt->parent->right == elt)
8446 elt = elt->parent;
8447 elt = elt->parent;
8448 if (elt
8449 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8450 bit_position (elt->purpose))))
8451 {
8452 next = elt->purpose;
8453 break;
8454 }
8455 }
8456 }
8457 }
8458 }
de520661 8459
3e4093b6
RS
8460 /* Ordinarily return, but not if we want to output all
8461 and there are elements left. */
3f75a254 8462 if (!(all && next != 0))
e5cfb88f
RK
8463 return;
8464
3e4093b6
RS
8465 /* If it's not incremental, just skip over the gap, so that after
8466 jumping to retry we will output the next successive element. */
8467 if (TREE_CODE (constructor_type) == RECORD_TYPE
8468 || TREE_CODE (constructor_type) == UNION_TYPE)
8469 constructor_unfilled_fields = next;
8470 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8471 constructor_unfilled_index = next;
de520661 8472
3e4093b6
RS
8473 /* ELT now points to the node in the pending tree with the next
8474 initializer to output. */
8475 goto retry;
de520661
RS
8476}
8477\f
3e4093b6
RS
8478/* Add one non-braced element to the current constructor level.
8479 This adjusts the current position within the constructor's type.
8480 This may also start or terminate implicit levels
8481 to handle a partly-braced initializer.
e5e809f4 8482
3e4093b6 8483 Once this has found the correct level for the new element,
b295aee2
JJ
8484 it calls output_init_element.
8485
8486 IMPLICIT is true if value comes from pop_init_level (1),
8487 the new initializer has been merged with the existing one
8488 and thus no warnings should be emitted about overriding an
8489 existing initializer. */
3e4093b6
RS
8490
8491void
a1e3b3d9
LB
8492process_init_element (struct c_expr value, bool implicit,
8493 struct obstack * braced_init_obstack)
e5e809f4 8494{
916c5919
JM
8495 tree orig_value = value.value;
8496 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8497 bool strict_string = value.original_code == STRING_CST;
e5e809f4 8498
3e4093b6 8499 designator_depth = 0;
b06df647 8500 designator_erroneous = 0;
e5e809f4 8501
3e4093b6
RS
8502 /* Handle superfluous braces around string cst as in
8503 char x[] = {"foo"}; */
8504 if (string_flag
8505 && constructor_type
8506 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8507 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8508 && integer_zerop (constructor_unfilled_index))
e5e809f4 8509 {
916c5919 8510 if (constructor_stack->replacement_value.value)
c22cacf3 8511 error_init ("excess elements in char array initializer");
3e4093b6
RS
8512 constructor_stack->replacement_value = value;
8513 return;
e5e809f4 8514 }
8b6a5902 8515
916c5919 8516 if (constructor_stack->replacement_value.value != 0)
3e4093b6
RS
8517 {
8518 error_init ("excess elements in struct initializer");
8519 return;
e5e809f4
JL
8520 }
8521
3e4093b6
RS
8522 /* Ignore elements of a brace group if it is entirely superfluous
8523 and has already been diagnosed. */
8524 if (constructor_type == 0)
8525 return;
e5e809f4 8526
3e4093b6
RS
8527 /* If we've exhausted any levels that didn't have braces,
8528 pop them now. */
8529 while (constructor_stack->implicit)
8530 {
8531 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8532 || TREE_CODE (constructor_type) == UNION_TYPE)
8533 && constructor_fields == 0)
a1e3b3d9
LB
8534 process_init_element (pop_init_level (1, braced_init_obstack),
8535 true, braced_init_obstack);
53650abe
AP
8536 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8537 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8538 && constructor_max_index
8539 && tree_int_cst_lt (constructor_max_index,
8540 constructor_index))
a1e3b3d9
LB
8541 process_init_element (pop_init_level (1, braced_init_obstack),
8542 true, braced_init_obstack);
3e4093b6
RS
8543 else
8544 break;
8545 }
e5e809f4 8546
3e4093b6
RS
8547 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8548 if (constructor_range_stack)
e5e809f4 8549 {
3e4093b6
RS
8550 /* If value is a compound literal and we'll be just using its
8551 content, don't put it into a SAVE_EXPR. */
916c5919 8552 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
8553 || !require_constant_value
8554 || flag_isoc99)
8ce94e44
JM
8555 {
8556 tree semantic_type = NULL_TREE;
8557 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8558 {
8559 semantic_type = TREE_TYPE (value.value);
8560 value.value = TREE_OPERAND (value.value, 0);
8561 }
8562 value.value = c_save_expr (value.value);
8563 if (semantic_type)
8564 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8565 value.value);
8566 }
3e4093b6 8567 }
e5e809f4 8568
3e4093b6
RS
8569 while (1)
8570 {
8571 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8572 {
3e4093b6
RS
8573 tree fieldtype;
8574 enum tree_code fieldcode;
e5e809f4 8575
3e4093b6
RS
8576 if (constructor_fields == 0)
8577 {
509c9d60
MLI
8578 pedwarn_init (input_location, 0,
8579 "excess elements in struct initializer");
3e4093b6
RS
8580 break;
8581 }
e5e809f4 8582
3e4093b6
RS
8583 fieldtype = TREE_TYPE (constructor_fields);
8584 if (fieldtype != error_mark_node)
8585 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8586 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8587
3e4093b6
RS
8588 /* Error for non-static initialization of a flexible array member. */
8589 if (fieldcode == ARRAY_TYPE
8590 && !require_constant_value
8591 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8592 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6
RS
8593 {
8594 error_init ("non-static initialization of a flexible array member");
8595 break;
8596 }
e5e809f4 8597
3e4093b6 8598 /* Accept a string constant to initialize a subarray. */
916c5919 8599 if (value.value != 0
3e4093b6 8600 && fieldcode == ARRAY_TYPE
197463ae 8601 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8602 && string_flag)
916c5919 8603 value.value = orig_value;
3e4093b6
RS
8604 /* Otherwise, if we have come to a subaggregate,
8605 and we don't have an element of its type, push into it. */
0953878d 8606 else if (value.value != 0
916c5919
JM
8607 && value.value != error_mark_node
8608 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8609 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8610 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8611 {
a1e3b3d9 8612 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8613 continue;
8614 }
e5e809f4 8615
916c5919 8616 if (value.value)
3e4093b6
RS
8617 {
8618 push_member_name (constructor_fields);
bbbbb16a
ILT
8619 output_init_element (value.value, value.original_type,
8620 strict_string, fieldtype,
a1e3b3d9
LB
8621 constructor_fields, 1, implicit,
8622 braced_init_obstack);
3e4093b6 8623 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8624 }
8625 else
3e4093b6
RS
8626 /* Do the bookkeeping for an element that was
8627 directly output as a constructor. */
e5e809f4 8628 {
3e4093b6
RS
8629 /* For a record, keep track of end position of last field. */
8630 if (DECL_SIZE (constructor_fields))
c22cacf3 8631 constructor_bit_index
db3927fb
AH
8632 = size_binop_loc (input_location, PLUS_EXPR,
8633 bit_position (constructor_fields),
8634 DECL_SIZE (constructor_fields));
3e4093b6
RS
8635
8636 /* If the current field was the first one not yet written out,
8637 it isn't now, so update. */
8638 if (constructor_unfilled_fields == constructor_fields)
8639 {
910ad8de 8640 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8641 /* Skip any nameless bit fields. */
8642 while (constructor_unfilled_fields != 0
8643 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8644 && DECL_NAME (constructor_unfilled_fields) == 0)
8645 constructor_unfilled_fields =
910ad8de 8646 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8647 }
e5e809f4 8648 }
3e4093b6 8649
910ad8de 8650 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8651 /* Skip any nameless bit fields at the beginning. */
8652 while (constructor_fields != 0
8653 && DECL_C_BIT_FIELD (constructor_fields)
8654 && DECL_NAME (constructor_fields) == 0)
910ad8de 8655 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8656 }
3e4093b6 8657 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8658 {
3e4093b6
RS
8659 tree fieldtype;
8660 enum tree_code fieldcode;
e5e809f4 8661
3e4093b6
RS
8662 if (constructor_fields == 0)
8663 {
509c9d60
MLI
8664 pedwarn_init (input_location, 0,
8665 "excess elements in union initializer");
3e4093b6
RS
8666 break;
8667 }
e5e809f4 8668
3e4093b6
RS
8669 fieldtype = TREE_TYPE (constructor_fields);
8670 if (fieldtype != error_mark_node)
8671 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8672 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8673
3e4093b6
RS
8674 /* Warn that traditional C rejects initialization of unions.
8675 We skip the warning if the value is zero. This is done
8676 under the assumption that the zero initializer in user
8677 code appears conditioned on e.g. __STDC__ to avoid
8678 "missing initializer" warnings and relies on default
8679 initialization to zero in the traditional C case.
8680 We also skip the warning if the initializer is designated,
8681 again on the assumption that this must be conditional on
8682 __STDC__ anyway (and we've already complained about the
8683 member-designator already). */
3176a0c2 8684 if (!in_system_header && !constructor_designated
916c5919
JM
8685 && !(value.value && (integer_zerop (value.value)
8686 || real_zerop (value.value))))
3176a0c2
DD
8687 warning (OPT_Wtraditional, "traditional C rejects initialization "
8688 "of unions");
e5e809f4 8689
3e4093b6 8690 /* Accept a string constant to initialize a subarray. */
916c5919 8691 if (value.value != 0
3e4093b6 8692 && fieldcode == ARRAY_TYPE
197463ae 8693 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8694 && string_flag)
916c5919 8695 value.value = orig_value;
3e4093b6
RS
8696 /* Otherwise, if we have come to a subaggregate,
8697 and we don't have an element of its type, push into it. */
0953878d 8698 else if (value.value != 0
916c5919
JM
8699 && value.value != error_mark_node
8700 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8701 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8702 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8703 {
a1e3b3d9 8704 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8705 continue;
8706 }
e5e809f4 8707
916c5919 8708 if (value.value)
3e4093b6
RS
8709 {
8710 push_member_name (constructor_fields);
bbbbb16a
ILT
8711 output_init_element (value.value, value.original_type,
8712 strict_string, fieldtype,
a1e3b3d9
LB
8713 constructor_fields, 1, implicit,
8714 braced_init_obstack);
3e4093b6 8715 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8716 }
8717 else
3e4093b6
RS
8718 /* Do the bookkeeping for an element that was
8719 directly output as a constructor. */
e5e809f4 8720 {
3e4093b6 8721 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8722 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8723 }
e5e809f4 8724
3e4093b6
RS
8725 constructor_fields = 0;
8726 }
8727 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8728 {
8729 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8730 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8731
3e4093b6 8732 /* Accept a string constant to initialize a subarray. */
916c5919 8733 if (value.value != 0
3e4093b6 8734 && eltcode == ARRAY_TYPE
197463ae 8735 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8736 && string_flag)
916c5919 8737 value.value = orig_value;
3e4093b6
RS
8738 /* Otherwise, if we have come to a subaggregate,
8739 and we don't have an element of its type, push into it. */
0953878d 8740 else if (value.value != 0
916c5919
JM
8741 && value.value != error_mark_node
8742 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 8743 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 8744 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 8745 {
a1e3b3d9 8746 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8747 continue;
8748 }
8b6a5902 8749
3e4093b6
RS
8750 if (constructor_max_index != 0
8751 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8752 || integer_all_onesp (constructor_max_index)))
8753 {
509c9d60
MLI
8754 pedwarn_init (input_location, 0,
8755 "excess elements in array initializer");
3e4093b6
RS
8756 break;
8757 }
8b6a5902 8758
3e4093b6 8759 /* Now output the actual element. */
916c5919 8760 if (value.value)
3e4093b6 8761 {
a0f0ab9f 8762 push_array_bounds (tree_low_cst (constructor_index, 1));
bbbbb16a
ILT
8763 output_init_element (value.value, value.original_type,
8764 strict_string, elttype,
a1e3b3d9
LB
8765 constructor_index, 1, implicit,
8766 braced_init_obstack);
3e4093b6
RS
8767 RESTORE_SPELLING_DEPTH (constructor_depth);
8768 }
2f6e4e97 8769
3e4093b6 8770 constructor_index
db3927fb
AH
8771 = size_binop_loc (input_location, PLUS_EXPR,
8772 constructor_index, bitsize_one_node);
8b6a5902 8773
916c5919 8774 if (!value.value)
3e4093b6
RS
8775 /* If we are doing the bookkeeping for an element that was
8776 directly output as a constructor, we must update
8777 constructor_unfilled_index. */
8778 constructor_unfilled_index = constructor_index;
8779 }
8780 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8781 {
8782 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 8783
c22cacf3
MS
8784 /* Do a basic check of initializer size. Note that vectors
8785 always have a fixed size derived from their type. */
3e4093b6
RS
8786 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8787 {
509c9d60
MLI
8788 pedwarn_init (input_location, 0,
8789 "excess elements in vector initializer");
3e4093b6
RS
8790 break;
8791 }
8b6a5902 8792
3e4093b6 8793 /* Now output the actual element. */
916c5919 8794 if (value.value)
53650abe
AP
8795 {
8796 if (TREE_CODE (value.value) == VECTOR_CST)
8797 elttype = TYPE_MAIN_VARIANT (constructor_type);
8798 output_init_element (value.value, value.original_type,
8799 strict_string, elttype,
a1e3b3d9
LB
8800 constructor_index, 1, implicit,
8801 braced_init_obstack);
53650abe 8802 }
8b6a5902 8803
3e4093b6 8804 constructor_index
db3927fb
AH
8805 = size_binop_loc (input_location,
8806 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 8807
916c5919 8808 if (!value.value)
3e4093b6
RS
8809 /* If we are doing the bookkeeping for an element that was
8810 directly output as a constructor, we must update
8811 constructor_unfilled_index. */
8812 constructor_unfilled_index = constructor_index;
8813 }
8b6a5902 8814
3e4093b6
RS
8815 /* Handle the sole element allowed in a braced initializer
8816 for a scalar variable. */
b4519d39
SB
8817 else if (constructor_type != error_mark_node
8818 && constructor_fields == 0)
8b6a5902 8819 {
509c9d60
MLI
8820 pedwarn_init (input_location, 0,
8821 "excess elements in scalar initializer");
3e4093b6 8822 break;
8b6a5902
JJ
8823 }
8824 else
8825 {
916c5919 8826 if (value.value)
bbbbb16a
ILT
8827 output_init_element (value.value, value.original_type,
8828 strict_string, constructor_type,
a1e3b3d9
LB
8829 NULL_TREE, 1, implicit,
8830 braced_init_obstack);
3e4093b6 8831 constructor_fields = 0;
8b6a5902
JJ
8832 }
8833
3e4093b6
RS
8834 /* Handle range initializers either at this level or anywhere higher
8835 in the designator stack. */
8836 if (constructor_range_stack)
8b6a5902 8837 {
3e4093b6
RS
8838 struct constructor_range_stack *p, *range_stack;
8839 int finish = 0;
8840
8841 range_stack = constructor_range_stack;
8842 constructor_range_stack = 0;
8843 while (constructor_stack != range_stack->stack)
8b6a5902 8844 {
366de0ce 8845 gcc_assert (constructor_stack->implicit);
a1e3b3d9
LB
8846 process_init_element (pop_init_level (1,
8847 braced_init_obstack),
8848 true, braced_init_obstack);
8b6a5902 8849 }
3e4093b6
RS
8850 for (p = range_stack;
8851 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8852 p = p->prev)
8b6a5902 8853 {
366de0ce 8854 gcc_assert (constructor_stack->implicit);
a1e3b3d9
LB
8855 process_init_element (pop_init_level (1, braced_init_obstack),
8856 true, braced_init_obstack);
8b6a5902 8857 }
3e4093b6 8858
db3927fb
AH
8859 p->index = size_binop_loc (input_location,
8860 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
8861 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8862 finish = 1;
8863
8864 while (1)
8865 {
8866 constructor_index = p->index;
8867 constructor_fields = p->fields;
8868 if (finish && p->range_end && p->index == p->range_start)
8869 {
8870 finish = 0;
8871 p->prev = 0;
8872 }
8873 p = p->next;
8874 if (!p)
8875 break;
a1e3b3d9 8876 push_init_level (2, braced_init_obstack);
3e4093b6
RS
8877 p->stack = constructor_stack;
8878 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8879 p->index = p->range_start;
8880 }
8881
8882 if (!finish)
8883 constructor_range_stack = range_stack;
8884 continue;
8b6a5902
JJ
8885 }
8886
3e4093b6 8887 break;
8b6a5902
JJ
8888 }
8889
3e4093b6
RS
8890 constructor_range_stack = 0;
8891}
8892\f
9f0e2d86
ZW
8893/* Build a complete asm-statement, whose components are a CV_QUALIFIER
8894 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 8895 an ASM_EXPR node). */
3e4093b6 8896tree
9f0e2d86 8897build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 8898{
6de9cd9a
DN
8899 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8900 ASM_VOLATILE_P (args) = 1;
9f0e2d86 8901 return add_stmt (args);
8b6a5902
JJ
8902}
8903
9f0e2d86
ZW
8904/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8905 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8906 SIMPLE indicates whether there was anything at all after the
8907 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 8908 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 8909tree
c2255bc4 8910build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 8911 tree clobbers, tree labels, bool simple)
e5e809f4 8912{
3e4093b6 8913 tree tail;
9f0e2d86 8914 tree args;
6de9cd9a
DN
8915 int i;
8916 const char *constraint;
74f0c611 8917 const char **oconstraints;
6de9cd9a 8918 bool allows_mem, allows_reg, is_inout;
74f0c611 8919 int ninputs, noutputs;
6de9cd9a
DN
8920
8921 ninputs = list_length (inputs);
8922 noutputs = list_length (outputs);
74f0c611
RH
8923 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8924
1c384bf1 8925 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 8926
6de9cd9a
DN
8927 /* Remove output conversions that change the type but not the mode. */
8928 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 8929 {
3e4093b6 8930 tree output = TREE_VALUE (tail);
74f0c611 8931
eadd3d0d
JJ
8932 output = c_fully_fold (output, false, NULL);
8933
74f0c611
RH
8934 /* ??? Really, this should not be here. Users should be using a
8935 proper lvalue, dammit. But there's a long history of using casts
8936 in the output operands. In cases like longlong.h, this becomes a
8937 primitive form of typechecking -- if the cast can be removed, then
8938 the output operand had a type of the proper width; otherwise we'll
8939 get an error. Gross, but ... */
3e4093b6 8940 STRIP_NOPS (output);
74f0c611 8941
7bd11157 8942 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 8943 output = error_mark_node;
8b6a5902 8944
5544530a
PB
8945 if (output != error_mark_node
8946 && (TREE_READONLY (output)
8947 || TYPE_READONLY (TREE_TYPE (output))
8948 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8949 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8950 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8951 readonly_error (output, lv_asm);
8952
6de9cd9a 8953 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
8954 oconstraints[i] = constraint;
8955
8956 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8957 &allows_mem, &allows_reg, &is_inout))
8958 {
8959 /* If the operand is going to end up in memory,
8960 mark it addressable. */
8961 if (!allows_reg && !c_mark_addressable (output))
8962 output = error_mark_node;
bae5cddf
JJ
8963 if (!(!allows_reg && allows_mem)
8964 && output != error_mark_node
8965 && VOID_TYPE_P (TREE_TYPE (output)))
8966 {
8967 error_at (loc, "invalid use of void expression");
8968 output = error_mark_node;
8969 }
74f0c611
RH
8970 }
8971 else
c22cacf3 8972 output = error_mark_node;
3e4093b6 8973
74f0c611 8974 TREE_VALUE (tail) = output;
8b6a5902 8975 }
3e4093b6 8976
74f0c611
RH
8977 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8978 {
8979 tree input;
8980
8981 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8982 input = TREE_VALUE (tail);
8983
74f0c611
RH
8984 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8985 oconstraints, &allows_mem, &allows_reg))
8986 {
8987 /* If the operand is going to end up in memory,
8988 mark it addressable. */
b4c33883
AP
8989 if (!allows_reg && allows_mem)
8990 {
eadd3d0d
JJ
8991 input = c_fully_fold (input, false, NULL);
8992
b4c33883
AP
8993 /* Strip the nops as we allow this case. FIXME, this really
8994 should be rejected or made deprecated. */
8995 STRIP_NOPS (input);
8996 if (!c_mark_addressable (input))
8997 input = error_mark_node;
bae5cddf 8998 }
eadd3d0d 8999 else
bae5cddf 9000 {
eadd3d0d
JJ
9001 struct c_expr expr;
9002 memset (&expr, 0, sizeof (expr));
9003 expr.value = input;
267bac10 9004 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9005 input = c_fully_fold (expr.value, false, NULL);
9006
9007 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9008 {
9009 error_at (loc, "invalid use of void expression");
9010 input = error_mark_node;
9011 }
bae5cddf 9012 }
74f0c611
RH
9013 }
9014 else
9015 input = error_mark_node;
9016
9017 TREE_VALUE (tail) = input;
9018 }
3e4093b6 9019
1c384bf1
RH
9020 /* ASMs with labels cannot have outputs. This should have been
9021 enforced by the parser. */
9022 gcc_assert (outputs == NULL || labels == NULL);
9023
9024 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9025
5544530a
PB
9026 /* asm statements without outputs, including simple ones, are treated
9027 as volatile. */
9028 ASM_INPUT_P (args) = simple;
9029 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9030
9f0e2d86 9031 return args;
e5e809f4 9032}
3e4093b6 9033\f
c2255bc4
AH
9034/* Generate a goto statement to LABEL. LOC is the location of the
9035 GOTO. */
506e2710
RH
9036
9037tree
c2255bc4 9038c_finish_goto_label (location_t loc, tree label)
506e2710 9039{
e1b7793c 9040 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9041 if (!decl)
9042 return NULL_TREE;
506e2710 9043 TREE_USED (decl) = 1;
c2255bc4
AH
9044 {
9045 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9046 SET_EXPR_LOCATION (t, loc);
9047 return add_stmt (t);
9048 }
506e2710
RH
9049}
9050
c2255bc4
AH
9051/* Generate a computed goto statement to EXPR. LOC is the location of
9052 the GOTO. */
506e2710
RH
9053
9054tree
c2255bc4 9055c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9056{
c2255bc4 9057 tree t;
c1771a20 9058 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9059 expr = c_fully_fold (expr, false, NULL);
506e2710 9060 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9061 t = build1 (GOTO_EXPR, void_type_node, expr);
9062 SET_EXPR_LOCATION (t, loc);
9063 return add_stmt (t);
506e2710
RH
9064}
9065
5088b058 9066/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4
AH
9067 to return, or a null pointer for `return;' with no value. LOC is
9068 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
9069 is the original type of RETVAL. */
de520661 9070
506e2710 9071tree
c2255bc4 9072c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9073{
0c9b182b
JJ
9074 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9075 bool no_warning = false;
928c19bb 9076 bool npc = false;
36536d79 9077 size_t rank = 0;
3e4093b6
RS
9078
9079 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9080 warning_at (loc, 0,
9081 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9082
36536d79
BI
9083 if (flag_enable_cilkplus && contains_array_notation_expr (retval))
9084 {
9085 /* Array notations are allowed in a return statement if it is inside a
9086 built-in array notation reduction function. */
9087 if (!find_rank (loc, retval, retval, false, &rank))
9088 return error_mark_node;
9089 if (rank >= 1)
9090 {
9091 error_at (loc, "array notation expression cannot be used as a "
9092 "return value");
9093 return error_mark_node;
9094 }
9095 }
939b37da
BI
9096 if (flag_enable_cilkplus && retval && TREE_CODE (retval) == CILK_SPAWN_STMT)
9097 {
9098 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9099 "allowed");
9100 return error_mark_node;
9101 }
928c19bb
JM
9102 if (retval)
9103 {
8ce94e44 9104 tree semantic_type = NULL_TREE;
928c19bb 9105 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9106 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9107 {
9108 semantic_type = TREE_TYPE (retval);
9109 retval = TREE_OPERAND (retval, 0);
9110 }
928c19bb 9111 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9112 if (semantic_type)
9113 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9114 }
9115
3e4093b6 9116 if (!retval)
de520661 9117 {
3e4093b6
RS
9118 current_function_returns_null = 1;
9119 if ((warn_return_type || flag_isoc99)
9120 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9121 {
b8698a0f 9122 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
fcf73884 9123 "%<return%> with no value, in "
0c9b182b
JJ
9124 "function returning non-void");
9125 no_warning = true;
9126 }
400fbf9f 9127 }
3e4093b6 9128 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9129 {
3e4093b6 9130 current_function_returns_null = 1;
2397c575 9131 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9132 pedwarn (loc, 0,
509c9d60 9133 "%<return%> with a value, in function returning void");
b8698a0f 9134 else
c1771a20 9135 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9136 "%<return%> with expression, in function returning void");
de520661 9137 }
3e4093b6 9138 else
de520661 9139 {
c2255bc4
AH
9140 tree t = convert_for_assignment (loc, valtype, retval, origtype,
9141 ic_return,
9142 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9143 tree res = DECL_RESULT (current_function_decl);
9144 tree inner;
9feb29df 9145 bool save;
3e4093b6
RS
9146
9147 current_function_returns_value = 1;
9148 if (t == error_mark_node)
506e2710 9149 return NULL_TREE;
3e4093b6 9150
9feb29df
JJ
9151 save = in_late_binary_op;
9152 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9153 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9154 in_late_binary_op = true;
3e4093b6 9155 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9156 in_late_binary_op = save;
3e4093b6
RS
9157
9158 /* Strip any conversions, additions, and subtractions, and see if
9159 we are returning the address of a local variable. Warn if so. */
9160 while (1)
8b6a5902 9161 {
3e4093b6 9162 switch (TREE_CODE (inner))
8b6a5902 9163 {
849421a3
JJ
9164 CASE_CONVERT:
9165 case NON_LVALUE_EXPR:
3e4093b6 9166 case PLUS_EXPR:
849421a3 9167 case POINTER_PLUS_EXPR:
3e4093b6
RS
9168 inner = TREE_OPERAND (inner, 0);
9169 continue;
9170
9171 case MINUS_EXPR:
9172 /* If the second operand of the MINUS_EXPR has a pointer
9173 type (or is converted from it), this may be valid, so
9174 don't give a warning. */
9175 {
9176 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9177
3f75a254 9178 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9179 && (CONVERT_EXPR_P (op1)
9180 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9181 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9182
3e4093b6
RS
9183 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9184 break;
8b6a5902 9185
3e4093b6
RS
9186 inner = TREE_OPERAND (inner, 0);
9187 continue;
9188 }
400fbf9f 9189
3e4093b6
RS
9190 case ADDR_EXPR:
9191 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9192
6615c446 9193 while (REFERENCE_CLASS_P (inner)
c22cacf3 9194 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 9195 inner = TREE_OPERAND (inner, 0);
8b6a5902 9196
a2f1f4c3 9197 if (DECL_P (inner)
3f75a254
JM
9198 && !DECL_EXTERNAL (inner)
9199 && !TREE_STATIC (inner)
3e4093b6 9200 && DECL_CONTEXT (inner) == current_function_decl)
c2255bc4 9201 warning_at (loc,
880661a4
JW
9202 OPT_Wreturn_local_addr, "function returns address "
9203 "of local variable");
3e4093b6 9204 break;
8b6a5902 9205
3e4093b6
RS
9206 default:
9207 break;
9208 }
de520661 9209
3e4093b6
RS
9210 break;
9211 }
9212
53fb4de3 9213 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9214 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9215
9216 if (warn_sequence_point)
9217 verify_sequence_points (retval);
de520661 9218 }
8b6a5902 9219
c2255bc4 9220 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9221 TREE_NO_WARNING (ret_stmt) |= no_warning;
9222 return add_stmt (ret_stmt);
de520661 9223}
3e4093b6
RS
9224\f
9225struct c_switch {
604f5adf
ILT
9226 /* The SWITCH_EXPR being built. */
9227 tree switch_expr;
a6c0a76c 9228
89dbed81 9229 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9230 default conversion is applied. */
9231 tree orig_type;
9232
3e4093b6
RS
9233 /* A splay-tree mapping the low element of a case range to the high
9234 element, or NULL_TREE if there is no high element. Used to
9235 determine whether or not a new case label duplicates an old case
9236 label. We need a tree, rather than simply a hash table, because
9237 of the GNU case range extension. */
9238 splay_tree cases;
a6c0a76c 9239
e1b7793c
ILT
9240 /* The bindings at the point of the switch. This is used for
9241 warnings crossing decls when branching to a case label. */
9242 struct c_spot_bindings *bindings;
187230a7 9243
3e4093b6
RS
9244 /* The next node on the stack. */
9245 struct c_switch *next;
9246};
400fbf9f 9247
3e4093b6
RS
9248/* A stack of the currently active switch statements. The innermost
9249 switch statement is on the top of the stack. There is no need to
9250 mark the stack for garbage collection because it is only active
9251 during the processing of the body of a function, and we never
9252 collect at that point. */
de520661 9253
506e2710 9254struct c_switch *c_switch_stack;
de520661 9255
3e4093b6 9256/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4
AH
9257 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9258 SWITCH_COND_LOC is the location of the switch's condition. */
de520661 9259
3e4093b6 9260tree
c2255bc4
AH
9261c_start_case (location_t switch_loc,
9262 location_t switch_cond_loc,
9263 tree exp)
de520661 9264{
c58e8676 9265 tree orig_type = error_mark_node;
3e4093b6 9266 struct c_switch *cs;
2f6e4e97 9267
3e4093b6 9268 if (exp != error_mark_node)
de520661 9269 {
3e4093b6
RS
9270 orig_type = TREE_TYPE (exp);
9271
c58e8676 9272 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9273 {
c58e8676
VR
9274 if (orig_type != error_mark_node)
9275 {
c2255bc4 9276 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9277 orig_type = error_mark_node;
9278 }
3e4093b6 9279 exp = integer_zero_node;
de520661 9280 }
3e4093b6 9281 else
de520661 9282 {
c58e8676 9283 tree type = TYPE_MAIN_VARIANT (orig_type);
8b6a5902 9284
3176a0c2 9285 if (!in_system_header
3e4093b6
RS
9286 && (type == long_integer_type_node
9287 || type == long_unsigned_type_node))
c2255bc4
AH
9288 warning_at (switch_cond_loc,
9289 OPT_Wtraditional, "%<long%> switch expression not "
9290 "converted to %<int%> in ISO C");
8b6a5902 9291
928c19bb 9292 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9293 exp = default_conversion (exp);
ca085fd7
MLI
9294
9295 if (warn_sequence_point)
9296 verify_sequence_points (exp);
3e4093b6
RS
9297 }
9298 }
9299
604f5adf 9300 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9301 cs = XNEW (struct c_switch);
604f5adf 9302 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9303 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9304 cs->orig_type = orig_type;
3e4093b6 9305 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9306 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9307 cs->next = c_switch_stack;
9308 c_switch_stack = cs;
3e4093b6 9309
604f5adf 9310 return add_stmt (cs->switch_expr);
3e4093b6
RS
9311}
9312
c2255bc4 9313/* Process a case label at location LOC. */
3e4093b6
RS
9314
9315tree
c2255bc4 9316do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9317{
9318 tree label = NULL_TREE;
9319
17cede2e
JM
9320 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9321 {
9322 low_value = c_fully_fold (low_value, false, NULL);
9323 if (TREE_CODE (low_value) == INTEGER_CST)
c1771a20 9324 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9325 "case label is not an integer constant expression");
9326 }
9327
9328 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9329 {
9330 high_value = c_fully_fold (high_value, false, NULL);
9331 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9332 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9333 "case label is not an integer constant expression");
9334 }
9335
e1b7793c 9336 if (c_switch_stack == NULL)
187230a7
JM
9337 {
9338 if (low_value)
e1b7793c 9339 error_at (loc, "case label not within a switch statement");
187230a7 9340 else
e1b7793c
ILT
9341 error_at (loc, "%<default%> label not within a switch statement");
9342 return NULL_TREE;
187230a7 9343 }
de520661 9344
e1b7793c
ILT
9345 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9346 EXPR_LOCATION (c_switch_stack->switch_expr),
9347 loc))
9348 return NULL_TREE;
9349
9350 label = c_add_case_label (loc, c_switch_stack->cases,
9351 SWITCH_COND (c_switch_stack->switch_expr),
9352 c_switch_stack->orig_type,
9353 low_value, high_value);
9354 if (label == error_mark_node)
9355 label = NULL_TREE;
3e4093b6
RS
9356 return label;
9357}
de520661 9358
3e4093b6 9359/* Finish the switch statement. */
de520661 9360
3e4093b6 9361void
325c3691 9362c_finish_case (tree body)
3e4093b6 9363{
506e2710 9364 struct c_switch *cs = c_switch_stack;
fbc315db 9365 location_t switch_location;
3e4093b6 9366
604f5adf 9367 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9368
6de9cd9a 9369 /* Emit warnings as needed. */
c2255bc4 9370 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db
ILT
9371 c_do_switch_warnings (cs->cases, switch_location,
9372 TREE_TYPE (cs->switch_expr),
9373 SWITCH_COND (cs->switch_expr));
6de9cd9a 9374
3e4093b6 9375 /* Pop the stack. */
506e2710 9376 c_switch_stack = cs->next;
3e4093b6 9377 splay_tree_delete (cs->cases);
e1b7793c 9378 c_release_switch_bindings (cs->bindings);
5d038c4c 9379 XDELETE (cs);
de520661 9380}
325c3691 9381\f
506e2710
RH
9382/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9383 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9384 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9385 statement, and was not surrounded with parenthesis. */
325c3691 9386
9e51cf9d 9387void
506e2710
RH
9388c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9389 tree else_block, bool nested_if)
325c3691 9390{
506e2710 9391 tree stmt;
325c3691 9392
25c22937
BI
9393 /* If the condition has array notations, then the rank of the then_block and
9394 else_block must be either 0 or be equal to the rank of the condition. If
9395 the condition does not have array notations then break them up as it is
9396 broken up in a normal expression. */
9397 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
9398 {
9399 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9400 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9401 return;
9402 if (then_block
9403 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9404 return;
9405 if (else_block
9406 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9407 return;
9408 if (cond_rank != then_rank && then_rank != 0)
9409 {
9410 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9411 " and the then-block");
9412 return;
9413 }
9414 else if (cond_rank != else_rank && else_rank != 0)
9415 {
9416 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9417 " and the else-block");
9418 return;
9419 }
9420 }
506e2710
RH
9421 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9422 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9423 {
506e2710 9424 tree inner_if = then_block;
16865eaa 9425
61ada8ae 9426 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9427 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9428 it might not be exactly THEN_BLOCK, but should be the last
9429 non-container statement within. */
9430 while (1)
9431 switch (TREE_CODE (inner_if))
9432 {
9433 case COND_EXPR:
9434 goto found;
9435 case BIND_EXPR:
9436 inner_if = BIND_EXPR_BODY (inner_if);
9437 break;
9438 case STATEMENT_LIST:
9439 inner_if = expr_last (then_block);
9440 break;
9441 case TRY_FINALLY_EXPR:
9442 case TRY_CATCH_EXPR:
9443 inner_if = TREE_OPERAND (inner_if, 0);
9444 break;
9445 default:
366de0ce 9446 gcc_unreachable ();
506e2710
RH
9447 }
9448 found:
16865eaa 9449
506e2710 9450 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9451 warning_at (if_locus, OPT_Wparentheses,
9452 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9453 }
16865eaa 9454
2214de30 9455 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9456 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9457 add_stmt (stmt);
325c3691
RH
9458}
9459
506e2710
RH
9460/* Emit a general-purpose loop construct. START_LOCUS is the location of
9461 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9462 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9463 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9464 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9465
9466void
506e2710
RH
9467c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9468 tree blab, tree clab, bool cond_is_first)
325c3691 9469{
506e2710
RH
9470 tree entry = NULL, exit = NULL, t;
9471
36536d79
BI
9472 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
9473 {
9474 error_at (start_locus, "array notation expression cannot be used in a "
9475 "loop%'s condition");
9476 return;
9477 }
9478
28af952a
RS
9479 /* If the condition is zero don't generate a loop construct. */
9480 if (cond && integer_zerop (cond))
9481 {
9482 if (cond_is_first)
9483 {
9484 t = build_and_jump (&blab);
9485 SET_EXPR_LOCATION (t, start_locus);
9486 add_stmt (t);
9487 }
9488 }
9489 else
506e2710
RH
9490 {
9491 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9492
506e2710 9493 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9494 out of the loop, or to the top of it. If there's no exit condition,
9495 then we just build a jump back to the top. */
506e2710 9496 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9497
28af952a 9498 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9499 {
9500 /* Canonicalize the loop condition to the end. This means
9501 generating a branch to the loop condition. Reuse the
9502 continue label, if possible. */
9503 if (cond_is_first)
9504 {
9505 if (incr || !clab)
9506 {
9507 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9508 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9509 }
9510 else
9511 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9512 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9513 add_stmt (t);
9514 }
9515
506e2710 9516 t = build_and_jump (&blab);
506e2710 9517 if (cond_is_first)
db3927fb
AH
9518 exit = fold_build3_loc (start_locus,
9519 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9520 else
db3927fb
AH
9521 exit = fold_build3_loc (input_location,
9522 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9523 }
9524
506e2710
RH
9525 add_stmt (top);
9526 }
c22cacf3 9527
506e2710
RH
9528 if (body)
9529 add_stmt (body);
9530 if (clab)
9531 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9532 if (incr)
9533 add_stmt (incr);
9534 if (entry)
9535 add_stmt (entry);
9536 if (exit)
9537 add_stmt (exit);
9538 if (blab)
9539 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9540}
325c3691
RH
9541
9542tree
c2255bc4 9543c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9544{
089efaa4 9545 bool skip;
506e2710 9546 tree label = *label_p;
325c3691 9547
089efaa4
ILT
9548 /* In switch statements break is sometimes stylistically used after
9549 a return statement. This can lead to spurious warnings about
9550 control reaching the end of a non-void function when it is
9551 inlined. Note that we are calling block_may_fallthru with
9552 language specific tree nodes; this works because
9553 block_may_fallthru returns true when given something it does not
9554 understand. */
9555 skip = !block_may_fallthru (cur_stmt_list);
9556
506e2710 9557 if (!label)
089efaa4
ILT
9558 {
9559 if (!skip)
c2255bc4 9560 *label_p = label = create_artificial_label (loc);
089efaa4 9561 }
953ff289
DN
9562 else if (TREE_CODE (label) == LABEL_DECL)
9563 ;
9564 else switch (TREE_INT_CST_LOW (label))
506e2710 9565 {
953ff289 9566 case 0:
506e2710 9567 if (is_break)
c2255bc4 9568 error_at (loc, "break statement not within loop or switch");
506e2710 9569 else
c2255bc4 9570 error_at (loc, "continue statement not within a loop");
506e2710 9571 return NULL_TREE;
953ff289
DN
9572
9573 case 1:
9574 gcc_assert (is_break);
c2255bc4 9575 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9576 return NULL_TREE;
9577
9578 default:
9579 gcc_unreachable ();
506e2710 9580 }
325c3691 9581
089efaa4
ILT
9582 if (skip)
9583 return NULL_TREE;
9584
2e28e797
JH
9585 if (!is_break)
9586 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9587
53fb4de3 9588 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9589}
9590
506e2710 9591/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9592
9593static void
c2255bc4 9594emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9595{
e6b5a630
RH
9596 if (expr == error_mark_node)
9597 ;
9598 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9599 {
9600 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9601 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9602 }
27f33b15 9603 else
c2255bc4 9604 warn_if_unused_value (expr, loc);
3a5b9284
RH
9605}
9606
506e2710 9607/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9608 diagnostics, but do not call ADD_STMT. LOC is the location of the
9609 statement. */
3a5b9284 9610
506e2710 9611tree
c2255bc4 9612c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9613{
056928b2
JJ
9614 tree exprv;
9615
3a5b9284 9616 if (!expr)
506e2710 9617 return NULL_TREE;
3a5b9284 9618
928c19bb
JM
9619 expr = c_fully_fold (expr, false, NULL);
9620
3a5b9284
RH
9621 if (warn_sequence_point)
9622 verify_sequence_points (expr);
9623
9624 if (TREE_TYPE (expr) != error_mark_node
9625 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9626 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9627 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9628
9629 /* If we're not processing a statement expression, warn about unused values.
9630 Warnings for statement expressions will be emitted later, once we figure
9631 out which is the result. */
9632 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9633 && warn_unused_value)
c2255bc4 9634 emit_side_effect_warnings (loc, expr);
3a5b9284 9635
056928b2
JJ
9636 exprv = expr;
9637 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9638 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9639 while (CONVERT_EXPR_P (exprv))
9640 exprv = TREE_OPERAND (exprv, 0);
9641 if (DECL_P (exprv)
9642 || handled_component_p (exprv)
9643 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9644 mark_exp_read (exprv);
fa8351f8 9645
3a5b9284
RH
9646 /* If the expression is not of a type to which we cannot assign a line
9647 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9648 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9649 {
9650 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9651 SET_EXPR_LOCATION (expr, loc);
9652 }
506e2710
RH
9653
9654 return expr;
9655}
9656
c2255bc4
AH
9657/* Emit an expression as a statement. LOC is the location of the
9658 expression. */
506e2710
RH
9659
9660tree
c2255bc4 9661c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9662{
9663 if (expr)
c2255bc4 9664 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9665 else
9666 return NULL;
3a5b9284
RH
9667}
9668
9669/* Do the opposite and emit a statement as an expression. To begin,
9670 create a new binding level and return it. */
325c3691
RH
9671
9672tree
9673c_begin_stmt_expr (void)
9674{
9675 tree ret;
9676
9677 /* We must force a BLOCK for this level so that, if it is not expanded
9678 later, there is a way to turn off the entire subtree of blocks that
9679 are contained in it. */
9680 keep_next_level ();
9681 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
9682
9683 c_bindings_start_stmt_expr (c_switch_stack == NULL
9684 ? NULL
9685 : c_switch_stack->bindings);
325c3691
RH
9686
9687 /* Mark the current statement list as belonging to a statement list. */
9688 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9689
9690 return ret;
9691}
9692
c2255bc4
AH
9693/* LOC is the location of the compound statement to which this body
9694 belongs. */
9695
325c3691 9696tree
c2255bc4 9697c_finish_stmt_expr (location_t loc, tree body)
325c3691 9698{
3a5b9284 9699 tree last, type, tmp, val;
325c3691
RH
9700 tree *last_p;
9701
c2255bc4 9702 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
9703
9704 c_bindings_end_stmt_expr (c_switch_stack == NULL
9705 ? NULL
9706 : c_switch_stack->bindings);
325c3691 9707
3a5b9284
RH
9708 /* Locate the last statement in BODY. See c_end_compound_stmt
9709 about always returning a BIND_EXPR. */
9710 last_p = &BIND_EXPR_BODY (body);
9711 last = BIND_EXPR_BODY (body);
9712
9713 continue_searching:
325c3691
RH
9714 if (TREE_CODE (last) == STATEMENT_LIST)
9715 {
3a5b9284
RH
9716 tree_stmt_iterator i;
9717
9718 /* This can happen with degenerate cases like ({ }). No value. */
9719 if (!TREE_SIDE_EFFECTS (last))
9720 return body;
9721
9722 /* If we're supposed to generate side effects warnings, process
9723 all of the statements except the last. */
27f33b15 9724 if (warn_unused_value)
325c3691 9725 {
3a5b9284 9726 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
9727 {
9728 location_t tloc;
9729 tree t = tsi_stmt (i);
9730
9731 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9732 emit_side_effect_warnings (tloc, t);
9733 }
325c3691
RH
9734 }
9735 else
3a5b9284
RH
9736 i = tsi_last (last);
9737 last_p = tsi_stmt_ptr (i);
9738 last = *last_p;
325c3691
RH
9739 }
9740
3a5b9284
RH
9741 /* If the end of the list is exception related, then the list was split
9742 by a call to push_cleanup. Continue searching. */
9743 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9744 || TREE_CODE (last) == TRY_CATCH_EXPR)
9745 {
9746 last_p = &TREE_OPERAND (last, 0);
9747 last = *last_p;
9748 goto continue_searching;
9749 }
9750
26d8af35
JM
9751 if (last == error_mark_node)
9752 return last;
9753
3a5b9284
RH
9754 /* In the case that the BIND_EXPR is not necessary, return the
9755 expression out from inside it. */
26d8af35
JM
9756 if (last == BIND_EXPR_BODY (body)
9757 && BIND_EXPR_VARS (body) == NULL)
591baeb0 9758 {
928c19bb
JM
9759 /* Even if this looks constant, do not allow it in a constant
9760 expression. */
e5a94231 9761 last = c_wrap_maybe_const (last, true);
591baeb0
JM
9762 /* Do not warn if the return value of a statement expression is
9763 unused. */
928c19bb 9764 TREE_NO_WARNING (last) = 1;
591baeb0
JM
9765 return last;
9766 }
325c3691
RH
9767
9768 /* Extract the type of said expression. */
9769 type = TREE_TYPE (last);
325c3691 9770
3a5b9284
RH
9771 /* If we're not returning a value at all, then the BIND_EXPR that
9772 we already have is a fine expression to return. */
9773 if (!type || VOID_TYPE_P (type))
9774 return body;
9775
9776 /* Now that we've located the expression containing the value, it seems
9777 silly to make voidify_wrapper_expr repeat the process. Create a
9778 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9779 tmp = create_tmp_var_raw (type, NULL);
9780
9781 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9782 tree_expr_nonnegative_p giving up immediately. */
9783 val = last;
9784 if (TREE_CODE (val) == NOP_EXPR
9785 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9786 val = TREE_OPERAND (val, 0);
9787
53fb4de3 9788 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 9789 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 9790
c2255bc4
AH
9791 {
9792 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9793 SET_EXPR_LOCATION (t, loc);
9794 return t;
9795 }
325c3691
RH
9796}
9797\f
9798/* Begin and end compound statements. This is as simple as pushing
9799 and popping new statement lists from the tree. */
9800
9801tree
9802c_begin_compound_stmt (bool do_scope)
9803{
9804 tree stmt = push_stmt_list ();
9805 if (do_scope)
4dfa0342 9806 push_scope ();
325c3691
RH
9807 return stmt;
9808}
9809
c2255bc4
AH
9810/* End a compound statement. STMT is the statement. LOC is the
9811 location of the compound statement-- this is usually the location
9812 of the opening brace. */
9813
325c3691 9814tree
c2255bc4 9815c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
9816{
9817 tree block = NULL;
9818
9819 if (do_scope)
9820 {
9821 if (c_dialect_objc ())
9822 objc_clear_super_receiver ();
9823 block = pop_scope ();
9824 }
9825
9826 stmt = pop_stmt_list (stmt);
c2255bc4 9827 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
9828
9829 /* If this compound statement is nested immediately inside a statement
9830 expression, then force a BIND_EXPR to be created. Otherwise we'll
9831 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9832 STATEMENT_LISTs merge, and thus we can lose track of what statement
9833 was really last. */
38e01f9e 9834 if (building_stmt_list_p ()
325c3691
RH
9835 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9836 && TREE_CODE (stmt) != BIND_EXPR)
9837 {
53fb4de3 9838 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 9839 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 9840 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
9841 }
9842
9843 return stmt;
9844}
5a508662
RH
9845
9846/* Queue a cleanup. CLEANUP is an expression/statement to be executed
9847 when the current scope is exited. EH_ONLY is true when this is not
9848 meant to apply to normal control flow transfer. */
9849
9850void
c2255bc4 9851push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 9852{
3a5b9284
RH
9853 enum tree_code code;
9854 tree stmt, list;
9855 bool stmt_expr;
9856
9857 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 9858 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 9859 add_stmt (stmt);
3a5b9284
RH
9860 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9861 list = push_stmt_list ();
9862 TREE_OPERAND (stmt, 0) = list;
9863 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 9864}
325c3691 9865\f
3e4093b6
RS
9866/* Build a binary-operation expression without default conversions.
9867 CODE is the kind of expression to build.
ba47d38d 9868 LOCATION is the operator's location.
3e4093b6
RS
9869 This function differs from `build' in several ways:
9870 the data type of the result is computed and recorded in it,
9871 warnings are generated if arg data types are invalid,
9872 special handling for addition and subtraction of pointers is known,
9873 and some optimization is done (operations on narrow ints
9874 are done in the narrower type when that gives the same result).
9875 Constant folding is also done before the result is returned.
de520661 9876
3e4093b6
RS
9877 Note that the operands will never have enumeral types, or function
9878 or array types, because either they will have the default conversions
9879 performed or they have both just been converted to some other type in which
9880 the arithmetic is to be done. */
9881
9882tree
ba47d38d
AH
9883build_binary_op (location_t location, enum tree_code code,
9884 tree orig_op0, tree orig_op1, int convert_p)
de520661 9885{
8ce94e44
JM
9886 tree type0, type1, orig_type0, orig_type1;
9887 tree eptype;
3e4093b6
RS
9888 enum tree_code code0, code1;
9889 tree op0, op1;
c9f9eb5d 9890 tree ret = error_mark_node;
4de67c26 9891 const char *invalid_op_diag;
4d84fe7c 9892 bool op0_int_operands, op1_int_operands;
928c19bb 9893 bool int_const, int_const_or_overflow, int_operands;
b62acd60 9894
3e4093b6
RS
9895 /* Expression code to give to the expression when it is built.
9896 Normally this is CODE, which is what the caller asked for,
9897 but in some special cases we change it. */
9898 enum tree_code resultcode = code;
8b6a5902 9899
3e4093b6
RS
9900 /* Data type in which the computation is to be performed.
9901 In the simplest cases this is the common type of the arguments. */
9902 tree result_type = NULL;
9903
8ce94e44
JM
9904 /* When the computation is in excess precision, the type of the
9905 final EXCESS_PRECISION_EXPR. */
2d2e923f 9906 tree semantic_result_type = NULL;
8ce94e44 9907
3e4093b6
RS
9908 /* Nonzero means operands have already been type-converted
9909 in whatever way is necessary.
9910 Zero means they need to be converted to RESULT_TYPE. */
9911 int converted = 0;
9912
9913 /* Nonzero means create the expression with this type, rather than
9914 RESULT_TYPE. */
9915 tree build_type = 0;
9916
9917 /* Nonzero means after finally constructing the expression
9918 convert it to this type. */
9919 tree final_type = 0;
9920
9921 /* Nonzero if this is an operation like MIN or MAX which can
9922 safely be computed in short if both args are promoted shorts.
9923 Also implies COMMON.
9924 -1 indicates a bitwise operation; this makes a difference
9925 in the exact conditions for when it is safe to do the operation
9926 in a narrower mode. */
9927 int shorten = 0;
9928
9929 /* Nonzero if this is a comparison operation;
9930 if both args are promoted shorts, compare the original shorts.
9931 Also implies COMMON. */
9932 int short_compare = 0;
9933
9934 /* Nonzero if this is a right-shift operation, which can be computed on the
9935 original short and then promoted if the operand is a promoted short. */
9936 int short_shift = 0;
9937
9938 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9939 int common = 0;
9940
58393038
ZL
9941 /* True means types are compatible as far as ObjC is concerned. */
9942 bool objc_ok;
9943
8ce94e44
JM
9944 /* True means this is an arithmetic operation that may need excess
9945 precision. */
9946 bool may_need_excess_precision;
9947
180f8dbb
JM
9948 /* True means this is a boolean operation that converts both its
9949 operands to truth-values. */
9950 bool boolean_op = false;
9951
de5a5fa1
MP
9952 /* Remember whether we're doing / or %. */
9953 bool doing_div_or_mod = false;
9954
9955 /* Remember whether we're doing << or >>. */
9956 bool doing_shift = false;
9957
9958 /* Tree holding instrumentation expression. */
9959 tree instrument_expr = NULL;
9960
ba47d38d
AH
9961 if (location == UNKNOWN_LOCATION)
9962 location = input_location;
9963
4d84fe7c
JM
9964 op0 = orig_op0;
9965 op1 = orig_op1;
9966
9967 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9968 if (op0_int_operands)
9969 op0 = remove_c_maybe_const_expr (op0);
9970 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9971 if (op1_int_operands)
9972 op1 = remove_c_maybe_const_expr (op1);
9973 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
9974 if (int_operands)
9975 {
9976 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9977 && TREE_CODE (orig_op1) == INTEGER_CST);
9978 int_const = (int_const_or_overflow
9979 && !TREE_OVERFLOW (orig_op0)
9980 && !TREE_OVERFLOW (orig_op1));
9981 }
9982 else
9983 int_const = int_const_or_overflow = false;
9984
0e3a99ae 9985 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
9986 if (convert_p
9987 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 9988 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 9989 {
4d84fe7c
JM
9990 op0 = default_conversion (op0);
9991 op1 = default_conversion (op1);
790e9490
RS
9992 }
9993
36536d79
BI
9994 /* When Cilk Plus is enabled and there are array notations inside op0, then
9995 we check to see if there are builtin array notation functions. If
9996 so, then we take on the type of the array notation inside it. */
9997 if (flag_enable_cilkplus && contains_array_notation_expr (op0))
9998 orig_type0 = type0 = find_correct_array_notation_type (op0);
9999 else
10000 orig_type0 = type0 = TREE_TYPE (op0);
10001
10002 if (flag_enable_cilkplus && contains_array_notation_expr (op1))
10003 orig_type1 = type1 = find_correct_array_notation_type (op1);
10004 else
10005 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10006
3e4093b6
RS
10007 /* The expression codes of the data types of the arguments tell us
10008 whether the arguments are integers, floating, pointers, etc. */
10009 code0 = TREE_CODE (type0);
10010 code1 = TREE_CODE (type1);
10011
10012 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10013 STRIP_TYPE_NOPS (op0);
10014 STRIP_TYPE_NOPS (op1);
10015
10016 /* If an error was already reported for one of the arguments,
10017 avoid reporting another error. */
10018
10019 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10020 return error_mark_node;
10021
4de67c26
JM
10022 if ((invalid_op_diag
10023 = targetm.invalid_binary_op (code, type0, type1)))
10024 {
ba47d38d 10025 error_at (location, invalid_op_diag);
4de67c26
JM
10026 return error_mark_node;
10027 }
10028
8ce94e44
JM
10029 switch (code)
10030 {
10031 case PLUS_EXPR:
10032 case MINUS_EXPR:
10033 case MULT_EXPR:
10034 case TRUNC_DIV_EXPR:
10035 case CEIL_DIV_EXPR:
10036 case FLOOR_DIV_EXPR:
10037 case ROUND_DIV_EXPR:
10038 case EXACT_DIV_EXPR:
10039 may_need_excess_precision = true;
10040 break;
10041 default:
10042 may_need_excess_precision = false;
10043 break;
10044 }
10045 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10046 {
10047 op0 = TREE_OPERAND (op0, 0);
10048 type0 = TREE_TYPE (op0);
10049 }
10050 else if (may_need_excess_precision
10051 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10052 {
10053 type0 = eptype;
10054 op0 = convert (eptype, op0);
10055 }
10056 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10057 {
10058 op1 = TREE_OPERAND (op1, 0);
10059 type1 = TREE_TYPE (op1);
10060 }
10061 else if (may_need_excess_precision
10062 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10063 {
10064 type1 = eptype;
10065 op1 = convert (eptype, op1);
10066 }
10067
58393038
ZL
10068 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10069
0e3a99ae
AS
10070 /* In case when one of the operands of the binary operation is
10071 a vector and another is a scalar -- convert scalar to vector. */
10072 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10073 {
a212e43f
MG
10074 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10075 true);
f90e8e2e 10076
0e3a99ae
AS
10077 switch (convert_flag)
10078 {
10079 case stv_error:
10080 return error_mark_node;
10081 case stv_firstarg:
10082 {
10083 bool maybe_const = true;
10084 tree sc;
10085 sc = c_fully_fold (op0, false, &maybe_const);
10086 sc = save_expr (sc);
10087 sc = convert (TREE_TYPE (type1), sc);
10088 op0 = build_vector_from_val (type1, sc);
10089 if (!maybe_const)
10090 op0 = c_wrap_maybe_const (op0, true);
10091 orig_type0 = type0 = TREE_TYPE (op0);
10092 code0 = TREE_CODE (type0);
10093 converted = 1;
10094 break;
10095 }
10096 case stv_secondarg:
10097 {
10098 bool maybe_const = true;
10099 tree sc;
10100 sc = c_fully_fold (op1, false, &maybe_const);
10101 sc = save_expr (sc);
10102 sc = convert (TREE_TYPE (type0), sc);
10103 op1 = build_vector_from_val (type0, sc);
10104 if (!maybe_const)
54b9f838 10105 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10106 orig_type1 = type1 = TREE_TYPE (op1);
10107 code1 = TREE_CODE (type1);
10108 converted = 1;
10109 break;
10110 }
10111 default:
10112 break;
10113 }
10114 }
10115
3e4093b6 10116 switch (code)
de520661 10117 {
3e4093b6
RS
10118 case PLUS_EXPR:
10119 /* Handle the pointer + int case. */
10120 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10121 {
db3927fb 10122 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10123 goto return_build_binary_op;
10124 }
3e4093b6 10125 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10126 {
db3927fb 10127 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10128 goto return_build_binary_op;
10129 }
fe67cf58 10130 else
3e4093b6
RS
10131 common = 1;
10132 break;
400fbf9f 10133
3e4093b6
RS
10134 case MINUS_EXPR:
10135 /* Subtraction of two similar pointers.
10136 We must subtract them as integers, then divide by object size. */
10137 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10138 && comp_target_types (location, type0, type1))
c9f9eb5d 10139 {
db3927fb 10140 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10141 goto return_build_binary_op;
10142 }
3e4093b6
RS
10143 /* Handle pointer minus int. Just like pointer plus int. */
10144 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10145 {
db3927fb 10146 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10147 goto return_build_binary_op;
10148 }
3e4093b6
RS
10149 else
10150 common = 1;
10151 break;
8b6a5902 10152
3e4093b6
RS
10153 case MULT_EXPR:
10154 common = 1;
10155 break;
10156
10157 case TRUNC_DIV_EXPR:
10158 case CEIL_DIV_EXPR:
10159 case FLOOR_DIV_EXPR:
10160 case ROUND_DIV_EXPR:
10161 case EXACT_DIV_EXPR:
de5a5fa1 10162 doing_div_or_mod = true;
c9f9eb5d 10163 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10164
10165 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10166 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10167 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10168 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10169 || code1 == FIXED_POINT_TYPE
3e4093b6 10170 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10171 {
5bed876a
AH
10172 enum tree_code tcode0 = code0, tcode1 = code1;
10173
3a021db2 10174 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10175 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10176 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10177 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10178
ab22c1fa
CF
10179 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10180 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10181 resultcode = RDIV_EXPR;
10182 else
10183 /* Although it would be tempting to shorten always here, that
10184 loses on some targets, since the modulo instruction is
10185 undefined if the quotient can't be represented in the
10186 computation mode. We shorten only if unsigned or if
10187 dividing by something we know != -1. */
8df83eae 10188 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10189 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10190 && !integer_all_onesp (op1)));
3e4093b6
RS
10191 common = 1;
10192 }
10193 break;
de520661 10194
3e4093b6 10195 case BIT_AND_EXPR:
3e4093b6
RS
10196 case BIT_IOR_EXPR:
10197 case BIT_XOR_EXPR:
10198 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10199 shorten = -1;
9ef0c8d9
AP
10200 /* Allow vector types which are not floating point types. */
10201 else if (code0 == VECTOR_TYPE
10202 && code1 == VECTOR_TYPE
10203 && !VECTOR_FLOAT_TYPE_P (type0)
10204 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10205 common = 1;
10206 break;
10207
10208 case TRUNC_MOD_EXPR:
10209 case FLOOR_MOD_EXPR:
de5a5fa1 10210 doing_div_or_mod = true;
c9f9eb5d 10211 warn_for_div_by_zero (location, op1);
de520661 10212
5cfd5d9b
AP
10213 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10214 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10215 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10216 common = 1;
10217 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10218 {
10219 /* Although it would be tempting to shorten always here, that loses
10220 on some targets, since the modulo instruction is undefined if the
10221 quotient can't be represented in the computation mode. We shorten
10222 only if unsigned or if dividing by something we know != -1. */
8df83eae 10223 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10224 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10225 && !integer_all_onesp (op1)));
3e4093b6
RS
10226 common = 1;
10227 }
10228 break;
de520661 10229
3e4093b6
RS
10230 case TRUTH_ANDIF_EXPR:
10231 case TRUTH_ORIF_EXPR:
10232 case TRUTH_AND_EXPR:
10233 case TRUTH_OR_EXPR:
10234 case TRUTH_XOR_EXPR:
10235 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10236 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10237 || code0 == FIXED_POINT_TYPE)
3e4093b6 10238 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10239 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10240 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10241 {
10242 /* Result of these operations is always an int,
10243 but that does not mean the operands should be
10244 converted to ints! */
10245 result_type = integer_type_node;
a27d595d
JM
10246 if (op0_int_operands)
10247 {
10248 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10249 op0 = remove_c_maybe_const_expr (op0);
10250 }
10251 else
10252 op0 = c_objc_common_truthvalue_conversion (location, op0);
10253 if (op1_int_operands)
10254 {
10255 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10256 op1 = remove_c_maybe_const_expr (op1);
10257 }
10258 else
10259 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10260 converted = 1;
180f8dbb 10261 boolean_op = true;
3e4093b6 10262 }
928c19bb
JM
10263 if (code == TRUTH_ANDIF_EXPR)
10264 {
10265 int_const_or_overflow = (int_operands
10266 && TREE_CODE (orig_op0) == INTEGER_CST
10267 && (op0 == truthvalue_false_node
10268 || TREE_CODE (orig_op1) == INTEGER_CST));
10269 int_const = (int_const_or_overflow
10270 && !TREE_OVERFLOW (orig_op0)
10271 && (op0 == truthvalue_false_node
10272 || !TREE_OVERFLOW (orig_op1)));
10273 }
10274 else if (code == TRUTH_ORIF_EXPR)
10275 {
10276 int_const_or_overflow = (int_operands
10277 && TREE_CODE (orig_op0) == INTEGER_CST
10278 && (op0 == truthvalue_true_node
10279 || TREE_CODE (orig_op1) == INTEGER_CST));
10280 int_const = (int_const_or_overflow
10281 && !TREE_OVERFLOW (orig_op0)
10282 && (op0 == truthvalue_true_node
10283 || !TREE_OVERFLOW (orig_op1)));
10284 }
3e4093b6 10285 break;
eba80994 10286
3e4093b6
RS
10287 /* Shift operations: result has same type as first operand;
10288 always convert second operand to int.
10289 Also set SHORT_SHIFT if shifting rightward. */
de520661 10290
3e4093b6 10291 case RSHIFT_EXPR:
f87bd04b
AS
10292 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10293 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10294 {
10295 result_type = type0;
10296 converted = 1;
10297 }
10298 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10299 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10300 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10301 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10302 {
10303 result_type = type0;
10304 converted = 1;
10305 }
10306 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10307 && code1 == INTEGER_TYPE)
3e4093b6 10308 {
de5a5fa1 10309 doing_shift = true;
928c19bb 10310 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10311 {
3e4093b6 10312 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10313 {
10314 int_const = false;
7d882b83 10315 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10316 warning (0, "right shift count is negative");
10317 }
3e4093b6 10318 else
bbb818c6 10319 {
3f75a254 10320 if (!integer_zerop (op1))
3e4093b6
RS
10321 short_shift = 1;
10322
10323 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10324 {
10325 int_const = false;
7d882b83 10326 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10327 warning (0, "right shift count >= width of type");
10328 }
bbb818c6 10329 }
b62acd60 10330 }
de520661 10331
3e4093b6
RS
10332 /* Use the type of the value to be shifted. */
10333 result_type = type0;
f87bd04b
AS
10334 /* Convert the non vector shift-count to an integer, regardless
10335 of size of value being shifted. */
10336 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10337 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10338 op1 = convert (integer_type_node, op1);
10339 /* Avoid converting op1 to result_type later. */
10340 converted = 1;
400fbf9f 10341 }
3e4093b6 10342 break;
253b6b82 10343
3e4093b6 10344 case LSHIFT_EXPR:
f87bd04b
AS
10345 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10346 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10347 {
10348 result_type = type0;
10349 converted = 1;
10350 }
10351 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10352 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10353 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10354 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10355 {
10356 result_type = type0;
10357 converted = 1;
10358 }
10359 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10360 && code1 == INTEGER_TYPE)
3e4093b6 10361 {
de5a5fa1 10362 doing_shift = true;
928c19bb 10363 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10364 {
3e4093b6 10365 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10366 {
10367 int_const = false;
7d882b83 10368 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10369 warning (0, "left shift count is negative");
10370 }
de520661 10371
3e4093b6 10372 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10373 {
10374 int_const = false;
7d882b83 10375 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10376 warning (0, "left shift count >= width of type");
10377 }
94ba5069 10378 }
de520661 10379
3e4093b6
RS
10380 /* Use the type of the value to be shifted. */
10381 result_type = type0;
f87bd04b
AS
10382 /* Convert the non vector shift-count to an integer, regardless
10383 of size of value being shifted. */
10384 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10385 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10386 op1 = convert (integer_type_node, op1);
10387 /* Avoid converting op1 to result_type later. */
10388 converted = 1;
400fbf9f 10389 }
3e4093b6 10390 break;
de520661 10391
3e4093b6
RS
10392 case EQ_EXPR:
10393 case NE_EXPR:
d246ab4f
AS
10394 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10395 {
10396 tree intt;
0af94e6f 10397 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10398 {
10399 error_at (location, "comparing vectors with different "
10400 "element types");
10401 return error_mark_node;
10402 }
10403
10404 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10405 {
10406 error_at (location, "comparing vectors with different "
10407 "number of elements");
10408 return error_mark_node;
10409 }
10410
10411 /* Always construct signed integer vector type. */
10412 intt = c_common_type_for_size (GET_MODE_BITSIZE
10413 (TYPE_MODE (TREE_TYPE (type0))), 0);
10414 result_type = build_opaque_vector_type (intt,
10415 TYPE_VECTOR_SUBPARTS (type0));
10416 converted = 1;
10417 break;
10418 }
ae311566 10419 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10420 warning_at (location,
10421 OPT_Wfloat_equal,
10422 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10423 /* Result of comparison is always int,
10424 but don't convert the args to int! */
10425 build_type = integer_type_node;
10426 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10427 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10428 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10429 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10430 short_compare = 1;
637f1455
SZ
10431 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10432 {
10433 if (TREE_CODE (op0) == ADDR_EXPR
10434 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10435 {
10436 if (code == EQ_EXPR)
10437 warning_at (location,
10438 OPT_Waddress,
10439 "the comparison will always evaluate as %<false%> "
10440 "for the address of %qD will never be NULL",
10441 TREE_OPERAND (op0, 0));
10442 else
10443 warning_at (location,
10444 OPT_Waddress,
10445 "the comparison will always evaluate as %<true%> "
10446 "for the address of %qD will never be NULL",
10447 TREE_OPERAND (op0, 0));
10448 }
10449 result_type = type0;
10450 }
10451 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10452 {
10453 if (TREE_CODE (op1) == ADDR_EXPR
10454 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10455 {
10456 if (code == EQ_EXPR)
10457 warning_at (location,
f90e8e2e 10458 OPT_Waddress,
637f1455
SZ
10459 "the comparison will always evaluate as %<false%> "
10460 "for the address of %qD will never be NULL",
10461 TREE_OPERAND (op1, 0));
10462 else
10463 warning_at (location,
10464 OPT_Waddress,
10465 "the comparison will always evaluate as %<true%> "
10466 "for the address of %qD will never be NULL",
10467 TREE_OPERAND (op1, 0));
10468 }
10469 result_type = type1;
10470 }
3e4093b6
RS
10471 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10472 {
10473 tree tt0 = TREE_TYPE (type0);
10474 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10475 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10476 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10477 addr_space_t as_common = ADDR_SPACE_GENERIC;
10478
3e4093b6
RS
10479 /* Anything compares with void *. void * compares with anything.
10480 Otherwise, the targets must be compatible
10481 and both must be object or both incomplete. */
744aa42f 10482 if (comp_target_types (location, type0, type1))
10bc1b1b 10483 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10484 else if (!addr_space_superset (as0, as1, &as_common))
10485 {
10486 error_at (location, "comparison of pointers to "
10487 "disjoint address spaces");
10488 return error_mark_node;
10489 }
267bac10 10490 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10491 {
36c5e70a 10492 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10493 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10494 "comparison of %<void *%> with function pointer");
ee2990e7 10495 }
267bac10 10496 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10497 {
36c5e70a 10498 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10499 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10500 "comparison of %<void *%> with function pointer");
e6834654 10501 }
3e4093b6 10502 else
58393038
ZL
10503 /* Avoid warning about the volatile ObjC EH puts on decls. */
10504 if (!objc_ok)
ba47d38d 10505 pedwarn (location, 0,
509c9d60 10506 "comparison of distinct pointer types lacks a cast");
e6834654 10507
3e4093b6 10508 if (result_type == NULL_TREE)
36c5e70a
BE
10509 {
10510 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10511 result_type = build_pointer_type
10512 (build_qualified_type (void_type_node, qual));
10513 }
e6834654 10514 }
3e4093b6 10515 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10516 {
3e4093b6 10517 result_type = type0;
ba47d38d 10518 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10519 }
3e4093b6 10520 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10521 {
3e4093b6 10522 result_type = type1;
ba47d38d 10523 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10524 }
3e4093b6 10525 break;
8b6a5902 10526
3e4093b6
RS
10527 case LE_EXPR:
10528 case GE_EXPR:
10529 case LT_EXPR:
10530 case GT_EXPR:
d246ab4f
AS
10531 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10532 {
10533 tree intt;
0af94e6f 10534 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10535 {
10536 error_at (location, "comparing vectors with different "
10537 "element types");
10538 return error_mark_node;
10539 }
10540
10541 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10542 {
10543 error_at (location, "comparing vectors with different "
10544 "number of elements");
10545 return error_mark_node;
10546 }
10547
10548 /* Always construct signed integer vector type. */
10549 intt = c_common_type_for_size (GET_MODE_BITSIZE
10550 (TYPE_MODE (TREE_TYPE (type0))), 0);
10551 result_type = build_opaque_vector_type (intt,
10552 TYPE_VECTOR_SUBPARTS (type0));
10553 converted = 1;
10554 break;
10555 }
3e4093b6 10556 build_type = integer_type_node;
ab22c1fa
CF
10557 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10558 || code0 == FIXED_POINT_TYPE)
10559 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10560 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10561 short_compare = 1;
10562 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10563 {
36c5e70a
BE
10564 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10565 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10566 addr_space_t as_common;
10567
744aa42f 10568 if (comp_target_types (location, type0, type1))
3e4093b6 10569 {
10bc1b1b 10570 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10571 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10572 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10573 pedwarn (location, 0,
509c9d60 10574 "comparison of complete and incomplete pointers");
fcf73884 10575 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10576 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10577 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10578 else if (null_pointer_constant_p (orig_op0)
10579 || null_pointer_constant_p (orig_op1))
10580 warning_at (location, OPT_Wextra,
10581 "ordered comparison of pointer with null pointer");
10582
3e4093b6 10583 }
36c5e70a
BE
10584 else if (!addr_space_superset (as0, as1, &as_common))
10585 {
10586 error_at (location, "comparison of pointers to "
10587 "disjoint address spaces");
10588 return error_mark_node;
10589 }
3e4093b6
RS
10590 else
10591 {
36c5e70a
BE
10592 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10593 result_type = build_pointer_type
10594 (build_qualified_type (void_type_node, qual));
ba47d38d 10595 pedwarn (location, 0,
509c9d60 10596 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10597 }
10598 }
6aa3c60d 10599 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10600 {
10601 result_type = type0;
fcf73884 10602 if (pedantic)
c1771a20 10603 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10604 "ordered comparison of pointer with integer zero");
10605 else if (extra_warnings)
ba47d38d 10606 warning_at (location, OPT_Wextra,
d42ba3b8 10607 "ordered comparison of pointer with integer zero");
3e4093b6 10608 }
6aa3c60d 10609 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10610 {
10611 result_type = type1;
d42ba3b8 10612 if (pedantic)
c1771a20 10613 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10614 "ordered comparison of pointer with integer zero");
10615 else if (extra_warnings)
10616 warning_at (location, OPT_Wextra,
10617 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10618 }
10619 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10620 {
10621 result_type = type0;
ba47d38d 10622 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10623 }
10624 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10625 {
10626 result_type = type1;
ba47d38d 10627 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10628 }
10629 break;
64094f6a 10630
3e4093b6 10631 default:
37b2f290 10632 gcc_unreachable ();
c9fe6f9f 10633 }
8f17b5c5 10634
e57e265b
PB
10635 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10636 return error_mark_node;
10637
5bed876a
AH
10638 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10639 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10640 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10641 {
ba47d38d 10642 binary_op_error (location, code, type0, type1);
5bed876a
AH
10643 return error_mark_node;
10644 }
10645
3e4093b6 10646 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10647 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10648 &&
10649 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10650 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10651 {
2ca862e9
JM
10652 bool first_complex = (code0 == COMPLEX_TYPE);
10653 bool second_complex = (code1 == COMPLEX_TYPE);
10654 int none_complex = (!first_complex && !second_complex);
39b726dd 10655
3e4093b6 10656 if (shorten || common || short_compare)
3bf6bfcc
JJ
10657 {
10658 result_type = c_common_type (type0, type1);
0a0b3574
MM
10659 do_warn_double_promotion (result_type, type0, type1,
10660 "implicit conversion from %qT to %qT "
10661 "to match other operand of binary "
10662 "expression",
10663 location);
3bf6bfcc
JJ
10664 if (result_type == error_mark_node)
10665 return error_mark_node;
10666 }
400fbf9f 10667
2ca862e9
JM
10668 if (first_complex != second_complex
10669 && (code == PLUS_EXPR
10670 || code == MINUS_EXPR
10671 || code == MULT_EXPR
10672 || (code == TRUNC_DIV_EXPR && first_complex))
10673 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10674 && flag_signed_zeros)
10675 {
10676 /* An operation on mixed real/complex operands must be
10677 handled specially, but the language-independent code can
10678 more easily optimize the plain complex arithmetic if
10679 -fno-signed-zeros. */
10680 tree real_type = TREE_TYPE (result_type);
10681 tree real, imag;
10682 if (type0 != orig_type0 || type1 != orig_type1)
10683 {
10684 gcc_assert (may_need_excess_precision && common);
2d2e923f 10685 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
10686 }
10687 if (first_complex)
10688 {
10689 if (TREE_TYPE (op0) != result_type)
10690 op0 = convert_and_check (result_type, op0);
10691 if (TREE_TYPE (op1) != real_type)
10692 op1 = convert_and_check (real_type, op1);
10693 }
10694 else
10695 {
10696 if (TREE_TYPE (op0) != real_type)
10697 op0 = convert_and_check (real_type, op0);
10698 if (TREE_TYPE (op1) != result_type)
10699 op1 = convert_and_check (result_type, op1);
10700 }
10701 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10702 return error_mark_node;
10703 if (first_complex)
10704 {
10705 op0 = c_save_expr (op0);
10706 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10707 op0, 1);
10708 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10709 op0, 1);
10710 switch (code)
10711 {
10712 case MULT_EXPR:
10713 case TRUNC_DIV_EXPR:
c4603e7c 10714 op1 = c_save_expr (op1);
2ca862e9
JM
10715 imag = build2 (resultcode, real_type, imag, op1);
10716 /* Fall through. */
10717 case PLUS_EXPR:
10718 case MINUS_EXPR:
10719 real = build2 (resultcode, real_type, real, op1);
10720 break;
10721 default:
10722 gcc_unreachable();
10723 }
10724 }
10725 else
10726 {
10727 op1 = c_save_expr (op1);
10728 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10729 op1, 1);
10730 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10731 op1, 1);
10732 switch (code)
10733 {
10734 case MULT_EXPR:
c4603e7c 10735 op0 = c_save_expr (op0);
2ca862e9
JM
10736 imag = build2 (resultcode, real_type, op0, imag);
10737 /* Fall through. */
10738 case PLUS_EXPR:
10739 real = build2 (resultcode, real_type, op0, real);
10740 break;
10741 case MINUS_EXPR:
10742 real = build2 (resultcode, real_type, op0, real);
10743 imag = build1 (NEGATE_EXPR, real_type, imag);
10744 break;
10745 default:
10746 gcc_unreachable();
10747 }
10748 }
10749 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10750 goto return_build_binary_op;
10751 }
10752
3e4093b6
RS
10753 /* For certain operations (which identify themselves by shorten != 0)
10754 if both args were extended from the same smaller type,
10755 do the arithmetic in that type and then extend.
400fbf9f 10756
3e4093b6
RS
10757 shorten !=0 and !=1 indicates a bitwise operation.
10758 For them, this optimization is safe only if
10759 both args are zero-extended or both are sign-extended.
10760 Otherwise, we might change the result.
10761 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10762 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 10763
3e4093b6
RS
10764 if (shorten && none_complex)
10765 {
3e4093b6 10766 final_type = result_type;
b8698a0f 10767 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 10768 shorten == -1);
3e4093b6 10769 }
88a3dbc1 10770
3e4093b6 10771 /* Shifts can be shortened if shifting right. */
2f6e4e97 10772
3e4093b6
RS
10773 if (short_shift)
10774 {
10775 int unsigned_arg;
10776 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 10777
3e4093b6 10778 final_type = result_type;
abe80e6d 10779
3e4093b6 10780 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 10781 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 10782
3e4093b6 10783 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 10784 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
10785 /* We can shorten only if the shift count is less than the
10786 number of bits in the smaller type size. */
10787 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10788 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 10789 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
10790 {
10791 /* Do an unsigned shift if the operand was zero-extended. */
10792 result_type
10793 = c_common_signed_or_unsigned_type (unsigned_arg,
10794 TREE_TYPE (arg0));
10795 /* Convert value-to-be-shifted to that type. */
10796 if (TREE_TYPE (op0) != result_type)
10797 op0 = convert (result_type, op0);
10798 converted = 1;
abe80e6d 10799 }
88a3dbc1
RK
10800 }
10801
3e4093b6
RS
10802 /* Comparison operations are shortened too but differently.
10803 They identify themselves by setting short_compare = 1. */
56cb9733 10804
3e4093b6
RS
10805 if (short_compare)
10806 {
10807 /* Don't write &op0, etc., because that would prevent op0
10808 from being kept in a register.
10809 Instead, make copies of the our local variables and
10810 pass the copies by reference, then copy them back afterward. */
10811 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10812 enum tree_code xresultcode = resultcode;
10813 tree val
10814 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8f17b5c5 10815
3e4093b6 10816 if (val != 0)
c9f9eb5d
AH
10817 {
10818 ret = val;
10819 goto return_build_binary_op;
10820 }
8f17b5c5 10821
3e4093b6
RS
10822 op0 = xop0, op1 = xop1;
10823 converted = 1;
10824 resultcode = xresultcode;
8f17b5c5 10825
7d882b83 10826 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10827 {
10828 bool op0_maybe_const = true;
10829 bool op1_maybe_const = true;
10830 tree orig_op0_folded, orig_op1_folded;
10831
10832 if (in_late_binary_op)
10833 {
10834 orig_op0_folded = orig_op0;
10835 orig_op1_folded = orig_op1;
10836 }
10837 else
10838 {
10839 /* Fold for the sake of possible warnings, as in
10840 build_conditional_expr. This requires the
10841 "original" values to be folded, not just op0 and
10842 op1. */
f5178456 10843 c_inhibit_evaluation_warnings++;
928c19bb
JM
10844 op0 = c_fully_fold (op0, require_constant_value,
10845 &op0_maybe_const);
10846 op1 = c_fully_fold (op1, require_constant_value,
10847 &op1_maybe_const);
f5178456 10848 c_inhibit_evaluation_warnings--;
928c19bb
JM
10849 orig_op0_folded = c_fully_fold (orig_op0,
10850 require_constant_value,
10851 NULL);
10852 orig_op1_folded = c_fully_fold (orig_op1,
10853 require_constant_value,
10854 NULL);
10855 }
10856
10857 if (warn_sign_compare)
10858 warn_for_sign_compare (location, orig_op0_folded,
10859 orig_op1_folded, op0, op1,
10860 result_type, resultcode);
5c2f94b4 10861 if (!in_late_binary_op && !int_operands)
928c19bb
JM
10862 {
10863 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 10864 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 10865 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 10866 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 10867 }
3e4093b6 10868 }
2ad1815d 10869 }
64094f6a 10870 }
64094f6a 10871
3e4093b6
RS
10872 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10873 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10874 Then the expression will be built.
10875 It will be given type FINAL_TYPE if that is nonzero;
10876 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 10877
3e4093b6
RS
10878 if (!result_type)
10879 {
ba47d38d 10880 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
10881 return error_mark_node;
10882 }
400fbf9f 10883
3e4093b6 10884 if (build_type == NULL_TREE)
8ce94e44
JM
10885 {
10886 build_type = result_type;
180f8dbb
JM
10887 if ((type0 != orig_type0 || type1 != orig_type1)
10888 && !boolean_op)
8ce94e44
JM
10889 {
10890 gcc_assert (may_need_excess_precision && common);
2d2e923f 10891 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
10892 }
10893 }
400fbf9f 10894
2d2e923f
MLI
10895 if (!converted)
10896 {
10897 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10898 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10899
10900 /* This can happen if one operand has a vector type, and the other
10901 has a different type. */
10902 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10903 return error_mark_node;
10904 }
10905
a24d975c 10906 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
de5a5fa1 10907 && current_function_decl != 0
ce6923c5
MP
10908 && !lookup_attribute ("no_sanitize_undefined",
10909 DECL_ATTRIBUTES (current_function_decl))
de5a5fa1
MP
10910 && (doing_div_or_mod || doing_shift))
10911 {
10912 /* OP0 and/or OP1 might have side-effects. */
10913 op0 = c_save_expr (op0);
10914 op1 = c_save_expr (op1);
10915 op0 = c_fully_fold (op0, false, NULL);
10916 op1 = c_fully_fold (op1, false, NULL);
a24d975c 10917 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
de5a5fa1 10918 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 10919 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
10920 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
10921 }
10922
c9f9eb5d 10923 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
10924 if (int_const_or_overflow)
10925 ret = (require_constant_value
db3927fb
AH
10926 ? fold_build2_initializer_loc (location, resultcode, build_type,
10927 op0, op1)
10928 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
10929 else
10930 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
10931 if (final_type != 0)
10932 ret = convert (final_type, ret);
10933
10934 return_build_binary_op:
10935 gcc_assert (ret != error_mark_node);
928c19bb
JM
10936 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10937 ret = (int_operands
10938 ? note_integer_operands (ret)
10939 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10940 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10941 && !in_late_binary_op)
10942 ret = note_integer_operands (ret);
2d2e923f
MLI
10943 if (semantic_result_type)
10944 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 10945 protected_set_expr_location (ret, location);
de5a5fa1 10946
a24d975c 10947 if (instrument_expr != NULL)
de5a5fa1
MP
10948 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
10949 instrument_expr, ret);
10950
c9f9eb5d 10951 return ret;
400fbf9f 10952}
85498824
JM
10953
10954
10955/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 10956 purpose. LOCATION is the source location for the expression. */
85498824
JM
10957
10958tree
ba47d38d 10959c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 10960{
928c19bb
JM
10961 bool int_const, int_operands;
10962
85498824
JM
10963 switch (TREE_CODE (TREE_TYPE (expr)))
10964 {
10965 case ARRAY_TYPE:
ba47d38d 10966 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
10967 return error_mark_node;
10968
10969 case RECORD_TYPE:
ba47d38d 10970 error_at (location, "used struct type value where scalar is required");
85498824
JM
10971 return error_mark_node;
10972
10973 case UNION_TYPE:
ba47d38d 10974 error_at (location, "used union type value where scalar is required");
85498824
JM
10975 return error_mark_node;
10976
04af8788
NP
10977 case VOID_TYPE:
10978 error_at (location, "void value not ignored as it ought to be");
10979 return error_mark_node;
10980
46bdb9cf
JM
10981 case FUNCTION_TYPE:
10982 gcc_unreachable ();
10983
d246ab4f
AS
10984 case VECTOR_TYPE:
10985 error_at (location, "used vector type where scalar is required");
10986 return error_mark_node;
10987
85498824
JM
10988 default:
10989 break;
10990 }
10991
928c19bb
JM
10992 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10993 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
10994 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
10995 {
10996 expr = remove_c_maybe_const_expr (expr);
10997 expr = build2 (NE_EXPR, integer_type_node, expr,
10998 convert (TREE_TYPE (expr), integer_zero_node));
10999 expr = note_integer_operands (expr);
11000 }
11001 else
11002 /* ??? Should we also give an error for vectors rather than leaving
11003 those to give errors later? */
11004 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11005
11006 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11007 {
11008 if (TREE_OVERFLOW (expr))
11009 return expr;
11010 else
11011 return note_integer_operands (expr);
11012 }
11013 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11014 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11015 return expr;
85498824 11016}
73f397d4
JM
11017\f
11018
11019/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11020 required. */
11021
11022tree
51eed280 11023c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11024{
11025 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11026 {
11027 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11028 /* Executing a compound literal inside a function reinitializes
11029 it. */
11030 if (!TREE_STATIC (decl))
11031 *se = true;
11032 return decl;
11033 }
11034 else
11035 return expr;
11036}
953ff289 11037\f
c0220ea4 11038/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11039
11040tree
11041c_begin_omp_parallel (void)
11042{
11043 tree block;
11044
11045 keep_next_level ();
11046 block = c_begin_compound_stmt (true);
11047
11048 return block;
11049}
11050
c2255bc4
AH
11051/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11052 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11053
953ff289 11054tree
c2255bc4 11055c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11056{
11057 tree stmt;
11058
c2255bc4 11059 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11060
11061 stmt = make_node (OMP_PARALLEL);
11062 TREE_TYPE (stmt) = void_type_node;
11063 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11064 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11065 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11066
11067 return add_stmt (stmt);
11068}
11069
a68ab351
JJ
11070/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11071
11072tree
11073c_begin_omp_task (void)
11074{
11075 tree block;
11076
11077 keep_next_level ();
11078 block = c_begin_compound_stmt (true);
11079
11080 return block;
11081}
11082
c2255bc4
AH
11083/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11084 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11085
11086tree
c2255bc4 11087c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11088{
11089 tree stmt;
11090
c2255bc4 11091 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11092
11093 stmt = make_node (OMP_TASK);
11094 TREE_TYPE (stmt) = void_type_node;
11095 OMP_TASK_CLAUSES (stmt) = clauses;
11096 OMP_TASK_BODY (stmt) = block;
c2255bc4 11097 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11098
11099 return add_stmt (stmt);
11100}
11101
acf0174b
JJ
11102/* Generate GOMP_cancel call for #pragma omp cancel. */
11103
11104void
11105c_finish_omp_cancel (location_t loc, tree clauses)
11106{
11107 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11108 int mask = 0;
11109 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11110 mask = 1;
11111 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11112 mask = 2;
11113 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11114 mask = 4;
11115 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11116 mask = 8;
11117 else
11118 {
11119 error_at (loc, "%<#pragma omp cancel must specify one of "
11120 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11121 "clauses");
11122 return;
11123 }
11124 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11125 if (ifc != NULL_TREE)
11126 {
11127 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11128 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11129 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11130 build_zero_cst (type));
11131 }
11132 else
11133 ifc = boolean_true_node;
11134 tree stmt = build_call_expr_loc (loc, fn, 2,
11135 build_int_cst (integer_type_node, mask),
11136 ifc);
11137 add_stmt (stmt);
11138}
11139
11140/* Generate GOMP_cancellation_point call for
11141 #pragma omp cancellation point. */
11142
11143void
11144c_finish_omp_cancellation_point (location_t loc, tree clauses)
11145{
11146 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11147 int mask = 0;
11148 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11149 mask = 1;
11150 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11151 mask = 2;
11152 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11153 mask = 4;
11154 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11155 mask = 8;
11156 else
11157 {
11158 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11159 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11160 "clauses");
11161 return;
11162 }
11163 tree stmt = build_call_expr_loc (loc, fn, 1,
11164 build_int_cst (integer_type_node, mask));
11165 add_stmt (stmt);
11166}
11167
11168/* Helper function for handle_omp_array_sections. Called recursively
11169 to handle multiple array-section-subscripts. C is the clause,
11170 T current expression (initially OMP_CLAUSE_DECL), which is either
11171 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11172 expression if specified, TREE_VALUE length expression if specified,
11173 TREE_CHAIN is what it has been specified after, or some decl.
11174 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11175 set to true if any of the array-section-subscript could have length
11176 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11177 first array-section-subscript which is known not to have length
11178 of one. Given say:
11179 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11180 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11181 all are or may have length of 1, array-section-subscript [:2] is the
11182 first one knonwn not to have length 1. For array-section-subscript
11183 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11184 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11185 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11186 case though, as some lengths could be zero. */
11187
11188static tree
11189handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11190 bool &maybe_zero_len, unsigned int &first_non_one)
11191{
11192 tree ret, low_bound, length, type;
11193 if (TREE_CODE (t) != TREE_LIST)
11194 {
11195 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11196 return error_mark_node;
11197 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11198 {
11199 if (DECL_P (t))
11200 error_at (OMP_CLAUSE_LOCATION (c),
11201 "%qD is not a variable in %qs clause", t,
11202 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11203 else
11204 error_at (OMP_CLAUSE_LOCATION (c),
11205 "%qE is not a variable in %qs clause", t,
11206 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11207 return error_mark_node;
11208 }
11209 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11210 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11211 {
11212 error_at (OMP_CLAUSE_LOCATION (c),
11213 "%qD is threadprivate variable in %qs clause", t,
11214 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11215 return error_mark_node;
11216 }
11217 return t;
11218 }
11219
11220 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11221 maybe_zero_len, first_non_one);
11222 if (ret == error_mark_node || ret == NULL_TREE)
11223 return ret;
11224
11225 type = TREE_TYPE (ret);
11226 low_bound = TREE_PURPOSE (t);
11227 length = TREE_VALUE (t);
11228
11229 if (low_bound == error_mark_node || length == error_mark_node)
11230 return error_mark_node;
11231
11232 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11233 {
11234 error_at (OMP_CLAUSE_LOCATION (c),
11235 "low bound %qE of array section does not have integral type",
11236 low_bound);
11237 return error_mark_node;
11238 }
11239 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11240 {
11241 error_at (OMP_CLAUSE_LOCATION (c),
11242 "length %qE of array section does not have integral type",
11243 length);
11244 return error_mark_node;
11245 }
11246 if (low_bound
11247 && TREE_CODE (low_bound) == INTEGER_CST
11248 && TYPE_PRECISION (TREE_TYPE (low_bound))
11249 > TYPE_PRECISION (sizetype))
11250 low_bound = fold_convert (sizetype, low_bound);
11251 if (length
11252 && TREE_CODE (length) == INTEGER_CST
11253 && TYPE_PRECISION (TREE_TYPE (length))
11254 > TYPE_PRECISION (sizetype))
11255 length = fold_convert (sizetype, length);
11256 if (low_bound == NULL_TREE)
11257 low_bound = integer_zero_node;
11258
11259 if (length != NULL_TREE)
11260 {
11261 if (!integer_nonzerop (length))
11262 maybe_zero_len = true;
11263 if (first_non_one == types.length ()
11264 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11265 first_non_one++;
11266 }
11267 if (TREE_CODE (type) == ARRAY_TYPE)
11268 {
11269 if (length == NULL_TREE
11270 && (TYPE_DOMAIN (type) == NULL_TREE
11271 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11272 {
11273 error_at (OMP_CLAUSE_LOCATION (c),
11274 "for unknown bound array type length expression must "
11275 "be specified");
11276 return error_mark_node;
11277 }
11278 if (TREE_CODE (low_bound) == INTEGER_CST
11279 && tree_int_cst_sgn (low_bound) == -1)
11280 {
11281 error_at (OMP_CLAUSE_LOCATION (c),
11282 "negative low bound in array section in %qs clause",
11283 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11284 return error_mark_node;
11285 }
11286 if (length != NULL_TREE
11287 && TREE_CODE (length) == INTEGER_CST
11288 && tree_int_cst_sgn (length) == -1)
11289 {
11290 error_at (OMP_CLAUSE_LOCATION (c),
11291 "negative length in array section in %qs clause",
11292 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11293 return error_mark_node;
11294 }
11295 if (TYPE_DOMAIN (type)
11296 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11297 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11298 == INTEGER_CST)
11299 {
11300 tree size = size_binop (PLUS_EXPR,
11301 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11302 size_one_node);
11303 if (TREE_CODE (low_bound) == INTEGER_CST)
11304 {
11305 if (tree_int_cst_lt (size, low_bound))
11306 {
11307 error_at (OMP_CLAUSE_LOCATION (c),
11308 "low bound %qE above array section size "
11309 "in %qs clause", low_bound,
11310 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11311 return error_mark_node;
11312 }
11313 if (tree_int_cst_equal (size, low_bound))
11314 maybe_zero_len = true;
11315 else if (length == NULL_TREE
11316 && first_non_one == types.length ()
11317 && tree_int_cst_equal
11318 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11319 low_bound))
11320 first_non_one++;
11321 }
11322 else if (length == NULL_TREE)
11323 {
11324 maybe_zero_len = true;
11325 if (first_non_one == types.length ())
11326 first_non_one++;
11327 }
11328 if (length && TREE_CODE (length) == INTEGER_CST)
11329 {
11330 if (tree_int_cst_lt (size, length))
11331 {
11332 error_at (OMP_CLAUSE_LOCATION (c),
11333 "length %qE above array section size "
11334 "in %qs clause", length,
11335 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11336 return error_mark_node;
11337 }
11338 if (TREE_CODE (low_bound) == INTEGER_CST)
11339 {
11340 tree lbpluslen
11341 = size_binop (PLUS_EXPR,
11342 fold_convert (sizetype, low_bound),
11343 fold_convert (sizetype, length));
11344 if (TREE_CODE (lbpluslen) == INTEGER_CST
11345 && tree_int_cst_lt (size, lbpluslen))
11346 {
11347 error_at (OMP_CLAUSE_LOCATION (c),
11348 "high bound %qE above array section size "
11349 "in %qs clause", lbpluslen,
11350 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11351 return error_mark_node;
11352 }
11353 }
11354 }
11355 }
11356 else if (length == NULL_TREE)
11357 {
11358 maybe_zero_len = true;
11359 if (first_non_one == types.length ())
11360 first_non_one++;
11361 }
11362
11363 /* For [lb:] we will need to evaluate lb more than once. */
11364 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11365 {
11366 tree lb = c_save_expr (low_bound);
11367 if (lb != low_bound)
11368 {
11369 TREE_PURPOSE (t) = lb;
11370 low_bound = lb;
11371 }
11372 }
11373 }
11374 else if (TREE_CODE (type) == POINTER_TYPE)
11375 {
11376 if (length == NULL_TREE)
11377 {
11378 error_at (OMP_CLAUSE_LOCATION (c),
11379 "for pointer type length expression must be specified");
11380 return error_mark_node;
11381 }
11382 /* If there is a pointer type anywhere but in the very first
11383 array-section-subscript, the array section can't be contiguous. */
11384 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11385 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11386 {
11387 error_at (OMP_CLAUSE_LOCATION (c),
11388 "array section is not contiguous in %qs clause",
11389 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11390 return error_mark_node;
11391 }
11392 }
11393 else
11394 {
11395 error_at (OMP_CLAUSE_LOCATION (c),
11396 "%qE does not have pointer or array type", ret);
11397 return error_mark_node;
11398 }
11399 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11400 types.safe_push (TREE_TYPE (ret));
11401 /* We will need to evaluate lb more than once. */
11402 tree lb = c_save_expr (low_bound);
11403 if (lb != low_bound)
11404 {
11405 TREE_PURPOSE (t) = lb;
11406 low_bound = lb;
11407 }
11408 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11409 return ret;
11410}
11411
11412/* Handle array sections for clause C. */
11413
11414static bool
11415handle_omp_array_sections (tree c)
11416{
11417 bool maybe_zero_len = false;
11418 unsigned int first_non_one = 0;
11419 vec<tree> types = vNULL;
11420 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11421 maybe_zero_len, first_non_one);
11422 if (first == error_mark_node)
11423 {
11424 types.release ();
11425 return true;
11426 }
11427 if (first == NULL_TREE)
11428 {
11429 types.release ();
11430 return false;
11431 }
11432 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11433 {
11434 tree t = OMP_CLAUSE_DECL (c);
11435 tree tem = NULL_TREE;
11436 types.release ();
11437 /* Need to evaluate side effects in the length expressions
11438 if any. */
11439 while (TREE_CODE (t) == TREE_LIST)
11440 {
11441 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11442 {
11443 if (tem == NULL_TREE)
11444 tem = TREE_VALUE (t);
11445 else
11446 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11447 TREE_VALUE (t), tem);
11448 }
11449 t = TREE_CHAIN (t);
11450 }
11451 if (tem)
11452 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11453 first = c_fully_fold (first, false, NULL);
11454 OMP_CLAUSE_DECL (c) = first;
11455 }
11456 else
11457 {
11458 unsigned int num = types.length (), i;
11459 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11460 tree condition = NULL_TREE;
11461
11462 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11463 maybe_zero_len = true;
11464
11465 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11466 t = TREE_CHAIN (t))
11467 {
11468 tree low_bound = TREE_PURPOSE (t);
11469 tree length = TREE_VALUE (t);
11470
11471 i--;
11472 if (low_bound
11473 && TREE_CODE (low_bound) == INTEGER_CST
11474 && TYPE_PRECISION (TREE_TYPE (low_bound))
11475 > TYPE_PRECISION (sizetype))
11476 low_bound = fold_convert (sizetype, low_bound);
11477 if (length
11478 && TREE_CODE (length) == INTEGER_CST
11479 && TYPE_PRECISION (TREE_TYPE (length))
11480 > TYPE_PRECISION (sizetype))
11481 length = fold_convert (sizetype, length);
11482 if (low_bound == NULL_TREE)
11483 low_bound = integer_zero_node;
11484 if (!maybe_zero_len && i > first_non_one)
11485 {
11486 if (integer_nonzerop (low_bound))
11487 goto do_warn_noncontiguous;
11488 if (length != NULL_TREE
11489 && TREE_CODE (length) == INTEGER_CST
11490 && TYPE_DOMAIN (types[i])
11491 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11492 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11493 == INTEGER_CST)
11494 {
11495 tree size;
11496 size = size_binop (PLUS_EXPR,
11497 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11498 size_one_node);
11499 if (!tree_int_cst_equal (length, size))
11500 {
11501 do_warn_noncontiguous:
11502 error_at (OMP_CLAUSE_LOCATION (c),
11503 "array section is not contiguous in %qs "
11504 "clause",
11505 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11506 types.release ();
11507 return true;
11508 }
11509 }
11510 if (length != NULL_TREE
11511 && TREE_SIDE_EFFECTS (length))
11512 {
11513 if (side_effects == NULL_TREE)
11514 side_effects = length;
11515 else
11516 side_effects = build2 (COMPOUND_EXPR,
11517 TREE_TYPE (side_effects),
11518 length, side_effects);
11519 }
11520 }
11521 else
11522 {
11523 tree l;
11524
11525 if (i > first_non_one && length && integer_nonzerop (length))
11526 continue;
11527 if (length)
11528 l = fold_convert (sizetype, length);
11529 else
11530 {
11531 l = size_binop (PLUS_EXPR,
11532 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11533 size_one_node);
11534 l = size_binop (MINUS_EXPR, l,
11535 fold_convert (sizetype, low_bound));
11536 }
11537 if (i > first_non_one)
11538 {
11539 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11540 size_zero_node);
11541 if (condition == NULL_TREE)
11542 condition = l;
11543 else
11544 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11545 l, condition);
11546 }
11547 else if (size == NULL_TREE)
11548 {
11549 size = size_in_bytes (TREE_TYPE (types[i]));
11550 size = size_binop (MULT_EXPR, size, l);
11551 if (condition)
11552 size = fold_build3 (COND_EXPR, sizetype, condition,
11553 size, size_zero_node);
11554 }
11555 else
11556 size = size_binop (MULT_EXPR, size, l);
11557 }
11558 }
11559 types.release ();
11560 if (side_effects)
11561 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11562 first = c_fully_fold (first, false, NULL);
11563 OMP_CLAUSE_DECL (c) = first;
11564 if (size)
11565 size = c_fully_fold (size, false, NULL);
11566 OMP_CLAUSE_SIZE (c) = size;
11567 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11568 return false;
11569 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11570 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11571 if (!c_mark_addressable (t))
11572 return false;
11573 OMP_CLAUSE_DECL (c2) = t;
11574 t = build_fold_addr_expr (first);
11575 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11576 tree ptr = OMP_CLAUSE_DECL (c2);
11577 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11578 ptr = build_fold_addr_expr (ptr);
11579 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11580 ptrdiff_type_node, t,
11581 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11582 ptrdiff_type_node, ptr));
11583 t = c_fully_fold (t, false, NULL);
11584 OMP_CLAUSE_SIZE (c2) = t;
11585 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11586 OMP_CLAUSE_CHAIN (c) = c2;
11587 }
11588 return false;
11589}
11590
11591/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11592 an inline call. But, remap
11593 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11594 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11595
11596static tree
11597c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11598 tree decl, tree placeholder)
11599{
11600 copy_body_data id;
11601 struct pointer_map_t *decl_map = pointer_map_create ();
11602
11603 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
11604 *pointer_map_insert (decl_map, omp_decl2) = decl;
11605 memset (&id, 0, sizeof (id));
11606 id.src_fn = DECL_CONTEXT (omp_decl1);
11607 id.dst_fn = current_function_decl;
11608 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11609 id.decl_map = decl_map;
11610
11611 id.copy_decl = copy_decl_no_change;
11612 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11613 id.transform_new_cfg = true;
11614 id.transform_return_to_modify = false;
11615 id.transform_lang_insert_block = NULL;
11616 id.eh_lp_nr = 0;
11617 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11618 pointer_map_destroy (decl_map);
11619 return stmt;
11620}
11621
11622/* Helper function of c_finish_omp_clauses, called via walk_tree.
11623 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11624
11625static tree
11626c_find_omp_placeholder_r (tree *tp, int *, void *data)
11627{
11628 if (*tp == (tree) data)
11629 return *tp;
11630 return NULL_TREE;
11631}
11632
953ff289
DN
11633/* For all elements of CLAUSES, validate them vs OpenMP constraints.
11634 Remove any elements from the list that are invalid. */
11635
11636tree
11637c_finish_omp_clauses (tree clauses)
11638{
11639 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 11640 bitmap_head aligned_head;
953ff289 11641 tree c, t, *pc = &clauses;
acf0174b
JJ
11642 bool branch_seen = false;
11643 bool copyprivate_seen = false;
11644 tree *nowait_clause = NULL;
953ff289
DN
11645
11646 bitmap_obstack_initialize (NULL);
11647 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11648 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11649 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 11650 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
11651
11652 for (pc = &clauses, c = clauses; c ; c = *pc)
11653 {
11654 bool remove = false;
11655 bool need_complete = false;
11656 bool need_implicitly_determined = false;
11657
aaf46ef9 11658 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
11659 {
11660 case OMP_CLAUSE_SHARED:
953ff289
DN
11661 need_implicitly_determined = true;
11662 goto check_dup_generic;
11663
11664 case OMP_CLAUSE_PRIVATE:
953ff289
DN
11665 need_complete = true;
11666 need_implicitly_determined = true;
11667 goto check_dup_generic;
11668
11669 case OMP_CLAUSE_REDUCTION:
953ff289
DN
11670 need_implicitly_determined = true;
11671 t = OMP_CLAUSE_DECL (c);
acf0174b
JJ
11672 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
11673 && FLOAT_TYPE_P (TREE_TYPE (t)))
953ff289
DN
11674 {
11675 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11676 const char *r_name = NULL;
11677
11678 switch (r_code)
11679 {
11680 case PLUS_EXPR:
11681 case MULT_EXPR:
11682 case MINUS_EXPR:
20906c66
JJ
11683 case MIN_EXPR:
11684 case MAX_EXPR:
953ff289
DN
11685 break;
11686 case BIT_AND_EXPR:
11687 r_name = "&";
11688 break;
11689 case BIT_XOR_EXPR:
11690 r_name = "^";
11691 break;
11692 case BIT_IOR_EXPR:
11693 r_name = "|";
11694 break;
11695 case TRUTH_ANDIF_EXPR:
11696 r_name = "&&";
11697 break;
11698 case TRUTH_ORIF_EXPR:
11699 r_name = "||";
11700 break;
11701 default:
11702 gcc_unreachable ();
11703 }
11704 if (r_name)
11705 {
c2255bc4
AH
11706 error_at (OMP_CLAUSE_LOCATION (c),
11707 "%qE has invalid type for %<reduction(%s)%>",
11708 t, r_name);
953ff289 11709 remove = true;
ee1d5a02 11710 break;
953ff289
DN
11711 }
11712 }
acf0174b
JJ
11713 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11714 {
11715 error_at (OMP_CLAUSE_LOCATION (c),
11716 "user defined reduction not found for %qD", t);
11717 remove = true;
ee1d5a02 11718 break;
acf0174b
JJ
11719 }
11720 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11721 {
11722 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11723 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11724 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11725 VAR_DECL, NULL_TREE, type);
11726 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11727 DECL_ARTIFICIAL (placeholder) = 1;
11728 DECL_IGNORED_P (placeholder) = 1;
11729 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11730 c_mark_addressable (placeholder);
11731 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11732 c_mark_addressable (OMP_CLAUSE_DECL (c));
11733 OMP_CLAUSE_REDUCTION_MERGE (c)
11734 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11735 TREE_VEC_ELT (list, 0),
11736 TREE_VEC_ELT (list, 1),
11737 OMP_CLAUSE_DECL (c), placeholder);
11738 OMP_CLAUSE_REDUCTION_MERGE (c)
11739 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11740 void_type_node, NULL_TREE,
11741 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11742 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11743 if (TREE_VEC_LENGTH (list) == 6)
11744 {
11745 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11746 c_mark_addressable (OMP_CLAUSE_DECL (c));
11747 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11748 c_mark_addressable (placeholder);
11749 tree init = TREE_VEC_ELT (list, 5);
11750 if (init == error_mark_node)
11751 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11752 OMP_CLAUSE_REDUCTION_INIT (c)
11753 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11754 TREE_VEC_ELT (list, 3),
11755 OMP_CLAUSE_DECL (c), placeholder);
11756 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11757 OMP_CLAUSE_REDUCTION_INIT (c)
11758 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11759 OMP_CLAUSE_REDUCTION_INIT (c));
11760 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11761 c_find_omp_placeholder_r,
11762 placeholder, NULL))
11763 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11764 }
11765 else
11766 {
11767 tree init;
11768 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11769 init = build_constructor (TREE_TYPE (t), NULL);
11770 else
11771 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11772 OMP_CLAUSE_REDUCTION_INIT (c)
11773 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11774 }
11775 OMP_CLAUSE_REDUCTION_INIT (c)
11776 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11777 void_type_node, NULL_TREE,
11778 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11779 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11780 }
953ff289
DN
11781 goto check_dup_generic;
11782
11783 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
11784 copyprivate_seen = true;
11785 if (nowait_clause)
11786 {
11787 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11788 "%<nowait%> clause must not be used together "
11789 "with %<copyprivate%>");
11790 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11791 nowait_clause = NULL;
11792 }
953ff289
DN
11793 goto check_dup_generic;
11794
11795 case OMP_CLAUSE_COPYIN:
953ff289
DN
11796 t = OMP_CLAUSE_DECL (c);
11797 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11798 {
c2255bc4
AH
11799 error_at (OMP_CLAUSE_LOCATION (c),
11800 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 11801 remove = true;
ee1d5a02 11802 break;
953ff289
DN
11803 }
11804 goto check_dup_generic;
11805
acf0174b
JJ
11806 case OMP_CLAUSE_LINEAR:
11807 t = OMP_CLAUSE_DECL (c);
11808 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11809 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11810 {
11811 error_at (OMP_CLAUSE_LOCATION (c),
11812 "linear clause applied to non-integral non-pointer "
11813 "variable with type %qT", TREE_TYPE (t));
11814 remove = true;
11815 break;
11816 }
11817 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11818 {
11819 tree s = OMP_CLAUSE_LINEAR_STEP (c);
11820 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
11821 OMP_CLAUSE_DECL (c), s);
11822 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11823 sizetype, s, OMP_CLAUSE_DECL (c));
11824 if (s == error_mark_node)
11825 s = size_one_node;
11826 OMP_CLAUSE_LINEAR_STEP (c) = s;
11827 }
11828 goto check_dup_generic;
11829
953ff289
DN
11830 check_dup_generic:
11831 t = OMP_CLAUSE_DECL (c);
11832 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11833 {
c2255bc4 11834 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
11835 "%qE is not a variable in clause %qs", t,
11836 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
11837 remove = true;
11838 }
11839 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11840 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
11841 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11842 {
c2255bc4
AH
11843 error_at (OMP_CLAUSE_LOCATION (c),
11844 "%qE appears more than once in data clauses", t);
953ff289
DN
11845 remove = true;
11846 }
11847 else
11848 bitmap_set_bit (&generic_head, DECL_UID (t));
11849 break;
11850
11851 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
11852 t = OMP_CLAUSE_DECL (c);
11853 need_complete = true;
11854 need_implicitly_determined = true;
11855 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11856 {
c2255bc4
AH
11857 error_at (OMP_CLAUSE_LOCATION (c),
11858 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
11859 remove = true;
11860 }
11861 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11862 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
11863 {
c2255bc4
AH
11864 error_at (OMP_CLAUSE_LOCATION (c),
11865 "%qE appears more than once in data clauses", t);
953ff289
DN
11866 remove = true;
11867 }
11868 else
11869 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
11870 break;
11871
11872 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
11873 t = OMP_CLAUSE_DECL (c);
11874 need_complete = true;
11875 need_implicitly_determined = true;
11876 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11877 {
c2255bc4
AH
11878 error_at (OMP_CLAUSE_LOCATION (c),
11879 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
11880 remove = true;
11881 }
11882 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11883 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11884 {
c2255bc4
AH
11885 error_at (OMP_CLAUSE_LOCATION (c),
11886 "%qE appears more than once in data clauses", t);
953ff289
DN
11887 remove = true;
11888 }
11889 else
11890 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
11891 break;
11892
acf0174b
JJ
11893 case OMP_CLAUSE_ALIGNED:
11894 t = OMP_CLAUSE_DECL (c);
11895 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11896 {
11897 error_at (OMP_CLAUSE_LOCATION (c),
11898 "%qE is not a variable in %<aligned%> clause", t);
11899 remove = true;
11900 }
5a9785fb
JJ
11901 else if (!POINTER_TYPE_P (TREE_TYPE (t))
11902 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
11903 {
11904 error_at (OMP_CLAUSE_LOCATION (c),
11905 "%qE in %<aligned%> clause is neither a pointer nor "
11906 "an array", t);
11907 remove = true;
11908 }
acf0174b
JJ
11909 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
11910 {
11911 error_at (OMP_CLAUSE_LOCATION (c),
11912 "%qE appears more than once in %<aligned%> clauses",
11913 t);
11914 remove = true;
11915 }
11916 else
11917 bitmap_set_bit (&aligned_head, DECL_UID (t));
11918 break;
11919
11920 case OMP_CLAUSE_DEPEND:
11921 t = OMP_CLAUSE_DECL (c);
11922 if (TREE_CODE (t) == TREE_LIST)
11923 {
11924 if (handle_omp_array_sections (c))
11925 remove = true;
11926 break;
11927 }
11928 if (t == error_mark_node)
11929 remove = true;
11930 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11931 {
11932 error_at (OMP_CLAUSE_LOCATION (c),
11933 "%qE is not a variable in %<depend%> clause", t);
11934 remove = true;
11935 }
11936 else if (!c_mark_addressable (t))
11937 remove = true;
11938 break;
11939
11940 case OMP_CLAUSE_MAP:
11941 case OMP_CLAUSE_TO:
11942 case OMP_CLAUSE_FROM:
11943 t = OMP_CLAUSE_DECL (c);
11944 if (TREE_CODE (t) == TREE_LIST)
11945 {
11946 if (handle_omp_array_sections (c))
11947 remove = true;
11948 else
11949 {
11950 t = OMP_CLAUSE_DECL (c);
11951 if (!COMPLETE_TYPE_P (TREE_TYPE (t)))
11952 {
11953 error_at (OMP_CLAUSE_LOCATION (c),
11954 "array section does not have mappable type "
11955 "in %qs clause",
11956 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11957 remove = true;
11958 }
11959 }
11960 break;
11961 }
11962 if (t == error_mark_node)
11963 remove = true;
11964 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11965 {
11966 error_at (OMP_CLAUSE_LOCATION (c),
11967 "%qE is not a variable in %qs clause", t,
11968 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11969 remove = true;
11970 }
11971 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11972 {
11973 error_at (OMP_CLAUSE_LOCATION (c),
11974 "%qD is threadprivate variable in %qs clause", t,
11975 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11976 remove = true;
11977 }
11978 else if (!c_mark_addressable (t))
11979 remove = true;
11980 else if (!COMPLETE_TYPE_P (TREE_TYPE (t))
11981 && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
11982 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER))
11983 {
11984 error_at (OMP_CLAUSE_LOCATION (c),
11985 "%qD does not have a mappable type in %qs clause", t,
11986 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11987 remove = true;
11988 }
11989 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
11990 {
11991 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11992 error ("%qD appears more than once in motion clauses", t);
11993 else
11994 error ("%qD appears more than once in map clauses", t);
11995 remove = true;
11996 }
11997 else
11998 bitmap_set_bit (&generic_head, DECL_UID (t));
11999 break;
12000
12001 case OMP_CLAUSE_UNIFORM:
12002 t = OMP_CLAUSE_DECL (c);
12003 if (TREE_CODE (t) != PARM_DECL)
12004 {
12005 if (DECL_P (t))
12006 error_at (OMP_CLAUSE_LOCATION (c),
12007 "%qD is not an argument in %<uniform%> clause", t);
12008 else
12009 error_at (OMP_CLAUSE_LOCATION (c),
12010 "%qE is not an argument in %<uniform%> clause", t);
12011 remove = true;
ee1d5a02 12012 break;
acf0174b 12013 }
ee1d5a02 12014 goto check_dup_generic;
acf0174b
JJ
12015
12016 case OMP_CLAUSE_NOWAIT:
12017 if (copyprivate_seen)
12018 {
12019 error_at (OMP_CLAUSE_LOCATION (c),
12020 "%<nowait%> clause must not be used together "
12021 "with %<copyprivate%>");
12022 remove = true;
12023 break;
12024 }
12025 nowait_clause = pc;
12026 pc = &OMP_CLAUSE_CHAIN (c);
12027 continue;
12028
953ff289
DN
12029 case OMP_CLAUSE_IF:
12030 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12031 case OMP_CLAUSE_NUM_TEAMS:
12032 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12033 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12034 case OMP_CLAUSE_ORDERED:
12035 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12036 case OMP_CLAUSE_UNTIED:
12037 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12038 case OMP_CLAUSE_FINAL:
12039 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12040 case OMP_CLAUSE_SAFELEN:
12041 case OMP_CLAUSE_SIMDLEN:
12042 case OMP_CLAUSE_DEVICE:
12043 case OMP_CLAUSE_DIST_SCHEDULE:
12044 case OMP_CLAUSE_PARALLEL:
12045 case OMP_CLAUSE_FOR:
12046 case OMP_CLAUSE_SECTIONS:
12047 case OMP_CLAUSE_TASKGROUP:
12048 case OMP_CLAUSE_PROC_BIND:
12049 pc = &OMP_CLAUSE_CHAIN (c);
12050 continue;
12051
12052 case OMP_CLAUSE_INBRANCH:
12053 case OMP_CLAUSE_NOTINBRANCH:
12054 if (branch_seen)
12055 {
12056 error_at (OMP_CLAUSE_LOCATION (c),
12057 "%<inbranch%> clause is incompatible with "
12058 "%<notinbranch%>");
12059 remove = true;
12060 break;
12061 }
12062 branch_seen = true;
953ff289
DN
12063 pc = &OMP_CLAUSE_CHAIN (c);
12064 continue;
12065
12066 default:
12067 gcc_unreachable ();
12068 }
12069
12070 if (!remove)
12071 {
12072 t = OMP_CLAUSE_DECL (c);
12073
12074 if (need_complete)
12075 {
12076 t = require_complete_type (t);
12077 if (t == error_mark_node)
12078 remove = true;
12079 }
12080
12081 if (need_implicitly_determined)
12082 {
12083 const char *share_name = NULL;
12084
12085 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12086 share_name = "threadprivate";
12087 else switch (c_omp_predetermined_sharing (t))
12088 {
12089 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12090 break;
12091 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12092 /* const vars may be specified in firstprivate clause. */
12093 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12094 && TREE_READONLY (t))
12095 break;
953ff289
DN
12096 share_name = "shared";
12097 break;
12098 case OMP_CLAUSE_DEFAULT_PRIVATE:
12099 share_name = "private";
12100 break;
12101 default:
12102 gcc_unreachable ();
12103 }
12104 if (share_name)
12105 {
c2255bc4
AH
12106 error_at (OMP_CLAUSE_LOCATION (c),
12107 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12108 t, share_name,
12109 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12110 remove = true;
12111 }
12112 }
12113 }
12114
12115 if (remove)
12116 *pc = OMP_CLAUSE_CHAIN (c);
12117 else
12118 pc = &OMP_CLAUSE_CHAIN (c);
12119 }
12120
12121 bitmap_obstack_release (NULL);
12122 return clauses;
12123}
9ae165a0 12124
0a35513e
AH
12125/* Create a transaction node. */
12126
12127tree
12128c_finish_transaction (location_t loc, tree block, int flags)
12129{
12130 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12131 if (flags & TM_STMT_ATTR_OUTER)
12132 TRANSACTION_EXPR_OUTER (stmt) = 1;
12133 if (flags & TM_STMT_ATTR_RELAXED)
12134 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12135 return add_stmt (stmt);
12136}
12137
9ae165a0
DG
12138/* Make a variant type in the proper way for C/C++, propagating qualifiers
12139 down to the element type of an array. */
12140
12141tree
12142c_build_qualified_type (tree type, int type_quals)
12143{
12144 if (type == error_mark_node)
12145 return type;
12146
12147 if (TREE_CODE (type) == ARRAY_TYPE)
12148 {
12149 tree t;
12150 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12151 type_quals);
12152
12153 /* See if we already have an identically qualified type. */
12154 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12155 {
12156 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12157 && TYPE_NAME (t) == TYPE_NAME (type)
12158 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12159 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12160 TYPE_ATTRIBUTES (type)))
12161 break;
12162 }
12163 if (!t)
12164 {
12165 tree domain = TYPE_DOMAIN (type);
12166
12167 t = build_variant_type_copy (type);
12168 TREE_TYPE (t) = element_type;
12169
12170 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12171 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12172 SET_TYPE_STRUCTURAL_EQUALITY (t);
12173 else if (TYPE_CANONICAL (element_type) != element_type
12174 || (domain && TYPE_CANONICAL (domain) != domain))
12175 {
b8698a0f 12176 tree unqualified_canon
9ae165a0 12177 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12178 domain? TYPE_CANONICAL (domain)
9ae165a0 12179 : NULL_TREE);
b8698a0f 12180 TYPE_CANONICAL (t)
9ae165a0
DG
12181 = c_build_qualified_type (unqualified_canon, type_quals);
12182 }
12183 else
12184 TYPE_CANONICAL (t) = t;
12185 }
12186 return t;
12187 }
12188
12189 /* A restrict-qualified pointer type must be a pointer to object or
12190 incomplete type. Note that the use of POINTER_TYPE_P also allows
12191 REFERENCE_TYPEs, which is appropriate for C++. */
12192 if ((type_quals & TYPE_QUAL_RESTRICT)
12193 && (!POINTER_TYPE_P (type)
12194 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12195 {
12196 error ("invalid use of %<restrict%>");
12197 type_quals &= ~TYPE_QUAL_RESTRICT;
12198 }
12199
12200 return build_qualified_type (type, type_quals);
12201}
72b5577d
ILT
12202
12203/* Build a VA_ARG_EXPR for the C parser. */
12204
12205tree
c2255bc4 12206c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d
ILT
12207{
12208 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12209 warning_at (loc, OPT_Wc___compat,
12210 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12211 return build_va_arg (loc, expr, type);
72b5577d 12212}
acf0174b
JJ
12213
12214/* Return truthvalue of whether T1 is the same tree structure as T2.
12215 Return 1 if they are the same. Return 0 if they are different. */
12216
12217bool
12218c_tree_equal (tree t1, tree t2)
12219{
12220 enum tree_code code1, code2;
12221
12222 if (t1 == t2)
12223 return true;
12224 if (!t1 || !t2)
12225 return false;
12226
12227 for (code1 = TREE_CODE (t1);
12228 CONVERT_EXPR_CODE_P (code1)
12229 || code1 == NON_LVALUE_EXPR;
12230 code1 = TREE_CODE (t1))
12231 t1 = TREE_OPERAND (t1, 0);
12232 for (code2 = TREE_CODE (t2);
12233 CONVERT_EXPR_CODE_P (code2)
12234 || code2 == NON_LVALUE_EXPR;
12235 code2 = TREE_CODE (t2))
12236 t2 = TREE_OPERAND (t2, 0);
12237
12238 /* They might have become equal now. */
12239 if (t1 == t2)
12240 return true;
12241
12242 if (code1 != code2)
12243 return false;
12244
12245 switch (code1)
12246 {
12247 case INTEGER_CST:
12248 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
12249 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
12250
12251 case REAL_CST:
12252 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12253
12254 case STRING_CST:
12255 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12256 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12257 TREE_STRING_LENGTH (t1));
12258
12259 case FIXED_CST:
12260 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12261 TREE_FIXED_CST (t2));
12262
12263 case COMPLEX_CST:
12264 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12265 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12266
12267 case VECTOR_CST:
12268 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12269
12270 case CONSTRUCTOR:
12271 /* We need to do this when determining whether or not two
12272 non-type pointer to member function template arguments
12273 are the same. */
12274 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12275 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12276 return false;
12277 {
12278 tree field, value;
12279 unsigned int i;
12280 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12281 {
12282 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12283 if (!c_tree_equal (field, elt2->index)
12284 || !c_tree_equal (value, elt2->value))
12285 return false;
12286 }
12287 }
12288 return true;
12289
12290 case TREE_LIST:
12291 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12292 return false;
12293 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12294 return false;
12295 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12296
12297 case SAVE_EXPR:
12298 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12299
12300 case CALL_EXPR:
12301 {
12302 tree arg1, arg2;
12303 call_expr_arg_iterator iter1, iter2;
12304 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12305 return false;
12306 for (arg1 = first_call_expr_arg (t1, &iter1),
12307 arg2 = first_call_expr_arg (t2, &iter2);
12308 arg1 && arg2;
12309 arg1 = next_call_expr_arg (&iter1),
12310 arg2 = next_call_expr_arg (&iter2))
12311 if (!c_tree_equal (arg1, arg2))
12312 return false;
12313 if (arg1 || arg2)
12314 return false;
12315 return true;
12316 }
12317
12318 case TARGET_EXPR:
12319 {
12320 tree o1 = TREE_OPERAND (t1, 0);
12321 tree o2 = TREE_OPERAND (t2, 0);
12322
12323 /* Special case: if either target is an unallocated VAR_DECL,
12324 it means that it's going to be unified with whatever the
12325 TARGET_EXPR is really supposed to initialize, so treat it
12326 as being equivalent to anything. */
12327 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12328 && !DECL_RTL_SET_P (o1))
12329 /*Nop*/;
12330 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12331 && !DECL_RTL_SET_P (o2))
12332 /*Nop*/;
12333 else if (!c_tree_equal (o1, o2))
12334 return false;
12335
12336 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12337 }
12338
12339 case COMPONENT_REF:
12340 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12341 return false;
12342 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12343
12344 case PARM_DECL:
12345 case VAR_DECL:
12346 case CONST_DECL:
12347 case FIELD_DECL:
12348 case FUNCTION_DECL:
12349 case IDENTIFIER_NODE:
12350 case SSA_NAME:
12351 return false;
12352
12353 case TREE_VEC:
12354 {
12355 unsigned ix;
12356 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12357 return false;
12358 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12359 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12360 TREE_VEC_ELT (t2, ix)))
12361 return false;
12362 return true;
12363 }
12364
12365 default:
12366 break;
12367 }
12368
12369 switch (TREE_CODE_CLASS (code1))
12370 {
12371 case tcc_unary:
12372 case tcc_binary:
12373 case tcc_comparison:
12374 case tcc_expression:
12375 case tcc_vl_exp:
12376 case tcc_reference:
12377 case tcc_statement:
12378 {
12379 int i, n = TREE_OPERAND_LENGTH (t1);
12380
12381 switch (code1)
12382 {
12383 case PREINCREMENT_EXPR:
12384 case PREDECREMENT_EXPR:
12385 case POSTINCREMENT_EXPR:
12386 case POSTDECREMENT_EXPR:
12387 n = 1;
12388 break;
12389 case ARRAY_REF:
12390 n = 2;
12391 break;
12392 default:
12393 break;
12394 }
12395
12396 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12397 && n != TREE_OPERAND_LENGTH (t2))
12398 return false;
12399
12400 for (i = 0; i < n; ++i)
12401 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12402 return false;
12403
12404 return true;
12405 }
12406
12407 case tcc_type:
12408 return comptypes (t1, t2);
12409 default:
12410 gcc_unreachable ();
12411 }
12412 /* We can get here with --disable-checking. */
12413 return false;
12414}