Use TypeManager.GetInterfaces().
[mono.git] / mcs / gmcs / convert.cs
index f70eab18c2f8eab0ca46b1f432519abb4047ceca..637c4150f718621bebbb5137db0efde148a3150d 100644 (file)
@@ -66,7 +66,7 @@ namespace Mono.CSharp {
                                // System.Enum is not a value type, it is a class, so we need
                                // a boxing conversion
                                //
-                               if (expr_type.IsEnum)
+                               if (expr_type.IsEnum || expr_type.IsGenericParameter)
                                        return new BoxedCast (expr);
 
                                return new EmptyCast (expr, target_type);
@@ -89,7 +89,9 @@ namespace Mono.CSharp {
                                // from any class-type S to any interface-type T.
                                if (target_type.IsInterface) {
                                        if (TypeManager.ImplementsInterface (expr_type, target_type)){
-                                               if (expr_type.IsClass)
+                                               if (expr_type.IsGenericParameter)
+                                                       return new BoxedCast (expr, target_type);
+                                               else if (expr_type.IsClass)
                                                        return new EmptyCast (expr, target_type);
                                                else if (TypeManager.IsValueType (expr_type) ||
                                                         TypeManager.IsEnumType (expr_type))
@@ -133,14 +135,14 @@ namespace Mono.CSharp {
                                
                                // from any delegate type to System.Delegate
                                if ((expr_type == TypeManager.delegate_type || 
-                                    expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
+                                    TypeManager.IsDelegateType (expr_type)) &&
                                    target_type == TypeManager.delegate_type)
                                        return new EmptyCast (expr, target_type);
                                        
                                // from any array-type or delegate type into System.ICloneable.
                                if (expr_type.IsArray ||
                                    expr_type == TypeManager.delegate_type ||
-                                   expr_type.IsSubclassOf (TypeManager.delegate_type))
+                                   TypeManager.IsDelegateType (expr_type))
                                        if (target_type == TypeManager.icloneable_type)
                                                return new EmptyCast (expr, target_type);
 
@@ -170,7 +172,7 @@ namespace Mono.CSharp {
                                if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
                                    expr_type.IsInterface || expr_type == TypeManager.enum_type)
                                        return true;
-                       } else if (TypeManager.IsSubclassOf (expr_type, target_type)) 
+                       } else if (TypeManager.IsSubclassOf (expr_type, target_type))
                                return true;
                        else {
                                // Please remember that all code below actually comes
@@ -212,7 +214,7 @@ namespace Mono.CSharp {
                                
                                // from any delegate type to System.Delegate
                                if ((expr_type == TypeManager.delegate_type ||
-                                    expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
+                                    TypeManager.IsDelegateType (expr_type)) &&
                                    target_type == TypeManager.delegate_type)
                                        if (target_type.IsAssignableFrom (expr_type))
                                                return true;
@@ -220,7 +222,7 @@ namespace Mono.CSharp {
                                // from any array-type or delegate type into System.ICloneable.
                                if (expr_type.IsArray ||
                                    expr_type == TypeManager.delegate_type ||
-                                   expr_type.IsSubclassOf (TypeManager.delegate_type))
+                                   TypeManager.IsDelegateType (expr_type))
                                        if (target_type == TypeManager.icloneable_type)
                                                return true;
                                
@@ -1413,11 +1415,18 @@ namespace Mono.CSharp {
                /// </summary>
                public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
                {
+                       bool target_is_type_param = target_type.IsGenericParameter;
                        bool target_is_value_type = target_type.IsValueType;
                        
                        if (source_type == target_type)
                                return true;
-                       
+
+                       //
+                       // From object to a generic parameter
+                       //
+                       if (source_type == TypeManager.object_type && target_is_type_param)
+                               return true;
+
                        //
                        // From object to any reference type
                        //
@@ -1486,7 +1495,7 @@ namespace Mono.CSharp {
                        // From System delegate to any delegate-type
                        //
                        if (source_type == TypeManager.delegate_type &&
-                           target_type.IsSubclassOf (TypeManager.delegate_type))
+                           TypeManager.IsDelegateType (target_type))
                                return true;
 
                        //
@@ -1506,8 +1515,15 @@ namespace Mono.CSharp {
                static Expression ExplicitReferenceConversion (Expression source, Type target_type)
                {
                        Type source_type = source.Type;
+                       bool target_is_type_param = target_type.IsGenericParameter;
                        bool target_is_value_type = target_type.IsValueType;
 
+                       //
+                       // From object to a generic parameter
+                       //
+                       if (source_type == TypeManager.object_type && target_is_type_param)
+                               return new UnboxCast (source, target_type);
+
                        //
                        // From object to any reference type
                        //
@@ -1537,7 +1553,7 @@ namespace Mono.CSharp {
                                else
                                        return new ClassCast (source, target_type);
                        }
-                           
+
                        //
                        // From any class type S to any interface T, provides S is not sealed
                        // and provided S does not implement T.
@@ -1596,7 +1612,7 @@ namespace Mono.CSharp {
                        // From System delegate to any delegate-type
                        //
                        if (source_type == TypeManager.delegate_type &&
-                           target_type.IsSubclassOf (TypeManager.delegate_type))
+                           TypeManager.IsDelegateType (target_type))
                                return new ClassCast (source, target_type);
 
                        //