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