1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1997, 1998,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 /* These routines are somewhat language-independent utility function
24 intended to be called by the language-specific convert () functions. */
28 #include "coretypes.h"
34 #include "langhooks.h"
37 /* Convert EXPR to some pointer or reference type TYPE.
38 EXPR must be pointer, reference, integer, enumeral, or literal zero;
39 in other cases error is called. */
42 convert_to_pointer (tree type, tree expr)
44 if (TREE_TYPE (expr) == type)
47 if (integer_zerop (expr))
49 tree t = build_int_cst (type, 0);
50 if (TREE_OVERFLOW (expr) || TREE_CONSTANT_OVERFLOW (expr))
51 t = force_fit_type (t, 0, TREE_OVERFLOW (expr),
52 TREE_CONSTANT_OVERFLOW (expr));
56 switch (TREE_CODE (TREE_TYPE (expr)))
60 return fold_build1 (NOP_EXPR, type, expr);
65 if (TYPE_PRECISION (TREE_TYPE (expr)) != POINTER_SIZE)
66 expr = fold_build1 (NOP_EXPR,
67 lang_hooks.types.type_for_size (POINTER_SIZE, 0),
69 return fold_build1 (CONVERT_EXPR, type, expr);
71 /* APPLE LOCAL begin blocks (C++ ck) */
72 case BLOCK_POINTER_TYPE:
73 /* APPLE LOCAL begin radar 5809099 */
75 || (TREE_CODE (type) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type))))
76 /* APPLE LOCAL end radar 5809099 */
77 return fold_build1 (NOP_EXPR, type, expr);
78 /* APPLE LOCAL end blocks (C++ ck) */
80 error ("cannot convert to a pointer type");
81 return convert_to_pointer (type, integer_zero_node);
85 /* APPLE LOCAL begin blocks (C++ ck) */
87 convert_to_block_pointer (tree type, tree expr)
89 if (TREE_TYPE (expr) == type)
92 if (integer_zerop (expr))
94 tree t = build_int_cst (type, 0);
95 if (TREE_OVERFLOW (expr) || TREE_CONSTANT_OVERFLOW (expr))
96 t = force_fit_type (t, 0, TREE_OVERFLOW (expr),
97 TREE_CONSTANT_OVERFLOW (expr));
101 switch (TREE_CODE (TREE_TYPE (expr)))
103 case BLOCK_POINTER_TYPE:
104 return fold_build1 (NOP_EXPR, type, expr);
107 if (TYPE_PRECISION (TREE_TYPE (expr)) != POINTER_SIZE)
108 expr = fold_build1 (NOP_EXPR,
109 lang_hooks.types.type_for_size (POINTER_SIZE, 0),
111 return fold_build1 (CONVERT_EXPR, type, expr);
114 /* APPLE LOCAL radar 5809099 */
115 if (objc_is_id (TREE_TYPE (expr)) || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (expr))))
116 return build1 (NOP_EXPR, type, expr);
120 error ("cannot convert to a block pointer type");
121 return convert_to_block_pointer (type, integer_zero_node);
125 /* APPLE LOCAL end blocks (C++ ck) */
127 /* Avoid any floating point extensions from EXP. */
129 strip_float_extensions (tree exp)
131 tree sub, expt, subt;
133 /* For floating point constant look up the narrowest type that can hold
134 it properly and handle it like (type)(narrowest_type)constant.
135 This way we can optimize for instance a=a*2.0 where "a" is float
136 but 2.0 is double constant. */
137 if (TREE_CODE (exp) == REAL_CST)
139 REAL_VALUE_TYPE orig;
142 orig = TREE_REAL_CST (exp);
143 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
144 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
145 type = float_type_node;
146 else if (TYPE_PRECISION (TREE_TYPE (exp))
147 > TYPE_PRECISION (double_type_node)
148 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
149 type = double_type_node;
151 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
154 if (TREE_CODE (exp) != NOP_EXPR
155 && TREE_CODE (exp) != CONVERT_EXPR)
158 sub = TREE_OPERAND (exp, 0);
159 subt = TREE_TYPE (sub);
160 expt = TREE_TYPE (exp);
162 if (!FLOAT_TYPE_P (subt))
165 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
168 return strip_float_extensions (sub);
172 /* Convert EXPR to some floating-point type TYPE.
174 EXPR must be float, integer, or enumeral;
175 in other cases error is called. */
178 convert_to_real (tree type, tree expr)
180 enum built_in_function fcode = builtin_mathfn_code (expr);
181 tree itype = TREE_TYPE (expr);
183 /* Disable until we figure out how to decide whether the functions are
184 present in runtime. */
185 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
187 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
188 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
192 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
229 tree arg0 = strip_float_extensions (TREE_VALUE (TREE_OPERAND (expr, 1)));
232 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
233 the both as the safe type for operation. */
234 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
235 newtype = TREE_TYPE (arg0);
237 /* Be careful about integer to fp conversions.
238 These may overflow still. */
239 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
240 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
241 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
242 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
245 tree fn = mathfn_built_in (newtype, fcode);
249 arglist = build_tree_list (NULL_TREE, fold (convert_to_real (newtype, arg0)));
250 expr = build_function_call_expr (fn, arglist);
261 && (((fcode == BUILT_IN_FLOORL
262 || fcode == BUILT_IN_CEILL
263 || fcode == BUILT_IN_ROUNDL
264 || fcode == BUILT_IN_RINTL
265 || fcode == BUILT_IN_TRUNCL
266 || fcode == BUILT_IN_NEARBYINTL)
267 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
268 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
269 || ((fcode == BUILT_IN_FLOOR
270 || fcode == BUILT_IN_CEIL
271 || fcode == BUILT_IN_ROUND
272 || fcode == BUILT_IN_RINT
273 || fcode == BUILT_IN_TRUNC
274 || fcode == BUILT_IN_NEARBYINT)
275 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
277 tree fn = mathfn_built_in (type, fcode);
282 = strip_float_extensions (TREE_VALUE (TREE_OPERAND (expr, 1)));
284 /* Make sure (type)arg0 is an extension, otherwise we could end up
285 changing (float)floor(double d) into floorf((float)d), which is
286 incorrect because (float)d uses round-to-nearest and can round
287 up to the next integer. */
288 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
290 build_function_call_expr (fn,
291 build_tree_list (NULL_TREE,
292 fold (convert_to_real (type, arg))));
296 /* Propagate the cast into the operation. */
297 if (itype != type && FLOAT_TYPE_P (type))
298 switch (TREE_CODE (expr))
300 /* Convert (float)-x into -(float)x. This is safe for
301 round-to-nearest rounding mode. */
304 if (!flag_rounding_math
305 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
306 return build1 (TREE_CODE (expr), type,
307 fold (convert_to_real (type,
308 TREE_OPERAND (expr, 0))));
310 /* Convert (outertype)((innertype0)a+(innertype1)b)
311 into ((newtype)a+(newtype)b) where newtype
312 is the widest mode from all of these. */
318 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
319 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
321 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
322 && FLOAT_TYPE_P (TREE_TYPE (arg1)))
326 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
327 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode)
328 newtype = dfloat32_type_node;
329 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
330 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode)
331 newtype = dfloat64_type_node;
332 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
333 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode)
334 newtype = dfloat128_type_node;
335 if (newtype == dfloat32_type_node
336 || newtype == dfloat64_type_node
337 || newtype == dfloat128_type_node)
339 expr = build2 (TREE_CODE (expr), newtype,
340 fold (convert_to_real (newtype, arg0)),
341 fold (convert_to_real (newtype, arg1)));
347 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
348 newtype = TREE_TYPE (arg0);
349 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
350 newtype = TREE_TYPE (arg1);
351 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype))
353 expr = build2 (TREE_CODE (expr), newtype,
354 fold (convert_to_real (newtype, arg0)),
355 fold (convert_to_real (newtype, arg1)));
366 switch (TREE_CODE (TREE_TYPE (expr)))
369 /* Ignore the conversion if we don't need to store intermediate
370 results and neither type is a decimal float. */
371 return build1 ((flag_float_store
372 || DECIMAL_FLOAT_TYPE_P (type)
373 || DECIMAL_FLOAT_TYPE_P (itype))
374 ? CONVERT_EXPR : NOP_EXPR, type, expr);
379 return build1 (FLOAT_EXPR, type, expr);
382 return convert (type,
383 fold_build1 (REALPART_EXPR,
384 TREE_TYPE (TREE_TYPE (expr)), expr));
388 error ("pointer value used where a floating point value was expected");
389 return convert_to_real (type, integer_zero_node);
392 error ("aggregate value used where a float was expected");
393 return convert_to_real (type, integer_zero_node);
397 /* Convert EXPR to some integer (or enum) type TYPE.
399 EXPR must be pointer, integer, discrete (enum, char, or bool), float, or
400 vector; in other cases error is called.
402 The result of this is always supposed to be a newly created tree node
403 not in use in any existing structure. */
406 convert_to_integer (tree type, tree expr)
408 enum tree_code ex_form = TREE_CODE (expr);
409 tree intype = TREE_TYPE (expr);
410 unsigned int inprec = TYPE_PRECISION (intype);
411 unsigned int outprec = TYPE_PRECISION (type);
413 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
414 be. Consider `enum E = { a, b = (enum E) 3 };'. */
415 if (!COMPLETE_TYPE_P (type))
417 error ("conversion to incomplete type");
418 return error_mark_node;
421 /* Convert e.g. (long)round(d) -> lround(d). */
422 /* If we're converting to char, we may encounter differing behavior
423 between converting from double->char vs double->long->char.
424 We're in "undefined" territory but we prefer to be conservative,
425 so only proceed in "unsafe" math mode. */
427 && (flag_unsafe_math_optimizations
428 || (long_integer_type_node
429 && outprec >= TYPE_PRECISION (long_integer_type_node))))
431 tree s_expr = strip_float_extensions (expr);
432 tree s_intype = TREE_TYPE (s_expr);
433 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
438 CASE_FLT_FN (BUILT_IN_CEIL):
439 /* Only convert in ISO C99 mode. */
440 if (!TARGET_C99_FUNCTIONS)
442 if (outprec < TYPE_PRECISION (long_integer_type_node)
443 || (outprec == TYPE_PRECISION (long_integer_type_node)
444 && !TYPE_UNSIGNED (type)))
445 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
446 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
447 && !TYPE_UNSIGNED (type))
448 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
451 CASE_FLT_FN (BUILT_IN_FLOOR):
452 /* Only convert in ISO C99 mode. */
453 if (!TARGET_C99_FUNCTIONS)
455 if (outprec < TYPE_PRECISION (long_integer_type_node)
456 || (outprec == TYPE_PRECISION (long_integer_type_node)
457 && !TYPE_UNSIGNED (type)))
458 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
459 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
460 && !TYPE_UNSIGNED (type))
461 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
464 CASE_FLT_FN (BUILT_IN_ROUND):
465 if (outprec < TYPE_PRECISION (long_integer_type_node)
466 || (outprec == TYPE_PRECISION (long_integer_type_node)
467 && !TYPE_UNSIGNED (type)))
468 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
469 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
470 && !TYPE_UNSIGNED (type))
471 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
474 CASE_FLT_FN (BUILT_IN_NEARBYINT):
475 /* Only convert nearbyint* if we can ignore math exceptions. */
476 if (flag_trapping_math)
478 /* ... Fall through ... */
479 CASE_FLT_FN (BUILT_IN_RINT):
480 if (outprec < TYPE_PRECISION (long_integer_type_node)
481 || (outprec == TYPE_PRECISION (long_integer_type_node)
482 && !TYPE_UNSIGNED (type)))
483 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
484 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
485 && !TYPE_UNSIGNED (type))
486 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
489 CASE_FLT_FN (BUILT_IN_TRUNC):
491 tree arglist = TREE_OPERAND (s_expr, 1);
492 return convert_to_integer (type, TREE_VALUE (arglist));
501 tree arglist = TREE_OPERAND (s_expr, 1);
502 tree newexpr = build_function_call_expr (fn, arglist);
503 return convert_to_integer (type, newexpr);
507 switch (TREE_CODE (intype))
511 /* APPLE LOCAL radar 6035389 */
512 case BLOCK_POINTER_TYPE:
513 if (integer_zerop (expr))
514 return build_int_cst (type, 0);
516 /* Convert to an unsigned integer of the correct width first,
517 and from there widen/truncate to the required type. */
518 expr = fold_build1 (CONVERT_EXPR,
519 lang_hooks.types.type_for_size (POINTER_SIZE, 0),
521 return fold_convert (type, expr);
526 /* If this is a logical operation, which just returns 0 or 1, we can
527 change the type of the expression. */
529 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
531 expr = copy_node (expr);
532 TREE_TYPE (expr) = type;
536 /* If we are widening the type, put in an explicit conversion.
537 Similarly if we are not changing the width. After this, we know
538 we are truncating EXPR. */
540 else if (outprec >= inprec)
545 /* If the precision of the EXPR's type is K bits and the
546 destination mode has more bits, and the sign is changing,
547 it is not safe to use a NOP_EXPR. For example, suppose
548 that EXPR's type is a 3-bit unsigned integer type, the
549 TYPE is a 3-bit signed integer type, and the machine mode
550 for the types is 8-bit QImode. In that case, the
551 conversion necessitates an explicit sign-extension. In
552 the signed-to-unsigned case the high-order bits have to
554 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
555 && (TYPE_PRECISION (TREE_TYPE (expr))
556 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
561 tem = fold_unary (code, type, expr);
565 tem = build1 (code, type, expr);
566 TREE_NO_WARNING (tem) = 1;
570 /* If TYPE is an enumeral type or a type with a precision less
571 than the number of bits in its mode, do the conversion to the
572 type corresponding to its mode, then do a nop conversion
574 else if (TREE_CODE (type) == ENUMERAL_TYPE
575 || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
576 return build1 (NOP_EXPR, type,
577 convert (lang_hooks.types.type_for_mode
578 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
581 /* Here detect when we can distribute the truncation down past some
582 arithmetic. For example, if adding two longs and converting to an
583 int, we can equally well convert both to ints and then add.
584 For the operations handled here, such truncation distribution
586 It is desirable in these cases:
587 1) when truncating down to full-word from a larger size
588 2) when truncating takes no work.
589 3) when at least one operand of the arithmetic has been extended
590 (as by C's default conversions). In this case we need two conversions
591 if we do the arithmetic as already requested, so we might as well
592 truncate both and then combine. Perhaps that way we need only one.
594 Note that in general we cannot do the arithmetic in a type
595 shorter than the desired result of conversion, even if the operands
596 are both extended from a shorter type, because they might overflow
597 if combined in that type. The exceptions to this--the times when
598 two narrow values can be combined in their narrow type even to
599 make a wider result--are handled by "shorten" in build_binary_op. */
604 /* We can pass truncation down through right shifting
605 when the shift count is a nonpositive constant. */
606 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
607 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
612 /* We can pass truncation down through left shifting
613 when the shift count is a nonnegative constant and
614 the target type is unsigned. */
615 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
616 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
617 && TYPE_UNSIGNED (type)
618 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
620 /* If shift count is less than the width of the truncated type,
622 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
623 /* In this case, shifting is like multiplication. */
627 /* If it is >= that width, result is zero.
628 Handling this with trunc1 would give the wrong result:
629 (int) ((long long) a << 32) is well defined (as 0)
630 but (int) a << 32 is undefined and would get a
633 tree t = build_int_cst (type, 0);
635 /* If the original expression had side-effects, we must
637 if (TREE_SIDE_EFFECTS (expr))
638 return build2 (COMPOUND_EXPR, type, expr, t);
649 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
650 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
652 /* Don't distribute unless the output precision is at least as big
653 as the actual inputs. Otherwise, the comparison of the
654 truncated values will be wrong. */
655 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
656 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
657 /* If signedness of arg0 and arg1 don't match,
658 we can't necessarily find a type to compare them in. */
659 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
660 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
672 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
673 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
675 if (outprec >= BITS_PER_WORD
676 || TRULY_NOOP_TRUNCATION (outprec, inprec)
677 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
678 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
680 /* Do the arithmetic in type TYPEX,
681 then convert result to TYPE. */
684 /* Can't do arithmetic in enumeral types
685 so use an integer type that will hold the values. */
686 if (TREE_CODE (typex) == ENUMERAL_TYPE)
687 typex = lang_hooks.types.type_for_size
688 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
690 /* But now perhaps TYPEX is as wide as INPREC.
691 In that case, do nothing special here.
692 (Otherwise would recurse infinitely in convert. */
693 if (TYPE_PRECISION (typex) != inprec)
695 /* Don't do unsigned arithmetic where signed was wanted,
697 Exception: if both of the original operands were
698 unsigned then we can safely do the work as unsigned.
699 Exception: shift operations take their type solely
700 from the first argument.
701 Exception: the LSHIFT_EXPR case above requires that
702 we perform this operation unsigned lest we produce
703 signed-overflow undefinedness.
704 And we may need to do it as unsigned
705 if we truncate to the original size. */
706 if (TYPE_UNSIGNED (TREE_TYPE (expr))
707 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
708 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
709 || ex_form == LSHIFT_EXPR
710 || ex_form == RSHIFT_EXPR
711 || ex_form == LROTATE_EXPR
712 || ex_form == RROTATE_EXPR))
713 || ex_form == LSHIFT_EXPR
714 /* If we have !flag_wrapv, and either ARG0 or
715 ARG1 is of a signed type, we have to do
716 PLUS_EXPR or MINUS_EXPR in an unsigned
717 type. Otherwise, we would introduce
718 signed-overflow undefinedness. */
719 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
720 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
721 && (ex_form == PLUS_EXPR
722 || ex_form == MINUS_EXPR)))
723 typex = lang_hooks.types.unsigned_type (typex);
725 typex = lang_hooks.types.signed_type (typex);
726 return convert (type,
727 fold_build2 (ex_form, typex,
728 convert (typex, arg0),
729 convert (typex, arg1)));
737 /* This is not correct for ABS_EXPR,
738 since we must test the sign before truncation. */
742 /* Don't do unsigned arithmetic where signed was wanted,
744 if (TYPE_UNSIGNED (TREE_TYPE (expr)))
745 typex = lang_hooks.types.unsigned_type (type);
747 typex = lang_hooks.types.signed_type (type);
748 return convert (type,
749 fold_build1 (ex_form, typex,
751 TREE_OPERAND (expr, 0))));
756 "can't convert between vector values of different size" error. */
757 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
758 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
759 != GET_MODE_SIZE (TYPE_MODE (type))))
761 /* If truncating after truncating, might as well do all at once.
762 If truncating after extending, we may get rid of wasted work. */
763 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
766 /* It is sometimes worthwhile to push the narrowing down through
767 the conditional and never loses. */
768 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
769 convert (type, TREE_OPERAND (expr, 1)),
770 convert (type, TREE_OPERAND (expr, 2)));
776 return build1 (CONVERT_EXPR, type, expr);
779 return build1 (FIX_TRUNC_EXPR, type, expr);
782 return convert (type,
783 fold_build1 (REALPART_EXPR,
784 TREE_TYPE (TREE_TYPE (expr)), expr));
787 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
789 error ("can't convert between vector values of different size");
790 return error_mark_node;
792 return build1 (VIEW_CONVERT_EXPR, type, expr);
795 error ("aggregate value used where an integer was expected");
796 return convert (type, integer_zero_node);
800 /* Convert EXPR to the complex type TYPE in the usual ways. */
803 convert_to_complex (tree type, tree expr)
805 tree subtype = TREE_TYPE (type);
807 switch (TREE_CODE (TREE_TYPE (expr)))
813 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
814 convert (subtype, integer_zero_node));
818 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
820 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
822 else if (TREE_CODE (expr) == COMPLEX_EXPR)
823 return fold_build2 (COMPLEX_EXPR, type,
824 convert (subtype, TREE_OPERAND (expr, 0)),
825 convert (subtype, TREE_OPERAND (expr, 1)));
828 expr = save_expr (expr);
830 fold_build2 (COMPLEX_EXPR, type,
832 fold_build1 (REALPART_EXPR,
833 TREE_TYPE (TREE_TYPE (expr)),
836 fold_build1 (IMAGPART_EXPR,
837 TREE_TYPE (TREE_TYPE (expr)),
844 error ("pointer value used where a complex was expected");
845 return convert_to_complex (type, integer_zero_node);
848 error ("aggregate value used where a complex was expected");
849 return convert_to_complex (type, integer_zero_node);
853 /* Convert EXPR to the vector type TYPE in the usual ways. */
856 convert_to_vector (tree type, tree expr)
858 switch (TREE_CODE (TREE_TYPE (expr)))
862 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
864 error ("can't convert between vector values of different size");
865 return error_mark_node;
867 return build1 (VIEW_CONVERT_EXPR, type, expr);
870 error ("can't convert value to a vector");
871 return error_mark_node;