2008-03-05 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System.Core / System.Linq.Expressions / Expression.cs
index 31669ba74b8b0b7c01a9195553bb5ca16a505621..d24322901d8a585964e76affb0b1370ee726374a 100644 (file)
-// Permission is hereby granted, free of charge, to any person obtaining
+//
+// Expression.cs
+//
+// Author:
+//   Jb Evain (jbevain@novell.com)
+//   Miguel de Icaza (miguel@novell.com)
+//
+// (C) 2008 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.
 //
-// Authors:
-//             Marek Safar (marek.safar@seznam.cz)
-//             Antonello Provenzano  <antonello@deveel.com>
-//             Federico Di Gregorio <fog@initd.org>
 
+using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
+using System.Linq;
 using System.Reflection;
-using System.Text;
 
-namespace System.Linq.Expressions
-{
-       public abstract class Expression
-       {
-               #region .ctor
-               protected Expression (ExpressionType nodeType, Type type)
-               {
-                       this.nodeType = nodeType;
-                       this.type = type;
-               }
-               #endregion
-               
-               #region Fields
-               private Type type;
-               private ExpressionType nodeType;
-               #endregion
-               
-               #region Properties
-               public Type Type {
-                       get { return type; }
-               }
+namespace System.Linq.Expressions {
+
+       public abstract class Expression {
+
+               ExpressionType node_type;
+               Type type;
+
+               static BindingFlags PublicInstance = BindingFlags.Public | BindingFlags.Instance;
+               static BindingFlags PublicStatic = BindingFlags.Public | BindingFlags.Static;
+               static BindingFlags AllInstance = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
+               static BindingFlags AllStatic = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
+               static BindingFlags All = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
 
                public ExpressionType NodeType {
-                       get { return nodeType; }
+                       get { return node_type; }
                }
-               #endregion
 
-               #region Internal methods 
-               internal virtual void BuildString (StringBuilder builder)
-               {
-                       builder.Append ("[").Append (nodeType).Append ("]");
+               public Type Type {
+                       get { return type; }
                }
-               
-               internal static Type GetNonNullableType(Type type)
+
+               protected Expression (ExpressionType node_type, Type type)
                {
-                       // The Nullable<> class takes a single generic type so we can directly return
-                       // the first element of the array (if the type is nullable.)
-                       
-                       if (IsNullableType (type))
-                               return type.GetGenericArguments ()[0];
-                       else
-                               return type;
+                       this.node_type = node_type;
+                       this.type = type;
                }
 
-               internal static bool IsNullableType(Type type)
+               public override string ToString ()
                {
-                       if (type == null)
-                               throw new ArgumentNullException("type");
+                       return ExpressionPrinter.ToString (this);
+               }
 
-                       if (type.IsGenericType) {
-                               Type genType = type.GetGenericTypeDefinition();
-                               return typeof(Nullable<>).IsAssignableFrom(genType);
-                       }
+               #region Binary Expressions
 
-                       return false;
-               }
-               #endregion
-               
-               #region Private support methods
-               private static int IsWhat(Type type)
-               {
-                       // This method return a "type code" that can be easily compared to a bitmask
-                       // to determine the "broad type" (integer, boolean, floating-point) of the given type.
-                       // It is used by the three methods below.
-
-                       if (IsNullableType (type))
-                               type = GetNonNullableType (type);
-                               
-                       switch (Type.GetTypeCode (type)) {
-                               case TypeCode.Byte:  case TypeCode.SByte:
-                               case TypeCode.Int16: case TypeCode.UInt16:
-                               case TypeCode.Int32: case TypeCode.UInt32:
-                               case TypeCode.Int64: case TypeCode.UInt64:
-                               return 1;
-                               
-                               case TypeCode.Boolean:
-                               return 2;
-                               
-                               case TypeCode.Single:
-                               case TypeCode.Double:
-                               case TypeCode.Decimal:
-                               return 4;
-                               
-                               default:
-                               return 0;
-                       }
-               }
-               
-               private static bool IsInteger (Type type)
+               static MethodInfo GetUnaryOperator (string oper_name, Type on_type, Expression expression)
                {
-                       return (IsWhat(type) & 1) != 0;
-               }
+                       var methods = on_type.GetMethods (PublicStatic);
 
-               private static bool IsIntegerOrBool (Type type)
-               {
-                       return (IsWhat(type) & 3) != 0;
-               }
+                       foreach (var method in methods) {
+                               if (method.Name != oper_name)
+                                       continue;
 
-               private static bool IsNumeric (Type type)
-               {
-                       return (IsWhat(type) & 5) != 0;         
-               }
-               
-               private const BindingFlags opBindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
+                               var parameters = method.GetParameters ();
+                               if (parameters.Length != 1)
+                                       continue;
 
-               private static MethodInfo GetUserDefinedBinaryOperator (Type leftType, Type rightType, string name)
-               {
-                       Type[] types = new Type[2] { leftType, rightType };
-                       MethodInfo method;
-                       
-                       method  = leftType.GetMethod (name, opBindingFlags, null, types, null);
-                       if (method != null) return method;
-                               
-                       method = rightType.GetMethod (name, opBindingFlags, null, types, null);
-                       if (method != null) return method;
+                               if (!parameters [0].ParameterType.IsAssignableFrom (expression.Type))
+                                       continue;
+
+                               return method;
+                       }
 
-                       if (method == null && IsNullableType (leftType) && IsNullableType (rightType))
-                               return GetUserDefinedBinaryOperator (GetNonNullableType (leftType), GetNonNullableType (rightType), name);
-               
                        return null;
                }
 
-               private static BinaryExpression GetUserDefinedBinaryOperatorOrThrow (ExpressionType nodeType, string name,
-                               Expression left, Expression right)
+               static MethodInfo UnaryCoreCheck (string oper_name, Expression expression, MethodInfo method)
                {
-                       MethodInfo method = GetUserDefinedBinaryOperator(left.type, right.type, name);
+                       if (expression == null)
+                               throw new ArgumentNullException ("expression");
 
-                       if (method != null)
-                               return new BinaryExpression (nodeType, left, right, method, method.ReturnType);
-                       else
-                               throw new InvalidOperationException (String.Format (
-                                       "The binary operator Add is not defined for the types '{0}' and '{1}'.", left.type, right.type));
+                       if (method != null) {
+                               if (method.ReturnType == typeof (void))
+                                       throw new ArgumentException ("Specified method must return a value", "method");
 
-                       // Note: here the code in ExpressionUtils has a series of checks to make sure that
-                       // the method is static, that its return type is not void and that the number of
-                       // parameters is 2 and they are of the right type, but we already know that! Or not?
-               }
-               
-               private static MethodInfo FindMethod (Type type, string methodName, Type [] typeArgs, Expression [] args, BindingFlags flags)
-               {
-                       MemberInfo[] members = type.FindMembers(MemberTypes.Method, flags,
-                               delegate(MemberInfo mi, object obj) { return mi.Name == (String)obj; },
-                               methodName);
-                       if (members.Length == 0)
-                               throw new InvalidOperationException (String.Format (
-                                       "No method '{0}' exists on type '{1}'.", methodName, type.FullName));
+                               if (!method.IsStatic)
+                                       throw new ArgumentException ("Method must be static", "method");
 
-                       MethodInfo methodDefinition = null;
-                       MethodInfo method = null;
-                       int methodCount = 1;            
+                               var parameters = method.GetParameters ();
 
-                       foreach (MemberInfo member in members) {
-                               MethodInfo mi = (MethodInfo)member;
-                               if (mi.IsGenericMethodDefinition) {
-                                       // If the generic method definition matches we save it away to be able to make the
-                                       // correct closed method later on.
-                                       Type[] genericArgs = mi.GetGenericArguments();
-                                       if (genericArgs.Length != typeArgs.Length) goto next;
+                               if (parameters.Length != 1)
+                                       throw new ArgumentException ("Must have only one parameters", "method");
+
+                               if (!parameters [0].ParameterType.IsAssignableFrom (expression.Type))
+                                       throw new InvalidOperationException ("left-side argument type does not match left expression type");
 
-                                       methodDefinition = mi;
-                                       goto next;
-                               }
-                               
-                               // If there is a discrepancy between method's generic types and the given types or if
-                               // the method is open we simply discard it and go on.
-                               if ((mi.IsGenericMethod && (typeArgs == null || mi.ContainsGenericParameters))
-                                        || (!mi.IsGenericMethod && typeArgs != null))
-                                       goto next;
-                                       
-                               // If the method is a closed generic we try to match the generic types.
-                               if (mi.IsGenericMethod) {
-                                       Type[] genericArgs = mi.GetGenericArguments();
-                                       if (genericArgs.Length != typeArgs.Length) goto next;
-                                       for (int i=0 ; i < genericArgs.Length ; i++)
-                                               if (genericArgs[i] != typeArgs[i]) goto next;
-                               }
-                               
-                               // Finally we test for the method's parameters.
-                               ParameterInfo[] parameters = mi.GetParameters ();
-                               if (parameters.Length != args.Length) goto next;
-                               for (int i=0 ; i < parameters.Length ; i++)
-                                       if (parameters[i].ParameterType != args[i].type) goto next;
-
-                               method = mi;
-                               break;
-                               
-                        next:
-                               continue;
-                       }
-                       
-                       if (method != null)
                                return method;
-                       else
-                               throw new InvalidOperationException(String.Format(
-                                       "No method '{0}' on type '{1}' is compatible with the supplied arguments.", methodName, type.FullName));
-               }
-
-               private static PropertyInfo GetProperty (MethodInfo mi)
-               {
-                       // If the method has the hidebysig and specialname attributes it can be a property accessor;
-                       // if that's the case we try to extract the type of the property and then we use it and the
-                       // property name (derived from the method name) to find the right PropertyInfo.
-                       
-                       if (mi.IsHideBySig && mi.IsSpecialName) {
-                               Type propertyType = null;
-                               if (mi.Name.StartsWith("set_")) {
-                                       ParameterInfo[] parameters = mi.GetParameters();
-                                       if (parameters.Length == 1)
-                                               propertyType = parameters[0].ParameterType;
-                               }
-                               else if (mi.Name.StartsWith("get_")) {
-                                       propertyType = mi.ReturnType;
+                       } else {
+                               if (IsNumber (expression.Type))
+                                       return null;
+
+                               if (oper_name != null) {
+                                       method = GetUnaryOperator (oper_name, expression.Type, expression);
+                                       if (method != null)
+                                               return method;
                                }
-                               
-                               if (propertyType != null) {
-                                       PropertyInfo pi = mi.DeclaringType.GetProperty(mi.Name.Substring(4),
-                                               BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance,
-                                               null, propertyType, new Type[0], null);
-                                       if (pi != null) return pi;
-                               }
-                       }
-                       
-                       throw new ArgumentException (String.Format( 
-                               "The method '{0}.{1}' is not a property accessor", mi.DeclaringType.FullName, mi.Name));
-               }
-               
-               private static void ValidateUserDefinedConditionalLogicOperator (ExpressionType nodeType, Type left, Type right, MethodInfo method)
-               {
-                       // Conditional logic need the "definitely true" and "definitely false" operators.
-                       Type[] types = new Type[1] { left };
-                                               
-                       MethodInfo opTrue  = left.GetMethod ("op_True", opBindingFlags, null, types, null);
-                       MethodInfo opFalse = left.GetMethod ("op_False", opBindingFlags, null, types, null);
-                       
-                       if (opTrue == null || opFalse == null)
-                               throw new ArgumentException (String.Format (
-                                       "The user-defined operator method '{0}' for operator '{1}' must have associated boolean True and False operators.",
-                                       method.Name, nodeType));
-               }
-               
-               private static void ValidateSettableFieldOrPropertyMember (MemberInfo member, out Type memberType)
-               {
-                       if (member.MemberType == MemberTypes.Field) {
-                               memberType = (member as FieldInfo).FieldType;
-                       }
-                       else if (member.MemberType == MemberTypes.Property) {
-                               PropertyInfo pi = (PropertyInfo)member;
-                               if (!pi.CanWrite)
-                                       throw new ArgumentException (String.Format ("The property '{0}' has no 'set' accessor", pi));
-                               memberType = (member as PropertyInfo).PropertyType;
-                       }
-                       else {
-                               throw new ArgumentException ("Argument must be either a FieldInfo or PropertyInfo");   
+
+                               throw new InvalidOperationException (
+                                       String.Format ("Operation {0} not defined for {1}", oper_name != null ? oper_name.Substring (3) : "is", expression.Type));
                        }
                }
 
-               private static void ValidateGettableFieldOrPropertyMember (MemberInfo member, out Type memberType)
+               static MethodInfo GetBinaryOperator (string oper_name, Type on_type, Expression left, Expression right)
                {
-                       if (member.MemberType == MemberTypes.Field) {
-                               memberType = (member as FieldInfo).FieldType;
-                       }
-                       else if (member.MemberType == MemberTypes.Property) {
-                               PropertyInfo pi = (PropertyInfo)member;
-                               if (!pi.CanRead)
-                                       throw new ArgumentException (String.Format ("The property '{0}' has no 'get' accessor", pi));
-                               memberType = (member as PropertyInfo).PropertyType;
-                       }
-                       else {
-                               throw new ArgumentException ("Argument must be either a FieldInfo or PropertyInfo");   
+                       MethodInfo [] methods = on_type.GetMethods (PublicStatic);
+
+                       foreach (MethodInfo m in methods) {
+                               if (m.Name != oper_name)
+                                       continue;
+
+                               ParameterInfo [] pi = m.GetParameters ();
+                               if (pi.Length != 2)
+                                       continue;
+
+                               if (!pi [0].ParameterType.IsAssignableFrom (left.Type))
+                                       continue;
+
+                               if (!pi [1].ParameterType.IsAssignableFrom (right.Type))
+                                       continue;
+
+                               // Method has papers in order.
+                               return m;
                        }
+
+                       return null;
                }
-               #endregion
-                               
-               #region ToString
-               public override string ToString()
-               {
-                       StringBuilder builder = new StringBuilder ();
-                       BuildString (builder);
-                       return builder.ToString ();
-               }
-               #endregion
 
-               #region Add
-               public static BinaryExpression Add(Expression left, Expression right, MethodInfo method)
+               //
+               // Performs basic checks on the incoming expressions for binary expressions
+               // and any provided MethodInfo.
+               //
+               static MethodInfo BinaryCoreCheck (string oper_name, Expression left, Expression right, MethodInfo method)
                {
                        if (left == null)
                                throw new ArgumentNullException ("left");
                        if (right == null)
                                throw new ArgumentNullException ("right");
 
-                       if (method != null)
-                               return new BinaryExpression(ExpressionType.Add, left, right, method, method.ReturnType);
-                       
-                       // Since both the expressions define the same numeric type we don't have
-                       // to look for the "op_Addition" method.
-                       if (left.type == right.type && IsNumeric (left.type))
-                               return new BinaryExpression(ExpressionType.Add, left, right, left.type);
+                       if (method != null){
+                               if (method.ReturnType == typeof (void))
+                                       throw new ArgumentException ("Specified method must return a value", "method");
 
-                       // Else we try for a user-defined operator.
-                       return GetUserDefinedBinaryOperatorOrThrow (ExpressionType.Add, "op_Addition", left, right);
-               }
+                               if (!method.IsStatic)
+                                       throw new ArgumentException ("Method must be static", "method");
+                               ParameterInfo [] pi = method.GetParameters ();
 
-               public static BinaryExpression Add(Expression left, Expression right)
-               {
-                       return Add(left, right, null);
-               }
-               #endregion
-               
-               #region AddChecked
-               public static BinaryExpression AddChecked(Expression left, Expression right, MethodInfo method)
-               {
-                       if (left == null)
-                               throw new ArgumentNullException ("left");
-                       if (right == null)
-                               throw new ArgumentNullException ("right");
+                               if (pi.Length != 2)
+                                       throw new ArgumentException ("Must have only two parameters", "method");
 
-                       if (method != null)
-                               return new BinaryExpression(ExpressionType.AddChecked, left, right, method, method.ReturnType);
+                               if (!pi [0].ParameterType.IsAssignableFrom (GetNotNullableOf (left.Type)))
+                                       throw new InvalidOperationException ("left-side argument type does not match left expression type");
 
-                       // Since both the expressions define the same numeric type we don't have
-                       // to look for the "op_Addition" method.
-                       if (left.type == right.type && IsNumeric (left.type))
-                               return new BinaryExpression(ExpressionType.AddChecked, left, right, left.type);
+                               if (!pi [1].ParameterType.IsAssignableFrom (GetNotNullableOf (right.Type)))
+                                       throw new InvalidOperationException ("right-side argument type does not match right expression type");
 
-                       method = GetUserDefinedBinaryOperator (left.type, right.type, "op_Addition");
-                       if (method == null)
-                               throw new InvalidOperationException(String.Format(
-                                       "The binary operator AddChecked is not defined for the types '{0}' and '{1}'.", left.type, right.type));
-                       
-                       Type retType = method.ReturnType;
+                               return method;
+                       } else {
+                               Type ltype = left.Type;
+                               Type rtype = right.Type;
+                               Type ultype = GetNotNullableOf (ltype);
+                               Type urtype = GetNotNullableOf (rtype);
+
+                               if (oper_name == "op_BitwiseOr" || oper_name == "op_BitwiseAnd") {
+                                       if (ultype == typeof (bool)) {
+                                               if (ultype == urtype && ltype == rtype)
+                                                       return null;
+                                       }
+                               }
 
-                       // Note: here the code did some very strange checks for bool (but note that bool does
-                       // not define an addition operator) and created nullables for value types (but the new
-                       // MS code does not do that). All that has been removed.
+                               // Use IsNumber to avoid expensive reflection.
+                               if (IsNumber (ultype)){
+                                       if (ultype == urtype && ltype == rtype)
+                                               return null;
 
-                       return new BinaryExpression(ExpressionType.AddChecked, left, right, method, retType);
-               }
+                                       if (oper_name != null){
+                                               method = GetBinaryOperator (oper_name, rtype, left, right);
+                                               if (method != null)
+                                                       return method;
+                                       }
+                               }
 
-               public static BinaryExpression AddChecked(Expression left, Expression right)
-               {
-                       return AddChecked(left, right, null);
+                               if (oper_name != null){
+                                       method = GetBinaryOperator (oper_name, ltype, left, right);
+                                       if (method != null)
+                                               return method;
+                               }
+
+                               //
+                               // == and != allow reference types without operators defined.
+                               //
+                               if (!ltype.IsValueType && !rtype.IsValueType &&
+                                       (oper_name == "op_Equality" || oper_name == "op_Inequality"))
+                                       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));
+                       }
                }
-               #endregion
 
-               #region And
-               public static BinaryExpression And(Expression left, Expression right, MethodInfo method)
+               //
+               // This is like BinaryCoreCheck, but if no method is used adds the restriction that
+               // only ints and bools are allowed
+               //
+               static MethodInfo BinaryBitwiseCoreCheck (string oper_name, Expression left, Expression right, MethodInfo method)
                {
                        if (left == null)
                                throw new ArgumentNullException ("left");
                        if (right == null)
                                throw new ArgumentNullException ("right");
 
-                       if (method != null)
-                               return new BinaryExpression(ExpressionType.And, left, right, method, method.ReturnType);
-                       
-                       // Since both the expressions define the same integer or boolean type we don't have
-                       // to look for the "op_BitwiseAnd" method.
-                       if (left.type == right.type && IsIntegerOrBool (left.type))
-                               return new BinaryExpression(ExpressionType.And, left, right, left.type);
+                       if (method == null) {
+                               // avoid reflection shortcut and catches Ints/bools before we check Numbers in general
+                               if (left.Type == right.Type && IsIntOrBool (left.Type))
+                                       return null;
+                       }
+
+                       method = BinaryCoreCheck (oper_name, left, right, method);
+                       if (method == null) {
+                               // The check in BinaryCoreCheck allows a bit more than we do
+                               // (floats and doubles).  Catch this here
+                               if (left.Type == typeof (double) || left.Type == typeof (float))
+                                       throw new InvalidOperationException ("Types not supported");
+                       }
 
-                       // Else we try for a user-defined operator.
-                       return GetUserDefinedBinaryOperatorOrThrow (ExpressionType.And, "op_BitwiseAnd", left, right);
+                       return method;
                }
 
-               public static BinaryExpression And(Expression left, Expression right)
+               static Type GetResultType (Expression expression, MethodInfo method)
                {
-                       return And(left, right, null);
+                       return method == null ? expression.Type : method.ReturnType;
                }
-               #endregion
-               
-               #region AndAlso
-               public static BinaryExpression AndAlso(Expression left, Expression right, MethodInfo method)
+
+               static BinaryExpression MakeSimpleBinary (ExpressionType et, Expression left, Expression right, MethodInfo method)
                {
-                       if (left == null)
-                               throw new ArgumentNullException ("left");
-                       if (right == null)
-                               throw new ArgumentNullException ("right");
+                       bool is_lifted;
 
-                       // Since both the expressions define the same boolean type we don't have
-                       // to look for the "op_BitwiseAnd" method.
-                       if (left.type == right.type && left.type == typeof(bool))
-                               return new BinaryExpression(ExpressionType.AndAlso, left, right, left.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");
 
-                       // Else we must validate the method to make sure it has companion "true" and "false" operators.
-                       if (method == null)
-                               method = GetUserDefinedBinaryOperator (left.type, right.type, "op_BitwiseAnd");
-                       if (method == null)
-                               throw new InvalidOperationException(String.Format(
-                                       "The binary operator AndAlso is not defined for the types '{0}' and '{1}'.", left.type, right.type));
-                       ValidateUserDefinedConditionalLogicOperator(ExpressionType.AndAlso, left.type, right.type, method);
-                       
-                       return new BinaryExpression(ExpressionType.AndAlso, left, right, method, method.ReturnType);
+                                       is_lifted = true;
+                               } else
+                                       is_lifted = false;
+                       } else {
+                               //
+                               // FIXME: implement
+                               //
+                               is_lifted = false;
+                       }
+
+                       return new BinaryExpression (et, GetResultType (left, method), left, right, is_lifted, is_lifted, method, null);
+               }
+
+               static UnaryExpression MakeSimpleUnary (ExpressionType et, Expression expression, MethodInfo method)
+               {
+                       return new UnaryExpression (et, expression, GetResultType (expression, method), method);
+               }
+
+               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;
+
+                       // Implement the rules as described in "Expression.Equal" method.
+                       if (method == null) {
+                               if (!lnullable && !rnullable) {
+                                       is_lifted = false;
+                                       liftToNull = false;
+                                       result = typeof (bool);
+                               } else if (lnullable && rnullable) {
+                                       is_lifted = true;
+                                       result = liftToNull ? typeof(bool?) : typeof (bool);
+                               } else
+                                       throw new InvalidOperationException ("Internal error: this should have been caught in BinaryCoreCheck");
+                       } else {
+                               ParameterInfo [] pi = method.GetParameters ();
+                               Type mltype = pi [0].ParameterType;
+                               Type mrtype = pi [1].ParameterType;
+
+                               if (ltype == mltype && rtype == mrtype) {
+                                       is_lifted = false;
+                                       liftToNull = false;
+                                       result = method.ReturnType;
+                               } else if (ltype.IsValueType && rtype.IsValueType &&
+                                          ((lnullable && GetNullableOf (ltype) == mltype) ||
+                                               (rnullable && GetNullableOf (rtype) == mrtype))){
+                                       is_lifted = true;
+                                       if (method.ReturnType == typeof(bool)){
+                                               result = liftToNull ? typeof(bool?) : typeof(bool);
+                                       } else {
+                                               //
+                                               // This behavior is not documented: what
+                                               // happens if the result is not typeof(bool), but
+                                               // the parameters are nullable: the result
+                                               // becomes nullable<returntype>
+                                               //
+                                               // 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;
+                               }
+                       }
+
+                       return new BinaryExpression (et, result, left, right, liftToNull, is_lifted, method, null);
                }
 
-               public static BinaryExpression AndAlso(Expression left, Expression right)
+               //
+               // Arithmetic
+               //
+               public static BinaryExpression Add (Expression left, Expression right)
                {
-                       return AndAlso(left, right, null);
+                       return Add (left, right, null);
                }
-               #endregion
-               
-               #region ArrayIndex
-               public static BinaryExpression ArrayIndex(Expression array, Expression index)
+
+               public static BinaryExpression Add (Expression left, Expression right, MethodInfo method)
                {
-                       if (array == null)
-                               throw new ArgumentNullException ("array");
-                       if (index == null)
-                               throw new ArgumentNullException ("index");
-                       if (!array.type.IsArray)
-                               throw new ArgumentException ("Argument must be array");
-                       if (index.type != typeof(int))
-                               throw new ArgumentException ("Argument for array index must be of type Int32");
+                       method = BinaryCoreCheck ("op_Addition", left, right, method);
 
-                       return new BinaryExpression(ExpressionType.ArrayIndex, array, index, array.type.GetElementType());
+                       return MakeSimpleBinary (ExpressionType.Add, left, right, method);
                }
 
-               public static MethodCallExpression ArrayIndex(Expression array, params Expression[] indexes)
+               public static BinaryExpression AddChecked (Expression left, Expression right)
                {
-                       return ArrayIndex(array, (IEnumerable<Expression>)indexes);
+                       return AddChecked (left, right, null);
                }
 
-               public static MethodCallExpression ArrayIndex(Expression array, IEnumerable<Expression> indexes)
+               public static BinaryExpression AddChecked (Expression left, Expression right, MethodInfo method)
                {
-                       if (array == null)
-                               throw new ArgumentNullException ("array");
-                       if (indexes == null)
-                               throw new ArgumentNullException ("indexes");
-                       if (!array.type.IsArray)
-                               throw new ArgumentException ("Argument must be array");
-
-                       // We'll need an array of typeof(Type) elements long as the array's rank later
-                       // and also a generic List to hold the indexes (ReadOnlyCollection wants that.)
-                       
-                       Type[] types = (Type[])Array.CreateInstance(typeof(Type), array.type.GetArrayRank());
-                       Expression[] indexesList = new Expression[array.type.GetArrayRank()];
-                       
-                       int rank = 0;
-                       foreach (Expression index in indexes) {
-                               if (index.type != typeof(int))
-                                       throw new ArgumentException ("Argument for array index must be of type Int32");
-                               if (rank == array.type.GetArrayRank())
-                                       throw new ArgumentException ("Incorrect number of indexes");
-
-                               types[rank] = index.type;
-                               indexesList[rank] = index;
-                               rank += 1;
+                       method = BinaryCoreCheck ("op_Addition", left, right, method);
+
+                       // The check in BinaryCoreCheck allows a bit more than we do
+                       // (byte, sbyte).  Catch that here
+                       if (method == null) {
+                               if (left.Type == typeof (byte) || left.Type == typeof (sbyte))
+                                       throw new InvalidOperationException (String.Format ("AddChecked not defined for {0} and {1}", left.Type, right.Type));
                        }
-                               
-                       // If the array's rank is equalto the number of given indexes we can go on and
-                       // look for a Get(Int32, ...) method with "rank" parameters to generate the
-                       // MethodCallExpression.
 
-                       MethodInfo method = array.type.GetMethod("Get",
-                               BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);
+                       return MakeSimpleBinary (ExpressionType.AddChecked, left, right, method);
+               }
 
-                       // This should not happen, but we check anyway.
-                       if (method == null)
-                               throw new InvalidOperationException(String.Format(
-                                       "The method Get(...) is not defined for the type '{0}'.", array.type));
-       
-                       return new MethodCallExpression(ExpressionType.Call, method, array, new ReadOnlyCollection<Expression>(indexesList));
+               public static BinaryExpression Subtract (Expression left, Expression right)
+               {
+                       return Subtract (left, right, null);
                }
-               #endregion
-               
-               #region ArrayLength
-               public static UnaryExpression ArrayLength(Expression array)
+
+               public static BinaryExpression Subtract (Expression left, Expression right, MethodInfo method)
                {
-                       if (array == null)
-                               throw new ArgumentNullException ("array");
-                       if (!array.type.IsArray)
-                               throw new ArgumentException ("Argument must be array");
+                       method = BinaryCoreCheck ("op_Subtraction", left, right, method);
 
-                       return new UnaryExpression(ExpressionType.ArrayLength, array, typeof(Int32));           
+                       return MakeSimpleBinary (ExpressionType.Subtract, left, right, method);
                }
-               #endregion
-               
-               #region Bind
-               public static MemberAssignment Bind (MemberInfo member, Expression expression)
+
+               public static BinaryExpression SubtractChecked (Expression left, Expression right)
                {
-                       if (member == null)
-                               throw new ArgumentNullException ("member");
-                       if (expression == null)
-                               throw new ArgumentNullException ("expression");
-                               
-                       Type memberType;
-                       ValidateSettableFieldOrPropertyMember(member, out memberType);                  
-               
-                       return new MemberAssignment(member, expression);
+                       return SubtractChecked (left, right, null);
                }
 
-               public static MemberAssignment Bind (MethodInfo propertyAccessor, Expression expression)
+               public static BinaryExpression SubtractChecked (Expression left, Expression right, MethodInfo method)
                {
-                       if (propertyAccessor == null)
-                               throw new ArgumentNullException ("propertyAccessor");
-                       if (expression == null)
-                               throw new ArgumentNullException ("expression");
+                       method = BinaryCoreCheck ("op_Subtraction", left, right, method);
+
+                       // The check in BinaryCoreCheck allows a bit more than we do
+                       // (byte, sbyte).  Catch that here
+                       if (method == null) {
+                               if (left.Type == typeof (byte) || left.Type == typeof (sbyte))
+                                       throw new InvalidOperationException (String.Format ("SubtractChecked not defined for {0} and {1}", left.Type, right.Type));
+                       }
 
-                       return new MemberAssignment(GetProperty(propertyAccessor), expression);         
+                       return MakeSimpleBinary (ExpressionType.SubtractChecked, left, right, method);
                }
-               #endregion
-               
-               #region Call
-               public static MethodCallExpression Call(Expression instance, MethodInfo method)
+
+               public static BinaryExpression Modulo (Expression left, Expression right)
                {
-                       if (method == null)
-                               throw new ArgumentNullException("method");
-                       if (instance == null && !method.IsStatic)
-                               throw new ArgumentNullException("instance");
-                               
-                       return Call(instance, method, (Expression[])null);
+                       return Modulo (left, right, null);
                }
 
-               public static MethodCallExpression Call(Expression instance, MethodInfo method, params Expression[] arguments)
+               public static BinaryExpression Modulo (Expression left, Expression right, MethodInfo method)
                {
-                       return Call(instance, method, (IEnumerable<Expression>)arguments);
+                       method = BinaryCoreCheck ("op_Modulus", left, right, method);
+
+                       return MakeSimpleBinary (ExpressionType.Modulo, left, right, method);
                }
 
-               public static MethodCallExpression Call(Expression instance, MethodInfo method, IEnumerable<Expression> arguments)
+               public static BinaryExpression Multiply (Expression left, Expression right)
                {
-                       if (method == null)
-                               throw new ArgumentNullException("method");
-                       if (arguments == null)
-                               throw new ArgumentNullException("arguments");
-                       if (instance == null && !method.IsStatic)
-                               throw new ArgumentNullException("instance");
-                               
-                       if (method.IsGenericMethodDefinition)
-                                       throw new ArgumentException();
-                       if (method.ContainsGenericParameters)
-                                       throw new ArgumentException();
-                       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 MethodCallExpression(ExpressionType.Call, method, instance, roArgs);
+                       return Multiply (left, right, null);
                }
 
-               public static MethodCallExpression Call (Expression instance, string methodName, Type [] typeArguments, params Expression [] arguments)
+               public static BinaryExpression Multiply (Expression left, Expression right, MethodInfo method)
                {
-                       if (instance == null)
-                               throw new ArgumentNullException("instance");
-                       if (arguments == null)
-                               throw new ArgumentNullException("arguments");
+                       method = BinaryCoreCheck ("op_Multiply", left, right, method);
 
-                       return Call (null, FindMethod (instance.type, methodName, typeArguments, arguments,
-                                       BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance),
-                               (IEnumerable<Expression>)arguments);            
+                       return MakeSimpleBinary (ExpressionType.Multiply, left, right, method);
                }
-               
-               public static MethodCallExpression Call(MethodInfo method, params Expression[] arguments)
+
+               public static BinaryExpression MultiplyChecked (Expression left, Expression right)
                {
-                       return Call(null, method, (IEnumerable<Expression>)arguments);
+                       return MultiplyChecked (left, right, null);
                }
 
-               public static MethodCallExpression Call (Type type, string methodName, Type [] typeArguments, params Expression [] arguments)
+               public static BinaryExpression MultiplyChecked (Expression left, Expression right, MethodInfo method)
                {
-                       // FIXME: MS implementation does not check for type here and simply lets FindMethod() raise
-                       // a NullReferenceException. Shall we do the same or raise the correct exception here?
-                       //if (type == null)
-                       //      throw new ArgumentNullException("type");
-                       
-                       if (methodName == null)
-                               throw new ArgumentNullException("methodName");
-                       if (arguments == null)
-                               throw new ArgumentNullException("arguments");
+                       method = BinaryCoreCheck ("op_Multiply", left, right, method);
 
-                       // Note that we're looking for static methods only (this version of Call() doesn't take an instance).
-                       return Call (null, FindMethod (type, methodName, typeArguments, arguments,
-                                       BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static),
-                               (IEnumerable<Expression>)arguments);
+                       return MakeSimpleBinary (ExpressionType.MultiplyChecked, left, right, method);
                }
-               #endregion
 
-               // NOTE: CallVirtual is not implemented because it is already marked as Obsolete by MS.
-               
-               public static ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse)
+               public static BinaryExpression Divide (Expression left, Expression right)
                {
-                       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);
+                       return Divide (left, right, null);
                }
 
-               public static ConstantExpression Constant(object value, Type type)
+               public static BinaryExpression Divide (Expression left, Expression right, MethodInfo method)
                {
-                       if (type == null)
-                               throw new ArgumentNullException("type");
-                       if (value == null && !IsNullableType(type))
-                               throw new ArgumentException("Argument types do not match");
+                       method = BinaryCoreCheck ("op_Division", left, right, method);
 
-                       return new ConstantExpression(value, type);
+                       return MakeSimpleBinary (ExpressionType.Divide, left, right, method);
                }
 
-               public static ConstantExpression Constant(object value)
+               public static BinaryExpression Power (Expression left, Expression right)
                {
-                       if (value != null)
-                               return new ConstantExpression(value, value.GetType());
-                       else
-                               return new ConstantExpression(null, typeof(object));
+                       return Power (left, right, null);
                }
 
-               #region Divide
-               public static BinaryExpression Divide(Expression left, Expression right, MethodInfo method)
+               public static BinaryExpression Power (Expression left, Expression right, MethodInfo method)
                {
-                       if (left == null)
-                               throw new ArgumentNullException ("left");
-                       if (right == null)
-                               throw new ArgumentNullException ("right");
+                       method = BinaryCoreCheck (null, left, right, method);
 
-                       if (method != null)
-                               return new BinaryExpression(ExpressionType.Divide, left, right, method, method.ReturnType);
-                       
-                       // Since both the expressions define the same numeric type we don't have
-                       // to look for the "op_Addition" method.
-                       if (left.type == right.type && IsNumeric (left.type))
-                               return new BinaryExpression(ExpressionType.Divide, left, right, left.type);
+                       if (left.Type != typeof (double))
+                               throw new InvalidOperationException ("Power only supports double arguments");
 
-                       // Else we try for a user-defined operator.
-                       return GetUserDefinedBinaryOperatorOrThrow (ExpressionType.Divide, "op_Division", left, right);
+                       return MakeSimpleBinary (ExpressionType.Power, left, right, method);
                }
 
-               public static BinaryExpression Divide(Expression left, Expression right)
+               //
+               // Bitwise
+               //
+               public static BinaryExpression And (Expression left, Expression right)
                {
-                       return Divide(left, right, null);
+                       return And (left, right, null);
                }
-               #endregion
-               
-               #region ExclusiveOr
-               public static BinaryExpression ExclusiveOr (Expression left, Expression right, System.Reflection.MethodInfo method)
+
+               public static BinaryExpression And (Expression left, Expression right, MethodInfo method)
                {
-                       if (left == null)
-                               throw new ArgumentNullException ("left");
-                       if (right == null)
-                               throw new ArgumentNullException ("right");
+                       method = BinaryBitwiseCoreCheck ("op_BitwiseAnd", left, right, method);
+
+                       return MakeSimpleBinary (ExpressionType.And, left, right, method);
+               }
+
+               public static BinaryExpression Or (Expression left, Expression right)
+               {
+                       return Or (left, right, null);
+               }
 
-                       if (method != null)
-                               return new BinaryExpression(ExpressionType.ExclusiveOr, left, right, method, method.ReturnType);
-                       
-                       // Since both the expressions define the same integer or boolean type we don't have
-                       // to look for the "op_BitwiseAnd" method.
-                       if (left.type == right.type && IsIntegerOrBool (left.type))
-                               return new BinaryExpression(ExpressionType.ExclusiveOr, left, right, left.type);
+               public static BinaryExpression Or (Expression left, Expression right, MethodInfo method)
+               {
+                       method = BinaryBitwiseCoreCheck ("op_BitwiseOr", left, right, method);
 
-                       // Else we try for a user-defined operator.
-                       return GetUserDefinedBinaryOperatorOrThrow (ExpressionType.ExclusiveOr, "op_ExclusiveOr", left, right);
+                       return MakeSimpleBinary (ExpressionType.Or, left, right, method);
                }
-               
+
                public static BinaryExpression ExclusiveOr (Expression left, Expression right)
                {
                        return ExclusiveOr (left, right, null);
                }
-               #endregion
-               
-               #region Field
-               public static MemberExpression Field (Expression expression, FieldInfo field)
-               {
-                       // Note that expression can be (and should be) null when the access is to a static field.
 
-                       if (field == null)
-                               throw new ArgumentNullException("field");
+               public static BinaryExpression ExclusiveOr (Expression left, Expression right, MethodInfo method)
+               {
+                       method = BinaryBitwiseCoreCheck ("op_ExclusiveOr", left, right, method);
 
-                       Type fieldType;
-                       ValidateGettableFieldOrPropertyMember(field, out fieldType);
+                       return MakeSimpleBinary (ExpressionType.ExclusiveOr, left, right, method);
+               }
 
-                       return new MemberExpression(expression, field, fieldType);
+               public static BinaryExpression LeftShift (Expression left, Expression right)
+               {
+                       return LeftShift (left, right, null);
                }
 
-               public static MemberExpression Field (Expression expression, string fieldName)
+               public static BinaryExpression LeftShift (Expression left, Expression right, MethodInfo method)
                {
-                       if (expression == null)
-                               throw new ArgumentNullException("expression");
-                       if (fieldName == null)
-                               throw new ArgumentNullException("fieldName");
+                       method = BinaryBitwiseCoreCheck ("op_LeftShift", left, right, method);
 
-                       FieldInfo field = expression.Type.GetField(fieldName,
-                               BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
-                       if (field == null)
-                               throw new ArgumentException (String.Format ("Field {0} is not defined for type {1}",
-                                       fieldName, expression.type.FullName));
+                       return MakeSimpleBinary (ExpressionType.LeftShift, left, right, method);
+               }
 
-                       return Field(expression, field);
+               public static BinaryExpression RightShift (Expression left, Expression right)
+               {
+                       return RightShift (left, right, null);
                }
-               #endregion
 
-               public static Type GetFuncType(params Type[] typeArgs)
+               public static BinaryExpression RightShift (Expression left, Expression right, MethodInfo method)
                {
-                       if (typeArgs == null)
-                               throw new ArgumentNullException("typeArgs");
-                       if (typeArgs.Length > 5)
-                               throw new ArgumentException();
+                       method = BinaryCoreCheck ("op_RightShift", left, right, method);
 
-                       return typeof(Func<,,,,>).MakeGenericType(typeArgs);
+                       return MakeSimpleBinary (ExpressionType.RightShift, left, right, method);
                }
 
-               #region LeftShift
-               public static BinaryExpression LeftShift (Expression left, Expression right, MethodInfo method)
+               //
+               // Short-circuit
+               //
+               public static BinaryExpression AndAlso (Expression left, Expression right)
                {
-                       if (left == null)
-                               throw new ArgumentNullException ("left");
-                       if (right == null)
-                               throw new ArgumentNullException ("right");
+                       return AndAlso (left, right, null);
+               }
 
-                       if (method != null)
-                               return new BinaryExpression(ExpressionType.LeftShift, left, right, method, method.ReturnType);
-                       
-                       // If the left side is any kind of integer and the right is int32 we don't have
-                       // to look for the "op_Addition" method.
-                       if (IsInteger(left.type) && right.type == typeof(Int32))
-                               return new BinaryExpression(ExpressionType.LeftShift, left, right, left.type);
+               public static BinaryExpression AndAlso (Expression left, Expression right, MethodInfo method)
+               {
+                       method = ConditionalBinaryCheck ("op_BitwiseAnd", left, right, method);
 
-                       // Else we try for a user-defined operator.
-                       return GetUserDefinedBinaryOperatorOrThrow (ExpressionType.LeftShift, "op_LeftShift", left, right);
+                       return MakeBoolBinary (ExpressionType.AndAlso, left, right, true, method);
                }
 
-               public static BinaryExpression LeftShift (Expression left, Expression right)
+               static MethodInfo ConditionalBinaryCheck (string oper, Expression left, Expression right, MethodInfo method)
                {
-                       return LeftShift (left, right, null);
+                       method = BinaryCoreCheck (oper, left, right, method);
+
+                       if (method == null) {
+                               if (GetNotNullableOf (left.Type) != typeof (bool))
+                                       throw new InvalidOperationException ("Only booleans are allowed");
+                       } else {
+                               // 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");
+                       }
+
+                       return method;
                }
-               #endregion
-               
-               public static ListInitExpression ListInit(NewExpression newExpression, params ElementInit[] initializers)
-               {
-                       if (initializers == null)
-                               throw new ArgumentNullException("inizializers");
 
-                       return ListInit(newExpression, Enumerable.ToReadOnlyCollection<ElementInit>(initializers));
+               public static BinaryExpression OrElse (Expression left, Expression right)
+               {
+                       return OrElse (left, right, null);
                }
 
-               public static ListInitExpression ListInit(NewExpression newExpression, IEnumerable<ElementInit> initializers)
+               public static BinaryExpression OrElse (Expression left, Expression right, MethodInfo method)
                {
-                       if (newExpression == null)
-                               throw new ArgumentNullException("newExpression");
-                       if (initializers == null)
-                               throw new ArgumentNullException("inizializers");
+                       method = ConditionalBinaryCheck ("op_BitwiseOr", left, right, method);
 
-                       return new ListInitExpression(newExpression, Enumerable.ToReadOnlyCollection<ElementInit>(initializers));
+                       return MakeBoolBinary (ExpressionType.OrElse, left, right, true, method);
                }
 
-               public static MemberInitExpression MemberInit(NewExpression newExpression, IEnumerable<MemberBinding> bindings)
+               //
+               // Comparison
+               //
+               public static BinaryExpression Equal (Expression left, Expression right)
                {
-                       if (newExpression == null)
-                               throw new ArgumentNullException("newExpression");
+                       return Equal (left, right, false, null);
+               }
 
-                       if (bindings == null)
-                               throw new ArgumentNullException("bindings");
+               public static BinaryExpression Equal (Expression left, Expression right, bool liftToNull, MethodInfo method)
+               {
+                       method = BinaryCoreCheck ("op_Equality", left, right, method);
 
-                       return new MemberInitExpression(newExpression, Enumerable.ToReadOnlyCollection<MemberBinding>(bindings));
+                       return MakeBoolBinary (ExpressionType.Equal, left, right, liftToNull, method);
                }
 
-               #region Modulo
-               public static BinaryExpression Modulo (Expression left, Expression right, MethodInfo method)
+               public static BinaryExpression NotEqual (Expression left, Expression right)
                {
-                       if (left == null)
-                               throw new ArgumentNullException ("left");
-                       if (right == null)
-                               throw new ArgumentNullException ("right");
+                       return NotEqual (left, right, false, null);
+               }
 
-                       if (method != null)
-                               return new BinaryExpression(ExpressionType.Modulo, left, right, method, method.ReturnType);
-                       
-                       // Since both the expressions define the same integer or boolean type we don't have
-                       // to look for the "op_BitwiseAnd" method.
-                       if (left.type == right.type && IsNumeric (left.type))
-                               return new BinaryExpression(ExpressionType.Modulo, left, right, left.type);
 
-                       // Else we try for a user-defined operator.
-                       return GetUserDefinedBinaryOperatorOrThrow (ExpressionType.Modulo, "op_Modulus", left, right);
-               
-               }
-               
-               public static BinaryExpression Modulo (Expression left, Expression right)
+               public static BinaryExpression NotEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
                {
-                       return Modulo (left, right, null);              
+                       method = BinaryCoreCheck ("op_Inequality", left, right, method);
+
+                       return MakeBoolBinary (ExpressionType.NotEqual, left, right, liftToNull, method);
                }
-               #endregion
-               
-               #region Multiply
-               public static BinaryExpression Multiply (Expression left, Expression right, MethodInfo method)
+
+               public static BinaryExpression GreaterThan (Expression left, Expression right)
                {
-                       if (left == null)
-                               throw new ArgumentNullException ("left");
-                       if (right == null)
-                               throw new ArgumentNullException ("right");
+                       return GreaterThan (left, right, false, null);
+               }
 
-                       if (method != null)
-                               return new BinaryExpression(ExpressionType.Multiply, left, right, method, method.ReturnType);
-                       
-                       // Since both the expressions define the same integer or boolean type we don't have
-                       // to look for the "op_BitwiseAnd" method.
-                       if (left.type == right.type && IsNumeric (left.type))
-                               return new BinaryExpression(ExpressionType.Multiply, left, right, left.type);
+               public static BinaryExpression GreaterThan (Expression left, Expression right, bool liftToNull, MethodInfo method)
+               {
+                       method = BinaryCoreCheck ("op_GreaterThan", left, right, method);
 
-                       // Else we try for a user-defined operator.
-                       return GetUserDefinedBinaryOperatorOrThrow (ExpressionType.Multiply, "op_Multiply", left, right);
-               
+                       return MakeBoolBinary (ExpressionType.GreaterThan, left, right, liftToNull, method);
                }
-               
-               public static BinaryExpression Multiply (Expression left, Expression right)
+
+               public static BinaryExpression GreaterThanOrEqual (Expression left, Expression right)
                {
-                       return Multiply (left, right, null);
+                       return GreaterThanOrEqual (left, right, false, null);
                }
-               #endregion
-               
-               #region MultiplyChecked
-               public static BinaryExpression MultiplyChecked (Expression left, Expression right, MethodInfo method)
+
+
+               public static BinaryExpression GreaterThanOrEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
                {
-                       if (left == null)
-                               throw new ArgumentNullException ("left");
-                       if (right == null)
-                               throw new ArgumentNullException ("right");
+                       method = BinaryCoreCheck ("op_GreaterThanOrEqual", left, right, method);
 
-                       if (method != null)
-                               return new BinaryExpression(ExpressionType.MultiplyChecked, left, right, method, method.ReturnType);
+                       return MakeBoolBinary (ExpressionType.GreaterThanOrEqual, left, right, liftToNull, method);
+               }
 
-                       // Since both the expressions define the same numeric type we don't have
-                       // to look for the "op_Addition" method.
-                       if (left.type == right.type && IsNumeric (left.type))
-                               return new BinaryExpression(ExpressionType.MultiplyChecked, left, right, left.type);
+               public static BinaryExpression LessThan (Expression left, Expression right)
+               {
+                       return LessThan (left, right, false, null);
+               }
 
-                       method = GetUserDefinedBinaryOperator (left.type, right.type, "op_Multiply");
-                       if (method == null)
-                               throw new InvalidOperationException(String.Format(
-                                       "The binary operator MultiplyChecked is not defined for the types '{0}' and '{1}'.", left.type, right.type));
-                       
-                       Type retType = method.ReturnType;
+               public static BinaryExpression LessThan (Expression left, Expression right, bool liftToNull, MethodInfo method)
+               {
+                       method = BinaryCoreCheck ("op_LessThan", left, right, method);
 
-                       return new BinaryExpression(ExpressionType.MultiplyChecked, left, right, method, retType);
+                       return MakeBoolBinary (ExpressionType.LessThan, left, right, liftToNull, method);
                }
-               
-               public static BinaryExpression MultiplyChecked (Expression left, Expression right)
+
+               public static BinaryExpression LessThanOrEqual (Expression left, Expression right)
                {
-                       return MultiplyChecked(left, right, null);
+                       return LessThanOrEqual (left, right, false, null);
                }
-               #endregion
-               
-               #region Or
-               public static BinaryExpression Or (Expression left, Expression right, MethodInfo method)
+
+               public static BinaryExpression LessThanOrEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
                {
-                       if (left == null)
-                               throw new ArgumentNullException ("left");
-                       if (right == null)
-                               throw new ArgumentNullException ("right");
+                       method = BinaryCoreCheck ("op_LessThanOrEqual", left, right, method);
+
+                       return MakeBoolBinary (ExpressionType.LessThanOrEqual, left, right, liftToNull, method);
+               }
 
-                       if (method != null)
-                               return new BinaryExpression(ExpressionType.Or, left, right, method, method.ReturnType);
-                       
-                       // Since both the expressions define the same integer or boolean type we don't have
-                       // to look for the "op_BitwiseOr" method.
-                       if (left.type == right.type && IsIntegerOrBool (left.type))
-                               return new BinaryExpression(ExpressionType.Or, left, right, left.type);
+               //
+               // Miscelaneous
+               //
 
-                       // Else we try for a user-defined operator.
-                       return GetUserDefinedBinaryOperatorOrThrow (ExpressionType.Or, "op_BitwiseOr", left, right);
-               
+               static void CheckArray (Expression array)
+               {
+                       if (array == null)
+                               throw new ArgumentNullException ("array");
+                       if (!array.Type.IsArray)
+                               throw new ArgumentException ("The array argument must be of type array");
                }
 
-               public static BinaryExpression Or (Expression left, Expression right)
+               public static BinaryExpression ArrayIndex (Expression array, Expression index)
                {
-                       return Or (left, right, null);
+                       CheckArray (array);
+
+                       if (index == null)
+                               throw new ArgumentNullException ("index");
+                       if (array.Type.GetArrayRank () != 1)
+                               throw new ArgumentException ("The array argument must be a single dimensional array");
+                       if (index.Type != typeof (int))
+                               throw new ArgumentException ("The index must be of type int");
+
+                       return new BinaryExpression (ExpressionType.ArrayIndex, array.Type.GetElementType (), array, index);
                }
-               #endregion
-               
-               #region OrElse
-               public static BinaryExpression OrElse (Expression left, Expression right, MethodInfo method)
+
+               public static BinaryExpression Coalesce (Expression left, Expression right)
+               {
+                       return Coalesce (left, right, null);
+               }
+
+               public static BinaryExpression Coalesce (Expression left, Expression right, LambdaExpression conversion)
                {
                        if (left == null)
                                throw new ArgumentNullException ("left");
                        if (right == null)
                                throw new ArgumentNullException ("right");
 
-                       // Since both the expressions define the same boolean type we don't have
-                       // to look for the "op_BitwiseOr" method.
-                       if (left.type == right.type && left.type == typeof(bool))
-                               return new BinaryExpression(ExpressionType.OrElse, left, right, left.type);
+                       //
+                       // First arg must ne nullable (either Nullable<T> or a reference type
+                       //
+                       if (left.Type.IsValueType && !IsNullable (left.Type))
+                               throw new InvalidOperationException ("Left expression can never be null");
 
-                       // Else we must validate the method to make sure it has companion "true" and "false" operators.
-                       if (method == null)
-                               method = GetUserDefinedBinaryOperator (left.type, right.type, "op_BitwiseOr");
-                       if (method == null)
-                               throw new InvalidOperationException(String.Format(
-                                       "The binary operator OrElse is not defined for the types '{0}' and '{1}'.", left.type, right.type));
-                       ValidateUserDefinedConditionalLogicOperator(ExpressionType.OrElse, left.type, right.type, method);
-                       
-                       return new BinaryExpression(ExpressionType.OrElse, left, right, method, method.ReturnType);
+                       Type result = null;
+
+                       if (IsNullable (left.Type)){
+                               Type lbase = GetNullableOf (left.Type);
+
+                               if (!IsNullable (right.Type) && lbase.IsAssignableFrom (right.Type))
+                                       result = lbase;
+                       }
+
+                       if (result == null && left.Type.IsAssignableFrom (right.Type))
+                               result = left.Type;
+
+                       if (result == null){
+                               if (IsNullable (left.Type) && right.Type.IsAssignableFrom (GetNullableOf (left.Type))){
+                                       result = right.Type;
+                               }
+                       }
+
+                       if (result == null)
+                               throw new ArgumentException ("Incompatible argument types");
+
+                       //
+                       // FIXME: What do we do with "conversion"?
+                       //
+                       return new BinaryExpression (ExpressionType.Coalesce, result, left, right, false, false, null, conversion);
+               }
+
+               //
+               // MakeBinary constructors
+               //
+               public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right)
+               {
+                       return MakeBinary (binaryType, left, right, false, null);
+               }
+
+               public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right, bool liftToNull, MethodInfo method)
+               {
+                       return MakeBinary (binaryType, left, right, liftToNull, method, null);
+               }
+
+               public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right, bool liftToNull, MethodInfo method, LambdaExpression conversion)
+               {
+                       switch (binaryType) {
+                       case ExpressionType.Add:
+                               return Add (left, right, method);
+                       case ExpressionType.AddChecked:
+                               return AddChecked (left, right, method);
+                       case ExpressionType.AndAlso:
+                               return AndAlso (left, right);
+                       case ExpressionType.Coalesce:
+                               return Coalesce (left, right, conversion);
+                       case ExpressionType.Divide:
+                               return Divide (left, right, method);
+                       case ExpressionType.Equal:
+                               return Equal (left, right, liftToNull, method);
+                       case ExpressionType.ExclusiveOr:
+                               return ExclusiveOr (left, right, method);
+                       case ExpressionType.GreaterThan:
+                               return GreaterThan (left, right, liftToNull, method);
+                       case ExpressionType.GreaterThanOrEqual:
+                               return GreaterThanOrEqual (left, right, liftToNull, method);
+                       case ExpressionType.LeftShift:
+                               return LeftShift (left, right, method);
+                       case ExpressionType.LessThan:
+                               return LessThan (left, right, liftToNull, method);
+                       case ExpressionType.LessThanOrEqual:
+                               return LessThanOrEqual (left, right, liftToNull, method);
+                       case ExpressionType.Modulo:
+                               return Modulo (left, right, method);
+                       case ExpressionType.Multiply:
+                               return Multiply (left, right, method);
+                       case ExpressionType.MultiplyChecked:
+                               return MultiplyChecked (left, right, method);
+                       case ExpressionType.NotEqual:
+                               return NotEqual (left, right, liftToNull, method);
+                       case ExpressionType.OrElse:
+                               return OrElse (left, right);
+                       case ExpressionType.Power:
+                               return Power (left, right, method);
+                       case ExpressionType.RightShift:
+                               return RightShift (left, right, method);
+                       case ExpressionType.Subtract:
+                               return Subtract (left, right, method);
+                       case ExpressionType.SubtractChecked:
+                               return SubtractChecked (left, right, method);
+                       case ExpressionType.And:
+                               return And (left, right, method);
+                       case ExpressionType.Or:
+                               return Or (left, right, method);
+                       }
+
+                       throw new ArgumentException ("MakeBinary expect a binary node type");
                }
-               
-               public static BinaryExpression OrElse (Expression left, Expression right)
+
+               #endregion
+
+               public static MethodCallExpression ArrayIndex (Expression array, params Expression [] indexes)
                {
-                       return OrElse(left, right, null);
+                       return ArrayIndex (array, indexes as IEnumerable<Expression>);
                }
-               #endregion
 
-               #region Property
-               public static MemberExpression Property (Expression expression, MethodInfo propertyAccessor)
+               public static MethodCallExpression ArrayIndex (Expression array, IEnumerable<Expression> indexes)
                {
-                       if (propertyAccessor == null)
-                               throw new ArgumentNullException("propertyAccessor");
-               
-                       return Property(expression, GetProperty(propertyAccessor));
+                       CheckArray (array);
+
+                       if (indexes == null)
+                               throw new ArgumentNullException ("indexes");
+
+                       var args = indexes.ToReadOnlyCollection ();
+                       if (array.Type.GetArrayRank () != args.Count)
+                               throw new ArgumentException ("The number of arguments doesn't match the rank of the array");
+
+                       foreach (var arg in args)
+                               if (arg.Type != typeof (int))
+                                       throw new ArgumentException ("The index must be of type int");
+
+                       return Call (array, array.Type.GetMethod ("Get", PublicInstance), args);
                }
 
-               public static MemberExpression Property (Expression expression, PropertyInfo property)
+               public static UnaryExpression ArrayLength (Expression array)
                {
-                       if (property == null)
-                               throw new ArgumentNullException("property");
+                       if (array == null)
+                               throw new ArgumentNullException ("array");
+                       if (!array.Type.IsArray)
+                               throw new ArgumentException ("The type of the expression must me Array");
+                       if (array.Type.GetArrayRank () != 1)
+                               throw new ArgumentException ("The array must be a single dimensional array");
 
-                       Type propertyType;
-                       ValidateGettableFieldOrPropertyMember(property, out propertyType);
-                       
-                       return new MemberExpression(expression, property, propertyType);
+                       return new UnaryExpression (ExpressionType.ArrayLength, array, typeof (int));
                }
-               
-               
-               public static MemberExpression Property(Expression expression, string propertyName)
+
+               public static MemberAssignment Bind (MemberInfo member, Expression expression)
                {
+                       if (member == null)
+                               throw new ArgumentNullException ("member");
                        if (expression == null)
                                throw new ArgumentNullException ("expression");
-                       if (propertyName == null)
-                               throw new ArgumentNullException ("propertyName");
 
-                       PropertyInfo property = expression.Type.GetProperty (propertyName,
-                               BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
+                       Type type = null;
 
-                       if (property == null)
-                               throw new ArgumentException (String.Format ("{0} is not a member of type {1}",
-                                       propertyName, expression.type.FullName));
+                       var prop = member as PropertyInfo;
+                       if (prop != null && prop.GetSetMethod (true) != null)
+                               type = prop.PropertyType;
+
+                       var field = member as FieldInfo;
+                       if (field != null)
+                               type = field.FieldType;
+
+                       if (type == null)
+                               throw new ArgumentException ("member");
+
+                       if (!type.IsAssignableFrom (expression.Type))
+                               throw new ArgumentException ("member");
 
-                       return Property (expression, property);
+                       return new MemberAssignment (member, expression);
                }
-               #endregion
-               
-               #region PropertyOrField
-               public static MemberExpression PropertyOrField(Expression expression, string propertyOrFieldName)
+
+               public static MemberAssignment Bind (MethodInfo propertyAccessor, Expression expression)
                {
+                       if (propertyAccessor == null)
+                               throw new ArgumentNullException ("propertyAccessor");
                        if (expression == null)
                                throw new ArgumentNullException ("expression");
-                       if (propertyOrFieldName == null)
-                               throw new ArgumentNullException ("propertyOrFieldName");
 
-                       PropertyInfo property = expression.type.GetProperty (propertyOrFieldName,
-                               BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
-                       if (property != null)
-                               return Property (expression, property);
+                       var prop = GetAssociatedProperty (propertyAccessor);
+                       if (prop == null)
+                               throw new ArgumentException ("propertyAccessor");
 
-                       FieldInfo field = expression.type.GetField (propertyOrFieldName,
-                               BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
-                       if (field != null)
-                               return Field (expression, field);
+                       var setter = prop.GetSetMethod (true);
+                       if (setter == null)
+                               throw new ArgumentException ("setter");
+
+                       if (!prop.PropertyType.IsAssignableFrom (expression.Type))
+                               throw new ArgumentException ("member");
 
-                       throw new ArgumentException (String.Format ("{0} is not a member of type {1}",
-                               propertyOrFieldName, expression.type.FullName));
+                       return new MemberAssignment (prop, expression);
                }
-               #endregion
-               
-               #region Quote
-               public static UnaryExpression Quote(Expression expression)
+
+               public static MethodCallExpression Call (Expression instance, MethodInfo method)
                {
-                       if (expression == null)
-                               throw new ArgumentNullException ("expression");
-                               
-                       return new UnaryExpression (ExpressionType.Quote, expression, expression.GetType());
-               
+                       return Call (instance, method, null as IEnumerable<Expression>);
                }
-               #endregion
 
-               #region RightShift
-               public static BinaryExpression RightShift (Expression left, Expression right, MethodInfo method)
+               public static MethodCallExpression Call (MethodInfo method, params Expression [] arguments)
                {
-                       if (left == null)
-                               throw new ArgumentNullException ("left");
-                       if (right == null)
-                               throw new ArgumentNullException ("right");
+                       return Call (null, method, arguments as IEnumerable<Expression>);
+               }
+
+               public static MethodCallExpression Call (Expression instance, MethodInfo method, params Expression [] arguments)
+               {
+                       return Call (instance, method, arguments as IEnumerable<Expression>);
+               }
 
-                       if (method != null)
-                               return new BinaryExpression (ExpressionType.RightShift, left, right, method, method.ReturnType);
-                       
-                       // If the left side is any kind of integer and the right is int32 we don't have
-                       // to look for the "op_Addition" method.
-                       if (IsInteger(left.type) && right.type == typeof(Int32))
-                               return new BinaryExpression (ExpressionType.RightShift, left, right, left.type);
+               public static MethodCallExpression Call (Expression instance, MethodInfo method, IEnumerable<Expression> arguments)
+               {
+                       if (method == null)
+                               throw new ArgumentNullException ("method");
+                       if (instance == null && !method.IsStatic)
+                               throw new ArgumentNullException ("instance");
+                       if (instance != null && !method.DeclaringType.IsAssignableFrom (instance.Type))
+                               throw new ArgumentException ("Type is not assignable to the declaring type of the method");
+
+                       var args = arguments.ToReadOnlyCollection ();
 
-                       // Else we try for a user-defined operator.
-                       return GetUserDefinedBinaryOperatorOrThrow (ExpressionType.RightShift, "op_RightShift", left, right);
+                       CheckMethodArguments (method, args);
+
+                       return new MethodCallExpression (instance, method, args);
                }
 
-               public static BinaryExpression RightShift (Expression left, Expression right)
+               static Type [] CollectTypes (IEnumerable<Expression> expressions)
                {
-                       return RightShift (left, right, null);
+                       return (from arg in expressions select arg.Type).ToArray ();
                }
-               #endregion
 
-               #region Subtract
-               public static BinaryExpression Subtract (Expression left, Expression right, MethodInfo method)
+               static MethodInfo TryMakeGeneric (MethodInfo method, Type [] args)
                {
-                       if (left == null)
-                               throw new ArgumentNullException ("left");
-                       if (right == null)
-                               throw new ArgumentNullException ("right");
+                       if (method == null)
+                               return null;
+
+                       if (!method.IsGenericMethod && args == null)
+                               return method;
 
-                       if (method != null)
-                               return new BinaryExpression (ExpressionType.Subtract, left, right, method, method.ReturnType);
-                       
-                       // Since both the expressions define the same numeric type we don't have
-                       // to look for the "op_Addition" method.
-                       if (left.type == right.type && IsNumeric (left.type))
-                               return new BinaryExpression (ExpressionType.Subtract, left, right, left.type);
+                       if (args.Length == method.GetGenericArguments ().Length)
+                               return method.MakeGenericMethod (args);
 
-                       // Else we try for a user-defined operator.
-                       return GetUserDefinedBinaryOperatorOrThrow (ExpressionType.Subtract, "op_Subtraction", left, right);            
+                       return null;
                }
-               
-               public static BinaryExpression Subtract (Expression left, Expression right)
+
+               public static MethodCallExpression Call (Expression instance, string methodName, Type [] typeArguments, params Expression [] arguments)
+               {
+                       if (instance == null)
+                               throw new ArgumentNullException ("instance");
+                       if (methodName == null)
+                               throw new ArgumentNullException ("methodName");
+
+                       var method = instance.Type.GetMethod (methodName, AllInstance, null, CollectTypes (arguments), null);
+                       method = TryMakeGeneric (method, typeArguments);
+                       if (method == null)
+                               throw new InvalidOperationException ("No such method");
+
+                       var args = arguments.ToReadOnlyCollection ();
+                       CheckMethodArguments (method, args);
+
+                       return new MethodCallExpression (instance, method, args);
+               }
+
+               public static MethodCallExpression Call (Type type, string methodName, Type [] typeArguments, params Expression [] arguments)
                {
-                       return Subtract (left, right, null);            
+                       if (type == null)
+                               throw new ArgumentNullException ("type");
+                       if (methodName == null)
+                               throw new ArgumentNullException ("methodName");
+
+                       var method = type.GetMethod (methodName, AllStatic, null, CollectTypes (arguments), null);
+                       method = TryMakeGeneric (method, typeArguments);
+                       if (method == null)
+                               throw new InvalidOperationException ("No such method");
+
+                       var args = arguments.ToReadOnlyCollection ();
+                       CheckMethodArguments (method, args);
+
+                       return new MethodCallExpression (method, args);
                }
-               #endregion
-               
-               #region SubtractChecked
-               public static BinaryExpression SubtractChecked (Expression left, Expression right, MethodInfo method)
+
+               public static ConditionalExpression Condition (Expression test, Expression ifTrue, Expression ifFalse)
                {
-                       if (left == null)
-                               throw new ArgumentNullException ("left");
-                       if (right == null)
-                               throw new ArgumentNullException ("right");
+                       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 ("Test expression should be of type bool");
+                       if (ifTrue.Type != ifFalse.Type)
+                               throw new ArgumentException ("The ifTrue and ifFalse type do not match");
+
+                       return new ConditionalExpression (test, ifTrue, ifFalse);
+               }
 
-                       if (method != null)
-                               return new BinaryExpression (ExpressionType.SubtractChecked, left, right, method, method.ReturnType);
+               public static ConstantExpression Constant (object value)
+               {
+                       if (value == null)
+                               return new ConstantExpression (null, typeof (object));
 
-                       // Since both the expressions define the same numeric type we don't have
-                       // to look for the "op_Addition" method.
-                       if (left.type == right.type && IsNumeric (left.type))
-                               return new BinaryExpression (ExpressionType.SubtractChecked, left, right, left.type);
+                       return Constant (value, value.GetType ());
+               }
 
-                       method = GetUserDefinedBinaryOperator (left.type, right.type, "op_Subtraction");
-                       if (method == null)
-                               throw new InvalidOperationException (String.Format (
-                                       "The binary operator AddChecked is not defined for the types '{0}' and '{1}'.", left.type, right.type));
-                       
-                       Type retType = method.ReturnType;
+               public static ConstantExpression Constant (object value, Type type)
+               {
+                       if (type == null)
+                               throw new ArgumentNullException ("type");
+
+                       //
+                       // value must be compatible with type, no conversions
+                       // are allowed
+                       //
+                       if (value == null){
+                               if (type.IsValueType && !IsNullable (type))
+                                       throw new ArgumentException ();
+                       } else {
+                               if (!(type.IsValueType && IsNullable (type)) && value.GetType () != type)
+                                       throw new ArgumentException ();
 
-                       return new BinaryExpression (ExpressionType.SubtractChecked, left, right, method, retType);
+                       }
+
+                       return new ConstantExpression (value, type);
                }
-               
-               public static BinaryExpression SubtractChecked (Expression left, Expression right)
+
+               [MonoTODO]
+               public static UnaryExpression Convert (Expression expression, Type type)
                {
-                       return SubtractChecked (left, right, null);
+                       throw new NotImplementedException ();
                }
-               #endregion
 
-               #region TypeAs
-               public static UnaryExpression TypeAs (Expression expression, Type type)
+               [MonoTODO]
+               public static UnaryExpression Convert (Expression expression, Type type, MethodInfo method)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               [MonoTODO]
+               public static UnaryExpression ConvertChecked (Expression expression, Type type)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               [MonoTODO]
+               public static UnaryExpression ConvertChecked (Expression expression, Type type, MethodInfo method)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public static ElementInit ElementInit (MethodInfo addMethod, params Expression [] arguments)
+               {
+                       return ElementInit (addMethod, arguments as IEnumerable<Expression>);
+               }
+
+               public static ElementInit ElementInit (MethodInfo addMethod, IEnumerable<Expression> arguments)
+               {
+                       if (addMethod == null)
+                               throw new ArgumentNullException ("addMethod");
+                       if (arguments == null)
+                               throw new ArgumentNullException ("arguments");
+                       if (addMethod.Name.ToLowerInvariant () != "add")
+                               throw new ArgumentException ("addMethod");
+                       if (addMethod.IsStatic)
+                               throw new ArgumentException ("addMethod must be an instance method", "addMethod");
+
+                       var args = arguments.ToReadOnlyCollection ();
+
+                       CheckMethodArguments (addMethod, args);
+
+                       return new ElementInit (addMethod, args);
+               }
+
+               public static MemberExpression Field (Expression expression, FieldInfo field)
+               {
+                       if (field == null)
+                               throw new ArgumentNullException ("field");
+                       if (!field.IsStatic) {
+                               if (expression == null)
+                                       throw new ArgumentNullException ("expression");
+                               if (!field.DeclaringType.IsAssignableFrom (expression.Type))
+                                       throw new ArgumentException ("field");
+                       }
+
+                       return new MemberExpression (expression, field, field.FieldType);
+               }
+
+               public static MemberExpression Field (Expression expression, string fieldName)
                {
                        if (expression == null)
                                throw new ArgumentNullException ("expression");
-                       if (type == null)
-                               throw new ArgumentNullException ("type");
 
-                       return new UnaryExpression (ExpressionType.TypeAs, expression, type);
+                       var field = expression.Type.GetField (fieldName, AllInstance);
+                       if (field == null)
+                               throw new ArgumentException (string.Format ("No field named {0} on {1}", fieldName, expression.Type));
+
+                       return new MemberExpression (expression, field, field.FieldType);
                }
-               #endregion
 
-               #region TypeIs
-               public static TypeBinaryExpression TypeIs (Expression expression, Type type)
+               public static Type GetActionType (params Type [] typeArgs)
+               {
+                       if (typeArgs == null)
+                               throw new ArgumentNullException ("typeArgs");
+
+                       if (typeArgs.Length > 4)
+                               throw new ArgumentException ("No Action type of this arity");
+
+                       if (typeArgs.Length == 0)
+                               return typeof (Action);
+
+                       Type action = null;
+                       switch (typeArgs.Length) {
+                       case 1:
+                               action = typeof (Action<>);
+                               break;
+                       case 2:
+                               action = typeof (Action<,>);
+                               break;
+                       case 3:
+                               action = typeof (Action<,,>);
+                               break;
+                       case 4:
+                               action = typeof (Action<,,,>);
+                               break;
+                       }
+
+                       return action.MakeGenericType (typeArgs);
+               }
+
+               public static Type GetFuncType (params Type [] typeArgs)
+               {
+                       if (typeArgs == null)
+                               throw new ArgumentNullException ("typeArgs");
+
+                       if (typeArgs.Length < 1 || typeArgs.Length > 5)
+                               throw new ArgumentException ("No Func type of this arity");
+
+                       Type func = null;
+                       switch (typeArgs.Length) {
+                       case 1:
+                               func = typeof (Func<>);
+                               break;
+                       case 2:
+                               func = typeof (Func<,>);
+                               break;
+                       case 3:
+                               func = typeof (Func<,,>);
+                               break;
+                       case 4:
+                               func = typeof (Func<,,,>);
+                               break;
+                       case 5:
+                               func = typeof (Func<,,,,>);
+                               break;
+                       }
+
+                       return func.MakeGenericType (typeArgs);
+               }
+
+               public static InvocationExpression Invoke (Expression expression, params Expression [] arguments)
+               {
+                       return Invoke (expression, arguments as IEnumerable<Expression>);
+               }
+
+               static Type GetInvokableType (Type t)
+               {
+                       if (typeof (Delegate).IsAssignableFrom (t))
+                               return t;
+
+                       return GetGenericType (t, typeof (Expression<>));
+               }
+
+               static Type GetGenericType (Type t, Type def)
+               {
+                       if (t == null)
+                               return null;
+
+                       if (t.IsGenericType && t.GetGenericTypeDefinition () == def)
+                               return t;
+
+                       return GetGenericType (t.BaseType, def);
+               }
+
+               public static InvocationExpression Invoke (Expression expression, IEnumerable<Expression> arguments)
                {
                        if (expression == null)
                                throw new ArgumentNullException ("expression");
+
+                       var type = GetInvokableType (expression.Type);
                        if (type == null)
-                               throw new ArgumentNullException ("type"); 
-                       
-                       return new TypeBinaryExpression (ExpressionType.TypeIs, expression, type, typeof(bool));
+                               throw new ArgumentException ("The type of the expression is not invokable");
+
+                       var args = arguments.ToReadOnlyCollection ();
+                       CheckForNull (args, "arguments");
+
+                       var invoke = type.GetMethod ("Invoke");
+                       if (invoke == null)
+                               throw new ArgumentException ("expression");
+
+                       if (invoke.GetParameters ().Length != args.Count)
+                               throw new InvalidOperationException ("Arguments count doesn't match parameters length");
+
+                       CheckMethodArguments (invoke, args);
+
+                       return new InvocationExpression (expression, invoke.ReturnType, args);
                }
-               #endregion
+
+               public static Expression<TDelegate> Lambda<TDelegate> (Expression body, params ParameterExpression [] parameters)
+               {
+                       return Lambda<TDelegate> (body, parameters as IEnumerable<ParameterExpression>);
+               }
+
+               public static Expression<TDelegate> Lambda<TDelegate> (Expression body, IEnumerable<ParameterExpression> parameters)
+               {
+                       if (body == null)
+                               throw new ArgumentNullException ("body");
+
+                       return new Expression<TDelegate> (body, parameters.ToReadOnlyCollection ());
+               }
+
+               public static LambdaExpression Lambda (Expression body, params ParameterExpression [] parameters)
+               {
+                       if (body == null)
+                               throw new ArgumentNullException ("body");
+                       if (parameters.Length > 4)
+                               throw new ArgumentException ("Too many parameters");
+
+                       return Lambda (GetDelegateType (body.Type, parameters), body, parameters);
+               }
+
+               static Type GetDelegateType (Type return_type, ParameterExpression [] parameters)
+               {
+                       if (parameters == null)
+                               parameters = new ParameterExpression [0];
+
+                       if (return_type == typeof (void))
+                               return GetActionType (parameters.Select (p => p.Type).ToArray ());
+
+                       var types = new Type [parameters.Length + 1];
+                       for (int i = 0; i < types.Length - 1; i++)
+                               types [i] = parameters [i].Type;
+
+                       types [types.Length - 1] = return_type;
+                       return GetFuncType (types);
+               }
+
+               public static LambdaExpression Lambda (Type delegateType, Expression body, params ParameterExpression [] parameters)
+               {
+                       return Lambda (delegateType, body, parameters as IEnumerable<ParameterExpression>);
+               }
+
+               public static LambdaExpression Lambda (Type delegateType, Expression body, IEnumerable<ParameterExpression> parameters)
+               {
+                       if (delegateType == null)
+                               throw new ArgumentNullException ("delegateType");
+                       if (body == null)
+                               throw new ArgumentNullException ("body");
+
+                       return new LambdaExpression (delegateType, body, parameters.ToReadOnlyCollection ());
+               }
+
+               public static MemberListBinding ListBind (MemberInfo member, params ElementInit [] initializers)
+               {
+                       return ListBind (member, initializers as IEnumerable<ElementInit>);
+               }
+
+               static void CheckIsAssignableToIEnumerable (Type t)
+               {
+                       if (!typeof (IEnumerable).IsAssignableFrom (t))
+                               throw new ArgumentException (string.Format ("Type {0} doesn't implemen IEnumerable", t));
+               }
+
+               public static MemberListBinding ListBind (MemberInfo member, IEnumerable<ElementInit> initializers)
+               {
+                       if (member == null)
+                               throw new ArgumentNullException ("member");
+                       if (initializers == null)
+                               throw new ArgumentNullException ("initializers");
+
+                       var inits = initializers.ToReadOnlyCollection ();
+                       CheckForNull (inits, "initializers");
+
+                       switch (member.MemberType) {
+                       case MemberTypes.Field:
+                               CheckIsAssignableToIEnumerable ((member as FieldInfo).FieldType);
+                               break;
+                       case MemberTypes.Property:
+                               CheckIsAssignableToIEnumerable ((member as PropertyInfo).PropertyType);
+                               break;
+                       default:
+                               throw new ArgumentException ("member");
+                       }
+
+                       return new MemberListBinding (member, inits);
+               }
+
+               public static MemberListBinding ListBind (MethodInfo propertyAccessor, params ElementInit [] initializers)
+               {
+                       return ListBind (propertyAccessor, initializers as IEnumerable<ElementInit>);
+               }
+
+               static void CheckForNull<T> (ReadOnlyCollection<T> collection, string name) where T : class
+               {
+                       foreach (var t in collection)
+                               if (t == null)
+                                       throw new ArgumentNullException (name);
+               }
+
+               public static MemberListBinding ListBind (MethodInfo propertyAccessor, IEnumerable<ElementInit> initializers)
+               {
+                       if (propertyAccessor == null)
+                               throw new ArgumentNullException ("propertyAccessor");
+                       if (initializers == null)
+                               throw new ArgumentNullException ("initializers");
+
+                       var inits = initializers.ToReadOnlyCollection ();
+                       CheckForNull (inits, "initializers");
+
+                       var prop = GetAssociatedProperty (propertyAccessor);
+                       if (prop == null)
+                               throw new ArgumentException ("propertyAccessor");
+
+                       CheckIsAssignableToIEnumerable (prop.PropertyType);
+
+                       return new MemberListBinding (prop, inits);
+               }
+
+               public static ListInitExpression ListInit (NewExpression newExpression, params ElementInit [] initializers)
+               {
+                       return ListInit (newExpression, initializers as IEnumerable<ElementInit>);
+               }
+
+               public static ListInitExpression ListInit (NewExpression newExpression, IEnumerable<ElementInit> initializers)
+               {
+                       var inits = CheckListInit (newExpression, initializers);
+
+                       return new ListInitExpression (newExpression, inits);
+               }
+
+               public static ListInitExpression ListInit (NewExpression newExpression, params Expression [] initializers)
+               {
+                       return ListInit (newExpression, initializers as IEnumerable<Expression>);
+               }
+
+               public static ListInitExpression ListInit (NewExpression newExpression, IEnumerable<Expression> initializers)
+               {
+                       var inits = CheckListInit (newExpression, initializers);
+
+                       var add_method = GetAddMethod (newExpression.Type, inits [0].Type);
+                       if (add_method == null)
+                               throw new InvalidOperationException ("No suitable add method found");
+
+                       return new ListInitExpression (newExpression, CreateInitializers (add_method, inits));
+               }
+
+               static ReadOnlyCollection<ElementInit> CreateInitializers (MethodInfo add_method, ReadOnlyCollection<Expression> initializers)
+               {
+                       return (from init in initializers select Expression.ElementInit (add_method, init)).ToReadOnlyCollection ();
+               }
+
+               static MethodInfo GetAddMethod (Type type, Type arg)
+               {
+                       return type.GetMethod ("Add", PublicInstance | BindingFlags.IgnoreCase, null, new [] { arg }, null);
+               }
+
+               public static ListInitExpression ListInit (NewExpression newExpression, MethodInfo addMethod, params Expression [] initializers)
+               {
+                       return ListInit (newExpression, addMethod, initializers as IEnumerable<Expression>);
+               }
+
+               static ReadOnlyCollection<T> CheckListInit<T> (NewExpression newExpression, IEnumerable<T> initializers) where T : class
+               {
+                       if (newExpression == null)
+                               throw new ArgumentNullException ("newExpression");
+                       if (initializers == null)
+                               throw new ArgumentNullException ("initializers");
+                       if (!typeof (IEnumerable).IsAssignableFrom (newExpression.Type))
+                               throw new InvalidOperationException ("The type of the new expression does not implement IEnumerable");
+
+                       var inits = initializers.ToReadOnlyCollection ();
+                       if (inits.Count == 0)
+                               throw new ArgumentException ("Empty initializers");
+
+                       CheckForNull (inits, "initializers");
+
+                       return inits;
+               }
+
+               public static ListInitExpression ListInit (NewExpression newExpression, MethodInfo addMethod, IEnumerable<Expression> initializers)
+               {
+                       var inits = CheckListInit (newExpression, initializers);
+
+                       if (addMethod != null) {
+                               if (addMethod.Name.ToLowerInvariant () != "add")
+                                       throw new ArgumentException ("addMethod");
+
+                               var parameters = addMethod.GetParameters ();
+                               if (parameters.Length != 1)
+                                       throw new ArgumentException ("addMethod");
+
+                               var type = parameters [0].ParameterType;
+
+                               foreach (var exp in inits)
+                                       if (!type.IsAssignableFrom (exp.Type))
+                                               throw new InvalidOperationException ("Initializer not assignable to the add method parameter type");
+                       }
+
+                       if (addMethod == null)
+                               addMethod = GetAddMethod (newExpression.Type, inits [0].Type);
+
+                       if (addMethod == null)
+                               throw new InvalidOperationException ("No suitable add method found");
+
+                       return new ListInitExpression (newExpression, CreateInitializers (addMethod, inits));
+               }
+
+               public static MemberExpression MakeMemberAccess (Expression expression, MemberInfo member)
+               {
+                       if (expression == null)
+                               throw new ArgumentNullException ("expression");
+                       if (member == null)
+                               throw new ArgumentNullException ("member");
+
+                       var field = member as FieldInfo;
+                       if (field != null)
+                               return Field (expression, field);
+
+                       var property = member as PropertyInfo;
+                       if (property != null)
+                               return Property (expression, property);
+
+                       throw new ArgumentException ("Member should either be a field or a property");
+               }
+
+               public static UnaryExpression MakeUnary (ExpressionType unaryType, Expression operand, Type type)
+               {
+                       return MakeUnary (unaryType, operand, type, null);
+               }
+
+               public static UnaryExpression MakeUnary (ExpressionType unaryType, Expression operand, Type type, MethodInfo method)
+               {
+                       switch (unaryType) {
+                       case ExpressionType.ArrayLength:
+                               return ArrayLength (operand);
+                       case ExpressionType.Convert:
+                               return Convert (operand, type, method);
+                       case ExpressionType.ConvertChecked:
+                               return ConvertChecked (operand, type, method);
+                       case ExpressionType.Negate:
+                               return Negate (operand, method);
+                       case ExpressionType.NegateChecked:
+                               return NegateChecked (operand, method);
+                       case ExpressionType.Not:
+                               return Not (operand, method);
+                       case ExpressionType.Quote:
+                               return Quote (operand);
+                       case ExpressionType.TypeAs:
+                               return TypeAs (operand, type);
+                       case ExpressionType.UnaryPlus:
+                               return UnaryPlus (operand, method);
+                       }
+
+                       throw new ArgumentException ("MakeUnary expect an unary operator");
+               }
+
+               public static MemberMemberBinding MemberBind (MemberInfo member, params MemberBinding [] bindings)
+               {
+                       return MemberBind (member, bindings as IEnumerable<MemberBinding>);
+               }
+
+               public static MemberMemberBinding MemberBind (MemberInfo member, IEnumerable<MemberBinding> bindings)
+               {
+                       if (member == null)
+                               throw new ArgumentNullException ("member");
+
+                       Type type = null;
+                       switch (member.MemberType) {
+                       case MemberTypes.Field:
+                               type = (member as FieldInfo).FieldType;
+                               break;
+                       case MemberTypes.Property:
+                               type = (member as PropertyInfo).PropertyType;
+                               break;
+                       default:
+                               throw new ArgumentException ("Member is neither a field or a property");
+                       }
+
+                       return new MemberMemberBinding (member, CheckMemberBindings (type, bindings));
+               }
+
+               public static MemberMemberBinding MemberBind (MethodInfo propertyAccessor, params MemberBinding [] bindings)
+               {
+                       return MemberBind (propertyAccessor, bindings as IEnumerable<MemberBinding>);
+               }
+
+               public static MemberMemberBinding MemberBind (MethodInfo propertyAccessor, IEnumerable<MemberBinding> bindings)
+               {
+                       if (propertyAccessor == null)
+                               throw new ArgumentNullException ("propertyAccessor");
+
+                       var bds = bindings.ToReadOnlyCollection ();
+                       CheckForNull (bds, "bindings");
+
+                       var prop = GetAssociatedProperty (propertyAccessor);
+                       if (prop == null)
+                               throw new ArgumentException ("propertyAccessor");
+
+                       return new MemberMemberBinding (prop, CheckMemberBindings (prop.PropertyType, bindings));
+               }
+
+               static ReadOnlyCollection<MemberBinding> CheckMemberBindings (Type type, IEnumerable<MemberBinding> bindings)
+               {
+                       if (bindings == null)
+                               throw new ArgumentNullException ("bindings");
+
+                       var bds = bindings.ToReadOnlyCollection ();
+                       CheckForNull (bds, "bindings");
+
+                       foreach (var binding in bds)
+                               if (!binding.Member.DeclaringType.IsAssignableFrom (type))
+                                       throw new ArgumentException ("Type not assignable to member type");
+
+                       return bds;
+               }
+
+               public static MemberInitExpression MemberInit (NewExpression newExpression, params MemberBinding [] bindings)
+               {
+                       return MemberInit (newExpression, bindings as IEnumerable<MemberBinding>);
+               }
+
+               public static MemberInitExpression MemberInit (NewExpression newExpression, IEnumerable<MemberBinding> bindings)
+               {
+                       if (newExpression == null)
+                               throw new ArgumentNullException ("newExpression");
+
+                       return new MemberInitExpression (newExpression, CheckMemberBindings (newExpression.Type, bindings));
+               }
+
+               public static UnaryExpression Negate (Expression expression)
+               {
+                       return Negate (expression, null);
+               }
+
+               public static UnaryExpression Negate (Expression expression, MethodInfo method)
+               {
+                       method = UnaryCoreCheck ("op_UnaryNegation", expression, method);
+
+                       return MakeSimpleUnary (ExpressionType.Negate, expression, method);
+               }
+
+               public static UnaryExpression NegateChecked (Expression expression)
+               {
+                       return NegateChecked (expression, null);
+               }
+
+               public static UnaryExpression NegateChecked (Expression expression, MethodInfo method)
+               {
+                       method = UnaryCoreCheck ("op_UnaryNegation", expression, method);
+
+                       return MakeSimpleUnary (ExpressionType.Negate, expression, method);
+               }
+
+               public static NewExpression New (ConstructorInfo constructor)
+               {
+                       if (constructor == null)
+                               throw new ArgumentNullException ("constructor");
+
+                       if (constructor.GetParameters ().Length > 0)
+                               throw new ArgumentException ("Constructor must be parameter less");
+
+                       return new NewExpression (constructor, (null as IEnumerable<Expression>).ToReadOnlyCollection (), null);
+               }
+
+               public static NewExpression New (Type type)
+               {
+                       if (type == null)
+                               throw new ArgumentNullException ("type");
+
+                       var args = (null as IEnumerable<Expression>).ToReadOnlyCollection ();
+
+                       if (type.IsValueType)
+                               return new NewExpression (type, args);
+
+                       var ctor = type.GetConstructor (Type.EmptyTypes);
+                       if (ctor == null)
+                               throw new ArgumentException ("Type doesn't have a parameter less constructor");
+
+                       return new NewExpression (ctor, args, null);
+               }
+
+               public static NewExpression New (ConstructorInfo constructor, params Expression [] arguments)
+               {
+                       return New (constructor, arguments as IEnumerable<Expression>);
+               }
+
+               public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments)
+               {
+                       if (constructor == null)
+                               throw new ArgumentNullException ("constructor");
+
+                       var args = arguments.ToReadOnlyCollection ();
+
+                       CheckMethodArguments (constructor, args);
+
+                       return new NewExpression (constructor, args, null);
+               }
+
+               static void CheckMethodArguments (MethodBase method, ReadOnlyCollection<Expression> arguments)
+               {
+                       var parameters = method.GetParameters ();
+
+                       if (arguments.Count != parameters.Length)
+                               throw new ArgumentException ("The number of arguments doesn't match the number of parameters");
+
+                       for (int i = 0; i < parameters.Length; i++) {
+                               if (arguments [i] == null)
+                                       throw new ArgumentNullException ("arguments");
+
+                               if (!parameters [i].ParameterType.IsAssignableFrom (arguments [i].Type))
+                                       throw new ArgumentException ("arguments");
+                       }
+               }
+
+               public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments, params MemberInfo [] members)
+               {
+                       return New (constructor, arguments, members as IEnumerable<MemberInfo>);
+               }
+
+               public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments, IEnumerable<MemberInfo> members)
+               {
+                       if (constructor == null)
+                               throw new ArgumentNullException ("constructor");
+
+                       var args = arguments.ToReadOnlyCollection ();
+                       var mmbs = members.ToReadOnlyCollection ();
+
+                       CheckForNull (args, "arguments");
+                       CheckForNull (mmbs, "members");
+
+                       CheckMethodArguments (constructor, args);
+
+                       if (args.Count != mmbs.Count)
+                               throw new ArgumentException ("Arguments count does not match members count");
+
+                       for (int i = 0; i < mmbs.Count; i++) {
+                               var member = mmbs [i];
+                               Type type = null;
+                               switch (member.MemberType) {
+                               case MemberTypes.Field:
+                                       type = (member as FieldInfo).FieldType;
+                                       break;
+                               case MemberTypes.Method:
+                                       type = (member as MethodInfo).ReturnType;
+                                       break;
+                               case MemberTypes.Property:
+                                       var prop = member as PropertyInfo;
+                                       if (prop.GetGetMethod (true) == null)
+                                               throw new ArgumentException ("Property must have a getter");
+
+                                       type = (member as PropertyInfo).PropertyType;
+                                       break;
+                               default:
+                                       throw new ArgumentException ("Member type not allowed");
+                               }
+
+                               if (!type.IsAssignableFrom (args [i].Type))
+                                       throw new ArgumentException ("Argument type not assignable to member type");
+                       }
+
+                       return new NewExpression (constructor, args, mmbs);
+               }
+
+               public static NewArrayExpression NewArrayBounds (Type type, params Expression [] bounds)
+               {
+                       return NewArrayBounds (type, bounds as IEnumerable<Expression>);
+               }
+
+               public static NewArrayExpression NewArrayBounds (Type type, IEnumerable<Expression> bounds)
+               {
+                       if (type == null)
+                               throw new ArgumentNullException ("type");
+                       if (bounds == null)
+                               throw new ArgumentNullException ("bounds");
+
+                       var array_bounds = bounds.ToReadOnlyCollection ();
+                       foreach (var expression in array_bounds)
+                               if (!IsInt (expression.Type))
+                                       throw new ArgumentException ("The bounds collection can only contain expression of integers types");
+
+                       return new NewArrayExpression (ExpressionType.NewArrayBounds, type.MakeArrayType (array_bounds.Count), array_bounds);
+               }
+
+               public static NewArrayExpression NewArrayInit (Type type, params Expression [] initializers)
+               {
+                       return NewArrayInit (type, initializers as IEnumerable<Expression>);
+               }
+
+               public static NewArrayExpression NewArrayInit (Type type, IEnumerable<Expression> initializers)
+               {
+                       if (type == null)
+                               throw new ArgumentNullException ("type");
+                       if (initializers == null)
+                               throw new ArgumentNullException ("initializers");
+
+                       var array_initializers = initializers.ToReadOnlyCollection ();
+
+                       foreach (var expression in initializers) {
+                               if (expression == null)
+                                       throw new ArgumentNullException ("initializers");
+
+                               if (!type.IsAssignableFrom (expression.Type))
+                                       throw new InvalidOperationException ();
+
+                               // TODO: Quote elements if type == typeof (Expression)
+                       }
+
+                       return new NewArrayExpression (ExpressionType.NewArrayInit, type.MakeArrayType (), array_initializers);
+               }
+
+               public static UnaryExpression Not (Expression expression)
+               {
+                       return Not (expression, null);
+               }
+
+               public static UnaryExpression Not (Expression expression, MethodInfo method)
+               {
+                       method = UnaryCoreCheck ("op_LogicalNot", expression, method);
+
+                       return MakeSimpleUnary (ExpressionType.Not, expression, method);
+               }
+
+               public static ParameterExpression Parameter (Type type, string name)
+               {
+                       if (type == null)
+                               throw new ArgumentNullException ("type");
+
+                       return new ParameterExpression (type, name);
+               }
+
+               public static MemberExpression Property (Expression expression, MethodInfo propertyAccessor)
+               {
+                       if (propertyAccessor == null)
+                               throw new ArgumentNullException ("propertyAccessor");
+
+                       if (!propertyAccessor.IsStatic) {
+                               if (expression == null)
+                                       throw new ArgumentNullException ("expression");
+                               if (!propertyAccessor.DeclaringType.IsAssignableFrom (expression.Type))
+                                       throw new ArgumentException ("expression");
+                       }
+
+                       var prop = GetAssociatedProperty (propertyAccessor);
+                       if (prop == null)
+                               throw new ArgumentException (string.Format ("Method {0} has no associated property", propertyAccessor));
+
+                       return new MemberExpression (expression, prop, prop.PropertyType);
+               }
+
+               static PropertyInfo GetAssociatedProperty (MethodInfo method)
+               {
+                       foreach (var prop in method.DeclaringType.GetProperties (All)) {
+                               if (prop.GetGetMethod (true) == method)
+                                       return prop;
+                               if (prop.GetSetMethod (true) ==  method)
+                                       return prop;
+                       }
+
+                       return null;
+               }
+
+               public static MemberExpression Property (Expression expression, PropertyInfo property)
+               {
+                       if (property == null)
+                               throw new ArgumentNullException ("property");
+
+                       var getter = property.GetGetMethod (true);
+                       if (getter == null)
+                               throw new ArgumentException ("getter");
+
+                       if (!getter.IsStatic) {
+                               if (expression == null)
+                                       throw new ArgumentNullException ("expression");
+                               if (!property.DeclaringType.IsAssignableFrom (expression.Type))
+                                       throw new ArgumentException ("expression");
+                       }
+
+                       return new MemberExpression (expression, property, property.PropertyType);
+               }
+
+               public static MemberExpression Property (Expression expression, string propertyName)
+               {
+                       if (expression == null)
+                               throw new ArgumentNullException ("expression");
+
+                       var prop = expression.Type.GetProperty (propertyName, AllInstance);
+                       if (prop == null)
+                               throw new ArgumentException (string.Format ("No property named {0} on {1}", propertyName, expression.Type));
+
+                       return new MemberExpression (expression, prop, prop.PropertyType);
+               }
+
+               public static MemberExpression PropertyOrField (Expression expression, string propertyOrFieldName)
+               {
+                       if (expression == null)
+                               throw new ArgumentNullException ("expression");
+                       if (propertyOrFieldName == null)
+                               throw new ArgumentNullException ("propertyOrFieldName");
+
+                       var prop = expression.Type.GetProperty (propertyOrFieldName, AllInstance);
+                       if (prop != null)
+                               return new MemberExpression (expression, prop, prop.PropertyType);
+
+                       var field = expression.Type.GetField (propertyOrFieldName, AllInstance);
+                       if (field != null)
+                               return new MemberExpression (expression, field, field.FieldType);
+
+                       throw new ArgumentException (string.Format ("No field or property named {0} on {1}", propertyOrFieldName, expression.Type));
+               }
+
+               public static UnaryExpression Quote (Expression expression)
+               {
+                       if (expression == null)
+                               throw new ArgumentNullException ("expression");
+
+                       return new UnaryExpression (ExpressionType.Quote, expression, expression.GetType ());
+               }
+
+               public static UnaryExpression TypeAs (Expression expression, Type type)
+               {
+                       if (expression == null)
+                               throw new ArgumentNullException ("expression");
+                       if (type == null)
+                               throw new ArgumentNullException ("type");
+                       if (type.IsValueType && !IsNullable (type))
+                               throw new ArgumentException ("TypeAs expect a reference or a nullable type");
+
+                       return new UnaryExpression (ExpressionType.TypeAs, expression, type);
+               }
+
+               public static TypeBinaryExpression TypeIs (Expression expression, Type type)
+               {
+                       if (expression == null)
+                               throw new ArgumentNullException ("expression");
+                       if (type == null)
+                               throw new ArgumentNullException ("type");
+
+                       return new TypeBinaryExpression (ExpressionType.TypeIs, expression, type, typeof (bool));
+               }
+
+               public static UnaryExpression UnaryPlus (Expression expression)
+               {
+                       return UnaryPlus (expression, null);
+               }
+
+               public static UnaryExpression UnaryPlus (Expression expression, MethodInfo method)
+               {
+                       method = UnaryCoreCheck ("op_UnaryPlus", expression, method);
+
+                       return MakeSimpleUnary (ExpressionType.UnaryPlus, expression, method);
+               }
+
+               static bool IsInt (Type t)
+               {
+                       return t == typeof (byte) || t == typeof (sbyte) ||
+                               t == typeof (short) || t == typeof (ushort) ||
+                               t == typeof (int) || t == typeof (uint) ||
+                               t == typeof (long) || t == typeof (ulong);
+               }
+
+               static bool IsIntOrBool (Type t)
+               {
+                       return IsInt (t) || t == typeof (bool);
+               }
+
+               static bool IsNumber (Type t)
+               {
+                       if (IsInt (t))
+                               return true;
+
+                       return t == typeof (float) || t == typeof (double) || t == typeof (decimal);
+               }
+
+               internal static bool IsNullable (Type type)
+               {
+                       return type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>);
+               }
+
+               internal static bool IsUnsigned (Type t)
+               {
+                       if (t.IsPointer)
+                               return IsUnsigned (t.GetElementType ());
+
+                       return t == typeof (ushort) ||
+                               t == typeof (uint) ||
+                               t == typeof (ulong) ||
+                               t == typeof (byte);
+               }
+
+               //
+               // returns the T in a a Nullable<T> type.
+               //
+               internal static Type GetNullableOf (Type type)
+               {
+                       return type.GetGenericArguments () [0];
+               }
+
+               internal static Type GetNotNullableOf (Type type)
+               {
+                       return IsNullable (type) ? GetNullableOf (type) : type;
+               }
+
+               //
+               // This method must be overwritten by derived classes to
+               // compile the expression
+               //
+               internal abstract void Emit (EmitContext ec);
        }
 }