entry for a change in HttpResponseStream.cs
[mono.git] / mcs / mcs / expression.cs
index 8b4f747cc70ea2c4ba300f20571e9d6a07d7a212..84980efd5f599e287b40d3b870acd2b1e9f83c56 100644 (file)
 
 namespace Mono.CSharp {
        using System;
-       using System.Collections;
        using System.Collections.Generic;
        using System.Reflection;
        using System.Reflection.Emit;
        using System.Text;
-
-#if NET_4_0
        using System.Linq;
        using SLE = System.Linq.Expressions;
-#endif
 
        //
        // This is an user operator expression, automatically created during
@@ -40,7 +36,7 @@ namespace Mono.CSharp {
                        this.arguments = args;
                        this.expr_tree = expr_tree;
 
-                       type = TypeManager.TypeToCoreType (((MethodInfo) mg).ReturnType);
+                       type = TypeManager.TypeToCoreType (((MethodSpec) mg).ReturnType);
                        eclass = ExprClass.Value;
                        this.loc = loc;
                }
@@ -75,12 +71,11 @@ namespace Mono.CSharp {
                        mg.EmitCall (ec, arguments);
                }
 
-#if NET_4_0
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       return SLE.Expression.Call ((MethodInfo) mg, Arguments.MakeExpression (arguments, ctx));
+                       var method = ((MethodSpec) mg).MetaInfo as MethodInfo;
+                       return SLE.Expression.Call (method, Arguments.MakeExpression (arguments, ctx));
                }
-#endif
 
                public MethodGroupExpr Method {
                        get { return mg; }
@@ -128,11 +123,11 @@ namespace Mono.CSharp {
                public Expression Expr;
                Expression enum_conversion;
 
-               public Unary (Operator op, Expression expr)
+               public Unary (Operator op, Expression expr, Location loc)
                {
                        Oper = op;
                        Expr = expr;
-                       loc = expr.Location;
+                       this.loc = loc;
                }
 
                // <summary>
@@ -215,7 +210,7 @@ namespace Mono.CSharp {
                                if (expr_type == TypeManager.uint32_type) {
                                        UIntLiteral uil = e as UIntLiteral;
                                        if (uil != null) {
-                                               if (uil.Value == 2147483648)
+                                               if (uil.Value == int.MaxValue + (uint) 1)
                                                        return new IntLiteral (int.MinValue, e.Location);
                                                return new LongLiteral (-uil.Value, e.Location);
                                        }
@@ -447,7 +442,7 @@ namespace Mono.CSharp {
                        }
 
                        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.
@@ -596,7 +591,6 @@ namespace Mono.CSharp {
                        throw new NotImplementedException (oper.ToString ());
                }
 
-#if NET_4_0
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
                        var expr = Expr.MakeExpression (ctx);
@@ -607,13 +601,14 @@ namespace Mono.CSharp {
                                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 ());
                        }
                }
-#endif
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
@@ -993,10 +988,10 @@ namespace Mono.CSharp {
                // 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;
                }
 
@@ -1135,7 +1130,7 @@ namespace Mono.CSharp {
                                // 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);
+                               operation = new Binary (op, operation, one, loc);
                                operation = operation.Resolve (ec);
                                if (operation != null && operation.Type != type)
                                        operation = Convert.ExplicitNumericConversion (operation, type);
@@ -1692,7 +1687,6 @@ namespace Mono.CSharp {
        /// </summary>
        public class Binary : Expression, IDynamicBinder
        {
-
                protected class PredefinedOperator {
                        protected readonly Type left;
                        protected readonly Type right;
@@ -1740,17 +1734,21 @@ namespace Mono.CSharp {
                                if (left == TypeManager.decimal_type)
                                        return b.ResolveUserOperator (ec, b.left.Type, b.right.Type);
 
-                               var c = b.right as IntegralConstant;
+                               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);
+                                               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 IntegralConstant;
+                               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);
+                                               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;
                                }
 
@@ -1850,7 +1848,7 @@ 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
@@ -1983,18 +1981,18 @@ namespace Mono.CSharp {
                static PredefinedOperator [] standard_operators;
                static PredefinedOperator [] pointer_operators;
                
-               public Binary (Operator oper, Expression left, Expression right, bool isCompound)
-                       : this (oper, left, right)
+               public Binary (Operator oper, Expression left, Expression right, bool isCompound, Location loc)
+                       : this (oper, left, right, loc)
                {
                        this.is_compound = isCompound;
                }
 
-               public Binary (Operator oper, Expression left, Expression right)
+               public Binary (Operator oper, Expression left, Expression right, Location loc)
                {
                        this.oper = oper;
                        this.left = left;
                        this.right = right;
-                       this.loc = left.Location;
+                       this.loc = loc;
                }
 
                public Operator Oper {
@@ -2077,7 +2075,7 @@ namespace Mono.CSharp {
 
                public static void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right, Operator oper, Location loc)
                {
-                       new Binary (oper, left, right).Error_OperatorCannotBeApplied (ec, left, right);
+                       new Binary (oper, left, right, loc).Error_OperatorCannotBeApplied (ec, left, right);
                }
 
                public static void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right, string oper, Location loc)
@@ -2463,7 +2461,7 @@ namespace Mono.CSharp {
 
                static void CreatePointerOperatorsTable ()
                {
-                       ArrayList temp = new ArrayList ();
+                       var temp = new List<PredefinedPointerOperator> ();
 
                        //
                        // Pointer arithmetic:
@@ -2494,12 +2492,12 @@ namespace Mono.CSharp {
                        //
                        temp.Add (new PredefinedPointerOperator (null, Operator.SubtractionMask, TypeManager.int64_type));
 
-                       pointer_operators = (PredefinedOperator []) temp.ToArray (typeof (PredefinedOperator));
+                       pointer_operators = temp.ToArray ();
                }
 
                static void CreateStandardOperatorsTable ()
                {
-                       ArrayList temp = new ArrayList ();
+                       var temp = new List<PredefinedOperator> ();
                        Type bool_type = TypeManager.bool_type;
 
                        temp.Add (new PredefinedOperator (TypeManager.int32_type, Operator.ArithmeticMask | Operator.BitwiseMask));
@@ -2532,7 +2530,7 @@ 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 ();
                }
 
                //
@@ -2573,7 +2571,7 @@ namespace Mono.CSharp {
                                // A compile-time error occurs if the other operand is of type sbyte, short, int, or long
                                //
                                if (type == TypeManager.int32_type || type == TypeManager.int64_type ||
-                                       type == TypeManager.sbyte_type || type == TypeManager.sbyte_type)
+                                       type == TypeManager.short_type || type == TypeManager.sbyte_type)
                                        return false;
                        }
 
@@ -2729,7 +2727,6 @@ namespace Mono.CSharp {
                        return expr;
                }
 
-#if NET_4_0
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
                        var le = left.MakeExpression (ctx);
@@ -2777,7 +2774,6 @@ namespace Mono.CSharp {
                                throw new NotImplementedException (oper.ToString ());
                        }
                }
-#endif
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
@@ -2819,7 +2815,7 @@ namespace Mono.CSharp {
                        if (is_equality)
                                return ResolveUserOperator (ec, l, r);
 
-                       MethodInfo method;
+                       MethodSpec method;
                        Arguments args = new Arguments (2);
                        args.Add (new Argument (left));
                        args.Add (new Argument (right));
@@ -2840,7 +2836,7 @@ namespace Mono.CSharp {
                                method = TypeManager.delegate_remove_delegate_delegate;
                        }
 
-                       MethodGroupExpr mg = new MethodGroupExpr (new MemberInfo [] { method }, TypeManager.delegate_type, loc);
+                       MethodGroupExpr mg = new MethodGroupExpr (new [] { method }, TypeManager.delegate_type, loc);
                        mg = mg.OverloadResolve (ec, ref args, false, loc);
 
                        return new ClassCast (new UserOperatorCall (mg, args, CreateExpressionTree, loc), l);
@@ -3319,7 +3315,7 @@ namespace Mono.CSharp {
                                                if (left is NullLiteral || right is NullLiteral)
                                                        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
@@ -3682,7 +3678,7 @@ namespace Mono.CSharp {
 
                public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
                {
-                       Arguments binder_args = new Arguments (3);
+                       Arguments binder_args = new Arguments (4);
 
                        MemberAccess sle = new MemberAccess (new MemberAccess (
                                new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Linq", loc), "Expressions", loc);
@@ -3696,6 +3692,7 @@ namespace Mono.CSharp {
 
                        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 Invocation (DynamicExpressionStatement.GetBinder ("BinaryOperation", loc), binder_args);
@@ -3861,7 +3858,7 @@ namespace Mono.CSharp {
                        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);
                }
 
@@ -3913,7 +3910,6 @@ namespace Mono.CSharp {
                                concat.Emit (ec);
                }
 
-#if NET_4_0
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
                        if (arguments.Count != 2)
@@ -3922,7 +3918,6 @@ namespace Mono.CSharp {
                        var concat = TypeManager.string_type.GetMethod ("Concat", new[] { typeof (object), typeof (object) });
                        return SLE.Expression.Add (arguments[0].Expr.MakeExpression (ctx), arguments[1].Expr.MakeExpression (ctx), concat);
                }
-#endif
                
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
@@ -3947,13 +3942,13 @@ namespace Mono.CSharp {
                
                protected override Expression DoResolve (ResolveContext ec)
                {
-                       MethodInfo method = (MethodInfo)mg;
+                       var method = mg.BestCandidate;
                        type = TypeManager.TypeToCoreType (method.ReturnType);
-                       AParametersCollection pd = TypeManager.GetParameterData (method);
+                       AParametersCollection pd = method.Parameters;
                        if (!TypeManager.IsEqual (type, type) || !TypeManager.IsEqual (type, pd.Types [0]) || !TypeManager.IsEqual (type, pd.Types [1])) {
                                ec.Report.Error (217, loc,
                                        "A user-defined operator `{0}' must have parameters and return values of the same type in order to be applicable as a short circuit operator",
-                                       TypeManager.CSharpSignature (method));
+                                       TypeManager.CSharpSignature (method.MetaInfo));
                                return null;
                        }
 
@@ -3963,7 +3958,7 @@ namespace Mono.CSharp {
                        if (op_true == null || op_false == null) {
                                ec.Report.Error (218, loc,
                                        "The type `{0}' must have operator `true' and operator `false' defined when `{1}' is used as a short circuit operator",
-                                       TypeManager.CSharpName (type), TypeManager.CSharpSignature (method));
+                                       TypeManager.CSharpName (type), TypeManager.CSharpSignature (method.MetaInfo));
                                return null;
                        }
 
@@ -4036,7 +4031,7 @@ namespace Mono.CSharp {
                        } else {
                                FieldExpr fe = left as FieldExpr;
                                if (fe != null)
-                                       element = AttributeTester.GetFixedBuffer (fe.FieldInfo).ElementType;
+                                       element = ((FixedFieldSpec) (fe.Spec)).ElementType;
                                else
                                        element = op_type;
                        }
@@ -4078,7 +4073,7 @@ namespace Mono.CSharp {
 
                                left.Emit (ec);
 
-                               Constant right_const = right as Constant;
+                               var right_const = right as Constant;
                                if (right_const != null) {
                                        //
                                        // Optimize 0-based arithmetic
@@ -4086,16 +4081,16 @@ namespace Mono.CSharp {
                                        if (right_const.IsDefaultValue)
                                                return;
 
-                                       if (size != 0) {
-                                               // TODO: Should be the checks resolve context sensitive?
-                                               ResolveContext rc = new ResolveContext (ec.MemberContext);
-                                               right = ConstantFold.BinaryFold (rc, Binary.Operator.Multiply, new IntConstant (size, right.Location).Resolve (rc), 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);
@@ -4254,14 +4249,12 @@ namespace Mono.CSharp {
                                Expression conv = Convert.ImplicitConversion (ec, true_expr, false_type, loc);
                                if (conv != null) {
                                        //
-                                       // Check if both can convert implicitl to each other's type
+                                       // Check if both can convert implicitly to each other's type
                                        //
                                        if (Convert.ImplicitConversion (ec, false_expr, true_type, loc) != null) {
-                                               ec.Report.Error (172, loc,
-                                                          "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;
@@ -4269,7 +4262,7 @@ namespace Mono.CSharp {
                                } else if ((conv = Convert.ImplicitConversion (ec, false_expr, true_type, loc)) != null) {
                                        false_expr = conv;
                                } else {
-                                       ec.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}'",
                                                TypeManager.CSharpName (true_type), TypeManager.CSharpName (false_type));
                                        return null;
@@ -4281,7 +4274,7 @@ namespace Mono.CSharp {
                        if (c != null){
                                bool is_false = c.IsDefaultValue;
                                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);
+                               return ReducedExpression.Create (is_false ? false_expr : true_expr, this).Resolve (ec);
                        }
 
                        return this;
@@ -5005,7 +4998,7 @@ namespace Mono.CSharp {
                        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);
 
@@ -5102,24 +5095,24 @@ namespace Mono.CSharp {
                        return mg.OverloadResolve (ec, ref arguments, false, loc);
                }
 
-               public static bool IsSpecialMethodInvocation (ResolveContext ec, MethodBase method, Location loc)
+               public static bool IsSpecialMethodInvocation (ResolveContext ec, MethodSpec method, Location loc)
                {
-                       if (!TypeManager.IsSpecialMethod (method))
+                       if (!TypeManager.IsSpecialMethod (method.MetaInfo))
                                return false;
 
                        if (ec.HasSet (ResolveContext.Options.InvokeSpecialName))
                                return false;
 
-                       ec.Report.SymbolRelatedToPreviousError (method);
+                       ec.Report.SymbolRelatedToPreviousError (method.MetaInfo);
                        ec.Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
-                               TypeManager.CSharpSignature (method, true));
+                               TypeManager.CSharpSignature (method.MetaInfo, true));
        
                        return true;
                }
 
-               static Type[] GetVarargsTypes (MethodBase mb, Arguments arguments)
+               static Type[] GetVarargsTypes (MethodSpec mb, Arguments arguments)
                {
-                       AParametersCollection pd = TypeManager.GetParameterData (mb);
+                       AParametersCollection pd = mb.Parameters;
                        
                        Argument a = arguments [pd.Count - 1];
                        Arglist list = (Arglist) a.Expr;
@@ -5130,14 +5123,14 @@ namespace Mono.CSharp {
                /// <summary>
                /// This checks the ConditionalAttribute on the method 
                /// </summary>
-               public static bool IsMethodExcluded (MethodBase method, Location loc)
+               public static bool IsMethodExcluded (MethodSpec method, Location loc)
                {
                        if (method.IsConstructor)
                                return false;
 
-                       method = TypeManager.DropGenericMethodArguments (method);
-                       if (TypeManager.IsBeingCompiled (method)) {
-                               IMethodData md = TypeManager.GetMethod (method);
+                       var mb = TypeManager.DropGenericMethodArguments (method.MetaInfo);
+                       if (TypeManager.IsBeingCompiled (mb)) {
+                               IMethodData md = TypeManager.GetMethod (mb);
                                if (md != null)
                                        return md.IsExcluded ();
 
@@ -5146,7 +5139,7 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       return AttributeTester.IsConditionalMethodExcluded (method, loc);
+                       return AttributeTester.IsConditionalMethodExcluded (mb, loc);
                }
 
                /// <remarks>
@@ -5166,7 +5159,7 @@ namespace Mono.CSharp {
                /// </remarks>
                public static void EmitCall (EmitContext ec, bool is_base,
                                             Expression instance_expr,
-                                            MethodBase method, Arguments Arguments, Location loc)
+                                            MethodSpec method, Arguments Arguments, Location loc)
                {
                        EmitCall (ec, is_base, instance_expr, method, Arguments, loc, false, false);
                }
@@ -5179,7 +5172,7 @@ namespace Mono.CSharp {
                // only have been evaluated once.
                public static void EmitCall (EmitContext ec, bool is_base,
                                             Expression instance_expr,
-                                            MethodBase method, Arguments Arguments, Location loc,
+                                            MethodSpec method, Arguments Arguments, Location loc,
                                             bool dup_args, bool omit_args)
                {
                        ILGenerator ig = ec.ig;
@@ -5271,9 +5264,9 @@ namespace Mono.CSharp {
                                        ig.Emit (OpCodes.Constrained, instance_expr.Type);
                        }
 
-                       if ((method.CallingConvention & CallingConventions.VarArgs) != 0) {
+                       if ((method.MetaInfo.CallingConvention & CallingConventions.VarArgs) != 0) {
                                Type[] varargs_types = GetVarargsTypes (method, Arguments);
-                               ig.EmitCall (call_op, (MethodInfo) method, varargs_types);
+                               ig.EmitCall (call_op, (MethodInfo) method.MetaInfo, varargs_types);
                                return;
                        }
 
@@ -5283,10 +5276,10 @@ namespace Mono.CSharp {
                        // and DoFoo is not virtual, you can omit the callvirt,
                        // because you don't need the null checking behavior.
                        //
-                       if (method is MethodInfo)
-                               ig.Emit (call_op, (MethodInfo) method);
+                       if (method.IsConstructor)
+                               ig.Emit (call_op, (ConstructorInfo) method.MetaInfo);
                        else
-                               ig.Emit (call_op, (ConstructorInfo) method);
+                               ig.Emit (call_op, (MethodInfo) method.MetaInfo);
                }
 
                public override void Emit (EmitContext ec)
@@ -5315,18 +5308,16 @@ namespace Mono.CSharp {
                        target.expr = expr.Clone (clonectx);
                }
 
-#if NET_4_0
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       return MakeExpression (ctx, mg.InstanceExpression, (MethodInfo) mg, arguments);
+                       return MakeExpression (ctx, mg.InstanceExpression, (MethodSpec) mg, arguments);
                }
 
-               public static SLE.Expression MakeExpression (BuilderContext ctx, Expression instance, MethodInfo mi, Arguments args)
+               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, mi, Arguments.MakeExpression (args, ctx));
+                       return SLE.Expression.Call (instance_expr, (MethodInfo) mi.MetaInfo, Arguments.MakeExpression (args, ctx));
                }
-#endif
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
@@ -5495,7 +5486,7 @@ namespace Mono.CSharp {
                                }
 
                                if (TypeManager.activator_create_instance == null) {
-                                       Type activator_type = TypeManager.CoreLookupType (ec.Compiler, "System", "Activator", Kind.Class, true);
+                                       Type activator_type = TypeManager.CoreLookupType (ec.Compiler, "System", "Activator", MemberKind.Class, true);
                                        if (activator_type != null) {
                                                TypeManager.activator_create_instance = TypeManager.GetPredefinedMethod (
                                                        activator_type, "CreateInstance", loc, Type.EmptyTypes);
@@ -5571,8 +5562,8 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
 
-                       MethodInfo ci = TypeManager.activator_create_instance.MakeGenericMethod (
-                               new Type [] { type });
+                       MethodInfo ci = (MethodInfo) TypeManager.activator_create_instance.MetaInfo;
+                       ci = ci.MakeGenericMethod (new Type [] { type });
 
                        GenericConstraints gc = TypeManager.GetTypeParameterConstraints (type);
                        if (gc.HasReferenceTypeConstraint || gc.HasClassConstraint) {
@@ -5656,7 +5647,7 @@ namespace Mono.CSharp {
                                }
 
                                if (vr != null) {
-                                       ig.Emit (OpCodes.Call, (ConstructorInfo) method);
+                                       ig.Emit (OpCodes.Call, (ConstructorInfo) method.BestCandidate.MetaInfo);
                                        return false;
                                }
                        }
@@ -5664,7 +5655,7 @@ namespace Mono.CSharp {
                        if (is_type_parameter)
                                return DoEmitTypeParameter (ec);                        
 
-                       ConstructorInfo ci = (ConstructorInfo) method;
+                       ConstructorInfo ci = (ConstructorInfo) method.BestCandidate.MetaInfo;
 #if MS_COMPATIBLE
                        if (TypeManager.IsGenericType (type) && type.IsGenericTypeDefinition)
                                ci = TypeBuilder.GetConstructor (type, ci);
@@ -5738,7 +5729,7 @@ namespace Mono.CSharp {
                                if (Arguments != null)
                                        Arguments.Emit (ec);
 
-                               ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
+                               ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method.BestCandidate.MetaInfo);
                        }
                        
                        value_target.AddressOf (ec, mode);
@@ -5755,12 +5746,10 @@ namespace Mono.CSharp {
                        }
                }
 
-#if NET_4_0
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       return SLE.Expression.New ((ConstructorInfo) method, Arguments.MakeExpression (Arguments, ctx));
+                       return SLE.Expression.New ((ConstructorInfo) method.BestCandidate.MetaInfo, Arguments.MakeExpression (Arguments, ctx));
                }
-#endif
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
@@ -5775,6 +5764,57 @@ namespace Mono.CSharp {
                }
        }
 
+       public class ArrayInitializer : ShimExpression
+       {
+               List<Expression> elements;
+
+               public ArrayInitializer (List<Expression> init, Location loc)
+                       : base (null)
+               {
+                       elements = init;
+               }
+
+               public ArrayInitializer (int count, Location loc)
+                       : base (null)
+               {
+                       elements = new List<Expression> (count);
+               }
+
+               public ArrayInitializer (Location loc)
+                       : this (4, loc)
+               {
+               }
+
+               public void Add (Expression expr)
+               {
+                       elements.Add (expr);
+               }
+
+               protected override void CloneTo (CloneContext clonectx, Expression t)
+               {
+                       var target = (ArrayInitializer) t;
+
+                       target.elements = new List<Expression> (elements.Count);
+                       foreach (var element in elements)
+                               target.elements.Add (element.Clone (clonectx));
+
+                       base.CloneTo (clonectx, t);
+               }
+
+               public int Count {
+                       get { return elements.Count; }
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public Expression this [int index] {
+                       get { return elements [index]; }
+               }
+       }
+
        /// <summary>
        ///   14.5.10.2: Represents an array creation expression.
        /// </summary>
@@ -5785,15 +5825,16 @@ namespace Mono.CSharp {
        ///   initialization data and the other which does not need dimensions
        ///   specified but where initialization data is mandatory.
        /// </remarks>
-       public class ArrayCreation : Expression {
+       class ArrayCreation : Expression
+       {
                FullNamedExpression requested_base_type;
-               ArrayList initializers;
+               ArrayInitializer initializers;
 
                //
                // The list of Argument types.
                // This is used to construct the `newarray' or constructor signature
                //
-               protected ArrayList arguments;
+               protected List<Expression> arguments;
                
                protected Type array_element_type;
                bool expect_initializers = false;
@@ -5805,28 +5846,24 @@ namespace Mono.CSharp {
 
                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;
@@ -5846,10 +5883,10 @@ namespace Mono.CSharp {
                        ec.Report.Error (248, loc, "Cannot create an array with a negative size");
                }
 
-               bool CheckIndices (ResolveContext ec, ArrayList probe, int idx, bool specified_dims, int child_bounds)
+               bool CheckIndices (ResolveContext ec, ArrayInitializer probe, int idx, bool specified_dims, int child_bounds)
                {
                        if (specified_dims) { 
-                               Expression a = (Expression) arguments [idx];
+                               Expression a = arguments [idx];
                                a = a.Resolve (ec);
                                if (a == null)
                                        return false;
@@ -5876,9 +5913,9 @@ 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){
                                                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;
@@ -5888,9 +5925,9 @@ namespace Mono.CSharp {
                                        if (!ret)
                                                return false;
                                } else if (child_bounds > 1) {
-                                       ec.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;
 
@@ -5938,7 +5975,7 @@ namespace Mono.CSharp {
                                for (int i = 0; i < array_data.Count; ++i) {
                                        Expression e = array_data [i];
                                        if (e == null)
-                                               e = Convert.ImplicitConversion (ec, (Expression) initializers [i], array_element_type, loc);
+                                               e = Convert.ImplicitConversion (ec, initializers [i], array_element_type, loc);
 
                                        args.Add (new Argument (e.CreateExpressionTree (ec)));
                                }
@@ -5950,14 +5987,14 @@ namespace Mono.CSharp {
                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);
@@ -5997,12 +6034,12 @@ namespace Mono.CSharp {
                        // will need to store them in the byte blob later
                        //
                        array_data = new List<Expression> ();
-                       bounds = new System.Collections.Specialized.HybridDictionary ();
+                       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;
@@ -6017,11 +6054,6 @@ namespace Mono.CSharp {
                //
                bool ResolveArrayType (ResolveContext ec)
                {
-                       if (requested_base_type == 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;
-                       }
-
                        if (requested_base_type is VarExpr) {
                                ec.Report.Error (820, loc, "An implicitly typed local variable declarator cannot use an array initializer");
                                return false;
@@ -6050,6 +6082,11 @@ namespace Mono.CSharp {
                                return false;
 
                        type = array_type_expr.Type;
+                       if (!type.IsArray) {
+                               ec.Report.Error (622, loc, "Can only use array initializer expressions to assign to array types. Try using a new expression instead");
+                               return false;
+                       }
+
                        array_element_type = TypeManager.GetElementType (type);
                        dimensions = type.GetArrayRank ();
 
@@ -6072,7 +6109,7 @@ namespace Mono.CSharp {
                                return null;
 
                        for (int i = 0; i < arguments.Count; ++i) {
-                               Expression e = ((Expression) arguments[i]).Resolve (ec);
+                               Expression e = arguments[i].Resolve (ec);
                                if (e == null)
                                        continue;
 
@@ -6316,8 +6353,7 @@ namespace Mono.CSharp {
 
                        ig.Emit (OpCodes.Dup);
                        ig.Emit (OpCodes.Ldtoken, fb);
-                       ig.Emit (OpCodes.Call,
-                                TypeManager.void_initializearray_array_fieldhandle);
+                       ig.Emit (OpCodes.Call, (MethodInfo) TypeManager.void_initializearray_array_fieldhandle.MetaInfo);
                }
 
                //
@@ -6349,7 +6385,7 @@ namespace Mono.CSharp {
 
                        for (int i = 0; i < array_data.Count; i++){
 
-                               Expression e = (Expression)array_data [i];
+                               Expression e = array_data [i];
 
                                // Constant can be initialized via StaticInitializer
                                if (e != null && !(!emitConstants && e is Constant)) {
@@ -6392,7 +6428,7 @@ namespace Mono.CSharp {
                                //
                                for (int j = dims - 1; j >= 0; j--){
                                        current_pos [j]++;
-                                       if (current_pos [j] < (int) bounds [j])
+                                       if (current_pos [j] < bounds [j])
                                                break;
                                        current_pos [j] = 0;
                                }
@@ -6445,7 +6481,7 @@ namespace Mono.CSharp {
                        }
 
                        if (array_data == null) {
-                               Expression arg = (Expression) arguments[0];
+                               Expression arg = arguments [0];
                                object arg_value;
                                if (arg.GetAttributableValue (ec, arg.Type, out arg_value) && arg_value is int && (int)arg_value == 0) {
                                        value = Array.CreateInstance (array_element_type, 0);
@@ -6460,7 +6496,7 @@ namespace Mono.CSharp {
                        object element_value;
                        for (int i = 0; i < ret.Length; ++i)
                        {
-                               Expression e = (Expression)array_data [i];
+                               Expression e = array_data [i];
 
                                // Is null when an initializer is optimized (value == predefined value)
                                if (e == null) 
@@ -6484,33 +6520,22 @@ 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 (rank.Length > 2) {
@@ -6992,7 +7017,7 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        ec.ig.Emit (OpCodes.Ldtoken, TypeManager.TypeToReflectionType (typearg));
-                       ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
+                       ec.ig.Emit (OpCodes.Call, (MethodInfo) TypeManager.system_type_get_type_from_handle.MetaInfo);
                }
 
                public override bool GetAttributableValue (ResolveContext ec, Type value_type, out object value)
@@ -7016,7 +7041,8 @@ namespace Mono.CSharp {
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
-                       typearg = storey.MutateType (typearg);
+                       if (!TypeManager.IsGenericTypeDefinition (typearg))
+                               typearg = storey.MutateType (typearg);
                }
 
                public Type TypeArgument {
@@ -7051,23 +7077,23 @@ namespace Mono.CSharp {
                }
        }
 
-       class TypeOfMethod : TypeOfMember
+       class TypeOfMethod : TypeOfMember<MethodSpec>
        {
-               public TypeOfMethod (MethodBase method, Location loc)
+               public TypeOfMethod (MethodSpec method, Location loc)
                        : base (method, loc)
                {
                }
 
                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 (ec.Compiler, "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 (ec.Compiler, "System.Reflection", "ConstructorInfo", Kind.Class, true);
+                                       type = TypeManager.methodinfo_type = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", "MethodInfo", MemberKind.Class, true);
                        }
 
                        return base.DoResolve (ec);
@@ -7075,10 +7101,10 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       if (member is ConstructorInfo)
-                               ec.ig.Emit (OpCodes.Ldtoken, (ConstructorInfo) member);
+                       if (member.IsConstructor)
+                               ec.ig.Emit (OpCodes.Ldtoken, (ConstructorInfo) member.MetaInfo);
                        else
-                               ec.ig.Emit (OpCodes.Ldtoken, (MethodInfo) member);
+                               ec.ig.Emit (OpCodes.Ldtoken, (MethodInfo) member.MetaInfo);
 
                        base.Emit (ec);
                        ec.ig.Emit (OpCodes.Castclass, type);
@@ -7092,7 +7118,7 @@ namespace Mono.CSharp {
                        get { return "RuntimeMethodHandle"; }
                }
 
-               protected override MethodInfo TypeFromHandle {
+               protected override MethodSpec TypeFromHandle {
                        get {
                                return TypeManager.methodbase_get_type_from_handle;
                        }
@@ -7101,7 +7127,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               protected override MethodInfo TypeFromHandleGeneric {
+               protected override MethodSpec TypeFromHandleGeneric {
                        get {
                                return TypeManager.methodbase_get_type_from_handle_generic;
                        }
@@ -7115,11 +7141,11 @@ namespace Mono.CSharp {
                }
        }
 
-       abstract class TypeOfMember : Expression
+       abstract class TypeOfMember<T> : Expression where T : MemberSpec
        {
-               protected readonly MemberInfo member;
+               protected readonly T member;
 
-               protected TypeOfMember (MemberInfo member, Location loc)
+               protected TypeOfMember (T member, Location loc)
                {
                        this.member = member;
                        this.loc = loc;
@@ -7136,11 +7162,11 @@ namespace Mono.CSharp {
                protected override Expression DoResolve (ResolveContext ec)
                {
                        bool is_generic = TypeManager.IsGenericType (member.DeclaringType);
-                       MethodInfo mi = is_generic ? TypeFromHandleGeneric : TypeFromHandle;
+                       var mi = is_generic ? TypeFromHandleGeneric : TypeFromHandle;
 
                        if (mi == null) {
-                               Type t = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", TypeName, Kind.Class, true);
-                               Type handle_type = TypeManager.CoreLookupType (ec.Compiler, "System", RuntimeHandleName, Kind.Class, true);
+                               Type t = TypeManager.CoreLookupType (ec.Compiler, "System.Reflection", TypeName, MemberKind.Class, true);
+                               Type handle_type = TypeManager.CoreLookupType (ec.Compiler, "System", RuntimeHandleName, MemberKind.Class, true);
 
                                if (t == null || handle_type == null)
                                        return null;
@@ -7163,7 +7189,7 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        bool is_generic = TypeManager.IsGenericType (member.DeclaringType);
-                       MethodInfo mi;
+                       MethodSpec mi;
                        if (is_generic) {
                                mi = TypeFromHandleGeneric;
                                ec.ig.Emit (OpCodes.Ldtoken, member.DeclaringType);
@@ -7171,19 +7197,19 @@ namespace Mono.CSharp {
                                mi = TypeFromHandle;
                        }
 
-                       ec.ig.Emit (OpCodes.Call, mi);
+                       ec.ig.Emit (OpCodes.Call, (MethodInfo) mi.MetaInfo);
                }
 
                protected abstract string GetMethodName { get; }
                protected abstract string RuntimeHandleName { get; }
-               protected abstract MethodInfo TypeFromHandle { get; set; }
-               protected abstract MethodInfo TypeFromHandleGeneric { get; set; }
+               protected abstract MethodSpec TypeFromHandle { get; set; }
+               protected abstract MethodSpec TypeFromHandleGeneric { get; set; }
                protected abstract string TypeName { get; }
        }
 
-       class TypeOfField : TypeOfMember
+       class TypeOfField : TypeOfMember<FieldSpec>
        {
-               public TypeOfField (FieldInfo field, Location loc)
+               public TypeOfField (FieldSpec field, Location loc)
                        : base (field, loc)
                {
                }
@@ -7191,7 +7217,7 @@ namespace Mono.CSharp {
                protected override Expression DoResolve (ResolveContext ec)
                {
                        if (TypeManager.fieldinfo_type == null)
-                               TypeManager.fieldinfo_type = TypeManager.CoreLookupType (ec.Compiler, "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);
@@ -7199,7 +7225,7 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       ec.ig.Emit (OpCodes.Ldtoken, (FieldInfo) member);
+                       ec.ig.Emit (OpCodes.Ldtoken, member.MetaInfo);
                        base.Emit (ec);
                }
 
@@ -7211,7 +7237,7 @@ namespace Mono.CSharp {
                        get { return "RuntimeFieldHandle"; }
                }
 
-               protected override MethodInfo TypeFromHandle {
+               protected override MethodSpec TypeFromHandle {
                        get {
                                return TypeManager.fieldinfo_get_field_from_handle;
                        }
@@ -7220,7 +7246,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               protected override MethodInfo TypeFromHandleGeneric {
+               protected override MethodSpec TypeFromHandleGeneric {
                        get {
                                return TypeManager.fieldinfo_get_field_from_handle_generic;
                        }
@@ -7759,14 +7785,12 @@ namespace Mono.CSharp {
                                Expr.EmitBranchable (ec, target, on_true);
                }
 
-#if NET_4_0
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
                        using (ctx.With (BuilderContext.Options.AllCheckStateFlags, true)) {
                                return Expr.MakeExpression (ctx);
                        }
                }
-#endif
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
@@ -7906,7 +7930,7 @@ namespace Mono.CSharp {
 
                        FieldExpr fe = Expr as FieldExpr;
                        if (fe != null) {
-                               IFixedBuffer ff = AttributeTester.GetFixedBuffer (fe.FieldInfo);
+                               var ff = fe.Spec as FixedFieldSpec;
                                if (ff != null) {
                                        return MakePointerAccess (ec, ff.ElementType);
                                }
@@ -8304,6 +8328,7 @@ namespace Mono.CSharp {
                                ea.Expr.MakeExpression (ctx),
                                Arguments.MakeExpression (ea.Arguments, ctx));
                }
+#endif
 
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
@@ -8311,7 +8336,6 @@ namespace Mono.CSharp {
                                ea.Expr.MakeExpression (ctx),
                                Arguments.MakeExpression (ea.Arguments, ctx));
                }
-#endif
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
@@ -8330,7 +8354,7 @@ namespace Mono.CSharp {
                        public IndexerMethodGroupExpr (Indexers indexers, Location loc)
                                : base (null, loc)
                        {
-                               Methods = (MethodBase []) indexers.Methods.ToArray (typeof (MethodBase));
+                               Methods = indexers.Methods.ToArray ();
                        }
 
                        public override string Name {
@@ -8339,7 +8363,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       protected override int GetApplicableParametersCount (MethodBase method, AParametersCollection parameters)
+                       protected override int GetApplicableParametersCount (MethodSpec method, AParametersCollection parameters)
                        {
                                //
                                // Here is the trick, decrease number of arguments by 1 when only
@@ -8357,8 +8381,8 @@ namespace Mono.CSharp {
                class Indexers
                {
                        // Contains either property getter or setter
-                       public ArrayList Methods;
-                       public ArrayList Properties;
+                       public List<MethodSpec> Methods;
+                       public List<PropertyInfo> Properties;
 
                        Indexers ()
                        {
@@ -8375,11 +8399,11 @@ namespace Mono.CSharp {
                                                accessor = property.GetSetMethod (true);
 
                                        if (Methods == null) {
-                                               Methods = new ArrayList ();
-                                               Properties = new ArrayList ();
+                                               Methods = new List<MethodSpec> ();
+                                               Properties = new List<PropertyInfo> ();
                                        }
 
-                                       Methods.Add (accessor);
+                                       Methods.Add (Import.CreateMethod (accessor));
                                        Properties.Add (property);
                                }
                        }
@@ -8439,7 +8463,7 @@ namespace Mono.CSharp {
                //
                // Points to our "data" repository
                //
-               MethodInfo get, set;
+               MethodSpec get, set;
                bool is_base_indexer;
                bool prepared;
                LocalTemporary temp;
@@ -8549,10 +8573,10 @@ namespace Mono.CSharp {
                                return expr.Resolve (ec);
                        }
 
-                       MethodInfo mi = (MethodInfo) mg;
+                       var mi = (MethodSpec) mg;
                        PropertyInfo pi = null;
                        for (int i = 0; i < ilist.Methods.Count; ++i) {
-                               if (ilist.Methods [i] == mi) {
+                               if (ilist.Methods [i].MetaInfo == mi.MetaInfo) {
                                        pi = (PropertyInfo) ilist.Properties [i];
                                        break;
                                }
@@ -8562,11 +8586,15 @@ namespace Mono.CSharp {
                        if (type.IsPointer && !ec.IsUnsafe)
                                UnsafeError (ec, loc);
 
-                       MethodInfo accessor;
+                       MethodSpec accessor = null;
                        if (right_side == null) {
-                               accessor = get = pi.GetGetMethod (true);
+                               var m = pi.GetGetMethod (true);
+                               if (m != null)
+                                       accessor = get = Import.CreateMethod (m);
                        } else {
-                               accessor = set = pi.GetSetMethod (true);
+                               var m = pi.GetSetMethod (true);
+                               if (m != null)
+                                       accessor = set = Import.CreateMethod (m);
                                if (accessor == null && pi.GetGetMethod (true) != null) {
                                        ec.Report.SymbolRelatedToPreviousError (pi);
                                        ec.Report.Error (200, loc, "The read only property or indexer `{0}' cannot be assigned to",
@@ -8593,14 +8621,19 @@ namespace Mono.CSharp {
 
                        bool must_do_cs1540_check;
                        if (!IsAccessorAccessible (ec.CurrentType, accessor, out must_do_cs1540_check)) {
-                               if (set == null)
-                                       set = pi.GetSetMethod (true);
-                               else
-                                       get = pi.GetGetMethod (true);
+                               if (set == null) {
+                                       var m = pi.GetSetMethod (true);
+                                       if (m != null)
+                                               set = Import.CreateMethod (m);
+                               } else {
+                                       var m = pi.GetGetMethod (true);
+                                       if (m != null)
+                                               get = Import.CreateMethod (m);
+                               }
 
                                if (set != null && get != null &&
-                                       (set.Attributes & MethodAttributes.MemberAccessMask) != (get.Attributes & MethodAttributes.MemberAccessMask)) {
-                                       ec.Report.SymbolRelatedToPreviousError (accessor);
+                                       (set.MetaInfo.Attributes & MethodAttributes.MemberAccessMask) != (get.MetaInfo.Attributes & MethodAttributes.MemberAccessMask)) {
+                                       ec.Report.SymbolRelatedToPreviousError (accessor.MetaInfo);
                                        ec.Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because a `{1}' accessor is inaccessible",
                                                TypeManager.GetFullNameSignature (pi), GetAccessorName (right_side != null));
                                } else {
@@ -8610,6 +8643,16 @@ namespace Mono.CSharp {
                        }
 
                        instance_expr.CheckMarshalByRefAccess (ec);
+
+                       if (must_do_cs1540_check && (instance_expr != EmptyExpression.Null) &&
+                           !TypeManager.IsInstantiationOfSameGenericType (instance_expr.Type, ec.CurrentType) &&
+                           !TypeManager.IsNestedChildOf (ec.CurrentType, instance_expr.Type) &&
+                           !TypeManager.IsSubclassOf (instance_expr.Type, ec.CurrentType)) {
+                               ec.Report.SymbolRelatedToPreviousError (accessor.MetaInfo);
+                               Error_CannotAccessProtected (ec, loc, accessor.MetaInfo, instance_expr.Type, ec.CurrentType);
+                               return null;
+                       }
+
                        eclass = ExprClass.IndexerAccess;
                        return this;
                }
@@ -8679,7 +8722,7 @@ namespace Mono.CSharp {
                
                public override string GetSignatureForError ()
                {
-                       return TypeManager.CSharpSignature (get != null ? get : set, false);
+                       return TypeManager.CSharpSignature (get != null ? get.MetaInfo : set.MetaInfo, false);
                }
 
 #if NET_4_0
@@ -8689,23 +8732,23 @@ namespace Mono.CSharp {
                        var args = Arguments.MakeExpression (arguments, ctx).Concat (value);
 
                        return SLE.Expression.Block (
-                                       SLE.Expression.Call (instance_expr.MakeExpression (ctx), set, args),
+                                       SLE.Expression.Call (instance_expr.MakeExpression (ctx), (MethodInfo) set.MetaInfo, args),
                                        value [0]);
                }
+#endif
 
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
                        var args = Arguments.MakeExpression (arguments, ctx);
-                       return SLE.Expression.Call (instance_expr.MakeExpression (ctx), get, args);
+                       return SLE.Expression.Call (instance_expr.MakeExpression (ctx), (MethodInfo) get.MetaInfo, args);
                }
-#endif
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
                        if (get != null)
-                               get = storey.MutateGenericMethod (get);
+                               storey.MutateGenericMethod (get);
                        if (set != null)
-                               set = storey.MutateGenericMethod (set);
+                               storey.MutateGenericMethod (set);
 
                        instance_expr.MutateHoistedGenericType (storey);
                        if (arguments != null)
@@ -9000,10 +9043,10 @@ namespace Mono.CSharp {
        }       
 
        public class UserCast : Expression {
-               MethodInfo method;
+               MethodSpec method;
                Expression source;
                
-               public UserCast (MethodInfo method, Expression source, Location l)
+               public UserCast (MethodSpec method, Expression source, Location l)
                {
                        this.method = method;
                        this.source = source;
@@ -9028,7 +9071,7 @@ namespace Mono.CSharp {
                        
                protected override Expression DoResolve (ResolveContext ec)
                {
-                       ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (method);
+                       ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (method.MetaInfo);
                        if (oa != null)
                                AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, ec.Report);
 
@@ -9039,25 +9082,23 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        source.Emit (ec);
-                       ec.ig.Emit (OpCodes.Call, method);
+                       ec.ig.Emit (OpCodes.Call, (MethodInfo) method.MetaInfo);
                }
 
                public override string GetSignatureForError ()
                {
-                       return TypeManager.CSharpSignature (method);
+                       return TypeManager.CSharpSignature (method.MetaInfo);
                }
 
-#if NET_4_0
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       return SLE.Expression.Convert (source.MakeExpression (ctx), type, method);
+                       return SLE.Expression.Convert (source.MakeExpression (ctx), type, (MethodInfo) method.MetaInfo);
                }
-#endif
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
                        source.MutateHoistedGenericType (storey);
-                       method = storey.MutateGenericMethod (method);
+                       storey.MutateGenericMethod (method);
                }
        }
 
@@ -9451,7 +9492,7 @@ namespace Mono.CSharp {
                        this.loc = argument.Location;
                }
 
-               public CollectionElementInitializer (ArrayList arguments, Location loc)
+               public CollectionElementInitializer (List<Expression> arguments, Location loc)
                        : base (null, new Arguments (arguments.Count))
                {
                        foreach (Expression e in arguments)
@@ -9465,7 +9506,7 @@ namespace Mono.CSharp {
                        Arguments args = new Arguments (2);
                        args.Add (new Argument (mg.CreateExpressionTree (ec)));
 
-                       ArrayList expr_initializers = new ArrayList (arguments.Count);
+                       var expr_initializers = new ArrayInitializer (arguments.Count, loc);
                        foreach (Argument a in arguments)
                                expr_initializers.Add (a.CreateExpressionTree (ec));
 
@@ -9494,13 +9535,13 @@ namespace Mono.CSharp {
        //
        public class CollectionOrObjectInitializers : ExpressionStatement
        {
-               ArrayList initializers;
+               IList<Expression> initializers;
                bool is_collection_initialization;
                
                public static readonly CollectionOrObjectInitializers Empty = 
-                       new CollectionOrObjectInitializers (new ArrayList (0), Location.Null);
+                       new CollectionOrObjectInitializers (Array.AsReadOnly (new Expression [0]), Location.Null);
 
-               public CollectionOrObjectInitializers (ArrayList initializers, Location loc)
+               public CollectionOrObjectInitializers (IList<Expression> initializers, Location loc)
                {
                        this.initializers = initializers;
                        this.loc = loc;
@@ -9522,14 +9563,14 @@ namespace Mono.CSharp {
                {
                        CollectionOrObjectInitializers t = (CollectionOrObjectInitializers) target;
 
-                       t.initializers = new ArrayList (initializers.Count);
-                       foreach (Expression e in initializers)
+                       t.initializers = new List<Expression> (initializers.Count);
+                       foreach (var e in initializers)
                                t.initializers.Add (e.Clone (clonectx));
                }
 
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       ArrayList expr_initializers = new ArrayList (initializers.Count);
+                       var expr_initializers = new ArrayInitializer (initializers.Count, loc);
                        foreach (Expression e in initializers) {
                                Expression expr = e.CreateExpressionTree (ec);
                                if (expr != null)
@@ -9541,14 +9582,14 @@ namespace Mono.CSharp {
                
                protected override Expression DoResolve (ResolveContext ec)
                {
-                       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);
@@ -9777,13 +9818,13 @@ namespace Mono.CSharp {
 
        public class NewAnonymousType : New
        {
-               static readonly ArrayList EmptyParameters = new ArrayList (0);
+               static readonly IList<AnonymousTypeParameter> EmptyParameters = Array.AsReadOnly (new AnonymousTypeParameter[0]);
 
-               ArrayList parameters;
+               List<AnonymousTypeParameter> parameters;
                readonly TypeContainer parent;
                AnonymousTypeClass anonymous_type;
 
-               public NewAnonymousType (ArrayList parameters, TypeContainer parent, Location loc)
+               public NewAnonymousType (List<AnonymousTypeParameter> parameters, TypeContainer parent, Location loc)
                         : base (null, null, loc)
                {
                        this.parameters = parameters;
@@ -9796,12 +9837,12 @@ namespace Mono.CSharp {
                                return;
 
                        NewAnonymousType t = (NewAnonymousType) target;
-                       t.parameters = new ArrayList (parameters.Count);
+                       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 (ResolveContext ec, ArrayList parameters)
+               AnonymousTypeClass CreateAnonymousType (ResolveContext ec, IList<AnonymousTypeParameter> parameters)
                {
                        AnonymousTypeClass type = parent.Module.Compiled.GetAnonymousType (parameters);
                        if (type != null)
@@ -9826,11 +9867,11 @@ namespace Mono.CSharp {
                        if (parameters == null)
                                return base.CreateExpressionTree (ec);
 
-                       ArrayList init = new ArrayList (parameters.Count);
+                       var init = new ArrayInitializer (parameters.Count, loc);
                        foreach (Property p in anonymous_type.Properties)
-                               init.Add (new TypeOfMethod (TypeBuilder.GetMethod (type, p.GetBuilder), loc));
+                               init.Add (new TypeOfMethod (Import.CreateMethod (TypeBuilder.GetMethod (type, p.GetBuilder)), loc));
 
-                       ArrayList ctor_args = new ArrayList (Arguments.Count);
+                       var ctor_args = new ArrayInitializer (Arguments.Count, loc);
                        foreach (Argument a in Arguments)
                                ctor_args.Add (a.CreateExpressionTree (ec));