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