2008-06-26 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / nullable.cs
index 81e2d28ef1eeecac7e4f6963882e9e2401de8a15..73f711a7e50394d00734d8c98ec2cc806d6582ce 100644 (file)
@@ -87,7 +87,6 @@ namespace Mono.CSharp.Nullable
                NullableInfo info;
 
                LocalTemporary temp;
-               bool has_temp;
 
                protected Unwrap (Expression expr)
                {
@@ -110,8 +109,6 @@ namespace Mono.CSharp.Nullable
                        if (expr == null)
                                return null;
 
-                       temp = new LocalTemporary (expr.Type);
-
                        info = new NullableInfo (expr.Type);
                        type = info.UnderlyingType;
                        eclass = expr.eclass;
@@ -125,18 +122,21 @@ namespace Mono.CSharp.Nullable
 
                public override void Emit (EmitContext ec)
                {
+                       Store (ec);
                        AddressOf (ec, AddressOp.LoadStore);
                        ec.ig.EmitCall (OpCodes.Call, info.Value, null);
                }
 
                public void EmitCheck (EmitContext ec)
                {
+                       Store (ec);
                        AddressOf (ec, AddressOp.LoadStore);
                        ec.ig.EmitCall (OpCodes.Call, info.HasValue, null);
                }
 
                public void EmitGetValueOrDefault (EmitContext ec)
                {
+                       Store (ec);
                        AddressOf (ec, AddressOp.LoadStore);
                        ec.ig.EmitCall (OpCodes.Call, info.GetValueOrDefault, null);
                }
@@ -164,43 +164,50 @@ namespace Mono.CSharp.Nullable
                        }
                }
 
-               public void Store (EmitContext ec)
+               void Store (EmitContext ec)
                {
-                       create_temp (ec);
-               }
+                       if (expr is VariableReference)
+                               return;
 
-               void create_temp (EmitContext ec)
-               {
-                       if ((temp != null) && !has_temp) {
-                               expr.Emit (ec);
-                               temp.Store (ec);
-                               has_temp = true;
-                       }
+                       if (temp != null)
+                               return;
+
+                       expr.Emit (ec);
+                       LocalVariable.Store (ec);
                }
 
-               public void LoadTemporary (EmitContext ec)
+               public void Load (EmitContext ec)
                {
-                       temp.Emit (ec);
+                       if (expr is VariableReference)
+                               expr.Emit (ec);
+                       else
+                               LocalVariable.Emit (ec);
                }
 
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
-                       create_temp (ec);
-                       if (temp != null)
-                               temp.AddressOf (ec, AddressOp.LoadStore);
+                       IMemoryLocation ml = expr as VariableReference;
+                       if (ml != null) 
+                               ml.AddressOf (ec, mode);
                        else
-                               ((IMemoryLocation) expr).AddressOf (ec, AddressOp.LoadStore);
+                               LocalVariable.AddressOf (ec, mode);
+               }
+
+               //
+               // Keeps result of non-variable expression
+               //
+               LocalTemporary LocalVariable {
+                       get {
+                               if (temp == null)
+                                       temp = new LocalTemporary (info.Type);
+                               return temp;
+                       }
                }
 
                public void Emit (EmitContext ec, bool leave_copy)
                {
-                       create_temp (ec);
-                       if (leave_copy) {
-                               if (temp != null)
-                                       temp.Emit (ec);
-                               else
-                                       expr.Emit (ec);
-                       }
+                       if (leave_copy)
+                               Load (ec);
 
                        Emit (ec);
                }
@@ -258,6 +265,13 @@ namespace Mono.CSharp.Nullable
 
                public static Expression Create (Expression expr, Type type)
                {
+                       //
+                       // Avoid unwraping and wraping of the same type
+                       //
+                       Unwrap unwrap = expr as Unwrap;
+                       if (unwrap != null && TypeManager.IsEqual (expr.Type, TypeManager.GetTypeArguments (type) [0]))
+                               return unwrap.Original;
+               
                        return new Wrap (expr, type);
                }
                
@@ -533,8 +547,15 @@ namespace Mono.CSharp.Nullable
                        if (expr == null)
                                return null;
 
-                       user_operator = LiftExpression (ec, expr);
-                       return user_operator;
+                       //
+                       // When a user operator is of non-nullable type
+                       //
+                       if (Expr is Unwrap) {
+                               user_operator = LiftExpression (ec, expr);
+                               return user_operator;
+                       }
+
+                       return expr;
                }
        }
 
@@ -649,16 +670,16 @@ namespace Mono.CSharp.Nullable
                        ig.MarkLabel (load_left);
 
                        if (Oper == Operator.BitwiseAnd) {
-                               left_unwrap.LoadTemporary (ec);
+                               left_unwrap.Load (ec);
                        } else {
-                               right_unwrap.LoadTemporary (ec);
+                               right_unwrap.Load (ec);
                                right_unwrap = left_unwrap;
                        }
                        ig.Emit (OpCodes.Br_S, end_label);
 
                        // load right
                        ig.MarkLabel (load_right);
-                       right_unwrap.LoadTemporary (ec);
+                       right_unwrap.Load (ec);
 
                        ig.MarkLabel (end_label);
                }
@@ -746,15 +767,11 @@ namespace Mono.CSharp.Nullable
 
                public override void Emit (EmitContext ec)
                {
-                       if (left_unwrap != null)
-                               left_unwrap.Store (ec);
-
-                       if (right_unwrap != null) {
-                               if (right.Equals (left))
-                                       right_unwrap = left_unwrap;
-                               else
-                                       right_unwrap.Store (ec);
-                       }
+                       //
+                       // Optimize same expression operation
+                       //
+                       if (right_unwrap != null && right.Equals (left))
+                               right_unwrap = left_unwrap;
 
                        if (user_operator == null && IsBitwiseBoolean) {
                                EmitBitwiseBoolean (ec);
@@ -984,20 +1001,20 @@ namespace Mono.CSharp.Nullable
                        Type ltype = left.Type, rtype = right.Type;
 
                        //
-                       // If left is a nullable type and an implicit conversion exists from right to left,
-                       // the result type is left
+                       // If left is a nullable type and an implicit conversion exists from right to underlying type of left,
+                       // the result is underlying type of left
                        //
                        if (TypeManager.IsNullableType (ltype) && left.eclass != ExprClass.MethodGroup) {
                                unwrap = Unwrap.Create (left, ec);
                                if (unwrap == null)
                                        return null;
 
-                               if (Convert.ImplicitConversionExists (ec, right, ltype)) {
+                               if (Convert.ImplicitConversionExists (ec, right, unwrap.Type)) {
                                        left = unwrap;
-                                       right = Convert.ImplicitConversion (ec, right, ltype, loc);
                                        type = left.Type;
+                                       right = Convert.ImplicitConversion (ec, right, type, loc);
                                        return this;
-                               }
+                               }                       
                        } else if (TypeManager.IsReferenceType (ltype) && right.eclass != ExprClass.MethodGroup) {
                                if (Convert.ImplicitConversionExists (ec, right, ltype)) {
                                        //
@@ -1014,7 +1031,7 @@ namespace Mono.CSharp.Nullable
                                                return ReducedExpression.Create (lc != null ? right : left, this).Resolve (ec);
 
                                        right = Convert.ImplicitConversion (ec, right, ltype, loc);
-                                       type = right.Type;
+                                       type = left.Type;
                                        return this;
                                }
                        } else {
@@ -1022,7 +1039,7 @@ namespace Mono.CSharp.Nullable
                                return null;
                        }
 
-                       if (!Convert.ImplicitConversionExists (ec, left, rtype)) {
+                       if (!Convert.ImplicitConversionExists (ec, unwrap != null ? unwrap : left, rtype)) {
                                Binary.Error_OperatorCannotBeApplied (left, right, "??", loc);
                                return null;
                        }
@@ -1033,7 +1050,7 @@ namespace Mono.CSharp.Nullable
                        if (left.IsNull)
                                return ReducedExpression.Create (right, this).Resolve (ec);
 
-                       left = Convert.ImplicitConversion (ec, left, rtype, loc);
+                       left = Convert.ImplicitConversion (ec, unwrap != null ? unwrap : left, rtype, loc);
                        type = rtype;
                        return this;
                }
@@ -1083,7 +1100,7 @@ namespace Mono.CSharp.Nullable
        public class LiftedUnaryMutator : ExpressionStatement
        {
                public readonly UnaryMutator.Mode Mode;
-               Expression expr, null_value;
+               Expression expr;
                UnaryMutator underlying;
                Unwrap unwrap;
 
@@ -1116,7 +1133,6 @@ namespace Mono.CSharp.Nullable
                                return null;
 
                        type = expr.Type;
-                       null_value = LiftedNull.Create (type, loc);
                        return this;
                }
 
@@ -1129,15 +1145,16 @@ namespace Mono.CSharp.Nullable
                        unwrap.EmitCheck (ec);
                        ig.Emit (OpCodes.Brfalse, is_null_label);
 
-                       if (is_expr)
+                       if (is_expr) {
                                underlying.Emit (ec);
-                       else
+                               ig.Emit (OpCodes.Br_S, end_label);
+                       } else {
                                underlying.EmitStatement (ec);
-                       ig.Emit (OpCodes.Br, end_label);
+                       }
 
                        ig.MarkLabel (is_null_label);
                        if (is_expr)
-                               null_value.Emit (ec);
+                               LiftedNull.Create (type, loc).Emit (ec);
 
                        ig.MarkLabel (end_label);
                }