2008-06-05 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System.Core / System.Linq.Expressions / Expression.cs
index 0e9e3fffa87ba3858c63ebc19a0d6e1301e3d072..98646167e2c533aa9c355e44c4c5ee3a470c38c7 100644 (file)
@@ -42,11 +42,12 @@ namespace System.Linq.Expressions {
                ExpressionType node_type;
                Type type;
 
-               const BindingFlags PublicInstance = BindingFlags.Public | BindingFlags.Instance;
-               const BindingFlags PublicStatic = BindingFlags.Public | BindingFlags.Static;
-               const BindingFlags AllInstance = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
-               const BindingFlags AllStatic = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
-               const BindingFlags All = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
+               internal const BindingFlags PublicInstance = BindingFlags.Public | BindingFlags.Instance;
+               internal const BindingFlags NonPublicInstance = BindingFlags.NonPublic | BindingFlags.Instance;
+               internal const BindingFlags PublicStatic = BindingFlags.Public | BindingFlags.Static;
+               internal const BindingFlags AllInstance = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
+               internal const BindingFlags AllStatic = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
+               internal const BindingFlags All = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
 
                public ExpressionType NodeType {
                        get { return node_type; }
@@ -76,7 +77,7 @@ namespace System.Linq.Expressions {
 
                static MethodInfo GetUnaryOperator (string oper_name, Type declaring, Type param, Type ret)
                {
-                       var methods = declaring.GetMethods (PublicStatic);
+                       var methods = GetNotNullableOf (declaring).GetMethods (PublicStatic);
 
                        foreach (var method in methods) {
                                if (method.Name != oper_name)
@@ -86,7 +87,7 @@ namespace System.Linq.Expressions {
                                if (parameters.Length != 1)
                                        continue;
 
-                               if (!IsAssignableToParameterType (param, parameters [0]))
+                               if (!IsAssignableToParameterType (GetNotNullableOf (param), parameters [0]))
                                        continue;
 
                                if (ret != null && method.ReturnType != GetNotNullableOf (ret))
@@ -100,7 +101,11 @@ namespace System.Linq.Expressions {
 
                static bool IsAssignableToParameterType (Type type, ParameterInfo param)
                {
-                       return GetNotNullableOf (type).IsAssignableTo (param.ParameterType);
+                       var ptype = param.ParameterType;
+                       if (ptype.IsByRef)
+                               ptype = ptype.GetElementType ();
+
+                       return GetNotNullableOf (type).IsAssignableTo (ptype);
                }
 
                static MethodInfo CheckUnaryMethod (MethodInfo method, Type param)
@@ -116,7 +121,7 @@ namespace System.Linq.Expressions {
                        if (parameters.Length != 1)
                                throw new ArgumentException ("Must have only one parameters", "method");
 
-                       if (!IsAssignableToParameterType (param, parameters [0]))
+                       if (!IsAssignableToParameterType (GetNotNullableOf (param), parameters [0]))
                                throw new InvalidOperationException ("left-side argument type does not match expression type");
 
                        return method;
@@ -231,12 +236,21 @@ namespace System.Linq.Expressions {
                                                return method;
                                }
 
-                               //
-                               // == and != allow reference types without operators defined.
-                               //
-                               if (!ltype.IsValueType && !rtype.IsValueType &&
-                                       (oper_name == "op_Equality" || oper_name == "op_Inequality"))
-                                       return null;
+                               if (oper_name == "op_Equality" || oper_name == "op_Inequality") {
+                                       //
+                                       // == and != allow reference types without operators defined.
+                                       //
+                                       if (!ltype.IsValueType && !rtype.IsValueType)
+                                               return null;
+
+                                       if (ltype == rtype && ultype == typeof (bool))
+                                               return null;
+                               }
+
+                               if (oper_name == "op_LeftShift" || oper_name == "op_RightShift") {
+                                       if (IsInt (ultype) && urtype == typeof (int))
+                                               return null;
+                               }
 
                                throw new InvalidOperationException (
                                        String.Format ("Operation {0} not defined for {1} and {2}", oper_name != null ? oper_name.Substring (3) : "is", ltype, rtype));
@@ -279,75 +293,109 @@ namespace System.Linq.Expressions {
                static BinaryExpression MakeSimpleBinary (ExpressionType et, Expression left, Expression right, MethodInfo method)
                {
                        bool is_lifted;
+                       Type type;
 
                        if (method == null) {
-                               if (IsNullable (left.Type)) {
-                                       if (!IsNullable (right.Type))
-                                               throw new InvalidOperationException ("Assertion, internal error: left is nullable, requires right to be as well");
+                               is_lifted = left.Type.IsNullable ();
+                               type = left.Type;
+                       } else {
+                               var parameters = method.GetParameters ();
+
+                               var lp = parameters [0];
+                               var rp = parameters [1];
+
+                               if (IsAssignableToOperatorParameter (left, lp) && IsAssignableToOperatorParameter (right, rp)) {
+                                       is_lifted = false;
+                                       type = method.ReturnType;
+                               } else if (left.Type.IsNullable ()
+                                       && right.Type.IsNullable ()
+                                       && GetNotNullableOf (left.Type) == lp.ParameterType
+                                       && GetNotNullableOf (right.Type) == rp.ParameterType
+                                       && !method.ReturnType.IsNullable ()) {
 
                                        is_lifted = true;
+                                       type = method.ReturnType.MakeNullableType ();
                                } else
-                                       is_lifted = false;
-                       } else {
-                               //
-                               // FIXME: implement
-                               //
-                               is_lifted = false;
+                                       throw new InvalidOperationException ();
                        }
 
-                       return new BinaryExpression (et, GetResultType (left, method), left, right, is_lifted, is_lifted, method, null);
+                       return new BinaryExpression (et, type, left, right, is_lifted, is_lifted, method, null);
+               }
+
+               static bool IsAssignableToOperatorParameter (Expression expression, ParameterInfo parameter)
+               {
+                       if (expression.Type == parameter.ParameterType)
+                               return true;
+
+                       if ((!expression.Type.IsNullable () && !parameter.ParameterType.IsNullable ())
+                               && IsAssignableToParameterType (expression.Type, parameter))
+                               return true;
+
+                       return false;
                }
 
                static UnaryExpression MakeSimpleUnary (ExpressionType et, Expression expression, MethodInfo method)
                {
                        bool is_lifted;
+                       Type type;
 
                        if (method == null) {
-                               is_lifted = IsNullable (expression.Type);
+                               type = expression.Type;
+                               is_lifted = type.IsNullable ();
                        } else {
-                               // FIXME: implement
-                               is_lifted = false;
+                               var parameter = method.GetParameters () [0];
+
+                               if (IsAssignableToOperatorParameter (expression, parameter)) {
+                                       is_lifted = false;
+                                       type = method.ReturnType;
+                               } else if (expression.Type.IsNullable ()
+                                       && GetNotNullableOf (expression.Type) == parameter.ParameterType
+                                       && !method.ReturnType.IsNullable ()) {
+
+                                       is_lifted = true;
+                                       type = method.ReturnType.MakeNullableType ();
+                               } else
+                                       throw new InvalidOperationException ();
                        }
 
-                       return new UnaryExpression (et, expression, GetResultType (expression, method), method, is_lifted);
+                       return new UnaryExpression (et, expression, type, method, is_lifted);
                }
 
                static BinaryExpression MakeBoolBinary (ExpressionType et, Expression left, Expression right, bool liftToNull, MethodInfo method)
                {
-                       Type result;
-                       Type ltype = left.Type;
-                       Type rtype = right.Type;
-                       bool lnullable = IsNullable (ltype);
-                       bool rnullable = IsNullable (rtype);
                        bool is_lifted;
+                       Type type;
 
-                       // Implement the rules as described in "Expression.Equal" method.
                        if (method == null) {
-                               if (!lnullable && !rnullable) {
+                               if (!left.Type.IsNullable () && !right.Type.IsNullable ()) {
                                        is_lifted = false;
                                        liftToNull = false;
-                                       result = typeof (bool);
-                               } else if (lnullable && rnullable) {
+                                       type = typeof (bool);
+                               } else if (left.Type.IsNullable () && right.Type.IsNullable ()) {
                                        is_lifted = true;
-                                       result = liftToNull ? typeof(bool?) : typeof (bool);
+                                       type = liftToNull ? typeof (bool?) : typeof (bool);
                                } else
-                                       throw new InvalidOperationException ("Internal error: this should have been caught in BinaryCoreCheck");
+                                       throw new InvalidOperationException ();
                        } else {
-                               ParameterInfo [] pi = method.GetParameters ();
-                               Type mltype = pi [0].ParameterType;
-                               Type mrtype = pi [1].ParameterType;
+                               var parameters = method.GetParameters ();
+
+                               var lp = parameters [0];
+                               var rp = parameters [1];
 
-                               if (ltype == mltype && rtype == mrtype) {
+                               if (IsAssignableToOperatorParameter (left, lp) && IsAssignableToOperatorParameter (right, rp)) {
                                        is_lifted = false;
                                        liftToNull = false;
-                                       result = method.ReturnType;
-                               } else if (ltype.IsValueType && rtype.IsValueType &&
-                                          ((lnullable && GetNullableOf (ltype) == mltype) ||
-                                               (rnullable && GetNullableOf (rtype) == mrtype))){
+                                       type = method.ReturnType;
+                               } else if (left.Type.IsNullable ()
+                                       && right.Type.IsNullable ()
+                                       && GetNotNullableOf (left.Type) == lp.ParameterType
+                                       && GetNotNullableOf (right.Type) == rp.ParameterType) {
+
                                        is_lifted = true;
-                                       if (method.ReturnType == typeof(bool)){
-                                               result = liftToNull ? typeof(bool?) : typeof(bool);
-                                       } else {
+
+                                       if (method.ReturnType == typeof (bool))
+                                               type = liftToNull ? typeof (bool?) : typeof (bool);
+                                       else if (!method.ReturnType.IsNullable ()) {
                                                //
                                                // This behavior is not documented: what
                                                // happens if the result is not typeof(bool), but
@@ -356,16 +404,15 @@ namespace System.Linq.Expressions {
                                                //
                                                // See:
                                                // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=323139
-                                               result = typeof (Nullable<>).MakeGenericType (method.ReturnType);
-                                       }
-                               } else {
-                                       is_lifted = false;
-                                       liftToNull = false;
-                                       result = method.ReturnType;
-                               }
+
+                                               type = method.ReturnType.MakeNullableType ();
+                                       } else
+                                               throw new InvalidOperationException ();
+                               } else
+                                       throw new InvalidOperationException ();
                        }
 
-                       return new BinaryExpression (et, result, left, right, liftToNull, is_lifted, method, null);
+                       return new BinaryExpression (et, type, left, right, liftToNull, is_lifted, method, null);
                }
 
                //
@@ -490,7 +537,7 @@ namespace System.Linq.Expressions {
                {
                        method = BinaryCoreCheck (null, left, right, method);
 
-                       if (left.Type != typeof (double))
+                       if (GetNotNullableOf (left.Type) != typeof (double))
                                throw new InvalidOperationException ("Power only supports double arguments");
 
                        return MakeSimpleBinary (ExpressionType.Power, left, right, method);
@@ -585,6 +632,12 @@ namespace System.Linq.Expressions {
                                // The method should have identical parameter and return types.
                                if (left.Type != right.Type || method.ReturnType != left.Type)
                                        throw new ArgumentException ("left, right and return type must match");
+
+                               var optrue = left.Type.GetMethod ("op_True", AllStatic);
+                               var opfalse = left.Type.GetMethod ("op_False", AllStatic);
+
+                               if (optrue == null || opfalse == null)
+                                       throw new ArgumentException ("Operators true and false are required but not defined");
                        }
 
                        return method;
@@ -720,15 +773,15 @@ namespace System.Linq.Expressions {
                        //
                        // First arg must ne nullable (either Nullable<T> or a reference type
                        //
-                       if (left.Type.IsValueType && !IsNullable (left.Type))
+                       if (left.Type.IsValueType && !left.Type.IsNullable ())
                                throw new InvalidOperationException ("Left expression can never be null");
 
                        Type result = null;
 
-                       if (IsNullable (left.Type)) {
-                               Type lbase = GetNullableOf (left.Type);
+                       if (left.Type.IsNullable ()) {
+                               Type lbase = GetNullableArgumentType (left.Type);
 
-                               if (!IsNullable (right.Type) && right.Type.IsAssignableTo (lbase))
+                               if (!right.Type.IsNullable () && right.Type.IsAssignableTo (lbase))
                                        result = lbase;
                        }
 
@@ -736,7 +789,7 @@ namespace System.Linq.Expressions {
                                result = left.Type;
 
                        if (result == null) {
-                               if (IsNullable (left.Type) && GetNullableOf (left.Type).IsAssignableTo (right.Type))
+                               if (left.Type.IsNullable () && GetNullableArgumentType (left.Type).IsAssignableTo (right.Type))
                                        result = right.Type;
                        }
 
@@ -1054,10 +1107,10 @@ namespace System.Linq.Expressions {
                        // are allowed
                        //
                        if (value == null){
-                               if (type.IsValueType && !IsNullable (type))
+                               if (type.IsValueType && !type.IsNullable ())
                                        throw new ArgumentException ();
                        } else {
-                               if (!(type.IsValueType && IsNullable (type)) && !value.GetType ().IsAssignableTo (type))
+                               if (!(type.IsValueType && type.IsNullable ()) && !value.GetType ().IsAssignableTo (type))
                                        throw new ArgumentException ();
 
                        }
@@ -1116,6 +1169,10 @@ namespace System.Linq.Expressions {
                        var method = GetUnaryOperator ("op_Explicit", type, type, target);
                        if (method == null)
                                method = GetUnaryOperator ("op_Implicit", type, type, target);
+                       if (method == null)
+                               method = GetUnaryOperator ("op_Explicit", target, type, target);
+                       if (method == null)
+                               method = GetUnaryOperator ("op_Implicit", target, type, target);
                        if (method == null)
                                throw new InvalidOperationException ();
 
@@ -1144,12 +1201,12 @@ namespace System.Linq.Expressions {
                static bool IsConvertNodeLifted (MethodInfo method, Expression operand, Type target)
                {
                        if (method == null)
-                               return IsNullable (operand.Type) || IsNullable (target);
+                               return operand.Type.IsNullable () || target.IsNullable ();
 
-                       if (IsNullable (operand.Type) && !ParameterMatch (method, operand.Type))
+                       if (operand.Type.IsNullable () && !ParameterMatch (method, operand.Type))
                                return true;
 
-                       if (IsNullable (target) && !ReturnTypeMatch (method, target))
+                       if (target.IsNullable () && !ReturnTypeMatch (method, target))
                                return true;
 
                        return false;
@@ -1348,6 +1405,37 @@ namespace System.Linq.Expressions {
                        return new InvocationExpression (expression, invoke.ReturnType, args);
                }
 
+               static bool CanAssign (Type target, Type source)
+               {
+                       // This catches object and value type mixage, type compatibility is handled later
+                       if (target.IsValueType ^ source.IsValueType)
+                               return false;
+
+                       return source.IsAssignableTo (target);
+               }
+
+               static void CheckLambda (Type delegateType, Expression body, ReadOnlyCollection<ParameterExpression> parameters)
+               {
+                       if (!delegateType.IsSubclassOf (typeof (System.Delegate)))
+                               throw new ArgumentException ("delegateType");
+
+                       var invoke = delegateType.GetMethod ("Invoke", BindingFlags.Instance | BindingFlags.Public);
+                       if (invoke == null)
+                               throw new ArgumentException ("delegate must contain an Invoke method", "delegateType");
+
+                       var invoke_parameters = invoke.GetParameters ();
+                       if (invoke_parameters.Length != parameters.Count)
+                               throw new ArgumentException (string.Format ("Different number of arguments in delegate {0}", delegateType), "delegateType");
+
+                       for (int i = 0; i < invoke_parameters.Length; i++) {
+                               if (!CanAssign (parameters [i].Type, invoke_parameters [i].ParameterType))
+                                       throw new ArgumentException (String.Format ("Can not assign a {0} to a {1}", invoke_parameters [i].ParameterType, parameters [i].Type));
+                       }
+
+                       if (invoke.ReturnType != typeof (void) && !CanAssign (invoke.ReturnType, body.Type))
+                               throw new ArgumentException (String.Format ("body type {0} can not be assigned to {1}", body.Type, invoke.ReturnType));
+               }
+
                public static Expression<TDelegate> Lambda<TDelegate> (Expression body, params ParameterExpression [] parameters)
                {
                        return Lambda<TDelegate> (body, parameters as IEnumerable<ParameterExpression>);
@@ -1358,7 +1446,11 @@ namespace System.Linq.Expressions {
                        if (body == null)
                                throw new ArgumentNullException ("body");
 
-                       return new Expression<TDelegate> (body, parameters.ToReadOnlyCollection ());
+                       var ps = parameters.ToReadOnlyCollection ();
+
+                       CheckLambda (typeof (TDelegate), body, ps);
+
+                       return new Expression<TDelegate> (body, ps);
                }
 
                public static LambdaExpression Lambda (Expression body, params ParameterExpression [] parameters)
@@ -1392,6 +1484,13 @@ namespace System.Linq.Expressions {
                        return Lambda (delegateType, body, parameters as IEnumerable<ParameterExpression>);
                }
 
+               static LambdaExpression CreateExpressionOf (Type type, Expression body, ReadOnlyCollection<ParameterExpression> parameters)
+               {
+                       return (LambdaExpression) Activator.CreateInstance (
+                               typeof (Expression<>).MakeGenericType (type),
+                               NonPublicInstance, null, new object [] { body, parameters }, null);
+               }
+
                public static LambdaExpression Lambda (Type delegateType, Expression body, IEnumerable<ParameterExpression> parameters)
                {
                        if (delegateType == null)
@@ -1399,7 +1498,11 @@ namespace System.Linq.Expressions {
                        if (body == null)
                                throw new ArgumentNullException ("body");
 
-                       return new LambdaExpression (delegateType, body, parameters.ToReadOnlyCollection ());
+                       var ps = parameters.ToReadOnlyCollection ();
+
+                       CheckLambda (delegateType, body, ps);
+
+                       return CreateExpressionOf (delegateType, body, ps);
                }
 
                public static MemberListBinding ListBind (MemberInfo member, params ElementInit [] initializers)
@@ -1683,7 +1786,7 @@ namespace System.Linq.Expressions {
                {
                        method = UnaryCoreCheck ("op_UnaryNegation", expression, method, type => IsSignedNumber (type));
 
-                       return MakeSimpleUnary (ExpressionType.Negate, expression, method);
+                       return MakeSimpleUnary (ExpressionType.NegateChecked, expression, method);
                }
 
                public static NewExpression New (ConstructorInfo constructor)
@@ -1976,7 +2079,7 @@ namespace System.Linq.Expressions {
                                throw new ArgumentNullException ("expression");
                        if (type == null)
                                throw new ArgumentNullException ("type");
-                       if (type.IsValueType && !IsNullable (type))
+                       if (type.IsValueType && !type.IsNullable ())
                                throw new ArgumentException ("TypeAs expect a reference or a nullable type");
 
                        return new UnaryExpression (ExpressionType.TypeAs, expression, type);
@@ -2027,11 +2130,6 @@ namespace System.Linq.Expressions {
                        return t == typeof (float) || t == typeof (double) || t == typeof (decimal);
                }
 
-               internal static bool IsNullable (Type type)
-               {
-                       return type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>);
-               }
-
                static bool IsSignedNumber (Type t)
                {
                        return IsNumber (t) && !IsUnsigned (t);
@@ -2039,9 +2137,11 @@ namespace System.Linq.Expressions {
 
                internal static bool IsUnsigned (Type t)
                {
+#if !TARGET_JVM
                        if (t.IsPointer)
                                return IsUnsigned (t.GetElementType ());
 
+#endif
                        return t == typeof (ushort) ||
                                t == typeof (uint) ||
                                t == typeof (ulong) ||
@@ -2051,14 +2151,14 @@ namespace System.Linq.Expressions {
                //
                // returns the T in a a Nullable<T> type.
                //
-               internal static Type GetNullableOf (Type type)
+               internal static Type GetNullableArgumentType (Type type)
                {
-                       return type.GetGenericArguments () [0];
+                       return type.GetFirstGenericArgument ();
                }
 
                internal static Type GetNotNullableOf (Type type)
                {
-                       return IsNullable (type) ? GetNullableOf (type) : type;
+                       return type.IsNullable () ? GetNullableArgumentType (type) : type;
                }
 
                //