Author: martin
Date: 2005-02-28 00:36:42 -0500 (Mon, 28 Feb 2005)
New Revision: 41274

Modified:
   trunk/mcs/gmcs/ChangeLog
   trunk/mcs/gmcs/convert.cs
   trunk/mcs/gmcs/cs-parser.jay
   trunk/mcs/gmcs/generic.cs
Log:
2005-02-28  Martin Baulig  <[EMAIL PROTECTED]>

        * generic.cs (TypeManager.IsNullableType): New static method.
        (Nullable): New abstract class.
        (Nullable.NullLiteral): New public class.
        (Nullable.LiftedConversion): New public class.

        * cs-parser.jay (non_expression_type): Changed `builtin_types' to
        `builtin_types opt_nullable'.

        * convert.cs
        (Convert.ImplicitConversionStandard): Added nullable conversions.
        (Convert.ExplicitConversionStandard): Likewise.
        (Convert.ExplicitConversion): Likewise.



Modified: trunk/mcs/gmcs/ChangeLog
===================================================================
--- trunk/mcs/gmcs/ChangeLog    2005-02-28 05:13:56 UTC (rev 41273)
+++ trunk/mcs/gmcs/ChangeLog    2005-02-28 05:36:42 UTC (rev 41274)
@@ -1,3 +1,18 @@
+2005-02-28  Martin Baulig  <[EMAIL PROTECTED]>
+
+       * generic.cs (TypeManager.IsNullableType): New static method.
+       (Nullable): New abstract class.
+       (Nullable.NullLiteral): New public class.
+       (Nullable.LiftedConversion): New public class.
+
+       * cs-parser.jay (non_expression_type): Changed `builtin_types' to
+       `builtin_types opt_nullable'.
+
+       * convert.cs
+       (Convert.ImplicitConversionStandard): Added nullable conversions.
+       (Convert.ExplicitConversionStandard): Likewise.
+       (Convert.ExplicitConversion): Likewise.
+
 2005-02-26  Martin Baulig  <[EMAIL PROTECTED]>
 
        * expression.cs (ComposedCast.DoResolveAsTypeStep): Allow `dim' to

Modified: trunk/mcs/gmcs/convert.cs
===================================================================
--- trunk/mcs/gmcs/convert.cs   2005-02-28 05:13:56 UTC (rev 41273)
+++ trunk/mcs/gmcs/convert.cs   2005-02-28 05:36:42 UTC (rev 41274)
@@ -548,9 +548,14 @@
                /// </summary>
                public static bool ImplicitConversionExists (EmitContext ec, 
Expression expr, Type target_type)
                {
-                       if ((expr is NullLiteral) && 
target_type.IsGenericParameter)
-                               return TypeParameter_to_Null (target_type);
+                       if (expr is NullLiteral) {
+                               if (target_type.IsGenericParameter)
+                                       return TypeParameter_to_Null 
(target_type);
 
+                               if (TypeManager.IsNullableType (target_type))
+                                       return true;
+                       }
+
                        if (ImplicitStandardConversionExists (ec, expr, 
target_type))
                                return true;
 
@@ -776,6 +781,9 @@
                        if (target_type == TypeManager.void_ptr_type && 
expr_type.IsPointer)
                                return true;
 
+                       if (TypeManager.IsNullableType (expr_type) && 
TypeManager.IsNullableType (target_type))
+                               return true;
+
                        if (expr_type == TypeManager.anonymous_method_type){
                                if (!TypeManager.IsDelegateType (target_type))
                                        return false;
@@ -1219,9 +1227,17 @@
                        Type expr_type = expr.Type;
                        Expression e;
 
-                       if ((expr is NullLiteral) && 
target_type.IsGenericParameter)
-                               return TypeParameter_to_Null (expr, 
target_type, loc);
+                       if (expr is NullLiteral) {
+                               if (target_type.IsGenericParameter)
+                                       return TypeParameter_to_Null (expr, 
target_type, loc);
 
+                               if (TypeManager.IsNullableType (target_type))
+                                       return new Nullable.NullLiteral 
(target_type, loc);
+                       }
+
+                       if (TypeManager.IsNullableType (expr_type) && 
TypeManager.IsNullableType (target_type))
+                               return Nullable.LiftedConversion.Create (ec, 
expr, target_type, false, loc);
+
                        if (expr.eclass == ExprClass.MethodGroup){
                                if (!TypeManager.IsDelegateType (target_type)){
                                        return null;
@@ -1905,6 +1921,9 @@
                        if (ne != null)
                                return ne;
 
+                       if (TypeManager.IsNullableType (expr.Type) && 
TypeManager.IsNullableType (target_type))
+                               return Nullable.LiftedConversion.Create (ec, 
expr, target_type, true, loc);
+
                        ne = ExplicitNumericConversion (ec, expr, target_type, 
loc);
                        if (ne != null)
                                return ne;
@@ -2012,6 +2031,9 @@
                        if (ne != null)
                                return ne;
 
+                       if (TypeManager.IsNullableType (expr.Type) && 
TypeManager.IsNullableType (target_type))
+                               return Nullable.LiftedConversion.Create (ec, 
expr, target_type, true, l);
+
                        ne = ExplicitNumericConversion (ec, expr, target_type, 
l);
                        if (ne != null)
                                return ne;

Modified: trunk/mcs/gmcs/cs-parser.jay
===================================================================
--- trunk/mcs/gmcs/cs-parser.jay        2005-02-28 05:13:56 UTC (rev 41273)
+++ trunk/mcs/gmcs/cs-parser.jay        2005-02-28 05:36:42 UTC (rev 41274)
@@ -2521,7 +2521,11 @@
        ;
 
 non_expression_type
-       : builtin_types 
+       : builtin_types opt_nullable
+         {
+               if ((bool) $2)
+                       $$ = new NullableType ((Expression) $1, lexer.Location);
+         }
        | non_expression_type rank_specifier
          {
                $$ = new ComposedCast ((Expression) $1, (string) $2, 
lexer.Location);

Modified: trunk/mcs/gmcs/generic.cs
===================================================================
--- trunk/mcs/gmcs/generic.cs   2005-02-28 05:13:56 UTC (rev 41273)
+++ trunk/mcs/gmcs/generic.cs   2005-02-28 05:36:42 UTC (rev 41274)
@@ -2217,6 +2217,15 @@
                        method = method.BindGenericParameters (infered_types);
                        return true;
                }
+
+               public static bool IsNullableType (Type t)
+               {
+                       if (!t.IsGenericInstance)
+                               return false;
+
+                       Type gt = t.GetGenericTypeDefinition ();
+                       return gt == generic_nullable_type;
+               }
        }
 
        public class NullCoalescingOperator : Expression
@@ -2241,4 +2250,139 @@
                {
                }
        }
+
+       public abstract class Nullable
+       {
+               protected sealed class NullableInfo
+               {
+                       public readonly Type Type;
+                       public readonly Type UnderlyingType;
+                       public readonly MethodInfo HasValue;
+                       public readonly MethodInfo Value;
+                       public readonly ConstructorInfo Constructor;
+
+                       public NullableInfo (Type type)
+                       {
+                               Type = type;
+                               UnderlyingType = TypeManager.GetTypeArguments 
(type) [0];
+
+                               PropertyInfo has_value_pi = type.GetProperty 
("HasValue");
+                               PropertyInfo value_pi = type.GetProperty 
("Value");
+
+                               HasValue = has_value_pi.GetGetMethod (false);
+                               Value = value_pi.GetGetMethod (false);
+                               Constructor = type.GetConstructor (new Type[] { 
UnderlyingType });
+                       }
+               }
+
+               public class NullLiteral : Expression, IMemoryLocation {
+                       public NullLiteral (Type target_type, Location loc)
+                       {
+                               this.type = target_type;
+                               this.loc = loc;
+
+                               eclass = ExprClass.Value;
+                       }
+               
+                       public override Expression DoResolve (EmitContext ec)
+                       {
+                               return this;
+                       }
+
+                       public override void Emit (EmitContext ec)
+                       {
+                               LocalTemporary value_target = new 
LocalTemporary (ec, type);
+
+                               value_target.AddressOf (ec, AddressOp.Store);
+                               ec.ig.Emit (OpCodes.Initobj, type);
+                               value_target.Emit (ec);
+                       }
+
+                       public void AddressOf (EmitContext ec, AddressOp Mode)
+                       {
+                               LocalTemporary value_target = new 
LocalTemporary (ec, type);
+                                       
+                               value_target.AddressOf (ec, AddressOp.Store);
+                               ec.ig.Emit (OpCodes.Initobj, type);
+                               ((IMemoryLocation) value_target).AddressOf (ec, 
Mode);
+                       }
+               }
+
+               public class LiftedConversion : Expression
+               {
+                       Expression source;
+                       bool is_explicit;
+                       NullableInfo source_info, target_info;
+                       Expression conversion;
+
+                       protected LiftedConversion (Expression source, Type 
target_type, bool is_explicit,
+                                                   Location loc)
+                       {
+                               this.source = source;
+                               this.type = target_type;
+                               this.is_explicit = is_explicit;
+                               this.loc = loc;
+
+                               eclass = source.eclass;
+                       }
+
+                       public static Expression Create (EmitContext ec, 
Expression source, Type target_type,
+                                                        bool is_explicit, 
Location loc)
+                       {
+                               Expression expr = new LiftedConversion (source, 
target_type, is_explicit, loc);
+                               return expr.Resolve (ec);
+                       }
+
+                       public override Expression DoResolve (EmitContext ec)
+                       {
+                               source_info = new NullableInfo (source.Type);
+                               target_info = new NullableInfo (type);
+
+                               source = source.Resolve (ec);
+                               if (source == null)
+                                       return null;
+
+                               if (is_explicit)
+                                       conversion = 
Convert.ExplicitConversionStandard (
+                                               ec, new EmptyExpression 
(source_info.UnderlyingType),
+                                               target_info.UnderlyingType, 
loc);
+                               else
+                                       conversion = 
Convert.ImplicitConversionStandard (
+                                               ec, new EmptyExpression 
(source_info.UnderlyingType),
+                                               target_info.UnderlyingType, 
loc);
+
+                               if (conversion == null)
+                                       return null;
+
+                               return this;
+                       }
+
+                       public override void Emit (EmitContext ec)
+                       {
+                               ILGenerator ig = ec.ig;
+                               Label has_value_label = ig.DefineLabel ();
+                               Label end_label = ig.DefineLabel ();
+
+                               ((IMemoryLocation) source).AddressOf (ec, 
AddressOp.LoadStore);
+                               ig.EmitCall (OpCodes.Call, 
source_info.HasValue, null);
+                               ig.Emit (OpCodes.Brtrue, has_value_label);
+
+                               LocalTemporary value_target = new 
LocalTemporary (ec, type);
+                               value_target.AddressOf (ec, AddressOp.Store);
+                               ig.Emit (OpCodes.Initobj, type);
+                               value_target.Emit (ec);
+                               value_target.Release (ec);
+                               ig.Emit (OpCodes.Br, end_label);
+
+                               ig.MarkLabel (has_value_label);
+
+                               ((IMemoryLocation) source).AddressOf (ec, 
AddressOp.LoadStore);
+                               ig.EmitCall (OpCodes.Call, source_info.Value, 
null);
+                               conversion.Emit (ec);
+                               ig.Emit (OpCodes.Newobj, 
target_info.Constructor);
+
+                               ig.MarkLabel (end_label);
+                       }
+               }
+       }
 }

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

Reply via email to