]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-typeck.c
(if_then_else_cond): Don't do anything with COMPARE.
[thirdparty/gcc.git] / gcc / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
fd5d5b94 2 Copyright (C) 1987, 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
400fbf9f
JW
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization.
25
26 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
27 and to process initializations in declarations (since they work
28 like a strange sort of assignment). */
29
30#include "config.h"
31#include <stdio.h>
32#include "tree.h"
33#include "c-tree.h"
34#include "flags.h"
35
b71c7f8a 36/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 37 message within this initializer. */
b71c7f8a 38static int missing_braces_mentioned;
103b7b17 39
3845b542
MS
40extern char *index ();
41extern char *rindex ();
42
400fbf9f
JW
43int mark_addressable ();
44static tree convert_for_assignment ();
45static void warn_for_assignment ();
46static int function_types_compatible_p ();
47static int type_lists_compatible_p ();
805f961c 48int self_promoting_args_p ();
400fbf9f
JW
49static int self_promoting_type_p ();
50static int comp_target_types ();
51static tree pointer_int_sum ();
52static tree pointer_diff ();
53static tree convert_sequence ();
54static tree unary_complex_lvalue ();
55static tree process_init_constructor ();
56static tree convert_arguments ();
d45cf215 57static char *get_spelling ();
de520661 58static tree digest_init ();
400fbf9f
JW
59static void pedantic_lvalue_warning ();
60tree truthvalue_conversion ();
61void incomplete_type_error ();
62void readonly_warning ();
82bde854
MM
63static tree internal_build_compound_expr ();
64
de520661 65void process_init_element ();
400fbf9f
JW
66\f
67/* Do `exp = require_complete_type (exp);' to make sure exp
68 does not have an incomplete type. (That includes void types.) */
69
70tree
71require_complete_type (value)
72 tree value;
73{
74 tree type = TREE_TYPE (value);
75
76 /* First, detect a valid value with a complete type. */
77 if (TYPE_SIZE (type) != 0
78 && type != void_type_node)
79 return value;
80
81 incomplete_type_error (value, type);
82 return error_mark_node;
83}
84
85/* Print an error message for invalid use of an incomplete type.
86 VALUE is the expression that was used (or 0 if that isn't known)
87 and TYPE is the type that was invalid. */
88
89void
90incomplete_type_error (value, type)
91 tree value;
92 tree type;
93{
94 char *errmsg;
95
96 /* Avoid duplicate error message. */
97 if (TREE_CODE (type) == ERROR_MARK)
98 return;
99
100 if (value != 0 && (TREE_CODE (value) == VAR_DECL
101 || TREE_CODE (value) == PARM_DECL))
102 error ("`%s' has an incomplete type",
103 IDENTIFIER_POINTER (DECL_NAME (value)));
104 else
105 {
106 retry:
107 /* We must print an error message. Be clever about what it says. */
108
109 switch (TREE_CODE (type))
110 {
111 case RECORD_TYPE:
112 errmsg = "invalid use of undefined type `struct %s'";
113 break;
114
115 case UNION_TYPE:
116 errmsg = "invalid use of undefined type `union %s'";
117 break;
118
119 case ENUMERAL_TYPE:
120 errmsg = "invalid use of undefined type `enum %s'";
121 break;
122
123 case VOID_TYPE:
124 error ("invalid use of void expression");
125 return;
126
127 case ARRAY_TYPE:
128 if (TYPE_DOMAIN (type))
129 {
130 type = TREE_TYPE (type);
131 goto retry;
132 }
133 error ("invalid use of array with unspecified bounds");
134 return;
135
136 default:
137 abort ();
138 }
139
140 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
141 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
142 else
143 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
144 error ("invalid use of incomplete typedef `%s'",
145 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
146 }
147}
148
149/* Return a variant of TYPE which has all the type qualifiers of LIKE
150 as well as those of TYPE. */
151
152static tree
153qualify_type (type, like)
154 tree type, like;
155{
156 int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
157 int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
158 return c_build_type_variant (type, constflag, volflag);
159}
160\f
161/* Return the common type of two types.
162 We assume that comptypes has already been done and returned 1;
6cb72a7d
RS
163 if that isn't so, this may crash. In particular, we assume that qualifiers
164 match.
400fbf9f
JW
165
166 This is the type for the result of most arithmetic operations
6cb72a7d 167 if the operands have the given two types. */
400fbf9f
JW
168
169tree
170common_type (t1, t2)
171 tree t1, t2;
172{
173 register enum tree_code code1;
174 register enum tree_code code2;
175
176 /* Save time if the two types are the same. */
177
178 if (t1 == t2) return t1;
179
180 /* If one type is nonsense, use the other. */
181 if (t1 == error_mark_node)
182 return t2;
183 if (t2 == error_mark_node)
184 return t1;
185
186 /* Treat an enum type as the unsigned integer type of the same width. */
187
188 if (TREE_CODE (t1) == ENUMERAL_TYPE)
189 t1 = type_for_size (TYPE_PRECISION (t1), 1);
190 if (TREE_CODE (t2) == ENUMERAL_TYPE)
191 t2 = type_for_size (TYPE_PRECISION (t2), 1);
192
193 code1 = TREE_CODE (t1);
194 code2 = TREE_CODE (t2);
195
75326e8c
RK
196 /* If one type is complex, form the common type of the non-complex
197 components, then make that complex. Use T1 or T2 if it is the
198 required type. */
b6a10c9f
RS
199 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
200 {
75326e8c
RK
201 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
202 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
203 tree subtype = common_type (subtype1, subtype2);
204
205 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
206 return t1;
207 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
208 return t2;
b6a10c9f 209 else
75326e8c 210 return build_complex_type (subtype);
b6a10c9f
RS
211 }
212
400fbf9f
JW
213 switch (code1)
214 {
215 case INTEGER_TYPE:
216 case REAL_TYPE:
217 /* If only one is real, use it as the result. */
218
219 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
220 return t1;
221
222 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
223 return t2;
224
225 /* Both real or both integers; use the one with greater precision. */
226
227 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
228 return t1;
229 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
230 return t2;
231
232 /* Same precision. Prefer longs to ints even when same size. */
233
36618528
RS
234 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
235 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
400fbf9f
JW
236 return long_unsigned_type_node;
237
36618528
RS
238 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
239 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
400fbf9f
JW
240 {
241 /* But preserve unsignedness from the other type,
242 since long cannot hold all the values of an unsigned int. */
243 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
244 return long_unsigned_type_node;
245 return long_integer_type_node;
246 }
247
248 /* Otherwise prefer the unsigned one. */
249
250 if (TREE_UNSIGNED (t1))
251 return t1;
252 else return t2;
253
254 case POINTER_TYPE:
400fbf9f
JW
255 /* For two pointers, do this recursively on the target type,
256 and combine the qualifiers of the two types' targets. */
8706edbc
RS
257 /* This code was turned off; I don't know why.
258 But ANSI C specifies doing this with the qualifiers.
259 So I turned it on again. */
400fbf9f
JW
260 {
261 tree target = common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
262 TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
263 int constp
264 = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
265 int volatilep
266 = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
267 return build_pointer_type (c_build_type_variant (target, constp, volatilep));
268 }
8706edbc 269#if 0
400fbf9f 270 return build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
8706edbc 271#endif
400fbf9f
JW
272
273 case ARRAY_TYPE:
274 {
275 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
276 /* Save space: see if the result is identical to one of the args. */
277 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
278 return t1;
279 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
280 return t2;
281 /* Merge the element types, and have a size if either arg has one. */
282 return build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
283 }
284
285 case FUNCTION_TYPE:
286 /* Function types: prefer the one that specified arg types.
287 If both do, merge the arg types. Also merge the return types. */
288 {
289 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
290 tree p1 = TYPE_ARG_TYPES (t1);
291 tree p2 = TYPE_ARG_TYPES (t2);
292 int len;
293 tree newargs, n;
294 int i;
295
296 /* Save space: see if the result is identical to one of the args. */
297 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
298 return t1;
299 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
300 return t2;
301
302 /* Simple way if one arg fails to specify argument types. */
303 if (TYPE_ARG_TYPES (t1) == 0)
304 return build_function_type (valtype, TYPE_ARG_TYPES (t2));
305 if (TYPE_ARG_TYPES (t2) == 0)
306 return build_function_type (valtype, TYPE_ARG_TYPES (t1));
307
308 /* If both args specify argument types, we must merge the two
309 lists, argument by argument. */
310
311 len = list_length (p1);
312 newargs = 0;
313
314 for (i = 0; i < len; i++)
8d9bfdc5 315 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
316
317 n = newargs;
318
319 for (; p1;
320 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
321 {
322 /* A null type means arg type is not specified.
323 Take whatever the other function type has. */
324 if (TREE_VALUE (p1) == 0)
325 {
326 TREE_VALUE (n) = TREE_VALUE (p2);
327 goto parm_done;
328 }
329 if (TREE_VALUE (p2) == 0)
330 {
331 TREE_VALUE (n) = TREE_VALUE (p1);
332 goto parm_done;
333 }
334
335 /* Given wait (union {union wait *u; int *i} *)
336 and wait (union wait *),
337 prefer union wait * as type of parm. */
338 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
339 && TREE_VALUE (p1) != TREE_VALUE (p2))
340 {
341 tree memb;
342 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
343 memb; memb = TREE_CHAIN (memb))
344 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
345 {
346 TREE_VALUE (n) = TREE_VALUE (p2);
347 if (pedantic)
348 pedwarn ("function types not truly compatible in ANSI C");
349 goto parm_done;
350 }
351 }
352 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
353 && TREE_VALUE (p2) != TREE_VALUE (p1))
354 {
355 tree memb;
356 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
357 memb; memb = TREE_CHAIN (memb))
358 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
359 {
360 TREE_VALUE (n) = TREE_VALUE (p1);
361 if (pedantic)
362 pedwarn ("function types not truly compatible in ANSI C");
363 goto parm_done;
364 }
365 }
366 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
367 parm_done: ;
368 }
369
370 return build_function_type (valtype, newargs);
371 }
372
373 default:
374 return t1;
375 }
376
377}
378\f
379/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
380 or various other operations. Return 2 if they are compatible
381 but a warning may be needed if you use them together. */
382
383int
384comptypes (type1, type2)
385 tree type1, type2;
386{
387 register tree t1 = type1;
388 register tree t2 = type2;
389
390 /* Suppress errors caused by previously reported errors. */
391
392 if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
393 return 1;
394
395 /* Treat an enum type as the unsigned integer type of the same width. */
396
397 if (TREE_CODE (t1) == ENUMERAL_TYPE)
398 t1 = type_for_size (TYPE_PRECISION (t1), 1);
399 if (TREE_CODE (t2) == ENUMERAL_TYPE)
400 t2 = type_for_size (TYPE_PRECISION (t2), 1);
401
402 if (t1 == t2)
403 return 1;
404
405 /* Different classes of types can't be compatible. */
406
407 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
408
409 /* Qualifiers must match. */
410
411 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
412 return 0;
413 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
414 return 0;
415
08632da2
RS
416 /* Allow for two different type nodes which have essentially the same
417 definition. Note that we already checked for equality of the type
418 type qualifiers (just above). */
400fbf9f
JW
419
420 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
421 return 1;
422
423 switch (TREE_CODE (t1))
424 {
425 case POINTER_TYPE:
426 return (TREE_TYPE (t1) == TREE_TYPE (t2)
427 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
428
429 case FUNCTION_TYPE:
430 return function_types_compatible_p (t1, t2);
431
432 case ARRAY_TYPE:
433 {
434 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
435 int val = 1;
436 tree d1 = TYPE_DOMAIN (t1);
437 tree d2 = TYPE_DOMAIN (t2);
438
439 /* Target types must match incl. qualifiers. */
440 if (TREE_TYPE (t1) != TREE_TYPE (t2)
441 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
442 return 0;
443
444 /* Sizes must match unless one is missing or variable. */
445 if (d1 == 0 || d2 == 0 || d1 == d2
446 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
447 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
448 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
449 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
450 return val;
451
452 return (((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
453 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
454 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
455 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
456 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
457 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
458 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
459 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))))
460 ? val : 0);
461 }
462
463 case RECORD_TYPE:
392202b0
TW
464 if (maybe_objc_comptypes (t1, t2, 0) == 1)
465 return 1;
400fbf9f
JW
466 }
467 return 0;
468}
469
470/* Return 1 if TTL and TTR are pointers to types that are equivalent,
471 ignoring their qualifiers. */
472
473static int
474comp_target_types (ttl, ttr)
475 tree ttl, ttr;
476{
392202b0 477 int val;
8b40563c 478
392202b0
TW
479 /* Give maybe_objc_comptypes a crack at letting these types through. */
480 if (val = maybe_objc_comptypes (ttl, ttr, 1) >= 0)
481 return val;
8b40563c 482
392202b0
TW
483 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
484 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
8b40563c 485
400fbf9f
JW
486 if (val == 2 && pedantic)
487 pedwarn ("types are not quite compatible");
488 return val;
489}
490\f
491/* Subroutines of `comptypes'. */
492
493/* Return 1 if two function types F1 and F2 are compatible.
494 If either type specifies no argument types,
495 the other must specify a fixed number of self-promoting arg types.
496 Otherwise, if one type specifies only the number of arguments,
497 the other must specify that number of self-promoting arg types.
498 Otherwise, the argument types must match. */
499
500static int
501function_types_compatible_p (f1, f2)
502 tree f1, f2;
503{
504 tree args1, args2;
505 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
506 int val = 1;
507 int val1;
508
509 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
510 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
511 return 0;
512
513 args1 = TYPE_ARG_TYPES (f1);
514 args2 = TYPE_ARG_TYPES (f2);
515
516 /* An unspecified parmlist matches any specified parmlist
517 whose argument types don't need default promotions. */
518
519 if (args1 == 0)
520 {
521 if (!self_promoting_args_p (args2))
522 return 0;
523 /* If one of these types comes from a non-prototype fn definition,
524 compare that with the other type's arglist.
525 If they don't match, ask for a warning (but no error). */
526 if (TYPE_ACTUAL_ARG_TYPES (f1)
527 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
528 val = 2;
529 return val;
530 }
531 if (args2 == 0)
532 {
533 if (!self_promoting_args_p (args1))
534 return 0;
535 if (TYPE_ACTUAL_ARG_TYPES (f2)
536 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
537 val = 2;
538 return val;
539 }
540
541 /* Both types have argument lists: compare them and propagate results. */
542 val1 = type_lists_compatible_p (args1, args2);
543 return val1 != 1 ? val1 : val;
544}
545
546/* Check two lists of types for compatibility,
547 returning 0 for incompatible, 1 for compatible,
548 or 2 for compatible with warning. */
549
550static int
551type_lists_compatible_p (args1, args2)
552 tree args1, args2;
553{
554 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
555 int val = 1;
556 int newval;
557
558 while (1)
559 {
560 if (args1 == 0 && args2 == 0)
561 return val;
562 /* If one list is shorter than the other,
563 they fail to match. */
564 if (args1 == 0 || args2 == 0)
565 return 0;
566 /* A null pointer instead of a type
567 means there is supposed to be an argument
568 but nothing is specified about what type it has.
569 So match anything that self-promotes. */
570 if (TREE_VALUE (args1) == 0)
571 {
572 if (! self_promoting_type_p (TREE_VALUE (args2)))
573 return 0;
574 }
575 else if (TREE_VALUE (args2) == 0)
576 {
577 if (! self_promoting_type_p (TREE_VALUE (args1)))
578 return 0;
579 }
580 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
581 {
582 /* Allow wait (union {union wait *u; int *i} *)
583 and wait (union wait *) to be compatible. */
584 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
585 && TYPE_NAME (TREE_VALUE (args1)) == 0
586 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
587 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
588 TYPE_SIZE (TREE_VALUE (args2))))
589 {
590 tree memb;
591 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
592 memb; memb = TREE_CHAIN (memb))
593 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
594 break;
595 if (memb == 0)
596 return 0;
597 }
598 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
599 && TYPE_NAME (TREE_VALUE (args2)) == 0
600 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
601 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
602 TYPE_SIZE (TREE_VALUE (args1))))
603 {
604 tree memb;
605 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
606 memb; memb = TREE_CHAIN (memb))
607 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
608 break;
609 if (memb == 0)
610 return 0;
611 }
612 else
613 return 0;
614 }
615
616 /* comptypes said ok, but record if it said to warn. */
617 if (newval > val)
618 val = newval;
619
620 args1 = TREE_CHAIN (args1);
621 args2 = TREE_CHAIN (args2);
622 }
623}
624
625/* Return 1 if PARMS specifies a fixed number of parameters
626 and none of their types is affected by default promotions. */
627
805f961c 628int
400fbf9f
JW
629self_promoting_args_p (parms)
630 tree parms;
631{
632 register tree t;
633 for (t = parms; t; t = TREE_CHAIN (t))
634 {
635 register tree type = TREE_VALUE (t);
636
637 if (TREE_CHAIN (t) == 0 && type != void_type_node)
638 return 0;
639
d627ed1b
RS
640 if (type == 0)
641 return 0;
642
6cb72a7d 643 if (TYPE_MAIN_VARIANT (type) == float_type_node)
400fbf9f
JW
644 return 0;
645
d627ed1b 646 if (C_PROMOTING_INTEGER_TYPE_P (type))
400fbf9f
JW
647 return 0;
648 }
649 return 1;
650}
651
652/* Return 1 if TYPE is not affected by default promotions. */
653
654static int
655self_promoting_type_p (type)
656 tree type;
657{
6cb72a7d 658 if (TYPE_MAIN_VARIANT (type) == float_type_node)
400fbf9f
JW
659 return 0;
660
d627ed1b 661 if (C_PROMOTING_INTEGER_TYPE_P (type))
400fbf9f
JW
662 return 0;
663
664 return 1;
665}
666\f
667/* Return an unsigned type the same as TYPE in other respects. */
668
669tree
670unsigned_type (type)
671 tree type;
672{
6cb72a7d
RS
673 tree type1 = TYPE_MAIN_VARIANT (type);
674 if (type1 == signed_char_type_node || type1 == char_type_node)
400fbf9f 675 return unsigned_char_type_node;
6cb72a7d 676 if (type1 == integer_type_node)
400fbf9f 677 return unsigned_type_node;
6cb72a7d 678 if (type1 == short_integer_type_node)
400fbf9f 679 return short_unsigned_type_node;
6cb72a7d 680 if (type1 == long_integer_type_node)
400fbf9f 681 return long_unsigned_type_node;
6cb72a7d 682 if (type1 == long_long_integer_type_node)
400fbf9f
JW
683 return long_long_unsigned_type_node;
684 return type;
685}
686
687/* Return a signed type the same as TYPE in other respects. */
688
689tree
690signed_type (type)
691 tree type;
692{
6cb72a7d
RS
693 tree type1 = TYPE_MAIN_VARIANT (type);
694 if (type1 == unsigned_char_type_node || type1 == char_type_node)
400fbf9f 695 return signed_char_type_node;
6cb72a7d 696 if (type1 == unsigned_type_node)
400fbf9f 697 return integer_type_node;
6cb72a7d 698 if (type1 == short_unsigned_type_node)
400fbf9f 699 return short_integer_type_node;
6cb72a7d 700 if (type1 == long_unsigned_type_node)
400fbf9f 701 return long_integer_type_node;
6cb72a7d 702 if (type1 == long_long_unsigned_type_node)
400fbf9f
JW
703 return long_long_integer_type_node;
704 return type;
705}
706
707/* Return a type the same as TYPE except unsigned or
708 signed according to UNSIGNEDP. */
709
710tree
711signed_or_unsigned_type (unsignedp, type)
712 int unsignedp;
713 tree type;
714{
715 if (TREE_CODE (type) != INTEGER_TYPE)
716 return type;
717 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
718 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
719 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
720 return unsignedp ? unsigned_type_node : integer_type_node;
721 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
722 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
723 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
724 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
725 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
726 return (unsignedp ? long_long_unsigned_type_node
727 : long_long_integer_type_node);
728 return type;
729}
730
731/* Compute the value of the `sizeof' operator. */
732
733tree
734c_sizeof (type)
735 tree type;
736{
737 enum tree_code code = TREE_CODE (type);
f7c8fb3f 738 tree t;
400fbf9f
JW
739
740 if (code == FUNCTION_TYPE)
741 {
742 if (pedantic || warn_pointer_arith)
743 pedwarn ("sizeof applied to a function type");
744 return size_int (1);
745 }
746 if (code == VOID_TYPE)
747 {
748 if (pedantic || warn_pointer_arith)
749 pedwarn ("sizeof applied to a void type");
750 return size_int (1);
751 }
752 if (code == ERROR_MARK)
753 return size_int (1);
754 if (TYPE_SIZE (type) == 0)
755 {
756 error ("sizeof applied to an incomplete type");
757 return size_int (0);
758 }
759
760 /* Convert in case a char is more than one unit. */
f7c8fb3f
RS
761 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
762 size_int (TYPE_PRECISION (char_type_node)));
fa427131 763 /* size_binop does not put the constant in range, so do it now. */
10d5caec
PE
764 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
765 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
f7c8fb3f 766 return t;
400fbf9f
JW
767}
768
769tree
770c_sizeof_nowarn (type)
771 tree type;
772{
773 enum tree_code code = TREE_CODE (type);
f7c8fb3f 774 tree t;
400fbf9f
JW
775
776 if (code == FUNCTION_TYPE
777 || code == VOID_TYPE
778 || code == ERROR_MARK)
779 return size_int (1);
780 if (TYPE_SIZE (type) == 0)
781 return size_int (0);
782
783 /* Convert in case a char is more than one unit. */
f7c8fb3f
RS
784 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
785 size_int (TYPE_PRECISION (char_type_node)));
e58cd767 786 force_fit_type (t, 0);
f7c8fb3f 787 return t;
400fbf9f
JW
788}
789
790/* Compute the size to increment a pointer by. */
791
792tree
793c_size_in_bytes (type)
794 tree type;
795{
796 enum tree_code code = TREE_CODE (type);
f7c8fb3f 797 tree t;
400fbf9f
JW
798
799 if (code == FUNCTION_TYPE)
800 return size_int (1);
801 if (code == VOID_TYPE)
802 return size_int (1);
803 if (code == ERROR_MARK)
804 return size_int (1);
805 if (TYPE_SIZE (type) == 0)
806 {
807 error ("arithmetic on pointer to an incomplete type");
808 return size_int (1);
809 }
810
811 /* Convert in case a char is more than one unit. */
f7c8fb3f 812 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
400fbf9f 813 size_int (BITS_PER_UNIT));
e58cd767 814 force_fit_type (t, 0);
f7c8fb3f 815 return t;
400fbf9f
JW
816}
817
818/* Implement the __alignof keyword: Return the minimum required
819 alignment of TYPE, measured in bytes. */
820
821tree
822c_alignof (type)
823 tree type;
824{
825 enum tree_code code = TREE_CODE (type);
826
827 if (code == FUNCTION_TYPE)
828 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
829
830 if (code == VOID_TYPE || code == ERROR_MARK)
831 return size_int (1);
832
833 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
834}
835\f
836/* Implement the __alignof keyword: Return the minimum required
837 alignment of EXPR, measured in bytes. For VAR_DECL's and
838 FIELD_DECL's return DECL_ALIGN (which can be set from an
839 "aligned" __attribute__ specification). */
9e9bd45d 840
400fbf9f
JW
841tree
842c_alignof_expr (expr)
843 tree expr;
844{
845 if (TREE_CODE (expr) == VAR_DECL)
846 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
847
848 if (TREE_CODE (expr) == COMPONENT_REF
849 && DECL_BIT_FIELD (TREE_OPERAND (expr, 1)))
850 {
851 error ("`__alignof' applied to a bit-field");
852 return size_int (1);
853 }
854 else if (TREE_CODE (expr) == COMPONENT_REF
855 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
856 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
857
858 if (TREE_CODE (expr) == INDIRECT_REF)
859 {
860 tree t = TREE_OPERAND (expr, 0);
861 tree best = t;
862 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
863
864 while (TREE_CODE (t) == NOP_EXPR
865 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
866 {
867 int thisalign;
868
869 t = TREE_OPERAND (t, 0);
870 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
871 if (thisalign > bestalign)
872 best = t, bestalign = thisalign;
873 }
874 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
875 }
876 else
877 return c_alignof (TREE_TYPE (expr));
878}
879/* Return either DECL or its known constant value (if it has one). */
880
881static tree
882decl_constant_value (decl)
883 tree decl;
884{
885 if (! TREE_PUBLIC (decl)
886 /* Don't change a variable array bound or initial value to a constant
887 in a place where a variable is invalid. */
888 && current_function_decl != 0
889 && ! pedantic
890 && ! TREE_THIS_VOLATILE (decl)
891 && DECL_INITIAL (decl) != 0
892 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
893 /* This is invalid if initial value is not constant.
894 If it has either a function call, a memory reference,
895 or a variable, then re-evaluating it could give different results. */
896 && TREE_CONSTANT (DECL_INITIAL (decl))
897 /* Check for cases where this is sub-optimal, even though valid. */
898 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
899 && DECL_MODE (decl) != BLKmode)
900 return DECL_INITIAL (decl);
901 return decl;
902}
903
904/* Perform default promotions for C data used in expressions.
905 Arrays and functions are converted to pointers;
906 enumeral types or short or char, to int.
907 In addition, manifest constants symbols are replaced by their values. */
908
909tree
910default_conversion (exp)
911 tree exp;
912{
913 register tree type = TREE_TYPE (exp);
914 register enum tree_code code = TREE_CODE (type);
915
916 /* Constants can be used directly unless they're not loadable. */
917 if (TREE_CODE (exp) == CONST_DECL)
918 exp = DECL_INITIAL (exp);
919 /* Replace a nonvolatile const static variable with its value. */
920 else if (optimize
921 && TREE_CODE (exp) == VAR_DECL
922 && TREE_READONLY (exp)
ec193993
RS
923 /* But not for iterators! */
924 && !ITERATOR_P (exp)
400fbf9f
JW
925 && DECL_MODE (exp) != BLKmode)
926 {
927 exp = decl_constant_value (exp);
928 type = TREE_TYPE (exp);
929 }
930
a7d53fce
RS
931 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
932 an lvalue. */
933 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
934 to integer and cause infinite recursion. */
935 while (TREE_CODE (exp) == NON_LVALUE_EXPR
936 || (TREE_CODE (exp) == NOP_EXPR
937 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
938 exp = TREE_OPERAND (exp, 0);
400fbf9f
JW
939
940 /* Normally convert enums to int,
941 but convert wide enums to something wider. */
942 if (code == ENUMERAL_TYPE)
943 {
944 type = type_for_size (MAX (TYPE_PRECISION (type),
945 TYPE_PRECISION (integer_type_node)),
86463d5d
RS
946 ((flag_traditional
947 || TYPE_PRECISION (type) >= TYPE_PRECISION (integer_type_node))
948 && TREE_UNSIGNED (type)));
400fbf9f
JW
949 return convert (type, exp);
950 }
951
d627ed1b 952 if (C_PROMOTING_INTEGER_TYPE_P (type))
400fbf9f 953 {
e83d45c4
RS
954 /* Traditionally, unsignedness is preserved in default promotions.
955 Also preserve unsignedness if not really getting any wider. */
956 if (TREE_UNSIGNED (type)
957 && (flag_traditional
958 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
400fbf9f
JW
959 return convert (unsigned_type_node, exp);
960 return convert (integer_type_node, exp);
961 }
19d76e60
RK
962 if (flag_traditional && !flag_allow_single_precision
963 && TYPE_MAIN_VARIANT (type) == float_type_node)
400fbf9f
JW
964 return convert (double_type_node, exp);
965 if (code == VOID_TYPE)
966 {
967 error ("void value not ignored as it ought to be");
968 return error_mark_node;
969 }
970 if (code == FUNCTION_TYPE)
971 {
972 return build_unary_op (ADDR_EXPR, exp, 0);
973 }
974 if (code == ARRAY_TYPE)
975 {
976 register tree adr;
977 tree restype = TREE_TYPE (type);
978 tree ptrtype;
d11fdb45
RS
979 int constp = 0;
980 int volatilep = 0;
981
982 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
983 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
984 {
985 constp = TREE_READONLY (exp);
986 volatilep = TREE_THIS_VOLATILE (exp);
987 }
988
989 if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
990 || constp || volatilep)
991 restype = c_build_type_variant (restype,
992 TYPE_READONLY (type) || constp,
993 TYPE_VOLATILE (type) || volatilep);
400fbf9f
JW
994
995 if (TREE_CODE (exp) == INDIRECT_REF)
996 return convert (TYPE_POINTER_TO (restype),
997 TREE_OPERAND (exp, 0));
998
999 if (TREE_CODE (exp) == COMPOUND_EXPR)
1000 {
1001 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1002 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1003 TREE_OPERAND (exp, 0), op1);
1004 }
1005
1006 if (!lvalue_p (exp)
1007 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1008 {
8efabd13
RS
1009 error ("invalid use of non-lvalue array");
1010 return error_mark_node;
400fbf9f
JW
1011 }
1012
400fbf9f
JW
1013 ptrtype = build_pointer_type (restype);
1014
1015 if (TREE_CODE (exp) == VAR_DECL)
1016 {
1017 /* ??? This is not really quite correct
1018 in that the type of the operand of ADDR_EXPR
1019 is not the target type of the type of the ADDR_EXPR itself.
1020 Question is, can this lossage be avoided? */
1021 adr = build1 (ADDR_EXPR, ptrtype, exp);
1022 if (mark_addressable (exp) == 0)
1023 return error_mark_node;
1024 TREE_CONSTANT (adr) = staticp (exp);
1025 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1026 return adr;
1027 }
1028 /* This way is better for a COMPONENT_REF since it can
1029 simplify the offset for a component. */
1030 adr = build_unary_op (ADDR_EXPR, exp, 1);
1031 return convert (ptrtype, adr);
1032 }
1033 return exp;
1034}
1035\f
19d76e60
RK
1036/* Look up component name in the structure type definition.
1037
1038 If this component name is found indirectly within an anonymous union,
1039 store in *INDIRECT the component which directly contains
1040 that anonymous union. Otherwise, set *INDIRECT to 0. */
2f2d13da
DE
1041
1042static tree
19d76e60 1043lookup_field (type, component, indirect)
2f2d13da 1044 tree type, component;
19d76e60 1045 tree *indirect;
2f2d13da
DE
1046{
1047 tree field;
1048
1049 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1050 to the field elements. Use a binary search on this array to quickly
1051 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1052 will always be set for structures which have many elements. */
1053
1054 if (TYPE_LANG_SPECIFIC (type))
1055 {
1056 int bot, top, half;
1057 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1058
1059 field = TYPE_FIELDS (type);
1060 bot = 0;
1061 top = TYPE_LANG_SPECIFIC (type)->len;
1062 while (top - bot > 1)
1063 {
c1719013 1064 HOST_WIDE_INT cmp;
2f2d13da
DE
1065
1066 half = (top - bot + 1) >> 1;
1067 field = field_array[bot+half];
1068
1069 if (DECL_NAME (field) == NULL_TREE)
1070 {
1071 /* Step through all anon unions in linear fashion. */
1072 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1073 {
19d76e60
RK
1074 tree anon, junk;
1075
2f2d13da 1076 field = field_array[bot++];
19d76e60 1077 anon = lookup_field (TREE_TYPE (field), component, &junk);
2f2d13da 1078 if (anon != NULL_TREE)
19d76e60
RK
1079 {
1080 *indirect = field;
1081 return anon;
1082 }
2f2d13da
DE
1083 }
1084
1085 /* Entire record is only anon unions. */
1086 if (bot > top)
1087 return NULL_TREE;
1088
1089 /* Restart the binary search, with new lower bound. */
1090 continue;
1091 }
1092
c1719013 1093 cmp = (HOST_WIDE_INT) DECL_NAME (field) - (HOST_WIDE_INT) component;
2f2d13da
DE
1094 if (cmp == 0)
1095 break;
1096 if (cmp < 0)
1097 bot += half;
1098 else
1099 top = bot + half;
1100 }
1101
1102 if (DECL_NAME (field_array[bot]) == component)
1103 field = field_array[bot];
1104 else if (DECL_NAME (field) != component)
1105 field = 0;
1106 }
1107 else
1108 {
1109 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1110 {
1111 if (DECL_NAME (field) == NULL_TREE)
1112 {
19d76e60
RK
1113 tree junk;
1114 tree anon = lookup_field (TREE_TYPE (field), component, &junk);
2f2d13da 1115 if (anon != NULL_TREE)
19d76e60
RK
1116 {
1117 *indirect = field;
1118 return anon;
1119 }
2f2d13da
DE
1120 }
1121
1122 if (DECL_NAME (field) == component)
1123 break;
1124 }
1125 }
1126
19d76e60 1127 *indirect = NULL_TREE;
2f2d13da
DE
1128 return field;
1129}
1130
400fbf9f
JW
1131/* Make an expression to refer to the COMPONENT field of
1132 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1133
1134tree
1135build_component_ref (datum, component)
1136 tree datum, component;
1137{
1138 register tree type = TREE_TYPE (datum);
1139 register enum tree_code code = TREE_CODE (type);
1140 register tree field = NULL;
1141 register tree ref;
1142
1143 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1144 unless we are not to support things not strictly ANSI. */
1145 switch (TREE_CODE (datum))
1146 {
1147 case COMPOUND_EXPR:
1148 {
1149 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
400fbf9f
JW
1150 return build (COMPOUND_EXPR, TREE_TYPE (value),
1151 TREE_OPERAND (datum, 0), value);
1152 }
1153 case COND_EXPR:
400fbf9f
JW
1154 return build_conditional_expr
1155 (TREE_OPERAND (datum, 0),
1156 build_component_ref (TREE_OPERAND (datum, 1), component),
1157 build_component_ref (TREE_OPERAND (datum, 2), component));
1158 }
1159
1160 /* See if there is a field or component with name COMPONENT. */
1161
1162 if (code == RECORD_TYPE || code == UNION_TYPE)
1163 {
19d76e60
RK
1164 tree indirect = 0;
1165
400fbf9f
JW
1166 if (TYPE_SIZE (type) == 0)
1167 {
8d9bfdc5 1168 incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
1169 return error_mark_node;
1170 }
1171
19d76e60 1172 field = lookup_field (type, component, &indirect);
400fbf9f
JW
1173
1174 if (!field)
1175 {
1176 error (code == RECORD_TYPE
1177 ? "structure has no member named `%s'"
1178 : "union has no member named `%s'",
1179 IDENTIFIER_POINTER (component));
1180 return error_mark_node;
1181 }
1182 if (TREE_TYPE (field) == error_mark_node)
1183 return error_mark_node;
1184
19d76e60
RK
1185 /* If FIELD was found buried within an anonymous union,
1186 make one COMPONENT_REF to get that anonymous union,
1187 then fall thru to make a second COMPONENT_REF to get FIELD. */
1188 if (indirect != 0)
1189 {
1190 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1191 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1192 TREE_READONLY (ref) = 1;
1193 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1194 TREE_THIS_VOLATILE (ref) = 1;
1195 datum = ref;
1196 }
1197
400fbf9f
JW
1198 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1199
1200 if (TREE_READONLY (datum) || TREE_READONLY (field))
1201 TREE_READONLY (ref) = 1;
1202 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1203 TREE_THIS_VOLATILE (ref) = 1;
1204
1205 return ref;
1206 }
1207 else if (code != ERROR_MARK)
1208 error ("request for member `%s' in something not a structure or union",
1209 IDENTIFIER_POINTER (component));
1210
1211 return error_mark_node;
1212}
1213\f
1214/* Given an expression PTR for a pointer, return an expression
1215 for the value pointed to.
1216 ERRORSTRING is the name of the operator to appear in error messages. */
1217
1218tree
1219build_indirect_ref (ptr, errorstring)
1220 tree ptr;
1221 char *errorstring;
1222{
1223 register tree pointer = default_conversion (ptr);
1224 register tree type = TREE_TYPE (pointer);
1225
1226 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b
RS
1227 {
1228 if (TREE_CODE (pointer) == ADDR_EXPR
1229 && !flag_volatile
1230 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1231 == TREE_TYPE (type)))
1232 return TREE_OPERAND (pointer, 0);
1233 else
1234 {
1235 tree t = TREE_TYPE (type);
1236 register tree ref = build1 (INDIRECT_REF,
1237 TYPE_MAIN_VARIANT (t), pointer);
400fbf9f 1238
870cc33b
RS
1239 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1240 {
1241 error ("dereferencing pointer to incomplete type");
1242 return error_mark_node;
1243 }
1244 if (TREE_CODE (t) == VOID_TYPE)
1245 warning ("dereferencing `void *' pointer");
1246
1247 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1248 so that we get the proper error message if the result is used
1249 to assign to. Also, &* is supposed to be a no-op.
1250 And ANSI C seems to specify that the type of the result
1251 should be the const type. */
1252 /* A de-reference of a pointer to const is not a const. It is valid
1253 to change it via some other pointer. */
1254 TREE_READONLY (ref) = TYPE_READONLY (t);
1255 TREE_SIDE_EFFECTS (ref)
1256 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
493692cd 1257 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
870cc33b
RS
1258 return ref;
1259 }
1260 }
400fbf9f
JW
1261 else if (TREE_CODE (pointer) != ERROR_MARK)
1262 error ("invalid type argument of `%s'", errorstring);
1263 return error_mark_node;
1264}
1265
1266/* This handles expressions of the form "a[i]", which denotes
1267 an array reference.
1268
1269 This is logically equivalent in C to *(a+i), but we may do it differently.
1270 If A is a variable or a member, we generate a primitive ARRAY_REF.
1271 This avoids forcing the array out of registers, and can work on
1272 arrays that are not lvalues (for example, members of structures returned
1273 by functions). */
1274
1275tree
1276build_array_ref (array, index)
1277 tree array, index;
1278{
1279 if (index == 0)
1280 {
1281 error ("subscript missing in array reference");
1282 return error_mark_node;
1283 }
1284
1285 if (TREE_TYPE (array) == error_mark_node
1286 || TREE_TYPE (index) == error_mark_node)
1287 return error_mark_node;
1288
1289 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1290 && TREE_CODE (array) != INDIRECT_REF)
1291 {
1292 tree rval, type;
1293
400fbf9f
JW
1294 /* Subscripting with type char is likely to lose
1295 on a machine where chars are signed.
1296 So warn on any machine, but optionally.
1297 Don't warn for unsigned char since that type is safe.
1298 Don't warn for signed char because anyone who uses that
1299 must have done so deliberately. */
1300 if (warn_char_subscripts
1301 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1302 warning ("array subscript has type `char'");
1303
0e51ef9b
RS
1304 /* Apply default promotions *after* noticing character types. */
1305 index = default_conversion (index);
1306
fdeefd49
RS
1307 /* Require integer *after* promotion, for sake of enums. */
1308 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1309 {
1310 error ("array subscript is not an integer");
1311 return error_mark_node;
1312 }
1313
400fbf9f
JW
1314 /* An array that is indexed by a non-constant
1315 cannot be stored in a register; we must be able to do
1316 address arithmetic on its address.
1317 Likewise an array of elements of variable size. */
1318 if (TREE_CODE (index) != INTEGER_CST
1319 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1320 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1321 {
1322 if (mark_addressable (array) == 0)
1323 return error_mark_node;
1324 }
e6d52559
JW
1325 /* An array that is indexed by a constant value which is not within
1326 the array bounds cannot be stored in a register either; because we
1327 would get a crash in store_bit_field/extract_bit_field when trying
1328 to access a non-existent part of the register. */
1329 if (TREE_CODE (index) == INTEGER_CST
1330 && TYPE_VALUES (TREE_TYPE (array))
1331 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1332 {
1333 if (mark_addressable (array) == 0)
1334 return error_mark_node;
1335 }
400fbf9f
JW
1336
1337 if (pedantic && !lvalue_p (array))
1338 {
1394aabd 1339 if (DECL_REGISTER (array))
400fbf9f
JW
1340 pedwarn ("ANSI C forbids subscripting `register' array");
1341 else
1342 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1343 }
1344
1345 if (pedantic)
1346 {
1347 tree foo = array;
1348 while (TREE_CODE (foo) == COMPONENT_REF)
1349 foo = TREE_OPERAND (foo, 0);
1394aabd 1350 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
400fbf9f
JW
1351 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1352 }
1353
1354 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1355 rval = build (ARRAY_REF, type, array, index);
1356 /* Array ref is const/volatile if the array elements are
1357 or if the array is. */
1358 TREE_READONLY (rval)
1359 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1360 | TREE_READONLY (array));
1361 TREE_SIDE_EFFECTS (rval)
1362 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1363 | TREE_SIDE_EFFECTS (array));
1364 TREE_THIS_VOLATILE (rval)
1365 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1366 /* This was added by rms on 16 Nov 91.
1367 It fixes vol struct foo *a; a->elts[1]
1368 in an inline function.
1369 Hope it doesn't break something else. */
1370 | TREE_THIS_VOLATILE (array));
1371 return require_complete_type (fold (rval));
1372 }
1373
1374 {
1375 tree ar = default_conversion (array);
1376 tree ind = default_conversion (index);
1377
1378 /* Put the integer in IND to simplify error checking. */
1379 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1380 {
1381 tree temp = ar;
1382 ar = ind;
1383 ind = temp;
1384 }
1385
1386 if (ar == error_mark_node)
1387 return ar;
1388
1389 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
1390 {
1391 error ("subscripted value is neither array nor pointer");
1392 return error_mark_node;
1393 }
1394 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1395 {
1396 error ("array subscript is not an integer");
1397 return error_mark_node;
1398 }
1399
1400 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1401 "array indexing");
1402 }
1403}
1404\f
400fbf9f
JW
1405/* Build a function call to function FUNCTION with parameters PARAMS.
1406 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1407 TREE_VALUE of each node is a parameter-expression.
1408 FUNCTION's data type may be a function type or a pointer-to-function. */
1409
1410tree
1411build_function_call (function, params)
1412 tree function, params;
1413{
346d29dc 1414 register tree fntype, fundecl = 0;
400fbf9f 1415 register tree coerced_params;
19d76e60 1416 tree name = NULL_TREE, assembler_name = NULL_TREE;
400fbf9f 1417
fc76e425 1418 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 1419 STRIP_TYPE_NOPS (function);
400fbf9f
JW
1420
1421 /* Convert anything with function type to a pointer-to-function. */
1422 if (TREE_CODE (function) == FUNCTION_DECL)
1423 {
1424 name = DECL_NAME (function);
19d76e60
RK
1425 assembler_name = DECL_ASSEMBLER_NAME (function);
1426
400fbf9f
JW
1427 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1428 (because calling an inline function does not mean the function
1429 needs to be separately compiled). */
1430 fntype = build_type_variant (TREE_TYPE (function),
1431 TREE_READONLY (function),
1432 TREE_THIS_VOLATILE (function));
9b7267b8 1433 fundecl = function;
400fbf9f
JW
1434 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1435 }
1436 else
1437 function = default_conversion (function);
1438
1439 fntype = TREE_TYPE (function);
1440
1441 if (TREE_CODE (fntype) == ERROR_MARK)
1442 return error_mark_node;
1443
1444 if (!(TREE_CODE (fntype) == POINTER_TYPE
1445 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1446 {
1447 error ("called object is not a function");
1448 return error_mark_node;
1449 }
1450
1451 /* fntype now gets the type of function pointed to. */
1452 fntype = TREE_TYPE (fntype);
1453
1454 /* Convert the parameters to the types declared in the
1455 function prototype, or apply default promotions. */
1456
1457 coerced_params
9b7267b8 1458 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
400fbf9f
JW
1459
1460 /* Check for errors in format strings. */
400fbf9f 1461
19d76e60
RK
1462 if (warn_format && (name || assembler_name))
1463 check_function_format (name, assembler_name, coerced_params);
400fbf9f
JW
1464
1465 /* Recognize certain built-in functions so we can make tree-codes
1466 other than CALL_EXPR. We do this when it enables fold-const.c
1467 to do something useful. */
1468
1469 if (TREE_CODE (function) == ADDR_EXPR
1470 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1471 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1472 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1473 {
1474 case BUILT_IN_ABS:
1475 case BUILT_IN_LABS:
1476 case BUILT_IN_FABS:
1477 if (coerced_params == 0)
1478 return integer_zero_node;
1479 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1480 }
1481
1482 {
1483 register tree result
1484 = build (CALL_EXPR, TREE_TYPE (fntype),
1485 function, coerced_params, NULL_TREE);
1486
1487 TREE_SIDE_EFFECTS (result) = 1;
1488 if (TREE_TYPE (result) == void_type_node)
1489 return result;
1490 return require_complete_type (result);
1491 }
1492}
1493\f
1494/* Convert the argument expressions in the list VALUES
1495 to the types in the list TYPELIST. The result is a list of converted
1496 argument expressions.
1497
1498 If TYPELIST is exhausted, or when an element has NULL as its type,
1499 perform the default conversions.
1500
1501 PARMLIST is the chain of parm decls for the function being called.
1502 It may be 0, if that info is not available.
1503 It is used only for generating error messages.
1504
1505 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1506
1507 This is also where warnings about wrong number of args are generated.
1508
1509 Both VALUES and the returned value are chains of TREE_LIST nodes
1510 with the elements of the list in the TREE_VALUE slots of those nodes. */
1511
1512static tree
9b7267b8
RS
1513convert_arguments (typelist, values, name, fundecl)
1514 tree typelist, values, name, fundecl;
400fbf9f
JW
1515{
1516 register tree typetail, valtail;
1517 register tree result = NULL;
1518 int parmnum;
1519
1520 /* Scan the given expressions and types, producing individual
1521 converted arguments and pushing them on RESULT in reverse order. */
1522
1523 for (valtail = values, typetail = typelist, parmnum = 0;
1524 valtail;
1525 valtail = TREE_CHAIN (valtail), parmnum++)
1526 {
1527 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1528 register tree val = TREE_VALUE (valtail);
1529
1530 if (type == void_type_node)
1531 {
1532 if (name)
1533 error ("too many arguments to function `%s'",
1534 IDENTIFIER_POINTER (name));
1535 else
1536 error ("too many arguments to function");
1537 break;
1538 }
1539
1540 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fc76e425
RS
1541 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1542 to convert automatically to a pointer. */
400fbf9f
JW
1543 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1544 val = TREE_OPERAND (val, 0);
1545
1546 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1547 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1548 val = default_conversion (val);
1549
1550 val = require_complete_type (val);
1551
1552 if (type != 0)
1553 {
1554 /* Formal parm type is specified by a function prototype. */
1555 tree parmval;
1556
1557 if (TYPE_SIZE (type) == 0)
1558 {
1559 error ("type of formal parameter %d is incomplete", parmnum + 1);
1560 parmval = val;
1561 }
1562 else
1563 {
1564 tree parmname;
36618528 1565 tree type0 = type;
400fbf9f
JW
1566#ifdef PROMOTE_PROTOTYPES
1567 /* Rather than truncating and then reextending,
1568 convert directly to int, if that's the type we will want. */
1569 if (! flag_traditional
7fb90b98
RK
1570 && (TREE_CODE (type) == INTEGER_TYPE
1571 || TREE_CODE (type) == ENUMERAL_TYPE)
400fbf9f
JW
1572 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1573 type = integer_type_node;
1574#endif
1575
1576#if 0 /* This turns out not to win--there's no way to write a prototype
1577 for a function whose arg type is a union with no tag. */
1578 /* Nameless union automatically casts the types it contains. */
1579 if (TREE_CODE (type) == UNION_TYPE && TYPE_NAME (type) == 0)
1580 {
1581 tree field;
1582
1583 for (field = TYPE_FIELDS (type); field;
1584 field = TREE_CHAIN (field))
1585 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
1586 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1587 break;
1588
1589 if (field)
1590 val = build1 (CONVERT_EXPR, type, val);
1591 }
1592#endif
1593
d45cf215
RS
1594 /* Optionally warn about conversions that
1595 differ from the default conversions. */
400fbf9f
JW
1596 if (warn_conversion)
1597 {
1598 int formal_prec = TYPE_PRECISION (type);
400fbf9f
JW
1599
1600 if (TREE_CODE (type) != REAL_TYPE
1601 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
754a4d82 1602 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
400fbf9f
JW
1603 else if (TREE_CODE (type) == REAL_TYPE
1604 && TREE_CODE (TREE_TYPE (val)) != REAL_TYPE)
754a4d82 1605 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
d45cf215
RS
1606 else if (TREE_CODE (type) == REAL_TYPE
1607 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1608 {
1609 /* Warn if any argument is passed as `float',
047de90b 1610 since without a prototype it would be `double'. */
d45cf215 1611 if (formal_prec == TYPE_PRECISION (float_type_node))
754a4d82 1612 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
d45cf215 1613 }
400fbf9f
JW
1614 /* Detect integer changing in width or signedness. */
1615 else if ((TREE_CODE (type) == INTEGER_TYPE
1616 || TREE_CODE (type) == ENUMERAL_TYPE)
1617 && (TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
d45cf215 1618 || TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE))
400fbf9f 1619 {
d45cf215
RS
1620 tree would_have_been = default_conversion (val);
1621 tree type1 = TREE_TYPE (would_have_been);
1622
754a4d82
RS
1623 if (TREE_CODE (type) == ENUMERAL_TYPE
1624 && type == TREE_TYPE (val))
1625 /* No warning if function asks for enum
1626 and the actual arg is that enum type. */
1627 ;
1628 else if (formal_prec != TYPE_PRECISION (type1))
1629 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
d45cf215
RS
1630 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1631 ;
800cd3b9
RS
1632 /* Don't complain if the formal parameter type
1633 is an enum, because we can't tell now whether
1634 the value was an enum--even the same enum. */
1635 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1636 ;
400fbf9f
JW
1637 else if (TREE_CODE (val) == INTEGER_CST
1638 && int_fits_type_p (val, type))
1639 /* Change in signedness doesn't matter
1640 if a constant value is unaffected. */
1641 ;
4bbbc5d9
RS
1642 /* Likewise for a constant in a NOP_EXPR. */
1643 else if (TREE_CODE (val) == NOP_EXPR
1644 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1645 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1646 ;
1647#if 0 /* We never get such tree structure here. */
047de90b
RS
1648 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1649 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1650 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1651 /* Change in signedness doesn't matter
1652 if an enum value is unaffected. */
1653 ;
4bbbc5d9 1654#endif
ce9895ae
RS
1655 /* If the value is extended from a narrower
1656 unsigned type, it doesn't matter whether we
1657 pass it as signed or unsigned; the value
1658 certainly is the same either way. */
1659 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1660 && TREE_UNSIGNED (TREE_TYPE (val)))
1661 ;
400fbf9f 1662 else if (TREE_UNSIGNED (type))
754a4d82 1663 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
400fbf9f 1664 else
754a4d82 1665 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
400fbf9f
JW
1666 }
1667 }
1668
1669 parmval = convert_for_assignment (type, val,
1670 (char *)0, /* arg passing */
9b7267b8 1671 fundecl, name, parmnum + 1);
400fbf9f
JW
1672
1673#ifdef PROMOTE_PROTOTYPES
7fb90b98
RK
1674 if ((TREE_CODE (type) == INTEGER_TYPE
1675 || TREE_CODE (type) == ENUMERAL_TYPE)
400fbf9f
JW
1676 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1677 parmval = default_conversion (parmval);
1678#endif
1679 }
8d9bfdc5 1680 result = tree_cons (NULL_TREE, parmval, result);
400fbf9f
JW
1681 }
1682 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1683 && (TYPE_PRECISION (TREE_TYPE (val))
1684 < TYPE_PRECISION (double_type_node)))
1685 /* Convert `float' to `double'. */
1686 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1687 else
1688 /* Convert `short' and `char' to full-size `int'. */
1689 result = tree_cons (NULL_TREE, default_conversion (val), result);
1690
1691 if (typetail)
1692 typetail = TREE_CHAIN (typetail);
1693 }
1694
1695 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1696 {
1697 if (name)
1698 error ("too few arguments to function `%s'",
1699 IDENTIFIER_POINTER (name));
1700 else
1701 error ("too few arguments to function");
1702 }
1703
1704 return nreverse (result);
1705}
1706\f
1707/* This is the entry point used by the parser
1708 for binary operators in the input.
1709 In addition to constructing the expression,
1710 we check for operands that were written with other binary operators
1711 in a way that is likely to confuse the user. */
edc7c4ec 1712
400fbf9f
JW
1713tree
1714parser_build_binary_op (code, arg1, arg2)
1715 enum tree_code code;
1716 tree arg1, arg2;
1717{
1718 tree result = build_binary_op (code, arg1, arg2, 1);
1719
1720 char class;
1721 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1722 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1723 enum tree_code code1 = ERROR_MARK;
1724 enum tree_code code2 = ERROR_MARK;
1725
1726 if (class1 == 'e' || class1 == '1'
1727 || class1 == '2' || class1 == '<')
1728 code1 = C_EXP_ORIGINAL_CODE (arg1);
1729 if (class2 == 'e' || class2 == '1'
1730 || class2 == '2' || class2 == '<')
1731 code2 = C_EXP_ORIGINAL_CODE (arg2);
1732
1733 /* Check for cases such as x+y<<z which users are likely
1734 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1735 is cleared to prevent these warnings. */
1736 if (warn_parentheses)
1737 {
1738 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1739 {
1740 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1741 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1742 warning ("suggest parentheses around + or - inside shift");
1743 }
1744
1745 if (code == TRUTH_ORIF_EXPR)
1746 {
1747 if (code1 == TRUTH_ANDIF_EXPR
1748 || code2 == TRUTH_ANDIF_EXPR)
1749 warning ("suggest parentheses around && within ||");
1750 }
1751
1752 if (code == BIT_IOR_EXPR)
1753 {
1754 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1755 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1756 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1757 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1758 warning ("suggest parentheses around arithmetic in operand of |");
1759 }
1760
1761 if (code == BIT_XOR_EXPR)
1762 {
1763 if (code1 == BIT_AND_EXPR
1764 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1765 || code2 == BIT_AND_EXPR
1766 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1767 warning ("suggest parentheses around arithmetic in operand of ^");
1768 }
1769
1770 if (code == BIT_AND_EXPR)
1771 {
1772 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1773 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1774 warning ("suggest parentheses around + or - in operand of &");
1775 }
1776 }
1777
001af587 1778 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
edc7c4ec 1779 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
001af587
RS
1780 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1781 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1782
e58cd767
RS
1783 unsigned_conversion_warning (result, arg1);
1784 unsigned_conversion_warning (result, arg2);
1785 overflow_warning (result);
1786
edc7c4ec
RS
1787 class = TREE_CODE_CLASS (TREE_CODE (result));
1788
400fbf9f
JW
1789 /* Record the code that was specified in the source,
1790 for the sake of warnings about confusing nesting. */
1791 if (class == 'e' || class == '1'
1792 || class == '2' || class == '<')
1793 C_SET_EXP_ORIGINAL_CODE (result, code);
1794 else
1795 {
1796 int flag = TREE_CONSTANT (result);
d11fdb45
RS
1797 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1798 so that convert_for_assignment wouldn't strip it.
1799 That way, we got warnings for things like p = (1 - 1).
1800 But it turns out we should not get those warnings. */
1801 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
400fbf9f
JW
1802 C_SET_EXP_ORIGINAL_CODE (result, code);
1803 TREE_CONSTANT (result) = flag;
1804 }
1805
1806 return result;
1807}
1808
1809/* Build a binary-operation expression without default conversions.
1810 CODE is the kind of expression to build.
1811 This function differs from `build' in several ways:
1812 the data type of the result is computed and recorded in it,
1813 warnings are generated if arg data types are invalid,
1814 special handling for addition and subtraction of pointers is known,
1815 and some optimization is done (operations on narrow ints
1816 are done in the narrower type when that gives the same result).
1817 Constant folding is also done before the result is returned.
1818
1819 Note that the operands will never have enumeral types, or function
1820 or array types, because either they will have the default conversions
1821 performed or they have both just been converted to some other type in which
1822 the arithmetic is to be done. */
1823
1824tree
1825build_binary_op (code, orig_op0, orig_op1, convert_p)
1826 enum tree_code code;
1827 tree orig_op0, orig_op1;
1828 int convert_p;
1829{
1830 tree type0, type1;
1831 register enum tree_code code0, code1;
1832 tree op0, op1;
1833
1834 /* Expression code to give to the expression when it is built.
1835 Normally this is CODE, which is what the caller asked for,
1836 but in some special cases we change it. */
1837 register enum tree_code resultcode = code;
1838
1839 /* Data type in which the computation is to be performed.
1840 In the simplest cases this is the common type of the arguments. */
1841 register tree result_type = NULL;
1842
1843 /* Nonzero means operands have already been type-converted
1844 in whatever way is necessary.
1845 Zero means they need to be converted to RESULT_TYPE. */
1846 int converted = 0;
1847
1848 /* Nonzero means after finally constructing the expression
1849 give it this type. Otherwise, give it type RESULT_TYPE. */
1850 tree final_type = 0;
1851
1852 /* Nonzero if this is an operation like MIN or MAX which can
1853 safely be computed in short if both args are promoted shorts.
1854 Also implies COMMON.
1855 -1 indicates a bitwise operation; this makes a difference
1856 in the exact conditions for when it is safe to do the operation
1857 in a narrower mode. */
1858 int shorten = 0;
1859
1860 /* Nonzero if this is a comparison operation;
1861 if both args are promoted shorts, compare the original shorts.
1862 Also implies COMMON. */
1863 int short_compare = 0;
1864
1865 /* Nonzero if this is a right-shift operation, which can be computed on the
1866 original short and then promoted if the operand is a promoted short. */
1867 int short_shift = 0;
1868
1869 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1870 int common = 0;
1871
1872 if (convert_p)
1873 {
1874 op0 = default_conversion (orig_op0);
1875 op1 = default_conversion (orig_op1);
1876 }
1877 else
1878 {
1879 op0 = orig_op0;
1880 op1 = orig_op1;
1881 }
1882
1883 type0 = TREE_TYPE (op0);
1884 type1 = TREE_TYPE (op1);
1885
1886 /* The expression codes of the data types of the arguments tell us
1887 whether the arguments are integers, floating, pointers, etc. */
1888 code0 = TREE_CODE (type0);
1889 code1 = TREE_CODE (type1);
1890
fc76e425 1891 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce
RS
1892 STRIP_TYPE_NOPS (op0);
1893 STRIP_TYPE_NOPS (op1);
400fbf9f
JW
1894
1895 /* If an error was already reported for one of the arguments,
1896 avoid reporting another error. */
1897
1898 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1899 return error_mark_node;
1900
1901 switch (code)
1902 {
1903 case PLUS_EXPR:
1904 /* Handle the pointer + int case. */
1905 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1906 return pointer_int_sum (PLUS_EXPR, op0, op1);
1907 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1908 return pointer_int_sum (PLUS_EXPR, op1, op0);
1909 else
1910 common = 1;
1911 break;
1912
1913 case MINUS_EXPR:
1914 /* Subtraction of two similar pointers.
1915 We must subtract them as integers, then divide by object size. */
1916 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1917 && comp_target_types (type0, type1))
1918 return pointer_diff (op0, op1);
1919 /* Handle pointer minus int. Just like pointer plus int. */
1920 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1921 return pointer_int_sum (MINUS_EXPR, op0, op1);
1922 else
1923 common = 1;
1924 break;
1925
1926 case MULT_EXPR:
1927 common = 1;
1928 break;
1929
1930 case TRUNC_DIV_EXPR:
1931 case CEIL_DIV_EXPR:
1932 case FLOOR_DIV_EXPR:
1933 case ROUND_DIV_EXPR:
1934 case EXACT_DIV_EXPR:
b6a10c9f
RS
1935 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1936 || code0 == COMPLEX_TYPE)
1937 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1938 || code1 == COMPLEX_TYPE))
400fbf9f
JW
1939 {
1940 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1941 resultcode = RDIV_EXPR;
1942 else
8b39ed65
TG
1943 {
1944 /* Although it would be tempting to shorten always here, that
1945 loses on some targets, since the modulo instruction is
1946 undefined if the quotient can't be represented in the
1947 computation mode. We shorten only if unsigned or if
1948 dividing by something we know != -1. */
1949 shorten = (TREE_UNSIGNED (orig_op0)
1950 || (TREE_CODE (op1) == INTEGER_CST
1951 && (TREE_INT_CST_LOW (op1) != -1
1952 || TREE_INT_CST_HIGH (op1) != -1)));
1953 }
400fbf9f
JW
1954 common = 1;
1955 }
1956 break;
1957
1958 case BIT_AND_EXPR:
1959 case BIT_ANDTC_EXPR:
1960 case BIT_IOR_EXPR:
1961 case BIT_XOR_EXPR:
1962 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
1963 shorten = -1;
1964 /* If one operand is a constant, and the other is a short type
1965 that has been converted to an int,
1966 really do the work in the short type and then convert the
1967 result to int. If we are lucky, the constant will be 0 or 1
1968 in the short type, making the entire operation go away. */
1969 if (TREE_CODE (op0) == INTEGER_CST
1970 && TREE_CODE (op1) == NOP_EXPR
1971 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
1972 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
1973 {
1974 final_type = result_type;
1975 op1 = TREE_OPERAND (op1, 0);
1976 result_type = TREE_TYPE (op1);
1977 }
1978 if (TREE_CODE (op1) == INTEGER_CST
1979 && TREE_CODE (op0) == NOP_EXPR
1980 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
1981 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
1982 {
1983 final_type = result_type;
1984 op0 = TREE_OPERAND (op0, 0);
1985 result_type = TREE_TYPE (op0);
1986 }
1987 break;
1988
1989 case TRUNC_MOD_EXPR:
047de90b 1990 case FLOOR_MOD_EXPR:
400fbf9f 1991 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
03d5b1f5
RS
1992 {
1993 /* Although it would be tempting to shorten always here, that loses
1994 on some targets, since the modulo instruction is undefined if the
1995 quotient can't be represented in the computation mode. We shorten
1996 only if unsigned or if dividing by something we know != -1. */
1997 shorten = (TREE_UNSIGNED (orig_op0)
1998 || (TREE_CODE (op1) == INTEGER_CST
1999 && (TREE_INT_CST_LOW (op1) != -1
2000 || TREE_INT_CST_HIGH (op1) != -1)));
2001 common = 1;
2002 }
400fbf9f
JW
2003 break;
2004
2005 case TRUTH_ANDIF_EXPR:
2006 case TRUTH_ORIF_EXPR:
2007 case TRUTH_AND_EXPR:
2008 case TRUTH_OR_EXPR:
1eca8b1e 2009 case TRUTH_XOR_EXPR:
b6a10c9f
RS
2010 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2011 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2012 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2013 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
400fbf9f
JW
2014 {
2015 /* Result of these operations is always an int,
2016 but that does not mean the operands should be
2017 converted to ints! */
2018 result_type = integer_type_node;
2019 op0 = truthvalue_conversion (op0);
2020 op1 = truthvalue_conversion (op1);
2021 converted = 1;
2022 }
2023 break;
2024
2025 /* Shift operations: result has same type as first operand;
2026 always convert second operand to int.
2027 Also set SHORT_SHIFT if shifting rightward. */
2028
2029 case RSHIFT_EXPR:
2030 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2031 {
2032 if (TREE_CODE (op1) == INTEGER_CST)
2033 {
17651386 2034 if (tree_int_cst_lt (op1, integer_zero_node))
315da535 2035 warning ("right shift count is negative");
17651386
RS
2036 else
2037 {
2038 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2039 short_shift = 1;
2040 if (TREE_INT_CST_HIGH (op1) != 0
2041 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2042 >= TYPE_PRECISION (type0)))
315da535 2043 warning ("right shift count >= width of type");
17651386 2044 }
400fbf9f 2045 }
d45cf215
RS
2046 /* Use the type of the value to be shifted.
2047 This is what most traditional C compilers do. */
2048 result_type = type0;
400fbf9f
JW
2049 /* Unless traditional, convert the shift-count to an integer,
2050 regardless of size of value being shifted. */
2051 if (! flag_traditional)
2052 {
6cb72a7d 2053 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
400fbf9f
JW
2054 op1 = convert (integer_type_node, op1);
2055 /* Avoid converting op1 to result_type later. */
2056 converted = 1;
2057 }
400fbf9f
JW
2058 }
2059 break;
2060
2061 case LSHIFT_EXPR:
2062 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2063 {
17651386
RS
2064 if (TREE_CODE (op1) == INTEGER_CST)
2065 {
2066 if (tree_int_cst_lt (op1, integer_zero_node))
315da535 2067 warning ("left shift count is negative");
17651386
RS
2068 else if (TREE_INT_CST_HIGH (op1) != 0
2069 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2070 >= TYPE_PRECISION (type0)))
315da535 2071 warning ("left shift count >= width of type");
17651386 2072 }
d45cf215
RS
2073 /* Use the type of the value to be shifted.
2074 This is what most traditional C compilers do. */
2075 result_type = type0;
400fbf9f
JW
2076 /* Unless traditional, convert the shift-count to an integer,
2077 regardless of size of value being shifted. */
2078 if (! flag_traditional)
2079 {
6cb72a7d 2080 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
400fbf9f
JW
2081 op1 = convert (integer_type_node, op1);
2082 /* Avoid converting op1 to result_type later. */
2083 converted = 1;
2084 }
400fbf9f
JW
2085 }
2086 break;
2087
2088 case RROTATE_EXPR:
2089 case LROTATE_EXPR:
2090 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2091 {
17651386
RS
2092 if (TREE_CODE (op1) == INTEGER_CST)
2093 {
2094 if (tree_int_cst_lt (op1, integer_zero_node))
2095 warning ("shift count is negative");
2096 else if (TREE_INT_CST_HIGH (op1) != 0
2097 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2098 >= TYPE_PRECISION (type0)))
2099 warning ("shift count >= width of type");
2100 }
d45cf215
RS
2101 /* Use the type of the value to be shifted.
2102 This is what most traditional C compilers do. */
2103 result_type = type0;
400fbf9f
JW
2104 /* Unless traditional, convert the shift-count to an integer,
2105 regardless of size of value being shifted. */
2106 if (! flag_traditional)
2107 {
6cb72a7d 2108 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
400fbf9f
JW
2109 op1 = convert (integer_type_node, op1);
2110 /* Avoid converting op1 to result_type later. */
2111 converted = 1;
2112 }
400fbf9f
JW
2113 }
2114 break;
2115
2116 case EQ_EXPR:
2117 case NE_EXPR:
2118 /* Result of comparison is always int,
2119 but don't convert the args to int! */
2120 result_type = integer_type_node;
2121 converted = 1;
b6a10c9f
RS
2122 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2123 || code0 == COMPLEX_TYPE)
2124 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2125 || code1 == COMPLEX_TYPE))
400fbf9f
JW
2126 short_compare = 1;
2127 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2128 {
2129 register tree tt0 = TREE_TYPE (type0);
2130 register tree tt1 = TREE_TYPE (type1);
2131 /* Anything compares with void *. void * compares with anything.
d11fdb45
RS
2132 Otherwise, the targets must be compatible
2133 and both must be object or both incomplete. */
400fbf9f
JW
2134 if (comp_target_types (type0, type1))
2135 ;
2136 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2137 {
fd5d5b94
RS
2138 /* op0 != orig_op0 detects the case of something
2139 whose value is 0 but which isn't a valid null ptr const. */
2140 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
400fbf9f
JW
2141 && TREE_CODE (tt1) == FUNCTION_TYPE)
2142 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2143 }
2144 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2145 {
fd5d5b94 2146 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
400fbf9f
JW
2147 && TREE_CODE (tt0) == FUNCTION_TYPE)
2148 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2149 }
2150 else
2151 pedwarn ("comparison of distinct pointer types lacks a cast");
2152 }
2153 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2154 && integer_zerop (op1))
2155 op1 = null_pointer_node;
2156 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2157 && integer_zerop (op0))
2158 op0 = null_pointer_node;
2159 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2160 {
2161 if (! flag_traditional)
2162 pedwarn ("comparison between pointer and integer");
2163 op1 = convert (TREE_TYPE (op0), op1);
2164 }
2165 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2166 {
2167 if (! flag_traditional)
2168 pedwarn ("comparison between pointer and integer");
2169 op0 = convert (TREE_TYPE (op1), op0);
2170 }
2171 else
2172 /* If args are not valid, clear out RESULT_TYPE
2173 to cause an error message later. */
2174 result_type = 0;
2175 break;
2176
2177 case MAX_EXPR:
2178 case MIN_EXPR:
9db931af
RS
2179 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2180 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
400fbf9f
JW
2181 shorten = 1;
2182 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2183 {
2184 if (! comp_target_types (type0, type1))
2185 pedwarn ("comparison of distinct pointer types lacks a cast");
2186 else if (pedantic
2187 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2188 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2189 result_type = common_type (type0, type1);
2190 }
2191 break;
2192
2193 case LE_EXPR:
2194 case GE_EXPR:
2195 case LT_EXPR:
2196 case GT_EXPR:
9db931af
RS
2197 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2198 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
400fbf9f
JW
2199 short_compare = 1;
2200 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2201 {
2202 if (! comp_target_types (type0, type1))
2203 pedwarn ("comparison of distinct pointer types lacks a cast");
d11fdb45
RS
2204 else if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2205 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2206 pedwarn ("comparison of complete and incomplete pointers");
400fbf9f
JW
2207 else if (pedantic
2208 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2209 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2210 result_type = integer_type_node;
2211 }
2212 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2213 && integer_zerop (op1))
2214 {
2215 result_type = integer_type_node;
2216 op1 = null_pointer_node;
840f571b 2217 if (pedantic)
400fbf9f
JW
2218 pedwarn ("ordered comparison of pointer with integer zero");
2219 }
2220 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2221 && integer_zerop (op0))
2222 {
2223 result_type = integer_type_node;
2224 op0 = null_pointer_node;
2225 if (pedantic)
2226 pedwarn ("ordered comparison of pointer with integer zero");
2227 }
2228 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2229 {
2230 result_type = integer_type_node;
2231 if (! flag_traditional)
2232 pedwarn ("comparison between pointer and integer");
2233 op1 = convert (TREE_TYPE (op0), op1);
2234 }
2235 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2236 {
2237 result_type = integer_type_node;
2238 if (! flag_traditional)
2239 pedwarn ("comparison between pointer and integer");
2240 op0 = convert (TREE_TYPE (op1), op0);
2241 }
2242 converted = 1;
2243 break;
2244 }
2245
b6a10c9f
RS
2246 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2247 &&
2248 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
400fbf9f 2249 {
b6a10c9f
RS
2250 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2251
400fbf9f
JW
2252 if (shorten || common || short_compare)
2253 result_type = common_type (type0, type1);
2254
2255 /* For certain operations (which identify themselves by shorten != 0)
2256 if both args were extended from the same smaller type,
2257 do the arithmetic in that type and then extend.
2258
2259 shorten !=0 and !=1 indicates a bitwise operation.
2260 For them, this optimization is safe only if
2261 both args are zero-extended or both are sign-extended.
2262 Otherwise, we might change the result.
2263 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2264 but calculated in (unsigned short) it would be (unsigned short)-1. */
2265
b6a10c9f 2266 if (shorten && none_complex)
400fbf9f
JW
2267 {
2268 int unsigned0, unsigned1;
2269 tree arg0 = get_narrower (op0, &unsigned0);
2270 tree arg1 = get_narrower (op1, &unsigned1);
2271 /* UNS is 1 if the operation to be done is an unsigned one. */
2272 int uns = TREE_UNSIGNED (result_type);
2273 tree type;
2274
2275 final_type = result_type;
2276
e7951b3f 2277 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
400fbf9f
JW
2278 but it *requires* conversion to FINAL_TYPE. */
2279
e7951b3f
RS
2280 if ((TYPE_PRECISION (TREE_TYPE (op0))
2281 == TYPE_PRECISION (TREE_TYPE (arg0)))
2282 && TREE_TYPE (op0) != final_type)
400fbf9f 2283 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
e7951b3f
RS
2284 if ((TYPE_PRECISION (TREE_TYPE (op1))
2285 == TYPE_PRECISION (TREE_TYPE (arg1)))
2286 && TREE_TYPE (op1) != final_type)
400fbf9f
JW
2287 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2288
2289 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2290
2291 /* For bitwise operations, signedness of nominal type
2292 does not matter. Consider only how operands were extended. */
2293 if (shorten == -1)
2294 uns = unsigned0;
2295
2296 /* Note that in all three cases below we refrain from optimizing
2297 an unsigned operation on sign-extended args.
2298 That would not be valid. */
2299
2300 /* Both args variable: if both extended in same way
2301 from same width, do it in that width.
2302 Do it unsigned if args were zero-extended. */
2303 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2304 < TYPE_PRECISION (result_type))
2305 && (TYPE_PRECISION (TREE_TYPE (arg1))
2306 == TYPE_PRECISION (TREE_TYPE (arg0)))
2307 && unsigned0 == unsigned1
2308 && (unsigned0 || !uns))
2309 result_type
2310 = signed_or_unsigned_type (unsigned0,
2311 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2312 else if (TREE_CODE (arg0) == INTEGER_CST
2313 && (unsigned1 || !uns)
2314 && (TYPE_PRECISION (TREE_TYPE (arg1))
2315 < TYPE_PRECISION (result_type))
2316 && (type = signed_or_unsigned_type (unsigned1,
2317 TREE_TYPE (arg1)),
2318 int_fits_type_p (arg0, type)))
2319 result_type = type;
2320 else if (TREE_CODE (arg1) == INTEGER_CST
2321 && (unsigned0 || !uns)
2322 && (TYPE_PRECISION (TREE_TYPE (arg0))
2323 < TYPE_PRECISION (result_type))
2324 && (type = signed_or_unsigned_type (unsigned0,
2325 TREE_TYPE (arg0)),
2326 int_fits_type_p (arg1, type)))
2327 result_type = type;
2328 }
2329
2330 /* Shifts can be shortened if shifting right. */
2331
2332 if (short_shift)
2333 {
2334 int unsigned_arg;
2335 tree arg0 = get_narrower (op0, &unsigned_arg);
2336
2337 final_type = result_type;
2338
2339 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2340 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2341
2342 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2343 /* If arg is sign-extended and then unsigned-shifted,
2344 we can simulate this with a signed shift in arg's type
2345 only if the extended result is at least twice as wide
2346 as the arg. Otherwise, the shift could use up all the
2347 ones made by sign-extension and bring in zeros.
2348 We can't optimize that case at all, but in most machines
2349 it never happens because available widths are 2**N. */
2350 && (!TREE_UNSIGNED (final_type)
2351 || unsigned_arg
2352 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2353 {
2354 /* Do an unsigned shift if the operand was zero-extended. */
2355 result_type
2356 = signed_or_unsigned_type (unsigned_arg,
2357 TREE_TYPE (arg0));
2358 /* Convert value-to-be-shifted to that type. */
2359 if (TREE_TYPE (op0) != result_type)
2360 op0 = convert (result_type, op0);
2361 converted = 1;
2362 }
2363 }
2364
2365 /* Comparison operations are shortened too but differently.
2366 They identify themselves by setting short_compare = 1. */
2367
75326e8c 2368 if (short_compare)
400fbf9f
JW
2369 {
2370 /* Don't write &op0, etc., because that would prevent op0
2371 from being kept in a register.
2372 Instead, make copies of the our local variables and
2373 pass the copies by reference, then copy them back afterward. */
2374 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2375 enum tree_code xresultcode = resultcode;
2376 tree val
2377 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2378 if (val != 0)
2379 return val;
2380 op0 = xop0, op1 = xop1, result_type = xresult_type;
2381 resultcode = xresultcode;
2382
2383 if (extra_warnings)
2384 {
2385 tree op0_type = TREE_TYPE (orig_op0);
2386 tree op1_type = TREE_TYPE (orig_op1);
2387 int op0_unsigned = TREE_UNSIGNED (op0_type);
2388 int op1_unsigned = TREE_UNSIGNED (op1_type);
2389
2390 /* Give warnings for comparisons between signed and unsigned
2391 quantities that will fail. Do not warn if the signed quantity
2392 is an unsuffixed integer literal (or some static constant
2393 expression involving such literals) and it is positive.
2394 Do not warn if the width of the unsigned quantity is less
2395 than that of the signed quantity, since in this case all
2396 values of the unsigned quantity fit in the signed quantity.
2397 Do not warn if the signed type is the same size as the
2398 result_type since sign extension does not cause trouble in
2399 this case. */
2400 /* Do the checking based on the original operand trees, so that
2401 casts will be considered, but default promotions won't be. */
2402 if (op0_unsigned != op1_unsigned
2403 && ((op0_unsigned
2404 && TYPE_PRECISION (op0_type) >= TYPE_PRECISION (op1_type)
2405 && TYPE_PRECISION (op0_type) < TYPE_PRECISION (result_type)
2406 && (TREE_CODE (op1) != INTEGER_CST
2407 || (TREE_CODE (op1) == INTEGER_CST
2408 && INT_CST_LT (op1, integer_zero_node))))
2409 ||
2410 (op1_unsigned
2411 && TYPE_PRECISION (op1_type) >= TYPE_PRECISION (op0_type)
2412 && TYPE_PRECISION (op1_type) < TYPE_PRECISION (result_type)
2413 && (TREE_CODE (op0) != INTEGER_CST
2414 || (TREE_CODE (op0) == INTEGER_CST
2415 && INT_CST_LT (op0, integer_zero_node))))))
2416 warning ("comparison between signed and unsigned");
2417 }
2418 }
2419 }
2420
2421 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2422 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2423 Then the expression will be built.
2424 It will be given type FINAL_TYPE if that is nonzero;
2425 otherwise, it will be given type RESULT_TYPE. */
2426
2427 if (!result_type)
2428 {
2429 binary_op_error (code);
2430 return error_mark_node;
2431 }
2432
2433 if (! converted)
2434 {
2435 if (TREE_TYPE (op0) != result_type)
2436 op0 = convert (result_type, op0);
2437 if (TREE_TYPE (op1) != result_type)
2438 op1 = convert (result_type, op1);
2439 }
2440
2441 {
2442 register tree result = build (resultcode, result_type, op0, op1);
2443 register tree folded;
2444
2445 folded = fold (result);
2446 if (folded == result)
2447 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2448 if (final_type != 0)
2449 return convert (final_type, folded);
2450 return folded;
2451 }
2452}
2453\f
2454/* Return a tree for the sum or difference (RESULTCODE says which)
2455 of pointer PTROP and integer INTOP. */
2456
2457static tree
2458pointer_int_sum (resultcode, ptrop, intop)
2459 enum tree_code resultcode;
2460 register tree ptrop, intop;
2461{
2462 tree size_exp;
2463
2464 register tree result;
2465 register tree folded;
2466
2467 /* The result is a pointer of the same type that is being added. */
2468
2469 register tree result_type = TREE_TYPE (ptrop);
2470
2471 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2472 {
2473 if (pedantic || warn_pointer_arith)
2474 pedwarn ("pointer of type `void *' used in arithmetic");
2475 size_exp = integer_one_node;
2476 }
2477 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2478 {
2479 if (pedantic || warn_pointer_arith)
2480 pedwarn ("pointer to a function used in arithmetic");
2481 size_exp = integer_one_node;
2482 }
2483 else
2484 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2485
2486 /* If what we are about to multiply by the size of the elements
2487 contains a constant term, apply distributive law
2488 and multiply that constant term separately.
2489 This helps produce common subexpressions. */
2490
2491 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2492 && ! TREE_CONSTANT (intop)
2493 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2494 && TREE_CONSTANT (size_exp)
2495 /* If the constant comes from pointer subtraction,
2496 skip this optimization--it would cause an error. */
2497 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE)
2498 {
2499 enum tree_code subcode = resultcode;
d45cf215 2500 tree int_type = TREE_TYPE (intop);
400fbf9f
JW
2501 if (TREE_CODE (intop) == MINUS_EXPR)
2502 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
d45cf215
RS
2503 /* Convert both subexpression types to the type of intop,
2504 because weird cases involving pointer arithmetic
2505 can result in a sum or difference with different type args. */
2506 ptrop = build_binary_op (subcode, ptrop,
2507 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2508 intop = convert (int_type, TREE_OPERAND (intop, 0));
400fbf9f
JW
2509 }
2510
2511 /* Convert the integer argument to a type the same size as a pointer
2512 so the multiply won't overflow spuriously. */
2513
2514 if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE)
2515 intop = convert (type_for_size (POINTER_SIZE, 0), intop);
2516
2517 /* Replace the integer argument
2518 with a suitable product by the object size. */
2519
2520 intop = build_binary_op (MULT_EXPR, intop, size_exp, 1);
2521
2522 /* Create the sum or difference. */
2523
2524 result = build (resultcode, result_type, ptrop, intop);
2525
2526 folded = fold (result);
2527 if (folded == result)
2528 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2529 return folded;
2530}
2531
2532/* Return a tree for the difference of pointers OP0 and OP1.
2533 The resulting tree has type int. */
2534
2535static tree
2536pointer_diff (op0, op1)
2537 register tree op0, op1;
2538{
2539 register tree result, folded;
2540 tree restype = ptrdiff_type_node;
2541
2542 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2543
2544 if (pedantic || warn_pointer_arith)
2545 {
2546 if (TREE_CODE (target_type) == VOID_TYPE)
2547 pedwarn ("pointer of type `void *' used in subtraction");
2548 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2549 pedwarn ("pointer to a function used in subtraction");
2550 }
2551
2552 /* First do the subtraction as integers;
2553 then drop through to build the divide operator. */
2554
2555 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2556 convert (restype, op1), 1);
ea8dd784
JW
2557 /* This generates an error if op1 is pointer to incomplete type. */
2558 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2559 error ("arithmetic on pointer to an incomplete type");
2560 /* This generates an error if op0 is pointer to incomplete type. */
400fbf9f
JW
2561 op1 = c_size_in_bytes (target_type);
2562
2563 /* Divide by the size, in easiest possible way. */
2564
2565 result = build (EXACT_DIV_EXPR, restype, op0, op1);
2566
2567 folded = fold (result);
2568 if (folded == result)
2569 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2570 return folded;
2571}
2572\f
2573/* Construct and perhaps optimize a tree representation
2574 for a unary operation. CODE, a tree_code, specifies the operation
2575 and XARG is the operand. NOCONVERT nonzero suppresses
2576 the default promotions (such as from short to int). */
2577
2578tree
2579build_unary_op (code, xarg, noconvert)
2580 enum tree_code code;
2581 tree xarg;
2582 int noconvert;
2583{
2584 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2585 register tree arg = xarg;
2586 register tree argtype = 0;
2587 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2588 char *errstring = NULL;
2589 tree val;
2590
2591 if (typecode == ERROR_MARK)
2592 return error_mark_node;
2593 if (typecode == ENUMERAL_TYPE)
2594 typecode = INTEGER_TYPE;
2595
2596 switch (code)
2597 {
2598 case CONVERT_EXPR:
2599 /* This is used for unary plus, because a CONVERT_EXPR
2600 is enough to prevent anybody from looking inside for
2601 associativity, but won't generate any code. */
b6a10c9f
RS
2602 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2603 || typecode == COMPLEX_TYPE))
400fbf9f
JW
2604 errstring = "wrong type argument to unary plus";
2605 else if (!noconvert)
2606 arg = default_conversion (arg);
2607 break;
2608
2609 case NEGATE_EXPR:
b6a10c9f
RS
2610 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2611 || typecode == COMPLEX_TYPE))
400fbf9f
JW
2612 errstring = "wrong type argument to unary minus";
2613 else if (!noconvert)
2614 arg = default_conversion (arg);
2615 break;
2616
2617 case BIT_NOT_EXPR:
1c2a9b35
RS
2618 if (typecode == COMPLEX_TYPE)
2619 {
2620 code = CONJ_EXPR;
2621 if (!noconvert)
2622 arg = default_conversion (arg);
2623 }
2624 else if (typecode != INTEGER_TYPE)
400fbf9f
JW
2625 errstring = "wrong type argument to bit-complement";
2626 else if (!noconvert)
2627 arg = default_conversion (arg);
2628 break;
2629
2630 case ABS_EXPR:
b6a10c9f
RS
2631 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2632 || typecode == COMPLEX_TYPE))
400fbf9f
JW
2633 errstring = "wrong type argument to abs";
2634 else if (!noconvert)
2635 arg = default_conversion (arg);
2636 break;
2637
1c2a9b35
RS
2638 case CONJ_EXPR:
2639 /* Conjugating a real value is a no-op, but allow it anyway. */
2640 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2641 || typecode == COMPLEX_TYPE))
2642 errstring = "wrong type argument to conjugation";
2643 else if (!noconvert)
2644 arg = default_conversion (arg);
2645 break;
2646
400fbf9f
JW
2647 case TRUTH_NOT_EXPR:
2648 if (typecode != INTEGER_TYPE
2649 && typecode != REAL_TYPE && typecode != POINTER_TYPE
b6a10c9f 2650 && typecode != COMPLEX_TYPE
400fbf9f
JW
2651 /* These will convert to a pointer. */
2652 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2653 {
2654 errstring = "wrong type argument to unary exclamation mark";
2655 break;
2656 }
2657 arg = truthvalue_conversion (arg);
2658 return invert_truthvalue (arg);
2659
2660 case NOP_EXPR:
2661 break;
b6a10c9f
RS
2662
2663 case REALPART_EXPR:
2664 if (TREE_CODE (arg) == COMPLEX_CST)
2665 return TREE_REALPART (arg);
2666 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2667 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2668 else
2669 return arg;
2670
2671 case IMAGPART_EXPR:
2672 if (TREE_CODE (arg) == COMPLEX_CST)
2673 return TREE_IMAGPART (arg);
2674 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2675 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2676 else
2677 return convert (TREE_TYPE (arg), integer_zero_node);
400fbf9f
JW
2678
2679 case PREINCREMENT_EXPR:
2680 case POSTINCREMENT_EXPR:
2681 case PREDECREMENT_EXPR:
2682 case POSTDECREMENT_EXPR:
2683 /* Handle complex lvalues (when permitted)
2684 by reduction to simpler cases. */
2685
2686 val = unary_complex_lvalue (code, arg);
2687 if (val != 0)
2688 return val;
2689
b6a10c9f
RS
2690 /* Increment or decrement the real part of the value,
2691 and don't change the imaginary part. */
2692 if (typecode == COMPLEX_TYPE)
2693 {
2694 tree real, imag;
2695
2696 arg = stabilize_reference (arg);
2697 real = build_unary_op (REALPART_EXPR, arg, 1);
2698 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2699 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2700 build_unary_op (code, real, 1), imag);
2701 }
2702
400fbf9f
JW
2703 /* Report invalid types. */
2704
2705 if (typecode != POINTER_TYPE
2706 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2707 {
2708 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2709 errstring ="wrong type argument to increment";
2710 else
2711 errstring ="wrong type argument to decrement";
2712 break;
2713 }
2714
2715 {
2716 register tree inc;
2717 tree result_type = TREE_TYPE (arg);
2718
2719 arg = get_unwidened (arg, 0);
2720 argtype = TREE_TYPE (arg);
2721
2722 /* Compute the increment. */
2723
2724 if (typecode == POINTER_TYPE)
2725 {
6bc4e3d0
RS
2726 /* If pointer target is an undefined struct,
2727 we just cannot know how to do the arithmetic. */
2728 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2729 error ("%s of pointer to unknown structure",
2730 ((code == PREINCREMENT_EXPR
2731 || code == POSTINCREMENT_EXPR)
2732 ? "increment" : "decrement"));
2733 else if ((pedantic || warn_pointer_arith)
2734 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2735 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
400fbf9f
JW
2736 pedwarn ("wrong type argument to %s",
2737 ((code == PREINCREMENT_EXPR
2738 || code == POSTINCREMENT_EXPR)
2739 ? "increment" : "decrement"));
2740 inc = c_sizeof_nowarn (TREE_TYPE (result_type));
2741 }
2742 else
2743 inc = integer_one_node;
2744
2745 inc = convert (argtype, inc);
2746
2747 /* Handle incrementing a cast-expression. */
2748
2749 while (1)
2750 switch (TREE_CODE (arg))
2751 {
2752 case NOP_EXPR:
2753 case CONVERT_EXPR:
2754 case FLOAT_EXPR:
2755 case FIX_TRUNC_EXPR:
2756 case FIX_FLOOR_EXPR:
2757 case FIX_ROUND_EXPR:
2758 case FIX_CEIL_EXPR:
ee71df46 2759 pedantic_lvalue_warning (CONVERT_EXPR);
400fbf9f
JW
2760 /* If the real type has the same machine representation
2761 as the type it is cast to, we can make better output
2762 by adding directly to the inside of the cast. */
2763 if ((TREE_CODE (TREE_TYPE (arg))
2764 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2765 && (TYPE_MODE (TREE_TYPE (arg))
2766 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2767 arg = TREE_OPERAND (arg, 0);
2768 else
2769 {
2770 tree incremented, modify, value;
400fbf9f
JW
2771 arg = stabilize_reference (arg);
2772 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2773 value = arg;
2774 else
2775 value = save_expr (arg);
2776 incremented = build (((code == PREINCREMENT_EXPR
2777 || code == POSTINCREMENT_EXPR)
2778 ? PLUS_EXPR : MINUS_EXPR),
2779 argtype, value, inc);
2780 TREE_SIDE_EFFECTS (incremented) = 1;
2781 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2782 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2783 TREE_USED (value) = 1;
2784 return value;
2785 }
2786 break;
2787
2788 default:
2789 goto give_up;
2790 }
2791 give_up:
2792
2793 /* Complain about anything else that is not a true lvalue. */
2794 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2795 || code == POSTINCREMENT_EXPR)
2796 ? "increment" : "decrement")))
2797 return error_mark_node;
2798
2799 /* Report a read-only lvalue. */
26b3c423 2800 if (TREE_READONLY (arg))
400fbf9f
JW
2801 readonly_warning (arg,
2802 ((code == PREINCREMENT_EXPR
2803 || code == POSTINCREMENT_EXPR)
2804 ? "increment" : "decrement"));
2805
2806 val = build (code, TREE_TYPE (arg), arg, inc);
2807 TREE_SIDE_EFFECTS (val) = 1;
2808 val = convert (result_type, val);
2809 if (TREE_CODE (val) != code)
2810 TREE_NO_UNUSED_WARNING (val) = 1;
2811 return val;
2812 }
2813
2814 case ADDR_EXPR:
2815 /* Note that this operation never does default_conversion
2816 regardless of NOCONVERT. */
2817
2818 /* Let &* cancel out to simplify resulting code. */
2819 if (TREE_CODE (arg) == INDIRECT_REF)
2820 {
2821 /* Don't let this be an lvalue. */
2822 if (lvalue_p (TREE_OPERAND (arg, 0)))
2823 return non_lvalue (TREE_OPERAND (arg, 0));
2824 return TREE_OPERAND (arg, 0);
2825 }
2826
2827 /* For &x[y], return x+y */
2828 if (TREE_CODE (arg) == ARRAY_REF)
2829 {
2830 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
2831 return error_mark_node;
2832 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2833 TREE_OPERAND (arg, 1), 1);
2834 }
2835
2836 /* Handle complex lvalues (when permitted)
2837 by reduction to simpler cases. */
2838 val = unary_complex_lvalue (code, arg);
2839 if (val != 0)
2840 return val;
2841
2842#if 0 /* Turned off because inconsistent;
2843 float f; *&(int)f = 3.4 stores in int format
2844 whereas (int)f = 3.4 stores in float format. */
2845 /* Address of a cast is just a cast of the address
2846 of the operand of the cast. */
2847 switch (TREE_CODE (arg))
2848 {
2849 case NOP_EXPR:
2850 case CONVERT_EXPR:
2851 case FLOAT_EXPR:
2852 case FIX_TRUNC_EXPR:
2853 case FIX_FLOOR_EXPR:
2854 case FIX_ROUND_EXPR:
2855 case FIX_CEIL_EXPR:
2856 if (pedantic)
2857 pedwarn ("ANSI C forbids the address of a cast expression");
2858 return convert (build_pointer_type (TREE_TYPE (arg)),
2859 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
2860 0));
2861 }
2862#endif
2863
2864 /* Allow the address of a constructor if all the elements
2865 are constant. */
2866 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
2867 ;
2868 /* Anything not already handled and not a true memory reference
2869 is an error. */
2870 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
2871 return error_mark_node;
2872
2873 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2874 argtype = TREE_TYPE (arg);
2875 /* If the lvalue is const or volatile,
2876 merge that into the type that the address will point to. */
2877 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
2878 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2879 {
2880 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
2881 argtype = c_build_type_variant (argtype,
2882 TREE_READONLY (arg),
2883 TREE_THIS_VOLATILE (arg));
2884 }
2885
2886 argtype = build_pointer_type (argtype);
2887
2888 if (mark_addressable (arg) == 0)
2889 return error_mark_node;
2890
2891 {
2892 tree addr;
2893
2894 if (TREE_CODE (arg) == COMPONENT_REF)
2895 {
2896 tree field = TREE_OPERAND (arg, 1);
2897
2898 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
2899
2900 if (DECL_BIT_FIELD (field))
2901 {
2902 error ("attempt to take address of bit-field structure member `%s'",
2903 IDENTIFIER_POINTER (DECL_NAME (field)));
2904 return error_mark_node;
2905 }
2906
2907 addr = convert (argtype, addr);
2908
2909 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
2910 {
2911 tree offset
2912 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
2913 size_int (BITS_PER_UNIT));
2914 int flag = TREE_CONSTANT (addr);
2915 addr = fold (build (PLUS_EXPR, argtype,
2916 addr, convert (argtype, offset)));
2917 TREE_CONSTANT (addr) = flag;
2918 }
2919 }
2920 else
2921 addr = build1 (code, argtype, arg);
2922
2923 /* Address of a static or external variable or
8706edbc
RS
2924 file-scope function counts as a constant. */
2925 if (staticp (arg)
2926 && ! (TREE_CODE (arg) == FUNCTION_DECL
2927 && DECL_CONTEXT (arg) != 0))
7d2d49af 2928 TREE_CONSTANT (addr) = 1;
400fbf9f
JW
2929 return addr;
2930 }
2931 }
2932
2933 if (!errstring)
2934 {
2935 if (argtype == 0)
2936 argtype = TREE_TYPE (arg);
2937 return fold (build1 (code, argtype, arg));
2938 }
2939
2940 error (errstring);
2941 return error_mark_node;
2942}
2943
2944#if 0
2945/* If CONVERSIONS is a conversion expression or a nested sequence of such,
2946 convert ARG with the same conversions in the same order
2947 and return the result. */
2948
2949static tree
2950convert_sequence (conversions, arg)
2951 tree conversions;
2952 tree arg;
2953{
2954 switch (TREE_CODE (conversions))
2955 {
2956 case NOP_EXPR:
2957 case CONVERT_EXPR:
2958 case FLOAT_EXPR:
2959 case FIX_TRUNC_EXPR:
2960 case FIX_FLOOR_EXPR:
2961 case FIX_ROUND_EXPR:
2962 case FIX_CEIL_EXPR:
2963 return convert (TREE_TYPE (conversions),
2964 convert_sequence (TREE_OPERAND (conversions, 0),
2965 arg));
2966
2967 default:
2968 return arg;
2969 }
2970}
2971#endif /* 0 */
2972
2973/* Return nonzero if REF is an lvalue valid for this language.
2974 Lvalues can be assigned, unless their type has TYPE_READONLY.
1394aabd 2975 Lvalues can have their address taken, unless they have DECL_REGISTER. */
400fbf9f
JW
2976
2977int
2978lvalue_p (ref)
2979 tree ref;
2980{
2981 register enum tree_code code = TREE_CODE (ref);
2982
2983 switch (code)
2984 {
b6a10c9f
RS
2985 case REALPART_EXPR:
2986 case IMAGPART_EXPR:
400fbf9f
JW
2987 case COMPONENT_REF:
2988 return lvalue_p (TREE_OPERAND (ref, 0));
2989
2990 case STRING_CST:
2991 return 1;
2992
2993 case INDIRECT_REF:
2994 case ARRAY_REF:
2995 case VAR_DECL:
2996 case PARM_DECL:
2997 case RESULT_DECL:
2998 case ERROR_MARK:
2999 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3000 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
3001 return 1;
3002 break;
3003 }
3004 return 0;
3005}
3006
3007/* Return nonzero if REF is an lvalue valid for this language;
3008 otherwise, print an error message and return zero. */
3009
3010int
3011lvalue_or_else (ref, string)
3012 tree ref;
3013 char *string;
3014{
3015 int win = lvalue_p (ref);
3016 if (! win)
3017 error ("invalid lvalue in %s", string);
3018 return win;
3019}
3020
3021/* Apply unary lvalue-demanding operator CODE to the expression ARG
3022 for certain kinds of expressions which are not really lvalues
3023 but which we can accept as lvalues.
3024
3025 If ARG is not a kind of expression we can handle, return zero. */
3026
3027static tree
3028unary_complex_lvalue (code, arg)
3029 enum tree_code code;
3030 tree arg;
3031{
3032 /* Handle (a, b) used as an "lvalue". */
3033 if (TREE_CODE (arg) == COMPOUND_EXPR)
3034 {
3035 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3036 pedantic_lvalue_warning (COMPOUND_EXPR);
3037 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3038 TREE_OPERAND (arg, 0), real_result);
3039 }
3040
3041 /* Handle (a ? b : c) used as an "lvalue". */
3042 if (TREE_CODE (arg) == COND_EXPR)
3043 {
3044 pedantic_lvalue_warning (COND_EXPR);
3045 return (build_conditional_expr
3046 (TREE_OPERAND (arg, 0),
3047 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3048 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3049 }
3050
3051 return 0;
3052}
3053
3054/* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3055 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3056
3057static void
3058pedantic_lvalue_warning (code)
3059 enum tree_code code;
3060{
3061 if (pedantic)
3062 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3063 code == COND_EXPR ? "conditional"
3064 : code == COMPOUND_EXPR ? "compound" : "cast");
3065}
3066\f
3067/* Warn about storing in something that is `const'. */
3068
3069void
3070readonly_warning (arg, string)
3071 tree arg;
3072 char *string;
3073{
3074 char buf[80];
3075 strcpy (buf, string);
3076
3791970d 3077 /* Forbid assignments to iterators. */
550707f7 3078 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3791970d
RS
3079 {
3080 strcat (buf, " of iterator `%s'");
550707f7 3081 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3791970d
RS
3082 }
3083
400fbf9f
JW
3084 if (TREE_CODE (arg) == COMPONENT_REF)
3085 {
3086 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3087 readonly_warning (TREE_OPERAND (arg, 0), string);
3088 else
3089 {
3090 strcat (buf, " of read-only member `%s'");
3091 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3092 }
3093 }
3094 else if (TREE_CODE (arg) == VAR_DECL)
3095 {
3096 strcat (buf, " of read-only variable `%s'");
3097 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3098 }
3099 else
3100 {
3101 pedwarn ("%s of read-only location", buf);
3102 }
3103}
3104\f
3105/* Mark EXP saying that we need to be able to take the
3106 address of it; it should not be allocated in a register.
3107 Value is 1 if successful. */
3108
3109int
3110mark_addressable (exp)
3111 tree exp;
3112{
3113 register tree x = exp;
3114 while (1)
3115 switch (TREE_CODE (x))
3116 {
3117 case ADDR_EXPR:
3118 case COMPONENT_REF:
3119 case ARRAY_REF:
ce95080d
RS
3120 case REALPART_EXPR:
3121 case IMAGPART_EXPR:
400fbf9f
JW
3122 x = TREE_OPERAND (x, 0);
3123 break;
3124
3125 case CONSTRUCTOR:
3126 TREE_ADDRESSABLE (x) = 1;
3127 return 1;
3128
3129 case VAR_DECL:
3130 case CONST_DECL:
3131 case PARM_DECL:
3132 case RESULT_DECL:
1394aabd
RS
3133 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3134 && DECL_NONLOCAL (x))
4bb6d2f8
RS
3135 {
3136 if (TREE_PUBLIC (x))
3137 {
3138 error ("global register variable `%s' used in nested function",
3139 IDENTIFIER_POINTER (DECL_NAME (x)));
3140 return 0;
3141 }
3142 pedwarn ("register variable `%s' used in nested function",
3143 IDENTIFIER_POINTER (DECL_NAME (x)));
3144 }
1394aabd 3145 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
400fbf9f
JW
3146 {
3147 if (TREE_PUBLIC (x))
3148 {
3149 error ("address of global register variable `%s' requested",
3150 IDENTIFIER_POINTER (DECL_NAME (x)));
3151 return 0;
3152 }
3153 pedwarn ("address of register variable `%s' requested",
3154 IDENTIFIER_POINTER (DECL_NAME (x)));
3155 }
3156 put_var_into_stack (x);
3157
3158 /* drops in */
3159 case FUNCTION_DECL:
3160 TREE_ADDRESSABLE (x) = 1;
3161#if 0 /* poplevel deals with this now. */
3162 if (DECL_CONTEXT (x) == 0)
3163 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3164#endif
3165
3166 default:
3167 return 1;
3168 }
3169}
3170\f
3171/* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3172
3173tree
3174build_conditional_expr (ifexp, op1, op2)
3175 tree ifexp, op1, op2;
3176{
3177 register tree type1;
3178 register tree type2;
3179 register enum tree_code code1;
3180 register enum tree_code code2;
3181 register tree result_type = NULL;
fd5d5b94 3182 tree orig_op1 = op1, orig_op2 = op2;
400fbf9f
JW
3183
3184 /* If second operand is omitted, it is the same as the first one;
3185 make sure it is calculated only once. */
3186 if (op1 == 0)
3187 {
3188 if (pedantic)
3189 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3190 ifexp = op1 = save_expr (ifexp);
3191 }
3192
3193 ifexp = truthvalue_conversion (default_conversion (ifexp));
3194
400fbf9f
JW
3195#if 0 /* Produces wrong result if within sizeof. */
3196 /* Don't promote the operands separately if they promote
3197 the same way. Return the unpromoted type and let the combined
3198 value get promoted if necessary. */
3199
3200 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3201 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3202 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3203 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3204 {
3205 if (TREE_CODE (ifexp) == INTEGER_CST)
a29f2ec1 3206 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
400fbf9f
JW
3207
3208 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3209 }
3210#endif
3211
e855c5ce 3212 /* Promote both alternatives. */
400fbf9f
JW
3213
3214 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3215 op1 = default_conversion (op1);
3216 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3217 op2 = default_conversion (op2);
3218
e855c5ce
RS
3219 if (TREE_CODE (ifexp) == ERROR_MARK
3220 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3221 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3222 return error_mark_node;
3223
400fbf9f
JW
3224 type1 = TREE_TYPE (op1);
3225 code1 = TREE_CODE (type1);
3226 type2 = TREE_TYPE (op2);
3227 code2 = TREE_CODE (type2);
3228
3229 /* Quickly detect the usual case where op1 and op2 have the same type
3230 after promotion. */
1ad409d2
RS
3231 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3232 {
3233 if (type1 == type2)
3234 result_type = type1;
3235 else
3236 result_type = TYPE_MAIN_VARIANT (type1);
3237 }
400fbf9f
JW
3238 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3239 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3240 {
3241 result_type = common_type (type1, type2);
3242 }
3243 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3244 {
3245 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3246 pedwarn ("ANSI C forbids conditional expr with only one void side");
3247 result_type = void_type_node;
3248 }
3249 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3250 {
3251 if (comp_target_types (type1, type2))
3252 result_type = common_type (type1, type2);
fd5d5b94
RS
3253 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3254 && TREE_CODE (orig_op1) != NOP_EXPR)
400fbf9f 3255 result_type = qualify_type (type2, type1);
fd5d5b94
RS
3256 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3257 && TREE_CODE (orig_op2) != NOP_EXPR)
400fbf9f
JW
3258 result_type = qualify_type (type1, type2);
3259 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3260 {
3261 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3262 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3263 result_type = qualify_type (type1, type2);
3264 }
3265 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3266 {
3267 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3268 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3269 result_type = qualify_type (type2, type1);
3270 }
3271 else
3272 {
3273 pedwarn ("pointer type mismatch in conditional expression");
3274 result_type = build_pointer_type (void_type_node);
3275 }
3276 }
3277 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3278 {
3279 if (! integer_zerop (op2))
3280 pedwarn ("pointer/integer type mismatch in conditional expression");
3281 else
3282 {
3283 op2 = null_pointer_node;
3284#if 0 /* The spec seems to say this is permitted. */
3285 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3286 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3287#endif
3288 }
3289 result_type = type1;
3290 }
3291 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3292 {
3293 if (!integer_zerop (op1))
3294 pedwarn ("pointer/integer type mismatch in conditional expression");
3295 else
3296 {
3297 op1 = null_pointer_node;
3298#if 0 /* The spec seems to say this is permitted. */
3299 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3300 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3301#endif
3302 }
3303 result_type = type2;
3304 }
3305
3306 if (!result_type)
3307 {
3308 if (flag_cond_mismatch)
3309 result_type = void_type_node;
3310 else
3311 {
3312 error ("type mismatch in conditional expression");
3313 return error_mark_node;
3314 }
3315 }
3316
1dfdf85d
RS
3317 /* Merge const and volatile flags of the incoming types. */
3318 result_type
3319 = build_type_variant (result_type,
48c73063
RS
3320 TREE_READONLY (op1) || TREE_READONLY (op2),
3321 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
e58cd767 3322
400fbf9f 3323 if (result_type != TREE_TYPE (op1))
e58cd767 3324 op1 = convert_and_check (result_type, op1);
400fbf9f 3325 if (result_type != TREE_TYPE (op2))
e58cd767 3326 op2 = convert_and_check (result_type, op2);
400fbf9f
JW
3327
3328#if 0
3329 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3330 {
3331 result_type = TREE_TYPE (op1);
3332 if (TREE_CONSTANT (ifexp))
a29f2ec1 3333 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
400fbf9f
JW
3334
3335 if (TYPE_MODE (result_type) == BLKmode)
3336 {
3337 register tree tempvar
3338 = build_decl (VAR_DECL, NULL_TREE, result_type);
3339 register tree xop1 = build_modify_expr (tempvar, op1);
3340 register tree xop2 = build_modify_expr (tempvar, op2);
3341 register tree result = fold (build (COND_EXPR, result_type,
3342 ifexp, xop1, xop2));
3343
3344 layout_decl (tempvar, TYPE_ALIGN (result_type));
3345 /* No way to handle variable-sized objects here.
3346 I fear that the entire handling of BLKmode conditional exprs
3347 needs to be redone. */
3348 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3349 abort ();
3350 DECL_RTL (tempvar)
3351 = assign_stack_local (DECL_MODE (tempvar),
3352 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3353 + BITS_PER_UNIT - 1)
3354 / BITS_PER_UNIT,
3355 0);
3356
3357 TREE_SIDE_EFFECTS (result)
3358 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3359 | TREE_SIDE_EFFECTS (op2);
3360 return build (COMPOUND_EXPR, result_type, result, tempvar);
3361 }
3362 }
3363#endif /* 0 */
5abb45f2
RS
3364
3365 if (TREE_CODE (ifexp) == INTEGER_CST)
a29f2ec1 3366 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
400fbf9f 3367
400fbf9f
JW
3368 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3369}
3370\f
3371/* Given a list of expressions, return a compound expression
3372 that performs them all and returns the value of the last of them. */
3373
3374tree
3375build_compound_expr (list)
3376 tree list;
82bde854 3377{
43a5a542 3378 return internal_build_compound_expr (list, TRUE);
82bde854
MM
3379}
3380
3381static tree
3382internal_build_compound_expr (list, first_p)
3383 tree list;
3384 int first_p;
400fbf9f
JW
3385{
3386 register tree rest;
3387
3388 if (TREE_CHAIN (list) == 0)
3389 {
6dc42e49 3390#if 0 /* If something inside inhibited lvalueness, we should not override. */
400fbf9f
JW
3391 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3392
3393 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3394 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3395 list = TREE_OPERAND (list, 0);
3396#endif
3397
439f6027 3398 /* Don't let (0, 0) be null pointer constant. */
82bde854 3399 if (!first_p && integer_zerop (TREE_VALUE (list)))
439f6027
RS
3400 return non_lvalue (TREE_VALUE (list));
3401 return TREE_VALUE (list);
400fbf9f
JW
3402 }
3403
3404 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3405 {
3406 /* Convert arrays to pointers when there really is a comma operator. */
3407 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3408 TREE_VALUE (TREE_CHAIN (list))
3409 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3410 }
3411
82bde854 3412 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
400fbf9f 3413
29df70e6
JW
3414 /* When pedantic, a compound expression can be neither an lvalue
3415 nor an integer constant expression. */
3416 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
400fbf9f
JW
3417 return rest;
3418
3419 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3420}
3421
3422/* Build an expression representing a cast to type TYPE of expression EXPR. */
3423
3424tree
3425build_c_cast (type, expr)
3426 register tree type;
3427 tree expr;
3428{
3429 register tree value = expr;
3430
3431 if (type == error_mark_node || expr == error_mark_node)
3432 return error_mark_node;
3433 type = TYPE_MAIN_VARIANT (type);
3434
3435#if 0
3436 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3437 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3438 value = TREE_OPERAND (value, 0);
3439#endif
3440
3441 if (TREE_CODE (type) == ARRAY_TYPE)
3442 {
3443 error ("cast specifies array type");
3444 return error_mark_node;
3445 }
3446
3447 if (TREE_CODE (type) == FUNCTION_TYPE)
3448 {
3449 error ("cast specifies function type");
3450 return error_mark_node;
3451 }
3452
3453 if (type == TREE_TYPE (value))
3454 {
3455 if (pedantic)
3456 {
3457 if (TREE_CODE (type) == RECORD_TYPE
3458 || TREE_CODE (type) == UNION_TYPE)
3459 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3460 }
3461 }
3462 else if (TREE_CODE (type) == UNION_TYPE)
3463 {
3464 tree field;
0c16ddf7
RS
3465 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3466 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3467 value = default_conversion (value);
3468
400fbf9f
JW
3469 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3470 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3471 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3472 break;
3473
3474 if (field)
3475 {
805f961c 3476 char *name;
281ec92f 3477 tree t;
805f961c 3478
400fbf9f
JW
3479 if (pedantic)
3480 pedwarn ("ANSI C forbids casts to union type");
805f961c
RS
3481 if (TYPE_NAME (type) != 0)
3482 {
3483 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3484 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3485 else
3486 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3487 }
3488 else
3489 name = "";
281ec92f
RS
3490 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3491 build_tree_list (field, value)),
3492 0, 0);
3493 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3494 return t;
400fbf9f
JW
3495 }
3496 error ("cast to union type from type not present in union");
3497 return error_mark_node;
3498 }
3499 else
3500 {
10d5caec 3501 tree otype, ovalue;
53b01f59
RS
3502
3503 /* If casting to void, avoid the error that would come
3504 from default_conversion in the case of a non-lvalue array. */
3505 if (type == void_type_node)
3506 return build1 (CONVERT_EXPR, type, value);
3507
400fbf9f
JW
3508 /* Convert functions and arrays to pointers,
3509 but don't convert any other types. */
3510 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3511 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3512 value = default_conversion (value);
3513 otype = TREE_TYPE (value);
3514
d45cf215 3515 /* Optionally warn about potentially worrisome casts. */
400fbf9f
JW
3516
3517 if (warn_cast_qual
3518 && TREE_CODE (type) == POINTER_TYPE
3519 && TREE_CODE (otype) == POINTER_TYPE)
3520 {
3521 if (TYPE_VOLATILE (TREE_TYPE (otype))
3522 && ! TYPE_VOLATILE (TREE_TYPE (type)))
3523 pedwarn ("cast discards `volatile' from pointer target type");
3524 if (TYPE_READONLY (TREE_TYPE (otype))
3525 && ! TYPE_READONLY (TREE_TYPE (type)))
3526 pedwarn ("cast discards `const' from pointer target type");
3527 }
3528
3529 /* Warn about possible alignment problems. */
d45cf215 3530 if (STRICT_ALIGNMENT && warn_cast_align
400fbf9f
JW
3531 && TREE_CODE (type) == POINTER_TYPE
3532 && TREE_CODE (otype) == POINTER_TYPE
3533 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3534 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3535 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3536 warning ("cast increases required alignment of target type");
400fbf9f
JW
3537
3538 if (TREE_CODE (type) == INTEGER_TYPE
3539 && TREE_CODE (otype) == POINTER_TYPE
c9b7f31c
RS
3540 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3541 && !TREE_CONSTANT (value))
400fbf9f
JW
3542 warning ("cast from pointer to integer of different size");
3543
3544 if (TREE_CODE (type) == POINTER_TYPE
3545 && TREE_CODE (otype) == INTEGER_TYPE
2918ed3c 3546 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
c9b7f31c 3547#if 0
2918ed3c
RS
3548 /* Don't warn about converting 0 to pointer,
3549 provided the 0 was explicit--not cast or made by folding. */
c9b7f31c
RS
3550 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3551#endif
3552 /* Don't warn about converting any constant. */
3553 && !TREE_CONSTANT (value))
400fbf9f
JW
3554 warning ("cast to pointer from integer of different size");
3555
10d5caec 3556 ovalue = value;
400fbf9f 3557 value = convert (type, value);
e58cd767
RS
3558
3559 /* Ignore any integer overflow caused by the cast. */
3560 if (TREE_CODE (value) == INTEGER_CST)
10d5caec
PE
3561 {
3562 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3563 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3564 }
400fbf9f
JW
3565 }
3566
fd5d5b94
RS
3567 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3568 if (pedantic && TREE_CODE (value) == INTEGER_CST
3569 && TREE_CODE (expr) == INTEGER_CST
3570 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3571 value = non_lvalue (value);
3572
3573 /* If pedantic, don't let a cast be an lvalue. */
400fbf9f 3574 if (value == expr && pedantic)
fd5d5b94
RS
3575 value = non_lvalue (value);
3576
400fbf9f
JW
3577 return value;
3578}
3579\f
3580/* Build an assignment expression of lvalue LHS from value RHS.
3581 MODIFYCODE is the code for a binary operator that we use
3582 to combine the old value of LHS with RHS to get the new value.
3583 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3584
3585tree
3586build_modify_expr (lhs, modifycode, rhs)
3587 tree lhs, rhs;
3588 enum tree_code modifycode;
3589{
3590 register tree result;
3591 tree newrhs;
3592 tree lhstype = TREE_TYPE (lhs);
3593 tree olhstype = lhstype;
3594
3595 /* Types that aren't fully specified cannot be used in assignments. */
3596 lhs = require_complete_type (lhs);
3597
3598 /* Avoid duplicate error messages from operands that had errors. */
3599 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3600 return error_mark_node;
3601
3602 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fc76e425
RS
3603 /* Do not use STRIP_NOPS here. We do not want an enumerator
3604 whose value is 0 to count as a null pointer constant. */
400fbf9f
JW
3605 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3606 rhs = TREE_OPERAND (rhs, 0);
3607
3608 newrhs = rhs;
3609
3610 /* Handle control structure constructs used as "lvalues". */
3611
3612 switch (TREE_CODE (lhs))
3613 {
3614 /* Handle (a, b) used as an "lvalue". */
3615 case COMPOUND_EXPR:
3616 pedantic_lvalue_warning (COMPOUND_EXPR);
19d76e60
RK
3617 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3618 modifycode, rhs);
3619 if (TREE_CODE (newrhs) == ERROR_MARK)
3620 return error_mark_node;
400fbf9f 3621 return build (COMPOUND_EXPR, lhstype,
19d76e60
RK
3622 TREE_OPERAND (lhs, 0), newrhs);
3623
400fbf9f
JW
3624 /* Handle (a ? b : c) used as an "lvalue". */
3625 case COND_EXPR:
3626 pedantic_lvalue_warning (COND_EXPR);
3627 rhs = save_expr (rhs);
3628 {
3629 /* Produce (a ? (b = rhs) : (c = rhs))
3630 except that the RHS goes through a save-expr
3631 so the code to compute it is only emitted once. */
3632 tree cond
3633 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3634 build_modify_expr (TREE_OPERAND (lhs, 1),
3635 modifycode, rhs),
3636 build_modify_expr (TREE_OPERAND (lhs, 2),
3637 modifycode, rhs));
19d76e60
RK
3638 if (TREE_CODE (cond) == ERROR_MARK)
3639 return cond;
400fbf9f
JW
3640 /* Make sure the code to compute the rhs comes out
3641 before the split. */
3642 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3643 /* But cast it to void to avoid an "unused" error. */
3644 convert (void_type_node, rhs), cond);
3645 }
3646 }
3647
3648 /* If a binary op has been requested, combine the old LHS value with the RHS
3649 producing the value we should actually store into the LHS. */
3650
3651 if (modifycode != NOP_EXPR)
3652 {
3653 lhs = stabilize_reference (lhs);
3654 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3655 }
3656
3657 /* Handle a cast used as an "lvalue".
3658 We have already performed any binary operator using the value as cast.
3659 Now convert the result to the cast type of the lhs,
3660 and then true type of the lhs and store it there;
3661 then convert result back to the cast type to be the value
3662 of the assignment. */
3663
3664 switch (TREE_CODE (lhs))
3665 {
3666 case NOP_EXPR:
3667 case CONVERT_EXPR:
3668 case FLOAT_EXPR:
3669 case FIX_TRUNC_EXPR:
3670 case FIX_FLOOR_EXPR:
3671 case FIX_ROUND_EXPR:
3672 case FIX_CEIL_EXPR:
3673 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3674 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3675 newrhs = default_conversion (newrhs);
3676 {
3677 tree inner_lhs = TREE_OPERAND (lhs, 0);
3678 tree result;
3679 result = build_modify_expr (inner_lhs, NOP_EXPR,
3680 convert (TREE_TYPE (inner_lhs),
3681 convert (lhstype, newrhs)));
19d76e60
RK
3682 if (TREE_CODE (result) == ERROR_MARK)
3683 return result;
400fbf9f
JW
3684 pedantic_lvalue_warning (CONVERT_EXPR);
3685 return convert (TREE_TYPE (lhs), result);
3686 }
3687 }
3688
3689 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3690 Reject anything strange now. */
3691
3692 if (!lvalue_or_else (lhs, "assignment"))
3693 return error_mark_node;
3694
3695 /* Warn about storing in something that is `const'. */
3696
3697 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3698 || ((TREE_CODE (lhstype) == RECORD_TYPE
3699 || TREE_CODE (lhstype) == UNION_TYPE)
3700 && C_TYPE_FIELDS_READONLY (lhstype)))
3701 readonly_warning (lhs, "assignment");
3702
3703 /* If storing into a structure or union member,
3704 it has probably been given type `int'.
3705 Compute the type that would go with
3706 the actual amount of storage the member occupies. */
3707
3708 if (TREE_CODE (lhs) == COMPONENT_REF
3709 && (TREE_CODE (lhstype) == INTEGER_TYPE
3710 || TREE_CODE (lhstype) == REAL_TYPE
3711 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3712 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3713
3714 /* If storing in a field that is in actuality a short or narrower than one,
3715 we must store in the field in its actual type. */
3716
3717 if (lhstype != TREE_TYPE (lhs))
3718 {
3719 lhs = copy_node (lhs);
3720 TREE_TYPE (lhs) = lhstype;
3721 }
3722
3723 /* Convert new value to destination type. */
3724
3725 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
9b7267b8 3726 NULL_TREE, NULL_TREE, 0);
400fbf9f
JW
3727 if (TREE_CODE (newrhs) == ERROR_MARK)
3728 return error_mark_node;
3729
3730 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3731 TREE_SIDE_EFFECTS (result) = 1;
3732
3733 /* If we got the LHS in a different type for storing in,
3734 convert the result back to the nominal type of LHS
3735 so that the value we return always has the same type
3736 as the LHS argument. */
3737
3738 if (olhstype == TREE_TYPE (result))
3739 return result;
9b7267b8
RS
3740 return convert_for_assignment (olhstype, result, "assignment",
3741 NULL_TREE, NULL_TREE, 0);
400fbf9f
JW
3742}
3743\f
3744/* Convert value RHS to type TYPE as preparation for an assignment
3745 to an lvalue of type TYPE.
3746 The real work of conversion is done by `convert'.
3747 The purpose of this function is to generate error messages
3748 for assignments that are not allowed in C.
3749 ERRTYPE is a string to use in error messages:
3750 "assignment", "return", etc. If it is null, this is parameter passing
d45cf215
RS
3751 for a function call (and different error messages are output). Otherwise,
3752 it may be a name stored in the spelling stack and interpreted by
3753 get_spelling.
400fbf9f
JW
3754
3755 FUNNAME is the name of the function being called,
3756 as an IDENTIFIER_NODE, or null.
3757 PARMNUM is the number of the argument, for printing in error messages. */
3758
3759static tree
9b7267b8 3760convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
400fbf9f
JW
3761 tree type, rhs;
3762 char *errtype;
9b7267b8 3763 tree fundecl, funname;
400fbf9f
JW
3764 int parmnum;
3765{
3766 register enum tree_code codel = TREE_CODE (type);
3767 register tree rhstype;
3768 register enum tree_code coder;
3769
3770 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fc76e425
RS
3771 /* Do not use STRIP_NOPS here. We do not want an enumerator
3772 whose value is 0 to count as a null pointer constant. */
400fbf9f
JW
3773 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3774 rhs = TREE_OPERAND (rhs, 0);
3775
3776 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3777 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3778 rhs = default_conversion (rhs);
3779
3780 rhstype = TREE_TYPE (rhs);
3781 coder = TREE_CODE (rhstype);
3782
3783 if (coder == ERROR_MARK)
3784 return error_mark_node;
3785
3786 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
e58cd767
RS
3787 {
3788 overflow_warning (rhs);
8b40563c
TW
3789 /* Check for Objective-C protocols. This will issue a warning if
3790 there are protocol violations. No need to use the return value. */
3791 maybe_objc_comptypes (type, rhstype, 0);
e58cd767
RS
3792 return rhs;
3793 }
400fbf9f
JW
3794
3795 if (coder == VOID_TYPE)
3796 {
3797 error ("void value not ignored as it ought to be");
3798 return error_mark_node;
3799 }
3800 /* Arithmetic types all interconvert, and enum is treated like int. */
b6a10c9f
RS
3801 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
3802 || codel == COMPLEX_TYPE)
400fbf9f 3803 &&
b6a10c9f 3804 (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
b037a230 3805 || coder == COMPLEX_TYPE))
da3c6115 3806 return convert_and_check (type, rhs);
9b7267b8 3807 /* Conversion to a union from its member types. */
db9e5545 3808 else if (codel == UNION_TYPE)
9b7267b8
RS
3809 {
3810 tree memb_types;
3811 for (memb_types = TYPE_FIELDS (type); memb_types;
3812 memb_types = TREE_CHAIN (memb_types))
3813 {
3814 if (comptypes (TREE_TYPE (memb_types), TREE_TYPE (rhs)))
3815 {
3816 if (pedantic
3817 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
3818 pedwarn ("ANSI C prohibits argument conversion to union type");
3819 return build1 (NOP_EXPR, type, rhs);
3820 }
2df34974
RS
3821 else if (coder == POINTER_TYPE
3822 && TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE)
3823 {
3824 tree memb_type = TREE_TYPE (memb_types);
3825 register tree ttl = TREE_TYPE (memb_type);
3826 register tree ttr = TREE_TYPE (rhstype);
3827
3828 /* Any non-function converts to a [const][volatile] void *
3829 and vice versa; otherwise, targets must be the same.
3830 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3831 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3832 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3833 || comp_target_types (memb_type, rhstype))
3834 {
3835 /* Const and volatile mean something different for function types,
3836 so the usual warnings are not appropriate. */
3837 if (TREE_CODE (ttr) != FUNCTION_TYPE
3838 || TREE_CODE (ttl) != FUNCTION_TYPE)
3839 {
3840 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
3841 warn_for_assignment ("%s discards `const' from pointer target type",
3842 get_spelling (errtype), funname, parmnum);
3843 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
3844 warn_for_assignment ("%s discards `volatile' from pointer target type",
3845 get_spelling (errtype), funname, parmnum);
3846 }
3847 else
3848 {
3849 /* Because const and volatile on functions are restrictions
3850 that say the function will not do certain things,
3851 it is okay to use a const or volatile function
3852 where an ordinary one is wanted, but not vice-versa. */
3853 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
3854 warn_for_assignment ("%s makes `const *' function pointer from non-const",
3855 get_spelling (errtype), funname, parmnum);
3856 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
3857 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
3858 get_spelling (errtype), funname, parmnum);
3859 }
3860 if (pedantic
3861 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
3862 pedwarn ("ANSI C prohibits argument conversion to union type");
3863 return build1 (NOP_EXPR, type, rhs);
3864 }
3865 }
9b7267b8
RS
3866 }
3867 }
400fbf9f
JW
3868 /* Conversions among pointers */
3869 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
3870 {
3871 register tree ttl = TREE_TYPE (type);
3872 register tree ttr = TREE_TYPE (rhstype);
3873
3874 /* Any non-function converts to a [const][volatile] void *
3875 and vice versa; otherwise, targets must be the same.
3876 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3877 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3878 || TYPE_MAIN_VARIANT (ttr) == void_type_node
790e9490
RS
3879 || comp_target_types (type, rhstype)
3880 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
3881 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
400fbf9f
JW
3882 {
3883 if (pedantic
3884 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
3885 && TREE_CODE (ttr) == FUNCTION_TYPE)
3886 ||
3887 (TYPE_MAIN_VARIANT (ttr) == void_type_node
fd5d5b94
RS
3888 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3889 which are not ANSI null ptr constants. */
3890 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
400fbf9f
JW
3891 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3892 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
d45cf215 3893 get_spelling (errtype), funname, parmnum);
400fbf9f
JW
3894 /* Const and volatile mean something different for function types,
3895 so the usual warnings are not appropriate. */
3896 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3897 || TREE_CODE (ttl) != FUNCTION_TYPE)
3898 {
3899 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
3900 warn_for_assignment ("%s discards `const' from pointer target type",
d45cf215 3901 get_spelling (errtype), funname, parmnum);
790e9490 3902 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
400fbf9f 3903 warn_for_assignment ("%s discards `volatile' from pointer target type",
d45cf215 3904 get_spelling (errtype), funname, parmnum);
790e9490
RS
3905 /* If this is not a case of ignoring a mismatch in signedness,
3906 no warning. */
3907 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
3908 || TYPE_MAIN_VARIANT (ttr) == void_type_node
3909 || comp_target_types (type, rhstype))
3910 ;
3911 /* If there is a mismatch, do warn. */
3912 else if (pedantic)
3913 warn_for_assignment ("pointer targets in %s differ in signedness",
3914 get_spelling (errtype), funname, parmnum);
400fbf9f
JW
3915 }
3916 else
3917 {
3918 /* Because const and volatile on functions are restrictions
3919 that say the function will not do certain things,
3920 it is okay to use a const or volatile function
3921 where an ordinary one is wanted, but not vice-versa. */
3922 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
3923 warn_for_assignment ("%s makes `const *' function pointer from non-const",
d45cf215 3924 get_spelling (errtype), funname, parmnum);
400fbf9f
JW
3925 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
3926 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
d45cf215 3927 get_spelling (errtype), funname, parmnum);
400fbf9f
JW
3928 }
3929 }
400fbf9f
JW
3930 else
3931 warn_for_assignment ("%s from incompatible pointer type",
d45cf215 3932 get_spelling (errtype), funname, parmnum);
400fbf9f
JW
3933 return convert (type, rhs);
3934 }
3935 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3936 {
2918ed3c 3937 /* An explicit constant 0 can convert to a pointer,
f1a2b955
RS
3938 or one that results from arithmetic, even including
3939 a cast to integer type. */
3940 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3941 &&
3942 ! (TREE_CODE (rhs) == NOP_EXPR
3943 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3944 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3945 && integer_zerop (TREE_OPERAND (rhs, 0))))
400fbf9f
JW
3946 {
3947 warn_for_assignment ("%s makes pointer from integer without a cast",
d45cf215 3948 get_spelling (errtype), funname, parmnum);
400fbf9f
JW
3949 return convert (type, rhs);
3950 }
3951 return null_pointer_node;
3952 }
3953 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3954 {
3955 warn_for_assignment ("%s makes integer from pointer without a cast",
d45cf215 3956 get_spelling (errtype), funname, parmnum);
400fbf9f
JW
3957 return convert (type, rhs);
3958 }
3959
3960 if (!errtype)
3961 {
3962 if (funname)
8b40563c
TW
3963 {
3964 tree selector = maybe_building_objc_message_expr ();
3965
3966 if (selector && parmnum > 2)
3967 error ("incompatible type for argument %d of `%s'",
3968 parmnum - 2, IDENTIFIER_POINTER (selector));
3969 else
3970 error ("incompatible type for argument %d of `%s'",
3971 parmnum, IDENTIFIER_POINTER (funname));
3972 }
400fbf9f
JW
3973 else
3974 error ("incompatible type for argument %d of indirect function call",
3975 parmnum);
3976 }
3977 else
d45cf215 3978 error ("incompatible types in %s", get_spelling (errtype));
400fbf9f
JW
3979
3980 return error_mark_node;
3981}
3982
3983/* Print a warning using MSG.
3984 It gets OPNAME as its one parameter.
3985 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3986 FUNCTION and ARGNUM are handled specially if we are building an
3987 Objective-C selector. */
3988
3989static void
3990warn_for_assignment (msg, opname, function, argnum)
3991 char *msg;
3992 char *opname;
3993 tree function;
3994 int argnum;
3995{
3996 static char argstring[] = "passing arg %d of `%s'";
3997 static char argnofun[] = "passing arg %d";
3998
3999 if (opname == 0)
4000 {
4001 tree selector = maybe_building_objc_message_expr ();
4002
4003 if (selector && argnum > 2)
4004 {
4005 function = selector;
4006 argnum -= 2;
4007 }
4008 if (function)
4009 {
4010 /* Function name is known; supply it. */
4011 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4012 + sizeof (argstring) + 25 /*%d*/ + 1);
4013 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4014 }
4015 else
4016 {
4017 /* Function name unknown (call through ptr); just give arg number. */
4018 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4019 sprintf (opname, argnofun, argnum);
4020 }
4021 }
4022 pedwarn (msg, opname);
4023}
4024\f
4025/* Return nonzero if VALUE is a valid constant-valued expression
4026 for use in initializing a static variable; one that can be an
4027 element of a "constant" initializer.
4028
4029 Return null_pointer_node if the value is absolute;
4030 if it is relocatable, return the variable that determines the relocation.
4031 We assume that VALUE has been folded as much as possible;
4032 therefore, we do not need to check for such things as
4033 arithmetic-combinations of integers. */
4034
4035static tree
f0c70ef0 4036initializer_constant_valid_p (value, endtype)
400fbf9f 4037 tree value;
f0c70ef0 4038 tree endtype;
400fbf9f
JW
4039{
4040 switch (TREE_CODE (value))
4041 {
4042 case CONSTRUCTOR:
281ec92f
RS
4043 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4044 && TREE_CONSTANT (value))
4045 return initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)));
4046
400fbf9f
JW
4047 return TREE_STATIC (value) ? null_pointer_node : 0;
4048
4049 case INTEGER_CST:
4050 case REAL_CST:
4051 case STRING_CST:
466e9220 4052 case COMPLEX_CST:
400fbf9f
JW
4053 return null_pointer_node;
4054
4055 case ADDR_EXPR:
4056 return TREE_OPERAND (value, 0);
4057
4058 case NON_LVALUE_EXPR:
f0c70ef0 4059 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
400fbf9f
JW
4060
4061 case CONVERT_EXPR:
4062 case NOP_EXPR:
4063 /* Allow conversions between pointer types. */
4064 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4065 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
f0c70ef0 4066 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
400fbf9f
JW
4067 /* Allow conversions between real types. */
4068 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4069 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
f0c70ef0 4070 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
400fbf9f
JW
4071 /* Allow length-preserving conversions between integer types. */
4072 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4073 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4074 && tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (value)),
4075 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value, 0)))))
f0c70ef0 4076 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
400fbf9f
JW
4077 /* Allow conversions between integer types only if explicit value. */
4078 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4079 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4080 {
f0c70ef0
RS
4081 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4082 endtype);
400fbf9f
JW
4083 if (inner == null_pointer_node)
4084 return null_pointer_node;
4085 return 0;
4086 }
9b7267b8 4087 /* Allow (int) &foo provided int is as wide as a pointer. */
400fbf9f
JW
4088 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4089 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
9b7267b8
RS
4090 && ! tree_int_cst_lt (TYPE_SIZE (TREE_TYPE (value)),
4091 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value, 0)))))
f0c70ef0
RS
4092 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4093 endtype);
805f961c
RS
4094 /* Allow conversions to union types if the value inside is okay. */
4095 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
f0c70ef0
RS
4096 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4097 endtype);
400fbf9f
JW
4098 return 0;
4099
4100 case PLUS_EXPR:
1bbe9280
RS
4101 if (TREE_CODE (endtype) == INTEGER_TYPE
4102 && TYPE_PRECISION (endtype) < POINTER_SIZE)
f0c70ef0 4103 return 0;
400fbf9f 4104 {
f0c70ef0
RS
4105 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4106 endtype);
4107 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4108 endtype);
400fbf9f
JW
4109 /* If either term is absolute, use the other terms relocation. */
4110 if (valid0 == null_pointer_node)
4111 return valid1;
4112 if (valid1 == null_pointer_node)
4113 return valid0;
4114 return 0;
4115 }
4116
4117 case MINUS_EXPR:
1bbe9280
RS
4118 if (TREE_CODE (endtype) == INTEGER_TYPE
4119 && TYPE_PRECISION (endtype) < POINTER_SIZE)
f0c70ef0 4120 return 0;
400fbf9f 4121 {
f0c70ef0
RS
4122 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4123 endtype);
4124 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4125 endtype);
400fbf9f
JW
4126 /* Win if second argument is absolute. */
4127 if (valid1 == null_pointer_node)
4128 return valid0;
4129 /* Win if both arguments have the same relocation.
4130 Then the value is absolute. */
4131 if (valid0 == valid1)
4132 return null_pointer_node;
4133 return 0;
4134 }
4135 }
4136
4137 return 0;
4138}
d9fc6069
JW
4139
4140/* If VALUE is a compound expr all of whose expressions are constant, then
4141 return its value. Otherwise, return error_mark_node.
4142
4143 This is for handling COMPOUND_EXPRs as initializer elements
4144 which is allowed with a warning when -pedantic is specified. */
4145
4146static tree
4147valid_compound_expr_initializer (value, endtype)
4148 tree value;
4149 tree endtype;
4150{
4151 if (TREE_CODE (value) == COMPOUND_EXPR)
4152 {
4153 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4154 == error_mark_node)
4155 return error_mark_node;
4156 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4157 endtype);
4158 }
4159 else if (! TREE_CONSTANT (value)
4160 && ! initializer_constant_valid_p (value, endtype))
4161 return error_mark_node;
4162 else
4163 return value;
4164}
400fbf9f
JW
4165\f
4166/* Perform appropriate conversions on the initial value of a variable,
4167 store it in the declaration DECL,
4168 and print any error messages that are appropriate.
4169 If the init is invalid, store an ERROR_MARK. */
4170
4171void
4172store_init_value (decl, init)
4173 tree decl, init;
4174{
4175 register tree value, type;
4176
4177 /* If variable's type was invalidly declared, just ignore it. */
4178
4179 type = TREE_TYPE (decl);
4180 if (TREE_CODE (type) == ERROR_MARK)
4181 return;
4182
4183 /* Digest the specified initializer into an expression. */
4184
790e9490
RS
4185 value = digest_init (type, init, TREE_STATIC (decl),
4186 TREE_STATIC (decl) || pedantic);
400fbf9f
JW
4187
4188 /* Store the expression if valid; else report error. */
4189
4190#if 0
4191 /* Note that this is the only place we can detect the error
4192 in a case such as struct foo bar = (struct foo) { x, y };
d45cf215 4193 where there is one initial value which is a constructor expression. */
400fbf9f
JW
4194 if (value == error_mark_node)
4195 ;
4196 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4197 {
4198 error ("initializer for static variable is not constant");
4199 value = error_mark_node;
4200 }
4201 else if (TREE_STATIC (decl)
f0c70ef0 4202 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
400fbf9f
JW
4203 {
4204 error ("initializer for static variable uses complicated arithmetic");
4205 value = error_mark_node;
4206 }
4207 else
4208 {
4209 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4210 {
4211 if (! TREE_CONSTANT (value))
4212 pedwarn ("aggregate initializer is not constant");
4213 else if (! TREE_STATIC (value))
4214 pedwarn ("aggregate initializer uses complicated arithmetic");
4215 }
4216 }
4217#endif
4218
10d5caec
PE
4219 DECL_INITIAL (decl) = value;
4220
26b3c423 4221 /* ANSI wants warnings about out-of-range constant initializers. */
10d5caec 4222 STRIP_TYPE_NOPS (value);
26b3c423 4223 constant_expression_warning (value);
400fbf9f
JW
4224}
4225\f
075fc632 4226/* Methods for storing and printing names for error messages. */
d45cf215
RS
4227
4228/* Implement a spelling stack that allows components of a name to be pushed
4229 and popped. Each element on the stack is this structure. */
4230
4231struct spelling
4232{
4233 int kind;
4234 union
4235 {
4236 int i;
4237 char *s;
4238 } u;
4239};
4240
4241#define SPELLING_STRING 1
4242#define SPELLING_MEMBER 2
4243#define SPELLING_BOUNDS 3
4244
4245static struct spelling *spelling; /* Next stack element (unused). */
4246static struct spelling *spelling_base; /* Spelling stack base. */
4247static int spelling_size; /* Size of the spelling stack. */
4248
4249/* Macros to save and restore the spelling stack around push_... functions.
4250 Alternative to SAVE_SPELLING_STACK. */
4251
4252#define SPELLING_DEPTH() (spelling - spelling_base)
4253#define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4254
4255/* Save and restore the spelling stack around arbitrary C code. */
4256
4257#define SAVE_SPELLING_DEPTH(code) \
4258{ \
4259 int __depth = SPELLING_DEPTH (); \
4260 code; \
4261 RESTORE_SPELLING_DEPTH (__depth); \
4262}
4263
4264/* Push an element on the spelling stack with type KIND and assign VALUE
4265 to MEMBER. */
4266
4267#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4268{ \
4269 int depth = SPELLING_DEPTH (); \
4270 \
4271 if (depth >= spelling_size) \
4272 { \
4273 spelling_size += 10; \
4274 if (spelling_base == 0) \
4275 spelling_base \
4276 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4277 else \
4278 spelling_base \
4279 = (struct spelling *) xrealloc (spelling_base, \
4280 spelling_size * sizeof (struct spelling)); \
4281 RESTORE_SPELLING_DEPTH (depth); \
4282 } \
4283 \
4284 spelling->kind = (KIND); \
4285 spelling->MEMBER = (VALUE); \
4286 spelling++; \
4287}
4288
4289/* Push STRING on the stack. Printed literally. */
4290
4291static void
4292push_string (string)
4293 char *string;
4294{
4295 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4296}
4297
4298/* Push a member name on the stack. Printed as '.' STRING. */
4299
4300static void
19d76e60
RK
4301push_member_name (decl)
4302 tree decl;
4303
d45cf215 4304{
19d76e60
RK
4305 char *string
4306 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
d45cf215
RS
4307 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4308}
4309
4310/* Push an array bounds on the stack. Printed as [BOUNDS]. */
4311
4312static void
4313push_array_bounds (bounds)
4314 int bounds;
4315{
4316 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4317}
4318
4319/* Compute the maximum size in bytes of the printed spelling. */
4320
4321static int
4322spelling_length ()
4323{
4324 register int size = 0;
4325 register struct spelling *p;
4326
4327 for (p = spelling_base; p < spelling; p++)
4328 {
4329 if (p->kind == SPELLING_BOUNDS)
4330 size += 25;
4331 else
4332 size += strlen (p->u.s) + 1;
4333 }
4334
4335 return size;
4336}
4337
4338/* Print the spelling to BUFFER and return it. */
4339
4340static char *
4341print_spelling (buffer)
4342 register char *buffer;
4343{
4344 register char *d = buffer;
4345 register char *s;
4346 register struct spelling *p;
4347
4348 for (p = spelling_base; p < spelling; p++)
4349 if (p->kind == SPELLING_BOUNDS)
4350 {
4351 sprintf (d, "[%d]", p->u.i);
4352 d += strlen (d);
4353 }
4354 else
4355 {
4356 if (p->kind == SPELLING_MEMBER)
4357 *d++ = '.';
4358 for (s = p->u.s; *d = *s++; d++)
4359 ;
4360 }
4361 *d++ = '\0';
4362 return buffer;
4363}
4364
4365/* Provide a means to pass component names derived from the spelling stack. */
4366
4367char initialization_message;
4368
4369/* Interpret the spelling of the given ERRTYPE message. */
4370
4371static char *
4372get_spelling (errtype)
4373 char *errtype;
4374{
4375 static char *buffer;
4376 static int size = -1;
4377
4378 if (errtype == &initialization_message)
4379 {
4380 /* Avoid counting chars */
4381 static char message[] = "initialization of `%s'";
4382 register int needed = sizeof (message) + spelling_length () + 1;
047de90b 4383 char *temp;
d45cf215
RS
4384
4385 if (size < 0)
4386 buffer = (char *) xmalloc (size = needed);
4387 if (needed > size)
4388 buffer = (char *) xrealloc (buffer, size = needed);
4389
047de90b
RS
4390 temp = (char *) alloca (needed);
4391 sprintf (buffer, message, print_spelling (temp));
d45cf215
RS
4392 return buffer;
4393 }
4394
4395 return errtype;
4396}
4397
400fbf9f
JW
4398/* Issue an error message for a bad initializer component.
4399 FORMAT describes the message. OFWHAT is the name for the component.
4400 LOCAL is a format string for formatting the insertion of the name
4401 into the message.
4402
d45cf215 4403 If OFWHAT is null, the component name is stored on the spelling stack.
6dc42e49 4404 If the component name is a null string, then LOCAL is omitted entirely. */
400fbf9f
JW
4405
4406void
4407error_init (format, local, ofwhat)
4408 char *format, *local, *ofwhat;
4409{
d45cf215
RS
4410 char *buffer;
4411
4412 if (ofwhat == 0)
73a424d3 4413 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d45cf215 4414 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
400fbf9f
JW
4415
4416 if (*ofwhat)
4417 sprintf (buffer, local, ofwhat);
4418 else
4419 buffer[0] = 0;
4420
4421 error (format, buffer);
4422}
4423
4424/* Issue a pedantic warning for a bad initializer component.
4425 FORMAT describes the message. OFWHAT is the name for the component.
4426 LOCAL is a format string for formatting the insertion of the name
4427 into the message.
4428
d45cf215 4429 If OFWHAT is null, the component name is stored on the spelling stack.
6dc42e49 4430 If the component name is a null string, then LOCAL is omitted entirely. */
400fbf9f
JW
4431
4432void
4433pedwarn_init (format, local, ofwhat)
4434 char *format, *local, *ofwhat;
4435{
d45cf215
RS
4436 char *buffer;
4437
4438 if (ofwhat == 0)
73a424d3 4439 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d45cf215 4440 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
400fbf9f
JW
4441
4442 if (*ofwhat)
4443 sprintf (buffer, local, ofwhat);
4444 else
4445 buffer[0] = 0;
4446
4447 pedwarn (format, buffer);
4448}
b71c7f8a
RK
4449
4450/* Issue a warning for a bad initializer component.
4451 FORMAT describes the message. OFWHAT is the name for the component.
4452 LOCAL is a format string for formatting the insertion of the name
4453 into the message.
4454
4455 If OFWHAT is null, the component name is stored on the spelling stack.
4456 If the component name is a null string, then LOCAL is omitted entirely. */
4457
4458static void
4459warning_init (format, local, ofwhat)
4460 char *format, *local, *ofwhat;
4461{
4462 char *buffer;
4463
4464 if (ofwhat == 0)
4465 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4466 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4467
4468 if (*ofwhat)
4469 sprintf (buffer, local, ofwhat);
4470 else
4471 buffer[0] = 0;
4472
4473 warning (format, buffer);
4474}
400fbf9f
JW
4475\f
4476/* Digest the parser output INIT as an initializer for type TYPE.
4477 Return a C expression of type TYPE to represent the initial value.
4478
400fbf9f
JW
4479 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4480 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
59b22f64 4481 applies only to elements of constructors. */
400fbf9f 4482
b62acd60 4483static tree
790e9490
RS
4484digest_init (type, init, require_constant, constructor_constant)
4485 tree type, init;
400fbf9f 4486 int require_constant, constructor_constant;
400fbf9f
JW
4487{
4488 enum tree_code code = TREE_CODE (type);
047de90b 4489 tree inside_init = init;
400fbf9f 4490
400fbf9f
JW
4491 if (init == error_mark_node)
4492 return init;
4493
4494 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fc76e425
RS
4495 /* Do not use STRIP_NOPS here. We do not want an enumerator
4496 whose value is 0 to count as a null pointer constant. */
400fbf9f 4497 if (TREE_CODE (init) == NON_LVALUE_EXPR)
047de90b 4498 inside_init = TREE_OPERAND (init, 0);
400fbf9f 4499
400fbf9f
JW
4500 /* Initialization of an array of chars from a string constant
4501 optionally enclosed in braces. */
4502
4503 if (code == ARRAY_TYPE)
4504 {
4505 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4506 if ((typ1 == char_type_node
4507 || typ1 == signed_char_type_node
4508 || typ1 == unsigned_char_type_node
4509 || typ1 == unsigned_wchar_type_node
4510 || typ1 == signed_wchar_type_node)
fd5d5b94 4511 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
400fbf9f 4512 {
4d65300e
RS
4513 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4514 TYPE_MAIN_VARIANT (type)))
fd5d5b94 4515 return inside_init;
d11fdb45 4516
fd5d5b94 4517 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
400fbf9f
JW
4518 != char_type_node)
4519 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4520 {
4521 error_init ("char-array%s initialized from wide string",
de520661 4522 " `%s'", NULL);
400fbf9f
JW
4523 return error_mark_node;
4524 }
fd5d5b94 4525 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
400fbf9f
JW
4526 == char_type_node)
4527 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4528 {
4529 error_init ("int-array%s initialized from non-wide string",
de520661 4530 " `%s'", NULL);
400fbf9f
JW
4531 return error_mark_node;
4532 }
4533
fd5d5b94 4534 TREE_TYPE (inside_init) = type;
400fbf9f
JW
4535 if (TYPE_DOMAIN (type) != 0
4536 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4537 {
4538 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4539 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
fe9ef5d7
RS
4540 /* Subtract 1 (or sizeof (wchar_t))
4541 because it's ok to ignore the terminating null char
400fbf9f 4542 that is counted in the length of the constant. */
fd5d5b94 4543 if (size < TREE_STRING_LENGTH (inside_init)
fe9ef5d7
RS
4544 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4545 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4546 : 1))
400fbf9f
JW
4547 pedwarn_init (
4548 "initializer-string for array of chars%s is too long",
de520661 4549 " `%s'", NULL);
400fbf9f 4550 }
fd5d5b94 4551 return inside_init;
400fbf9f
JW
4552 }
4553 }
4554
de520661
RS
4555 /* Any type can be initialized
4556 from an expression of the same type, optionally with braces. */
400fbf9f 4557
2726966d 4558 if (inside_init && TREE_TYPE (inside_init) != 0
5522c047
PB
4559 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4560 TYPE_MAIN_VARIANT (type))
2726966d 4561 || (code == ARRAY_TYPE
3c3fa147
RS
4562 && comptypes (TREE_TYPE (inside_init), type))
4563 || (code == POINTER_TYPE
3c3fa147
RS
4564 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4565 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4566 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4567 TREE_TYPE (type)))))
400fbf9f
JW
4568 {
4569 if (code == POINTER_TYPE
047de90b
RS
4570 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4571 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4572 inside_init = default_conversion (inside_init);
de520661
RS
4573 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4574 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f
JW
4575 {
4576 error_init ("array%s initialized from non-constant array expression",
de520661 4577 " `%s'", NULL);
400fbf9f
JW
4578 return error_mark_node;
4579 }
4580
047de90b
RS
4581 if (optimize && TREE_READONLY (inside_init)
4582 && TREE_CODE (inside_init) == VAR_DECL)
4583 inside_init = decl_constant_value (inside_init);
400fbf9f 4584
d9fc6069
JW
4585 /* Compound expressions can only occur here if -pedantic or
4586 -pedantic-errors is specified. In the later case, we always want
4587 an error. In the former case, we simply want a warning. */
4588 if (require_constant && pedantic
4589 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4590 {
4591 inside_init
4592 = valid_compound_expr_initializer (inside_init,
4593 TREE_TYPE (inside_init));
4594 if (inside_init == error_mark_node)
4595 error_init ("initializer element%s is not constant",
4596 " for `%s'", NULL);
4597 else
4598 pedwarn_init ("initializer element%s is not constant",
4599 " for `%s'", NULL);
4600 if (flag_pedantic_errors)
4601 inside_init = error_mark_node;
4602 }
4603 else if (require_constant && ! TREE_CONSTANT (inside_init))
400fbf9f
JW
4604 {
4605 error_init ("initializer element%s is not constant",
de520661 4606 " for `%s'", NULL);
047de90b 4607 inside_init = error_mark_node;
400fbf9f 4608 }
f0c70ef0
RS
4609 else if (require_constant
4610 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
400fbf9f
JW
4611 {
4612 error_init ("initializer element%s is not computable at load time",
de520661 4613 " for `%s'", NULL);
047de90b 4614 inside_init = error_mark_node;
400fbf9f
JW
4615 }
4616
047de90b 4617 return inside_init;
400fbf9f
JW
4618 }
4619
400fbf9f
JW
4620 /* Handle scalar types, including conversions. */
4621
4622 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
337633f9 4623 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
400fbf9f 4624 {
e3a12f0c
RS
4625 /* Note that convert_for_assignment calls default_conversion
4626 for arrays and functions. We must not call it in the
4627 case where inside_init is a null pointer constant. */
4628 inside_init
4629 = convert_for_assignment (type, init, "initialization",
4630 NULL_TREE, NULL_TREE, 0);
400fbf9f 4631
047de90b 4632 if (require_constant && ! TREE_CONSTANT (inside_init))
400fbf9f
JW
4633 {
4634 error_init ("initializer element%s is not constant",
de520661 4635 " for `%s'", NULL);
047de90b 4636 inside_init = error_mark_node;
400fbf9f 4637 }
f0c70ef0
RS
4638 else if (require_constant
4639 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
400fbf9f
JW
4640 {
4641 error_init ("initializer element%s is not computable at load time",
de520661 4642 " for `%s'", NULL);
047de90b 4643 inside_init = error_mark_node;
400fbf9f
JW
4644 }
4645
047de90b 4646 return inside_init;
400fbf9f
JW
4647 }
4648
4649 /* Come here only for records and arrays. */
4650
4651 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4652 {
4653 error_init ("variable-sized object%s may not be initialized",
de520661 4654 " `%s'", NULL);
400fbf9f
JW
4655 return error_mark_node;
4656 }
4657
81a55c6c
RS
4658 /* Traditionally, you can write struct foo x = 0;
4659 and it initializes the first element of x to 0. */
4660 if (flag_traditional)
4661 {
4662 tree top = 0, prev = 0;
4663 while (TREE_CODE (type) == RECORD_TYPE
4664 || TREE_CODE (type) == ARRAY_TYPE
4665 || TREE_CODE (type) == QUAL_UNION_TYPE
4666 || TREE_CODE (type) == UNION_TYPE)
4667 {
4668 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4669 if (prev == 0)
4670 top = temp;
4671 else
4672 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4673 prev = temp;
4674 if (TREE_CODE (type) == ARRAY_TYPE)
4675 type = TREE_TYPE (type);
4676 else if (TYPE_FIELDS (type))
4677 type = TREE_TYPE (TYPE_FIELDS (type));
4678 else
4679 {
4680 error_init ("invalid initializer%s", " for `%s'", NULL);
4681 return error_mark_node;
4682 }
4683 }
4684 TREE_OPERAND (prev, 1)
4685 = build_tree_list (NULL_TREE,
4686 digest_init (type, init, require_constant,
4687 constructor_constant));
4688 return top;
4689 }
de520661 4690 error_init ("invalid initializer%s", " for `%s'", NULL);
400fbf9f
JW
4691 return error_mark_node;
4692}
4693\f
de520661 4694/* Handle initializers that use braces. */
400fbf9f 4695
34403047 4696static void output_init_element ();
de520661
RS
4697static void output_pending_init_elements ();
4698static void check_init_type_bitfields ();
400fbf9f 4699
de520661
RS
4700/* Type of object we are accumulating a constructor for.
4701 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4702static tree constructor_type;
400fbf9f 4703
de520661
RS
4704/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4705 left to fill. */
4706static tree constructor_fields;
400fbf9f 4707
de520661
RS
4708/* For an ARRAY_TYPE, this is the specified index
4709 at which to store the next element we get.
4710 This is a special INTEGER_CST node that we modify in place. */
4711static tree constructor_index;
400fbf9f 4712
de520661
RS
4713/* For an ARRAY_TYPE, this is the end index of the range
4714 to intitialize with the next element, or NULL in the ordinary case
4715 where the element is used just once. */
4716static tree constructor_range_end;
400fbf9f 4717
de520661
RS
4718/* For an ARRAY_TYPE, this is the maximum index. */
4719static tree constructor_max_index;
103b7b17 4720
de520661
RS
4721/* For a RECORD_TYPE, this is the first field not yet written out. */
4722static tree constructor_unfilled_fields;
400fbf9f 4723
de520661
RS
4724/* For an ARRAY_TYPE, this is the index of the first element
4725 not yet written out.
4726 This is a special INTEGER_CST node that we modify in place. */
4727static tree constructor_unfilled_index;
4728
b62acd60
RS
4729/* In a RECORD_TYPE, the byte index of the next consecutive field.
4730 This is so we can generate gaps between fields, when appropriate.
4731 This is a special INTEGER_CST node that we modify in place. */
4732static tree constructor_bit_index;
4733
de520661
RS
4734/* If we are saving up the elements rather than allocating them,
4735 this is the list of elements so far (in reverse order,
4736 most recent first). */
4737static tree constructor_elements;
4738
4739/* 1 if so far this constructor's elements are all compile-time constants. */
4740static int constructor_constant;
4741
4742/* 1 if so far this constructor's elements are all valid address constants. */
4743static int constructor_simple;
4744
4745/* 1 if this constructor is erroneous so far. */
4746static int constructor_erroneous;
4747
4748/* 1 if have called defer_addressed_constants. */
4749static int constructor_subconstants_deferred;
4750
4751/* List of pending elements at this constructor level.
4752 These are elements encountered out of order
4753 which belong at places we haven't reached yet in actually
4754 writing the output. */
4755static tree constructor_pending_elts;
4756
4757/* The SPELLING_DEPTH of this constructor. */
4758static int constructor_depth;
4759
cc77d4d5
PB
4760/* 0 if implicitly pushing constructor levels is allowed. */
4761int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4762
de520661
RS
4763/* 1 if this constructor level was entered implicitly. */
4764static int constructor_implicit;
4765
4766static int require_constant_value;
4767static int require_constant_elements;
4768
4769/* 1 if it is ok to output this constructor as we read it.
4770 0 means must accumulate a CONSTRUCTOR expression. */
4771static int constructor_incremental;
4772
4773/* DECL node for which an initializer is being read.
4774 0 means we are reading a constructor expression
4775 such as (struct foo) {...}. */
4776static tree constructor_decl;
4777
4778/* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4779static char *constructor_asmspec;
4780
4781/* Nonzero if this is an initializer for a top-level decl. */
4782static int constructor_top_level;
4783
4784/* When we finish reading a constructor expression
4785 (constructor_decl is 0), the CONSTRUCTOR goes here. */
4786static tree constructor_result;
b62acd60
RS
4787\f
4788/* This stack has a level for each implicit or explicit level of
4789 structuring in the initializer, including the outermost one. It
4790 saves the values of most of the variables above. */
de520661
RS
4791
4792struct constructor_stack
400fbf9f 4793{
de520661
RS
4794 struct constructor_stack *next;
4795 tree type;
4796 tree fields;
4797 tree index;
4798 tree range_end;
4799 tree max_index;
4800 tree unfilled_index;
4801 tree unfilled_fields;
b62acd60 4802 tree bit_index;
de520661
RS
4803 tree elements;
4804 int offset;
4805 tree pending_elts;
4806 int depth;
790e9490
RS
4807 /* If nonzero, this value should replace the entire
4808 constructor at this level. */
4809 tree replacement_value;
de520661
RS
4810 char constant;
4811 char simple;
4812 char implicit;
4813 char incremental;
4814 char erroneous;
4815 char outer;
4816};
d45cf215 4817
de520661 4818struct constructor_stack *constructor_stack;
400fbf9f 4819
de520661
RS
4820/* This stack records separate initializers that are nested.
4821 Nested initializers can't happen in ANSI C, but GNU C allows them
4822 in cases like { ... (struct foo) { ... } ... }. */
400fbf9f 4823
de520661
RS
4824struct initializer_stack
4825{
4826 struct initializer_stack *next;
4827 tree decl;
4828 char *asmspec;
4829 struct constructor_stack *constructor_stack;
dea273df 4830 tree elements;
de520661
RS
4831 struct spelling *spelling;
4832 struct spelling *spelling_base;
4833 int spelling_size;
4834 char top_level;
4835 char incremental;
4836 char require_constant_value;
4837 char require_constant_elements;
4838 char deferred;
4839};
4840
4841struct initializer_stack *initializer_stack;
4842\f
4843/* Prepare to parse and output the initializer for variable DECL. */
4844
4845void
e28cae4f 4846start_init (decl, asmspec_tree, top_level)
de520661 4847 tree decl;
e28cae4f 4848 tree asmspec_tree;
de520661
RS
4849 int top_level;
4850{
4851 char *locus;
4852 struct initializer_stack *p
4853 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
e28cae4f
RS
4854 char *asmspec = 0;
4855
4856 if (asmspec_tree)
4857 asmspec = TREE_STRING_POINTER (asmspec_tree);
de520661
RS
4858
4859 p->decl = constructor_decl;
4860 p->asmspec = constructor_asmspec;
4861 p->incremental = constructor_incremental;
4862 p->require_constant_value = require_constant_value;
4863 p->require_constant_elements = require_constant_elements;
4864 p->constructor_stack = constructor_stack;
dea273df 4865 p->elements = constructor_elements;
de520661
RS
4866 p->spelling = spelling;
4867 p->spelling_base = spelling_base;
4868 p->spelling_size = spelling_size;
4869 p->deferred = constructor_subconstants_deferred;
4870 p->top_level = constructor_top_level;
b62acd60 4871 p->next = initializer_stack;
de520661
RS
4872 initializer_stack = p;
4873
4874 constructor_decl = decl;
4875 constructor_incremental = top_level;
4876 constructor_asmspec = asmspec;
4877 constructor_subconstants_deferred = 0;
4878 constructor_top_level = top_level;
4879
4880 if (decl != 0)
3c3fa147 4881 {
de520661 4882 require_constant_value = TREE_STATIC (decl);
f1a2b955
RS
4883 require_constant_elements
4884 = ((TREE_STATIC (decl) || pedantic)
4885 /* For a scalar, you can always use any value to initialize,
4886 even within braces. */
4887 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4888 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4889 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4890 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
de520661
RS
4891 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4892 constructor_incremental |= TREE_STATIC (decl);
3c3fa147 4893 }
400fbf9f 4894 else
de520661
RS
4895 {
4896 require_constant_value = 0;
4897 require_constant_elements = 0;
4898 locus = "(anonymous)";
4899 }
400fbf9f 4900
de520661 4901 constructor_stack = 0;
400fbf9f 4902
b71c7f8a
RK
4903 missing_braces_mentioned = 0;
4904
de520661
RS
4905 spelling_base = 0;
4906 spelling_size = 0;
4907 RESTORE_SPELLING_DEPTH (0);
d45cf215 4908
de520661
RS
4909 if (locus)
4910 push_string (locus);
4911}
400fbf9f 4912
de520661
RS
4913void
4914finish_init ()
4915{
4916 struct initializer_stack *p = initializer_stack;
400fbf9f 4917
de520661
RS
4918 /* Output subconstants (string constants, usually)
4919 that were referenced within this initializer and saved up.
4920 Must do this if and only if we called defer_addressed_constants. */
4921 if (constructor_subconstants_deferred)
4922 output_deferred_addressed_constants ();
4f77a31b 4923
de520661
RS
4924 /* Free the whole constructor stack of this initializer. */
4925 while (constructor_stack)
4926 {
4927 struct constructor_stack *q = constructor_stack;
4928 constructor_stack = q->next;
4929 free (q);
4930 }
400fbf9f 4931
de520661
RS
4932 /* Pop back to the data of the outer initializer (if any). */
4933 constructor_decl = p->decl;
4934 constructor_asmspec = p->asmspec;
4935 constructor_incremental = p->incremental;
4936 require_constant_value = p->require_constant_value;
4937 require_constant_elements = p->require_constant_elements;
4938 constructor_stack = p->constructor_stack;
dea273df 4939 constructor_elements = p->elements;
de520661
RS
4940 spelling = p->spelling;
4941 spelling_base = p->spelling_base;
4942 spelling_size = p->spelling_size;
4943 constructor_subconstants_deferred = p->deferred;
4944 constructor_top_level = p->top_level;
4945 initializer_stack = p->next;
4946 free (p);
4947}
4948\f
4949/* Call here when we see the initializer is surrounded by braces.
4950 This is instead of a call to push_init_level;
4951 it is matched by a call to pop_init_level.
400fbf9f 4952
de520661
RS
4953 TYPE is the type to initialize, for a constructor expression.
4954 For an initializer for a decl, TYPE is zero. */
5a7ec9d9 4955
de520661
RS
4956void
4957really_start_incremental_init (type)
4958 tree type;
4959{
4960 struct constructor_stack *p
4961 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
4962
4963 if (type == 0)
4964 type = TREE_TYPE (constructor_decl);
4965
4966 /* Turn off constructor_incremental if type is a struct with bitfields.
4967 Do this before the first push, so that the corrected value
4968 is available in finish_init. */
4969 check_init_type_bitfields (type);
4970
4971 p->type = constructor_type;
4972 p->fields = constructor_fields;
4973 p->index = constructor_index;
4974 p->range_end = constructor_range_end;
4975 p->max_index = constructor_max_index;
4976 p->unfilled_index = constructor_unfilled_index;
4977 p->unfilled_fields = constructor_unfilled_fields;
b62acd60 4978 p->bit_index = constructor_bit_index;
de520661
RS
4979 p->elements = 0;
4980 p->constant = constructor_constant;
4981 p->simple = constructor_simple;
4982 p->erroneous = constructor_erroneous;
4983 p->pending_elts = constructor_pending_elts;
4984 p->depth = constructor_depth;
790e9490 4985 p->replacement_value = 0;
de520661
RS
4986 p->implicit = 0;
4987 p->incremental = constructor_incremental;
4988 p->outer = 0;
4989 p->next = 0;
4990 constructor_stack = p;
4991
4992 constructor_constant = 1;
4993 constructor_simple = 1;
4994 constructor_depth = SPELLING_DEPTH ();
4995 constructor_elements = 0;
4996 constructor_pending_elts = 0;
4997 constructor_type = type;
4998
4999 if (TREE_CODE (constructor_type) == RECORD_TYPE
5000 || TREE_CODE (constructor_type) == UNION_TYPE)
5001 {
5002 constructor_fields = TYPE_FIELDS (constructor_type);
fc623854
RS
5003 /* Skip any nameless bit fields atthe beginning. */
5004 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5005 && DECL_NAME (constructor_fields) == 0)
5006 constructor_fields = TREE_CHAIN (constructor_fields);
de520661 5007 constructor_unfilled_fields = constructor_fields;
b62acd60 5008 constructor_bit_index = copy_node (integer_zero_node);
de520661
RS
5009 }
5010 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5011 {
de520661 5012 constructor_range_end = 0;
de520661 5013 if (TYPE_DOMAIN (constructor_type))
2bede729
PB
5014 {
5015 constructor_max_index
5016 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5017 constructor_index
5018 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5019 }
5020 else
5021 constructor_index = copy_node (integer_zero_node);
5022 constructor_unfilled_index = copy_node (constructor_index);
de520661
RS
5023 }
5024 else
5025 {
5026 /* Handle the case of int x = {5}; */
5027 constructor_fields = constructor_type;
5028 constructor_unfilled_fields = constructor_type;
5029 }
400fbf9f 5030
de520661
RS
5031 if (constructor_incremental)
5032 {
5033 int momentary = suspend_momentary ();
5034 push_obstacks_nochange ();
5035 if (TREE_PERMANENT (constructor_decl))
5036 end_temporary_allocation ();
5037 make_decl_rtl (constructor_decl, constructor_asmspec,
5038 constructor_top_level);
5039 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5040 pop_obstacks ();
5041 resume_momentary (momentary);
5042 }
400fbf9f 5043
de520661
RS
5044 if (constructor_incremental)
5045 {
5046 defer_addressed_constants ();
5047 constructor_subconstants_deferred = 1;
5048 }
5049}
5050\f
5051/* Push down into a subobject, for initialization.
5052 If this is for an explicit set of braces, IMPLICIT is 0.
5053 If it is because the next element belongs at a lower level,
5054 IMPLICIT is 1. */
400fbf9f 5055
de520661
RS
5056void
5057push_init_level (implicit)
5058 int implicit;
5059{
94ba5069
RS
5060 struct constructor_stack *p;
5061
5062 /* If we've exhausted any levels that didn't have braces,
5063 pop them now. */
5064 while (constructor_stack->implicit)
5065 {
5066 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5067 || TREE_CODE (constructor_type) == UNION_TYPE)
5068 && constructor_fields == 0)
5069 process_init_element (pop_init_level (1));
5070 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5071 && tree_int_cst_lt (constructor_max_index, constructor_index))
5072 process_init_element (pop_init_level (1));
5073 else
5074 break;
5075 }
5076
e700c8ec
RS
5077 /* Structure elements may require alignment. Do this now
5078 if necessary for the subaggregate. */
9d3a34d4
RS
5079 if (constructor_incremental && TREE_CODE (constructor_type) == RECORD_TYPE
5080 && constructor_fields)
e700c8ec
RS
5081 {
5082 /* Advance to offset of this element. */
5083 if (! tree_int_cst_equal (constructor_bit_index,
5084 DECL_FIELD_BITPOS (constructor_fields)))
5085 {
5086 int next = (TREE_INT_CST_LOW
5087 (DECL_FIELD_BITPOS (constructor_fields))
5088 / BITS_PER_UNIT);
5089 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5090 / BITS_PER_UNIT);
5091
5092 assemble_zeros (next - here);
5093 }
5094 }
5095
94ba5069 5096 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
de520661
RS
5097 p->type = constructor_type;
5098 p->fields = constructor_fields;
5099 p->index = constructor_index;
5100 p->range_end = constructor_range_end;
5101 p->max_index = constructor_max_index;
5102 p->unfilled_index = constructor_unfilled_index;
5103 p->unfilled_fields = constructor_unfilled_fields;
b62acd60 5104 p->bit_index = constructor_bit_index;
de520661
RS
5105 p->elements = constructor_elements;
5106 p->constant = constructor_constant;
5107 p->simple = constructor_simple;
5108 p->erroneous = constructor_erroneous;
5109 p->pending_elts = constructor_pending_elts;
5110 p->depth = constructor_depth;
790e9490 5111 p->replacement_value = 0;
de520661
RS
5112 p->implicit = implicit;
5113 p->incremental = constructor_incremental;
5114 p->outer = 0;
5115 p->next = constructor_stack;
5116 constructor_stack = p;
5117
5118 constructor_constant = 1;
5119 constructor_simple = 1;
5120 constructor_depth = SPELLING_DEPTH ();
5121 constructor_elements = 0;
5122 constructor_pending_elts = 0;
5123
94ba5069
RS
5124 /* Don't die if an entire brace-pair level is superfluous
5125 in the containing level. */
5126 if (constructor_type == 0)
5127 ;
5128 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5129 || TREE_CODE (constructor_type) == UNION_TYPE)
de520661 5130 {
91fa3c30
RS
5131 /* Don't die if there are extra init elts at the end. */
5132 if (constructor_fields == 0)
5133 constructor_type = 0;
5134 else
5135 {
5136 constructor_type = TREE_TYPE (constructor_fields);
19d76e60 5137 push_member_name (constructor_fields);
91fa3c30 5138 }
de520661
RS
5139 }
5140 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5141 {
5142 constructor_type = TREE_TYPE (constructor_type);
5143 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5144 }
400fbf9f 5145
91fa3c30
RS
5146 if (constructor_type == 0)
5147 {
5148 error_init ("extra brace group at end of initializer%s",
5149 " for `%s'", NULL);
5150 constructor_fields = 0;
5151 constructor_unfilled_fields = 0;
b71c7f8a 5152 return;
91fa3c30 5153 }
b71c7f8a
RK
5154
5155 /* Turn off constructor_incremental if type is a struct with bitfields. */
5156 check_init_type_bitfields (constructor_type);
5157
5158 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5159 {
5160 missing_braces_mentioned = 1;
5161 warning_init ("missing braces around initializer%s", " for `%s'", NULL);
5162 }
5163
5164 if (TREE_CODE (constructor_type) == RECORD_TYPE
91fa3c30 5165 || TREE_CODE (constructor_type) == UNION_TYPE)
de520661
RS
5166 {
5167 constructor_fields = TYPE_FIELDS (constructor_type);
fc623854
RS
5168 /* Skip any nameless bit fields atthe beginning. */
5169 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5170 && DECL_NAME (constructor_fields) == 0)
5171 constructor_fields = TREE_CHAIN (constructor_fields);
de520661 5172 constructor_unfilled_fields = constructor_fields;
b62acd60 5173 constructor_bit_index = copy_node (integer_zero_node);
de520661
RS
5174 }
5175 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5176 {
de520661 5177 constructor_range_end = 0;
de520661 5178 if (TYPE_DOMAIN (constructor_type))
2bede729
PB
5179 {
5180 constructor_max_index
5181 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5182 constructor_index
5183 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5184 }
5185 else
5186 constructor_index = copy_node (integer_zero_node);
5187 constructor_unfilled_index = copy_node (constructor_index);
de520661
RS
5188 }
5189 else
5190 {
b71c7f8a 5191 warning_init ("braces around scalar initializer%s", " for `%s'", NULL);
de520661
RS
5192 constructor_fields = constructor_type;
5193 constructor_unfilled_fields = constructor_type;
5194 }
5195}
400fbf9f 5196
de520661
RS
5197/* Don't read a struct incrementally if it has any bitfields,
5198 because the incremental reading code doesn't know how to
5199 handle bitfields yet. */
d45cf215 5200
de520661
RS
5201static void
5202check_init_type_bitfields (type)
5203 tree type;
5204{
5205 if (TREE_CODE (type) == RECORD_TYPE)
5206 {
5207 tree tail;
5208 for (tail = TYPE_FIELDS (type); tail;
5209 tail = TREE_CHAIN (tail))
3c9d8baf
RK
5210 {
5211 if (DECL_BIT_FIELD (tail)
5212 /* This catches cases like `int foo : 8;'. */
5213 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5214 {
5215 constructor_incremental = 0;
5216 break;
5217 }
5218
5219 check_init_type_bitfields (TREE_TYPE (tail));
5220 }
400fbf9f 5221 }
3c9d8baf
RK
5222
5223 else if (TREE_CODE (type) == ARRAY_TYPE)
5224 check_init_type_bitfields (TREE_TYPE (type));
de520661
RS
5225}
5226
5227/* At the end of an implicit or explicit brace level,
5228 finish up that level of constructor.
5229 If we were outputting the elements as they are read, return 0
5230 from inner levels (process_init_element ignores that),
5231 but return error_mark_node from the outermost level
5232 (that's what we want to put in DECL_INITIAL).
5233 Otherwise, return a CONSTRUCTOR expression. */
5234
5235tree
5236pop_init_level (implicit)
5237 int implicit;
5238{
5239 struct constructor_stack *p;
5240 int size;
5241 tree constructor = 0;
5242
5243 if (implicit == 0)
400fbf9f 5244 {
de520661
RS
5245 /* When we come to an explicit close brace,
5246 pop any inner levels that didn't have explicit braces. */
5247 while (constructor_stack->implicit)
5248 process_init_element (pop_init_level (1));
5249 }
400fbf9f 5250
de520661 5251 p = constructor_stack;
91fa3c30
RS
5252
5253 if (constructor_type != 0)
5254 size = int_size_in_bytes (constructor_type);
400fbf9f 5255
de520661
RS
5256 /* Now output all pending elements. */
5257 output_pending_init_elements (1);
5258
b62acd60
RS
5259#if 0 /* c-parse.in warns about {}. */
5260 /* In ANSI, each brace level must have at least one element. */
5261 if (! implicit && pedantic
5262 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5263 ? integer_zerop (constructor_unfilled_index)
5264 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5265 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5266#endif
5267
de520661
RS
5268 /* Pad out the end of the structure. */
5269
790e9490
RS
5270 if (p->replacement_value)
5271 {
5272 /* If this closes a superfluous brace pair,
5273 just pass out the element between them. */
5274 constructor = p->replacement_value;
5275 /* If this is the top level thing within the initializer,
d11fdb45 5276 and it's for a variable, then since we already called
790e9490
RS
5277 assemble_variable, we must output the value now. */
5278 if (p->next == 0 && constructor_decl != 0
5279 && constructor_incremental)
5280 {
5281 constructor = digest_init (constructor_type, constructor,
5282 0, 0);
5283
5284 /* If initializing an array of unknown size,
5285 determine the size now. */
5286 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5287 && TYPE_DOMAIN (constructor_type) == 0)
5288 {
5289 int failure;
5ded5b76 5290 int momentary_p;
790e9490
RS
5291
5292 push_obstacks_nochange ();
5293 if (TREE_PERMANENT (constructor_type))
5294 end_temporary_allocation ();
5295
5ded5b76
RK
5296 momentary_p = suspend_momentary ();
5297
790e9490
RS
5298 /* We shouldn't have an incomplete array type within
5299 some other type. */
5300 if (constructor_stack->next)
5301 abort ();
5302
5303 failure
5304 = complete_array_type (constructor_type,
5305 constructor, 0);
5306 if (failure)
5307 abort ();
5308
5309 size = int_size_in_bytes (constructor_type);
5ded5b76 5310 resume_momentary (momentary_p);
790e9490
RS
5311 pop_obstacks ();
5312 }
5313
5314 output_constant (constructor, size);
5315 }
5316 }
91fa3c30
RS
5317 else if (constructor_type == 0)
5318 ;
19d76e60
RK
5319 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5320 && TREE_CODE (constructor_type) != UNION_TYPE
5321 && TREE_CODE (constructor_type) != ARRAY_TYPE
5322 && ! constructor_incremental)
5323 {
5324 /* A nonincremental scalar initializer--just return
5325 the element, after verifying there is just one. */
5326 if (constructor_elements == 0)
5327 {
5328 error_init ("empty scalar initializer%s",
5329 " for `%s'", NULL);
5330 constructor = error_mark_node;
5331 }
5332 else if (TREE_CHAIN (constructor_elements) != 0)
5333 {
5334 error_init ("extra elements in scalar initializer%s",
5335 " for `%s'", NULL);
5336 constructor = TREE_VALUE (constructor_elements);
5337 }
5338 else
5339 constructor = TREE_VALUE (constructor_elements);
5340 }
790e9490 5341 else if (! constructor_incremental)
de520661
RS
5342 {
5343 if (constructor_erroneous)
5344 constructor = error_mark_node;
5345 else
400fbf9f 5346 {
de520661
RS
5347 int momentary = suspend_momentary ();
5348
5349 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5350 nreverse (constructor_elements));
5351 if (constructor_constant)
5352 TREE_CONSTANT (constructor) = 1;
5353 if (constructor_constant && constructor_simple)
5354 TREE_STATIC (constructor) = 1;
19d76e60 5355
de520661
RS
5356 resume_momentary (momentary);
5357 }
5358 }
5359 else
5360 {
5361 tree filled;
5362 int momentary = suspend_momentary ();
400fbf9f 5363
de520661
RS
5364 if (TREE_CODE (constructor_type) == RECORD_TYPE
5365 || TREE_CODE (constructor_type) == UNION_TYPE)
5366 {
de520661
RS
5367 /* Find the offset of the end of that field. */
5368 filled = size_binop (CEIL_DIV_EXPR,
b62acd60 5369 constructor_bit_index,
de520661
RS
5370 size_int (BITS_PER_UNIT));
5371 }
5372 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5373 {
5374 /* If initializing an array of unknown size,
5375 determine the size now. */
5376 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5377 && TYPE_DOMAIN (constructor_type) == 0)
400fbf9f 5378 {
de520661
RS
5379 tree maxindex
5380 = size_binop (MINUS_EXPR,
5381 constructor_unfilled_index,
5382 integer_one_node);
5383
5384 push_obstacks_nochange ();
5385 if (TREE_PERMANENT (constructor_type))
5386 end_temporary_allocation ();
5387 maxindex = copy_node (maxindex);
5388 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5389 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5390
de520661
RS
5391 if (pedantic
5392 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)),
5393 integer_zero_node))
5394 error_with_decl (constructor_decl, "zero-size array `%s'");
5395 layout_type (constructor_type);
5396 size = int_size_in_bytes (constructor_type);
5397 pop_obstacks ();
400fbf9f
JW
5398 }
5399
de520661
RS
5400 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5401 size_in_bytes (TREE_TYPE (constructor_type)));
5402 }
5403 else
5404 filled = 0;
400fbf9f 5405
de520661
RS
5406 if (filled != 0)
5407 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5408
5409 resume_momentary (momentary);
5410 }
5411
5412
5413 constructor_type = p->type;
5414 constructor_fields = p->fields;
5415 constructor_index = p->index;
5416 constructor_range_end = p->range_end;
5417 constructor_max_index = p->max_index;
5418 constructor_unfilled_index = p->unfilled_index;
5419 constructor_unfilled_fields = p->unfilled_fields;
b62acd60 5420 constructor_bit_index = p->bit_index;
de520661
RS
5421 constructor_elements = p->elements;
5422 constructor_constant = p->constant;
5423 constructor_simple = p->simple;
5424 constructor_erroneous = p->erroneous;
5425 constructor_pending_elts = p->pending_elts;
5426 constructor_depth = p->depth;
5427 constructor_incremental = p->incremental;
5428 RESTORE_SPELLING_DEPTH (constructor_depth);
5429
5430 constructor_stack = p->next;
5431 free (p);
5432
5433 if (constructor == 0)
5434 {
5435 if (constructor_stack == 0)
5436 return error_mark_node;
5437 return NULL_TREE;
5438 }
5439 return constructor;
5440}
5441
5442/* Within an array initializer, specify the next index to be initialized.
5443 FIRST is that index. If LAST is nonzero, then initialize a range
5444 of indices, running from FIRST through LAST. */
5445
5446void
5447set_init_index (first, last)
5448 tree first, last;
5449{
19d76e60
RK
5450 while ((TREE_CODE (first) == NOP_EXPR
5451 || TREE_CODE (first) == CONVERT_EXPR
5452 || TREE_CODE (first) == NON_LVALUE_EXPR)
5453 && (TYPE_MODE (TREE_TYPE (first))
5454 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5455 (first) = TREE_OPERAND (first, 0);
5456 if (last)
5457 while ((TREE_CODE (last) == NOP_EXPR
5458 || TREE_CODE (last) == CONVERT_EXPR
5459 || TREE_CODE (last) == NON_LVALUE_EXPR)
5460 && (TYPE_MODE (TREE_TYPE (last))
5461 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5462 (last) = TREE_OPERAND (last, 0);
5463
94ba5069
RS
5464 if (TREE_CODE (first) != INTEGER_CST)
5465 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5466 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5467 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5468 else if (tree_int_cst_lt (first, constructor_unfilled_index))
de520661
RS
5469 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5470 else
5471 {
5472 TREE_INT_CST_LOW (constructor_index)
5473 = TREE_INT_CST_LOW (first);
5474 TREE_INT_CST_HIGH (constructor_index)
5475 = TREE_INT_CST_HIGH (first);
5476
5477 if (last != 0 && tree_int_cst_lt (last, first))
5478 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5479 else
b62acd60
RS
5480 {
5481 if (pedantic)
5482 pedwarn ("ANSI C forbids specifying element to initialize");
5483 constructor_range_end = last;
5484 }
de520661
RS
5485 }
5486}
5487
5488/* Within a struct initializer, specify the next field to be initialized. */
5489
94ba5069 5490void
de520661
RS
5491set_init_label (fieldname)
5492 tree fieldname;
5493{
5494 tree tail;
5495 int passed = 0;
5496
5497 for (tail = TYPE_FIELDS (constructor_type); tail;
5498 tail = TREE_CHAIN (tail))
5499 {
5500 if (tail == constructor_unfilled_fields)
5501 passed = 1;
5502 if (DECL_NAME (tail) == fieldname)
5503 break;
5504 }
5505
5506 if (tail == 0)
5507 error ("unknown field `%s' specified in initializer",
5508 IDENTIFIER_POINTER (fieldname));
5509 else if (!passed)
5510 error ("field `%s' already initialized",
5511 IDENTIFIER_POINTER (fieldname));
5512 else
b62acd60
RS
5513 {
5514 constructor_fields = tail;
5515 if (pedantic)
5516 pedwarn ("ANSI C forbids specifying structure member to initialize");
5517 }
de520661
RS
5518}
5519\f
5520/* "Output" the next constructor element.
5521 At top level, really output it to assembler code now.
5522 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5523 TYPE is the data type that the containing data type wants here.
5524 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5525
5526 PENDING if non-nil means output pending elements that belong
5527 right after this element. (PENDING is normally 1;
5528 it is 0 while outputting pending elements, to avoid recursion.) */
5529
34403047 5530static void
de520661
RS
5531output_init_element (value, type, field, pending)
5532 tree value, type, field;
5533 int pending;
5534{
5535 int duplicate = 0;
5536
d3ab9753
RS
5537 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5538 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
fd5d5b94
RS
5539 && !(TREE_CODE (value) == STRING_CST
5540 && TREE_CODE (type) == ARRAY_TYPE
5541 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
1e40eab8
RS
5542 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5543 TYPE_MAIN_VARIANT (type))))
d3ab9753
RS
5544 value = default_conversion (value);
5545
5546 if (value == error_mark_node)
5547 constructor_erroneous = 1;
5548 else if (!TREE_CONSTANT (value))
5549 constructor_constant = 0;
5550 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5551 constructor_simple = 0;
5552
de520661
RS
5553 if (require_constant_value && ! TREE_CONSTANT (value))
5554 {
5555 error_init ("initializer element%s is not constant",
5556 " for `%s'", NULL);
5557 value = error_mark_node;
5558 }
5559 else if (require_constant_elements
5560 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5561 {
5562 error_init ("initializer element%s is not computable at load time",
5563 " for `%s'", NULL);
5564 value = error_mark_node;
5565 }
5566
5567 /* If this element duplicates one on constructor_pending_elts,
5568 print a message and ignore it. Don't do this when we're
5569 processing elements taken off constructor_pending_elts,
5570 because we'd always get spurious errors. */
5571 if (pending)
5572 {
5573 if (TREE_CODE (constructor_type) == RECORD_TYPE
5574 || TREE_CODE (constructor_type) == UNION_TYPE)
5575 {
5576 if (purpose_member (field, constructor_pending_elts))
400fbf9f 5577 {
de520661
RS
5578 error_init ("duplicate initializer%s", " for `%s'", NULL);
5579 duplicate = 1;
400fbf9f 5580 }
de520661
RS
5581 }
5582 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5583 {
5584 tree tail;
5585 for (tail = constructor_pending_elts; tail;
5586 tail = TREE_CHAIN (tail))
5587 if (TREE_PURPOSE (tail) != 0
5588 && TREE_CODE (TREE_PURPOSE (tail)) == INTEGER_CST
5589 && tree_int_cst_equal (TREE_PURPOSE (tail), constructor_index))
5590 break;
400fbf9f 5591
de520661 5592 if (tail != 0)
400fbf9f 5593 {
de520661
RS
5594 error_init ("duplicate initializer%s", " for `%s'", NULL);
5595 duplicate = 1;
400fbf9f 5596 }
400fbf9f
JW
5597 }
5598 }
400fbf9f 5599
de520661
RS
5600 /* If this element doesn't come next in sequence,
5601 put it on constructor_pending_elts. */
5602 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5603 && !tree_int_cst_equal (field, constructor_unfilled_index))
5604 {
5605 if (! duplicate)
8348547a
RS
5606 /* The copy_node is needed in case field is actually
5607 constructor_index, which is modified in place. */
de520661 5608 constructor_pending_elts
8348547a 5609 = tree_cons (copy_node (field),
d11fdb45 5610 digest_init (type, value, 0, 0),
de520661
RS
5611 constructor_pending_elts);
5612 }
76aaaae2 5613 else if (TREE_CODE (constructor_type) == RECORD_TYPE
de520661
RS
5614 && field != constructor_unfilled_fields)
5615 {
76aaaae2
RS
5616 /* We do this for records but not for unions. In a union,
5617 no matter which field is specified, it can be initialized
5618 right away since it starts at the beginning of the union. */
de520661
RS
5619 if (!duplicate)
5620 constructor_pending_elts
5621 = tree_cons (field,
d11fdb45 5622 digest_init (type, value, 0, 0),
de520661
RS
5623 constructor_pending_elts);
5624 }
5625 else
5626 {
5627 /* Otherwise, output this element either to
5628 constructor_elements or to the assembler file. */
400fbf9f 5629
de520661 5630 if (!duplicate)
c2f4acb7 5631 {
de520661 5632 if (! constructor_incremental)
94ba5069 5633 {
19d76e60 5634 if (field && TREE_CODE (field) == INTEGER_CST)
94ba5069
RS
5635 field = copy_node (field);
5636 constructor_elements
5637 = tree_cons (field, digest_init (type, value, 0, 0),
5638 constructor_elements);
5639 }
de520661 5640 else
b62acd60
RS
5641 {
5642 /* Structure elements may require alignment.
5643 Do this, if necessary. */
5644 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5645 {
5646 /* Advance to offset of this element. */
5647 if (! tree_int_cst_equal (constructor_bit_index,
5648 DECL_FIELD_BITPOS (constructor_fields)))
5649 {
5650 int next = (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field))
5651 / BITS_PER_UNIT);
5652 int here = (TREE_INT_CST_LOW (constructor_bit_index)
5653 / BITS_PER_UNIT);
5654
5655 assemble_zeros (next - here);
5656 }
5657 }
d11fdb45
RS
5658 output_constant (digest_init (type, value, 0, 0),
5659 int_size_in_bytes (type));
b62acd60 5660
925d5bbf
RS
5661 /* For a record or union,
5662 keep track of end position of last field. */
5663 if (TREE_CODE (constructor_type) == RECORD_TYPE
5664 || TREE_CODE (constructor_type) == UNION_TYPE)
b62acd60
RS
5665 {
5666 tree temp = size_binop (PLUS_EXPR,
5667 DECL_FIELD_BITPOS (constructor_fields),
5668 DECL_SIZE (constructor_fields));
5669 TREE_INT_CST_LOW (constructor_bit_index)
5670 = TREE_INT_CST_LOW (temp);
5671 TREE_INT_CST_HIGH (constructor_bit_index)
5672 = TREE_INT_CST_HIGH (temp);
5673 }
5674 }
c2f4acb7
RS
5675 }
5676
de520661
RS
5677 /* Advance the variable that indicates sequential elements output. */
5678 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
400fbf9f 5679 {
de520661
RS
5680 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
5681 integer_one_node);
5682 TREE_INT_CST_LOW (constructor_unfilled_index)
5683 = TREE_INT_CST_LOW (tem);
5684 TREE_INT_CST_HIGH (constructor_unfilled_index)
5685 = TREE_INT_CST_HIGH (tem);
5686 }
5687 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5688 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5689 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5690 constructor_unfilled_fields = 0;
5691
5692 /* Now output any pending elements which have become next. */
5693 if (pending)
5694 output_pending_init_elements (0);
5695 }
5696}
400fbf9f 5697
de520661
RS
5698/* Output any pending elements which have become next.
5699 As we output elements, constructor_unfilled_{fields,index}
5700 advances, which may cause other elements to become next;
5701 if so, they too are output.
5702
5703 If ALL is 0, we return when there are
5704 no more pending elements to output now.
5705
5706 If ALL is 1, we output space as necessary so that
5707 we can output all the pending elements. */
5708
5709static void
5710output_pending_init_elements (all)
5711 int all;
5712{
5713 tree tail;
5714 tree next;
5715
5716 retry:
5717
5718 /* Look thru the whole pending list.
5719 If we find an element that should be output now,
5720 output it. Otherwise, set NEXT to the element
5721 that comes first among those still pending. */
5722
5723 next = 0;
5724 for (tail = constructor_pending_elts; tail;
5725 tail = TREE_CHAIN (tail))
5726 {
5727 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5728 {
5729 if (tree_int_cst_equal (TREE_PURPOSE (tail),
5730 constructor_unfilled_index))
400fbf9f 5731 {
de520661
RS
5732 output_init_element (TREE_VALUE (tail), TREE_TYPE (constructor_type),
5733 constructor_unfilled_index, 0);
5734 goto retry;
5735 }
5736 else if (tree_int_cst_lt (TREE_PURPOSE (tail),
5737 constructor_unfilled_index))
5738 ;
5739 else if (next == 0
5740 || tree_int_cst_lt (TREE_PURPOSE (tail),
5741 next))
5742 next = TREE_PURPOSE (tail);
5743 }
5744 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5745 || TREE_CODE (constructor_type) == UNION_TYPE)
5746 {
5747 if (TREE_PURPOSE (tail) == constructor_unfilled_fields)
5748 {
5749 output_init_element (TREE_VALUE (tail),
5750 TREE_TYPE (constructor_unfilled_fields),
5751 constructor_unfilled_fields,
5752 0);
5753 goto retry;
400fbf9f 5754 }
281ec92f
RS
5755 else if (constructor_unfilled_fields == 0
5756 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
5757 DECL_FIELD_BITPOS (constructor_unfilled_fields)))
de520661
RS
5758 ;
5759 else if (next == 0
5760 || tree_int_cst_lt (DECL_FIELD_BITPOS (TREE_PURPOSE (tail)),
5761 DECL_FIELD_BITPOS (next)))
5762 next = TREE_PURPOSE (tail);
400fbf9f 5763 }
de520661
RS
5764 }
5765
5766 /* Ordinarily return, but not if we want to output all
5767 and there are elements left. */
5768 if (! (all && next != 0))
5769 return;
5770
5771 /* Generate space up to the position of NEXT. */
5772 if (constructor_incremental)
5773 {
5774 tree filled;
5775 tree nextpos_tree;
400fbf9f 5776
de520661
RS
5777 if (TREE_CODE (constructor_type) == RECORD_TYPE
5778 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 5779 {
de520661
RS
5780 /* Find the last field written out. */
5781 for (tail = TYPE_FIELDS (constructor_type); tail;
5782 tail = TREE_CHAIN (tail))
5783 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
5784 break;
5785 /* Find the offset of the end of that field. */
5786 filled = size_binop (CEIL_DIV_EXPR,
5787 size_binop (PLUS_EXPR,
5788 DECL_FIELD_BITPOS (tail),
5789 DECL_SIZE (tail)),
5790 size_int (BITS_PER_UNIT));
5791 nextpos_tree = size_binop (CEIL_DIV_EXPR,
5792 DECL_FIELD_BITPOS (next),
5793 size_int (BITS_PER_UNIT));
5794 constructor_unfilled_fields = next;
400fbf9f 5795 }
de520661 5796 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
400fbf9f 5797 {
de520661
RS
5798 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5799 size_in_bytes (TREE_TYPE (constructor_type)));
5800 nextpos_tree
5801 = size_binop (MULT_EXPR, next,
5802 size_in_bytes (TREE_TYPE (constructor_type)));
5803 TREE_INT_CST_LOW (constructor_unfilled_index)
5804 = TREE_INT_CST_LOW (next);
5805 TREE_INT_CST_HIGH (constructor_unfilled_index)
5806 = TREE_INT_CST_HIGH (next);
400fbf9f 5807 }
de520661
RS
5808 else
5809 filled = 0;
400fbf9f 5810
de520661 5811 if (filled)
fe67cf58 5812 {
de520661
RS
5813 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
5814
5815 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
fe67cf58 5816 }
de520661 5817 }
94ba5069
RS
5818 else
5819 {
5820 /* If it's not incremental, just skip over the gap,
5821 so that after jumping to retry we will output the next
5822 successive element. */
5823 if (TREE_CODE (constructor_type) == RECORD_TYPE
5824 || TREE_CODE (constructor_type) == UNION_TYPE)
5825 constructor_unfilled_fields = next;
5826 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5827 {
5828 TREE_INT_CST_LOW (constructor_unfilled_index)
5829 = TREE_INT_CST_LOW (next);
5830 TREE_INT_CST_HIGH (constructor_unfilled_index)
5831 = TREE_INT_CST_HIGH (next);
5832 }
5833 }
de520661
RS
5834
5835 goto retry;
5836}
5837\f
5838/* Add one non-braced element to the current constructor level.
5839 This adjusts the current position within the constructor's type.
5840 This may also start or terminate implicit levels
5841 to handle a partly-braced initializer.
5842
5843 Once this has found the correct level for the new element,
5844 it calls output_init_element.
5845
5846 Note: if we are incrementally outputting this constructor,
5847 this function may be called with a null argument
5848 representing a sub-constructor that was already incrementally output.
5849 When that happens, we output nothing, but we do the bookkeeping
5850 to skip past that element of the current constructor. */
5851
5852void
5853process_init_element (value)
5854 tree value;
5855{
b62acd60
RS
5856 tree orig_value = value;
5857 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5858
790e9490
RS
5859 /* Handle superfluous braces around string cst as in
5860 char x[] = {"foo"}; */
5861 if (string_flag
5862 && TREE_CODE (constructor_type) == ARRAY_TYPE
61e215dd 5863 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
790e9490
RS
5864 && integer_zerop (constructor_unfilled_index))
5865 {
5866 constructor_stack->replacement_value = value;
5867 return;
5868 }
5869
790e9490
RS
5870 if (constructor_stack->replacement_value != 0)
5871 {
5872 error_init ("excess elements in struct initializer%s",
5873 " after `%s'", NULL_PTR);
5874 return;
5875 }
5876
91fa3c30
RS
5877 /* Ignore elements of a brace group if it is entirely superfluous
5878 and has already been diagnosed. */
5879 if (constructor_type == 0)
5880 return;
5881
de520661
RS
5882 /* If we've exhausted any levels that didn't have braces,
5883 pop them now. */
5884 while (constructor_stack->implicit)
5885 {
5886 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5887 || TREE_CODE (constructor_type) == UNION_TYPE)
5888 && constructor_fields == 0)
5889 process_init_element (pop_init_level (1));
5890 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5891 && tree_int_cst_lt (constructor_max_index, constructor_index))
5892 process_init_element (pop_init_level (1));
fe67cf58 5893 else
de520661 5894 break;
400fbf9f
JW
5895 }
5896
de520661 5897 while (1)
400fbf9f 5898 {
de520661 5899 if (TREE_CODE (constructor_type) == RECORD_TYPE)
400fbf9f 5900 {
de520661
RS
5901 tree fieldtype;
5902 enum tree_code fieldcode;
5903
5904 if (constructor_fields == 0)
5905 {
5906 pedwarn_init ("excess elements in struct initializer%s",
5907 " after `%s'", NULL_PTR);
5908 break;
5909 }
5910
5911 fieldtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_fields));
5912 fieldcode = TREE_CODE (fieldtype);
5913
b62acd60
RS
5914 /* Accept a string constant to initialize a subarray. */
5915 if (value != 0
5916 && fieldcode == ARRAY_TYPE
5917 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5918 && string_flag)
5919 value = orig_value;
5920 /* Otherwise, if we have come to a subaggregate,
5921 and we don't have an element of its type, push into it. */
cc77d4d5 5922 else if (value != 0 && !constructor_no_implicit
b62acd60
RS
5923 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5924 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5925 || fieldcode == UNION_TYPE))
de520661
RS
5926 {
5927 push_init_level (1);
5928 continue;
5929 }
5930
5931 if (value)
5932 {
19d76e60 5933 push_member_name (constructor_fields);
de520661
RS
5934 output_init_element (value, fieldtype, constructor_fields, 1);
5935 RESTORE_SPELLING_DEPTH (constructor_depth);
5936 }
5937 else
b62acd60
RS
5938 /* Do the bookkeeping for an element that was
5939 directly output as a constructor. */
5940 {
5941 /* For a record, keep track of end position of last field. */
5942 tree temp = size_binop (PLUS_EXPR,
5943 DECL_FIELD_BITPOS (constructor_fields),
5944 DECL_SIZE (constructor_fields));
5945 TREE_INT_CST_LOW (constructor_bit_index)
5946 = TREE_INT_CST_LOW (temp);
5947 TREE_INT_CST_HIGH (constructor_bit_index)
5948 = TREE_INT_CST_HIGH (temp);
5949
5950 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5951 }
de520661
RS
5952
5953 constructor_fields = TREE_CHAIN (constructor_fields);
fc623854
RS
5954 /* Skip any nameless bit fields atthe beginning. */
5955 while (constructor_fields != 0 && DECL_BIT_FIELD (constructor_fields)
5956 && DECL_NAME (constructor_fields) == 0)
5957 constructor_fields = TREE_CHAIN (constructor_fields);
de520661 5958 break;
400fbf9f 5959 }
de520661 5960 if (TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 5961 {
de520661
RS
5962 tree fieldtype;
5963 enum tree_code fieldcode;
5964
5965 if (constructor_fields == 0)
5966 {
5967 pedwarn_init ("excess elements in union initializer%s",
5968 " after `%s'", NULL_PTR);
5969 break;
5970 }
5971
5972 fieldtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_fields));
5973 fieldcode = TREE_CODE (fieldtype);
5974
b62acd60
RS
5975 /* Accept a string constant to initialize a subarray. */
5976 if (value != 0
5977 && fieldcode == ARRAY_TYPE
5978 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5979 && string_flag)
5980 value = orig_value;
5981 /* Otherwise, if we have come to a subaggregate,
5982 and we don't have an element of its type, push into it. */
cc77d4d5 5983 else if (value != 0 && !constructor_no_implicit
b62acd60
RS
5984 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5985 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5986 || fieldcode == UNION_TYPE))
de520661
RS
5987 {
5988 push_init_level (1);
5989 continue;
5990 }
5991
5992 if (value)
5993 {
19d76e60 5994 push_member_name (constructor_fields);
de520661
RS
5995 output_init_element (value, fieldtype, constructor_fields, 1);
5996 RESTORE_SPELLING_DEPTH (constructor_depth);
5997 }
5998 else
94ba5069
RS
5999 /* Do the bookkeeping for an element that was
6000 directly output as a constructor. */
6001 {
6002 TREE_INT_CST_LOW (constructor_bit_index)
6003 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6004 TREE_INT_CST_HIGH (constructor_bit_index)
6005 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6006
6007 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6008 }
de520661
RS
6009
6010 constructor_fields = 0;
6011 break;
400fbf9f 6012 }
de520661
RS
6013 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6014 {
6015 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6016 enum tree_code eltcode = TREE_CODE (elttype);
6017
b62acd60
RS
6018 /* Accept a string constant to initialize a subarray. */
6019 if (value != 0
6020 && eltcode == ARRAY_TYPE
6021 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6022 && string_flag)
6023 value = orig_value;
6024 /* Otherwise, if we have come to a subaggregate,
6025 and we don't have an element of its type, push into it. */
cc77d4d5 6026 else if (value != 0 && !constructor_no_implicit
b62acd60
RS
6027 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6028 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6029 || eltcode == UNION_TYPE))
de520661
RS
6030 {
6031 push_init_level (1);
6032 continue;
6033 }
6034
6035 if (constructor_max_index != 0
6036 && tree_int_cst_lt (constructor_max_index, constructor_index))
6037 {
6038 pedwarn_init ("excess elements in array initializer%s",
6039 " after `%s'", NULL_PTR);
6040 break;
6041 }
400fbf9f 6042
de520661
RS
6043 /* Now output the actual element.
6044 Ordinarily, output once.
6045 If there is a range, repeat it till we advance past the range. */
6046 do
6047 {
6048 tree tem;
d45cf215 6049
de520661
RS
6050 if (value)
6051 {
6052 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6053 output_init_element (value, elttype, constructor_index, 1);
6054 RESTORE_SPELLING_DEPTH (constructor_depth);
6055 }
d45cf215 6056
de520661
RS
6057 tem = size_binop (PLUS_EXPR, constructor_index,
6058 integer_one_node);
6059 TREE_INT_CST_LOW (constructor_index)
6060 = TREE_INT_CST_LOW (tem);
6061 TREE_INT_CST_HIGH (constructor_index)
6062 = TREE_INT_CST_HIGH (tem);
6063
6064 if (!value)
6065 /* If we are doing the bookkeeping for an element that was
6066 directly output as a constructor,
6067 we must update constructor_unfilled_index. */
6068 {
6069 TREE_INT_CST_LOW (constructor_unfilled_index)
6070 = TREE_INT_CST_LOW (constructor_index);
6071 TREE_INT_CST_HIGH (constructor_unfilled_index)
6072 = TREE_INT_CST_HIGH (constructor_index);
6073 }
6074 }
6075 while (! (constructor_range_end == 0
6076 || tree_int_cst_lt (constructor_range_end,
6077 constructor_index)));
400fbf9f 6078
de520661
RS
6079 break;
6080 }
6081
6082 /* Handle the sole element allowed in a braced initializer
6083 for a scalar variable. */
6084 if (constructor_fields == 0)
6085 {
6086 pedwarn_init ("excess elements in scalar initializer%s",
6087 " after `%s'", NULL_PTR);
6088 break;
6089 }
6090
6091 if (value)
6092 output_init_element (value, constructor_type, NULL_TREE, 1);
6093 constructor_fields = 0;
6094 break;
fe67cf58 6095 }
de520661
RS
6096
6097 /* If the (lexically) previous elments are not now saved,
6098 we can discard the storage for them. */
fe23d5ad 6099 if (constructor_incremental && constructor_pending_elts == 0 && value != 0)
de520661 6100 clear_momentary ();
400fbf9f
JW
6101}
6102\f
6103/* Expand an ASM statement with operands, handling output operands
6104 that are not variables or INDIRECT_REFS by transforming such
6105 cases into cases that expand_asm_operands can handle.
6106
6107 Arguments are same as for expand_asm_operands. */
6108
6109void
6110c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6111 tree string, outputs, inputs, clobbers;
6112 int vol;
6113 char *filename;
6114 int line;
6115{
6116 int noutputs = list_length (outputs);
6117 register int i;
6118 /* o[I] is the place that output number I should be written. */
6119 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6120 register tree tail;
6121
6122 if (TREE_CODE (string) == ADDR_EXPR)
6123 string = TREE_OPERAND (string, 0);
6124 if (TREE_CODE (string) != STRING_CST)
6125 {
6126 error ("asm template is not a string constant");
6127 return;
6128 }
6129
7b6327ae 6130 /* Record the contents of OUTPUTS before it is modified. */
400fbf9f
JW
6131 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6132 o[i] = TREE_VALUE (tail);
6133
6134 /* Perform default conversions on array and function inputs. */
6135 /* Don't do this for other types--
6136 it would screw up operands expected to be in memory. */
6137 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6138 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6139 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6140 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6141
6142 /* Generate the ASM_OPERANDS insn;
6143 store into the TREE_VALUEs of OUTPUTS some trees for
6144 where the values were actually stored. */
6145 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6146
6147 /* Copy all the intermediate outputs into the specified outputs. */
6148 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6149 {
6150 if (o[i] != TREE_VALUE (tail))
6151 {
6152 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6153 0, VOIDmode, 0);
6154 free_temp_slots ();
6155 }
6156 /* Detect modification of read-only values.
6157 (Otherwise done by build_modify_expr.) */
6158 else
6159 {
6160 tree type = TREE_TYPE (o[i]);
6161 if (TYPE_READONLY (type)
6162 || ((TREE_CODE (type) == RECORD_TYPE
6163 || TREE_CODE (type) == UNION_TYPE)
6164 && C_TYPE_FIELDS_READONLY (type)))
6165 readonly_warning (o[i], "modification by `asm'");
6166 }
6167 }
6168
6169 /* Those MODIFY_EXPRs could do autoincrements. */
6170 emit_queue ();
6171}
6172\f
6173/* Expand a C `return' statement.
6174 RETVAL is the expression for what to return,
6175 or a null pointer for `return;' with no value. */
6176
6177void
6178c_expand_return (retval)
6179 tree retval;
6180{
6181 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6182
6183 if (TREE_THIS_VOLATILE (current_function_decl))
6184 warning ("function declared `volatile' has a `return' statement");
6185
6186 if (!retval)
6187 {
6188 current_function_returns_null = 1;
6189 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6190 warning ("`return' with no value, in function returning non-void");
6191 expand_null_return ();
6192 }
6193 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6194 {
6195 current_function_returns_null = 1;
6196 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6197 pedwarn ("`return' with a value, in function returning void");
6198 expand_return (retval);
6199 }
6200 else
6201 {
6202 tree t = convert_for_assignment (valtype, retval, "return",
9b7267b8 6203 NULL_TREE, NULL_TREE, 0);
400fbf9f 6204 tree res = DECL_RESULT (current_function_decl);
70768eda
RK
6205
6206 if (t == error_mark_node)
6207 return;
6208
400fbf9f
JW
6209 t = build (MODIFY_EXPR, TREE_TYPE (res),
6210 res, convert (TREE_TYPE (res), t));
1c2a9b35 6211 TREE_SIDE_EFFECTS (t) = 1;
400fbf9f
JW
6212 expand_return (t);
6213 current_function_returns_value = 1;
6214 }
6215}
6216\f
6217/* Start a C switch statement, testing expression EXP.
6218 Return EXP if it is valid, an error node otherwise. */
6219
6220tree
6221c_expand_start_case (exp)
6222 tree exp;
6223{
6224 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6225 tree type = TREE_TYPE (exp);
6226
6227 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6228 {
6229 error ("switch quantity not an integer");
6230 exp = error_mark_node;
6231 }
6232 else
6233 {
6234 tree index;
6cb72a7d 6235 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
400fbf9f
JW
6236
6237 if (warn_traditional
6cb72a7d
RS
6238 && (type == long_integer_type_node
6239 || type == long_unsigned_type_node))
400fbf9f
JW
6240 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
6241
6242 exp = default_conversion (exp);
6243 type = TREE_TYPE (exp);
8d9bfdc5 6244 index = get_unwidened (exp, NULL_TREE);
400fbf9f
JW
6245 /* We can't strip a conversion from a signed type to an unsigned,
6246 because if we did, int_fits_type_p would do the wrong thing
6247 when checking case values for being in range,
6248 and it's too hard to do the right thing. */
6249 if (TREE_UNSIGNED (TREE_TYPE (exp))
6250 == TREE_UNSIGNED (TREE_TYPE (index)))
6251 exp = index;
6252 }
6253
6254 expand_start_case (1, exp, type, "switch statement");
6255
6256 return exp;
6257}