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