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