示例#1
0
        internal static Expression ExplicitConvert(Expression /*!*/ expr, Type /*!*/ fromType, Type /*!*/ toType)
        {
            expr = AstUtils.Convert(expr, fromType);

            if (HasExplicitNumericConversion(fromType, toType))
            {
                // special cases to mimic Ruby behavior precisely:
                if (fromType == typeof(BigInteger))
                {
                    if (toType == typeof(int))
                    {
                        return(Methods.ConvertBignumToFixnum.OpCall(expr));
                    }
                    else if (toType == typeof(double))
                    {
                        return(Methods.ConvertBignumToFloat.OpCall(expr));
                    }
                }
                else if (fromType == typeof(double) && toType == typeof(int))
                {
                    return(Methods.ConvertDoubleToFixnum.OpCall(expr));
                }

                return(Ast.ConvertChecked(expr, toType));
            }

            MethodInfo converter = CompilerHelpers.GetExplicitConverter(fromType, toType);

            if (converter != null)
            {
                return(Ast.Call(null, converter, expr));
            }

            if (fromType == typeof(char) && toType == typeof(string))
            {
                return(Ast.Call(null, fromType.GetMethod("ToString", BindingFlags.Public | BindingFlags.Static), expr));
            }

            if (toType == typeof(bool))
            {
                Debug.Assert(fromType != typeof(bool));
                return(fromType.IsValueType ? AstUtils.Constant(true) : Ast.NotEqual(expr, AstUtils.Constant(null)));
            }

            // TODO:
            //if (TypeConverter...(fromType, toType)) {
            //    return true;
            //}

            return(null);
        }