2010-01-15 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / mcs / nullable.cs
index f423927c8dfded370adf836e6a547476d56ae201..3d37b687536b1459d3f2b58d914d3d9baba137b6 100644 (file)
@@ -14,7 +14,6 @@
 using System;
 using System.Reflection;
 using System.Reflection.Emit;
-using System.Collections;
        
 namespace Mono.CSharp.Nullable
 {
@@ -38,7 +37,7 @@ namespace Mono.CSharp.Nullable
                {
                        if (TypeManager.generic_nullable_type == null) {
                                TypeManager.generic_nullable_type = TypeManager.CoreLookupType (ec.Compiler,
-                                       "System", "Nullable`1", Kind.Struct, true);
+                                       "System", "Nullable`1", MemberKind.Struct, true);
                        }
 
                        TypeArguments args = new TypeArguments (underlying);
@@ -56,45 +55,43 @@ namespace Mono.CSharp.Nullable
        {
                public readonly Type Type;
                public readonly Type UnderlyingType;
-               public MethodInfo HasValue;
-               public MethodInfo Value;
-               public MethodInfo GetValueOrDefault;
-               public ConstructorInfo Constructor;
+               public MethodSpec HasValue;
+               public MethodSpec Value;
+               public MethodSpec GetValueOrDefault;
+               public MethodSpec Constructor;
 
                public NullableInfo (Type type)
                {
                        Type = type;
                        UnderlyingType = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (type) [0]);
 
-                       PropertyInfo has_value_pi = TypeManager.GetPredefinedProperty (type, "HasValue", Location.Null, Type.EmptyTypes);
-                       PropertyInfo value_pi = TypeManager.GetPredefinedProperty (type, "Value", Location.Null, Type.EmptyTypes);
+                       var has_value_pi = TypeManager.GetPredefinedProperty (type, "HasValue", Location.Null, Type.EmptyTypes);
+                       var value_pi = TypeManager.GetPredefinedProperty (type, "Value", Location.Null, Type.EmptyTypes);
                        GetValueOrDefault = TypeManager.GetPredefinedMethod (type, "GetValueOrDefault", Location.Null, Type.EmptyTypes);
 
-                       HasValue = has_value_pi.GetGetMethod (false);
-                       Value = value_pi.GetGetMethod (false);
+                       HasValue = Import.CreateMethod (has_value_pi.MetaInfo.GetGetMethod (false));
+                       Value = Import.CreateMethod (value_pi.MetaInfo.GetGetMethod (false));
 
                        // When compiling corlib
-                       if (type.Module == RootContext.ToplevelTypes.Builder) {
+                       if (TypeManager.IsBeingCompiled (type)) {
                                TypeContainer tc = TypeManager.LookupGenericTypeContainer (type);
                                
                                // TODO: check for correct overload
                                Constructor c = ((Constructor) tc.InstanceConstructors [0]);
 
-#if GMCS_SOURCE
-                               Constructor = TypeBuilder.GetConstructor (type, c.ConstructorBuilder);
-#endif
+                               Constructor = Import.CreateMethod (TypeBuilder.GetConstructor (type, c.ConstructorBuilder));
                                return;
                        }
 
 #if MS_COMPATIBLE
-                       if (UnderlyingType.Module == RootContext.ToplevelTypes.Builder) {
-                               ConstructorInfo cinfo = TypeManager.DropGenericTypeArguments (type).GetConstructors ()[0];
-                               Constructor = TypeBuilder.GetConstructor (type, cinfo);
-                               return;
-                       }
+//                     if (TypeManager.IsBeingCompiled (UnderlyingType)) {
+//                             ConstructorInfo cinfo = TypeManager.DropGenericTypeArguments (type).GetConstructors ()[0];
+//                             Constructor = TypeBuilder.GetConstructor (type, cinfo);
+//                             return;
+//                     }
 #endif
 
-                       Constructor = type.GetConstructor (new Type[] { UnderlyingType });
+                       Constructor = Import.CreateMethod (type.GetConstructor (new Type[] { UnderlyingType }));
                }
        }
 
@@ -139,7 +136,7 @@ namespace Mono.CSharp.Nullable
                        return expr.CreateExpressionTree (ec);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return this;
                }
@@ -207,20 +204,18 @@ namespace Mono.CSharp.Nullable
                                LocalVariable.Emit (ec);
                }
 
-#if NET_4_0
                public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
                {
                        return expr.MakeExpression (ctx);
                }
-#endif
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
                        type = storey.MutateType (type);
-                       info.Constructor = storey.MutateConstructor (info.Constructor);
-                       info.HasValue = storey.MutateGenericMethod (info.HasValue);
-                       info.GetValueOrDefault = storey.MutateGenericMethod (info.GetValueOrDefault);
-                       info.Value = storey.MutateGenericMethod (info.Value);
+                       storey.MutateConstructor (info.Constructor);
+                       storey.MutateGenericMethod (info.HasValue);
+                       storey.MutateGenericMethod (info.GetValueOrDefault);
+                       storey.MutateGenericMethod (info.Value);
                }
 
                public void AddressOf (EmitContext ec, AddressOp mode)
@@ -278,7 +273,7 @@ namespace Mono.CSharp.Nullable
                                throw new NotSupportedException ("ET");
                        }
 
-                       public override Expression DoResolve (ResolveContext ec)
+                       protected override Expression DoResolve (ResolveContext ec)
                        {
                                return this;
                        }
@@ -286,7 +281,7 @@ namespace Mono.CSharp.Nullable
                        public override void Emit (EmitContext ec)
                        {
                                expr.Emit (ec);
-                               ec.ig.Emit (OpCodes.Newobj, info.Constructor);
+                               ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) info.Constructor.MetaInfo);
                        }
                }
        }
@@ -332,17 +327,17 @@ namespace Mono.CSharp.Nullable
                public override void Emit (EmitContext ec)
                {
                        child.Emit (ec);
-                       ec.ig.Emit (OpCodes.Newobj, info.Constructor);
+                       ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) info.Constructor.MetaInfo);
                }
        }
 
        //
        // Represents null literal lifted to nullable type
        //
-       public class LiftedNull : EmptyConstantCast, IMemoryLocation
+       public class LiftedNull : NullConstant, IMemoryLocation
        {
                private LiftedNull (Type nullable_type, Location loc)
-                       : base (new NullLiteral (loc), nullable_type)
+                       : base (nullable_type, loc)
                {
                        eclass = ExprClass.Value;
                }
@@ -360,15 +355,6 @@ namespace Mono.CSharp.Nullable
                        return ReducedExpression.Create (Create (e.Type, e.Location), e);
                }
 
-               public override Expression CreateExpressionTree (ResolveContext ec)
-               {
-                       Arguments args = new Arguments (2);
-                       args.Add (new Argument (this));
-                       args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
-
-                       return CreateExpressionFactoryCall (ec, "Constant", args);
-               }
-
                public override void Emit (EmitContext ec)
                {
                        // TODO: generate less temporary variables
@@ -389,9 +375,12 @@ namespace Mono.CSharp.Nullable
                }
        }
 
+       //
+       // Generic lifting expression, supports all S/S? -> T/T? cases
+       //
        public class Lifted : Expression, IMemoryLocation
        {
-               Expression expr, wrap, null_value;
+               Expression expr, null_value;
                Unwrap unwrap;
 
                public Lifted (Expression expr, Unwrap unwrap, Type type)
@@ -409,22 +398,33 @@ namespace Mono.CSharp.Nullable
                
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       return wrap.CreateExpressionTree (ec);
+                       return expr.CreateExpressionTree (ec);
                }                       
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       wrap = Wrap.Create (expr, type);
-                       if (wrap == null)
-                               return null;
-
                        //
-                       // It's null when lifted conversion is transparent
+                       // It's null when lifting non-nullable type
                        //
-                       if (unwrap == null)
-                               return wrap;
+                       if (unwrap == null) {
+                               // S -> T? is wrap only
+                               if (TypeManager.IsNullableType (type))
+                                       return Wrap.Create (expr, type);
 
-                       null_value = LiftedNull.Create (type, loc);
+                               // S -> T can be simplified
+                               return expr;
+                       }
+
+                       // Wrap target for T?
+                       if (TypeManager.IsNullableType (type)) {
+                               expr = Wrap.Create (expr, type);
+                               if (expr == null)
+                                       return null;
+
+                               null_value = LiftedNull.Create (type, loc);
+                       } else {
+                               null_value = new NullConstant (type, loc);
+                       }
 
                        eclass = ExprClass.Value;
                        return this;
@@ -439,12 +439,12 @@ namespace Mono.CSharp.Nullable
                        unwrap.EmitCheck (ec);
                        ig.Emit (OpCodes.Brfalse, is_null_label);
 
-                       wrap.Emit (ec);
-                       ig.Emit (OpCodes.Br, end_label);
+                       expr.Emit (ec);
 
+                       ig.Emit (OpCodes.Br, end_label);
                        ig.MarkLabel (is_null_label);
-                       null_value.Emit (ec);
 
+                       null_value.Emit (ec);
                        ig.MarkLabel (end_label);
                }
 
@@ -480,11 +480,8 @@ namespace Mono.CSharp.Nullable
                        return base.CreateExpressionTree (ec);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        unwrap = Unwrap.Create (Expr, false);
                        if (unwrap == null)
                                return null;
@@ -522,7 +519,7 @@ namespace Mono.CSharp.Nullable
                                EmitOperator (ec, ni.UnderlyingType);
                        }
 
-                       ig.Emit (OpCodes.Newobj, ni.Constructor);
+                       ig.Emit (OpCodes.Newobj, (ConstructorInfo) ni.Constructor.MetaInfo);
                        ig.Emit (OpCodes.Br_S, end_label);
 
                        ig.MarkLabel (is_null_label);
@@ -576,7 +573,7 @@ namespace Mono.CSharp.Nullable
                bool left_null_lifted, right_null_lifted;
                Expression left_orig, right_orig;
                Expression user_operator;
-               ConstructorInfo wrap_ctor;
+               MethodSpec wrap_ctor;
 
                public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right,
                                             Location loc)
@@ -601,24 +598,21 @@ namespace Mono.CSharp.Nullable
                Expression CreateNullConstant (ResolveContext ec, Expression expr)
                {
                        // FIXME: Handle side effect constants
-                       Constant c = new BoolConstant (Oper == Operator.Inequality, loc);
+                       Constant c = new BoolConstant (Oper == Operator.Inequality, loc).Resolve (ec);
 
                        if ((Oper & Operator.EqualityMask) != 0) {
                                ec.Report.Warning (472, 2, loc, "The result of comparing value type `{0}' with null is `{1}'",
-                                               expr.GetSignatureForError (), c.AsString ());
+                                       TypeManager.CSharpName (expr.Type), c.AsString ());
                        } else {
                                ec.Report.Warning (464, 2, loc, "The result of comparing type `{0}' with null is always `{1}'",
-                                               expr.GetSignatureForError (), c.AsString ());
+                                       TypeManager.CSharpName (expr.Type), c.AsString ());
                        }
 
                        return ReducedExpression.Create (c, this);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        if ((Oper & Operator.LogicalMask) != 0) {
                                Error_OperatorCannotBeApplied (ec, left, right);
                                return null;
@@ -810,7 +804,7 @@ namespace Mono.CSharp.Nullable
                        EmitOperator (ec, left.Type);
 
                        if (wrap_ctor != null)
-                               ig.Emit (OpCodes.Newobj, wrap_ctor);
+                               ig.Emit (OpCodes.Newobj, (ConstructorInfo) wrap_ctor.MetaInfo);
 
                        ig.Emit (OpCodes.Br_S, end_label);
                        ig.MarkLabel (is_null_label);
@@ -888,7 +882,7 @@ namespace Mono.CSharp.Nullable
                        if (left_null_lifted) {
                                left = LiftedNull.Create (right.Type, left.Location);
 
-                               if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
+                               if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask | Operator.BitwiseMask)) != 0)
                                        return LiftedNull.CreateFromExpression (ec, res_expr);
 
                                //
@@ -901,14 +895,14 @@ namespace Mono.CSharp.Nullable
                        if (right_null_lifted) {
                                right = LiftedNull.Create (left.Type, right.Location);
 
-                               if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
+                               if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask | Operator.BitwiseMask)) != 0)
                                        return LiftedNull.CreateFromExpression (ec, res_expr);
 
                                //
                                // Value types and null comparison
                                //
                                if (left_unwrap == null || (Oper & Operator.RelationalMask) != 0)
-                                       return CreateNullConstant (ec, left_orig).Resolve (ec);
+                                       return CreateNullConstant (ec, left_orig);
                        }
 
                        return res_expr;
@@ -1025,7 +1019,7 @@ namespace Mono.CSharp.Nullable
                                        // 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);
+                                               return ReducedExpression.Create (lc != null ? right : left, this);
 
                                        right = Convert.ImplicitConversion (ec, right, ltype, loc);
                                        type = left.Type;
@@ -1043,18 +1037,15 @@ namespace Mono.CSharp.Nullable
                        // Reduce (null ?? right) to right
                        //
                        if (left.IsNull)
-                               return ReducedExpression.Create (right, this).Resolve (ec);
+                               return ReducedExpression.Create (right, this);
 
                        left = Convert.ImplicitConversion (ec, unwrap != null ? unwrap : left, rtype, loc);
                        type = rtype;
                        return this;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        left = left.Resolve (ec);
                        right = right.Resolve (ec);
 
@@ -1133,8 +1124,6 @@ namespace Mono.CSharp.Nullable
                        this.expr = expr;
                        this.Mode = mode;
                        this.loc = loc;
-
-                       eclass = ExprClass.Value;
                }
 
                public override Expression CreateExpressionTree (ResolveContext ec)
@@ -1142,7 +1131,7 @@ namespace Mono.CSharp.Nullable
                        return new SimpleAssign (this, this).CreateExpressionTree (ec);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        expr = expr.Resolve (ec);
                        if (expr == null)
@@ -1156,6 +1145,8 @@ namespace Mono.CSharp.Nullable
                        if (underlying == null)
                                return null;
 
+
+                       eclass = ExprClass.Value;
                        type = expr.Type;
                        return this;
                }