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