[Mono.Debugger.Soft] Implement IsGenericType/Method for protocol < 2.12
[mono.git] / mcs / mcs / nullable.cs
index d7cf82b76b0711bc248c006d1373570cc2562a91..75048365c4ed5d25514508dafba7bcdd1fafac50 100644 (file)
@@ -9,6 +9,7 @@
 //
 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
 // Copyright 2004-2008 Novell, Inc
+// Copyright 2011 Xamarin Inc
 //
 
 using System;
@@ -79,7 +80,7 @@ namespace Mono.CSharp.Nullable
                }
        }
 
-       public class Unwrap : Expression, IMemoryLocation, IAssignMethod
+       public class Unwrap : Expression, IMemoryLocation
        {
                Expression expr;
 
@@ -96,6 +97,11 @@ namespace Mono.CSharp.Nullable
                        eclass = expr.eclass;
                }
 
+               public override bool ContainsEmitWithAwait ()
+               {
+                       return expr.ContainsEmitWithAwait ();
+               }
+
                public static Expression Create (Expression expr)
                {
                        //
@@ -132,16 +138,24 @@ namespace Mono.CSharp.Nullable
                public override void Emit (EmitContext ec)
                {
                        Store (ec);
+
+                       var call = new CallEmitter ();
+                       call.InstanceExpression = this;
+
                        if (useDefaultValue)
-                               Invocation.EmitCall (ec, this, NullableInfo.GetGetValueOrDefault (expr.Type), null, loc);
+                               call.EmitPredefined (ec, NullableInfo.GetGetValueOrDefault (expr.Type), null);
                        else
-                               Invocation.EmitCall (ec, this, NullableInfo.GetValue (expr.Type), null, loc);
+                               call.EmitPredefined (ec, NullableInfo.GetValue (expr.Type), null);
                }
 
                public void EmitCheck (EmitContext ec)
                {
                        Store (ec);
-                       Invocation.EmitCall (ec, this, NullableInfo.GetHasValue (expr.Type), null, loc);
+
+                       var call = new CallEmitter ();
+                       call.InstanceExpression = this;
+
+                       call.EmitPredefined (ec, NullableInfo.GetHasValue (expr.Type), null);
                }
 
                public override bool Equals (object obj)
@@ -169,10 +183,10 @@ namespace Mono.CSharp.Nullable
 
                void Store (EmitContext ec)
                {
-                       if (expr is VariableReference)
+                       if (temp != null)
                                return;
 
-                       if (temp != null)
+                       if (expr is VariableReference)
                                return;
 
                        expr.Emit (ec);
@@ -211,51 +225,6 @@ namespace Mono.CSharp.Nullable
                                return temp;
                        }
                }
-
-               public void Emit (EmitContext ec, bool leave_copy)
-               {
-                       if (leave_copy)
-                               Load (ec);
-
-                       Emit (ec);
-               }
-
-               public void EmitAssign (EmitContext ec, Expression source,
-                                       bool leave_copy, bool prepare_for_load)
-               {
-                       InternalWrap wrap = new InternalWrap (source, expr.Type, loc);
-                       ((IAssignMethod) expr).EmitAssign (ec, wrap, leave_copy, false);
-               }
-
-               class InternalWrap : Expression
-               {
-                       public Expression expr;
-
-                       public InternalWrap (Expression expr, TypeSpec type, Location loc)
-                       {
-                               this.expr = expr;
-                               this.loc = loc;
-                               this.type = type;
-
-                               eclass = ExprClass.Value;
-                       }
-
-                       public override Expression CreateExpressionTree (ResolveContext ec)
-                       {
-                               throw new NotSupportedException ("ET");
-                       }
-
-                       protected override Expression DoResolve (ResolveContext ec)
-                       {
-                               return this;
-                       }
-
-                       public override void Emit (EmitContext ec)
-                       {
-                               expr.Emit (ec);
-                               ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
-                       }
-               }
        }
 
        //
@@ -280,7 +249,9 @@ namespace Mono.CSharp.Nullable
 
                public override void Emit (EmitContext ec)
                {
-                       Invocation.EmitCall (ec, Child, NullableInfo.GetValue (Child.Type), null, loc);
+                       var call = new CallEmitter ();
+                       call.InstanceExpression = Child;
+                       call.EmitPredefined (ec, NullableInfo.GetValue (Child.Type), null);
                }
        }
 
@@ -354,6 +325,7 @@ namespace Mono.CSharp.Nullable
                        value_target.AddressOf (ec, AddressOp.Store);
                        ec.Emit (OpCodes.Initobj, type);
                        value_target.Emit (ec);
+                       value_target.Release (ec);
                }
 
                public void AddressOf (EmitContext ec, AddressOp Mode)
@@ -386,6 +358,11 @@ namespace Mono.CSharp.Nullable
                        : this (expr, unwrap as Unwrap, type)
                {
                }
+
+               public override bool ContainsEmitWithAwait ()
+               {
+                       return unwrap.ContainsEmitWithAwait ();
+               }
                
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
@@ -413,7 +390,7 @@ namespace Mono.CSharp.Nullable
                                        return null;
 
                                null_value = LiftedNull.Create (type, loc);
-                       } else if (TypeManager.IsValueType (type)) {
+                       } else if (TypeSpec.IsValueType (type)) {
                                null_value = LiftedNull.Create (type, loc);
                        } else {
                                null_value = new NullConstant (type, loc);
@@ -677,7 +654,7 @@ namespace Mono.CSharp.Nullable
                        }
 
                        left_unwrap.Emit (ec);
-                       ec.Emit (OpCodes.Brtrue_S, load_right);
+                       ec.Emit (OpCodes.Brtrue, load_right);
 
                        // value & null, value | null
                        if (right_unwrap != null) {
@@ -733,7 +710,7 @@ namespace Mono.CSharp.Nullable
                        if (left_unwrap != null && (IsRightNullLifted || right.IsNull)) {
                                left_unwrap.EmitCheck (ec);
                                if (Oper == Binary.Operator.Equality) {
-                                       ec.Emit (OpCodes.Ldc_I4_0);
+                                       ec.EmitInt (0);
                                        ec.Emit (OpCodes.Ceq);
                                }
                                return;
@@ -742,7 +719,7 @@ namespace Mono.CSharp.Nullable
                        if (right_unwrap != null && (IsLeftNullLifted || left.IsNull)) {
                                right_unwrap.EmitCheck (ec);
                                if (Oper == Binary.Operator.Equality) {
-                                       ec.Emit (OpCodes.Ldc_I4_0);
+                                       ec.EmitInt (0);
                                        ec.Emit (OpCodes.Ceq);
                                }
                                return;
@@ -755,6 +732,11 @@ namespace Mono.CSharp.Nullable
                                user_operator.Emit (ec);
                                ec.Emit (Oper == Operator.Equality ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, dissimilar_label);
                        } else {
+                               if (ec.HasSet (BuilderContext.Options.AsyncBody) && right.ContainsEmitWithAwait ()) {
+                                       left = left.EmitToField (ec);
+                                       right = right.EmitToField (ec);
+                               }
+
                                left.Emit (ec);
                                right.Emit (ec);
 
@@ -774,7 +756,7 @@ namespace Mono.CSharp.Nullable
                                        ec.Emit (OpCodes.Ceq);
                        } else {
                                if (Oper == Operator.Inequality) {
-                                       ec.Emit (OpCodes.Ldc_I4_0);
+                                       ec.EmitInt (0);
                                        ec.Emit (OpCodes.Ceq);
                                }
                        }
@@ -783,9 +765,9 @@ namespace Mono.CSharp.Nullable
 
                        ec.MarkLabel (dissimilar_label);
                        if (Oper == Operator.Inequality)
-                               ec.Emit (OpCodes.Ldc_I4_1);
+                               ec.EmitInt (1);
                        else
-                               ec.Emit (OpCodes.Ldc_I4_0);
+                               ec.EmitInt (0);
 
                        ec.MarkLabel (end_label);
                }
@@ -839,7 +821,7 @@ namespace Mono.CSharp.Nullable
                        ec.MarkLabel (is_null_label);
 
                        if ((Oper & Operator.ComparisonMask) != 0) {
-                               ec.Emit (OpCodes.Ldc_I4_0);
+                               ec.EmitInt (0);
                        } else {
                                LiftedNull.Create (type, loc).Emit (ec);
                        }
@@ -854,8 +836,14 @@ namespace Mono.CSharp.Nullable
                                return;
                        }
 
-                       if (l.IsNullableType)
-                               l = TypeManager.GetTypeArguments (l) [0];
+                       if (left.Type.IsNullableType) {
+                               l = NullableInfo.GetUnderlyingType (left.Type);
+                               left = EmptyCast.Create (left, l);
+                       }
+
+                       if (right.Type.IsNullableType) {
+                               right = EmptyCast.Create (right, NullableInfo.GetUnderlyingType (right.Type));
+                       }
 
                        base.EmitOperator (ec, l);
                }
@@ -992,7 +980,7 @@ namespace Mono.CSharp.Nullable
                        // Lift the result in the case it can be null and predefined or user operator
                        // result type is of a value type
                        //
-                       if (!TypeManager.IsValueType (expr.Type))
+                       if (!TypeSpec.IsValueType (expr.Type))
                                return null;
 
                        if (state != orig_state)
@@ -1019,6 +1007,18 @@ namespace Mono.CSharp.Nullable
                        this.right = right;
                        this.loc = loc;
                }
+
+               public Expression LeftExpression {
+                       get {
+                               return left;
+                       }
+               }
+
+               public Expression RightExpression {
+                       get {
+                               return right;
+                       }
+               }
                
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
@@ -1087,7 +1087,7 @@ namespace Mono.CSharp.Nullable
                                        type = ltype;
                                        return this;
                                }
-                       } else if (TypeManager.IsReferenceType (ltype)) {
+                       } else if (TypeSpec.IsReferenceType (ltype)) {
                                if (Convert.ImplicitConversionExists (ec, right, ltype)) {
                                        //
                                        // If right is a dynamic expression, the result type is dynamic
@@ -1141,6 +1141,14 @@ namespace Mono.CSharp.Nullable
                        return this;
                }
 
+               public override bool ContainsEmitWithAwait ()
+               {
+                       if (unwrap != null)
+                               return unwrap.ContainsEmitWithAwait () || right.ContainsEmitWithAwait ();
+
+                       return left.ContainsEmitWithAwait () || right.ContainsEmitWithAwait ();
+               }
+
                protected override Expression DoResolve (ResolveContext ec)
                {
                        left = left.Resolve (ec);
@@ -1202,77 +1210,65 @@ namespace Mono.CSharp.Nullable
                        target.left = left.Clone (clonectx);
                        target.right = right.Clone (clonectx);
                }
-       }
-
-       public class LiftedUnaryMutator : ExpressionStatement
-       {
-               public readonly UnaryMutator.Mode Mode;
-               Expression expr;
-               UnaryMutator underlying;
-               Unwrap unwrap;
-
-               public LiftedUnaryMutator (UnaryMutator.Mode mode, Expression expr, Location loc)
+               
+               public override object Accept (StructuralVisitor visitor)
                {
-                       this.expr = expr;
-                       this.Mode = mode;
-                       this.loc = loc;
+                       return visitor.Visit (this);
                }
+       }
 
-               public override Expression CreateExpressionTree (ResolveContext ec)
+       class LiftedUnaryMutator : UnaryMutator
+       {
+               public LiftedUnaryMutator (Mode mode, Expression expr, Location loc)
+                       : base (mode, expr, loc)
                {
-                       return new SimpleAssign (this, this).CreateExpressionTree (ec);
                }
 
                protected override Expression DoResolve (ResolveContext ec)
                {
-                       expr = expr.Resolve (ec);
-                       if (expr == null)
-                               return null;
+                       var orig_expr = expr;
 
-                       unwrap = Unwrap.Create (expr, false);
-                       if (unwrap == null)
-                               return null;
-
-                       underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap, loc).Resolve (ec);
-                       if (underlying == null)
-                               return null;
+                       expr = Unwrap.Create (expr);
 
+                       var res = base.DoResolveOperation (ec);
 
-                       eclass = ExprClass.Value;
+                       expr = orig_expr;
                        type = expr.Type;
-                       return this;
+
+                       return res;
                }
 
-               void DoEmit (EmitContext ec, bool is_expr)
+               protected override void EmitOperation (EmitContext ec)
                {
                        Label is_null_label = ec.DefineLabel ();
                        Label end_label = ec.DefineLabel ();
 
-                       unwrap.EmitCheck (ec);
+                       LocalTemporary lt = new LocalTemporary (type);
+
+                       // Value is on the stack
+                       lt.Store (ec);
+
+                       var call = new CallEmitter ();
+                       call.InstanceExpression = lt;
+                       call.EmitPredefined (ec, NullableInfo.GetHasValue (expr.Type), null);
+
                        ec.Emit (OpCodes.Brfalse, is_null_label);
 
-                       if (is_expr) {
-                               underlying.Emit (ec);
-                               ec.Emit (OpCodes.Br_S, end_label);
-                       } else {
-                               underlying.EmitStatement (ec);
-                       }
+                       call = new CallEmitter ();
+                       call.InstanceExpression = lt;
+                       call.EmitPredefined (ec, NullableInfo.GetValue (expr.Type), null);
 
-                       ec.MarkLabel (is_null_label);
-                       if (is_expr)
-                               LiftedNull.Create (type, loc).Emit (ec);
+                       lt.Release (ec);
 
-                       ec.MarkLabel (end_label);
-               }
+                       base.EmitOperation (ec);
 
-               public override void Emit (EmitContext ec)
-               {
-                       DoEmit (ec, true);
-               }
+                       ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
+                       ec.Emit (OpCodes.Br_S, end_label);
 
-               public override void EmitStatement (EmitContext ec)
-               {
-                       DoEmit (ec, false);
+                       ec.MarkLabel (is_null_label);
+                       LiftedNull.Create (type, loc).Emit (ec);
+
+                       ec.MarkLabel (end_label);
                }
        }
 }