From 66d72192d182c9e0c61bd7fe201a07a0c4c22bcc Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Tue, 29 Sep 2009 17:18:58 +0000 Subject: [PATCH] 2009-09-29 Marek Safar * CSharpBinaryOperationBinder.cs, Extensions.cs, CSharpBinder.cs, CSharpUnaryOperationBinder.cs: Dynamic unary expressions. svn path=/trunk/mcs/; revision=142905 --- .../CSharpBinaryOperationBinder.cs | 9 +--- .../CSharpBinder.cs | 7 +++ .../CSharpUnaryOperationBinder.cs | 51 +++++++++++++++++-- .../Microsoft.CSharp.RuntimeBinder/ChangeLog | 5 ++ .../Extensions.cs | 18 +++++++ 5 files changed, 79 insertions(+), 11 deletions(-) diff --git a/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpBinaryOperationBinder.cs b/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpBinaryOperationBinder.cs index f73ccac24d9..758f276614f 100644 --- a/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpBinaryOperationBinder.cs +++ b/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpBinaryOperationBinder.cs @@ -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); - } } } diff --git a/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpBinder.cs b/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpBinder.cs index a81d47c9f85..4dfd3f4ed83 100644 --- a/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpBinder.cs +++ b/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpBinder.cs @@ -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) diff --git a/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpUnaryOperationBinder.cs b/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpUnaryOperationBinder.cs index 6ee4046f4f7..3a38c42c485 100644 --- a/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpUnaryOperationBinder.cs +++ b/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpUnaryOperationBinder.cs @@ -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); } } } diff --git a/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/ChangeLog b/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/ChangeLog index 1fc1dc61581..58d3a6103fd 100644 --- a/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/ChangeLog +++ b/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/ChangeLog @@ -1,3 +1,8 @@ +2009-09-29 Marek Safar + + * CSharpBinaryOperationBinder.cs, Extensions.cs, CSharpBinder.cs, + CSharpUnaryOperationBinder.cs: Dynamic unary expressions. + 2009-09-25 Marek Safar * CSharpBinaryOperationBinder.cs, CSharpBinder.cs: More dynamic diff --git a/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/Extensions.cs b/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/Extensions.cs index 8a50746bede..ff3b7c78d4e 100644 --- a/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/Extensions.cs +++ b/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/Extensions.cs @@ -43,6 +43,24 @@ namespace Microsoft.CSharp.RuntimeBinder new ReadOnlyCollectionBuilder (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; -- 2.25.1