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