New tests.
[mono.git] / mcs / mcs / expression.cs
index 643b3fad7ff85316da11ae6df556e22aa30f0740..233f9295ebfc03c02109681509afbd85c43a7d9d 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
        // resolve phase
        //
        public class UserOperatorCall : Expression {
-               public delegate Expression ExpressionTreeExpression (EmitContext ec, MethodGroupExpr mg);
+               public delegate Expression ExpressionTreeExpression (ResolveContext ec, MethodGroupExpr mg);
 
                protected readonly Arguments arguments;
                protected readonly MethodGroupExpr mg;
@@ -34,12 +36,12 @@ namespace Mono.CSharp {
                        this.arguments = args;
                        this.expr_tree = expr_tree;
 
-                       type = TypeManager.TypeToCoreType (((MethodInfo) mg).ReturnType);
+                       type = mg.BestCandidate.ReturnType;
                        eclass = ExprClass.Value;
                        this.loc = loc;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        if (expr_tree != null)
                                return expr_tree (ec, mg);
@@ -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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        //
                        // We are born fully resolved
@@ -69,53 +71,33 @@ namespace Mono.CSharp {
                        mg.EmitCall (ec, arguments);
                }
 
-               public MethodGroupExpr Method {
-                       get { return mg; }
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
+               {
+                       var method = mg.BestCandidate.GetMetaInfo () as MethodInfo;
+                       return SLE.Expression.Call (method, Arguments.MakeExpression (arguments, ctx));
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       arguments.MutateHoistedGenericType (storey);
-                       mg.MutateHoistedGenericType (storey);
+               public MethodGroupExpr Method {
+                       get { return mg; }
                }
        }
 
-       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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       throw new NotSupportedException ("ET");
+                       return expr.Resolve (ec);
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
-                       Expr = Expr.Resolve (ec);
-                       return Expr;
-               }
-
-               public override Expression DoResolveLValue (EmitContext 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);
                }
        }
        
@@ -129,24 +111,24 @@ namespace Mono.CSharp {
                        AddressOf,  TOP
                }
 
-               static Type [] [] predefined_operators;
+               static TypeSpec[][] predefined_operators;
 
                public readonly Operator Oper;
                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>
                //   This routine will attempt to simplify the unary expression when the
                //   argument is a constant.
                // </summary>
-               Constant TryReduceConstant (EmitContext ec, Constant e)
+               Constant TryReduceConstant (ResolveContext ec, Constant e)
                {
                        if (e is EmptyConstantCast)
                                return TryReduceConstant (ec, ((EmptyConstantCast) e).child);
@@ -156,7 +138,7 @@ namespace Mono.CSharp {
                                return r == null ? null : new SideEffectConstant (r, e, r.Location);
                        }
 
-                       Type expr_type = e.Type;
+                       TypeSpec expr_type = e.Type;
                        
                        switch (Oper){
                        case Operator.UnaryPlus:
@@ -200,7 +182,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 +193,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 +204,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);
                                        }
@@ -298,14 +280,14 @@ namespace Mono.CSharp {
                        throw new Exception ("Can not constant fold: " + Oper.ToString());
                }
                
-               protected Expression ResolveOperator (EmitContext ec, Expression expr)
+               protected Expression ResolveOperator (ResolveContext ec, Expression expr)
                {
                        eclass = ExprClass.Value;
 
                        if (predefined_operators == null)
                                CreatePredefinedOperatorsTable ();
 
-                       Type expr_type = expr.Type;
+                       TypeSpec expr_type = expr.Type;
                        Expression best_expr;
 
                        //
@@ -330,9 +312,9 @@ namespace Mono.CSharp {
                        return ResolveUserType (ec, expr);
                }
 
-               protected virtual Expression ResolveEnumOperator (EmitContext ec, Expression expr)
+               protected virtual Expression ResolveEnumOperator (ResolveContext ec, Expression expr)
                {
-                       Type underlying_type = TypeManager.GetEnumUnderlyingType (expr.Type);
+                       TypeSpec underlying_type = EnumSpec.GetUnderlyingType (expr.Type);
                        Expression best_expr = ResolvePrimitivePredefinedType (EmptyCast.Create (expr, underlying_type));
                        if (best_expr == null)
                                return null;
@@ -343,20 +325,20 @@ namespace Mono.CSharp {
                        return EmptyCast.Create (this, type);
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        return CreateExpressionTree (ec, null);
                }
 
-               Expression CreateExpressionTree (EmitContext ec, MethodGroupExpr user_op)
+               Expression CreateExpressionTree (ResolveContext ec, MethodGroupExpr user_op)
                {
                        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,17 +358,17 @@ 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 ()
                {
-                       predefined_operators = new Type [(int) Operator.TOP] [];
+                       predefined_operators = new TypeSpec [(int) Operator.TOP] [];
 
                        //
                        // 7.6.1 Unary plus operator
                        //
-                       predefined_operators [(int) Operator.UnaryPlus] = new Type [] {
+                       predefined_operators [(int) Operator.UnaryPlus] = new TypeSpec [] {
                                TypeManager.int32_type, TypeManager.uint32_type,
                                TypeManager.int64_type, TypeManager.uint64_type,
                                TypeManager.float_type, TypeManager.double_type,
@@ -396,7 +378,7 @@ namespace Mono.CSharp {
                        //
                        // 7.6.2 Unary minus operator
                        //
-                       predefined_operators [(int) Operator.UnaryNegation] = new Type [] {
+                       predefined_operators [(int) Operator.UnaryNegation] = new TypeSpec [] {
                                TypeManager.int32_type, 
                                TypeManager.int64_type,
                                TypeManager.float_type, TypeManager.double_type,
@@ -406,14 +388,14 @@ namespace Mono.CSharp {
                        //
                        // 7.6.3 Logical negation operator
                        //
-                       predefined_operators [(int) Operator.LogicalNot] = new Type [] {
+                       predefined_operators [(int) Operator.LogicalNot] = new TypeSpec [] {
                                TypeManager.bool_type
                        };
 
                        //
                        // 7.6.4 Bitwise complement operator
                        //
-                       predefined_operators [(int) Operator.OnesComplement] = new Type [] {
+                       predefined_operators [(int) Operator.OnesComplement] = new TypeSpec [] {
                                TypeManager.int32_type, TypeManager.uint32_type,
                                TypeManager.int64_type, TypeManager.uint64_type
                        };
@@ -424,7 +406,7 @@ namespace Mono.CSharp {
                //
                static Expression DoNumericPromotion (Operator op, Expression expr)
                {
-                       Type expr_type = expr.Type;
+                       TypeSpec expr_type = expr.Type;
                        if ((op == Operator.UnaryPlus || op == Operator.UnaryNegation || op == Operator.OnesComplement) &&
                                expr_type == TypeManager.byte_type || expr_type == TypeManager.sbyte_type ||
                                expr_type == TypeManager.short_type || expr_type == TypeManager.ushort_type ||
@@ -437,7 +419,7 @@ namespace Mono.CSharp {
                        return expr;
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (Oper == Operator.AddressOf) {
                                return ResolveAddressOf (ec);
@@ -447,14 +429,14 @@ namespace Mono.CSharp {
                        if (Expr == null)
                                return null;
 
-                       if (TypeManager.IsDynamicType (Expr.Type)) {
+                       if (Expr.Type == InternalType.Dynamic) {
                                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 +445,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
@@ -479,7 +461,7 @@ namespace Mono.CSharp {
                        return expr;
                }
 
-               public override Expression DoResolveLValue (EmitContext ec, Expression right)
+               public override Expression DoResolveLValue (ResolveContext ec, Expression right)
                {
                        return null;
                }
@@ -489,10 +471,8 @@ namespace Mono.CSharp {
                        EmitOperator (ec, type);
                }
 
-               protected void EmitOperator (EmitContext ec, Type type)
+               protected void EmitOperator (EmitContext ec, TypeSpec type)
                {
-                       ILGenerator ig = ec.ig;
-
                        switch (Oper) {
                        case Operator.UnaryPlus:
                                Expr.Emit (ec);
@@ -500,27 +480,27 @@ namespace Mono.CSharp {
                                
                        case Operator.UnaryNegation:
                                if (ec.HasSet (EmitContext.Options.CheckedScope) && !IsFloat (type)) {
-                                       ig.Emit (OpCodes.Ldc_I4_0);
+                                       ec.Emit (OpCodes.Ldc_I4_0);
                                        if (type == TypeManager.int64_type)
-                                               ig.Emit (OpCodes.Conv_U8);
+                                               ec.Emit (OpCodes.Conv_U8);
                                        Expr.Emit (ec);
-                                       ig.Emit (OpCodes.Sub_Ovf);
+                                       ec.Emit (OpCodes.Sub_Ovf);
                                } else {
                                        Expr.Emit (ec);
-                                       ig.Emit (OpCodes.Neg);
+                                       ec.Emit (OpCodes.Neg);
                                }
                                
                                break;
                                
                        case Operator.LogicalNot:
                                Expr.Emit (ec);
-                               ig.Emit (OpCodes.Ldc_I4_0);
-                               ig.Emit (OpCodes.Ceq);
+                               ec.Emit (OpCodes.Ldc_I4_0);
+                               ec.Emit (OpCodes.Ceq);
                                break;
                                
                        case Operator.OnesComplement:
                                Expr.Emit (ec);
-                               ig.Emit (OpCodes.Not);
+                               ec.Emit (OpCodes.Not);
                                break;
                                
                        case Operator.AddressOf:
@@ -552,9 +532,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, TypeSpec 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));
                }
 
@@ -577,7 +557,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               static bool IsFloat (Type t)
+               static bool IsFloat (TypeSpec t)
                {
                        return t == TypeManager.float_type || t == TypeManager.double_type;
                }
@@ -603,24 +583,42 @@ namespace Mono.CSharp {
                        throw new NotImplementedException (oper.ToString ());
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+               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 static void Reset ()
                {
-                       type = storey.MutateType (type);
-                       Expr.MutateHoistedGenericType (storey);
+                       predefined_operators = null;
                }
 
-               Expression ResolveAddressOf (EmitContext ec)
+               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,18 +640,18 @@ 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);
+                       type = PointerContainer.MakeType (Expr.Type);
                        eclass = ExprClass.Value;
                        return this;
                }
@@ -661,9 +659,9 @@ namespace Mono.CSharp {
                Expression ResolvePrimitivePredefinedType (Expression expr)
                {
                        expr = DoNumericPromotion (Oper, expr);
-                       Type expr_type = expr.Type;
-                       Type[] predefined = predefined_operators [(int) Oper];
-                       foreach (Type t in predefined) {
+                       TypeSpec expr_type = expr.Type;
+                       TypeSpec[] predefined = predefined_operators [(int) Oper];
+                       foreach (TypeSpec t in predefined) {
                                if (t == expr_type)
                                        return expr;
                        }
@@ -673,7 +671,7 @@ namespace Mono.CSharp {
                //
                // Perform user-operator overload resolution
                //
-               protected virtual Expression ResolveUserOperator (EmitContext ec, Expression expr)
+               protected virtual Expression ResolveUserOperator (ResolveContext ec, Expression expr)
                {
                        CSharp.Operator.OpType op_type;
                        switch (Oper) {
@@ -690,7 +688,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 = MethodLookup (ec.Compiler, ec.CurrentType, expr.Type, MemberKind.Operator, op_name, 0, expr.Location);
                        if (user_op == null)
                                return null;
 
@@ -708,15 +706,15 @@ namespace Mono.CSharp {
                //
                // Unary user type overload resolution
                //
-               Expression ResolveUserType (EmitContext ec, Expression expr)
+               Expression ResolveUserType (ResolveContext ec, Expression expr)
                {
                        Expression best_expr = ResolveUserOperator (ec, expr);
                        if (best_expr != null)
                                return best_expr;
 
-                       Type[] predefined = predefined_operators [(int) Oper];
-                       foreach (Type t in predefined) {
-                               Expression oper_expr = Convert.UserDefinedConversion (ec, expr, t, expr.Location, false);
+                       TypeSpec[] predefined = predefined_operators [(int) Oper];
+                       foreach (TypeSpec t in predefined) {
+                               Expression oper_expr = Convert.UserDefinedConversion (ec, expr, t, expr.Location, false, false);
                                if (oper_expr == null)
                                        continue;
 
@@ -738,7 +736,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;
                                }
@@ -785,9 +783,9 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       Error_PointerInsideExpressionTree ();
+                       Error_PointerInsideExpressionTree (ec);
                        return null;
                }
                
@@ -802,14 +800,14 @@ namespace Mono.CSharp {
                        if (!prepared)
                                expr.Emit (ec);
                        
-                       LoadFromPtr (ec.ig, Type);
+                       ec.EmitLoadFromPtr (Type);
                }
 
                public void Emit (EmitContext ec, bool leave_copy)
                {
                        Emit (ec);
                        if (leave_copy) {
-                               ec.ig.Emit (OpCodes.Dup);
+                               ec.Emit (OpCodes.Dup);
                                temporary = new LocalTemporary (expr.Type);
                                temporary.Store (ec);
                        }
@@ -822,16 +820,16 @@ namespace Mono.CSharp {
                        expr.Emit (ec);
 
                        if (prepare_for_load)
-                               ec.ig.Emit (OpCodes.Dup);
+                               ec.Emit (OpCodes.Dup);
                        
                        source.Emit (ec);
                        if (leave_copy) {
-                               ec.ig.Emit (OpCodes.Dup);
+                               ec.Emit (OpCodes.Dup);
                                temporary = new LocalTemporary (expr.Type);
                                temporary.Store (ec);
                        }
                        
-                       StoreFromPtr (ec.ig, type);
+                       ec.EmitStoreFromPtr (type);
                        
                        if (temporary != null) {
                                temporary.Emit (ec);
@@ -844,27 +842,27 @@ namespace Mono.CSharp {
                        expr.Emit (ec);
                }
 
-               public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
                        return DoResolve (ec);
                }
 
-               public override Expression DoResolve (EmitContext 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 +895,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,189 +972,53 @@ 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 (EmitContext 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 (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        return new SimpleAssign (this, this).CreateExpressionTree (ec);
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        expr = expr.Resolve (ec);
                        
                        if (expr == null)
                                return null;
 
-                       if (TypeManager.IsDynamicType (expr.Type)) {
+                       if (expr.Type == InternalType.Dynamic) {
+                               //
+                               // 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 +1035,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 +1055,34 @@ namespace Mono.CSharp {
                //
                string GetOperatorExpressionTypeName ()
                {
-                       if ((mode & Mode.IsDecrement) != 0)
-                               return "Decrement";
+                       return IsDecrement ? "Decrement" : "Increment";
+               }
 
-                       return "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 (TypeSpec t)
+               {
+                       return (TypeManager.IsPrimitiveType (t) && t != TypeManager.bool_type) ||
+                               TypeManager.IsEnumType (t) ||
+                               t.IsPointer && t != TypeManager.void_ptr_type;
+               }
+
+#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 +1090,77 @@ 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
+                       //
+                       string op_name;
+
+                       if (IsDecrement)
+                               op_name = Operator.GetMetadataName (Operator.OpType.Decrement);
+                       else
+                               op_name = Operator.GetMetadataName (Operator.OpType.Increment);
+
+                       var mg = MethodLookup (ec.Compiler, ec.CurrentType, type, MemberKind.Operator, op_name, 0, loc);
+
+                       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 +1189,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        probe_type_expr = ProbeType.ResolveAsTypeTerminal (ec, false);
                        if (probe_type_expr == null)
@@ -1182,19 +1199,19 @@ namespace Mono.CSharp {
                        if (expr == null)
                                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",
+                       if (probe_type_expr.Type.IsStatic) {
+                               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;
                        }
@@ -1202,12 +1219,6 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       expr.MutateHoistedGenericType (storey);
-                       probe_type_expr.MutateHoistedGenericType (storey);
-               }
-
                protected abstract string OperatorName { get; }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -1231,59 +1242,57 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        Arguments args = Arguments.CreateForExpressionTree (ec, null,
                                expr.CreateExpressionTree (ec),
                                new TypeOf (probe_type_expr, loc));
 
-                       return CreateExpressionFactoryCall ("TypeIs", args);
+                       return CreateExpressionFactoryCall (ec, "TypeIs", args);
                }
                
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
                        if (expr_unwrap != null) {
                                expr_unwrap.EmitCheck (ec);
                                return;
                        }
 
                        expr.Emit (ec);
-                       ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
-                       ig.Emit (OpCodes.Ldnull);
-                       ig.Emit (OpCodes.Cgt_Un);
+                       ec.Emit (OpCodes.Isinst, probe_type_expr.Type);
+                       ec.Emit (OpCodes.Ldnull);
+                       ec.Emit (OpCodes.Cgt_Un);
                }
 
                public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
                {
-                       ILGenerator ig = ec.ig;
                        if (expr_unwrap != null) {
                                expr_unwrap.EmitCheck (ec);
                        } else {
                                expr.Emit (ec);
-                               ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
+                               ec.Emit (OpCodes.Isinst, probe_type_expr.Type);
                        }                       
-                       ig.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
+                       ec.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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (base.DoResolve (ec) == null)
                                return null;
 
-                       Type d = expr.Type;
+                       TypeSpec d = expr.Type;
                        bool d_is_nullable = false;
 
                        //
@@ -1291,20 +1300,26 @@ 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]);
-                               d_is_nullable = true;
+                       if (TypeManager.IsNullableType (d)) {
+                               var ut = Nullable.NullableInfo.GetUnderlyingType (d);
+                               if (!ut.IsGenericParameter) {
+                                       d = ut;
+                                       d_is_nullable = true;
+                               }
                        }
 
                        type = TypeManager.bool_type;
                        eclass = ExprClass.Value;
-                       Type t = probe_type_expr.Type;
+                       TypeSpec t = probe_type_expr.Type;
                        bool t_is_nullable = false;
-                       if (TypeManager.IsNullableType (t) && !TypeManager.ContainsGenericParameters (t)) {
-                               t = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (t) [0]);
-                               t_is_nullable = true;
+                       if (TypeManager.IsNullableType (t)) {
+                               var ut = Nullable.NullableInfo.GetUnderlyingType (t);
+                               if (!ut.IsGenericParameter) {
+                                       t = ut;
+                                       t_is_nullable = true;
+                               }
                        }
 
                        if (TypeManager.IsStruct (t)) {
@@ -1320,11 +1335,12 @@ 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);
+                               var tp = d as TypeParameterSpec;
+                               if (tp != null)
+                                       return ResolveGenericParameter (ec, t, tp);
 
                                //
                                // An unboxing conversion exists
@@ -1333,15 +1349,15 @@ namespace Mono.CSharp {
                                        return this;
                        } else {
                                if (TypeManager.IsGenericParameter (t))
-                                       return ResolveGenericParameter (d, t);
+                                       return ResolveGenericParameter (ec, d, (TypeParameterSpec) 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, (TypeParameterSpec) d);
 
                                        if (TypeManager.ContainsGenericParameters (d))
                                                return this;
@@ -1353,22 +1369,22 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       return CreateConstantResult (false);
+                       return CreateConstantResult (ec, false);
                }
 
-               Expression ResolveGenericParameter (Type d, Type t)
+               Expression ResolveGenericParameter (ResolveContext ec, TypeSpec d, TypeParameterSpec 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));
+                       if (t.IsReferenceType) {
+                               if (TypeManager.IsStruct (d))
+                                       return CreateConstantResult (ec, false);
                        }
 
-                       if (TypeManager.IsGenericParameter (expr.Type))
+                       if (TypeManager.IsGenericParameter (expr.Type)) {
+                               if (t.IsValueType && expr.Type == t)
+                                       return CreateConstantResult (ec, true);
+
                                expr = new BoxedCast (expr, d);
+                       }
 
                        return this;
                }
@@ -1390,36 +1406,28 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        Arguments args = Arguments.CreateForExpressionTree (ec, null,
                                expr.CreateExpressionTree (ec),
                                new TypeOf (probe_type_expr, loc));
 
-                       return CreateExpressionFactoryCall ("TypeAs", args);
+                       return CreateExpressionFactoryCall (ec, "TypeAs", args);
                }
 
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-
                        expr.Emit (ec);
 
                        if (do_isinst)
-                               ig.Emit (OpCodes.Isinst, type);
+                               ec.Emit (OpCodes.Isinst, type);
 
-#if GMCS_SOURCE
                        if (TypeManager.IsGenericParameter (type) || TypeManager.IsNullableType (type))
-                               ig.Emit (OpCodes.Unbox_Any, type);
-#endif
+                               ec.Emit (OpCodes.Unbox_Any, type);
                }
 
-               public override Expression DoResolve (EmitContext 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);
 
@@ -1429,15 +1437,15 @@ namespace Mono.CSharp {
 
                        type = probe_type_expr.Type;
                        eclass = ExprClass.Value;
-                       Type etype = expr.Type;
+                       TypeSpec etype = expr.Type;
 
                        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 +1453,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 +1478,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;
@@ -1479,17 +1487,6 @@ namespace Mono.CSharp {
                protected override string OperatorName {
                        get { return "as"; }
                }
-
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       type = storey.MutateType (type);
-                       base.MutateHoistedGenericType (storey);
-               }
-       
-               public override bool GetAttributableValue (EmitContext ec, Type value_type, out object value)
-               {
-                       return expr.GetAttributableValue (ec, value_type, out value);
-               }
        }
        
        /// <summary>
@@ -1498,9 +1495,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 +1504,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 +1514,7 @@ namespace Mono.CSharp {
                        get { return target_type; }
                }
 
-               public Expression Expr {
-                       get { return expr; }
-               }
-
-               public override Expression CreateExpressionTree (EmitContext ec)
-               {
-                       throw new NotSupportedException ("ET");
-               }
-
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        expr = expr.Resolve (ec);
                        if (expr == null)
@@ -1539,8 +1526,8 @@ namespace Mono.CSharp {
 
                        type = target.Type;
 
-                       if (type.IsAbstract && type.IsSealed) {
-                               Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
+                       if (type.IsStatic) {
+                               ec.Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
                                return null;
                        }
 
@@ -1553,23 +1540,18 @@ namespace Mono.CSharp {
                                        return c;
                        }
 
-                       if (type.IsPointer && !ec.InUnsafe) {
-                               UnsafeError (loc);
-                       } else if (TypeManager.IsDynamicType (expr.Type)) {
+                       if (type.IsPointer && !ec.IsUnsafe) {
+                               UnsafeError (ec, loc);
+                       } else if (expr.Type == InternalType.Dynamic) {
                                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 +1560,39 @@ namespace Mono.CSharp {
                        target.expr = expr.Clone (clonectx);
                }
        }
+
+       public class ImplicitCast : ShimExpression
+       {
+               bool arrayAccess;
+
+               public ImplicitCast (Expression expr, TypeSpec 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 (EmitContext ec, Location loc, Type t, bool expl)
-                       {
-                               Error_ValueCannotBeConvertedCore (ec, loc, t, expl);
-                       }
-               }
-
-
                Expression expr;
 
                public DefaultValueExpression (Expression expr, Location loc)
@@ -1606,15 +1601,15 @@ namespace Mono.CSharp {
                        this.loc = loc;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        TypeExpr texpr = expr.ResolveAsTypeTerminal (ec, false);
                        if (texpr == null)
@@ -1622,19 +1617,19 @@ 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");
+                       if (type.IsStatic) {
+                               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;
@@ -1645,15 +1640,10 @@ namespace Mono.CSharp {
                        LocalTemporary temp_storage = new LocalTemporary(type);
 
                        temp_storage.AddressOf(ec, AddressOp.LoadStore);
-                       ec.ig.Emit(OpCodes.Initobj, type);
+                       ec.Emit(OpCodes.Initobj, type);
                        temp_storage.Emit(ec);
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       type = storey.MutateType (type);
-               }
-               
                protected override void CloneTo (CloneContext clonectx, Expression t)
                {
                        DefaultValueExpression target = (DefaultValueExpression) t;
@@ -1667,29 +1657,28 @@ namespace Mono.CSharp {
        /// </summary>
        public class Binary : Expression, IDynamicBinder
        {
-
                protected class PredefinedOperator {
-                       protected readonly Type left;
-                       protected readonly Type right;
+                       protected readonly TypeSpec left;
+                       protected readonly TypeSpec right;
                        public readonly Operator OperatorsMask;
-                       public Type ReturnType;
+                       public TypeSpec ReturnType;
 
-                       public PredefinedOperator (Type ltype, Type rtype, Operator op_mask)
+                       public PredefinedOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask)
                                : this (ltype, rtype, op_mask, ltype)
                        {
                        }
 
-                       public PredefinedOperator (Type type, Operator op_mask, Type return_type)
+                       public PredefinedOperator (TypeSpec type, Operator op_mask, TypeSpec return_type)
                                : this (type, type, op_mask, return_type)
                        {
                        }
 
-                       public PredefinedOperator (Type type, Operator op_mask)
+                       public PredefinedOperator (TypeSpec type, Operator op_mask)
                                : this (type, type, op_mask, type)
                        {
                        }
 
-                       public PredefinedOperator (Type ltype, Type rtype, Operator op_mask, Type return_type)
+                       public PredefinedOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask, TypeSpec return_type)
                        {
                                if ((op_mask & Operator.ValuesOnlyMask) != 0)
                                        throw new InternalErrorException ("Only masked values can be used");
@@ -1700,7 +1689,7 @@ namespace Mono.CSharp {
                                this.ReturnType = return_type;
                        }
 
-                       public virtual Expression ConvertResult (EmitContext ec, Binary b)
+                       public virtual Expression ConvertResult (ResolveContext ec, Binary b)
                        {
                                b.type = ReturnType;
 
@@ -1715,10 +1704,28 @@ 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;
                        }
 
-                       public bool IsPrimitiveApplicable (Type ltype, Type rtype)
+                       public bool IsPrimitiveApplicable (TypeSpec ltype, TypeSpec rtype)
                        {
                                //
                                // We are dealing with primitive types only
@@ -1726,7 +1733,7 @@ namespace Mono.CSharp {
                                return left == ltype && ltype == rtype;
                        }
 
-                       public virtual bool IsApplicable (EmitContext ec, Expression lexpr, Expression rexpr)
+                       public virtual bool IsApplicable (ResolveContext ec, Expression lexpr, Expression rexpr)
                        {
                                if (TypeManager.IsEqual (left, lexpr.Type) &&
                                        TypeManager.IsEqual (right, rexpr.Type))
@@ -1736,7 +1743,7 @@ namespace Mono.CSharp {
                                        Convert.ImplicitConversionExists (ec, rexpr, right);
                        }
 
-                       public PredefinedOperator ResolveBetterOperator (EmitContext ec, PredefinedOperator best_operator)
+                       public PredefinedOperator ResolveBetterOperator (ResolveContext ec, PredefinedOperator best_operator)
                        {
                                int result = 0;
                                if (left != null && best_operator.left != null) {
@@ -1758,19 +1765,19 @@ namespace Mono.CSharp {
                }
 
                class PredefinedStringOperator : PredefinedOperator {
-                       public PredefinedStringOperator (Type type, Operator op_mask)
+                       public PredefinedStringOperator (TypeSpec type, Operator op_mask)
                                : base (type, op_mask, type)
                        {
                                ReturnType = TypeManager.string_type;
                        }
 
-                       public PredefinedStringOperator (Type ltype, Type rtype, Operator op_mask)
+                       public PredefinedStringOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask)
                                : base (ltype, rtype, op_mask)
                        {
                                ReturnType = TypeManager.string_type;
                        }
 
-                       public override Expression ConvertResult (EmitContext ec, Binary b)
+                       public override Expression ConvertResult (ResolveContext ec, Binary b)
                        {
                                //
                                // Use original expression for nullable arguments
@@ -1789,21 +1796,21 @@ 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);
                        }
                }
 
                class PredefinedShiftOperator : PredefinedOperator {
-                       public PredefinedShiftOperator (Type ltype, Operator op_mask) :
+                       public PredefinedShiftOperator (TypeSpec ltype, Operator op_mask) :
                                base (ltype, TypeManager.int32_type, op_mask)
                        {
                        }
 
-                       public override Expression ConvertResult (EmitContext ec, Binary b)
+                       public override Expression ConvertResult (ResolveContext ec, Binary b)
                        {
                                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,34 +1818,42 @@ 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;
                        }
                }
 
                class PredefinedPointerOperator : PredefinedOperator {
-                       public PredefinedPointerOperator (Type ltype, Type rtype, Operator op_mask)
+                       public PredefinedPointerOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask)
                                : base (ltype, rtype, op_mask)
                        {
                        }
 
-                       public PredefinedPointerOperator (Type ltype, Type rtype, Operator op_mask, Type retType)
+                       public PredefinedPointerOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask, TypeSpec retType)
                                : base (ltype, rtype, op_mask, retType)
                        {
                        }
 
-                       public PredefinedPointerOperator (Type type, Operator op_mask, Type return_type)
+                       public PredefinedPointerOperator (TypeSpec type, Operator op_mask, TypeSpec return_type)
                                : base (type, op_mask, return_type)
                        {
                        }
 
-                       public override bool IsApplicable (EmitContext ec, Expression lexpr, Expression rexpr)
+                       public override bool IsApplicable (ResolveContext ec, Expression lexpr, Expression rexpr)
                        {
                                if (left == null) {
                                        if (!lexpr.Type.IsPointer)
@@ -1859,7 +1874,7 @@ namespace Mono.CSharp {
                                return true;
                        }
 
-                       public override Expression ConvertResult (EmitContext ec, Binary b)
+                       public override Expression ConvertResult (ResolveContext ec, Binary b)
                        {
                                if (left != null) {
                                        b.left = EmptyCast.Create (b.left, left);
@@ -1867,7 +1882,7 @@ namespace Mono.CSharp {
                                        b.right = EmptyCast.Create (b.right, right);
                                }
 
-                               Type r_type = ReturnType;
+                               TypeSpec r_type = ReturnType;
                                Expression left_arg, right_arg;
                                if (r_type == null) {
                                        if (left == null) {
@@ -1933,21 +1948,21 @@ namespace Mono.CSharp {
                readonly bool is_compound;
                Expression enum_conversion;
 
-               static PredefinedOperator [] standard_operators;
-               static PredefinedOperator [] pointer_operators;
+               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 +2043,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);
                }
 
                //
@@ -2138,14 +2153,13 @@ namespace Mono.CSharp {
                        return CSharp.Operator.GetMetadataName (op_type);
                }
 
-               public static void EmitOperatorOpcode (EmitContext ec, Operator oper, Type l)
+               public static void EmitOperatorOpcode (EmitContext ec, Operator oper, TypeSpec l)
                {
                        OpCode opcode;
-                       ILGenerator ig = ec.ig;
 
                        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 +2186,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 +2198,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))
@@ -2211,8 +2225,8 @@ namespace Mono.CSharp {
                                break;
 
                        case Operator.Inequality:
-                               ig.Emit (OpCodes.Ceq);
-                               ig.Emit (OpCodes.Ldc_I4_0);
+                               ec.Emit (OpCodes.Ceq);
+                               ec.Emit (OpCodes.Ldc_I4_0);
                                
                                opcode = OpCodes.Ceq;
                                break;
@@ -2233,21 +2247,21 @@ namespace Mono.CSharp {
 
                        case Operator.LessThanOrEqual:
                                if (IsUnsigned (l) || IsFloat (l))
-                                       ig.Emit (OpCodes.Cgt_Un);
+                                       ec.Emit (OpCodes.Cgt_Un);
                                else
-                                       ig.Emit (OpCodes.Cgt);
-                               ig.Emit (OpCodes.Ldc_I4_0);
+                                       ec.Emit (OpCodes.Cgt);
+                               ec.Emit (OpCodes.Ldc_I4_0);
                                
                                opcode = OpCodes.Ceq;
                                break;
 
                        case Operator.GreaterThanOrEqual:
                                if (IsUnsigned (l) || IsFloat (l))
-                                       ig.Emit (OpCodes.Clt_Un);
+                                       ec.Emit (OpCodes.Clt_Un);
                                else
-                                       ig.Emit (OpCodes.Clt);
+                                       ec.Emit (OpCodes.Clt);
                                
-                               ig.Emit (OpCodes.Ldc_I4_0);
+                               ec.Emit (OpCodes.Ldc_I4_0);
                                
                                opcode = OpCodes.Ceq;
                                break;
@@ -2268,10 +2282,10 @@ namespace Mono.CSharp {
                                throw new InternalErrorException (oper.ToString ());
                        }
 
-                       ig.Emit (opcode);
+                       ec.Emit (opcode);
                }
 
-               static bool IsUnsigned (Type t)
+               static bool IsUnsigned (TypeSpec t)
                {
                        if (t.IsPointer)
                                return true;
@@ -2280,15 +2294,20 @@ namespace Mono.CSharp {
                                t == TypeManager.ushort_type || t == TypeManager.byte_type);
                }
 
-               static bool IsFloat (Type t)
+               static bool IsFloat (TypeSpec t)
                {
                        return t == TypeManager.float_type || t == TypeManager.double_type;
                }
 
-               Expression ResolveOperator (EmitContext ec)
+               public static void Reset ()
+               {
+                       pointer_operators = standard_operators = null;
+               }
+
+               Expression ResolveOperator (ResolveContext ec)
                {
-                       Type l = left.Type;
-                       Type r = right.Type;
+                       TypeSpec l = left.Type;
+                       TypeSpec r = right.Type;
                        Expression expr;
                        bool primitives_only = false;
 
@@ -2350,7 +2369,7 @@ namespace Mono.CSharp {
 
                // at least one of 'left' or 'right' is an enumeration constant (EnumConstant or SideEffectConstant or ...)
                // if 'left' is not an enumeration constant, create one from the type of 'right'
-               Constant EnumLiftUp (EmitContext ec, Constant left, Constant right, Location loc)
+               Constant EnumLiftUp (ResolveContext ec, Constant left, Constant right, Location loc)
                {
                        switch (oper) {
                        case Operator.BitwiseOr:
@@ -2383,14 +2402,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 +2428,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,13 +2466,13 @@ 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 ();
-                       Type bool_type = TypeManager.bool_type;
+                       var temp = new List<PredefinedOperator> ();
+                       TypeSpec bool_type = TypeManager.bool_type;
 
                        temp.Add (new PredefinedOperator (TypeManager.int32_type, Operator.ArithmeticMask | Operator.BitwiseMask));
                        temp.Add (new PredefinedOperator (TypeManager.uint32_type, Operator.ArithmeticMask | Operator.BitwiseMask));
@@ -2485,20 +2504,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, TypeSpec type)
                {
                        Expression temp;
-                       Type etype;
+                       TypeSpec 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 +2532,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 +2545,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;
                        }
 
@@ -2541,25 +2560,25 @@ namespace Mono.CSharp {
                //
                // 7.2.6.2 Binary numeric promotions
                //
-               public bool DoBinaryOperatorPromotion (EmitContext ec)
+               public bool DoBinaryOperatorPromotion (ResolveContext ec)
                {
-                       Type ltype = left.Type;
-                       Type rtype = right.Type;
+                       TypeSpec ltype = left.Type;
+                       TypeSpec rtype = right.Type;
                        Expression temp;
 
-                       foreach (Type t in ConstantFold.binary_promotions) {
+                       foreach (TypeSpec t in ConstantFold.BinaryPromotionsTypes) {
                                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;
+                       TypeSpec 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 +2590,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 +2602,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (left == null)
                                return null;
@@ -2595,7 +2614,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 +2632,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,46 +2644,32 @@ 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)) {
+                       if (left.Type == InternalType.Dynamic || right.Type == InternalType.Dynamic) {
                                Arguments args = new Arguments (2);
                                args.Add (new Argument (left));
                                args.Add (new Argument (right));
@@ -2681,25 +2686,67 @@ namespace Mono.CSharp {
                        return DoResolveCore (ec, left, right);
                }
 
-               protected Expression DoResolveCore (EmitContext ec, Expression left_orig, Expression right_orig)
+               protected Expression DoResolveCore (ResolveContext ec, Expression left_orig, Expression right_orig)
                {
                        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 void MutateHoistedGenericType (AnonymousMethodStorey storey)
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       left.MutateHoistedGenericType (storey);
-                       right.MutateHoistedGenericType (storey);
+                       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 ());
+                       }
                }
 
                //
@@ -2708,10 +2755,10 @@ namespace Mono.CSharp {
                // bool operator == (D x, D y)
                // bool operator != (D x, D y)
                //
-               Expression ResolveOperatorDelegate (EmitContext ec, Type l, Type r)
+               Expression ResolveOperatorDelegate (ResolveContext ec, TypeSpec l, TypeSpec r)
                {
                        bool is_equality = (oper & Operator.EqualityMask) != 0;
-                       if (!TypeManager.IsEqual (l, r) && !TypeManager.IsVariantOf (r, l)) {
+                       if (!TypeManager.IsEqual (l, r) && !TypeSpecComparer.Variant.IsEqual (r, l)) {
                                Expression tmp;
                                if (right.eclass == ExprClass.MethodGroup || (r == InternalType.AnonymousMethod && !is_equality)) {
                                        tmp = Convert.ImplicitConversionRequired (ec, right, l, loc);
@@ -2736,7 +2783,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 +2804,10 @@ namespace Mono.CSharp {
                                method = TypeManager.delegate_remove_delegate_delegate;
                        }
 
-                       MethodGroupExpr mg = new MethodGroupExpr (new MemberInfo [] { method }, TypeManager.delegate_type, loc);
+                       if (method == null)
+                               return new EmptyExpression (TypeManager.decimal_type);
+
+                       MethodGroupExpr mg = new MethodGroupExpr (method, TypeManager.delegate_type, loc);
                        mg = mg.OverloadResolve (ec, ref args, false, loc);
 
                        return new ClassCast (new UserOperatorCall (mg, args, CreateExpressionTree, loc), l);
@@ -2766,7 +2816,7 @@ namespace Mono.CSharp {
                //
                // Enumeration operators
                //
-               Expression ResolveOperatorEnum (EmitContext ec, bool lenum, bool renum, Type ltype, Type rtype)
+               Expression ResolveOperatorEnum (ResolveContext ec, bool lenum, bool renum, TypeSpec ltype, TypeSpec rtype)
                {
                        //
                        // bool operator == (E x, E y);
@@ -2787,15 +2837,16 @@ 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;
                        Expression rtemp = right;
-                       Type underlying_type;
+                       TypeSpec 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) {
@@ -2812,19 +2863,19 @@ namespace Mono.CSharp {
                        }                       
 
                        if (TypeManager.IsEqual (ltype, rtype)) {
-                               underlying_type = TypeManager.GetEnumUnderlyingType (ltype);
+                               underlying_type = EnumSpec.GetUnderlyingType (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) {
-                               underlying_type = TypeManager.GetEnumUnderlyingType (ltype);
+                               underlying_type = EnumSpec.GetUnderlyingType (ltype);
 
                                if (oper != Operator.Subtraction && oper != Operator.Addition) {
                                        Constant c = right as Constant;
@@ -2838,12 +2889,12 @@ 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);
 
                        } else if (renum) {
-                               underlying_type = TypeManager.GetEnumUnderlyingType (rtype);
+                               underlying_type = EnumSpec.GetUnderlyingType (rtype);
 
                                if (oper != Operator.Addition) {
                                        Constant c = left as Constant;
@@ -2857,7 +2908,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);
 
@@ -2875,9 +2926,9 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       Type res_type = null;
+                       TypeSpec res_type = null;
                        if ((oper & Operator.BitwiseMask) != 0 || oper == Operator.Subtraction || oper == Operator.Addition) {
-                               Type promoted_type = lenum ? left.Type : right.Type;
+                               TypeSpec promoted_type = lenum ? left.Type : right.Type;
                                enum_conversion = Convert.ExplicitNumericConversion (
                                        new EmptyExpression (promoted_type), underlying_type);
 
@@ -2894,23 +2945,35 @@ 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;
                }
 
                //
                // 7.9.6 Reference type equality operators
                //
-               Binary ResolveOperatorEqualityRerefence (EmitContext ec, Type l, Type r)
+               Binary ResolveOperatorEqualityRerefence (ResolveContext ec, TypeSpec l, TypeSpec r)
                {
                        //
                        // operator != (object a, object b)
@@ -2923,12 +2986,14 @@ namespace Mono.CSharp {
                                return null;
 
                        type = TypeManager.bool_type;
-                       GenericConstraints constraints;
 
-                       bool lgen = TypeManager.IsGenericParameter (l);
+                       var lgen = l as TypeParameterSpec;
+
+                       if (l == r) {
+                               if (l is InternalType)
+                                       return null;
 
-                       if (TypeManager.IsEqual (l, r)) {
-                               if (lgen) {
+                               if (lgen != null) {
                                        //
                                        // Only allow to compare same reference type parameter
                                        //
@@ -2941,16 +3006,13 @@ namespace Mono.CSharp {
                                        return null;
                                }
 
-                               if (l == InternalType.AnonymousMethod)
-                                       return null;
-
                                if (TypeManager.IsValueType (l))
                                        return null;
 
                                return this;
                        }
 
-                       bool rgen = TypeManager.IsGenericParameter (r);
+                       var rgen = r as TypeParameterSpec;
 
                        //
                        // a, Both operands are reference-type values or the value null
@@ -2959,18 +3021,16 @@ namespace Mono.CSharp {
                        // value type constrain
                        //
                        if (left is NullLiteral || right is NullLiteral) {
-                               if (lgen) {
-                                       constraints = TypeManager.GetTypeParameterConstraints (l);
-                                       if (constraints != null && constraints.HasValueTypeConstraint)
+                               if (lgen != null) {
+                                       if (lgen.HasSpecialStruct)
                                                return null;
 
                                        left = new BoxedCast (left, TypeManager.object_type);
                                        return this;
                                }
 
-                               if (rgen) {
-                                       constraints = TypeManager.GetTypeParameterConstraints (r);
-                                       if (constraints != null && constraints.HasValueTypeConstraint)
+                               if (rgen != null) {
+                                       if (rgen.HasSpecialStruct)
                                                return null;
 
                                        right = new BoxedCast (right, TypeManager.object_type);
@@ -2983,20 +3043,24 @@ namespace Mono.CSharp {
                        // standard conversion is applied. It's not clear from the
                        // standard but it looks like it works like that.
                        //
-                       if (lgen) {
+                       if (lgen != null) {
                                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)) {
                                return null;
                        }
 
-                       if (rgen) {
+                       if (rgen != null) {
                                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 +3077,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;
                        }
@@ -3029,7 +3093,7 @@ namespace Mono.CSharp {
                }
 
 
-               Expression ResolveOperatorPointer (EmitContext ec, Type l, Type r)
+               Expression ResolveOperatorPointer (ResolveContext ec, TypeSpec l, TypeSpec r)
                {
                        //
                        // bool operator == (void* x, void* y);
@@ -3068,11 +3132,11 @@ namespace Mono.CSharp {
                //
                // Build-in operators method overloading
                //
-               protected virtual Expression ResolveOperatorPredefined (EmitContext ec, PredefinedOperator [] operators, bool primitives_only, Type enum_type)
+               protected virtual Expression ResolveOperatorPredefined (ResolveContext ec, PredefinedOperator [] operators, bool primitives_only, TypeSpec enum_type)
                {
                        PredefinedOperator best_operator = null;
-                       Type l = left.Type;
-                       Type r = right.Type;
+                       TypeSpec l = left.Type;
+                       TypeSpec r = right.Type;
                        Operator oper_mask = oper & ~Operator.ValuesOnlyMask;
 
                        foreach (PredefinedOperator po in operators) {
@@ -3098,8 +3162,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 +3174,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;
 
@@ -3123,7 +3206,7 @@ namespace Mono.CSharp {
                //
                // Performs user-operator overloading
                //
-               protected virtual Expression ResolveUserOperator (EmitContext ec, Type l, Type r)
+               protected virtual Expression ResolveUserOperator (ResolveContext ec, TypeSpec l, TypeSpec r)
                {
                        Operator user_oper;
                        if (oper == Operator.LogicalAnd)
@@ -3135,11 +3218,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 = MethodLookup (ec.Compiler, ec.CurrentType, l, MemberKind.Operator, op, 0, loc);
                        MethodGroupExpr right_operators = null;
 
                        if (!TypeManager.IsEqual (r, l)) {
-                               right_operators = MemberLookup (ec.CurrentType, r, op, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
+                               right_operators = MethodLookup (ec.Compiler, ec.CurrentType, r, MemberKind.Operator, op, 0, loc);
                                if (right_operators == null && left_operators == null)
                                        return null;
                        } else if (left_operators == null) {
@@ -3198,9 +3281,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
@@ -3216,12 +3299,12 @@ namespace Mono.CSharp {
                        return oper_expr;
                }
 
-               public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
+               public override TypeExpr ResolveAsTypeTerminal (IMemberContext ec, bool silent)
                {
                        return null;
                }
 
-               private void CheckUselessComparison (Constant c, Type type)
+               private void CheckUselessComparison (ResolveContext ec, Constant c, TypeSpec type)
                {
                        if (c == null || !IsTypeIntegral (type)
                                || c is StringConstant
@@ -3245,7 +3328,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,10 +3354,10 @@ namespace Mono.CSharp {
                                return;
 
                        if (IsValueOutOfRange (value, type))
-                               WarnUselessComparison (type);
+                               WarnUselessComparison (ec, type);
                }
 
-               static bool IsValueOutOfRange (long value, Type type)
+               static bool IsValueOutOfRange (long value, TypeSpec type)
                {
                        if (IsTypeUnsigned (type) && value < 0)
                                return true;
@@ -3286,13 +3369,13 @@ namespace Mono.CSharp {
                                type == TypeManager.uint32_type && value >= 0x100000000;
                }
 
-               static bool IsBuildInEqualityOperator (Type t)
+               static bool IsBuildInEqualityOperator (TypeSpec t)
                {
                        return t == TypeManager.object_type || t == TypeManager.string_type ||
                                t == TypeManager.delegate_type || TypeManager.IsDelegateType (t);
                }
 
-               static bool IsPredefinedUserOperator (Type t, Operator op)
+               static bool IsPredefinedUserOperator (TypeSpec t, Operator op)
                {
                        //
                        // Some predefined types have user operators
@@ -3300,7 +3383,7 @@ namespace Mono.CSharp {
                        return (op & Operator.EqualityMask) != 0 && (t == TypeManager.string_type || t == TypeManager.decimal_type);
                }
 
-               private static bool IsTypeIntegral (Type type)
+               private static bool IsTypeIntegral (TypeSpec type)
                {
                        return type == TypeManager.uint64_type ||
                                type == TypeManager.int64_type ||
@@ -3313,7 +3396,7 @@ namespace Mono.CSharp {
                                type == TypeManager.char_type;
                }
 
-               private static bool IsTypeUnsigned (Type type)
+               private static bool IsTypeUnsigned (TypeSpec type)
                {
                        return type == TypeManager.uint64_type ||
                                type == TypeManager.uint32_type ||
@@ -3322,9 +3405,9 @@ namespace Mono.CSharp {
                                type == TypeManager.char_type;
                }
 
-               private void WarnUselessComparison (Type type)
+               private void WarnUselessComparison (ResolveContext ec, TypeSpec 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));
                }
 
@@ -3338,8 +3421,6 @@ namespace Mono.CSharp {
                /// </remarks>
                public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
                {
-                       ILGenerator ig = ec.ig;
-
                        //
                        // This is more complicated than it looks, but its just to avoid
                        // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
@@ -3371,11 +3452,11 @@ namespace Mono.CSharp {
                        } else if (oper == Operator.LogicalAnd) {
 
                                if (on_true) {
-                                       Label tests_end = ig.DefineLabel ();
+                                       Label tests_end = ec.DefineLabel ();
                                        
                                        left.EmitBranchable (ec, tests_end, false);
                                        right.EmitBranchable (ec, target, true);
-                                       ig.MarkLabel (tests_end);                                       
+                                       ec.MarkLabel (tests_end);                                       
                                } else {
                                        //
                                        // This optimizes code like this 
@@ -3396,10 +3477,10 @@ namespace Mono.CSharp {
                                        right.EmitBranchable (ec, target, true);
                                        
                                } else {
-                                       Label tests_end = ig.DefineLabel ();
+                                       Label tests_end = ec.DefineLabel ();
                                        left.EmitBranchable (ec, tests_end, true);
                                        right.EmitBranchable (ec, target, false);
-                                       ig.MarkLabel (tests_end);
+                                       ec.MarkLabel (tests_end);
                                }
                                
                                return;
@@ -3414,76 +3495,76 @@ namespace Mono.CSharp {
                        left.Emit (ec);
                        right.Emit (ec);
 
-                       Type t = left.Type;
+                       TypeSpec t = left.Type;
                        bool is_float = IsFloat (t);
                        bool is_unsigned = is_float || IsUnsigned (t);
                        
                        switch (oper){
                        case Operator.Equality:
                                if (on_true)
-                                       ig.Emit (OpCodes.Beq, target);
+                                       ec.Emit (OpCodes.Beq, target);
                                else
-                                       ig.Emit (OpCodes.Bne_Un, target);
+                                       ec.Emit (OpCodes.Bne_Un, target);
                                break;
 
                        case Operator.Inequality:
                                if (on_true)
-                                       ig.Emit (OpCodes.Bne_Un, target);
+                                       ec.Emit (OpCodes.Bne_Un, target);
                                else
-                                       ig.Emit (OpCodes.Beq, target);
+                                       ec.Emit (OpCodes.Beq, target);
                                break;
 
                        case Operator.LessThan:
                                if (on_true)
                                        if (is_unsigned && !is_float)
-                                               ig.Emit (OpCodes.Blt_Un, target);
+                                               ec.Emit (OpCodes.Blt_Un, target);
                                        else
-                                               ig.Emit (OpCodes.Blt, target);
+                                               ec.Emit (OpCodes.Blt, target);
                                else
                                        if (is_unsigned)
-                                               ig.Emit (OpCodes.Bge_Un, target);
+                                               ec.Emit (OpCodes.Bge_Un, target);
                                        else
-                                               ig.Emit (OpCodes.Bge, target);
+                                               ec.Emit (OpCodes.Bge, target);
                                break;
 
                        case Operator.GreaterThan:
                                if (on_true)
                                        if (is_unsigned && !is_float)
-                                               ig.Emit (OpCodes.Bgt_Un, target);
+                                               ec.Emit (OpCodes.Bgt_Un, target);
                                        else
-                                               ig.Emit (OpCodes.Bgt, target);
+                                               ec.Emit (OpCodes.Bgt, target);
                                else
                                        if (is_unsigned)
-                                               ig.Emit (OpCodes.Ble_Un, target);
+                                               ec.Emit (OpCodes.Ble_Un, target);
                                        else
-                                               ig.Emit (OpCodes.Ble, target);
+                                               ec.Emit (OpCodes.Ble, target);
                                break;
 
                        case Operator.LessThanOrEqual:
                                if (on_true)
                                        if (is_unsigned && !is_float)
-                                               ig.Emit (OpCodes.Ble_Un, target);
+                                               ec.Emit (OpCodes.Ble_Un, target);
                                        else
-                                               ig.Emit (OpCodes.Ble, target);
+                                               ec.Emit (OpCodes.Ble, target);
                                else
                                        if (is_unsigned)
-                                               ig.Emit (OpCodes.Bgt_Un, target);
+                                               ec.Emit (OpCodes.Bgt_Un, target);
                                        else
-                                               ig.Emit (OpCodes.Bgt, target);
+                                               ec.Emit (OpCodes.Bgt, target);
                                break;
 
 
                        case Operator.GreaterThanOrEqual:
                                if (on_true)
                                        if (is_unsigned && !is_float)
-                                               ig.Emit (OpCodes.Bge_Un, target);
+                                               ec.Emit (OpCodes.Bge_Un, target);
                                        else
-                                               ig.Emit (OpCodes.Bge, target);
+                                               ec.Emit (OpCodes.Bge, target);
                                else
                                        if (is_unsigned)
-                                               ig.Emit (OpCodes.Blt_Un, target);
+                                               ec.Emit (OpCodes.Blt_Un, target);
                                        else
-                                               ig.Emit (OpCodes.Blt, target);
+                                               ec.Emit (OpCodes.Blt, target);
                                break;
                        default:
                                throw new InternalErrorException (oper.ToString ());
@@ -3495,43 +3576,40 @@ namespace Mono.CSharp {
                        EmitOperator (ec, left.Type);
                }
 
-               protected virtual void EmitOperator (EmitContext ec, Type l)
+               protected virtual void EmitOperator (EmitContext ec, TypeSpec l)
                {
-                       ILGenerator ig = ec.ig;
-
                        //
                        // Handle short-circuit operators differently
                        // than the rest
                        //
                        if ((oper & Operator.LogicalMask) != 0) {
-                               Label load_result = ig.DefineLabel ();
-                               Label end = ig.DefineLabel ();
+                               Label load_result = ec.DefineLabel ();
+                               Label end = ec.DefineLabel ();
 
                                bool is_or = oper == Operator.LogicalOr;
                                left.EmitBranchable (ec, load_result, is_or);
                                right.Emit (ec);
-                               ig.Emit (OpCodes.Br_S, end);
+                               ec.Emit (OpCodes.Br_S, end);
                                
-                               ig.MarkLabel (load_result);
-                               ig.Emit (is_or ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
-                               ig.MarkLabel (end);
+                               ec.MarkLabel (load_result);
+                               ec.Emit (is_or ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
+                               ec.MarkLabel (end);
                                return;
                        }
 
-                       left.Emit (ec);
-
-                       //
-                       // Optimize zero-based operations
                        //
-                       // TODO: Implement more optimizations, but it should probably go to PredefinedOperators
+                       // Optimize zero-based operations which cannot be optimized at expression level
                        //
-                       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);
+                                       ec.Emit (OpCodes.Neg);
                                        return;
                                }
                        }
 
+                       left.Emit (ec);
                        right.Emit (ec);
                        EmitOperatorOpcode (ec, oper, l);
 
@@ -3546,7 +3624,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);
@@ -3562,38 +3640,41 @@ namespace Mono.CSharp {
                        target.right = right.Clone (clonectx);
                }
 
-               public Expression CreateCallSiteBinder (EmitContext ec, Arguments args)
+               public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
                {
                        Arguments binder_args = new Arguments (4);
 
                        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 (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        return CreateExpressionTree (ec, null);
                }
 
-               Expression CreateExpressionTree (EmitContext ec, MethodGroupExpr method)                
+               Expression CreateExpressionTree (ResolveContext ec, MethodGroupExpr method)             
                {
                        string method_name;
                        bool lift_arg = false;
                        
                        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 +3728,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 +3737,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 +3757,7 @@ namespace Mono.CSharp {
                                args.Add (new Argument (method.CreateExpressionTree (ec)));
                        }
                        
-                       return CreateExpressionFactoryCall (method_name, args);
+                       return CreateExpressionFactoryCall (ec, method_name, args);
                }
        }
        
@@ -3687,18 +3768,27 @@ 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 override Expression CreateExpressionTree (EmitContext ec)
+               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)
                {
                        Argument arg = arguments [0];
                        return CreateExpressionAddCall (ec, arg, arg.CreateExpressionTree (ec), 1);
@@ -3707,7 +3797,7 @@ namespace Mono.CSharp {
                //
                // Creates nested calls tree from an array of arguments used for IL emit
                //
-               Expression CreateExpressionAddCall (EmitContext ec, Argument left, Expression left_etree, int pos)
+               Expression CreateExpressionAddCall (ResolveContext ec, Argument left, Expression left_etree, int pos)
                {
                        Arguments concat_args = new Arguments (2);
                        Arguments add_args = new Arguments (3);
@@ -3728,20 +3818,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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return this;
                }
                
-               public void Append (Expression operand)
+               void Append (ResolveContext rc, Expression operand)
                {
                        //
                        // Constant folding
@@ -3753,7 +3843,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;
                                        }
                                }
@@ -3779,15 +3869,19 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        Expression concat = new Invocation (CreateConcatMemberExpression (), arguments, true);
-                       concat = concat.Resolve (ec);
+                       concat = concat.Resolve (new ResolveContext (ec.MemberContext));
                        if (concat != null)
                                concat.Emit (ec);
                }
-               
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       arguments.MutateHoistedGenericType (storey);
-               }               
+                       if (arguments.Count != 2)
+                               throw new NotImplementedException ("arguments.Count != 2");
+
+                       var concat = typeof (string).GetMethod ("Concat", new[] { typeof (object), typeof (object) });
+                       return SLE.Expression.Add (arguments[0].Expr.MakeExpression (ctx), arguments[1].Expr.MakeExpression (ctx), concat);
+               }
        }
 
        //
@@ -3802,15 +3896,16 @@ namespace Mono.CSharp {
                        : base (oper_method, arguments, expr_tree, loc)
                {
                        this.is_and = is_and;
+                       eclass = ExprClass.Unresolved;
                }
                
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       MethodInfo method = (MethodInfo)mg;
-                       type = TypeManager.TypeToCoreType (method.ReturnType);
-                       AParametersCollection pd = TypeManager.GetParameterData (method);
+                       var method = mg.BestCandidate;
+                       type = method.ReturnType;
+                       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));
                                return null;
@@ -3820,7 +3915,7 @@ 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));
                                return null;
@@ -3833,19 +3928,18 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-                       Label end_target = ig.DefineLabel ();
+                       Label end_target = ec.DefineLabel ();
 
                        //
                        // Emit and duplicate left argument
                        //
                        arguments [0].Expr.Emit (ec);
-                       ig.Emit (OpCodes.Dup);
+                       ec.Emit (OpCodes.Dup);
                        arguments.RemoveAt (0);
 
                        oper.EmitBranchable (ec, end_target, true);
                        base.Emit (ec);
-                       ig.MarkLabel (end_target);
+                       ec.MarkLabel (end_target);
                }
        }
 
@@ -3856,7 +3950,7 @@ namespace Mono.CSharp {
                //
                // We assume that `l' is always a pointer
                //
-               public PointerArithmetic (Binary.Operator op, Expression l, Expression r, Type t, Location loc)
+               public PointerArithmetic (Binary.Operator op, Expression l, Expression r, TypeSpec t, Location loc)
                {
                        type = t;
                        this.loc = loc;
@@ -3865,18 +3959,18 @@ namespace Mono.CSharp {
                        this.op = op;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       Error_PointerInsideExpressionTree ();
+                       Error_PointerInsideExpressionTree (ec);
                        return null;
                }
 
-               public override Expression DoResolve (EmitContext 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;
                        }
                        
@@ -3885,23 +3979,22 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       Type op_type = left.Type;
-                       ILGenerator ig = ec.ig;
+                       TypeSpec op_type = left.Type;
                        
                        // It must be either array or fixed buffer
-                       Type element;
+                       TypeSpec element;
                        if (TypeManager.HasElementType (op_type)) {
                                element = TypeManager.GetElementType (op_type);
                        } else {
                                FieldExpr fe = left as FieldExpr;
                                if (fe != null)
-                                       element = AttributeTester.GetFixedBuffer (fe.FieldInfo).ElementType;
+                                       element = ((FixedFieldSpec) (fe.Spec)).ElementType;
                                else
                                        element = op_type;
                        }
 
                        int size = GetTypeSize (element);
-                       Type rtype = right.Type;
+                       TypeSpec rtype = right.Type;
                        
                        if ((op & Binary.Operator.SubtractionMask) != 0 && rtype.IsPointer){
                                //
@@ -3909,16 +4002,16 @@ namespace Mono.CSharp {
                                //
                                left.Emit (ec);
                                right.Emit (ec);
-                               ig.Emit (OpCodes.Sub);
+                               ec.Emit (OpCodes.Sub);
 
                                if (size != 1){
                                        if (size == 0)
-                                               ig.Emit (OpCodes.Sizeof, element);
+                                               ec.Emit (OpCodes.Sizeof, element);
                                        else 
-                                               IntLiteral.EmitInt (ig, size);
-                                       ig.Emit (OpCodes.Div);
+                                               ec.EmitInt (size);
+                                       ec.Emit (OpCodes.Div);
                                }
-                               ig.Emit (OpCodes.Conv_I8);
+                               ec.Emit (OpCodes.Conv_I8);
                        } else {
                                //
                                // handle + and - on (pointer op int)
@@ -3937,7 +4030,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,54 +4038,118 @@ namespace Mono.CSharp {
                                        if (right_const.IsDefaultValue)
                                                return;
 
-                                       if (size != 0) {
-                                               right = ConstantFold.BinaryFold (ec, 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);
                                if (rtype == TypeManager.sbyte_type || rtype == TypeManager.byte_type ||
                                        rtype == TypeManager.short_type || rtype == TypeManager.ushort_type) {
-                                       ig.Emit (OpCodes.Conv_I);
+                                       ec.Emit (OpCodes.Conv_I);
                                } else if (rtype == TypeManager.uint32_type) {
-                                       ig.Emit (OpCodes.Conv_U);
+                                       ec.Emit (OpCodes.Conv_U);
                                }
 
                                if (right_const == null && size != 1){
                                        if (size == 0)
-                                               ig.Emit (OpCodes.Sizeof, element);
+                                               ec.Emit (OpCodes.Sizeof, element);
                                        else 
-                                               IntLiteral.EmitInt (ig, size);
+                                               ec.EmitInt (size);
                                        if (rtype == TypeManager.int64_type || rtype == TypeManager.uint64_type)
-                                               ig.Emit (OpCodes.Conv_I8);
+                                               ec.Emit (OpCodes.Conv_I8);
 
                                        Binary.EmitOperatorOpcode (ec, Binary.Operator.Multiply, rtype);
                                }
 
                                if (left_const == null) {
                                        if (rtype == TypeManager.int64_type)
-                                               ig.Emit (OpCodes.Conv_I);
+                                               ec.Emit (OpCodes.Conv_I);
                                        else if (rtype == TypeManager.uint64_type)
-                                               ig.Emit (OpCodes.Conv_U);
+                                               ec.Emit (OpCodes.Conv_U);
 
                                        Binary.EmitOperatorOpcode (ec, op, op_type);
                                }
                        }
                }
        }
+
+       //
+       // 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 (expr.Type == InternalType.Dynamic) {
+                               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;
@@ -4018,24 +4175,18 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        Arguments args = new Arguments (3);
                        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 (EmitContext 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);
 
@@ -4043,8 +4194,8 @@ namespace Mono.CSharp {
                                return null;
 
                        eclass = ExprClass.Value;
-                       Type true_type = true_expr.Type;
-                       Type false_type = false_expr.Type;
+                       TypeSpec true_type = true_expr.Type;
+                       TypeSpec false_type = false_expr.Type;
                        type = true_type;
 
                        //
@@ -4055,14 +4206,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;
@@ -4070,9 +4219,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;
                                }
                        }                       
@@ -4081,46 +4230,37 @@ 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);
                        }
 
                        return this;
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       expr.MutateHoistedGenericType (storey);
-                       true_expr.MutateHoistedGenericType (storey);
-                       false_expr.MutateHoistedGenericType (storey);
-                       type = storey.MutateType (type);
-               }
-
-               public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
+               public override TypeExpr ResolveAsTypeTerminal (IMemberContext ec, bool silent)
                {
                        return null;
                }
 
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-                       Label false_target = ig.DefineLabel ();
-                       Label end_target = ig.DefineLabel ();
+                       Label false_target = ec.DefineLabel ();
+                       Label end_target = ec.DefineLabel ();
 
                        expr.EmitBranchable (ec, false_target, false);
                        true_expr.Emit (ec);
 
                        if (type.IsInterface) {
                                LocalBuilder temp = ec.GetTemporaryLocal (type);
-                               ig.Emit (OpCodes.Stloc, temp);
-                               ig.Emit (OpCodes.Ldloc, temp);
+                               ec.Emit (OpCodes.Stloc, temp);
+                               ec.Emit (OpCodes.Ldloc, temp);
                                ec.FreeTemporaryLocal (temp, type);
                        }
 
-                       ig.Emit (OpCodes.Br, end_target);
-                       ig.MarkLabel (false_target);
+                       ec.Emit (OpCodes.Br, end_target);
+                       ec.MarkLabel (false_target);
                        false_expr.Emit (ec);
-                       ig.MarkLabel (end_target);
+                       ec.MarkLabel (end_target);
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -4137,7 +4277,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; }
@@ -4154,7 +4294,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) {
@@ -4165,6 +4305,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);
@@ -4203,11 +4358,11 @@ namespace Mono.CSharp {
                                // If we are a reference, we loaded on the stack a pointer
                                // Now lets load the real value
                                //
-                               LoadFromPtr (ec.ig, type);
+                               ec.EmitLoadFromPtr (type);
                        }
 
                        if (leave_copy) {
-                               ec.ig.Emit (OpCodes.Dup);
+                               ec.Emit (OpCodes.Dup);
 
                                if (IsRef) {
                                        temp = new LocalTemporary (Type);
@@ -4240,7 +4395,7 @@ namespace Mono.CSharp {
                        }
 
                        if (leave_copy) {
-                               ec.ig.Emit (OpCodes.Dup);
+                               ec.Emit (OpCodes.Dup);
                                if (IsRef) {
                                        temp = new LocalTemporary (Type);
                                        temp.Store (ec);
@@ -4248,7 +4403,7 @@ namespace Mono.CSharp {
                        }
 
                        if (IsRef)
-                               StoreFromPtr (ec.ig, type);
+                               ec.EmitStoreFromPtr (type);
                        else
                                Variable.EmitAssign (ec);
 
@@ -4259,12 +4414,7 @@ namespace Mono.CSharp {
                }
 
                public bool IsHoisted {
-                       get { return GetHoistedVariable (null) != null; }
-               }
-
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       type = storey.MutateType (type);
+                       get { return GetHoistedVariable ((AnonymousExpression) null) != null; }
                }
        }
 
@@ -4276,7 +4426,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)
                {
@@ -4301,9 +4450,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;
                }
 
                //              
@@ -4325,7 +4474,7 @@ namespace Mono.CSharp {
                        get { return name; }
                }
 
-               public bool VerifyAssigned (EmitContext ec)
+               public bool VerifyAssigned (ResolveContext ec)
                {
                        VariableInfo variable_info = local_info.VariableInfo;
                        return variable_info == null || variable_info.IsAssigned (ec, loc);
@@ -4345,21 +4494,19 @@ namespace Mono.CSharp {
                        local_info.AddressTaken = true;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        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 (EmitContext ec)
+               Expression DoResolveBase (ResolveContext ec)
                {
-                       type = local_info.VariableType;
-
                        Expression e = Block.GetConstantExpression (Name);
                        if (e != null)
                                return e.Resolve (ec);
@@ -4372,7 +4519,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);
@@ -4380,34 +4527,31 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       resolved |= ec.DoFlowAnalysis;
                        eclass = ExprClass.Variable;
+                       type = local_info.VariableType;
                        return this;
                }
 
-               public override Expression DoResolve (EmitContext 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;
                        }
                        
                        return DoResolveBase (ec);
                }
 
-               public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
                        ResolveLocalInfo ();
 
                        // is out param
-                       if (right_side == EmptyExpression.OutAccess)
+                       if (right_side == EmptyExpression.OutAccess.Instance)
                                local_info.Used = true;
 
                        // Infer implicitly typed local variable
@@ -4423,7 +4567,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}'";
@@ -4434,7 +4578,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);
                        }
@@ -4496,9 +4640,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;
                }
 
                //
@@ -4525,7 +4669,7 @@ namespace Mono.CSharp {
                        get { return Parameter; }
                }
 
-               public bool IsAssigned (EmitContext ec, Location loc)
+               public bool IsAssigned (ResolveContext ec, Location loc)
                {
                        // HACK: Variables are not captured in probing mode
                        if (ec.IsInProbingMode)
@@ -4534,7 +4678,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;
                }
 
@@ -4543,13 +4687,13 @@ namespace Mono.CSharp {
                        Parameter.HasAddressTaken = true;
                }
 
-               void SetAssigned (EmitContext ec)
+               void SetAssigned (ResolveContext ec)
                {
                        if (HasOutModifier && ec.DoFlowAnalysis)
                                ec.CurrentBranching.SetAssigned (VariableInfo);
                }
 
-               bool DoResolveBase (EmitContext ec)
+               bool DoResolveBase (ResolveContext ec)
                {
                        type = pi.ParameterType;
                        eclass = ExprClass.Variable;
@@ -4560,37 +4704,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;
@@ -4609,17 +4752,30 @@ 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)
                {
                        // Nothing to clone
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        HoistedVariable hv = GetHoistedVariable (ec);
                        if (hv != null)
-                               return hv.CreateExpressionTree (ec);
+                               return hv.CreateExpressionTree ();
 
                        return Parameter.ExpressionTreeVariableReference ();
                }
@@ -4636,7 +4792,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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (!DoResolveBase (ec))
                                return null;
@@ -4652,7 +4808,7 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               override public Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
                        if (!DoResolveBase (ec))
                                return null;
@@ -4664,18 +4820,18 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               static public void EmitLdArg (ILGenerator ig, int x)
+               static public void EmitLdArg (EmitContext ec, int x)
                {
                        switch (x) {
-                       case 0: ig.Emit (OpCodes.Ldarg_0); break;
-                       case 1: ig.Emit (OpCodes.Ldarg_1); break;
-                       case 2: ig.Emit (OpCodes.Ldarg_2); break;
-                       case 3: ig.Emit (OpCodes.Ldarg_3); break;
+                       case 0: ec.Emit (OpCodes.Ldarg_0); break;
+                       case 1: ec.Emit (OpCodes.Ldarg_1); break;
+                       case 2: ec.Emit (OpCodes.Ldarg_2); break;
+                       case 3: ec.Emit (OpCodes.Ldarg_3); break;
                        default:
                                if (x > byte.MaxValue)
-                                       ig.Emit (OpCodes.Ldarg, x);
+                                       ec.Emit (OpCodes.Ldarg, x);
                                else
-                                       ig.Emit (OpCodes.Ldarg_S, (byte) x);
+                                       ec.Emit (OpCodes.Ldarg_S, (byte) x);
                                break;
                        }
                }
@@ -4714,41 +4870,26 @@ namespace Mono.CSharp {
                        this.arguments_resolved = arguments_resolved;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               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 (EmitContext 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;
 
                        //
@@ -4758,82 +4899,51 @@ 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;
-                                       }
+                       TypeSpec expr_type = member_expr.Type;
+                       mg = member_expr as MethodGroupExpr;
 
-                                       args = arguments;
+                       bool dynamic_member = expr_type == InternalType.Dynamic;
 
-                                       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, -1, 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);
+                               type = method.ReturnType;
 
                                // TODO: this is a copy of mg.ResolveMemberAccess method
                                Expression iexpr = mg.InstanceExpression;
@@ -4843,7 +4953,7 @@ namespace Mono.CSharp {
                                                mg.IdenticalTypeName) {
                                                mg.InstanceExpression = null;
                                        } else {
-                                               MemberExpr.error176 (loc, mg.GetSignatureForError ());
+                                               MemberExpr.error176 (ec, loc, mg.GetSignatureForError ());
                                                return null;
                                        }
                                } else {
@@ -4852,31 +4962,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);
@@ -4885,26 +4988,74 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               protected virtual MethodGroupExpr DoResolveOverload (EmitContext ec)
+               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 (!method.IsReservedMethod)
+                               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);
+                       ec.Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
+                               method.GetSignatureForError ());
        
                        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;
@@ -4912,28 +5063,6 @@ namespace Mono.CSharp {
                        return list.ArgumentTypes;
                }
 
-               /// <summary>
-               /// This checks the ConditionalAttribute on the method 
-               /// </summary>
-               public static bool IsMethodExcluded (MethodBase method, Location loc)
-               {
-                       if (method.IsConstructor)
-                               return false;
-
-                       method = TypeManager.DropGenericMethodArguments (method);
-                       if (method.DeclaringType.Module == RootContext.ToplevelTypes.Builder) {
-                               IMethodData md = TypeManager.GetMethod (method);
-                               if (md != null)
-                                       return md.IsExcluded ();
-
-                               // For some methods (generated by delegate class) GetMethod returns null
-                               // because they are not included in builder_to_method table
-                               return false;
-                       }
-
-                       return AttributeTester.IsConditionalMethodExcluded (method, loc);
-               }
-
                /// <remarks>
                ///   is_base tells whether we want to force the use of the `call'
                ///   opcode instead of using callvirt.  Call is required to call
@@ -4951,7 +5080,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);
                }
@@ -4964,17 +5093,17 @@ 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;
                        bool struct_call = false;
                        bool this_call = false;
                        LocalTemporary this_arg = null;
 
-                       Type decl_type = method.DeclaringType;
+                       TypeSpec decl_type = method.DeclaringType;
 
-                       if (IsMethodExcluded (method, loc))
+                       // Speed up the check by not doing it on not allowed targets
+                       if (method.ReturnType == TypeManager.void_type && method.IsConditionallyExcluded (loc))
                                return;
                        
                        bool is_static = method.IsStatic;
@@ -4987,8 +5116,8 @@ namespace Mono.CSharp {
                                // If this is ourselves, push "this"
                                //
                                if (!omit_args) {
-                                       Type t = null;
-                                       Type iexpr_type = instance_expr.Type;
+                                       TypeSpec t = null;
+                                       TypeSpec iexpr_type = instance_expr.Type;
 
                                        //
                                        // Push the instance expression
@@ -5019,13 +5148,13 @@ namespace Mono.CSharp {
 
                                                        // avoid the overhead of doing this all the time.
                                                        if (dup_args)
-                                                               t = TypeManager.GetReferenceType (iexpr_type);
+                                                               t = ReferenceContainer.MakeType (iexpr_type);
                                                } else {
                                                        instance_expr.Emit (ec);
                                                        
                                                        // FIXME: should use instance_expr is IMemoryLocation + constraint.
                                                        // to help JIT to produce better code
-                                                       ig.Emit (OpCodes.Box, instance_expr.Type);
+                                                       ec.Emit (OpCodes.Box, instance_expr.Type);
                                                        t = TypeManager.object_type;
                                                }
                                        } else {
@@ -5034,207 +5163,95 @@ namespace Mono.CSharp {
                                        }
 
                                        if (dup_args) {
-                                               ig.Emit (OpCodes.Dup);
+                                               ec.Emit (OpCodes.Dup);
                                                if (Arguments != null && Arguments.Count != 0) {
                                                        this_arg = new LocalTemporary (t);
                                                        this_arg.Store (ec);
                                                }
                                        }
-                               }
-                       }
-
-                       if (!omit_args && Arguments != null)
-                               Arguments.Emit (ec, dup_args, this_arg);
-
-                       OpCode call_op;
-                       if (is_static || struct_call || is_base || (this_call && !method.IsVirtual)) {
-                               call_op = OpCodes.Call;
-                       } 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) {
-                               Type[] varargs_types = GetVarargsTypes (method, Arguments);
-                               ig.EmitCall (call_op, (MethodInfo) method, varargs_types);
-                               return;
-                       }
-
-                       //
-                       // If you have:
-                       // this.DoFoo ();
-                       // 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);
-                       else
-                               ig.Emit (call_op, (ConstructorInfo) method);
-               }
-
-               public override void Emit (EmitContext ec)
-               {
-                       mg.EmitCall (ec, arguments);
-               }
-               
-               public override void EmitStatement (EmitContext ec)
-               {
-                       Emit (ec);
-
-                       // 
-                       // Pop the return value if there is one
-                       //
-                       if (TypeManager.TypeToCoreType (type) != TypeManager.void_type)
-                               ec.ig.Emit (OpCodes.Pop);
-               }
-
-               protected override void CloneTo (CloneContext clonectx, Expression t)
-               {
-                       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 (EmitContext ec)
-               {
-                       throw new NotSupportedException ("ET");
-               }
-
-               public override Expression DoResolve (EmitContext 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;
+                       if (!omit_args && Arguments != null)
+                               Arguments.Emit (ec, dup_args, this_arg);
 
-                       //
-                       // Ok, so it's a Cast.
-                       //
-                       if (expr.eclass == ExprClass.Type || expr.eclass == ExprClass.TypeParameter) {
-                               return new Cast (expr, argument, loc);
+                       OpCode call_op;
+                       if (is_static || struct_call || is_base || (this_call && !method.IsVirtual)) {
+                               call_op = OpCodes.Call;
+                       } else {
+                               call_op = OpCodes.Callvirt;
+                               
+                               if ((instance_expr != null) && (instance_expr.Type.IsGenericParameter))
+                                       ec.Emit (OpCodes.Constrained, instance_expr.Type);
                        }
 
-                       if (expr.eclass == ExprClass.Namespace) {
-                               expr.Error_UnexpectedKind (null, "type", loc);
-                               return null;
-                       }                       
+                       if (method.Parameters.HasArglist) {
+                               Type[] varargs_types = GetVarargsTypes (method, Arguments);
+                               ec.Emit (call_op, method, varargs_types);
+                               return;
+                       }
 
                        //
-                       // It's a delegate invocation.
+                       // If you have:
+                       // this.DoFoo ();
+                       // and DoFoo is not virtual, you can omit the callvirt,
+                       // because you don't need the null checking behavior.
                        //
-                       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;
-                       }
-
-                       return s.ResolveStatement (ec);
+                       ec.Emit (call_op, method);
                }
 
                public override void Emit (EmitContext ec)
                {
-                       throw new Exception ("Cannot happen");
+                       mg.EmitCall (ec, arguments);
                }
-
+               
                public override void EmitStatement (EmitContext ec)
                {
-                       throw new Exception ("Cannot happen");
+                       Emit (ec);
+
+                       // 
+                       // Pop the return value if there is one
+                       //
+                       if (type != TypeManager.void_type)
+                               ec.Emit (OpCodes.Pop);
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
                {
-                       InvocationOrCast target = (InvocationOrCast) t;
+                       Invocation target = (Invocation) t;
+
+                       if (arguments != null)
+                               target.arguments = arguments.Clone (clonectx);
 
                        target.expr = expr.Clone (clonectx);
-                       target.argument = argument.Clone (clonectx);
+               }
+
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
+               {
+                       return MakeExpression (ctx, mg.InstanceExpression, (MethodSpec) mg, arguments);
+               }
+
+               public static SLE.Expression MakeExpression (BuilderContext ctx, Expression instance, MethodSpec mi, Arguments args)
+               {
+                       var instance_expr = instance == null ? null : instance.MakeExpression (ctx);
+                       return SLE.Expression.Call (instance_expr, (MethodInfo) mi.GetMetaInfo (), Arguments.MakeExpression (args, ctx));
                }
        }
-*/
 
        /// <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;
-
-               bool is_type_parameter;
+               protected MethodGroupExpr method;
 
                public New (Expression requested_type, Arguments arguments, Location l)
                {
@@ -5246,7 +5263,7 @@ namespace Mono.CSharp {
                /// <summary>
                /// Converts complex core type syntax like 'new int ()' to simple constant
                /// </summary>
-               public static Constant Constantify (Type t)
+               public static Constant Constantify (TypeSpec t)
                {
                        if (t == TypeManager.int32_type)
                                return new IntConstant (0, Location.Null);
@@ -5275,7 +5292,7 @@ namespace Mono.CSharp {
                        if (t == TypeManager.decimal_type)
                                return new DecimalConstant (0, Location.Null);
                        if (TypeManager.IsEnumType (t))
-                               return new EnumConstant (Constantify (TypeManager.GetEnumUnderlyingType (t)), t);
+                               return new EnumConstant (Constantify (EnumSpec.GetUnderlyingType (t)), t);
                        if (TypeManager.IsNullableType (t))
                                return Nullable.LiftedNull.Create (t, Location.Null);
 
@@ -5287,7 +5304,7 @@ namespace Mono.CSharp {
                // [ComImport, CoClass] attributes and must be treated
                // specially
                //
-               public Expression CheckComImport (EmitContext ec)
+               public Expression CheckComImport (ResolveContext ec)
                {
                        if (!type.IsInterface)
                                return null;
@@ -5296,7 +5313,7 @@ namespace Mono.CSharp {
                        // Turn the call into:
                        // (the-interface-stated) (new class-referenced-in-coclassattribute ())
                        //
-                       Type real_class = AttributeTester.GetCoClassAttribute (type);
+                       var real_class = type.MemberDefinition.GetAttributeCoClass ();
                        if (real_class == null)
                                return null;
 
@@ -5305,21 +5322,22 @@ namespace Mono.CSharp {
                        return cast.Resolve (ec);
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        Arguments args;
                        if (method == null) {
                                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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        //
                        // The New DoResolve might be called twice when initializing field
@@ -5342,7 +5360,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;
                        }
@@ -5350,49 +5368,42 @@ 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)) {
                                return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
                        }
 
-                       if (TypeManager.IsGenericParameter (type)) {
-                               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));
-                                       return null;
+                       var tparam = type as TypeParameterSpec;
+                       if (tparam != null) {
+                               if (!tparam.HasSpecialConstructor && !tparam.HasSpecialStruct) {
+                                       ec.Report.Error (304, loc,
+                                               "Cannot create an instance of the variable type `{0}' because it does not have the new() constraint",
+                                               TypeManager.CSharpName (type));
                                }
 
                                if ((Arguments != null) && (Arguments.Count != 0)) {
-                                       Error (417, String.Format (
-                                                      "`{0}': cannot provide arguments " +
-                                                      "when creating an instance of a " +
-                                                      "variable type.", type));
-                                       return null;
+                                       ec.Report.Error (417, loc,
+                                               "`{0}': cannot provide arguments when creating an instance of a variable type",
+                                               TypeManager.CSharpName (type));
                                }
 
                                if (TypeManager.activator_create_instance == null) {
-                                       Type activator_type = TypeManager.CoreLookupType ("System", "Activator", Kind.Class, true);
+                                       TypeSpec 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);
+                                                       activator_type, MemberFilter.Method ("CreateInstance", 1, ParametersCompiled.EmptyReadOnlyParameters, null), loc);
                                        }
                                }
 
-                               is_type_parameter = true;
                                eclass = ExprClass.Value;
                                return this;
                        }
 
-                       if (type.IsAbstract && type.IsSealed) {
-                               Report.SymbolRelatedToPreviousError (type);
-                               Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
+                       if (type.IsStatic) {
+                               ec.Report.SymbolRelatedToPreviousError (type);
+                               ec.Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
                                return null;
                        }
 
@@ -5403,8 +5414,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;
                        }
 
@@ -5419,17 +5430,14 @@ namespace Mono.CSharp {
                                return this;
 
                        // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
-                       Expression ml = MemberLookupFinal (ec, type, type, ConstructorInfo.ConstructorName,
-                               MemberTypes.Constructor, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
+                       Expression ml = MemberLookupFinal (ec, type, type, ConstructorInfo.ConstructorName, 0,
+                               MemberKind.Constructor, BindingRestriction.AccessibleOnly | BindingRestriction.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)
@@ -5437,7 +5445,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;
                        }
 
@@ -5445,21 +5453,21 @@ 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;
+                       var ctor_factory = TypeManager.activator_create_instance.MakeGenericMethod (type);
+                       var tparam = (TypeParameterSpec) type;
 
-                       MethodInfo ci = TypeManager.activator_create_instance.MakeGenericMethod (
-                               new Type [] { type });
-
-                       GenericConstraints gc = TypeManager.GetTypeParameterConstraints (type);
-                       if (gc.HasReferenceTypeConstraint || gc.HasClassConstraint) {
-                               ig.Emit (OpCodes.Call, ci);
+                       if (tparam.IsReferenceType) {
+                               ec.Emit (OpCodes.Call, ctor_factory);
                                return true;
                        }
 
@@ -5468,29 +5476,26 @@ namespace Mono.CSharp {
                        // you can't share LocalBuilders among ILGeneators.
                        LocalTemporary temp = new LocalTemporary (type);
 
-                       Label label_activator = ig.DefineLabel ();
-                       Label label_end = ig.DefineLabel ();
+                       Label label_activator = ec.DefineLabel ();
+                       Label label_end = ec.DefineLabel ();
 
                        temp.AddressOf (ec, AddressOp.Store);
-                       ig.Emit (OpCodes.Initobj, type);
+                       ec.Emit (OpCodes.Initobj, type);
 
                        temp.Emit (ec);
-                       ig.Emit (OpCodes.Box, type);
-                       ig.Emit (OpCodes.Brfalse, label_activator);
+                       ec.Emit (OpCodes.Box, type);
+                       ec.Emit (OpCodes.Brfalse, label_activator);
 
                        temp.AddressOf (ec, AddressOp.Store);
-                       ig.Emit (OpCodes.Initobj, type);
+                       ec.Emit (OpCodes.Initobj, type);
                        temp.Emit (ec);
-                       ig.Emit (OpCodes.Br_S, label_end);
+                       ec.Emit (OpCodes.Br_S, label_end);
 
-                       ig.MarkLabel (label_activator);
+                       ec.MarkLabel (label_activator);
 
-                       ig.Emit (OpCodes.Call, ci);
-                       ig.MarkLabel (label_end);
+                       ec.Emit (OpCodes.Call, ctor_factory);
+                       ec.MarkLabel (label_end);
                        return true;
-#else
-                       throw new InternalErrorException ();
-#endif
                }
 
                //
@@ -5523,7 +5528,6 @@ namespace Mono.CSharp {
                public virtual bool Emit (EmitContext ec, IMemoryLocation target)
                {
                        bool is_value_type = TypeManager.IsValueType (type);
-                       ILGenerator ig = ec.ig;
                        VariableReference vr = target as VariableReference;
 
                        if (target != null && is_value_type && (vr != null || method == null)) {
@@ -5537,26 +5541,20 @@ namespace Mono.CSharp {
 
                        if (is_value_type) {
                                if (method == null) {
-                                       ig.Emit (OpCodes.Initobj, type);
+                                       ec.Emit (OpCodes.Initobj, type);
                                        return false;
                                }
 
                                if (vr != null) {
-                                       ig.Emit (OpCodes.Call, (ConstructorInfo) method);
+                                       ec.Emit (OpCodes.Call, method.BestCandidate);
                                        return false;
                                }
                        }
                        
-                       if (is_type_parameter)
+                       if (type is TypeParameterSpec)
                                return DoEmitTypeParameter (ec);                        
 
-                       ConstructorInfo ci = (ConstructorInfo) method;
-#if MS_COMPATIBLE
-                       if (TypeManager.IsGenericType (type))
-                               ci = TypeBuilder.GetConstructor (type, ci);
-#endif
-
-                       ig.Emit (OpCodes.Newobj, ci);
+                       ec.Emit (OpCodes.Newobj, method.BestCandidate);
                        return true;
                }
 
@@ -5581,13 +5579,7 @@ namespace Mono.CSharp {
                        }
 
                        if (Emit (ec, v))
-                               ec.ig.Emit (OpCodes.Pop);
-               }
-
-               public bool IsDefaultValueType {
-                       get {
-                               return TypeManager.IsValueType (type) && !HasInitializer && Arguments == null;
-                       }
+                               ec.Emit (OpCodes.Pop);
                }
 
                public virtual bool HasInitializer {
@@ -5605,7 +5597,7 @@ namespace Mono.CSharp {
                {
                        LocalTemporary value_target = new LocalTemporary (type);
 
-                       if (is_type_parameter) {
+                       if (type is TypeParameterSpec) {
                                DoEmitTypeParameter (ec);
                                value_target.Store (ec);
                                value_target.AddressOf (ec, mode);
@@ -5625,12 +5617,12 @@ namespace Mono.CSharp {
                        value_target.AddressOf (ec, AddressOp.Store);
 
                        if (method == null) {
-                               ec.ig.Emit (OpCodes.Initobj, type);
+                               ec.Emit (OpCodes.Initobj, type);
                        } else {
                                if (Arguments != null)
                                        Arguments.Emit (ec);
 
-                               ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
+                               ec.Emit (OpCodes.Call, method.BestCandidate);
                        }
                        
                        value_target.AddressOf (ec, mode);
@@ -5647,16 +5639,60 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       if (method != null) {
-                               method.MutateHoistedGenericType (storey);
-                               if (Arguments != null) {
-                                       Arguments.MutateHoistedGenericType (storey);
-                               }
-                       }
+                       return SLE.Expression.New ((ConstructorInfo) method.BestCandidate.GetMetaInfo (), Arguments.MakeExpression (Arguments, ctx));
+               }
+       }
+
+       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; }
+               }
 
-                       type = storey.MutateType (type);
+               protected override Expression DoResolve (ResolveContext rc)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public Expression this [int index] {
+                       get { return elements [index]; }
                }
        }
 
@@ -5670,46 +5706,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;
+               protected TypeSpec 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;
@@ -5724,20 +5759,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 (EmitContext 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;
@@ -5748,14 +5778,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.ToString ());
                                        return false;
                                }
                                
@@ -5764,11 +5794,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;
                                        }
                                        
@@ -5776,19 +5806,16 @@ 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;
 
                                        // Initializers with the default values can be ignored
                                        Constant c = element as Constant;
                                        if (c != null) {
-                                               if (c.IsDefaultInitializer (array_element_type)) {
-                                                       element = null;
-                                               }
-                                               else {
+                                               if (!c.IsDefaultInitializer (array_element_type)) {
                                                        ++const_initializers_count;
                                                }
                                        } else {
@@ -5802,27 +5829,21 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        Arguments args;
 
                        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;
                        }
 
@@ -5830,28 +5851,25 @@ 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];
-                                       if (e == null)
-                                               e = Convert.ImplicitConversion (ec, (Expression) initializers [i], array_element_type, loc);
-
+                                       Expression e = array_data [i];
                                        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);
@@ -5861,13 +5879,9 @@ namespace Mono.CSharp {
                                        return;
                                }
                        }
-
                }
 
-               Expression first_emit;
-               LocalTemporary first_emit_temp;
-
-               protected virtual Expression ResolveArrayElement (EmitContext ec, Expression element)
+               protected virtual Expression ResolveArrayElement (ResolveContext ec, Expression element)
                {
                        element = element.Resolve (ec);
                        if (element == null)
@@ -5884,7 +5898,7 @@ namespace Mono.CSharp {
                                ec, element, array_element_type, loc);
                }
 
-               protected bool ResolveInitializers (EmitContext ec)
+               protected bool ResolveInitializers (ResolveContext ec)
                {
                        if (initializers == null) {
                                return !expect_initializers;
@@ -5894,13 +5908,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;
@@ -5913,19 +5927,14 @@ namespace Mono.CSharp {
                //
                // Resolved the type of the array
                //
-               bool ResolveArrayType (EmitContext ec)
+               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;
                        }
                        
-                       StringBuilder array_qualifier = new StringBuilder (rank);
+                       StringBuilder array_qualifier = new StringBuilder ();
 
                        //
                        // `In the first form allocates an array instace of the type that results
@@ -5938,6 +5947,8 @@ namespace Mono.CSharp {
                                array_qualifier.Append ("]");
                        }
 
+                       array_qualifier.Append (rank);
+
                        //
                        // Lookup the type
                        //
@@ -5948,13 +5959,19 @@ namespace Mono.CSharp {
                                return false;
 
                        type = array_type_expr.Type;
-                       array_element_type = TypeManager.GetElementType (type);
-                       dimensions = type.GetArrayRank ();
+                       var ac = type as ArrayContainer;
+                       if (ac == null) {
+                               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 = ac.Element;
+                       dimensions = ac.Rank;
 
                        return true;
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (type != null)
                                return this;
@@ -5970,7 +5987,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;
 
@@ -5981,26 +5998,6 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               MethodInfo GetArrayMethod (int arguments)
-               {
-                       ModuleBuilder mb = RootContext.ToplevelTypes.Builder;
-
-                       Type[] arg_types = new Type[arguments];
-                       for (int i = 0; i < arguments; i++)
-                               arg_types[i] = TypeManager.int32_type;
-
-                       MethodInfo mi = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
-                                                       arg_types);
-
-                       if (mi == null) {
-                               Report.Error (-6, "New invocation: Can not find a constructor for " +
-                                                 "this argument list");
-                               return null;
-                       }
-
-                       return mi; 
-               }
-
                byte [] MakeByteBlob ()
                {
                        int factor;
@@ -6008,12 +6005,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);
+                       TypeSpec element_type = array_element_type;
+                       if (TypeManager.IsEnumType (element_type))
+                               element_type = EnumSpec.GetUnderlyingType (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;
@@ -6031,7 +6029,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;
                                                
@@ -6040,7 +6038,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;
 
@@ -6049,7 +6047,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);
                                                        
@@ -6058,7 +6056,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);
 
@@ -6069,28 +6067,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;
                                        
@@ -6099,7 +6097,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;
                                        
@@ -6108,22 +6106,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;
@@ -6142,35 +6140,30 @@ 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;
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+#if NET_4_0
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       array_element_type = storey.MutateType (array_element_type);
-                       type = storey.MutateType (type);
-                       if (arguments != null) {
-                               foreach (Expression e in arguments)
-                                       e.MutateHoistedGenericType (storey);
+                       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.GetMetaInfo ());
+                               else
+                                       initializers [i] = array_data [i].MakeExpression (ctx);
                        }
-                       
-                       if (array_data != null) {
-                               foreach (Expression e in array_data) {
-                                       // Don't mutate values optimized away
-                                       if (e == null)
-                                               continue;
 
-                                       e.MutateHoistedGenericType (storey);
-                               }
-                       }
+                       return SLE.Expression.NewArrayInit (array_element_type.GetMetaInfo (), initializers);
                }
-
+#endif
                //
                // Emits the initializers for the array
                //
@@ -6189,16 +6182,14 @@ namespace Mono.CSharp {
                        // First, the static data
                        //
                        FieldBuilder fb;
-                       ILGenerator ig = ec.ig;
                        
                        byte [] data = MakeByteBlob ();
 
                        fb = RootContext.MakeStaticData (data);
 
-                       ig.Emit (OpCodes.Dup);
-                       ig.Emit (OpCodes.Ldtoken, fb);
-                       ig.Emit (OpCodes.Call,
-                                TypeManager.void_initializearray_array_fieldhandle);
+                       ec.Emit (OpCodes.Dup);
+                       ec.Emit (OpCodes.Ldtoken, fb);
+                       ec.Emit (OpCodes.Call, TypeManager.void_initializearray_array_fieldhandle);
                }
 
                //
@@ -6209,37 +6200,22 @@ namespace Mono.CSharp {
                //
                void EmitDynamicInitializers (EmitContext ec, bool emitConstants)
                {
-                       ILGenerator ig = ec.ig;
                        int dims = bounds.Count;
                        int [] current_pos = new int [dims];
 
-                       MethodInfo set = null;
-
-                       if (dims != 1){
-                               Type [] args = new Type [dims + 1];
-
-                               for (int j = 0; j < dims; j++)
-                                       args [j] = TypeManager.int32_type;
-                               args [dims] = array_element_type;
-
-                               set = RootContext.ToplevelTypes.Builder.GetArrayMethod (
-                                       type, "Set",
-                                       CallingConventions.HasThis | CallingConventions.Standard,
-                                       TypeManager.void_type, args);
-                       }
-
                        for (int i = 0; i < array_data.Count; i++){
 
-                               Expression e = (Expression)array_data [i];
+                               Expression e = array_data [i];
+                               var c = e as Constant;
 
                                // Constant can be initialized via StaticInitializer
-                               if (e != null && !(!emitConstants && e is Constant)) {
-                                       Type etype = e.Type;
+                               if (c == null || (c != null && emitConstants && !c.IsDefaultInitializer (array_element_type))) {
+                                       TypeSpec etype = e.Type;
 
-                                       ig.Emit (OpCodes.Dup);
+                                       ec.Emit (OpCodes.Dup);
 
                                        for (int idx = 0; idx < dims; idx++) 
-                                               IntConstant.EmitInt (ig, current_pos [idx]);
+                                               ec.EmitInt (current_pos [idx]);
 
                                        //
                                        // If we are dealing with a struct, get the
@@ -6249,23 +6225,12 @@ namespace Mono.CSharp {
                                            (!TypeManager.IsBuiltinOrEnum (etype) ||
                                             etype == TypeManager.decimal_type)) {
 
-                                               ig.Emit (OpCodes.Ldelema, etype);
+                                               ec.Emit (OpCodes.Ldelema, etype);
                                        }
 
                                        e.Emit (ec);
 
-                                       if (dims == 1) {
-                                               bool is_stobj, has_type_arg;
-                                               OpCode op = ArrayAccess.GetStoreOpcode (etype, out is_stobj, out has_type_arg);
-                                               if (is_stobj)
-                                                       ig.Emit (OpCodes.Stobj, etype);
-                                               else if (has_type_arg)
-                                                       ig.Emit (op, etype);
-                                               else
-                                                       ig.Emit (op);
-                                       } else 
-                                               ig.Emit (OpCodes.Call, set);
-
+                                       ec.EmitArrayStore ((ArrayContainer) type);
                                }
                                
                                //
@@ -6273,7 +6238,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;
                                }
@@ -6282,8 +6247,6 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-
                        if (first_emit != null) {
                                first_emit.Emit (ec);
                                first_emit_temp.Store (ec);
@@ -6292,20 +6255,16 @@ namespace Mono.CSharp {
                        foreach (Expression e in arguments)
                                e.Emit (ec);
 
-                       if (arguments.Count == 1)
-                               ig.Emit (OpCodes.Newarr, array_element_type);
-                       else {
-                               ig.Emit (OpCodes.Newobj, GetArrayMethod (arguments.Count));
-                       }
+                       ec.EmitArrayNew ((ArrayContainer) type);
                        
                        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)
@@ -6318,41 +6277,40 @@ namespace Mono.CSharp {
                                first_emit_temp.Release (ec);
                }
 
-               public override bool GetAttributableValue (EmitContext ec, Type value_type, out object value)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
                {
-                       if (arguments.Count != 1) {
-                               // Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
-                               return base.GetAttributableValue (ec, null, out value);
+                       // no multi dimensional or jagged arrays
+                       if (arguments.Count != 1 || array_element_type.IsArray) {
+                               base.EncodeAttributeValue (rc, enc, targetType);
+                               return;
                        }
 
-                       if (array_data == null) {
-                               Constant c = (Constant) arguments [0];
-                               if (c.IsDefaultValue) {
-                                       value = Array.CreateInstance (array_element_type, 0);
-                                       return true;
+                       // No array covariance, except for array -> object
+                       if (type != targetType) {
+                               if (targetType != TypeManager.object_type) {
+                                       base.EncodeAttributeValue (rc, enc, targetType);
+                                       return;
                                }
-                               // Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
-                               return base.GetAttributableValue (ec, null, out value);
-                       }
-                       
-                       Array ret = Array.CreateInstance (array_element_type, array_data.Count);
-                       object element_value;
-                       for (int i = 0; i < ret.Length; ++i)
-                       {
-                               Expression e = (Expression)array_data [i];
 
-                               // Is null when an initializer is optimized (value == predefined value)
-                               if (e == null) 
-                                       continue;
+                               enc.Encode (type);
+                       }
 
-                               if (!e.GetAttributableValue (ec, array_element_type, out element_value)) {
-                                       value = null;
-                                       return false;
+                       // Single dimensional array of 0 size
+                       if (array_data == null) {
+                               IntConstant ic = arguments[0] as IntConstant;
+                               if (ic == null || !ic.IsDefaultValue) {
+                                       base.EncodeAttributeValue (rc, enc, targetType);
+                               } else {
+                                       enc.Stream.Write (0);
                                }
-                               ret.SetValue (element_value, i);
+
+                               return;
+                       }
+
+                       enc.Stream.Write ((int) array_data.Count);
+                       foreach (var element in array_data) {
+                               element.EncodeAttributeValue (rc, enc, array_element_type);
                        }
-                       value = ret;
-                       return true;
                }
                
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -6363,38 +6321,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 {
@@ -6402,7 +6346,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (type != null)
                                return this;
@@ -6412,8 +6356,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;
                        }
 
@@ -6429,16 +6374,16 @@ 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");
                }
 
                //
                // Converts static initializer only
                //
-               void UnifyInitializerElement (EmitContext ec)
+               void UnifyInitializerElement (ResolveContext ec)
                {
                        for (int i = 0; i < array_data.Count; ++i) {
                                Expression e = (Expression)array_data[i];
@@ -6447,7 +6392,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               protected override Expression ResolveArrayElement (EmitContext ec, Expression element)
+               protected override Expression ResolveArrayElement (ResolveContext ec, Expression element)
                {
                        element = element.Resolve (ec);
                        if (element == null)
@@ -6469,7 +6414,7 @@ namespace Mono.CSharp {
                                return element;
                        }
 
-                       Error_NoBestType ();
+                       Error_NoBestType (ec);
                        return null;
                }
        }       
@@ -6483,21 +6428,23 @@ namespace Mono.CSharp {
                {
                }
 
-               public CompilerGeneratedThis (Type type, Location loc)
+               public CompilerGeneratedThis (TypeSpec type, Location loc)
                        : base (loc)
                {
                        this.type = type;
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        eclass = ExprClass.Variable;
                        if (type == null)
                                type = ec.CurrentType;
+
+                       is_struct = TypeManager.IsStruct (type);
                        return this;
                }
 
-               public override HoistedVariable GetHoistedVariable (EmitContext ec)
+               public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
                {
                        return null;
                }
@@ -6515,7 +6462,7 @@ namespace Mono.CSharp {
 
                        public void Emit (EmitContext ec)
                        {
-                               ec.ig.Emit (OpCodes.Ldarg_0);
+                               ec.Emit (OpCodes.Ldarg_0);
                        }
 
                        public void EmitAssign (EmitContext ec)
@@ -6525,13 +6472,13 @@ namespace Mono.CSharp {
 
                        public void EmitAddressOf (EmitContext ec)
                        {
-                               ec.ig.Emit (OpCodes.Ldarg_0);
+                               ec.Emit (OpCodes.Ldarg_0);
                        }
                }
 
                Block block;
                VariableInfo variable_info;
-               bool is_struct;
+               protected bool is_struct;
 
                public This (Block block, Location loc)
                {
@@ -6552,16 +6499,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)
@@ -6581,41 +6524,38 @@ namespace Mono.CSharp {
                        get { return ThisVariable.Instance; }
                }
 
-               public static bool IsThisAvailable (EmitContext ec)
+               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)
                                return true;
 
-                       if (ec.CurrentType.IsValueType && ec.CurrentIterator == null)
+                       if (TypeManager.IsStruct (ec.CurrentType) && ec.CurrentIterator == null)
                                return false;
 
                        return true;
                }
 
-               public bool ResolveBase (EmitContext ec)
+               public bool ResolveBase (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return true;
-
                        eclass = ExprClass.Variable;
                        type = ec.CurrentType;
 
                        if (!IsThisAvailable (ec)) {
-                               if (ec.IsStatic) {
-                                       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");
                                }
                        }
 
-                       is_struct = type.IsValueType;
+                       is_struct = TypeManager.IsStruct (type);
 
                        if (block != null) {
                                if (block.Toplevel.ThisVariable != null)
@@ -6633,33 +6573,33 @@ namespace Mono.CSharp {
                //
                // Called from Invocation to check if the invocation is correct
                //
-               public override void CheckMarshalByRefAccess (EmitContext ec)
+               public override void CheckMarshalByRefAccess (ResolveContext ec)
                {
                        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);
                        }
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        Arguments args = new Arguments (1);
                        args.Add (new Argument (this));
                        
                        // 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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        ResolveBase (ec);
                        return this;
                }
 
-               override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               override public Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
                        if (!ResolveBase (ec))
                                return null;
@@ -6669,11 +6609,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;
@@ -6720,19 +6660,19 @@ namespace Mono.CSharp {
                        this.loc = loc;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        throw new NotSupportedException ("ET");
                }
 
-               public override Expression DoResolve (EmitContext 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;
@@ -6740,7 +6680,7 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       ec.ig.Emit (OpCodes.Arglist);
+                       ec.Emit (OpCodes.Arglist);
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression target)
@@ -6770,23 +6710,23 @@ namespace Mono.CSharp {
                public Type[] ArgumentTypes {
                    get {
                                if (Arguments == null)
-                                       return Type.EmptyTypes;
+                                       return System.Type.EmptyTypes;
 
-                       Type[] retval = new Type [Arguments.Count];
+                       var retval = new Type [Arguments.Count];
                        for (int i = 0; i < retval.Length; i++)
-                           retval [i] = Arguments [i].Expr.Type;
+                                       retval[i] = Arguments[i].Expr.Type.GetMetaInfo ();
 
                        return retval;
                    }
                }
                
-               public override Expression CreateExpressionTree (EmitContext ec)
+               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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        eclass = ExprClass.Variable;
                        type = InternalType.Arglist;
@@ -6804,12 +6744,6 @@ namespace Mono.CSharp {
                                Arguments.Emit (ec);
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       if (Arguments != null)
-                               Arguments.MutateHoistedGenericType (storey);
-               }
-
                protected override void CloneTo (CloneContext clonectx, Expression t)
                {
                        Arglist target = (Arglist) t;
@@ -6824,7 +6758,7 @@ namespace Mono.CSharp {
        /// </summary>
        public class TypeOf : Expression {
                Expression QueriedType;
-               protected Type typearg;
+               protected TypeSpec typearg;
                
                public TypeOf (Expression queried_type, Location l)
                {
@@ -6832,19 +6766,16 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
                        if (texpr == null)
                                return null;
@@ -6852,11 +6783,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");
                        }
 
@@ -6878,37 +6809,44 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               public override void Emit (EmitContext ec)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
                {
-                       ec.ig.Emit (OpCodes.Ldtoken, TypeManager.TypeToReflectionType (typearg));
-                       ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
-               }
+                       // Target type is not System.Type therefore must be object
+                       // and we need to use different encoding sequence
+                       if (targetType != type)
+                               enc.Encode (type);
 
-               public override bool GetAttributableValue (EmitContext 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",
-                                            TypeManager.CSharpName (typearg));
-                               value = null;
-                               return false;
+/*
+                       var gi = typearg as InflatedTypeSpec;
+                       if (gi != null) {
+                               // TODO: This has to be recursive, handle arrays, etc.
+                               // I could probably do it after CustomAttribute encoder rewrite
+                               foreach (var ta in gi.TypeArguments) {
+                                       if (ta.IsGenericParameter) {
+                                               ec.Report.SymbolRelatedToPreviousError (typearg);
+                                               ec.Report.Error (416, loc, "`{0}': an attribute argument cannot use type parameters",
+                                                                TypeManager.CSharpName (typearg));
+                                               value = null;
+                                               return false;
+                                       }
+                               }
                        }
+ */
 
-                       if (value_type == TypeManager.object_type) {
-                               value = (object)typearg;
-                               return true;
+                       if (!enc.EncodeTypeName (typearg)) {
+                               rc.Compiler.Report.SymbolRelatedToPreviousError (typearg);
+                               rc.Compiler.Report.Error (416, loc, "`{0}': an attribute argument cannot use type parameters",
+                                       TypeManager.CSharpName (typearg));
                        }
-                       value = typearg;
-                       return true;
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+               public override void Emit (EmitContext ec)
                {
-                       typearg = storey.MutateType (typearg);
+                       ec.Emit (OpCodes.Ldtoken, typearg);
+                       ec.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
                }
 
-               public Type TypeArgument {
+               public TypeSpec TypeArgument {
                        get {
                                return typearg;
                        }
@@ -6931,7 +6869,7 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        type = TypeManager.type_type;
                        typearg = TypeManager.void_type;
@@ -6940,23 +6878,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 (EmitContext 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);
@@ -6964,13 +6902,10 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       if (member is ConstructorInfo)
-                               ec.ig.Emit (OpCodes.Ldtoken, (ConstructorInfo) member);
-                       else
-                               ec.ig.Emit (OpCodes.Ldtoken, (MethodInfo) member);
+                       ec.Emit (OpCodes.Ldtoken, member);
 
                        base.Emit (ec);
-                       ec.ig.Emit (OpCodes.Castclass, type);
+                       ec.Emit (OpCodes.Castclass, type);
                }
 
                protected override string GetMethodName {
@@ -6981,7 +6916,7 @@ namespace Mono.CSharp {
                        get { return "RuntimeMethodHandle"; }
                }
 
-               protected override MethodInfo TypeFromHandle {
+               protected override MethodSpec TypeFromHandle {
                        get {
                                return TypeManager.methodbase_get_type_from_handle;
                        }
@@ -6990,7 +6925,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               protected override MethodInfo TypeFromHandleGeneric {
+               protected override MethodSpec TypeFromHandleGeneric {
                        get {
                                return TypeManager.methodbase_get_type_from_handle_generic;
                        }
@@ -7004,40 +6939,40 @@ 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;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       bool is_generic = TypeManager.IsGenericType (member.DeclaringType);
-                       MethodInfo mi = is_generic ? TypeFromHandleGeneric : TypeFromHandle;
+                       bool is_generic = member.DeclaringType.IsGenericOrParentIsGeneric;
+                       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);
+                               TypeSpec t = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", TypeName, MemberKind.Class, true);
+                               TypeSpec handle_type = TypeManager.CoreLookupType (ec.Compiler, "System", RuntimeHandleName, MemberKind.Struct, true);
 
                                if (t == null || handle_type == null)
                                        return null;
 
                                mi = TypeManager.GetPredefinedMethod (t, GetMethodName, loc,
                                        is_generic ?
-                                       new Type[] { handle_type, TypeManager.runtime_handle_type } :
-                                       new Type[] { handle_type } );
+                                       new TypeSpec[] { handle_type, TypeManager.runtime_handle_type } :
+                                       new TypeSpec[] { handle_type } );
 
                                if (is_generic)
                                        TypeFromHandleGeneric = mi;
@@ -7051,36 +6986,36 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       bool is_generic = TypeManager.IsGenericType (member.DeclaringType);
-                       MethodInfo mi;
+                       bool is_generic = member.DeclaringType.IsGenericOrParentIsGeneric;
+                       MethodSpec mi;
                        if (is_generic) {
                                mi = TypeFromHandleGeneric;
-                               ec.ig.Emit (OpCodes.Ldtoken, member.DeclaringType);
+                               ec.Emit (OpCodes.Ldtoken, member.DeclaringType);
                        } else {
                                mi = TypeFromHandle;
                        }
 
-                       ec.ig.Emit (OpCodes.Call, mi);
+                       ec.Emit (OpCodes.Call, mi);
                }
 
                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 (EmitContext 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);
@@ -7088,7 +7023,7 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       ec.ig.Emit (OpCodes.Ldtoken, (FieldInfo) member);
+                       ec.Emit (OpCodes.Ldtoken, member);
                        base.Emit (ec);
                }
 
@@ -7100,7 +7035,7 @@ namespace Mono.CSharp {
                        get { return "RuntimeFieldHandle"; }
                }
 
-               protected override MethodInfo TypeFromHandle {
+               protected override MethodSpec TypeFromHandle {
                        get {
                                return TypeManager.fieldinfo_get_field_from_handle;
                        }
@@ -7109,7 +7044,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               protected override MethodInfo TypeFromHandleGeneric {
+               protected override MethodSpec TypeFromHandleGeneric {
                        get {
                                return TypeManager.fieldinfo_get_field_from_handle_generic;
                        }
@@ -7128,7 +7063,7 @@ namespace Mono.CSharp {
        /// </summary>
        public class SizeOf : Expression {
                readonly Expression QueriedType;
-               Type type_queried;
+               TypeSpec type_queried;
                
                public SizeOf (Expression queried_type, Location l)
                {
@@ -7136,13 +7071,13 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       Error_PointerInsideExpressionTree ();
+                       Error_PointerInsideExpressionTree (ec);
                        return null;
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
                        if (texpr == null)
@@ -7150,19 +7085,19 @@ namespace Mono.CSharp {
 
                        type_queried = texpr.Type;
                        if (TypeManager.IsEnumType (type_queried))
-                               type_queried = TypeManager.GetEnumUnderlyingType (type_queried);
+                               type_queried = EnumSpec.GetUnderlyingType (type_queried);
 
                        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));
                        }
@@ -7174,12 +7109,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.Emit (OpCodes.Sizeof, type_queried);
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -7195,30 +7125,36 @@ namespace Mono.CSharp {
                readonly string alias;
                public static readonly string GlobalAlias = "global";
 
+               public QualifiedAliasMember (string alias, string identifier, Location l)
+                       : base (null, identifier, l)
+               {
+                       this.alias = alias;
+               }
+
                public QualifiedAliasMember (string alias, string identifier, TypeArguments targs, Location l)
                        : base (null, identifier, targs, l)
                {
                        this.alias = alias;
                }
 
-               public QualifiedAliasMember (string alias, string identifier, Location l)
-                       : base (null, identifier, l)
+               public QualifiedAliasMember (string alias, string identifier, int arity, Location l)
+                       : base (null, identifier, arity, l)
                {
                        this.alias = alias;
                }
 
-               public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
+               public override FullNamedExpression ResolveAsTypeStep (IMemberContext ec, bool silent)
                {
                        if (alias == GlobalAlias) {
                                expr = GlobalRootNamespace.Instance;
                                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;
                        }
 
@@ -7228,7 +7164,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;
@@ -7237,14 +7173,14 @@ namespace Mono.CSharp {
                        return fne;
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return ResolveAsTypeStep (ec, false);
                }
 
-               protected override void Error_IdentifierNotFound (IResolveContext rc, FullNamedExpression expr_type, string identifier)
+               protected override void Error_IdentifierNotFound (IMemberContext rc, TypeSpec 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 ());
                }
@@ -7253,8 +7189,7 @@ namespace Mono.CSharp {
                {
                        string name = Name;
                        if (targs != null) {
-                               name = TypeManager.RemoveGenericArity (Name) + "<" +
-                                       targs.GetSignatureForError () + ">";
+                               name = Name + "<" + targs.GetSignatureForError () + ">";
                        }
 
                        return alias + "::" + name;
@@ -7290,7 +7225,13 @@ namespace Mono.CSharp {
                        this.expr = expr;
                }
 
-               Expression DoResolve (EmitContext ec, Expression right_side)
+               public MemberAccess (Expression expr, string identifier, int arity, Location loc)
+                       : base (identifier, arity, loc)
+               {
+                       this.expr = expr;
+               }
+
+               Expression DoResolve (ResolveContext ec, Expression right_side)
                {
                        if (type != null)
                                throw new Exception ();
@@ -7303,62 +7244,53 @@ 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;
 
-                       string LookupIdentifier = MemberName.MakeName (Name, targs);
-
                        Namespace ns = expr_resolved as Namespace;
                        if (ns != null) {
-                               FullNamedExpression retval = ns.Lookup (LookupIdentifier, loc);
+                               FullNamedExpression retval = ns.Lookup (ec.Compiler, Name, Arity, loc);
 
                                if (retval == null)
-                                       ns.Error_NamespaceDoesNotExist (loc, LookupIdentifier);
-                               else if (targs != null)
+                                       ns.Error_NamespaceDoesNotExist (loc, Name, Arity, ec);
+                               else if (HasTypeArguments)
                                        retval = new GenericTypeExpr (retval.Type, targs, loc).ResolveAsTypeStep (ec, false);
 
                                return retval;
                        }
 
-                       Type expr_type = expr_resolved.Type;
-                       if (TypeManager.IsDynamicType (expr_type)) {
-                               Arguments args = new Arguments (2);
+                       TypeSpec expr_type = expr_resolved.Type;
+                       if (expr_type == InternalType.Dynamic) {
+                               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");
                        }
 
-                       if (targs != null) {
-                               if (!targs.Resolve (ec))
-                                       return null;
-                       }
+                       var arity = HasTypeArguments ? targs.Count : -1;
 
-                       Expression member_lookup;
-                       member_lookup = MemberLookup (
-                               ec.CurrentType, expr_type, expr_type, Name, loc);
-
-                       if (member_lookup == null && targs != null) {
-                               member_lookup = MemberLookup (
-                                       ec.CurrentType, expr_type, expr_type, LookupIdentifier, loc);
-                       }
+                       var member_lookup = MemberLookup (ec.Compiler,
+                               ec.CurrentType, expr_type, expr_type, Name, arity, BindingRestriction.NoOverrides, loc);
 
                        if (member_lookup == null) {
                                ExprClass expr_eclass = expr_resolved.eclass;
@@ -7369,22 +7301,25 @@ namespace Mono.CSharp {
                                if (expr_eclass == ExprClass.Value || expr_eclass == ExprClass.Variable ||
                                        expr_eclass == ExprClass.IndexerAccess || expr_eclass == ExprClass.PropertyAccess ||
                                        expr_eclass == ExprClass.EventAccess) {
-                                       ExtensionMethodGroupExpr ex_method_lookup = ec.LookupExtensionMethod (expr_type, Name, loc);
+                                       ExtensionMethodGroupExpr ex_method_lookup = ec.LookupExtensionMethod (expr_type, Name, arity, loc);
                                        if (ex_method_lookup != null) {
                                                ex_method_lookup.ExtensionExpression = expr_resolved;
 
-                                               if (targs != null) {
-                                                       ex_method_lookup.SetTypeArguments (targs);
+                                               if (HasTypeArguments) {
+                                                       if (!targs.Resolve (ec))
+                                                               return null;
+
+                                                       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 (
-                                       ec.CurrentType, expr_type, expr_type, Name, null,
-                                       AllMemberTypes, AllBindingFlags);
+                               member_lookup = Error_MemberLookupFailed (ec,
+                                       ec.CurrentType, expr_type, expr_type, Name, arity, null,
+                                       MemberKind.All, BindingRestriction.AccessibleOnly);
                                if (member_lookup == null)
                                        return null;
                        }
@@ -7393,29 +7328,20 @@ 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.GenericDeclContainer)) {
-                                       Report.SymbolRelatedToPreviousError (member_lookup.Type);
-                                       ErrorIsInaccesible (loc, TypeManager.CSharpName (member_lookup.Type));
+                               if (!texpr.CheckAccessLevel (ec.MemberContext)) {
+                                       ec.Report.SymbolRelatedToPreviousError (member_lookup.Type);
+                                       ErrorIsInaccesible (loc, TypeManager.CSharpName (member_lookup.Type), ec.Report);
                                        return null;
                                }
 
                                GenericTypeExpr ct = expr_resolved as GenericTypeExpr;
-                               if (ct != null) {
-                                       //
-                                       // When looking up a nested type in a generic instance
-                                       // via reflection, we always get a generic type definition
-                                       // and not a generic instance - so we have to do this here.
-                                       //
-                                       // See gtest-172-lib.cs and gtest-172.cs for an example.
-                                       //
-                                       ct = new GenericTypeExpr (
-                                               member_lookup.Type, ct.TypeArguments, loc);
-
+                               if (ct != null && Arity > 0) {
+                                       ct = new GenericTypeExpr (member_lookup.Type, targs, loc);
                                        return ct.ResolveAsTypeStep (ec, false);
                                }
 
@@ -7427,11 +7353,14 @@ namespace Mono.CSharp {
                        if (me == null)
                                return null;
 
-                       if (targs != null) {
-                               me.SetTypeArguments (targs);
+                       if (HasTypeArguments) {
+                               if (!targs.Resolve (ec))
+                                       return null;
+
+                               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))
@@ -7445,41 +7374,41 @@ 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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return DoResolve (ec, null);
                }
 
-               public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
                        return DoResolve (ec, right_side);
                }
 
-               public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
+               public override FullNamedExpression ResolveAsTypeStep (IMemberContext ec, bool silent)
                {
                        return ResolveNamespaceOrType (ec, silent);
                }
 
-               public FullNamedExpression ResolveNamespaceOrType (IResolveContext rc, bool silent)
+               public FullNamedExpression ResolveNamespaceOrType (IMemberContext rc, bool silent)
                {
                        FullNamedExpression expr_resolved = expr.ResolveAsTypeStep (rc, silent);
 
                        if (expr_resolved == null)
                                return null;
 
-                       string LookupIdentifier = MemberName.MakeName (Name, targs);
-
                        Namespace ns = expr_resolved as Namespace;
                        if (ns != null) {
-                               FullNamedExpression retval = ns.Lookup (LookupIdentifier, loc);
+                               FullNamedExpression retval = ns.Lookup (rc.Compiler, Name, Arity, loc);
 
-                               if (retval == null && !silent)
-                                       ns.Error_NamespaceDoesNotExist (loc, LookupIdentifier);
-                               else if (targs != null)
+                               if (retval == null) {
+                                       if (!silent)
+                                               ns.Error_NamespaceDoesNotExist (loc, Name, Arity, rc);
+                               } else if (HasTypeArguments) {
                                        retval = new GenericTypeExpr (retval.Type, targs, loc).ResolveAsTypeStep (rc, silent);
+                               }
 
                                return retval;
                        }
@@ -7488,93 +7417,71 @@ namespace Mono.CSharp {
                        if (tnew_expr == null)
                                return null;
 
-                       Type expr_type = tnew_expr.Type;
+                       TypeSpec 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 (
-                               rc.CurrentType, expr_type, expr_type, LookupIdentifier,
-                               MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
-                       if (member_lookup == null) {
+                       var nested = MemberCache.FindNestedType (expr_type, Name, Arity);
+                       if (nested == null) {
                                if (silent)
                                        return null;
 
-                               Error_IdentifierNotFound (rc, expr_resolved, LookupIdentifier);
+                               Error_IdentifierNotFound (rc, expr_type, Name);
                                return null;
                        }
 
-                       TypeExpr texpr = member_lookup.ResolveAsTypeTerminal (rc, false);
-                       if (texpr == null)
-                               return null;
-
-                       TypeArguments the_args = targs;
-                       Type declaring_type = texpr.Type.DeclaringType;
-                       if (TypeManager.HasGenericArguments (declaring_type) && !TypeManager.IsGenericTypeDefinition (expr_type)) {
-                               while (!TypeManager.IsEqual (TypeManager.DropGenericTypeArguments (expr_type), declaring_type)) {
-                                       expr_type = expr_type.BaseType;
-                               }
-                               
-                               TypeArguments new_args = new TypeArguments ();
-                               foreach (Type decl in TypeManager.GetTypeArguments (expr_type))
-                                       new_args.Add (new TypeExpression (TypeManager.TypeToCoreType (decl), loc));
-
-                               if (targs != null)
-                                       new_args.Add (targs);
-
-                               the_args = new_args;
+                       bool extra_check;
+                       if (!IsMemberAccessible (rc.CurrentType ?? InternalType.FakeInternalType, nested, out extra_check)) {
+                               ErrorIsInaccesible (loc, nested.GetSignatureForError (), rc.Compiler.Report);
                        }
-
-                       if (the_args != null) {
-                               GenericTypeExpr ctype = new GenericTypeExpr (texpr.Type, the_args, loc);
-                               return ctype.ResolveAsTypeStep (rc, false);
+                       
+                       TypeExpr texpr;
+                       if (HasTypeArguments) {
+                               texpr = new GenericTypeExpr (nested, targs, loc);
+                       } else {
+                               texpr = new TypeExpression (nested, loc);
                        }
 
-                       return texpr;
+                       return texpr.ResolveAsTypeStep (rc, false);
                }
 
-               protected virtual void Error_IdentifierNotFound (IResolveContext rc, FullNamedExpression expr_type, string identifier)
+               protected virtual void Error_IdentifierNotFound (IMemberContext rc, TypeSpec expr_type, string identifier)
                {
-                       Expression member_lookup = MemberLookup (
-                               rc.CurrentType, expr_type.Type, expr_type.Type, SimpleName.RemoveGenericArity (identifier),
-                               MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
-
-                       if (member_lookup != null) {
-                               expr_type = member_lookup.ResolveAsTypeTerminal (rc, false);
-                               if (expr_type == null)
-                                       return;
+                       var nested = MemberCache.FindNestedType (expr_type, Name, -System.Math.Max (1, Arity));
 
-                               Namespace.Error_TypeArgumentsCannotBeUsed (expr_type, loc);
+                       if (nested != null) {
+                               Error_TypeArgumentsCannotBeUsed (rc.Compiler.Report, expr.Location, nested, Arity);
                                return;
                        }
 
-                       member_lookup = MemberLookup (
-                               rc.CurrentType, expr_type.Type, expr_type.Type, identifier,
-                                       MemberTypes.All, BindingFlags.Public | BindingFlags.NonPublic, loc);
+                       var member_lookup = MemberLookup (rc.Compiler,
+                               rc.CurrentType, expr_type, expr_type, identifier, -1,
+                                       MemberKind.All, BindingRestriction.None, 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, TypeSpec 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 ()
@@ -7609,15 +7516,15 @@ namespace Mono.CSharp {
                        loc = l;
                }
                
-               public override Expression CreateExpressionTree (EmitContext ec)
+               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 (EmitContext 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)
@@ -7643,9 +7550,11 @@ namespace Mono.CSharp {
                                Expr.EmitBranchable (ec, target, on_true);
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       Expr.MutateHoistedGenericType (storey);
+                       using (ctx.With (BuilderContext.Options.AllCheckStateFlags, true)) {
+                               return Expr.MakeExpression (ctx);
+                       }
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -7669,15 +7578,15 @@ namespace Mono.CSharp {
                        loc = l;
                }
                
-               public override Expression CreateExpressionTree (EmitContext ec)
+               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 (EmitContext 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)
@@ -7703,11 +7612,6 @@ namespace Mono.CSharp {
                                Expr.EmitBranchable (ec, target, on_true);
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       Expr.MutateHoistedGenericType (storey);
-               }
-
                protected override void CloneTo (CloneContext clonectx, Expression t)
                {
                        UnCheckedExpr target = (UnCheckedExpr) t;
@@ -7733,29 +7637,29 @@ namespace Mono.CSharp {
                        this.Arguments = args;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        Arguments args = Arguments.CreateForExpressionTree (ec, Arguments,
                                Expr.CreateExpressionTree (ec));
 
-                       return CreateExpressionFactoryCall ("ArrayIndex", args);
+                       return CreateExpressionFactoryCall (ec, "ArrayIndex", args);
                }
 
-               Expression MakePointerAccess (EmitContext ec, Type t)
+               Expression MakePointerAccess (ResolveContext ec, TypeSpec 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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        Expr = Expr.Resolve (ec);
                        if (Expr == null)
@@ -7767,10 +7671,10 @@ namespace Mono.CSharp {
                        //
                        // I am experimenting with this pattern.
                        //
-                       Type t = Expr.Type;
+                       TypeSpec 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;
                        }
                        
@@ -7781,7 +7685,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);
                                }
@@ -7789,7 +7693,7 @@ namespace Mono.CSharp {
                        return (new IndexerAccess (this, loc)).Resolve (ec);
                }
 
-               public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
                        Expr = Expr.Resolve (ec);
                        if (Expr == null)
@@ -7813,9 +7717,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 ()
@@ -7836,7 +7740,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
                //
@@ -7852,52 +7756,38 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        return ea.CreateExpressionTree (ec);
                }
 
-               public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
                        return DoResolve (ec);
                }
 
-               public override Expression DoResolve (EmitContext 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);
 
-                       Type t = ea.Expr.Type;
+                       TypeSpec 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}'",
-                                         ea.Arguments.Count.ToString (), t.GetArrayRank ().ToString ());
+                       if (t.GetMetaInfo ().GetArrayRank () != rank) {
+                               ec.Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
+                                         ea.Arguments.Count.ToString (), t.GetMetaInfo ().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);
                        }
@@ -7907,143 +7797,27 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               /// <summary>
-               ///    Emits the right opcode to load an object of Type `t'
-               ///    from an array of T
-               /// </summary>
-               void EmitLoadOpcode (ILGenerator ig, Type type, int rank)
-               {
-                       if (rank > 1) {
-                               MethodInfo get = FetchGetMethod ();
-                               ig.Emit (OpCodes.Call, get);
-                               return;
-                       }
-
-                       if (type == TypeManager.byte_type || type == TypeManager.bool_type)
-                               ig.Emit (OpCodes.Ldelem_U1);
-                       else if (type == TypeManager.sbyte_type)
-                               ig.Emit (OpCodes.Ldelem_I1);
-                       else if (type == TypeManager.short_type)
-                               ig.Emit (OpCodes.Ldelem_I2);
-                       else if (type == TypeManager.ushort_type || type == TypeManager.char_type)
-                               ig.Emit (OpCodes.Ldelem_U2);
-                       else if (type == TypeManager.int32_type)
-                               ig.Emit (OpCodes.Ldelem_I4);
-                       else if (type == TypeManager.uint32_type)
-                               ig.Emit (OpCodes.Ldelem_U4);
-                       else if (type == TypeManager.uint64_type)
-                               ig.Emit (OpCodes.Ldelem_I8);
-                       else if (type == TypeManager.int64_type)
-                               ig.Emit (OpCodes.Ldelem_I8);
-                       else if (type == TypeManager.float_type)
-                               ig.Emit (OpCodes.Ldelem_R4);
-                       else if (type == TypeManager.double_type)
-                               ig.Emit (OpCodes.Ldelem_R8);
-                       else if (type == TypeManager.intptr_type)
-                               ig.Emit (OpCodes.Ldelem_I);
-                       else if (TypeManager.IsEnumType (type)){
-                               EmitLoadOpcode (ig, TypeManager.GetEnumUnderlyingType (type), rank);
-                       } 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)
-               {
-                       Report.Warning (251, 2, loc, "Indexing an array with a negative index (array indices always start at zero)");
-               }
-
-               /// <summary>
-               ///    Returns the right opcode to store an object of Type `t'
-               ///    from an array of T.  
-               /// </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))
-                               t = TypeManager.GetEnumUnderlyingType (t);
-                       if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
-                           t == TypeManager.bool_type)
-                               return OpCodes.Stelem_I1;
-                       else if (t == TypeManager.short_type || t == TypeManager.ushort_type ||
-                                t == TypeManager.char_type)
-                               return OpCodes.Stelem_I2;
-                       else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
-                               return OpCodes.Stelem_I4;
-                       else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
-                               return OpCodes.Stelem_I8;
-                       else if (t == TypeManager.float_type)
-                               return OpCodes.Stelem_R4;
-                       else if (t == TypeManager.double_type)
-                               return OpCodes.Stelem_R8;
-                       else if (t == TypeManager.intptr_type) {
-                                has_type_arg = true;
-                               is_stobj = true;
-                                return OpCodes.Stobj;
-                       } else if (TypeManager.IsStruct (t)) {
-                               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
-                               return OpCodes.Stelem_Ref;
-               }
-
-               MethodInfo FetchGetMethod ()
+               protected override void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
                {
-                       ModuleBuilder mb = RootContext.ToplevelTypes.Builder;
-                       int arg_count = ea.Arguments.Count;
-                       Type [] args = new Type [arg_count];
-                       MethodInfo get;
-                       
-                       for (int i = 0; i < arg_count; i++){
-                               //args [i++] = a.Type;
-                               args [i] = TypeManager.int32_type;
-                       }
-                       
-                       get = mb.GetArrayMethod (
-                               ea.Expr.Type, "Get",
-                               CallingConventions.HasThis |
-                               CallingConventions.Standard,
-                               type, args);
-                       return get;
+                       ec.Report.Warning (251, 2, loc, "Indexing an array with a negative index (array indices always start at zero)");
                }
-                               
 
                MethodInfo FetchAddressMethod ()
                {
                        ModuleBuilder mb = RootContext.ToplevelTypes.Builder;
                        int arg_count = ea.Arguments.Count;
-                       Type [] args = new Type [arg_count];
+                       var args = new Type [arg_count];
                        MethodInfo address;
-                       Type ret_type;
                        
-                       ret_type = TypeManager.GetReferenceType (type);
+                       var ret_type = TypeManager.GetReferenceType (type);
                        
                        for (int i = 0; i < arg_count; i++){
                                //args [i++] = a.Type;
-                               args [i] = TypeManager.int32_type;
+                               args[i] = TypeManager.int32_type.GetMetaInfo ();
                        }
                        
                        address = mb.GetArrayMethod (
-                               ea.Expr.Type, "Address",
+                               ea.Expr.Type.GetMetaInfo (), "Address",
                                CallingConventions.HasThis |
                                CallingConventions.Standard,
                                ret_type, args);
@@ -8065,18 +7839,17 @@ namespace Mono.CSharp {
 
                public void Emit (EmitContext ec, bool leave_copy)
                {
-                       int rank = ea.Expr.Type.GetArrayRank ();
-                       ILGenerator ig = ec.ig;
+                       var ac = ea.Expr.Type as ArrayContainer;
 
                        if (prepared) {
-                               LoadFromPtr (ig, this.type);
+                               ec.EmitLoadFromPtr (type);
                        } else {
                                LoadArrayAndArguments (ec);
-                               EmitLoadOpcode (ig, type, rank);
+                               ec.EmitArrayLoad (ac);
                        }       
 
                        if (leave_copy) {
-                               ig.Emit (OpCodes.Dup);
+                               ec.Emit (OpCodes.Dup);
                                temp = new LocalTemporary (this.type);
                                temp.Store (ec);
                        }
@@ -8089,74 +7862,43 @@ namespace Mono.CSharp {
 
                public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
-                       int rank = ea.Expr.Type.GetArrayRank ();
-                       ILGenerator ig = ec.ig;
-                       Type t = source.Type;
+                       var ac = (ArrayContainer) ea.Expr.Type;
+                       TypeSpec t = source.Type;
                        prepared = prepare_for_load;
 
                        if (prepared) {
                                AddressOf (ec, AddressOp.LoadStore);
-                               ec.ig.Emit (OpCodes.Dup);
+                               ec.Emit (OpCodes.Dup);
                        } else {
                                LoadArrayAndArguments (ec);
-                       }
 
-                       if (rank == 1) {
-                               bool is_stobj, has_type_arg;
-                               OpCode op = GetStoreOpcode (t, out is_stobj, out has_type_arg);
+                               //
+                               // If we are dealing with a struct, get the
+                               // address of it, so we can store it.
+                               //
+                               // The stobj opcode used by value types will need
+                               // an address on the stack, not really an array/array
+                               // pair
+                               //
+                               if (ac.Rank == 1 && TypeManager.IsStruct (t) &&
+                                       (!TypeManager.IsBuiltinOrEnum (t) ||
+                                        t == TypeManager.decimal_type)) {
 
-                               if (!prepared) {
-                                       //
-                                       // The stobj opcode used by value types will need
-                                       // an address on the stack, not really an array/array
-                                       // pair
-                                       //
-                                       if (is_stobj)
-                                               ig.Emit (OpCodes.Ldelema, t);
-                               }
-                               
-                               source.Emit (ec);
-                               if (leave_copy) {
-                                       ec.ig.Emit (OpCodes.Dup);
-                                       temp = new LocalTemporary (this.type);
-                                       temp.Store (ec);
-                               }
-                               
-                               if (prepared)
-                                       StoreFromPtr (ig, t);
-                               else if (is_stobj)
-                                       ig.Emit (OpCodes.Stobj, t);
-                               else if (has_type_arg)
-                                       ig.Emit (op, t);
-                               else
-                                       ig.Emit (op);
-                       } else {
-                               source.Emit (ec);
-                               if (leave_copy) {
-                                       ec.ig.Emit (OpCodes.Dup);
-                                       temp = new LocalTemporary (this.type);
-                                       temp.Store (ec);
+                                       ec.Emit (OpCodes.Ldelema, t);
                                }
+                       }
 
-                               if (prepared) {
-                                       StoreFromPtr (ig, t);
-                               } else {
-                                       int arg_count = ea.Arguments.Count;
-                                       Type [] args = new Type [arg_count + 1];
-                                       for (int i = 0; i < arg_count; i++) {
-                                               //args [i++] = a.Type;
-                                               args [i] = TypeManager.int32_type;
-                                       }
-                                       args [arg_count] = type;
-
-                                       MethodInfo set = RootContext.ToplevelTypes.Builder.GetArrayMethod (
-                                               ea.Expr.Type, "Set",
-                                               CallingConventions.HasThis |
-                                               CallingConventions.Standard,
-                                               TypeManager.void_type, args);
+                       source.Emit (ec);
+                       if (leave_copy) {
+                               ec.Emit (OpCodes.Dup);
+                               temp = new LocalTemporary (this.type);
+                               temp.Store (ec);
+                       }
 
-                                       ig.Emit (OpCodes.Call, set);
-                               }
+                       if (prepared) {
+                               ec.EmitStoreFromPtr (t);
+                       } else {
+                               ec.EmitArrayStore (ac);
                        }
                        
                        if (temp != null) {
@@ -8179,37 +7921,72 @@ namespace Mono.CSharp {
 
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
-                       int rank = ea.Expr.Type.GetArrayRank ();
-                       ILGenerator ig = ec.ig;
+                       int rank = ea.Expr.Type.GetMetaInfo ().GetArrayRank ();
 
                        LoadArrayAndArguments (ec);
 
                        if (rank == 1){
-                               ig.Emit (OpCodes.Ldelema, type);
+                               ec.Emit (OpCodes.Ldelema, type);
                        } else {
                                MethodInfo address = FetchAddressMethod ();
-                               ig.Emit (OpCodes.Call, address);
+                               ec.Emit (OpCodes.Call, address);
                        }
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+#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)
                {
-                       type = storey.MutateType (type);
-                       ea.Expr.Type = storey.MutateType (ea.Expr.Type);
+                       return SLE.Expression.ArrayIndex (
+                               ea.Expr.MakeExpression (ctx),
+                               Arguments.MakeExpression (ea.Arguments, ctx));
                }
        }
 
        /// <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)
+                       IEnumerable<IndexerSpec> candidates;
+
+                       public IndexerMethodGroupExpr (IEnumerable<IndexerSpec> indexers, Location loc)
+                               : base (FilterAccessors (indexers).ToList (), null, loc)
+                       {
+                               candidates = indexers;
+                       }
+
+                       public IndexerSpec BestIndexer ()
+                       {
+                               return candidates.Where (l => l.Get == BestCandidate || l.Set == BestCandidate).First ();
+                       }
+
+                       static IEnumerable<MemberSpec> FilterAccessors (IEnumerable<IndexerSpec> indexers)
+                       {
+                               foreach (IndexerSpec i in indexers) {
+                                       if (i.HasGet)
+                                               yield return i.Get;
+                                       else
+                                               yield return i.Set;
+                               }
+                       }
+
+                       protected override IList<MemberSpec> GetBaseTypeMethods (ResolveContext rc, TypeSpec type)
                        {
-                               Methods = (MethodBase []) indexers.Methods.ToArray (typeof (MethodBase));
+                               candidates = GetIndexersForType (type);
+                               if (candidates == null)
+                                       return null;
+
+                               return FilterAccessors (candidates).ToList ();
                        }
 
                        public override string Name {
@@ -8218,7 +7995,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
@@ -8233,100 +8010,18 @@ namespace Mono.CSharp {
                        }
                }
 
-               class Indexers
-               {
-                       // Contains either property getter or setter
-                       public ArrayList Methods;
-                       public ArrayList Properties;
-
-                       Indexers ()
-                       {
-                       }
-
-                       void Append (Type caller_type, MemberInfo [] mi)
-                       {
-                               if (mi == null)
-                                       return;
-
-                               foreach (PropertyInfo property in mi) {
-                                       MethodInfo accessor = property.GetGetMethod (true);
-                                       if (accessor == null)
-                                               accessor = property.GetSetMethod (true);
-
-                                       if (Methods == null) {
-                                               Methods = new ArrayList ();
-                                               Properties = new ArrayList ();
-                                       }
-
-                                       Methods.Add (accessor);
-                                       Properties.Add (property);
-                               }
-                       }
-
-                       static MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
-                       {
-                               string p_name = TypeManager.IndexerPropertyName (lookup_type);
-
-                               return TypeManager.MemberLookup (
-                                       caller_type, caller_type, lookup_type, MemberTypes.Property,
-                                       BindingFlags.Public | BindingFlags.Instance |
-                                       BindingFlags.DeclaredOnly, p_name, null);
-                       }
-                       
-                       public static Indexers GetIndexersForType (Type caller_type, Type lookup_type) 
-                       {
-                               Indexers ix = new Indexers ();
-
-                               if (TypeManager.IsGenericParameter (lookup_type)) {
-                                       GenericConstraints gc = TypeManager.GetTypeParameterConstraints (lookup_type);
-                                       if (gc == null)
-                                               return ix;
-
-                                       if (gc.HasClassConstraint) {
-                                               Type class_contraint = gc.ClassConstraint;
-                                               while (class_contraint != TypeManager.object_type && class_contraint != null) {
-                                                       ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, class_contraint));
-                                                       class_contraint = class_contraint.BaseType;
-                                               }
-                                       }
-
-                                       Type[] ifaces = gc.InterfaceConstraints;
-                                       foreach (Type itype in ifaces)
-                                               ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
-
-                                       return ix;
-                               }
-
-                               Type copy = lookup_type;
-                               while (copy != TypeManager.object_type && copy != null){
-                                       ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, copy));
-                                       copy = copy.BaseType;
-                               }
-
-                               if (lookup_type.IsInterface) {
-                                       Type [] ifaces = TypeManager.GetInterfaces (lookup_type);
-                                       if (ifaces != null) {
-                                               foreach (Type itype in ifaces)
-                                                       ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
-                                       }
-                               }
-
-                               return ix;
-                       }
-               }
-
                //
                // Points to our "data" repository
                //
-               MethodInfo get, set;
+               IndexerSpec spec;
                bool is_base_indexer;
                bool prepared;
                LocalTemporary temp;
                LocalTemporary prepared_value;
                Expression set_expr;
 
-               protected Type indexer_type;
-               protected Type current_type;
+               protected TypeSpec indexer_type;
+               protected TypeSpec current_type;
                protected Expression instance_expr;
                protected Arguments arguments;
                
@@ -8341,7 +8036,6 @@ namespace Mono.CSharp {
                {
                        this.instance_expr = instance_expr;
                        this.is_base_indexer = is_base_indexer;
-                       this.eclass = ExprClass.Value;
                        this.loc = loc;
                }
 
@@ -8350,31 +8044,35 @@ namespace Mono.CSharp {
                        return isSet ? "set" : "get";
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        Arguments args = Arguments.CreateForExpressionTree (ec, arguments,
                                instance_expr.CreateExpressionTree (ec),
-                               new TypeOfMethod (get, loc));
+                               new TypeOfMethod (spec.Get, loc));
+
+                       return CreateExpressionFactoryCall (ec, "Call", args);
+               }
 
-                       return CreateExpressionFactoryCall ("Call", args);
+               static IEnumerable<IndexerSpec> GetIndexersForType (TypeSpec lookup_type)
+               {
+                       return MemberCache.FindIndexers (lookup_type, BindingRestriction.AccessibleOnly | BindingRestriction.NoOverrides);
                }
 
-               protected virtual void CommonResolve (EmitContext ec)
+               protected virtual void CommonResolve (ResolveContext ec)
                {
                        indexer_type = instance_expr.Type;
                        current_type = ec.CurrentType;
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return ResolveAccessor (ec, null);
                }
 
-               public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
-                       if (right_side == EmptyExpression.OutAccess) {
-                               Report.Error (206, loc, "A property or indexer `{0}' may not be passed as an out or ref parameter",
-                                             GetSignatureForError ());
+                       if (right_side == EmptyExpression.OutAccess.Instance) {
+                               right_side.DoResolveLValue (ec, this);
                                return null;
                        }
 
@@ -8386,115 +8084,125 @@ namespace Mono.CSharp {
                        return ResolveAccessor (ec, right_side);
                }
 
-               Expression ResolveAccessor (EmitContext ec, Expression right_side)
+               Expression ResolveAccessor (ResolveContext ec, Expression right_side)
                {
                        CommonResolve (ec);
 
                        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 (indexer_type == InternalType.Dynamic) {
+                               dynamic = true;
+                       } else {
+                               var ilist = GetIndexersForType (/*current_type,*/ indexer_type);
+                               if (ilist == null) {
+                                       ec.Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
+                                                         TypeManager.CSharpName (indexer_type));
+                                       return null;
+                               }
+
+                               var mg = new IndexerMethodGroupExpr (ilist, loc);
+                               mg = mg.OverloadResolve (ec, ref arguments, false, loc) as IndexerMethodGroupExpr;
+                               if (mg == null)
+                                       return null;
+
+                               if (!dynamic)
+                                       spec = mg.BestIndexer ();
+                       }
+
+                       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);
-                       }
 
-                       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;
-                       }
-
-                       MethodGroupExpr mg = new IndexerMethodGroupExpr (ilist, loc);
-                       mg = mg.OverloadResolve (ec, ref arguments, false, loc);
-                       if (mg == null)
-                               return null;
+                               var expr = new DynamicIndexBinder (args, loc);
+                               if (right_side != null)
+                                       return expr.ResolveLValue (ec, right_side);
 
-                       MethodInfo mi = (MethodInfo) mg;
-                       PropertyInfo pi = null;
-                       for (int i = 0; i < ilist.Methods.Count; ++i) {
-                               if (ilist.Methods [i] == mi) {
-                                       pi = (PropertyInfo) ilist.Properties [i];
-                                       break;
-                               }
+                               return expr.Resolve (ec);
                        }
 
-                       type = TypeManager.TypeToCoreType (pi.PropertyType);
-                       if (type.IsPointer && !ec.InUnsafe)
-                               UnsafeError (loc);
+                       type = spec.MemberType;
+                       if (type.IsPointer && !ec.IsUnsafe)
+                               UnsafeError (ec, loc);
 
-                       MethodInfo accessor;
+                       MethodSpec accessor;
                        if (right_side == null) {
-                               accessor = get = pi.GetGetMethod (true);
+                               accessor = spec.Get;
                        } else {
-                               accessor = set = pi.GetSetMethod (true);
-                               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",
-                                               TypeManager.GetFullNameSignature (pi));
+                               accessor = spec.Set;
+                               if (!spec.HasSet && spec.HasGet) {
+                                       ec.Report.SymbolRelatedToPreviousError (spec);
+                                       ec.Report.Error (200, loc, "The read only property or indexer `{0}' cannot be assigned to",
+                                               spec.GetSignatureForError ());
                                        return null;
                                }
 
                                set_expr = Convert.ImplicitConversion (ec, right_side, type, loc);
                        }
 
-                       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",
-                                       TypeManager.GetFullNameSignature (pi), GetAccessorName (right_side != null));
+                       if (accessor == null || accessor.Kind == MemberKind.FakeMethod) {
+                               ec.Report.SymbolRelatedToPreviousError (spec);
+                               ec.Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks a `{1}' accessor",
+                                       spec.GetSignatureForError (), GetAccessorName (right_side != null));
                                return null;
                        }
 
                        //
                        // Only base will allow this invocation to happen.
                        //
-                       if (accessor.IsAbstract && this is BaseIndexerAccess) {
-                               Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (pi));
+                       if (spec.IsAbstract && this is BaseIndexerAccess) {
+                               Error_CannotCallAbstractBase (ec, spec.GetSignatureForError ());
                        }
 
                        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 && 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",
-                                               TypeManager.GetFullNameSignature (pi), GetAccessorName (right_side != null));
+                       if (!IsMemberAccessible (ec.CurrentType, accessor, out must_do_cs1540_check)) {
+                               if (spec.HasDifferentAccessibility) {
+                                       ec.Report.SymbolRelatedToPreviousError (accessor);
+                                       ec.Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because a `{1}' accessor is inaccessible",
+                                               TypeManager.GetFullNameSignature (spec), GetAccessorName (right_side != null));
                                } else {
-                                       Report.SymbolRelatedToPreviousError (pi);
-                                       ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (pi));
+                                       ec.Report.SymbolRelatedToPreviousError (spec);
+                                       ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (spec), 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);
+                               Error_CannotAccessProtected (ec, loc, spec, 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)
                {
                        if (prepared) {
                                prepared_value.Emit (ec);
                        } else {
-                               Invocation.EmitCall (ec, is_base_indexer, instance_expr, get,
+                               Invocation.EmitCall (ec, is_base_indexer, instance_expr, spec.Get,
                                        arguments, loc, false, false);
                        }
 
                        if (leave_copy) {
-                               ec.ig.Emit (OpCodes.Dup);
+                               ec.Emit (OpCodes.Dup);
                                temp = new LocalTemporary (Type);
                                temp.Store (ec);
                        }
@@ -8511,7 +8219,7 @@ namespace Mono.CSharp {
                        Expression value = set_expr;
 
                        if (prepared) {
-                               Invocation.EmitCall (ec, is_base_indexer, instance_expr, get,
+                               Invocation.EmitCall (ec, is_base_indexer, instance_expr, spec.Get,
                                        arguments, loc, true, false);
 
                                prepared_value = new LocalTemporary (type);
@@ -8520,7 +8228,7 @@ namespace Mono.CSharp {
                                prepared_value.Release (ec);
 
                                if (leave_copy) {
-                                       ec.ig.Emit (OpCodes.Dup);
+                                       ec.Emit (OpCodes.Dup);
                                        temp = new LocalTemporary (Type);
                                        temp.Store (ec);
                                }
@@ -8534,7 +8242,7 @@ namespace Mono.CSharp {
                        if (!prepared)
                                arguments.Add (new Argument (value));
 
-                       Invocation.EmitCall (ec, is_base_indexer, instance_expr, set, arguments, loc, false, prepared);
+                       Invocation.EmitCall (ec, is_base_indexer, instance_expr, spec.Set, arguments, loc, false, prepared);
                        
                        if (temp != null) {
                                temp.Emit (ec);
@@ -8542,28 +8250,27 @@ namespace Mono.CSharp {
                        }
                }
                
-               public override void Emit (EmitContext ec)
-               {
-                       Emit (ec, false);
-               }
-
                public override string GetSignatureForError ()
                {
-                       return TypeManager.CSharpSignature (get != null ? get : set, false);
+                       return spec.GetSignatureForError ();
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+#if NET_4_0
+               public SLE.Expression MakeAssignExpression (BuilderContext ctx)
                {
-                       if (get != null)
-                               get = storey.MutateGenericMethod (get);
-                       if (set != null)
-                               set = storey.MutateGenericMethod (set);
+                       var value = new[] { set_expr.MakeExpression (ctx) };
+                       var args = Arguments.MakeExpression (arguments, ctx).Concat (value);
 
-                       instance_expr.MutateHoistedGenericType (storey);
-                       if (arguments != null)
-                               arguments.MutateHoistedGenericType (storey);
+                       return SLE.Expression.Block (
+                                       SLE.Expression.Call (instance_expr.MakeExpression (ctx), (MethodInfo) spec.Set.GetMetaInfo (), args),
+                                       value [0]);
+               }
+#endif
 
-                       type = storey.MutateType (type);
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
+               {
+                       var args = Arguments.MakeExpression (arguments, ctx);
+                       return SLE.Expression.Call (instance_expr.MakeExpression (ctx), (MethodInfo) spec.Get.GetMetaInfo (), args);
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -8597,12 +8304,12 @@ namespace Mono.CSharp {
                        this.args = args;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        throw new NotSupportedException ("ET");
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        Expression c = CommonResolve (ec);
 
@@ -8617,7 +8324,7 @@ namespace Mono.CSharp {
                        return c;
                }
 
-               public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
                        Expression c = CommonResolve (ec);
 
@@ -8633,26 +8340,27 @@ namespace Mono.CSharp {
                        return c;
                }
 
-               Expression CommonResolve (EmitContext ec)
+               Expression CommonResolve (ResolveContext ec)
                {
                        Expression member_lookup;
-                       Type current_type = ec.CurrentType;
-                       Type base_type = current_type.BaseType;
+                       TypeSpec current_type = ec.CurrentType;
+                       TypeSpec base_type = current_type.BaseType;
 
                        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,
-                                                     AllMemberTypes, AllBindingFlags, loc);
+
+                       var arity = args == null ? -1 : args.Count;
+                       member_lookup = MemberLookup (ec.Compiler, ec.CurrentType, null, base_type, Identifier, arity,
+                                                     MemberKind.All, BindingRestriction.AccessibleOnly, loc);
                        if (member_lookup == null) {
-                               Error_MemberLookupFailed (ec.CurrentType, base_type, base_type, Identifier,
-                                       null, AllMemberTypes, AllBindingFlags);
+                               Error_MemberLookupFailed (ec, ec.CurrentType, base_type, base_type, Identifier, arity,
+                                       null, MemberKind.All, BindingRestriction.AccessibleOnly);
                                return null;
                        }
 
@@ -8663,7 +8371,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;
@@ -8671,7 +8391,7 @@ namespace Mono.CSharp {
                        me.IsBase = true;
                        if (args != null) {
                                args.Resolve (ec);
-                               me.SetTypeArguments (args);
+                               me.SetTypeArguments (ec, args);
                        }
 
                        return me;
@@ -8701,7 +8421,7 @@ namespace Mono.CSharp {
                        this.arguments = args;
                }
 
-               protected override void CommonResolve (EmitContext ec)
+               protected override void CommonResolve (ResolveContext ec)
                {
                        instance_expr = ec.GetThis (loc);
 
@@ -8709,9 +8429,9 @@ namespace Mono.CSharp {
                        indexer_type = current_type;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       MemberExpr.Error_BaseAccessInExpressionTree (loc);
+                       MemberExpr.Error_BaseAccessInExpressionTree (ec, loc);
                        return base.CreateExpressionTree (ec);
                }
        }
@@ -8729,7 +8449,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 ();
@@ -8755,19 +8487,19 @@ namespace Mono.CSharp {
                        loc = Location.Null;
                }
 
-               public EmptyExpression (Type t)
+               public EmptyExpression (TypeSpec t)
                {
                        type = t;
                        eclass = ExprClass.Value;
                        loc = Location.Null;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        throw new NotSupportedException ("ET");
                }
                
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return this;
                }
@@ -8786,7 +8518,7 @@ namespace Mono.CSharp {
                // instead of creating gazillions of EmptyExpressions.
                // (CanImplicitConversion uses it)
                //
-               public void SetType (Type t)
+               public void SetType (TypeSpec t)
                {
                        type = t;
                }
@@ -8801,11 +8533,10 @@ namespace Mono.CSharp {
 
                private EmptyExpressionStatement ()
                {
-                       eclass = ExprClass.Value;
                        loc = Location.Null;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        return null;
                }
@@ -8815,8 +8546,9 @@ namespace Mono.CSharp {
                        // Do nothing
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
+                       eclass = ExprClass.Value;
                        type = TypeManager.object_type;
                        return this;
                }
@@ -8828,14 +8560,14 @@ 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;
-                       type = TypeManager.TypeToCoreType (method.ReturnType);
+                       type = method.ReturnType;
                        loc = l;
                }
 
@@ -8845,20 +8577,20 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        Arguments args = new Arguments (3);
                        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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (method);
+                       ObsoleteAttribute oa = method.GetAttributeObsolete ();
                        if (oa != null)
-                               AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc);
+                               AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, ec.Report);
 
                        eclass = ExprClass.Value;
                        return this;
@@ -8867,7 +8599,7 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        source.Emit (ec);
-                       ec.ig.Emit (OpCodes.Call, method);
+                       ec.Emit (OpCodes.Call, method);
                }
 
                public override string GetSignatureForError ()
@@ -8875,10 +8607,9 @@ namespace Mono.CSharp {
                        return TypeManager.CSharpSignature (method);
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+               public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       source.MutateHoistedGenericType (storey);
-                       method = storey.MutateGenericMethod (method);
+                       return SLE.Expression.Convert (source.MakeExpression (ctx), type.GetMetaInfo (), (MethodInfo) method.GetMetaInfo ());
                }
        }
 
@@ -8904,13 +8635,13 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
+               protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
                {
                        TypeExpr lexpr = left.ResolveAsTypeTerminal (ec, false);
                        if (lexpr == null)
                                return null;
 
-                       Type ltype = lexpr.Type;
+                       TypeSpec ltype = lexpr.Type;
                        if ((dim.Length > 0) && (dim [0] == '?')) {
                                TypeExpr nullable = new Nullable.NullableType (lexpr, loc);
                                if (dim.Length > 1)
@@ -8918,18 +8649,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}'", 
+                               if (ltype.IsStatic) {
+                                       ec.Compiler.Report.SymbolRelatedToPreviousError (ltype);
+                                       ec.Compiler.Report.Error (719, loc, "Array elements cannot be of static type `{0}'", 
                                                TypeManager.CSharpName (ltype));
                                }
                        }
@@ -8942,8 +8673,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;
@@ -8954,28 +8685,23 @@ namespace Mono.CSharp {
                {
                        return left.GetSignatureForError () + dim;
                }
-
-               public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
-               {
-                       return ResolveAsBaseTerminal (ec, silent);
-               }               
        }
 
        public class FixedBufferPtr : Expression {
                Expression array;
 
-               public FixedBufferPtr (Expression array, Type array_type, Location l)
+               public FixedBufferPtr (Expression array, TypeSpec array_type, Location l)
                {
                        this.array = array;
                        this.loc = l;
 
-                       type = TypeManager.GetPointerType (array_type);
+                       type = PointerContainer.MakeType (array_type);
                        eclass = ExprClass.Value;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       Error_PointerInsideExpressionTree ();
+                       Error_PointerInsideExpressionTree (ec);
                        return null;
                }
 
@@ -8984,7 +8710,7 @@ namespace Mono.CSharp {
                        array.Emit (ec);
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        //
                        // We are born fully resolved
@@ -9000,9 +8726,9 @@ namespace Mono.CSharp {
        // for fixed (char *pa = a)
        //
        public class ArrayPtr : FixedBufferPtr {
-               Type array_type;
+               TypeSpec array_type;
                
-               public ArrayPtr (Expression array, Type array_type, Location l):
+               public ArrayPtr (Expression array, TypeSpec array_type, Location l):
                        base (array, array_type, l)
                {
                        this.array_type = array_type;
@@ -9012,9 +8738,8 @@ namespace Mono.CSharp {
                {
                        base.Emit (ec);
                        
-                       ILGenerator ig = ec.ig;
-                       IntLiteral.EmitInt (ig, 0);
-                       ig.Emit (OpCodes.Ldelema, array_type);
+                       ec.EmitInt (0);
+                       ec.Emit (OpCodes.Ldelema, array_type);
                }
        }
 
@@ -9024,31 +8749,31 @@ 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 (EmitContext ec)
+               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)
-                               ec.ig.Emit (OpCodes.Conv_U);
-                       else if (type == TypeManager.int64_type)
-                               ec.ig.Emit (OpCodes.Conv_Ovf_I);
-                       else if (type == TypeManager.uint64_type)
-                               ec.ig.Emit (OpCodes.Conv_Ovf_I_Un);
+                       var expr_type = child.Type;
+
+                       if (expr_type == TypeManager.uint32_type)
+                               ec.Emit (OpCodes.Conv_U);
+                       else if (expr_type == TypeManager.int64_type)
+                               ec.Emit (OpCodes.Conv_Ovf_I);
+                       else if (expr_type == TypeManager.uint64_type)
+                               ec.Emit (OpCodes.Conv_Ovf_I_Un);
                        else
                                throw new InternalErrorException ("Cannot emit cast to unknown array element type", type);
                }
@@ -9058,7 +8783,7 @@ namespace Mono.CSharp {
        // Implements the `stackalloc' keyword
        //
        public class StackAlloc : Expression {
-               Type otype;
+               TypeSpec otype;
                Expression t;
                Expression count;
                
@@ -9069,12 +8794,12 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        throw new NotSupportedException ("ET");
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        count = count.Resolve (ec);
                        if (count == null)
@@ -9088,13 +8813,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);
@@ -9103,10 +8826,10 @@ namespace Mono.CSharp {
 
                        otype = texpr.Type;
 
-                       if (!TypeManager.VerifyUnManaged (otype, loc))
+                       if (!TypeManager.VerifyUnmanaged (ec.Compiler, otype, loc))
                                return null;
 
-                       type = TypeManager.GetPointerType (otype);
+                       type = PointerContainer.MakeType (otype);
                        eclass = ExprClass.Value;
 
                        return this;
@@ -9115,17 +8838,16 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        int size = GetTypeSize (otype);
-                       ILGenerator ig = ec.ig;
 
                        count.Emit (ec);
 
                        if (size == 0)
-                               ig.Emit (OpCodes.Sizeof, otype);
+                               ec.Emit (OpCodes.Sizeof, otype);
                        else
-                               IntConstant.EmitInt (ig, size);
+                               ec.EmitInt (size);
 
-                       ig.Emit (OpCodes.Mul_Ovf_Un);
-                       ig.Emit (OpCodes.Localloc);
+                       ec.Emit (OpCodes.Mul_Ovf_Un);
+                       ec.Emit (OpCodes.Localloc);
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -9155,7 +8877,7 @@ namespace Mono.CSharp {
                        target.source = source.Clone (clonectx);
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        Arguments args = new Arguments (2);
                        FieldExpr fe = target as FieldExpr;
@@ -9165,18 +8887,18 @@ 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 (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (source == null)
                                return EmptyExpressionStatement.Instance;
                        
                        MemberExpr me = MemberLookupFinal (ec, ec.CurrentInitializerVariable.Type, ec.CurrentInitializerVariable.Type,
-                               Name, MemberTypes.Field | MemberTypes.Property, BindingFlags.Public | BindingFlags.Instance, loc) as MemberExpr;
+                               Name, 0, MemberKind.Field | MemberKind.Property, BindingRestriction.AccessibleOnly | BindingRestriction.InstanceOnly, loc) as MemberExpr;
 
                        if (me == null)
                                return null;
@@ -9206,19 +8928,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 MemberExpr Error_MemberLookupFailed (ResolveContext ec, TypeSpec type, IList<MemberSpec> 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 " +
+                       var member = members.First ();
+                       if (member.Kind != MemberKind.Property && member.Kind != MemberKind.Field)
+                               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;
@@ -9253,12 +8975,12 @@ namespace Mono.CSharp {
                        {
                        }
 
-                       protected override void Error_TypeDoesNotContainDefinition (Type type, string name)
+                       protected override void Error_TypeDoesNotContainDefinition (ResolveContext ec, TypeSpec type, string name)
                        {
                                if (TypeManager.HasElementType (type))
                                        return;
 
-                               base.Error_TypeDoesNotContainDefinition (type, name);
+                               base.Error_TypeDoesNotContainDefinition (ec, type, name);
                        }
                }
 
@@ -9269,7 +8991,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)
@@ -9278,18 +9000,18 @@ namespace Mono.CSharp {
                        this.loc = loc;
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        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)
@@ -9299,11 +9021,8 @@ namespace Mono.CSharp {
                                target.arguments = arguments.Clone (clonectx);
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        base.expr = new AddMemberAccess (ec.CurrentInitializerVariable, loc);
 
                        return base.DoResolve (ec);
@@ -9315,13 +9034,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;
@@ -9343,14 +9062,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 (EmitContext ec)
+               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)
@@ -9360,26 +9079,23 @@ namespace Mono.CSharp {
                        return new ImplicitlyTypedArrayCreation ("[]", expr_initializers, loc);
                }
                
-               public override Expression DoResolve (EmitContext 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 " +
+                                               if (!ec.CurrentInitializerVariable.Type.ImplementsInterface (TypeManager.ienumerable_type)) {
+                                                       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),
@@ -9390,14 +9106,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 {
@@ -9416,7 +9132,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));
                                }
                        }
@@ -9435,12 +9151,6 @@ namespace Mono.CSharp {
                        foreach (ExpressionStatement e in initializers)
                                e.EmitStatement (ec);
                }
-
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       foreach (Expression e in initializers)
-                               e.MutateHoistedGenericType (storey);
-               }
        }
        
        //
@@ -9465,18 +9175,18 @@ namespace Mono.CSharp {
                                this.new_instance = newInstance;
                        }
 
-                       public override Expression CreateExpressionTree (EmitContext ec)
+                       public override Expression CreateExpressionTree (ResolveContext ec)
                        {
                                // Should not be reached
                                throw new NotSupportedException ("ET");
                        }
 
-                       public override Expression DoResolve (EmitContext ec)
+                       protected override Expression DoResolve (ResolveContext ec)
                        {
                                return this;
                        }
 
-                       public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+                       public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                        {
                                return this;
                        }
@@ -9524,23 +9234,20 @@ namespace Mono.CSharp {
                        target.initializers = (CollectionOrObjectInitializers) initializers.Clone (clonectx);
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        Arguments args = new Arguments (2);
                        args.Add (new Argument (base.CreateExpressionTree (ec)));
                        if (!initializers.IsEmpty)
                                args.Add (new Argument (initializers.CreateExpressionTree (ec)));
 
-                       return CreateExpressionFactoryCall (
+                       return CreateExpressionFactoryCall (ec,
                                initializers.IsCollectionInitializer ? "ListInit" : "MemberInit",
                                args);
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-                       
                        Expression e = base.DoResolve (ec);
                        if (type == null)
                                return null;
@@ -9594,25 +9301,21 @@ namespace Mono.CSharp {
                                return !initializers.IsEmpty;
                        }
                }
-
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       base.MutateHoistedGenericType (storey);
-                       initializers.MutateHoistedGenericType (storey);
-               }
        }
 
-       public class AnonymousTypeDeclaration : Expression
+       public class NewAnonymousType : New
        {
-               ArrayList parameters;
+               static readonly AnonymousTypeParameter[] EmptyParameters = 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)
@@ -9620,54 +9323,70 @@ 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.CreateType ();
                        type.DefineType ();
+                       type.ResolveTypeParameters ();
                        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 (EmitContext ec)
+               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 (MemberCache.GetMember (type, p.Get.Spec), 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 (EmitContext 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.Definition, 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);
@@ -9676,59 +9395,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.Definition, 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 (EmitContext ec)
-               {
-                       throw new NotSupportedException ("ET");
-               }
-
                public override bool Equals (object o)
                {
                        AnonymousTypeParameter other = o as AnonymousTypeParameter;
@@ -9740,36 +9440,31 @@ namespace Mono.CSharp {
                        return Name.GetHashCode ();
                }
 
-               public override Expression DoResolve (EmitContext 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");
-               }
        }
 }