Author: antonello
Date: 2007-03-31 11:40:05 -0500 (Sat, 31 Mar 2007)
New Revision: 75242
Added:
trunk/mcs/class/System.Core/System.Linq.Expressions/LiftExpression.cs
Modified:
trunk/mcs/class/System.Core/
trunk/mcs/class/System.Core/System.Core.dll.sources
trunk/mcs/class/System.Core/System.Linq.Expressions/ChangeLog
trunk/mcs/class/System.Core/System.Linq.Expressions/Expression.cs
trunk/mcs/class/System.Core/System.Linq.Expressions/ExpressionType.cs
trunk/mcs/class/System.Core/System.Linq.Expressions/ExpressionUtil.cs
trunk/mcs/class/System.Core/System.Linq/Enumerable.cs
Log:
Property changes on: trunk/mcs/class/System.Core
___________________________________________________________________
Name: svn:ignore
- bin
obj
System.Core.csproj
System.Core.csproj.user
System.Core.sln
System.Core.suo
System.Core.pidb
System.Core.mdp System.Core.mds System.Core.userprefs System.Core.usertasks
+ bin
obj
System.Core.csproj
System.Core.csproj.user
System.Core.sln
System.Core.suo
System.Core.pidb
System.Core.mdp System.Core.mds System.Core.userprefs System.Core.usertasks
System.Core.csproj System.Core.csproj.user System.Core.sln System.Core.suo
Properties
Modified: trunk/mcs/class/System.Core/System.Core.dll.sources
===================================================================
--- trunk/mcs/class/System.Core/System.Core.dll.sources 2007-03-31 16:34:16 UTC
(rev 75241)
+++ trunk/mcs/class/System.Core/System.Core.dll.sources 2007-03-31 16:40:05 UTC
(rev 75242)
@@ -28,6 +28,7 @@
System.Linq.Expressions/InvocationExpression.cs
System.Linq.Expressions/IStrongBox.cs
System.Linq.Expressions/LambdaExpression.cs
+System.Linq.Expressions/LiftExpression.cs
System.Linq.Expressions/ListInitExpression.cs
System.Linq.Expressions/MemberAssignment.cs
System.Linq.Expressions/MemberBinding.cs
Modified: trunk/mcs/class/System.Core/System.Linq/Enumerable.cs
===================================================================
--- trunk/mcs/class/System.Core/System.Linq/Enumerable.cs 2007-03-31
16:34:16 UTC (rev 75241)
+++ trunk/mcs/class/System.Core/System.Linq/Enumerable.cs 2007-03-31
16:40:05 UTC (rev 75242)
@@ -2616,7 +2616,7 @@
#endregion
- #region ToArray
+ #region ToArray
public static T[] ToArray<T> (this IEnumerable<T> source)
{
if (source == null)
Modified: trunk/mcs/class/System.Core/System.Linq.Expressions/ChangeLog
===================================================================
--- trunk/mcs/class/System.Core/System.Linq.Expressions/ChangeLog
2007-03-31 16:34:16 UTC (rev 75241)
+++ trunk/mcs/class/System.Core/System.Linq.Expressions/ChangeLog
2007-03-31 16:40:05 UTC (rev 75242)
@@ -1,3 +1,20 @@
+2007-03-29 Antonello Provenzano <[EMAIL PROTECTED]>
+
+ * ExpressionUtil.cs:
+ - Implemented method GetOperator for general pourposes,
+ moving some implementations from Expression.cs specific methods
+ - IsNumber now returns 'true' for sbyte and unsigned integers
+ - Implemented method IsInteger
+ * Expression.cs: Implementation of the method:
+ - AddChecked
+ - Call
+ - Condition
+ - Divide
+ - LeftShift
+ - Quote
+ * ExpressionType.cs: Applied Obsolete attributes
+ * LiftExpression.cs: New file
+
2007-03-27 Antonello Provenzano <[EMAIL PROTECTED]>
* ExpressionUtil.cs: methods 'GetReadOnlyCollection' removed
Modified: trunk/mcs/class/System.Core/System.Linq.Expressions/Expression.cs
===================================================================
--- trunk/mcs/class/System.Core/System.Linq.Expressions/Expression.cs
2007-03-31 16:34:16 UTC (rev 75241)
+++ trunk/mcs/class/System.Core/System.Linq.Expressions/Expression.cs
2007-03-31 16:40:05 UTC (rev 75242)
@@ -63,12 +63,13 @@
if (right == null)
throw new ArgumentNullException("right");
}
+
#endregion
#region Internal Methods
internal virtual void BuildString(StringBuilder builder)
{
- //TODO:
+ builder.Append("[" + nodeType + "]");
}
#endregion
@@ -85,7 +86,13 @@
#region Internal Static Methos
internal static Type GetNonNullableType(Type type)
{
- //TODO:
+ if (IsNullableType(type))
+ {
+ //TODO: check this... should we return just the first argument?
+ Type[] argTypes = type.GetGenericArguments();
+ return argTypes[0];
+ }
+
return type;
}
@@ -93,9 +100,9 @@
{
if (type == null)
throw new ArgumentNullException("type");
+
if (type.IsGenericType)
{
- //TODO: check this...
Type genType = type.GetGenericTypeDefinition();
return typeof(Nullable<>).IsAssignableFrom(genType);
}
@@ -116,27 +123,150 @@
return new BinaryExpression(ExpressionType.Add, left, right,
left.type);
if (method == null)
+ method = ExpressionUtil.GetOperatorMethod("op_Addition",
left.type, right.type);
+
+ // ok if even op_Addition is not defined we need to throw an
exception...
+ if (method == null)
+ throw new InvalidOperationException();
+
+ return new BinaryExpression(ExpressionType.Add, left, right,
method, method.ReturnType);
+ }
+
+ public static BinaryExpression Add(Expression left, Expression right)
+ {
+ return Add(left, right, null);
+ }
+
+ public static BinaryExpression AddChecked(Expression left, Expression
right, MethodInfo method)
+ {
+ CheckLeftRight(left, right);
+
+ // sine both the expressions define the same numeric type we don't
have
+ // to look for the "op_Addition" method...
+ if (left.type == right.type &&
+ ExpressionUtil.IsNumber(left.type))
+ return new BinaryExpression(ExpressionType.AddChecked, left,
right, left.type);
+
+ if (method == null)
{
- method = ExpressionUtil.GetMethod("op_Addition", new Type[] {
left.type, right.type });
+ // in the case of a non-specified method we have to 'check'
+ method = ExpressionUtil.GetOperatorMethod("op_Addition",
left.type, right.type);
+ if (method == null)
+ throw new InvalidOperationException();
+
+ if (method.ReturnType.IsValueType &&
!IsNullableType(method.ReturnType))
+ {
+ Type retType = method.ReturnType;
+ if (retType != typeof(bool))
+ {
+ // in case the returned type is not a boolean
+ // we want to use a nullable version of the type...
+ if (IsNullableType(retType))
+ {
+ Type[] genTypes = retType.GetGenericArguments();
+ if (genTypes.Length > 1) //TODO: should we just
ignore it if is it an array greater
+ // than 1?
+ throw new InvalidOperationException();
+ retType = genTypes[0];
+ }
+
+ retType = ExpressionUtil.GetNullable(retType);
+ }
+ return new BinaryExpression(ExpressionType.AddChecked,
left, right, method, retType);
+ }
}
- if (method != null)
- {
- if (method.ReturnType == null) // it returns a void
+ return new BinaryExpression(ExpressionType.AddChecked, left,
right, method, method.ReturnType);
+ }
+
+ public static BinaryExpression AddChecked(Expression left, Expression
right)
+ {
+ return AddChecked(left, right, null);
+ }
+
+ public static BinaryExpression And(Expression left, Expression right,
MethodInfo method)
+ {
+ CheckLeftRight(left, right);
+
+ // sine both the expressions define the same numeric type or is a
boolean
+ // we don't have to look for the "op_BitwiseAnd" method...
+ if (left.type == right.type &&
+ (ExpressionUtil.IsInteger(left.type) || left.type ==
typeof(bool)))
+ return new BinaryExpression(ExpressionType.AddChecked, left,
right, left.type);
+
+ if (method == null)
+ method = ExpressionUtil.GetOperatorMethod("op_BitwiseAnd",
left.type, right.type);
+
+ // ok if even op_BitwiseAnd is not defined we need to throw an
exception...
+ if (method == null)
+ throw new InvalidOperationException();
+
+ return new BinaryExpression(ExpressionType.And, left, right,
method, method.ReturnType);
+ }
+
+ public static BinaryExpression And(Expression left, Expression right)
+ {
+ return And(left, right, null);
+ }
+
+ public static MethodCallExpression Call(Expression instance,
MethodInfo method)
+ {
+ return Call(instance, method, (Expression[])null);
+ }
+
+ public static MethodCallExpression Call(MethodInfo method, params
Expression[] arguments)
+ {
+ return Call(null, method,
Enumerable.ToReadOnlyCollection<Expression>(arguments));
+ }
+
+ public static MethodCallExpression Call(Expression instance,
MethodInfo method, params Expression[] arguments)
+ {
+ return Call(instance, method,
Enumerable.ToReadOnlyCollection<Expression>(arguments));
+ }
+
+ public static MethodCallExpression Call(Expression instance,
MethodInfo method, IEnumerable<Expression> arguments)
+ {
+ if (arguments == null)
+ throw new ArgumentNullException("arguments");
+ if (method == null)
+ throw new ArgumentNullException("method");
+ if (method.IsGenericMethodDefinition)
throw new ArgumentException();
- if (!method.IsStatic)
+ if (method.ContainsGenericParameters)
throw new ArgumentException();
- ParameterInfo[] pars = method.GetParameters();
- if (pars.Length != 2)
- throw new ArgumentException();
+ if (!method.IsStatic && instance == null)
+ throw new ArgumentNullException("instance");
+ if (instance != null &&
!instance.type.IsAssignableFrom(method.DeclaringType))
+ throw new ArgumentException();
+
+ ReadOnlyCollection<Expression> roArgs =
Enumerable.ToReadOnlyCollection<Expression>(arguments);
+
+ ParameterInfo[] pars = method.GetParameters();
+ if (Enumerable.Count<Expression>(arguments) != pars.Length)
+ throw new ArgumentException();
+
+ if (pars.Length > 0)
+ {
+ //TODO: validate the parameters against the arguments...
}
- return new BinaryExpression(ExpressionType.Add, left, right,
method, method.ReturnType);
+ return new MethodCallExpression(ExpressionType.Call, method,
instance, roArgs);
}
- public static BinaryExpression Add(Expression left, Expression right)
+ public static ConditionalExpression Condition(Expression test,
Expression ifTrue, Expression ifFalse)
{
- return Add(left, right, null);
+ if (test == null)
+ throw new ArgumentNullException("test");
+ if (ifTrue == null)
+ throw new ArgumentNullException("ifTrue");
+ if (ifFalse == null)
+ throw new ArgumentNullException("ifFalse");
+ if (test.type != typeof(bool))
+ throw new ArgumentException();
+ if (ifTrue.type != ifFalse.type)
+ throw new ArgumentException();
+
+ return new ConditionalExpression(test, ifTrue, ifFalse,
ifTrue.type);
}
public static ConstantExpression Constant(object value)
@@ -157,6 +287,31 @@
return new ConstantExpression(value, type);
}
+ public static BinaryExpression Divide(Expression left, Expression
right)
+ {
+ return Divide(left, right, null);
+ }
+
+ public static BinaryExpression Divide(Expression left, Expression
right, MethodInfo method)
+ {
+ CheckLeftRight(left, right);
+
+ // sine both the expressions define the same numeric type we don't
have
+ // to look for the "op_Division" method...
+ if (left.type == right.type &&
+ ExpressionUtil.IsNumber(left.type))
+ return new BinaryExpression(ExpressionType.Divide, left,
right, left.type);
+
+ if (method == null)
+ method = ExpressionUtil.GetOperatorMethod("op_Division",
left.type, right.type);
+
+ // ok if even op_Division is not defined we need to throw an
exception...
+ if (method == null)
+ throw new InvalidOperationException();
+
+ return new BinaryExpression(ExpressionType.Divide, left, right,
method, method.ReturnType);
+ }
+
public static MemberExpression Field(Expression expression, FieldInfo
field)
{
if (field == null)
@@ -197,23 +352,36 @@
return typeof(Func<,,,,>).MakeGenericType(typeArgs);
}
- internal static ReadOnlyCollection<TSource>
ToReadOnlyCollection<TSource>(IEnumerable<TSource> source)
+ public static BinaryExpression LeftShift(Expression left, Expression
right, MethodInfo method)
{
- if (source == null)
- return new ReadOnlyCollection<TSource>(new List<TSource>());
+ CheckLeftRight(left, right);
- if (typeof(ReadOnlyCollection<TSource>).IsInstanceOfType(source))
- return source as ReadOnlyCollection<TSource>;
+ // since the left expression is of an integer type and the right
is of
+ // an integer we don't have to look for the "op_LeftShift"
method...
+ if (ExpressionUtil.IsInteger(left.type) && right.type ==
typeof(int))
+ return new BinaryExpression(ExpressionType.LeftShift, left,
right, left.type);
- return new
ReadOnlyCollection<TSource>(System.Linq.Enumerable.ToArray<TSource>(source));
+ if (method == null)
+ method = ExpressionUtil.GetOperatorMethod("op_LeftShift",
left.type, right.type);
+
+ // ok if even op_Division is not defined we need to throw an
exception...
+ if (method == null)
+ throw new InvalidOperationException();
+
+ return new BinaryExpression(ExpressionType.LeftShift, left, right,
method, method.ReturnType);
}
+ public static BinaryExpression LeftShift(Expression left, Expression
right)
+ {
+ return LeftShift(left, right, null);
+ }
+
public static ListInitExpression ListInit(NewExpression newExpression,
params Expression[] initializers)
{
if (initializers == null)
throw new ArgumentNullException("inizializers");
- return ListInit(newExpression,
ToReadOnlyCollection<Expression>(initializers));
+ return ListInit(newExpression,
Enumerable.ToReadOnlyCollection<Expression>(initializers));
}
public static ListInitExpression ListInit(NewExpression newExpression,
IEnumerable<Expression> initializers)
@@ -223,7 +391,7 @@
if (initializers == null)
throw new ArgumentNullException("inizializers");
- return new ListInitExpression(newExpression,
ToReadOnlyCollection<Expression>(initializers));
+ return new ListInitExpression(newExpression,
Enumerable.ToReadOnlyCollection<Expression>(initializers));
}
public static MemberInitExpression MemberInit(NewExpression
newExpression, IEnumerable<MemberBinding> bindings)
@@ -234,7 +402,7 @@
if (bindings == null)
throw new ArgumentNullException("bindings");
- return new MemberInitExpression(newExpression,
ToReadOnlyCollection<MemberBinding>(bindings));
+ return new MemberInitExpression(newExpression,
Enumerable.ToReadOnlyCollection<MemberBinding>(bindings));
}
public static MemberExpression Property(Expression expression,
PropertyInfo property)
@@ -250,6 +418,15 @@
return new MemberExpression(expression, property,
property.PropertyType);
}
+ public static UnaryExpression Quote(Expression expression)
+ {
+ if (expression == null)
+ throw new ArgumentNullException("expression");
+
+ return new UnaryExpression(ExpressionType.Quote, expression,
expression.GetType());
+ }
+
+
public static MemberExpression Property(Expression expression, string
propertyName)
{
if (expression == null)
@@ -281,16 +458,7 @@
throw new ArgumentException();
}
- public static UnaryExpression Quote(Expression expression)
- {
- if (expression == null)
- throw new ArgumentNullException("expression");
- //TODO: is this right?
- return new UnaryExpression(ExpressionType.Quote, expression,
expression.GetType());
- }
-
-
public static TypeBinaryExpression TypeIs(Expression expression, Type
type)
{
if (expression == null)
Modified: trunk/mcs/class/System.Core/System.Linq.Expressions/ExpressionType.cs
===================================================================
--- trunk/mcs/class/System.Core/System.Linq.Expressions/ExpressionType.cs
2007-03-31 16:34:16 UTC (rev 75241)
+++ trunk/mcs/class/System.Core/System.Linq.Expressions/ExpressionType.cs
2007-03-31 16:40:05 UTC (rev 75242)
@@ -29,7 +29,9 @@
ArrayIndex,
ArrayLength,
Call,
+ [Obsolete]
CallVirtual,
+ [Obsolete]
Cast,
Coalesce,
Conditional,
@@ -47,10 +49,15 @@
LeftShift,
LessThan,
LessThanOrEqual,
+ [Obsolete]
Lift,
+ [Obsolete]
LiftEqual,
+ [Obsolete]
LiftFalse,
+ [Obsolete]
LiftNotEqual,
+ [Obsolete]
LiftTrue,
ListInit,
MemberAccess,
Modified: trunk/mcs/class/System.Core/System.Linq.Expressions/ExpressionUtil.cs
===================================================================
--- trunk/mcs/class/System.Core/System.Linq.Expressions/ExpressionUtil.cs
2007-03-31 16:34:16 UTC (rev 75241)
+++ trunk/mcs/class/System.Core/System.Linq.Expressions/ExpressionUtil.cs
2007-03-31 16:40:05 UTC (rev 75242)
@@ -7,33 +7,74 @@
{
static class ExpressionUtil
{
- public static bool IsNumber(Type type)
+ private static TypeCode GetTypeCode(Type type)
{
- // enum can also be in a numeric form, but we
- // want it to be considered as a number?
- if (type.IsEnum)
+ if (Expression.IsNullableType(type))
+ type = Expression.GetNonNullableType(type);
+
+ return Type.GetTypeCode(type);
+ }
+
+ private static bool IsNumber(TypeCode typeCode)
+ {
+ if (IsInteger(typeCode) ||
+ typeCode == TypeCode.Single ||
+ typeCode == TypeCode.Double ||
+ typeCode == TypeCode.Decimal)
+ return true;
+
return false;
+ }
- TypeCode typeCode = Type.GetTypeCode(type);
+ private static bool IsInteger(TypeCode typeCode)
+ {
if (typeCode == TypeCode.Byte ||
- typeCode == TypeCode.Decimal ||
- typeCode == TypeCode.Double ||
+ typeCode == TypeCode.SByte ||
typeCode == TypeCode.Int16 ||
+ typeCode == TypeCode.UInt16 ||
typeCode == TypeCode.Int32 ||
+ typeCode == TypeCode.UInt32 ||
typeCode == TypeCode.Int64 ||
- typeCode == TypeCode.Single)
+ typeCode == TypeCode.UInt64)
return true;
return false;
}
+ public static bool IsNumber(Type type)
+ {
+ // fast exit: enum is not numeric
+ if (type.IsEnum)
+ return false;
+
+ return IsNumber(GetTypeCode(type));
+ }
+
+ public static bool IsInteger(Type type)
+ {
+ // fast exit: enum is not numeric
+ if (type.IsEnum)
+ return false;
+
+ return IsInteger(GetTypeCode(type));
+ }
+
public static bool IsNumber(object value)
{
if (value == null)
return false;
+
return IsNumber(value.GetType());
}
+ public static bool IsInteger(object value)
+ {
+ if (value == null)
+ return false;
+
+ return IsInteger(value.GetType());
+ }
+
/// <summary>
/// tries to find the method with the given name in one of the
/// given types in the array.
@@ -55,5 +96,58 @@
return method;
}
+
+ public static MethodInfo GetOperatorMethod(string methodName, Type
left, Type right)
+ {
+ MethodInfo opMethod = GetMethod(methodName, new Type[] { left,
right });
+ if (opMethod == null)
+ {
+ if (Expression.IsNullableType(left) &&
+ Expression.IsNullableType(right))
+ {
+ // they're nullable types: try to refine them and get the
method again...
+ left = Expression.GetNonNullableType(left);
+ right = Expression.GetNonNullableType(right);
+ opMethod = GetMethod(methodName, new Type[] { left, right
});
+ }
+
+ // hope at this point we've found it...
+ return opMethod;
+ }
+
+ if (!opMethod.IsStatic)
+ throw new ArgumentException();
+ if (opMethod.ReturnType == typeof(void))
+ throw new ArgumentException();
+
+ ParameterInfo[] pars = opMethod.GetParameters();
+ if (pars.Length != 2)
+ throw new ArgumentException();
+
+ ParameterInfo p1 = pars[0];
+ ParameterInfo p2 = pars[1];
+ if (p1.ParameterType.IsAssignableFrom(left))
+ throw new ArgumentException();
+ if (p2.ParameterType.IsAssignableFrom (right))
+ throw new ArgumentException();
+
+ // check for the nullability of the types...
+ if (Expression.IsNullableType(p1.ParameterType) &&
+ !Expression.IsNullableType(left))
+ throw new InvalidOperationException();
+
+ if (Expression.IsNullableType(p2.ParameterType) &&
+ !Expression.IsNullableType(right))
+ throw new InvalidOperationException();
+
+ return opMethod;
+ }
+
+ public static Type GetNullable(Type type)
+ {
+ if (Expression.IsNullableType(type))
+ return type;
+ return typeof(Nullable<>).MakeGenericType(type);
+ }
}
}
Added: trunk/mcs/class/System.Core/System.Linq.Expressions/LiftExpression.cs
===================================================================
--- trunk/mcs/class/System.Core/System.Linq.Expressions/LiftExpression.cs
2007-03-31 16:34:16 UTC (rev 75241)
+++ trunk/mcs/class/System.Core/System.Linq.Expressions/LiftExpression.cs
2007-03-31 16:40:05 UTC (rev 75242)
@@ -0,0 +1,88 @@
+// 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
+//
+// Authors:
+// Antonello Provenzano <[EMAIL PROTECTED]>
+//
+
+using System.Collections.ObjectModel;
+using System.Text;
+
+namespace System.Linq.Expressions
+{
+ [Obsolete]
+ public sealed class LiftExpression : Expression
+ {
+ #region .ctor
+ internal LiftExpression(ExpressionType eType, Type resultType,
Expression expression, ReadOnlyCollection<ParameterExpression> parameters,
ReadOnlyCollection<Expression> arguments)
+ : base(eType, resultType)
+ {
+ this.expression = expression;
+ this.parameters = parameters;
+ this.arguments = arguments;
+ }
+ #endregion
+
+ #region Fields
+ private ReadOnlyCollection<Expression> arguments;
+ private Expression expression;
+ private ReadOnlyCollection<ParameterExpression> parameters;
+ #endregion
+
+ #region Properties
+ public ReadOnlyCollection<Expression> Arguments
+ {
+ get { return arguments; }
+ }
+
+ public Expression Expression
+ {
+ get { return expression; }
+ }
+
+ public ReadOnlyCollection<ParameterExpression> Parameters
+ {
+ get { return parameters; }
+ }
+ #endregion
+
+ #region Internal Methods
+ internal override void BuildString(StringBuilder builder)
+ {
+ builder.Append(NodeType);
+ builder.Append("(");
+
+ int parCount = parameters.Count;
+ for (int i = 0; i < parCount; i++)
+ {
+ parameters[i].BuildString(builder);
+ builder.Append("=");
+ arguments[i].BuildString(builder);
+
+ if (i < parCount - 1)
+ builder.Append(", ");
+ }
+
+ builder.Append(", ");
+ expression.BuildString(builder);
+
+ builder.Append(")");
+ }
+ #endregion
+ }
+}
\ No newline at end of file
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches