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