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