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