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; }
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)
if (parameters.Length != 1)
continue;
- if (!IsAssignableToParameterType (param, parameters [0]))
+ if (!IsAssignableToParameterType (GetNotNullableOf (param), parameters [0]))
continue;
if (ret != null && method.ReturnType != GetNotNullableOf (ret))
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)
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;
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));
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
//
// 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);
}
//
{
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);
// 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;
//
// 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;
}
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;
}
// 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 ();
}
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 ();
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;
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>);
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)
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)
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)
{
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)
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);
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);
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) ||
//
// 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;
}
//