entry for a change in HttpResponseStream.cs
[mono.git] / mcs / mcs / expression.cs
index 8c2b12231c32cedff0fb94c643d82dd984173c89..84980efd5f599e287b40d3b870acd2b1e9f83c56 100644 (file)
 
 namespace Mono.CSharp {
        using System;
-       using System.Collections;
+       using System.Collections.Generic;
        using System.Reflection;
        using System.Reflection.Emit;
        using System.Text;
+       using System.Linq;
+       using SLE = System.Linq.Expressions;
 
        //
        // This is an user operator expression, automatically created during
@@ -34,7 +36,7 @@ namespace Mono.CSharp {
                        this.arguments = args;
                        this.expr_tree = expr_tree;
 
-                       type = TypeManager.TypeToCoreType (((MethodInfo) mg).ReturnType);
+                       type = TypeManager.TypeToCoreType (((MethodSpec) mg).ReturnType);
                        eclass = ExprClass.Value;
                        this.loc = loc;
                }
@@ -48,7 +50,7 @@ namespace Mono.CSharp {
                                new NullLiteral (loc),
                                mg.CreateExpressionTree (ec));
 
-                       return CreateExpressionFactoryCall ("Call", args);
+                       return CreateExpressionFactoryCall (ec, "Call", args);
                }
 
                protected override void CloneTo (CloneContext context, Expression target)
@@ -56,7 +58,7 @@ namespace Mono.CSharp {
                        // Nothing to clone
                }
                
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        //
                        // We are born fully resolved
@@ -69,6 +71,12 @@ namespace Mono.CSharp {
                        mg.EmitCall (ec, arguments);
                }
 
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
+               {
+                       var method = ((MethodSpec) mg).MetaInfo as MethodInfo;
+                       return SLE.Expression.Call (method, Arguments.MakeExpression (arguments, ctx));
+               }
+
                public MethodGroupExpr Method {
                        get { return mg; }
                }
@@ -80,42 +88,22 @@ namespace Mono.CSharp {
                }
        }
 
-       public class ParenthesizedExpression : Expression
+       public class ParenthesizedExpression : ShimExpression
        {
-               public Expression Expr;
-
                public ParenthesizedExpression (Expression expr)
+                       : base (expr)
                {
-                       Expr = expr;
                        loc = expr.Location;
                }
 
-               public override Expression CreateExpressionTree (ResolveContext ec)
-               {
-                       throw new NotSupportedException ("ET");
-               }
-
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       Expr = Expr.Resolve (ec);
-                       return Expr;
+                       return expr.Resolve (ec);
                }
 
                public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
-                       return Expr.DoResolveLValue (ec, right_side);
-               }
-
-               public override void Emit (EmitContext ec)
-               {
-                       throw new Exception ("Should not happen");
-               }
-
-               protected override void CloneTo (CloneContext clonectx, Expression t)
-               {
-                       ParenthesizedExpression target = (ParenthesizedExpression) t;
-
-                       target.Expr = Expr.Clone (clonectx);
+                       return expr.DoResolveLValue (ec, right_side);
                }
        }
        
@@ -135,11 +123,11 @@ namespace Mono.CSharp {
                public Expression Expr;
                Expression enum_conversion;
 
-               public Unary (Operator op, Expression expr)
+               public Unary (Operator op, Expression expr, Location loc)
                {
                        Oper = op;
                        Expr = expr;
-                       loc = expr.Location;
+                       this.loc = loc;
                }
 
                // <summary>
@@ -200,7 +188,7 @@ namespace Mono.CSharp {
                                        int value = ((IntConstant)e).Value;
                                        if (value == int.MinValue) {
                                                if (ec.ConstantCheckState) {
-                                                       ConstantFold.Error_CompileTimeOverflow (loc);
+                                                       ConstantFold.Error_CompileTimeOverflow (ec, loc);
                                                        return null;
                                                }
                                                return e;
@@ -211,7 +199,7 @@ namespace Mono.CSharp {
                                        long value = ((LongConstant)e).Value;
                                        if (value == long.MinValue) {
                                                if (ec.ConstantCheckState) {
-                                                       ConstantFold.Error_CompileTimeOverflow (loc);
+                                                       ConstantFold.Error_CompileTimeOverflow (ec, loc);
                                                        return null;
                                                }
                                                return e;
@@ -222,7 +210,7 @@ namespace Mono.CSharp {
                                if (expr_type == TypeManager.uint32_type) {
                                        UIntLiteral uil = e as UIntLiteral;
                                        if (uil != null) {
-                                               if (uil.Value == 2147483648)
+                                               if (uil.Value == int.MaxValue + (uint) 1)
                                                        return new IntLiteral (int.MinValue, e.Location);
                                                return new LongLiteral (-uil.Value, e.Location);
                                        }
@@ -353,10 +341,10 @@ namespace Mono.CSharp {
                        string method_name;
                        switch (Oper) {
                        case Operator.AddressOf:
-                               Error_PointerInsideExpressionTree ();
+                               Error_PointerInsideExpressionTree (ec);
                                return null;
                        case Operator.UnaryNegation:
-                               if (ec.HasSet (EmitContext.Options.CheckedScope) && user_op == null && !IsFloat (type))
+                               if (ec.HasSet (ResolveContext.Options.CheckedScope) && user_op == null && !IsFloat (type))
                                        method_name = "NegateChecked";
                                else
                                        method_name = "Negate";
@@ -376,7 +364,7 @@ namespace Mono.CSharp {
                        args.Add (new Argument (Expr.CreateExpressionTree (ec)));
                        if (user_op != null)
                                args.Add (new Argument (user_op.CreateExpressionTree (ec)));
-                       return CreateExpressionFactoryCall (method_name, args);
+                       return CreateExpressionFactoryCall (ec, method_name, args);
                }
 
                static void CreatePredefinedOperatorsTable ()
@@ -437,7 +425,7 @@ namespace Mono.CSharp {
                        return expr;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (Oper == Operator.AddressOf) {
                                return ResolveAddressOf (ec);
@@ -450,11 +438,11 @@ namespace Mono.CSharp {
                        if (TypeManager.IsDynamicType (Expr.Type)) {
                                Arguments args = new Arguments (1);
                                args.Add (new Argument (Expr));
-                               return new DynamicUnaryConversion (GetOperatorExpressionTypeName (), args, loc).DoResolve (ec);
+                               return new DynamicUnaryConversion (GetOperatorExpressionTypeName (), args, loc).Resolve (ec);
                        }
 
                        if (TypeManager.IsNullableType (Expr.Type))
-                               return new Nullable.LiftedUnaryOperator (Oper, Expr).Resolve (ec);
+                               return new Nullable.LiftedUnaryOperator (Oper, Expr, loc).Resolve (ec);
 
                        //
                        // Attempt to use a constant folding operation.
@@ -463,12 +451,12 @@ namespace Mono.CSharp {
                        if (cexpr != null) {
                                cexpr = TryReduceConstant (ec, cexpr);
                                if (cexpr != null)
-                                       return cexpr;
+                                       return cexpr.Resolve (ec);
                        }
 
                        Expression expr = ResolveOperator (ec, Expr);
                        if (expr == null)
-                               Error_OperatorCannotBeApplied (loc, OperName (Oper), Expr.Type);
+                               Error_OperatorCannotBeApplied (ec, loc, OperName (Oper), Expr.Type);
                        
                        //
                        // Reduce unary operator on predefined types
@@ -552,9 +540,9 @@ namespace Mono.CSharp {
                        Expr.EmitSideEffect (ec);
                }
 
-               public static void Error_OperatorCannotBeApplied (Location loc, string oper, Type t)
+               public static void Error_OperatorCannotBeApplied (ResolveContext ec, Location loc, string oper, Type t)
                {
-                       Report.Error (23, loc, "The `{0}' operator cannot be applied to operand of type `{1}'",
+                       ec.Report.Error (23, loc, "The `{0}' operator cannot be applied to operand of type `{1}'",
                                oper, TypeManager.CSharpName (t));
                }
 
@@ -603,6 +591,25 @@ namespace Mono.CSharp {
                        throw new NotImplementedException (oper.ToString ());
                }
 
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
+               {
+                       var expr = Expr.MakeExpression (ctx);
+                       bool is_checked = ctx.HasSet (BuilderContext.Options.CheckedScope);
+
+                       switch (Oper) {
+                       case Operator.UnaryNegation:
+                               return is_checked ? SLE.Expression.NegateChecked (expr) : SLE.Expression.Negate (expr);
+                       case Operator.LogicalNot:
+                               return SLE.Expression.Not (expr);
+#if NET_4_0
+                       case Operator.OnesComplement:
+                               return SLE.Expression.OnesComplement (expr);
+#endif
+                       default:
+                               throw new NotImplementedException (Oper.ToString ());
+                       }
+               }
+
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
                        type = storey.MutateType (type);
@@ -611,16 +618,16 @@ namespace Mono.CSharp {
 
                Expression ResolveAddressOf (ResolveContext ec)
                {
-                       if (!ec.InUnsafe)
-                               UnsafeError (loc);
+                       if (!ec.IsUnsafe)
+                               UnsafeError (ec, loc);
 
                        Expr = Expr.DoResolveLValue (ec, EmptyExpression.UnaryAddress);
                        if (Expr == null || Expr.eclass != ExprClass.Variable) {
-                               Error (211, "Cannot take the address of the given expression");
+                               ec.Report.Error (211, loc, "Cannot take the address of the given expression");
                                return null;
                        }
 
-                       if (!TypeManager.VerifyUnManaged (Expr.Type, loc)) {
+                       if (!TypeManager.VerifyUnmanaged (ec.Compiler, Expr.Type, loc)) {
                                return null;
                        }
 
@@ -642,15 +649,15 @@ namespace Mono.CSharp {
                                vr.SetHasAddressTaken ();
 
                                if (vr.IsHoisted) {
-                                       AnonymousMethodExpression.Error_AddressOfCapturedVar (vr, loc);
+                                       AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, vr, loc);
                                }
                        } else {
                                IFixedExpression fe = Expr as IFixedExpression;
                                is_fixed = fe != null && fe.IsFixed;
                        }
 
-                       if (!is_fixed && !ec.HasSet (EmitContext.Options.FixedInitializerScope)) {
-                               Error (212, "You can only take the address of unfixed expression inside of a fixed statement initializer");
+                       if (!is_fixed && !ec.HasSet (ResolveContext.Options.FixedInitializerScope)) {
+                               ec.Report.Error (212, loc, "You can only take the address of unfixed expression inside of a fixed statement initializer");
                        }
 
                        type = TypeManager.GetPointerType (Expr.Type);
@@ -690,7 +697,7 @@ namespace Mono.CSharp {
                        }
 
                        string op_name = CSharp.Operator.GetMetadataName (op_type);
-                       MethodGroupExpr user_op = MemberLookup (ec.CurrentType, expr.Type, op_name, MemberTypes.Method, AllBindingFlags, expr.Location) as MethodGroupExpr;
+                       MethodGroupExpr user_op = MemberLookup (ec.Compiler, ec.CurrentType, expr.Type, op_name, MemberTypes.Method, AllBindingFlags, expr.Location) as MethodGroupExpr;
                        if (user_op == null)
                                return null;
 
@@ -716,7 +723,7 @@ namespace Mono.CSharp {
 
                        Type[] predefined = predefined_operators [(int) Oper];
                        foreach (Type t in predefined) {
-                               Expression oper_expr = Convert.UserDefinedConversion (ec, expr, t, expr.Location, false);
+                               Expression oper_expr = Convert.UserDefinedConversion (ec, expr, t, expr.Location, false, false);
                                if (oper_expr == null)
                                        continue;
 
@@ -738,7 +745,7 @@ namespace Mono.CSharp {
 
                                int result = MethodGroupExpr.BetterTypeConversion (ec, best_expr.Type, t);
                                if (result == 0) {
-                                       Report.Error (35, loc, "Operator `{0}' is ambiguous on an operand of type `{1}'",
+                                       ec.Report.Error (35, loc, "Operator `{0}' is ambiguous on an operand of type `{1}'",
                                                OperName (Oper), TypeManager.CSharpName (expr.Type));
                                        break;
                                }
@@ -787,7 +794,7 @@ namespace Mono.CSharp {
 
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       Error_PointerInsideExpressionTree ();
+                       Error_PointerInsideExpressionTree (ec);
                        return null;
                }
                
@@ -849,22 +856,22 @@ namespace Mono.CSharp {
                        return DoResolve (ec);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        expr = expr.Resolve (ec);
                        if (expr == null)
                                return null;
 
-                       if (!ec.InUnsafe)
-                               UnsafeError (loc);
+                       if (!ec.IsUnsafe)
+                               UnsafeError (ec, loc);
 
                        if (!expr.Type.IsPointer) {
-                               Error (193, "The * or -> operator must be applied to a pointer");
+                               ec.Report.Error (193, loc, "The * or -> operator must be applied to a pointer");
                                return null;
                        }
 
                        if (expr.Type == TypeManager.void_ptr_type) {
-                               Error (242, "The operation in question is undefined on void pointers");
+                               ec.Report.Error (242, loc, "The operation in question is undefined on void pointers");
                                return null;
                        }
 
@@ -897,7 +904,69 @@ namespace Mono.CSharp {
        /// classes (indexers require temporary access;  overloaded require method)
        ///
        /// </remarks>
-       public class UnaryMutator : ExpressionStatement {
+       public class UnaryMutator : ExpressionStatement
+       {
+               class DynamicPostMutator : Expression, IAssignMethod
+               {
+                       LocalTemporary temp;
+                       Expression expr;
+
+                       public DynamicPostMutator (Expression expr)
+                       {
+                               this.expr = expr;
+                               this.type = expr.Type;
+                               this.loc = expr.Location;
+                       }
+
+                       public override Expression CreateExpressionTree (ResolveContext ec)
+                       {
+                               throw new NotImplementedException ("ET");
+                       }
+
+                       protected override Expression DoResolve (ResolveContext rc)
+                       {
+                               eclass = expr.eclass;
+                               return this;
+                       }
+
+                       public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
+                       {
+                               expr.DoResolveLValue (ec, right_side);
+                               return DoResolve (ec);
+                       }
+
+                       public override void Emit (EmitContext ec)
+                       {
+                               temp.Emit (ec);
+                       }
+
+                       public void Emit (EmitContext ec, bool leave_copy)
+                       {
+                               throw new NotImplementedException ();
+                       }
+
+                       //
+                       // Emits target assignment using unmodified source value
+                       //
+                       public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
+                       {
+                               //
+                               // Allocate temporary variable to keep original value before it's modified
+                               //
+                               temp = new LocalTemporary (type);
+                               expr.Emit (ec);
+                               temp.Store (ec);
+
+                               ((IAssignMethod) expr).EmitAssign (ec, source, false, prepare_for_load);
+
+                               if (leave_copy)
+                                       Emit (ec);
+
+                               temp.Release (ec);
+                               temp = null;
+                       }
+               }
+
                [Flags]
                public enum Mode : byte {
                        IsIncrement    = 0,
@@ -912,107 +981,26 @@ namespace Mono.CSharp {
                }
 
                Mode mode;
-               bool is_expr = false;
-               bool recurse = false;
+               bool is_expr, recurse;
 
                Expression expr;
 
-               //
-               // This is expensive for the simplest case.
-               //
-               UserOperatorCall method;
+               // Holds the real operation
+               Expression operation;
 
-               public UnaryMutator (Mode m, Expression e)
+               public UnaryMutator (Mode m, Expression e, Location loc)
                {
                        mode = m;
-                       loc = e.Location;
+                       this.loc = loc;
                        expr = e;
                }
 
-               static string OperName (Mode mode)
-               {
-                       return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
-                               "++" : "--";
-               }
-
-               /// <summary>
-               ///   Returns whether an object of type `t' can be incremented
-               ///   or decremented with add/sub (ie, basically whether we can
-               ///   use pre-post incr-decr operations on it, but it is not a
-               ///   System.Decimal, which we require operator overloading to catch)
-               /// </summary>
-               static bool IsIncrementableNumber (Type t)
-               {
-                       return (t == TypeManager.sbyte_type) ||
-                               (t == TypeManager.byte_type) ||
-                               (t == TypeManager.short_type) ||
-                               (t == TypeManager.ushort_type) ||
-                               (t == TypeManager.int32_type) ||
-                               (t == TypeManager.uint32_type) ||
-                               (t == TypeManager.int64_type) ||
-                               (t == TypeManager.uint64_type) ||
-                               (t == TypeManager.char_type) ||
-                               (TypeManager.IsSubclassOf (t, TypeManager.enum_type)) ||
-                               (t == TypeManager.float_type) ||
-                               (t == TypeManager.double_type) ||
-                               (t.IsPointer && t != TypeManager.void_ptr_type);
-               }
-
-               Expression ResolveOperator (ResolveContext ec)
-               {
-                       type = expr.Type;
-                       
-                       //
-                       // The operand of the prefix/postfix increment decrement operators
-                       // should be an expression that is classified as a variable,
-                       // a property access or an indexer access
-                       //
-                       if (expr.eclass == ExprClass.Variable || expr.eclass == ExprClass.IndexerAccess || expr.eclass == ExprClass.PropertyAccess) {
-                               expr = expr.ResolveLValue (ec, expr);
-                       } else {
-                               Report.Error (1059, loc, "The operand of an increment or decrement operator must be a variable, property or indexer");
-                       }
-
-                       //
-                       // Step 1: Perform Operator Overload location
-                       //
-                       MethodGroupExpr mg;
-                       string op_name;
-                       
-                       if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
-                               op_name = Operator.GetMetadataName (Operator.OpType.Increment);
-                       else
-                               op_name = Operator.GetMetadataName (Operator.OpType.Decrement);
-
-                       mg = MemberLookup (ec.CurrentType, type, op_name, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
-
-                       if (mg != null) {
-                               Arguments args = new Arguments (1);
-                               args.Add (new Argument (expr));
-                               mg = mg.OverloadResolve (ec, ref args, false, loc);
-                               if (mg == null)
-                                       return null;
-
-                               method = new UserOperatorCall (mg, args, null, loc);
-                               Convert.ImplicitConversionRequired (ec, method, type, loc);
-                               return this;
-                       }
-
-                       if (!IsIncrementableNumber (type)) {
-                               Error (187, "No such operator '" + OperName (mode) + "' defined for type '" +
-                                          TypeManager.CSharpName (type) + "'");
-                               return null;
-                       }
-
-                       return this;
-               }
-
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        return new SimpleAssign (this, this).CreateExpressionTree (ec);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        expr = expr.Resolve (ec);
                        
@@ -1020,81 +1008,26 @@ namespace Mono.CSharp {
                                return null;
 
                        if (TypeManager.IsDynamicType (expr.Type)) {
+                               //
+                               // Handle postfix unary operators using local
+                               // temporary variable
+                               //
+                               if ((mode & Mode.IsPost) != 0)
+                                       expr = new DynamicPostMutator (expr);
+
                                Arguments args = new Arguments (1);
                                args.Add (new Argument (expr));
-                               return new DynamicUnaryConversion (GetOperatorExpressionTypeName (), args, loc).DoResolve (ec);
+                               return new SimpleAssign (expr, new DynamicUnaryConversion (GetOperatorExpressionTypeName (), args, loc)).Resolve (ec);
                        }
 
-                       eclass = ExprClass.Value;
-
                        if (TypeManager.IsNullableType (expr.Type))
                                return new Nullable.LiftedUnaryMutator (mode, expr, loc).Resolve (ec);
 
+                       eclass = ExprClass.Value;
+                       type = expr.Type;
                        return ResolveOperator (ec);
                }
 
-               //
-               // Loads the proper "1" into the stack based on the type, then it emits the
-               // opcode for the operation requested
-               //
-               void LoadOneAndEmitOp (EmitContext ec, Type t)
-               {
-                       //
-                       // Measure if getting the typecode and using that is more/less efficient
-                       // that comparing types.  t.GetTypeCode() is an internal call.
-                       //
-                       ILGenerator ig = ec.ig;
-                                                    
-                       if (t == TypeManager.uint64_type || t == TypeManager.int64_type)
-                               LongConstant.EmitLong (ig, 1);
-                       else if (t == TypeManager.double_type)
-                               ig.Emit (OpCodes.Ldc_R8, 1.0);
-                       else if (t == TypeManager.float_type)
-                               ig.Emit (OpCodes.Ldc_R4, 1.0F);
-                       else if (t.IsPointer){
-                               Type et = TypeManager.GetElementType (t);
-                               int n = GetTypeSize (et);
-                               
-                               if (n == 0)
-                                       ig.Emit (OpCodes.Sizeof, et);
-                               else {
-                                       IntConstant.EmitInt (ig, n);
-                                       ig.Emit (OpCodes.Conv_I);
-                               }
-                       } else 
-                               ig.Emit (OpCodes.Ldc_I4_1);
-
-                       //
-                       // Now emit the operation
-                       //
-
-                       Binary.Operator op = (mode & Mode.IsDecrement) != 0 ? Binary.Operator.Subtraction : Binary.Operator.Addition;
-                       Binary.EmitOperatorOpcode (ec, op, t);
-
-                       if (t == TypeManager.sbyte_type){
-                               if (ec.HasSet (EmitContext.Options.CheckedScope))
-                                       ig.Emit (OpCodes.Conv_Ovf_I1);
-                               else
-                                       ig.Emit (OpCodes.Conv_I1);
-                       } else if (t == TypeManager.byte_type){
-                               if (ec.HasSet (EmitContext.Options.CheckedScope))
-                                       ig.Emit (OpCodes.Conv_Ovf_U1);
-                               else
-                                       ig.Emit (OpCodes.Conv_U1);
-                       } else if (t == TypeManager.short_type){
-                               if (ec.HasSet (EmitContext.Options.CheckedScope))
-                                       ig.Emit (OpCodes.Conv_Ovf_I2);
-                               else
-                                       ig.Emit (OpCodes.Conv_I2);
-                       } else if (t == TypeManager.ushort_type || t == TypeManager.char_type){
-                               if (ec.HasSet (EmitContext.Options.CheckedScope))
-                                       ig.Emit (OpCodes.Conv_Ovf_U2);
-                               else
-                                       ig.Emit (OpCodes.Conv_U2);
-                       }
-                       
-               }
-
                void EmitCode (EmitContext ec, bool is_expr)
                {
                        recurse = true;
@@ -1111,10 +1044,9 @@ namespace Mono.CSharp {
                        //
                        if (recurse) {
                                ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement || mode == Mode.PostDecrement));
-                               if (method == null)
-                                       LoadOneAndEmitOp (ec, expr.Type);
-                               else
-                                       ec.ig.Emit (OpCodes.Call, (MethodInfo)method.Method);
+
+                               operation.Emit (ec);
+
                                recurse = false;
                                return;
                        }
@@ -1132,11 +1064,34 @@ namespace Mono.CSharp {
                //
                string GetOperatorExpressionTypeName ()
                {
-                       if ((mode & Mode.IsDecrement) != 0)
-                               return "Decrement";
+                       return IsDecrement ? "Decrement" : "Increment";
+               }
+
+               bool IsDecrement {
+                       get { return (mode & Mode.IsDecrement) != 0; }
+               }
+
+               //
+               //   Returns whether an object of type `t' can be incremented
+               //   or decremented with add/sub (ie, basically whether we can
+               //   use pre-post incr-decr operations on it, but it is not a
+               //   System.Decimal, which we require operator overloading to catch)
+               //
+               static bool IsPredefinedOperator (Type t)
+               {
+                       return (TypeManager.IsPrimitiveType (t) && t != TypeManager.bool_type) ||
+                               TypeManager.IsEnumType (t) ||
+                               t.IsPointer && t != TypeManager.void_ptr_type;
+               }
 
-                       return "Increment";
+#if NET_4_0
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
+               {
+                       var target = ((RuntimeValueExpression) expr).MetaObject.Expression;
+                       var source = SLE.Expression.Convert (operation.MakeExpression (ctx), target.Type);
+                       return SLE.Expression.Assign (target, source);
                }
+#endif
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
                {
@@ -1144,6 +1099,78 @@ namespace Mono.CSharp {
 
                        target.expr = expr.Clone (clonectx);
                }
+
+               Expression ResolveOperator (ResolveContext ec)
+               {
+                       if (expr is RuntimeValueExpression) {
+                               operation = expr;
+                       } else {
+                               // Use itself at the top of the stack
+                               operation = new EmptyExpression (type);
+                       }
+
+                       //
+                       // The operand of the prefix/postfix increment decrement operators
+                       // should be an expression that is classified as a variable,
+                       // a property access or an indexer access
+                       //
+                       if (expr.eclass == ExprClass.Variable || expr.eclass == ExprClass.IndexerAccess || expr.eclass == ExprClass.PropertyAccess) {
+                               expr = expr.ResolveLValue (ec, expr);
+                       } else {
+                               ec.Report.Error (1059, loc, "The operand of an increment or decrement operator must be a variable, property or indexer");
+                       }
+
+                       //
+                       // 1. Check predefined types
+                       //
+                       if (IsPredefinedOperator (type)) {
+                               // TODO: Move to IntConstant once I get rid of int32_type
+                               var one = new IntConstant (1, loc);
+
+                               // TODO: Cache this based on type when using EmptyExpression in
+                               // context cache
+                               Binary.Operator op = IsDecrement ? Binary.Operator.Subtraction : Binary.Operator.Addition;
+                               operation = new Binary (op, operation, one, loc);
+                               operation = operation.Resolve (ec);
+                               if (operation != null && operation.Type != type)
+                                       operation = Convert.ExplicitNumericConversion (operation, type);
+
+                               return this;
+                       }
+
+                       //
+                       // Step 2: Perform Operator Overload location
+                       //
+                       MethodGroupExpr mg;
+                       string op_name;
+
+                       if (IsDecrement)
+                               op_name = Operator.GetMetadataName (Operator.OpType.Decrement);
+                       else
+                               op_name = Operator.GetMetadataName (Operator.OpType.Increment);
+
+                       mg = MemberLookup (ec.Compiler, ec.CurrentType, type, op_name, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
+
+                       if (mg != null) {
+                               Arguments args = new Arguments (1);
+                               args.Add (new Argument (expr));
+                               mg = mg.OverloadResolve (ec, ref args, false, loc);
+                               if (mg == null)
+                                       return null;
+
+                               args[0].Expr = operation;
+                               operation = new UserOperatorCall (mg, args, null, loc);
+                               operation = Convert.ImplicitConversionRequired (ec, operation, type, loc);
+                               return this;
+                       }
+
+                       string name = IsDecrement ?
+                               Operator.GetName (Operator.OpType.Decrement) :
+                               Operator.GetName (Operator.OpType.Increment);
+
+                       Unary.Error_OperatorCannotBeApplied (ec, loc, name, type);
+                       return null;
+               }
        }
 
        /// <summary>
@@ -1172,7 +1199,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        probe_type_expr = ProbeType.ResolveAsTypeTerminal (ec, false);
                        if (probe_type_expr == null)
@@ -1183,18 +1210,18 @@ namespace Mono.CSharp {
                                return null;
 
                        if ((probe_type_expr.Type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
-                               Report.Error (-244, loc, "The `{0}' operator cannot be applied to an operand of a static type",
+                               ec.Report.Error (-244, loc, "The `{0}' operator cannot be applied to an operand of a static type",
                                        OperatorName);
                        }
                        
                        if (expr.Type.IsPointer || probe_type_expr.Type.IsPointer) {
-                               Report.Error (244, loc, "The `{0}' operator cannot be applied to an operand of pointer type",
+                               ec.Report.Error (244, loc, "The `{0}' operator cannot be applied to an operand of pointer type",
                                        OperatorName);
                                return null;
                        }
 
                        if (expr.Type == InternalType.AnonymousMethod) {
-                               Report.Error (837, loc, "The `{0}' operator cannot be applied to a lambda expression or anonymous method",
+                               ec.Report.Error (837, loc, "The `{0}' operator cannot be applied to a lambda expression or anonymous method",
                                        OperatorName);
                                return null;
                        }
@@ -1237,7 +1264,7 @@ namespace Mono.CSharp {
                                expr.CreateExpressionTree (ec),
                                new TypeOf (probe_type_expr, loc));
 
-                       return CreateExpressionFactoryCall ("TypeIs", args);
+                       return CreateExpressionFactoryCall (ec, "TypeIs", args);
                }
                
                public override void Emit (EmitContext ec)
@@ -1266,19 +1293,19 @@ namespace Mono.CSharp {
                        ig.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
                }
                
-               Expression CreateConstantResult (bool result)
+               Expression CreateConstantResult (ResolveContext ec, bool result)
                {
                        if (result)
-                               Report.Warning (183, 1, loc, "The given expression is always of the provided (`{0}') type",
+                               ec.Report.Warning (183, 1, loc, "The given expression is always of the provided (`{0}') type",
                                        TypeManager.CSharpName (probe_type_expr.Type));
                        else
-                               Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type",
+                               ec.Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type",
                                        TypeManager.CSharpName (probe_type_expr.Type));
 
-                       return ReducedExpression.Create (new BoolConstant (result, loc), this);
+                       return ReducedExpression.Create (new BoolConstant (result, loc).Resolve (ec), this);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (base.DoResolve (ec) == null)
                                return null;
@@ -1291,7 +1318,7 @@ namespace Mono.CSharp {
                        // type or a nullable type and the value of E is null, the result is false
                        //
                        if (expr.IsNull || expr.eclass == ExprClass.MethodGroup)
-                               return CreateConstantResult (false);
+                               return CreateConstantResult (ec, false);
 
                        if (TypeManager.IsNullableType (d) && !TypeManager.ContainsGenericParameters (d)) {
                                d = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (d) [0]);
@@ -1320,11 +1347,11 @@ namespace Mono.CSharp {
                                        //
                                        // The result is true if D and T are the same value types
                                        //
-                                       return CreateConstantResult (true);
+                                       return CreateConstantResult (ec, true);
                                }
 
                                if (TypeManager.IsGenericParameter (d))
-                                       return ResolveGenericParameter (t, d);
+                                       return ResolveGenericParameter (ec, t, d);
 
                                //
                                // An unboxing conversion exists
@@ -1333,15 +1360,15 @@ namespace Mono.CSharp {
                                        return this;
                        } else {
                                if (TypeManager.IsGenericParameter (t))
-                                       return ResolveGenericParameter (d, t);
+                                       return ResolveGenericParameter (ec, d, t);
 
                                if (TypeManager.IsStruct (d)) {
                                        bool temp;
                                        if (Convert.ImplicitBoxingConversionExists (expr, t, out temp))
-                                               return CreateConstantResult (true);
+                                               return CreateConstantResult (ec, true);
                                } else {
                                        if (TypeManager.IsGenericParameter (d))
-                                               return ResolveGenericParameter (t, d);
+                                               return ResolveGenericParameter (ec, t, d);
 
                                        if (TypeManager.ContainsGenericParameters (d))
                                                return this;
@@ -1353,22 +1380,23 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       return CreateConstantResult (false);
+                       return CreateConstantResult (ec, false);
                }
 
-               Expression ResolveGenericParameter (Type d, Type t)
+               Expression ResolveGenericParameter (ResolveContext ec, Type d, Type t)
                {
                        GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
                        if (constraints != null) {
                                if (constraints.IsReferenceType && TypeManager.IsStruct (d))
-                                       return CreateConstantResult (false);
-
-                               if (constraints.IsValueType && !TypeManager.IsStruct (d))
-                                       return CreateConstantResult (TypeManager.IsEqual (d, t));
+                                       return CreateConstantResult (ec, false);
                        }
 
-                       if (TypeManager.IsGenericParameter (expr.Type))
+                       if (TypeManager.IsGenericParameter (expr.Type)) {
+                               if (constraints != null && constraints.IsValueType && expr.Type == t)
+                                       return CreateConstantResult (ec, true);
+
                                expr = new BoxedCast (expr, d);
+                       }
 
                        return this;
                }
@@ -1396,7 +1424,7 @@ namespace Mono.CSharp {
                                expr.CreateExpressionTree (ec),
                                new TypeOf (probe_type_expr, loc));
 
-                       return CreateExpressionFactoryCall ("TypeAs", args);
+                       return CreateExpressionFactoryCall (ec, "TypeAs", args);
                }
 
                public override void Emit (EmitContext ec)
@@ -1408,18 +1436,12 @@ namespace Mono.CSharp {
                        if (do_isinst)
                                ig.Emit (OpCodes.Isinst, type);
 
-#if GMCS_SOURCE
                        if (TypeManager.IsGenericParameter (type) || TypeManager.IsNullableType (type))
                                ig.Emit (OpCodes.Unbox_Any, type);
-#endif
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       // Because expr is modified
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        if (resolved_type == null) {
                                resolved_type = base.DoResolve (ec);
 
@@ -1433,11 +1455,11 @@ namespace Mono.CSharp {
 
                        if (!TypeManager.IsReferenceType (type) && !TypeManager.IsNullableType (type)) {
                                if (TypeManager.IsGenericParameter (type)) {
-                                       Report.Error (413, loc,
+                                       ec.Report.Error (413, loc,
                                                "The `as' operator cannot be used with a non-reference type parameter `{0}'. Consider adding `class' or a reference type constraint",
                                                probe_type_expr.GetSignatureForError ());
                                } else {
-                                       Report.Error (77, loc,
+                                       ec.Report.Error (77, loc,
                                                "The `as' operator cannot be used with a non-nullable value type `{0}'",
                                                TypeManager.CSharpName (type));
                                }
@@ -1445,7 +1467,7 @@ namespace Mono.CSharp {
                        }
 
                        if (expr.IsNull && TypeManager.IsNullableType (type)) {
-                               return Nullable.LiftedNull.CreateFromExpression (this);
+                               return Nullable.LiftedNull.CreateFromExpression (ec, this);
                        }
                        
                        Expression e = Convert.ImplicitConversion (ec, expr, type, loc);
@@ -1470,7 +1492,7 @@ namespace Mono.CSharp {
                                return this;
                        }
 
-                       Report.Error (39, loc, "Cannot convert type `{0}' to `{1}' via a built-in conversion",
+                       ec.Report.Error (39, loc, "Cannot convert type `{0}' to `{1}' via a built-in conversion",
                                TypeManager.CSharpName (etype), TypeManager.CSharpName (type));
 
                        return null;
@@ -1498,9 +1520,8 @@ namespace Mono.CSharp {
        ///   FIXME: Cast expressions have an unusual set of parsing
        ///   rules, we need to figure those out.
        /// </summary>
-       public class Cast : Expression {
+       public class Cast : ShimExpression {
                Expression target_type;
-               Expression expr;
                        
                public Cast (Expression cast_type, Expression expr)
                        : this (cast_type, expr, cast_type.Location)
@@ -1508,9 +1529,9 @@ namespace Mono.CSharp {
                }
 
                public Cast (Expression cast_type, Expression expr, Location loc)
+                       : base (expr)
                {
                        this.target_type = cast_type;
-                       this.expr = expr;
                        this.loc = loc;
                }
 
@@ -1518,16 +1539,7 @@ namespace Mono.CSharp {
                        get { return target_type; }
                }
 
-               public Expression Expr {
-                       get { return expr; }
-               }
-
-               public override Expression CreateExpressionTree (ResolveContext ec)
-               {
-                       throw new NotSupportedException ("ET");
-               }
-
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        expr = expr.Resolve (ec);
                        if (expr == null)
@@ -1540,7 +1552,7 @@ namespace Mono.CSharp {
                        type = target.Type;
 
                        if (type.IsAbstract && type.IsSealed) {
-                               Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
+                               ec.Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
                                return null;
                        }
 
@@ -1553,23 +1565,18 @@ namespace Mono.CSharp {
                                        return c;
                        }
 
-                       if (type.IsPointer && !ec.InUnsafe) {
-                               UnsafeError (loc);
+                       if (type.IsPointer && !ec.IsUnsafe) {
+                               UnsafeError (ec, loc);
                        } else if (TypeManager.IsDynamicType (expr.Type)) {
                                Arguments arg = new Arguments (1);
                                arg.Add (new Argument (expr));
-                               return new DynamicConversion (type, true, arg, loc).Resolve (ec);
+                               return new DynamicConversion (type, CSharpBinderFlags.ConvertExplicit, arg, loc).Resolve (ec);
                        }
 
                        expr = Convert.ExplicitConversion (ec, expr, type, loc);
                        return expr;
                }
                
-               public override void Emit (EmitContext ec)
-               {
-                       throw new Exception ("Should not happen");
-               }
-
                protected override void CloneTo (CloneContext clonectx, Expression t)
                {
                        Cast target = (Cast) t;
@@ -1578,26 +1585,39 @@ namespace Mono.CSharp {
                        target.expr = expr.Clone (clonectx);
                }
        }
+
+       public class ImplicitCast : ShimExpression
+       {
+               bool arrayAccess;
+
+               public ImplicitCast (Expression expr, Type target, bool arrayAccess)
+                       : base (expr)
+               {
+                       this.loc = expr.Location;
+                       this.type = target;
+                       this.arrayAccess = arrayAccess;
+               }
+
+               protected override Expression DoResolve (ResolveContext ec)
+               {
+                       expr = expr.Resolve (ec);
+                       if (expr == null)
+                               return null;
+
+                       if (arrayAccess)
+                               expr = ConvertExpressionToArrayIndex (ec, expr);
+                       else
+                               expr = Convert.ImplicitConversionRequired (ec, expr, type, loc);
+
+                       return expr;
+               }
+       }
        
        //
        // C# 2.0 Default value expression
        //
        public class DefaultValueExpression : Expression
        {
-               sealed class DefaultValueNullLiteral : NullLiteral
-               {
-                       public DefaultValueNullLiteral (DefaultValueExpression expr)
-                               : base (expr.type, expr.loc)
-                       {
-                       }
-
-                       public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, Type t, bool expl)
-                       {
-                               Error_ValueCannotBeConvertedCore (ec, loc, t, expl);
-                       }
-               }
-
-
                Expression expr;
 
                public DefaultValueExpression (Expression expr, Location loc)
@@ -1611,10 +1631,10 @@ namespace Mono.CSharp {
                        Arguments args = new Arguments (2);
                        args.Add (new Argument (this));
                        args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
-                       return CreateExpressionFactoryCall ("Constant", args);
+                       return CreateExpressionFactoryCall (ec, "Constant", args);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        TypeExpr texpr = expr.ResolveAsTypeTerminal (ec, false);
                        if (texpr == null)
@@ -1623,18 +1643,18 @@ namespace Mono.CSharp {
                        type = texpr.Type;
 
                        if ((type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
-                               Report.Error (-244, loc, "The `default value' operator cannot be applied to an operand of a static type");
+                               ec.Report.Error (-244, loc, "The `default value' operator cannot be applied to an operand of a static type");
                        }
 
                        if (type.IsPointer)
-                               return new NullLiteral (Location).ConvertImplicitly (type);
+                               return new NullLiteral (Location).ConvertImplicitly (ec, type);
 
                        if (TypeManager.IsReferenceType (type))
-                               return new DefaultValueNullLiteral (this);
+                               return new NullConstant (type, loc);
 
                        Constant c = New.Constantify (type);
                        if (c != null)
-                               return c;
+                               return c.Resolve (ec);
 
                        eclass = ExprClass.Variable;
                        return this;
@@ -1667,7 +1687,6 @@ namespace Mono.CSharp {
        /// </summary>
        public class Binary : Expression, IDynamicBinder
        {
-
                protected class PredefinedOperator {
                        protected readonly Type left;
                        protected readonly Type right;
@@ -1715,6 +1734,24 @@ namespace Mono.CSharp {
                                if (left == TypeManager.decimal_type)
                                        return b.ResolveUserOperator (ec, b.left.Type, b.right.Type);
 
+                               var c = b.right as Constant;
+                               if (c != null) {
+                                       if (c.IsDefaultValue && (b.oper == Operator.Addition || b.oper == Operator.BitwiseOr || b.oper == Operator.Subtraction))
+                                               return ReducedExpression.Create (b.left, b).Resolve (ec);
+                                       if ((b.oper == Operator.Multiply || b.oper == Operator.Division) && c.IsOneInteger)
+                                               return ReducedExpression.Create (b.left, b).Resolve (ec);
+                                       return b;
+                               }
+
+                               c = b.left as Constant;
+                               if (c != null) {
+                                       if (c.IsDefaultValue && (b.oper == Operator.Addition || b.oper == Operator.BitwiseOr))
+                                               return ReducedExpression.Create (b.right, b).Resolve (ec);
+                                       if (b.oper == Operator.Multiply && c.IsOneInteger)
+                                               return ReducedExpression.Create (b.right, b).Resolve (ec);
+                                       return b;
+                               }
+
                                return b;
                        }
 
@@ -1789,7 +1826,7 @@ namespace Mono.CSharp {
                                //
                                // Start a new concat expression using converted expression
                                //
-                               return new StringConcat (b.loc, b.left, b.right).Resolve (ec);
+                               return StringConcat.Create (ec, b.left, b.right, b.loc);
                        }
                }
 
@@ -1803,7 +1840,7 @@ namespace Mono.CSharp {
                        {
                                b.left = Convert.ImplicitConversion (ec, b.left, left, b.left.Location);
 
-                               Expression expr_tree_expr = EmptyCast.Create (b.right, TypeManager.int32_type);
+                               Expression expr_tree_expr = Convert.ImplicitConversion (ec, b.right, TypeManager.int32_type, b.right.Location);
 
                                int right_mask = left == TypeManager.int32_type || left == TypeManager.uint32_type ? 0x1f : 0x3f;
 
@@ -1811,13 +1848,21 @@ namespace Mono.CSharp {
                                // b = b.left >> b.right & (0x1f|0x3f)
                                //
                                b.right = new Binary (Operator.BitwiseAnd,
-                                       b.right, new IntConstant (right_mask, b.right.Location)).Resolve (ec);
+                                       b.right, new IntConstant (right_mask, b.right.Location), b.loc).Resolve (ec);
 
                                //
                                // Expression tree representation does not use & mask
                                //
                                b.right = ReducedExpression.Create (b.right, expr_tree_expr).Resolve (ec);
                                b.type = ReturnType;
+
+                               //
+                               // Optimize shift by 0
+                               //
+                               var c = b.right as Constant;
+                               if (c != null && c.IsDefaultValue)
+                                       return ReducedExpression.Create (b.left, b).Resolve (ec);
+
                                return b;
                        }
                }
@@ -1936,18 +1981,18 @@ namespace Mono.CSharp {
                static PredefinedOperator [] standard_operators;
                static PredefinedOperator [] pointer_operators;
                
-               public Binary (Operator oper, Expression left, Expression right, bool isCompound)
-                       : this (oper, left, right)
+               public Binary (Operator oper, Expression left, Expression right, bool isCompound, Location loc)
+                       : this (oper, left, right, loc)
                {
                        this.is_compound = isCompound;
                }
 
-               public Binary (Operator oper, Expression left, Expression right)
+               public Binary (Operator oper, Expression left, Expression right, Location loc)
                {
                        this.oper = oper;
                        this.left = left;
                        this.right = right;
-                       this.loc = left.Location;
+                       this.loc = loc;
                }
 
                public Operator Oper {
@@ -2028,24 +2073,24 @@ namespace Mono.CSharp {
                        return s;
                }
 
-               public static void Error_OperatorCannotBeApplied (Expression left, Expression right, Operator oper, Location loc)
+               public static void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right, Operator oper, Location loc)
                {
-                       new Binary (oper, left, right).Error_OperatorCannotBeApplied (left, right);
+                       new Binary (oper, left, right, loc).Error_OperatorCannotBeApplied (ec, left, right);
                }
 
-               public static void Error_OperatorCannotBeApplied (Expression left, Expression right, string oper, Location loc)
+               public static void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right, string oper, Location loc)
                {
                        string l, r;
                        l = TypeManager.CSharpName (left.Type);
                        r = TypeManager.CSharpName (right.Type);
 
-                       Report.Error (19, loc, "Operator `{0}' cannot be applied to operands of type `{1}' and `{2}'",
+                       ec.Report.Error (19, loc, "Operator `{0}' cannot be applied to operands of type `{1}' and `{2}'",
                                oper, l, r);
                }
                
-               protected void Error_OperatorCannotBeApplied (Expression left, Expression right)
+               protected void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right)
                {
-                       Error_OperatorCannotBeApplied (left, right, OperName (oper), loc);
+                       Error_OperatorCannotBeApplied (ec, left, right, OperName (oper), loc);
                }
 
                //
@@ -2145,7 +2190,7 @@ namespace Mono.CSharp {
 
                        switch (oper){
                        case Operator.Multiply:
-                               if (ec.HasSet (EmitContext.Options.CheckedScope)){
+                               if (ec.HasSet (EmitContext.Options.CheckedScope)) {
                                        if (l == TypeManager.int32_type || l == TypeManager.int64_type)
                                                opcode = OpCodes.Mul_Ovf;
                                        else if (!IsFloat (l))
@@ -2172,7 +2217,7 @@ namespace Mono.CSharp {
                                break;
 
                        case Operator.Addition:
-                               if (ec.HasSet (EmitContext.Options.CheckedScope)){
+                               if (ec.HasSet (EmitContext.Options.CheckedScope)) {
                                        if (l == TypeManager.int32_type || l == TypeManager.int64_type)
                                                opcode = OpCodes.Add_Ovf;
                                        else if (!IsFloat (l))
@@ -2184,7 +2229,7 @@ namespace Mono.CSharp {
                                break;
 
                        case Operator.Subtraction:
-                               if (ec.HasSet (EmitContext.Options.CheckedScope)){
+                               if (ec.HasSet (EmitContext.Options.CheckedScope)) {
                                        if (l == TypeManager.int32_type || l == TypeManager.int64_type)
                                                opcode = OpCodes.Sub_Ovf;
                                        else if (!IsFloat (l))
@@ -2383,14 +2428,14 @@ namespace Mono.CSharp {
                                        break;
                                return left;
                        }
-                       Error_OperatorCannotBeApplied (this.left, this.right);
+                       Error_OperatorCannotBeApplied (ec, this.left, this.right);
                        return null;
                }
 
                //
                // The `|' operator used on types which were extended is dangerous
                //
-               void CheckBitwiseOrOnSignExtended ()
+               void CheckBitwiseOrOnSignExtended (ResolveContext ec)
                {
                        OpcodeCast lcast = left as OpcodeCast;
                        if (lcast != null) {
@@ -2409,14 +2454,14 @@ namespace Mono.CSharp {
 
                        // FIXME: consider constants
 
-                       Report.Warning (675, 3, loc,
+                       ec.Report.Warning (675, 3, loc,
                                "The operator `|' used on the sign-extended type `{0}'. Consider casting to a smaller unsigned type first",
                                TypeManager.CSharpName (lcast != null ? lcast.UnderlyingType : rcast.UnderlyingType));
                }
 
                static void CreatePointerOperatorsTable ()
                {
-                       ArrayList temp = new ArrayList ();
+                       var temp = new List<PredefinedPointerOperator> ();
 
                        //
                        // Pointer arithmetic:
@@ -2447,12 +2492,12 @@ namespace Mono.CSharp {
                        //
                        temp.Add (new PredefinedPointerOperator (null, Operator.SubtractionMask, TypeManager.int64_type));
 
-                       pointer_operators = (PredefinedOperator []) temp.ToArray (typeof (PredefinedOperator));
+                       pointer_operators = temp.ToArray ();
                }
 
                static void CreateStandardOperatorsTable ()
                {
-                       ArrayList temp = new ArrayList ();
+                       var temp = new List<PredefinedOperator> ();
                        Type bool_type = TypeManager.bool_type;
 
                        temp.Add (new PredefinedOperator (TypeManager.int32_type, Operator.ArithmeticMask | Operator.BitwiseMask));
@@ -2485,20 +2530,20 @@ namespace Mono.CSharp {
                        temp.Add (new PredefinedShiftOperator (TypeManager.int64_type, Operator.ShiftMask));
                        temp.Add (new PredefinedShiftOperator (TypeManager.uint64_type, Operator.ShiftMask));
 
-                       standard_operators = (PredefinedOperator []) temp.ToArray (typeof (PredefinedOperator));
+                       standard_operators = temp.ToArray ();
                }
 
                //
                // Rules used during binary numeric promotion
                //
-               static bool DoNumericPromotion (ref Expression prim_expr, ref Expression second_expr, Type type)
+               static bool DoNumericPromotion (ResolveContext rc, ref Expression prim_expr, ref Expression second_expr, Type type)
                {
                        Expression temp;
                        Type etype;
 
                        Constant c = prim_expr as Constant;
                        if (c != null) {
-                               temp = c.ConvertImplicitly (type);
+                               temp = c.ConvertImplicitly (rc, type);
                                if (temp != null) {
                                        prim_expr = temp;
                                        return true;
@@ -2513,7 +2558,7 @@ namespace Mono.CSharp {
                                        if (type != second_expr.Type) {
                                                c = second_expr as Constant;
                                                if (c != null)
-                                                       temp = c.ConvertImplicitly (type);
+                                                       temp = c.ConvertImplicitly (rc, type);
                                                else
                                                        temp = Convert.ImplicitNumericConversion (second_expr, type);
                                                if (temp == null)
@@ -2526,7 +2571,7 @@ namespace Mono.CSharp {
                                // A compile-time error occurs if the other operand is of type sbyte, short, int, or long
                                //
                                if (type == TypeManager.int32_type || type == TypeManager.int64_type ||
-                                       type == TypeManager.sbyte_type || type == TypeManager.sbyte_type)
+                                       type == TypeManager.short_type || type == TypeManager.sbyte_type)
                                        return false;
                        }
 
@@ -2549,17 +2594,17 @@ namespace Mono.CSharp {
 
                        foreach (Type t in ConstantFold.binary_promotions) {
                                if (t == ltype)
-                                       return t == rtype || DoNumericPromotion (ref right, ref left, t);
+                                       return t == rtype || DoNumericPromotion (ec, ref right, ref left, t);
 
                                if (t == rtype)
-                                       return t == ltype || DoNumericPromotion (ref left, ref right, t);
+                                       return t == ltype || DoNumericPromotion (ec, ref left, ref right, t);
                        }
 
                        Type int32 = TypeManager.int32_type;
                        if (ltype != int32) {
                                Constant c = left as Constant;
                                if (c != null)
-                                       temp = c.ConvertImplicitly (int32);
+                                       temp = c.ConvertImplicitly (ec, int32);
                                else
                                        temp = Convert.ImplicitNumericConversion (left, int32);
 
@@ -2571,7 +2616,7 @@ namespace Mono.CSharp {
                        if (rtype != int32) {
                                Constant c = right as Constant;
                                if (c != null)
-                                       temp = c.ConvertImplicitly (int32);
+                                       temp = c.ConvertImplicitly (ec, int32);
                                else
                                        temp = Convert.ImplicitNumericConversion (right, int32);
 
@@ -2583,7 +2628,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (left == null)
                                return null;
@@ -2595,7 +2640,7 @@ namespace Mono.CSharp {
                                        return null;
 
                                if (left.eclass == ExprClass.Type) {
-                                       Report.Error (75, loc, "To cast a negative value, you must enclose the value in parentheses");
+                                       ec.Report.Error (75, loc, "To cast a negative value, you must enclose the value in parentheses");
                                        return null;
                                }
                        } else
@@ -2613,7 +2658,7 @@ namespace Mono.CSharp {
                                // FIXME: resolve right expression as unreachable
                                // right.Resolve (ec);
 
-                               Report.Warning (429, 4, loc, "Unreachable expression code detected");
+                               ec.Report.Warning (429, 4, loc, "Unreachable expression code detected");
                                return left;
                        }
 
@@ -2625,43 +2670,29 @@ namespace Mono.CSharp {
                        Constant rc = right as Constant;
 
                        // The conversion rules are ignored in enum context but why
-                       if (!ec.HasSet (EmitContext.Options.EnumScope) && lc != null && rc != null && (TypeManager.IsEnumType (left.Type) || TypeManager.IsEnumType (right.Type))) {
+                       if (!ec.HasSet (ResolveContext.Options.EnumScope) && lc != null && rc != null && (TypeManager.IsEnumType (left.Type) || TypeManager.IsEnumType (right.Type))) {
                                lc = EnumLiftUp (ec, lc, rc, loc);
                                if (lc != null)
                                        rc = EnumLiftUp (ec, rc, lc, loc);
                        }
 
                        if (rc != null && lc != null) {
-                               int prev_e = Report.Errors;
-                               Expression e = ConstantFold.BinaryFold (
-                                       ec, oper, lc, rc, loc);
-                               if (e != null || Report.Errors != prev_e)
-                                       return e;
-                       } else if ((oper == Operator.BitwiseAnd || oper == Operator.LogicalAnd) && !TypeManager.IsDynamicType (left.Type) &&
-                                       ((lc != null && lc.IsDefaultValue) || (rc != null && rc.IsDefaultValue))) {
-
-                               if ((ResolveOperator (ec)) == null) {
-                                       Error_OperatorCannotBeApplied (left, right);
-                                       return null;
-                               }
-
-                               //
-                               // The result is a constant with side-effect
-                               //
-                               Constant side_effect = rc == null ?
-                                       new SideEffectConstant (lc, right, loc) :
-                                       new SideEffectConstant (rc, left, loc);
+                               int prev_e = ec.Report.Errors;
+                               Expression e = ConstantFold.BinaryFold (ec, oper, lc, rc, loc);
+                               if (e != null)
+                                       e = e.Resolve (ec);
 
-                               return ReducedExpression.Create (side_effect, this);
+                               if (e != null || ec.Report.Errors != prev_e)
+                                       return e;
                        }
 
                        // Comparison warnings
                        if ((oper & Operator.ComparisonMask) != 0) {
                                if (left.Equals (right)) {
-                                       Report.Warning (1718, 3, loc, "A comparison made to same variable. Did you mean to compare something else?");
+                                       ec.Report.Warning (1718, 3, loc, "A comparison made to same variable. Did you mean to compare something else?");
                                }
-                               CheckUselessComparison (lc, right.Type);
-                               CheckUselessComparison (rc, left.Type);
+                               CheckUselessComparison (ec, lc, right.Type);
+                               CheckUselessComparison (ec, rc, left.Type);
                        }
 
                        if (TypeManager.IsDynamicType (left.Type) || TypeManager.IsDynamicType (right.Type)) {
@@ -2685,17 +2716,65 @@ namespace Mono.CSharp {
                {
                        Expression expr = ResolveOperator (ec);
                        if (expr == null)
-                               Error_OperatorCannotBeApplied (left_orig, right_orig);
+                               Error_OperatorCannotBeApplied (ec, left_orig, right_orig);
 
                        if (left == null || right == null)
                                throw new InternalErrorException ("Invalid conversion");
 
                        if (oper == Operator.BitwiseOr)
-                               CheckBitwiseOrOnSignExtended ();
+                               CheckBitwiseOrOnSignExtended (ec);
 
                        return expr;
                }
 
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
+               {
+                       var le = left.MakeExpression (ctx);
+                       var re = right.MakeExpression (ctx);
+                       bool is_checked = ctx.HasSet (BuilderContext.Options.CheckedScope);
+
+                       switch (oper) {
+                       case Operator.Addition:
+                               return is_checked ? SLE.Expression.AddChecked (le, re) : SLE.Expression.Add (le, re);
+                       case Operator.BitwiseAnd:
+                               return SLE.Expression.And (le, re);
+                       case Operator.BitwiseOr:
+                               return SLE.Expression.Or (le, re);
+                       case Operator.Division:
+                               return SLE.Expression.Divide (le, re);
+                       case Operator.Equality:
+                               return SLE.Expression.Equal (le, re);
+                       case Operator.ExclusiveOr:
+                               return SLE.Expression.ExclusiveOr (le, re);
+                       case Operator.GreaterThan:
+                               return SLE.Expression.GreaterThan (le, re);
+                       case Operator.GreaterThanOrEqual:
+                               return SLE.Expression.GreaterThanOrEqual (le, re);
+                       case Operator.Inequality:
+                               return SLE.Expression.NotEqual (le, re);
+                       case Operator.LeftShift:
+                               return SLE.Expression.LeftShift (le, re);
+                       case Operator.LessThan:
+                               return SLE.Expression.LessThan (le, re);
+                       case Operator.LessThanOrEqual:
+                               return SLE.Expression.LessThanOrEqual (le, re);
+                       case Operator.LogicalAnd:
+                               return SLE.Expression.AndAlso (le, re);
+                       case Operator.LogicalOr:
+                               return SLE.Expression.OrElse (le, re);
+                       case Operator.Modulus:
+                               return SLE.Expression.Modulo (le, re);
+                       case Operator.Multiply:
+                               return is_checked ? SLE.Expression.MultiplyChecked (le, re) : SLE.Expression.Multiply (le, re);
+                       case Operator.RightShift:
+                               return SLE.Expression.RightShift (le, re);
+                       case Operator.Subtraction:
+                               return is_checked ? SLE.Expression.SubtractChecked (le, re) : SLE.Expression.Subtract (le, re);
+                       default:
+                               throw new NotImplementedException (oper.ToString ());
+                       }
+               }
+
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
                        left.MutateHoistedGenericType (storey);
@@ -2736,7 +2815,7 @@ namespace Mono.CSharp {
                        if (is_equality)
                                return ResolveUserOperator (ec, l, r);
 
-                       MethodInfo method;
+                       MethodSpec method;
                        Arguments args = new Arguments (2);
                        args.Add (new Argument (left));
                        args.Add (new Argument (right));
@@ -2757,7 +2836,7 @@ namespace Mono.CSharp {
                                method = TypeManager.delegate_remove_delegate_delegate;
                        }
 
-                       MethodGroupExpr mg = new MethodGroupExpr (new MemberInfo [] { method }, TypeManager.delegate_type, loc);
+                       MethodGroupExpr mg = new MethodGroupExpr (new [] { method }, TypeManager.delegate_type, loc);
                        mg = mg.OverloadResolve (ec, ref args, false, loc);
 
                        return new ClassCast (new UserOperatorCall (mg, args, CreateExpressionTree, loc), l);
@@ -2787,7 +2866,8 @@ namespace Mono.CSharp {
                        // E operator + (E e, U x)
                        //
                        if (!((oper & (Operator.ComparisonMask | Operator.BitwiseMask)) != 0 ||
-                               (oper == Operator.Subtraction && lenum) || (oper == Operator.Addition && lenum != renum)))
+                               (oper == Operator.Subtraction && lenum) ||
+                               (oper == Operator.Addition && (lenum != renum || type != null))))       // type != null for lifted null
                                return null;
 
                        Expression ltemp = left;
@@ -2795,7 +2875,7 @@ namespace Mono.CSharp {
                        Type underlying_type;
                        Expression expr;
                        
-                       if ((oper & Operator.ComparisonMask | Operator.BitwiseMask) != 0) {
+                       if ((oper & (Operator.ComparisonMask | Operator.BitwiseMask)) != 0) {
                                if (renum) {
                                        expr = Convert.ImplicitConversion (ec, left, rtype, loc);
                                        if (expr != null) {
@@ -2815,12 +2895,12 @@ namespace Mono.CSharp {
                                underlying_type = TypeManager.GetEnumUnderlyingType (ltype);
 
                                if (left is Constant)
-                                       left = ((Constant) left).ConvertExplicitly (false, underlying_type);
+                                       left = ((Constant) left).ConvertExplicitly (false, underlying_type).Resolve (ec);
                                else
                                        left = EmptyCast.Create (left, underlying_type);
 
                                if (right is Constant)
-                                       right = ((Constant) right).ConvertExplicitly (false, underlying_type);
+                                       right = ((Constant) right).ConvertExplicitly (false, underlying_type).Resolve (ec);
                                else
                                        right = EmptyCast.Create (right, underlying_type);
                        } else if (lenum) {
@@ -2838,7 +2918,7 @@ namespace Mono.CSharp {
                                }
 
                                if (left is Constant)
-                                       left = ((Constant) left).ConvertExplicitly (false, underlying_type);
+                                       left = ((Constant) left).ConvertExplicitly (false, underlying_type).Resolve (ec);
                                else
                                        left = EmptyCast.Create (left, underlying_type);
 
@@ -2857,7 +2937,7 @@ namespace Mono.CSharp {
                                }
 
                                if (right is Constant)
-                                       right = ((Constant) right).ConvertExplicitly (false, underlying_type);
+                                       right = ((Constant) right).ConvertExplicitly (false, underlying_type).Resolve (ec);
                                else
                                        right = EmptyCast.Create (right, underlying_type);
 
@@ -2894,17 +2974,29 @@ namespace Mono.CSharp {
                                return expr;
 
                        //
-                       // TODO: Need to corectly implemented Coumpound Assigment for all operators
                        // Section: 7.16.2
                        //
-                       if (Convert.ImplicitConversionExists (ec, left, rtype))
+
+                       //
+                       // If the return type of the selected operator is implicitly convertible to the type of x
+                       //
+                       if (Convert.ImplicitConversionExists (ec, expr, ltype))
                                return expr;
 
-                       if (!Convert.ImplicitConversionExists (ec, ltemp, rtype))
+                       //
+                       // Otherwise, if the selected operator is a predefined operator, if the return type of the
+                       // selected operator is explicitly convertible to the type of x, and if y is implicitly
+                       // convertible to the type of x or the operator is a shift operator, then the operation
+                       // is evaluated as x = (T)(x op y), where T is the type of x
+                       //
+                       expr = Convert.ExplicitConversion (ec, expr, ltype, loc);
+                       if (expr == null)
                                return null;
 
-                       expr = Convert.ExplicitConversion (ec, expr, rtype, loc);
-                       return expr;
+                       if (Convert.ImplicitConversionExists (ec, ltemp, ltype))
+                               return expr;
+
+                       return null;
                }
 
                //
@@ -2986,7 +3078,9 @@ namespace Mono.CSharp {
                        if (lgen) {
                                if (!TypeManager.IsReferenceType (l))
                                        return null;
-                               left = new BoxedCast (left, TypeManager.object_type);
+
+                               l = TypeManager.object_type;
+                               left = new BoxedCast (left, l);
                        } else if (l.IsInterface) {
                                l = TypeManager.object_type;
                        } else if (TypeManager.IsStruct (l)) {
@@ -2996,7 +3090,9 @@ namespace Mono.CSharp {
                        if (rgen) {
                                if (!TypeManager.IsReferenceType (r))
                                        return null;
-                               right = new BoxedCast (right, TypeManager.object_type);
+
+                               r = TypeManager.object_type;
+                               right = new BoxedCast (right, r);
                        } else if (r.IsInterface) {
                                r = TypeManager.object_type;
                        } else if (TypeManager.IsStruct (r)) {
@@ -3013,14 +3109,14 @@ namespace Mono.CSharp {
                        //
                        if (Convert.ImplicitReferenceConversionExists (left, r)) {
                                if (l == TypeManager.string_type)
-                                       Report.Warning (253, 2, loc, ref_comparison, "right");
+                                       ec.Report.Warning (253, 2, loc, ref_comparison, "right");
 
                                return this;
                        }
 
                        if (Convert.ImplicitReferenceConversionExists (right, l)) {
                                if (r == TypeManager.string_type)
-                                       Report.Warning (252, 2, loc, ref_comparison, "left");
+                                       ec.Report.Warning (252, 2, loc, ref_comparison, "left");
 
                                return this;
                        }
@@ -3098,8 +3194,8 @@ namespace Mono.CSharp {
                                best_operator = po.ResolveBetterOperator (ec, best_operator);
 
                                if (best_operator == null) {
-                                       Report.Error (34, loc, "Operator `{0}' is ambiguous on operands of type `{1}' and `{2}'",
-                                               OperName (oper), left.GetSignatureForError (), right.GetSignatureForError ());
+                                       ec.Report.Error (34, loc, "Operator `{0}' is ambiguous on operands of type `{1}' and `{2}'",
+                                               OperName (oper), TypeManager.CSharpName (l), TypeManager.CSharpName (r));
 
                                        best_operator = po;
                                        break;
@@ -3110,6 +3206,25 @@ namespace Mono.CSharp {
                                return null;
 
                        Expression expr = best_operator.ConvertResult (ec, this);
+
+                       //
+                       // Optimize &/&& constant expressions with 0 value
+                       //
+                       if (oper == Operator.BitwiseAnd || oper == Operator.LogicalAnd) {
+                               Constant rc = right as Constant;
+                               Constant lc = left as Constant;
+                               if ((lc != null && lc.IsDefaultValue) || (rc != null && rc.IsDefaultValue)) {
+                                       //
+                                       // The result is a constant with side-effect
+                                       //
+                                       Constant side_effect = rc == null ?
+                                               new SideEffectConstant (lc, right, loc) :
+                                               new SideEffectConstant (rc, left, loc);
+
+                                       return ReducedExpression.Create (side_effect.Resolve (ec), expr);
+                               }
+                       }
+
                        if (enum_type == null)
                                return expr;
 
@@ -3135,11 +3250,11 @@ namespace Mono.CSharp {
 
                        string op = GetOperatorMetadataName (user_oper);
 
-                       MethodGroupExpr left_operators = MemberLookup (ec.CurrentType, l, op, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
+                       MethodGroupExpr left_operators = MemberLookup (ec.Compiler, ec.CurrentType, l, op, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
                        MethodGroupExpr right_operators = null;
 
                        if (!TypeManager.IsEqual (r, l)) {
-                               right_operators = MemberLookup (ec.CurrentType, r, op, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
+                               right_operators = MemberLookup (ec.Compiler, ec.CurrentType, r, op, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
                                if (right_operators == null && left_operators == null)
                                        return null;
                        } else if (left_operators == null) {
@@ -3198,9 +3313,9 @@ namespace Mono.CSharp {
                                                (right is NullLiteral && IsBuildInEqualityOperator (l))) {
                                                type = TypeManager.bool_type;
                                                if (left is NullLiteral || right is NullLiteral)
-                                                       oper_expr = ReducedExpression.Create (this, oper_expr).Resolve (ec);
+                                                       oper_expr = ReducedExpression.Create (this, oper_expr);
                                        } else if (l != r) {
-                                               MethodInfo mi = (MethodInfo) union;
+                                               var mi = union.BestCandidate;
                                                
                                                //
                                                // Two System.Delegate(s) are never equal
@@ -3221,7 +3336,7 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               private void CheckUselessComparison (Constant c, Type type)
+               private void CheckUselessComparison (ResolveContext ec, Constant c, Type type)
                {
                        if (c == null || !IsTypeIntegral (type)
                                || c is StringConstant
@@ -3245,7 +3360,7 @@ namespace Mono.CSharp {
                                            type == TypeManager.uint32_type ||
                                            type == TypeManager.int64_type ||
                                                type == TypeManager.char_type)
-                                               WarnUselessComparison (type);
+                                               WarnUselessComparison (ec, type);
                                        return;
                                }
                                value = (long) uvalue;
@@ -3271,7 +3386,7 @@ namespace Mono.CSharp {
                                return;
 
                        if (IsValueOutOfRange (value, type))
-                               WarnUselessComparison (type);
+                               WarnUselessComparison (ec, type);
                }
 
                static bool IsValueOutOfRange (long value, Type type)
@@ -3322,9 +3437,9 @@ namespace Mono.CSharp {
                                type == TypeManager.char_type;
                }
 
-               private void WarnUselessComparison (Type type)
+               private void WarnUselessComparison (ResolveContext ec, Type type)
                {
-                       Report.Warning (652, 2, loc, "A comparison between a constant and a variable is useless. The constant is out of the range of the variable type `{0}'",
+                       ec.Report.Warning (652, 2, loc, "A comparison between a constant and a variable is useless. The constant is out of the range of the variable type `{0}'",
                                TypeManager.CSharpName (type));
                }
 
@@ -3518,20 +3633,19 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       left.Emit (ec);
-
                        //
-                       // Optimize zero-based operations
+                       // Optimize zero-based operations which cannot be optimized at expression level
                        //
-                       // TODO: Implement more optimizations, but it should probably go to PredefinedOperators
-                       //
-                       if ((oper & Operator.ShiftMask) != 0 || oper == Operator.Addition || oper == Operator.Subtraction) {
-                               Constant rc = right as Constant;
-                               if (rc != null && rc.IsDefaultValue) {
+                       if (oper == Operator.Subtraction) {
+                               var lc = left as IntegralConstant;
+                               if (lc != null && lc.IsDefaultValue) {
+                                       right.Emit (ec);
+                                       ig.Emit (OpCodes.Neg);
                                        return;
                                }
                        }
 
+                       left.Emit (ec);
                        right.Emit (ec);
                        EmitOperatorOpcode (ec, oper, l);
 
@@ -3546,7 +3660,7 @@ namespace Mono.CSharp {
                public override void EmitSideEffect (EmitContext ec)
                {
                        if ((oper & Operator.LogicalMask) != 0 ||
-                           (ec.HasSet (EmitContext.Options.CheckedScope) && (oper == Operator.Multiply || oper == Operator.Addition || oper == Operator.Subtraction))) {
+                               (ec.HasSet (EmitContext.Options.CheckedScope) && (oper == Operator.Multiply || oper == Operator.Addition || oper == Operator.Subtraction))) {
                                base.EmitSideEffect (ec);
                        } else {
                                left.EmitSideEffect (ec);
@@ -3569,16 +3683,19 @@ namespace Mono.CSharp {
                        MemberAccess sle = new MemberAccess (new MemberAccess (
                                new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Linq", loc), "Expressions", loc);
 
-                       MemberAccess binder = DynamicExpressionStatement.GetBinderNamespace (loc);
+                       CSharpBinderFlags flags = 0;
+                       if (ec.HasSet (ResolveContext.Options.CheckedScope))
+                               flags = CSharpBinderFlags.CheckedContext;
 
-                       binder_args.Add (new Argument (new MemberAccess (new MemberAccess (sle, "ExpressionType", loc), GetOperatorExpressionTypeName (), loc)));
-                       binder_args.Add (new Argument (new BoolLiteral (ec.HasSet (EmitContext.Options.CheckedScope), loc)));
+                       if ((oper & Operator.LogicalMask) != 0)
+                               flags |= CSharpBinderFlags.BinaryOperationLogical;
 
-                       bool member_access = left is DynamicMemberBinder || right is DynamicMemberBinder;
-                       binder_args.Add (new Argument (new BoolLiteral (member_access, loc)));
-                       binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (), loc)));
+                       binder_args.Add (new Argument (new EnumConstant (new IntLiteral ((int) flags, loc), TypeManager.binder_flags)));
+                       binder_args.Add (new Argument (new MemberAccess (new MemberAccess (sle, "ExpressionType", loc), GetOperatorExpressionTypeName (), loc)));
+                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));                                                                    
+                       binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (ec), loc)));
 
-                       return new New (new MemberAccess (binder, "CSharpBinaryOperationBinder", loc), binder_args, loc);
+                       return new Invocation (DynamicExpressionStatement.GetBinder ("BinaryOperation", loc), binder_args);
                }
                
                public override Expression CreateExpressionTree (ResolveContext ec)
@@ -3593,7 +3710,7 @@ namespace Mono.CSharp {
                        
                        switch (oper) {
                        case Operator.Addition:
-                               if (method == null && ec.HasSet (EmitContext.Options.CheckedScope) && !IsFloat (type))
+                               if (method == null && ec.HasSet (ResolveContext.Options.CheckedScope) && !IsFloat (type))
                                        method_name = "AddChecked";
                                else
                                        method_name = "Add";
@@ -3647,7 +3764,7 @@ namespace Mono.CSharp {
                                method_name = "Modulo";
                                break;
                        case Operator.Multiply:
-                               if (method == null && ec.HasSet (EmitContext.Options.CheckedScope) && !IsFloat (type))
+                               if (method == null && ec.HasSet (ResolveContext.Options.CheckedScope) && !IsFloat (type))
                                        method_name = "MultiplyChecked";
                                else
                                        method_name = "Multiply";
@@ -3656,7 +3773,7 @@ namespace Mono.CSharp {
                                method_name = "RightShift";
                                break;
                        case Operator.Subtraction:
-                               if (method == null && ec.HasSet (EmitContext.Options.CheckedScope) && !IsFloat (type))
+                               if (method == null && ec.HasSet (ResolveContext.Options.CheckedScope) && !IsFloat (type))
                                        method_name = "SubtractChecked";
                                else
                                        method_name = "Subtract";
@@ -3676,7 +3793,7 @@ namespace Mono.CSharp {
                                args.Add (new Argument (method.CreateExpressionTree (ec)));
                        }
                        
-                       return CreateExpressionFactoryCall (method_name, args);
+                       return CreateExpressionFactoryCall (ec, method_name, args);
                }
        }
        
@@ -3687,15 +3804,24 @@ namespace Mono.CSharp {
        public class StringConcat : Expression {
                Arguments arguments;
                
-               public StringConcat (Location loc, Expression left, Expression right)
+               public StringConcat (Expression left, Expression right, Location loc)
                {
                        this.loc = loc;
                        type = TypeManager.string_type;
                        eclass = ExprClass.Value;
 
                        arguments = new Arguments (2);
-                       Append (left);
-                       Append (right);
+               }
+
+               public static StringConcat Create (ResolveContext rc, Expression left, Expression right, Location loc)
+               {
+                       if (left.eclass == ExprClass.Unresolved || right.eclass == ExprClass.Unresolved)
+                               throw new ArgumentException ();
+
+                       var s = new StringConcat (left, right, loc);
+                       s.Append (rc, left);
+                       s.Append (rc, right);
+                       return s;
                }
 
                public override Expression CreateExpressionTree (ResolveContext ec)
@@ -3728,20 +3854,20 @@ namespace Mono.CSharp {
 
                        add_args.Add (new Argument (method.CreateExpressionTree (ec)));
 
-                       Expression expr = CreateExpressionFactoryCall ("Add", add_args);
+                       Expression expr = CreateExpressionFactoryCall (ec, "Add", add_args);
                        if (++pos == arguments.Count)
                                return expr;
 
-                       left = new Argument (new EmptyExpression (((MethodInfo)method).ReturnType));
+                       left = new Argument (new EmptyExpression (method.BestCandidate.ReturnType));
                        return CreateExpressionAddCall (ec, left, expr, pos);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return this;
                }
                
-               public void Append (Expression operand)
+               void Append (ResolveContext rc, Expression operand)
                {
                        //
                        // Constant folding
@@ -3753,7 +3879,7 @@ namespace Mono.CSharp {
                                        StringConstant last_expr_constant = last_argument.Expr as StringConstant;
                                        if (last_expr_constant != null) {
                                                last_argument.Expr = new StringConstant (
-                                                       last_expr_constant.Value + sc.Value, sc.Location);
+                                                       last_expr_constant.Value + sc.Value, sc.Location).Resolve (rc);
                                                return;
                                        }
                                }
@@ -3783,6 +3909,15 @@ namespace Mono.CSharp {
                        if (concat != null)
                                concat.Emit (ec);
                }
+
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
+               {
+                       if (arguments.Count != 2)
+                               throw new NotImplementedException ("arguments.Count != 2");
+
+                       var concat = TypeManager.string_type.GetMethod ("Concat", new[] { typeof (object), typeof (object) });
+                       return SLE.Expression.Add (arguments[0].Expr.MakeExpression (ctx), arguments[1].Expr.MakeExpression (ctx), concat);
+               }
                
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
@@ -3802,17 +3937,18 @@ namespace Mono.CSharp {
                        : base (oper_method, arguments, expr_tree, loc)
                {
                        this.is_and = is_and;
+                       eclass = ExprClass.Unresolved;
                }
                
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       MethodInfo method = (MethodInfo)mg;
+                       var method = mg.BestCandidate;
                        type = TypeManager.TypeToCoreType (method.ReturnType);
-                       AParametersCollection pd = TypeManager.GetParameterData (method);
+                       AParametersCollection pd = method.Parameters;
                        if (!TypeManager.IsEqual (type, type) || !TypeManager.IsEqual (type, pd.Types [0]) || !TypeManager.IsEqual (type, pd.Types [1])) {
-                               Report.Error (217, loc,
+                               ec.Report.Error (217, loc,
                                        "A user-defined operator `{0}' must have parameters and return values of the same type in order to be applicable as a short circuit operator",
-                                       TypeManager.CSharpSignature (method));
+                                       TypeManager.CSharpSignature (method.MetaInfo));
                                return null;
                        }
 
@@ -3820,9 +3956,9 @@ namespace Mono.CSharp {
                        Expression op_true = GetOperatorTrue (ec, left_dup, loc);
                        Expression op_false = GetOperatorFalse (ec, left_dup, loc);
                        if (op_true == null || op_false == null) {
-                               Report.Error (218, loc,
+                               ec.Report.Error (218, loc,
                                        "The type `{0}' must have operator `true' and operator `false' defined when `{1}' is used as a short circuit operator",
-                                       TypeManager.CSharpName (type), TypeManager.CSharpSignature (method));
+                                       TypeManager.CSharpName (type), TypeManager.CSharpSignature (method.MetaInfo));
                                return null;
                        }
 
@@ -3867,16 +4003,16 @@ namespace Mono.CSharp {
 
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       Error_PointerInsideExpressionTree ();
+                       Error_PointerInsideExpressionTree (ec);
                        return null;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        eclass = ExprClass.Variable;
                        
                        if (left.Type == TypeManager.void_ptr_type) {
-                               Error (242, "The operation in question is undefined on void pointers");
+                               ec.Report.Error (242, loc, "The operation in question is undefined on void pointers");
                                return null;
                        }
                        
@@ -3895,7 +4031,7 @@ namespace Mono.CSharp {
                        } else {
                                FieldExpr fe = left as FieldExpr;
                                if (fe != null)
-                                       element = AttributeTester.GetFixedBuffer (fe.FieldInfo).ElementType;
+                                       element = ((FixedFieldSpec) (fe.Spec)).ElementType;
                                else
                                        element = op_type;
                        }
@@ -3937,7 +4073,7 @@ namespace Mono.CSharp {
 
                                left.Emit (ec);
 
-                               Constant right_const = right as Constant;
+                               var right_const = right as Constant;
                                if (right_const != null) {
                                        //
                                        // Optimize 0-based arithmetic
@@ -3945,16 +4081,16 @@ namespace Mono.CSharp {
                                        if (right_const.IsDefaultValue)
                                                return;
 
-                                       if (size != 0) {
-                                               // TODO: Should be the checks resolve context sensitive?
-                                               ResolveContext rc = new ResolveContext (ec.MemberContext);
-                                               right = ConstantFold.BinaryFold (rc, Binary.Operator.Multiply, new IntConstant (size, right.Location), right_const, loc);
-                                               if (right == null)
-                                                       return;
-                                       } else {
-                                               ig.Emit (OpCodes.Sizeof, element);
-                                               right = EmptyExpression.Null;
-                                       }
+                                       if (size != 0)
+                                               right = new IntConstant (size, right.Location);
+                                       else
+                                               right = new SizeOf (new TypeExpression (element, right.Location), right.Location);
+                                       
+                                       // TODO: Should be the checks resolve context sensitive?
+                                       ResolveContext rc = new ResolveContext (ec.MemberContext, ResolveContext.Options.UnsafeScope);
+                                       right = new Binary (Binary.Operator.Multiply, right, right_const, loc).Resolve (rc);
+                                       if (right == null)
+                                               return;
                                }
 
                                right.Emit (ec);
@@ -3987,14 +4123,76 @@ namespace Mono.CSharp {
                        }
                }
        }
+
+       //
+       // A boolean-expression is an expression that yields a result
+       // of type bool
+       //
+       public class BooleanExpression : ShimExpression
+       {
+               public BooleanExpression (Expression expr)
+                       : base (expr)
+               {
+                       this.loc = expr.Location;
+               }
+
+               public override Expression CreateExpressionTree (ResolveContext ec)
+               {
+                       // TODO: We should emit IsTrue (v4) instead of direct user operator
+                       // call but that would break csc compatibility
+                       return base.CreateExpressionTree (ec);
+               }
+
+               protected override Expression DoResolve (ResolveContext ec)
+               {
+                       // A boolean-expression is required to be of a type
+                       // that can be implicitly converted to bool or of
+                       // a type that implements operator true
+
+                       expr = expr.Resolve (ec);
+                       if (expr == null)
+                               return null;
+
+                       Assign ass = expr as Assign;
+                       if (ass != null && ass.Source is Constant) {
+                               ec.Report.Warning (665, 3, loc,
+                                       "Assignment in conditional expression is always constant. Did you mean to use `==' instead ?");
+                       }
+
+                       if (expr.Type == TypeManager.bool_type)
+                               return expr;
+
+                       if (TypeManager.IsDynamicType (expr.Type)) {
+                               Arguments args = new Arguments (1);
+                               args.Add (new Argument (expr));
+                               return new DynamicUnaryConversion ("IsTrue", args, loc).Resolve (ec);
+                       }
+
+                       type = TypeManager.bool_type;
+                       Expression converted = Convert.ImplicitConversion (ec, expr, type, loc);
+                       if (converted != null)
+                               return converted;
+
+                       //
+                       // If no implicit conversion to bool exists, try using `operator true'
+                       //
+                       converted = GetOperatorTrue (ec, expr, loc);
+                       if (converted == null) {
+                               expr.Error_ValueCannotBeConverted (ec, loc, type, false);
+                               return null;
+                       }
+
+                       return converted;
+               }
+       }
        
        /// <summary>
        ///   Implements the ternary conditional operator (?:)
        /// </summary>
        public class Conditional : Expression {
                Expression expr, true_expr, false_expr;
-               
-               public Conditional (Expression expr, Expression true_expr, Expression false_expr)
+
+               public Conditional (BooleanExpression expr, Expression true_expr, Expression false_expr)
                {
                        this.expr = expr;
                        this.true_expr = true_expr;
@@ -4026,18 +4224,12 @@ namespace Mono.CSharp {
                        args.Add (new Argument (expr.CreateExpressionTree (ec)));
                        args.Add (new Argument (true_expr.CreateExpressionTree (ec)));
                        args.Add (new Argument (false_expr.CreateExpressionTree (ec)));
-                       return CreateExpressionFactoryCall ("Condition", args);
+                       return CreateExpressionFactoryCall (ec, "Condition", args);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       expr = Expression.ResolveBoolean (ec, expr, loc);
-                       
-                       Assign ass = expr as Assign;
-                       if (ass != null && ass.Source is Constant) {
-                               Report.Warning (665, 3, loc, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
-                       }
-
+                       expr = expr.Resolve (ec);
                        true_expr = true_expr.Resolve (ec);
                        false_expr = false_expr.Resolve (ec);
 
@@ -4057,14 +4249,12 @@ namespace Mono.CSharp {
                                Expression conv = Convert.ImplicitConversion (ec, true_expr, false_type, loc);
                                if (conv != null) {
                                        //
-                                       // Check if both can convert implicitl to each other's type
+                                       // Check if both can convert implicitly to each other's type
                                        //
                                        if (Convert.ImplicitConversion (ec, false_expr, true_type, loc) != null) {
-                                               Error (172,
-                                                          "Can not compute type of conditional expression " +
-                                                          "as `" + TypeManager.CSharpName (true_expr.Type) +
-                                                          "' and `" + TypeManager.CSharpName (false_expr.Type) +
-                                                          "' convert implicitly to each other");
+                                               ec.Report.Error (172, true_expr.Location,
+                                                       "Type of conditional expression cannot be determined as `{0}' and `{1}' convert implicitly to each other",
+                                                       TypeManager.CSharpName (true_type), TypeManager.CSharpName (false_type));
                                                return null;
                                        }
                                        type = false_type;
@@ -4072,9 +4262,9 @@ namespace Mono.CSharp {
                                } else if ((conv = Convert.ImplicitConversion (ec, false_expr, true_type, loc)) != null) {
                                        false_expr = conv;
                                } else {
-                                       Report.Error (173, loc,
+                                       ec.Report.Error (173, true_expr.Location,
                                                "Type of conditional expression cannot be determined because there is no implicit conversion between `{0}' and `{1}'",
-                                               true_expr.GetSignatureForError (), false_expr.GetSignatureForError ());
+                                               TypeManager.CSharpName (true_type), TypeManager.CSharpName (false_type));
                                        return null;
                                }
                        }                       
@@ -4083,7 +4273,7 @@ namespace Mono.CSharp {
                        Constant c = expr as Constant;
                        if (c != null){
                                bool is_false = c.IsDefaultValue;
-                               Report.Warning (429, 4, is_false ? true_expr.Location : false_expr.Location, "Unreachable expression code detected");
+                               ec.Report.Warning (429, 4, is_false ? true_expr.Location : false_expr.Location, "Unreachable expression code detected");
                                return ReducedExpression.Create (is_false ? false_expr : true_expr, this).Resolve (ec);
                        }
 
@@ -4139,7 +4329,7 @@ namespace Mono.CSharp {
                LocalTemporary temp;
 
                #region Abstract
-               public abstract HoistedVariable GetHoistedVariable (EmitContext ec);
+               public abstract HoistedVariable GetHoistedVariable (AnonymousExpression ae);
                public abstract bool IsFixed { get; }
                public abstract bool IsRef { get; }
                public abstract string Name { get; }
@@ -4156,7 +4346,7 @@ namespace Mono.CSharp {
                public abstract VariableInfo VariableInfo { get; }
                #endregion
 
-               public void AddressOf (EmitContext ec, AddressOp mode)
+               public virtual void AddressOf (EmitContext ec, AddressOp mode)
                {
                        HoistedVariable hv = GetHoistedVariable (ec);
                        if (hv != null) {
@@ -4167,6 +4357,21 @@ namespace Mono.CSharp {
                        Variable.EmitAddressOf (ec);
                }
 
+               public HoistedVariable GetHoistedVariable (ResolveContext rc)
+               {
+                       return GetHoistedVariable (rc.CurrentAnonymousMethod);
+               }
+
+               public HoistedVariable GetHoistedVariable (EmitContext ec)
+               {
+                       return GetHoistedVariable (ec.CurrentAnonymousMethod);
+               }
+
+               public override string GetSignatureForError ()
+               {
+                       return Name;
+               }
+
                public override void Emit (EmitContext ec)
                {
                        Emit (ec, false);
@@ -4261,7 +4466,7 @@ namespace Mono.CSharp {
                }
 
                public bool IsHoisted {
-                       get { return GetHoistedVariable (null) != null; }
+                       get { return GetHoistedVariable ((AnonymousExpression) null) != null; }
                }
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
@@ -4278,7 +4483,6 @@ namespace Mono.CSharp {
                public Block Block;
                public LocalInfo local_info;
                bool is_readonly;
-               bool resolved;  // TODO: merge with eclass
 
                public LocalVariableReference (Block block, string name, Location l)
                {
@@ -4303,9 +4507,9 @@ namespace Mono.CSharp {
                        get { return local_info.VariableInfo; }
                }
 
-               public override HoistedVariable GetHoistedVariable (EmitContext ec)
+               public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
                {
-                       return local_info.HoistedVariableReference;
+                       return local_info.HoistedVariant;
                }
 
                //              
@@ -4351,17 +4555,15 @@ namespace Mono.CSharp {
                {
                        HoistedVariable hv = GetHoistedVariable (ec);
                        if (hv != null)
-                               return hv.CreateExpressionTree (ec);
+                               return hv.CreateExpressionTree ();
 
                        Arguments arg = new Arguments (1);
                        arg.Add (new Argument (this));
-                       return CreateExpressionFactoryCall ("Constant", arg);
+                       return CreateExpressionFactoryCall (ec, "Constant", arg);
                }
 
                Expression DoResolveBase (ResolveContext ec)
                {
-                       type = local_info.VariableType;
-
                        Expression e = Block.GetConstantExpression (Name);
                        if (e != null)
                                return e.Resolve (ec);
@@ -4374,7 +4576,7 @@ namespace Mono.CSharp {
                        //
                        if (ec.MustCaptureVariable (local_info)) {
                                if (local_info.AddressTaken)
-                                       AnonymousMethodExpression.Error_AddressOfCapturedVar (this, loc);
+                                       AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, this, loc);
 
                                if (ec.IsVariableCapturingRequired) {
                                        AnonymousMethodStorey storey = local_info.Block.Explicit.CreateAnonymousMethodStorey (ec);
@@ -4382,22 +4584,19 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       resolved |= ec.DoFlowAnalysis;
                        eclass = ExprClass.Variable;
+                       type = local_info.VariableType;
                        return this;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (resolved)
-                               return this;
-
                        ResolveLocalInfo ();
                        local_info.Used = true;
 
                        if (type == null && local_info.Type is VarExpr) {
                            local_info.VariableType = TypeManager.object_type;
-                               Error_VariableIsUsedBeforeItIsDeclared (Name);
+                               Error_VariableIsUsedBeforeItIsDeclared (ec.Report, Name);
                            return null;
                        }
                        
@@ -4409,7 +4608,7 @@ namespace Mono.CSharp {
                        ResolveLocalInfo ();
 
                        // is out param
-                       if (right_side == EmptyExpression.OutAccess)
+                       if (right_side == EmptyExpression.OutAccess.Instance)
                                local_info.Used = true;
 
                        // Infer implicitly typed local variable
@@ -4425,7 +4624,7 @@ namespace Mono.CSharp {
                        if (is_readonly) {
                                int code;
                                string msg;
-                               if (right_side == EmptyExpression.OutAccess) {
+                               if (right_side == EmptyExpression.OutAccess.Instance) {
                                        code = 1657; msg = "Cannot pass `{0}' as a ref or out argument because it is a `{1}'";
                                } else if (right_side == EmptyExpression.LValueMemberAccess) {
                                        code = 1654; msg = "Cannot assign to members of `{0}' because it is a `{1}'";
@@ -4436,7 +4635,7 @@ namespace Mono.CSharp {
                                } else {
                                        code = 1656; msg = "Cannot assign to `{0}' because it is a `{1}'";
                                }
-                               Report.Error (code, loc, msg, Name, local_info.GetReadOnlyContext ());
+                               ec.Report.Error (code, loc, msg, Name, local_info.GetReadOnlyContext ());
                        } else if (VariableInfo != null) {
                                VariableInfo.SetAssigned (ec);
                        }
@@ -4498,9 +4697,9 @@ namespace Mono.CSharp {
                        get { return pi.Parameter.ModFlags == Parameter.Modifier.OUT; }
                }
 
-               public override HoistedVariable GetHoistedVariable (EmitContext ec)
+               public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
                {
-                       return pi.Parameter.HoistedVariableReference;
+                       return pi.Parameter.HoistedVariant;
                }
 
                //
@@ -4536,7 +4735,7 @@ namespace Mono.CSharp {
                        if (!ec.DoFlowAnalysis || !HasOutModifier || ec.CurrentBranching.IsAssigned (VariableInfo))
                                return true;
 
-                       Report.Error (269, loc, "Use of unassigned out parameter `{0}'", Name);
+                       ec.Report.Error (269, loc, "Use of unassigned out parameter `{0}'", Name);
                        return false;
                }
 
@@ -4562,37 +4761,36 @@ namespace Mono.CSharp {
 
                        Block b = ec.CurrentBlock;
                        while (b != null) {
+                               b = b.Toplevel;
                                IParameterData[] p = b.Toplevel.Parameters.FixedParameters;
                                for (int i = 0; i < p.Length; ++i) {
                                        if (p [i] != Parameter)
                                                continue;
 
                                        //
-                                       // Skip closest anonymous method parameters
+                                       // Don't capture local parameters
                                        //
-                                       if (b == ec.CurrentBlock && !am.IsIterator)
+                                       if (b == ec.CurrentBlock.Toplevel && !am.IsIterator)
                                                return true;
 
                                        if (IsRef) {
-                                               Report.Error (1628, loc,
+                                               ec.Report.Error (1628, loc,
                                                        "Parameter `{0}' cannot be used inside `{1}' when using `ref' or `out' modifier",
                                                        Name, am.ContainerType);
                                        }
 
-                                       b = null;
-                                       break;
-                               }
+                                       if (pi.Parameter.HasAddressTaken)
+                                               AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, this, loc);
 
-                               if (b != null)
-                                       b = b.Toplevel.Parent;
-                       }
+                                       if (ec.IsVariableCapturingRequired && !b.Toplevel.IsExpressionTree) {
+                                               AnonymousMethodStorey storey = pi.Block.CreateAnonymousMethodStorey (ec);
+                                               storey.CaptureParameter (ec, this);
+                                       }
 
-                       if (pi.Parameter.HasAddressTaken)
-                               AnonymousMethodExpression.Error_AddressOfCapturedVar (this, loc);
+                                       return true;
+                               }
 
-                       if (ec.IsVariableCapturingRequired) {
-                               AnonymousMethodStorey storey = pi.Block.CreateAnonymousMethodStorey (ec);
-                               storey.CaptureParameter (ec, this);
+                               b = b.Parent;
                        }
 
                        return true;
@@ -4611,6 +4809,19 @@ namespace Mono.CSharp {
 
                        return Name == pr.Name;
                }
+
+               public override void AddressOf (EmitContext ec, AddressOp mode)
+               {
+                       //
+                       // ParameterReferences might already be a reference
+                       //
+                       if (IsRef) {
+                               EmitLoad (ec);
+                               return;
+                       }
+
+                       base.AddressOf (ec, mode);
+               }
                
                protected override void CloneTo (CloneContext clonectx, Expression target)
                {
@@ -4621,7 +4832,7 @@ namespace Mono.CSharp {
                {
                        HoistedVariable hv = GetHoistedVariable (ec);
                        if (hv != null)
-                               return hv.CreateExpressionTree (ec);
+                               return hv.CreateExpressionTree ();
 
                        return Parameter.ExpressionTreeVariableReference ();
                }
@@ -4638,7 +4849,7 @@ namespace Mono.CSharp {
                // the type as it is expected, but when we generate the code, we generate
                // the alternate kind of code.
                //
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (!DoResolveBase (ec))
                                return null;
@@ -4718,39 +4929,24 @@ namespace Mono.CSharp {
 
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       Arguments args;
-
-                       //
-                       // Special conversion for nested expression trees
-                       //
-                       if (TypeManager.DropGenericTypeArguments (type) == TypeManager.expression_type) {
-                               args = new Arguments (1);
-                               args.Add (new Argument (this));
-                               return CreateExpressionFactoryCall ("Quote", args);
-                       }
-
                        Expression instance = mg.IsInstance ?
                                mg.InstanceExpression.CreateExpressionTree (ec) :
                                new NullLiteral (loc);
 
-                       args = Arguments.CreateForExpressionTree (ec, arguments,
+                       var args = Arguments.CreateForExpressionTree (ec, arguments,
                                instance,
                                mg.CreateExpressionTree (ec));
 
                        if (mg.IsBase)
-                               MemberExpr.Error_BaseAccessInExpressionTree (loc);
+                               MemberExpr.Error_BaseAccessInExpressionTree (ec, loc);
 
-                       return CreateExpressionFactoryCall ("Call", args);
+                       return CreateExpressionFactoryCall (ec, "Call", args);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       // Don't resolve already resolved expression
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-                       
-                       Expression expr_resolved = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
-                       if (expr_resolved == null)
+                       Expression member_expr = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
+                       if (member_expr == null)
                                return null;
 
                        //
@@ -4760,80 +4956,49 @@ namespace Mono.CSharp {
                        if (arguments != null && !arguments_resolved)
                                arguments.Resolve (ec, out dynamic_arg);
 
-                       Type expr_type = expr_resolved.Type;
-                       mg = expr_resolved as MethodGroupExpr;
-
-                       if (dynamic_arg || TypeManager.IsDynamicType (expr_type)) {
-                               Arguments args;
-                               DynamicMemberBinder dmb = expr_resolved as DynamicMemberBinder;
-                               if (dmb != null) {
-                                       args = dmb.Arguments;
-                                       if (arguments != null)
-                                               args.AddRange (arguments);
-                               } else if (mg == null) {
-                                       if (arguments == null)
-                                               args = new Arguments (1);
-                                       else
-                                               args = arguments;
-
-                                       args.Insert (0, new Argument (expr_resolved));
-                                       expr = null;
-                               } else {
-                                       if (mg.IsBase) {
-                                               Report.Error (1971, loc,
-                                                       "The base call to method `{0}' cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access",
-                                                       mg.Name);
-                                               return null;
-                                       }
+                       Type expr_type = member_expr.Type;
+                       mg = member_expr as MethodGroupExpr;
 
-                                       args = arguments;
+                       bool dynamic_member = TypeManager.IsDynamicType (expr_type);
 
-                                       if (mg.IsStatic != mg.IsInstance) {
-                                               if (args == null)
-                                                       args = new Arguments (1);
+                       if (!dynamic_member) {
+                               Expression invoke = null;
 
-                                               if (mg.IsStatic) {
-                                                       args.Insert (0, new Argument (new TypeOf (new TypeExpression (mg.DeclaringType, loc), loc).Resolve (ec), Argument.AType.DynamicStatic));
-                                               } else {
-                                                       MemberAccess ma = expr as MemberAccess;
-                                                       if (ma != null)
-                                                               args.Insert (0, new Argument (ma.Left.Resolve (ec)));
-                                                       else
-                                                               args.Insert (0, new Argument (new This (loc).Resolve (ec)));
+                               if (mg == null) {
+                                       if (expr_type != null && TypeManager.IsDelegateType (expr_type)) {
+                                               invoke = new DelegateInvocation (member_expr, arguments, loc);
+                                               invoke = invoke.Resolve (ec);
+                                               if (invoke == null || !dynamic_arg)
+                                                       return invoke;
+                                       } else {
+                                               MemberExpr me = member_expr as MemberExpr;
+                                               if (me == null) {
+                                                       member_expr.Error_UnexpectedKind (ec, ResolveFlags.MethodGroup, loc);
+                                                       return null;
                                                }
-                                       }
-                               }
 
-                               return new DynamicInvocation (expr as ATypeNameExpression, args, loc).Resolve (ec);
-                       }
+                                               mg = ec.LookupExtensionMethod (me.Type, me.Name, loc);
+                                               if (mg == null) {
+                                                       ec.Report.Error (1955, loc, "The member `{0}' cannot be used as method or delegate",
+                                                               member_expr.GetSignatureForError ());
+                                                       return null;
+                                               }
 
-                       if (mg == null) {
-                               if (expr_type != null && TypeManager.IsDelegateType (expr_type)){
-                                       return (new DelegateInvocation (
-                                               expr_resolved, arguments, loc)).Resolve (ec);
+                                               ((ExtensionMethodGroupExpr) mg).ExtensionExpression = me.InstanceExpression;
+                                       }
                                }
 
-                               MemberExpr me = expr_resolved as MemberExpr;
-                               if (me == null) {
-                                       expr_resolved.Error_UnexpectedKind (ResolveFlags.MethodGroup, loc);
-                                       return null;
-                               }
-                               
-                               mg = ec.LookupExtensionMethod (me.Type, me.Name, loc);
-                               if (mg == null) {
-                                       Report.Error (1955, loc, "The member `{0}' cannot be used as method or delegate",
-                                               expr_resolved.GetSignatureForError ());
-                                       return null;
+                               if (invoke == null) {
+                                       mg = DoResolveOverload (ec);
+                                       if (mg == null)
+                                               return null;
                                }
-
-                               ((ExtensionMethodGroupExpr)mg).ExtensionExpression = me.InstanceExpression;
                        }
 
-                       mg = DoResolveOverload (ec);
-                       if (mg == null)
-                               return null;
+                       if (dynamic_arg || dynamic_member)
+                               return DoResolveDynamic (ec, member_expr);
 
-                       MethodInfo method = (MethodInfo)mg;
+                       var method = mg.BestCandidate;
                        if (method != null) {
                                type = TypeManager.TypeToCoreType (method.ReturnType);
 
@@ -4845,7 +5010,7 @@ namespace Mono.CSharp {
                                                mg.IdenticalTypeName) {
                                                mg.InstanceExpression = null;
                                        } else {
-                                               MemberExpr.error176 (loc, mg.GetSignatureForError ());
+                                               MemberExpr.error176 (ec, loc, mg.GetSignatureForError ());
                                                return null;
                                        }
                                } else {
@@ -4854,31 +5019,24 @@ namespace Mono.CSharp {
                                        }
                                }
                        }
-
-                       if (type.IsPointer){
-                               if (!ec.InUnsafe){
-                                       UnsafeError (loc);
-                                       return null;
-                               }
-                       }
-                       
+               
                        //
                        // Only base will allow this invocation to happen.
                        //
                        if (mg.IsBase && method.IsAbstract){
-                               Error_CannotCallAbstractBase (TypeManager.CSharpSignature (method));
+                               Error_CannotCallAbstractBase (ec, TypeManager.CSharpSignature (method));
                                return null;
                        }
 
                        if (arguments == null && method.DeclaringType == TypeManager.object_type && method.Name == Destructor.MetadataName) {
                                if (mg.IsBase)
-                                       Report.Error (250, loc, "Do not directly call your base class Finalize method. It is called automatically from your destructor");
+                                       ec.Report.Error (250, loc, "Do not directly call your base class Finalize method. It is called automatically from your destructor");
                                else
-                                       Report.Error (245, loc, "Destructors and object.Finalize cannot be called directly. Consider calling IDisposable.Dispose if available");
+                                       ec.Report.Error (245, loc, "Destructors and object.Finalize cannot be called directly. Consider calling IDisposable.Dispose if available");
                                return null;
                        }
 
-                       IsSpecialMethodInvocation (method, loc);
+                       IsSpecialMethodInvocation (ec, method, loc);
                        
                        if (mg.InstanceExpression != null)
                                mg.InstanceExpression.CheckMarshalByRefAccess (ec);
@@ -4887,26 +5045,74 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               Expression DoResolveDynamic (ResolveContext ec, Expression memberExpr)
+               {
+                       Arguments args;
+                       DynamicMemberBinder dmb = memberExpr as DynamicMemberBinder;
+                       if (dmb != null) {
+                               args = dmb.Arguments;
+                               if (arguments != null)
+                                       args.AddRange (arguments);
+                       } else if (mg == null) {
+                               if (arguments == null)
+                                       args = new Arguments (1);
+                               else
+                                       args = arguments;
+
+                               args.Insert (0, new Argument (memberExpr));
+                               this.expr = null;
+                       } else {
+                               if (mg.IsBase) {
+                                       ec.Report.Error (1971, loc,
+                                               "The base call to method `{0}' cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access",
+                                               mg.Name);
+                                       return null;
+                               }
+
+                               args = arguments;
+
+                               if (mg.IsStatic != mg.IsInstance) {
+                                       if (args == null)
+                                               args = new Arguments (1);
+
+                                       if (mg.IsStatic) {
+                                               args.Insert (0, new Argument (new TypeOf (new TypeExpression (mg.DeclaringType, loc), loc).Resolve (ec), Argument.AType.DynamicTypeName));
+                                       } else {
+                                               MemberAccess ma = expr as MemberAccess;
+                                               if (ma != null)
+                                                       args.Insert (0, new Argument (ma.Left.Resolve (ec)));
+                                               else
+                                                       args.Insert (0, new Argument (new This (loc).Resolve (ec)));
+                                       }
+                               }
+                       }
+
+                       return new DynamicInvocation (expr as ATypeNameExpression, args, loc).Resolve (ec);
+               }
+
                protected virtual MethodGroupExpr DoResolveOverload (ResolveContext ec)
                {
                        return mg.OverloadResolve (ec, ref arguments, false, loc);
                }
 
-               public static bool IsSpecialMethodInvocation (MethodBase method, Location loc)
+               public static bool IsSpecialMethodInvocation (ResolveContext ec, MethodSpec method, Location loc)
                {
-                       if (!TypeManager.IsSpecialMethod (method))
+                       if (!TypeManager.IsSpecialMethod (method.MetaInfo))
+                               return false;
+
+                       if (ec.HasSet (ResolveContext.Options.InvokeSpecialName))
                                return false;
 
-                       Report.SymbolRelatedToPreviousError (method);
-                       Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
-                               TypeManager.CSharpSignature (method, true));
+                       ec.Report.SymbolRelatedToPreviousError (method.MetaInfo);
+                       ec.Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
+                               TypeManager.CSharpSignature (method.MetaInfo, true));
        
                        return true;
                }
 
-               static Type[] GetVarargsTypes (MethodBase mb, Arguments arguments)
+               static Type[] GetVarargsTypes (MethodSpec mb, Arguments arguments)
                {
-                       AParametersCollection pd = TypeManager.GetParameterData (mb);
+                       AParametersCollection pd = mb.Parameters;
                        
                        Argument a = arguments [pd.Count - 1];
                        Arglist list = (Arglist) a.Expr;
@@ -4917,14 +5123,14 @@ namespace Mono.CSharp {
                /// <summary>
                /// This checks the ConditionalAttribute on the method 
                /// </summary>
-               public static bool IsMethodExcluded (MethodBase method, Location loc)
+               public static bool IsMethodExcluded (MethodSpec method, Location loc)
                {
                        if (method.IsConstructor)
                                return false;
 
-                       method = TypeManager.DropGenericMethodArguments (method);
-                       if (method.DeclaringType.Module == RootContext.ToplevelTypes.Builder) {
-                               IMethodData md = TypeManager.GetMethod (method);
+                       var mb = TypeManager.DropGenericMethodArguments (method.MetaInfo);
+                       if (TypeManager.IsBeingCompiled (mb)) {
+                               IMethodData md = TypeManager.GetMethod (mb);
                                if (md != null)
                                        return md.IsExcluded ();
 
@@ -4933,7 +5139,7 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       return AttributeTester.IsConditionalMethodExcluded (method, loc);
+                       return AttributeTester.IsConditionalMethodExcluded (mb, loc);
                }
 
                /// <remarks>
@@ -4953,7 +5159,7 @@ namespace Mono.CSharp {
                /// </remarks>
                public static void EmitCall (EmitContext ec, bool is_base,
                                             Expression instance_expr,
-                                            MethodBase method, Arguments Arguments, Location loc)
+                                            MethodSpec method, Arguments Arguments, Location loc)
                {
                        EmitCall (ec, is_base, instance_expr, method, Arguments, loc, false, false);
                }
@@ -4966,7 +5172,7 @@ namespace Mono.CSharp {
                // only have been evaluated once.
                public static void EmitCall (EmitContext ec, bool is_base,
                                             Expression instance_expr,
-                                            MethodBase method, Arguments Arguments, Location loc,
+                                            MethodSpec method, Arguments Arguments, Location loc,
                                             bool dup_args, bool omit_args)
                {
                        ILGenerator ig = ec.ig;
@@ -5054,15 +5260,13 @@ namespace Mono.CSharp {
                        } else {
                                call_op = OpCodes.Callvirt;
                                
-#if GMCS_SOURCE
                                if ((instance_expr != null) && (instance_expr.Type.IsGenericParameter))
                                        ig.Emit (OpCodes.Constrained, instance_expr.Type);
-#endif
                        }
 
-                       if ((method.CallingConvention & CallingConventions.VarArgs) != 0) {
+                       if ((method.MetaInfo.CallingConvention & CallingConventions.VarArgs) != 0) {
                                Type[] varargs_types = GetVarargsTypes (method, Arguments);
-                               ig.EmitCall (call_op, (MethodInfo) method, varargs_types);
+                               ig.EmitCall (call_op, (MethodInfo) method.MetaInfo, varargs_types);
                                return;
                        }
 
@@ -5072,10 +5276,10 @@ namespace Mono.CSharp {
                        // and DoFoo is not virtual, you can omit the callvirt,
                        // because you don't need the null checking behavior.
                        //
-                       if (method is MethodInfo)
-                               ig.Emit (call_op, (MethodInfo) method);
+                       if (method.IsConstructor)
+                               ig.Emit (call_op, (ConstructorInfo) method.MetaInfo);
                        else
-                               ig.Emit (call_op, (ConstructorInfo) method);
+                               ig.Emit (call_op, (MethodInfo) method.MetaInfo);
                }
 
                public override void Emit (EmitContext ec)
@@ -5099,142 +5303,46 @@ namespace Mono.CSharp {
                        Invocation target = (Invocation) t;
 
                        if (arguments != null)
-                               target.arguments = arguments.Clone (clonectx);
-
-                       target.expr = expr.Clone (clonectx);
-               }
-
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       mg.MutateHoistedGenericType (storey);
-                       type = storey.MutateType (type);
-                       if (arguments != null) {
-                               arguments.MutateHoistedGenericType (storey);
-                       }
-               }
-       }
-/*
-       //
-       // It's either a cast or delegate invocation
-       //
-       public class InvocationOrCast : ExpressionStatement
-       {
-               Expression expr;
-               Expression argument;
-
-               public InvocationOrCast (Expression expr, Expression argument)
-               {
-                       this.expr = expr;
-                       this.argument = argument;
-                       this.loc = expr.Location;
-               }
-
-               public override Expression CreateExpressionTree (ResolveContext ec)
-               {
-                       throw new NotSupportedException ("ET");
-               }
-
-               public override Expression DoResolve (ResolveContext ec)
-               {
-                       Expression e = ResolveCore (ec);
-                       if (e == null)
-                               return null;
-
-                       return e.Resolve (ec);
-               }
-
-               Expression ResolveCore (EmitContext ec)
-               {
-                       //
-                       // First try to resolve it as a cast.
-                       //
-                       TypeExpr te = expr.ResolveAsBaseTerminal (ec, true);
-                       if (te != null) {
-                               return new Cast (te, argument, loc);
-                       }
-
-                       //
-                       // This can either be a type or a delegate invocation.
-                       // Let's just resolve it and see what we'll get.
-                       //
-                       expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
-                       if (expr == null)
-                               return null;
-
-                       //
-                       // Ok, so it's a Cast.
-                       //
-                       if (expr.eclass == ExprClass.Type || expr.eclass == ExprClass.TypeParameter) {
-                               return new Cast (expr, argument, loc);
-                       }
-
-                       if (expr.eclass == ExprClass.Namespace) {
-                               expr.Error_UnexpectedKind (null, "type", loc);
-                               return null;
-                       }                       
-
-                       //
-                       // It's a delegate invocation.
-                       //
-                       if (!TypeManager.IsDelegateType (expr.Type)) {
-                               Error (149, "Method name expected");
-                               return null;
-                       }
-
-                       ArrayList args = new ArrayList (1);
-                       args.Add (new Argument (argument, Argument.AType.Expression));
-                       return new DelegateInvocation (expr, args, loc);
-               }
-
-               public override ExpressionStatement ResolveStatement (EmitContext ec)
-               {
-                       Expression e = ResolveCore (ec);
-                       if (e == null)
-                               return null;
-
-                       ExpressionStatement s = e as ExpressionStatement;
-                       if (s == null) {
-                               Error_InvalidExpressionStatement ();
-                               return null;
-                       }
+                               target.arguments = arguments.Clone (clonectx);
 
-                       return s.ResolveStatement (ec);
+                       target.expr = expr.Clone (clonectx);
                }
 
-               public override void Emit (EmitContext ec)
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       throw new Exception ("Cannot happen");
+                       return MakeExpression (ctx, mg.InstanceExpression, (MethodSpec) mg, arguments);
                }
 
-               public override void EmitStatement (EmitContext ec)
+               public static SLE.Expression MakeExpression (BuilderContext ctx, Expression instance, MethodSpec mi, Arguments args)
                {
-                       throw new Exception ("Cannot happen");
+                       var instance_expr = instance == null ? null : instance.MakeExpression (ctx);
+                       return SLE.Expression.Call (instance_expr, (MethodInfo) mi.MetaInfo, Arguments.MakeExpression (args, ctx));
                }
 
-               protected override void CloneTo (CloneContext clonectx, Expression t)
+               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
-                       InvocationOrCast target = (InvocationOrCast) t;
-
-                       target.expr = expr.Clone (clonectx);
-                       target.argument = argument.Clone (clonectx);
+                       mg.MutateHoistedGenericType (storey);
+                       type = storey.MutateType (type);
+                       if (arguments != null) {
+                               arguments.MutateHoistedGenericType (storey);
+                       }
                }
        }
-*/
 
        /// <summary>
        ///    Implements the new expression 
        /// </summary>
        public class New : ExpressionStatement, IMemoryLocation {
-               Arguments Arguments;
+               protected Arguments Arguments;
 
                //
                // During bootstrap, it contains the RequestedType,
                // but if `type' is not null, it *might* contain a NewDelegate
                // (because of field multi-initialization)
                //
-               Expression RequestedType;
+               protected Expression RequestedType;
 
-               MethodGroupExpr method;
+               protected MethodGroupExpr method;
 
                bool is_type_parameter;
 
@@ -5314,14 +5422,15 @@ namespace Mono.CSharp {
                                args = new Arguments (1);
                                args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
                        } else {
-                               args = Arguments.CreateForExpressionTree (ec, Arguments,
+                               args = Arguments.CreateForExpressionTree (ec,
+                                       Arguments,
                                        method.CreateExpressionTree (ec));
                        }
 
-                       return CreateExpressionFactoryCall ("New", args);
+                       return CreateExpressionFactoryCall (ec, "New", args);
                }
                
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        //
                        // The New DoResolve might be called twice when initializing field
@@ -5344,7 +5453,7 @@ namespace Mono.CSharp {
                        type = texpr.Type;
 
                        if (type.IsPointer) {
-                               Report.Error (1919, loc, "Unsafe type `{0}' cannot be used in an object creation expression",
+                               ec.Report.Error (1919, loc, "Unsafe type `{0}' cannot be used in an object creation expression",
                                        TypeManager.CSharpName (type));
                                return null;
                        }
@@ -5352,7 +5461,7 @@ namespace Mono.CSharp {
                        if (Arguments == null) {
                                Constant c = Constantify (type);
                                if (c != null)
-                                       return ReducedExpression.Create (c, this);
+                                       return ReducedExpression.Create (c.Resolve (ec), this);
                        }
 
                        if (TypeManager.IsDelegateType (type)) {
@@ -5363,24 +5472,21 @@ namespace Mono.CSharp {
                                GenericConstraints gc = TypeManager.GetTypeParameterConstraints (type);
 
                                if ((gc == null) || (!gc.HasConstructorConstraint && !gc.IsValueType)) {
-                                       Error (304, String.Format (
-                                                      "Cannot create an instance of the " +
-                                                      "variable type '{0}' because it " +
-                                                      "doesn't have the new() constraint",
-                                                      type));
+                                       ec.Report.Error (304, loc,
+                                               "Cannot create an instance of the variable type '{0}' because it doesn't have the new() constraint",
+                                               TypeManager.CSharpName (type));
                                        return null;
                                }
 
                                if ((Arguments != null) && (Arguments.Count != 0)) {
-                                       Error (417, String.Format (
-                                                      "`{0}': cannot provide arguments " +
-                                                      "when creating an instance of a " +
-                                                      "variable type.", type));
+                                       ec.Report.Error (417, loc,
+                                               "`{0}': cannot provide arguments when creating an instance of a variable type",
+                                               TypeManager.CSharpName (type));
                                        return null;
                                }
 
                                if (TypeManager.activator_create_instance == null) {
-                                       Type activator_type = TypeManager.CoreLookupType ("System", "Activator", Kind.Class, true);
+                                       Type activator_type = TypeManager.CoreLookupType (ec.Compiler, "System", "Activator", MemberKind.Class, true);
                                        if (activator_type != null) {
                                                TypeManager.activator_create_instance = TypeManager.GetPredefinedMethod (
                                                        activator_type, "CreateInstance", loc, Type.EmptyTypes);
@@ -5393,8 +5499,8 @@ namespace Mono.CSharp {
                        }
 
                        if (type.IsAbstract && type.IsSealed) {
-                               Report.SymbolRelatedToPreviousError (type);
-                               Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
+                               ec.Report.SymbolRelatedToPreviousError (type);
+                               ec.Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
                                return null;
                        }
 
@@ -5405,8 +5511,8 @@ namespace Mono.CSharp {
                                                return RequestedType;
                                }
                                
-                               Report.SymbolRelatedToPreviousError (type);
-                               Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
+                               ec.Report.SymbolRelatedToPreviousError (type);
+                               ec.Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
                                return null;
                        }
 
@@ -5424,14 +5530,11 @@ namespace Mono.CSharp {
                        Expression ml = MemberLookupFinal (ec, type, type, ConstructorInfo.ConstructorName,
                                MemberTypes.Constructor, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
 
+                       bool dynamic;
                        if (Arguments != null) {
-                               bool dynamic;
                                Arguments.Resolve (ec, out dynamic);
-
-                               if (dynamic) {
-                                       Arguments.Insert (0, new Argument (new TypeOf (texpr, loc).Resolve (ec)));
-                                       return new DynamicInvocation (new SimpleName (ConstructorInfo.ConstructorName, loc), Arguments, type, loc).Resolve (ec);
-                               }
+                       } else {
+                               dynamic = false;
                        }
 
                        if (ml == null)
@@ -5439,7 +5542,7 @@ namespace Mono.CSharp {
 
                        method = ml as MethodGroupExpr;
                        if (method == null) {
-                               ml.Error_UnexpectedKind (ResolveFlags.MethodGroup, loc);
+                               ml.Error_UnexpectedKind (ec, ResolveFlags.MethodGroup, loc);
                                return null;
                        }
 
@@ -5447,17 +5550,20 @@ namespace Mono.CSharp {
                        if (method == null)
                                return null;
 
+                       if (dynamic) {
+                               Arguments.Insert (0, new Argument (new TypeOf (texpr, loc).Resolve (ec), Argument.AType.DynamicTypeName));
+                               return new DynamicConstructorBinder (type, Arguments, loc).Resolve (ec);
+                       }
+
                        return this;
                }
 
                bool DoEmitTypeParameter (EmitContext ec)
                {
-#if GMCS_SOURCE
                        ILGenerator ig = ec.ig;
-//                     IMemoryLocation ml;
 
-                       MethodInfo ci = TypeManager.activator_create_instance.MakeGenericMethod (
-                               new Type [] { type });
+                       MethodInfo ci = (MethodInfo) TypeManager.activator_create_instance.MetaInfo;
+                       ci = ci.MakeGenericMethod (new Type [] { type });
 
                        GenericConstraints gc = TypeManager.GetTypeParameterConstraints (type);
                        if (gc.HasReferenceTypeConstraint || gc.HasClassConstraint) {
@@ -5490,9 +5596,6 @@ namespace Mono.CSharp {
                        ig.Emit (OpCodes.Call, ci);
                        ig.MarkLabel (label_end);
                        return true;
-#else
-                       throw new InternalErrorException ();
-#endif
                }
 
                //
@@ -5544,7 +5647,7 @@ namespace Mono.CSharp {
                                }
 
                                if (vr != null) {
-                                       ig.Emit (OpCodes.Call, (ConstructorInfo) method);
+                                       ig.Emit (OpCodes.Call, (ConstructorInfo) method.BestCandidate.MetaInfo);
                                        return false;
                                }
                        }
@@ -5552,9 +5655,9 @@ namespace Mono.CSharp {
                        if (is_type_parameter)
                                return DoEmitTypeParameter (ec);                        
 
-                       ConstructorInfo ci = (ConstructorInfo) method;
+                       ConstructorInfo ci = (ConstructorInfo) method.BestCandidate.MetaInfo;
 #if MS_COMPATIBLE
-                       if (TypeManager.IsGenericType (type))
+                       if (TypeManager.IsGenericType (type) && type.IsGenericTypeDefinition)
                                ci = TypeBuilder.GetConstructor (type, ci);
 #endif
 
@@ -5586,12 +5689,6 @@ namespace Mono.CSharp {
                                ec.ig.Emit (OpCodes.Pop);
                }
 
-               public bool IsDefaultValueType {
-                       get {
-                               return TypeManager.IsValueType (type) && !HasInitializer && Arguments == null;
-                       }
-               }
-
                public virtual bool HasInitializer {
                        get {
                                return false;
@@ -5632,7 +5729,7 @@ namespace Mono.CSharp {
                                if (Arguments != null)
                                        Arguments.Emit (ec);
 
-                               ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
+                               ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method.BestCandidate.MetaInfo);
                        }
                        
                        value_target.AddressOf (ec, mode);
@@ -5649,6 +5746,11 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
+               {
+                       return SLE.Expression.New ((ConstructorInfo) method.BestCandidate.MetaInfo, Arguments.MakeExpression (Arguments, ctx));
+               }
+
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
                        if (method != null) {
@@ -5662,6 +5764,57 @@ namespace Mono.CSharp {
                }
        }
 
+       public class ArrayInitializer : ShimExpression
+       {
+               List<Expression> elements;
+
+               public ArrayInitializer (List<Expression> init, Location loc)
+                       : base (null)
+               {
+                       elements = init;
+               }
+
+               public ArrayInitializer (int count, Location loc)
+                       : base (null)
+               {
+                       elements = new List<Expression> (count);
+               }
+
+               public ArrayInitializer (Location loc)
+                       : this (4, loc)
+               {
+               }
+
+               public void Add (Expression expr)
+               {
+                       elements.Add (expr);
+               }
+
+               protected override void CloneTo (CloneContext clonectx, Expression t)
+               {
+                       var target = (ArrayInitializer) t;
+
+                       target.elements = new List<Expression> (elements.Count);
+                       foreach (var element in elements)
+                               target.elements.Add (element.Clone (clonectx));
+
+                       base.CloneTo (clonectx, t);
+               }
+
+               public int Count {
+                       get { return elements.Count; }
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public Expression this [int index] {
+                       get { return elements [index]; }
+               }
+       }
+
        /// <summary>
        ///   14.5.10.2: Represents an array creation expression.
        /// </summary>
@@ -5672,46 +5825,45 @@ namespace Mono.CSharp {
        ///   initialization data and the other which does not need dimensions
        ///   specified but where initialization data is mandatory.
        /// </remarks>
-       public class ArrayCreation : Expression {
+       class ArrayCreation : Expression
+       {
                FullNamedExpression requested_base_type;
-               ArrayList initializers;
+               ArrayInitializer initializers;
 
                //
                // The list of Argument types.
                // This is used to construct the `newarray' or constructor signature
                //
-               protected ArrayList arguments;
+               protected List<Expression> arguments;
                
                protected Type array_element_type;
                bool expect_initializers = false;
                int num_arguments = 0;
                protected int dimensions;
                protected readonly string rank;
+               Expression first_emit;
+               LocalTemporary first_emit_temp;
 
-               protected ArrayList array_data;
+               protected List<Expression> array_data;
 
-               IDictionary bounds;
+               Dictionary<int, int> bounds;
 
                // The number of constants in array initializers
                int const_initializers_count;
                bool only_constant_initializers;
-               
-               public ArrayCreation (FullNamedExpression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l)
+
+               public ArrayCreation (FullNamedExpression requested_base_type, List<Expression> exprs, string rank, ArrayInitializer initializers, Location l)
                {
                        this.requested_base_type = requested_base_type;
                        this.initializers = initializers;
                        this.rank = rank;
                        loc = l;
 
-                       arguments = new ArrayList (exprs.Count);
-
-                       foreach (Expression e in exprs) {
-                               arguments.Add (e);
-                               num_arguments++;
-                       }
+                       arguments = new List<Expression> (exprs);
+                       num_arguments = arguments.Count;
                }
 
-               public ArrayCreation (FullNamedExpression requested_base_type, string rank, ArrayList initializers, Location l)
+               public ArrayCreation (FullNamedExpression requested_base_type, string rank, ArrayInitializer initializers, Location l)
                {
                        this.requested_base_type = requested_base_type;
                        this.initializers = initializers;
@@ -5726,20 +5878,15 @@ namespace Mono.CSharp {
                        expect_initializers = true;
                }
 
-               public static void Error_IncorrectArrayInitializer (Location loc)
-               {
-                       Report.Error (178, loc, "Invalid rank specifier: expected `,' or `]'");
-               }
-
-               protected override void Error_NegativeArrayIndex (Location loc)
+               protected override void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
                {
-                       Report.Error (248, loc, "Cannot create an array with a negative size");
+                       ec.Report.Error (248, loc, "Cannot create an array with a negative size");
                }
 
-               bool CheckIndices (ResolveContext ec, ArrayList probe, int idx, bool specified_dims, int child_bounds)
+               bool CheckIndices (ResolveContext ec, ArrayInitializer probe, int idx, bool specified_dims, int child_bounds)
                {
                        if (specified_dims) { 
-                               Expression a = (Expression) arguments [idx];
+                               Expression a = arguments [idx];
                                a = a.Resolve (ec);
                                if (a == null)
                                        return false;
@@ -5750,14 +5897,14 @@ namespace Mono.CSharp {
                                }
 
                                if (c == null) {
-                                       Report.Error (150, a.Location, "A constant value is expected");
+                                       ec.Report.Error (150, a.Location, "A constant value is expected");
                                        return false;
                                }
 
                                int value = (int) c.GetValue ();
                                
                                if (value != probe.Count) {
-                                       Report.Error (847, loc, "An array initializer of length `{0}' was expected", value);
+                                       ec.Report.Error (847, loc, "An array initializer of length `{0}' was expected", value);
                                        return false;
                                }
                                
@@ -5766,11 +5913,11 @@ namespace Mono.CSharp {
 
                        only_constant_initializers = true;
                        for (int i = 0; i < probe.Count; ++i) {
-                               object o = probe [i];
-                               if (o is ArrayList) {
-                                       ArrayList sub_probe = o as ArrayList;
+                               var o = probe [i];
+                               if (o is ArrayInitializer) {
+                                       var sub_probe = o as ArrayInitializer;
                                        if (idx + 1 >= dimensions){
-                                               Error (623, "Array initializers can only be used in a variable or field initializer. Try using a new expression instead");
+                                               ec.Report.Error (623, loc, "Array initializers can only be used in a variable or field initializer. Try using a new expression instead");
                                                return false;
                                        }
                                        
@@ -5778,9 +5925,9 @@ namespace Mono.CSharp {
                                        if (!ret)
                                                return false;
                                } else if (child_bounds > 1) {
-                                       Report.Error (846, ((Expression) o).Location, "A nested array initializer was expected");
+                                       ec.Report.Error (846, o.Location, "A nested array initializer was expected");
                                } else {
-                                       Expression element = ResolveArrayElement (ec, (Expression) o);
+                                       Expression element = ResolveArrayElement (ec, o);
                                        if (element == null)
                                                continue;
 
@@ -5811,20 +5958,14 @@ namespace Mono.CSharp {
                        if (array_data == null) {
                                args = new Arguments (arguments.Count + 1);
                                args.Add (new Argument (new TypeOf (new TypeExpression (array_element_type, loc), loc)));
-                               foreach (Expression a in arguments) {
-                                       if (arguments.Count == 1) {
-                                               Constant c = a as Constant;
-                                               if (c.IsDefaultValue)
-                                                       return CreateExpressionFactoryCall ("NewArrayInit", args);
-                                       }
+                               foreach (Expression a in arguments)
                                        args.Add (new Argument (a.CreateExpressionTree (ec)));
-                               }
 
-                               return CreateExpressionFactoryCall ("NewArrayBounds", args);
+                               return CreateExpressionFactoryCall (ec, "NewArrayBounds", args);
                        }
 
                        if (dimensions > 1) {
-                               Report.Error (838, loc, "An expression tree cannot contain a multidimensional array initializer");
+                               ec.Report.Error (838, loc, "An expression tree cannot contain a multidimensional array initializer");
                                return null;
                        }
 
@@ -5832,28 +5973,28 @@ namespace Mono.CSharp {
                        args.Add (new Argument (new TypeOf (new TypeExpression (array_element_type, loc), loc)));
                        if (array_data != null) {
                                for (int i = 0; i < array_data.Count; ++i) {
-                                       Expression e = (Expression) array_data [i];
+                                       Expression e = array_data [i];
                                        if (e == null)
-                                               e = Convert.ImplicitConversion (ec, (Expression) initializers [i], array_element_type, loc);
+                                               e = Convert.ImplicitConversion (ec, initializers [i], array_element_type, loc);
 
                                        args.Add (new Argument (e.CreateExpressionTree (ec)));
                                }
                        }
 
-                       return CreateExpressionFactoryCall ("NewArrayInit", args);
+                       return CreateExpressionFactoryCall (ec, "NewArrayInit", args);
                }               
                
                public void UpdateIndices ()
                {
                        int i = 0;
-                       for (ArrayList probe = initializers; probe != null;) {
-                               if (probe.Count > 0 && probe [0] is ArrayList) {
+                       for (var probe = initializers; probe != null;) {
+                               if (probe.Count > 0 && probe [0] is ArrayInitializer) {
                                        Expression e = new IntConstant (probe.Count, Location.Null);
                                        arguments.Add (e);
 
-                                       bounds [i++] =  probe.Count;
-                                       
-                                       probe = (ArrayList) probe [0];
+                                       bounds [i++] = probe.Count;
+
+                                       probe = (ArrayInitializer) probe[0];
                                        
                                } else {
                                        Expression e = new IntConstant (probe.Count, Location.Null);
@@ -5863,12 +6004,8 @@ namespace Mono.CSharp {
                                        return;
                                }
                        }
-
                }
 
-               Expression first_emit;
-               LocalTemporary first_emit_temp;
-
                protected virtual Expression ResolveArrayElement (ResolveContext ec, Expression element)
                {
                        element = element.Resolve (ec);
@@ -5896,13 +6033,13 @@ namespace Mono.CSharp {
                        // We use this to store all the date values in the order in which we
                        // will need to store them in the byte blob later
                        //
-                       array_data = new ArrayList ();
-                       bounds = new System.Collections.Specialized.HybridDictionary ();
+                       array_data = new List<Expression> ();
+                       bounds = new Dictionary<int, int> ();
                        
                        if (arguments != null)
                                return CheckIndices (ec, initializers, 0, true, dimensions);
 
-                       arguments = new ArrayList ();
+                       arguments = new List<Expression> ();
 
                        if (!CheckIndices (ec, initializers, 0, false, dimensions))
                                return false;
@@ -5917,13 +6054,8 @@ namespace Mono.CSharp {
                //
                bool ResolveArrayType (ResolveContext ec)
                {
-                       if (requested_base_type == null) {
-                               Report.Error (622, loc, "Can only use array initializer expressions to assign to array types. Try using a new expression instead");
-                               return false;
-                       }
-
                        if (requested_base_type is VarExpr) {
-                               Report.Error (820, loc, "An implicitly typed local variable declarator cannot use an array initializer");
+                               ec.Report.Error (820, loc, "An implicitly typed local variable declarator cannot use an array initializer");
                                return false;
                        }
                        
@@ -5950,13 +6082,18 @@ namespace Mono.CSharp {
                                return false;
 
                        type = array_type_expr.Type;
+                       if (!type.IsArray) {
+                               ec.Report.Error (622, loc, "Can only use array initializer expressions to assign to array types. Try using a new expression instead");
+                               return false;
+                       }
+
                        array_element_type = TypeManager.GetElementType (type);
                        dimensions = type.GetArrayRank ();
 
                        return true;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (type != null)
                                return this;
@@ -5972,7 +6109,7 @@ namespace Mono.CSharp {
                                return null;
 
                        for (int i = 0; i < arguments.Count; ++i) {
-                               Expression e = ((Expression) arguments[i]).Resolve (ec);
+                               Expression e = arguments[i].Resolve (ec);
                                if (e == null)
                                        continue;
 
@@ -5983,7 +6120,7 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               MethodInfo GetArrayMethod (int arguments)
+               MethodInfo GetArrayMethod (EmitContext ec, int arguments)
                {
                        ModuleBuilder mb = RootContext.ToplevelTypes.Builder;
 
@@ -5995,7 +6132,7 @@ namespace Mono.CSharp {
                                                        arg_types);
 
                        if (mi == null) {
-                               Report.Error (-6, "New invocation: Can not find a constructor for " +
+                               ec.Report.Error (-6, "New invocation: Can not find a constructor for " +
                                                  "this argument list");
                                return null;
                        }
@@ -6010,12 +6147,13 @@ namespace Mono.CSharp {
                        byte [] element;
                        int count = array_data.Count;
 
-                       if (TypeManager.IsEnumType (array_element_type))
-                               array_element_type = TypeManager.GetEnumUnderlyingType (array_element_type);
-                       
-                       factor = GetTypeSize (array_element_type);
+                       Type element_type = array_element_type;
+                       if (TypeManager.IsEnumType (element_type))
+                               element_type = TypeManager.GetEnumUnderlyingType (element_type);
+
+                       factor = GetTypeSize (element_type);
                        if (factor == 0)
-                               throw new Exception ("unrecognized type in MakeByteBlob: " + array_element_type);
+                               throw new Exception ("unrecognized type in MakeByteBlob: " + element_type);
 
                        data = new byte [(count * factor + 3) & ~3];
                        int idx = 0;
@@ -6033,7 +6171,7 @@ namespace Mono.CSharp {
                                        continue;
                                }
                                
-                               if (array_element_type == TypeManager.int64_type){
+                               if (element_type == TypeManager.int64_type){
                                        if (!(v is Expression)){
                                                long val = (long) v;
                                                
@@ -6042,7 +6180,7 @@ namespace Mono.CSharp {
                                                        val = (val >> 8);
                                                }
                                        }
-                               } else if (array_element_type == TypeManager.uint64_type){
+                               } else if (element_type == TypeManager.uint64_type){
                                        if (!(v is Expression)){
                                                ulong val = (ulong) v;
 
@@ -6051,7 +6189,7 @@ namespace Mono.CSharp {
                                                        val = (val >> 8);
                                                }
                                        }
-                               } else if (array_element_type == TypeManager.float_type) {
+                               } else if (element_type == TypeManager.float_type) {
                                        if (!(v is Expression)){
                                                element = BitConverter.GetBytes ((float) v);
                                                        
@@ -6060,7 +6198,7 @@ namespace Mono.CSharp {
                                                if (!BitConverter.IsLittleEndian)
                                                        System.Array.Reverse (data, idx, 4);
                                        }
-                               } else if (array_element_type == TypeManager.double_type) {
+                               } else if (element_type == TypeManager.double_type) {
                                        if (!(v is Expression)){
                                                element = BitConverter.GetBytes ((double) v);
 
@@ -6071,28 +6209,28 @@ namespace Mono.CSharp {
                                                if (!BitConverter.IsLittleEndian)
                                                        System.Array.Reverse (data, idx, 8);
                                        }
-                               } else if (array_element_type == TypeManager.char_type){
+                               } else if (element_type == TypeManager.char_type){
                                        if (!(v is Expression)){
                                                int val = (int) ((char) v);
                                                
                                                data [idx] = (byte) (val & 0xff);
                                                data [idx+1] = (byte) (val >> 8);
                                        }
-                               } else if (array_element_type == TypeManager.short_type){
+                               } else if (element_type == TypeManager.short_type){
                                        if (!(v is Expression)){
                                                int val = (int) ((short) v);
                                        
                                                data [idx] = (byte) (val & 0xff);
                                                data [idx+1] = (byte) (val >> 8);
                                        }
-                               } else if (array_element_type == TypeManager.ushort_type){
+                               } else if (element_type == TypeManager.ushort_type){
                                        if (!(v is Expression)){
                                                int val = (int) ((ushort) v);
                                        
                                                data [idx] = (byte) (val & 0xff);
                                                data [idx+1] = (byte) (val >> 8);
                                        }
-                               } else if (array_element_type == TypeManager.int32_type) {
+                               } else if (element_type == TypeManager.int32_type) {
                                        if (!(v is Expression)){
                                                int val = (int) v;
                                        
@@ -6101,7 +6239,7 @@ namespace Mono.CSharp {
                                                data [idx+2] = (byte) ((val >> 16) & 0xff);
                                                data [idx+3] = (byte) (val >> 24);
                                        }
-                               } else if (array_element_type == TypeManager.uint32_type) {
+                               } else if (element_type == TypeManager.uint32_type) {
                                        if (!(v is Expression)){
                                                uint val = (uint) v;
                                        
@@ -6110,22 +6248,22 @@ namespace Mono.CSharp {
                                                data [idx+2] = (byte) ((val >> 16) & 0xff);
                                                data [idx+3] = (byte) (val >> 24);
                                        }
-                               } else if (array_element_type == TypeManager.sbyte_type) {
+                               } else if (element_type == TypeManager.sbyte_type) {
                                        if (!(v is Expression)){
                                                sbyte val = (sbyte) v;
                                                data [idx] = (byte) val;
                                        }
-                               } else if (array_element_type == TypeManager.byte_type) {
+                               } else if (element_type == TypeManager.byte_type) {
                                        if (!(v is Expression)){
                                                byte val = (byte) v;
                                                data [idx] = (byte) val;
                                        }
-                               } else if (array_element_type == TypeManager.bool_type) {
+                               } else if (element_type == TypeManager.bool_type) {
                                        if (!(v is Expression)){
                                                bool val = (bool) v;
                                                data [idx] = (byte) (val ? 1 : 0);
                                        }
-                               } else if (array_element_type == TypeManager.decimal_type){
+                               } else if (element_type == TypeManager.decimal_type){
                                        if (!(v is Expression)){
                                                int [] bits = Decimal.GetBits ((decimal) v);
                                                int p = idx;
@@ -6144,15 +6282,31 @@ namespace Mono.CSharp {
                                                        data [p++] = (byte) (nbits [j] >> 24);
                                                }
                                        }
-                               } else
-                                       throw new Exception ("Unrecognized type in MakeByteBlob: " + array_element_type);
+                               } else {
+                                       throw new Exception ("Unrecognized type in MakeByteBlob: " + element_type);
+                               }
 
-                                idx += factor;
+                               idx += factor;
                        }
 
                        return data;
                }
 
+#if NET_4_0
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
+               {
+                       var initializers = new SLE.Expression [array_data.Count];
+                       for (var i = 0; i < initializers.Length; i++) {
+                               if (array_data [i] == null)
+                                       initializers [i] = SLE.Expression.Default (array_element_type);
+                               else
+                                       initializers [i] = array_data [i].MakeExpression (ctx);
+                       }
+
+                       return SLE.Expression.NewArrayInit (array_element_type, initializers);
+               }
+#endif
+
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
                        array_element_type = storey.MutateType (array_element_type);
@@ -6199,8 +6353,7 @@ namespace Mono.CSharp {
 
                        ig.Emit (OpCodes.Dup);
                        ig.Emit (OpCodes.Ldtoken, fb);
-                       ig.Emit (OpCodes.Call,
-                                TypeManager.void_initializearray_array_fieldhandle);
+                       ig.Emit (OpCodes.Call, (MethodInfo) TypeManager.void_initializearray_array_fieldhandle.MetaInfo);
                }
 
                //
@@ -6232,7 +6385,7 @@ namespace Mono.CSharp {
 
                        for (int i = 0; i < array_data.Count; i++){
 
-                               Expression e = (Expression)array_data [i];
+                               Expression e = array_data [i];
 
                                // Constant can be initialized via StaticInitializer
                                if (e != null && !(!emitConstants && e is Constant)) {
@@ -6275,7 +6428,7 @@ namespace Mono.CSharp {
                                //
                                for (int j = dims - 1; j >= 0; j--){
                                        current_pos [j]++;
-                                       if (current_pos [j] < (int) bounds [j])
+                                       if (current_pos [j] < bounds [j])
                                                break;
                                        current_pos [j] = 0;
                                }
@@ -6295,19 +6448,19 @@ namespace Mono.CSharp {
                                e.Emit (ec);
 
                        if (arguments.Count == 1)
-                               ig.Emit (OpCodes.Newarr, array_element_type);
+                               ig.Emit (OpCodes.Newarr, TypeManager.TypeToReflectionType (array_element_type));
                        else {
-                               ig.Emit (OpCodes.Newobj, GetArrayMethod (arguments.Count));
+                               ig.Emit (OpCodes.Newobj, GetArrayMethod (ec, arguments.Count));
                        }
                        
                        if (initializers == null)
                                return;
 
-                       // Emit static initializer for arrays which have contain more than 4 items and
+                       // Emit static initializer for arrays which have contain more than 2 items and
                        // the static initializer will initialize at least 25% of array values.
                        // NOTE: const_initializers_count does not contain default constant values.
-                       if (const_initializers_count >= 4 && const_initializers_count * 4 > (array_data.Count) &&
-                               TypeManager.IsPrimitiveType (array_element_type)) {
+                       if (const_initializers_count > 2 && const_initializers_count * 4 > (array_data.Count) &&
+                               (TypeManager.IsPrimitiveType (array_element_type) || TypeManager.IsEnumType (array_element_type))) {
                                EmitStaticInitializers (ec);
 
                                if (!only_constant_initializers)
@@ -6323,17 +6476,19 @@ namespace Mono.CSharp {
                public override bool GetAttributableValue (ResolveContext ec, Type value_type, out object value)
                {
                        if (arguments.Count != 1) {
-                               // Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
+                               // ec.Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
                                return base.GetAttributableValue (ec, null, out value);
                        }
 
                        if (array_data == null) {
-                               Constant c = (Constant) arguments [0];
-                               if (c.IsDefaultValue) {
+                               Expression arg = arguments [0];
+                               object arg_value;
+                               if (arg.GetAttributableValue (ec, arg.Type, out arg_value) && arg_value is int && (int)arg_value == 0) {
                                        value = Array.CreateInstance (array_element_type, 0);
                                        return true;
                                }
-                               // Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
+
+                               // ec.Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
                                return base.GetAttributableValue (ec, null, out value);
                        }
                        
@@ -6341,7 +6496,7 @@ namespace Mono.CSharp {
                        object element_value;
                        for (int i = 0; i < ret.Length; ++i)
                        {
-                               Expression e = (Expression)array_data [i];
+                               Expression e = array_data [i];
 
                                // Is null when an initializer is optimized (value == predefined value)
                                if (e == null) 
@@ -6365,38 +6520,24 @@ namespace Mono.CSharp {
                                target.requested_base_type = (FullNamedExpression)requested_base_type.Clone (clonectx);
 
                        if (arguments != null){
-                               target.arguments = new ArrayList (arguments.Count);
+                               target.arguments = new List<Expression> (arguments.Count);
                                foreach (Expression e in arguments)
                                        target.arguments.Add (e.Clone (clonectx));
                        }
 
-                       if (initializers != null){
-                               target.initializers = new ArrayList (initializers.Count);
-                               foreach (object initializer in initializers)
-                                       if (initializer is ArrayList) {
-                                               ArrayList this_al = (ArrayList)initializer;
-                                               ArrayList al = new ArrayList (this_al.Count);
-                                               target.initializers.Add (al);
-                                               foreach (Expression e in this_al)
-                                                       al.Add (e.Clone (clonectx));
-                                       } else {
-                                               target.initializers.Add (((Expression)initializer).Clone (clonectx));
-                                       }
-                       }
+                       if (initializers != null)
+                               target.initializers = (ArrayInitializer) initializers.Clone (clonectx);
                }
        }
        
        //
        // Represents an implicitly typed array epxression
        //
-       public class ImplicitlyTypedArrayCreation : ArrayCreation
+       class ImplicitlyTypedArrayCreation : ArrayCreation
        {
-               public ImplicitlyTypedArrayCreation (string rank, ArrayList initializers, Location loc)
+               public ImplicitlyTypedArrayCreation (string rank, ArrayInitializer initializers, Location loc)
                        : base (null, rank, initializers, loc)
-               {
-                       if (RootContext.Version <= LanguageVersion.ISO_2)
-                               Report.FeatureIsNotAvailable (loc, "implicitly typed arrays");
-                               
+               {                       
                        if (rank.Length > 2) {
                                while (rank [++dimensions] == ',');
                        } else {
@@ -6404,7 +6545,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (type != null)
                                return this;
@@ -6414,8 +6555,9 @@ namespace Mono.CSharp {
 
                        if (array_element_type == null || array_element_type == TypeManager.null_type ||
                                array_element_type == TypeManager.void_type || array_element_type == InternalType.AnonymousMethod ||
+                               array_element_type == InternalType.MethodGroup ||
                                arguments.Count != dimensions) {
-                               Error_NoBestType ();
+                               Error_NoBestType (ec);
                                return null;
                        }
 
@@ -6431,9 +6573,9 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               void Error_NoBestType ()
+               void Error_NoBestType (ResolveContext ec)
                {
-                       Report.Error (826, loc,
+                       ec.Report.Error (826, loc,
                                "The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly");
                }
 
@@ -6471,7 +6613,7 @@ namespace Mono.CSharp {
                                return element;
                        }
 
-                       Error_NoBestType ();
+                       Error_NoBestType (ec);
                        return null;
                }
        }       
@@ -6491,7 +6633,7 @@ namespace Mono.CSharp {
                        this.type = type;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        eclass = ExprClass.Variable;
                        if (type == null)
@@ -6501,7 +6643,7 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               public override HoistedVariable GetHoistedVariable (EmitContext ec)
+               public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
                {
                        return null;
                }
@@ -6556,16 +6698,12 @@ namespace Mono.CSharp {
                        get { return false; }
                }
 
-               public override HoistedVariable GetHoistedVariable (EmitContext ec)
+               public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
                {
-                       // Is null when probing IsHoisted
-                       if (ec == null)
-                               return null;
-
-                       if (ec.CurrentAnonymousMethod == null)
+                       if (ae == null)
                                return null;
 
-                       AnonymousMethodStorey storey = ec.CurrentAnonymousMethod.Storey;
+                       AnonymousMethodStorey storey = ae.Storey;
                        while (storey != null) {
                                AnonymousMethodStorey temp = storey.Parent as AnonymousMethodStorey;
                                if (temp == null)
@@ -6587,7 +6725,7 @@ namespace Mono.CSharp {
 
                public static bool IsThisAvailable (ResolveContext ec)
                {
-                       if (ec.IsStatic || ec.HasAny (EmitContext.Options.FieldInitializerScope | EmitContext.Options.BaseInitializer | EmitContext.Options.ConstantScope))
+                       if (ec.IsStatic || ec.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.BaseInitializer | ResolveContext.Options.ConstantScope))
                                return false;
 
                        if (ec.CurrentAnonymousMethod == null)
@@ -6601,21 +6739,18 @@ namespace Mono.CSharp {
 
                public bool ResolveBase (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return true;
-
                        eclass = ExprClass.Variable;
                        type = ec.CurrentType;
 
                        if (!IsThisAvailable (ec)) {
-                               if (ec.IsStatic && !ec.HasSet (EmitContext.Options.ConstantScope)) {
-                                       Error (26, "Keyword `this' is not valid in a static property, static method, or static field initializer");
+                               if (ec.IsStatic && !ec.HasSet (ResolveContext.Options.ConstantScope)) {
+                                       ec.Report.Error (26, loc, "Keyword `this' is not valid in a static property, static method, or static field initializer");
                                } else if (ec.CurrentAnonymousMethod != null) {
-                                       Report.Error (1673, loc,
+                                       ec.Report.Error (1673, loc,
                                                "Anonymous methods inside structs cannot access instance members of `this'. " +
                                                "Consider copying `this' to a local variable outside the anonymous method and using the local instead");
                                } else {
-                                       Error (27, "Keyword `this' is not available in the current context");
+                                       ec.Report.Error (27, loc, "Keyword `this' is not available in the current context");
                                }
                        }
 
@@ -6641,8 +6776,8 @@ namespace Mono.CSharp {
                {
                        if ((variable_info != null) && !(TypeManager.IsStruct (type) && ec.OmitStructFlowAnalysis) &&
                            !variable_info.IsAssigned (ec)) {
-                               Error (188, "The `this' object cannot be used before all of its " +
-                                      "fields are assigned to");
+                               ec.Report.Error (188, loc,
+                                       "The `this' object cannot be used before all of its fields are assigned to");
                                variable_info.SetAssigned (ec);
                        }
                }
@@ -6654,10 +6789,10 @@ namespace Mono.CSharp {
                        
                        // Use typeless constant for ldarg.0 to save some
                        // space and avoid problems with anonymous stories
-                       return CreateExpressionFactoryCall ("Constant", args);
+                       return CreateExpressionFactoryCall (ec, "Constant", args);
                }
                
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        ResolveBase (ec);
                        return this;
@@ -6673,11 +6808,11 @@ namespace Mono.CSharp {
 
                        if (ec.CurrentType.IsClass){
                                if (right_side == EmptyExpression.UnaryAddress)
-                                       Report.Error (459, loc, "Cannot take the address of `this' because it is read-only");
-                               else if (right_side == EmptyExpression.OutAccess)
-                                       Report.Error (1605, loc, "Cannot pass `this' as a ref or out argument because it is read-only");
+                                       ec.Report.Error (459, loc, "Cannot take the address of `this' because it is read-only");
+                               else if (right_side == EmptyExpression.OutAccess.Instance)
+                                       ec.Report.Error (1605, loc, "Cannot pass `this' as a ref or out argument because it is read-only");
                                else
-                                       Report.Error (1604, loc, "Cannot assign to `this' because it is read-only");
+                                       ec.Report.Error (1604, loc, "Cannot assign to `this' because it is read-only");
                        }
 
                        return this;
@@ -6729,14 +6864,14 @@ namespace Mono.CSharp {
                        throw new NotSupportedException ("ET");
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        eclass = ExprClass.Variable;
                        type = TypeManager.runtime_argument_handle_type;
 
-                       if (ec.HasSet (EmitContext.Options.FieldInitializerScope) || !ec.CurrentBlock.Toplevel.Parameters.HasArglist) {
-                               Error (190, "The __arglist construct is valid only within " +
-                                      "a variable argument method");
+                       if (ec.HasSet (ResolveContext.Options.FieldInitializerScope) || !ec.CurrentBlock.Toplevel.Parameters.HasArglist) {
+                               ec.Report.Error (190, loc,
+                                       "The __arglist construct is valid only within a variable argument method");
                        }
 
                        return this;
@@ -6786,11 +6921,11 @@ namespace Mono.CSharp {
                
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       Report.Error (1952, loc, "An expression tree cannot contain a method with variable arguments");
+                       ec.Report.Error (1952, loc, "An expression tree cannot contain a method with variable arguments");
                        return null;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        eclass = ExprClass.Variable;
                        type = InternalType.Arglist;
@@ -6841,14 +6976,11 @@ namespace Mono.CSharp {
                        Arguments args = new Arguments (2);
                        args.Add (new Argument (this));
                        args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
-                       return CreateExpressionFactoryCall ("Constant", args);
+                       return CreateExpressionFactoryCall (ec, "Constant", args);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
                        if (texpr == null)
                                return null;
@@ -6856,11 +6988,11 @@ namespace Mono.CSharp {
                        typearg = texpr.Type;
 
                        if (typearg == TypeManager.void_type) {
-                               Report.Error (673, loc, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
-                       } else if (typearg.IsPointer && !ec.InUnsafe){
-                               UnsafeError (loc);
+                               ec.Report.Error (673, loc, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
+                       } else if (typearg.IsPointer && !ec.IsUnsafe){
+                               UnsafeError (ec, loc);
                        } else if (texpr is DynamicTypeExpr) {
-                               Report.Error (1962, QueriedType.Location,
+                               ec.Report.Error (1962, QueriedType.Location,
                                        "The typeof operator cannot be used on the dynamic type");
                        }
 
@@ -6885,15 +7017,15 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        ec.ig.Emit (OpCodes.Ldtoken, TypeManager.TypeToReflectionType (typearg));
-                       ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
+                       ec.ig.Emit (OpCodes.Call, (MethodInfo) TypeManager.system_type_get_type_from_handle.MetaInfo);
                }
 
                public override bool GetAttributableValue (ResolveContext ec, Type value_type, out object value)
                {
                        if (TypeManager.ContainsGenericParameters (typearg) &&
                                !TypeManager.IsGenericTypeDefinition (typearg)) {
-                               Report.SymbolRelatedToPreviousError (typearg);
-                               Report.Error (416, loc, "`{0}': an attribute argument cannot use type parameters",
+                               ec.Report.SymbolRelatedToPreviousError (typearg);
+                               ec.Report.Error (416, loc, "`{0}': an attribute argument cannot use type parameters",
                                             TypeManager.CSharpName (typearg));
                                value = null;
                                return false;
@@ -6909,7 +7041,8 @@ namespace Mono.CSharp {
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
-                       typearg = storey.MutateType (typearg);
+                       if (!TypeManager.IsGenericTypeDefinition (typearg))
+                               typearg = storey.MutateType (typearg);
                }
 
                public Type TypeArgument {
@@ -6935,7 +7068,7 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        type = TypeManager.type_type;
                        typearg = TypeManager.void_type;
@@ -6944,23 +7077,23 @@ namespace Mono.CSharp {
                }
        }
 
-       class TypeOfMethod : TypeOfMember
+       class TypeOfMethod : TypeOfMember<MethodSpec>
        {
-               public TypeOfMethod (MethodBase method, Location loc)
+               public TypeOfMethod (MethodSpec method, Location loc)
                        : base (method, loc)
                {
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (member is MethodInfo) {
-                               type = TypeManager.methodinfo_type;
+                       if (member.IsConstructor) {
+                               type = TypeManager.ctorinfo_type;
                                if (type == null)
-                                       type = TypeManager.methodinfo_type = TypeManager.CoreLookupType ("System.Reflection", "MethodInfo", Kind.Class, true);
+                                       type = TypeManager.ctorinfo_type = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", "ConstructorInfo", MemberKind.Class, true);
                        } else {
-                               type = TypeManager.ctorinfo_type;
+                               type = TypeManager.methodinfo_type;
                                if (type == null)
-                                       type = TypeManager.ctorinfo_type = TypeManager.CoreLookupType ("System.Reflection", "ConstructorInfo", Kind.Class, true);
+                                       type = TypeManager.methodinfo_type = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", "MethodInfo", MemberKind.Class, true);
                        }
 
                        return base.DoResolve (ec);
@@ -6968,10 +7101,10 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       if (member is ConstructorInfo)
-                               ec.ig.Emit (OpCodes.Ldtoken, (ConstructorInfo) member);
+                       if (member.IsConstructor)
+                               ec.ig.Emit (OpCodes.Ldtoken, (ConstructorInfo) member.MetaInfo);
                        else
-                               ec.ig.Emit (OpCodes.Ldtoken, (MethodInfo) member);
+                               ec.ig.Emit (OpCodes.Ldtoken, (MethodInfo) member.MetaInfo);
 
                        base.Emit (ec);
                        ec.ig.Emit (OpCodes.Castclass, type);
@@ -6985,7 +7118,7 @@ namespace Mono.CSharp {
                        get { return "RuntimeMethodHandle"; }
                }
 
-               protected override MethodInfo TypeFromHandle {
+               protected override MethodSpec TypeFromHandle {
                        get {
                                return TypeManager.methodbase_get_type_from_handle;
                        }
@@ -6994,7 +7127,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               protected override MethodInfo TypeFromHandleGeneric {
+               protected override MethodSpec TypeFromHandleGeneric {
                        get {
                                return TypeManager.methodbase_get_type_from_handle_generic;
                        }
@@ -7008,11 +7141,11 @@ namespace Mono.CSharp {
                }
        }
 
-       abstract class TypeOfMember : Expression
+       abstract class TypeOfMember<T> : Expression where T : MemberSpec
        {
-               protected readonly MemberInfo member;
+               protected readonly T member;
 
-               protected TypeOfMember (MemberInfo member, Location loc)
+               protected TypeOfMember (T member, Location loc)
                {
                        this.member = member;
                        this.loc = loc;
@@ -7023,17 +7156,17 @@ namespace Mono.CSharp {
                        Arguments args = new Arguments (2);
                        args.Add (new Argument (this));
                        args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
-                       return CreateExpressionFactoryCall ("Constant", args);
+                       return CreateExpressionFactoryCall (ec, "Constant", args);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        bool is_generic = TypeManager.IsGenericType (member.DeclaringType);
-                       MethodInfo mi = is_generic ? TypeFromHandleGeneric : TypeFromHandle;
+                       var mi = is_generic ? TypeFromHandleGeneric : TypeFromHandle;
 
                        if (mi == null) {
-                               Type t = TypeManager.CoreLookupType ("System.Reflection", TypeName, Kind.Class, true);
-                               Type handle_type = TypeManager.CoreLookupType ("System", RuntimeHandleName, Kind.Class, true);
+                               Type t = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", TypeName, MemberKind.Class, true);
+                               Type handle_type = TypeManager.CoreLookupType (ec.Compiler, "System", RuntimeHandleName, MemberKind.Class, true);
 
                                if (t == null || handle_type == null)
                                        return null;
@@ -7056,7 +7189,7 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        bool is_generic = TypeManager.IsGenericType (member.DeclaringType);
-                       MethodInfo mi;
+                       MethodSpec mi;
                        if (is_generic) {
                                mi = TypeFromHandleGeneric;
                                ec.ig.Emit (OpCodes.Ldtoken, member.DeclaringType);
@@ -7064,27 +7197,27 @@ namespace Mono.CSharp {
                                mi = TypeFromHandle;
                        }
 
-                       ec.ig.Emit (OpCodes.Call, mi);
+                       ec.ig.Emit (OpCodes.Call, (MethodInfo) mi.MetaInfo);
                }
 
                protected abstract string GetMethodName { get; }
                protected abstract string RuntimeHandleName { get; }
-               protected abstract MethodInfo TypeFromHandle { get; set; }
-               protected abstract MethodInfo TypeFromHandleGeneric { get; set; }
+               protected abstract MethodSpec TypeFromHandle { get; set; }
+               protected abstract MethodSpec TypeFromHandleGeneric { get; set; }
                protected abstract string TypeName { get; }
        }
 
-       class TypeOfField : TypeOfMember
+       class TypeOfField : TypeOfMember<FieldSpec>
        {
-               public TypeOfField (FieldInfo field, Location loc)
+               public TypeOfField (FieldSpec field, Location loc)
                        : base (field, loc)
                {
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (TypeManager.fieldinfo_type == null)
-                               TypeManager.fieldinfo_type = TypeManager.CoreLookupType ("System.Reflection", TypeName, Kind.Class, true);
+                               TypeManager.fieldinfo_type = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", TypeName, MemberKind.Class, true);
 
                        type = TypeManager.fieldinfo_type;
                        return base.DoResolve (ec);
@@ -7092,7 +7225,7 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       ec.ig.Emit (OpCodes.Ldtoken, (FieldInfo) member);
+                       ec.ig.Emit (OpCodes.Ldtoken, member.MetaInfo);
                        base.Emit (ec);
                }
 
@@ -7104,7 +7237,7 @@ namespace Mono.CSharp {
                        get { return "RuntimeFieldHandle"; }
                }
 
-               protected override MethodInfo TypeFromHandle {
+               protected override MethodSpec TypeFromHandle {
                        get {
                                return TypeManager.fieldinfo_get_field_from_handle;
                        }
@@ -7113,7 +7246,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               protected override MethodInfo TypeFromHandleGeneric {
+               protected override MethodSpec TypeFromHandleGeneric {
                        get {
                                return TypeManager.fieldinfo_get_field_from_handle_generic;
                        }
@@ -7142,11 +7275,11 @@ namespace Mono.CSharp {
 
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       Error_PointerInsideExpressionTree ();
+                       Error_PointerInsideExpressionTree (ec);
                        return null;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
                        if (texpr == null)
@@ -7158,15 +7291,15 @@ namespace Mono.CSharp {
 
                        int size_of = GetTypeSize (type_queried);
                        if (size_of > 0) {
-                               return new IntConstant (size_of, loc);
+                               return new IntConstant (size_of, loc).Resolve (ec);
                        }
 
-                       if (!TypeManager.VerifyUnManaged (type_queried, loc)){
+                       if (!TypeManager.VerifyUnmanaged (ec.Compiler, type_queried, loc)){
                                return null;
                        }
 
-                       if (!ec.InUnsafe) {
-                               Report.Error (233, loc,
+                       if (!ec.IsUnsafe) {
+                               ec.Report.Error (233, loc,
                                        "`{0}' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)",
                                        TypeManager.CSharpName (type_queried));
                        }
@@ -7178,12 +7311,7 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       int size = GetTypeSize (type_queried);
-
-                       if (size == 0)
-                               ec.ig.Emit (OpCodes.Sizeof, type_queried);
-                       else
-                               IntConstant.EmitInt (ec.ig, size);
+                       ec.ig.Emit (OpCodes.Sizeof, type_queried);
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -7218,11 +7346,11 @@ namespace Mono.CSharp {
                                return base.ResolveAsTypeStep (ec, silent);
                        }
 
-                       int errors = Report.Errors;
+                       int errors = ec.Compiler.Report.Errors;
                        expr = ec.LookupNamespaceAlias (alias);
                        if (expr == null) {
-                               if (errors == Report.Errors)
-                                       Report.Error (432, loc, "Alias `{0}' not found", alias);
+                               if (errors == ec.Compiler.Report.Errors)
+                                       ec.Compiler.Report.Error (432, loc, "Alias `{0}' not found", alias);
                                return null;
                        }
 
@@ -7232,7 +7360,7 @@ namespace Mono.CSharp {
 
                        if (expr.eclass == ExprClass.Type) {
                                if (!silent) {
-                                       Report.Error (431, loc,
+                                       ec.Compiler.Report.Error (431, loc,
                                                "Alias `{0}' cannot be used with '::' since it denotes a type. Consider replacing '::' with '.'", alias);
                                }
                                return null;
@@ -7241,14 +7369,14 @@ namespace Mono.CSharp {
                        return fne;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return ResolveAsTypeStep (ec, false);
                }
 
                protected override void Error_IdentifierNotFound (IMemberContext rc, FullNamedExpression expr_type, string identifier)
                {
-                       Report.Error (687, loc,
+                       rc.Compiler.Report.Error (687, loc,
                                "A namespace alias qualifier `{0}' did not resolve to a namespace or a type",
                                GetSignatureForError ());
                }
@@ -7307,9 +7435,10 @@ namespace Mono.CSharp {
                        //
 
                        SimpleName original = expr as SimpleName;
-                       Expression expr_resolved = expr.Resolve (ec,
-                               ResolveFlags.VariableOrValue | ResolveFlags.Type |
-                               ResolveFlags.Intermediate | ResolveFlags.DisableStructFlowAnalysis);
+                       Expression expr_resolved;
+                       using (ec.Set (ResolveContext.Options.OmitStructFlowAnalysis)) {
+                               expr_resolved = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type | ResolveFlags.Intermediate);
+                       }
 
                        if (expr_resolved == null)
                                return null;
@@ -7318,10 +7447,10 @@ namespace Mono.CSharp {
 
                        Namespace ns = expr_resolved as Namespace;
                        if (ns != null) {
-                               FullNamedExpression retval = ns.Lookup (LookupIdentifier, loc);
+                               FullNamedExpression retval = ns.Lookup (ec.Compiler, LookupIdentifier, loc);
 
                                if (retval == null)
-                                       ns.Error_NamespaceDoesNotExist (loc, LookupIdentifier);
+                                       ns.Error_NamespaceDoesNotExist (loc, LookupIdentifier, ec);
                                else if (targs != null)
                                        retval = new GenericTypeExpr (retval.Type, targs, loc).ResolveAsTypeStep (ec, false);
 
@@ -7330,23 +7459,24 @@ namespace Mono.CSharp {
 
                        Type expr_type = expr_resolved.Type;
                        if (TypeManager.IsDynamicType (expr_type)) {
-                               Arguments args = new Arguments (2);
+                               Arguments args = new Arguments (1);
                                args.Add (new Argument (expr_resolved.Resolve (ec)));
+                               expr = new DynamicMemberBinder (Name, args, loc);
                                if (right_side != null)
-                                       args.Add (new Argument (right_side));
+                                       return expr.DoResolveLValue (ec, right_side);
 
-                               return new DynamicMemberBinder (right_side != null, Name, args, loc).Resolve (ec);
+                               return expr.Resolve (ec);
                        }
 
                        if (expr_type.IsPointer || expr_type == TypeManager.void_type ||
                                expr_type == TypeManager.null_type || expr_type == InternalType.AnonymousMethod) {
-                               Unary.Error_OperatorCannotBeApplied (loc, ".", expr_type);
+                               Unary.Error_OperatorCannotBeApplied (ec, loc, ".", expr_type);
                                return null;
                        }
 
                        Constant c = expr_resolved as Constant;
                        if (c != null && c.GetValue () == null) {
-                               Report.Warning (1720, 1, loc, "Expression will always cause a `{0}'",
+                               ec.Report.Warning (1720, 1, loc, "Expression will always cause a `{0}'",
                                        "System.NullReferenceException");
                        }
 
@@ -7356,11 +7486,11 @@ namespace Mono.CSharp {
                        }
 
                        Expression member_lookup;
-                       member_lookup = MemberLookup (
+                       member_lookup = MemberLookup (ec.Compiler,
                                ec.CurrentType, expr_type, expr_type, Name, loc);
 
                        if (member_lookup == null && targs != null) {
-                               member_lookup = MemberLookup (
+                               member_lookup = MemberLookup (ec.Compiler,
                                        ec.CurrentType, expr_type, expr_type, LookupIdentifier, loc);
                        }
 
@@ -7378,15 +7508,15 @@ namespace Mono.CSharp {
                                                ex_method_lookup.ExtensionExpression = expr_resolved;
 
                                                if (targs != null) {
-                                                       ex_method_lookup.SetTypeArguments (targs);
+                                                       ex_method_lookup.SetTypeArguments (ec, targs);
                                                }
 
-                                               return ex_method_lookup.DoResolve (ec);
+                                               return ex_method_lookup.Resolve (ec);
                                        }
                                }
 
                                expr = expr_resolved;
-                               member_lookup = Error_MemberLookupFailed (
+                               member_lookup = Error_MemberLookupFailed (ec,
                                        ec.CurrentType, expr_type, expr_type, Name, null,
                                        AllMemberTypes, AllBindingFlags);
                                if (member_lookup == null)
@@ -7397,14 +7527,14 @@ namespace Mono.CSharp {
                        if (texpr != null) {
                                if (!(expr_resolved is TypeExpr) && 
                                    (original == null || !original.IdenticalNameAndTypeName (ec, expr_resolved, loc))) {
-                                       Report.Error (572, loc, "`{0}': cannot reference a type through an expression; try `{1}' instead",
+                                       ec.Report.Error (572, loc, "`{0}': cannot reference a type through an expression; try `{1}' instead",
                                                Name, member_lookup.GetSignatureForError ());
                                        return null;
                                }
 
                                if (!texpr.CheckAccessLevel (ec.MemberContext)) {
-                                       Report.SymbolRelatedToPreviousError (member_lookup.Type);
-                                       ErrorIsInaccesible (loc, TypeManager.CSharpName (member_lookup.Type));
+                                       ec.Report.SymbolRelatedToPreviousError (member_lookup.Type);
+                                       ErrorIsInaccesible (loc, TypeManager.CSharpName (member_lookup.Type), ec.Report);
                                        return null;
                                }
 
@@ -7417,8 +7547,16 @@ namespace Mono.CSharp {
                                        //
                                        // See gtest-172-lib.cs and gtest-172.cs for an example.
                                        //
-                                       ct = new GenericTypeExpr (
-                                               member_lookup.Type, ct.TypeArguments, loc);
+
+                                       TypeArguments nested_targs;
+                                       if (HasTypeArguments) {
+                                               nested_targs = ct.TypeArguments.Clone ();
+                                               nested_targs.Add (targs);
+                                       } else {
+                                               nested_targs = ct.TypeArguments;
+                                       }
+
+                                       ct = new GenericTypeExpr (member_lookup.Type, nested_targs, loc);
 
                                        return ct.ResolveAsTypeStep (ec, false);
                                }
@@ -7432,10 +7570,10 @@ namespace Mono.CSharp {
                                return null;
 
                        if (targs != null) {
-                               me.SetTypeArguments (targs);
+                               me.SetTypeArguments (ec, targs);
                        }
 
-                       if (original != null && !TypeManager.IsValueType (expr_type)) {
+                       if (original != null && (!TypeManager.IsValueType (expr_type) || me is PropertyExpr)) {
                                if (me.IsInstance) {
                                        LocalVariableReference var = expr_resolved as LocalVariableReference;
                                        if (var != null && !var.VerifyAssigned (ec))
@@ -7449,10 +7587,10 @@ namespace Mono.CSharp {
                        if (right_side != null)
                                return me.DoResolveLValue (ec, right_side);
                        else
-                               return me.DoResolve (ec);
+                               return me.Resolve (ec);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return DoResolve (ec, null);
                }
@@ -7478,10 +7616,10 @@ namespace Mono.CSharp {
 
                        Namespace ns = expr_resolved as Namespace;
                        if (ns != null) {
-                               FullNamedExpression retval = ns.Lookup (LookupIdentifier, loc);
+                               FullNamedExpression retval = ns.Lookup (rc.Compiler, LookupIdentifier, loc);
 
                                if (retval == null && !silent)
-                                       ns.Error_NamespaceDoesNotExist (loc, LookupIdentifier);
+                                       ns.Error_NamespaceDoesNotExist (loc, LookupIdentifier, rc);
                                else if (targs != null)
                                        retval = new GenericTypeExpr (retval.Type, targs, loc).ResolveAsTypeStep (rc, silent);
 
@@ -7494,12 +7632,12 @@ namespace Mono.CSharp {
 
                        Type expr_type = tnew_expr.Type;
                        if (TypeManager.IsGenericParameter (expr_type)) {
-                               Report.Error (704, loc, "A nested type cannot be specified through a type parameter `{0}'",
+                               rc.Compiler.Report.Error (704, loc, "A nested type cannot be specified through a type parameter `{0}'",
                                        tnew_expr.GetSignatureForError ());
                                return null;
                        }
 
-                       Expression member_lookup = MemberLookup (
+                       Expression member_lookup = MemberLookup (rc.Compiler,
                                rc.CurrentType, expr_type, expr_type, LookupIdentifier,
                                MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
                        if (member_lookup == null) {
@@ -7541,7 +7679,7 @@ namespace Mono.CSharp {
 
                protected virtual void Error_IdentifierNotFound (IMemberContext rc, FullNamedExpression expr_type, string identifier)
                {
-                       Expression member_lookup = MemberLookup (
+                       Expression member_lookup = MemberLookup (rc.Compiler,
                                rc.CurrentType, expr_type.Type, expr_type.Type, SimpleName.RemoveGenericArity (identifier),
                                MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
 
@@ -7550,35 +7688,35 @@ namespace Mono.CSharp {
                                if (expr_type == null)
                                        return;
 
-                               Namespace.Error_TypeArgumentsCannotBeUsed (expr_type, loc);
+                               expr_type.Error_TypeArgumentsCannotBeUsed (rc.Compiler.Report, loc);
                                return;
                        }
 
-                       member_lookup = MemberLookup (
+                       member_lookup = MemberLookup (rc.Compiler,
                                rc.CurrentType, expr_type.Type, expr_type.Type, identifier,
                                        MemberTypes.All, BindingFlags.Public | BindingFlags.NonPublic, loc);
 
                        if (member_lookup == null) {
-                               Report.Error (426, loc, "The nested type `{0}' does not exist in the type `{1}'",
+                               rc.Compiler.Report.Error (426, loc, "The nested type `{0}' does not exist in the type `{1}'",
                                                  Name, expr_type.GetSignatureForError ());
                        } else {
                                // TODO: Report.SymbolRelatedToPreviousError
-                               member_lookup.Error_UnexpectedKind (null, "type", loc);
+                               member_lookup.Error_UnexpectedKind (rc.Compiler.Report, null, "type", loc);
                        }
                }
 
-               protected override void Error_TypeDoesNotContainDefinition (Type type, string name)
+               protected override void Error_TypeDoesNotContainDefinition (ResolveContext ec, Type type, string name)
                {
-                       if (RootContext.Version > LanguageVersion.ISO_2 &&
+                       if (RootContext.Version > LanguageVersion.ISO_2 && !ec.Compiler.IsRuntimeBinder &&
                                ((expr.eclass & (ExprClass.Value | ExprClass.Variable)) != 0)) {
-                               Report.Error (1061, loc, "Type `{0}' does not contain a definition for `{1}' and no " +
+                               ec.Report.Error (1061, loc, "Type `{0}' does not contain a definition for `{1}' and no " +
                                        "extension method `{1}' of type `{0}' could be found " +
                                        "(are you missing a using directive or an assembly reference?)",
                                        TypeManager.CSharpName (type), name);
                                return;
                        }
 
-                       base.Error_TypeDoesNotContainDefinition (type, name);
+                       base.Error_TypeDoesNotContainDefinition (ec, type, name);
                }
 
                public override string GetSignatureForError ()
@@ -7615,13 +7753,13 @@ namespace Mono.CSharp {
                
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       using (ec.With (EmitContext.Options.AllCheckStateFlags, true))
+                       using (ec.With (ResolveContext.Options.AllCheckStateFlags, true))
                                return Expr.CreateExpressionTree (ec);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       using (ec.With (EmitContext.Options.AllCheckStateFlags, true))
+                       using (ec.With (ResolveContext.Options.AllCheckStateFlags, true))
                                Expr = Expr.Resolve (ec);
                        
                        if (Expr == null)
@@ -7647,6 +7785,13 @@ namespace Mono.CSharp {
                                Expr.EmitBranchable (ec, target, on_true);
                }
 
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
+               {
+                       using (ctx.With (BuilderContext.Options.AllCheckStateFlags, true)) {
+                               return Expr.MakeExpression (ctx);
+                       }
+               }
+
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
                        Expr.MutateHoistedGenericType (storey);
@@ -7675,13 +7820,13 @@ namespace Mono.CSharp {
                
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       using (ec.With (EmitContext.Options.AllCheckStateFlags, false))
+                       using (ec.With (ResolveContext.Options.AllCheckStateFlags, false))
                                return Expr.CreateExpressionTree (ec);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       using (ec.With (EmitContext.Options.AllCheckStateFlags, false))
+                       using (ec.With (ResolveContext.Options.AllCheckStateFlags, false))
                                Expr = Expr.Resolve (ec);
 
                        if (Expr == null)
@@ -7742,24 +7887,24 @@ namespace Mono.CSharp {
                        Arguments args = Arguments.CreateForExpressionTree (ec, Arguments,
                                Expr.CreateExpressionTree (ec));
 
-                       return CreateExpressionFactoryCall ("ArrayIndex", args);
+                       return CreateExpressionFactoryCall (ec, "ArrayIndex", args);
                }
 
                Expression MakePointerAccess (ResolveContext ec, Type t)
                {
                        if (Arguments.Count != 1){
-                               Error (196, "A pointer must be indexed by only one value");
+                               ec.Report.Error (196, loc, "A pointer must be indexed by only one value");
                                return null;
                        }
 
                        if (Arguments [0] is NamedArgument)
-                               Error_NamedArgument ((NamedArgument) Arguments[0]);
+                               Error_NamedArgument ((NamedArgument) Arguments[0], ec.Report);
 
                        Expression p = new PointerArithmetic (Binary.Operator.Addition, Expr, Arguments [0].Expr.Resolve (ec), t, loc);
                        return new Indirection (p, loc).Resolve (ec);
                }
                
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        Expr = Expr.Resolve (ec);
                        if (Expr == null)
@@ -7774,7 +7919,7 @@ namespace Mono.CSharp {
                        Type t = Expr.Type;
 
                        if (t == TypeManager.array_type){
-                               Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `System.Array'");
+                               ec.Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `System.Array'");
                                return null;
                        }
                        
@@ -7785,7 +7930,7 @@ namespace Mono.CSharp {
 
                        FieldExpr fe = Expr as FieldExpr;
                        if (fe != null) {
-                               IFixedBuffer ff = AttributeTester.GetFixedBuffer (fe.FieldInfo);
+                               var ff = fe.Spec as FixedFieldSpec;
                                if (ff != null) {
                                        return MakePointerAccess (ec, ff.ElementType);
                                }
@@ -7817,9 +7962,9 @@ namespace Mono.CSharp {
                        throw new Exception ("Should never be reached");
                }
 
-               public static void Error_NamedArgument (NamedArgument na)
+               public static void Error_NamedArgument (NamedArgument na, Report Report)
                {
-                       Report.Error (1742, na.Name.Location, "An element access expression cannot use named argument");
+                       Report.Error (1742, na.Location, "An element access expression cannot use named argument");
                }
 
                public override string GetSignatureForError ()
@@ -7840,7 +7985,7 @@ namespace Mono.CSharp {
        /// <summary>
        ///   Implements array access 
        /// </summary>
-       public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
+       public class ArrayAccess : Expression, IDynamicAssign, IMemoryLocation {
                //
                // Points to our "data" repository
                //
@@ -7866,22 +8011,8 @@ namespace Mono.CSharp {
                        return DoResolve (ec);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-#if false
-                       ExprClass eclass = ea.Expr.eclass;
-
-                       // As long as the type is valid
-                       if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
-                             eclass == ExprClass.Value)) {
-                               ea.Expr.Error_UnexpectedKind ("variable or value");
-                               return null;
-                       }
-#endif
-
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        // dynamic is used per argument in ConvertExpressionToArrayIndex case
                        bool dynamic;
                        ea.Arguments.Resolve (ec, out dynamic);
@@ -7889,19 +8020,19 @@ namespace Mono.CSharp {
                        Type t = ea.Expr.Type;
                        int rank = ea.Arguments.Count;
                        if (t.GetArrayRank () != rank) {
-                               Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
+                               ec.Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
                                          ea.Arguments.Count.ToString (), t.GetArrayRank ().ToString ());
                                return null;
                        }
 
                        type = TypeManager.GetElementType (t);
-                       if (type.IsPointer && !ec.InUnsafe) {
-                               UnsafeError (ea.Location);
+                       if (type.IsPointer && !ec.IsUnsafe) {
+                               UnsafeError (ec, ea.Location);
                        }
 
                        foreach (Argument a in ea.Arguments) {
                                if (a is NamedArgument)
-                                       ElementAccess.Error_NamedArgument ((NamedArgument) a);
+                                       ElementAccess.Error_NamedArgument ((NamedArgument) a, ec.Report);
 
                                a.Expr = ConvertExpressionToArrayIndex (ec, a.Expr);
                        }
@@ -7950,19 +8081,17 @@ namespace Mono.CSharp {
                        } else if (TypeManager.IsStruct (type)){
                                ig.Emit (OpCodes.Ldelema, type);
                                ig.Emit (OpCodes.Ldobj, type);
-#if GMCS_SOURCE
                        } else if (type.IsGenericParameter) {
                                ig.Emit (OpCodes.Ldelem, type);
-#endif
                        } else if (type.IsPointer)
                                ig.Emit (OpCodes.Ldelem_I);
                        else
                                ig.Emit (OpCodes.Ldelem_Ref);
                }
 
-               protected override void Error_NegativeArrayIndex (Location loc)
+               protected override void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
                {
-                       Report.Warning (251, 2, loc, "Indexing an array with a negative index (array indices always start at zero)");
+                       ec.Report.Warning (251, 2, loc, "Indexing an array with a negative index (array indices always start at zero)");
                }
 
                /// <summary>
@@ -7971,7 +8100,6 @@ namespace Mono.CSharp {
                /// </summary>
                static public OpCode GetStoreOpcode (Type t, out bool is_stobj, out bool has_type_arg)
                {
-                       //Console.WriteLine (new System.Diagnostics.StackTrace ());
                        has_type_arg = false; is_stobj = false;
                        t = TypeManager.TypeToCoreType (t);
                        if (TypeManager.IsEnumType (t))
@@ -7998,12 +8126,9 @@ namespace Mono.CSharp {
                                has_type_arg = true;
                                is_stobj = true;
                                return OpCodes.Stobj;
-#if GMCS_SOURCE
                        } else if (t.IsGenericParameter) {
                                has_type_arg = true;
                                return OpCodes.Stelem;
-#endif
-
                        } else if (t.IsPointer)
                                return OpCodes.Stelem_I;
                        else
@@ -8196,6 +8321,22 @@ namespace Mono.CSharp {
                        }
                }
 
+#if NET_4_0
+               public SLE.Expression MakeAssignExpression (BuilderContext ctx)
+               {
+                       return SLE.Expression.ArrayAccess (
+                               ea.Expr.MakeExpression (ctx),
+                               Arguments.MakeExpression (ea.Arguments, ctx));
+               }
+#endif
+
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
+               {
+                       return SLE.Expression.ArrayIndex (
+                               ea.Expr.MakeExpression (ctx),
+                               Arguments.MakeExpression (ea.Arguments, ctx));
+               }
+
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
                        type = storey.MutateType (type);
@@ -8206,14 +8347,14 @@ namespace Mono.CSharp {
        /// <summary>
        ///   Expressions that represent an indexer call.
        /// </summary>
-       public class IndexerAccess : Expression, IAssignMethod
+       public class IndexerAccess : Expression, IDynamicAssign
        {
                class IndexerMethodGroupExpr : MethodGroupExpr
                {
                        public IndexerMethodGroupExpr (Indexers indexers, Location loc)
                                : base (null, loc)
                        {
-                               Methods = (MethodBase []) indexers.Methods.ToArray (typeof (MethodBase));
+                               Methods = indexers.Methods.ToArray ();
                        }
 
                        public override string Name {
@@ -8222,7 +8363,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       protected override int GetApplicableParametersCount (MethodBase method, AParametersCollection parameters)
+                       protected override int GetApplicableParametersCount (MethodSpec method, AParametersCollection parameters)
                        {
                                //
                                // Here is the trick, decrease number of arguments by 1 when only
@@ -8240,8 +8381,8 @@ namespace Mono.CSharp {
                class Indexers
                {
                        // Contains either property getter or setter
-                       public ArrayList Methods;
-                       public ArrayList Properties;
+                       public List<MethodSpec> Methods;
+                       public List<PropertyInfo> Properties;
 
                        Indexers ()
                        {
@@ -8258,11 +8399,11 @@ namespace Mono.CSharp {
                                                accessor = property.GetSetMethod (true);
 
                                        if (Methods == null) {
-                                               Methods = new ArrayList ();
-                                               Properties = new ArrayList ();
+                                               Methods = new List<MethodSpec> ();
+                                               Properties = new List<PropertyInfo> ();
                                        }
 
-                                       Methods.Add (accessor);
+                                       Methods.Add (Import.CreateMethod (accessor));
                                        Properties.Add (property);
                                }
                        }
@@ -8322,7 +8463,7 @@ namespace Mono.CSharp {
                //
                // Points to our "data" repository
                //
-               MethodInfo get, set;
+               MethodSpec get, set;
                bool is_base_indexer;
                bool prepared;
                LocalTemporary temp;
@@ -8345,7 +8486,6 @@ namespace Mono.CSharp {
                {
                        this.instance_expr = instance_expr;
                        this.is_base_indexer = is_base_indexer;
-                       this.eclass = ExprClass.Value;
                        this.loc = loc;
                }
 
@@ -8360,7 +8500,7 @@ namespace Mono.CSharp {
                                instance_expr.CreateExpressionTree (ec),
                                new TypeOfMethod (get, loc));
 
-                       return CreateExpressionFactoryCall ("Call", args);
+                       return CreateExpressionFactoryCall (ec, "Call", args);
                }
 
                protected virtual void CommonResolve (ResolveContext ec)
@@ -8369,16 +8509,15 @@ namespace Mono.CSharp {
                        current_type = ec.CurrentType;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return ResolveAccessor (ec, null);
                }
 
                public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
-                       if (right_side == EmptyExpression.OutAccess) {
-                               Report.Error (206, loc,
-                                       "A property or indexer may not be passed as an out or ref parameter");
+                       if (right_side == EmptyExpression.OutAccess.Instance) {
+                               right_side.DoResolveLValue (ec, this);
                                return null;
                        }
 
@@ -8394,56 +8533,71 @@ namespace Mono.CSharp {
                {
                        CommonResolve (ec);
 
+                       MethodGroupExpr mg;
+                       Indexers ilist;
                        bool dynamic;
+
                        arguments.Resolve (ec, out dynamic);
-                       if (dynamic || TypeManager.IsDynamicType (indexer_type)) {
-                               int additional = right_side == null ? 1 : 2;
-                               Arguments args = new Arguments (arguments.Count + additional);
+
+                       if (TypeManager.IsDynamicType (indexer_type)) {
+                               dynamic = true;
+                               mg = null;
+                               ilist = null;
+                       } else {
+                               ilist = Indexers.GetIndexersForType (current_type, indexer_type);
+                               if (ilist.Methods == null) {
+                                       ec.Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
+                                                         TypeManager.CSharpName (indexer_type));
+                                       return null;
+                               }
+
+                               mg = new IndexerMethodGroupExpr (ilist, loc);
+                               mg = mg.OverloadResolve (ec, ref arguments, false, loc);
+                               if (mg == null)
+                                       return null;
+                       }
+
+                       if (dynamic) {
+                               Arguments args = new Arguments (arguments.Count + 1);
                                if (is_base_indexer) {
-                                       Report.Error (1972, loc, "The indexer base access cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access");
+                                       ec.Report.Error (1972, loc, "The indexer base access cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access");
                                } else {
                                        args.Add (new Argument (instance_expr));
                                }
                                args.AddRange (arguments);
-                               if (right_side != null)
-                                       args.Add (new Argument (right_side));
 
-                               return new DynamicIndexBinder (right_side != null, args, loc).Resolve (ec);
-                       }
+                               var expr = new DynamicIndexBinder (args, loc);
+                               if (right_side != null)
+                                       return expr.ResolveLValue (ec, right_side);
 
-                       Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type);
-                       if (ilist.Methods == null) {
-                               Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
-                                                 TypeManager.CSharpName (indexer_type));
-                               return null;
+                               return expr.Resolve (ec);
                        }
 
-                       MethodGroupExpr mg = new IndexerMethodGroupExpr (ilist, loc);
-                       mg = mg.OverloadResolve (ec, ref arguments, false, loc);
-                       if (mg == null)
-                               return null;
-
-                       MethodInfo mi = (MethodInfo) mg;
+                       var mi = (MethodSpec) mg;
                        PropertyInfo pi = null;
                        for (int i = 0; i < ilist.Methods.Count; ++i) {
-                               if (ilist.Methods [i] == mi) {
+                               if (ilist.Methods [i].MetaInfo == mi.MetaInfo) {
                                        pi = (PropertyInfo) ilist.Properties [i];
                                        break;
                                }
                        }
 
                        type = TypeManager.TypeToCoreType (pi.PropertyType);
-                       if (type.IsPointer && !ec.InUnsafe)
-                               UnsafeError (loc);
+                       if (type.IsPointer && !ec.IsUnsafe)
+                               UnsafeError (ec, loc);
 
-                       MethodInfo accessor;
+                       MethodSpec accessor = null;
                        if (right_side == null) {
-                               accessor = get = pi.GetGetMethod (true);
+                               var m = pi.GetGetMethod (true);
+                               if (m != null)
+                                       accessor = get = Import.CreateMethod (m);
                        } else {
-                               accessor = set = pi.GetSetMethod (true);
+                               var m = pi.GetSetMethod (true);
+                               if (m != null)
+                                       accessor = set = Import.CreateMethod (m);
                                if (accessor == null && pi.GetGetMethod (true) != null) {
-                                       Report.SymbolRelatedToPreviousError (pi);
-                                       Report.Error (200, loc, "The read only property or indexer `{0}' cannot be assigned to",
+                                       ec.Report.SymbolRelatedToPreviousError (pi);
+                                       ec.Report.Error (200, loc, "The read only property or indexer `{0}' cannot be assigned to",
                                                TypeManager.GetFullNameSignature (pi));
                                        return null;
                                }
@@ -8452,8 +8606,8 @@ namespace Mono.CSharp {
                        }
 
                        if (accessor == null) {
-                               Report.SymbolRelatedToPreviousError (pi);
-                               Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks a `{1}' accessor",
+                               ec.Report.SymbolRelatedToPreviousError (pi);
+                               ec.Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks a `{1}' accessor",
                                        TypeManager.GetFullNameSignature (pi), GetAccessorName (right_side != null));
                                return null;
                        }
@@ -8462,31 +8616,51 @@ namespace Mono.CSharp {
                        // Only base will allow this invocation to happen.
                        //
                        if (accessor.IsAbstract && this is BaseIndexerAccess) {
-                               Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (pi));
+                               Error_CannotCallAbstractBase (ec, TypeManager.GetFullNameSignature (pi));
                        }
 
                        bool must_do_cs1540_check;
                        if (!IsAccessorAccessible (ec.CurrentType, accessor, out must_do_cs1540_check)) {
-                               if (set == null)
-                                       set = pi.GetSetMethod (true);
-                               else
-                                       get = pi.GetGetMethod (true);
+                               if (set == null) {
+                                       var m = pi.GetSetMethod (true);
+                                       if (m != null)
+                                               set = Import.CreateMethod (m);
+                               } else {
+                                       var m = pi.GetGetMethod (true);
+                                       if (m != null)
+                                               get = Import.CreateMethod (m);
+                               }
 
                                if (set != null && get != null &&
-                                       (set.Attributes & MethodAttributes.MemberAccessMask) != (get.Attributes & MethodAttributes.MemberAccessMask)) {
-                                       Report.SymbolRelatedToPreviousError (accessor);
-                                       Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because a `{1}' accessor is inaccessible",
+                                       (set.MetaInfo.Attributes & MethodAttributes.MemberAccessMask) != (get.MetaInfo.Attributes & MethodAttributes.MemberAccessMask)) {
+                                       ec.Report.SymbolRelatedToPreviousError (accessor.MetaInfo);
+                                       ec.Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because a `{1}' accessor is inaccessible",
                                                TypeManager.GetFullNameSignature (pi), GetAccessorName (right_side != null));
                                } else {
-                                       Report.SymbolRelatedToPreviousError (pi);
-                                       ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (pi));
+                                       ec.Report.SymbolRelatedToPreviousError (pi);
+                                       ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (pi), ec.Report);
                                }
                        }
 
                        instance_expr.CheckMarshalByRefAccess (ec);
+
+                       if (must_do_cs1540_check && (instance_expr != EmptyExpression.Null) &&
+                           !TypeManager.IsInstantiationOfSameGenericType (instance_expr.Type, ec.CurrentType) &&
+                           !TypeManager.IsNestedChildOf (ec.CurrentType, instance_expr.Type) &&
+                           !TypeManager.IsSubclassOf (instance_expr.Type, ec.CurrentType)) {
+                               ec.Report.SymbolRelatedToPreviousError (accessor.MetaInfo);
+                               Error_CannotAccessProtected (ec, loc, accessor.MetaInfo, instance_expr.Type, ec.CurrentType);
+                               return null;
+                       }
+
                        eclass = ExprClass.IndexerAccess;
                        return this;
                }
+
+               public override void Emit (EmitContext ec)
+               {
+                       Emit (ec, false);
+               }
                
                public void Emit (EmitContext ec, bool leave_copy)
                {
@@ -8546,22 +8720,35 @@ namespace Mono.CSharp {
                        }
                }
                
-               public override void Emit (EmitContext ec)
+               public override string GetSignatureForError ()
                {
-                       Emit (ec, false);
+                       return TypeManager.CSharpSignature (get != null ? get.MetaInfo : set.MetaInfo, false);
                }
 
-               public override string GetSignatureForError ()
+#if NET_4_0
+               public SLE.Expression MakeAssignExpression (BuilderContext ctx)
+               {
+                       var value = new[] { set_expr.MakeExpression (ctx) };
+                       var args = Arguments.MakeExpression (arguments, ctx).Concat (value);
+
+                       return SLE.Expression.Block (
+                                       SLE.Expression.Call (instance_expr.MakeExpression (ctx), (MethodInfo) set.MetaInfo, args),
+                                       value [0]);
+               }
+#endif
+
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       return TypeManager.CSharpSignature (get != null ? get : set, false);
+                       var args = Arguments.MakeExpression (arguments, ctx);
+                       return SLE.Expression.Call (instance_expr.MakeExpression (ctx), (MethodInfo) get.MetaInfo, args);
                }
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
                        if (get != null)
-                               get = storey.MutateGenericMethod (get);
+                               storey.MutateGenericMethod (get);
                        if (set != null)
-                               set = storey.MutateGenericMethod (set);
+                               storey.MutateGenericMethod (set);
 
                        instance_expr.MutateHoistedGenericType (storey);
                        if (arguments != null)
@@ -8606,7 +8793,7 @@ namespace Mono.CSharp {
                        throw new NotSupportedException ("ET");
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        Expression c = CommonResolve (ec);
 
@@ -8645,17 +8832,17 @@ namespace Mono.CSharp {
 
                        if (!This.IsThisAvailable (ec)) {
                                if (ec.IsStatic) {
-                                       Error (1511, "Keyword `base' is not available in a static method");
+                                       ec.Report.Error (1511, loc, "Keyword `base' is not available in a static method");
                                } else {
-                                       Error (1512, "Keyword `base' is not available in the current context");
+                                       ec.Report.Error (1512, loc, "Keyword `base' is not available in the current context");
                                }
                                return null;
                        }
                        
-                       member_lookup = MemberLookup (ec.CurrentType, null, base_type, Identifier,
+                       member_lookup = MemberLookup (ec.Compiler, ec.CurrentType, null, base_type, Identifier,
                                                      AllMemberTypes, AllBindingFlags, loc);
                        if (member_lookup == null) {
-                               Error_MemberLookupFailed (ec.CurrentType, base_type, base_type, Identifier,
+                               Error_MemberLookupFailed (ec, ec.CurrentType, base_type, base_type, Identifier,
                                        null, AllMemberTypes, AllBindingFlags);
                                return null;
                        }
@@ -8667,7 +8854,19 @@ namespace Mono.CSharp {
                        else
                                left = ec.GetThis (loc);
 
-                       MemberExpr me = (MemberExpr) member_lookup;
+                       MemberExpr me = member_lookup as MemberExpr;
+                       if (me == null){
+                               if (member_lookup is TypeExpression){
+                                       ec.Report.Error (582, loc, "{0}: Can not reference a type through an expression, try `{1}' instead",
+                                                        Identifier, member_lookup.GetSignatureForError ());
+                               } else {
+                                       ec.Report.Error (582, loc, "{0}: Can not reference a {1} through an expression", 
+                                                        Identifier, member_lookup.ExprClassName);
+                               }
+                               
+                               return null;
+                       }
+                       
                        me = me.ResolveMemberAccess (ec, left, loc, null);
                        if (me == null)
                                return null;
@@ -8675,7 +8874,7 @@ namespace Mono.CSharp {
                        me.IsBase = true;
                        if (args != null) {
                                args.Resolve (ec);
-                               me.SetTypeArguments (args);
+                               me.SetTypeArguments (ec, args);
                        }
 
                        return me;
@@ -8715,7 +8914,7 @@ namespace Mono.CSharp {
 
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       MemberExpr.Error_BaseAccessInExpressionTree (loc);
+                       MemberExpr.Error_BaseAccessInExpressionTree (ec, loc);
                        return base.CreateExpressionTree (ec);
                }
        }
@@ -8733,7 +8932,19 @@ namespace Mono.CSharp {
        public class EmptyExpression : Expression {
                public static readonly Expression Null = new EmptyExpression ();
 
-               public static readonly EmptyExpression OutAccess = new EmptyExpression ();
+               public class OutAccess : EmptyExpression
+               {
+                       public static readonly OutAccess Instance = new OutAccess ();
+
+                       public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
+                       {
+                               rc.Report.Error (206, right_side.Location,
+                                       "A property, indexer or dynamic member access may not be passed as `ref' or `out' parameter");
+
+                               return null;
+                       }
+               }
+
                public static readonly EmptyExpression LValueMemberAccess = new EmptyExpression ();
                public static readonly EmptyExpression LValueMemberOutAccess = new EmptyExpression ();
                public static readonly EmptyExpression UnaryAddress = new EmptyExpression ();
@@ -8771,7 +8982,7 @@ namespace Mono.CSharp {
                        throw new NotSupportedException ("ET");
                }
                
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return this;
                }
@@ -8805,7 +9016,6 @@ namespace Mono.CSharp {
 
                private EmptyExpressionStatement ()
                {
-                       eclass = ExprClass.Value;
                        loc = Location.Null;
                }
 
@@ -8819,8 +9029,9 @@ namespace Mono.CSharp {
                        // Do nothing
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
+                       eclass = ExprClass.Value;
                        type = TypeManager.object_type;
                        return this;
                }
@@ -8832,10 +9043,10 @@ namespace Mono.CSharp {
        }       
 
        public class UserCast : Expression {
-               MethodInfo method;
+               MethodSpec method;
                Expression source;
                
-               public UserCast (MethodInfo method, Expression source, Location l)
+               public UserCast (MethodSpec method, Expression source, Location l)
                {
                        this.method = method;
                        this.source = source;
@@ -8855,14 +9066,14 @@ namespace Mono.CSharp {
                        args.Add (new Argument (source.CreateExpressionTree (ec)));
                        args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
                        args.Add (new Argument (new TypeOfMethod (method, loc)));
-                       return CreateExpressionFactoryCall ("Convert", args);
+                       return CreateExpressionFactoryCall (ec, "Convert", args);
                }
                        
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (method);
+                       ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (method.MetaInfo);
                        if (oa != null)
-                               AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc);
+                               AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, ec.Report);
 
                        eclass = ExprClass.Value;
                        return this;
@@ -8871,18 +9082,23 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        source.Emit (ec);
-                       ec.ig.Emit (OpCodes.Call, method);
+                       ec.ig.Emit (OpCodes.Call, (MethodInfo) method.MetaInfo);
                }
 
                public override string GetSignatureForError ()
                {
-                       return TypeManager.CSharpSignature (method);
+                       return TypeManager.CSharpSignature (method.MetaInfo);
+               }
+
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
+               {
+                       return SLE.Expression.Convert (source.MakeExpression (ctx), type, (MethodInfo) method.MetaInfo);
                }
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
                        source.MutateHoistedGenericType (storey);
-                       method = storey.MutateGenericMethod (method);
+                       storey.MutateGenericMethod (method);
                }
        }
 
@@ -8922,18 +9138,18 @@ namespace Mono.CSharp {
                                return nullable.ResolveAsTypeTerminal (ec, false);
                        }
 
-                       if (dim == "*" && !TypeManager.VerifyUnManaged (ltype, loc))
+                       if (dim == "*" && !TypeManager.VerifyUnmanaged (ec.Compiler, ltype, loc))
                                return null;
 
                        if (dim.Length != 0 && dim [0] == '[') {
                                if (TypeManager.IsSpecialType (ltype)) {
-                                       Report.Error (611, loc, "Array elements cannot be of type `{0}'", TypeManager.CSharpName (ltype));
+                                       ec.Compiler.Report.Error (611, loc, "Array elements cannot be of type `{0}'", TypeManager.CSharpName (ltype));
                                        return null;
                                }
 
                                if ((ltype.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
-                                       Report.SymbolRelatedToPreviousError (ltype);
-                                       Report.Error (719, loc, "Array elements cannot be of static type `{0}'", 
+                                       ec.Compiler.Report.SymbolRelatedToPreviousError (ltype);
+                                       ec.Compiler.Report.Error (719, loc, "Array elements cannot be of static type `{0}'", 
                                                TypeManager.CSharpName (ltype));
                                }
                        }
@@ -8946,8 +9162,8 @@ namespace Mono.CSharp {
                        if (type == null)
                                throw new InternalErrorException ("Couldn't create computed type " + ltype + dim);
 
-                       if (type.IsPointer && !ec.IsInUnsafeScope){
-                               UnsafeError (loc);
+                       if (type.IsPointer && !ec.IsUnsafe){
+                               UnsafeError (ec.Compiler.Report, loc);
                        }
 
                        eclass = ExprClass.Type;
@@ -8979,7 +9195,7 @@ namespace Mono.CSharp {
 
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       Error_PointerInsideExpressionTree ();
+                       Error_PointerInsideExpressionTree (ec);
                        return null;
                }
 
@@ -8988,7 +9204,7 @@ namespace Mono.CSharp {
                        array.Emit (ec);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        //
                        // We are born fully resolved
@@ -9028,34 +9244,39 @@ namespace Mono.CSharp {
        public class ArrayIndexCast : TypeCast
        {
                public ArrayIndexCast (Expression expr)
-                       : base (expr, expr.Type)
+                       : base (expr, TypeManager.int32_type)
                {
+                       if (expr.Type == TypeManager.int32_type)
+                               throw new ArgumentException ("unnecessary array index conversion");
                }
 
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       Arguments args = new Arguments (2);
-                       args.Add (new Argument (child.CreateExpressionTree (ec)));
-                       args.Add (new Argument (new TypeOf (new TypeExpression (TypeManager.int32_type, loc), loc)));
-                       return CreateExpressionFactoryCall ("ConvertChecked", args);
+                       using (ec.Set (ResolveContext.Options.CheckedScope)) {
+                               return base.CreateExpressionTree (ec);
+                       }
                }
 
                public override void Emit (EmitContext ec)
                {
                        child.Emit (ec);
-                               
-                       if (type == TypeManager.int32_type)
-                               return;
 
-                       if (type == TypeManager.uint32_type)
+                       var expr_type = child.Type;
+
+                       if (expr_type == TypeManager.uint32_type)
                                ec.ig.Emit (OpCodes.Conv_U);
-                       else if (type == TypeManager.int64_type)
+                       else if (expr_type == TypeManager.int64_type)
                                ec.ig.Emit (OpCodes.Conv_Ovf_I);
-                       else if (type == TypeManager.uint64_type)
+                       else if (expr_type == TypeManager.uint64_type)
                                ec.ig.Emit (OpCodes.Conv_Ovf_I_Un);
                        else
                                throw new InternalErrorException ("Cannot emit cast to unknown array element type", type);
                }
+
+               public override bool GetAttributableValue (ResolveContext ec, Type value_type, out object value)
+               {
+                       return child.GetAttributableValue (ec, value_type, out value);
+               }
        }
 
        //
@@ -9078,7 +9299,7 @@ namespace Mono.CSharp {
                        throw new NotSupportedException ("ET");
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        count = count.Resolve (ec);
                        if (count == null)
@@ -9092,13 +9313,11 @@ namespace Mono.CSharp {
 
                        Constant c = count as Constant;
                        if (c != null && c.IsNegative) {
-                               Report.Error (247, loc, "Cannot use a negative size with stackalloc");
-                               return null;
+                               ec.Report.Error (247, loc, "Cannot use a negative size with stackalloc");
                        }
 
-                       if (ec.HasAny (EmitContext.Options.CatchScope | EmitContext.Options.FinallyScope)) {
-                               Error (255, "Cannot use stackalloc in finally or catch");
-                               return null;
+                       if (ec.HasAny (ResolveContext.Options.CatchScope | ResolveContext.Options.FinallyScope)) {
+                               ec.Report.Error (255, loc, "Cannot use stackalloc in finally or catch");
                        }
 
                        TypeExpr texpr = t.ResolveAsTypeTerminal (ec, false);
@@ -9107,7 +9326,7 @@ namespace Mono.CSharp {
 
                        otype = texpr.Type;
 
-                       if (!TypeManager.VerifyUnManaged (otype, loc))
+                       if (!TypeManager.VerifyUnmanaged (ec.Compiler, otype, loc))
                                return null;
 
                        type = TypeManager.GetPointerType (otype);
@@ -9169,12 +9388,12 @@ namespace Mono.CSharp {
                                args.Add (new Argument (((PropertyExpr)target).CreateSetterTypeOfExpression ()));
 
                        args.Add (new Argument (source.CreateExpressionTree (ec)));
-                       return CreateExpressionFactoryCall (
+                       return CreateExpressionFactoryCall (ec,
                                source is CollectionOrObjectInitializers ? "ListBind" : "Bind",
                                args);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (source == null)
                                return EmptyExpressionStatement.Instance;
@@ -9210,19 +9429,19 @@ namespace Mono.CSharp {
                        //
                        Constant c = source as Constant;
                        if (c != null && c.IsDefaultInitializer (type) && target.eclass == ExprClass.Variable)
-                               return EmptyExpressionStatement.Instance.DoResolve (ec);
+                               return EmptyExpressionStatement.Instance.Resolve (ec);
 
                        return expr;
                }
 
-               protected override Expression Error_MemberLookupFailed (Type type, MemberInfo[] members)
+               protected override Expression Error_MemberLookupFailed (ResolveContext ec, Type type, MemberInfo[] members)
                {
                        MemberInfo member = members [0];
                        if (member.MemberType != MemberTypes.Property && member.MemberType != MemberTypes.Field)
-                               Report.Error (1913, loc, "Member `{0}' cannot be initialized. An object " +
+                               ec.Report.Error (1913, loc, "Member `{0}' cannot be initialized. An object " +
                                        "initializer may only be used for fields, or properties", TypeManager.GetFullNameSignature (member));
                        else
-                               Report.Error (1914, loc, " Static field or property `{0}' cannot be assigned in an object initializer",
+                               ec.Report.Error (1914, loc, " Static field or property `{0}' cannot be assigned in an object initializer",
                                        TypeManager.GetFullNameSignature (member));
 
                        return null;
@@ -9257,12 +9476,12 @@ namespace Mono.CSharp {
                        {
                        }
 
-                       protected override void Error_TypeDoesNotContainDefinition (Type type, string name)
+                       protected override void Error_TypeDoesNotContainDefinition (ResolveContext ec, Type type, string name)
                        {
                                if (TypeManager.HasElementType (type))
                                        return;
 
-                               base.Error_TypeDoesNotContainDefinition (type, name);
+                               base.Error_TypeDoesNotContainDefinition (ec, type, name);
                        }
                }
 
@@ -9273,7 +9492,7 @@ namespace Mono.CSharp {
                        this.loc = argument.Location;
                }
 
-               public CollectionElementInitializer (ArrayList arguments, Location loc)
+               public CollectionElementInitializer (List<Expression> arguments, Location loc)
                        : base (null, new Arguments (arguments.Count))
                {
                        foreach (Expression e in arguments)
@@ -9287,13 +9506,13 @@ namespace Mono.CSharp {
                        Arguments args = new Arguments (2);
                        args.Add (new Argument (mg.CreateExpressionTree (ec)));
 
-                       ArrayList expr_initializers = new ArrayList (arguments.Count);
+                       var expr_initializers = new ArrayInitializer (arguments.Count, loc);
                        foreach (Argument a in arguments)
                                expr_initializers.Add (a.CreateExpressionTree (ec));
 
                        args.Add (new Argument (new ArrayCreation (
-                               CreateExpressionTypeExpression (loc), "[]", expr_initializers, loc)));
-                       return CreateExpressionFactoryCall ("ElementInit", args);
+                               CreateExpressionTypeExpression (ec, loc), "[]", expr_initializers, loc)));
+                       return CreateExpressionFactoryCall (ec, "ElementInit", args);
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -9303,11 +9522,8 @@ namespace Mono.CSharp {
                                target.arguments = arguments.Clone (clonectx);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        base.expr = new AddMemberAccess (ec.CurrentInitializerVariable, loc);
 
                        return base.DoResolve (ec);
@@ -9319,13 +9535,13 @@ namespace Mono.CSharp {
        //
        public class CollectionOrObjectInitializers : ExpressionStatement
        {
-               ArrayList initializers;
+               IList<Expression> initializers;
                bool is_collection_initialization;
                
                public static readonly CollectionOrObjectInitializers Empty = 
-                       new CollectionOrObjectInitializers (new ArrayList (0), Location.Null);
+                       new CollectionOrObjectInitializers (Array.AsReadOnly (new Expression [0]), Location.Null);
 
-               public CollectionOrObjectInitializers (ArrayList initializers, Location loc)
+               public CollectionOrObjectInitializers (IList<Expression> initializers, Location loc)
                {
                        this.initializers = initializers;
                        this.loc = loc;
@@ -9347,14 +9563,14 @@ namespace Mono.CSharp {
                {
                        CollectionOrObjectInitializers t = (CollectionOrObjectInitializers) target;
 
-                       t.initializers = new ArrayList (initializers.Count);
-                       foreach (Expression e in initializers)
+                       t.initializers = new List<Expression> (initializers.Count);
+                       foreach (var e in initializers)
                                t.initializers.Add (e.Clone (clonectx));
                }
 
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       ArrayList expr_initializers = new ArrayList (initializers.Count);
+                       var expr_initializers = new ArrayInitializer (initializers.Count, loc);
                        foreach (Expression e in initializers) {
                                Expression expr = e.CreateExpressionTree (ec);
                                if (expr != null)
@@ -9364,26 +9580,23 @@ namespace Mono.CSharp {
                        return new ImplicitlyTypedArrayCreation ("[]", expr_initializers, loc);
                }
                
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
-                       ArrayList element_names = null;
+                       List<string> element_names = null;
                        for (int i = 0; i < initializers.Count; ++i) {
                                Expression initializer = (Expression) initializers [i];
                                ElementInitializer element_initializer = initializer as ElementInitializer;
 
                                if (i == 0) {
                                        if (element_initializer != null) {
-                                               element_names = new ArrayList (initializers.Count);
+                                               element_names = new List<string> (initializers.Count);
                                                element_names.Add (element_initializer.Name);
                                        } else if (initializer is CompletingExpression){
                                                initializer.Resolve (ec);
                                                throw new InternalErrorException ("This line should never be reached");
                                        } else {
                                                if (!TypeManager.ImplementsInterface (ec.CurrentInitializerVariable.Type, TypeManager.ienumerable_type)) {
-                                                       Report.Error (1922, loc, "A field or property `{0}' cannot be initialized with a collection " +
+                                                       ec.Report.Error (1922, loc, "A field or property `{0}' cannot be initialized with a collection " +
                                                                "object initializer because type `{1}' does not implement `{2}' interface",
                                                                ec.CurrentInitializerVariable.GetSignatureForError (),
                                                                TypeManager.CSharpName (ec.CurrentInitializerVariable.Type),
@@ -9394,14 +9607,14 @@ namespace Mono.CSharp {
                                        }
                                } else {
                                        if (is_collection_initialization != (element_initializer == null)) {
-                                               Report.Error (747, initializer.Location, "Inconsistent `{0}' member declaration",
+                                               ec.Report.Error (747, initializer.Location, "Inconsistent `{0}' member declaration",
                                                        is_collection_initialization ? "collection initializer" : "object initializer");
                                                continue;
                                        }
 
                                        if (!is_collection_initialization) {
                                                if (element_names.Contains (element_initializer.Name)) {
-                                                       Report.Error (1912, element_initializer.Location,
+                                                       ec.Report.Error (1912, element_initializer.Location,
                                                                "An object initializer includes more than one member `{0}' initialization",
                                                                element_initializer.Name);
                                                } else {
@@ -9420,7 +9633,7 @@ namespace Mono.CSharp {
                        type = ec.CurrentInitializerVariable.Type;
                        if (is_collection_initialization) {
                                if (TypeManager.HasElementType (type)) {
-                                       Report.Error (1925, loc, "Cannot initialize object of type `{0}' with a collection initializer",
+                                       ec.Report.Error (1925, loc, "Cannot initialize object of type `{0}' with a collection initializer",
                                                TypeManager.CSharpName (type));
                                }
                        }
@@ -9475,7 +9688,7 @@ namespace Mono.CSharp {
                                throw new NotSupportedException ("ET");
                        }
 
-                       public override Expression DoResolve (ResolveContext ec)
+                       protected override Expression DoResolve (ResolveContext ec)
                        {
                                return this;
                        }
@@ -9535,16 +9748,13 @@ namespace Mono.CSharp {
                        if (!initializers.IsEmpty)
                                args.Add (new Argument (initializers.CreateExpressionTree (ec)));
 
-                       return CreateExpressionFactoryCall (
+                       return CreateExpressionFactoryCall (ec,
                                initializers.IsCollectionInitializer ? "ListInit" : "MemberInit",
                                args);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-                       
                        Expression e = base.DoResolve (ec);
                        if (type == null)
                                return null;
@@ -9606,17 +9816,19 @@ namespace Mono.CSharp {
                }
        }
 
-       public class AnonymousTypeDeclaration : Expression
+       public class NewAnonymousType : New
        {
-               ArrayList parameters;
+               static readonly IList<AnonymousTypeParameter> EmptyParameters = Array.AsReadOnly (new AnonymousTypeParameter[0]);
+
+               List<AnonymousTypeParameter> parameters;
                readonly TypeContainer parent;
-               static readonly ArrayList EmptyParameters = new ArrayList (0);
+               AnonymousTypeClass anonymous_type;
 
-               public AnonymousTypeDeclaration (ArrayList parameters, TypeContainer parent, Location loc)
+               public NewAnonymousType (List<AnonymousTypeParameter> parameters, TypeContainer parent, Location loc)
+                        : base (null, null, loc)
                {
                        this.parameters = parameters;
                        this.parent = parent;
-                       this.loc = loc;
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression target)
@@ -9624,54 +9836,68 @@ namespace Mono.CSharp {
                        if (parameters == null)
                                return;
 
-                       AnonymousTypeDeclaration t = (AnonymousTypeDeclaration) target;
-                       t.parameters = new ArrayList (parameters.Count);
+                       NewAnonymousType t = (NewAnonymousType) target;
+                       t.parameters = new List<AnonymousTypeParameter> (parameters.Count);
                        foreach (AnonymousTypeParameter atp in parameters)
-                               t.parameters.Add (atp.Clone (clonectx));
+                               t.parameters.Add ((AnonymousTypeParameter) atp.Clone (clonectx));
                }
 
-               AnonymousTypeClass CreateAnonymousType (ArrayList parameters)
+               AnonymousTypeClass CreateAnonymousType (ResolveContext ec, IList<AnonymousTypeParameter> parameters)
                {
-                       AnonymousTypeClass type = parent.Module.GetAnonymousType (parameters);
+                       AnonymousTypeClass type = parent.Module.Compiled.GetAnonymousType (parameters);
                        if (type != null)
                                return type;
 
-                       type = AnonymousTypeClass.Create (parent, parameters, loc);
+                       type = AnonymousTypeClass.Create (ec.Compiler, parent, parameters, loc);
                        if (type == null)
                                return null;
 
                        type.DefineType ();
                        type.Define ();
                        type.EmitType ();
-                       if (Report.Errors == 0)
+                       if (ec.Report.Errors == 0)
                                type.CloseType ();
 
-                       parent.Module.AddAnonymousType (type);
+                       parent.Module.Compiled.AddAnonymousType (type);
                        return type;
                }
 
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       throw new NotSupportedException ("ET");
+                       if (parameters == null)
+                               return base.CreateExpressionTree (ec);
+
+                       var init = new ArrayInitializer (parameters.Count, loc);
+                       foreach (Property p in anonymous_type.Properties)
+                               init.Add (new TypeOfMethod (Import.CreateMethod (TypeBuilder.GetMethod (type, p.GetBuilder)), loc));
+
+                       var ctor_args = new ArrayInitializer (Arguments.Count, loc);
+                       foreach (Argument a in Arguments)
+                               ctor_args.Add (a.CreateExpressionTree (ec));
+
+                       Arguments args = new Arguments (3);
+                       args.Add (new Argument (method.CreateExpressionTree (ec)));
+                       args.Add (new Argument (new ArrayCreation (TypeManager.expression_type_expr, "[]", ctor_args, loc)));
+                       args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", init, loc)));
+
+                       return CreateExpressionFactoryCall (ec, "New", args);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       AnonymousTypeClass anonymous_type;
-
-                       if (ec.HasSet (EmitContext.Options.ConstantScope)) {
-                               Report.Error (836, loc, "Anonymous types cannot be used in this expression");
+                       if (ec.HasSet (ResolveContext.Options.ConstantScope)) {
+                               ec.Report.Error (836, loc, "Anonymous types cannot be used in this expression");
                                return null;
                        }
 
                        if (parameters == null) {
-                               anonymous_type = CreateAnonymousType (EmptyParameters);
-                               return new New (new TypeExpression (anonymous_type.TypeBuilder, loc),
-                                       null, loc).Resolve (ec);
+                               anonymous_type = CreateAnonymousType (ec, EmptyParameters);
+                               RequestedType = new TypeExpression (anonymous_type.TypeBuilder, loc);
+                               return base.DoResolve (ec);
                        }
 
                        bool error = false;
-                       Arguments arguments = new Arguments (parameters.Count);
+                       Arguments = new Arguments (parameters.Count);
                        TypeExpression [] t_args = new TypeExpression [parameters.Count];
                        for (int i = 0; i < parameters.Count; ++i) {
                                Expression e = ((AnonymousTypeParameter) parameters [i]).Resolve (ec);
@@ -9680,59 +9906,40 @@ namespace Mono.CSharp {
                                        continue;
                                }
 
-                               arguments.Add (new Argument (e));
+                               Arguments.Add (new Argument (e));
                                t_args [i] = new TypeExpression (e.Type, e.Location);
                        }
 
                        if (error)
                                return null;
 
-                       anonymous_type = CreateAnonymousType (parameters);
+                       anonymous_type = CreateAnonymousType (ec, parameters);
                        if (anonymous_type == null)
                                return null;
 
-                       GenericTypeExpr te = new GenericTypeExpr (anonymous_type.TypeBuilder,
-                               new TypeArguments (t_args), loc);
-
-                       return new New (te, arguments, loc).Resolve (ec);
-               }
-
-               public override void Emit (EmitContext ec)
-               {
-                       throw new InternalErrorException ("Should not be reached");
+                       RequestedType = new GenericTypeExpr (anonymous_type.TypeBuilder, new TypeArguments (t_args), loc);
+                       return base.DoResolve (ec);
                }
        }
 
-       public class AnonymousTypeParameter : Expression
+       public class AnonymousTypeParameter : ShimExpression
        {
                public readonly string Name;
-               Expression initializer;
 
                public AnonymousTypeParameter (Expression initializer, string name, Location loc)
+                       : base (initializer)
                {
                        this.Name = name;
                        this.loc = loc;
-                       this.initializer = initializer;
                }
                
                public AnonymousTypeParameter (Parameter parameter)
+                       : base (new SimpleName (parameter.Name, parameter.Location))
                {
                        this.Name = parameter.Name;
                        this.loc = parameter.Location;
-                       this.initializer = new SimpleName (Name, loc);
                }               
 
-               protected override void CloneTo (CloneContext clonectx, Expression target)
-               {
-                       AnonymousTypeParameter t = (AnonymousTypeParameter) target;
-                       t.initializer = initializer.Clone (clonectx);
-               }
-
-               public override Expression CreateExpressionTree (ResolveContext ec)
-               {
-                       throw new NotSupportedException ("ET");
-               }
-
                public override bool Equals (object o)
                {
                        AnonymousTypeParameter other = o as AnonymousTypeParameter;
@@ -9744,36 +9951,31 @@ namespace Mono.CSharp {
                        return Name.GetHashCode ();
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       Expression e = initializer.Resolve (ec);
+                       Expression e = expr.Resolve (ec);
                        if (e == null)
                                return null;
 
                        if (e.eclass == ExprClass.MethodGroup) {
-                               Error_InvalidInitializer (e.ExprClassName);
+                               Error_InvalidInitializer (ec, e.ExprClassName);
                                return null;
                        }
 
                        type = e.Type;
                        if (type == TypeManager.void_type || type == TypeManager.null_type ||
                                type == InternalType.AnonymousMethod || type.IsPointer) {
-                               Error_InvalidInitializer (e.GetSignatureForError ());
+                               Error_InvalidInitializer (ec, e.GetSignatureForError ());
                                return null;
                        }
 
                        return e;
                }
 
-               protected virtual void Error_InvalidInitializer (string initializer)
+               protected virtual void Error_InvalidInitializer (ResolveContext ec, string initializer)
                {
-                       Report.Error (828, loc, "An anonymous type property `{0}' cannot be initialized with `{1}'",
+                       ec.Report.Error (828, loc, "An anonymous type property `{0}' cannot be initialized with `{1}'",
                                Name, initializer);
                }
-
-               public override void Emit (EmitContext ec)
-               {
-                       throw new InternalErrorException ("Should not be reached");
-               }
        }
 }