Author: martin
Date: 2005-03-05 13:00:51 -0500 (Sat, 05 Mar 2005)
New Revision: 41467

Modified:
   trunk/mcs/gmcs/ChangeLog
   trunk/mcs/gmcs/convert.cs
   trunk/mcs/gmcs/expression.cs
   trunk/mcs/gmcs/generic.cs
Log:
2005-03-05  Martin Baulig  <[EMAIL PROTECTED]>

        * generic.cs (Nullable.Unwrap, Wrap): New protected classes.
        (Nullable.Lifted): New abstract class; rewrote the lifted conversions.
        (Nullable): Added support for lifted unary and binary operators.

        * expression.cs (Unary.DoResolve): Added support for nullable types.
        (Binary.DoResolve): Likewise.
        (Conditional.DoResolve): Likewise.



Modified: trunk/mcs/gmcs/ChangeLog
===================================================================
--- trunk/mcs/gmcs/ChangeLog    2005-03-05 12:09:43 UTC (rev 41466)
+++ trunk/mcs/gmcs/ChangeLog    2005-03-05 18:00:51 UTC (rev 41467)
@@ -1,3 +1,13 @@
+2005-03-05  Martin Baulig  <[EMAIL PROTECTED]>
+
+       * generic.cs (Nullable.Unwrap, Wrap): New protected classes.
+       (Nullable.Lifted): New abstract class; rewrote the lifted conversions.
+       (Nullable): Added support for lifted unary and binary operators.
+
+       * expression.cs (Unary.DoResolve): Added support for nullable types.
+       (Binary.DoResolve): Likewise.
+       (Conditional.DoResolve): Likewise.
+
 2005-03-02  Martin Baulig  <[EMAIL PROTECTED]>
 
        * decl.cs (DeclSpace.SetParameterInfo): Make this virtual.

Modified: trunk/mcs/gmcs/convert.cs
===================================================================
--- trunk/mcs/gmcs/convert.cs   2005-03-05 12:09:43 UTC (rev 41466)
+++ trunk/mcs/gmcs/convert.cs   2005-03-05 18:00:51 UTC (rev 41467)
@@ -1129,8 +1129,8 @@
                        MethodBase method = null;
 
                        if (TypeManager.IsNullableType (source_type) && 
TypeManager.IsNullableType (target))
-                               return Nullable.LiftedConversion.Create (
-                                       ec, source, target, true, 
look_for_explicit, loc);
+                               return new Nullable.LiftedConversion (
+                                       source, target, true, 
look_for_explicit, loc).Resolve (ec);
 
                        union = GetConversionOperators (ec, source_type, 
target, loc, look_for_explicit);
                        if (union == null)
@@ -1240,7 +1240,8 @@
                        }
 
                        if (TypeManager.IsNullableType (expr_type) && 
TypeManager.IsNullableType (target_type))
-                               return Nullable.LiftedConversion.Create (ec, 
expr, target_type, false, false, loc);
+                               return new Nullable.LiftedConversion (
+                                       expr, target_type, false, false, 
loc).Resolve (ec);
 
                        if (expr.eclass == ExprClass.MethodGroup){
                                if (!TypeManager.IsDelegateType (target_type)){
@@ -1933,7 +1934,8 @@
                                return ne;
 
                        if (TypeManager.IsNullableType (expr.Type) && 
TypeManager.IsNullableType (target_type))
-                               return Nullable.LiftedConversion.Create (ec, 
expr, target_type, false, true, loc);
+                               return new Nullable.LiftedConversion (
+                                       expr, target_type, false, true, 
loc).Resolve (ec);
 
                        ne = ExplicitNumericConversion (ec, expr, target_type, 
loc);
                        if (ne != null)
@@ -2043,7 +2045,8 @@
                                return ne;
 
                        if (TypeManager.IsNullableType (expr.Type) && 
TypeManager.IsNullableType (target_type))
-                               return Nullable.LiftedConversion.Create (ec, 
expr, target_type, false, true, l);
+                               return new Nullable.LiftedConversion (
+                                       expr, target_type, false, true, 
l).Resolve (ec);
 
                        ne = ExplicitNumericConversion (ec, expr, target_type, 
l);
                        if (ne != null)

Modified: trunk/mcs/gmcs/expression.cs
===================================================================
--- trunk/mcs/gmcs/expression.cs        2005-03-05 12:09:43 UTC (rev 41466)
+++ trunk/mcs/gmcs/expression.cs        2005-03-05 18:00:51 UTC (rev 41467)
@@ -581,6 +581,9 @@
                        if (Expr == null)
                                return null;
 
+                       if (TypeManager.IsNullableType (Expr.Type))
+                               return new Nullable.LiftedUnaryOperator (Oper, 
Expr, loc).Resolve (ec);
+
                        eclass = ExprClass.Value;
                        return ResolveOperator (ec);
                }
@@ -2472,11 +2475,6 @@
                                                else
                                                        method = 
TypeManager.delegate_remove_delegate_delegate;
 
-                                               Report.Debug (64, "BINARY 
DELEGATE", l, r, l.GetType (), r.GetType (),
-                                                             left, right, l == 
r, l.Equals (r),
-                                                             
TypeManager.IsEqual (l, r), TypeManager.IsEqual (r, l),
-                                                             
TypeManager.IsEqualGenericInstance (l, r));
-
                                                if (!TypeManager.IsEqual (l, 
r)) {
                                                        
Error_OperatorCannotBeApplied ();
                                                        return null;
@@ -2748,6 +2746,9 @@
                                                return e;
                        }
 
+                       if (TypeManager.IsNullableType (left.Type) || 
TypeManager.IsNullableType (right.Type))
+                               return new Nullable.LiftedBinaryOperator (oper, 
left, right, loc).Resolve (ec);
+
                        return ResolveOperator (ec);
                }
 
@@ -3507,6 +3508,9 @@
 
                        if (expr == null)
                                return null;
+
+                       if (TypeManager.IsNullableType (expr.Type))
+                               return new Nullable.LiftedConditional (expr, 
trueExpr, falseExpr, loc).Resolve (ec);
                        
                        if (expr.Type != TypeManager.bool_type){
                                expr = Expression.ResolveBoolean (

Modified: trunk/mcs/gmcs/generic.cs
===================================================================
--- trunk/mcs/gmcs/generic.cs   2005-03-05 12:09:43 UTC (rev 41466)
+++ trunk/mcs/gmcs/generic.cs   2005-03-05 18:00:51 UTC (rev 41467)
@@ -1598,6 +1598,10 @@
                        eclass = ExprClass.Type;
                }
 
+               public NullableType (Type type, Location loc)
+                       : this (new TypeExpression (type, loc), loc)
+               { }
+
                public override string Name {
                        get { return underlying.ToString () + "?"; }
                }
@@ -2310,6 +2314,77 @@
                        }
                }
 
+               protected class Unwrap : Expression
+               {
+                       Expression expr;
+                       NullableInfo info;
+
+                       public Unwrap (Expression expr, Location loc)
+                       {
+                               this.expr = expr;
+                               this.loc = loc;
+                       }
+
+                       public override Expression DoResolve (EmitContext ec)
+                       {
+                               expr = expr.Resolve (ec);
+                               if (expr == null)
+                                       return null;
+
+                               info = new NullableInfo (expr.Type);
+                               type = info.UnderlyingType;
+                               eclass = expr.eclass;
+                               return this;
+                       }
+
+                       public override void Emit (EmitContext ec)
+                       {
+                               ((IMemoryLocation) expr).AddressOf (ec, 
AddressOp.LoadStore);
+                               ec.ig.EmitCall (OpCodes.Call, info.Value, null);
+                       }
+
+                       public void EmitCheck (EmitContext ec)
+                       {
+                               ((IMemoryLocation) expr).AddressOf (ec, 
AddressOp.LoadStore);
+                               ec.ig.EmitCall (OpCodes.Call, info.HasValue, 
null);
+                       }
+               }
+
+               protected class Wrap : Expression
+               {
+                       Expression expr;
+                       NullableInfo info;
+
+                       public Wrap (Expression expr, Location loc)
+                       {
+                               this.expr = expr;
+                               this.loc = loc;
+                       }
+
+                       public override Expression DoResolve (EmitContext ec)
+                       {
+                               expr = expr.Resolve (ec);
+                               if (expr == null)
+                                       return null;
+
+                               TypeExpr target_type = new NullableType 
(expr.Type, loc);
+                               target_type = target_type.ResolveAsTypeTerminal 
(ec);
+                               if (target_type == null)
+                                       return null;
+
+                               type = target_type.Type;
+                               info = new NullableInfo (type);
+                               eclass = ExprClass.Value;
+                               return this;
+                       }
+
+                       public override void Emit (EmitContext ec)
+                       {
+                               expr.Emit (ec);
+                               ec.ig.Emit (OpCodes.Newobj, info.Constructor);
+                       }
+               }
+
                public class NullLiteral : Expression, IMemoryLocation {
                        public NullLiteral (Type target_type, Location loc)
                        {
@@ -2343,88 +2418,206 @@
                        }
                }
 
-               public class LiftedConversion : Expression
+               public abstract class Lifted : Expression
                {
-                       Expression source;
-                       NullableInfo source_info, target_info;
-                       Expression conversion;
+                       Expression expr, underlying, wrap, null_value;
+                       Unwrap unwrap;
 
-                       protected LiftedConversion (Expression source, Type 
target_type,
-                                                   NullableInfo source_info, 
NullableInfo target_info,
-                                                   Expression conversion, 
Location loc)
+                       protected Lifted (Expression expr, Location loc)
                        {
-                               this.source = source;
-                               this.type = target_type;
-                               this.source_info = source_info;
-                               this.target_info = target_info;
-                               this.conversion = conversion;
+                               this.expr = expr;
                                this.loc = loc;
-
-                               eclass = source.eclass;
                        }
 
-                       public static Expression Create (EmitContext ec, 
Expression source, Type target_type,
-                                                        bool is_user, bool 
is_explicit, Location loc)
+                       public override Expression DoResolve (EmitContext ec)
                        {
-                               NullableInfo source_info = new NullableInfo 
(source.Type);
-                               NullableInfo target_info = new NullableInfo 
(target_type);
+                               expr = expr.Resolve (ec);
+                               if (expr == null)
+                                       return null;
 
-                               source = source.Resolve (ec);
-                               if (source == null)
+                               unwrap = (Unwrap) new Unwrap (expr, 
loc).Resolve (ec);
+                               if (unwrap == null)
                                        return null;
 
-                               Expression conversion;
-                               if (is_user) {
-                                       conversion = 
Convert.UserDefinedConversion (
-                                               ec, new EmptyExpression 
(source_info.UnderlyingType),
-                                               target_info.UnderlyingType, 
loc, is_explicit);
+                               underlying = ResolveUnderlying (unwrap, ec);
+                               if (underlying == null)
+                                       return null;
+
+                               wrap = new Wrap (underlying, loc).Resolve (ec);
+                               if (wrap == null)
+                                       return null;
+
+                               null_value = new NullLiteral (wrap.Type, 
loc).Resolve (ec);
+                               if (null_value == null)
+                                       return null;
+
+                               type = wrap.Type;
+                               eclass = ExprClass.Value;
+                               return this;
+                       }
+
+                       protected abstract Expression ResolveUnderlying 
(Expression unwrap, EmitContext ec);
+
+                       public override void Emit (EmitContext ec)
+                       {
+                               ILGenerator ig = ec.ig;
+                               Label is_null_label = ig.DefineLabel ();
+                               Label end_label = ig.DefineLabel ();
+
+                               unwrap.EmitCheck (ec);
+                               ig.Emit (OpCodes.Brfalse, is_null_label);
+
+                               wrap.Emit (ec);
+                               ig.Emit (OpCodes.Br, end_label);
+
+                               ig.MarkLabel (is_null_label);
+                               null_value.Emit (ec);
+
+                               ig.MarkLabel (end_label);
+                       }
+               }
+
+               public class LiftedConversion : Lifted
+               {
+                       public readonly bool IsUser;
+                       public readonly bool IsExplicit;
+                       public readonly Type TargetType;
+
+                       public LiftedConversion (Expression expr, Type 
target_type, bool is_user,
+                                                bool is_explicit, Location loc)
+                               : base (expr, loc)
+                       {
+                               this.IsUser = is_user;
+                               this.IsExplicit = is_explicit;
+                               this.TargetType = target_type;
+                       }
+
+                       protected override Expression ResolveUnderlying 
(Expression unwrap, EmitContext ec)
+                       {
+                               if (IsUser) {
+                                       return Convert.UserDefinedConversion (
+                                               ec, unwrap, TargetType, loc, 
IsExplicit);
                                } else {
-                                       if (is_explicit)
-                                               conversion = 
Convert.ExplicitConversion (
-                                                       ec, new EmptyExpression 
(source_info.UnderlyingType),
-                                                       
target_info.UnderlyingType, loc);
+                                       if (IsExplicit)
+                                               return 
Convert.ExplicitConversion (ec, unwrap, TargetType, loc);
                                        else
-                                               conversion = 
Convert.ImplicitConversion (
-                                                       ec, new EmptyExpression 
(source_info.UnderlyingType),
-                                                       
target_info.UnderlyingType, loc);
+                                               return 
Convert.ImplicitConversion (ec, unwrap, TargetType, loc);
                                }
+                       }
+               }
 
-                               if (conversion == null)
-                                       return null;
+               public class LiftedUnaryOperator : Lifted
+               {
+                       public readonly Unary.Operator Oper;
 
-                               return new LiftedConversion (
-                                       source, target_type, source_info, 
target_info, conversion, loc);
+                       public LiftedUnaryOperator (Unary.Operator op, 
Expression expr, Location loc)
+                               : base (expr, loc)
+                       {
+                               this.Oper = op;
                        }
 
+                       protected override Expression ResolveUnderlying 
(Expression unwrap, EmitContext ec)
+                       {
+                               return new Unary (Oper, unwrap, loc);
+                       }
+               }
+
+               public class LiftedConditional : Lifted
+               {
+                       Expression expr, true_expr, false_expr;
+                       Unwrap unwrap;
+
+                       public LiftedConditional (Expression expr, Expression 
true_expr, Expression false_expr,
+                                                 Location loc)
+                               : base (expr, loc)
+                       {
+                               this.true_expr = true_expr;
+                               this.false_expr = false_expr;
+                       }
+
+                       protected override Expression ResolveUnderlying 
(Expression unwrap, EmitContext ec)
+                       {
+                               return new Conditional (unwrap, true_expr, 
false_expr, loc);
+                       }
+               }
+
+               public class LiftedBinaryOperator : Expression
+               {
+                       public readonly Binary.Operator Oper;
+
+                       Expression left, right, underlying, null_value;
+                       Unwrap left_unwrap, right_unwrap;
+
+                       public LiftedBinaryOperator (Binary.Operator op, 
Expression left, Expression right,
+                                                    Location loc)
+                       {
+                               this.Oper = op;
+                               this.left = left;
+                               this.right = right;
+                               this.loc = loc;
+                       }
+
                        public override Expression DoResolve (EmitContext ec)
                        {
+                               left = left.Resolve (ec);
+                               if (left == null)
+                                       return null;
+
+                               right = right.Resolve (ec);
+                               if (right == null)
+                                       return null;
+
+                               if (TypeManager.IsNullableType (left.Type)) {
+                                       left_unwrap = new Unwrap (left, loc);
+                                       left = left_unwrap.Resolve (ec);
+                                       if (left == null)
+                                               return null;
+                               }
+
+                               if (TypeManager.IsNullableType (right.Type)) {
+                                       right_unwrap = new Unwrap (right, loc);
+                                       right = right_unwrap.Resolve (ec);
+                                       if (right == null)
+                                               return null;
+                               }
+
+                               underlying = new Wrap (new Binary (Oper, left, 
right, loc), loc);
+                               underlying = underlying.Resolve (ec);
+                               if (underlying == null)
+                                       return null;
+
+                               null_value = new NullLiteral (underlying.Type, 
loc).Resolve (ec);
+                               if (null_value == null)
+                                       return null;
+
+                               type = underlying.Type;
+                               eclass = ExprClass.Value;
                                return this;
                        }
 
                        public override void Emit (EmitContext ec)
                        {
                                ILGenerator ig = ec.ig;
-                               Label has_value_label = ig.DefineLabel ();
+
+                               Label is_null_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);
+                               if (left_unwrap != null) {
+                                       left_unwrap.EmitCheck (ec);
+                                       ig.Emit (OpCodes.Brfalse, 
is_null_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);
+                               if (right_unwrap != null) {
+                                       right_unwrap.EmitCheck (ec);
+                                       ig.Emit (OpCodes.Brfalse, 
is_null_label);
+                               }
+
+                               underlying.Emit (ec);
                                ig.Emit (OpCodes.Br, end_label);
 
-                               ig.MarkLabel (has_value_label);
+                               ig.MarkLabel (is_null_label);
+                               null_value.Emit (ec);
 
-                               ((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