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