2009-09-29 Marek Safar <marek.safar@gmail.com>
authorMarek Safar <marek.safar@gmail.com>
Tue, 29 Sep 2009 17:18:58 +0000 (17:18 -0000)
committerMarek Safar <marek.safar@gmail.com>
Tue, 29 Sep 2009 17:18:58 +0000 (17:18 -0000)
* CSharpBinaryOperationBinder.cs, Extensions.cs, CSharpBinder.cs,
CSharpUnaryOperationBinder.cs: Dynamic unary expressions.

svn path=/trunk/mcs/; revision=142905

mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpBinaryOperationBinder.cs
mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpBinder.cs
mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpUnaryOperationBinder.cs
mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/ChangeLog
mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/Extensions.cs

index f73ccac24d9d87fc6c4267acd9050a7db7188076..758f276614f3de40c7c489e4592b860c034d3f9b 100644 (file)
@@ -179,15 +179,8 @@ namespace Microsoft.CSharp.RuntimeBinder
                        if (is_checked)
                                expr = new Compiler.CheckedExpr (expr, Compiler.Location.Null);
 
-                       var restrictions = CreateRestrictionsOnTarget (target).Merge (CreateRestrictionsOnTarget (arg));
+                       var restrictions = CSharpBinder.CreateRestrictionsOnTarget (target).Merge (CSharpBinder.CreateRestrictionsOnTarget (arg));
                        return CSharpBinder.Bind (target, expr, restrictions, errorSuggestion);
                }
-
-               static BindingRestrictions CreateRestrictionsOnTarget (DynamicMetaObject arg)
-               {
-                       return arg.HasValue && arg.Value == null ?
-                               BindingRestrictions.GetInstanceRestriction (arg.Expression, null) :
-                               BindingRestrictions.GetTypeRestriction (arg.Expression, arg.LimitType);
-               }
        }
 }
index a81d47c9f8574607cac3c7abe1195a70af3f95d6..4dfd3f4ed83ff2946e80672ae6865dddd451a7e9 100644 (file)
@@ -105,6 +105,13 @@ namespace Microsoft.CSharp.RuntimeBinder
                        return new Compiler.RuntimeValueExpression (value, typed);
                }
 
+               public static BindingRestrictions CreateRestrictionsOnTarget (DynamicMetaObject arg)
+               {
+                       return arg.HasValue && arg.Value == null ?
+                               BindingRestrictions.GetInstanceRestriction (arg.Expression, null) :
+                               BindingRestrictions.GetTypeRestriction (arg.Expression, arg.LimitType);
+               }
+
                static void InitializeCompiler (Compiler.CompilerContext ctx)
                {
                        if (compiler_initialized)
index 6ee4046f4f722aaa1d2dbc3a767192fb23bcb14e..3a38c42c48574cecdae1b9c142dabd1591a15339 100644 (file)
@@ -31,6 +31,7 @@ using System.Dynamic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq.Expressions;
+using Compiler = Mono.CSharp;
 
 namespace Microsoft.CSharp.RuntimeBinder
 {
@@ -57,16 +58,60 @@ namespace Microsoft.CSharp.RuntimeBinder
                                return is_checked;
                        }
                }
+
+               public override bool Equals (object obj)
+               {
+                       var other = obj as CSharpUnaryOperationBinder;
+                       return other != null && base.Equals (obj) && other.is_checked == is_checked &&
+                               other.argumentInfo.SequenceEqual (argumentInfo);
+               }
                
                public override int GetHashCode ()
                {
-                       return base.GetHashCode ();
+                       return Extensions.HashCode (
+                               base.GetHashCode (),
+                               is_checked.GetHashCode (),
+                               argumentInfo[0].GetHashCode ());
+               }
+
+               Compiler.Unary.Operator GetOperator ()
+               {
+                       switch (Operation) {
+                       case ExpressionType.Negate:
+                               return Compiler.Unary.Operator.UnaryNegation;
+                       case ExpressionType.Not:
+                               return Compiler.Unary.Operator.LogicalNot;
+                       case ExpressionType.OnesComplement:
+                               return Compiler.Unary.Operator.OnesComplement;
+                       case ExpressionType.UnaryPlus:
+                               return Compiler.Unary.Operator.UnaryPlus;
+                       default:
+                               throw new NotImplementedException (Operation.ToString ());
+                       }
                }
                
-               [MonoTODO]
                public override DynamicMetaObject FallbackUnaryOperation (DynamicMetaObject target, DynamicMetaObject errorSuggestion)
                {
-                       throw new NotImplementedException ();
+                       Compiler.Expression expr = CSharpBinder.CreateCompilerExpression (argumentInfo [0], target, true);
+
+                       if (Operation == ExpressionType.IsTrue) {
+                               expr = new Compiler.BooleanExpression (expr);
+                       } else {
+                               if (Operation == ExpressionType.Increment)
+                                       expr = new Compiler.UnaryMutator (Compiler.UnaryMutator.Mode.PreIncrement, expr);
+                               else if (Operation == ExpressionType.Decrement)
+                                       expr = new Compiler.UnaryMutator (Compiler.UnaryMutator.Mode.PreDecrement, expr);
+                               else
+                                       expr = new Compiler.Unary (GetOperator (), expr);
+
+                               expr = new Compiler.Cast (new Compiler.TypeExpression (typeof (object), Compiler.Location.Null), expr);
+
+                               if (is_checked)
+                                       expr = new Compiler.CheckedExpr (expr, Compiler.Location.Null);
+                       }
+
+                       var restrictions = CSharpBinder.CreateRestrictionsOnTarget (target);
+                       return CSharpBinder.Bind (target, expr, restrictions, errorSuggestion);
                }
        }
 }
index 1fc1dc615813de85eddd83563d3dca1fecd235dc..58d3a6103fd2a2c88bccec8d8a90a9dc6af33d69 100644 (file)
@@ -1,3 +1,8 @@
+2009-09-29  Marek Safar  <marek.safar@gmail.com>
+
+       * CSharpBinaryOperationBinder.cs, Extensions.cs, CSharpBinder.cs,
+       CSharpUnaryOperationBinder.cs: Dynamic unary expressions.
+
 2009-09-25  Marek Safar  <marek.safar@gmail.com>
 
        * CSharpBinaryOperationBinder.cs, CSharpBinder.cs: More dynamic
index 8a50746bede85bc70ad8f0a5ee51dbaeadb7bc03..ff3b7c78d4e4290082341c18757705e3f35172d9 100644 (file)
@@ -43,6 +43,24 @@ namespace Microsoft.CSharp.RuntimeBinder
                                new ReadOnlyCollectionBuilder<T> (col);
                }
 
+               public static int HashCode (int h1, int h2, int h3)
+               {
+                       const int FNV_prime = 16777619;
+                       int hash = unchecked ((int) 2166136261);
+
+                       hash = (hash ^ h1) * FNV_prime;
+                       hash = (hash ^ h2) * FNV_prime;
+                       hash = (hash ^ h3) * FNV_prime;
+
+                       hash += hash << 13;
+                       hash ^= hash >> 7;
+                       hash += hash << 3;
+                       hash ^= hash >> 17;
+                       hash += hash << 5;
+
+                       return hash;
+               }
+
                public static int HashCode (int h1, int h2, int h3, int h4, int h5)
                {
                        const int FNV_prime = 16777619;