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