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