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