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