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