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