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