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