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
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
require_complete_type(value)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
incomplete_type_error(value,type)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
qualify_type(type,like)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
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
common_type(t1,t2)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
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
comptypes(type1,type2)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
comp_target_types(ttl,ttr)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
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
function_types_compatible_p(f1,f2)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
type_lists_compatible_p(args1,args2)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
self_promoting_args_p(parms)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
self_promoting_type_p(type)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
667 /* Return an unsigned type the same as TYPE in other respects. */
668
669 tree
unsigned_type(type)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
signed_type(type)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
signed_or_unsigned_type(unsignedp,type)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
c_sizeof(type)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
c_sizeof_nowarn(type)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
c_size_in_bytes(type)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
c_alignof(type)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
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
c_alignof_expr(expr)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
decl_constant_value(decl)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
default_conversion(exp)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
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
build_component_ref(datum,component)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
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
build_indirect_ref(ptr,errorstring)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
build_array_ref(array,index)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
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
record_format_info(function_ident,is_scan,format_num,first_arg_num)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
init_format_info_table()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
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
check_format(info,params)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 (TREE_CODE (cur_type) != ERROR_MARK
1798 && TYPE_NAME (cur_type) != 0
1799 && TREE_CODE (cur_type) != INTEGER_TYPE
1800 && !(TREE_CODE (cur_type) == POINTER_TYPE
1801 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1802 {
1803 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1804 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1805 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1806 else
1807 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1808 }
1809
1810 /* A nameless type can't possibly match what the format wants.
1811 So there will be a warning for it.
1812 Make up a string to describe vaguely what it is. */
1813 if (that == 0)
1814 {
1815 if (TREE_CODE (cur_type) == POINTER_TYPE)
1816 that = "pointer";
1817 else
1818 that = "different type";
1819 }
1820
1821 if (strcmp (this, that) != 0)
1822 {
1823 sprintf (message, "%s format, %s arg (arg %d)",
1824 this, that, arg_num);
1825 warning (message);
1826 }
1827 }
1828 }
1829 }
1830
1831 /* Build a function call to function FUNCTION with parameters PARAMS.
1832 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1833 TREE_VALUE of each node is a parameter-expression.
1834 FUNCTION's data type may be a function type or a pointer-to-function. */
1835
1836 tree
build_function_call(function,params)1837 build_function_call (function, params)
1838 tree function, params;
1839 {
1840 register tree fntype, fundecl;
1841 register tree coerced_params;
1842 tree name = NULL_TREE;
1843
1844 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1845 STRIP_TYPE_NOPS (function);
1846
1847 /* Convert anything with function type to a pointer-to-function. */
1848 if (TREE_CODE (function) == FUNCTION_DECL)
1849 {
1850 name = DECL_NAME (function);
1851 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1852 (because calling an inline function does not mean the function
1853 needs to be separately compiled). */
1854 fntype = build_type_variant (TREE_TYPE (function),
1855 TREE_READONLY (function),
1856 TREE_THIS_VOLATILE (function));
1857 fundecl = function;
1858 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1859 }
1860 else
1861 function = default_conversion (function);
1862
1863 fntype = TREE_TYPE (function);
1864
1865 if (TREE_CODE (fntype) == ERROR_MARK)
1866 return error_mark_node;
1867
1868 if (!(TREE_CODE (fntype) == POINTER_TYPE
1869 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1870 {
1871 error ("called object is not a function");
1872 return error_mark_node;
1873 }
1874
1875 /* fntype now gets the type of function pointed to. */
1876 fntype = TREE_TYPE (fntype);
1877
1878 /* Convert the parameters to the types declared in the
1879 function prototype, or apply default promotions. */
1880
1881 coerced_params
1882 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1883
1884 /* Check for errors in format strings. */
1885 if (warn_format && name != 0)
1886 {
1887 unsigned int i;
1888
1889 /* See if this function is a format function. */
1890 for (i = 0; i < function_info_entries; i++)
1891 if (function_info_table[i].function_ident == name)
1892 {
1893 register char *message;
1894
1895 /* If so, check it. */
1896 check_format (&function_info_table[i], coerced_params);
1897 break;
1898 }
1899 }
1900
1901 /* Recognize certain built-in functions so we can make tree-codes
1902 other than CALL_EXPR. We do this when it enables fold-const.c
1903 to do something useful. */
1904
1905 if (TREE_CODE (function) == ADDR_EXPR
1906 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1907 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1908 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1909 {
1910 case BUILT_IN_ABS:
1911 case BUILT_IN_LABS:
1912 case BUILT_IN_FABS:
1913 if (coerced_params == 0)
1914 return integer_zero_node;
1915 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1916 }
1917
1918 {
1919 register tree result
1920 = build (CALL_EXPR, TREE_TYPE (fntype),
1921 function, coerced_params, NULL_TREE);
1922
1923 TREE_SIDE_EFFECTS (result) = 1;
1924 if (TREE_TYPE (result) == void_type_node)
1925 return result;
1926 return require_complete_type (result);
1927 }
1928 }
1929
1930 /* Convert the argument expressions in the list VALUES
1931 to the types in the list TYPELIST. The result is a list of converted
1932 argument expressions.
1933
1934 If TYPELIST is exhausted, or when an element has NULL as its type,
1935 perform the default conversions.
1936
1937 PARMLIST is the chain of parm decls for the function being called.
1938 It may be 0, if that info is not available.
1939 It is used only for generating error messages.
1940
1941 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1942
1943 This is also where warnings about wrong number of args are generated.
1944
1945 Both VALUES and the returned value are chains of TREE_LIST nodes
1946 with the elements of the list in the TREE_VALUE slots of those nodes. */
1947
1948 static tree
convert_arguments(typelist,values,name,fundecl)1949 convert_arguments (typelist, values, name, fundecl)
1950 tree typelist, values, name, fundecl;
1951 {
1952 register tree typetail, valtail;
1953 register tree result = NULL;
1954 int parmnum;
1955
1956 /* Scan the given expressions and types, producing individual
1957 converted arguments and pushing them on RESULT in reverse order. */
1958
1959 for (valtail = values, typetail = typelist, parmnum = 0;
1960 valtail;
1961 valtail = TREE_CHAIN (valtail), parmnum++)
1962 {
1963 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1964 register tree val = TREE_VALUE (valtail);
1965
1966 if (type == void_type_node)
1967 {
1968 if (name)
1969 error ("too many arguments to function `%s'",
1970 IDENTIFIER_POINTER (name));
1971 else
1972 error ("too many arguments to function");
1973 break;
1974 }
1975
1976 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1977 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1978 to convert automatically to a pointer. */
1979 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1980 val = TREE_OPERAND (val, 0);
1981
1982 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1983 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1984 val = default_conversion (val);
1985
1986 val = require_complete_type (val);
1987
1988 if (type != 0)
1989 {
1990 /* Formal parm type is specified by a function prototype. */
1991 tree parmval;
1992
1993 if (TYPE_SIZE (type) == 0)
1994 {
1995 error ("type of formal parameter %d is incomplete", parmnum + 1);
1996 parmval = val;
1997 }
1998 else
1999 {
2000 tree parmname;
2001 #ifdef PROMOTE_PROTOTYPES
2002 /* Rather than truncating and then reextending,
2003 convert directly to int, if that's the type we will want. */
2004 if (! flag_traditional
2005 && TREE_CODE (type) == INTEGER_TYPE
2006 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2007 type = integer_type_node;
2008 #endif
2009
2010 #if 0 /* This turns out not to win--there's no way to write a prototype
2011 for a function whose arg type is a union with no tag. */
2012 /* Nameless union automatically casts the types it contains. */
2013 if (TREE_CODE (type) == UNION_TYPE && TYPE_NAME (type) == 0)
2014 {
2015 tree field;
2016
2017 for (field = TYPE_FIELDS (type); field;
2018 field = TREE_CHAIN (field))
2019 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2020 TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2021 break;
2022
2023 if (field)
2024 val = build1 (CONVERT_EXPR, type, val);
2025 }
2026 #endif
2027
2028 /* Optionally warn about conversions that
2029 differ from the default conversions. */
2030 if (warn_conversion)
2031 {
2032 int formal_prec = TYPE_PRECISION (type);
2033
2034 if (TREE_CODE (type) != REAL_TYPE
2035 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2036 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
2037 else if (TREE_CODE (type) == REAL_TYPE
2038 && TREE_CODE (TREE_TYPE (val)) != REAL_TYPE)
2039 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
2040 else if (TREE_CODE (type) == REAL_TYPE
2041 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2042 {
2043 /* Warn if any argument is passed as `float',
2044 since without a prototype it would be `double'. */
2045 if (formal_prec == TYPE_PRECISION (float_type_node))
2046 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
2047 }
2048 /* Detect integer changing in width or signedness. */
2049 else if ((TREE_CODE (type) == INTEGER_TYPE
2050 || TREE_CODE (type) == ENUMERAL_TYPE)
2051 && (TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
2052 || TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE))
2053 {
2054 tree would_have_been = default_conversion (val);
2055 tree type1 = TREE_TYPE (would_have_been);
2056
2057 if (TREE_CODE (type) == ENUMERAL_TYPE
2058 && type == TREE_TYPE (val))
2059 /* No warning if function asks for enum
2060 and the actual arg is that enum type. */
2061 ;
2062 else if (formal_prec != TYPE_PRECISION (type1))
2063 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
2064 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
2065 ;
2066 /* Don't complain if the formal parameter type
2067 is an enum, because we can't tell now whether
2068 the value was an enum--even the same enum. */
2069 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2070 ;
2071 else if (TREE_CODE (val) == INTEGER_CST
2072 && int_fits_type_p (val, type))
2073 /* Change in signedness doesn't matter
2074 if a constant value is unaffected. */
2075 ;
2076 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
2077 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
2078 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
2079 /* Change in signedness doesn't matter
2080 if an enum value is unaffected. */
2081 ;
2082 else if (TREE_UNSIGNED (type))
2083 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
2084 else
2085 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
2086 }
2087 }
2088
2089 parmval = convert_for_assignment (type, val,
2090 (char *)0, /* arg passing */
2091 fundecl, name, parmnum + 1);
2092
2093 #ifdef PROMOTE_PROTOTYPES
2094 if (TREE_CODE (type) == INTEGER_TYPE
2095 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2096 parmval = default_conversion (parmval);
2097 #endif
2098 }
2099 result = tree_cons (NULL_TREE, parmval, result);
2100 }
2101 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2102 && (TYPE_PRECISION (TREE_TYPE (val))
2103 < TYPE_PRECISION (double_type_node)))
2104 /* Convert `float' to `double'. */
2105 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2106 else
2107 /* Convert `short' and `char' to full-size `int'. */
2108 result = tree_cons (NULL_TREE, default_conversion (val), result);
2109
2110 if (typetail)
2111 typetail = TREE_CHAIN (typetail);
2112 }
2113
2114 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2115 {
2116 if (name)
2117 error ("too few arguments to function `%s'",
2118 IDENTIFIER_POINTER (name));
2119 else
2120 error ("too few arguments to function");
2121 }
2122
2123 return nreverse (result);
2124 }
2125
2126 /* This is the entry point used by the parser
2127 for binary operators in the input.
2128 In addition to constructing the expression,
2129 we check for operands that were written with other binary operators
2130 in a way that is likely to confuse the user. */
2131
2132 tree
parser_build_binary_op(code,arg1,arg2)2133 parser_build_binary_op (code, arg1, arg2)
2134 enum tree_code code;
2135 tree arg1, arg2;
2136 {
2137 tree result = build_binary_op (code, arg1, arg2, 1);
2138
2139 char class;
2140 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
2141 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
2142 enum tree_code code1 = ERROR_MARK;
2143 enum tree_code code2 = ERROR_MARK;
2144
2145 if (class1 == 'e' || class1 == '1'
2146 || class1 == '2' || class1 == '<')
2147 code1 = C_EXP_ORIGINAL_CODE (arg1);
2148 if (class2 == 'e' || class2 == '1'
2149 || class2 == '2' || class2 == '<')
2150 code2 = C_EXP_ORIGINAL_CODE (arg2);
2151
2152 /* Check for cases such as x+y<<z which users are likely
2153 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2154 is cleared to prevent these warnings. */
2155 if (warn_parentheses)
2156 {
2157 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2158 {
2159 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2160 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2161 warning ("suggest parentheses around + or - inside shift");
2162 }
2163
2164 if (code == TRUTH_ORIF_EXPR)
2165 {
2166 if (code1 == TRUTH_ANDIF_EXPR
2167 || code2 == TRUTH_ANDIF_EXPR)
2168 warning ("suggest parentheses around && within ||");
2169 }
2170
2171 if (code == BIT_IOR_EXPR)
2172 {
2173 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2174 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2175 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2176 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2177 warning ("suggest parentheses around arithmetic in operand of |");
2178 }
2179
2180 if (code == BIT_XOR_EXPR)
2181 {
2182 if (code1 == BIT_AND_EXPR
2183 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2184 || code2 == BIT_AND_EXPR
2185 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2186 warning ("suggest parentheses around arithmetic in operand of ^");
2187 }
2188
2189 if (code == BIT_AND_EXPR)
2190 {
2191 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2192 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2193 warning ("suggest parentheses around + or - in operand of &");
2194 }
2195 }
2196
2197 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2198 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2199 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2200 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2201
2202 unsigned_conversion_warning (result, arg1);
2203 unsigned_conversion_warning (result, arg2);
2204 overflow_warning (result);
2205
2206 class = TREE_CODE_CLASS (TREE_CODE (result));
2207
2208 /* Record the code that was specified in the source,
2209 for the sake of warnings about confusing nesting. */
2210 if (class == 'e' || class == '1'
2211 || class == '2' || class == '<')
2212 C_SET_EXP_ORIGINAL_CODE (result, code);
2213 else
2214 {
2215 int flag = TREE_CONSTANT (result);
2216 /* We use NOP_EXPR rather than NON_LVALUE_EXPR
2217 so that convert_for_assignment won't strip it.
2218 That way, we get warnings for things like p = (1 - 1). */
2219 result = build1 (NOP_EXPR, TREE_TYPE (result), result);
2220 C_SET_EXP_ORIGINAL_CODE (result, code);
2221 TREE_CONSTANT (result) = flag;
2222 }
2223
2224 return result;
2225 }
2226
2227 /* Build a binary-operation expression without default conversions.
2228 CODE is the kind of expression to build.
2229 This function differs from `build' in several ways:
2230 the data type of the result is computed and recorded in it,
2231 warnings are generated if arg data types are invalid,
2232 special handling for addition and subtraction of pointers is known,
2233 and some optimization is done (operations on narrow ints
2234 are done in the narrower type when that gives the same result).
2235 Constant folding is also done before the result is returned.
2236
2237 Note that the operands will never have enumeral types, or function
2238 or array types, because either they will have the default conversions
2239 performed or they have both just been converted to some other type in which
2240 the arithmetic is to be done. */
2241
2242 tree
build_binary_op(code,orig_op0,orig_op1,convert_p)2243 build_binary_op (code, orig_op0, orig_op1, convert_p)
2244 enum tree_code code;
2245 tree orig_op0, orig_op1;
2246 int convert_p;
2247 {
2248 tree type0, type1;
2249 register enum tree_code code0, code1;
2250 tree op0, op1;
2251
2252 /* Expression code to give to the expression when it is built.
2253 Normally this is CODE, which is what the caller asked for,
2254 but in some special cases we change it. */
2255 register enum tree_code resultcode = code;
2256
2257 /* Data type in which the computation is to be performed.
2258 In the simplest cases this is the common type of the arguments. */
2259 register tree result_type = NULL;
2260
2261 /* Nonzero means operands have already been type-converted
2262 in whatever way is necessary.
2263 Zero means they need to be converted to RESULT_TYPE. */
2264 int converted = 0;
2265
2266 /* Nonzero means after finally constructing the expression
2267 give it this type. Otherwise, give it type RESULT_TYPE. */
2268 tree final_type = 0;
2269
2270 /* Nonzero if this is an operation like MIN or MAX which can
2271 safely be computed in short if both args are promoted shorts.
2272 Also implies COMMON.
2273 -1 indicates a bitwise operation; this makes a difference
2274 in the exact conditions for when it is safe to do the operation
2275 in a narrower mode. */
2276 int shorten = 0;
2277
2278 /* Nonzero if this is a comparison operation;
2279 if both args are promoted shorts, compare the original shorts.
2280 Also implies COMMON. */
2281 int short_compare = 0;
2282
2283 /* Nonzero if this is a right-shift operation, which can be computed on the
2284 original short and then promoted if the operand is a promoted short. */
2285 int short_shift = 0;
2286
2287 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2288 int common = 0;
2289
2290 if (convert_p)
2291 {
2292 op0 = default_conversion (orig_op0);
2293 op1 = default_conversion (orig_op1);
2294 }
2295 else
2296 {
2297 op0 = orig_op0;
2298 op1 = orig_op1;
2299 }
2300
2301 type0 = TREE_TYPE (op0);
2302 type1 = TREE_TYPE (op1);
2303
2304 /* The expression codes of the data types of the arguments tell us
2305 whether the arguments are integers, floating, pointers, etc. */
2306 code0 = TREE_CODE (type0);
2307 code1 = TREE_CODE (type1);
2308
2309 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2310 STRIP_TYPE_NOPS (op0);
2311 STRIP_TYPE_NOPS (op1);
2312
2313 /* If an error was already reported for one of the arguments,
2314 avoid reporting another error. */
2315
2316 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2317 return error_mark_node;
2318
2319 switch (code)
2320 {
2321 case PLUS_EXPR:
2322 /* Handle the pointer + int case. */
2323 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2324 return pointer_int_sum (PLUS_EXPR, op0, op1);
2325 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2326 return pointer_int_sum (PLUS_EXPR, op1, op0);
2327 else
2328 common = 1;
2329 break;
2330
2331 case MINUS_EXPR:
2332 /* Subtraction of two similar pointers.
2333 We must subtract them as integers, then divide by object size. */
2334 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2335 && comp_target_types (type0, type1))
2336 return pointer_diff (op0, op1);
2337 /* Handle pointer minus int. Just like pointer plus int. */
2338 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2339 return pointer_int_sum (MINUS_EXPR, op0, op1);
2340 else
2341 common = 1;
2342 break;
2343
2344 case MULT_EXPR:
2345 common = 1;
2346 break;
2347
2348 case TRUNC_DIV_EXPR:
2349 case CEIL_DIV_EXPR:
2350 case FLOOR_DIV_EXPR:
2351 case ROUND_DIV_EXPR:
2352 case EXACT_DIV_EXPR:
2353 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2354 || code0 == COMPLEX_TYPE)
2355 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2356 || code1 == COMPLEX_TYPE))
2357 {
2358 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2359 resultcode = RDIV_EXPR;
2360 else
2361 /* When dividing two signed integers, you have to promote to int.
2362 E.g. (short) -32868 / (short) -1 doesn't fit in a short. */
2363 shorten = TREE_UNSIGNED (op0);
2364 common = 1;
2365 }
2366 break;
2367
2368 case BIT_AND_EXPR:
2369 case BIT_ANDTC_EXPR:
2370 case BIT_IOR_EXPR:
2371 case BIT_XOR_EXPR:
2372 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2373 shorten = -1;
2374 /* If one operand is a constant, and the other is a short type
2375 that has been converted to an int,
2376 really do the work in the short type and then convert the
2377 result to int. If we are lucky, the constant will be 0 or 1
2378 in the short type, making the entire operation go away. */
2379 if (TREE_CODE (op0) == INTEGER_CST
2380 && TREE_CODE (op1) == NOP_EXPR
2381 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2382 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2383 {
2384 final_type = result_type;
2385 op1 = TREE_OPERAND (op1, 0);
2386 result_type = TREE_TYPE (op1);
2387 }
2388 if (TREE_CODE (op1) == INTEGER_CST
2389 && TREE_CODE (op0) == NOP_EXPR
2390 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2391 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2392 {
2393 final_type = result_type;
2394 op0 = TREE_OPERAND (op0, 0);
2395 result_type = TREE_TYPE (op0);
2396 }
2397 break;
2398
2399 case TRUNC_MOD_EXPR:
2400 case FLOOR_MOD_EXPR:
2401 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2402 shorten = 1;
2403 break;
2404
2405 case TRUTH_ANDIF_EXPR:
2406 case TRUTH_ORIF_EXPR:
2407 case TRUTH_AND_EXPR:
2408 case TRUTH_OR_EXPR:
2409 case TRUTH_XOR_EXPR:
2410 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2411 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2412 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2413 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2414 {
2415 /* Result of these operations is always an int,
2416 but that does not mean the operands should be
2417 converted to ints! */
2418 result_type = integer_type_node;
2419 op0 = truthvalue_conversion (op0);
2420 op1 = truthvalue_conversion (op1);
2421 converted = 1;
2422 }
2423 break;
2424
2425 /* Shift operations: result has same type as first operand;
2426 always convert second operand to int.
2427 Also set SHORT_SHIFT if shifting rightward. */
2428
2429 case RSHIFT_EXPR:
2430 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2431 {
2432 if (TREE_CODE (op1) == INTEGER_CST)
2433 {
2434 if (tree_int_cst_lt (op1, integer_zero_node))
2435 warning ("right shift count is negative");
2436 else
2437 {
2438 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2439 short_shift = 1;
2440 if (TREE_INT_CST_HIGH (op1) != 0
2441 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2442 >= TYPE_PRECISION (type0)))
2443 warning ("right shift count >= width of type");
2444 }
2445 }
2446 /* Use the type of the value to be shifted.
2447 This is what most traditional C compilers do. */
2448 result_type = type0;
2449 /* Unless traditional, convert the shift-count to an integer,
2450 regardless of size of value being shifted. */
2451 if (! flag_traditional)
2452 {
2453 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2454 op1 = convert (integer_type_node, op1);
2455 /* Avoid converting op1 to result_type later. */
2456 converted = 1;
2457 }
2458 }
2459 break;
2460
2461 case LSHIFT_EXPR:
2462 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2463 {
2464 if (TREE_CODE (op1) == INTEGER_CST)
2465 {
2466 if (tree_int_cst_lt (op1, integer_zero_node))
2467 warning ("left shift count is negative");
2468 else if (TREE_INT_CST_HIGH (op1) != 0
2469 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2470 >= TYPE_PRECISION (type0)))
2471 warning ("left shift count >= width of type");
2472 }
2473 /* Use the type of the value to be shifted.
2474 This is what most traditional C compilers do. */
2475 result_type = type0;
2476 /* Unless traditional, convert the shift-count to an integer,
2477 regardless of size of value being shifted. */
2478 if (! flag_traditional)
2479 {
2480 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2481 op1 = convert (integer_type_node, op1);
2482 /* Avoid converting op1 to result_type later. */
2483 converted = 1;
2484 }
2485 }
2486 break;
2487
2488 case RROTATE_EXPR:
2489 case LROTATE_EXPR:
2490 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2491 {
2492 if (TREE_CODE (op1) == INTEGER_CST)
2493 {
2494 if (tree_int_cst_lt (op1, integer_zero_node))
2495 warning ("shift count is negative");
2496 else if (TREE_INT_CST_HIGH (op1) != 0
2497 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2498 >= TYPE_PRECISION (type0)))
2499 warning ("shift count >= width of type");
2500 }
2501 /* Use the type of the value to be shifted.
2502 This is what most traditional C compilers do. */
2503 result_type = type0;
2504 /* Unless traditional, convert the shift-count to an integer,
2505 regardless of size of value being shifted. */
2506 if (! flag_traditional)
2507 {
2508 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2509 op1 = convert (integer_type_node, op1);
2510 /* Avoid converting op1 to result_type later. */
2511 converted = 1;
2512 }
2513 }
2514 break;
2515
2516 case EQ_EXPR:
2517 case NE_EXPR:
2518 /* Result of comparison is always int,
2519 but don't convert the args to int! */
2520 result_type = integer_type_node;
2521 converted = 1;
2522 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2523 || code0 == COMPLEX_TYPE)
2524 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2525 || code1 == COMPLEX_TYPE))
2526 short_compare = 1;
2527 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2528 {
2529 register tree tt0 = TREE_TYPE (type0);
2530 register tree tt1 = TREE_TYPE (type1);
2531 /* Anything compares with void *. void * compares with anything.
2532 Otherwise, the targets must be the same. */
2533 if (comp_target_types (type0, type1))
2534 ;
2535 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2536 {
2537 if (pedantic && !integer_zerop (op0)
2538 && TREE_CODE (tt1) == FUNCTION_TYPE)
2539 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2540 }
2541 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2542 {
2543 if (pedantic && !integer_zerop (op1)
2544 && TREE_CODE (tt0) == FUNCTION_TYPE)
2545 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2546 }
2547 else
2548 pedwarn ("comparison of distinct pointer types lacks a cast");
2549 }
2550 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2551 && integer_zerop (op1))
2552 op1 = null_pointer_node;
2553 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2554 && integer_zerop (op0))
2555 op0 = null_pointer_node;
2556 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2557 {
2558 if (! flag_traditional)
2559 pedwarn ("comparison between pointer and integer");
2560 op1 = convert (TREE_TYPE (op0), op1);
2561 }
2562 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2563 {
2564 if (! flag_traditional)
2565 pedwarn ("comparison between pointer and integer");
2566 op0 = convert (TREE_TYPE (op1), op0);
2567 }
2568 else
2569 /* If args are not valid, clear out RESULT_TYPE
2570 to cause an error message later. */
2571 result_type = 0;
2572 break;
2573
2574 case MAX_EXPR:
2575 case MIN_EXPR:
2576 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2577 || code0 == COMPLEX_TYPE)
2578 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2579 || code1 == COMPLEX_TYPE))
2580 shorten = 1;
2581 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2582 {
2583 if (! comp_target_types (type0, type1))
2584 pedwarn ("comparison of distinct pointer types lacks a cast");
2585 else if (pedantic
2586 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2587 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2588 result_type = common_type (type0, type1);
2589 }
2590 break;
2591
2592 case LE_EXPR:
2593 case GE_EXPR:
2594 case LT_EXPR:
2595 case GT_EXPR:
2596 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2597 || code0 == COMPLEX_TYPE)
2598 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2599 || code1 == COMPLEX_TYPE))
2600 short_compare = 1;
2601 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2602 {
2603 if (! comp_target_types (type0, type1))
2604 pedwarn ("comparison of distinct pointer types lacks a cast");
2605 else if (pedantic
2606 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2607 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2608 result_type = integer_type_node;
2609 }
2610 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2611 && integer_zerop (op1))
2612 {
2613 result_type = integer_type_node;
2614 op1 = null_pointer_node;
2615 if (! flag_traditional)
2616 pedwarn ("ordered comparison of pointer with integer zero");
2617 }
2618 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2619 && integer_zerop (op0))
2620 {
2621 result_type = integer_type_node;
2622 op0 = null_pointer_node;
2623 if (pedantic)
2624 pedwarn ("ordered comparison of pointer with integer zero");
2625 }
2626 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2627 {
2628 result_type = integer_type_node;
2629 if (! flag_traditional)
2630 pedwarn ("comparison between pointer and integer");
2631 op1 = convert (TREE_TYPE (op0), op1);
2632 }
2633 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2634 {
2635 result_type = integer_type_node;
2636 if (! flag_traditional)
2637 pedwarn ("comparison between pointer and integer");
2638 op0 = convert (TREE_TYPE (op1), op0);
2639 }
2640 converted = 1;
2641 break;
2642 }
2643
2644 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2645 &&
2646 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2647 {
2648 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2649
2650 if (shorten || common || short_compare)
2651 result_type = common_type (type0, type1);
2652
2653 /* For certain operations (which identify themselves by shorten != 0)
2654 if both args were extended from the same smaller type,
2655 do the arithmetic in that type and then extend.
2656
2657 shorten !=0 and !=1 indicates a bitwise operation.
2658 For them, this optimization is safe only if
2659 both args are zero-extended or both are sign-extended.
2660 Otherwise, we might change the result.
2661 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2662 but calculated in (unsigned short) it would be (unsigned short)-1. */
2663
2664 if (shorten && none_complex)
2665 {
2666 int unsigned0, unsigned1;
2667 tree arg0 = get_narrower (op0, &unsigned0);
2668 tree arg1 = get_narrower (op1, &unsigned1);
2669 /* UNS is 1 if the operation to be done is an unsigned one. */
2670 int uns = TREE_UNSIGNED (result_type);
2671 tree type;
2672
2673 final_type = result_type;
2674
2675 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2676 but it *requires* conversion to FINAL_TYPE. */
2677
2678 if ((TYPE_PRECISION (TREE_TYPE (op0))
2679 == TYPE_PRECISION (TREE_TYPE (arg0)))
2680 && TREE_TYPE (op0) != final_type)
2681 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2682 if ((TYPE_PRECISION (TREE_TYPE (op1))
2683 == TYPE_PRECISION (TREE_TYPE (arg1)))
2684 && TREE_TYPE (op1) != final_type)
2685 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2686
2687 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2688
2689 /* For bitwise operations, signedness of nominal type
2690 does not matter. Consider only how operands were extended. */
2691 if (shorten == -1)
2692 uns = unsigned0;
2693
2694 /* Note that in all three cases below we refrain from optimizing
2695 an unsigned operation on sign-extended args.
2696 That would not be valid. */
2697
2698 /* Both args variable: if both extended in same way
2699 from same width, do it in that width.
2700 Do it unsigned if args were zero-extended. */
2701 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2702 < TYPE_PRECISION (result_type))
2703 && (TYPE_PRECISION (TREE_TYPE (arg1))
2704 == TYPE_PRECISION (TREE_TYPE (arg0)))
2705 && unsigned0 == unsigned1
2706 && (unsigned0 || !uns))
2707 result_type
2708 = signed_or_unsigned_type (unsigned0,
2709 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2710 else if (TREE_CODE (arg0) == INTEGER_CST
2711 && (unsigned1 || !uns)
2712 && (TYPE_PRECISION (TREE_TYPE (arg1))
2713 < TYPE_PRECISION (result_type))
2714 && (type = signed_or_unsigned_type (unsigned1,
2715 TREE_TYPE (arg1)),
2716 int_fits_type_p (arg0, type)))
2717 result_type = type;
2718 else if (TREE_CODE (arg1) == INTEGER_CST
2719 && (unsigned0 || !uns)
2720 && (TYPE_PRECISION (TREE_TYPE (arg0))
2721 < TYPE_PRECISION (result_type))
2722 && (type = signed_or_unsigned_type (unsigned0,
2723 TREE_TYPE (arg0)),
2724 int_fits_type_p (arg1, type)))
2725 result_type = type;
2726 }
2727
2728 /* Shifts can be shortened if shifting right. */
2729
2730 if (short_shift)
2731 {
2732 int unsigned_arg;
2733 tree arg0 = get_narrower (op0, &unsigned_arg);
2734
2735 final_type = result_type;
2736
2737 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2738 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2739
2740 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2741 /* If arg is sign-extended and then unsigned-shifted,
2742 we can simulate this with a signed shift in arg's type
2743 only if the extended result is at least twice as wide
2744 as the arg. Otherwise, the shift could use up all the
2745 ones made by sign-extension and bring in zeros.
2746 We can't optimize that case at all, but in most machines
2747 it never happens because available widths are 2**N. */
2748 && (!TREE_UNSIGNED (final_type)
2749 || unsigned_arg
2750 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2751 {
2752 /* Do an unsigned shift if the operand was zero-extended. */
2753 result_type
2754 = signed_or_unsigned_type (unsigned_arg,
2755 TREE_TYPE (arg0));
2756 /* Convert value-to-be-shifted to that type. */
2757 if (TREE_TYPE (op0) != result_type)
2758 op0 = convert (result_type, op0);
2759 converted = 1;
2760 }
2761 }
2762
2763 /* Comparison operations are shortened too but differently.
2764 They identify themselves by setting short_compare = 1. */
2765
2766 if (short_compare && none_complex)
2767 {
2768 /* Don't write &op0, etc., because that would prevent op0
2769 from being kept in a register.
2770 Instead, make copies of the our local variables and
2771 pass the copies by reference, then copy them back afterward. */
2772 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2773 enum tree_code xresultcode = resultcode;
2774 tree val
2775 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2776 if (val != 0)
2777 return val;
2778 op0 = xop0, op1 = xop1, result_type = xresult_type;
2779 resultcode = xresultcode;
2780
2781 if (extra_warnings)
2782 {
2783 tree op0_type = TREE_TYPE (orig_op0);
2784 tree op1_type = TREE_TYPE (orig_op1);
2785 int op0_unsigned = TREE_UNSIGNED (op0_type);
2786 int op1_unsigned = TREE_UNSIGNED (op1_type);
2787
2788 /* Give warnings for comparisons between signed and unsigned
2789 quantities that will fail. Do not warn if the signed quantity
2790 is an unsuffixed integer literal (or some static constant
2791 expression involving such literals) and it is positive.
2792 Do not warn if the width of the unsigned quantity is less
2793 than that of the signed quantity, since in this case all
2794 values of the unsigned quantity fit in the signed quantity.
2795 Do not warn if the signed type is the same size as the
2796 result_type since sign extension does not cause trouble in
2797 this case. */
2798 /* Do the checking based on the original operand trees, so that
2799 casts will be considered, but default promotions won't be. */
2800 if (op0_unsigned != op1_unsigned
2801 && ((op0_unsigned
2802 && TYPE_PRECISION (op0_type) >= TYPE_PRECISION (op1_type)
2803 && TYPE_PRECISION (op0_type) < TYPE_PRECISION (result_type)
2804 && (TREE_CODE (op1) != INTEGER_CST
2805 || (TREE_CODE (op1) == INTEGER_CST
2806 && INT_CST_LT (op1, integer_zero_node))))
2807 ||
2808 (op1_unsigned
2809 && TYPE_PRECISION (op1_type) >= TYPE_PRECISION (op0_type)
2810 && TYPE_PRECISION (op1_type) < TYPE_PRECISION (result_type)
2811 && (TREE_CODE (op0) != INTEGER_CST
2812 || (TREE_CODE (op0) == INTEGER_CST
2813 && INT_CST_LT (op0, integer_zero_node))))))
2814 warning ("comparison between signed and unsigned");
2815 }
2816 }
2817 }
2818
2819 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2820 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2821 Then the expression will be built.
2822 It will be given type FINAL_TYPE if that is nonzero;
2823 otherwise, it will be given type RESULT_TYPE. */
2824
2825 if (!result_type)
2826 {
2827 binary_op_error (code);
2828 return error_mark_node;
2829 }
2830
2831 if (! converted)
2832 {
2833 if (TREE_TYPE (op0) != result_type)
2834 op0 = convert (result_type, op0);
2835 if (TREE_TYPE (op1) != result_type)
2836 op1 = convert (result_type, op1);
2837 }
2838
2839 {
2840 register tree result = build (resultcode, result_type, op0, op1);
2841 register tree folded;
2842
2843 folded = fold (result);
2844 if (folded == result)
2845 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2846 if (final_type != 0)
2847 return convert (final_type, folded);
2848 return folded;
2849 }
2850 }
2851
2852 /* Return a tree for the sum or difference (RESULTCODE says which)
2853 of pointer PTROP and integer INTOP. */
2854
2855 static tree
pointer_int_sum(resultcode,ptrop,intop)2856 pointer_int_sum (resultcode, ptrop, intop)
2857 enum tree_code resultcode;
2858 register tree ptrop, intop;
2859 {
2860 tree size_exp;
2861
2862 register tree result;
2863 register tree folded;
2864
2865 /* The result is a pointer of the same type that is being added. */
2866
2867 register tree result_type = TREE_TYPE (ptrop);
2868
2869 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2870 {
2871 if (pedantic || warn_pointer_arith)
2872 pedwarn ("pointer of type `void *' used in arithmetic");
2873 size_exp = integer_one_node;
2874 }
2875 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2876 {
2877 if (pedantic || warn_pointer_arith)
2878 pedwarn ("pointer to a function used in arithmetic");
2879 size_exp = integer_one_node;
2880 }
2881 else
2882 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2883
2884 /* If what we are about to multiply by the size of the elements
2885 contains a constant term, apply distributive law
2886 and multiply that constant term separately.
2887 This helps produce common subexpressions. */
2888
2889 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2890 && ! TREE_CONSTANT (intop)
2891 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2892 && TREE_CONSTANT (size_exp)
2893 /* If the constant comes from pointer subtraction,
2894 skip this optimization--it would cause an error. */
2895 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE)
2896 {
2897 enum tree_code subcode = resultcode;
2898 tree int_type = TREE_TYPE (intop);
2899 if (TREE_CODE (intop) == MINUS_EXPR)
2900 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2901 /* Convert both subexpression types to the type of intop,
2902 because weird cases involving pointer arithmetic
2903 can result in a sum or difference with different type args. */
2904 ptrop = build_binary_op (subcode, ptrop,
2905 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2906 intop = convert (int_type, TREE_OPERAND (intop, 0));
2907 }
2908
2909 /* Convert the integer argument to a type the same size as a pointer
2910 so the multiply won't overflow spuriously. */
2911
2912 if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE)
2913 intop = convert (type_for_size (POINTER_SIZE, 0), intop);
2914
2915 /* Replace the integer argument
2916 with a suitable product by the object size. */
2917
2918 intop = build_binary_op (MULT_EXPR, intop, size_exp, 1);
2919
2920 /* Create the sum or difference. */
2921
2922 result = build (resultcode, result_type, ptrop, intop);
2923
2924 folded = fold (result);
2925 if (folded == result)
2926 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2927 return folded;
2928 }
2929
2930 /* Return a tree for the difference of pointers OP0 and OP1.
2931 The resulting tree has type int. */
2932
2933 static tree
pointer_diff(op0,op1)2934 pointer_diff (op0, op1)
2935 register tree op0, op1;
2936 {
2937 register tree result, folded;
2938 tree restype = ptrdiff_type_node;
2939
2940 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2941
2942 if (pedantic || warn_pointer_arith)
2943 {
2944 if (TREE_CODE (target_type) == VOID_TYPE)
2945 pedwarn ("pointer of type `void *' used in subtraction");
2946 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2947 pedwarn ("pointer to a function used in subtraction");
2948 }
2949
2950 /* First do the subtraction as integers;
2951 then drop through to build the divide operator. */
2952
2953 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2954 convert (restype, op1), 1);
2955 /* This generates an error if op1 is pointer to incomplete type. */
2956 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2957 error ("arithmetic on pointer to an incomplete type");
2958 /* This generates an error if op0 is pointer to incomplete type. */
2959 op1 = c_size_in_bytes (target_type);
2960
2961 /* Divide by the size, in easiest possible way. */
2962
2963 result = build (EXACT_DIV_EXPR, restype, op0, op1);
2964
2965 folded = fold (result);
2966 if (folded == result)
2967 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2968 return folded;
2969 }
2970
2971 /* Construct and perhaps optimize a tree representation
2972 for a unary operation. CODE, a tree_code, specifies the operation
2973 and XARG is the operand. NOCONVERT nonzero suppresses
2974 the default promotions (such as from short to int). */
2975
2976 tree
build_unary_op(code,xarg,noconvert)2977 build_unary_op (code, xarg, noconvert)
2978 enum tree_code code;
2979 tree xarg;
2980 int noconvert;
2981 {
2982 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2983 register tree arg = xarg;
2984 register tree argtype = 0;
2985 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2986 char *errstring = NULL;
2987 tree val;
2988
2989 if (typecode == ERROR_MARK)
2990 return error_mark_node;
2991 if (typecode == ENUMERAL_TYPE)
2992 typecode = INTEGER_TYPE;
2993
2994 switch (code)
2995 {
2996 case CONVERT_EXPR:
2997 /* This is used for unary plus, because a CONVERT_EXPR
2998 is enough to prevent anybody from looking inside for
2999 associativity, but won't generate any code. */
3000 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3001 || typecode == COMPLEX_TYPE))
3002 errstring = "wrong type argument to unary plus";
3003 else if (!noconvert)
3004 arg = default_conversion (arg);
3005 break;
3006
3007 case NEGATE_EXPR:
3008 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3009 || typecode == COMPLEX_TYPE))
3010 errstring = "wrong type argument to unary minus";
3011 else if (!noconvert)
3012 arg = default_conversion (arg);
3013 break;
3014
3015 case BIT_NOT_EXPR:
3016 if (typecode != INTEGER_TYPE)
3017 errstring = "wrong type argument to bit-complement";
3018 else if (!noconvert)
3019 arg = default_conversion (arg);
3020 break;
3021
3022 case ABS_EXPR:
3023 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3024 || typecode == COMPLEX_TYPE))
3025 errstring = "wrong type argument to abs";
3026 else if (!noconvert)
3027 arg = default_conversion (arg);
3028 break;
3029
3030 case TRUTH_NOT_EXPR:
3031 if (typecode != INTEGER_TYPE
3032 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3033 && typecode != COMPLEX_TYPE
3034 /* These will convert to a pointer. */
3035 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
3036 {
3037 errstring = "wrong type argument to unary exclamation mark";
3038 break;
3039 }
3040 arg = truthvalue_conversion (arg);
3041 return invert_truthvalue (arg);
3042
3043 case NOP_EXPR:
3044 break;
3045
3046 case REALPART_EXPR:
3047 if (TREE_CODE (arg) == COMPLEX_CST)
3048 return TREE_REALPART (arg);
3049 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3050 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
3051 else
3052 return arg;
3053
3054 case IMAGPART_EXPR:
3055 if (TREE_CODE (arg) == COMPLEX_CST)
3056 return TREE_IMAGPART (arg);
3057 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3058 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
3059 else
3060 return convert (TREE_TYPE (arg), integer_zero_node);
3061
3062 case PREINCREMENT_EXPR:
3063 case POSTINCREMENT_EXPR:
3064 case PREDECREMENT_EXPR:
3065 case POSTDECREMENT_EXPR:
3066 /* Handle complex lvalues (when permitted)
3067 by reduction to simpler cases. */
3068
3069 val = unary_complex_lvalue (code, arg);
3070 if (val != 0)
3071 return val;
3072
3073 /* Increment or decrement the real part of the value,
3074 and don't change the imaginary part. */
3075 if (typecode == COMPLEX_TYPE)
3076 {
3077 tree real, imag;
3078
3079 arg = stabilize_reference (arg);
3080 real = build_unary_op (REALPART_EXPR, arg, 1);
3081 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
3082 return build (COMPLEX_EXPR, TREE_TYPE (arg),
3083 build_unary_op (code, real, 1), imag);
3084 }
3085
3086 /* Report invalid types. */
3087
3088 if (typecode != POINTER_TYPE
3089 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3090 {
3091 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3092 errstring ="wrong type argument to increment";
3093 else
3094 errstring ="wrong type argument to decrement";
3095 break;
3096 }
3097
3098 {
3099 register tree inc;
3100 tree result_type = TREE_TYPE (arg);
3101
3102 arg = get_unwidened (arg, 0);
3103 argtype = TREE_TYPE (arg);
3104
3105 /* Compute the increment. */
3106
3107 if (typecode == POINTER_TYPE)
3108 {
3109 /* If pointer target is an undefined struct,
3110 we just cannot know how to do the arithmetic. */
3111 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
3112 error ("%s of pointer to unknown structure",
3113 ((code == PREINCREMENT_EXPR
3114 || code == POSTINCREMENT_EXPR)
3115 ? "increment" : "decrement"));
3116 else if ((pedantic || warn_pointer_arith)
3117 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
3118 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
3119 pedwarn ("wrong type argument to %s",
3120 ((code == PREINCREMENT_EXPR
3121 || code == POSTINCREMENT_EXPR)
3122 ? "increment" : "decrement"));
3123 inc = c_sizeof_nowarn (TREE_TYPE (result_type));
3124 }
3125 else
3126 inc = integer_one_node;
3127
3128 inc = convert (argtype, inc);
3129
3130 /* Handle incrementing a cast-expression. */
3131
3132 while (1)
3133 switch (TREE_CODE (arg))
3134 {
3135 case NOP_EXPR:
3136 case CONVERT_EXPR:
3137 case FLOAT_EXPR:
3138 case FIX_TRUNC_EXPR:
3139 case FIX_FLOOR_EXPR:
3140 case FIX_ROUND_EXPR:
3141 case FIX_CEIL_EXPR:
3142 pedantic_lvalue_warning (CONVERT_EXPR);
3143 /* If the real type has the same machine representation
3144 as the type it is cast to, we can make better output
3145 by adding directly to the inside of the cast. */
3146 if ((TREE_CODE (TREE_TYPE (arg))
3147 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
3148 && (TYPE_MODE (TREE_TYPE (arg))
3149 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
3150 arg = TREE_OPERAND (arg, 0);
3151 else
3152 {
3153 tree incremented, modify, value;
3154 arg = stabilize_reference (arg);
3155 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3156 value = arg;
3157 else
3158 value = save_expr (arg);
3159 incremented = build (((code == PREINCREMENT_EXPR
3160 || code == POSTINCREMENT_EXPR)
3161 ? PLUS_EXPR : MINUS_EXPR),
3162 argtype, value, inc);
3163 TREE_SIDE_EFFECTS (incremented) = 1;
3164 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3165 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3166 TREE_USED (value) = 1;
3167 return value;
3168 }
3169 break;
3170
3171 default:
3172 goto give_up;
3173 }
3174 give_up:
3175
3176 /* Complain about anything else that is not a true lvalue. */
3177 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3178 || code == POSTINCREMENT_EXPR)
3179 ? "increment" : "decrement")))
3180 return error_mark_node;
3181
3182 /* Report a read-only lvalue. */
3183 if (TREE_READONLY (arg))
3184 readonly_warning (arg,
3185 ((code == PREINCREMENT_EXPR
3186 || code == POSTINCREMENT_EXPR)
3187 ? "increment" : "decrement"));
3188
3189 val = build (code, TREE_TYPE (arg), arg, inc);
3190 TREE_SIDE_EFFECTS (val) = 1;
3191 val = convert (result_type, val);
3192 if (TREE_CODE (val) != code)
3193 TREE_NO_UNUSED_WARNING (val) = 1;
3194 return val;
3195 }
3196
3197 case ADDR_EXPR:
3198 /* Note that this operation never does default_conversion
3199 regardless of NOCONVERT. */
3200
3201 /* Let &* cancel out to simplify resulting code. */
3202 if (TREE_CODE (arg) == INDIRECT_REF)
3203 {
3204 /* Don't let this be an lvalue. */
3205 if (lvalue_p (TREE_OPERAND (arg, 0)))
3206 return non_lvalue (TREE_OPERAND (arg, 0));
3207 return TREE_OPERAND (arg, 0);
3208 }
3209
3210 /* For &x[y], return x+y */
3211 if (TREE_CODE (arg) == ARRAY_REF)
3212 {
3213 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3214 return error_mark_node;
3215 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3216 TREE_OPERAND (arg, 1), 1);
3217 }
3218
3219 /* Handle complex lvalues (when permitted)
3220 by reduction to simpler cases. */
3221 val = unary_complex_lvalue (code, arg);
3222 if (val != 0)
3223 return val;
3224
3225 #if 0 /* Turned off because inconsistent;
3226 float f; *&(int)f = 3.4 stores in int format
3227 whereas (int)f = 3.4 stores in float format. */
3228 /* Address of a cast is just a cast of the address
3229 of the operand of the cast. */
3230 switch (TREE_CODE (arg))
3231 {
3232 case NOP_EXPR:
3233 case CONVERT_EXPR:
3234 case FLOAT_EXPR:
3235 case FIX_TRUNC_EXPR:
3236 case FIX_FLOOR_EXPR:
3237 case FIX_ROUND_EXPR:
3238 case FIX_CEIL_EXPR:
3239 if (pedantic)
3240 pedwarn ("ANSI C forbids the address of a cast expression");
3241 return convert (build_pointer_type (TREE_TYPE (arg)),
3242 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3243 0));
3244 }
3245 #endif
3246
3247 /* Allow the address of a constructor if all the elements
3248 are constant. */
3249 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3250 ;
3251 /* Anything not already handled and not a true memory reference
3252 is an error. */
3253 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
3254 return error_mark_node;
3255
3256 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3257 argtype = TREE_TYPE (arg);
3258 /* If the lvalue is const or volatile,
3259 merge that into the type that the address will point to. */
3260 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
3261 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3262 {
3263 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3264 argtype = c_build_type_variant (argtype,
3265 TREE_READONLY (arg),
3266 TREE_THIS_VOLATILE (arg));
3267 }
3268
3269 argtype = build_pointer_type (argtype);
3270
3271 if (mark_addressable (arg) == 0)
3272 return error_mark_node;
3273
3274 {
3275 tree addr;
3276
3277 if (TREE_CODE (arg) == COMPONENT_REF)
3278 {
3279 tree field = TREE_OPERAND (arg, 1);
3280
3281 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3282
3283 if (DECL_BIT_FIELD (field))
3284 {
3285 error ("attempt to take address of bit-field structure member `%s'",
3286 IDENTIFIER_POINTER (DECL_NAME (field)));
3287 return error_mark_node;
3288 }
3289
3290 addr = convert (argtype, addr);
3291
3292 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3293 {
3294 tree offset
3295 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3296 size_int (BITS_PER_UNIT));
3297 int flag = TREE_CONSTANT (addr);
3298 addr = fold (build (PLUS_EXPR, argtype,
3299 addr, convert (argtype, offset)));
3300 TREE_CONSTANT (addr) = flag;
3301 }
3302 }
3303 else
3304 addr = build1 (code, argtype, arg);
3305
3306 /* Address of a static or external variable or
3307 file-scope function counts as a constant. */
3308 if (staticp (arg)
3309 && ! (TREE_CODE (arg) == FUNCTION_DECL
3310 && DECL_CONTEXT (arg) != 0))
3311 TREE_CONSTANT (addr) = 1;
3312 return addr;
3313 }
3314 }
3315
3316 if (!errstring)
3317 {
3318 if (argtype == 0)
3319 argtype = TREE_TYPE (arg);
3320 return fold (build1 (code, argtype, arg));
3321 }
3322
3323 error (errstring);
3324 return error_mark_node;
3325 }
3326
3327 #if 0
3328 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3329 convert ARG with the same conversions in the same order
3330 and return the result. */
3331
3332 static tree
3333 convert_sequence (conversions, arg)
3334 tree conversions;
3335 tree arg;
3336 {
3337 switch (TREE_CODE (conversions))
3338 {
3339 case NOP_EXPR:
3340 case CONVERT_EXPR:
3341 case FLOAT_EXPR:
3342 case FIX_TRUNC_EXPR:
3343 case FIX_FLOOR_EXPR:
3344 case FIX_ROUND_EXPR:
3345 case FIX_CEIL_EXPR:
3346 return convert (TREE_TYPE (conversions),
3347 convert_sequence (TREE_OPERAND (conversions, 0),
3348 arg));
3349
3350 default:
3351 return arg;
3352 }
3353 }
3354 #endif /* 0 */
3355
3356 /* Return nonzero if REF is an lvalue valid for this language.
3357 Lvalues can be assigned, unless their type has TYPE_READONLY.
3358 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3359
3360 int
lvalue_p(ref)3361 lvalue_p (ref)
3362 tree ref;
3363 {
3364 register enum tree_code code = TREE_CODE (ref);
3365
3366 switch (code)
3367 {
3368 case REALPART_EXPR:
3369 case IMAGPART_EXPR:
3370 case COMPONENT_REF:
3371 return lvalue_p (TREE_OPERAND (ref, 0));
3372
3373 case STRING_CST:
3374 return 1;
3375
3376 case INDIRECT_REF:
3377 case ARRAY_REF:
3378 case VAR_DECL:
3379 case PARM_DECL:
3380 case RESULT_DECL:
3381 case ERROR_MARK:
3382 if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3383 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
3384 return 1;
3385 break;
3386 }
3387 return 0;
3388 }
3389
3390 /* Return nonzero if REF is an lvalue valid for this language;
3391 otherwise, print an error message and return zero. */
3392
3393 int
lvalue_or_else(ref,string)3394 lvalue_or_else (ref, string)
3395 tree ref;
3396 char *string;
3397 {
3398 int win = lvalue_p (ref);
3399 if (! win)
3400 error ("invalid lvalue in %s", string);
3401 return win;
3402 }
3403
3404 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3405 for certain kinds of expressions which are not really lvalues
3406 but which we can accept as lvalues.
3407
3408 If ARG is not a kind of expression we can handle, return zero. */
3409
3410 static tree
unary_complex_lvalue(code,arg)3411 unary_complex_lvalue (code, arg)
3412 enum tree_code code;
3413 tree arg;
3414 {
3415 /* Handle (a, b) used as an "lvalue". */
3416 if (TREE_CODE (arg) == COMPOUND_EXPR)
3417 {
3418 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3419 pedantic_lvalue_warning (COMPOUND_EXPR);
3420 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3421 TREE_OPERAND (arg, 0), real_result);
3422 }
3423
3424 /* Handle (a ? b : c) used as an "lvalue". */
3425 if (TREE_CODE (arg) == COND_EXPR)
3426 {
3427 pedantic_lvalue_warning (COND_EXPR);
3428 return (build_conditional_expr
3429 (TREE_OPERAND (arg, 0),
3430 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3431 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3432 }
3433
3434 return 0;
3435 }
3436
3437 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3438 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3439
3440 static void
pedantic_lvalue_warning(code)3441 pedantic_lvalue_warning (code)
3442 enum tree_code code;
3443 {
3444 if (pedantic)
3445 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3446 code == COND_EXPR ? "conditional"
3447 : code == COMPOUND_EXPR ? "compound" : "cast");
3448 }
3449
3450 /* Warn about storing in something that is `const'. */
3451
3452 void
readonly_warning(arg,string)3453 readonly_warning (arg, string)
3454 tree arg;
3455 char *string;
3456 {
3457 char buf[80];
3458 strcpy (buf, string);
3459
3460 /* Forbid assignments to iterators. */
3461 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3462 {
3463 strcat (buf, " of iterator `%s'");
3464 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3465 }
3466
3467 if (TREE_CODE (arg) == COMPONENT_REF)
3468 {
3469 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3470 readonly_warning (TREE_OPERAND (arg, 0), string);
3471 else
3472 {
3473 strcat (buf, " of read-only member `%s'");
3474 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3475 }
3476 }
3477 else if (TREE_CODE (arg) == VAR_DECL)
3478 {
3479 strcat (buf, " of read-only variable `%s'");
3480 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3481 }
3482 else
3483 {
3484 pedwarn ("%s of read-only location", buf);
3485 }
3486 }
3487
3488 /* Mark EXP saying that we need to be able to take the
3489 address of it; it should not be allocated in a register.
3490 Value is 1 if successful. */
3491
3492 int
mark_addressable(exp)3493 mark_addressable (exp)
3494 tree exp;
3495 {
3496 register tree x = exp;
3497 while (1)
3498 switch (TREE_CODE (x))
3499 {
3500 case ADDR_EXPR:
3501 case COMPONENT_REF:
3502 case ARRAY_REF:
3503 x = TREE_OPERAND (x, 0);
3504 break;
3505
3506 case CONSTRUCTOR:
3507 TREE_ADDRESSABLE (x) = 1;
3508 return 1;
3509
3510 case VAR_DECL:
3511 case CONST_DECL:
3512 case PARM_DECL:
3513 case RESULT_DECL:
3514 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3515 && DECL_NONLOCAL (x))
3516 {
3517 if (TREE_PUBLIC (x))
3518 {
3519 error ("global register variable `%s' used in nested function",
3520 IDENTIFIER_POINTER (DECL_NAME (x)));
3521 return 0;
3522 }
3523 pedwarn ("register variable `%s' used in nested function",
3524 IDENTIFIER_POINTER (DECL_NAME (x)));
3525 }
3526 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3527 {
3528 if (TREE_PUBLIC (x))
3529 {
3530 error ("address of global register variable `%s' requested",
3531 IDENTIFIER_POINTER (DECL_NAME (x)));
3532 return 0;
3533 }
3534 pedwarn ("address of register variable `%s' requested",
3535 IDENTIFIER_POINTER (DECL_NAME (x)));
3536 }
3537 put_var_into_stack (x);
3538
3539 /* drops in */
3540 case FUNCTION_DECL:
3541 TREE_ADDRESSABLE (x) = 1;
3542 #if 0 /* poplevel deals with this now. */
3543 if (DECL_CONTEXT (x) == 0)
3544 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3545 #endif
3546
3547 default:
3548 return 1;
3549 }
3550 }
3551
3552 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3553
3554 tree
build_conditional_expr(ifexp,op1,op2)3555 build_conditional_expr (ifexp, op1, op2)
3556 tree ifexp, op1, op2;
3557 {
3558 register tree type1;
3559 register tree type2;
3560 register enum tree_code code1;
3561 register enum tree_code code2;
3562 register tree result_type = NULL;
3563
3564 /* If second operand is omitted, it is the same as the first one;
3565 make sure it is calculated only once. */
3566 if (op1 == 0)
3567 {
3568 if (pedantic)
3569 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3570 ifexp = op1 = save_expr (ifexp);
3571 }
3572
3573 ifexp = truthvalue_conversion (default_conversion (ifexp));
3574
3575 #if 0 /* Produces wrong result if within sizeof. */
3576 /* Don't promote the operands separately if they promote
3577 the same way. Return the unpromoted type and let the combined
3578 value get promoted if necessary. */
3579
3580 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3581 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3582 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3583 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3584 {
3585 if (TREE_CODE (ifexp) == INTEGER_CST)
3586 return (integer_zerop (ifexp) ? op2 : op1);
3587
3588 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3589 }
3590 #endif
3591
3592 /* Promote both alternatives. */
3593
3594 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3595 op1 = default_conversion (op1);
3596 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3597 op2 = default_conversion (op2);
3598
3599 if (TREE_CODE (ifexp) == ERROR_MARK
3600 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3601 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3602 return error_mark_node;
3603
3604 type1 = TREE_TYPE (op1);
3605 code1 = TREE_CODE (type1);
3606 type2 = TREE_TYPE (op2);
3607 code2 = TREE_CODE (type2);
3608
3609 /* Quickly detect the usual case where op1 and op2 have the same type
3610 after promotion. */
3611 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3612 {
3613 if (type1 == type2)
3614 result_type = type1;
3615 else
3616 result_type = TYPE_MAIN_VARIANT (type1);
3617 }
3618 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3619 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3620 {
3621 result_type = common_type (type1, type2);
3622 }
3623 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3624 {
3625 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3626 pedwarn ("ANSI C forbids conditional expr with only one void side");
3627 result_type = void_type_node;
3628 }
3629 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3630 {
3631 if (comp_target_types (type1, type2))
3632 result_type = common_type (type1, type2);
3633 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node)
3634 result_type = qualify_type (type2, type1);
3635 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node)
3636 result_type = qualify_type (type1, type2);
3637 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3638 {
3639 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3640 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3641 result_type = qualify_type (type1, type2);
3642 }
3643 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3644 {
3645 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3646 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3647 result_type = qualify_type (type2, type1);
3648 }
3649 else
3650 {
3651 pedwarn ("pointer type mismatch in conditional expression");
3652 result_type = build_pointer_type (void_type_node);
3653 }
3654 }
3655 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3656 {
3657 if (! integer_zerop (op2))
3658 pedwarn ("pointer/integer type mismatch in conditional expression");
3659 else
3660 {
3661 op2 = null_pointer_node;
3662 #if 0 /* The spec seems to say this is permitted. */
3663 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3664 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3665 #endif
3666 }
3667 result_type = type1;
3668 }
3669 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3670 {
3671 if (!integer_zerop (op1))
3672 pedwarn ("pointer/integer type mismatch in conditional expression");
3673 else
3674 {
3675 op1 = null_pointer_node;
3676 #if 0 /* The spec seems to say this is permitted. */
3677 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3678 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3679 #endif
3680 }
3681 result_type = type2;
3682 }
3683
3684 if (!result_type)
3685 {
3686 if (flag_cond_mismatch)
3687 result_type = void_type_node;
3688 else
3689 {
3690 error ("type mismatch in conditional expression");
3691 return error_mark_node;
3692 }
3693 }
3694
3695 /* Merge const and volatile flags of the incoming types. */
3696 result_type
3697 = build_type_variant (result_type,
3698 TREE_READONLY (op1) || TREE_READONLY (op2),
3699 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3700
3701 if (result_type != TREE_TYPE (op1))
3702 op1 = convert_and_check (result_type, op1);
3703 if (result_type != TREE_TYPE (op2))
3704 op2 = convert_and_check (result_type, op2);
3705
3706 #if 0
3707 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3708 {
3709 result_type = TREE_TYPE (op1);
3710 if (TREE_CONSTANT (ifexp))
3711 return (integer_zerop (ifexp) ? op2 : op1);
3712
3713 if (TYPE_MODE (result_type) == BLKmode)
3714 {
3715 register tree tempvar
3716 = build_decl (VAR_DECL, NULL_TREE, result_type);
3717 register tree xop1 = build_modify_expr (tempvar, op1);
3718 register tree xop2 = build_modify_expr (tempvar, op2);
3719 register tree result = fold (build (COND_EXPR, result_type,
3720 ifexp, xop1, xop2));
3721
3722 layout_decl (tempvar, TYPE_ALIGN (result_type));
3723 /* No way to handle variable-sized objects here.
3724 I fear that the entire handling of BLKmode conditional exprs
3725 needs to be redone. */
3726 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3727 abort ();
3728 DECL_RTL (tempvar)
3729 = assign_stack_local (DECL_MODE (tempvar),
3730 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3731 + BITS_PER_UNIT - 1)
3732 / BITS_PER_UNIT,
3733 0);
3734
3735 TREE_SIDE_EFFECTS (result)
3736 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3737 | TREE_SIDE_EFFECTS (op2);
3738 return build (COMPOUND_EXPR, result_type, result, tempvar);
3739 }
3740 }
3741 #endif /* 0 */
3742
3743 if (TREE_CODE (ifexp) == INTEGER_CST)
3744 return integer_zerop (ifexp) ? op2 : op1;
3745
3746 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3747 }
3748
3749 /* Given a list of expressions, return a compound expression
3750 that performs them all and returns the value of the last of them. */
3751
3752 tree
build_compound_expr(list)3753 build_compound_expr (list)
3754 tree list;
3755 {
3756 return internal_build_compound_expr (list, TRUE);
3757 }
3758
3759 static tree
internal_build_compound_expr(list,first_p)3760 internal_build_compound_expr (list, first_p)
3761 tree list;
3762 int first_p;
3763 {
3764 register tree rest;
3765
3766 if (TREE_CHAIN (list) == 0)
3767 {
3768 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3769 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3770
3771 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3772 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3773 list = TREE_OPERAND (list, 0);
3774 #endif
3775
3776 /* Don't let (0, 0) be null pointer constant. */
3777 if (!first_p && integer_zerop (TREE_VALUE (list)))
3778 return non_lvalue (TREE_VALUE (list));
3779 return TREE_VALUE (list);
3780 }
3781
3782 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3783 {
3784 /* Convert arrays to pointers when there really is a comma operator. */
3785 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3786 TREE_VALUE (TREE_CHAIN (list))
3787 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3788 }
3789
3790 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3791
3792 /* When pedantic, a compound expression can be neither an lvalue
3793 nor an integer constant expression. */
3794 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
3795 return rest;
3796
3797 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3798 }
3799
3800 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3801
3802 tree
build_c_cast(type,expr)3803 build_c_cast (type, expr)
3804 register tree type;
3805 tree expr;
3806 {
3807 register tree value = expr;
3808
3809 if (type == error_mark_node || expr == error_mark_node)
3810 return error_mark_node;
3811 type = TYPE_MAIN_VARIANT (type);
3812
3813 #if 0
3814 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3815 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3816 value = TREE_OPERAND (value, 0);
3817 #endif
3818
3819 if (TREE_CODE (type) == ARRAY_TYPE)
3820 {
3821 error ("cast specifies array type");
3822 return error_mark_node;
3823 }
3824
3825 if (TREE_CODE (type) == FUNCTION_TYPE)
3826 {
3827 error ("cast specifies function type");
3828 return error_mark_node;
3829 }
3830
3831 if (type == TREE_TYPE (value))
3832 {
3833 if (pedantic)
3834 {
3835 if (TREE_CODE (type) == RECORD_TYPE
3836 || TREE_CODE (type) == UNION_TYPE)
3837 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3838 }
3839 }
3840 else if (TREE_CODE (type) == UNION_TYPE)
3841 {
3842 tree field;
3843 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3844 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3845 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3846 break;
3847
3848 if (field)
3849 {
3850 char *name;
3851 tree nvalue;
3852
3853 if (pedantic)
3854 pedwarn ("ANSI C forbids casts to union type");
3855 if (TYPE_NAME (type) != 0)
3856 {
3857 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3858 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3859 else
3860 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3861 }
3862 else
3863 name = "";
3864 return digest_init (type, build_nt (CONSTRUCTOR, NULL_TREE,
3865 build_tree_list (field, value)),
3866 NULL_PTR, 0, 0, name);
3867 }
3868 error ("cast to union type from type not present in union");
3869 return error_mark_node;
3870 }
3871 else
3872 {
3873 tree otype;
3874
3875 /* If casting to void, avoid the error that would come
3876 from default_conversion in the case of a non-lvalue array. */
3877 if (type == void_type_node)
3878 return build1 (CONVERT_EXPR, type, value);
3879
3880 /* Convert functions and arrays to pointers,
3881 but don't convert any other types. */
3882 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3883 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3884 value = default_conversion (value);
3885 otype = TREE_TYPE (value);
3886
3887 /* Optionally warn about potentially worrisome casts. */
3888
3889 if (warn_cast_qual
3890 && TREE_CODE (type) == POINTER_TYPE
3891 && TREE_CODE (otype) == POINTER_TYPE)
3892 {
3893 if (TYPE_VOLATILE (TREE_TYPE (otype))
3894 && ! TYPE_VOLATILE (TREE_TYPE (type)))
3895 pedwarn ("cast discards `volatile' from pointer target type");
3896 if (TYPE_READONLY (TREE_TYPE (otype))
3897 && ! TYPE_READONLY (TREE_TYPE (type)))
3898 pedwarn ("cast discards `const' from pointer target type");
3899 }
3900
3901 /* Warn about possible alignment problems. */
3902 if (STRICT_ALIGNMENT && warn_cast_align
3903 && TREE_CODE (type) == POINTER_TYPE
3904 && TREE_CODE (otype) == POINTER_TYPE
3905 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3906 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3907 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3908 warning ("cast increases required alignment of target type");
3909
3910 if (TREE_CODE (type) == INTEGER_TYPE
3911 && TREE_CODE (otype) == POINTER_TYPE
3912 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3913 && !TREE_CONSTANT (value))
3914 warning ("cast from pointer to integer of different size");
3915
3916 if (TREE_CODE (type) == POINTER_TYPE
3917 && TREE_CODE (otype) == INTEGER_TYPE
3918 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3919 #if 0
3920 /* Don't warn about converting 0 to pointer,
3921 provided the 0 was explicit--not cast or made by folding. */
3922 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3923 #endif
3924 /* Don't warn about converting any constant. */
3925 && !TREE_CONSTANT (value))
3926 warning ("cast to pointer from integer of different size");
3927
3928 value = convert (type, value);
3929
3930 /* Ignore any integer overflow caused by the cast. */
3931 if (TREE_CODE (value) == INTEGER_CST)
3932 TREE_CONSTANT_OVERFLOW (value) = 0;
3933 }
3934
3935 if (value == expr && pedantic)
3936 {
3937 /* If pedantic, don't let a cast be an lvalue. */
3938 return non_lvalue (value);
3939 }
3940 return value;
3941 }
3942
3943 /* Build an assignment expression of lvalue LHS from value RHS.
3944 MODIFYCODE is the code for a binary operator that we use
3945 to combine the old value of LHS with RHS to get the new value.
3946 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3947
3948 tree
build_modify_expr(lhs,modifycode,rhs)3949 build_modify_expr (lhs, modifycode, rhs)
3950 tree lhs, rhs;
3951 enum tree_code modifycode;
3952 {
3953 register tree result;
3954 tree newrhs;
3955 tree lhstype = TREE_TYPE (lhs);
3956 tree olhstype = lhstype;
3957
3958 /* Types that aren't fully specified cannot be used in assignments. */
3959 lhs = require_complete_type (lhs);
3960
3961 /* Avoid duplicate error messages from operands that had errors. */
3962 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3963 return error_mark_node;
3964
3965 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3966 /* Do not use STRIP_NOPS here. We do not want an enumerator
3967 whose value is 0 to count as a null pointer constant. */
3968 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3969 rhs = TREE_OPERAND (rhs, 0);
3970
3971 newrhs = rhs;
3972
3973 /* Handle control structure constructs used as "lvalues". */
3974
3975 switch (TREE_CODE (lhs))
3976 {
3977 /* Handle (a, b) used as an "lvalue". */
3978 case COMPOUND_EXPR:
3979 pedantic_lvalue_warning (COMPOUND_EXPR);
3980 return build (COMPOUND_EXPR, lhstype,
3981 TREE_OPERAND (lhs, 0),
3982 build_modify_expr (TREE_OPERAND (lhs, 1),
3983 modifycode, rhs));
3984
3985 /* Handle (a ? b : c) used as an "lvalue". */
3986 case COND_EXPR:
3987 pedantic_lvalue_warning (COND_EXPR);
3988 rhs = save_expr (rhs);
3989 {
3990 /* Produce (a ? (b = rhs) : (c = rhs))
3991 except that the RHS goes through a save-expr
3992 so the code to compute it is only emitted once. */
3993 tree cond
3994 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3995 build_modify_expr (TREE_OPERAND (lhs, 1),
3996 modifycode, rhs),
3997 build_modify_expr (TREE_OPERAND (lhs, 2),
3998 modifycode, rhs));
3999 /* Make sure the code to compute the rhs comes out
4000 before the split. */
4001 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
4002 /* But cast it to void to avoid an "unused" error. */
4003 convert (void_type_node, rhs), cond);
4004 }
4005 }
4006
4007 /* If a binary op has been requested, combine the old LHS value with the RHS
4008 producing the value we should actually store into the LHS. */
4009
4010 if (modifycode != NOP_EXPR)
4011 {
4012 lhs = stabilize_reference (lhs);
4013 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
4014 }
4015
4016 /* Handle a cast used as an "lvalue".
4017 We have already performed any binary operator using the value as cast.
4018 Now convert the result to the cast type of the lhs,
4019 and then true type of the lhs and store it there;
4020 then convert result back to the cast type to be the value
4021 of the assignment. */
4022
4023 switch (TREE_CODE (lhs))
4024 {
4025 case NOP_EXPR:
4026 case CONVERT_EXPR:
4027 case FLOAT_EXPR:
4028 case FIX_TRUNC_EXPR:
4029 case FIX_FLOOR_EXPR:
4030 case FIX_ROUND_EXPR:
4031 case FIX_CEIL_EXPR:
4032 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
4033 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
4034 newrhs = default_conversion (newrhs);
4035 {
4036 tree inner_lhs = TREE_OPERAND (lhs, 0);
4037 tree result;
4038 result = build_modify_expr (inner_lhs, NOP_EXPR,
4039 convert (TREE_TYPE (inner_lhs),
4040 convert (lhstype, newrhs)));
4041 pedantic_lvalue_warning (CONVERT_EXPR);
4042 return convert (TREE_TYPE (lhs), result);
4043 }
4044 }
4045
4046 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
4047 Reject anything strange now. */
4048
4049 if (!lvalue_or_else (lhs, "assignment"))
4050 return error_mark_node;
4051
4052 /* Warn about storing in something that is `const'. */
4053
4054 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
4055 || ((TREE_CODE (lhstype) == RECORD_TYPE
4056 || TREE_CODE (lhstype) == UNION_TYPE)
4057 && C_TYPE_FIELDS_READONLY (lhstype)))
4058 readonly_warning (lhs, "assignment");
4059
4060 /* If storing into a structure or union member,
4061 it has probably been given type `int'.
4062 Compute the type that would go with
4063 the actual amount of storage the member occupies. */
4064
4065 if (TREE_CODE (lhs) == COMPONENT_REF
4066 && (TREE_CODE (lhstype) == INTEGER_TYPE
4067 || TREE_CODE (lhstype) == REAL_TYPE
4068 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4069 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4070
4071 /* If storing in a field that is in actuality a short or narrower than one,
4072 we must store in the field in its actual type. */
4073
4074 if (lhstype != TREE_TYPE (lhs))
4075 {
4076 lhs = copy_node (lhs);
4077 TREE_TYPE (lhs) = lhstype;
4078 }
4079
4080 /* Convert new value to destination type. */
4081
4082 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
4083 NULL_TREE, NULL_TREE, 0);
4084 if (TREE_CODE (newrhs) == ERROR_MARK)
4085 return error_mark_node;
4086
4087 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4088 TREE_SIDE_EFFECTS (result) = 1;
4089
4090 /* If we got the LHS in a different type for storing in,
4091 convert the result back to the nominal type of LHS
4092 so that the value we return always has the same type
4093 as the LHS argument. */
4094
4095 if (olhstype == TREE_TYPE (result))
4096 return result;
4097 return convert_for_assignment (olhstype, result, "assignment",
4098 NULL_TREE, NULL_TREE, 0);
4099 }
4100
4101 /* Convert value RHS to type TYPE as preparation for an assignment
4102 to an lvalue of type TYPE.
4103 The real work of conversion is done by `convert'.
4104 The purpose of this function is to generate error messages
4105 for assignments that are not allowed in C.
4106 ERRTYPE is a string to use in error messages:
4107 "assignment", "return", etc. If it is null, this is parameter passing
4108 for a function call (and different error messages are output). Otherwise,
4109 it may be a name stored in the spelling stack and interpreted by
4110 get_spelling.
4111
4112 FUNNAME is the name of the function being called,
4113 as an IDENTIFIER_NODE, or null.
4114 PARMNUM is the number of the argument, for printing in error messages. */
4115
4116 static tree
convert_for_assignment(type,rhs,errtype,fundecl,funname,parmnum)4117 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4118 tree type, rhs;
4119 char *errtype;
4120 tree fundecl, funname;
4121 int parmnum;
4122 {
4123 register enum tree_code codel = TREE_CODE (type);
4124 register tree rhstype;
4125 register enum tree_code coder;
4126
4127 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4128 /* Do not use STRIP_NOPS here. We do not want an enumerator
4129 whose value is 0 to count as a null pointer constant. */
4130 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4131 rhs = TREE_OPERAND (rhs, 0);
4132
4133 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4134 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4135 rhs = default_conversion (rhs);
4136
4137 rhstype = TREE_TYPE (rhs);
4138 coder = TREE_CODE (rhstype);
4139
4140 if (coder == ERROR_MARK)
4141 return error_mark_node;
4142
4143 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4144 {
4145 overflow_warning (rhs);
4146 /* Check for Objective-C protocols. This will issue a warning if
4147 there are protocol violations. No need to use the return value. */
4148 maybe_objc_comptypes (type, rhstype, 0);
4149 return rhs;
4150 }
4151
4152 if (coder == VOID_TYPE)
4153 {
4154 error ("void value not ignored as it ought to be");
4155 return error_mark_node;
4156 }
4157 /* Arithmetic types all interconvert, and enum is treated like int. */
4158 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
4159 || codel == COMPLEX_TYPE)
4160 &&
4161 (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
4162 || codel == COMPLEX_TYPE))
4163 /* Don't use convert_and_check here. If the input has type int
4164 and did not overflow, and we are converting it here to a short,
4165 we don't want an error. A warning would be okay, but it's too risky now
4166 to add an option to convert_and_check to get just warnings. */
4167 return convert (type, rhs);
4168 /* Conversion to a union from its member types. */
4169 else if (codel == UNION_TYPE)
4170 {
4171 tree memb_types;
4172 for (memb_types = TYPE_FIELDS (type); memb_types;
4173 memb_types = TREE_CHAIN (memb_types))
4174 {
4175 if (comptypes (TREE_TYPE (memb_types), TREE_TYPE (rhs)))
4176 {
4177 if (pedantic
4178 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
4179 pedwarn ("ANSI C prohibits argument conversion to union type");
4180 return build1 (NOP_EXPR, type, rhs);
4181 }
4182 else if (coder == POINTER_TYPE
4183 && TREE_CODE (TREE_TYPE (memb_types)) == POINTER_TYPE)
4184 {
4185 tree memb_type = TREE_TYPE (memb_types);
4186 register tree ttl = TREE_TYPE (memb_type);
4187 register tree ttr = TREE_TYPE (rhstype);
4188
4189 /* Any non-function converts to a [const][volatile] void *
4190 and vice versa; otherwise, targets must be the same.
4191 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4192 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4193 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4194 || comp_target_types (memb_type, rhstype))
4195 {
4196 /* Const and volatile mean something different for function types,
4197 so the usual warnings are not appropriate. */
4198 if (TREE_CODE (ttr) != FUNCTION_TYPE
4199 || TREE_CODE (ttl) != FUNCTION_TYPE)
4200 {
4201 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4202 warn_for_assignment ("%s discards `const' from pointer target type",
4203 get_spelling (errtype), funname, parmnum);
4204 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4205 warn_for_assignment ("%s discards `volatile' from pointer target type",
4206 get_spelling (errtype), funname, parmnum);
4207 }
4208 else
4209 {
4210 /* Because const and volatile on functions are restrictions
4211 that say the function will not do certain things,
4212 it is okay to use a const or volatile function
4213 where an ordinary one is wanted, but not vice-versa. */
4214 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4215 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4216 get_spelling (errtype), funname, parmnum);
4217 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4218 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4219 get_spelling (errtype), funname, parmnum);
4220 }
4221 if (pedantic
4222 && !(fundecl != 0 && DECL_IN_SYSTEM_HEADER (fundecl)))
4223 pedwarn ("ANSI C prohibits argument conversion to union type");
4224 return build1 (NOP_EXPR, type, rhs);
4225 }
4226 }
4227 }
4228 }
4229 /* Conversions among pointers */
4230 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4231 {
4232 register tree ttl = TREE_TYPE (type);
4233 register tree ttr = TREE_TYPE (rhstype);
4234
4235 /* Any non-function converts to a [const][volatile] void *
4236 and vice versa; otherwise, targets must be the same.
4237 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4238 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4239 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4240 || comp_target_types (type, rhstype))
4241 {
4242 if (pedantic
4243 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4244 && TREE_CODE (ttr) == FUNCTION_TYPE)
4245 ||
4246 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4247 && !integer_zerop (rhs)
4248 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4249 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4250 get_spelling (errtype), funname, parmnum);
4251 /* Const and volatile mean something different for function types,
4252 so the usual warnings are not appropriate. */
4253 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4254 || TREE_CODE (ttl) != FUNCTION_TYPE)
4255 {
4256 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
4257 warn_for_assignment ("%s discards `const' from pointer target type",
4258 get_spelling (errtype), funname, parmnum);
4259 if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
4260 warn_for_assignment ("%s discards `volatile' from pointer target type",
4261 get_spelling (errtype), funname, parmnum);
4262 }
4263 else
4264 {
4265 /* Because const and volatile on functions are restrictions
4266 that say the function will not do certain things,
4267 it is okay to use a const or volatile function
4268 where an ordinary one is wanted, but not vice-versa. */
4269 if (TYPE_READONLY (ttl) && ! TYPE_READONLY (ttr))
4270 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4271 get_spelling (errtype), funname, parmnum);
4272 if (TYPE_VOLATILE (ttl) && ! TYPE_VOLATILE (ttr))
4273 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4274 get_spelling (errtype), funname, parmnum);
4275 }
4276 }
4277 else if (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4278 == unsigned_type (TYPE_MAIN_VARIANT (ttr)))
4279 {
4280 if (pedantic)
4281 warn_for_assignment ("pointer targets in %s differ in signedness",
4282 get_spelling (errtype), funname, parmnum);
4283 }
4284 else
4285 warn_for_assignment ("%s from incompatible pointer type",
4286 get_spelling (errtype), funname, parmnum);
4287 return convert (type, rhs);
4288 }
4289 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4290 {
4291 /* An explicit constant 0 can convert to a pointer,
4292 but not a 0 that results from casting or folding. */
4293 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)))
4294 {
4295 warn_for_assignment ("%s makes pointer from integer without a cast",
4296 get_spelling (errtype), funname, parmnum);
4297 return convert (type, rhs);
4298 }
4299 return null_pointer_node;
4300 }
4301 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4302 {
4303 warn_for_assignment ("%s makes integer from pointer without a cast",
4304 get_spelling (errtype), funname, parmnum);
4305 return convert (type, rhs);
4306 }
4307
4308 if (!errtype)
4309 {
4310 if (funname)
4311 {
4312 tree selector = maybe_building_objc_message_expr ();
4313
4314 if (selector && parmnum > 2)
4315 error ("incompatible type for argument %d of `%s'",
4316 parmnum - 2, IDENTIFIER_POINTER (selector));
4317 else
4318 error ("incompatible type for argument %d of `%s'",
4319 parmnum, IDENTIFIER_POINTER (funname));
4320 }
4321 else
4322 error ("incompatible type for argument %d of indirect function call",
4323 parmnum);
4324 }
4325 else
4326 error ("incompatible types in %s", get_spelling (errtype));
4327
4328 return error_mark_node;
4329 }
4330
4331 /* Print a warning using MSG.
4332 It gets OPNAME as its one parameter.
4333 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4334 FUNCTION and ARGNUM are handled specially if we are building an
4335 Objective-C selector. */
4336
4337 static void
warn_for_assignment(msg,opname,function,argnum)4338 warn_for_assignment (msg, opname, function, argnum)
4339 char *msg;
4340 char *opname;
4341 tree function;
4342 int argnum;
4343 {
4344 static char argstring[] = "passing arg %d of `%s'";
4345 static char argnofun[] = "passing arg %d";
4346
4347 if (opname == 0)
4348 {
4349 tree selector = maybe_building_objc_message_expr ();
4350
4351 if (selector && argnum > 2)
4352 {
4353 function = selector;
4354 argnum -= 2;
4355 }
4356 if (function)
4357 {
4358 /* Function name is known; supply it. */
4359 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4360 + sizeof (argstring) + 25 /*%d*/ + 1);
4361 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4362 }
4363 else
4364 {
4365 /* Function name unknown (call through ptr); just give arg number. */
4366 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4367 sprintf (opname, argnofun, argnum);
4368 }
4369 }
4370 pedwarn (msg, opname);
4371 }
4372
4373 /* Return nonzero if VALUE is a valid constant-valued expression
4374 for use in initializing a static variable; one that can be an
4375 element of a "constant" initializer.
4376
4377 Return null_pointer_node if the value is absolute;
4378 if it is relocatable, return the variable that determines the relocation.
4379 We assume that VALUE has been folded as much as possible;
4380 therefore, we do not need to check for such things as
4381 arithmetic-combinations of integers. */
4382
4383 static tree
initializer_constant_valid_p(value,endtype)4384 initializer_constant_valid_p (value, endtype)
4385 tree value;
4386 tree endtype;
4387 {
4388 switch (TREE_CODE (value))
4389 {
4390 case CONSTRUCTOR:
4391 return TREE_STATIC (value) ? null_pointer_node : 0;
4392
4393 case INTEGER_CST:
4394 case REAL_CST:
4395 case STRING_CST:
4396 case COMPLEX_CST:
4397 return null_pointer_node;
4398
4399 case ADDR_EXPR:
4400 return TREE_OPERAND (value, 0);
4401
4402 case NON_LVALUE_EXPR:
4403 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4404
4405 case CONVERT_EXPR:
4406 case NOP_EXPR:
4407 /* Allow conversions between pointer types. */
4408 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4409 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4410 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4411 /* Allow conversions between real types. */
4412 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4413 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4414 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4415 /* Allow length-preserving conversions between integer types. */
4416 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4417 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4418 && tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (value)),
4419 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value, 0)))))
4420 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4421 /* Allow conversions between integer types only if explicit value. */
4422 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4423 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4424 {
4425 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4426 endtype);
4427 if (inner == null_pointer_node)
4428 return null_pointer_node;
4429 return 0;
4430 }
4431 /* Allow (int) &foo provided int is as wide as a pointer. */
4432 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4433 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4434 && ! tree_int_cst_lt (TYPE_SIZE (TREE_TYPE (value)),
4435 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value, 0)))))
4436 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4437 endtype);
4438 /* Allow conversions to union types if the value inside is okay. */
4439 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4440 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4441 endtype);
4442 return 0;
4443
4444 case PLUS_EXPR:
4445 if (TREE_CODE (endtype) == INTEGER_TYPE
4446 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4447 return 0;
4448 {
4449 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4450 endtype);
4451 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4452 endtype);
4453 /* If either term is absolute, use the other terms relocation. */
4454 if (valid0 == null_pointer_node)
4455 return valid1;
4456 if (valid1 == null_pointer_node)
4457 return valid0;
4458 return 0;
4459 }
4460
4461 case MINUS_EXPR:
4462 if (TREE_CODE (endtype) == INTEGER_TYPE
4463 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4464 return 0;
4465 {
4466 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4467 endtype);
4468 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4469 endtype);
4470 /* Win if second argument is absolute. */
4471 if (valid1 == null_pointer_node)
4472 return valid0;
4473 /* Win if both arguments have the same relocation.
4474 Then the value is absolute. */
4475 if (valid0 == valid1)
4476 return null_pointer_node;
4477 return 0;
4478 }
4479 }
4480
4481 return 0;
4482 }
4483
4484 /* Perform appropriate conversions on the initial value of a variable,
4485 store it in the declaration DECL,
4486 and print any error messages that are appropriate.
4487 If the init is invalid, store an ERROR_MARK. */
4488
4489 void
store_init_value(decl,init)4490 store_init_value (decl, init)
4491 tree decl, init;
4492 {
4493 register tree value, type;
4494
4495 /* If variable's type was invalidly declared, just ignore it. */
4496
4497 type = TREE_TYPE (decl);
4498 if (TREE_CODE (type) == ERROR_MARK)
4499 return;
4500
4501 /* Digest the specified initializer into an expression. */
4502
4503 value = digest_init (type, init, NULL_PTR, TREE_STATIC (decl),
4504 TREE_STATIC (decl) || pedantic,
4505 IDENTIFIER_POINTER (DECL_NAME (decl)));
4506
4507 /* Store the expression if valid; else report error. */
4508
4509 #if 0
4510 /* Note that this is the only place we can detect the error
4511 in a case such as struct foo bar = (struct foo) { x, y };
4512 where there is one initial value which is a constructor expression. */
4513 if (value == error_mark_node)
4514 ;
4515 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4516 {
4517 error ("initializer for static variable is not constant");
4518 value = error_mark_node;
4519 }
4520 else if (TREE_STATIC (decl)
4521 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4522 {
4523 error ("initializer for static variable uses complicated arithmetic");
4524 value = error_mark_node;
4525 }
4526 else
4527 {
4528 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4529 {
4530 if (! TREE_CONSTANT (value))
4531 pedwarn ("aggregate initializer is not constant");
4532 else if (! TREE_STATIC (value))
4533 pedwarn ("aggregate initializer uses complicated arithmetic");
4534 }
4535 }
4536 #endif
4537
4538 /* ANSI wants warnings about out-of-range constant initializers. */
4539 constant_expression_warning (value);
4540
4541 DECL_INITIAL (decl) = value;
4542 }
4543
4544 /* Methods for storing and printing names for error messages. */
4545
4546 /* Implement a spelling stack that allows components of a name to be pushed
4547 and popped. Each element on the stack is this structure. */
4548
4549 struct spelling
4550 {
4551 int kind;
4552 union
4553 {
4554 int i;
4555 char *s;
4556 } u;
4557 };
4558
4559 #define SPELLING_STRING 1
4560 #define SPELLING_MEMBER 2
4561 #define SPELLING_BOUNDS 3
4562
4563 static struct spelling *spelling; /* Next stack element (unused). */
4564 static struct spelling *spelling_base; /* Spelling stack base. */
4565 static int spelling_size; /* Size of the spelling stack. */
4566
4567 /* Macros to save and restore the spelling stack around push_... functions.
4568 Alternative to SAVE_SPELLING_STACK. */
4569
4570 #define SPELLING_DEPTH() (spelling - spelling_base)
4571 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4572
4573 /* Save and restore the spelling stack around arbitrary C code. */
4574
4575 #define SAVE_SPELLING_DEPTH(code) \
4576 { \
4577 int __depth = SPELLING_DEPTH (); \
4578 code; \
4579 RESTORE_SPELLING_DEPTH (__depth); \
4580 }
4581
4582 /* Push an element on the spelling stack with type KIND and assign VALUE
4583 to MEMBER. */
4584
4585 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4586 { \
4587 int depth = SPELLING_DEPTH (); \
4588 \
4589 if (depth >= spelling_size) \
4590 { \
4591 spelling_size += 10; \
4592 if (spelling_base == 0) \
4593 spelling_base \
4594 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4595 else \
4596 spelling_base \
4597 = (struct spelling *) xrealloc (spelling_base, \
4598 spelling_size * sizeof (struct spelling)); \
4599 RESTORE_SPELLING_DEPTH (depth); \
4600 } \
4601 \
4602 spelling->kind = (KIND); \
4603 spelling->MEMBER = (VALUE); \
4604 spelling++; \
4605 }
4606
4607 /* Push STRING on the stack. Printed literally. */
4608
4609 static void
push_string(string)4610 push_string (string)
4611 char *string;
4612 {
4613 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4614 }
4615
4616 /* Push a member name on the stack. Printed as '.' STRING. */
4617
4618 static void
push_member_name(string)4619 push_member_name (string)
4620 char *string;
4621 {
4622 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4623 }
4624
4625 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4626
4627 static void
push_array_bounds(bounds)4628 push_array_bounds (bounds)
4629 int bounds;
4630 {
4631 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4632 }
4633
4634 /* Compute the maximum size in bytes of the printed spelling. */
4635
4636 static int
spelling_length()4637 spelling_length ()
4638 {
4639 register int size = 0;
4640 register struct spelling *p;
4641
4642 for (p = spelling_base; p < spelling; p++)
4643 {
4644 if (p->kind == SPELLING_BOUNDS)
4645 size += 25;
4646 else
4647 size += strlen (p->u.s) + 1;
4648 }
4649
4650 return size;
4651 }
4652
4653 /* Print the spelling to BUFFER and return it. */
4654
4655 static char *
print_spelling(buffer)4656 print_spelling (buffer)
4657 register char *buffer;
4658 {
4659 register char *d = buffer;
4660 register char *s;
4661 register struct spelling *p;
4662
4663 for (p = spelling_base; p < spelling; p++)
4664 if (p->kind == SPELLING_BOUNDS)
4665 {
4666 sprintf (d, "[%d]", p->u.i);
4667 d += strlen (d);
4668 }
4669 else
4670 {
4671 if (p->kind == SPELLING_MEMBER)
4672 *d++ = '.';
4673 for (s = p->u.s; *d = *s++; d++)
4674 ;
4675 }
4676 *d++ = '\0';
4677 return buffer;
4678 }
4679
4680 /* Provide a means to pass component names derived from the spelling stack. */
4681
4682 char initialization_message;
4683
4684 /* Interpret the spelling of the given ERRTYPE message. */
4685
4686 static char *
get_spelling(errtype)4687 get_spelling (errtype)
4688 char *errtype;
4689 {
4690 static char *buffer;
4691 static int size = -1;
4692
4693 if (errtype == &initialization_message)
4694 {
4695 /* Avoid counting chars */
4696 static char message[] = "initialization of `%s'";
4697 register int needed = sizeof (message) + spelling_length () + 1;
4698 char *temp;
4699
4700 if (size < 0)
4701 buffer = (char *) xmalloc (size = needed);
4702 if (needed > size)
4703 buffer = (char *) xrealloc (buffer, size = needed);
4704
4705 temp = (char *) alloca (needed);
4706 sprintf (buffer, message, print_spelling (temp));
4707 return buffer;
4708 }
4709
4710 return errtype;
4711 }
4712
4713 /* Issue an error message for a bad initializer component.
4714 FORMAT describes the message. OFWHAT is the name for the component.
4715 LOCAL is a format string for formatting the insertion of the name
4716 into the message.
4717
4718 If OFWHAT is null, the component name is stored on the spelling stack.
4719 If the component name is a null string, then LOCAL is omitted entirely. */
4720
4721 void
error_init(format,local,ofwhat)4722 error_init (format, local, ofwhat)
4723 char *format, *local, *ofwhat;
4724 {
4725 char *buffer;
4726
4727 if (ofwhat == 0)
4728 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4729 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4730
4731 if (*ofwhat)
4732 sprintf (buffer, local, ofwhat);
4733 else
4734 buffer[0] = 0;
4735
4736 error (format, buffer);
4737 }
4738
4739 /* Issue a pedantic warning for a bad initializer component.
4740 FORMAT describes the message. OFWHAT is the name for the component.
4741 LOCAL is a format string for formatting the insertion of the name
4742 into the message.
4743
4744 If OFWHAT is null, the component name is stored on the spelling stack.
4745 If the component name is a null string, then LOCAL is omitted entirely. */
4746
4747 void
pedwarn_init(format,local,ofwhat)4748 pedwarn_init (format, local, ofwhat)
4749 char *format, *local, *ofwhat;
4750 {
4751 char *buffer;
4752
4753 if (ofwhat == 0)
4754 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4755 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4756
4757 if (*ofwhat)
4758 sprintf (buffer, local, ofwhat);
4759 else
4760 buffer[0] = 0;
4761
4762 pedwarn (format, buffer);
4763 }
4764
4765 /* Keep a pointer to the last free TREE_LIST node as we digest an initializer,
4766 so that we can reuse it. This is set in digest_init, and used in
4767 process_init_constructor.
4768
4769 We will never keep more than one free TREE_LIST node here. This is for
4770 two main reasons. First, we take elements off the old list and add them
4771 to the new list one at a time, thus there should never be more than
4772 one free TREE_LIST at a time, and thus even if there is, we will never
4773 need more than one. Secondly, to avoid dangling pointers to freed obstacks,
4774 we want to always ensure that we have either a pointer to a valid TREE_LIST
4775 within the current initializer, or else a pointer to null. */
4776
4777 static tree free_tree_list = NULL_TREE;
4778
4779 /* Digest the parser output INIT as an initializer for type TYPE.
4780 Return a C expression of type TYPE to represent the initial value.
4781
4782 If TAIL is nonzero, it points to a variable holding a list of elements
4783 of which INIT is the first. We update the list stored there by
4784 removing from the head all the elements that we use.
4785 Normally this is only one; we use more than one element only if
4786 TYPE is an aggregate and INIT is not a constructor.
4787
4788 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4789 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4790 applies only to elements of constructors.
4791
4792 If OFWHAT is nonnull, it specifies what we are initializing, for error
4793 messages. Examples: variable name, variable.member, array[44].
4794 If OFWHAT is null, the component name is stored on the spelling stack.
4795 (That is true for all nested calls to digest_init.) */
4796
4797 tree
digest_init(type,init,tail,require_constant,constructor_constant,ofwhat)4798 digest_init (type, init, tail, require_constant, constructor_constant, ofwhat)
4799 tree type, init, *tail;
4800 int require_constant, constructor_constant;
4801 char *ofwhat;
4802 {
4803 enum tree_code code = TREE_CODE (type);
4804 tree element = 0;
4805 tree old_tail_contents;
4806 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
4807 tree node which has no TREE_TYPE. */
4808 int raw_constructor
4809 = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
4810 tree inside_init = init;
4811
4812 /* Make sure there is just one "partially bracketed" message
4813 per top-level initializer or constructor. */
4814 if (ofwhat != 0)
4815 partial_bracket_mentioned = 0;
4816
4817 /* By default, assume we use one element from a list.
4818 We correct this later in the cases where it is not true.
4819
4820 Thus, we update TAIL now to point to the next element, and save the
4821 old value in OLD_TAIL_CONTENTS. If we didn't actually use the first
4822 element, then we will reset TAIL before proceeding. FREE_TREE_LIST
4823 is handled similarly. */
4824
4825 if (tail)
4826 {
4827 old_tail_contents = *tail;
4828 *tail = TREE_CHAIN (*tail);
4829 free_tree_list = old_tail_contents;
4830 }
4831 else
4832 free_tree_list = 0;
4833
4834 if (init == error_mark_node)
4835 return init;
4836
4837 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4838 /* Do not use STRIP_NOPS here. We do not want an enumerator
4839 whose value is 0 to count as a null pointer constant. */
4840 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4841 inside_init = TREE_OPERAND (init, 0);
4842
4843 if (inside_init && raw_constructor
4844 && CONSTRUCTOR_ELTS (inside_init) != 0
4845 && TREE_CHAIN (CONSTRUCTOR_ELTS (inside_init)) == 0)
4846 {
4847 element = TREE_VALUE (CONSTRUCTOR_ELTS (inside_init));
4848 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4849 if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
4850 element = TREE_OPERAND (element, 0);
4851 }
4852
4853 /* Initialization of an array of chars from a string constant
4854 optionally enclosed in braces. */
4855
4856 if (code == ARRAY_TYPE)
4857 {
4858 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4859 if ((typ1 == char_type_node
4860 || typ1 == signed_char_type_node
4861 || typ1 == unsigned_char_type_node
4862 || typ1 == unsigned_wchar_type_node
4863 || typ1 == signed_wchar_type_node)
4864 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)
4865 || (element && TREE_CODE (element) == STRING_CST)))
4866 {
4867 tree string = element ? element : inside_init;
4868
4869 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
4870 != char_type_node)
4871 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4872 {
4873 error_init ("char-array%s initialized from wide string",
4874 " `%s'", ofwhat);
4875 return error_mark_node;
4876 }
4877 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
4878 == char_type_node)
4879 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4880 {
4881 error_init ("int-array%s initialized from non-wide string",
4882 " `%s'", ofwhat);
4883 return error_mark_node;
4884 }
4885
4886 TREE_TYPE (string) = type;
4887 if (TYPE_DOMAIN (type) != 0
4888 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4889 {
4890 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4891 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4892 /* Subtract 1 (or sizeof (wchar_t))
4893 because it's ok to ignore the terminating null char
4894 that is counted in the length of the constant. */
4895 if (size < TREE_STRING_LENGTH (string)
4896 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4897 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4898 : 1))
4899 pedwarn_init (
4900 "initializer-string for array of chars%s is too long",
4901 " `%s'", ofwhat);
4902 }
4903 return string;
4904 }
4905 }
4906
4907 /* Any type except an array can be initialized
4908 from an expression of the same type, optionally with braces.
4909 For an array, this is allowed only for a string constant. */
4910
4911 if (inside_init && TREE_TYPE (inside_init) != 0
4912 && ((TYPE_MAIN_VARIANT (TREE_TYPE (inside_init))
4913 == TYPE_MAIN_VARIANT (type))
4914 || (code == ARRAY_TYPE
4915 && comptypes (TREE_TYPE (inside_init), type))
4916 || (code == POINTER_TYPE
4917 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4918 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4919 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4920 TREE_TYPE (type)))))
4921 {
4922 if (code == POINTER_TYPE
4923 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4924 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4925 inside_init = default_conversion (inside_init);
4926 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST)
4927 {
4928 error_init ("array%s initialized from non-constant array expression",
4929 " `%s'", ofwhat);
4930 return error_mark_node;
4931 }
4932
4933 if (optimize && TREE_READONLY (inside_init)
4934 && TREE_CODE (inside_init) == VAR_DECL)
4935 inside_init = decl_constant_value (inside_init);
4936
4937 if (require_constant && ! TREE_CONSTANT (inside_init))
4938 {
4939 error_init ("initializer element%s is not constant",
4940 " for `%s'", ofwhat);
4941 inside_init = error_mark_node;
4942 }
4943 else if (require_constant
4944 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4945 {
4946 error_init ("initializer element%s is not computable at load time",
4947 " for `%s'", ofwhat);
4948 inside_init = error_mark_node;
4949 }
4950
4951 return inside_init;
4952 }
4953
4954 if (element && (TREE_TYPE (element) == type
4955 || (code == ARRAY_TYPE && TREE_TYPE (element)
4956 && comptypes (TREE_TYPE (element), type))))
4957 {
4958 if (code == ARRAY_TYPE)
4959 {
4960 error_init ("array%s initialized from non-constant array expression",
4961 " `%s'", ofwhat);
4962 return error_mark_node;
4963 }
4964 if (pedantic && (code == RECORD_TYPE || code == UNION_TYPE))
4965 pedwarn ("single-expression nonscalar initializer has braces");
4966 if (optimize && TREE_READONLY (element) && TREE_CODE (element) == VAR_DECL)
4967 element = decl_constant_value (element);
4968
4969 if (require_constant && ! TREE_CONSTANT (element))
4970 {
4971 error_init ("initializer element%s is not constant",
4972 " for `%s'", ofwhat);
4973 element = error_mark_node;
4974 }
4975 else if (require_constant
4976 && initializer_constant_valid_p (element, TREE_TYPE (element)) == 0)
4977 {
4978 error_init ("initializer element%s is not computable at load time",
4979 " for `%s'", ofwhat);
4980 element = error_mark_node;
4981 }
4982
4983 return element;
4984 }
4985
4986 /* Check for initializing a union by its first field.
4987 Such an initializer must use braces. */
4988
4989 if (code == UNION_TYPE)
4990 {
4991 tree result;
4992 tree field = TYPE_FIELDS (type);
4993
4994 /* Find the first named field. ANSI decided in September 1990
4995 that only named fields count here. */
4996 while (field && DECL_NAME (field) == 0)
4997 field = TREE_CHAIN (field);
4998
4999 if (field == 0)
5000 {
5001 error_init ("union%s with no named members cannot be initialized",
5002 " `%s'", ofwhat);
5003 return error_mark_node;
5004 }
5005
5006 if (raw_constructor)
5007 result = process_init_constructor (type, inside_init, NULL_PTR,
5008 require_constant,
5009 constructor_constant, ofwhat);
5010 else if (tail != 0)
5011 {
5012 *tail = old_tail_contents;
5013 free_tree_list = NULL_TREE;
5014 result = process_init_constructor (type, NULL_TREE, tail,
5015 require_constant,
5016 constructor_constant, ofwhat);
5017 }
5018 else
5019 result = 0;
5020
5021 if (result)
5022 return result;
5023 }
5024
5025 /* Handle scalar types, including conversions. */
5026
5027 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
5028 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
5029 {
5030 if (raw_constructor)
5031 {
5032 if (element == 0)
5033 {
5034 error_init (
5035 "initializer for scalar%s requires one element",
5036 " `%s'", ofwhat);
5037 return error_mark_node;
5038 }
5039 else
5040 {
5041 /* Deal with extra levels of {...}. */
5042 if (TREE_CODE (element) == CONSTRUCTOR
5043 && TREE_TYPE (element) == 0)
5044 {
5045 error_init (
5046 "initializer for scalar%s requires one element",
5047 " `%s'", ofwhat);
5048 return error_mark_node;
5049 }
5050 inside_init = element;
5051 }
5052 }
5053
5054 #if 0 /* A non-raw constructor is an actual expression. */
5055 if (TREE_CODE (inside_init) == CONSTRUCTOR)
5056 {
5057 error_init ("initializer for scalar%s has extra braces",
5058 " `%s'", ofwhat);
5059 return error_mark_node;
5060 }
5061 #endif
5062
5063 SAVE_SPELLING_DEPTH
5064 ({
5065 if (ofwhat)
5066 push_string (ofwhat);
5067 if (!raw_constructor)
5068 inside_init = init;
5069 /* Note that convert_for_assignment calls default_conversion
5070 for arrays and functions. We must not call it in the
5071 case where inside_init is a null pointer constant. */
5072 inside_init
5073 = convert_for_assignment (type, inside_init,
5074 &initialization_message,
5075 NULL_TREE, NULL_TREE, 0);
5076 });
5077
5078 if (require_constant && ! TREE_CONSTANT (inside_init))
5079 {
5080 error_init ("initializer element%s is not constant",
5081 " for `%s'", ofwhat);
5082 inside_init = error_mark_node;
5083 }
5084 else if (require_constant
5085 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
5086 {
5087 error_init ("initializer element%s is not computable at load time",
5088 " for `%s'", ofwhat);
5089 inside_init = error_mark_node;
5090 }
5091
5092 return inside_init;
5093 }
5094
5095 /* Come here only for records and arrays. */
5096
5097 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5098 {
5099 error_init ("variable-sized object%s may not be initialized",
5100 " `%s'", ofwhat);
5101 return error_mark_node;
5102 }
5103
5104 if (code == ARRAY_TYPE || code == RECORD_TYPE)
5105 {
5106 if (raw_constructor)
5107 return process_init_constructor (type, inside_init,
5108 NULL_PTR, constructor_constant,
5109 constructor_constant, ofwhat);
5110 else if (tail != 0)
5111 {
5112 *tail = old_tail_contents;
5113 free_tree_list = NULL_TREE;
5114 return process_init_constructor (type, NULL_TREE, tail,
5115 constructor_constant,
5116 constructor_constant, ofwhat);
5117 }
5118 else if (flag_traditional)
5119 /* Traditionally one can say `char x[100] = 0;'. */
5120 return process_init_constructor (type,
5121 build_nt (CONSTRUCTOR, NULL_TREE,
5122 tree_cons (NULL_TREE,
5123 inside_init,
5124 NULL_TREE)),
5125 NULL_PTR, constructor_constant,
5126 constructor_constant, ofwhat);
5127 }
5128
5129 error_init ("invalid initializer%s", " for `%s'", ofwhat);
5130 return error_mark_node;
5131 }
5132
5133 /* Process a constructor for a variable of type TYPE.
5134 The constructor elements may be specified either with INIT or with ELTS,
5135 only one of which should be non-null.
5136
5137 If INIT is specified, it is a CONSTRUCTOR node which is specifically
5138 and solely for initializing this datum.
5139
5140 If ELTS is specified, it is the address of a variable containing
5141 a list of expressions. We take as many elements as we need
5142 from the head of the list and update the list.
5143
5144 In the resulting constructor, TREE_CONSTANT is set if all elts are
5145 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
5146 constants that the assembler and linker can compute them.
5147
5148 The argument CONSTANT_VALUE says to print an error if either the
5149 value or any element is not a constant.
5150
5151 The argument CONSTANT_ELEMENT says to print an error if an element
5152 of an aggregate is not constant. It does not apply to a value
5153 which is not a constructor.
5154
5155 OFWHAT is a character string describing the object being initialized,
5156 for error messages. It might be "variable" or "variable.member"
5157 or "variable[17].member[5]".
5158
5159 If OFWHAT is null, the description string is stored on the spelling
5160 stack. That is always true for recursive calls. */
5161
5162 static tree
process_init_constructor(type,init,elts,constant_value,constant_element,ofwhat)5163 process_init_constructor (type, init, elts, constant_value, constant_element,
5164 ofwhat)
5165 tree type, init, *elts;
5166 int constant_value, constant_element;
5167 char *ofwhat;
5168 {
5169 register tree tail;
5170 /* List of the elements of the result constructor,
5171 in reverse order. */
5172 register tree members = NULL;
5173 tree result;
5174 int allconstant = 1;
5175 int allsimple = 1;
5176 int erroneous = 0;
5177 int depth = SPELLING_DEPTH ();
5178
5179 if (ofwhat)
5180 push_string (ofwhat);
5181
5182 /* Make TAIL be the list of elements to use for the initialization,
5183 no matter how the data was given to us. */
5184
5185 if (elts)
5186 {
5187 if (warn_missing_braces)
5188 {
5189 if (! partial_bracket_mentioned)
5190 warning ("aggregate has a partly bracketed initializer");
5191 partial_bracket_mentioned = 1;
5192 }
5193 tail = *elts;
5194 }
5195 else
5196 tail = CONSTRUCTOR_ELTS (init);
5197
5198 /* Gobble as many elements as needed, and make a constructor or initial value
5199 for each element of this aggregate. Chain them together in result.
5200 If there are too few, use 0 for each scalar ultimate component. */
5201
5202 if (TREE_CODE (type) == ARRAY_TYPE)
5203 {
5204 tree min_index, max_index;
5205 /* These are non-zero only within a range initializer. */
5206 tree start_index = 0, end_index = 0;
5207 /* Within a range, this is the value for the elts in the range. */
5208 tree range_val = 0;
5209 /* Do arithmetic using double integers, but don't use fold/build,
5210 because these allocate a new tree object everytime they are called,
5211 thus resulting in gcc using too much memory for large
5212 initializers. */
5213 union tree_node current_index_node, members_index_node;
5214 tree current_index = ¤t_index_node;
5215 tree members_index = &members_index_node;
5216 TREE_TYPE (current_index) = integer_type_node;
5217 TREE_TYPE (members_index) = integer_type_node;
5218
5219 /* If we have array bounds, set our bounds from that. Otherwise,
5220 we have a lower bound of zero and an unknown upper bound. */
5221 if (TYPE_DOMAIN (type))
5222 {
5223 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
5224 max_index = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
5225 }
5226 else
5227 {
5228 min_index = integer_zero_node;
5229 max_index = 0;
5230 }
5231
5232 TREE_INT_CST_LOW (members_index) = TREE_INT_CST_LOW (min_index);
5233 TREE_INT_CST_HIGH (members_index) = TREE_INT_CST_HIGH (min_index);
5234
5235 /* Don't leave the loop based on index if the next item has an explicit
5236 index value that will override it. */
5237
5238 for (TREE_INT_CST_LOW (current_index) = TREE_INT_CST_LOW (min_index),
5239 TREE_INT_CST_HIGH (current_index) = TREE_INT_CST_HIGH (min_index);
5240 tail != 0 || end_index;
5241 add_double (TREE_INT_CST_LOW (current_index),
5242 TREE_INT_CST_HIGH (current_index), 1, 0,
5243 &TREE_INT_CST_LOW (current_index),
5244 &TREE_INT_CST_HIGH (current_index)))
5245 {
5246 register tree next1 = 0;
5247
5248 /* Handle the case where we are inside of a range.
5249 current_index increments through the range,
5250 so just keep reusing the same element of TAIL
5251 until the end of the range. */
5252 if (end_index != 0)
5253 {
5254 next1 = range_val;
5255 if (!tree_int_cst_lt (current_index, end_index))
5256 end_index = 0;
5257 }
5258
5259 /* If this element specifies an index,
5260 move to that index before storing it in the new list. */
5261 else if (TREE_PURPOSE (tail) != 0)
5262 {
5263 int win = 0;
5264 tree index = TREE_PURPOSE (tail);
5265
5266 if (index && (TREE_CODE (index) == NON_LVALUE_EXPR
5267 || TREE_CODE (index) == NOP_EXPR))
5268 index = TREE_OPERAND (index, 0);
5269
5270 /* Begin a range. */
5271 if (TREE_CODE (index) == TREE_LIST)
5272 {
5273 start_index = TREE_PURPOSE (index);
5274 end_index = TREE_PURPOSE (TREE_CHAIN (index));
5275
5276 /* Expose constants. It Doesn't matter if we change
5277 the mode.*/
5278 if (end_index
5279 && (TREE_CODE (end_index) == NON_LVALUE_EXPR
5280 || TREE_CODE (end_index) == NOP_EXPR))
5281 end_index = TREE_OPERAND (end_index, 0);
5282 if (start_index
5283 && (TREE_CODE (start_index) == NON_LVALUE_EXPR
5284 || TREE_CODE (start_index) == NOP_EXPR))
5285 start_index = TREE_OPERAND (start_index, 0);
5286
5287 constant_expression_warning (start_index);
5288 constant_expression_warning (end_index);
5289
5290 if ((TREE_CODE (start_index) == IDENTIFIER_NODE)
5291 || (TREE_CODE (end_index) == IDENTIFIER_NODE))
5292 error ("field name used as index in array initializer");
5293 else if ((TREE_CODE (start_index) != INTEGER_CST)
5294 || (TREE_CODE (end_index) != INTEGER_CST))
5295 error ("non-constant or non-integer array index in initializer");
5296 else if (tree_int_cst_lt (start_index, min_index)
5297 || (max_index && tree_int_cst_lt (max_index, start_index))
5298 || tree_int_cst_lt (end_index, min_index)
5299 || (max_index && tree_int_cst_lt (max_index, end_index)))
5300 error ("array index out of range in initializer");
5301 else if (tree_int_cst_lt (end_index, start_index))
5302 {
5303 /* If the range is empty, don't initialize any elements,
5304 but do reset current_index for the next initializer
5305 element. */
5306 warning ("empty array initializer range");
5307 tail = TREE_CHAIN (tail);
5308 TREE_INT_CST_LOW (current_index)
5309 = TREE_INT_CST_LOW (end_index);
5310 TREE_INT_CST_HIGH (current_index)
5311 = TREE_INT_CST_HIGH (end_index);
5312 continue;
5313 }
5314 else
5315 {
5316 TREE_INT_CST_LOW (current_index)
5317 = TREE_INT_CST_LOW (start_index);
5318 TREE_INT_CST_HIGH (current_index)
5319 = TREE_INT_CST_HIGH (start_index);
5320 win = 1;
5321 /* See if the first element is also the last. */
5322 if (!tree_int_cst_lt (current_index, end_index))
5323 end_index = 0;
5324 }
5325 }
5326 else if (TREE_CODE (index) == IDENTIFIER_NODE)
5327 error ("field name used as index in array initializer");
5328 else if (TREE_CODE (index) != INTEGER_CST)
5329 error ("non-constant array index in initializer");
5330 else if (tree_int_cst_lt (index, min_index)
5331 || (max_index && tree_int_cst_lt (max_index, index)))
5332 error ("array index out of range in initializer");
5333 else
5334 {
5335 constant_expression_warning (index);
5336 TREE_INT_CST_LOW (current_index) = TREE_INT_CST_LOW (index);
5337 TREE_INT_CST_HIGH (current_index)
5338 = TREE_INT_CST_HIGH (index);
5339 win = 1;
5340 }
5341
5342 if (!win)
5343 {
5344 /* If there was an error, end the current range. */
5345 end_index = 0;
5346 TREE_VALUE (tail) = error_mark_node;
5347 }
5348 }
5349
5350 if (max_index && tree_int_cst_lt (max_index, current_index))
5351 break; /* Stop if we've indeed run out of elements. */
5352
5353 /* Now digest the value specified. */
5354 if (next1 != 0)
5355 ;
5356 else if (TREE_VALUE (tail) != 0)
5357 {
5358 tree tail1 = tail;
5359
5360 /* Build the element of this array, with "[]" notation. For
5361 error messages, we assume that the index fits within a
5362 host int. */
5363 SAVE_SPELLING_DEPTH
5364 ({
5365 push_array_bounds (TREE_INT_CST_LOW (current_index));
5366 next1 = digest_init (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
5367 TREE_VALUE (tail), &tail1,
5368 /* Both of these are the same because
5369 a value here is an elt overall. */
5370 constant_element, constant_element,
5371 NULL_PTR);
5372 });
5373
5374 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
5375 abort ();
5376 if (tail == tail1 && TYPE_DOMAIN (type) == 0)
5377 {
5378 error_init (
5379 "non-empty initializer for array%s of empty elements",
5380 " `%s'", NULL_PTR);
5381 /* Just ignore what we were supposed to use. */
5382 tail1 = 0;
5383 }
5384 tail = tail1;
5385 }
5386 else
5387 {
5388 next1 = error_mark_node;
5389 tail = TREE_CHAIN (tail);
5390 }
5391
5392 if (end_index != 0)
5393 range_val = next1;
5394
5395 if (next1 == error_mark_node)
5396 erroneous = 1;
5397 else if (!TREE_CONSTANT (next1))
5398 allconstant = 0;
5399 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
5400 allsimple = 0;
5401
5402 /* Now store NEXT1 in the list, I elements from the *end*.
5403 Make the list longer if necessary. */
5404 while (! tree_int_cst_lt (current_index, members_index))
5405 {
5406 if (free_tree_list)
5407 {
5408 TREE_CHAIN (free_tree_list) = members;
5409 TREE_PURPOSE (free_tree_list) = NULL_TREE;
5410 TREE_VALUE (free_tree_list) = NULL_TREE;
5411 members = free_tree_list;
5412 free_tree_list = NULL_TREE;
5413 }
5414 else
5415 members = tree_cons (NULL_TREE, NULL_TREE, members);
5416 add_double (TREE_INT_CST_LOW (members_index),
5417 TREE_INT_CST_HIGH (members_index), 1, 0,
5418 &TREE_INT_CST_LOW (members_index),
5419 &TREE_INT_CST_HIGH (members_index));
5420 }
5421
5422 {
5423 tree temp;
5424 union tree_node idx_node;
5425 tree idx = &idx_node;
5426 TREE_TYPE (idx) = integer_type_node;
5427
5428 temp = members;
5429 for (add_double (TREE_INT_CST_LOW (members_index),
5430 TREE_INT_CST_HIGH (members_index), -1, -1,
5431 &TREE_INT_CST_LOW (idx),
5432 &TREE_INT_CST_HIGH (idx));
5433 tree_int_cst_lt (current_index, idx);
5434 add_double (TREE_INT_CST_LOW (idx),
5435 TREE_INT_CST_HIGH (idx), -1, -1,
5436 &TREE_INT_CST_LOW (idx),
5437 &TREE_INT_CST_HIGH (idx)))
5438 temp = TREE_CHAIN (temp);
5439 TREE_VALUE (temp) = next1;
5440 }
5441 }
5442 }
5443 if (TREE_CODE (type) == RECORD_TYPE)
5444 {
5445 register tree field;
5446 int members_length = 0;
5447 int i;
5448
5449 /* Don't leave the loop based on field just yet; see if next item
5450 overrides the expected field first. */
5451
5452 for (field = TYPE_FIELDS (type), i = 0; tail;
5453 field = TREE_CHAIN (field), i++)
5454 {
5455 register tree next1;
5456
5457 /* If this element specifies a field,
5458 move to that field before storing it in the new list. */
5459 if (TREE_PURPOSE (tail) != 0)
5460 {
5461 int win = 0;
5462
5463 if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
5464 error ("index value instead of field name in structure initializer");
5465 else
5466 {
5467 tree temp;
5468 int j;
5469 for (temp = TYPE_FIELDS (type), j = 0;
5470 temp;
5471 temp = TREE_CHAIN (temp), j++)
5472 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
5473 break;
5474 if (temp)
5475 field = temp, i = j, win = 1;
5476 else
5477 error ("no field `%s' in structure being initialized",
5478 IDENTIFIER_POINTER (TREE_PURPOSE (tail)));
5479 }
5480 if (!win)
5481 TREE_VALUE (tail) = error_mark_node;
5482 }
5483
5484 if (field == 0)
5485 break; /* No more fields to init. */
5486
5487 if (! DECL_NAME (field))
5488 {
5489 next1 = integer_zero_node;
5490 }
5491 else if (TREE_VALUE (tail) != 0)
5492 {
5493 tree tail1 = tail;
5494
5495 /* Build the name of this member, with a "." for membership. */
5496 SAVE_SPELLING_DEPTH
5497 ({
5498 push_member_name (IDENTIFIER_POINTER (DECL_NAME (field)));
5499 next1 = digest_init (TREE_TYPE (field),
5500 TREE_VALUE (tail), &tail1,
5501 constant_element, constant_element,
5502 NULL_PTR);
5503 });
5504 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
5505 abort ();
5506 tail = tail1;
5507 }
5508 else
5509 {
5510 next1 = error_mark_node;
5511 tail = TREE_CHAIN (tail);
5512 }
5513
5514 if (next1 == error_mark_node)
5515 erroneous = 1;
5516 else if (!TREE_CONSTANT (next1))
5517 allconstant = 0;
5518 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
5519 allsimple = 0;
5520
5521 /* Now store NEXT1 in the list, I elements from the *end*.
5522 Make the list longer if necessary. */
5523 while (i >= members_length)
5524 {
5525 if (free_tree_list)
5526 {
5527 TREE_CHAIN (free_tree_list) = members;
5528 TREE_PURPOSE (free_tree_list) = NULL_TREE;
5529 TREE_VALUE (free_tree_list) = NULL_TREE;
5530 members = free_tree_list;
5531 free_tree_list = NULL_TREE;
5532 }
5533 else
5534 members = tree_cons (NULL_TREE, NULL_TREE, members);
5535 members_length++;
5536 }
5537 {
5538 tree temp;
5539 int j;
5540
5541 temp = members;
5542 for (j = members_length - 1; j > i; j--)
5543 temp = TREE_CHAIN (temp);
5544 TREE_VALUE (temp) = next1;
5545 TREE_PURPOSE (temp) = field;
5546 }
5547 }
5548 }
5549 if (TREE_CODE (type) == UNION_TYPE)
5550 {
5551 register tree field = TYPE_FIELDS (type);
5552 register tree next1;
5553
5554 /* Find the first named field. ANSI decided in September 1990
5555 that only named fields count here. */
5556 while (field && DECL_NAME (field) == 0)
5557 field = TREE_CHAIN (field);
5558
5559 /* For a union, get the initializer for 1 fld. */
5560
5561 if (tail == 0)
5562 {
5563 error ("empty initializer for union");
5564 tail = build_tree_list (0, 0);
5565 }
5566
5567 /* If this element specifies a field, initialize via that field. */
5568 if (TREE_PURPOSE (tail) != 0)
5569 {
5570 int win = 0;
5571
5572 if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
5573 /* Handle the case of a call by build_c_cast. */
5574 field = TREE_PURPOSE (tail), win = 1;
5575 else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
5576 error ("index value instead of field name in union initializer");
5577 else
5578 {
5579 tree temp;
5580 for (temp = TYPE_FIELDS (type);
5581 temp;
5582 temp = TREE_CHAIN (temp))
5583 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
5584 break;
5585 if (temp)
5586 field = temp, win = 1;
5587 else
5588 error ("no field `%s' in union being initialized",
5589 IDENTIFIER_POINTER (TREE_PURPOSE (tail)));
5590 }
5591 if (!win)
5592 TREE_VALUE (tail) = error_mark_node;
5593 }
5594
5595 if (TREE_VALUE (tail) != 0)
5596 {
5597 tree tail1 = tail;
5598
5599 /* Build the name of this member, with a "." for membership. */
5600 SAVE_SPELLING_DEPTH
5601 ({
5602 push_member_name (IDENTIFIER_POINTER (DECL_NAME (field)));
5603 next1 = digest_init (TREE_TYPE (field),
5604 TREE_VALUE (tail), &tail1,
5605 constant_value, constant_element, NULL_PTR);
5606 });
5607 if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
5608 abort ();
5609 tail = tail1;
5610 }
5611 else
5612 {
5613 next1 = error_mark_node;
5614 tail = TREE_CHAIN (tail);
5615 }
5616
5617 if (next1 == error_mark_node)
5618 erroneous = 1;
5619 else if (!TREE_CONSTANT (next1))
5620 allconstant = 0;
5621 else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
5622 allsimple = 0;
5623 if (free_tree_list)
5624 {
5625 TREE_CHAIN (free_tree_list) = members;
5626 TREE_PURPOSE (free_tree_list) = field;
5627 TREE_VALUE (free_tree_list) = next1;
5628 members = free_tree_list;
5629 free_tree_list = NULL_TREE;
5630 }
5631 else
5632 members = tree_cons (field, next1, members);
5633 }
5634
5635 /* If arguments were specified as a list, just remove the ones we used. */
5636 if (elts)
5637 *elts = tail;
5638 /* If arguments were specified as a constructor,
5639 complain unless we used all the elements of the constructor. */
5640 else if (tail)
5641 {
5642 if (TREE_CODE (type) == UNION_TYPE)
5643 {
5644 pedwarn_init ("excess elements in union initializer%s",
5645 " after `%s'", NULL_PTR);
5646 }
5647 else
5648 {
5649 pedwarn_init ("excess elements in aggregate initializer%s",
5650 " after `%s'", NULL_PTR);
5651 }
5652 }
5653
5654 /* It might be possible to use SAVE_SPELLING_DEPTH, but I suspect that
5655 some preprocessor somewhere won't accept that much text as an argument.
5656 It's also likely to make debugging difficult. */
5657
5658 RESTORE_SPELLING_DEPTH (depth);
5659
5660 if (erroneous)
5661 return error_mark_node;
5662
5663 if (elts)
5664 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
5665 else
5666 {
5667 result = init;
5668 CONSTRUCTOR_ELTS (result) = nreverse (members);
5669 TREE_TYPE (result) = type;
5670 TREE_CONSTANT (result) = 0;
5671 TREE_STATIC (result) = 0;
5672 }
5673 if (allconstant) TREE_CONSTANT (result) = 1;
5674 if (allconstant && allsimple) TREE_STATIC (result) = 1;
5675 return result;
5676 }
5677
5678 /* Expand an ASM statement with operands, handling output operands
5679 that are not variables or INDIRECT_REFS by transforming such
5680 cases into cases that expand_asm_operands can handle.
5681
5682 Arguments are same as for expand_asm_operands. */
5683
5684 void
c_expand_asm_operands(string,outputs,inputs,clobbers,vol,filename,line)5685 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
5686 tree string, outputs, inputs, clobbers;
5687 int vol;
5688 char *filename;
5689 int line;
5690 {
5691 int noutputs = list_length (outputs);
5692 register int i;
5693 /* o[I] is the place that output number I should be written. */
5694 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
5695 register tree tail;
5696
5697 if (TREE_CODE (string) == ADDR_EXPR)
5698 string = TREE_OPERAND (string, 0);
5699 if (TREE_CODE (string) != STRING_CST)
5700 {
5701 error ("asm template is not a string constant");
5702 return;
5703 }
5704
5705 /* Record the contents of OUTPUTS before it is modified. */
5706 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
5707 o[i] = TREE_VALUE (tail);
5708
5709 /* Perform default conversions on array and function inputs. */
5710 /* Don't do this for other types--
5711 it would screw up operands expected to be in memory. */
5712 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
5713 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
5714 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
5715 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
5716
5717 /* Generate the ASM_OPERANDS insn;
5718 store into the TREE_VALUEs of OUTPUTS some trees for
5719 where the values were actually stored. */
5720 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
5721
5722 /* Copy all the intermediate outputs into the specified outputs. */
5723 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
5724 {
5725 if (o[i] != TREE_VALUE (tail))
5726 {
5727 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
5728 0, VOIDmode, 0);
5729 free_temp_slots ();
5730 }
5731 /* Detect modification of read-only values.
5732 (Otherwise done by build_modify_expr.) */
5733 else
5734 {
5735 tree type = TREE_TYPE (o[i]);
5736 if (TYPE_READONLY (type)
5737 || ((TREE_CODE (type) == RECORD_TYPE
5738 || TREE_CODE (type) == UNION_TYPE)
5739 && C_TYPE_FIELDS_READONLY (type)))
5740 readonly_warning (o[i], "modification by `asm'");
5741 }
5742 }
5743
5744 /* Those MODIFY_EXPRs could do autoincrements. */
5745 emit_queue ();
5746 }
5747
5748 /* Expand a C `return' statement.
5749 RETVAL is the expression for what to return,
5750 or a null pointer for `return;' with no value. */
5751
5752 void
c_expand_return(retval)5753 c_expand_return (retval)
5754 tree retval;
5755 {
5756 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
5757
5758 if (TREE_THIS_VOLATILE (current_function_decl))
5759 warning ("function declared `volatile' has a `return' statement");
5760
5761 if (!retval)
5762 {
5763 current_function_returns_null = 1;
5764 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
5765 warning ("`return' with no value, in function returning non-void");
5766 expand_null_return ();
5767 }
5768 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
5769 {
5770 current_function_returns_null = 1;
5771 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
5772 pedwarn ("`return' with a value, in function returning void");
5773 expand_return (retval);
5774 }
5775 else
5776 {
5777 tree t = convert_for_assignment (valtype, retval, "return",
5778 NULL_TREE, NULL_TREE, 0);
5779 tree res = DECL_RESULT (current_function_decl);
5780 t = build (MODIFY_EXPR, TREE_TYPE (res),
5781 res, convert (TREE_TYPE (res), t));
5782 expand_return (t);
5783 current_function_returns_value = 1;
5784 }
5785 }
5786
5787 /* Start a C switch statement, testing expression EXP.
5788 Return EXP if it is valid, an error node otherwise. */
5789
5790 tree
c_expand_start_case(exp)5791 c_expand_start_case (exp)
5792 tree exp;
5793 {
5794 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
5795 tree type = TREE_TYPE (exp);
5796
5797 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
5798 {
5799 error ("switch quantity not an integer");
5800 exp = error_mark_node;
5801 }
5802 else
5803 {
5804 tree index;
5805 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
5806
5807 if (warn_traditional
5808 && (type == long_integer_type_node
5809 || type == long_unsigned_type_node))
5810 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
5811
5812 exp = default_conversion (exp);
5813 type = TREE_TYPE (exp);
5814 index = get_unwidened (exp, NULL_TREE);
5815 /* We can't strip a conversion from a signed type to an unsigned,
5816 because if we did, int_fits_type_p would do the wrong thing
5817 when checking case values for being in range,
5818 and it's too hard to do the right thing. */
5819 if (TREE_UNSIGNED (TREE_TYPE (exp))
5820 == TREE_UNSIGNED (TREE_TYPE (index)))
5821 exp = index;
5822 }
5823
5824 expand_start_case (1, exp, type, "switch statement");
5825
5826 return exp;
5827 }
5828