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