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