2010-01-07 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / convert.cs
index 72cead3f4bb8f56f99a69d817d2af6f6410761ce..c1e9980f2931e3930c3e6599ac6f30e20cc45093 100644 (file)
 // Copyright 2003-2008 Novell, Inc.
 //
 
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Reflection;
+using System.Reflection.Emit;
+
 namespace Mono.CSharp {
-       using System;
-       using System.Collections;
-       using System.Diagnostics;
-       using System.Reflection;
-       using System.Reflection.Emit;
 
        //
        // A container class for all the conversion operations
@@ -40,7 +41,7 @@ namespace Mono.CSharp {
                
                static Type TypeParam_EffectiveBaseType (GenericConstraints gc)
                {
-                       ArrayList list = new ArrayList ();
+                       var list = new List<Type> ();
                        list.Add (gc.EffectiveBaseClass);
                        foreach (Type t in gc.InterfaceConstraints) {
                                if (!TypeManager.IsGenericParameter (t))
@@ -245,7 +246,7 @@ namespace Mono.CSharp {
                        //
                        NullLiteral nl = expr as NullLiteral;
                        if (nl != null) {
-                               return nl.ConvertImplicitly(target_type);
+                               return nl.ConvertImplicitly (null, target_type);
                        }
 
                        if (ImplicitReferenceConversionExists (expr, target_type)) {
@@ -463,33 +464,44 @@ namespace Mono.CSharp {
                        if (expr_type == TypeManager.null_type)
                                return ec == null ? EmptyExpression.Null : Nullable.LiftedNull.Create (target_type, expr.Location);
 
-                       Type target = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (target_type)[0]);
-                       Expression e;
+                       // S -> T?
+                       Type t_el = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (target_type)[0]);
 
                        // S? -> T?
-                       if (TypeManager.IsNullableType (expr_type)) {
-                               Type etype = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (expr_type)[0]);
+                       if (TypeManager.IsNullableType (expr_type))
+                               expr_type = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (expr_type)[0]);
 
-                               if (ec == null)
-                                       return ImplicitConversionExists (ec, new EmptyExpression (etype), target) ? EmptyExpression.Null : null;
+                       //
+                       // Predefined implicit identity or implicit numeric conversion
+                       // has to exist between underlying type S and underlying type T
+                       //
 
-                               Expression unwrap = Nullable.Unwrap.Create (expr);
-                               e = ImplicitConversion (ec, unwrap, target, expr.Location);
-                               if (e == null)
-                                       return null;
+                       // Handles probing
+                       if (ec == null) {
+                               if (expr_type == t_el)
+                                       return EmptyExpression.Null;
 
-                               return new Nullable.Lifted (e, unwrap, target_type).Resolve (ec);
+                               return ImplicitNumericConversion (null, expr_type, t_el);
                        }
 
-                       // S -> T?
-                       if (ec == null)
-                               return ImplicitConversionExists (ec, expr, target) ? EmptyExpression.Null : null;
+                       Expression unwrap;
+                       if (expr_type != expr.Type)
+                               unwrap = Nullable.Unwrap.Create (expr);
+                       else
+                               unwrap = expr;
 
-                       e = ImplicitConversion (ec, expr, target, expr.Location);
-                       if (e != null)
-                               return Nullable.Wrap.Create (e, target_type);
+                       Expression conv = expr_type == t_el ? unwrap : ImplicitNumericConversion (unwrap, expr_type, t_el);
+                       if (conv == null)
+                               return null;
 
-                       return null;
+                       if (expr_type != expr.Type)
+                               return new Nullable.Lifted (conv, unwrap, target_type).Resolve (ec);
+
+                       // Do constant optimization for S -> T?
+                       if (unwrap is Constant)
+                               conv = ((Constant) unwrap).ConvertImplicitly (ec, t_el);
+
+                       return Nullable.Wrap.Create (conv, target_type);
                }
 
                /// <summary>
@@ -678,11 +690,6 @@ namespace Mono.CSharp {
                        return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
                }
 
-               public static bool ImplicitUserConversionExists (ResolveContext ec, Type source, Type target)
-               {
-                       return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
-               }
-
                /// <summary>
                ///  Determines if a standard implicit conversion exists from
                ///  expr_type to target_type
@@ -696,7 +703,7 @@ namespace Mono.CSharp {
                        if (expr_type == TypeManager.null_type) {
                                NullLiteral nl = expr as NullLiteral;
                                if (nl != null)
-                                       return nl.ConvertImplicitly (target_type) != null;
+                                       return nl.ConvertImplicitly (null, target_type) != null;
                        }
 
                        if (expr_type == TypeManager.void_type)
@@ -751,7 +758,7 @@ namespace Mono.CSharp {
                                                return true;
                                }
 
-                               if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
+                               if (value == 0 && TypeManager.IsEnumType (target_type))
                                        return true;
                        }
 
@@ -787,7 +794,7 @@ namespace Mono.CSharp {
                ///  Finds "most encompassed type" according to the spec (13.4.2)
                ///  amongst the methods in the MethodGroupExpr
                /// </summary>
-               static Type FindMostEncompassedType (ArrayList types)
+               static Type FindMostEncompassedType (IList<Type> types)
                {
                        Type best = null;
 
@@ -829,7 +836,7 @@ namespace Mono.CSharp {
                ///  Finds "most encompassing type" according to the spec (13.4.2)
                ///  amongst the types in the given set
                /// </summary>
-               static Type FindMostEncompassingType (ArrayList types)
+               static Type FindMostEncompassingType (IList<Type> types)
                {
                        Type best = null;
 
@@ -837,7 +844,7 @@ namespace Mono.CSharp {
                                return null;
 
                        if (types.Count == 1)
-                               return (Type) types [0];
+                               return types [0];
 
                        EmptyExpression expr = EmptyExpression.Grab ();
 
@@ -872,10 +879,10 @@ namespace Mono.CSharp {
                ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
                ///   for explicit and implicit conversion operators.
                /// </summary>
-               static public Type FindMostSpecificSource (IList list,
+               static public Type FindMostSpecificSource (IList<MethodInfo> list,
                                                           Expression source, bool apply_explicit_conv_rules)
                {
-                       ArrayList src_types_set = new ArrayList ();
+                       var src_types_set = new List<Type> ();
 
                        //
                        // If any operator converts from S then Sx = S
@@ -895,7 +902,7 @@ namespace Mono.CSharp {
                        // Explicit Conv rules
                        //
                        if (apply_explicit_conv_rules) {
-                               ArrayList candidate_set = new ArrayList ();
+                               var candidate_set = new List<Type> ();
 
                                foreach (Type param_type in src_types_set){
                                        if (ImplicitStandardConversionExists (source, param_type))
@@ -918,10 +925,10 @@ namespace Mono.CSharp {
                /// <summary>
                ///  Finds the most specific target Tx according to section 13.4.4
                /// </summary>
-               static public Type FindMostSpecificTarget (IList list,
+               static public Type FindMostSpecificTarget (IList<MethodInfo> list,
                                                           Type target, bool apply_explicit_conv_rules)
                {
-                       ArrayList tgt_types_set = new ArrayList ();
+                       var tgt_types_set = new List<Type> ();
 
                        //
                        // If any operator converts to T then Tx = T
@@ -938,7 +945,7 @@ namespace Mono.CSharp {
                        // Explicit conv rules
                        //
                        if (apply_explicit_conv_rules) {
-                               ArrayList candidate_set = new ArrayList ();
+                               var candidate_set = new List<Type> ();
 
                                EmptyExpression expr = EmptyExpression.Grab ();
 
@@ -970,27 +977,19 @@ namespace Mono.CSharp {
                static public Expression ImplicitUserConversion (ResolveContext ec, Expression source,
                                                                 Type target, Location loc)
                {
-                       Expression expr = UserDefinedConversion (ec, source, target, loc, false);
-                       if (expr != null && !TypeManager.IsEqual (expr.Type, target))
-                               expr = ImplicitConversionStandard (ec, expr, target, loc);
-
-                       return expr;
+                       return UserDefinedConversion (ec, source, target, loc, false, true);
                }
 
                /// <summary>
                ///  User-defined Explicit conversions
                /// </summary>
-               static public Expression ExplicitUserConversion (ResolveContext ec, Expression source,
+               static Expression ExplicitUserConversion (ResolveContext ec, Expression source,
                                                                 Type target, Location loc)
                {
-                       Expression expr = UserDefinedConversion (ec, source, target, loc, true);
-                       if (expr != null && !TypeManager.IsEqual (expr.Type, target))
-                               expr = ExplicitConversionStandard (ec, expr, target, loc);
-
-                       return expr;
+                       return UserDefinedConversion (ec, source, target, loc, true, true);
                }
 
-               static void AddConversionOperators (ArrayList list,
+               static void AddConversionOperators (List<MethodInfo> list,
                                                    Expression source, Type target_type,
                                                    bool look_for_explicit,
                                                    MethodGroupExpr mg)
@@ -1056,7 +1055,7 @@ namespace Mono.CSharp {
                /// </summary>
                static MethodInfo GetConversionOperator (CompilerContext ctx, Type container_type, Expression source, Type target_type, bool look_for_explicit)
                {
-                       ArrayList ops = new ArrayList (4);
+                       var ops = new List<MethodInfo> (4);
 
                        Type source_type = source.Type;
 
@@ -1110,12 +1109,13 @@ namespace Mono.CSharp {
                /// <summary>
                ///   User-defined conversions
                /// </summary>
-               static public Expression UserDefinedConversion (ResolveContext ec, Expression source,
+               public static Expression UserDefinedConversion (ResolveContext ec, Expression source,
                                                                Type target, Location loc,
-                                                               bool look_for_explicit)
+                                                               bool look_for_explicit, bool return_convert)
                {
                        Type source_type = source.Type;
                        MethodInfo method = null;
+                       Expression expr = null;
 
                        object o;
                        DoubleHash hash;
@@ -1135,31 +1135,80 @@ namespace Mono.CSharp {
                                if (TypeManager.IsDynamicType (source_type))
                                        return null;
 
-                               method = GetConversionOperator (RootContext.ToplevelTypes.Compiler, null, source, target, look_for_explicit);
-                               if (!(source is Constant))
-                                       hash.Insert (source_type, target, method);
+                               method = GetConversionOperator (ec.Compiler, null, source, target, look_for_explicit);
                        }
 
-                       if (method == null)
-                               return null;
+                       if (method != null) {
+                               Type most_specific_source = TypeManager.GetParameterData (method).Types[0];
 
-                       Type most_specific_source = TypeManager.GetParameterData (method).Types [0];
+                               //
+                               // This will do the conversion to the best match that we
+                               // found.  Now we need to perform an implict standard conversion
+                               // if the best match was not the type that we were requested
+                               // by target.
+                               //
+                               if (look_for_explicit) {
+                                       ReportPrinter temp = new SessionReportPrinter ();
+                                       ReportPrinter prev = ec.Report.SetPrinter (temp);
 
-                       //
-                       // This will do the conversion to the best match that we
-                       // found.  Now we need to perform an implict standard conversion
-                       // if the best match was not the type that we were requested
-                       // by target.
-                       //
-                       if (look_for_explicit)
-                               source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
-                       else
-                               source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
+                                       expr = ExplicitConversionStandard (ec, source, most_specific_source, loc);
 
-                       if (source == null)
-                               return null;
+                                       ec.Report.SetPrinter (prev);
+                                       if (temp.ErrorsCount != 0)
+                                               expr = null;
+                               } else {
+                                       if (ImplicitStandardConversionExists (source, most_specific_source))
+                                               expr = ImplicitConversionStandard (ec, source, most_specific_source, loc);
+                                       else
+                                               expr = null;
+                               }
+                       }
+
+                       if (expr == null) {
+                               bool nullable = false;
 
-                       return new UserCast (method, source, loc).DoResolve (ec);
+                               if (TypeManager.IsNullableType (source_type)) {
+                                       source = Nullable.Unwrap.Create (source);
+                                       nullable = true;
+                               }
+
+                               Type target_underlying;
+                               if (TypeManager.IsNullableType (target)) {
+                                       target_underlying = TypeManager.GetTypeArguments (target)[0];
+                                       nullable = true;
+                               } else {
+                                       // No implicit conversion S? -> T for non-reference type T
+                                       if (!look_for_explicit && !TypeManager.IsReferenceType (target))
+                                               nullable = false;
+
+                                       target_underlying = target;
+                               }
+
+                               if (nullable) {
+                                       expr = UserDefinedConversion (ec, source, target_underlying, loc, look_for_explicit, return_convert);
+
+                                       // Do result expression lifting only when it's needed
+                                       if (expr != null && (!look_for_explicit || TypeManager.IsReferenceType (target)))
+                                               expr = new Nullable.Lifted (expr, source, target).Resolve (ec);
+
+                                       return expr;
+                               }
+                       } else {
+                               expr = new UserCast (method, expr, loc).Resolve (ec);
+
+                               if (return_convert && !TypeManager.IsEqual (expr.Type, target)) {
+                                       if (look_for_explicit) {
+                                               expr = ExplicitConversionStandard (ec, expr, target, loc);
+                                       } else {
+                                               expr = ImplicitConversionStandard (ec, expr, target, loc);
+                                       }
+                               }
+                       }
+
+                       if (!(source is Constant))
+                               hash.Insert (source_type, target, method);
+
+                       return expr;
                }
 
                /// <summary>
@@ -1243,7 +1292,7 @@ namespace Mono.CSharp {
                        Constant c = expr as Constant;
                        if (c != null) {
                                try {
-                                       c = c.ConvertImplicitly (target_type);
+                                       c = c.ConvertImplicitly (ec, target_type);
                                } catch {
                                        Console.WriteLine ("Conversion error happened in line {0}", loc);
                                        throw;
@@ -1270,7 +1319,7 @@ namespace Mono.CSharp {
                                // type is an enum-type
                                //
                                if (i.IsDefaultValue)
-                                       return new EnumConstant (i, target_type);
+                                       return new EnumConstant (i, target_type).Resolve (ec);
                        }
 
                        if (ec.IsUnsafe) {
@@ -1298,7 +1347,7 @@ namespace Mono.CSharp {
                                AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
                                Expression am = ame.Compatible (ec, target_type);
                                if (am != null)
-                                       return am.DoResolve (ec);
+                                       return am.Resolve (ec);
                        }
 
                        if (expr_type == InternalType.Arglist && target_type == TypeManager.arg_iterator_type)
@@ -1319,6 +1368,12 @@ namespace Mono.CSharp {
                        if (e != null)
                                return e;
 
+                       if (TypeManager.IsDynamicType (source.Type)) {
+                               Arguments args = new Arguments (1);
+                               args.Add (new Argument (source));
+                               return new DynamicConversion (target_type, 0, args, loc).Resolve (ec);
+                       }
+
                        source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
                        return null;
                }
@@ -1819,25 +1874,32 @@ namespace Mono.CSharp {
 
                                if (expr_type == TypeManager.ushort_type ||
                                        expr_type == TypeManager.uint32_type ||
-                                       expr_type == TypeManager.uint64_type || expr_type == TypeManager.int64_type ||
                                        expr_type == TypeManager.byte_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
+
+                               if (expr_type == TypeManager.int64_type)
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_I);
+
+                               if (expr_type == TypeManager.uint64_type)
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_I);
                        }
 
                        if (expr_type.IsPointer){
                                if (target_type == TypeManager.sbyte_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
-                               else if (target_type == TypeManager.byte_type)
+                               if (target_type == TypeManager.byte_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
-                               else if (target_type == TypeManager.short_type)
+                               if (target_type == TypeManager.short_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
-                               else if (target_type == TypeManager.ushort_type)
+                               if (target_type == TypeManager.ushort_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
-                               else if (target_type == TypeManager.int32_type)
+                               if (target_type == TypeManager.int32_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
-                               else if (target_type == TypeManager.uint32_type)
+                               if (target_type == TypeManager.uint32_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
-                               else if (target_type == TypeManager.uint64_type || target_type == TypeManager.int64_type)
+                               if (target_type == TypeManager.int64_type)
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I_I8);
+                               if (target_type == TypeManager.uint64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
                        }
                        return null;
@@ -1880,8 +1942,20 @@ namespace Mono.CSharp {
                        Type target_type, Location loc)
                {
                        Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
-                       if (e != null)
+                       if (e != null) {
+                               //
+                               // Don't eliminate explicit precission casts
+                               //
+                               if (e == expr) {
+                                       if (target_type == TypeManager.float_type)
+                                               return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
+                                       
+                                       if (target_type == TypeManager.double_type)
+                                               return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
+                               }
+                                       
                                return e;
+                       }
 
                        Type expr_type = expr.Type;
                        if (TypeManager.IsNullableType (target_type)) {
@@ -1909,10 +1983,9 @@ namespace Mono.CSharp {
                                if (ImplicitBoxingConversionExists (e, target_type, out use_class_cast))
                                        return new BoxedCast (expr, target_type);
                                
-                               e = ExplicitConversion (ec, e, target_type, loc);
+                               e = ExplicitConversionCore (ec, e, target_type, loc);
                                if (e != null)
-                                       e = EmptyCast.Create (e, target_type);
-                               return e;
+                                       return EmptyCast.Create (e, target_type);
                        }
                        
                        e = ExplicitUserConversion (ec, expr, target_type, loc);