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