diff options
Diffstat (limited to 'mcs/class/System.Core/System.Linq.jvm')
-rw-r--r-- | mcs/class/System.Core/System.Linq.jvm/ChangeLog | 9 | ||||
-rw-r--r-- | mcs/class/System.Core/System.Linq.jvm/Conversion.cs | 463 | ||||
-rw-r--r-- | mcs/class/System.Core/System.Linq.jvm/ExpressionInterpreter.cs | 937 | ||||
-rw-r--r-- | mcs/class/System.Core/System.Linq.jvm/Math.cs | 695 | ||||
-rw-r--r-- | mcs/class/System.Core/System.Linq.jvm/Runner.cs | 227 |
5 files changed, 0 insertions, 2331 deletions
diff --git a/mcs/class/System.Core/System.Linq.jvm/ChangeLog b/mcs/class/System.Core/System.Linq.jvm/ChangeLog deleted file mode 100644 index 24d4b5a563..0000000000 --- a/mcs/class/System.Core/System.Linq.jvm/ChangeLog +++ /dev/null @@ -1,9 +0,0 @@ -2008-09-23 Jb Evain <jbevain@novell.com> - - * ExpressionInterpreter.cs - * Interpreter.cs - * Conversion.cs - * ExpressionValidator.cs - * Math.cs: - Integrate changes from db4objects, Inc. The interpreter now - passes all linq tests. diff --git a/mcs/class/System.Core/System.Linq.jvm/Conversion.cs b/mcs/class/System.Core/System.Linq.jvm/Conversion.cs deleted file mode 100644 index a3dc754ba0..0000000000 --- a/mcs/class/System.Core/System.Linq.jvm/Conversion.cs +++ /dev/null @@ -1,463 +0,0 @@ -// -// Conversion.cs -// -// (C) 2008 Mainsoft, Inc. (http://www.mainsoft.com) -// (C) 2008 db4objects, Inc. (http://www.db4o.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -using System; - -namespace System.Linq.jvm { - - class Conversion { - - public static object ConvertPrimitiveUnChecked (Type from, Type to, object value) - { - unchecked { - switch (Type.GetTypeCode (from)) { - case TypeCode.Byte: - return ConvertByte ((byte) value, to); - case TypeCode.Char: - return ConvertChar ((char) value, to); - case TypeCode.Decimal: - return ConvertDecimal ((decimal) value, to); - case TypeCode.Double: - return ConvertDouble ((double) value, to); - case TypeCode.Int16: - return ConvertShort ((short) value, to); - case TypeCode.Int32: - return ConvertInt ((int) value, to); - case TypeCode.Int64: - return ConvertLong ((long) value, to); - case TypeCode.SByte: - return ConvertSByte ((sbyte) value, to); - case TypeCode.Single: - return ConvertFloat ((float) value, to); - case TypeCode.UInt16: - return ConvertUShort ((ushort) value, to); - case TypeCode.UInt32: - return ConvertUInt ((uint) value, to); - case TypeCode.UInt64: - return ConvertULong ((ulong) value, to); - default: - throw new NotImplementedException (); - } - } - } - - static object ConvertByte (byte b, Type to) - { - unchecked { - switch (Type.GetTypeCode (to)) { - case TypeCode.Byte: - return (byte) b; - case TypeCode.Char: - return (char) b; - case TypeCode.Decimal: - return (decimal) b; - case TypeCode.Double: - return (double) b; - case TypeCode.Int16: - return (short) b; - case TypeCode.Int32: - return (int) b; - case TypeCode.Int64: - return (long) b; - case TypeCode.SByte: - return (sbyte) b; - case TypeCode.Single: - return (float) b; - case TypeCode.UInt16: - return (ushort) b; - case TypeCode.UInt32: - return (uint) b; - case TypeCode.UInt64: - return (ulong) b; - } - return null; - } - } - - static object ConvertChar (char b, Type to) - { - unchecked { - switch (Type.GetTypeCode (to)) { - case TypeCode.Byte: - return (byte) b; - case TypeCode.Char: - return (char) b; - case TypeCode.Decimal: - return (decimal) b; - case TypeCode.Double: - return (double) b; - case TypeCode.Int16: - return (short) b; - case TypeCode.Int32: - return (int) b; - case TypeCode.Int64: - return (long) b; - case TypeCode.SByte: - return (sbyte) b; - case TypeCode.Single: - return (float) b; - case TypeCode.UInt16: - return (ushort) b; - case TypeCode.UInt32: - return (uint) b; - case TypeCode.UInt64: - return (ulong) b; - } - return null; - } - } - - static object ConvertDecimal (decimal b, Type to) - { - unchecked { - switch (Type.GetTypeCode (to)) { - case TypeCode.Byte: - return (byte) b; - case TypeCode.Char: - return (char) (short) b; - case TypeCode.Decimal: - return (decimal) b; - case TypeCode.Double: - return (double) b; - case TypeCode.Int16: - return (short) b; - case TypeCode.Int32: - return (int) b; - case TypeCode.Int64: - return (long) b; - case TypeCode.SByte: - return (sbyte) b; - case TypeCode.Single: - return (float) b; - case TypeCode.UInt16: - return (ushort) b; - case TypeCode.UInt32: - return (uint) b; - case TypeCode.UInt64: - return (ulong) b; - } - return null; - } - } - - static object ConvertDouble (double b, Type to) - { - unchecked { - switch (Type.GetTypeCode (to)) { - case TypeCode.Byte: - return (byte) b; - case TypeCode.Char: - return (char) b; - case TypeCode.Decimal: - return (decimal) b; - case TypeCode.Double: - return (double) b; - case TypeCode.Int16: - return (short) b; - case TypeCode.Int32: - return (int) b; - case TypeCode.Int64: - return (long) b; - case TypeCode.SByte: - return (sbyte) b; - case TypeCode.Single: - return (float) b; - case TypeCode.UInt16: - return (ushort) b; - case TypeCode.UInt32: - return (uint) b; - case TypeCode.UInt64: - return (ulong) b; - } - return null; - } - } - - static object ConvertShort (short b, Type to) - { - unchecked { - switch (Type.GetTypeCode (to)) { - case TypeCode.Byte: - return (byte) b; - case TypeCode.Char: - return (char) b; - case TypeCode.Decimal: - return (decimal) b; - case TypeCode.Double: - return (double) b; - case TypeCode.Int16: - return (short) b; - case TypeCode.Int32: - return (int) b; - case TypeCode.Int64: - return (long) b; - case TypeCode.SByte: - return (sbyte) b; - case TypeCode.Single: - return (float) b; - case TypeCode.UInt16: - return (ushort) b; - case TypeCode.UInt32: - return (uint) b; - case TypeCode.UInt64: - return (ulong) b; - } - return null; - } - } - - static object ConvertInt (int b, Type to) - { - unchecked { - switch (Type.GetTypeCode (to)) { - case TypeCode.Byte: - return (byte) b; - case TypeCode.Char: - return (char) b; - case TypeCode.Decimal: - return (decimal) b; - case TypeCode.Double: - return (double) b; - case TypeCode.Int16: - return (short) b; - case TypeCode.Int32: - return (int) b; - case TypeCode.Int64: - return (long) b; - case TypeCode.SByte: - return (sbyte) b; - case TypeCode.Single: - return (float) b; - case TypeCode.UInt16: - return (ushort) b; - case TypeCode.UInt32: - return (uint) b; - case TypeCode.UInt64: - return (ulong) b; - } - return null; - } - } - - static object ConvertLong (long b, Type to) - { - unchecked { - switch (Type.GetTypeCode (to)) { - case TypeCode.Byte: - return (byte) b; - case TypeCode.Char: - return (char) b; - case TypeCode.Decimal: - return (decimal) b; - case TypeCode.Double: - return (double) b; - case TypeCode.Int16: - return (short) b; - case TypeCode.Int32: - return (int) b; - case TypeCode.Int64: - return (long) b; - case TypeCode.SByte: - return (sbyte) b; - case TypeCode.Single: - return (float) b; - case TypeCode.UInt16: - return (ushort) b; - case TypeCode.UInt32: - return (uint) b; - case TypeCode.UInt64: - return (ulong) b; - } - return null; - } - } - - static object ConvertSByte (sbyte b, Type to) - { - unchecked { - switch (Type.GetTypeCode (to)) { - case TypeCode.Byte: - return (byte) b; - case TypeCode.Char: - return (char) b; - case TypeCode.Decimal: - return (decimal) b; - case TypeCode.Double: - return (double) b; - case TypeCode.Int16: - return (short) b; - case TypeCode.Int32: - return (int) b; - case TypeCode.Int64: - return (long) b; - case TypeCode.SByte: - return (sbyte) b; - case TypeCode.Single: - return (float) b; - case TypeCode.UInt16: - return (ushort) b; - case TypeCode.UInt32: - return (uint) b; - case TypeCode.UInt64: - return (ulong) b; - } - return null; - } - } - - static object ConvertFloat (float b, Type to) - { - unchecked { - switch (Type.GetTypeCode (to)) { - case TypeCode.Byte: - return (byte) b; - case TypeCode.Char: - return (char) b; - case TypeCode.Decimal: - return (decimal) b; - case TypeCode.Double: - return (double) b; - case TypeCode.Int16: - return (short) b; - case TypeCode.Int32: - return (int) b; - case TypeCode.Int64: - return (long) b; - case TypeCode.SByte: - return (sbyte) b; - case TypeCode.Single: - return (float) b; - case TypeCode.UInt16: - return (ushort) b; - case TypeCode.UInt32: - return (uint) b; - case TypeCode.UInt64: - return (ulong) b; - } - return null; - } - } - - static object ConvertUShort (ushort b, Type to) - { - unchecked { - switch (Type.GetTypeCode (to)) { - case TypeCode.Byte: - return (byte) b; - case TypeCode.Char: - return (char) b; - case TypeCode.Decimal: - return (decimal) b; - case TypeCode.Double: - return (double) b; - case TypeCode.Int16: - return (short) b; - case TypeCode.Int32: - return (int) b; - case TypeCode.Int64: - return (long) b; - case TypeCode.SByte: - return (sbyte) b; - case TypeCode.Single: - return (float) b; - case TypeCode.UInt16: - return (ushort) b; - case TypeCode.UInt32: - return (uint) b; - case TypeCode.UInt64: - return (ulong) b; - } - return null; - } - } - - static object ConvertUInt (uint b, Type to) - { - unchecked { - switch (Type.GetTypeCode (to)) { - case TypeCode.Byte: - return (byte) b; - case TypeCode.Char: - return (char) b; - case TypeCode.Decimal: - return (decimal) b; - case TypeCode.Double: - return (double) b; - case TypeCode.Int16: - return (short) b; - case TypeCode.Int32: - return (int) b; - case TypeCode.Int64: - return (long) b; - case TypeCode.SByte: - return (sbyte) b; - case TypeCode.Single: - return (float) b; - case TypeCode.UInt16: - return (ushort) b; - case TypeCode.UInt32: - return (uint) b; - case TypeCode.UInt64: - return (ulong) b; - } - return null; - } - } - - static object ConvertULong (ulong b, Type to) - { - unchecked { - switch (Type.GetTypeCode (to)) { - case TypeCode.Byte: - return (byte) b; - case TypeCode.Char: - return (char) b; - case TypeCode.Decimal: - return (decimal) b; - case TypeCode.Double: - return (double) b; - case TypeCode.Int16: - return (short) b; - case TypeCode.Int32: - return (int) b; - case TypeCode.Int64: - return (long) b; - case TypeCode.SByte: - return (sbyte) b; - case TypeCode.Single: - return (float) b; - case TypeCode.UInt16: - return (ushort) b; - case TypeCode.UInt32: - return (uint) b; - case TypeCode.UInt64: - return (ulong) b; - } - return null; - } - } - } -} diff --git a/mcs/class/System.Core/System.Linq.jvm/ExpressionInterpreter.cs b/mcs/class/System.Core/System.Linq.jvm/ExpressionInterpreter.cs deleted file mode 100644 index 4d550d1ac5..0000000000 --- a/mcs/class/System.Core/System.Linq.jvm/ExpressionInterpreter.cs +++ /dev/null @@ -1,937 +0,0 @@ -// -// ExpressionInterpreter.cs -// -// (C) 2008 Mainsoft, Inc. (http://www.mainsoft.com) -// (C) 2008 db4objects, Inc. (http://www.db4o.com) -// (C) 2010 Novell, Inc. (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq.Expressions; -using System.Reflection; - -namespace System.Linq.jvm { - - struct LambdaInfo { - public readonly LambdaExpression Lambda; - public readonly object [] Arguments; - - public LambdaInfo (LambdaExpression lambda, object [] arguments) - { - this.Lambda = lambda; - this.Arguments = arguments; - } - } - - class HoistedVariableDetector : ExpressionVisitor { - - readonly Dictionary<ParameterExpression, LambdaExpression> parameter_to_lambda = - new Dictionary<ParameterExpression, LambdaExpression> (); - - Dictionary<LambdaExpression, List<ParameterExpression>> hoisted_map; - - LambdaExpression lambda; - - public Dictionary<LambdaExpression, List<ParameterExpression>> Process (LambdaExpression lambda) - { - Visit (lambda); - return hoisted_map; - } - - protected override void VisitLambda (LambdaExpression lambda) - { - this.lambda = lambda; - foreach (var parameter in lambda.Parameters) - parameter_to_lambda [parameter] = lambda; - base.VisitLambda (lambda); - } - - protected override void VisitParameter (ParameterExpression parameter) - { - if (lambda.Parameters.Contains (parameter)) - return; - - Hoist (parameter); - } - - void Hoist (ParameterExpression parameter) - { - LambdaExpression lambda; - if (!parameter_to_lambda.TryGetValue (parameter, out lambda)) - return; - - if (hoisted_map == null) - hoisted_map = new Dictionary<LambdaExpression, List<ParameterExpression>> (); - - List<ParameterExpression> hoisted; - if (!hoisted_map.TryGetValue (lambda, out hoisted)) { - hoisted = new List<ParameterExpression> (); - hoisted_map [lambda] = hoisted; - } - - hoisted.Add (parameter); - } - } - - - class ExpressionInterpreter : ExpressionVisitor { - - readonly Stack<LambdaInfo> lambdas = new Stack<LambdaInfo> (); - readonly Stack<object> stack = new Stack<object> (); - - readonly Dictionary<LambdaExpression, List<ParameterExpression>> hoisted_map; - readonly Dictionary<ParameterExpression, object> hoisted_values; - - void Push (object value) - { - stack.Push (value); - } - - object Pop () - { - return stack.Pop (); - } - - public ExpressionInterpreter (LambdaExpression lambda) - { - hoisted_map = new HoistedVariableDetector ().Process (lambda); - - if (hoisted_map != null) - hoisted_values = new Dictionary<ParameterExpression, object> (); - } - - private void VisitCoalesce (BinaryExpression binary) - { - Visit (binary.Left); - - var left = Pop (); - - if (left == null) { - Visit (binary.Right); - return; - } - - if (binary.Conversion == null) { - Push (left); - return; - } - - Push (Invoke (binary.Conversion.Compile (this), new [] { left })); - } - - void VisitAndAlso (BinaryExpression binary) - { - object left = null; - object right = null; - - Visit (binary.Left); - - left = Pop (); - - if (left == null || ((bool) left)) { - Visit (binary.Right); - right = Pop (); - } - - Push (Math.And (left, right)); - } - - void VisitUserDefinedAndAlso (BinaryExpression binary) - { - object left = null; - object right = null; - - Visit (binary.Left); - - left = Pop (); - - if (InvokeFalseOperator (binary, left)) { - Push (left); - return; - } - - Visit (binary.Right); - right = Pop (); - - if (binary.IsLiftedToNull && right == null) { - Push (null); - return; - } - - Push (InvokeMethod (binary.Method, null, new [] { left, right })); - } - - static bool InvokeTrueOperator (BinaryExpression binary, object target) - { - return (bool) InvokeMethod (GetTrueOperator (binary), null, new [] { target }); - } - - static bool InvokeFalseOperator (BinaryExpression binary, object target) - { - return (bool) InvokeMethod (GetFalseOperator (binary), null, new [] { target }); - } - - static MethodInfo GetFalseOperator (BinaryExpression binary) - { - return Expression.GetFalseOperator (binary.Left.Type.GetNotNullableType ()); - } - - static MethodInfo GetTrueOperator (BinaryExpression binary) - { - return Expression.GetTrueOperator (binary.Left.Type.GetNotNullableType ()); - } - - void VisitOrElse (BinaryExpression binary) - { - object left = null; - object right = null; - - Visit (binary.Left); - left = Pop (); - - if (left == null || !((bool) left)) { - Visit (binary.Right); - right = Pop (); - } - - Push (Math.Or (left, right)); - } - - void VisitUserDefinedOrElse (BinaryExpression binary) - { - object left = null; - object right = null; - - Visit (binary.Left); - left = Pop (); - - if (InvokeTrueOperator (binary, left)) { - Push (left); - return; - } - - Visit (binary.Right); - right = Pop (); - - if (binary.IsLiftedToNull && right == null) { - Push (null); - return; - } - - Push (InvokeMethod (binary.Method, null, new [] { left, right })); - } - - void VisitLogicalBinary (BinaryExpression binary) - { - Visit (binary.Left); - Visit (binary.Right); - - var right = Pop (); - var left = Pop (); - - Push (Math.Evaluate (left, right, binary.Type, binary.NodeType)); - } - - void VisitArithmeticBinary (BinaryExpression binary) - { - Visit (binary.Left); - Visit (binary.Right); - - if (IsNullBinaryLifting (binary)) - return; - - var right = Pop (); - var left = Pop (); - - switch (binary.NodeType) { - case ExpressionType.RightShift: - Push (Math.RightShift (left, Convert.ToInt32 (right), Type.GetTypeCode (binary.Type.GetNotNullableType ()))); - return; - case ExpressionType.LeftShift: - Push (Math.LeftShift (left, Convert.ToInt32 (right), Type.GetTypeCode (binary.Type.GetNotNullableType ()))); - return; - default: - Push (Math.Evaluate (left, right, binary.Type, binary.NodeType)); - break; - } - } - - bool IsNullRelationalBinaryLifting (BinaryExpression binary) - { - var right = Pop (); - var left = Pop (); - - if (binary.IsLifted && (left == null || right == null)) { - if (binary.IsLiftedToNull) { - Push (null); - return true; - } - - switch (binary.NodeType) { - case ExpressionType.Equal: - Push (BinaryEqual (binary, left, right)); - break; - case ExpressionType.NotEqual: - Push (BinaryNotEqual (binary, left, right)); - break; - default: - Push (false); - break; - } - - return true; - } - - Push (left); - Push (right); - - return false; - } - - void VisitRelationalBinary (BinaryExpression binary) - { - Visit (binary.Left); - Visit (binary.Right); - - if (IsNullRelationalBinaryLifting (binary)) - return; - - var right = Pop (); - var left = Pop (); - - switch (binary.NodeType) { - case ExpressionType.Equal: - Push (BinaryEqual (binary, left, right)); - return; - case ExpressionType.NotEqual: - Push (BinaryNotEqual (binary, left, right)); - return; - case ExpressionType.LessThan: - Push (Comparer<object>.Default.Compare (left, right) < 0); - return; - case ExpressionType.LessThanOrEqual: - Push (Comparer<object>.Default.Compare (left, right) <= 0); - return; - case ExpressionType.GreaterThan: - Push (Comparer<object>.Default.Compare (left, right) > 0); - return; - case ExpressionType.GreaterThanOrEqual: - Push (Comparer<object>.Default.Compare (left, right) >= 0); - return; - } - } - - void VisitLogicalShortCircuitBinary (BinaryExpression binary) - { - switch (binary.NodeType) { - case ExpressionType.AndAlso: - VisitAndAlso (binary); - return; - case ExpressionType.OrElse: - VisitOrElse (binary); - return; - } - } - - void VisitArrayIndex (BinaryExpression binary) - { - Visit (binary.Left); - var left = Pop (); - Visit (binary.Right); - var right = Pop (); - - Push (((Array) left).GetValue ((int) right)); - } - - bool IsNullBinaryLifting (BinaryExpression binary) - { - var right = Pop (); - var left = Pop (); - - if (binary.IsLifted && (right == null || left == null)) { - if (binary.IsLiftedToNull) - Push (null); - else - Push (GetDefaultValue (binary.Type)); - - return true; - } - - Push (left); - Push (right); - - return false; - } - - static object GetDefaultValue (Type type) - { - var array = (Array) Array.CreateInstance (type, 1); - return array.GetValue (0); - } - - void VisitUserDefinedBinary (BinaryExpression binary) - { - switch (binary.NodeType) { - case ExpressionType.AndAlso: - case ExpressionType.OrElse: - VisitUserDefinedLogicalShortCircuitBinary (binary); - return; - case ExpressionType.Equal: - case ExpressionType.NotEqual: - VisitUserDefinedRelationalBinary (binary); - return; - default: - VisitUserDefinedCommonBinary (binary); - return; - } - } - - void VisitUserDefinedLogicalShortCircuitBinary (BinaryExpression binary) - { - switch (binary.NodeType) { - case ExpressionType.AndAlso: - VisitUserDefinedAndAlso (binary); - return; - case ExpressionType.OrElse: - VisitUserDefinedOrElse (binary); - return; - } - } - - void VisitUserDefinedRelationalBinary (BinaryExpression binary) - { - Visit (binary.Left); - Visit (binary.Right); - - if (IsNullRelationalBinaryLifting (binary)) - return; - - var right = Pop (); - var left = Pop (); - - Push (InvokeBinary (binary, left, right)); - } - - void VisitUserDefinedCommonBinary (BinaryExpression binary) - { - Visit (binary.Left); - Visit (binary.Right); - - if (IsNullBinaryLifting (binary)) - return; - - var right = Pop (); - var left = Pop (); - - Push (InvokeBinary (binary, left, right)); - } - - object InvokeBinary (BinaryExpression binary, object left, object right) - { - return InvokeMethod (binary.Method, null, new [] { left, right }); - } - - bool BinaryEqual (BinaryExpression binary, object left, object right) - { - if (typeof (ValueType).IsAssignableFrom (binary.Right.Type)) - return ValueType.Equals (left, right); - else - return left == right; - } - - bool BinaryNotEqual (BinaryExpression binary, object left, object right) - { - if (typeof (ValueType).IsAssignableFrom (binary.Right.Type)) - return !ValueType.Equals (left, right); - else - return left != right; - } - - protected override void VisitBinary (BinaryExpression binary) - { - if (binary.Method != null) { - VisitUserDefinedBinary (binary); - return; - } - - switch (binary.NodeType) { - case ExpressionType.ArrayIndex: - VisitArrayIndex (binary); - return; - case ExpressionType.Coalesce: - VisitCoalesce (binary); - return; - case ExpressionType.AndAlso: - case ExpressionType.OrElse: - VisitLogicalShortCircuitBinary (binary); - return; - case ExpressionType.Equal: - case ExpressionType.NotEqual: - case ExpressionType.GreaterThan: - case ExpressionType.GreaterThanOrEqual: - case ExpressionType.LessThan: - case ExpressionType.LessThanOrEqual: - VisitRelationalBinary (binary); - return; - case ExpressionType.And: - case ExpressionType.Or: - VisitLogicalBinary (binary); - return; - case ExpressionType.Power: - case ExpressionType.Add: - case ExpressionType.AddChecked: - case ExpressionType.Divide: - case ExpressionType.ExclusiveOr: - case ExpressionType.LeftShift: - case ExpressionType.Modulo: - case ExpressionType.Multiply: - case ExpressionType.MultiplyChecked: - case ExpressionType.RightShift: - case ExpressionType.Subtract: - case ExpressionType.SubtractChecked: - VisitArithmeticBinary (binary); - return; - } - } - - void VisitTypeAs (UnaryExpression unary) - { - Visit (unary.Operand); - - var value = Pop (); - if (value == null || !Math.IsType (unary.Type, value)) - Push (null); - else - Push (value); - } - - void VisitArrayLength (UnaryExpression unary) - { - Visit (unary.Operand); - - var array = (Array) Pop (); - Push (array.Length); - } - - void VisitConvert (UnaryExpression unary) - { - if (unary.NodeType == ExpressionType.ConvertChecked) - VisitConvertChecked (unary); - else - VisitConvertUnchecked (unary); - } - - void VisitConvertChecked (UnaryExpression unary) - { - VisitConvert (unary, Math.ConvertToTypeChecked); - } - - void VisitConvertUnchecked (UnaryExpression unary) - { - VisitConvert (unary, Math.ConvertToTypeUnchecked); - } - - void VisitConvert (UnaryExpression unary, Func<object, Type, Type, object> converter) - { - Visit (unary.Operand); - Push (converter (Pop (), unary.Operand.Type, unary.Type)); - } - - bool IsNullUnaryLifting (UnaryExpression unary) - { - var value = Pop (); - - if (unary.IsLifted && value == null) { - if (unary.IsLiftedToNull) { - Push (null); - return true; - } else { - throw new InvalidOperationException (); - } - } - - Push (value); - return false; - } - - void VisitQuote (UnaryExpression unary) - { - Push (unary.Operand); - } - - void VisitUserDefinedUnary (UnaryExpression unary) - { - Visit (unary.Operand); - - if (IsNullUnaryLifting (unary)) - return; - - var value = Pop (); - - Push (InvokeUnary (unary, value)); - } - - object InvokeUnary (UnaryExpression unary, object value) - { - return InvokeMethod (unary.Method, null, new [] { value }); - } - - void VisitArithmeticUnary (UnaryExpression unary) - { - Visit (unary.Operand); - - if (IsNullUnaryLifting (unary)) - return; - - var value = Pop (); - - switch (unary.NodeType) { - case ExpressionType.Not: - if (unary.Type.GetNotNullableType () == typeof (bool)) - Push (!Convert.ToBoolean (value)); - else - Push (~Convert.ToInt32 (value)); - return; - case ExpressionType.Negate: - Push (Math.Negate (value, Type.GetTypeCode (unary.Type.GetNotNullableType ()))); - return; - case ExpressionType.NegateChecked: - Push (Math.NegateChecked (value, Type.GetTypeCode (unary.Type.GetNotNullableType ()))); - return; - case ExpressionType.UnaryPlus: - Push (value); - return; - } - } - - protected override void VisitUnary (UnaryExpression unary) - { - if (unary.Method != null) { - VisitUserDefinedUnary (unary); - return; - } - - switch (unary.NodeType) { - case ExpressionType.Quote: - VisitQuote (unary); - return; - case ExpressionType.TypeAs: - VisitTypeAs (unary); - return; - case ExpressionType.ArrayLength: - VisitArrayLength (unary); - return; - case ExpressionType.Convert: - case ExpressionType.ConvertChecked: - VisitConvert (unary); - return; - case ExpressionType.Negate: - case ExpressionType.NegateChecked: - case ExpressionType.Not: - case ExpressionType.UnaryPlus: - VisitArithmeticUnary (unary); - return; - default: - throw new NotImplementedException (unary.NodeType.ToString ()); - } - } - - protected override void VisitNew (NewExpression nex) - { - if (nex.Constructor == null) - Push (Activator.CreateInstance (nex.Type)); - else - Push (InvokeConstructor (nex.Constructor, VisitListExpressions (nex.Arguments))); - } - - static object InvokeConstructor (ConstructorInfo constructor, object [] arguments) - { - try { - return constructor.Invoke (arguments); - } catch (TargetInvocationException e) { - throw e.InnerException; - } - } - - protected override void VisitTypeIs (TypeBinaryExpression type) - { - Visit (type.Expression); - Push (Math.IsType (type.TypeOperand, Pop ())); - } - - void VisitMemberInfo (MemberInfo mi) - { - mi.OnFieldOrProperty ( - field => { - object target = null; - if (!field.IsStatic) - target = Pop (); - - Push (field.GetValue (target)); - }, - property => { - object target = null; - var getter = property.GetGetMethod (true); - if (!getter.IsStatic) - target = Pop (); - - Push (property.GetValue (target, null)); - }); - } - - protected override void VisitMemberAccess (MemberExpression member) - { - Visit (member.Expression); - VisitMemberInfo (member.Member); - } - - protected override void VisitNewArray (NewArrayExpression newArray) - { - switch (newArray.NodeType) { - case ExpressionType.NewArrayInit: - VisitNewArrayInit (newArray); - return; - case ExpressionType.NewArrayBounds: - VisitNewArrayBounds (newArray); - return; - } - - throw new NotSupportedException (); - } - - void VisitNewArrayBounds (NewArrayExpression newArray) - { - var lengths = new int [newArray.Expressions.Count]; - for (int i = 0; i < lengths.Length; i++) { - Visit (newArray.Expressions [i]); - lengths [i] = (int) Pop (); - } - - Push (Array.CreateInstance (newArray.Type.GetElementType (), lengths)); - } - - void VisitNewArrayInit (NewArrayExpression newArray) - { - var array = Array.CreateInstance ( - newArray.Type.GetElementType (), - newArray.Expressions.Count); - - for (int i = 0; i < array.Length; i++) { - Visit (newArray.Expressions [i]); - array.SetValue (Pop (), i); - } - - Push (array); - } - - protected override void VisitConditional (ConditionalExpression conditional) - { - Visit (conditional.Test); - - if ((bool) Pop ()) - Visit (conditional.IfTrue); - else - Visit (conditional.IfFalse); - } - - protected override void VisitMethodCall (MethodCallExpression call) - { - object instance = null; - if (call.Object != null) { - Visit (call.Object); - instance = Pop (); - } - - Push (InvokeMethod (call.Method, instance, VisitListExpressions (call.Arguments))); - } - - protected override void VisitParameter (ParameterExpression parameter) - { - var info = lambdas.Peek (); - - var lambda = info.Lambda; - var arguments = info.Arguments; - - var index = GetParameterIndex (lambda, parameter); - if (index >= 0) { - Push (arguments [index]); - return; - } - - object value; - if (hoisted_values.TryGetValue (parameter, out value)) { - Push (value); - return; - } - - throw new ArgumentException (); - } - - protected override void VisitConstant (ConstantExpression constant) - { - Push (constant.Value); - } - - protected override void VisitInvocation (InvocationExpression invocation) - { - Visit (invocation.Expression); - Push (Invoke ((Delegate) Pop (), VisitListExpressions (invocation.Arguments))); - } - - static object Invoke (Delegate dlg, object [] arguments) - { - return InvokeMethod (dlg.Method, dlg.Target, arguments); - } - - static object InvokeMethod (MethodBase method, object obj, object [] arguments) - { - try { - return method.Invoke (obj, arguments); - } catch (TargetInvocationException e) { - throw e.InnerException; - } - } - - protected override void VisitMemberListBinding (MemberListBinding binding) - { - var value = Pop (); - Push (value); - VisitMemberInfo (binding.Member); - VisitElementInitializerList (binding.Initializers); - Pop (); // pop the member - Push (value); // push the original target - } - - protected override void VisitElementInitializer (ElementInit initializer) - { - object target = null; - if (!initializer.AddMethod.IsStatic) - target = Pop (); - - var arguments = VisitListExpressions (initializer.Arguments); - InvokeMethod (initializer.AddMethod, target, arguments); - - if (!initializer.AddMethod.IsStatic) - Push (target); - } - - protected override void VisitMemberMemberBinding (MemberMemberBinding binding) - { - var value = Pop (); - Push (value); - VisitMemberInfo (binding.Member); - VisitBindingList (binding.Bindings); - Pop (); - Push (value); - } - - protected override void VisitMemberAssignment (MemberAssignment assignment) - { - Visit (assignment.Expression); - - var value = Pop (); - - assignment.Member.OnFieldOrProperty ( - field => { - object target = null; - if (!field.IsStatic) - target = Pop (); - - field.SetValue (target, value); - - if (!field.IsStatic) - Push (target); - }, - property => { - object target = null; - var getter = property.GetGetMethod (true); - if (!getter.IsStatic) - target = Pop (); - - property.SetValue (target, value, null); - - if (!getter.IsStatic) - Push (target); - }); - } - - protected override void VisitLambda (LambdaExpression lambda) - { - Push (lambda.Compile (this)); - } - - private object [] VisitListExpressions (ReadOnlyCollection<Expression> collection) - { - object [] results = new object [collection.Count]; - for (int i = 0; i < results.Length; i++) { - Visit (collection [i]); - results [i] = Pop (); - } - - return results; - } - - void StoreHoistedVariables (LambdaExpression lambda, object [] arguments) - { - if (hoisted_map == null) - return; - - List<ParameterExpression> variables; - if (!hoisted_map.TryGetValue (lambda, out variables)) - return; - - foreach (var variable in variables) - StoreHoistedVariable (variable, lambda, arguments); - } - - void StoreHoistedVariable (ParameterExpression variable, LambdaExpression lambda, object [] arguments) - { - var index = GetParameterIndex (lambda, variable); - if (index < 0) - return; - - hoisted_values [variable] = arguments [index]; - } - - static int GetParameterIndex (LambdaExpression lambda, ParameterExpression parameter) - { - return lambda.Parameters.IndexOf (parameter); - } - - public object Interpret (LambdaExpression lambda, object [] arguments) - { - lambdas.Push (new LambdaInfo (lambda, arguments)); - - StoreHoistedVariables (lambda, arguments); - - Visit (lambda.Body); - - lambdas.Pop (); - - if (lambda.GetReturnType () != typeof (void)) - return Pop (); - - return null; - } - } -} diff --git a/mcs/class/System.Core/System.Linq.jvm/Math.cs b/mcs/class/System.Core/System.Linq.jvm/Math.cs deleted file mode 100644 index b3e322cfed..0000000000 --- a/mcs/class/System.Core/System.Linq.jvm/Math.cs +++ /dev/null @@ -1,695 +0,0 @@ -// -// Math.cs -// -// (C) 2008 Mainsoft, Inc. (http://www.mainsoft.com) -// (C) 2008 db4objects, Inc. (http://www.db4o.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -using System; -using System.Globalization; -using System.Linq.Expressions; - -namespace System.Linq.jvm { - class Math { - - public static object Evaluate (object a, object b, Type t, ExpressionType et) - { - TypeCode tc = Type.GetTypeCode (t); - if (tc == TypeCode.Object) { - if (!t.IsNullable ()) { - throw new NotImplementedException ( - string.Format ( - "Expression with Node type {0} for type {1}", - t.FullName, - tc)); - - } - return EvaluateNullable (a, b, Type.GetTypeCode (t.GetGenericArguments () [0]), et); - } - return Evaluate (a, b, tc, et); - } - - public static object EvaluateNullable (object a, object b, TypeCode tc, ExpressionType et) - { - object o = null; - if (a == null || b == null) { - if (tc != TypeCode.Boolean) { - return null; - } - switch (et) { - case ExpressionType.And: - o = And (a, b); - break; - case ExpressionType.Or: - o = Or (a, b); - break; - case ExpressionType.ExclusiveOr: - o = ExclusiveOr (a, b); - break; - } - } else { - o = Evaluate (a, b, tc, et); - } - - return Convert2Nullable (o, tc); - - } - - private static object ExclusiveOr (object a, object b) - { - if (a == null || b == null) { - return null; - } - return (bool) a ^ (bool) b; - } - - public static object Or (object a, object b) - { - if (a == null) { - if (b == null || !((bool) b)) { - return null; - } - return true; - } - - if (b == null) { - if (a == null || !((bool) a)) { - return null; - } - return true; - } - - return (bool) a || (bool) b; - } - - public static object And (object a, object b) - { - if (a == null) { - if (b == null || (bool) b) { - return null; - } - return false; - } - - if (b == null) { - if (a == null || (bool) a) { - return null; - } - return false; - } - - return (bool) a && (bool) b; - } - - private static object Convert2Nullable (object o, TypeCode tc) - { - if (o == null) { - return null; - } - switch (tc) { - case TypeCode.Char: - return new Nullable<Char> ((Char) o); - case TypeCode.Byte: - return new Nullable<Byte> ((Byte) o); - case TypeCode.Decimal: - return new Nullable<Decimal> ((Decimal) o); - case TypeCode.Double: - return new Nullable<Double> ((Double) o); - case TypeCode.Int16: - return new Nullable<Int16> ((Int16) o); - case TypeCode.Int32: - return new Nullable<Int32> ((Int32) o); - case TypeCode.Int64: - return new Nullable<Int64> ((Int64) o); - case TypeCode.UInt16: - return new Nullable<UInt16> ((UInt16) o); - case TypeCode.UInt32: - return new Nullable<UInt32> ((UInt32) o); - case TypeCode.SByte: - return new Nullable<SByte> ((SByte) o); - case TypeCode.Single: - return new Nullable<Single> ((Single) o); - case TypeCode.Boolean: - return new Nullable<Boolean> ((Boolean) o); - } - - throw new NotImplementedException (); - } - - public static object Evaluate (object a, object b, TypeCode tc, ExpressionType et) - { - switch (tc) { - case TypeCode.Boolean: - return Evaluate (Convert.ToBoolean (a), Convert.ToBoolean (b), et); - case TypeCode.Char: - return Evaluate (Convert.ToChar (a), Convert.ToChar (b), et); - case TypeCode.Byte: - return unchecked ((Byte) Evaluate (Convert.ToByte (a), Convert.ToByte (b), et)); - case TypeCode.Decimal: - return Evaluate (Convert.ToDecimal (a), Convert.ToDecimal (b), et); - case TypeCode.Double: - return Evaluate (Convert.ToDouble (a), Convert.ToDouble (b), et); - case TypeCode.Int16: - return unchecked ((Int16) Evaluate (Convert.ToInt16 (a), Convert.ToInt16 (b), et)); - case TypeCode.Int32: - return Evaluate (Convert.ToInt32 (a), Convert.ToInt32 (b), et); - case TypeCode.Int64: - return Evaluate (Convert.ToInt64 (a), Convert.ToInt64 (b), et); - case TypeCode.UInt16: - return unchecked ((UInt16) Evaluate (Convert.ToUInt16 (a), Convert.ToUInt16 (b), et)); - case TypeCode.UInt32: - return Evaluate (Convert.ToUInt32 (a), Convert.ToUInt32 (b), et); - case TypeCode.UInt64: - return Evaluate (Convert.ToUInt64 (a), Convert.ToUInt64 (b), et); - case TypeCode.SByte: - return unchecked ((SByte) Evaluate (Convert.ToSByte (a), Convert.ToSByte (b), et)); - case TypeCode.Single: - return Evaluate (Convert.ToSingle (a), Convert.ToSingle (b), et); - - } - - throw new NotImplementedException (); - } - - public static object NegateChecked (object a, TypeCode tc) - { - switch (tc) { - case TypeCode.Char: - return checked (-Convert.ToChar (a)); - case TypeCode.Byte: - return checked (-Convert.ToByte (a)); - case TypeCode.Decimal: - return checked (-Convert.ToDecimal (a)); - case TypeCode.Double: - return checked (-Convert.ToDouble (a)); - case TypeCode.Int16: - return checked (-Convert.ToInt16 (a)); - case TypeCode.Int32: - return checked (-Convert.ToInt32 (a)); - case TypeCode.Int64: - return checked (-Convert.ToInt64 (a)); - case TypeCode.UInt16: - return checked (-Convert.ToUInt16 (a)); - case TypeCode.UInt32: - return checked (-Convert.ToUInt32 (a)); - case TypeCode.SByte: - return checked (-Convert.ToSByte (a)); - case TypeCode.Single: - return checked (-Convert.ToSingle (a)); - } - - throw new NotImplementedException (); - } - - static object CreateInstance (Type type, params object [] arguments) - { - return type.GetConstructor ( - (from argument in arguments select argument.GetType ()).ToArray ()).Invoke (arguments); - } - - public static object ConvertToTypeChecked (object a, Type fromType, Type toType) - { - if (toType.IsNullable ()) - return a == null ? a : CreateInstance (toType, - ConvertToTypeChecked (a, fromType.GetNotNullableType (), toType.GetNotNullableType ())); - - if (a == null) { - if (!toType.IsValueType) - return a; - if (fromType.IsNullable ()) - throw new InvalidOperationException ("Nullable object must have a value"); - } - - if (IsType (toType, a)) { - return a; - } - - if (Expression.IsPrimitiveConversion (fromType, toType)) - return Convert.ChangeType (a, toType, CultureInfo.CurrentCulture); - - throw new NotImplementedException ( - string.Format ("No Convert defined for type {0} ", toType)); - } - - public static object ConvertToTypeUnchecked (object a, Type fromType, Type toType) - { - if (toType.IsNullable ()) - return a == null ? a : CreateInstance (toType, - ConvertToTypeUnchecked (a, fromType.GetNotNullableType (), toType.GetNotNullableType ())); - - if (a == null) { - if (!toType.IsValueType) - return a; - if (fromType.IsNullable ()) - throw new InvalidOperationException ("Nullable object must have a value"); - } - - if (IsType (toType, a)) - return a; - - if (Expression.IsPrimitiveConversion (fromType, toType)) - return Conversion.ConvertPrimitiveUnChecked (fromType, toType, a); - - throw new NotImplementedException ( - string.Format ("No Convert defined for type {0} ", toType)); - } - - public static bool IsType (Type t, Object o) - { - return t.IsInstanceOfType (o); - } - - public static object Negate (object a, TypeCode tc) - { - switch (tc) { - case TypeCode.Char: - return unchecked (-Convert.ToChar (a)); - case TypeCode.Byte: - return unchecked (-Convert.ToByte (a)); - case TypeCode.Decimal: - return unchecked (-Convert.ToDecimal (a)); - case TypeCode.Double: - return unchecked (-Convert.ToDouble (a)); - case TypeCode.Int16: - return unchecked (-Convert.ToInt16 (a)); - case TypeCode.Int32: - return unchecked (-Convert.ToInt32 (a)); - case TypeCode.Int64: - return unchecked (-Convert.ToInt64 (a)); - case TypeCode.UInt16: - return unchecked (-Convert.ToUInt16 (a)); - case TypeCode.UInt32: - return unchecked (-Convert.ToUInt32 (a)); - case TypeCode.SByte: - return unchecked (-Convert.ToSByte (a)); - case TypeCode.Single: - return unchecked (-Convert.ToSingle (a)); - } - - throw new NotImplementedException (); - } - - public static object RightShift (object a, int n, TypeCode tc) - { - switch (tc) { - case TypeCode.Int16: - return Convert.ToInt16 (a) >> n; - case TypeCode.Int32: - return Convert.ToInt32 (a) >> n; - case TypeCode.Int64: - return Convert.ToInt64 (a) >> n; - case TypeCode.UInt16: - return Convert.ToUInt16 (a) >> n; - case TypeCode.UInt32: - return Convert.ToUInt32 (a) >> n; - case TypeCode.UInt64: - return Convert.ToUInt64 (a) >> n; - } - - throw new NotImplementedException (); - } - - public static object LeftShift (object a, int n, TypeCode tc) - { - switch (tc) { - case TypeCode.Int16: - return Convert.ToInt16 (a) << n; - case TypeCode.Int32: - return Convert.ToInt32 (a) << n; - case TypeCode.Int64: - return Convert.ToInt64 (a) << n; - case TypeCode.UInt16: - return Convert.ToUInt16 (a) << n; - case TypeCode.UInt32: - return Convert.ToUInt32 (a) << n; - case TypeCode.UInt64: - return Convert.ToUInt64 (a) << n; - } - - throw new NotImplementedException (); - } - - private static Decimal Evaluate (Decimal a, Decimal b, ExpressionType et) - { - switch (et) { - case ExpressionType.Add: - return unchecked (a + b); - case ExpressionType.AddChecked: - return checked (a + b); - case ExpressionType.Subtract: - return unchecked (a - b); - case ExpressionType.SubtractChecked: - return checked (a - b); - case ExpressionType.Multiply: - return unchecked (a * b); - case ExpressionType.MultiplyChecked: - return checked (a * b); - case ExpressionType.Divide: - return a / b; - case ExpressionType.Modulo: - return a % b; - - } - - throw new NotImplementedException (); - } - - private static Double Evaluate (Double a, Double b, ExpressionType et) - { - switch (et) { - case ExpressionType.Add: - return unchecked (a + b); - case ExpressionType.AddChecked: - return checked (a + b); - case ExpressionType.Subtract: - return unchecked (a - b); - case ExpressionType.SubtractChecked: - return checked (a - b); - case ExpressionType.Multiply: - return unchecked (a * b); - case ExpressionType.MultiplyChecked: - return checked (a * b); - case ExpressionType.Divide: - return a / b; - case ExpressionType.Modulo: - return a % b; - case ExpressionType.Power: - return System.Math.Pow (a, b); - } - - throw new NotImplementedException (); - - } - - private static Int32 Evaluate (Int16 a, Int16 b, ExpressionType et) - { - switch (et) { - case ExpressionType.Add: - return unchecked (a + b); - case ExpressionType.AddChecked: - return checked (a + b); - case ExpressionType.Subtract: - return unchecked (a - b); - case ExpressionType.SubtractChecked: - return checked (a - b); - case ExpressionType.Multiply: - return unchecked (a * b); - case ExpressionType.MultiplyChecked: - return checked (a * b); - case ExpressionType.Divide: - return a / b; - case ExpressionType.Modulo: - return a % b; - case ExpressionType.ExclusiveOr: - return a ^ b; - case ExpressionType.And: - return a & b; - case ExpressionType.Or: - return a | b; - } - - throw new NotImplementedException (); - } - - private static Int32 Evaluate (Int32 a, Int32 b, ExpressionType et) - { - switch (et) { - case ExpressionType.Add: - return unchecked (a + b); - case ExpressionType.AddChecked: - return checked (a + b); - case ExpressionType.Subtract: - return unchecked (a - b); - case ExpressionType.SubtractChecked: - return checked (a - b); - case ExpressionType.Multiply: - return unchecked (a * b); - case ExpressionType.MultiplyChecked: - return checked (a * b); - case ExpressionType.Divide: - return a / b; - case ExpressionType.Modulo: - return a % b; - case ExpressionType.ExclusiveOr: - return a ^ b; - case ExpressionType.And: - return a & b; - case ExpressionType.Or: - return a | b; - } - - throw new NotImplementedException (); - } - - private static Int64 Evaluate (Int64 a, Int64 b, ExpressionType et) - { - switch (et) { - case ExpressionType.Add: - return unchecked (a + b); - case ExpressionType.AddChecked: - return checked (a + b); - case ExpressionType.Subtract: - return unchecked (a - b); - case ExpressionType.SubtractChecked: - return checked (a - b); - case ExpressionType.Multiply: - return unchecked (a * b); - case ExpressionType.MultiplyChecked: - return checked (a * b); - case ExpressionType.Divide: - return a / b; - case ExpressionType.Modulo: - return a % b; - case ExpressionType.ExclusiveOr: - return a ^ b; - case ExpressionType.And: - return a & b; - case ExpressionType.Or: - return a | b; - } - - throw new NotImplementedException (); - } - - private static Int32 Evaluate (UInt16 a, UInt16 b, ExpressionType et) - { - switch (et) { - case ExpressionType.Add: - return unchecked (a + b); - case ExpressionType.AddChecked: - return checked (a + b); - case ExpressionType.Subtract: - return unchecked (a - b); - case ExpressionType.SubtractChecked: - return checked ((UInt16) (a - b)); - case ExpressionType.Multiply: - return unchecked (a * b); - case ExpressionType.MultiplyChecked: - return checked (a * b); - case ExpressionType.Divide: - return a / b; - case ExpressionType.Modulo: - return a % b; - case ExpressionType.ExclusiveOr: - return a ^ b; - case ExpressionType.And: - return a & b; - case ExpressionType.Or: - return a | b; - } - - throw new NotImplementedException (); - } - - private static UInt32 Evaluate (UInt32 a, UInt32 b, ExpressionType et) - { - switch (et) { - case ExpressionType.Add: - return unchecked (a + b); - case ExpressionType.AddChecked: - return checked (a + b); - case ExpressionType.Subtract: - return unchecked (a - b); - case ExpressionType.SubtractChecked: - return checked (a - b); - case ExpressionType.Multiply: - return unchecked (a * b); - case ExpressionType.MultiplyChecked: - return checked (a * b); - case ExpressionType.Divide: - return a / b; - case ExpressionType.Modulo: - return a % b; - case ExpressionType.ExclusiveOr: - return a ^ b; - case ExpressionType.And: - return a & b; - case ExpressionType.Or: - return a | b; - } - - throw new NotImplementedException (); - } - - private static UInt64 Evaluate (UInt64 a, UInt64 b, ExpressionType et) - { - switch (et) { - case ExpressionType.Add: - return unchecked (a + b); - case ExpressionType.AddChecked: - return checked (a + b); - case ExpressionType.Subtract: - return unchecked (a - b); - case ExpressionType.SubtractChecked: - return checked (a - b); - case ExpressionType.Multiply: - return unchecked (a * b); - case ExpressionType.MultiplyChecked: - return checked (a * b); - case ExpressionType.Divide: - return a / b; - case ExpressionType.Modulo: - return a % b; - case ExpressionType.ExclusiveOr: - return a ^ b; - case ExpressionType.And: - return a & b; - case ExpressionType.Or: - return a | b; - } - - throw new NotImplementedException (); - } - - private static object Evaluate (Char a, Char b, ExpressionType et) - { - switch (et) { - case ExpressionType.ExclusiveOr: - return a ^ b; - case ExpressionType.And: - return a & b; - case ExpressionType.Or: - return a | b; - } - - throw new NotImplementedException (); - } - - private static Int32 Evaluate (SByte a, SByte b, ExpressionType et) - { - switch (et) { - case ExpressionType.Add: - return unchecked (a + b); - case ExpressionType.AddChecked: - return checked (a + b); - case ExpressionType.Subtract: - return unchecked (a - b); - case ExpressionType.SubtractChecked: - return checked (a - b); - case ExpressionType.Multiply: - return unchecked (a * b); - case ExpressionType.MultiplyChecked: - return checked (a * b); - case ExpressionType.Divide: - return a / b; - case ExpressionType.Modulo: - return a % b; - case ExpressionType.ExclusiveOr: - return a ^ b; - case ExpressionType.And: - return a & b; - case ExpressionType.Or: - return a | b; - } - - throw new NotImplementedException (); - } - - private static Int32 Evaluate (Byte a, Byte b, ExpressionType et) - { - switch (et) { - case ExpressionType.Add: - return unchecked (a + b); - case ExpressionType.AddChecked: - return checked (a + b); - case ExpressionType.Subtract: - return unchecked (a - b); - case ExpressionType.SubtractChecked: - return checked (a - b); - case ExpressionType.Multiply: - return unchecked (a * b); - case ExpressionType.MultiplyChecked: - return checked (a * b); - case ExpressionType.Divide: - return a / b; - case ExpressionType.Modulo: - return a % b; - case ExpressionType.ExclusiveOr: - return a ^ b; - case ExpressionType.And: - return a & b; - case ExpressionType.Or: - return a | b; - } - - throw new NotImplementedException (); - } - - private static Single Evaluate (Single a, Single b, ExpressionType et) - { - switch (et) { - case ExpressionType.Add: - return unchecked (a + b); - case ExpressionType.AddChecked: - return checked (a + b); - case ExpressionType.Subtract: - return unchecked (a - b); - case ExpressionType.SubtractChecked: - return checked (a - b); - case ExpressionType.Multiply: - return unchecked (a * b); - case ExpressionType.MultiplyChecked: - return checked (a * b); - case ExpressionType.Divide: - return a / b; - case ExpressionType.Modulo: - return a % b; - } - - throw new NotImplementedException (); - } - - private static bool Evaluate (bool a, bool b, ExpressionType et) - { - switch (et) { - case ExpressionType.ExclusiveOr: - return a ^ b; - case ExpressionType.And: - return a & b; - case ExpressionType.Or: - return a | b; - } - - throw new NotImplementedException (); - } - } -} diff --git a/mcs/class/System.Core/System.Linq.jvm/Runner.cs b/mcs/class/System.Core/System.Linq.jvm/Runner.cs deleted file mode 100644 index 31f02100a2..0000000000 --- a/mcs/class/System.Core/System.Linq.jvm/Runner.cs +++ /dev/null @@ -1,227 +0,0 @@ -// -// Runner.cs -// -// (C) 2008 Mainsoft, Inc. (http://www.mainsoft.com) -// (C) 2008 db4objects, Inc. (http://www.db4o.com) -// (C) 2010 Novell, Inc. (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// - -using System.Linq.Expressions; -using System.Reflection; - -namespace System.Linq.jvm { - - sealed class Runner { - - sealed class VoidTypeMarker {} - - static readonly Type VoidMarker = typeof (VoidTypeMarker); - static readonly MethodInfo [] delegates = new MethodInfo [5]; - const BindingFlags method_flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance; - - readonly LambdaExpression lambda; - readonly ExpressionInterpreter interpreter; - - static Runner () - { - foreach (var method in typeof (Runner).GetMethods (method_flags).Where (m => m.Name == "GetDelegate")) - delegates [method.GetGenericArguments ().Length - 1] = method; - } - - public Runner (LambdaExpression lambda) - { - this.lambda = lambda; - } - - public Runner (LambdaExpression lambda, ExpressionInterpreter interpreter) - { - this.lambda = lambda; - this.interpreter = interpreter; - } - - public Delegate CreateDelegate () - { - var types = GetGenericSignature (); - var creator = delegates [types.Length - 1].MakeGenericMethod (types); - - return (Delegate) creator.Invoke (this, new object [0]); - } - - Type [] GetGenericSignature () - { - var count = lambda.Parameters.Count; - var types = new Type [count + 1]; - - var return_type = lambda.GetReturnType (); - if (return_type == typeof (void)) - return_type = VoidMarker; - - types [count] = return_type; - for (int i = 0; i < count; i++) { - types [i] = lambda.Parameters [i].Type; - } - - return types; - } - - object Run (object [] arguments) - { - var interpreter = this.interpreter ?? new ExpressionInterpreter (lambda); - - return interpreter.Interpret (lambda, arguments); - } - - MethodInfo GetActionRunner (params Type [] types) - { - return GetRunner ("ActionRunner", types); - } - - MethodInfo GetFuncRunner (params Type [] types) - { - return GetRunner ("FuncRunner", types); - } - - MethodInfo GetRunner (string name, Type [] type_arguments) - { - var method = GetMethod (name, type_arguments.Length); - if (method == null) - throw new InvalidOperationException (); - - if (type_arguments.Length == 0) - return method; - - return method.MakeGenericMethod (type_arguments); - } - - MethodInfo GetMethod (string name, int parameters) - { - foreach (var method in GetType ().GetMethods (method_flags)) { - if (method.Name != name) - continue; - - if (method.GetGenericArguments ().Length != parameters) - continue; - - return method; - } - - return null; - } - - Delegate CreateDelegate (MethodInfo runner) - { - return Delegate.CreateDelegate (lambda.Type, this, runner); - } - - // all methods below are called through reflection - - Delegate GetDelegate<TResult> () - { - if (typeof (TResult) == VoidMarker) - return CreateDelegate (GetActionRunner (Type.EmptyTypes)); - - return CreateDelegate (GetFuncRunner (typeof (TResult))); - } - - public TResult FuncRunner<TResult> () - { - return (TResult) Run (new object [0]); - } - - public void ActionRunner () - { - Run (new object [0]); - } - - Delegate GetDelegate<T, TResult> () - { - if (typeof (TResult) == VoidMarker) - return CreateDelegate (GetActionRunner (typeof (T))); - - return CreateDelegate (GetFuncRunner (typeof (T), typeof (TResult))); - } - - public TResult FuncRunner<T, TResult> (T arg) - { - return (TResult) Run (new object [] { arg }); - } - - public void ActionRunner<T> (T arg) - { - Run (new object [] { arg }); - } - - public Delegate GetDelegate<T1, T2, TResult> () - { - if (typeof (TResult) == VoidMarker) - return CreateDelegate (GetActionRunner (typeof (T1), typeof (T2))); - - return CreateDelegate (GetFuncRunner (typeof (T1), typeof (T2), typeof (TResult))); - } - - public TResult FuncRunner<T1, T2, TResult> (T1 arg1, T2 arg2) - { - return (TResult) Run (new object [] { arg1, arg2 }); - } - - public void ActionRunner<T1, T2> (T1 arg1, T2 arg2) - { - Run (new object [] { arg1, arg2 }); - } - - Delegate GetDelegate<T1, T2, T3, TResult> () - { - if (typeof (TResult) == VoidMarker) - return CreateDelegate (GetActionRunner (typeof (T1), typeof (T2), typeof (T3))); - - return CreateDelegate (GetFuncRunner (typeof (T1), typeof (T2), typeof (T3), typeof (TResult))); - } - - public TResult FuncRunner<T1, T2, T3, TResult> (T1 arg1, T2 arg2, T3 arg3) - { - return (TResult) Run (new object [] { arg1, arg2, arg3 }); - } - - public void ActionRunner<T1, T2, T3> (T1 arg1, T2 arg2, T3 arg3) - { - Run (new object [] { arg1, arg2, arg3 }); - } - - Delegate GetDelegate<T1, T2, T3, T4, TResult> () - { - if (typeof (TResult) == VoidMarker) - return CreateDelegate (GetActionRunner (typeof (T1), typeof (T2), typeof (T3), typeof (T4))); - - return CreateDelegate (GetFuncRunner (typeof (T1), typeof (T2), typeof (T3), typeof (T4), typeof (TResult))); - } - - public TResult FuncRunner<T1, T2, T3, T4, TResult> (T1 arg1, T2 arg2, T3 arg3, T4 arg4) - { - return (TResult) Run (new object [] { arg1, arg2, arg3, arg4 }); - } - - public void ActionRunner<T1, T2, T3, T4> (T1 arg1, T2 arg2, T3 arg3, T4 arg4) - { - Run (new object [] { arg1, arg2, arg3, arg4 }); - } - } -} |