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