2008-06-26 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / nullable.cs
index 40b93ce97c402001c6808696fed6c6336d828ee5..73f711a7e50394d00734d8c98ec2cc806d6582ce 100644 (file)
@@ -5,10 +5,10 @@
 //          Miguel de Icaza (miguel@ximian.com)
 //          Marek Safar (marek.safar@gmail.com)
 //
-// Licensed under the terms of the GNU GPL
+// Dual licensed under the terms of the MIT X11 or GNU GPL
 //
-// (C) 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
-// (C) 2004 Novell, Inc
+// Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
+// Copyright 2004-2008 Novell, Inc
 //
 
 using System;
@@ -34,14 +34,6 @@ namespace Mono.CSharp.Nullable
                        : this (new TypeExpression (type, loc), loc)
                { }
 
-               public override string Name {
-                       get { return underlying.ToString () + "?"; }
-               }
-
-               public override string FullName {
-                       get { return underlying.ToString () + "?"; }
-               }
-
                protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
                {
                        TypeArguments args = new TypeArguments (loc);
@@ -88,44 +80,6 @@ namespace Mono.CSharp.Nullable
                        Constructor = type.GetConstructor (new Type[] { UnderlyingType });
                }
        }
-       
-       public class HasValue : Expression
-       {
-               Expression expr;
-               NullableInfo info;
-
-               private HasValue (Expression expr)
-               {
-                       this.expr = expr;
-               }
-               
-               public static Expression Create (Expression expr, EmitContext ec)
-               {
-                       return new HasValue (expr).Resolve (ec);
-               }
-
-               public override void Emit (EmitContext ec)
-               {
-                       IMemoryLocation memory_loc = expr as IMemoryLocation;
-                       if (memory_loc == null) {
-                               LocalTemporary temp = new LocalTemporary (expr.Type);
-                               expr.Emit (ec);
-                               temp.Store (ec);
-                               memory_loc = temp;
-                       }
-                       memory_loc.AddressOf (ec, AddressOp.LoadStore);
-                       ec.ig.EmitCall (OpCodes.Call, info.HasValue, null);
-               }
-
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       this.info = new NullableInfo (expr.Type);
-
-                       type = TypeManager.bool_type;
-                       eclass = expr.eclass;
-                       return this;
-               }
-       }               
 
        public class Unwrap : Expression, IMemoryLocation, IAssignMethod
        {
@@ -133,7 +87,6 @@ namespace Mono.CSharp.Nullable
                NullableInfo info;
 
                LocalTemporary temp;
-               bool has_temp;
 
                protected Unwrap (Expression expr)
                {
@@ -156,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;
@@ -171,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);
                }
@@ -210,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);
                }
@@ -273,6 +234,11 @@ namespace Mono.CSharp.Nullable
                                eclass = ExprClass.Value;
                        }
 
+                       public override Expression CreateExpressionTree (EmitContext ec)
+                       {
+                               throw new NotSupportedException ("ET");
+                       }
+
                        public override Expression DoResolve (EmitContext ec)
                        {
                                return this;
@@ -286,7 +252,7 @@ namespace Mono.CSharp.Nullable
                }
        }
 
-       public class Wrap : EmptyCast
+       public class Wrap : TypeCast
        {
                readonly NullableInfo info;
 
@@ -297,8 +263,15 @@ namespace Mono.CSharp.Nullable
                        eclass = ExprClass.Value;
                }
 
-               public static new Expression Create (Expression expr, Type type)
+               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);
                }
                
@@ -375,7 +348,10 @@ namespace Mono.CSharp.Nullable
                
                public override Expression CreateExpressionTree (EmitContext ec)
                {
-                       return underlying.CreateExpressionTree (ec);
+                       ArrayList args = new ArrayList (2);
+                       args.Add (new Argument (expr.CreateExpressionTree (ec)));
+                       args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
+                       return CreateExpressionFactoryCall ("Convert", args);
                }                       
 
                public override Expression DoResolve (EmitContext ec)
@@ -467,19 +443,119 @@ namespace Mono.CSharp.Nullable
                }
        }
 
-       public class LiftedUnaryOperator : Lifted
+       public class LiftedUnaryOperator : Unary, IMemoryLocation
        {
-               public readonly Unary.Operator Oper;
+               Unwrap unwrap;
+               Expression user_operator;
 
                public LiftedUnaryOperator (Unary.Operator op, Expression expr, Location loc)
-                       : base (expr, loc)
+                       : base (op, expr, loc)
                {
-                       this.Oper = op;
                }
 
-               protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
+               public void AddressOf (EmitContext ec, AddressOp mode)
+               {
+                       unwrap.AddressOf (ec, mode);
+               }
+
+               public override Expression CreateExpressionTree (EmitContext ec)
+               {
+                       if (user_operator != null)
+                               return user_operator.CreateExpressionTree (ec);
+
+                       if (Oper == Operator.UnaryPlus)
+                               return Expr.CreateExpressionTree (ec);
+
+                       return base.CreateExpressionTree (ec);
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       if (eclass != ExprClass.Invalid)
+                               return this;
+
+                       unwrap = Unwrap.Create (Expr, ec);
+                       if (unwrap == null)
+                               return null;
+
+                       Expression res = base.ResolveOperator (ec, unwrap);
+                       if (res != this) {
+                               if (user_operator == null)
+                                       return res;
+                       } else {
+                               res = Expr = LiftExpression (ec, Expr);
+                       }
+
+                       if (res == null)
+                               return null;
+
+                       eclass = ExprClass.Value;
+                       type = res.Type;
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
                {
-                       return new Unary (Oper, unwrap, loc).Resolve (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);
+
+                       NullableInfo ni = new NullableInfo (type);
+
+                       if (user_operator != null) {
+                               user_operator.Emit (ec);
+                       } else {
+                               EmitOperator (ec, ni.UnderlyingType);
+                       }
+
+                       ig.Emit (OpCodes.Newobj, ni.Constructor);
+                       ig.Emit (OpCodes.Br_S, end_label);
+
+                       ig.MarkLabel (is_null_label);
+                       LiftedNull.Create (type, loc).Emit (ec);
+
+                       ig.MarkLabel (end_label);
+               }
+
+               Expression LiftExpression (EmitContext ec, Expression expr)
+               {
+                       TypeExpr lifted_type = new NullableType (expr.Type, expr.Location);
+                       lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
+                       if (lifted_type == null)
+                               return null;
+
+                       expr.Type = lifted_type.Type;
+                       return expr;
+               }
+
+               protected override Expression ResolveEnumOperator (EmitContext ec, Expression expr)
+               {
+                       expr = base.ResolveEnumOperator (ec, expr);
+                       if (expr == null)
+                               return null;
+
+                       Expr = LiftExpression (ec, Expr);
+                       return LiftExpression (ec, expr);
+               }
+
+               protected override Expression ResolveUserOperator (EmitContext ec, Expression expr)
+               {
+                       expr = base.ResolveUserOperator (ec, expr);
+                       if (expr == null)
+                               return null;
+
+                       //
+                       // When a user operator is of non-nullable type
+                       //
+                       if (Expr is Unwrap) {
+                               user_operator = LiftExpression (ec, expr);
+                               return user_operator;
+                       }
+
+                       return expr;
                }
        }
 
@@ -498,6 +574,14 @@ namespace Mono.CSharp.Nullable
                        this.loc = loc;
                }
 
+               public override Expression CreateExpressionTree (EmitContext ec)
+               {
+                       if (user_operator != null)
+                               return user_operator.CreateExpressionTree (ec);
+
+                       return base.CreateExpressionTree (ec);
+               }
+
                //
                // CSC 2 has this behavior, it allows structs to be compared
                // with the null literal *outside* of a generics context and
@@ -586,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);
                }
@@ -610,7 +694,7 @@ namespace Mono.CSharp.Nullable
                        //
                        // Either left or right is null
                        //
-                       if (left_unwrap != null && right.IsNull) {
+                       if (left_unwrap != null && (right_null_lifted || right.IsNull)) {
                                left_unwrap.EmitCheck (ec);
                                if (Oper == Binary.Operator.Equality) {
                                        ig.Emit (OpCodes.Ldc_I4_0);
@@ -619,7 +703,7 @@ namespace Mono.CSharp.Nullable
                                return true;
                        }
 
-                       if (right_unwrap != null && left.IsNull) {
+                       if (right_unwrap != null && (left_null_lifted || left.IsNull)) {
                                right_unwrap.EmitCheck (ec);
                                if (Oper == Binary.Operator.Equality) {
                                        ig.Emit (OpCodes.Ldc_I4_0);
@@ -683,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);
@@ -724,7 +804,7 @@ namespace Mono.CSharp.Nullable
                        EmitOperator (ec, left.Type);
 
                        if (wrap_ctor != null)
-                               ec.ig.Emit (OpCodes.Newobj, wrap_ctor);
+                               ig.Emit (OpCodes.Newobj, wrap_ctor);
 
                        ig.Emit (OpCodes.Br_S, end_label);
                        ig.MarkLabel (is_null_label);
@@ -774,7 +854,7 @@ namespace Mono.CSharp.Nullable
                                if (lifted_type == null)
                                        return null;
 
-                               if (left is UserCast || left is EmptyCast)
+                               if (left is UserCast || left is TypeCast)
                                        left.Type = lifted_type.Type;
                                else
                                        left = EmptyCast.Create (left, lifted_type.Type);
@@ -786,7 +866,7 @@ namespace Mono.CSharp.Nullable
                                if (lifted_type == null)
                                        return null;
 
-                               if (right is UserCast || right is EmptyCast)
+                               if (right is UserCast || right is TypeCast)
                                        right.Type = lifted_type.Type;
                                else
                                        right = EmptyCast.Create (right, lifted_type.Type);
@@ -799,7 +879,7 @@ namespace Mono.CSharp.Nullable
                                        return null;
 
                                wrap_ctor = new NullableInfo (lifted_type.Type).Constructor;
-                               res_expr.Type = lifted_type.Type;
+                               type = res_expr.Type = lifted_type.Type;
                        }
 
                        if (left_null_lifted) {
@@ -831,11 +911,11 @@ namespace Mono.CSharp.Nullable
                        return res_expr;
                }
 
-               protected override Expression ResolveOperatorPredefined (EmitContext ec, Binary.PredefinedOperator [] operators, bool primitives_only)
+               protected override Expression ResolveOperatorPredefined (EmitContext ec, Binary.PredefinedOperator [] operators, bool primitives_only, Type enum_type)
                {
-                       Expression e = base.ResolveOperatorPredefined (ec, operators, primitives_only);
+                       Expression e = base.ResolveOperatorPredefined (ec, operators, primitives_only, enum_type);
 
-                       if (e == this)
+                       if (e == this || enum_type != null)
                                return LiftResult (ec, e);
 
                        //
@@ -883,6 +963,9 @@ namespace Mono.CSharp.Nullable
                
                public override Expression CreateExpressionTree (EmitContext ec)
                {
+                       if (left is NullLiteral)
+                               Report.Error (845, loc, "An expression tree cannot contain a coalescing operator with null left side");
+
                        UserCast uc = left as UserCast;
                        Expression conversion = null;
                        if (uc != null) {
@@ -916,54 +999,71 @@ namespace Mono.CSharp.Nullable
 
                        eclass = ExprClass.Value;
                        Type ltype = left.Type, rtype = right.Type;
-                       Expression expr;
-
-                       if (TypeManager.IsNullableType (ltype)) {
-                               NullableInfo info = new NullableInfo (ltype);
 
+                       //
+                       // 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;
 
-                               expr = Convert.ImplicitConversion (ec, right, info.UnderlyingType, loc);
-                               if (expr != null) {
+                               if (Convert.ImplicitConversionExists (ec, right, unwrap.Type)) {
                                        left = unwrap;
-                                       right = expr;
-                                       type = expr.Type;
+                                       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)) {
+                                       //
+                                       // Reduce (constant ?? expr) to constant
+                                       //
+                                       Constant lc = left as Constant;
+                                       if (lc != null && !lc.IsDefaultValue)
+                                               return new SideEffectConstant (lc, right, loc).Resolve (ec);
+
+                                       //
+                                       // Reduce (left ?? null) to left OR (null-constant ?? right) to right
+                                       //
+                                       if (right.IsNull || lc != null)
+                                               return ReducedExpression.Create (lc != null ? right : left, this).Resolve (ec);
+
+                                       right = Convert.ImplicitConversion (ec, right, ltype, loc);
+                                       type = left.Type;
                                        return this;
                                }
-                       } else if (!TypeManager.IsReferenceType (ltype)) {
-                               Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
+                       } else {
+                               Binary.Error_OperatorCannotBeApplied (left, right, "??", loc);
                                return null;
                        }
 
-                       expr = Convert.ImplicitConversion (ec, right, ltype, loc);
-                       if (expr != null) {
-                               type = expr.Type;
-                               right = expr;
-                               return this;
+                       if (!Convert.ImplicitConversionExists (ec, unwrap != null ? unwrap : left, rtype)) {
+                               Binary.Error_OperatorCannotBeApplied (left, right, "??", loc);
+                               return null;
                        }
 
-                       Expression left_null = unwrap != null ? unwrap : left;
-                       expr = Convert.ImplicitConversion (ec, left_null, rtype, loc);
-                       if (expr != null) {
-                               left = expr;
-                               type = rtype;
-                               return this;
-                       }
+                       //
+                       // Reduce (null ?? right) to right
+                       //
+                       if (left.IsNull)
+                               return ReducedExpression.Create (right, this).Resolve (ec);
 
-                       Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
-                       return null;
+                       left = Convert.ImplicitConversion (ec, unwrap != null ? unwrap : left, rtype, loc);
+                       type = rtype;
+                       return this;
                }
 
                public override void Emit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
 
-                       Label is_null_label = ig.DefineLabel ();
                        Label end_label = ig.DefineLabel ();
 
                        if (unwrap != null) {
+                               Label is_null_label = ig.DefineLabel ();
+
                                unwrap.EmitCheck (ec);
                                ig.Emit (OpCodes.Brfalse, is_null_label);
 
@@ -974,19 +1074,20 @@ namespace Mono.CSharp.Nullable
                                right.Emit (ec);
 
                                ig.MarkLabel (end_label);
-                       } else {
-                               left.Emit (ec);
-                               ig.Emit (OpCodes.Dup);
-                               ig.Emit (OpCodes.Brtrue, end_label);
+                               return;
+                       }
 
-                               ig.MarkLabel (is_null_label);
+                       left.Emit (ec);
 
-                               ig.Emit (OpCodes.Pop);
-                               right.Emit (ec);
+                       ig.Emit (OpCodes.Dup);
+                       ig.Emit (OpCodes.Brtrue, end_label);
 
-                               ig.MarkLabel (end_label);
-                       }
+                       ig.Emit (OpCodes.Pop);
+                       right.Emit (ec);
+
+                       ig.MarkLabel (end_label);
                }
+
                protected override void CloneTo (CloneContext clonectx, Expression t)
                {
                        NullCoalescingOperator target = (NullCoalescingOperator) t;
@@ -999,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;
 
@@ -1012,6 +1113,11 @@ namespace Mono.CSharp.Nullable
                        eclass = ExprClass.Value;
                }
 
+               public override Expression CreateExpressionTree (EmitContext ec)
+               {
+                       return new SimpleAssign (this, this).CreateExpressionTree (ec);
+               }
+
                public override Expression DoResolve (EmitContext ec)
                {
                        expr = expr.Resolve (ec);
@@ -1027,7 +1133,6 @@ namespace Mono.CSharp.Nullable
                                return null;
 
                        type = expr.Type;
-                       null_value = LiftedNull.Create (type, loc);
                        return this;
                }
 
@@ -1040,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);
                }