Author: martin
Date: 2006-03-23 12:15:52 -0500 (Thu, 23 Mar 2006)
New Revision: 58367

Added:
   trunk/mcs/tests/gtest-263.cs
Modified:
   trunk/mcs/gmcs/ChangeLog
   trunk/mcs/gmcs/convert.cs
Log:
2006-03-23  Martin Baulig  <[EMAIL PROTECTED]>

        * convert.cs
        (Convert.ExplicitTypeParameterConversion): New method; implement
        explicit type parameter conversions.



Modified: trunk/mcs/gmcs/ChangeLog
===================================================================
--- trunk/mcs/gmcs/ChangeLog    2006-03-23 17:14:41 UTC (rev 58366)
+++ trunk/mcs/gmcs/ChangeLog    2006-03-23 17:15:52 UTC (rev 58367)
@@ -1,5 +1,11 @@
 2006-03-23  Martin Baulig  <[EMAIL PROTECTED]>
 
+       * convert.cs
+       (Convert.ExplicitTypeParameterConversion): New method; implement
+       explicit type parameter conversions.
+
+2006-03-23  Martin Baulig  <[EMAIL PROTECTED]>
+
        * convert.cs (Convert.ImplicitTypeParameterConversion): Do not
        blindly allow all conversions if we do not have any constraints.
 

Modified: trunk/mcs/gmcs/convert.cs
===================================================================
--- trunk/mcs/gmcs/convert.cs   2006-03-23 17:14:41 UTC (rev 58366)
+++ trunk/mcs/gmcs/convert.cs   2006-03-23 17:15:52 UTC (rev 58367)
@@ -116,6 +116,52 @@
                        return null;
                }
 
+               static bool ExplicitTypeParameterConversionExists (Type 
source_type, Type target_type)
+               {
+                       if (target_type.IsInterface)
+                               return true;
+
+                       if (source_type.IsGenericParameter) {
+                               GenericConstraints gc = 
TypeManager.GetTypeParameterConstraints (target_type);
+                               if (gc == null)
+                                       return false;
+
+                               foreach (Type iface in gc.InterfaceConstraints) 
{
+                                       if (!iface.IsGenericParameter)
+                                               continue;
+
+                                       if (TypeManager.IsSubclassOf 
(source_type, iface))
+                                               return true;
+                               }
+                       }
+
+                       return false;
+               }
+
+               static Expression ExplicitTypeParameterConversion (Expression 
source, Type target_type)
+               {
+                       Type source_type = source.Type;
+
+                       if (target_type.IsInterface)
+                               return new ClassCast (source, target_type);
+
+                       if (source_type.IsGenericParameter) {
+                               GenericConstraints gc = 
TypeManager.GetTypeParameterConstraints (target_type);
+                               if (gc == null)
+                                       return null;
+
+                               foreach (Type iface in gc.InterfaceConstraints) 
{
+                                       if (!iface.IsGenericParameter)
+                                               continue;
+
+                                       if (TypeManager.IsSubclassOf 
(source_type, iface))
+                                               return new ClassCast (source, 
target_type);
+                               }
+                       }
+
+                       return null;
+               }
+
                static EmptyExpression MyEmptyExpr;
                static public Expression ImplicitReferenceConversion 
(Expression expr, Type target_type)
                {
@@ -1613,7 +1659,7 @@
                        // From generic parameter to any type
                        //
                        if (source_type.IsGenericParameter)
-                               return true;
+                               return ExplicitTypeParameterConversionExists 
(source_type, target_type);
 
                        //
                        // From object to a generic parameter
@@ -1720,6 +1766,13 @@
                                return new UnboxCast (source, target_type);
 
                        //
+                       // Explicit type parameter conversion.
+                       //
+
+                       if (source_type.IsGenericParameter)
+                               return ExplicitTypeParameterConversion (source, 
target_type);
+
+                       //
                        // From object to any reference type
                        //
                        if (source_type == TypeManager.object_type && 
!target_is_value_type)

Added: trunk/mcs/tests/gtest-263.cs
===================================================================
--- trunk/mcs/tests/gtest-263.cs        2006-03-23 17:14:41 UTC (rev 58366)
+++ trunk/mcs/tests/gtest-263.cs        2006-03-23 17:15:52 UTC (rev 58367)
@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+
+class Foo<S>
+{
+       public ICloneable Test (S t)
+       {
+               return (ICloneable) t;
+       }
+}
+
+public static class ConvertHelper
+{
+       public static IEnumerator<T> Test<S,T> (S s)
+               where T : S
+       {
+               yield return (T) s;
+       }
+
+       static void Main ()
+       { }
+}


Property changes on: trunk/mcs/tests/gtest-263.cs
___________________________________________________________________
Name: svn:executable
   + *

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to