disable the building of mjs while I solve the problem with the unexposed ctr of Context
[mono.git] / mcs / mcs / convert.cs
index 29bc161d745ddd3931a92adc3197e22d69c1ef01..2bc65667b7d0e3ac55090c123df97f5467f210b7 100644 (file)
@@ -64,7 +64,7 @@ namespace Mono.CSharp {
                                if (expr_type.IsValueType)
                                        return new BoxedCast (expr);
                                if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
-                                       if (target_type == TypeManager.anonymous_method_type)
+                                       if (expr_type == TypeManager.anonymous_method_type)
                                                return null;
                                        return new EmptyCast (expr, target_type);
                                }
@@ -97,7 +97,7 @@ namespace Mono.CSharp {
                        // from the null type to any reference-type.
                        if (expr_type == TypeManager.null_type){
                                if (target_type.IsPointer)
-                                       return NullPointer.Null;
+                                       return new EmptyCast (expr, target_type);
                                        
                                if (!target_type.IsValueType)
                                        return new NullCast (expr, target_type);
@@ -198,6 +198,16 @@ namespace Mono.CSharp {
                                
                        // from any class-type S to any interface-type T.
                        if (target_type.IsInterface) {
+                               if (target_type != TypeManager.iconvertible_type &&
+                                   expr_type.IsValueType && (expr is Constant) &&
+                                   !(expr is IntLiteral || expr is BoolLiteral ||
+                                     expr is FloatLiteral || expr is DoubleLiteral ||
+                                     expr is LongLiteral || expr is CharLiteral ||
+                                     expr is StringLiteral || expr is DecimalLiteral ||
+                                     expr is UIntLiteral || expr is ULongLiteral)) {
+                                       return false;
+                               }
+                               
                                if (TypeManager.ImplementsInterface (expr_type, target_type))
                                        return true;
                        }
@@ -245,9 +255,13 @@ namespace Mono.CSharp {
                                        return true;
                                
                        // from the null type to any reference-type.
-                       if (expr_type == TypeManager.null_type && !target_type.IsValueType && !TypeManager.IsEnumType (target_type))
-                               return true;
-                               
+                       if (expr_type == TypeManager.null_type){
+                               if (target_type.IsPointer)
+                                       return true;
+                       
+                               if (!target_type.IsValueType)
+                                       return true;
+                       }
                        return false;
                }
 
@@ -458,6 +472,7 @@ namespace Mono.CSharp {
                        if (expr_type == target_type)
                                return true;
 
+
                        // First numeric conversions 
 
                        if (expr_type == TypeManager.sbyte_type){
@@ -564,6 +579,20 @@ namespace Mono.CSharp {
                                        return true;
                        }       
                        
+                       if (expr.eclass == ExprClass.MethodGroup){
+                               if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
+                                       MethodGroupExpr mg = expr as MethodGroupExpr;
+                                       if (mg != null){
+                                               //
+                                               // This should not happen frequently, so we can create an object
+                                               // to test compatibility
+                                               //
+                                               Expression c = ImplicitDelegateCreation.Create (ec, mg, target_type, Location.Null);
+                                               return c != null;
+                                       }
+                               }
+                       }
+                       
                        if (ImplicitReferenceConversionExists (expr, target_type))
                                return true;
 
@@ -622,6 +651,13 @@ namespace Mono.CSharp {
                                        return true;
                        }
 
+                       //
+                       // If `expr_type' implements `target_type' (which is an iface)
+                       // see TryImplicitIntConversion
+                       // 
+                       if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
+                               return true;
+
                        if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
                                return true;
 
@@ -630,13 +666,12 @@ namespace Mono.CSharp {
                                        return false;
 
                                AnonymousMethod am = (AnonymousMethod) expr;
-                               int errors = Report.Errors;
 
                                Expression conv = am.Compatible (ec, target_type, true);
                                if (conv != null)
                                        return true;
                        }
-                       
+
                        return false;
                }
 
@@ -730,7 +765,7 @@ namespace Mono.CSharp {
                        //
                        Type source_type = source.Type;
                        foreach (MethodBase mb in me.Methods){
-                               ParameterData pd = Invocation.GetParameterData (mb);
+                               ParameterData pd = TypeManager.GetParameterData (mb);
                                Type param_type = pd.ParameterType (0);
 
                                if (param_type == source_type)
@@ -988,7 +1023,7 @@ namespace Mono.CSharp {
 
                        
                        foreach (MethodBase mb in union.Methods){
-                               ParameterData pd = Invocation.GetParameterData (mb);
+                               ParameterData pd = TypeManager.GetParameterData (mb);
                                MethodInfo mi = (MethodInfo) mb;
                                
                                if (pd.ParameterType (0) == most_specific_source &&
@@ -1156,7 +1191,7 @@ namespace Mono.CSharp {
                }
 
                /// <summary>
-               ///   Attemps to perform an implict constant conversion of the IntConstant
+               ///   Attempts to perform an implicit constant conversion of the IntConstant
                ///   into a different data type using casts (See Implicit Constant
                ///   Expression Conversions)
                /// </summary>
@@ -1199,7 +1234,7 @@ namespace Mono.CSharp {
                                //
                                // Possibly, we need to create a different 0 literal before passing
                                // to EnumConstant
-                               //n
+                               //
                                if (underlying == TypeManager.int64_type)
                                        e = new LongLiteral (0);
                                else if (underlying == TypeManager.uint64_type)
@@ -1207,11 +1242,26 @@ namespace Mono.CSharp {
 
                                return new EnumConstant (e, target_type);
                        }
+
+                       //
+                       // If `target_type' is an interface and the type of `ic' implements the interface
+                       // e.g. target_type is IComparable, IConvertible, IFormattable
+                       //
+                       if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
+                               return new BoxedCast (ic);
+
                        return null;
                }
 
                static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
                {
+                       if (source.Name == target.Name){
+                               Report.ExtraInformation (loc,
+                                        String.Format (
+                                               "The type {0} has two conflicting definitons, one comes from {0} and the other from {1}",
+                                               source.Assembly.FullName, target.Assembly.FullName));
+                                                        
+                       }
                        Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
                                      source == TypeManager.anonymous_method_type ?
                                      "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
@@ -1219,7 +1269,7 @@ namespace Mono.CSharp {
                }
 
                /// <summary>
-               ///   Attemptes to implicityly convert `target' into `type', using
+               ///   Attempts to implicitly convert `source' into `target_type', using
                ///   ImplicitConversion.  If there is no implicit conversion, then
                ///   an error is signaled
                /// </summary>
@@ -1232,10 +1282,15 @@ namespace Mono.CSharp {
                        if (e != null)
                                return e;
 
-                       if (source is DoubleLiteral && target_type == TypeManager.float_type){
-                               Report.Error (664, loc,
-                                             "Double literal cannot be implicitly converted to " +
-                                             "float type, use F suffix to create a float literal");
+                       if (source is DoubleLiteral) {
+                               if (target_type == TypeManager.float_type) {
+                                       Error_664 (loc, "float", "f");
+                                       return null;
+                               }
+                               if (target_type == TypeManager.decimal_type) {
+                                       Error_664 (loc, "decimal", "m");
+                                       return null;
+                               }
                        }
 
                        if (source is Constant){
@@ -1250,6 +1305,12 @@ namespace Mono.CSharp {
                        return null;
                }
 
+               static void Error_664 (Location loc, string type, string suffix) {
+                       Report.Error (664, loc,
+                               "Literal of type double cannot be implicitly converted to type '{0}'. Add suffix '{1}' to create a literal of this type",
+                               type, suffix);
+               }
+
                /// <summary>
                ///   Performs the explicit numeric conversions
                /// </summary>