CS1526 error recovery
[mono.git] / mcs / mcs / nullable.cs
index e075b8b6cee28fd4a0162e7ccf9fe8a263b965e0..307ebfb5d32515543b1ad707ecb54863d979d330 100644 (file)
@@ -74,20 +74,18 @@ namespace Mono.CSharp.Nullable
                        Value = value_pi.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
                                return;
                        }
 
 #if MS_COMPATIBLE
-                       if (UnderlyingType.Module == RootContext.ToplevelTypes.Builder) {
+                       if (TypeManager.IsBeingCompiled (UnderlyingType)) {
                                ConstructorInfo cinfo = TypeManager.DropGenericTypeArguments (type).GetConstructors ()[0];
                                Constructor = TypeBuilder.GetConstructor (type, cinfo);
                                return;
@@ -207,6 +205,13 @@ 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);
@@ -332,10 +337,10 @@ namespace Mono.CSharp.Nullable
        //
        // 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;
                }
@@ -353,15 +358,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
@@ -382,9 +378,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)
@@ -402,22 +401,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)
                {
-                       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);
+
+                               // 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);
+                               null_value = LiftedNull.Create (type, loc);
+                       } else {
+                               null_value = new NullConstant (type, loc);
+                       }
 
                        eclass = ExprClass.Value;
                        return this;
@@ -432,12 +442,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);
                }
 
@@ -598,10 +608,10 @@ namespace Mono.CSharp.Nullable
 
                        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);
@@ -881,7 +891,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);
 
                                //
@@ -894,7 +904,7 @@ 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);
 
                                //