regmove.c (optimize_reg_copy_1): Undo Aug 18 change.
[gcc.git] / gcc / c-typeck.c
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 88, 91-97, 1998 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 /* This file is part of the C front end.
23 It contains routines to build C expressions given their operands,
24 including computing the types of the result, C-specific error checks,
25 and some optimization.
26
27 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
28 and to process initializations in declarations (since they work
29 like a strange sort of assignment). */
30
31 #include "config.h"
32 #include "system.h"
33 #include "tree.h"
34 #include "c-tree.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "rtl.h"
38 #include "expr.h"
39 #include "toplev.h"
40
41 /* Nonzero if we've already printed a "missing braces around initializer"
42 message within this initializer. */
43 static int missing_braces_mentioned;
44
45 static tree qualify_type PROTO((tree, tree));
46 static int comp_target_types PROTO((tree, tree));
47 static int function_types_compatible_p PROTO((tree, tree));
48 static int type_lists_compatible_p PROTO((tree, tree));
49 static int self_promoting_type_p PROTO((tree));
50 static tree decl_constant_value PROTO((tree));
51 static tree lookup_field PROTO((tree, tree, tree *));
52 static tree convert_arguments PROTO((tree, tree, tree, tree));
53 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
54 static tree pointer_diff PROTO((tree, tree));
55 static tree unary_complex_lvalue PROTO((enum tree_code, tree));
56 static void pedantic_lvalue_warning PROTO((enum tree_code));
57 static tree internal_build_compound_expr PROTO((tree, int));
58 static tree convert_for_assignment PROTO((tree, tree, char *, tree,
59 tree, int));
60 static void warn_for_assignment PROTO((char *, char *, tree, int));
61 static tree valid_compound_expr_initializer PROTO((tree, tree));
62 static void push_string PROTO((char *));
63 static void push_member_name PROTO((tree));
64 static void push_array_bounds PROTO((int));
65 static int spelling_length PROTO((void));
66 static char *print_spelling PROTO((char *));
67 static char *get_spelling PROTO((char *));
68 static void warning_init PROTO((char *, char *,
69 char *));
70 static tree digest_init PROTO((tree, tree, int, int));
71 static void check_init_type_bitfields PROTO((tree));
72 static void output_init_element PROTO((tree, tree, tree, int));
73 static void output_pending_init_elements PROTO((int));
74 static void add_pending_init PROTO((tree, tree));
75 static int pending_init_member PROTO((tree));
76 \f
77 /* Do `exp = require_complete_type (exp);' to make sure exp
78 does not have an incomplete type. (That includes void types.) */
79
80 tree
81 require_complete_type (value)
82 tree value;
83 {
84 tree type = TREE_TYPE (value);
85
86 /* First, detect a valid value with a complete type. */
87 if (TYPE_SIZE (type) != 0
88 && type != void_type_node)
89 return value;
90
91 incomplete_type_error (value, type);
92 return error_mark_node;
93 }
94
95 /* Print an error message for invalid use of an incomplete type.
96 VALUE is the expression that was used (or 0 if that isn't known)
97 and TYPE is the type that was invalid. */
98
99 void
100 incomplete_type_error (value, type)
101 tree value;
102 tree type;
103 {
104 char *errmsg;
105
106 /* Avoid duplicate error message. */
107 if (TREE_CODE (type) == ERROR_MARK)
108 return;
109
110 if (value != 0 && (TREE_CODE (value) == VAR_DECL
111 || TREE_CODE (value) == PARM_DECL))
112 error ("`%s' has an incomplete type",
113 IDENTIFIER_POINTER (DECL_NAME (value)));
114 else
115 {
116 retry:
117 /* We must print an error message. Be clever about what it says. */
118
119 switch (TREE_CODE (type))
120 {
121 case RECORD_TYPE:
122 errmsg = "invalid use of undefined type `struct %s'";
123 break;
124
125 case UNION_TYPE:
126 errmsg = "invalid use of undefined type `union %s'";
127 break;
128
129 case ENUMERAL_TYPE:
130 errmsg = "invalid use of undefined type `enum %s'";
131 break;
132
133 case VOID_TYPE:
134 error ("invalid use of void expression");
135 return;
136
137 case ARRAY_TYPE:
138 if (TYPE_DOMAIN (type))
139 {
140 type = TREE_TYPE (type);
141 goto retry;
142 }
143 error ("invalid use of array with unspecified bounds");
144 return;
145
146 default:
147 abort ();
148 }
149
150 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
151 error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
152 else
153 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
154 error ("invalid use of incomplete typedef `%s'",
155 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
156 }
157 }
158
159 /* Return a variant of TYPE which has all the type qualifiers of LIKE
160 as well as those of TYPE. */
161
162 static tree
163 qualify_type (type, like)
164 tree type, like;
165 {
166 return c_build_qualified_type (type, TYPE_QUALS (like));
167 }
168 \f
169 /* Return the common type of two types.
170 We assume that comptypes has already been done and returned 1;
171 if that isn't so, this may crash. In particular, we assume that qualifiers
172 match.
173
174 This is the type for the result of most arithmetic operations
175 if the operands have the given two types. */
176
177 tree
178 common_type (t1, t2)
179 tree t1, t2;
180 {
181 register enum tree_code code1;
182 register enum tree_code code2;
183 tree attributes;
184
185 /* Save time if the two types are the same. */
186
187 if (t1 == t2) return t1;
188
189 /* If one type is nonsense, use the other. */
190 if (t1 == error_mark_node)
191 return t2;
192 if (t2 == error_mark_node)
193 return t1;
194
195 /* Merge the attributes. */
196 attributes = merge_machine_type_attributes (t1, t2);
197
198 /* Treat an enum type as the unsigned integer type of the same width. */
199
200 if (TREE_CODE (t1) == ENUMERAL_TYPE)
201 t1 = type_for_size (TYPE_PRECISION (t1), 1);
202 if (TREE_CODE (t2) == ENUMERAL_TYPE)
203 t2 = type_for_size (TYPE_PRECISION (t2), 1);
204
205 code1 = TREE_CODE (t1);
206 code2 = TREE_CODE (t2);
207
208 /* If one type is complex, form the common type of the non-complex
209 components, then make that complex. Use T1 or T2 if it is the
210 required type. */
211 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
212 {
213 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
214 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
215 tree subtype = common_type (subtype1, subtype2);
216
217 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
218 return build_type_attribute_variant (t1, attributes);
219 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
220 return build_type_attribute_variant (t2, attributes);
221 else
222 return build_type_attribute_variant (build_complex_type (subtype),
223 attributes);
224 }
225
226 switch (code1)
227 {
228 case INTEGER_TYPE:
229 case REAL_TYPE:
230 /* If only one is real, use it as the result. */
231
232 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
233 return build_type_attribute_variant (t1, attributes);
234
235 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
236 return build_type_attribute_variant (t2, attributes);
237
238 /* Both real or both integers; use the one with greater precision. */
239
240 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
241 return build_type_attribute_variant (t1, attributes);
242 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
243 return build_type_attribute_variant (t2, attributes);
244
245 /* Same precision. Prefer longs to ints even when same size. */
246
247 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
248 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
249 return build_type_attribute_variant (long_unsigned_type_node,
250 attributes);
251
252 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
253 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
254 {
255 /* But preserve unsignedness from the other type,
256 since long cannot hold all the values of an unsigned int. */
257 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
258 t1 = long_unsigned_type_node;
259 else
260 t1 = long_integer_type_node;
261 return build_type_attribute_variant (t1, attributes);
262 }
263
264 /* Likewise, prefer long double to double even if same size. */
265 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
266 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
267 return build_type_attribute_variant (long_double_type_node,
268 attributes);
269
270 /* Otherwise prefer the unsigned one. */
271
272 if (TREE_UNSIGNED (t1))
273 return build_type_attribute_variant (t1, attributes);
274 else
275 return build_type_attribute_variant (t2, attributes);
276
277 case POINTER_TYPE:
278 /* For two pointers, do this recursively on the target type,
279 and combine the qualifiers of the two types' targets. */
280 /* This code was turned off; I don't know why.
281 But ANSI C specifies doing this with the qualifiers.
282 So I turned it on again. */
283 {
284 tree pointed_to_1 = TREE_TYPE (t1);
285 tree pointed_to_2 = TREE_TYPE (t2);
286 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
287 TYPE_MAIN_VARIANT (pointed_to_2));
288 t1 = build_pointer_type (c_build_qualified_type
289 (target,
290 TYPE_QUALS (pointed_to_1) |
291 TYPE_QUALS (pointed_to_2)));
292 return build_type_attribute_variant (t1, attributes);
293 }
294 #if 0
295 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
296 return build_type_attribute_variant (t1, attributes);
297 #endif
298
299 case ARRAY_TYPE:
300 {
301 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
302 /* Save space: see if the result is identical to one of the args. */
303 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
304 return build_type_attribute_variant (t1, attributes);
305 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
306 return build_type_attribute_variant (t2, attributes);
307 /* Merge the element types, and have a size if either arg has one. */
308 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
309 return build_type_attribute_variant (t1, attributes);
310 }
311
312 case FUNCTION_TYPE:
313 /* Function types: prefer the one that specified arg types.
314 If both do, merge the arg types. Also merge the return types. */
315 {
316 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
317 tree p1 = TYPE_ARG_TYPES (t1);
318 tree p2 = TYPE_ARG_TYPES (t2);
319 int len;
320 tree newargs, n;
321 int i;
322
323 /* Save space: see if the result is identical to one of the args. */
324 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
325 return build_type_attribute_variant (t1, attributes);
326 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
327 return build_type_attribute_variant (t2, attributes);
328
329 /* Simple way if one arg fails to specify argument types. */
330 if (TYPE_ARG_TYPES (t1) == 0)
331 {
332 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
333 return build_type_attribute_variant (t1, attributes);
334 }
335 if (TYPE_ARG_TYPES (t2) == 0)
336 {
337 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
338 return build_type_attribute_variant (t1, attributes);
339 }
340
341 /* If both args specify argument types, we must merge the two
342 lists, argument by argument. */
343
344 len = list_length (p1);
345 newargs = 0;
346
347 for (i = 0; i < len; i++)
348 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
349
350 n = newargs;
351
352 for (; p1;
353 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
354 {
355 /* A null type means arg type is not specified.
356 Take whatever the other function type has. */
357 if (TREE_VALUE (p1) == 0)
358 {
359 TREE_VALUE (n) = TREE_VALUE (p2);
360 goto parm_done;
361 }
362 if (TREE_VALUE (p2) == 0)
363 {
364 TREE_VALUE (n) = TREE_VALUE (p1);
365 goto parm_done;
366 }
367
368 /* Given wait (union {union wait *u; int *i} *)
369 and wait (union wait *),
370 prefer union wait * as type of parm. */
371 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
372 && TREE_VALUE (p1) != TREE_VALUE (p2))
373 {
374 tree memb;
375 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
376 memb; memb = TREE_CHAIN (memb))
377 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
378 {
379 TREE_VALUE (n) = TREE_VALUE (p2);
380 if (pedantic)
381 pedwarn ("function types not truly compatible in ANSI C");
382 goto parm_done;
383 }
384 }
385 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
386 && TREE_VALUE (p2) != TREE_VALUE (p1))
387 {
388 tree memb;
389 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
390 memb; memb = TREE_CHAIN (memb))
391 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
392 {
393 TREE_VALUE (n) = TREE_VALUE (p1);
394 if (pedantic)
395 pedwarn ("function types not truly compatible in ANSI C");
396 goto parm_done;
397 }
398 }
399 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
400 parm_done: ;
401 }
402
403 t1 = build_function_type (valtype, newargs);
404 /* ... falls through ... */
405 }
406
407 default:
408 return build_type_attribute_variant (t1, attributes);
409 }
410
411 }
412 \f
413 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
414 or various other operations. Return 2 if they are compatible
415 but a warning may be needed if you use them together. */
416
417 int
418 comptypes (type1, type2)
419 tree type1, type2;
420 {
421 register tree t1 = type1;
422 register tree t2 = type2;
423 int attrval, val;
424
425 /* Suppress errors caused by previously reported errors. */
426
427 if (t1 == t2 || !t1 || !t2
428 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
429 return 1;
430
431 /* Treat an enum type as the integer type of the same width and
432 signedness. */
433
434 if (TREE_CODE (t1) == ENUMERAL_TYPE)
435 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
436 if (TREE_CODE (t2) == ENUMERAL_TYPE)
437 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
438
439 if (t1 == t2)
440 return 1;
441
442 /* Different classes of types can't be compatible. */
443
444 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
445
446 /* Qualifiers must match. */
447
448 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
449 return 0;
450
451 /* Allow for two different type nodes which have essentially the same
452 definition. Note that we already checked for equality of the type
453 qualifiers (just above). */
454
455 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
456 return 1;
457
458 #ifndef COMP_TYPE_ATTRIBUTES
459 #define COMP_TYPE_ATTRIBUTES(t1,t2) 1
460 #endif
461
462 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
463 if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
464 return 0;
465
466 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
467 val = 0;
468
469 switch (TREE_CODE (t1))
470 {
471 case POINTER_TYPE:
472 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
473 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
474 break;
475
476 case FUNCTION_TYPE:
477 val = function_types_compatible_p (t1, t2);
478 break;
479
480 case ARRAY_TYPE:
481 {
482 tree d1 = TYPE_DOMAIN (t1);
483 tree d2 = TYPE_DOMAIN (t2);
484 val = 1;
485
486 /* Target types must match incl. qualifiers. */
487 if (TREE_TYPE (t1) != TREE_TYPE (t2)
488 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
489 return 0;
490
491 /* Sizes must match unless one is missing or variable. */
492 if (d1 == 0 || d2 == 0 || d1 == d2
493 || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
494 || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
495 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
496 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
497 break;
498
499 if (! ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
500 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
501 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
502 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
503 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
504 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
505 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
506 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2)))))
507 val = 0;
508 break;
509 }
510
511 case RECORD_TYPE:
512 if (maybe_objc_comptypes (t1, t2, 0) == 1)
513 val = 1;
514 break;
515
516 default:
517 break;
518 }
519 return attrval == 2 && val == 1 ? 2 : val;
520 }
521
522 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
523 ignoring their qualifiers. */
524
525 static int
526 comp_target_types (ttl, ttr)
527 tree ttl, ttr;
528 {
529 int val;
530
531 /* Give maybe_objc_comptypes a crack at letting these types through. */
532 if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0)
533 return val;
534
535 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
536 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
537
538 if (val == 2 && pedantic)
539 pedwarn ("types are not quite compatible");
540 return val;
541 }
542 \f
543 /* Subroutines of `comptypes'. */
544
545 /* Return 1 if two function types F1 and F2 are compatible.
546 If either type specifies no argument types,
547 the other must specify a fixed number of self-promoting arg types.
548 Otherwise, if one type specifies only the number of arguments,
549 the other must specify that number of self-promoting arg types.
550 Otherwise, the argument types must match. */
551
552 static int
553 function_types_compatible_p (f1, f2)
554 tree f1, f2;
555 {
556 tree args1, args2;
557 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
558 int val = 1;
559 int val1;
560
561 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
562 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
563 return 0;
564
565 args1 = TYPE_ARG_TYPES (f1);
566 args2 = TYPE_ARG_TYPES (f2);
567
568 /* An unspecified parmlist matches any specified parmlist
569 whose argument types don't need default promotions. */
570
571 if (args1 == 0)
572 {
573 if (!self_promoting_args_p (args2))
574 return 0;
575 /* If one of these types comes from a non-prototype fn definition,
576 compare that with the other type's arglist.
577 If they don't match, ask for a warning (but no error). */
578 if (TYPE_ACTUAL_ARG_TYPES (f1)
579 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
580 val = 2;
581 return val;
582 }
583 if (args2 == 0)
584 {
585 if (!self_promoting_args_p (args1))
586 return 0;
587 if (TYPE_ACTUAL_ARG_TYPES (f2)
588 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
589 val = 2;
590 return val;
591 }
592
593 /* Both types have argument lists: compare them and propagate results. */
594 val1 = type_lists_compatible_p (args1, args2);
595 return val1 != 1 ? val1 : val;
596 }
597
598 /* Check two lists of types for compatibility,
599 returning 0 for incompatible, 1 for compatible,
600 or 2 for compatible with warning. */
601
602 static int
603 type_lists_compatible_p (args1, args2)
604 tree args1, args2;
605 {
606 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
607 int val = 1;
608 int newval = 0;
609
610 while (1)
611 {
612 if (args1 == 0 && args2 == 0)
613 return val;
614 /* If one list is shorter than the other,
615 they fail to match. */
616 if (args1 == 0 || args2 == 0)
617 return 0;
618 /* A null pointer instead of a type
619 means there is supposed to be an argument
620 but nothing is specified about what type it has.
621 So match anything that self-promotes. */
622 if (TREE_VALUE (args1) == 0)
623 {
624 if (! self_promoting_type_p (TREE_VALUE (args2)))
625 return 0;
626 }
627 else if (TREE_VALUE (args2) == 0)
628 {
629 if (! self_promoting_type_p (TREE_VALUE (args1)))
630 return 0;
631 }
632 else if (! (newval = comptypes (TREE_VALUE (args1), TREE_VALUE (args2))))
633 {
634 /* Allow wait (union {union wait *u; int *i} *)
635 and wait (union wait *) to be compatible. */
636 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
637 && (TYPE_NAME (TREE_VALUE (args1)) == 0
638 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
639 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
640 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
641 TYPE_SIZE (TREE_VALUE (args2))))
642 {
643 tree memb;
644 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
645 memb; memb = TREE_CHAIN (memb))
646 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
647 break;
648 if (memb == 0)
649 return 0;
650 }
651 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
652 && (TYPE_NAME (TREE_VALUE (args2)) == 0
653 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
654 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
655 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
656 TYPE_SIZE (TREE_VALUE (args1))))
657 {
658 tree memb;
659 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
660 memb; memb = TREE_CHAIN (memb))
661 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
662 break;
663 if (memb == 0)
664 return 0;
665 }
666 else
667 return 0;
668 }
669
670 /* comptypes said ok, but record if it said to warn. */
671 if (newval > val)
672 val = newval;
673
674 args1 = TREE_CHAIN (args1);
675 args2 = TREE_CHAIN (args2);
676 }
677 }
678
679 /* Return 1 if PARMS specifies a fixed number of parameters
680 and none of their types is affected by default promotions. */
681
682 int
683 self_promoting_args_p (parms)
684 tree parms;
685 {
686 register tree t;
687 for (t = parms; t; t = TREE_CHAIN (t))
688 {
689 register tree type = TREE_VALUE (t);
690
691 if (TREE_CHAIN (t) == 0 && type != void_type_node)
692 return 0;
693
694 if (type == 0)
695 return 0;
696
697 if (TYPE_MAIN_VARIANT (type) == float_type_node)
698 return 0;
699
700 if (C_PROMOTING_INTEGER_TYPE_P (type))
701 return 0;
702 }
703 return 1;
704 }
705
706 /* Return 1 if TYPE is not affected by default promotions. */
707
708 static int
709 self_promoting_type_p (type)
710 tree type;
711 {
712 if (TYPE_MAIN_VARIANT (type) == float_type_node)
713 return 0;
714
715 if (C_PROMOTING_INTEGER_TYPE_P (type))
716 return 0;
717
718 return 1;
719 }
720 \f
721 /* Return an unsigned type the same as TYPE in other respects. */
722
723 tree
724 unsigned_type (type)
725 tree type;
726 {
727 tree type1 = TYPE_MAIN_VARIANT (type);
728 if (type1 == signed_char_type_node || type1 == char_type_node)
729 return unsigned_char_type_node;
730 if (type1 == integer_type_node)
731 return unsigned_type_node;
732 if (type1 == short_integer_type_node)
733 return short_unsigned_type_node;
734 if (type1 == long_integer_type_node)
735 return long_unsigned_type_node;
736 if (type1 == long_long_integer_type_node)
737 return long_long_unsigned_type_node;
738 if (type1 == intDI_type_node)
739 return unsigned_intDI_type_node;
740 if (type1 == intSI_type_node)
741 return unsigned_intSI_type_node;
742 if (type1 == intHI_type_node)
743 return unsigned_intHI_type_node;
744 if (type1 == intQI_type_node)
745 return unsigned_intQI_type_node;
746
747 return signed_or_unsigned_type (1, type);
748 }
749
750 /* Return a signed type the same as TYPE in other respects. */
751
752 tree
753 signed_type (type)
754 tree type;
755 {
756 tree type1 = TYPE_MAIN_VARIANT (type);
757 if (type1 == unsigned_char_type_node || type1 == char_type_node)
758 return signed_char_type_node;
759 if (type1 == unsigned_type_node)
760 return integer_type_node;
761 if (type1 == short_unsigned_type_node)
762 return short_integer_type_node;
763 if (type1 == long_unsigned_type_node)
764 return long_integer_type_node;
765 if (type1 == long_long_unsigned_type_node)
766 return long_long_integer_type_node;
767 if (type1 == unsigned_intDI_type_node)
768 return intDI_type_node;
769 if (type1 == unsigned_intSI_type_node)
770 return intSI_type_node;
771 if (type1 == unsigned_intHI_type_node)
772 return intHI_type_node;
773 if (type1 == unsigned_intQI_type_node)
774 return intQI_type_node;
775
776 return signed_or_unsigned_type (0, type);
777 }
778
779 /* Return a type the same as TYPE except unsigned or
780 signed according to UNSIGNEDP. */
781
782 tree
783 signed_or_unsigned_type (unsignedp, type)
784 int unsignedp;
785 tree type;
786 {
787 if ((! INTEGRAL_TYPE_P (type) && ! POINTER_TYPE_P (type))
788 || TREE_UNSIGNED (type) == unsignedp)
789 return type;
790 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
791 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
792 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
793 return unsignedp ? unsigned_type_node : integer_type_node;
794 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
795 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
796 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
797 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
798 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
799 return (unsignedp ? long_long_unsigned_type_node
800 : long_long_integer_type_node);
801 return type;
802 }
803
804 /* Compute the value of the `sizeof' operator. */
805
806 tree
807 c_sizeof (type)
808 tree type;
809 {
810 enum tree_code code = TREE_CODE (type);
811 tree t;
812
813 if (code == FUNCTION_TYPE)
814 {
815 if (pedantic || warn_pointer_arith)
816 pedwarn ("sizeof applied to a function type");
817 return size_int (1);
818 }
819 if (code == VOID_TYPE)
820 {
821 if (pedantic || warn_pointer_arith)
822 pedwarn ("sizeof applied to a void type");
823 return size_int (1);
824 }
825 if (code == ERROR_MARK)
826 return size_int (1);
827 if (TYPE_SIZE (type) == 0)
828 {
829 error ("sizeof applied to an incomplete type");
830 return size_int (0);
831 }
832
833 /* Convert in case a char is more than one unit. */
834 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
835 size_int (TYPE_PRECISION (char_type_node)));
836 t = convert (sizetype, t);
837 /* size_binop does not put the constant in range, so do it now. */
838 if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
839 TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
840 return t;
841 }
842
843 tree
844 c_sizeof_nowarn (type)
845 tree type;
846 {
847 enum tree_code code = TREE_CODE (type);
848 tree t;
849
850 if (code == FUNCTION_TYPE
851 || code == VOID_TYPE
852 || code == ERROR_MARK)
853 return size_int (1);
854 if (TYPE_SIZE (type) == 0)
855 return size_int (0);
856
857 /* Convert in case a char is more than one unit. */
858 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
859 size_int (TYPE_PRECISION (char_type_node)));
860 t = convert (sizetype, t);
861 force_fit_type (t, 0);
862 return t;
863 }
864
865 /* Compute the size to increment a pointer by. */
866
867 tree
868 c_size_in_bytes (type)
869 tree type;
870 {
871 enum tree_code code = TREE_CODE (type);
872 tree t;
873
874 if (code == FUNCTION_TYPE)
875 return size_int (1);
876 if (code == VOID_TYPE)
877 return size_int (1);
878 if (code == ERROR_MARK)
879 return size_int (1);
880 if (TYPE_SIZE (type) == 0)
881 {
882 error ("arithmetic on pointer to an incomplete type");
883 return size_int (1);
884 }
885
886 /* Convert in case a char is more than one unit. */
887 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
888 size_int (BITS_PER_UNIT));
889 t = convert (sizetype, t);
890 force_fit_type (t, 0);
891 return t;
892 }
893
894 /* Implement the __alignof keyword: Return the minimum required
895 alignment of TYPE, measured in bytes. */
896
897 tree
898 c_alignof (type)
899 tree type;
900 {
901 enum tree_code code = TREE_CODE (type);
902
903 if (code == FUNCTION_TYPE)
904 return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
905
906 if (code == VOID_TYPE || code == ERROR_MARK)
907 return size_int (1);
908
909 return size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
910 }
911 \f
912 /* Implement the __alignof keyword: Return the minimum required
913 alignment of EXPR, measured in bytes. For VAR_DECL's and
914 FIELD_DECL's return DECL_ALIGN (which can be set from an
915 "aligned" __attribute__ specification). */
916
917 tree
918 c_alignof_expr (expr)
919 tree expr;
920 {
921 if (TREE_CODE (expr) == VAR_DECL)
922 return size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
923
924 if (TREE_CODE (expr) == COMPONENT_REF
925 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
926 {
927 error ("`__alignof' applied to a bit-field");
928 return size_int (1);
929 }
930 else if (TREE_CODE (expr) == COMPONENT_REF
931 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
932 return size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
933
934 if (TREE_CODE (expr) == INDIRECT_REF)
935 {
936 tree t = TREE_OPERAND (expr, 0);
937 tree best = t;
938 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
939
940 while (TREE_CODE (t) == NOP_EXPR
941 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
942 {
943 int thisalign;
944
945 t = TREE_OPERAND (t, 0);
946 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
947 if (thisalign > bestalign)
948 best = t, bestalign = thisalign;
949 }
950 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
951 }
952 else
953 return c_alignof (TREE_TYPE (expr));
954 }
955
956 /* Return either DECL or its known constant value (if it has one). */
957
958 static tree
959 decl_constant_value (decl)
960 tree decl;
961 {
962 if (/* Don't change a variable array bound or initial value to a constant
963 in a place where a variable is invalid. */
964 current_function_decl != 0
965 && ! pedantic
966 && ! TREE_THIS_VOLATILE (decl)
967 && TREE_READONLY (decl) && ! ITERATOR_P (decl)
968 && DECL_INITIAL (decl) != 0
969 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
970 /* This is invalid if initial value is not constant.
971 If it has either a function call, a memory reference,
972 or a variable, then re-evaluating it could give different results. */
973 && TREE_CONSTANT (DECL_INITIAL (decl))
974 /* Check for cases where this is sub-optimal, even though valid. */
975 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
976 && DECL_MODE (decl) != BLKmode)
977 return DECL_INITIAL (decl);
978 return decl;
979 }
980
981 /* Perform default promotions for C data used in expressions.
982 Arrays and functions are converted to pointers;
983 enumeral types or short or char, to int.
984 In addition, manifest constants symbols are replaced by their values. */
985
986 tree
987 default_conversion (exp)
988 tree exp;
989 {
990 register tree type = TREE_TYPE (exp);
991 register enum tree_code code = TREE_CODE (type);
992
993 /* Constants can be used directly unless they're not loadable. */
994 if (TREE_CODE (exp) == CONST_DECL)
995 exp = DECL_INITIAL (exp);
996
997 /* Replace a nonvolatile const static variable with its value unless
998 it is an array, in which case we must be sure that taking the
999 address of the array produces consistent results. */
1000 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1001 {
1002 exp = decl_constant_value (exp);
1003 type = TREE_TYPE (exp);
1004 }
1005
1006 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1007 an lvalue. */
1008 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
1009 to integer and cause infinite recursion. */
1010 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1011 || (TREE_CODE (exp) == NOP_EXPR
1012 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1013 exp = TREE_OPERAND (exp, 0);
1014
1015 /* Normally convert enums to int,
1016 but convert wide enums to something wider. */
1017 if (code == ENUMERAL_TYPE)
1018 {
1019 type = type_for_size (MAX (TYPE_PRECISION (type),
1020 TYPE_PRECISION (integer_type_node)),
1021 ((flag_traditional
1022 || (TYPE_PRECISION (type)
1023 >= TYPE_PRECISION (integer_type_node)))
1024 && TREE_UNSIGNED (type)));
1025 return convert (type, exp);
1026 }
1027
1028 if (TREE_CODE (exp) == COMPONENT_REF
1029 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
1030 {
1031 tree width = DECL_SIZE (TREE_OPERAND (exp, 1));
1032 HOST_WIDE_INT low = TREE_INT_CST_LOW (width);
1033
1034 /* If it's thinner than an int, promote it like a
1035 C_PROMOTING_INTEGER_TYPE_P, otherwise leave it alone. */
1036
1037 if (low < TYPE_PRECISION (integer_type_node))
1038 {
1039 if (flag_traditional && TREE_UNSIGNED (type))
1040 return convert (unsigned_type_node, exp);
1041 else
1042 return convert (integer_type_node, exp);
1043 }
1044 }
1045
1046 if (C_PROMOTING_INTEGER_TYPE_P (type))
1047 {
1048 /* Traditionally, unsignedness is preserved in default promotions.
1049 Also preserve unsignedness if not really getting any wider. */
1050 if (TREE_UNSIGNED (type)
1051 && (flag_traditional
1052 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1053 return convert (unsigned_type_node, exp);
1054 return convert (integer_type_node, exp);
1055 }
1056 if (flag_traditional && !flag_allow_single_precision
1057 && TYPE_MAIN_VARIANT (type) == float_type_node)
1058 return convert (double_type_node, exp);
1059 if (code == VOID_TYPE)
1060 {
1061 error ("void value not ignored as it ought to be");
1062 return error_mark_node;
1063 }
1064 if (code == FUNCTION_TYPE)
1065 {
1066 return build_unary_op (ADDR_EXPR, exp, 0);
1067 }
1068 if (code == ARRAY_TYPE)
1069 {
1070 register tree adr;
1071 tree restype = TREE_TYPE (type);
1072 tree ptrtype;
1073 int constp = 0;
1074 int volatilep = 0;
1075
1076 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1077 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1078 {
1079 constp = TREE_READONLY (exp);
1080 volatilep = TREE_THIS_VOLATILE (exp);
1081 }
1082
1083 if (TYPE_QUALS (type) || constp || volatilep)
1084 restype
1085 = c_build_qualified_type (restype,
1086 TYPE_QUALS (type)
1087 | (constp * TYPE_QUAL_CONST)
1088 | (volatilep * TYPE_QUAL_VOLATILE));
1089
1090 if (TREE_CODE (exp) == INDIRECT_REF)
1091 return convert (TYPE_POINTER_TO (restype),
1092 TREE_OPERAND (exp, 0));
1093
1094 if (TREE_CODE (exp) == COMPOUND_EXPR)
1095 {
1096 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1097 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1098 TREE_OPERAND (exp, 0), op1);
1099 }
1100
1101 if (! lvalue_p (exp)
1102 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1103 {
1104 error ("invalid use of non-lvalue array");
1105 return error_mark_node;
1106 }
1107
1108 ptrtype = build_pointer_type (restype);
1109
1110 if (TREE_CODE (exp) == VAR_DECL)
1111 {
1112 /* ??? This is not really quite correct
1113 in that the type of the operand of ADDR_EXPR
1114 is not the target type of the type of the ADDR_EXPR itself.
1115 Question is, can this lossage be avoided? */
1116 adr = build1 (ADDR_EXPR, ptrtype, exp);
1117 if (mark_addressable (exp) == 0)
1118 return error_mark_node;
1119 TREE_CONSTANT (adr) = staticp (exp);
1120 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1121 return adr;
1122 }
1123 /* This way is better for a COMPONENT_REF since it can
1124 simplify the offset for a component. */
1125 adr = build_unary_op (ADDR_EXPR, exp, 1);
1126 return convert (ptrtype, adr);
1127 }
1128 return exp;
1129 }
1130 \f
1131 /* Look up component name in the structure type definition.
1132
1133 If this component name is found indirectly within an anonymous union,
1134 store in *INDIRECT the component which directly contains
1135 that anonymous union. Otherwise, set *INDIRECT to 0. */
1136
1137 static tree
1138 lookup_field (type, component, indirect)
1139 tree type, component;
1140 tree *indirect;
1141 {
1142 tree field;
1143
1144 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1145 to the field elements. Use a binary search on this array to quickly
1146 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1147 will always be set for structures which have many elements. */
1148
1149 if (TYPE_LANG_SPECIFIC (type))
1150 {
1151 int bot, top, half;
1152 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1153
1154 field = TYPE_FIELDS (type);
1155 bot = 0;
1156 top = TYPE_LANG_SPECIFIC (type)->len;
1157 while (top - bot > 1)
1158 {
1159 half = (top - bot + 1) >> 1;
1160 field = field_array[bot+half];
1161
1162 if (DECL_NAME (field) == NULL_TREE)
1163 {
1164 /* Step through all anon unions in linear fashion. */
1165 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1166 {
1167 tree anon = 0, junk;
1168
1169 field = field_array[bot++];
1170 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1171 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1172 anon = lookup_field (TREE_TYPE (field), component, &junk);
1173
1174 if (anon != NULL_TREE)
1175 {
1176 *indirect = field;
1177 return anon;
1178 }
1179 }
1180
1181 /* Entire record is only anon unions. */
1182 if (bot > top)
1183 return NULL_TREE;
1184
1185 /* Restart the binary search, with new lower bound. */
1186 continue;
1187 }
1188
1189 if (DECL_NAME (field) == component)
1190 break;
1191 if (DECL_NAME (field) < component)
1192 bot += half;
1193 else
1194 top = bot + half;
1195 }
1196
1197 if (DECL_NAME (field_array[bot]) == component)
1198 field = field_array[bot];
1199 else if (DECL_NAME (field) != component)
1200 field = 0;
1201 }
1202 else
1203 {
1204 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1205 {
1206 if (DECL_NAME (field) == NULL_TREE)
1207 {
1208 tree junk;
1209 tree anon = 0;
1210
1211 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1212 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1213 anon = lookup_field (TREE_TYPE (field), component, &junk);
1214
1215 if (anon != NULL_TREE)
1216 {
1217 *indirect = field;
1218 return anon;
1219 }
1220 }
1221
1222 if (DECL_NAME (field) == component)
1223 break;
1224 }
1225 }
1226
1227 *indirect = NULL_TREE;
1228 return field;
1229 }
1230
1231 /* Make an expression to refer to the COMPONENT field of
1232 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1233
1234 tree
1235 build_component_ref (datum, component)
1236 tree datum, component;
1237 {
1238 register tree type = TREE_TYPE (datum);
1239 register enum tree_code code = TREE_CODE (type);
1240 register tree field = NULL;
1241 register tree ref;
1242
1243 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1244 unless we are not to support things not strictly ANSI. */
1245 switch (TREE_CODE (datum))
1246 {
1247 case COMPOUND_EXPR:
1248 {
1249 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1250 return build (COMPOUND_EXPR, TREE_TYPE (value),
1251 TREE_OPERAND (datum, 0), value);
1252 }
1253 case COND_EXPR:
1254 return build_conditional_expr
1255 (TREE_OPERAND (datum, 0),
1256 build_component_ref (TREE_OPERAND (datum, 1), component),
1257 build_component_ref (TREE_OPERAND (datum, 2), component));
1258
1259 default:
1260 break;
1261 }
1262
1263 /* See if there is a field or component with name COMPONENT. */
1264
1265 if (code == RECORD_TYPE || code == UNION_TYPE)
1266 {
1267 tree indirect = 0;
1268
1269 if (TYPE_SIZE (type) == 0)
1270 {
1271 incomplete_type_error (NULL_TREE, type);
1272 return error_mark_node;
1273 }
1274
1275 field = lookup_field (type, component, &indirect);
1276
1277 if (!field)
1278 {
1279 error (code == RECORD_TYPE
1280 ? "structure has no member named `%s'"
1281 : "union has no member named `%s'",
1282 IDENTIFIER_POINTER (component));
1283 return error_mark_node;
1284 }
1285 if (TREE_TYPE (field) == error_mark_node)
1286 return error_mark_node;
1287
1288 /* If FIELD was found buried within an anonymous union,
1289 make one COMPONENT_REF to get that anonymous union,
1290 then fall thru to make a second COMPONENT_REF to get FIELD. */
1291 if (indirect != 0)
1292 {
1293 ref = build (COMPONENT_REF, TREE_TYPE (indirect), datum, indirect);
1294 if (TREE_READONLY (datum) || TREE_READONLY (indirect))
1295 TREE_READONLY (ref) = 1;
1296 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (indirect))
1297 TREE_THIS_VOLATILE (ref) = 1;
1298 datum = ref;
1299 }
1300
1301 ref = build (COMPONENT_REF, TREE_TYPE (field), datum, field);
1302
1303 if (TREE_READONLY (datum) || TREE_READONLY (field))
1304 TREE_READONLY (ref) = 1;
1305 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
1306 TREE_THIS_VOLATILE (ref) = 1;
1307
1308 return ref;
1309 }
1310 else if (code != ERROR_MARK)
1311 error ("request for member `%s' in something not a structure or union",
1312 IDENTIFIER_POINTER (component));
1313
1314 return error_mark_node;
1315 }
1316 \f
1317 /* Given an expression PTR for a pointer, return an expression
1318 for the value pointed to.
1319 ERRORSTRING is the name of the operator to appear in error messages. */
1320
1321 tree
1322 build_indirect_ref (ptr, errorstring)
1323 tree ptr;
1324 char *errorstring;
1325 {
1326 register tree pointer = default_conversion (ptr);
1327 register tree type = TREE_TYPE (pointer);
1328
1329 if (TREE_CODE (type) == POINTER_TYPE)
1330 {
1331 if (TREE_CODE (pointer) == ADDR_EXPR
1332 && !flag_volatile
1333 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1334 == TREE_TYPE (type)))
1335 return TREE_OPERAND (pointer, 0);
1336 else
1337 {
1338 tree t = TREE_TYPE (type);
1339 register tree ref = build1 (INDIRECT_REF,
1340 TYPE_MAIN_VARIANT (t), pointer);
1341
1342 if (TYPE_SIZE (t) == 0 && TREE_CODE (t) != ARRAY_TYPE)
1343 {
1344 error ("dereferencing pointer to incomplete type");
1345 return error_mark_node;
1346 }
1347 if (TREE_CODE (t) == VOID_TYPE && skip_evaluation == 0)
1348 warning ("dereferencing `void *' pointer");
1349
1350 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1351 so that we get the proper error message if the result is used
1352 to assign to. Also, &* is supposed to be a no-op.
1353 And ANSI C seems to specify that the type of the result
1354 should be the const type. */
1355 /* A de-reference of a pointer to const is not a const. It is valid
1356 to change it via some other pointer. */
1357 TREE_READONLY (ref) = TYPE_READONLY (t);
1358 TREE_SIDE_EFFECTS (ref)
1359 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1360 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1361 return ref;
1362 }
1363 }
1364 else if (TREE_CODE (pointer) != ERROR_MARK)
1365 error ("invalid type argument of `%s'", errorstring);
1366 return error_mark_node;
1367 }
1368
1369 /* This handles expressions of the form "a[i]", which denotes
1370 an array reference.
1371
1372 This is logically equivalent in C to *(a+i), but we may do it differently.
1373 If A is a variable or a member, we generate a primitive ARRAY_REF.
1374 This avoids forcing the array out of registers, and can work on
1375 arrays that are not lvalues (for example, members of structures returned
1376 by functions). */
1377
1378 tree
1379 build_array_ref (array, index)
1380 tree array, index;
1381 {
1382 if (index == 0)
1383 {
1384 error ("subscript missing in array reference");
1385 return error_mark_node;
1386 }
1387
1388 if (TREE_TYPE (array) == error_mark_node
1389 || TREE_TYPE (index) == error_mark_node)
1390 return error_mark_node;
1391
1392 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1393 && TREE_CODE (array) != INDIRECT_REF)
1394 {
1395 tree rval, type;
1396
1397 /* Subscripting with type char is likely to lose
1398 on a machine where chars are signed.
1399 So warn on any machine, but optionally.
1400 Don't warn for unsigned char since that type is safe.
1401 Don't warn for signed char because anyone who uses that
1402 must have done so deliberately. */
1403 if (warn_char_subscripts
1404 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1405 warning ("array subscript has type `char'");
1406
1407 /* Apply default promotions *after* noticing character types. */
1408 index = default_conversion (index);
1409
1410 /* Require integer *after* promotion, for sake of enums. */
1411 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1412 {
1413 error ("array subscript is not an integer");
1414 return error_mark_node;
1415 }
1416
1417 /* An array that is indexed by a non-constant
1418 cannot be stored in a register; we must be able to do
1419 address arithmetic on its address.
1420 Likewise an array of elements of variable size. */
1421 if (TREE_CODE (index) != INTEGER_CST
1422 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
1423 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1424 {
1425 if (mark_addressable (array) == 0)
1426 return error_mark_node;
1427 }
1428 /* An array that is indexed by a constant value which is not within
1429 the array bounds cannot be stored in a register either; because we
1430 would get a crash in store_bit_field/extract_bit_field when trying
1431 to access a non-existent part of the register. */
1432 if (TREE_CODE (index) == INTEGER_CST
1433 && TYPE_VALUES (TREE_TYPE (array))
1434 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1435 {
1436 if (mark_addressable (array) == 0)
1437 return error_mark_node;
1438 }
1439
1440 if (pedantic && !lvalue_p (array))
1441 {
1442 if (DECL_REGISTER (array))
1443 pedwarn ("ANSI C forbids subscripting `register' array");
1444 else
1445 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1446 }
1447
1448 if (pedantic)
1449 {
1450 tree foo = array;
1451 while (TREE_CODE (foo) == COMPONENT_REF)
1452 foo = TREE_OPERAND (foo, 0);
1453 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1454 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1455 }
1456
1457 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1458 rval = build (ARRAY_REF, type, array, index);
1459 /* Array ref is const/volatile if the array elements are
1460 or if the array is. */
1461 TREE_READONLY (rval)
1462 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1463 | TREE_READONLY (array));
1464 TREE_SIDE_EFFECTS (rval)
1465 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1466 | TREE_SIDE_EFFECTS (array));
1467 TREE_THIS_VOLATILE (rval)
1468 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1469 /* This was added by rms on 16 Nov 91.
1470 It fixes vol struct foo *a; a->elts[1]
1471 in an inline function.
1472 Hope it doesn't break something else. */
1473 | TREE_THIS_VOLATILE (array));
1474 return require_complete_type (fold (rval));
1475 }
1476
1477 {
1478 tree ar = default_conversion (array);
1479 tree ind = default_conversion (index);
1480
1481 /* Do the same warning check as above, but only on the part that's
1482 syntactically the index and only if it is also semantically
1483 the index. */
1484 if (warn_char_subscripts
1485 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1486 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1487 warning ("subscript has type `char'");
1488
1489 /* Put the integer in IND to simplify error checking. */
1490 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1491 {
1492 tree temp = ar;
1493 ar = ind;
1494 ind = temp;
1495 }
1496
1497 if (ar == error_mark_node)
1498 return ar;
1499
1500 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1501 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1502 {
1503 error ("subscripted value is neither array nor pointer");
1504 return error_mark_node;
1505 }
1506 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1507 {
1508 error ("array subscript is not an integer");
1509 return error_mark_node;
1510 }
1511
1512 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1513 "array indexing");
1514 }
1515 }
1516 \f
1517 /* Build a function call to function FUNCTION with parameters PARAMS.
1518 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1519 TREE_VALUE of each node is a parameter-expression.
1520 FUNCTION's data type may be a function type or a pointer-to-function. */
1521
1522 tree
1523 build_function_call (function, params)
1524 tree function, params;
1525 {
1526 register tree fntype, fundecl = 0;
1527 register tree coerced_params;
1528 tree name = NULL_TREE, assembler_name = NULL_TREE;
1529
1530 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1531 STRIP_TYPE_NOPS (function);
1532
1533 /* Convert anything with function type to a pointer-to-function. */
1534 if (TREE_CODE (function) == FUNCTION_DECL)
1535 {
1536 name = DECL_NAME (function);
1537 assembler_name = DECL_ASSEMBLER_NAME (function);
1538
1539 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1540 (because calling an inline function does not mean the function
1541 needs to be separately compiled). */
1542 fntype = build_type_variant (TREE_TYPE (function),
1543 TREE_READONLY (function),
1544 TREE_THIS_VOLATILE (function));
1545 fundecl = function;
1546 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1547 }
1548 else
1549 function = default_conversion (function);
1550
1551 fntype = TREE_TYPE (function);
1552
1553 if (TREE_CODE (fntype) == ERROR_MARK)
1554 return error_mark_node;
1555
1556 if (!(TREE_CODE (fntype) == POINTER_TYPE
1557 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1558 {
1559 error ("called object is not a function");
1560 return error_mark_node;
1561 }
1562
1563 /* fntype now gets the type of function pointed to. */
1564 fntype = TREE_TYPE (fntype);
1565
1566 /* Convert the parameters to the types declared in the
1567 function prototype, or apply default promotions. */
1568
1569 coerced_params
1570 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1571
1572 /* Check for errors in format strings. */
1573
1574 if (warn_format && (name || assembler_name))
1575 check_function_format (name, assembler_name, coerced_params);
1576
1577 /* Recognize certain built-in functions so we can make tree-codes
1578 other than CALL_EXPR. We do this when it enables fold-const.c
1579 to do something useful. */
1580
1581 if (TREE_CODE (function) == ADDR_EXPR
1582 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1583 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1584 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
1585 {
1586 case BUILT_IN_ABS:
1587 case BUILT_IN_LABS:
1588 case BUILT_IN_FABS:
1589 if (coerced_params == 0)
1590 return integer_zero_node;
1591 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
1592 default:
1593 break;
1594 }
1595
1596 {
1597 register tree result
1598 = build (CALL_EXPR, TREE_TYPE (fntype),
1599 function, coerced_params, NULL_TREE);
1600
1601 TREE_SIDE_EFFECTS (result) = 1;
1602 if (TREE_TYPE (result) == void_type_node)
1603 return result;
1604 return require_complete_type (result);
1605 }
1606 }
1607 \f
1608 /* Convert the argument expressions in the list VALUES
1609 to the types in the list TYPELIST. The result is a list of converted
1610 argument expressions.
1611
1612 If TYPELIST is exhausted, or when an element has NULL as its type,
1613 perform the default conversions.
1614
1615 PARMLIST is the chain of parm decls for the function being called.
1616 It may be 0, if that info is not available.
1617 It is used only for generating error messages.
1618
1619 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1620
1621 This is also where warnings about wrong number of args are generated.
1622
1623 Both VALUES and the returned value are chains of TREE_LIST nodes
1624 with the elements of the list in the TREE_VALUE slots of those nodes. */
1625
1626 static tree
1627 convert_arguments (typelist, values, name, fundecl)
1628 tree typelist, values, name, fundecl;
1629 {
1630 register tree typetail, valtail;
1631 register tree result = NULL;
1632 int parmnum;
1633
1634 /* Scan the given expressions and types, producing individual
1635 converted arguments and pushing them on RESULT in reverse order. */
1636
1637 for (valtail = values, typetail = typelist, parmnum = 0;
1638 valtail;
1639 valtail = TREE_CHAIN (valtail), parmnum++)
1640 {
1641 register tree type = typetail ? TREE_VALUE (typetail) : 0;
1642 register tree val = TREE_VALUE (valtail);
1643
1644 if (type == void_type_node)
1645 {
1646 if (name)
1647 error ("too many arguments to function `%s'",
1648 IDENTIFIER_POINTER (name));
1649 else
1650 error ("too many arguments to function");
1651 break;
1652 }
1653
1654 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1655 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1656 to convert automatically to a pointer. */
1657 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1658 val = TREE_OPERAND (val, 0);
1659
1660 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1661 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1662 val = default_conversion (val);
1663
1664 val = require_complete_type (val);
1665
1666 if (type != 0)
1667 {
1668 /* Formal parm type is specified by a function prototype. */
1669 tree parmval;
1670
1671 if (TYPE_SIZE (type) == 0)
1672 {
1673 error ("type of formal parameter %d is incomplete", parmnum + 1);
1674 parmval = val;
1675 }
1676 else
1677 {
1678 /* Optionally warn about conversions that
1679 differ from the default conversions. */
1680 if (warn_conversion)
1681 {
1682 int formal_prec = TYPE_PRECISION (type);
1683
1684 if (INTEGRAL_TYPE_P (type)
1685 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1686 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1687 else if (TREE_CODE (type) == COMPLEX_TYPE
1688 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1689 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1690 else if (TREE_CODE (type) == REAL_TYPE
1691 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1692 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1693 else if (TREE_CODE (type) == REAL_TYPE
1694 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1695 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1696 /* ??? At some point, messages should be written about
1697 conversions between complex types, but that's too messy
1698 to do now. */
1699 else if (TREE_CODE (type) == REAL_TYPE
1700 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1701 {
1702 /* Warn if any argument is passed as `float',
1703 since without a prototype it would be `double'. */
1704 if (formal_prec == TYPE_PRECISION (float_type_node))
1705 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1706 }
1707 /* Detect integer changing in width or signedness. */
1708 else if (INTEGRAL_TYPE_P (type)
1709 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1710 {
1711 tree would_have_been = default_conversion (val);
1712 tree type1 = TREE_TYPE (would_have_been);
1713
1714 if (TREE_CODE (type) == ENUMERAL_TYPE
1715 && type == TREE_TYPE (val))
1716 /* No warning if function asks for enum
1717 and the actual arg is that enum type. */
1718 ;
1719 else if (formal_prec != TYPE_PRECISION (type1))
1720 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1721 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1722 ;
1723 /* Don't complain if the formal parameter type
1724 is an enum, because we can't tell now whether
1725 the value was an enum--even the same enum. */
1726 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1727 ;
1728 else if (TREE_CODE (val) == INTEGER_CST
1729 && int_fits_type_p (val, type))
1730 /* Change in signedness doesn't matter
1731 if a constant value is unaffected. */
1732 ;
1733 /* Likewise for a constant in a NOP_EXPR. */
1734 else if (TREE_CODE (val) == NOP_EXPR
1735 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1736 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1737 ;
1738 #if 0 /* We never get such tree structure here. */
1739 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1740 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1741 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1742 /* Change in signedness doesn't matter
1743 if an enum value is unaffected. */
1744 ;
1745 #endif
1746 /* If the value is extended from a narrower
1747 unsigned type, it doesn't matter whether we
1748 pass it as signed or unsigned; the value
1749 certainly is the same either way. */
1750 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1751 && TREE_UNSIGNED (TREE_TYPE (val)))
1752 ;
1753 else if (TREE_UNSIGNED (type))
1754 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1755 else
1756 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1757 }
1758 }
1759
1760 parmval = convert_for_assignment (type, val,
1761 (char *) 0, /* arg passing */
1762 fundecl, name, parmnum + 1);
1763
1764 #ifdef PROMOTE_PROTOTYPES
1765 if ((TREE_CODE (type) == INTEGER_TYPE
1766 || TREE_CODE (type) == ENUMERAL_TYPE)
1767 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1768 parmval = default_conversion (parmval);
1769 #endif
1770 }
1771 result = tree_cons (NULL_TREE, parmval, result);
1772 }
1773 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1774 && (TYPE_PRECISION (TREE_TYPE (val))
1775 < TYPE_PRECISION (double_type_node)))
1776 /* Convert `float' to `double'. */
1777 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1778 else
1779 /* Convert `short' and `char' to full-size `int'. */
1780 result = tree_cons (NULL_TREE, default_conversion (val), result);
1781
1782 if (typetail)
1783 typetail = TREE_CHAIN (typetail);
1784 }
1785
1786 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1787 {
1788 if (name)
1789 error ("too few arguments to function `%s'",
1790 IDENTIFIER_POINTER (name));
1791 else
1792 error ("too few arguments to function");
1793 }
1794
1795 return nreverse (result);
1796 }
1797 \f
1798 /* This is the entry point used by the parser
1799 for binary operators in the input.
1800 In addition to constructing the expression,
1801 we check for operands that were written with other binary operators
1802 in a way that is likely to confuse the user. */
1803
1804 tree
1805 parser_build_binary_op (code, arg1, arg2)
1806 enum tree_code code;
1807 tree arg1, arg2;
1808 {
1809 tree result = build_binary_op (code, arg1, arg2, 1);
1810
1811 char class;
1812 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1813 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1814 enum tree_code code1 = ERROR_MARK;
1815 enum tree_code code2 = ERROR_MARK;
1816
1817 if (class1 == 'e' || class1 == '1'
1818 || class1 == '2' || class1 == '<')
1819 code1 = C_EXP_ORIGINAL_CODE (arg1);
1820 if (class2 == 'e' || class2 == '1'
1821 || class2 == '2' || class2 == '<')
1822 code2 = C_EXP_ORIGINAL_CODE (arg2);
1823
1824 /* Check for cases such as x+y<<z which users are likely
1825 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1826 is cleared to prevent these warnings. */
1827 if (warn_parentheses)
1828 {
1829 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1830 {
1831 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1832 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1833 warning ("suggest parentheses around + or - inside shift");
1834 }
1835
1836 if (code == TRUTH_ORIF_EXPR)
1837 {
1838 if (code1 == TRUTH_ANDIF_EXPR
1839 || code2 == TRUTH_ANDIF_EXPR)
1840 warning ("suggest parentheses around && within ||");
1841 }
1842
1843 if (code == BIT_IOR_EXPR)
1844 {
1845 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1846 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1847 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1848 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1849 warning ("suggest parentheses around arithmetic in operand of |");
1850 /* Check cases like x|y==z */
1851 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1852 warning ("suggest parentheses around comparison in operand of |");
1853 }
1854
1855 if (code == BIT_XOR_EXPR)
1856 {
1857 if (code1 == BIT_AND_EXPR
1858 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1859 || code2 == BIT_AND_EXPR
1860 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1861 warning ("suggest parentheses around arithmetic in operand of ^");
1862 /* Check cases like x^y==z */
1863 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1864 warning ("suggest parentheses around comparison in operand of ^");
1865 }
1866
1867 if (code == BIT_AND_EXPR)
1868 {
1869 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1870 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1871 warning ("suggest parentheses around + or - in operand of &");
1872 /* Check cases like x&y==z */
1873 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1874 warning ("suggest parentheses around comparison in operand of &");
1875 }
1876 }
1877
1878 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1879 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1880 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1881 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1882
1883 unsigned_conversion_warning (result, arg1);
1884 unsigned_conversion_warning (result, arg2);
1885 overflow_warning (result);
1886
1887 class = TREE_CODE_CLASS (TREE_CODE (result));
1888
1889 /* Record the code that was specified in the source,
1890 for the sake of warnings about confusing nesting. */
1891 if (class == 'e' || class == '1'
1892 || class == '2' || class == '<')
1893 C_SET_EXP_ORIGINAL_CODE (result, code);
1894 else
1895 {
1896 int flag = TREE_CONSTANT (result);
1897 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1898 so that convert_for_assignment wouldn't strip it.
1899 That way, we got warnings for things like p = (1 - 1).
1900 But it turns out we should not get those warnings. */
1901 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1902 C_SET_EXP_ORIGINAL_CODE (result, code);
1903 TREE_CONSTANT (result) = flag;
1904 }
1905
1906 return result;
1907 }
1908
1909 /* Build a binary-operation expression without default conversions.
1910 CODE is the kind of expression to build.
1911 This function differs from `build' in several ways:
1912 the data type of the result is computed and recorded in it,
1913 warnings are generated if arg data types are invalid,
1914 special handling for addition and subtraction of pointers is known,
1915 and some optimization is done (operations on narrow ints
1916 are done in the narrower type when that gives the same result).
1917 Constant folding is also done before the result is returned.
1918
1919 Note that the operands will never have enumeral types, or function
1920 or array types, because either they will have the default conversions
1921 performed or they have both just been converted to some other type in which
1922 the arithmetic is to be done. */
1923
1924 tree
1925 build_binary_op (code, orig_op0, orig_op1, convert_p)
1926 enum tree_code code;
1927 tree orig_op0, orig_op1;
1928 int convert_p;
1929 {
1930 tree type0, type1;
1931 register enum tree_code code0, code1;
1932 tree op0, op1;
1933
1934 /* Expression code to give to the expression when it is built.
1935 Normally this is CODE, which is what the caller asked for,
1936 but in some special cases we change it. */
1937 register enum tree_code resultcode = code;
1938
1939 /* Data type in which the computation is to be performed.
1940 In the simplest cases this is the common type of the arguments. */
1941 register tree result_type = NULL;
1942
1943 /* Nonzero means operands have already been type-converted
1944 in whatever way is necessary.
1945 Zero means they need to be converted to RESULT_TYPE. */
1946 int converted = 0;
1947
1948 /* Nonzero means create the expression with this type, rather than
1949 RESULT_TYPE. */
1950 tree build_type = 0;
1951
1952 /* Nonzero means after finally constructing the expression
1953 convert it to this type. */
1954 tree final_type = 0;
1955
1956 /* Nonzero if this is an operation like MIN or MAX which can
1957 safely be computed in short if both args are promoted shorts.
1958 Also implies COMMON.
1959 -1 indicates a bitwise operation; this makes a difference
1960 in the exact conditions for when it is safe to do the operation
1961 in a narrower mode. */
1962 int shorten = 0;
1963
1964 /* Nonzero if this is a comparison operation;
1965 if both args are promoted shorts, compare the original shorts.
1966 Also implies COMMON. */
1967 int short_compare = 0;
1968
1969 /* Nonzero if this is a right-shift operation, which can be computed on the
1970 original short and then promoted if the operand is a promoted short. */
1971 int short_shift = 0;
1972
1973 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1974 int common = 0;
1975
1976 if (convert_p)
1977 {
1978 op0 = default_conversion (orig_op0);
1979 op1 = default_conversion (orig_op1);
1980 }
1981 else
1982 {
1983 op0 = orig_op0;
1984 op1 = orig_op1;
1985 }
1986
1987 type0 = TREE_TYPE (op0);
1988 type1 = TREE_TYPE (op1);
1989
1990 /* The expression codes of the data types of the arguments tell us
1991 whether the arguments are integers, floating, pointers, etc. */
1992 code0 = TREE_CODE (type0);
1993 code1 = TREE_CODE (type1);
1994
1995 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1996 STRIP_TYPE_NOPS (op0);
1997 STRIP_TYPE_NOPS (op1);
1998
1999 /* If an error was already reported for one of the arguments,
2000 avoid reporting another error. */
2001
2002 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2003 return error_mark_node;
2004
2005 switch (code)
2006 {
2007 case PLUS_EXPR:
2008 /* Handle the pointer + int case. */
2009 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2010 return pointer_int_sum (PLUS_EXPR, op0, op1);
2011 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2012 return pointer_int_sum (PLUS_EXPR, op1, op0);
2013 else
2014 common = 1;
2015 break;
2016
2017 case MINUS_EXPR:
2018 /* Subtraction of two similar pointers.
2019 We must subtract them as integers, then divide by object size. */
2020 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
2021 && comp_target_types (type0, type1))
2022 return pointer_diff (op0, op1);
2023 /* Handle pointer minus int. Just like pointer plus int. */
2024 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2025 return pointer_int_sum (MINUS_EXPR, op0, op1);
2026 else
2027 common = 1;
2028 break;
2029
2030 case MULT_EXPR:
2031 common = 1;
2032 break;
2033
2034 case TRUNC_DIV_EXPR:
2035 case CEIL_DIV_EXPR:
2036 case FLOOR_DIV_EXPR:
2037 case ROUND_DIV_EXPR:
2038 case EXACT_DIV_EXPR:
2039 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2040 || code0 == COMPLEX_TYPE)
2041 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2042 || code1 == COMPLEX_TYPE))
2043 {
2044 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2045 resultcode = RDIV_EXPR;
2046 else
2047 {
2048 /* Although it would be tempting to shorten always here, that
2049 loses on some targets, since the modulo instruction is
2050 undefined if the quotient can't be represented in the
2051 computation mode. We shorten only if unsigned or if
2052 dividing by something we know != -1. */
2053 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2054 || (TREE_CODE (op1) == INTEGER_CST
2055 && (TREE_INT_CST_LOW (op1) != -1
2056 || TREE_INT_CST_HIGH (op1) != -1)));
2057 }
2058 common = 1;
2059 }
2060 break;
2061
2062 case BIT_AND_EXPR:
2063 case BIT_ANDTC_EXPR:
2064 case BIT_IOR_EXPR:
2065 case BIT_XOR_EXPR:
2066 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2067 shorten = -1;
2068 /* If one operand is a constant, and the other is a short type
2069 that has been converted to an int,
2070 really do the work in the short type and then convert the
2071 result to int. If we are lucky, the constant will be 0 or 1
2072 in the short type, making the entire operation go away. */
2073 if (TREE_CODE (op0) == INTEGER_CST
2074 && TREE_CODE (op1) == NOP_EXPR
2075 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2076 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2077 {
2078 final_type = result_type;
2079 op1 = TREE_OPERAND (op1, 0);
2080 result_type = TREE_TYPE (op1);
2081 }
2082 if (TREE_CODE (op1) == INTEGER_CST
2083 && TREE_CODE (op0) == NOP_EXPR
2084 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2085 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2086 {
2087 final_type = result_type;
2088 op0 = TREE_OPERAND (op0, 0);
2089 result_type = TREE_TYPE (op0);
2090 }
2091 break;
2092
2093 case TRUNC_MOD_EXPR:
2094 case FLOOR_MOD_EXPR:
2095 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2096 {
2097 /* Although it would be tempting to shorten always here, that loses
2098 on some targets, since the modulo instruction is undefined if the
2099 quotient can't be represented in the computation mode. We shorten
2100 only if unsigned or if dividing by something we know != -1. */
2101 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2102 || (TREE_CODE (op1) == INTEGER_CST
2103 && (TREE_INT_CST_LOW (op1) != -1
2104 || TREE_INT_CST_HIGH (op1) != -1)));
2105 common = 1;
2106 }
2107 break;
2108
2109 case TRUTH_ANDIF_EXPR:
2110 case TRUTH_ORIF_EXPR:
2111 case TRUTH_AND_EXPR:
2112 case TRUTH_OR_EXPR:
2113 case TRUTH_XOR_EXPR:
2114 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2115 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2116 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2117 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2118 {
2119 /* Result of these operations is always an int,
2120 but that does not mean the operands should be
2121 converted to ints! */
2122 result_type = integer_type_node;
2123 op0 = truthvalue_conversion (op0);
2124 op1 = truthvalue_conversion (op1);
2125 converted = 1;
2126 }
2127 break;
2128
2129 /* Shift operations: result has same type as first operand;
2130 always convert second operand to int.
2131 Also set SHORT_SHIFT if shifting rightward. */
2132
2133 case RSHIFT_EXPR:
2134 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2135 {
2136 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2137 {
2138 if (tree_int_cst_sgn (op1) < 0)
2139 warning ("right shift count is negative");
2140 else
2141 {
2142 if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
2143 short_shift = 1;
2144 if (TREE_INT_CST_HIGH (op1) != 0
2145 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2146 >= TYPE_PRECISION (type0)))
2147 warning ("right shift count >= width of type");
2148 }
2149 }
2150 /* Use the type of the value to be shifted.
2151 This is what most traditional C compilers do. */
2152 result_type = type0;
2153 /* Unless traditional, convert the shift-count to an integer,
2154 regardless of size of value being shifted. */
2155 if (! flag_traditional)
2156 {
2157 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2158 op1 = convert (integer_type_node, op1);
2159 /* Avoid converting op1 to result_type later. */
2160 converted = 1;
2161 }
2162 }
2163 break;
2164
2165 case LSHIFT_EXPR:
2166 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2167 {
2168 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2169 {
2170 if (tree_int_cst_sgn (op1) < 0)
2171 warning ("left shift count is negative");
2172 else if (TREE_INT_CST_HIGH (op1) != 0
2173 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2174 >= TYPE_PRECISION (type0)))
2175 warning ("left shift count >= width of type");
2176 }
2177 /* Use the type of the value to be shifted.
2178 This is what most traditional C compilers do. */
2179 result_type = type0;
2180 /* Unless traditional, convert the shift-count to an integer,
2181 regardless of size of value being shifted. */
2182 if (! flag_traditional)
2183 {
2184 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2185 op1 = convert (integer_type_node, op1);
2186 /* Avoid converting op1 to result_type later. */
2187 converted = 1;
2188 }
2189 }
2190 break;
2191
2192 case RROTATE_EXPR:
2193 case LROTATE_EXPR:
2194 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2195 {
2196 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2197 {
2198 if (tree_int_cst_sgn (op1) < 0)
2199 warning ("shift count is negative");
2200 else if (TREE_INT_CST_HIGH (op1) != 0
2201 || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
2202 >= TYPE_PRECISION (type0)))
2203 warning ("shift count >= width of type");
2204 }
2205 /* Use the type of the value to be shifted.
2206 This is what most traditional C compilers do. */
2207 result_type = type0;
2208 /* Unless traditional, convert the shift-count to an integer,
2209 regardless of size of value being shifted. */
2210 if (! flag_traditional)
2211 {
2212 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2213 op1 = convert (integer_type_node, op1);
2214 /* Avoid converting op1 to result_type later. */
2215 converted = 1;
2216 }
2217 }
2218 break;
2219
2220 case EQ_EXPR:
2221 case NE_EXPR:
2222 /* Result of comparison is always int,
2223 but don't convert the args to int! */
2224 build_type = integer_type_node;
2225 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2226 || code0 == COMPLEX_TYPE)
2227 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2228 || code1 == COMPLEX_TYPE))
2229 short_compare = 1;
2230 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2231 {
2232 register tree tt0 = TREE_TYPE (type0);
2233 register tree tt1 = TREE_TYPE (type1);
2234 /* Anything compares with void *. void * compares with anything.
2235 Otherwise, the targets must be compatible
2236 and both must be object or both incomplete. */
2237 if (comp_target_types (type0, type1))
2238 result_type = common_type (type0, type1);
2239 else if (TYPE_MAIN_VARIANT (tt0) == void_type_node)
2240 {
2241 /* op0 != orig_op0 detects the case of something
2242 whose value is 0 but which isn't a valid null ptr const. */
2243 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2244 && TREE_CODE (tt1) == FUNCTION_TYPE)
2245 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2246 }
2247 else if (TYPE_MAIN_VARIANT (tt1) == void_type_node)
2248 {
2249 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2250 && TREE_CODE (tt0) == FUNCTION_TYPE)
2251 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2252 }
2253 else
2254 pedwarn ("comparison of distinct pointer types lacks a cast");
2255
2256 if (result_type == NULL_TREE)
2257 result_type = ptr_type_node;
2258 }
2259 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2260 && integer_zerop (op1))
2261 result_type = type0;
2262 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2263 && integer_zerop (op0))
2264 result_type = type1;
2265 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2266 {
2267 result_type = type0;
2268 if (! flag_traditional)
2269 pedwarn ("comparison between pointer and integer");
2270 }
2271 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2272 {
2273 result_type = type1;
2274 if (! flag_traditional)
2275 pedwarn ("comparison between pointer and integer");
2276 }
2277 break;
2278
2279 case MAX_EXPR:
2280 case MIN_EXPR:
2281 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2282 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2283 shorten = 1;
2284 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2285 {
2286 if (comp_target_types (type0, type1))
2287 {
2288 result_type = common_type (type0, type1);
2289 if (pedantic
2290 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2291 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2292 }
2293 else
2294 {
2295 result_type = ptr_type_node;
2296 pedwarn ("comparison of distinct pointer types lacks a cast");
2297 }
2298 }
2299 break;
2300
2301 case LE_EXPR:
2302 case GE_EXPR:
2303 case LT_EXPR:
2304 case GT_EXPR:
2305 build_type = integer_type_node;
2306 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2307 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2308 short_compare = 1;
2309 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2310 {
2311 if (comp_target_types (type0, type1))
2312 {
2313 result_type = common_type (type0, type1);
2314 if ((TYPE_SIZE (TREE_TYPE (type0)) != 0)
2315 != (TYPE_SIZE (TREE_TYPE (type1)) != 0))
2316 pedwarn ("comparison of complete and incomplete pointers");
2317 else if (pedantic
2318 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2319 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2320 }
2321 else
2322 {
2323 result_type = ptr_type_node;
2324 pedwarn ("comparison of distinct pointer types lacks a cast");
2325 }
2326 }
2327 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2328 && integer_zerop (op1))
2329 {
2330 result_type = type0;
2331 if (pedantic || extra_warnings)
2332 pedwarn ("ordered comparison of pointer with integer zero");
2333 }
2334 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2335 && integer_zerop (op0))
2336 {
2337 result_type = type1;
2338 if (pedantic)
2339 pedwarn ("ordered comparison of pointer with integer zero");
2340 }
2341 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2342 {
2343 result_type = type0;
2344 if (! flag_traditional)
2345 pedwarn ("comparison between pointer and integer");
2346 }
2347 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2348 {
2349 result_type = type1;
2350 if (! flag_traditional)
2351 pedwarn ("comparison between pointer and integer");
2352 }
2353 break;
2354
2355 default:
2356 break;
2357 }
2358
2359 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2360 &&
2361 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2362 {
2363 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2364
2365 if (shorten || common || short_compare)
2366 result_type = common_type (type0, type1);
2367
2368 /* For certain operations (which identify themselves by shorten != 0)
2369 if both args were extended from the same smaller type,
2370 do the arithmetic in that type and then extend.
2371
2372 shorten !=0 and !=1 indicates a bitwise operation.
2373 For them, this optimization is safe only if
2374 both args are zero-extended or both are sign-extended.
2375 Otherwise, we might change the result.
2376 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2377 but calculated in (unsigned short) it would be (unsigned short)-1. */
2378
2379 if (shorten && none_complex)
2380 {
2381 int unsigned0, unsigned1;
2382 tree arg0 = get_narrower (op0, &unsigned0);
2383 tree arg1 = get_narrower (op1, &unsigned1);
2384 /* UNS is 1 if the operation to be done is an unsigned one. */
2385 int uns = TREE_UNSIGNED (result_type);
2386 tree type;
2387
2388 final_type = result_type;
2389
2390 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2391 but it *requires* conversion to FINAL_TYPE. */
2392
2393 if ((TYPE_PRECISION (TREE_TYPE (op0))
2394 == TYPE_PRECISION (TREE_TYPE (arg0)))
2395 && TREE_TYPE (op0) != final_type)
2396 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2397 if ((TYPE_PRECISION (TREE_TYPE (op1))
2398 == TYPE_PRECISION (TREE_TYPE (arg1)))
2399 && TREE_TYPE (op1) != final_type)
2400 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2401
2402 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2403
2404 /* For bitwise operations, signedness of nominal type
2405 does not matter. Consider only how operands were extended. */
2406 if (shorten == -1)
2407 uns = unsigned0;
2408
2409 /* Note that in all three cases below we refrain from optimizing
2410 an unsigned operation on sign-extended args.
2411 That would not be valid. */
2412
2413 /* Both args variable: if both extended in same way
2414 from same width, do it in that width.
2415 Do it unsigned if args were zero-extended. */
2416 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2417 < TYPE_PRECISION (result_type))
2418 && (TYPE_PRECISION (TREE_TYPE (arg1))
2419 == TYPE_PRECISION (TREE_TYPE (arg0)))
2420 && unsigned0 == unsigned1
2421 && (unsigned0 || !uns))
2422 result_type
2423 = signed_or_unsigned_type (unsigned0,
2424 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2425 else if (TREE_CODE (arg0) == INTEGER_CST
2426 && (unsigned1 || !uns)
2427 && (TYPE_PRECISION (TREE_TYPE (arg1))
2428 < TYPE_PRECISION (result_type))
2429 && (type = signed_or_unsigned_type (unsigned1,
2430 TREE_TYPE (arg1)),
2431 int_fits_type_p (arg0, type)))
2432 result_type = type;
2433 else if (TREE_CODE (arg1) == INTEGER_CST
2434 && (unsigned0 || !uns)
2435 && (TYPE_PRECISION (TREE_TYPE (arg0))
2436 < TYPE_PRECISION (result_type))
2437 && (type = signed_or_unsigned_type (unsigned0,
2438 TREE_TYPE (arg0)),
2439 int_fits_type_p (arg1, type)))
2440 result_type = type;
2441 }
2442
2443 /* Shifts can be shortened if shifting right. */
2444
2445 if (short_shift)
2446 {
2447 int unsigned_arg;
2448 tree arg0 = get_narrower (op0, &unsigned_arg);
2449
2450 final_type = result_type;
2451
2452 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2453 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2454
2455 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2456 /* We can shorten only if the shift count is less than the
2457 number of bits in the smaller type size. */
2458 && TREE_INT_CST_HIGH (op1) == 0
2459 && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
2460 /* If arg is sign-extended and then unsigned-shifted,
2461 we can simulate this with a signed shift in arg's type
2462 only if the extended result is at least twice as wide
2463 as the arg. Otherwise, the shift could use up all the
2464 ones made by sign-extension and bring in zeros.
2465 We can't optimize that case at all, but in most machines
2466 it never happens because available widths are 2**N. */
2467 && (!TREE_UNSIGNED (final_type)
2468 || unsigned_arg
2469 || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
2470 {
2471 /* Do an unsigned shift if the operand was zero-extended. */
2472 result_type
2473 = signed_or_unsigned_type (unsigned_arg,
2474 TREE_TYPE (arg0));
2475 /* Convert value-to-be-shifted to that type. */
2476 if (TREE_TYPE (op0) != result_type)
2477 op0 = convert (result_type, op0);
2478 converted = 1;
2479 }
2480 }
2481
2482 /* Comparison operations are shortened too but differently.
2483 They identify themselves by setting short_compare = 1. */
2484
2485 if (short_compare)
2486 {
2487 /* Don't write &op0, etc., because that would prevent op0
2488 from being kept in a register.
2489 Instead, make copies of the our local variables and
2490 pass the copies by reference, then copy them back afterward. */
2491 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2492 enum tree_code xresultcode = resultcode;
2493 tree val
2494 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2495 if (val != 0)
2496 return val;
2497 op0 = xop0, op1 = xop1;
2498 converted = 1;
2499 resultcode = xresultcode;
2500
2501 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2502 && skip_evaluation == 0)
2503 {
2504 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2505 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2506
2507 int unsignedp0, unsignedp1;
2508 tree primop0 = get_narrower (op0, &unsignedp0);
2509 tree primop1 = get_narrower (op1, &unsignedp1);
2510
2511 /* Avoid spurious warnings for comparison with enumerators. */
2512
2513 xop0 = orig_op0;
2514 xop1 = orig_op1;
2515 STRIP_TYPE_NOPS (xop0);
2516 STRIP_TYPE_NOPS (xop1);
2517
2518 /* Give warnings for comparisons between signed and unsigned
2519 quantities that may fail. */
2520 /* Do the checking based on the original operand trees, so that
2521 casts will be considered, but default promotions won't be. */
2522
2523 /* Do not warn if the comparison is being done in a signed type,
2524 since the signed type will only be chosen if it can represent
2525 all the values of the unsigned type. */
2526 if (! TREE_UNSIGNED (result_type))
2527 /* OK */;
2528 /* Do not warn if both operands are unsigned. */
2529 else if (op0_signed == op1_signed)
2530 /* OK */;
2531 /* Do not warn if the signed quantity is an unsuffixed
2532 integer literal (or some static constant expression
2533 involving such literals) and it is non-negative. */
2534 else if ((op0_signed && TREE_CODE (xop0) == INTEGER_CST
2535 && tree_int_cst_sgn (xop0) >= 0)
2536 || (op1_signed && TREE_CODE (xop1) == INTEGER_CST
2537 && tree_int_cst_sgn (xop1) >= 0))
2538 /* OK */;
2539 /* Do not warn if the comparison is an equality operation,
2540 the unsigned quantity is an integral constant and it does
2541 not use the most significant bit of result_type. */
2542 else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
2543 && ((op0_signed && TREE_CODE (xop1) == INTEGER_CST
2544 && int_fits_type_p (xop1, signed_type (result_type)))
2545 || (op1_signed && TREE_CODE (xop0) == INTEGER_CST
2546 && int_fits_type_p (xop0, signed_type (result_type)))))
2547 /* OK */;
2548 else
2549 warning ("comparison between signed and unsigned");
2550
2551 /* Warn if two unsigned values are being compared in a size
2552 larger than their original size, and one (and only one) is the
2553 result of a `~' operator. This comparison will always fail.
2554
2555 Also warn if one operand is a constant, and the constant
2556 does not have all bits set that are set in the ~ operand
2557 when it is extended. */
2558
2559 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2560 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2561 {
2562 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2563 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2564 &unsignedp0);
2565 else
2566 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2567 &unsignedp1);
2568
2569 if (TREE_CODE (primop0) == INTEGER_CST
2570 || TREE_CODE (primop1) == INTEGER_CST)
2571 {
2572 tree primop;
2573 long constant, mask;
2574 int unsignedp, bits;
2575
2576 if (TREE_CODE (primop0) == INTEGER_CST)
2577 {
2578 primop = primop1;
2579 unsignedp = unsignedp1;
2580 constant = TREE_INT_CST_LOW (primop0);
2581 }
2582 else
2583 {
2584 primop = primop0;
2585 unsignedp = unsignedp0;
2586 constant = TREE_INT_CST_LOW (primop1);
2587 }
2588
2589 bits = TYPE_PRECISION (TREE_TYPE (primop));
2590 if (bits < TYPE_PRECISION (result_type)
2591 && bits < HOST_BITS_PER_LONG && unsignedp)
2592 {
2593 mask = (~0L) << bits;
2594 if ((mask & constant) != mask)
2595 warning ("comparison of promoted ~unsigned with constant");
2596 }
2597 }
2598 else if (unsignedp0 && unsignedp1
2599 && (TYPE_PRECISION (TREE_TYPE (primop0))
2600 < TYPE_PRECISION (result_type))
2601 && (TYPE_PRECISION (TREE_TYPE (primop1))
2602 < TYPE_PRECISION (result_type)))
2603 warning ("comparison of promoted ~unsigned with unsigned");
2604 }
2605 }
2606 }
2607 }
2608
2609 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2610 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2611 Then the expression will be built.
2612 It will be given type FINAL_TYPE if that is nonzero;
2613 otherwise, it will be given type RESULT_TYPE. */
2614
2615 if (!result_type)
2616 {
2617 binary_op_error (code);
2618 return error_mark_node;
2619 }
2620
2621 if (! converted)
2622 {
2623 if (TREE_TYPE (op0) != result_type)
2624 op0 = convert (result_type, op0);
2625 if (TREE_TYPE (op1) != result_type)
2626 op1 = convert (result_type, op1);
2627 }
2628
2629 if (build_type == NULL_TREE)
2630 build_type = result_type;
2631
2632 {
2633 register tree result = build (resultcode, build_type, op0, op1);
2634 register tree folded;
2635
2636 folded = fold (result);
2637 if (folded == result)
2638 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2639 if (final_type != 0)
2640 return convert (final_type, folded);
2641 return folded;
2642 }
2643 }
2644 \f
2645 /* Return a tree for the sum or difference (RESULTCODE says which)
2646 of pointer PTROP and integer INTOP. */
2647
2648 static tree
2649 pointer_int_sum (resultcode, ptrop, intop)
2650 enum tree_code resultcode;
2651 register tree ptrop, intop;
2652 {
2653 tree size_exp;
2654
2655 register tree result;
2656 register tree folded;
2657
2658 /* The result is a pointer of the same type that is being added. */
2659
2660 register tree result_type = TREE_TYPE (ptrop);
2661
2662 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2663 {
2664 if (pedantic || warn_pointer_arith)
2665 pedwarn ("pointer of type `void *' used in arithmetic");
2666 size_exp = integer_one_node;
2667 }
2668 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2669 {
2670 if (pedantic || warn_pointer_arith)
2671 pedwarn ("pointer to a function used in arithmetic");
2672 size_exp = integer_one_node;
2673 }
2674 else
2675 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2676
2677 /* If what we are about to multiply by the size of the elements
2678 contains a constant term, apply distributive law
2679 and multiply that constant term separately.
2680 This helps produce common subexpressions. */
2681
2682 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2683 && ! TREE_CONSTANT (intop)
2684 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2685 && TREE_CONSTANT (size_exp)
2686 /* If the constant comes from pointer subtraction,
2687 skip this optimization--it would cause an error. */
2688 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2689 /* If the constant is unsigned, and smaller than the pointer size,
2690 then we must skip this optimization. This is because it could cause
2691 an overflow error if the constant is negative but INTOP is not. */
2692 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2693 || (TYPE_PRECISION (TREE_TYPE (intop))
2694 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2695 {
2696 enum tree_code subcode = resultcode;
2697 tree int_type = TREE_TYPE (intop);
2698 if (TREE_CODE (intop) == MINUS_EXPR)
2699 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2700 /* Convert both subexpression types to the type of intop,
2701 because weird cases involving pointer arithmetic
2702 can result in a sum or difference with different type args. */
2703 ptrop = build_binary_op (subcode, ptrop,
2704 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2705 intop = convert (int_type, TREE_OPERAND (intop, 0));
2706 }
2707
2708 /* Convert the integer argument to a type the same size as sizetype
2709 so the multiply won't overflow spuriously. */
2710
2711 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2712 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2713 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2714 TREE_UNSIGNED (sizetype)), intop);
2715
2716 /* Replace the integer argument with a suitable product by the object size.
2717 Do this multiplication as signed, then convert to the appropriate
2718 pointer type (actually unsigned integral). */
2719
2720 intop = convert (result_type,
2721 build_binary_op (MULT_EXPR, intop,
2722 convert (TREE_TYPE (intop), size_exp), 1));
2723
2724 /* Create the sum or difference. */
2725
2726 result = build (resultcode, result_type, ptrop, intop);
2727
2728 folded = fold (result);
2729 if (folded == result)
2730 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2731 return folded;
2732 }
2733
2734 /* Return a tree for the difference of pointers OP0 and OP1.
2735 The resulting tree has type int. */
2736
2737 static tree
2738 pointer_diff (op0, op1)
2739 register tree op0, op1;
2740 {
2741 register tree result, folded;
2742 tree restype = ptrdiff_type_node;
2743
2744 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2745
2746 if (pedantic || warn_pointer_arith)
2747 {
2748 if (TREE_CODE (target_type) == VOID_TYPE)
2749 pedwarn ("pointer of type `void *' used in subtraction");
2750 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2751 pedwarn ("pointer to a function used in subtraction");
2752 }
2753
2754 /* First do the subtraction as integers;
2755 then drop through to build the divide operator.
2756 Do not do default conversions on the minus operator
2757 in case restype is a short type. */
2758
2759 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2760 convert (restype, op1), 0);
2761 /* This generates an error if op1 is pointer to incomplete type. */
2762 if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
2763 error ("arithmetic on pointer to an incomplete type");
2764
2765 /* This generates an error if op0 is pointer to incomplete type. */
2766 op1 = c_size_in_bytes (target_type);
2767
2768 /* Divide by the size, in easiest possible way. */
2769
2770 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2771
2772 folded = fold (result);
2773 if (folded == result)
2774 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2775 return folded;
2776 }
2777 \f
2778 /* Construct and perhaps optimize a tree representation
2779 for a unary operation. CODE, a tree_code, specifies the operation
2780 and XARG is the operand. NOCONVERT nonzero suppresses
2781 the default promotions (such as from short to int). */
2782
2783 tree
2784 build_unary_op (code, xarg, noconvert)
2785 enum tree_code code;
2786 tree xarg;
2787 int noconvert;
2788 {
2789 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2790 register tree arg = xarg;
2791 register tree argtype = 0;
2792 register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2793 char *errstring = NULL;
2794 tree val;
2795
2796 if (typecode == ERROR_MARK)
2797 return error_mark_node;
2798 if (typecode == ENUMERAL_TYPE)
2799 typecode = INTEGER_TYPE;
2800
2801 switch (code)
2802 {
2803 case CONVERT_EXPR:
2804 /* This is used for unary plus, because a CONVERT_EXPR
2805 is enough to prevent anybody from looking inside for
2806 associativity, but won't generate any code. */
2807 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2808 || typecode == COMPLEX_TYPE))
2809 errstring = "wrong type argument to unary plus";
2810 else if (!noconvert)
2811 arg = default_conversion (arg);
2812 break;
2813
2814 case NEGATE_EXPR:
2815 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2816 || typecode == COMPLEX_TYPE))
2817 errstring = "wrong type argument to unary minus";
2818 else if (!noconvert)
2819 arg = default_conversion (arg);
2820 break;
2821
2822 case BIT_NOT_EXPR:
2823 if (typecode == COMPLEX_TYPE)
2824 {
2825 code = CONJ_EXPR;
2826 if (!noconvert)
2827 arg = default_conversion (arg);
2828 }
2829 else if (typecode != INTEGER_TYPE)
2830 errstring = "wrong type argument to bit-complement";
2831 else if (!noconvert)
2832 arg = default_conversion (arg);
2833 break;
2834
2835 case ABS_EXPR:
2836 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2837 || typecode == COMPLEX_TYPE))
2838 errstring = "wrong type argument to abs";
2839 else if (!noconvert)
2840 arg = default_conversion (arg);
2841 break;
2842
2843 case CONJ_EXPR:
2844 /* Conjugating a real value is a no-op, but allow it anyway. */
2845 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2846 || typecode == COMPLEX_TYPE))
2847 errstring = "wrong type argument to conjugation";
2848 else if (!noconvert)
2849 arg = default_conversion (arg);
2850 break;
2851
2852 case TRUTH_NOT_EXPR:
2853 if (typecode != INTEGER_TYPE
2854 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2855 && typecode != COMPLEX_TYPE
2856 /* These will convert to a pointer. */
2857 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2858 {
2859 errstring = "wrong type argument to unary exclamation mark";
2860 break;
2861 }
2862 arg = truthvalue_conversion (arg);
2863 return invert_truthvalue (arg);
2864
2865 case NOP_EXPR:
2866 break;
2867
2868 case REALPART_EXPR:
2869 if (TREE_CODE (arg) == COMPLEX_CST)
2870 return TREE_REALPART (arg);
2871 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2872 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2873 else
2874 return arg;
2875
2876 case IMAGPART_EXPR:
2877 if (TREE_CODE (arg) == COMPLEX_CST)
2878 return TREE_IMAGPART (arg);
2879 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2880 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2881 else
2882 return convert (TREE_TYPE (arg), integer_zero_node);
2883
2884 case PREINCREMENT_EXPR:
2885 case POSTINCREMENT_EXPR:
2886 case PREDECREMENT_EXPR:
2887 case POSTDECREMENT_EXPR:
2888 /* Handle complex lvalues (when permitted)
2889 by reduction to simpler cases. */
2890
2891 val = unary_complex_lvalue (code, arg);
2892 if (val != 0)
2893 return val;
2894
2895 /* Increment or decrement the real part of the value,
2896 and don't change the imaginary part. */
2897 if (typecode == COMPLEX_TYPE)
2898 {
2899 tree real, imag;
2900
2901 arg = stabilize_reference (arg);
2902 real = build_unary_op (REALPART_EXPR, arg, 1);
2903 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2904 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2905 build_unary_op (code, real, 1), imag);
2906 }
2907
2908 /* Report invalid types. */
2909
2910 if (typecode != POINTER_TYPE
2911 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2912 {
2913 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2914 errstring ="wrong type argument to increment";
2915 else
2916 errstring ="wrong type argument to decrement";
2917 break;
2918 }
2919
2920 {
2921 register tree inc;
2922 tree result_type = TREE_TYPE (arg);
2923
2924 arg = get_unwidened (arg, 0);
2925 argtype = TREE_TYPE (arg);
2926
2927 /* Compute the increment. */
2928
2929 if (typecode == POINTER_TYPE)
2930 {
2931 /* If pointer target is an undefined struct,
2932 we just cannot know how to do the arithmetic. */
2933 if (TYPE_SIZE (TREE_TYPE (result_type)) == 0)
2934 error ("%s of pointer to unknown structure",
2935 ((code == PREINCREMENT_EXPR
2936 || code == POSTINCREMENT_EXPR)
2937 ? "increment" : "decrement"));
2938 else if ((pedantic || warn_pointer_arith)
2939 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2940 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2941 pedwarn ("wrong type argument to %s",
2942 ((code == PREINCREMENT_EXPR
2943 || code == POSTINCREMENT_EXPR)
2944 ? "increment" : "decrement"));
2945 inc = c_size_in_bytes (TREE_TYPE (result_type));
2946 }
2947 else
2948 inc = integer_one_node;
2949
2950 inc = convert (argtype, inc);
2951
2952 /* Handle incrementing a cast-expression. */
2953
2954 while (1)
2955 switch (TREE_CODE (arg))
2956 {
2957 case NOP_EXPR:
2958 case CONVERT_EXPR:
2959 case FLOAT_EXPR:
2960 case FIX_TRUNC_EXPR:
2961 case FIX_FLOOR_EXPR:
2962 case FIX_ROUND_EXPR:
2963 case FIX_CEIL_EXPR:
2964 pedantic_lvalue_warning (CONVERT_EXPR);
2965 /* If the real type has the same machine representation
2966 as the type it is cast to, we can make better output
2967 by adding directly to the inside of the cast. */
2968 if ((TREE_CODE (TREE_TYPE (arg))
2969 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2970 && (TYPE_MODE (TREE_TYPE (arg))
2971 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2972 arg = TREE_OPERAND (arg, 0);
2973 else
2974 {
2975 tree incremented, modify, value;
2976 arg = stabilize_reference (arg);
2977 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2978 value = arg;
2979 else
2980 value = save_expr (arg);
2981 incremented = build (((code == PREINCREMENT_EXPR
2982 || code == POSTINCREMENT_EXPR)
2983 ? PLUS_EXPR : MINUS_EXPR),
2984 argtype, value, inc);
2985 TREE_SIDE_EFFECTS (incremented) = 1;
2986 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2987 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2988 TREE_USED (value) = 1;
2989 return value;
2990 }
2991 break;
2992
2993 default:
2994 goto give_up;
2995 }
2996 give_up:
2997
2998 /* Complain about anything else that is not a true lvalue. */
2999 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3000 || code == POSTINCREMENT_EXPR)
3001 ? "increment" : "decrement")))
3002 return error_mark_node;
3003
3004 /* Report a read-only lvalue. */
3005 if (TREE_READONLY (arg))
3006 readonly_warning (arg,
3007 ((code == PREINCREMENT_EXPR
3008 || code == POSTINCREMENT_EXPR)
3009 ? "increment" : "decrement"));
3010
3011 val = build (code, TREE_TYPE (arg), arg, inc);
3012 TREE_SIDE_EFFECTS (val) = 1;
3013 val = convert (result_type, val);
3014 if (TREE_CODE (val) != code)
3015 TREE_NO_UNUSED_WARNING (val) = 1;
3016 return val;
3017 }
3018
3019 case ADDR_EXPR:
3020 /* Note that this operation never does default_conversion
3021 regardless of NOCONVERT. */
3022
3023 /* Let &* cancel out to simplify resulting code. */
3024 if (TREE_CODE (arg) == INDIRECT_REF)
3025 {
3026 /* Don't let this be an lvalue. */
3027 if (lvalue_p (TREE_OPERAND (arg, 0)))
3028 return non_lvalue (TREE_OPERAND (arg, 0));
3029 return TREE_OPERAND (arg, 0);
3030 }
3031
3032 /* For &x[y], return x+y */
3033 if (TREE_CODE (arg) == ARRAY_REF)
3034 {
3035 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3036 return error_mark_node;
3037 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3038 TREE_OPERAND (arg, 1), 1);
3039 }
3040
3041 /* Handle complex lvalues (when permitted)
3042 by reduction to simpler cases. */
3043 val = unary_complex_lvalue (code, arg);
3044 if (val != 0)
3045 return val;
3046
3047 #if 0 /* Turned off because inconsistent;
3048 float f; *&(int)f = 3.4 stores in int format
3049 whereas (int)f = 3.4 stores in float format. */
3050 /* Address of a cast is just a cast of the address
3051 of the operand of the cast. */
3052 switch (TREE_CODE (arg))
3053 {
3054 case NOP_EXPR:
3055 case CONVERT_EXPR:
3056 case FLOAT_EXPR:
3057 case FIX_TRUNC_EXPR:
3058 case FIX_FLOOR_EXPR:
3059 case FIX_ROUND_EXPR:
3060 case FIX_CEIL_EXPR:
3061 if (pedantic)
3062 pedwarn ("ANSI C forbids the address of a cast expression");
3063 return convert (build_pointer_type (TREE_TYPE (arg)),
3064 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3065 0));
3066 }
3067 #endif
3068
3069 /* Allow the address of a constructor if all the elements
3070 are constant. */
3071 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3072 ;
3073 /* Anything not already handled and not a true memory reference
3074 is an error. */
3075 else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
3076 return error_mark_node;
3077
3078 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3079 argtype = TREE_TYPE (arg);
3080 /* If the lvalue is const or volatile, merge that into the type
3081 to which the address will point. Note that you can't get a
3082 restricted pointer by taking the address of something, so we
3083 only have to deal with `const' and `volatile' here. */
3084 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
3085 || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3086 {
3087 if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3088 argtype = c_build_type_variant (argtype,
3089 TREE_READONLY (arg),
3090 TREE_THIS_VOLATILE (arg));
3091 }
3092
3093 argtype = build_pointer_type (argtype);
3094
3095 if (mark_addressable (arg) == 0)
3096 return error_mark_node;
3097
3098 {
3099 tree addr;
3100
3101 if (TREE_CODE (arg) == COMPONENT_REF)
3102 {
3103 tree field = TREE_OPERAND (arg, 1);
3104
3105 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3106
3107 if (DECL_C_BIT_FIELD (field))
3108 {
3109 error ("attempt to take address of bit-field structure member `%s'",
3110 IDENTIFIER_POINTER (DECL_NAME (field)));
3111 return error_mark_node;
3112 }
3113
3114 addr = convert (argtype, addr);
3115
3116 if (! integer_zerop (DECL_FIELD_BITPOS (field)))
3117 {
3118 tree offset
3119 = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
3120 size_int (BITS_PER_UNIT));
3121 int flag = TREE_CONSTANT (addr);
3122 addr = fold (build (PLUS_EXPR, argtype,
3123 addr, convert (argtype, offset)));
3124 TREE_CONSTANT (addr) = flag;
3125 }
3126 }
3127 else
3128 addr = build1 (code, argtype, arg);
3129
3130 /* Address of a static or external variable or
3131 file-scope function counts as a constant. */
3132 if (staticp (arg)
3133 && ! (TREE_CODE (arg) == FUNCTION_DECL
3134 && DECL_CONTEXT (arg) != 0))
3135 TREE_CONSTANT (addr) = 1;
3136 return addr;
3137 }
3138
3139 default:
3140 break;
3141 }
3142
3143 if (!errstring)
3144 {
3145 if (argtype == 0)
3146 argtype = TREE_TYPE (arg);
3147 return fold (build1 (code, argtype, arg));
3148 }
3149
3150 error (errstring);
3151 return error_mark_node;
3152 }
3153
3154 #if 0
3155 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3156 convert ARG with the same conversions in the same order
3157 and return the result. */
3158
3159 static tree
3160 convert_sequence (conversions, arg)
3161 tree conversions;
3162 tree arg;
3163 {
3164 switch (TREE_CODE (conversions))
3165 {
3166 case NOP_EXPR:
3167 case CONVERT_EXPR:
3168 case FLOAT_EXPR:
3169 case FIX_TRUNC_EXPR:
3170 case FIX_FLOOR_EXPR:
3171 case FIX_ROUND_EXPR:
3172 case FIX_CEIL_EXPR:
3173 return convert (TREE_TYPE (conversions),
3174 convert_sequence (TREE_OPERAND (conversions, 0),
3175 arg));
3176
3177 default:
3178 return arg;
3179 }
3180 }
3181 #endif /* 0 */
3182
3183 /* Return nonzero if REF is an lvalue valid for this language.
3184 Lvalues can be assigned, unless their type has TYPE_READONLY.
3185 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3186
3187 int
3188 lvalue_p (ref)
3189 tree ref;
3190 {
3191 register enum tree_code code = TREE_CODE (ref);
3192
3193 switch (code)
3194 {
3195 case REALPART_EXPR:
3196 case IMAGPART_EXPR:
3197 case COMPONENT_REF:
3198 return lvalue_p (TREE_OPERAND (ref, 0));
3199
3200 case STRING_CST:
3201 return 1;
3202
3203 case INDIRECT_REF:
3204 case ARRAY_REF:
3205 case VAR_DECL:
3206 case PARM_DECL:
3207 case RESULT_DECL:
3208 case ERROR_MARK:
3209 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3210 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3211
3212 case BIND_EXPR:
3213 case RTL_EXPR:
3214 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3215
3216 default:
3217 return 0;
3218 }
3219 }
3220
3221 /* Return nonzero if REF is an lvalue valid for this language;
3222 otherwise, print an error message and return zero. */
3223
3224 int
3225 lvalue_or_else (ref, string)
3226 tree ref;
3227 char *string;
3228 {
3229 int win = lvalue_p (ref);
3230 if (! win)
3231 error ("invalid lvalue in %s", string);
3232 return win;
3233 }
3234
3235 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3236 for certain kinds of expressions which are not really lvalues
3237 but which we can accept as lvalues.
3238
3239 If ARG is not a kind of expression we can handle, return zero. */
3240
3241 static tree
3242 unary_complex_lvalue (code, arg)
3243 enum tree_code code;
3244 tree arg;
3245 {
3246 /* Handle (a, b) used as an "lvalue". */
3247 if (TREE_CODE (arg) == COMPOUND_EXPR)
3248 {
3249 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3250
3251 /* If this returns a function type, it isn't really being used as
3252 an lvalue, so don't issue a warning about it. */
3253 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3254 pedantic_lvalue_warning (COMPOUND_EXPR);
3255
3256 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3257 TREE_OPERAND (arg, 0), real_result);
3258 }
3259
3260 /* Handle (a ? b : c) used as an "lvalue". */
3261 if (TREE_CODE (arg) == COND_EXPR)
3262 {
3263 pedantic_lvalue_warning (COND_EXPR);
3264 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3265 pedantic_lvalue_warning (COMPOUND_EXPR);
3266
3267 return (build_conditional_expr
3268 (TREE_OPERAND (arg, 0),
3269 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3270 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3271 }
3272
3273 return 0;
3274 }
3275
3276 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3277 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3278
3279 static void
3280 pedantic_lvalue_warning (code)
3281 enum tree_code code;
3282 {
3283 if (pedantic)
3284 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3285 code == COND_EXPR ? "conditional"
3286 : code == COMPOUND_EXPR ? "compound" : "cast");
3287 }
3288 \f
3289 /* Warn about storing in something that is `const'. */
3290
3291 void
3292 readonly_warning (arg, string)
3293 tree arg;
3294 char *string;
3295 {
3296 char buf[80];
3297 strcpy (buf, string);
3298
3299 /* Forbid assignments to iterators. */
3300 if (TREE_CODE (arg) == VAR_DECL && ITERATOR_P (arg))
3301 {
3302 strcat (buf, " of iterator `%s'");
3303 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3304 }
3305
3306 if (TREE_CODE (arg) == COMPONENT_REF)
3307 {
3308 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3309 readonly_warning (TREE_OPERAND (arg, 0), string);
3310 else
3311 {
3312 strcat (buf, " of read-only member `%s'");
3313 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3314 }
3315 }
3316 else if (TREE_CODE (arg) == VAR_DECL)
3317 {
3318 strcat (buf, " of read-only variable `%s'");
3319 pedwarn (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
3320 }
3321 else
3322 {
3323 pedwarn ("%s of read-only location", buf);
3324 }
3325 }
3326 \f
3327 /* Mark EXP saying that we need to be able to take the
3328 address of it; it should not be allocated in a register.
3329 Value is 1 if successful. */
3330
3331 int
3332 mark_addressable (exp)
3333 tree exp;
3334 {
3335 register tree x = exp;
3336 while (1)
3337 switch (TREE_CODE (x))
3338 {
3339 case COMPONENT_REF:
3340 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3341 {
3342 error ("cannot take address of bitfield `%s'",
3343 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3344 return 0;
3345 }
3346
3347 /* ... fall through ... */
3348
3349 case ADDR_EXPR:
3350 case ARRAY_REF:
3351 case REALPART_EXPR:
3352 case IMAGPART_EXPR:
3353 x = TREE_OPERAND (x, 0);
3354 break;
3355
3356 case CONSTRUCTOR:
3357 TREE_ADDRESSABLE (x) = 1;
3358 return 1;
3359
3360 case VAR_DECL:
3361 case CONST_DECL:
3362 case PARM_DECL:
3363 case RESULT_DECL:
3364 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3365 && DECL_NONLOCAL (x))
3366 {
3367 if (TREE_PUBLIC (x))
3368 {
3369 error ("global register variable `%s' used in nested function",
3370 IDENTIFIER_POINTER (DECL_NAME (x)));
3371 return 0;
3372 }
3373 pedwarn ("register variable `%s' used in nested function",
3374 IDENTIFIER_POINTER (DECL_NAME (x)));
3375 }
3376 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3377 {
3378 if (TREE_PUBLIC (x))
3379 {
3380 error ("address of global register variable `%s' requested",
3381 IDENTIFIER_POINTER (DECL_NAME (x)));
3382 return 0;
3383 }
3384
3385 /* If we are making this addressable due to its having
3386 volatile components, give a different error message. Also
3387 handle the case of an unnamed parameter by not trying
3388 to give the name. */
3389
3390 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3391 {
3392 error ("cannot put object with volatile field into register");
3393 return 0;
3394 }
3395
3396 pedwarn ("address of register variable `%s' requested",
3397 IDENTIFIER_POINTER (DECL_NAME (x)));
3398 }
3399 put_var_into_stack (x);
3400
3401 /* drops in */
3402 case FUNCTION_DECL:
3403 TREE_ADDRESSABLE (x) = 1;
3404 #if 0 /* poplevel deals with this now. */
3405 if (DECL_CONTEXT (x) == 0)
3406 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3407 #endif
3408
3409 default:
3410 return 1;
3411 }
3412 }
3413 \f
3414 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3415
3416 tree
3417 build_conditional_expr (ifexp, op1, op2)
3418 tree ifexp, op1, op2;
3419 {
3420 register tree type1;
3421 register tree type2;
3422 register enum tree_code code1;
3423 register enum tree_code code2;
3424 register tree result_type = NULL;
3425 tree orig_op1 = op1, orig_op2 = op2;
3426
3427 ifexp = truthvalue_conversion (default_conversion (ifexp));
3428
3429 #if 0 /* Produces wrong result if within sizeof. */
3430 /* Don't promote the operands separately if they promote
3431 the same way. Return the unpromoted type and let the combined
3432 value get promoted if necessary. */
3433
3434 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3435 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3436 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3437 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3438 {
3439 if (TREE_CODE (ifexp) == INTEGER_CST)
3440 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3441
3442 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3443 }
3444 #endif
3445
3446 /* Promote both alternatives. */
3447
3448 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3449 op1 = default_conversion (op1);
3450 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3451 op2 = default_conversion (op2);
3452
3453 if (TREE_CODE (ifexp) == ERROR_MARK
3454 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3455 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3456 return error_mark_node;
3457
3458 type1 = TREE_TYPE (op1);
3459 code1 = TREE_CODE (type1);
3460 type2 = TREE_TYPE (op2);
3461 code2 = TREE_CODE (type2);
3462
3463 /* Quickly detect the usual case where op1 and op2 have the same type
3464 after promotion. */
3465 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3466 {
3467 if (type1 == type2)
3468 result_type = type1;
3469 else
3470 result_type = TYPE_MAIN_VARIANT (type1);
3471 }
3472 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
3473 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
3474 {
3475 result_type = common_type (type1, type2);
3476 }
3477 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3478 {
3479 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3480 pedwarn ("ANSI C forbids conditional expr with only one void side");
3481 result_type = void_type_node;
3482 }
3483 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3484 {
3485 if (comp_target_types (type1, type2))
3486 result_type = common_type (type1, type2);
3487 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3488 && TREE_CODE (orig_op1) != NOP_EXPR)
3489 result_type = qualify_type (type2, type1);
3490 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3491 && TREE_CODE (orig_op2) != NOP_EXPR)
3492 result_type = qualify_type (type1, type2);
3493 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
3494 {
3495 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3496 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3497 result_type = qualify_type (type1, type2);
3498 }
3499 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
3500 {
3501 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3502 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3503 result_type = qualify_type (type2, type1);
3504 }
3505 else
3506 {
3507 pedwarn ("pointer type mismatch in conditional expression");
3508 result_type = build_pointer_type (void_type_node);
3509 }
3510 }
3511 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3512 {
3513 if (! integer_zerop (op2))
3514 pedwarn ("pointer/integer type mismatch in conditional expression");
3515 else
3516 {
3517 op2 = null_pointer_node;
3518 #if 0 /* The spec seems to say this is permitted. */
3519 if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
3520 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3521 #endif
3522 }
3523 result_type = type1;
3524 }
3525 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3526 {
3527 if (!integer_zerop (op1))
3528 pedwarn ("pointer/integer type mismatch in conditional expression");
3529 else
3530 {
3531 op1 = null_pointer_node;
3532 #if 0 /* The spec seems to say this is permitted. */
3533 if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
3534 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3535 #endif
3536 }
3537 result_type = type2;
3538 }
3539
3540 if (!result_type)
3541 {
3542 if (flag_cond_mismatch)
3543 result_type = void_type_node;
3544 else
3545 {
3546 error ("type mismatch in conditional expression");
3547 return error_mark_node;
3548 }
3549 }
3550
3551 /* Merge const and volatile flags of the incoming types. */
3552 result_type
3553 = build_type_variant (result_type,
3554 TREE_READONLY (op1) || TREE_READONLY (op2),
3555 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3556
3557 if (result_type != TREE_TYPE (op1))
3558 op1 = convert_and_check (result_type, op1);
3559 if (result_type != TREE_TYPE (op2))
3560 op2 = convert_and_check (result_type, op2);
3561
3562 #if 0
3563 if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
3564 {
3565 result_type = TREE_TYPE (op1);
3566 if (TREE_CONSTANT (ifexp))
3567 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3568
3569 if (TYPE_MODE (result_type) == BLKmode)
3570 {
3571 register tree tempvar
3572 = build_decl (VAR_DECL, NULL_TREE, result_type);
3573 register tree xop1 = build_modify_expr (tempvar, op1);
3574 register tree xop2 = build_modify_expr (tempvar, op2);
3575 register tree result = fold (build (COND_EXPR, result_type,
3576 ifexp, xop1, xop2));
3577
3578 layout_decl (tempvar, TYPE_ALIGN (result_type));
3579 /* No way to handle variable-sized objects here.
3580 I fear that the entire handling of BLKmode conditional exprs
3581 needs to be redone. */
3582 if (TREE_CODE (DECL_SIZE (tempvar)) != INTEGER_CST)
3583 abort ();
3584 DECL_RTL (tempvar)
3585 = assign_stack_local (DECL_MODE (tempvar),
3586 (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
3587 + BITS_PER_UNIT - 1)
3588 / BITS_PER_UNIT,
3589 0);
3590
3591 TREE_SIDE_EFFECTS (result)
3592 = TREE_SIDE_EFFECTS (ifexp) | TREE_SIDE_EFFECTS (op1)
3593 | TREE_SIDE_EFFECTS (op2);
3594 return build (COMPOUND_EXPR, result_type, result, tempvar);
3595 }
3596 }
3597 #endif /* 0 */
3598
3599 if (TREE_CODE (ifexp) == INTEGER_CST)
3600 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3601
3602 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3603 }
3604 \f
3605 /* Given a list of expressions, return a compound expression
3606 that performs them all and returns the value of the last of them. */
3607
3608 tree
3609 build_compound_expr (list)
3610 tree list;
3611 {
3612 return internal_build_compound_expr (list, TRUE);
3613 }
3614
3615 static tree
3616 internal_build_compound_expr (list, first_p)
3617 tree list;
3618 int first_p;
3619 {
3620 register tree rest;
3621
3622 if (TREE_CHAIN (list) == 0)
3623 {
3624 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3625 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3626
3627 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3628 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3629 list = TREE_OPERAND (list, 0);
3630 #endif
3631
3632 /* Don't let (0, 0) be null pointer constant. */
3633 if (!first_p && integer_zerop (TREE_VALUE (list)))
3634 return non_lvalue (TREE_VALUE (list));
3635 return TREE_VALUE (list);
3636 }
3637
3638 if (TREE_CHAIN (list) != 0 && TREE_CHAIN (TREE_CHAIN (list)) == 0)
3639 {
3640 /* Convert arrays to pointers when there really is a comma operator. */
3641 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list)))) == ARRAY_TYPE)
3642 TREE_VALUE (TREE_CHAIN (list))
3643 = default_conversion (TREE_VALUE (TREE_CHAIN (list)));
3644 }
3645
3646 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3647
3648 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3649 {
3650 /* The left-hand operand of a comma expression is like an expression
3651 statement: with -W or -Wunused, we should warn if it doesn't have
3652 any side-effects, unless it was explicitly cast to (void). */
3653 if ((extra_warnings || warn_unused)
3654 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3655 && TREE_TYPE (TREE_VALUE (list)) == void_type_node))
3656 warning ("left-hand operand of comma expression has no effect");
3657
3658 /* When pedantic, a compound expression can be neither an lvalue
3659 nor an integer constant expression. */
3660 if (! pedantic)
3661 return rest;
3662 }
3663
3664 /* With -Wunused, we should also warn if the left-hand operand does have
3665 side-effects, but computes a value which is not used. For example, in
3666 `foo() + bar(), baz()' the result of the `+' operator is not used,
3667 so we should issue a warning. */
3668 else if (warn_unused)
3669 warn_if_unused_value (TREE_VALUE (list));
3670
3671 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3672 }
3673
3674 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3675
3676 tree
3677 build_c_cast (type, expr)
3678 register tree type;
3679 tree expr;
3680 {
3681 register tree value = expr;
3682
3683 if (type == error_mark_node || expr == error_mark_node)
3684 return error_mark_node;
3685 type = TYPE_MAIN_VARIANT (type);
3686
3687 #if 0
3688 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3689 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3690 value = TREE_OPERAND (value, 0);
3691 #endif
3692
3693 if (TREE_CODE (type) == ARRAY_TYPE)
3694 {
3695 error ("cast specifies array type");
3696 return error_mark_node;
3697 }
3698
3699 if (TREE_CODE (type) == FUNCTION_TYPE)
3700 {
3701 error ("cast specifies function type");
3702 return error_mark_node;
3703 }
3704
3705 if (type == TREE_TYPE (value))
3706 {
3707 if (pedantic)
3708 {
3709 if (TREE_CODE (type) == RECORD_TYPE
3710 || TREE_CODE (type) == UNION_TYPE)
3711 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3712 }
3713 }
3714 else if (TREE_CODE (type) == UNION_TYPE)
3715 {
3716 tree field;
3717 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3718 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3719 value = default_conversion (value);
3720
3721 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3722 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3723 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3724 break;
3725
3726 if (field)
3727 {
3728 char *name;
3729 tree t;
3730
3731 if (pedantic)
3732 pedwarn ("ANSI C forbids casts to union type");
3733 if (TYPE_NAME (type) != 0)
3734 {
3735 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3736 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3737 else
3738 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3739 }
3740 else
3741 name = "";
3742 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3743 build_tree_list (field, value)),
3744 0, 0);
3745 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3746 return t;
3747 }
3748 error ("cast to union type from type not present in union");
3749 return error_mark_node;
3750 }
3751 else
3752 {
3753 tree otype, ovalue;
3754
3755 /* If casting to void, avoid the error that would come
3756 from default_conversion in the case of a non-lvalue array. */
3757 if (type == void_type_node)
3758 return build1 (CONVERT_EXPR, type, value);
3759
3760 /* Convert functions and arrays to pointers,
3761 but don't convert any other types. */
3762 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3763 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3764 value = default_conversion (value);
3765 otype = TREE_TYPE (value);
3766
3767 /* Optionally warn about potentially worrisome casts. */
3768
3769 if (warn_cast_qual
3770 && TREE_CODE (type) == POINTER_TYPE
3771 && TREE_CODE (otype) == POINTER_TYPE)
3772 {
3773 /* Go to the innermost object being pointed to. */
3774 tree in_type = type;
3775 tree in_otype = otype;
3776
3777 while (TREE_CODE (in_type) == POINTER_TYPE)
3778 in_type = TREE_TYPE (in_type);
3779 while (TREE_CODE (in_otype) == POINTER_TYPE)
3780 in_otype = TREE_TYPE (in_otype);
3781
3782 if (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type))
3783 /* There are qualifiers present in IN_OTYPE that are not
3784 present in IN_TYPE. */
3785 pedwarn ("cast discards qualifiers from pointer target type");
3786 }
3787
3788 /* Warn about possible alignment problems. */
3789 if (STRICT_ALIGNMENT && warn_cast_align
3790 && TREE_CODE (type) == POINTER_TYPE
3791 && TREE_CODE (otype) == POINTER_TYPE
3792 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3793 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3794 /* Don't warn about opaque types, where the actual alignment
3795 restriction is unknown. */
3796 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3797 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3798 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3799 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3800 warning ("cast increases required alignment of target type");
3801
3802 if (TREE_CODE (type) == INTEGER_TYPE
3803 && TREE_CODE (otype) == POINTER_TYPE
3804 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3805 && !TREE_CONSTANT (value))
3806 warning ("cast from pointer to integer of different size");
3807
3808 if (warn_bad_function_cast
3809 && TREE_CODE (value) == CALL_EXPR
3810 && TREE_CODE (type) != TREE_CODE (otype))
3811 warning ("cast does not match function type");
3812
3813 if (TREE_CODE (type) == POINTER_TYPE
3814 && TREE_CODE (otype) == INTEGER_TYPE
3815 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3816 #if 0
3817 /* Don't warn about converting 0 to pointer,
3818 provided the 0 was explicit--not cast or made by folding. */
3819 && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value))
3820 #endif
3821 /* Don't warn about converting any constant. */
3822 && !TREE_CONSTANT (value))
3823 warning ("cast to pointer from integer of different size");
3824
3825 ovalue = value;
3826 value = convert (type, value);
3827
3828 /* Ignore any integer overflow caused by the cast. */
3829 if (TREE_CODE (value) == INTEGER_CST)
3830 {
3831 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3832 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3833 }
3834 }
3835
3836 /* Pedantically, don't ley (void *) (FOO *) 0 be a null pointer constant. */
3837 if (pedantic && TREE_CODE (value) == INTEGER_CST
3838 && TREE_CODE (expr) == INTEGER_CST
3839 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3840 value = non_lvalue (value);
3841
3842 /* If pedantic, don't let a cast be an lvalue. */
3843 if (value == expr && pedantic)
3844 value = non_lvalue (value);
3845
3846 return value;
3847 }
3848 \f
3849 /* Build an assignment expression of lvalue LHS from value RHS.
3850 MODIFYCODE is the code for a binary operator that we use
3851 to combine the old value of LHS with RHS to get the new value.
3852 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3853
3854 tree
3855 build_modify_expr (lhs, modifycode, rhs)
3856 tree lhs, rhs;
3857 enum tree_code modifycode;
3858 {
3859 register tree result;
3860 tree newrhs;
3861 tree lhstype = TREE_TYPE (lhs);
3862 tree olhstype = lhstype;
3863
3864 /* Types that aren't fully specified cannot be used in assignments. */
3865 lhs = require_complete_type (lhs);
3866
3867 /* Avoid duplicate error messages from operands that had errors. */
3868 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3869 return error_mark_node;
3870
3871 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3872 /* Do not use STRIP_NOPS here. We do not want an enumerator
3873 whose value is 0 to count as a null pointer constant. */
3874 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3875 rhs = TREE_OPERAND (rhs, 0);
3876
3877 newrhs = rhs;
3878
3879 /* Handle control structure constructs used as "lvalues". */
3880
3881 switch (TREE_CODE (lhs))
3882 {
3883 /* Handle (a, b) used as an "lvalue". */
3884 case COMPOUND_EXPR:
3885 pedantic_lvalue_warning (COMPOUND_EXPR);
3886 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
3887 modifycode, rhs);
3888 if (TREE_CODE (newrhs) == ERROR_MARK)
3889 return error_mark_node;
3890 return build (COMPOUND_EXPR, lhstype,
3891 TREE_OPERAND (lhs, 0), newrhs);
3892
3893 /* Handle (a ? b : c) used as an "lvalue". */
3894 case COND_EXPR:
3895 pedantic_lvalue_warning (COND_EXPR);
3896 rhs = save_expr (rhs);
3897 {
3898 /* Produce (a ? (b = rhs) : (c = rhs))
3899 except that the RHS goes through a save-expr
3900 so the code to compute it is only emitted once. */
3901 tree cond
3902 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3903 build_modify_expr (TREE_OPERAND (lhs, 1),
3904 modifycode, rhs),
3905 build_modify_expr (TREE_OPERAND (lhs, 2),
3906 modifycode, rhs));
3907 if (TREE_CODE (cond) == ERROR_MARK)
3908 return cond;
3909 /* Make sure the code to compute the rhs comes out
3910 before the split. */
3911 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3912 /* But cast it to void to avoid an "unused" error. */
3913 convert (void_type_node, rhs), cond);
3914 }
3915 default:
3916 break;
3917 }
3918
3919 /* If a binary op has been requested, combine the old LHS value with the RHS
3920 producing the value we should actually store into the LHS. */
3921
3922 if (modifycode != NOP_EXPR)
3923 {
3924 lhs = stabilize_reference (lhs);
3925 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3926 }
3927
3928 /* Handle a cast used as an "lvalue".
3929 We have already performed any binary operator using the value as cast.
3930 Now convert the result to the cast type of the lhs,
3931 and then true type of the lhs and store it there;
3932 then convert result back to the cast type to be the value
3933 of the assignment. */
3934
3935 switch (TREE_CODE (lhs))
3936 {
3937 case NOP_EXPR:
3938 case CONVERT_EXPR:
3939 case FLOAT_EXPR:
3940 case FIX_TRUNC_EXPR:
3941 case FIX_FLOOR_EXPR:
3942 case FIX_ROUND_EXPR:
3943 case FIX_CEIL_EXPR:
3944 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3945 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3946 newrhs = default_conversion (newrhs);
3947 {
3948 tree inner_lhs = TREE_OPERAND (lhs, 0);
3949 tree result;
3950 result = build_modify_expr (inner_lhs, NOP_EXPR,
3951 convert (TREE_TYPE (inner_lhs),
3952 convert (lhstype, newrhs)));
3953 if (TREE_CODE (result) == ERROR_MARK)
3954 return result;
3955 pedantic_lvalue_warning (CONVERT_EXPR);
3956 return convert (TREE_TYPE (lhs), result);
3957 }
3958
3959 default:
3960 break;
3961 }
3962
3963 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3964 Reject anything strange now. */
3965
3966 if (!lvalue_or_else (lhs, "assignment"))
3967 return error_mark_node;
3968
3969 /* Warn about storing in something that is `const'. */
3970
3971 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3972 || ((TREE_CODE (lhstype) == RECORD_TYPE
3973 || TREE_CODE (lhstype) == UNION_TYPE)
3974 && C_TYPE_FIELDS_READONLY (lhstype)))
3975 readonly_warning (lhs, "assignment");
3976
3977 /* If storing into a structure or union member,
3978 it has probably been given type `int'.
3979 Compute the type that would go with
3980 the actual amount of storage the member occupies. */
3981
3982 if (TREE_CODE (lhs) == COMPONENT_REF
3983 && (TREE_CODE (lhstype) == INTEGER_TYPE
3984 || TREE_CODE (lhstype) == REAL_TYPE
3985 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3986 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3987
3988 /* If storing in a field that is in actuality a short or narrower than one,
3989 we must store in the field in its actual type. */
3990
3991 if (lhstype != TREE_TYPE (lhs))
3992 {
3993 lhs = copy_node (lhs);
3994 TREE_TYPE (lhs) = lhstype;
3995 }
3996
3997 /* Convert new value to destination type. */
3998
3999 newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
4000 NULL_TREE, NULL_TREE, 0);
4001 if (TREE_CODE (newrhs) == ERROR_MARK)
4002 return error_mark_node;
4003
4004 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4005 TREE_SIDE_EFFECTS (result) = 1;
4006
4007 /* If we got the LHS in a different type for storing in,
4008 convert the result back to the nominal type of LHS
4009 so that the value we return always has the same type
4010 as the LHS argument. */
4011
4012 if (olhstype == TREE_TYPE (result))
4013 return result;
4014 return convert_for_assignment (olhstype, result, "assignment",
4015 NULL_TREE, NULL_TREE, 0);
4016 }
4017 \f
4018 /* Convert value RHS to type TYPE as preparation for an assignment
4019 to an lvalue of type TYPE.
4020 The real work of conversion is done by `convert'.
4021 The purpose of this function is to generate error messages
4022 for assignments that are not allowed in C.
4023 ERRTYPE is a string to use in error messages:
4024 "assignment", "return", etc. If it is null, this is parameter passing
4025 for a function call (and different error messages are output). Otherwise,
4026 it may be a name stored in the spelling stack and interpreted by
4027 get_spelling.
4028
4029 FUNNAME is the name of the function being called,
4030 as an IDENTIFIER_NODE, or null.
4031 PARMNUM is the number of the argument, for printing in error messages. */
4032
4033 static tree
4034 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4035 tree type, rhs;
4036 char *errtype;
4037 tree fundecl, funname;
4038 int parmnum;
4039 {
4040 register enum tree_code codel = TREE_CODE (type);
4041 register tree rhstype;
4042 register enum tree_code coder;
4043
4044 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4045 /* Do not use STRIP_NOPS here. We do not want an enumerator
4046 whose value is 0 to count as a null pointer constant. */
4047 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4048 rhs = TREE_OPERAND (rhs, 0);
4049
4050 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4051 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4052 rhs = default_conversion (rhs);
4053 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4054 rhs = decl_constant_value (rhs);
4055
4056 rhstype = TREE_TYPE (rhs);
4057 coder = TREE_CODE (rhstype);
4058
4059 if (coder == ERROR_MARK)
4060 return error_mark_node;
4061
4062 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4063 {
4064 overflow_warning (rhs);
4065 /* Check for Objective-C protocols. This will issue a warning if
4066 there are protocol violations. No need to use the return value. */
4067 maybe_objc_comptypes (type, rhstype, 0);
4068 return rhs;
4069 }
4070
4071 if (coder == VOID_TYPE)
4072 {
4073 error ("void value not ignored as it ought to be");
4074 return error_mark_node;
4075 }
4076 /* Arithmetic types all interconvert, and enum is treated like int. */
4077 if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE
4078 || codel == COMPLEX_TYPE)
4079 && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE
4080 || coder == COMPLEX_TYPE))
4081 return convert_and_check (type, rhs);
4082
4083 /* Conversion to a transparent union from its member types.
4084 This applies only to function arguments. */
4085 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4086 {
4087 tree memb_types;
4088 tree marginal_memb_type = 0;
4089
4090 for (memb_types = TYPE_FIELDS (type); memb_types;
4091 memb_types = TREE_CHAIN (memb_types))
4092 {
4093 tree memb_type = TREE_TYPE (memb_types);
4094
4095 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4096 TYPE_MAIN_VARIANT (rhstype)))
4097 break;
4098
4099 if (TREE_CODE (memb_type) != POINTER_TYPE)
4100 continue;
4101
4102 if (coder == POINTER_TYPE)
4103 {
4104 register tree ttl = TREE_TYPE (memb_type);
4105 register tree ttr = TREE_TYPE (rhstype);
4106
4107 /* Any non-function converts to a [const][volatile] void *
4108 and vice versa; otherwise, targets must be the same.
4109 Meanwhile, the lhs target must have all the qualifiers of
4110 the rhs. */
4111 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4112 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4113 || comp_target_types (memb_type, rhstype))
4114 {
4115 /* If this type won't generate any warnings, use it. */
4116 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4117 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4118 && TREE_CODE (ttl) == FUNCTION_TYPE)
4119 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4120 == TYPE_QUALS (ttr))
4121 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4122 == TYPE_QUALS (ttl))))
4123 break;
4124
4125 /* Keep looking for a better type, but remember this one. */
4126 if (! marginal_memb_type)
4127 marginal_memb_type = memb_type;
4128 }
4129 }
4130
4131 /* Can convert integer zero to any pointer type. */
4132 if (integer_zerop (rhs)
4133 || (TREE_CODE (rhs) == NOP_EXPR
4134 && integer_zerop (TREE_OPERAND (rhs, 0))))
4135 {
4136 rhs = null_pointer_node;
4137 break;
4138 }
4139 }
4140
4141 if (memb_types || marginal_memb_type)
4142 {
4143 if (! memb_types)
4144 {
4145 /* We have only a marginally acceptable member type;
4146 it needs a warning. */
4147 register tree ttl = TREE_TYPE (marginal_memb_type);
4148 register tree ttr = TREE_TYPE (rhstype);
4149
4150 /* Const and volatile mean something different for function
4151 types, so the usual warnings are not appropriate. */
4152 if (TREE_CODE (ttr) == FUNCTION_TYPE
4153 && TREE_CODE (ttl) == FUNCTION_TYPE)
4154 {
4155 /* Because const and volatile on functions are
4156 restrictions that say the function will not do
4157 certain things, it is okay to use a const or volatile
4158 function where an ordinary one is wanted, but not
4159 vice-versa. */
4160 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4161 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4162 get_spelling (errtype), funname,
4163 parmnum);
4164 }
4165 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4166 warn_for_assignment ("%s discards qualifiers from pointer target type",
4167 get_spelling (errtype), funname,
4168 parmnum);
4169 }
4170
4171 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4172 pedwarn ("ANSI C prohibits argument conversion to union type");
4173
4174 return build1 (NOP_EXPR, type, rhs);
4175 }
4176 }
4177
4178 /* Conversions among pointers */
4179 else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
4180 {
4181 register tree ttl = TREE_TYPE (type);
4182 register tree ttr = TREE_TYPE (rhstype);
4183
4184 /* Any non-function converts to a [const][volatile] void *
4185 and vice versa; otherwise, targets must be the same.
4186 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4187 if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4188 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4189 || comp_target_types (type, rhstype)
4190 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4191 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4192 {
4193 if (pedantic
4194 && ((TYPE_MAIN_VARIANT (ttl) == void_type_node
4195 && TREE_CODE (ttr) == FUNCTION_TYPE)
4196 ||
4197 (TYPE_MAIN_VARIANT (ttr) == void_type_node
4198 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4199 which are not ANSI null ptr constants. */
4200 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4201 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4202 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
4203 get_spelling (errtype), funname, parmnum);
4204 /* Const and volatile mean something different for function types,
4205 so the usual warnings are not appropriate. */
4206 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4207 && TREE_CODE (ttl) != FUNCTION_TYPE)
4208 {
4209 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4210 warn_for_assignment ("%s discards qualifiers from pointer target type",
4211 get_spelling (errtype), funname, parmnum);
4212 /* If this is not a case of ignoring a mismatch in signedness,
4213 no warning. */
4214 else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
4215 || TYPE_MAIN_VARIANT (ttr) == void_type_node
4216 || comp_target_types (type, rhstype))
4217 ;
4218 /* If there is a mismatch, do warn. */
4219 else if (pedantic)
4220 warn_for_assignment ("pointer targets in %s differ in signedness",
4221 get_spelling (errtype), funname, parmnum);
4222 }
4223 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4224 && TREE_CODE (ttr) == FUNCTION_TYPE)
4225 {
4226 /* Because const and volatile on functions are restrictions
4227 that say the function will not do certain things,
4228 it is okay to use a const or volatile function
4229 where an ordinary one is wanted, but not vice-versa. */
4230 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4231 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4232 get_spelling (errtype), funname, parmnum);
4233 }
4234 }
4235 else
4236 warn_for_assignment ("%s from incompatible pointer type",
4237 get_spelling (errtype), funname, parmnum);
4238 return convert (type, rhs);
4239 }
4240 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4241 {
4242 /* An explicit constant 0 can convert to a pointer,
4243 or one that results from arithmetic, even including
4244 a cast to integer type. */
4245 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4246 &&
4247 ! (TREE_CODE (rhs) == NOP_EXPR
4248 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4249 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4250 && integer_zerop (TREE_OPERAND (rhs, 0))))
4251 {
4252 warn_for_assignment ("%s makes pointer from integer without a cast",
4253 get_spelling (errtype), funname, parmnum);
4254 return convert (type, rhs);
4255 }
4256 return null_pointer_node;
4257 }
4258 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4259 {
4260 warn_for_assignment ("%s makes integer from pointer without a cast",
4261 get_spelling (errtype), funname, parmnum);
4262 return convert (type, rhs);
4263 }
4264
4265 if (!errtype)
4266 {
4267 if (funname)
4268 {
4269 tree selector = maybe_building_objc_message_expr ();
4270
4271 if (selector && parmnum > 2)
4272 error ("incompatible type for argument %d of `%s'",
4273 parmnum - 2, IDENTIFIER_POINTER (selector));
4274 else
4275 error ("incompatible type for argument %d of `%s'",
4276 parmnum, IDENTIFIER_POINTER (funname));
4277 }
4278 else
4279 error ("incompatible type for argument %d of indirect function call",
4280 parmnum);
4281 }
4282 else
4283 error ("incompatible types in %s", get_spelling (errtype));
4284
4285 return error_mark_node;
4286 }
4287
4288 /* Print a warning using MSG.
4289 It gets OPNAME as its one parameter.
4290 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4291 FUNCTION and ARGNUM are handled specially if we are building an
4292 Objective-C selector. */
4293
4294 static void
4295 warn_for_assignment (msg, opname, function, argnum)
4296 char *msg;
4297 char *opname;
4298 tree function;
4299 int argnum;
4300 {
4301 static char argstring[] = "passing arg %d of `%s'";
4302 static char argnofun[] = "passing arg %d";
4303
4304 if (opname == 0)
4305 {
4306 tree selector = maybe_building_objc_message_expr ();
4307
4308 if (selector && argnum > 2)
4309 {
4310 function = selector;
4311 argnum -= 2;
4312 }
4313 if (function)
4314 {
4315 /* Function name is known; supply it. */
4316 opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4317 + sizeof (argstring) + 25 /*%d*/ + 1);
4318 sprintf (opname, argstring, argnum, IDENTIFIER_POINTER (function));
4319 }
4320 else
4321 {
4322 /* Function name unknown (call through ptr); just give arg number. */
4323 opname = (char *) alloca (sizeof (argnofun) + 25 /*%d*/ + 1);
4324 sprintf (opname, argnofun, argnum);
4325 }
4326 }
4327 pedwarn (msg, opname);
4328 }
4329 \f
4330 /* Return nonzero if VALUE is a valid constant-valued expression
4331 for use in initializing a static variable; one that can be an
4332 element of a "constant" initializer.
4333
4334 Return null_pointer_node if the value is absolute;
4335 if it is relocatable, return the variable that determines the relocation.
4336 We assume that VALUE has been folded as much as possible;
4337 therefore, we do not need to check for such things as
4338 arithmetic-combinations of integers. */
4339
4340 tree
4341 initializer_constant_valid_p (value, endtype)
4342 tree value;
4343 tree endtype;
4344 {
4345 switch (TREE_CODE (value))
4346 {
4347 case CONSTRUCTOR:
4348 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
4349 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
4350 && TREE_CONSTANT (value)
4351 && CONSTRUCTOR_ELTS (value))
4352 return
4353 initializer_constant_valid_p (TREE_VALUE (CONSTRUCTOR_ELTS (value)),
4354 endtype);
4355
4356 return TREE_STATIC (value) ? null_pointer_node : 0;
4357
4358 case INTEGER_CST:
4359 case REAL_CST:
4360 case STRING_CST:
4361 case COMPLEX_CST:
4362 return null_pointer_node;
4363
4364 case ADDR_EXPR:
4365 return TREE_OPERAND (value, 0);
4366
4367 case NON_LVALUE_EXPR:
4368 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4369
4370 case CONVERT_EXPR:
4371 case NOP_EXPR:
4372 /* Allow conversions between pointer types. */
4373 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4374 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
4375 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4376
4377 /* Allow conversions between real types. */
4378 if (TREE_CODE (TREE_TYPE (value)) == REAL_TYPE
4379 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == REAL_TYPE)
4380 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4381
4382 /* Allow length-preserving conversions between integer types. */
4383 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4384 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE
4385 && (TYPE_PRECISION (TREE_TYPE (value))
4386 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4387 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
4388
4389 /* Allow conversions between other integer types only if
4390 explicit value. */
4391 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4392 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4393 {
4394 tree inner = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4395 endtype);
4396 if (inner == null_pointer_node)
4397 return null_pointer_node;
4398 return 0;
4399 }
4400
4401 /* Allow (int) &foo provided int is as wide as a pointer. */
4402 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
4403 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
4404 && (TYPE_PRECISION (TREE_TYPE (value))
4405 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0)))))
4406 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4407 endtype);
4408
4409 /* Likewise conversions from int to pointers, but also allow
4410 conversions from 0. */
4411 if (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
4412 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == INTEGER_TYPE)
4413 {
4414 if (integer_zerop (TREE_OPERAND (value, 0)))
4415 return null_pointer_node;
4416 else if (TYPE_PRECISION (TREE_TYPE (value))
4417 <= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (value, 0))))
4418 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4419 endtype);
4420 }
4421
4422 /* Allow conversions to union types if the value inside is okay. */
4423 if (TREE_CODE (TREE_TYPE (value)) == UNION_TYPE)
4424 return initializer_constant_valid_p (TREE_OPERAND (value, 0),
4425 endtype);
4426 return 0;
4427
4428 case PLUS_EXPR:
4429 if (TREE_CODE (endtype) == INTEGER_TYPE
4430 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4431 return 0;
4432 {
4433 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4434 endtype);
4435 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4436 endtype);
4437 /* If either term is absolute, use the other terms relocation. */
4438 if (valid0 == null_pointer_node)
4439 return valid1;
4440 if (valid1 == null_pointer_node)
4441 return valid0;
4442 return 0;
4443 }
4444
4445 case MINUS_EXPR:
4446 if (TREE_CODE (endtype) == INTEGER_TYPE
4447 && TYPE_PRECISION (endtype) < POINTER_SIZE)
4448 return 0;
4449 {
4450 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
4451 endtype);
4452 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
4453 endtype);
4454 /* Win if second argument is absolute. */
4455 if (valid1 == null_pointer_node)
4456 return valid0;
4457 /* Win if both arguments have the same relocation.
4458 Then the value is absolute. */
4459 if (valid0 == valid1)
4460 return null_pointer_node;
4461 return 0;
4462 }
4463
4464 default:
4465 return 0;
4466 }
4467 }
4468
4469 /* If VALUE is a compound expr all of whose expressions are constant, then
4470 return its value. Otherwise, return error_mark_node.
4471
4472 This is for handling COMPOUND_EXPRs as initializer elements
4473 which is allowed with a warning when -pedantic is specified. */
4474
4475 static tree
4476 valid_compound_expr_initializer (value, endtype)
4477 tree value;
4478 tree endtype;
4479 {
4480 if (TREE_CODE (value) == COMPOUND_EXPR)
4481 {
4482 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4483 == error_mark_node)
4484 return error_mark_node;
4485 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4486 endtype);
4487 }
4488 else if (! TREE_CONSTANT (value)
4489 && ! initializer_constant_valid_p (value, endtype))
4490 return error_mark_node;
4491 else
4492 return value;
4493 }
4494 \f
4495 /* Perform appropriate conversions on the initial value of a variable,
4496 store it in the declaration DECL,
4497 and print any error messages that are appropriate.
4498 If the init is invalid, store an ERROR_MARK. */
4499
4500 void
4501 store_init_value (decl, init)
4502 tree decl, init;
4503 {
4504 register tree value, type;
4505
4506 /* If variable's type was invalidly declared, just ignore it. */
4507
4508 type = TREE_TYPE (decl);
4509 if (TREE_CODE (type) == ERROR_MARK)
4510 return;
4511
4512 /* Digest the specified initializer into an expression. */
4513
4514 value = digest_init (type, init, TREE_STATIC (decl),
4515 TREE_STATIC (decl) || pedantic);
4516
4517 /* Store the expression if valid; else report error. */
4518
4519 #if 0
4520 /* Note that this is the only place we can detect the error
4521 in a case such as struct foo bar = (struct foo) { x, y };
4522 where there is one initial value which is a constructor expression. */
4523 if (value == error_mark_node)
4524 ;
4525 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4526 {
4527 error ("initializer for static variable is not constant");
4528 value = error_mark_node;
4529 }
4530 else if (TREE_STATIC (decl)
4531 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4532 {
4533 error ("initializer for static variable uses complicated arithmetic");
4534 value = error_mark_node;
4535 }
4536 else
4537 {
4538 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4539 {
4540 if (! TREE_CONSTANT (value))
4541 pedwarn ("aggregate initializer is not constant");
4542 else if (! TREE_STATIC (value))
4543 pedwarn ("aggregate initializer uses complicated arithmetic");
4544 }
4545 }
4546 #endif
4547
4548 DECL_INITIAL (decl) = value;
4549
4550 /* ANSI wants warnings about out-of-range constant initializers. */
4551 STRIP_TYPE_NOPS (value);
4552 constant_expression_warning (value);
4553 }
4554 \f
4555 /* Methods for storing and printing names for error messages. */
4556
4557 /* Implement a spelling stack that allows components of a name to be pushed
4558 and popped. Each element on the stack is this structure. */
4559
4560 struct spelling
4561 {
4562 int kind;
4563 union
4564 {
4565 int i;
4566 char *s;
4567 } u;
4568 };
4569
4570 #define SPELLING_STRING 1
4571 #define SPELLING_MEMBER 2
4572 #define SPELLING_BOUNDS 3
4573
4574 static struct spelling *spelling; /* Next stack element (unused). */
4575 static struct spelling *spelling_base; /* Spelling stack base. */
4576 static int spelling_size; /* Size of the spelling stack. */
4577
4578 /* Macros to save and restore the spelling stack around push_... functions.
4579 Alternative to SAVE_SPELLING_STACK. */
4580
4581 #define SPELLING_DEPTH() (spelling - spelling_base)
4582 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4583
4584 /* Save and restore the spelling stack around arbitrary C code. */
4585
4586 #define SAVE_SPELLING_DEPTH(code) \
4587 { \
4588 int __depth = SPELLING_DEPTH (); \
4589 code; \
4590 RESTORE_SPELLING_DEPTH (__depth); \
4591 }
4592
4593 /* Push an element on the spelling stack with type KIND and assign VALUE
4594 to MEMBER. */
4595
4596 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4597 { \
4598 int depth = SPELLING_DEPTH (); \
4599 \
4600 if (depth >= spelling_size) \
4601 { \
4602 spelling_size += 10; \
4603 if (spelling_base == 0) \
4604 spelling_base \
4605 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4606 else \
4607 spelling_base \
4608 = (struct spelling *) xrealloc (spelling_base, \
4609 spelling_size * sizeof (struct spelling)); \
4610 RESTORE_SPELLING_DEPTH (depth); \
4611 } \
4612 \
4613 spelling->kind = (KIND); \
4614 spelling->MEMBER = (VALUE); \
4615 spelling++; \
4616 }
4617
4618 /* Push STRING on the stack. Printed literally. */
4619
4620 static void
4621 push_string (string)
4622 char *string;
4623 {
4624 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4625 }
4626
4627 /* Push a member name on the stack. Printed as '.' STRING. */
4628
4629 static void
4630 push_member_name (decl)
4631 tree decl;
4632
4633 {
4634 char *string
4635 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4636 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4637 }
4638
4639 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4640
4641 static void
4642 push_array_bounds (bounds)
4643 int bounds;
4644 {
4645 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4646 }
4647
4648 /* Compute the maximum size in bytes of the printed spelling. */
4649
4650 static int
4651 spelling_length ()
4652 {
4653 register int size = 0;
4654 register struct spelling *p;
4655
4656 for (p = spelling_base; p < spelling; p++)
4657 {
4658 if (p->kind == SPELLING_BOUNDS)
4659 size += 25;
4660 else
4661 size += strlen (p->u.s) + 1;
4662 }
4663
4664 return size;
4665 }
4666
4667 /* Print the spelling to BUFFER and return it. */
4668
4669 static char *
4670 print_spelling (buffer)
4671 register char *buffer;
4672 {
4673 register char *d = buffer;
4674 register char *s;
4675 register struct spelling *p;
4676
4677 for (p = spelling_base; p < spelling; p++)
4678 if (p->kind == SPELLING_BOUNDS)
4679 {
4680 sprintf (d, "[%d]", p->u.i);
4681 d += strlen (d);
4682 }
4683 else
4684 {
4685 if (p->kind == SPELLING_MEMBER)
4686 *d++ = '.';
4687 for (s = p->u.s; (*d = *s++); d++)
4688 ;
4689 }
4690 *d++ = '\0';
4691 return buffer;
4692 }
4693
4694 /* Provide a means to pass component names derived from the spelling stack. */
4695
4696 char initialization_message;
4697
4698 /* Interpret the spelling of the given ERRTYPE message. */
4699
4700 static char *
4701 get_spelling (errtype)
4702 char *errtype;
4703 {
4704 static char *buffer;
4705 static int size = -1;
4706
4707 if (errtype == &initialization_message)
4708 {
4709 /* Avoid counting chars */
4710 static char message[] = "initialization of `%s'";
4711 register int needed = sizeof (message) + spelling_length () + 1;
4712 char *temp;
4713
4714 if (size < 0)
4715 buffer = (char *) xmalloc (size = needed);
4716 if (needed > size)
4717 buffer = (char *) xrealloc (buffer, size = needed);
4718
4719 temp = (char *) alloca (needed);
4720 sprintf (buffer, message, print_spelling (temp));
4721 return buffer;
4722 }
4723
4724 return errtype;
4725 }
4726
4727 /* Issue an error message for a bad initializer component.
4728 FORMAT describes the message. OFWHAT is the name for the component.
4729 LOCAL is a format string for formatting the insertion of the name
4730 into the message.
4731
4732 If OFWHAT is null, the component name is stored on the spelling stack.
4733 If the component name is a null string, then LOCAL is omitted entirely. */
4734
4735 void
4736 error_init (format, local, ofwhat)
4737 char *format, *local, *ofwhat;
4738 {
4739 char *buffer;
4740
4741 if (ofwhat == 0)
4742 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4743 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4744
4745 if (*ofwhat)
4746 sprintf (buffer, local, ofwhat);
4747 else
4748 buffer[0] = 0;
4749
4750 error (format, buffer);
4751 }
4752
4753 /* Issue a pedantic warning for a bad initializer component.
4754 FORMAT describes the message. OFWHAT is the name for the component.
4755 LOCAL is a format string for formatting the insertion of the name
4756 into the message.
4757
4758 If OFWHAT is null, the component name is stored on the spelling stack.
4759 If the component name is a null string, then LOCAL is omitted entirely. */
4760
4761 void
4762 pedwarn_init (format, local, ofwhat)
4763 char *format, *local, *ofwhat;
4764 {
4765 char *buffer;
4766
4767 if (ofwhat == 0)
4768 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4769 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4770
4771 if (*ofwhat)
4772 sprintf (buffer, local, ofwhat);
4773 else
4774 buffer[0] = 0;
4775
4776 pedwarn (format, buffer);
4777 }
4778
4779 /* Issue a warning for a bad initializer component.
4780 FORMAT describes the message. OFWHAT is the name for the component.
4781 LOCAL is a format string for formatting the insertion of the name
4782 into the message.
4783
4784 If OFWHAT is null, the component name is stored on the spelling stack.
4785 If the component name is a null string, then LOCAL is omitted entirely. */
4786
4787 static void
4788 warning_init (format, local, ofwhat)
4789 char *format, *local, *ofwhat;
4790 {
4791 char *buffer;
4792
4793 if (ofwhat == 0)
4794 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4795 buffer = (char *) alloca (strlen (local) + strlen (ofwhat) + 2);
4796
4797 if (*ofwhat)
4798 sprintf (buffer, local, ofwhat);
4799 else
4800 buffer[0] = 0;
4801
4802 warning (format, buffer);
4803 }
4804 \f
4805 /* Digest the parser output INIT as an initializer for type TYPE.
4806 Return a C expression of type TYPE to represent the initial value.
4807
4808 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4809 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4810 applies only to elements of constructors. */
4811
4812 static tree
4813 digest_init (type, init, require_constant, constructor_constant)
4814 tree type, init;
4815 int require_constant, constructor_constant;
4816 {
4817 enum tree_code code = TREE_CODE (type);
4818 tree inside_init = init;
4819
4820 if (init == error_mark_node)
4821 return init;
4822
4823 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4824 /* Do not use STRIP_NOPS here. We do not want an enumerator
4825 whose value is 0 to count as a null pointer constant. */
4826 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4827 inside_init = TREE_OPERAND (init, 0);
4828
4829 /* Initialization of an array of chars from a string constant
4830 optionally enclosed in braces. */
4831
4832 if (code == ARRAY_TYPE)
4833 {
4834 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4835 if ((typ1 == char_type_node
4836 || typ1 == signed_char_type_node
4837 || typ1 == unsigned_char_type_node
4838 || typ1 == unsigned_wchar_type_node
4839 || typ1 == signed_wchar_type_node)
4840 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4841 {
4842 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4843 TYPE_MAIN_VARIANT (type)))
4844 return inside_init;
4845
4846 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4847 != char_type_node)
4848 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4849 {
4850 error_init ("char-array%s initialized from wide string",
4851 " `%s'", NULL);
4852 return error_mark_node;
4853 }
4854 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4855 == char_type_node)
4856 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4857 {
4858 error_init ("int-array%s initialized from non-wide string",
4859 " `%s'", NULL);
4860 return error_mark_node;
4861 }
4862
4863 TREE_TYPE (inside_init) = type;
4864 if (TYPE_DOMAIN (type) != 0
4865 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
4866 {
4867 register int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4868 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
4869 /* Subtract 1 (or sizeof (wchar_t))
4870 because it's ok to ignore the terminating null char
4871 that is counted in the length of the constant. */
4872 if (size < TREE_STRING_LENGTH (inside_init)
4873 - (TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node)
4874 ? TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT
4875 : 1))
4876 pedwarn_init (
4877 "initializer-string for array of chars%s is too long",
4878 " `%s'", NULL);
4879 }
4880 return inside_init;
4881 }
4882 }
4883
4884 /* Any type can be initialized
4885 from an expression of the same type, optionally with braces. */
4886
4887 if (inside_init && TREE_TYPE (inside_init) != 0
4888 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4889 TYPE_MAIN_VARIANT (type))
4890 || (code == ARRAY_TYPE
4891 && comptypes (TREE_TYPE (inside_init), type))
4892 || (code == POINTER_TYPE
4893 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4894 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4895 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4896 TREE_TYPE (type)))))
4897 {
4898 if (code == POINTER_TYPE
4899 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4900 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4901 inside_init = default_conversion (inside_init);
4902 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4903 && TREE_CODE (inside_init) != CONSTRUCTOR)
4904 {
4905 error_init ("array%s initialized from non-constant array expression",
4906 " `%s'", NULL);
4907 return error_mark_node;
4908 }
4909
4910 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4911 inside_init = decl_constant_value (inside_init);
4912
4913 /* Compound expressions can only occur here if -pedantic or
4914 -pedantic-errors is specified. In the later case, we always want
4915 an error. In the former case, we simply want a warning. */
4916 if (require_constant && pedantic
4917 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4918 {
4919 inside_init
4920 = valid_compound_expr_initializer (inside_init,
4921 TREE_TYPE (inside_init));
4922 if (inside_init == error_mark_node)
4923 error_init ("initializer element%s is not constant",
4924 " for `%s'", NULL);
4925 else
4926 pedwarn_init ("initializer element%s is not constant",
4927 " for `%s'", NULL);
4928 if (flag_pedantic_errors)
4929 inside_init = error_mark_node;
4930 }
4931 else if (require_constant && ! TREE_CONSTANT (inside_init))
4932 {
4933 error_init ("initializer element%s is not constant",
4934 " for `%s'", NULL);
4935 inside_init = error_mark_node;
4936 }
4937 else if (require_constant
4938 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4939 {
4940 error_init ("initializer element%s is not computable at load time",
4941 " for `%s'", NULL);
4942 inside_init = error_mark_node;
4943 }
4944
4945 return inside_init;
4946 }
4947
4948 /* Handle scalar types, including conversions. */
4949
4950 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4951 || code == ENUMERAL_TYPE || code == COMPLEX_TYPE)
4952 {
4953 /* Note that convert_for_assignment calls default_conversion
4954 for arrays and functions. We must not call it in the
4955 case where inside_init is a null pointer constant. */
4956 inside_init
4957 = convert_for_assignment (type, init, "initialization",
4958 NULL_TREE, NULL_TREE, 0);
4959
4960 if (require_constant && ! TREE_CONSTANT (inside_init))
4961 {
4962 error_init ("initializer element%s is not constant",
4963 " for `%s'", NULL);
4964 inside_init = error_mark_node;
4965 }
4966 else if (require_constant
4967 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4968 {
4969 error_init ("initializer element%s is not computable at load time",
4970 " for `%s'", NULL);
4971 inside_init = error_mark_node;
4972 }
4973
4974 return inside_init;
4975 }
4976
4977 /* Come here only for records and arrays. */
4978
4979 if (TYPE_SIZE (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4980 {
4981 error_init ("variable-sized object%s may not be initialized",
4982 " `%s'", NULL);
4983 return error_mark_node;
4984 }
4985
4986 /* Traditionally, you can write struct foo x = 0;
4987 and it initializes the first element of x to 0. */
4988 if (flag_traditional)
4989 {
4990 tree top = 0, prev = 0, otype = type;
4991 while (TREE_CODE (type) == RECORD_TYPE
4992 || TREE_CODE (type) == ARRAY_TYPE
4993 || TREE_CODE (type) == QUAL_UNION_TYPE
4994 || TREE_CODE (type) == UNION_TYPE)
4995 {
4996 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4997 if (prev == 0)
4998 top = temp;
4999 else
5000 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
5001 prev = temp;
5002 if (TREE_CODE (type) == ARRAY_TYPE)
5003 type = TREE_TYPE (type);
5004 else if (TYPE_FIELDS (type))
5005 type = TREE_TYPE (TYPE_FIELDS (type));
5006 else
5007 {
5008 error_init ("invalid initializer%s", " for `%s'", NULL);
5009 return error_mark_node;
5010 }
5011 }
5012
5013 if (otype != type)
5014 {
5015 TREE_OPERAND (prev, 1)
5016 = build_tree_list (NULL_TREE,
5017 digest_init (type, init, require_constant,
5018 constructor_constant));
5019 return top;
5020 }
5021 else
5022 return error_mark_node;
5023 }
5024 error_init ("invalid initializer%s", " for `%s'", NULL);
5025 return error_mark_node;
5026 }
5027 \f
5028 /* Handle initializers that use braces. */
5029
5030 /* Type of object we are accumulating a constructor for.
5031 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5032 static tree constructor_type;
5033
5034 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
5035 left to fill. */
5036 static tree constructor_fields;
5037
5038 /* For an ARRAY_TYPE, this is the specified index
5039 at which to store the next element we get.
5040 This is a special INTEGER_CST node that we modify in place. */
5041 static tree constructor_index;
5042
5043 /* For an ARRAY_TYPE, this is the end index of the range
5044 to initialize with the next element, or NULL in the ordinary case
5045 where the element is used just once. */
5046 static tree constructor_range_end;
5047
5048 /* For an ARRAY_TYPE, this is the maximum index. */
5049 static tree constructor_max_index;
5050
5051 /* For a RECORD_TYPE, this is the first field not yet written out. */
5052 static tree constructor_unfilled_fields;
5053
5054 /* For an ARRAY_TYPE, this is the index of the first element
5055 not yet written out.
5056 This is a special INTEGER_CST node that we modify in place. */
5057 static tree constructor_unfilled_index;
5058
5059 /* In a RECORD_TYPE, the byte index of the next consecutive field.
5060 This is so we can generate gaps between fields, when appropriate.
5061 This is a special INTEGER_CST node that we modify in place. */
5062 static tree constructor_bit_index;
5063
5064 /* If we are saving up the elements rather than allocating them,
5065 this is the list of elements so far (in reverse order,
5066 most recent first). */
5067 static tree constructor_elements;
5068
5069 /* 1 if so far this constructor's elements are all compile-time constants. */
5070 static int constructor_constant;
5071
5072 /* 1 if so far this constructor's elements are all valid address constants. */
5073 static int constructor_simple;
5074
5075 /* 1 if this constructor is erroneous so far. */
5076 static int constructor_erroneous;
5077
5078 /* 1 if have called defer_addressed_constants. */
5079 static int constructor_subconstants_deferred;
5080
5081 /* Structure for managing pending initializer elements, organized as an
5082 AVL tree. */
5083
5084 struct init_node
5085 {
5086 struct init_node *left, *right;
5087 struct init_node *parent;
5088 int balance;
5089 tree purpose;
5090 tree value;
5091 };
5092
5093 /* Tree of pending elements at this constructor level.
5094 These are elements encountered out of order
5095 which belong at places we haven't reached yet in actually
5096 writing the output. */
5097 static struct init_node *constructor_pending_elts;
5098
5099 /* The SPELLING_DEPTH of this constructor. */
5100 static int constructor_depth;
5101
5102 /* 0 if implicitly pushing constructor levels is allowed. */
5103 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
5104
5105 static int require_constant_value;
5106 static int require_constant_elements;
5107
5108 /* 1 if it is ok to output this constructor as we read it.
5109 0 means must accumulate a CONSTRUCTOR expression. */
5110 static int constructor_incremental;
5111
5112 /* DECL node for which an initializer is being read.
5113 0 means we are reading a constructor expression
5114 such as (struct foo) {...}. */
5115 static tree constructor_decl;
5116
5117 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
5118 static char *constructor_asmspec;
5119
5120 /* Nonzero if this is an initializer for a top-level decl. */
5121 static int constructor_top_level;
5122
5123 \f
5124 /* This stack has a level for each implicit or explicit level of
5125 structuring in the initializer, including the outermost one. It
5126 saves the values of most of the variables above. */
5127
5128 struct constructor_stack
5129 {
5130 struct constructor_stack *next;
5131 tree type;
5132 tree fields;
5133 tree index;
5134 tree range_end;
5135 tree max_index;
5136 tree unfilled_index;
5137 tree unfilled_fields;
5138 tree bit_index;
5139 tree elements;
5140 int offset;
5141 struct init_node *pending_elts;
5142 int depth;
5143 /* If nonzero, this value should replace the entire
5144 constructor at this level. */
5145 tree replacement_value;
5146 char constant;
5147 char simple;
5148 char implicit;
5149 char incremental;
5150 char erroneous;
5151 char outer;
5152 };
5153
5154 struct constructor_stack *constructor_stack;
5155
5156 /* This stack records separate initializers that are nested.
5157 Nested initializers can't happen in ANSI C, but GNU C allows them
5158 in cases like { ... (struct foo) { ... } ... }. */
5159
5160 struct initializer_stack
5161 {
5162 struct initializer_stack *next;
5163 tree decl;
5164 char *asmspec;
5165 struct constructor_stack *constructor_stack;
5166 tree elements;
5167 struct spelling *spelling;
5168 struct spelling *spelling_base;
5169 int spelling_size;
5170 char top_level;
5171 char incremental;
5172 char require_constant_value;
5173 char require_constant_elements;
5174 char deferred;
5175 };
5176
5177 struct initializer_stack *initializer_stack;
5178 \f
5179 /* Prepare to parse and output the initializer for variable DECL. */
5180
5181 void
5182 start_init (decl, asmspec_tree, top_level)
5183 tree decl;
5184 tree asmspec_tree;
5185 int top_level;
5186 {
5187 char *locus;
5188 struct initializer_stack *p
5189 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5190 char *asmspec = 0;
5191
5192 if (asmspec_tree)
5193 asmspec = TREE_STRING_POINTER (asmspec_tree);
5194
5195 p->decl = constructor_decl;
5196 p->asmspec = constructor_asmspec;
5197 p->incremental = constructor_incremental;
5198 p->require_constant_value = require_constant_value;
5199 p->require_constant_elements = require_constant_elements;
5200 p->constructor_stack = constructor_stack;
5201 p->elements = constructor_elements;
5202 p->spelling = spelling;
5203 p->spelling_base = spelling_base;
5204 p->spelling_size = spelling_size;
5205 p->deferred = constructor_subconstants_deferred;
5206 p->top_level = constructor_top_level;
5207 p->next = initializer_stack;
5208 initializer_stack = p;
5209
5210 constructor_decl = decl;
5211 constructor_incremental = top_level;
5212 constructor_asmspec = asmspec;
5213 constructor_subconstants_deferred = 0;
5214 constructor_top_level = top_level;
5215
5216 if (decl != 0)
5217 {
5218 require_constant_value = TREE_STATIC (decl);
5219 require_constant_elements
5220 = ((TREE_STATIC (decl) || pedantic)
5221 /* For a scalar, you can always use any value to initialize,
5222 even within braces. */
5223 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5224 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5225 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5226 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5227 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5228 constructor_incremental |= TREE_STATIC (decl);
5229 }
5230 else
5231 {
5232 require_constant_value = 0;
5233 require_constant_elements = 0;
5234 locus = "(anonymous)";
5235 }
5236
5237 constructor_stack = 0;
5238
5239 missing_braces_mentioned = 0;
5240
5241 spelling_base = 0;
5242 spelling_size = 0;
5243 RESTORE_SPELLING_DEPTH (0);
5244
5245 if (locus)
5246 push_string (locus);
5247 }
5248
5249 void
5250 finish_init ()
5251 {
5252 struct initializer_stack *p = initializer_stack;
5253
5254 /* Output subconstants (string constants, usually)
5255 that were referenced within this initializer and saved up.
5256 Must do this if and only if we called defer_addressed_constants. */
5257 if (constructor_subconstants_deferred)
5258 output_deferred_addressed_constants ();
5259
5260 /* Free the whole constructor stack of this initializer. */
5261 while (constructor_stack)
5262 {
5263 struct constructor_stack *q = constructor_stack;
5264 constructor_stack = q->next;
5265 free (q);
5266 }
5267
5268 /* Pop back to the data of the outer initializer (if any). */
5269 constructor_decl = p->decl;
5270 constructor_asmspec = p->asmspec;
5271 constructor_incremental = p->incremental;
5272 require_constant_value = p->require_constant_value;
5273 require_constant_elements = p->require_constant_elements;
5274 constructor_stack = p->constructor_stack;
5275 constructor_elements = p->elements;
5276 spelling = p->spelling;
5277 spelling_base = p->spelling_base;
5278 spelling_size = p->spelling_size;
5279 constructor_subconstants_deferred = p->deferred;
5280 constructor_top_level = p->top_level;
5281 initializer_stack = p->next;
5282 free (p);
5283 }
5284 \f
5285 /* Call here when we see the initializer is surrounded by braces.
5286 This is instead of a call to push_init_level;
5287 it is matched by a call to pop_init_level.
5288
5289 TYPE is the type to initialize, for a constructor expression.
5290 For an initializer for a decl, TYPE is zero. */
5291
5292 void
5293 really_start_incremental_init (type)
5294 tree type;
5295 {
5296 struct constructor_stack *p
5297 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5298
5299 if (type == 0)
5300 type = TREE_TYPE (constructor_decl);
5301
5302 /* Turn off constructor_incremental if type is a struct with bitfields.
5303 Do this before the first push, so that the corrected value
5304 is available in finish_init. */
5305 check_init_type_bitfields (type);
5306
5307 p->type = constructor_type;
5308 p->fields = constructor_fields;
5309 p->index = constructor_index;
5310 p->range_end = constructor_range_end;
5311 p->max_index = constructor_max_index;
5312 p->unfilled_index = constructor_unfilled_index;
5313 p->unfilled_fields = constructor_unfilled_fields;
5314 p->bit_index = constructor_bit_index;
5315 p->elements = constructor_elements;
5316 p->constant = constructor_constant;
5317 p->simple = constructor_simple;
5318 p->erroneous = constructor_erroneous;
5319 p->pending_elts = constructor_pending_elts;
5320 p->depth = constructor_depth;
5321 p->replacement_value = 0;
5322 p->implicit = 0;
5323 p->incremental = constructor_incremental;
5324 p->outer = 0;
5325 p->next = 0;
5326 constructor_stack = p;
5327
5328 constructor_constant = 1;
5329 constructor_simple = 1;
5330 constructor_depth = SPELLING_DEPTH ();
5331 constructor_elements = 0;
5332 constructor_pending_elts = 0;
5333 constructor_type = type;
5334
5335 if (TREE_CODE (constructor_type) == RECORD_TYPE
5336 || TREE_CODE (constructor_type) == UNION_TYPE)
5337 {
5338 constructor_fields = TYPE_FIELDS (constructor_type);
5339 /* Skip any nameless bit fields at the beginning. */
5340 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5341 && DECL_NAME (constructor_fields) == 0)
5342 constructor_fields = TREE_CHAIN (constructor_fields);
5343 constructor_unfilled_fields = constructor_fields;
5344 constructor_bit_index = copy_node (integer_zero_node);
5345 TREE_TYPE (constructor_bit_index) = sbitsizetype;
5346 }
5347 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5348 {
5349 constructor_range_end = 0;
5350 if (TYPE_DOMAIN (constructor_type))
5351 {
5352 constructor_max_index
5353 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5354 constructor_index
5355 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5356 }
5357 else
5358 constructor_index = copy_node (integer_zero_node);
5359 constructor_unfilled_index = copy_node (constructor_index);
5360 }
5361 else
5362 {
5363 /* Handle the case of int x = {5}; */
5364 constructor_fields = constructor_type;
5365 constructor_unfilled_fields = constructor_type;
5366 }
5367
5368 if (constructor_incremental)
5369 {
5370 int momentary = suspend_momentary ();
5371 push_obstacks_nochange ();
5372 if (TREE_PERMANENT (constructor_decl))
5373 end_temporary_allocation ();
5374 make_decl_rtl (constructor_decl, constructor_asmspec,
5375 constructor_top_level);
5376 assemble_variable (constructor_decl, constructor_top_level, 0, 1);
5377 pop_obstacks ();
5378 resume_momentary (momentary);
5379 }
5380
5381 if (constructor_incremental)
5382 {
5383 defer_addressed_constants ();
5384 constructor_subconstants_deferred = 1;
5385 }
5386 }
5387 \f
5388 /* Push down into a subobject, for initialization.
5389 If this is for an explicit set of braces, IMPLICIT is 0.
5390 If it is because the next element belongs at a lower level,
5391 IMPLICIT is 1. */
5392
5393 void
5394 push_init_level (implicit)
5395 int implicit;
5396 {
5397 struct constructor_stack *p;
5398
5399 /* If we've exhausted any levels that didn't have braces,
5400 pop them now. */
5401 while (constructor_stack->implicit)
5402 {
5403 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5404 || TREE_CODE (constructor_type) == UNION_TYPE)
5405 && constructor_fields == 0)
5406 process_init_element (pop_init_level (1));
5407 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5408 && tree_int_cst_lt (constructor_max_index, constructor_index))
5409 process_init_element (pop_init_level (1));
5410 else
5411 break;
5412 }
5413
5414 /* Structure elements may require alignment. Do this now if necessary
5415 for the subaggregate, and if it comes next in sequence. Don't do
5416 this for subaggregates that will go on the pending list. */
5417 if (constructor_incremental && constructor_type != 0
5418 && TREE_CODE (constructor_type) == RECORD_TYPE && constructor_fields
5419 && constructor_fields == constructor_unfilled_fields)
5420 {
5421 /* Advance to offset of this element. */
5422 if (! tree_int_cst_equal (constructor_bit_index,
5423 DECL_FIELD_BITPOS (constructor_fields)))
5424 {
5425 /* By using unsigned arithmetic, the result will be correct even
5426 in case of overflows, if BITS_PER_UNIT is a power of two. */
5427 unsigned next = (TREE_INT_CST_LOW
5428 (DECL_FIELD_BITPOS (constructor_fields))
5429 / (unsigned)BITS_PER_UNIT);
5430 unsigned here = (TREE_INT_CST_LOW (constructor_bit_index)
5431 / (unsigned)BITS_PER_UNIT);
5432
5433 assemble_zeros ((next - here)
5434 * (unsigned)BITS_PER_UNIT
5435 / (unsigned)BITS_PER_UNIT);
5436 }
5437 /* Indicate that we have now filled the structure up to the current
5438 field. */
5439 constructor_unfilled_fields = constructor_fields;
5440 }
5441
5442 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5443 p->type = constructor_type;
5444 p->fields = constructor_fields;
5445 p->index = constructor_index;
5446 p->range_end = constructor_range_end;
5447 p->max_index = constructor_max_index;
5448 p->unfilled_index = constructor_unfilled_index;
5449 p->unfilled_fields = constructor_unfilled_fields;
5450 p->bit_index = constructor_bit_index;
5451 p->elements = constructor_elements;
5452 p->constant = constructor_constant;
5453 p->simple = constructor_simple;
5454 p->erroneous = constructor_erroneous;
5455 p->pending_elts = constructor_pending_elts;
5456 p->depth = constructor_depth;
5457 p->replacement_value = 0;
5458 p->implicit = implicit;
5459 p->incremental = constructor_incremental;
5460 p->outer = 0;
5461 p->next = constructor_stack;
5462 constructor_stack = p;
5463
5464 constructor_constant = 1;
5465 constructor_simple = 1;
5466 constructor_depth = SPELLING_DEPTH ();
5467 constructor_elements = 0;
5468 constructor_pending_elts = 0;
5469
5470 /* Don't die if an entire brace-pair level is superfluous
5471 in the containing level. */
5472 if (constructor_type == 0)
5473 ;
5474 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5475 || TREE_CODE (constructor_type) == UNION_TYPE)
5476 {
5477 /* Don't die if there are extra init elts at the end. */
5478 if (constructor_fields == 0)
5479 constructor_type = 0;
5480 else
5481 {
5482 constructor_type = TREE_TYPE (constructor_fields);
5483 push_member_name (constructor_fields);
5484 constructor_depth++;
5485 if (constructor_fields != constructor_unfilled_fields)
5486 constructor_incremental = 0;
5487 }
5488 }
5489 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5490 {
5491 constructor_type = TREE_TYPE (constructor_type);
5492 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
5493 constructor_depth++;
5494 if (! tree_int_cst_equal (constructor_index, constructor_unfilled_index)
5495 || constructor_range_end != 0)
5496 constructor_incremental = 0;
5497 }
5498
5499 if (constructor_type == 0)
5500 {
5501 error_init ("extra brace group at end of initializer%s",
5502 " for `%s'", NULL);
5503 constructor_fields = 0;
5504 constructor_unfilled_fields = 0;
5505 return;
5506 }
5507
5508 /* Turn off constructor_incremental if type is a struct with bitfields. */
5509 check_init_type_bitfields (constructor_type);
5510
5511 if (implicit && warn_missing_braces && !missing_braces_mentioned)
5512 {
5513 missing_braces_mentioned = 1;
5514 warning_init ("missing braces around initializer%s", " for `%s'", NULL);
5515 }
5516
5517 if (TREE_CODE (constructor_type) == RECORD_TYPE
5518 || TREE_CODE (constructor_type) == UNION_TYPE)
5519 {
5520 constructor_fields = TYPE_FIELDS (constructor_type);
5521 /* Skip any nameless bit fields at the beginning. */
5522 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5523 && DECL_NAME (constructor_fields) == 0)
5524 constructor_fields = TREE_CHAIN (constructor_fields);
5525 constructor_unfilled_fields = constructor_fields;
5526 constructor_bit_index = copy_node (integer_zero_node);
5527 TREE_TYPE (constructor_bit_index) = sbitsizetype;
5528 }
5529 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5530 {
5531 constructor_range_end = 0;
5532 if (TYPE_DOMAIN (constructor_type))
5533 {
5534 constructor_max_index
5535 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5536 constructor_index
5537 = copy_node (TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5538 }
5539 else
5540 constructor_index = copy_node (integer_zero_node);
5541 constructor_unfilled_index = copy_node (constructor_index);
5542 }
5543 else
5544 {
5545 warning_init ("braces around scalar initializer%s", " for `%s'", NULL);
5546 constructor_fields = constructor_type;
5547 constructor_unfilled_fields = constructor_type;
5548 }
5549 }
5550
5551 /* Don't read a struct incrementally if it has any bitfields,
5552 because the incremental reading code doesn't know how to
5553 handle bitfields yet. */
5554
5555 static void
5556 check_init_type_bitfields (type)
5557 tree type;
5558 {
5559 if (TREE_CODE (type) == RECORD_TYPE)
5560 {
5561 tree tail;
5562 for (tail = TYPE_FIELDS (type); tail;
5563 tail = TREE_CHAIN (tail))
5564 {
5565 if (DECL_C_BIT_FIELD (tail)
5566 /* This catches cases like `int foo : 8;'. */
5567 || DECL_MODE (tail) != TYPE_MODE (TREE_TYPE (tail)))
5568 {
5569 constructor_incremental = 0;
5570 break;
5571 }
5572
5573 check_init_type_bitfields (TREE_TYPE (tail));
5574 }
5575 }
5576
5577 else if (TREE_CODE (type) == ARRAY_TYPE)
5578 check_init_type_bitfields (TREE_TYPE (type));
5579 }
5580
5581 /* At the end of an implicit or explicit brace level,
5582 finish up that level of constructor.
5583 If we were outputting the elements as they are read, return 0
5584 from inner levels (process_init_element ignores that),
5585 but return error_mark_node from the outermost level
5586 (that's what we want to put in DECL_INITIAL).
5587 Otherwise, return a CONSTRUCTOR expression. */
5588
5589 tree
5590 pop_init_level (implicit)
5591 int implicit;
5592 {
5593 struct constructor_stack *p;
5594 int size = 0;
5595 tree constructor = 0;
5596
5597 if (implicit == 0)
5598 {
5599 /* When we come to an explicit close brace,
5600 pop any inner levels that didn't have explicit braces. */
5601 while (constructor_stack->implicit)
5602 process_init_element (pop_init_level (1));
5603 }
5604
5605 p = constructor_stack;
5606
5607 if (constructor_type != 0)
5608 size = int_size_in_bytes (constructor_type);
5609
5610 /* Warn when some struct elements are implicitly initialized to zero. */
5611 if (extra_warnings
5612 && constructor_type
5613 && TREE_CODE (constructor_type) == RECORD_TYPE
5614 && constructor_unfilled_fields)
5615 {
5616 push_member_name (constructor_unfilled_fields);
5617 warning_init ("missing initializer%s", " for `%s'", NULL);
5618 RESTORE_SPELLING_DEPTH (constructor_depth);
5619 }
5620
5621 /* Now output all pending elements. */
5622 output_pending_init_elements (1);
5623
5624 #if 0 /* c-parse.in warns about {}. */
5625 /* In ANSI, each brace level must have at least one element. */
5626 if (! implicit && pedantic
5627 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5628 ? integer_zerop (constructor_unfilled_index)
5629 : constructor_unfilled_fields == TYPE_FIELDS (constructor_type)))
5630 pedwarn_init ("empty braces in initializer%s", " for `%s'", NULL);
5631 #endif
5632
5633 /* Pad out the end of the structure. */
5634
5635 if (p->replacement_value)
5636 {
5637 /* If this closes a superfluous brace pair,
5638 just pass out the element between them. */
5639 constructor = p->replacement_value;
5640 /* If this is the top level thing within the initializer,
5641 and it's for a variable, then since we already called
5642 assemble_variable, we must output the value now. */
5643 if (p->next == 0 && constructor_decl != 0
5644 && constructor_incremental)
5645 {
5646 constructor = digest_init (constructor_type, constructor,
5647 require_constant_value,
5648 require_constant_elements);
5649
5650 /* If initializing an array of unknown size,
5651 determine the size now. */
5652 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5653 && TYPE_DOMAIN (constructor_type) == 0)
5654 {
5655 int failure;
5656 int momentary_p;
5657
5658 push_obstacks_nochange ();
5659 if (TREE_PERMANENT (constructor_type))
5660 end_temporary_allocation ();
5661
5662 momentary_p = suspend_momentary ();
5663
5664 /* We shouldn't have an incomplete array type within
5665 some other type. */
5666 if (constructor_stack->next)
5667 abort ();
5668
5669 failure
5670 = complete_array_type (constructor_type,
5671 constructor, 0);
5672 if (failure)
5673 abort ();
5674
5675 size = int_size_in_bytes (constructor_type);
5676 resume_momentary (momentary_p);
5677 pop_obstacks ();
5678 }
5679
5680 output_constant (constructor, size);
5681 }
5682 }
5683 else if (constructor_type == 0)
5684 ;
5685 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5686 && TREE_CODE (constructor_type) != UNION_TYPE
5687 && TREE_CODE (constructor_type) != ARRAY_TYPE
5688 && ! constructor_incremental)
5689 {
5690 /* A nonincremental scalar initializer--just return
5691 the element, after verifying there is just one. */
5692 if (constructor_elements == 0)
5693 {
5694 error_init ("empty scalar initializer%s",
5695 " for `%s'", NULL);
5696 constructor = error_mark_node;
5697 }
5698 else if (TREE_CHAIN (constructor_elements) != 0)
5699 {
5700 error_init ("extra elements in scalar initializer%s",
5701 " for `%s'", NULL);
5702 constructor = TREE_VALUE (constructor_elements);
5703 }
5704 else
5705 constructor = TREE_VALUE (constructor_elements);
5706 }
5707 else if (! constructor_incremental)
5708 {
5709 if (constructor_erroneous)
5710 constructor = error_mark_node;
5711 else
5712 {
5713 int momentary = suspend_momentary ();
5714
5715 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5716 nreverse (constructor_elements));
5717 if (constructor_constant)
5718 TREE_CONSTANT (constructor) = 1;
5719 if (constructor_constant && constructor_simple)
5720 TREE_STATIC (constructor) = 1;
5721
5722 resume_momentary (momentary);
5723 }
5724 }
5725 else
5726 {
5727 tree filled;
5728 int momentary = suspend_momentary ();
5729
5730 if (TREE_CODE (constructor_type) == RECORD_TYPE
5731 || TREE_CODE (constructor_type) == UNION_TYPE)
5732 {
5733 /* Find the offset of the end of that field. */
5734 filled = size_binop (CEIL_DIV_EXPR,
5735 constructor_bit_index,
5736 size_int (BITS_PER_UNIT));
5737 }
5738 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5739 {
5740 /* If initializing an array of unknown size,
5741 determine the size now. */
5742 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5743 && TYPE_DOMAIN (constructor_type) == 0)
5744 {
5745 tree maxindex
5746 = size_binop (MINUS_EXPR,
5747 constructor_unfilled_index,
5748 integer_one_node);
5749
5750 push_obstacks_nochange ();
5751 if (TREE_PERMANENT (constructor_type))
5752 end_temporary_allocation ();
5753 maxindex = copy_node (maxindex);
5754 TYPE_DOMAIN (constructor_type) = build_index_type (maxindex);
5755 TREE_TYPE (maxindex) = TYPE_DOMAIN (constructor_type);
5756
5757 /* TYPE_MAX_VALUE is always one less than the number of elements
5758 in the array, because we start counting at zero. Therefore,
5759 warn only if the value is less than zero. */
5760 if (pedantic
5761 && (tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5762 < 0))
5763 error_with_decl (constructor_decl,
5764 "zero or negative array size `%s'");
5765 layout_type (constructor_type);
5766 size = int_size_in_bytes (constructor_type);
5767 pop_obstacks ();
5768 }
5769
5770 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
5771 size_in_bytes (TREE_TYPE (constructor_type)));
5772 }
5773 else
5774 filled = 0;
5775
5776 if (filled != 0)
5777 assemble_zeros (size - TREE_INT_CST_LOW (filled));
5778
5779 resume_momentary (momentary);
5780 }
5781
5782
5783 constructor_type = p->type;
5784 constructor_fields = p->fields;
5785 constructor_index = p->index;
5786 constructor_range_end = p->range_end;
5787 constructor_max_index = p->max_index;
5788 constructor_unfilled_index = p->unfilled_index;
5789 constructor_unfilled_fields = p->unfilled_fields;
5790 constructor_bit_index = p->bit_index;
5791 constructor_elements = p->elements;
5792 constructor_constant = p->constant;
5793 constructor_simple = p->simple;
5794 constructor_erroneous = p->erroneous;
5795 constructor_pending_elts = p->pending_elts;
5796 constructor_depth = p->depth;
5797 constructor_incremental = p->incremental;
5798 RESTORE_SPELLING_DEPTH (constructor_depth);
5799
5800 constructor_stack = p->next;
5801 free (p);
5802
5803 if (constructor == 0)
5804 {
5805 if (constructor_stack == 0)
5806 return error_mark_node;
5807 return NULL_TREE;
5808 }
5809 return constructor;
5810 }
5811
5812 /* Within an array initializer, specify the next index to be initialized.
5813 FIRST is that index. If LAST is nonzero, then initialize a range
5814 of indices, running from FIRST through LAST. */
5815
5816 void
5817 set_init_index (first, last)
5818 tree first, last;
5819 {
5820 while ((TREE_CODE (first) == NOP_EXPR
5821 || TREE_CODE (first) == CONVERT_EXPR
5822 || TREE_CODE (first) == NON_LVALUE_EXPR)
5823 && (TYPE_MODE (TREE_TYPE (first))
5824 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5825 (first) = TREE_OPERAND (first, 0);
5826 if (last)
5827 while ((TREE_CODE (last) == NOP_EXPR
5828 || TREE_CODE (last) == CONVERT_EXPR
5829 || TREE_CODE (last) == NON_LVALUE_EXPR)
5830 && (TYPE_MODE (TREE_TYPE (last))
5831 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5832 (last) = TREE_OPERAND (last, 0);
5833
5834 if (TREE_CODE (first) != INTEGER_CST)
5835 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5836 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5837 error_init ("nonconstant array index in initializer%s", " for `%s'", NULL);
5838 else if (! constructor_unfilled_index)
5839 error_init ("array index in non-array initializer%s", " for `%s'", NULL);
5840 else if (tree_int_cst_lt (first, constructor_unfilled_index))
5841 error_init ("duplicate array index in initializer%s", " for `%s'", NULL);
5842 else
5843 {
5844 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (first);
5845 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (first);
5846
5847 if (last != 0 && tree_int_cst_lt (last, first))
5848 error_init ("empty index range in initializer%s", " for `%s'", NULL);
5849 else
5850 {
5851 if (pedantic)
5852 pedwarn ("ANSI C forbids specifying element to initialize");
5853 constructor_range_end = last;
5854 }
5855 }
5856 }
5857
5858 /* Within a struct initializer, specify the next field to be initialized. */
5859
5860 void
5861 set_init_label (fieldname)
5862 tree fieldname;
5863 {
5864 tree tail;
5865 int passed = 0;
5866
5867 /* Don't die if an entire brace-pair level is superfluous
5868 in the containing level. */
5869 if (constructor_type == 0)
5870 return;
5871
5872 for (tail = TYPE_FIELDS (constructor_type); tail;
5873 tail = TREE_CHAIN (tail))
5874 {
5875 if (tail == constructor_unfilled_fields)
5876 passed = 1;
5877 if (DECL_NAME (tail) == fieldname)
5878 break;
5879 }
5880
5881 if (tail == 0)
5882 error ("unknown field `%s' specified in initializer",
5883 IDENTIFIER_POINTER (fieldname));
5884 else if (!passed)
5885 error ("field `%s' already initialized",
5886 IDENTIFIER_POINTER (fieldname));
5887 else
5888 {
5889 constructor_fields = tail;
5890 if (pedantic)
5891 pedwarn ("ANSI C forbids specifying structure member to initialize");
5892 }
5893 }
5894 \f
5895 /* Add a new initializer to the tree of pending initializers. PURPOSE
5896 indentifies the initializer, either array index or field in a structure.
5897 VALUE is the value of that index or field. */
5898
5899 static void
5900 add_pending_init (purpose, value)
5901 tree purpose, value;
5902 {
5903 struct init_node *p, **q, *r;
5904
5905 q = &constructor_pending_elts;
5906 p = 0;
5907
5908 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5909 {
5910 while (*q != 0)
5911 {
5912 p = *q;
5913 if (tree_int_cst_lt (purpose, p->purpose))
5914 q = &p->left;
5915 else if (tree_int_cst_lt (p->purpose, purpose))
5916 q = &p->right;
5917 else
5918 abort ();
5919 }
5920 }
5921 else
5922 {
5923 while (*q != NULL)
5924 {
5925 p = *q;
5926 if (tree_int_cst_lt (DECL_FIELD_BITPOS (purpose),
5927 DECL_FIELD_BITPOS (p->purpose)))
5928 q = &p->left;
5929 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (p->purpose),
5930 DECL_FIELD_BITPOS (purpose)))
5931 q = &p->right;
5932 else
5933 abort ();
5934 }
5935 }
5936
5937 r = (struct init_node *) oballoc (sizeof (struct init_node));
5938 r->purpose = purpose;
5939 r->value = value;
5940
5941 *q = r;
5942 r->parent = p;
5943 r->left = 0;
5944 r->right = 0;
5945 r->balance = 0;
5946
5947 while (p)
5948 {
5949 struct init_node *s;
5950
5951 if (r == p->left)
5952 {
5953 if (p->balance == 0)
5954 p->balance = -1;
5955 else if (p->balance < 0)
5956 {
5957 if (r->balance < 0)
5958 {
5959 /* L rotation. */
5960 p->left = r->right;
5961 if (p->left)
5962 p->left->parent = p;
5963 r->right = p;
5964
5965 p->balance = 0;
5966 r->balance = 0;
5967
5968 s = p->parent;
5969 p->parent = r;
5970 r->parent = s;
5971 if (s)
5972 {
5973 if (s->left == p)
5974 s->left = r;
5975 else
5976 s->right = r;
5977 }
5978 else
5979 constructor_pending_elts = r;
5980 }
5981 else
5982 {
5983 /* LR rotation. */
5984 struct init_node *t = r->right;
5985
5986 r->right = t->left;
5987 if (r->right)
5988 r->right->parent = r;
5989 t->left = r;
5990
5991 p->left = t->right;
5992 if (p->left)
5993 p->left->parent = p;
5994 t->right = p;
5995
5996 p->balance = t->balance < 0;
5997 r->balance = -(t->balance > 0);
5998 t->balance = 0;
5999
6000 s = p->parent;
6001 p->parent = t;
6002 r->parent = t;
6003 t->parent = s;
6004 if (s)
6005 {
6006 if (s->left == p)
6007 s->left = t;
6008 else
6009 s->right = t;
6010 }
6011 else
6012 constructor_pending_elts = t;
6013 }
6014 break;
6015 }
6016 else
6017 {
6018 /* p->balance == +1; growth of left side balances the node. */
6019 p->balance = 0;
6020 break;
6021 }
6022 }
6023 else /* r == p->right */
6024 {
6025 if (p->balance == 0)
6026 /* Growth propagation from right side. */
6027 p->balance++;
6028 else if (p->balance > 0)
6029 {
6030 if (r->balance > 0)
6031 {
6032 /* R rotation. */
6033 p->right = r->left;
6034 if (p->right)
6035 p->right->parent = p;
6036 r->left = p;
6037
6038 p->balance = 0;
6039 r->balance = 0;
6040
6041 s = p->parent;
6042 p->parent = r;
6043 r->parent = s;
6044 if (s)
6045 {
6046 if (s->left == p)
6047 s->left = r;
6048 else
6049 s->right = r;
6050 }
6051 else
6052 constructor_pending_elts = r;
6053 }
6054 else /* r->balance == -1 */
6055 {
6056 /* RL rotation */
6057 struct init_node *t = r->left;
6058
6059 r->left = t->right;
6060 if (r->left)
6061 r->left->parent = r;
6062 t->right = r;
6063
6064 p->right = t->left;
6065 if (p->right)
6066 p->right->parent = p;
6067 t->left = p;
6068
6069 r->balance = (t->balance < 0);
6070 p->balance = -(t->balance > 0);
6071 t->balance = 0;
6072
6073 s = p->parent;
6074 p->parent = t;
6075 r->parent = t;
6076 t->parent = s;
6077 if (s)
6078 {
6079 if (s->left == p)
6080 s->left = t;
6081 else
6082 s->right = t;
6083 }
6084 else
6085 constructor_pending_elts = t;
6086 }
6087 break;
6088 }
6089 else
6090 {
6091 /* p->balance == -1; growth of right side balances the node. */
6092 p->balance = 0;
6093 break;
6094 }
6095 }
6096
6097 r = p;
6098 p = p->parent;
6099 }
6100 }
6101
6102 /* Return nonzero if FIELD is equal to the index of a pending initializer. */
6103
6104 static int
6105 pending_init_member (field)
6106 tree field;
6107 {
6108 struct init_node *p;
6109
6110 p = constructor_pending_elts;
6111 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6112 {
6113 while (p)
6114 {
6115 if (tree_int_cst_equal (field, p->purpose))
6116 return 1;
6117 else if (tree_int_cst_lt (field, p->purpose))
6118 p = p->left;
6119 else
6120 p = p->right;
6121 }
6122 }
6123 else
6124 {
6125 while (p)
6126 {
6127 if (field == p->purpose)
6128 return 1;
6129 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (field),
6130 DECL_FIELD_BITPOS (p->purpose)))
6131 p = p->left;
6132 else
6133 p = p->right;
6134 }
6135 }
6136
6137 return 0;
6138 }
6139
6140 /* "Output" the next constructor element.
6141 At top level, really output it to assembler code now.
6142 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6143 TYPE is the data type that the containing data type wants here.
6144 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6145
6146 PENDING if non-nil means output pending elements that belong
6147 right after this element. (PENDING is normally 1;
6148 it is 0 while outputting pending elements, to avoid recursion.) */
6149
6150 static void
6151 output_init_element (value, type, field, pending)
6152 tree value, type, field;
6153 int pending;
6154 {
6155 int duplicate = 0;
6156
6157 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6158 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6159 && !(TREE_CODE (value) == STRING_CST
6160 && TREE_CODE (type) == ARRAY_TYPE
6161 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6162 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6163 TYPE_MAIN_VARIANT (type))))
6164 value = default_conversion (value);
6165
6166 if (value == error_mark_node)
6167 constructor_erroneous = 1;
6168 else if (!TREE_CONSTANT (value))
6169 constructor_constant = 0;
6170 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6171 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6172 || TREE_CODE (constructor_type) == UNION_TYPE)
6173 && DECL_C_BIT_FIELD (field)
6174 && TREE_CODE (value) != INTEGER_CST))
6175 constructor_simple = 0;
6176
6177 if (require_constant_value && ! TREE_CONSTANT (value))
6178 {
6179 error_init ("initializer element%s is not constant",
6180 " for `%s'", NULL);
6181 value = error_mark_node;
6182 }
6183 else if (require_constant_elements
6184 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6185 {
6186 error_init ("initializer element%s is not computable at load time",
6187 " for `%s'", NULL);
6188 value = error_mark_node;
6189 }
6190
6191 /* If this element duplicates one on constructor_pending_elts,
6192 print a message and ignore it. Don't do this when we're
6193 processing elements taken off constructor_pending_elts,
6194 because we'd always get spurious errors. */
6195 if (pending)
6196 {
6197 if (TREE_CODE (constructor_type) == RECORD_TYPE
6198 || TREE_CODE (constructor_type) == UNION_TYPE
6199 || TREE_CODE (constructor_type) == ARRAY_TYPE)
6200 {
6201 if (pending_init_member (field))
6202 {
6203 error_init ("duplicate initializer%s", " for `%s'", NULL);
6204 duplicate = 1;
6205 }
6206 }
6207 }
6208
6209 /* If this element doesn't come next in sequence,
6210 put it on constructor_pending_elts. */
6211 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6212 && !tree_int_cst_equal (field, constructor_unfilled_index))
6213 {
6214 if (! duplicate)
6215 /* The copy_node is needed in case field is actually
6216 constructor_index, which is modified in place. */
6217 add_pending_init (copy_node (field),
6218 digest_init (type, value, require_constant_value,
6219 require_constant_elements));
6220 }
6221 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6222 && field != constructor_unfilled_fields)
6223 {
6224 /* We do this for records but not for unions. In a union,
6225 no matter which field is specified, it can be initialized
6226 right away since it starts at the beginning of the union. */
6227 if (!duplicate)
6228 add_pending_init (field,
6229 digest_init (type, value, require_constant_value,
6230 require_constant_elements));
6231 }
6232 else
6233 {
6234 /* Otherwise, output this element either to
6235 constructor_elements or to the assembler file. */
6236
6237 if (!duplicate)
6238 {
6239 if (! constructor_incremental)
6240 {
6241 if (field && TREE_CODE (field) == INTEGER_CST)
6242 field = copy_node (field);
6243 constructor_elements
6244 = tree_cons (field, digest_init (type, value,
6245 require_constant_value,
6246 require_constant_elements),
6247 constructor_elements);
6248 }
6249 else
6250 {
6251 /* Structure elements may require alignment.
6252 Do this, if necessary. */
6253 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6254 {
6255 /* Advance to offset of this element. */
6256 if (! tree_int_cst_equal (constructor_bit_index,
6257 DECL_FIELD_BITPOS (field)))
6258 {
6259 /* By using unsigned arithmetic, the result will be
6260 correct even in case of overflows, if BITS_PER_UNIT
6261 is a power of two. */
6262 unsigned next = (TREE_INT_CST_LOW
6263 (DECL_FIELD_BITPOS (field))
6264 / (unsigned)BITS_PER_UNIT);
6265 unsigned here = (TREE_INT_CST_LOW
6266 (constructor_bit_index)
6267 / (unsigned)BITS_PER_UNIT);
6268
6269 assemble_zeros ((next - here)
6270 * (unsigned)BITS_PER_UNIT
6271 / (unsigned)BITS_PER_UNIT);
6272 }
6273 }
6274 output_constant (digest_init (type, value,
6275 require_constant_value,
6276 require_constant_elements),
6277 int_size_in_bytes (type));
6278
6279 /* For a record or union,
6280 keep track of end position of last field. */
6281 if (TREE_CODE (constructor_type) == RECORD_TYPE
6282 || TREE_CODE (constructor_type) == UNION_TYPE)
6283 {
6284 tree temp = size_binop (PLUS_EXPR, DECL_FIELD_BITPOS (field),
6285 DECL_SIZE (field));
6286 TREE_INT_CST_LOW (constructor_bit_index)
6287 = TREE_INT_CST_LOW (temp);
6288 TREE_INT_CST_HIGH (constructor_bit_index)
6289 = TREE_INT_CST_HIGH (temp);
6290 }
6291 }
6292 }
6293
6294 /* Advance the variable that indicates sequential elements output. */
6295 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6296 {
6297 tree tem = size_binop (PLUS_EXPR, constructor_unfilled_index,
6298 integer_one_node);
6299 TREE_INT_CST_LOW (constructor_unfilled_index)
6300 = TREE_INT_CST_LOW (tem);
6301 TREE_INT_CST_HIGH (constructor_unfilled_index)
6302 = TREE_INT_CST_HIGH (tem);
6303 }
6304 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6305 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6306 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6307 constructor_unfilled_fields = 0;
6308
6309 /* Now output any pending elements which have become next. */
6310 if (pending)
6311 output_pending_init_elements (0);
6312 }
6313 }
6314
6315 /* Output any pending elements which have become next.
6316 As we output elements, constructor_unfilled_{fields,index}
6317 advances, which may cause other elements to become next;
6318 if so, they too are output.
6319
6320 If ALL is 0, we return when there are
6321 no more pending elements to output now.
6322
6323 If ALL is 1, we output space as necessary so that
6324 we can output all the pending elements. */
6325
6326 static void
6327 output_pending_init_elements (all)
6328 int all;
6329 {
6330 struct init_node *elt = constructor_pending_elts;
6331 tree next;
6332
6333 retry:
6334
6335 /* Look thru the whole pending tree.
6336 If we find an element that should be output now,
6337 output it. Otherwise, set NEXT to the element
6338 that comes first among those still pending. */
6339
6340 next = 0;
6341 while (elt)
6342 {
6343 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6344 {
6345 if (tree_int_cst_equal (elt->purpose,
6346 constructor_unfilled_index))
6347 output_init_element (elt->value,
6348 TREE_TYPE (constructor_type),
6349 constructor_unfilled_index, 0);
6350 else if (tree_int_cst_lt (constructor_unfilled_index,
6351 elt->purpose))
6352 {
6353 /* Advance to the next smaller node. */
6354 if (elt->left)
6355 elt = elt->left;
6356 else
6357 {
6358 /* We have reached the smallest node bigger than the
6359 current unfilled index. Fill the space first. */
6360 next = elt->purpose;
6361 break;
6362 }
6363 }
6364 else
6365 {
6366 /* Advance to the next bigger node. */
6367 if (elt->right)
6368 elt = elt->right;
6369 else
6370 {
6371 /* We have reached the biggest node in a subtree. Find
6372 the parent of it, which is the next bigger node. */
6373 while (elt->parent && elt->parent->right == elt)
6374 elt = elt->parent;
6375 elt = elt->parent;
6376 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6377 elt->purpose))
6378 {
6379 next = elt->purpose;
6380 break;
6381 }
6382 }
6383 }
6384 }
6385 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6386 || TREE_CODE (constructor_type) == UNION_TYPE)
6387 {
6388 /* If the current record is complete we are done. */
6389 if (constructor_unfilled_fields == 0)
6390 break;
6391 if (elt->purpose == constructor_unfilled_fields)
6392 {
6393 output_init_element (elt->value,
6394 TREE_TYPE (constructor_unfilled_fields),
6395 constructor_unfilled_fields,
6396 0);
6397 }
6398 else if (tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6399 DECL_FIELD_BITPOS (elt->purpose)))
6400 {
6401 /* Advance to the next smaller node. */
6402 if (elt->left)
6403 elt = elt->left;
6404 else
6405 {
6406 /* We have reached the smallest node bigger than the
6407 current unfilled field. Fill the space first. */
6408 next = elt->purpose;
6409 break;
6410 }
6411 }
6412 else
6413 {
6414 /* Advance to the next bigger node. */
6415 if (elt->right)
6416 elt = elt->right;
6417 else
6418 {
6419 /* We have reached the biggest node in a subtree. Find
6420 the parent of it, which is the next bigger node. */
6421 while (elt->parent && elt->parent->right == elt)
6422 elt = elt->parent;
6423 elt = elt->parent;
6424 if (elt
6425 && tree_int_cst_lt (DECL_FIELD_BITPOS (constructor_unfilled_fields),
6426 DECL_FIELD_BITPOS (elt->purpose)))
6427 {
6428 next = elt->purpose;
6429 break;
6430 }
6431 }
6432 }
6433 }
6434 }
6435
6436 /* Ordinarily return, but not if we want to output all
6437 and there are elements left. */
6438 if (! (all && next != 0))
6439 return;
6440
6441 /* Generate space up to the position of NEXT. */
6442 if (constructor_incremental)
6443 {
6444 tree filled;
6445 tree nextpos_tree = size_int (0);
6446
6447 if (TREE_CODE (constructor_type) == RECORD_TYPE
6448 || TREE_CODE (constructor_type) == UNION_TYPE)
6449 {
6450 tree tail;
6451 /* Find the last field written out, if any. */
6452 for (tail = TYPE_FIELDS (constructor_type); tail;
6453 tail = TREE_CHAIN (tail))
6454 if (TREE_CHAIN (tail) == constructor_unfilled_fields)
6455 break;
6456
6457 if (tail)
6458 /* Find the offset of the end of that field. */
6459 filled = size_binop (CEIL_DIV_EXPR,
6460 size_binop (PLUS_EXPR,
6461 DECL_FIELD_BITPOS (tail),
6462 DECL_SIZE (tail)),
6463 size_int (BITS_PER_UNIT));
6464 else
6465 filled = size_int (0);
6466
6467 nextpos_tree = size_binop (CEIL_DIV_EXPR,
6468 DECL_FIELD_BITPOS (next),
6469 size_int (BITS_PER_UNIT));
6470
6471 TREE_INT_CST_HIGH (constructor_bit_index)
6472 = TREE_INT_CST_HIGH (DECL_FIELD_BITPOS (next));
6473 TREE_INT_CST_LOW (constructor_bit_index)
6474 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (next));
6475 constructor_unfilled_fields = next;
6476 }
6477 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6478 {
6479 filled = size_binop (MULT_EXPR, constructor_unfilled_index,
6480 size_in_bytes (TREE_TYPE (constructor_type)));
6481 nextpos_tree
6482 = size_binop (MULT_EXPR, next,
6483 size_in_bytes (TREE_TYPE (constructor_type)));
6484 TREE_INT_CST_LOW (constructor_unfilled_index)
6485 = TREE_INT_CST_LOW (next);
6486 TREE_INT_CST_HIGH (constructor_unfilled_index)
6487 = TREE_INT_CST_HIGH (next);
6488 }
6489 else
6490 filled = 0;
6491
6492 if (filled)
6493 {
6494 int nextpos = TREE_INT_CST_LOW (nextpos_tree);
6495
6496 assemble_zeros (nextpos - TREE_INT_CST_LOW (filled));
6497 }
6498 }
6499 else
6500 {
6501 /* If it's not incremental, just skip over the gap,
6502 so that after jumping to retry we will output the next
6503 successive element. */
6504 if (TREE_CODE (constructor_type) == RECORD_TYPE
6505 || TREE_CODE (constructor_type) == UNION_TYPE)
6506 constructor_unfilled_fields = next;
6507 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6508 {
6509 TREE_INT_CST_LOW (constructor_unfilled_index)
6510 = TREE_INT_CST_LOW (next);
6511 TREE_INT_CST_HIGH (constructor_unfilled_index)
6512 = TREE_INT_CST_HIGH (next);
6513 }
6514 }
6515
6516 /* ELT now points to the node in the pending tree with the next
6517 initializer to output. */
6518 goto retry;
6519 }
6520 \f
6521 /* Add one non-braced element to the current constructor level.
6522 This adjusts the current position within the constructor's type.
6523 This may also start or terminate implicit levels
6524 to handle a partly-braced initializer.
6525
6526 Once this has found the correct level for the new element,
6527 it calls output_init_element.
6528
6529 Note: if we are incrementally outputting this constructor,
6530 this function may be called with a null argument
6531 representing a sub-constructor that was already incrementally output.
6532 When that happens, we output nothing, but we do the bookkeeping
6533 to skip past that element of the current constructor. */
6534
6535 void
6536 process_init_element (value)
6537 tree value;
6538 {
6539 tree orig_value = value;
6540 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6541
6542 /* Handle superfluous braces around string cst as in
6543 char x[] = {"foo"}; */
6544 if (string_flag
6545 && constructor_type
6546 && TREE_CODE (constructor_type) == ARRAY_TYPE
6547 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6548 && integer_zerop (constructor_unfilled_index))
6549 {
6550 constructor_stack->replacement_value = value;
6551 return;
6552 }
6553
6554 if (constructor_stack->replacement_value != 0)
6555 {
6556 error_init ("excess elements in struct initializer%s",
6557 " after `%s'", NULL_PTR);
6558 return;
6559 }
6560
6561 /* Ignore elements of a brace group if it is entirely superfluous
6562 and has already been diagnosed. */
6563 if (constructor_type == 0)
6564 return;
6565
6566 /* If we've exhausted any levels that didn't have braces,
6567 pop them now. */
6568 while (constructor_stack->implicit)
6569 {
6570 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6571 || TREE_CODE (constructor_type) == UNION_TYPE)
6572 && constructor_fields == 0)
6573 process_init_element (pop_init_level (1));
6574 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6575 && (constructor_max_index == 0
6576 || tree_int_cst_lt (constructor_max_index,
6577 constructor_index)))
6578 process_init_element (pop_init_level (1));
6579 else
6580 break;
6581 }
6582
6583 while (1)
6584 {
6585 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6586 {
6587 tree fieldtype;
6588 enum tree_code fieldcode;
6589
6590 if (constructor_fields == 0)
6591 {
6592 pedwarn_init ("excess elements in struct initializer%s",
6593 " after `%s'", NULL_PTR);
6594 break;
6595 }
6596
6597 fieldtype = TREE_TYPE (constructor_fields);
6598 if (fieldtype != error_mark_node)
6599 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6600 fieldcode = TREE_CODE (fieldtype);
6601
6602 /* Accept a string constant to initialize a subarray. */
6603 if (value != 0
6604 && fieldcode == ARRAY_TYPE
6605 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6606 && string_flag)
6607 value = orig_value;
6608 /* Otherwise, if we have come to a subaggregate,
6609 and we don't have an element of its type, push into it. */
6610 else if (value != 0 && !constructor_no_implicit
6611 && value != error_mark_node
6612 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6613 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6614 || fieldcode == UNION_TYPE))
6615 {
6616 push_init_level (1);
6617 continue;
6618 }
6619
6620 if (value)
6621 {
6622 push_member_name (constructor_fields);
6623 output_init_element (value, fieldtype, constructor_fields, 1);
6624 RESTORE_SPELLING_DEPTH (constructor_depth);
6625 }
6626 else
6627 /* Do the bookkeeping for an element that was
6628 directly output as a constructor. */
6629 {
6630 /* For a record, keep track of end position of last field. */
6631 tree temp = size_binop (PLUS_EXPR,
6632 DECL_FIELD_BITPOS (constructor_fields),
6633 DECL_SIZE (constructor_fields));
6634 TREE_INT_CST_LOW (constructor_bit_index)
6635 = TREE_INT_CST_LOW (temp);
6636 TREE_INT_CST_HIGH (constructor_bit_index)
6637 = TREE_INT_CST_HIGH (temp);
6638
6639 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6640 }
6641
6642 constructor_fields = TREE_CHAIN (constructor_fields);
6643 /* Skip any nameless bit fields at the beginning. */
6644 while (constructor_fields != 0
6645 && DECL_C_BIT_FIELD (constructor_fields)
6646 && DECL_NAME (constructor_fields) == 0)
6647 constructor_fields = TREE_CHAIN (constructor_fields);
6648 break;
6649 }
6650 if (TREE_CODE (constructor_type) == UNION_TYPE)
6651 {
6652 tree fieldtype;
6653 enum tree_code fieldcode;
6654
6655 if (constructor_fields == 0)
6656 {
6657 pedwarn_init ("excess elements in union initializer%s",
6658 " after `%s'", NULL_PTR);
6659 break;
6660 }
6661
6662 fieldtype = TREE_TYPE (constructor_fields);
6663 if (fieldtype != error_mark_node)
6664 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6665 fieldcode = TREE_CODE (fieldtype);
6666
6667 /* Accept a string constant to initialize a subarray. */
6668 if (value != 0
6669 && fieldcode == ARRAY_TYPE
6670 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6671 && string_flag)
6672 value = orig_value;
6673 /* Otherwise, if we have come to a subaggregate,
6674 and we don't have an element of its type, push into it. */
6675 else if (value != 0 && !constructor_no_implicit
6676 && value != error_mark_node
6677 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6678 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6679 || fieldcode == UNION_TYPE))
6680 {
6681 push_init_level (1);
6682 continue;
6683 }
6684
6685 if (value)
6686 {
6687 push_member_name (constructor_fields);
6688 output_init_element (value, fieldtype, constructor_fields, 1);
6689 RESTORE_SPELLING_DEPTH (constructor_depth);
6690 }
6691 else
6692 /* Do the bookkeeping for an element that was
6693 directly output as a constructor. */
6694 {
6695 TREE_INT_CST_LOW (constructor_bit_index)
6696 = TREE_INT_CST_LOW (DECL_SIZE (constructor_fields));
6697 TREE_INT_CST_HIGH (constructor_bit_index)
6698 = TREE_INT_CST_HIGH (DECL_SIZE (constructor_fields));
6699
6700 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6701 }
6702
6703 constructor_fields = 0;
6704 break;
6705 }
6706 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6707 {
6708 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6709 enum tree_code eltcode = TREE_CODE (elttype);
6710
6711 /* Accept a string constant to initialize a subarray. */
6712 if (value != 0
6713 && eltcode == ARRAY_TYPE
6714 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6715 && string_flag)
6716 value = orig_value;
6717 /* Otherwise, if we have come to a subaggregate,
6718 and we don't have an element of its type, push into it. */
6719 else if (value != 0 && !constructor_no_implicit
6720 && value != error_mark_node
6721 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6722 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6723 || eltcode == UNION_TYPE))
6724 {
6725 push_init_level (1);
6726 continue;
6727 }
6728
6729 if (constructor_max_index != 0
6730 && tree_int_cst_lt (constructor_max_index, constructor_index))
6731 {
6732 pedwarn_init ("excess elements in array initializer%s",
6733 " after `%s'", NULL_PTR);
6734 break;
6735 }
6736
6737 /* In the case of [LO .. HI] = VALUE, only evaluate VALUE once. */
6738 if (constructor_range_end)
6739 {
6740 if (constructor_max_index != 0
6741 && tree_int_cst_lt (constructor_max_index,
6742 constructor_range_end))
6743 {
6744 pedwarn_init ("excess elements in array initializer%s",
6745 " after `%s'", NULL_PTR);
6746 TREE_INT_CST_HIGH (constructor_range_end)
6747 = TREE_INT_CST_HIGH (constructor_max_index);
6748 TREE_INT_CST_LOW (constructor_range_end)
6749 = TREE_INT_CST_LOW (constructor_max_index);
6750 }
6751
6752 value = save_expr (value);
6753 }
6754
6755 /* Now output the actual element.
6756 Ordinarily, output once.
6757 If there is a range, repeat it till we advance past the range. */
6758 do
6759 {
6760 tree tem;
6761
6762 if (value)
6763 {
6764 push_array_bounds (TREE_INT_CST_LOW (constructor_index));
6765 output_init_element (value, elttype, constructor_index, 1);
6766 RESTORE_SPELLING_DEPTH (constructor_depth);
6767 }
6768
6769 tem = size_binop (PLUS_EXPR, constructor_index,
6770 integer_one_node);
6771 TREE_INT_CST_LOW (constructor_index) = TREE_INT_CST_LOW (tem);
6772 TREE_INT_CST_HIGH (constructor_index) = TREE_INT_CST_HIGH (tem);
6773
6774 if (!value)
6775 /* If we are doing the bookkeeping for an element that was
6776 directly output as a constructor,
6777 we must update constructor_unfilled_index. */
6778 {
6779 TREE_INT_CST_LOW (constructor_unfilled_index)
6780 = TREE_INT_CST_LOW (constructor_index);
6781 TREE_INT_CST_HIGH (constructor_unfilled_index)
6782 = TREE_INT_CST_HIGH (constructor_index);
6783 }
6784 }
6785 while (! (constructor_range_end == 0
6786 || tree_int_cst_lt (constructor_range_end,
6787 constructor_index)));
6788
6789 break;
6790 }
6791
6792 /* Handle the sole element allowed in a braced initializer
6793 for a scalar variable. */
6794 if (constructor_fields == 0)
6795 {
6796 pedwarn_init ("excess elements in scalar initializer%s",
6797 " after `%s'", NULL_PTR);
6798 break;
6799 }
6800
6801 if (value)
6802 output_init_element (value, constructor_type, NULL_TREE, 1);
6803 constructor_fields = 0;
6804 break;
6805 }
6806
6807 /* If the (lexically) previous elments are not now saved,
6808 we can discard the storage for them. */
6809 if (constructor_incremental && constructor_pending_elts == 0 && value != 0
6810 && constructor_stack == 0)
6811 clear_momentary ();
6812 }
6813 \f
6814 /* Expand an ASM statement with operands, handling output operands
6815 that are not variables or INDIRECT_REFS by transforming such
6816 cases into cases that expand_asm_operands can handle.
6817
6818 Arguments are same as for expand_asm_operands. */
6819
6820 void
6821 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6822 tree string, outputs, inputs, clobbers;
6823 int vol;
6824 char *filename;
6825 int line;
6826 {
6827 int noutputs = list_length (outputs);
6828 register int i;
6829 /* o[I] is the place that output number I should be written. */
6830 register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6831 register tree tail;
6832
6833 if (TREE_CODE (string) == ADDR_EXPR)
6834 string = TREE_OPERAND (string, 0);
6835 if (TREE_CODE (string) != STRING_CST)
6836 {
6837 error ("asm template is not a string constant");
6838 return;
6839 }
6840
6841 /* Record the contents of OUTPUTS before it is modified. */
6842 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6843 o[i] = TREE_VALUE (tail);
6844
6845 /* Perform default conversions on array and function inputs. */
6846 /* Don't do this for other types--
6847 it would screw up operands expected to be in memory. */
6848 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), i++)
6849 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6850 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6851 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6852
6853 /* Generate the ASM_OPERANDS insn;
6854 store into the TREE_VALUEs of OUTPUTS some trees for
6855 where the values were actually stored. */
6856 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6857
6858 /* Copy all the intermediate outputs into the specified outputs. */
6859 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6860 {
6861 if (o[i] != TREE_VALUE (tail))
6862 {
6863 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6864 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6865 free_temp_slots ();
6866 }
6867 /* Detect modification of read-only values.
6868 (Otherwise done by build_modify_expr.) */
6869 else
6870 {
6871 tree type = TREE_TYPE (o[i]);
6872 if (TREE_READONLY (o[i])
6873 || TYPE_READONLY (type)
6874 || ((TREE_CODE (type) == RECORD_TYPE
6875 || TREE_CODE (type) == UNION_TYPE)
6876 && C_TYPE_FIELDS_READONLY (type)))
6877 readonly_warning (o[i], "modification by `asm'");
6878 }
6879 }
6880
6881 /* Those MODIFY_EXPRs could do autoincrements. */
6882 emit_queue ();
6883 }
6884 \f
6885 /* Expand a C `return' statement.
6886 RETVAL is the expression for what to return,
6887 or a null pointer for `return;' with no value. */
6888
6889 void
6890 c_expand_return (retval)
6891 tree retval;
6892 {
6893 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6894
6895 if (TREE_THIS_VOLATILE (current_function_decl))
6896 warning ("function declared `noreturn' has a `return' statement");
6897
6898 if (!retval)
6899 {
6900 current_function_returns_null = 1;
6901 if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6902 warning ("`return' with no value, in function returning non-void");
6903 expand_null_return ();
6904 }
6905 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6906 {
6907 current_function_returns_null = 1;
6908 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6909 pedwarn ("`return' with a value, in function returning void");
6910 expand_return (retval);
6911 }
6912 else
6913 {
6914 tree t = convert_for_assignment (valtype, retval, "return",
6915 NULL_TREE, NULL_TREE, 0);
6916 tree res = DECL_RESULT (current_function_decl);
6917 tree inner;
6918
6919 if (t == error_mark_node)
6920 return;
6921
6922 inner = t = convert (TREE_TYPE (res), t);
6923
6924 /* Strip any conversions, additions, and subtractions, and see if
6925 we are returning the address of a local variable. Warn if so. */
6926 while (1)
6927 {
6928 switch (TREE_CODE (inner))
6929 {
6930 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6931 case PLUS_EXPR:
6932 inner = TREE_OPERAND (inner, 0);
6933 continue;
6934
6935 case MINUS_EXPR:
6936 /* If the second operand of the MINUS_EXPR has a pointer
6937 type (or is converted from it), this may be valid, so
6938 don't give a warning. */
6939 {
6940 tree op1 = TREE_OPERAND (inner, 1);
6941
6942 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6943 && (TREE_CODE (op1) == NOP_EXPR
6944 || TREE_CODE (op1) == NON_LVALUE_EXPR
6945 || TREE_CODE (op1) == CONVERT_EXPR))
6946 op1 = TREE_OPERAND (op1, 0);
6947
6948 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6949 break;
6950
6951 inner = TREE_OPERAND (inner, 0);
6952 continue;
6953 }
6954
6955 case ADDR_EXPR:
6956 inner = TREE_OPERAND (inner, 0);
6957
6958 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6959 inner = TREE_OPERAND (inner, 0);
6960
6961 if (TREE_CODE (inner) == VAR_DECL
6962 && ! DECL_EXTERNAL (inner)
6963 && ! TREE_STATIC (inner)
6964 && DECL_CONTEXT (inner) == current_function_decl)
6965 warning ("function returns address of local variable");
6966 break;
6967
6968 default:
6969 break;
6970 }
6971
6972 break;
6973 }
6974
6975 t = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6976 TREE_SIDE_EFFECTS (t) = 1;
6977 expand_return (t);
6978 current_function_returns_value = 1;
6979 }
6980 }
6981 \f
6982 /* Start a C switch statement, testing expression EXP.
6983 Return EXP if it is valid, an error node otherwise. */
6984
6985 tree
6986 c_expand_start_case (exp)
6987 tree exp;
6988 {
6989 register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
6990 tree type = TREE_TYPE (exp);
6991
6992 if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
6993 {
6994 error ("switch quantity not an integer");
6995 exp = error_mark_node;
6996 }
6997 else
6998 {
6999 tree index;
7000 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7001
7002 if (warn_traditional
7003 && (type == long_integer_type_node
7004 || type == long_unsigned_type_node))
7005 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
7006
7007 exp = default_conversion (exp);
7008 type = TREE_TYPE (exp);
7009 index = get_unwidened (exp, NULL_TREE);
7010 /* We can't strip a conversion from a signed type to an unsigned,
7011 because if we did, int_fits_type_p would do the wrong thing
7012 when checking case values for being in range,
7013 and it's too hard to do the right thing. */
7014 if (TREE_UNSIGNED (TREE_TYPE (exp))
7015 == TREE_UNSIGNED (TREE_TYPE (index)))
7016 exp = index;
7017 }
7018
7019 expand_start_case (1, exp, type, "switch statement");
7020
7021 return exp;
7022 }