2010-02-24 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / ecore.cs
index 429dc6dc07659113c606f0ca9d36e25118806433..31c8778cf5ab8a5e03e34db1befa5c8d1543b73f 100644 (file)
 //
 //
 
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Reflection;
+using System.Reflection.Emit;
+using System.Text;
+using SLE = System.Linq.Expressions;
+using System.Linq;
+
 namespace Mono.CSharp {
-       using System;
-       using System.Collections;
-       using System.Diagnostics;
-       using System.Reflection;
-       using System.Reflection.Emit;
-       using System.Text;
-
-#if NET_4_0
-       using SLE = System.Linq.Expressions;
-#endif
 
        /// <remarks>
        ///   The ExprClass class contains the is used to pass the 
@@ -29,7 +28,7 @@ namespace Mono.CSharp {
        ///   nothing).
        /// </remarks>
        public enum ExprClass : byte {
-               Invalid,
+               Unresolved      = 0,
                
                Value,
                Variable,
@@ -63,17 +62,8 @@ namespace Mono.CSharp {
                // Mask of all the expression class flags.
                MaskExprClass = VariableOrValue | Type | MethodGroup | TypeParameter,
 
-               // Disable control flow analysis while resolving the expression.
-               // This is used when resolving the instance expression of a field expression.
-               DisableFlowAnalysis     = 1 << 10,
-
                // Set if this is resolving the first part of a MemberAccess.
                Intermediate            = 1 << 11,
-
-               // Disable control flow analysis _of struct_ while resolving the expression.
-               // This is used when resolving the instance expression of a field expression.
-               DisableStructFlowAnalysis       = 1 << 12,
-
        }
 
        //
@@ -159,28 +149,26 @@ namespace Mono.CSharp {
                        return TypeManager.CSharpName (type);
                }
 
-               public static bool IsAccessorAccessible (Type invocation_type, MethodInfo mi, out bool must_do_cs1540_check)
+               public static bool IsAccessorAccessible (Type invocation_type, MethodSpec mi, out bool must_do_cs1540_check)
                {
-                       MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
+                       var ma = mi.Modifiers & Modifiers.AccessibilityMask;
 
                        must_do_cs1540_check = false; // by default we do not check for this
 
-                       if (ma == MethodAttributes.Public)
+                       if (ma == Modifiers.PUBLIC)
                                return true;
                        
                        //
                        // If only accessible to the current class or children
                        //
-                       if (ma == MethodAttributes.Private)
+                       if (ma == Modifiers.PRIVATE)
                                return TypeManager.IsPrivateAccessible (invocation_type, mi.DeclaringType) ||
                                        TypeManager.IsNestedChildOf (invocation_type, mi.DeclaringType);
 
-                       if (TypeManager.IsThisOrFriendAssembly (invocation_type.Assembly, mi.DeclaringType.Assembly)) {
-                               if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamORAssem)
-                                       return true;
-                       } else {
-                               if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamANDAssem)
-                                       return false;
+                       if ((ma & Modifiers.INTERNAL) != 0) {
+                               var b = TypeManager.IsThisOrFriendAssembly (invocation_type.Assembly, mi.DeclaringType.Assembly);
+                               if (b || ma == Modifiers.INTERNAL)
+                                       return b;
                        }
 
                        // Family and FamANDAssem require that we derive.
@@ -227,9 +215,9 @@ namespace Mono.CSharp {
                ///   to a valid type (this is the type of the
                ///   expression).
                /// </remarks>
-               public abstract Expression DoResolve (ResolveContext ec);
+               protected abstract Expression DoResolve (ResolveContext rc);
 
-               public virtual Expression DoResolveLValue (ResolveContext ec, Expression right_side)
+               public virtual Expression DoResolveLValue (ResolveContext rc, Expression right_side)
                {
                        return null;
                }
@@ -341,6 +329,17 @@ namespace Mono.CSharp {
 
                }
 
+               public void Error_ExpressionMustBeConstant (ResolveContext rc, Location loc, string e_name)
+               {
+                       rc.Report.Error (133, loc, "The expression being assigned to `{0}' must be constant", e_name);
+               }
+
+               public void Error_ConstantCanBeInitializedWithNullOnly (ResolveContext rc, Type type, Location loc, string name)
+               {
+                       rc.Report.Error (134, loc, "A constant `{0}' of reference type `{1}' can only be initialized with null",
+                               name, TypeManager.CSharpName (type));
+               }
+
                public static void Error_InvalidExpressionStatement (Report Report, Location loc)
                {
                        Report.Error (201, loc, "Only assignment, call, increment, decrement, and new object " +
@@ -446,8 +445,7 @@ namespace Mono.CSharp {
                        ec.Report.Error (131, loc, "The left-hand side of an assignment must be a variable, a property or an indexer");
                }
 
-               ResolveFlags ExprClassToResolveFlags
-               {
+               ResolveFlags ExprClassToResolveFlags {
                        get {
                                switch (eclass) {
                                case ExprClass.Type:
@@ -483,24 +481,14 @@ namespace Mono.CSharp {
                /// </remarks>
                public Expression Resolve (ResolveContext ec, ResolveFlags flags)
                {
-                       if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) 
-                               return ResolveAsTypeStep (ec, false);
-
-                       bool do_flow_analysis = ec.DoFlowAnalysis;
-                       bool omit_struct_analysis = ec.OmitStructFlowAnalysis;
-                       if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
-                               do_flow_analysis = false;
-                       if ((flags & ResolveFlags.DisableStructFlowAnalysis) != 0)
-                               omit_struct_analysis = true;
+                       if (eclass != ExprClass.Unresolved)
+                               return this;
 
                        Expression e;
-                       using (ec.WithFlowAnalysis (do_flow_analysis, omit_struct_analysis)) {
-                               if (this is SimpleName) {
-                                       bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate;
-                                       e = ((SimpleName) this).DoResolve (ec, intermediate);
-                               } else {
-                                       e = DoResolve (ec);
-                               }
+                       if (this is SimpleName) {
+                               e = ((SimpleName) this).DoResolve (ec, (flags & ResolveFlags.Intermediate) != 0);
+                       } else {
+                               e = DoResolve (ec);
                        }
 
                        if (e == null)
@@ -511,12 +499,8 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       if (e.type == null && !(e is Namespace)) {
-                               throw new Exception (
-                                       "Expression " + e.GetType () +
-                                       " did not set its type after Resolve\n" +
-                                       "called from: " + this.GetType ());
-                       }
+                       if (e.type == null)
+                               throw new InternalErrorException ("Expression `{0}' didn't set its type in DoResolve", e.GetType ());
 
                        return e;
                }
@@ -524,33 +508,9 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Resolves an expression and performs semantic analysis on it.
                /// </summary>
-               public Expression Resolve (ResolveContext ec)
-               {
-                       Expression e = Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
-
-                       if (e != null && e.eclass == ExprClass.MethodGroup && RootContext.Version == LanguageVersion.ISO_1) {
-                               ((MethodGroupExpr) e).ReportUsageError (ec);
-                               return null;
-                       }
-                       return e;
-               }
-
-               public Constant ResolveAsConstant (ResolveContext ec, MemberCore mc)
+               public Expression Resolve (ResolveContext rc)
                {
-                       Expression e = Resolve (ec);
-                       if (e == null)
-                               return null;
-
-                       Constant c = e as Constant;
-                       if (c != null)
-                               return c;
-
-                       if (type != null && TypeManager.IsReferenceType (type))
-                               Const.Error_ConstantCanBeInitializedWithNullOnly (type, loc, mc.GetSignatureForError (), ec.Report);
-                       else
-                               Const.Error_ExpressionMustBeConstant (loc, mc.GetSignatureForError (), ec.Report);
-
-                       return null;
+                       return Resolve (rc, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
                }
 
                /// <summary>
@@ -564,7 +524,7 @@ namespace Mono.CSharp {
                public Expression ResolveLValue (ResolveContext ec, Expression right_side)
                {
                        int errors = ec.Report.Errors;
-                       bool out_access = right_side == EmptyExpression.OutAccess;
+                       bool out_access = right_side == EmptyExpression.OutAccess.Instance;
 
                        Expression e = DoResolveLValue (ec, right_side);
 
@@ -587,7 +547,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       if (e.eclass == ExprClass.Invalid)
+                       if (e.eclass == ExprClass.Unresolved)
                                throw new Exception ("Expression " + e + " ExprClass is Invalid after resolve");
 
                        if ((e.type == null) && !(e is GenericTypeExpr))
@@ -632,8 +592,6 @@ namespace Mono.CSharp {
 
                protected Expression ()
                {
-                       eclass = ExprClass.Invalid;
-                       type = null;
                }
 
                /// <summary>
@@ -643,14 +601,15 @@ namespace Mono.CSharp {
                public static Expression ExprClassFromMemberInfo (Type container_type, MemberInfo mi, Location loc)
                {
                        if (mi is EventInfo)
-                               return new EventExpr ((EventInfo) mi, loc);
+                               return new EventExpr (Import.CreateEvent ((EventInfo) mi), loc);
                        else if (mi is FieldInfo) {
                                FieldInfo fi = (FieldInfo) mi;
-                               if (fi.IsLiteral || (fi.IsInitOnly && fi.FieldType == TypeManager.decimal_type))
-                                       return new ConstantExpr (fi, loc);
-                               return new FieldExpr (fi, loc);
+                               var spec = Import.CreateField (fi);
+                               if (spec is ConstSpec)
+                                       return new ConstantExpr ((ConstSpec) spec, loc);
+                               return new FieldExpr (spec, loc);
                        } else if (mi is PropertyInfo)
-                               return new PropertyExpr (container_type, (PropertyInfo) mi, loc);
+                               return new PropertyExpr (container_type, Import.CreateProperty ((PropertyInfo) mi), loc);
                        else if (mi is Type) {
                                return new TypeExpression ((System.Type) mi, loc);
                        }
@@ -659,7 +618,7 @@ namespace Mono.CSharp {
                }
 
                // TODO: [Obsolete ("Can be removed")]
-               protected static ArrayList almost_matched_members = new ArrayList (4);
+               protected static IList<MemberInfo> almost_matched_members = new List<MemberInfo> (4);
 
                //
                // FIXME: Probably implement a cache for (t,name,current_access_set)?
@@ -715,21 +674,21 @@ namespace Mono.CSharp {
 
                        if (mi.Length > 1) {
                                bool is_interface = qualifier_type != null && qualifier_type.IsInterface;
-                               ArrayList methods = new ArrayList (2);
-                               ArrayList non_methods = null;
+                               var methods = new List<MethodSpec> (2);
+                               List<MemberInfo> non_methods = null;
 
-                               foreach (MemberInfo m in mi) {
+                               foreach (var m in mi) {
                                        if (m is MethodBase) {
-                                               methods.Add (m);
+                                               methods.Add (Import.CreateMethod ((MethodBase) m));
                                                continue;
                                        }
 
                                        if (non_methods == null)
-                                               non_methods = new ArrayList (2);
+                                               non_methods = new List<MemberInfo> (2);
 
                                        bool is_candidate = true;
                                        for (int i = 0; i < non_methods.Count; ++i) {
-                                               MemberInfo n_m = (MemberInfo) non_methods [i];
+                                               MemberInfo n_m = non_methods [i];
                                                if (n_m.DeclaringType.IsInterface && TypeManager.ImplementsInterface (m.DeclaringType, n_m.DeclaringType)) {
                                                        non_methods.Remove (n_m);
                                                        --i;
@@ -745,11 +704,11 @@ namespace Mono.CSharp {
                                }
                                
                                if (methods.Count == 0 && non_methods != null && non_methods.Count > 1) {
-                                       ctx.Report.SymbolRelatedToPreviousError ((MemberInfo)non_methods [1]);
-                                       ctx.Report.SymbolRelatedToPreviousError ((MemberInfo)non_methods [0]);
+                                       ctx.Report.SymbolRelatedToPreviousError (non_methods [1]);
+                                       ctx.Report.SymbolRelatedToPreviousError (non_methods [0]);
                                        ctx.Report.Error (229, loc, "Ambiguity between `{0}' and `{1}'",
-                                               TypeManager.GetFullNameSignature ((MemberInfo)non_methods [1]),
-                                               TypeManager.GetFullNameSignature ((MemberInfo)non_methods [0]));
+                                               TypeManager.GetFullNameSignature (non_methods [1]),
+                                               TypeManager.GetFullNameSignature (non_methods [0]));
                                        return null;
                                }
 
@@ -757,23 +716,23 @@ namespace Mono.CSharp {
                                        return ExprClassFromMemberInfo (container_type, (MemberInfo)non_methods [0], loc);
 
                                if (non_methods != null && non_methods.Count > 0) {
-                                       MethodBase method = (MethodBase) methods [0];
+                                       var method = methods [0];
                                        MemberInfo non_method = (MemberInfo) non_methods [0];
                                        if (method.DeclaringType == non_method.DeclaringType) {
                                                // Cannot happen with C# code, but is valid in IL
-                                               ctx.Report.SymbolRelatedToPreviousError (method);
+                                               ctx.Report.SymbolRelatedToPreviousError (method.MetaInfo);
                                                ctx.Report.SymbolRelatedToPreviousError (non_method);
                                                ctx.Report.Error (229, loc, "Ambiguity between `{0}' and `{1}'",
                                                              TypeManager.GetFullNameSignature (non_method),
-                                                             TypeManager.CSharpSignature (method));
+                                                             TypeManager.CSharpSignature (method.MetaInfo));
                                                return null;
                                        }
 
                                        if (is_interface) {
-                                               ctx.Report.SymbolRelatedToPreviousError (method);
+                                               ctx.Report.SymbolRelatedToPreviousError (method.MetaInfo);
                                                ctx.Report.SymbolRelatedToPreviousError (non_method);
                                                ctx.Report.Warning (467, 2, loc, "Ambiguity between method `{0}' and non-method `{1}'. Using method `{0}'",
-                                                               TypeManager.CSharpSignature (method), TypeManager.GetFullNameSignature (non_method));
+                                                               TypeManager.CSharpSignature (method.MetaInfo), TypeManager.GetFullNameSignature (non_method));
                                        }
                                }
 
@@ -781,7 +740,7 @@ namespace Mono.CSharp {
                        }
 
                        if (mi [0] is MethodBase)
-                               return new MethodGroupExpr (mi, queried_type, loc);
+                               return new MethodGroupExpr (mi.Select (l => Import.CreateMethod ((MethodBase) l)).ToArray (), queried_type, loc);
 
                        return ExprClassFromMemberInfo (container_type, mi [0], loc);
                }
@@ -918,13 +877,16 @@ namespace Mono.CSharp {
 
                protected virtual Expression Error_MemberLookupFailed (ResolveContext ec, Type type, MemberInfo[] members)
                {
+                       List<MethodSpec> methods = new List<MethodSpec> ();
                        for (int i = 0; i < members.Length; ++i) {
                                if (!(members [i] is MethodBase))
                                        return null;
+
+                               methods.Add (Import.CreateMethod (members[i] as MethodBase));
                        }
 
                        // By default propagate the closest candidates upwards
-                       return new MethodGroupExpr (members, type, loc, true);
+                       return new MethodGroupExpr (methods.ToArray (), type, loc, true);
                }
 
                protected virtual void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
@@ -978,8 +940,8 @@ namespace Mono.CSharp {
                {
                        get {
                                switch (eclass){
-                               case ExprClass.Invalid:
-                                       return "Invalid";
+                               case ExprClass.Unresolved:
+                                       return "Unresolved";
                                case ExprClass.Value:
                                        return "value";
                                case ExprClass.Variable:
@@ -1194,7 +1156,7 @@ namespace Mono.CSharp {
                        if (TypeManager.IsDynamicType (source.type)) {
                                Arguments args = new Arguments (1);
                                args.Add (new Argument (source));
-                               return new DynamicConversion (TypeManager.int32_type, false, args, loc).Resolve (ec);
+                               return new DynamicConversion (TypeManager.int32_type, CSharpBinderFlags.ConvertArrayIndex, args, loc).Resolve (ec);
                        }
 
                        Expression converted;
@@ -1285,7 +1247,7 @@ namespace Mono.CSharp {
                {
                        TypeExpr texpr = TypeManager.expression_type_expr;
                        if (texpr == null) {
-                               Type t = TypeManager.CoreLookupType (ec.Compiler, "System.Linq.Expressions", "Expression", Kind.Class, true);
+                               Type t = TypeManager.CoreLookupType (ec.Compiler, "System.Linq.Expressions", "Expression", MemberKind.Class, true);
                                if (t == null)
                                        return null;
 
@@ -1295,7 +1257,6 @@ namespace Mono.CSharp {
                        return texpr;
                }
 
-#if NET_4_0
                //
                // Implemented by all expressions which support conversion from
                // compiler expression to invokable runtime expression. Used by
@@ -1305,7 +1266,6 @@ namespace Mono.CSharp {
                {
                        throw new NotImplementedException ("MakeExpression for " + GetType ());
                }
-#endif
 
                public virtual void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
@@ -1385,7 +1345,7 @@ namespace Mono.CSharp {
                        return CreateExpressionFactoryCall (ec, ec.HasSet (ResolveContext.Options.CheckedScope) ? "ConvertChecked" : "Convert", args);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        // This should never be invoked, we are born in fully
                        // initialized state.
@@ -1403,14 +1363,12 @@ namespace Mono.CSharp {
                        return child.GetAttributableValue (ec, value_type, out value);
                }
 
-#if NET_4_0
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
                        return ctx.HasSet (BuilderContext.Options.CheckedScope) ?
                                SLE.Expression.ConvertChecked (child.MakeExpression (ctx), type) :
                                SLE.Expression.Convert (child.MakeExpression (ctx), type);
                }
-#endif
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
@@ -1530,7 +1488,7 @@ namespace Mono.CSharp {
        /// </summary>
        public class CastFromDecimal : TypeCast
        {
-               static IDictionary operators;
+               static Dictionary<Type, MethodInfo> operators;
 
                public CastFromDecimal (Expression child, Type return_type)
                        : base (child, return_type)
@@ -1545,11 +1503,11 @@ namespace Mono.CSharp {
                public Expression Resolve ()
                {
                        if (operators == null) {
-                                MemberInfo[] all_oper = TypeManager.MemberLookup (TypeManager.decimal_type,
-                                       TypeManager.decimal_type, TypeManager.decimal_type, MemberTypes.Method,
-                                       BindingFlags.Static | BindingFlags.Public, "op_Explicit", null);
+                               MemberInfo[] all_oper = TypeManager.MemberLookup (TypeManager.decimal_type,
+                                  TypeManager.decimal_type, TypeManager.decimal_type, MemberTypes.Method,
+                                  BindingFlags.Static | BindingFlags.Public, "op_Explicit", null);
 
-                               operators = new System.Collections.Specialized.HybridDictionary ();
+                               operators = new Dictionary<Type, MethodInfo> (ReferenceEquality<Type>.Default);
                                foreach (MethodInfo oper in all_oper) {
                                        AParametersCollection pd = TypeManager.GetParameterData (oper);
                                        if (pd.Types [0] == TypeManager.decimal_type)
@@ -1557,7 +1515,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       return operators.Contains (type) ? this : null;
+                       return operators.ContainsKey (type) ? this : null;
                }
 
                public override void Emit (EmitContext ec)
@@ -1565,7 +1523,7 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
                        child.Emit (ec);
 
-                       ig.Emit (OpCodes.Call, (MethodInfo)operators [type]);
+                       ig.Emit (OpCodes.Call, operators [type]);
                }
        }
 
@@ -1577,13 +1535,16 @@ namespace Mono.CSharp {
        //
        public class EmptyConstantCast : Constant
        {
-               public readonly Constant child;
+               public Constant child;
 
-               public EmptyConstantCast(Constant child, Type type)
+               public EmptyConstantCast (Constant child, Type type)
                        : base (child.Location)
                {
-                       eclass = child.eclass;
+                       if (child == null)
+                               throw new ArgumentNullException ("child");
+
                        this.child = child;
+                       this.eclass = child.eclass;
                        this.type = type;
                }
 
@@ -1599,6 +1560,9 @@ namespace Mono.CSharp {
 
                public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
+                       if (child.Type == target_type)
+                               return child;
+
                        // FIXME: check that 'type' can be converted to 'target_type' first
                        return child.ConvertExplicitly (in_checked_context, target_type);
                }
@@ -1615,11 +1579,6 @@ namespace Mono.CSharp {
                        return CreateExpressionFactoryCall (ec, "Convert", args);
                }
 
-               public override Constant Increment ()
-               {
-                       return child.Increment ();
-               }
-
                public override bool IsDefaultValue {
                        get { return child.IsDefaultValue; }
                }
@@ -1631,10 +1590,19 @@ namespace Mono.CSharp {
                public override bool IsNull {
                        get { return child.IsNull; }
                }
+               
+               public override bool IsOneInteger {
+                       get { return child.IsOneInteger; }
+               }
 
                public override bool IsZeroInteger {
                        get { return child.IsZeroInteger; }
                }
+
+               protected override Expression DoResolve (ResolveContext rc)
+               {
+                       return this;
+               }
                
                public override void Emit (EmitContext ec)
                {
@@ -1655,12 +1623,12 @@ namespace Mono.CSharp {
                        child.EmitSideEffect (ec);
                }
 
-               public override Constant ConvertImplicitly (Type target_type)
+               public override Constant ConvertImplicitly (ResolveContext rc, Type target_type)
                {
                        // FIXME: Do we need to check user conversions?
                        if (!Convert.ImplicitStandardConversionExists (this, target_type))
                                return null;
-                       return child.ConvertImplicitly (target_type);
+                       return child.ConvertImplicitly (rc, target_type);
                }
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
@@ -1669,26 +1637,29 @@ namespace Mono.CSharp {
                }
        }
 
-
        /// <summary>
        ///  This class is used to wrap literals which belong inside Enums
        /// </summary>
-       public class EnumConstant : Constant {
+       public class EnumConstant : Constant
+       {
                public Constant Child;
 
-               public EnumConstant (Constant child, Type enum_type):
-                       base (child.Location)
+               public EnumConstant (Constant child, Type enum_type)
+                       base (child.Location)
                {
-                       eclass = child.eclass;
                        this.Child = child;
-                       type = enum_type;
+                       this.type = enum_type;
                }
-               
-               public override Expression DoResolve (ResolveContext ec)
+
+               protected EnumConstant (Location loc)
+                       : base (loc)
                {
-                       // This should never be invoked, we are born in fully
-                       // initialized state.
+               }
 
+               protected override Expression DoResolve (ResolveContext rc)
+               {
+                       Child = Child.Resolve (rc);
+                       this.eclass = ExprClass.Value;
                        return this;
                }
 
@@ -1738,7 +1709,7 @@ namespace Mono.CSharp {
                        //
                        // This works only sometimes
                        //
-                       if (type.Module == RootContext.ToplevelTypes.Builder)
+                       if (TypeManager.IsBeingCompiled (type))
                                return Child.GetValue ();
 #endif
 
@@ -1750,9 +1721,9 @@ namespace Mono.CSharp {
                        return Child.AsString ();
                }
 
-               public override Constant Increment()
+               public EnumConstant Increment()
                {
-                       return new EnumConstant (Child.Increment (), type);
+                       return new EnumConstant (((IntegralConstant) Child).Increment (), type);
                }
 
                public override bool IsDefaultValue {
@@ -1779,7 +1750,7 @@ namespace Mono.CSharp {
                        return Child.ConvertExplicitly (in_checked_context, target_type);
                }
 
-               public override Constant ConvertImplicitly (Type type)
+               public override Constant ConvertImplicitly (ResolveContext rc, Type type)
                {
                        Type this_type = TypeManager.DropGenericTypeArguments (Type);
                        type = TypeManager.DropGenericTypeArguments (type);
@@ -1791,7 +1762,7 @@ namespace Mono.CSharp {
 
                                Type child_type = TypeManager.DropGenericTypeArguments (Child.Type);
                                if (type.UnderlyingSystemType != child_type)
-                                       Child = Child.ConvertImplicitly (type.UnderlyingSystemType);
+                                       Child = Child.ConvertImplicitly (rc, type.UnderlyingSystemType);
                                return this;
                        }
 
@@ -1799,9 +1770,8 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       return Child.ConvertImplicitly(type);
+                       return Child.ConvertImplicitly (rc, type);
                }
-
        }
 
        /// <summary>
@@ -1818,7 +1788,7 @@ namespace Mono.CSharp {
                        eclass = ExprClass.Value;
                }
                
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        // This should never be invoked, we are born in fully
                        // initialized state.
@@ -1851,7 +1821,7 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        // This should never be invoked, we are born in fully
                        // initialized state.
@@ -1912,7 +1882,7 @@ namespace Mono.CSharp {
                        mode = m;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        // This should never be invoked, we are born in fully
                        // initialized state.
@@ -2114,7 +2084,7 @@ namespace Mono.CSharp {
                        this.op = op;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        // This should never be invoked, we are born in fully
                        // initialized state.
@@ -2187,11 +2157,12 @@ namespace Mono.CSharp {
                                this.orig_expr = orig_expr;
                        }
 
-                       public override Constant ConvertImplicitly (Type target_type)
+                       public override Constant ConvertImplicitly (ResolveContext rc, Type target_type)
                        {
-                               Constant c = base.ConvertImplicitly (target_type);
+                               Constant c = base.ConvertImplicitly (rc, target_type);
                                if (c != null)
                                        c = new ReducedConstantExpression (c, orig_expr);
+
                                return c;
                        }
 
@@ -2237,7 +2208,7 @@ namespace Mono.CSharp {
                                return orig_expr.CreateExpressionTree (ec);
                        }
 
-                       public override Expression DoResolve (ResolveContext ec)
+                       protected override Expression DoResolve (ResolveContext ec)
                        {
                                eclass = stm.eclass;
                                type = stm.Type;
@@ -2265,12 +2236,20 @@ namespace Mono.CSharp {
                private ReducedExpression (Expression expr, Expression orig_expr)
                {
                        this.expr = expr;
+                       this.eclass = expr.eclass;
+                       this.type = expr.Type;
                        this.orig_expr = orig_expr;
                        this.loc = orig_expr.Location;
                }
 
+               //
+               // Creates fully resolved expression switcher
+               //
                public static Constant Create (Constant expr, Expression original_expr)
                {
+                       if (expr.eclass == ExprClass.Unresolved)
+                               throw new ArgumentException ("Unresolved expression");
+
                        return new ReducedConstantExpression (expr, original_expr);
                }
 
@@ -2279,6 +2258,10 @@ namespace Mono.CSharp {
                        return new ReducedExpressionStatement (s, orig);
                }
 
+               //
+               // Creates unresolved reduce expression. The original expression has to be
+               // already resolved
+               //
                public static Expression Create (Expression expr, Expression original_expr)
                {
                        Constant c = expr as Constant;
@@ -2289,6 +2272,9 @@ namespace Mono.CSharp {
                        if (s != null)
                                return Create (s, original_expr);
 
+                       if (expr.eclass == ExprClass.Unresolved)
+                               throw new ArgumentException ("Unresolved expression");
+
                        return new ReducedExpression (expr, original_expr);
                }
 
@@ -2297,10 +2283,8 @@ namespace Mono.CSharp {
                        return orig_expr.CreateExpressionTree (ec);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       eclass = expr.eclass;
-                       type = expr.Type;
                        return this;
                }
 
@@ -2314,12 +2298,10 @@ namespace Mono.CSharp {
                        expr.EmitBranchable (ec, target, on_true);
                }
 
-#if NET_4_0
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
                        return orig_expr.MakeExpression (ctx);
                }
-#endif
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
@@ -2327,6 +2309,49 @@ namespace Mono.CSharp {
                }
        }
 
+       //
+       // Standard composite pattern
+       //
+       public abstract class CompositeExpression : Expression
+       {
+               Expression expr;
+
+               protected CompositeExpression (Expression expr)
+               {
+                       this.expr = expr;
+                       this.loc = expr.Location;
+               }
+
+               public override Expression CreateExpressionTree (ResolveContext ec)
+               {
+                       return expr.CreateExpressionTree (ec);
+               }
+
+               public Expression Child {
+                       get { return expr; }
+               }
+
+               protected override Expression DoResolve (ResolveContext ec)
+               {
+                       expr = expr.Resolve (ec);
+                       if (expr != null) {
+                               type = expr.Type;
+                               eclass = expr.eclass;
+                       }
+
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       expr.Emit (ec);
+               }
+
+               public override bool IsNull {
+                       get { return expr.IsNull; }
+               }
+       }
+
        //
        // Base of expressions used only to narrow resolve flow
        //
@@ -2437,9 +2462,8 @@ namespace Mono.CSharp {
        ///   SimpleName expressions are formed of a single word and only happen at the beginning 
        ///   of a dotted-name.
        /// </summary>
-       public class SimpleName : ATypeNameExpression {
-               bool in_transit;
-
+       public class SimpleName : ATypeNameExpression
+       {
                public SimpleName (string name, Location l)
                        : base (name, l)
                {
@@ -2510,7 +2534,7 @@ namespace Mono.CSharp {
                                (mc.LookupNamespaceOrType (Name, loc, /* ignore_cs0104 = */ true) != null);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return SimpleNameResolve (ec, null, false);
                }
@@ -2519,7 +2543,6 @@ namespace Mono.CSharp {
                {
                        return SimpleNameResolve (ec, right_side, false);
                }
-               
 
                public Expression DoResolve (ResolveContext ec, bool intermediate)
                {
@@ -2596,8 +2619,18 @@ namespace Mono.CSharp {
                                return fne;
                        }
 
-                       if (!HasTypeArguments && Name == "dynamic" && RootContext.Version > LanguageVersion.V_3)
+                       if (!HasTypeArguments && Name == "dynamic" &&
+                               RootContext.Version > LanguageVersion.V_3 &&
+                               RootContext.MetadataCompatibilityVersion > MetadataVersion.v2) {
+
+                               if (!PredefinedAttributes.Get.Dynamic.IsDefined) {
+                                       ec.Compiler.Report.Error (1980, Location,
+                                               "Dynamic keyword requires `{0}' to be defined. Are you missing System.Core.dll assembly reference?",
+                                               PredefinedAttributes.Get.Dynamic.GetSignatureForError ());
+                               }
+
                                return new DynamicTypeExpr (loc);
+                       }
 
                        if (silent || errors != ec.Compiler.Report.Errors)
                                return null;
@@ -2631,7 +2664,7 @@ namespace Mono.CSharp {
                                if (ec.CurrentTypeDefinition != null) {
                                        Type t = ec.CurrentTypeDefinition.LookupAnyGeneric (Name);
                                        if (t != null) {
-                                               Namespace.Error_InvalidNumberOfTypeArguments (t, loc);
+                                               Namespace.Error_InvalidNumberOfTypeArguments (ec.Compiler.Report, t, loc);
                                                return;
                                        }
                                }
@@ -2670,12 +2703,7 @@ namespace Mono.CSharp {
 
                Expression SimpleNameResolve (ResolveContext ec, Expression right_side, bool intermediate)
                {
-                       if (in_transit)
-                               return null;
-
-                       in_transit = true;
                        Expression e = DoSimpleNameResolve (ec, right_side, intermediate);
-                       in_transit = false;
 
                        if (e == null)
                                return null;
@@ -2719,11 +2747,13 @@ namespace Mono.CSharp {
                                        if (right_side != null) {
                                                e = e.ResolveLValue (ec, right_side);
                                        } else {
-                                               ResolveFlags rf = ResolveFlags.VariableOrValue;
-                                               if (intermediate)
-                                                       rf |= ResolveFlags.DisableFlowAnalysis;
-
-                                               e = e.Resolve (ec, rf);
+                                               if (intermediate) {
+                                                       using (ec.With (ResolveContext.Options.DoFlowAnalysis, false)) {
+                                                               e = e.Resolve (ec, ResolveFlags.VariableOrValue);
+                                                       }
+                                               } else {
+                                                       e = e.Resolve (ec, ResolveFlags.VariableOrValue);
+                                               }
                                        }
 
                                        if (targs != null && e != null)
@@ -2751,7 +2781,7 @@ namespace Mono.CSharp {
                        //
 
                        Type almost_matched_type = null;
-                       ArrayList almost_matched = null;
+                       IList<MemberInfo> almost_matched = null;
                        for (Type lookup_ds = ec.CurrentType; lookup_ds != null; lookup_ds = lookup_ds.DeclaringType) {
                                e = MemberLookup (ec.Compiler, ec.CurrentType, lookup_ds, Name, loc);
                                if (e != null) {
@@ -2777,14 +2807,14 @@ namespace Mono.CSharp {
 
                                if (almost_matched == null && almost_matched_members.Count > 0) {
                                        almost_matched_type = lookup_ds;
-                                       almost_matched = (ArrayList) almost_matched_members.Clone ();
+                                       almost_matched = new List<MemberInfo>(almost_matched_members);
                                }
                        }
 
                        if (e == null) {
                                if (almost_matched == null && almost_matched_members.Count > 0) {
                                        almost_matched_type = ec.CurrentType;
-                                       almost_matched = (ArrayList) almost_matched_members.Clone ();
+                                       almost_matched = new List<MemberInfo> (almost_matched_members);
                                }
                                e = ResolveAsTypeStep (ec, true);
                        }
@@ -2806,7 +2836,7 @@ namespace Mono.CSharp {
                                if (RootContext.EvalMode){
                                        FieldInfo fi = Evaluator.LookupField (Name);
                                        if (fi != null)
-                                               return new FieldExpr (fi, loc).Resolve (ec);
+                                               return new FieldExpr (Import.CreateField (fi), loc).Resolve (ec);
                                }
 
                                if (almost_matched != null)
@@ -2872,7 +2902,7 @@ namespace Mono.CSharp {
 
                                return (right_side != null)
                                        ? me.DoResolveLValue (ec, right_side)
-                                       : me.DoResolve (ec);
+                                       : me.Resolve (ec);
                        }
 
                        return e;
@@ -2927,7 +2957,7 @@ namespace Mono.CSharp {
                        return t;
                }
 
-               override public Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return ResolveAsTypeTerminal (ec, false);
                }
@@ -3222,7 +3252,7 @@ namespace Mono.CSharp {
                public Expression ExtensionExpression;
                Argument extension_argument;
 
-               public ExtensionMethodGroupExpr (ArrayList list, NamespaceEntry n, Type extensionType, Location l)
+               public ExtensionMethodGroupExpr (List<MethodSpec> list, NamespaceEntry n, Type extensionType, Location l)
                        : base (list, extensionType, l)
                {
                        this.namespace_entry = n;
@@ -3288,13 +3318,13 @@ namespace Mono.CSharp {
        {
                public interface IErrorHandler
                {
-                       bool AmbiguousCall (ResolveContext ec, MethodBase ambiguous);
-                       bool NoExactMatch (ResolveContext ec, MethodBase method);
+                       bool AmbiguousCall (ResolveContext ec, MethodSpec ambiguous);
+                       bool NoExactMatch (ResolveContext ec, MethodSpec method);
                }
 
-               public IErrorHandler CustomErrorHandler;                
-               public MethodBase [] Methods;
-               MethodBase best_candidate;
+               public IErrorHandler CustomErrorHandler;
+               public MethodSpec [] Methods;
+               MethodSpec best_candidate;
                // TODO: make private
                public TypeArguments type_arguments;
                bool identical_type_name;
@@ -3302,31 +3332,31 @@ namespace Mono.CSharp {
                Type delegate_type;
                Type queried_type;
                
-               public MethodGroupExpr (MemberInfo [] mi, Type type, Location l)
+               public MethodGroupExpr (MethodSpec [] mi, Type type, Location l)
                        : this (type, l)
                {
-                       Methods = new MethodBase [mi.Length];
+                       Methods = new MethodSpec[mi.Length];
                        mi.CopyTo (Methods, 0);
                }
 
-               public MethodGroupExpr (MemberInfo[] mi, Type type, Location l, bool inacessibleCandidatesOnly)
+               public MethodGroupExpr (MethodSpec[] mi, Type type, Location l, bool inacessibleCandidatesOnly)
                        : this (mi, type, l)
                {
                        has_inaccessible_candidates_only = inacessibleCandidatesOnly;
                }
 
-               public MethodGroupExpr (ArrayList list, Type type, Location l)
+               public MethodGroupExpr (List<MethodSpec> list, Type type, Location l)
                        : this (type, l)
                {
                        try {
-                               Methods = (MethodBase[])list.ToArray (typeof (MethodBase));
+                               Methods = list.ToArray ();
                        } catch {
-                               foreach (MemberInfo m in list){
-                                       if (!(m is MethodBase)){
-                                               Console.WriteLine ("Name " + m.Name);
-                                               Console.WriteLine ("Found a: " + m.GetType ().FullName);
-                                       }
-                               }
+                               //foreach (MemberInfo m in list){
+                               //    if (!(m is MethodBase)){
+                               //        Console.WriteLine ("Name " + m.Name);
+                               //        Console.WriteLine ("Found a: " + m.GetType ().FullName);
+                               //    }
+                               //}
                                throw;
                        }
 
@@ -3347,6 +3377,12 @@ namespace Mono.CSharp {
                        }
                }
 
+               public MethodSpec BestCandidate {
+                       get {
+                               return best_candidate;
+                       }
+               }
+
                public Type DelegateType {
                        set {
                                delegate_type = value;
@@ -3362,9 +3398,9 @@ namespace Mono.CSharp {
                public override string GetSignatureForError ()
                {
                        if (best_candidate != null)
-                               return TypeManager.CSharpSignature (best_candidate);
+                               return TypeManager.CSharpSignature (best_candidate.MetaInfo);
                        
-                       return TypeManager.CSharpSignature (Methods [0]);
+                       return TypeManager.CSharpSignature (Methods [0].MetaInfo);
                }
 
                public override string Name {
@@ -3378,7 +3414,7 @@ namespace Mono.CSharp {
                                if (best_candidate != null)
                                        return !best_candidate.IsStatic;
 
-                               foreach (MethodBase mb in Methods)
+                               foreach (var mb in Methods)
                                        if (!mb.IsStatic)
                                                return true;
 
@@ -3391,22 +3427,17 @@ namespace Mono.CSharp {
                                if (best_candidate != null)
                                        return best_candidate.IsStatic;
 
-                               foreach (MethodBase mb in Methods)
+                               foreach (var mb in Methods)
                                        if (mb.IsStatic)
                                                return true;
 
                                return false;
                        }
                }
-               
-               public static explicit operator ConstructorInfo (MethodGroupExpr mg)
-               {
-                       return (ConstructorInfo)mg.best_candidate;
-               }
 
-               public static explicit operator MethodInfo (MethodGroupExpr mg)
+               public static explicit operator MethodSpec (MethodGroupExpr mg)
                {
-                       return (MethodInfo)mg.best_candidate;
+                       return mg.best_candidate;
                }
 
                //
@@ -3468,6 +3499,9 @@ namespace Mono.CSharp {
                                if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
                                        q == TypeManager.uint64_type)
                                        return 1;
+                       } else if (p == InternalType.Dynamic) {
+                               if (q == TypeManager.object_type)
+                                       return 2;
                        }
 
                        if (q == TypeManager.int32_type) {
@@ -3484,6 +3518,9 @@ namespace Mono.CSharp {
                                if (p == TypeManager.ushort_type || p == TypeManager.uint32_type ||
                                        p == TypeManager.uint64_type)
                                        return 2;
+                       } else if (q == InternalType.Dynamic) {
+                               if (p == TypeManager.object_type)
+                                       return 1;
                        }
 
                        // TODO: this is expensive
@@ -3512,11 +3549,11 @@ namespace Mono.CSharp {
                ///     true  if candidate is better than the current best match
                /// </remarks>
                static bool BetterFunction (ResolveContext ec, Arguments args, int argument_count,
-                       MethodBase candidate, bool candidate_params,
-                       MethodBase best, bool best_params)
+                       MethodSpec candidate, bool candidate_params,
+                       MethodSpec best, bool best_params)
                {
-                       AParametersCollection candidate_pd = TypeManager.GetParameterData (candidate);
-                       AParametersCollection best_pd = TypeManager.GetParameterData (best);
+                       AParametersCollection candidate_pd = candidate.Parameters;
+                       AParametersCollection best_pd = best.Parameters;
                
                        bool better_at_least_one = false;
                        bool same = true;
@@ -3542,8 +3579,8 @@ namespace Mono.CSharp {
                                        bt = TypeManager.GetElementType (bt);
                                        --b_idx;
                                }
-
-                               if (ct.Equals (bt))
+                               
+                               if (TypeManager.IsEqual (ct, bt))
                                        continue;
 
                                same = false;
@@ -3578,10 +3615,10 @@ namespace Mono.CSharp {
                        //
                        // The two methods have equal parameter types.  Now apply tie-breaking rules
                        //
-                       if (TypeManager.IsGenericMethod (best)) {
-                               if (!TypeManager.IsGenericMethod (candidate))
+                       if (best.IsGenericMethod) {
+                               if (!candidate.IsGenericMethod)
                                        return true;
-                       } else if (TypeManager.IsGenericMethod (candidate)) {
+                       } else if (candidate.IsGenericMethod) {
                                return false;
                        }
 
@@ -3669,7 +3706,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       IMethodData md = TypeManager.GetMethod (best_candidate);
+                       IMethodData md = TypeManager.GetMethod (best_candidate.MetaInfo);
                        if (md != null && md.IsExcluded ())
                                ec.Report.Error (765, loc,
                                        "Partial methods with only a defining declaration or removed conditional methods cannot be used in an expression tree");
@@ -3677,10 +3714,12 @@ namespace Mono.CSharp {
                        return new TypeOfMethod (best_candidate, loc);
                }
                
-               override public Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
+                       this.eclass = ExprClass.MethodGroup;
+
                        if (InstanceExpression != null) {
-                               InstanceExpression = InstanceExpression.DoResolve (ec);
+                               InstanceExpression = InstanceExpression.Resolve (ec);
                                if (InstanceExpression == null)
                                        return null;
                        }
@@ -3705,23 +3744,23 @@ namespace Mono.CSharp {
                        Invocation.EmitCall (ec, IsBase, InstanceExpression, best_candidate, arguments, loc);                   
                }
 
-               void Error_AmbiguousCall (ResolveContext ec, MethodBase ambiguous)
+               void Error_AmbiguousCall (ResolveContext ec, MethodSpec ambiguous)
                {
                        if (CustomErrorHandler != null && CustomErrorHandler.AmbiguousCall (ec, ambiguous))
                                return;
 
-                       ec.Report.SymbolRelatedToPreviousError (best_candidate);
+                       ec.Report.SymbolRelatedToPreviousError (best_candidate.MetaInfo);
                        ec.Report.Error (121, loc, "The call is ambiguous between the following methods or properties: `{0}' and `{1}'",
-                               TypeManager.CSharpSignature (ambiguous), TypeManager.CSharpSignature (best_candidate));
+                               TypeManager.CSharpSignature (ambiguous), TypeManager.CSharpSignature (best_candidate.MetaInfo));
                }
 
-               protected virtual void Error_InvalidArguments (ResolveContext ec, Location loc, int idx, MethodBase method,
+               protected virtual void Error_InvalidArguments (ResolveContext ec, Location loc, int idx, MethodSpec method,
                                                                                                        Argument a, AParametersCollection expected_par, Type paramType)
                {
                        ExtensionMethodGroupExpr emg = this as ExtensionMethodGroupExpr;
 
                        if (a is CollectionElementInitializer.ElementInitializerArgument) {
-                               ec.Report.SymbolRelatedToPreviousError (method);
+                               ec.Report.SymbolRelatedToPreviousError (method.MetaInfo);
                                if ((expected_par.FixedParameters [idx].ModFlags & Parameter.Modifier.ISBYREF) != 0) {
                                        ec.Report.Error (1954, loc, "The best overloaded collection initalizer method `{0}' cannot have 'ref', or `out' modifier",
                                                TypeManager.CSharpSignature (method));
@@ -3733,7 +3772,7 @@ namespace Mono.CSharp {
                                ec.Report.Error (1594, loc, "Delegate `{0}' has some invalid arguments",
                                        TypeManager.CSharpName (method.DeclaringType));
                        } else {
-                               ec.Report.SymbolRelatedToPreviousError (method);
+                               ec.Report.SymbolRelatedToPreviousError (method.MetaInfo);
                                if (emg != null) {
                                        ec.Report.Error (1928, loc,
                                                "Type `{0}' does not contain a member `{1}' and the best extension method overload `{2}' has some invalid arguments",
@@ -3788,7 +3827,7 @@ namespace Mono.CSharp {
                                      Name, arg_count.ToString ());
                }
                
-               protected virtual int GetApplicableParametersCount (MethodBase method, AParametersCollection parameters)
+               protected virtual int GetApplicableParametersCount (MethodSpec method, AParametersCollection parameters)
                {
                        return parameters.Count;
                }               
@@ -3807,11 +3846,11 @@ namespace Mono.CSharp {
                /// 0 = the best, int.MaxValue = the worst
                ///
                public int IsApplicable (ResolveContext ec,
-                                               ref Arguments arguments, int arg_count, ref MethodBase method, ref bool params_expanded_form)
+                                               ref Arguments arguments, int arg_count, ref MethodSpec method, ref bool params_expanded_form)
                {
-                       MethodBase candidate = method;
+                       var candidate = method;
 
-                       AParametersCollection pd = TypeManager.GetParameterData (candidate);
+                       AParametersCollection pd = candidate.Parameters;
                        int param_count = GetApplicableParametersCount (candidate, pd);
                        int optional_count = 0;
 
@@ -3823,7 +3862,7 @@ namespace Mono.CSharp {
                                        }
                                }
 
-                               int args_gap = Math.Abs (arg_count - param_count);
+                               int args_gap = System.Math.Abs (arg_count - param_count);
                                if (optional_count != 0) {
                                        if (args_gap > optional_count)
                                                return int.MaxValue - 10000 + args_gap - optional_count;
@@ -3876,7 +3915,7 @@ namespace Mono.CSharp {
                                                        if (na == null)
                                                                break;
 
-                                                       int index = pd.GetParameterIndexByName (na.Name.Value);
+                                                       int index = pd.GetParameterIndexByName (na.Name);
 
                                                        // Named parameter not found or already reordered
                                                        if (index <= i)
@@ -3909,26 +3948,25 @@ namespace Mono.CSharp {
                        //
                        // 1. Handle generic method using type arguments when specified or type inference
                        //
-                       if (TypeManager.IsGenericMethod (candidate)) {
+                       if (candidate.IsGenericMethod) {
                                if (type_arguments != null) {
                                        Type [] g_args = candidate.GetGenericArguments ();
                                        if (g_args.Length != type_arguments.Count)
-                                               return int.MaxValue - 20000 + Math.Abs (type_arguments.Count - g_args.Length);
+                                               return int.MaxValue - 20000 + System.Math.Abs (type_arguments.Count - g_args.Length);
 
-                                       // TODO: Don't create new method, create Parameters only
-                                       method = ((MethodInfo) candidate).MakeGenericMethod (type_arguments.Arguments);
+                                       method = candidate.Inflate (type_arguments.Arguments);
                                        candidate = method;
-                                       pd = TypeManager.GetParameterData (candidate);
+                                       pd = candidate.Parameters;
                                } else {
                                        int score = TypeManager.InferTypeArguments (ec, arguments, ref candidate);
                                        if (score != 0)
                                                return score - 20000;
 
-                                       if (TypeManager.IsGenericMethodDefinition (candidate))
+                                       if (TypeManager.IsGenericMethodDefinition (candidate.MetaInfo))
                                                throw new InternalErrorException ("A generic method `{0}' definition took part in overload resolution",
-                                                       TypeManager.CSharpSignature (candidate));
+                                                       TypeManager.CSharpSignature (candidate.MetaInfo));
 
-                                       pd = TypeManager.GetParameterData (candidate);
+                                       pd = candidate.Parameters;
                                }
                        } else {
                                if (type_arguments != null)
@@ -4000,11 +4038,19 @@ namespace Mono.CSharp {
                                if (TypeManager.HasElementType (a_type))
                                        a_type = TypeManager.GetElementType (a_type);
 
-                               if (a_type != parameter)
+                               if (a_type != parameter) {
+                                       if (TypeManager.IsDynamicType (a_type))
+                                               return 0;
+
                                        return 2;
+                               }
                        } else {
-                               if (!Convert.ImplicitConversionExists (ec, argument.Expr, parameter))
+                               if (!Convert.ImplicitConversionExists (ec, argument.Expr, parameter)) {
+                                       if (TypeManager.IsDynamicType (argument.Type))
+                                               return 0;
+
                                        return 2;
+                               }
                        }
 
                        if (arg_mod != param_mod)
@@ -4048,10 +4094,10 @@ namespace Mono.CSharp {
 
                        if (mg2 == null)
                                return mg1;
-                       
-                       ArrayList all = new ArrayList (mg1.Methods);
-                       foreach (MethodBase m in mg2.Methods){
-                               if (!TypeManager.ArrayContainsMethod (mg1.Methods, m, false))
+
+                       var all = new List<MethodSpec> (mg1.Methods);
+                       foreach (var m in mg2.Methods){
+                               if (!TypeManager.ArrayContainsMethod (mg1.Methods.Select (l => l.MetaInfo).ToArray (), m.MetaInfo, false))
                                        all.Add (m);
                        }
 
@@ -4105,13 +4151,11 @@ namespace Mono.CSharp {
                {
                        base.MutateHoistedGenericType (storey);
 
-                       MethodInfo mi = best_candidate as MethodInfo;
-                       if (mi != null) {
-                               best_candidate = storey.MutateGenericMethod (mi);
-                               return;
+                       if (best_candidate.IsConstructor) {
+                               storey.MutateConstructor (best_candidate);
+                       } else {
+                               storey.MutateGenericMethod (best_candidate);                            
                        }
-
-                       best_candidate = storey.MutateConstructor ((ConstructorInfo) this);
                }
 
                /// <summary>
@@ -4135,8 +4179,8 @@ namespace Mono.CSharp {
                {
                        bool method_params = false;
                        Type applicable_type = null;
-                       ArrayList candidates = new ArrayList (2);
-                       ArrayList candidate_overrides = null;
+                       var candidates = new List<MethodSpec> (2);
+                       List<MethodSpec> candidate_overrides = null;
 
                        //
                        // Used to keep a map between the candidate
@@ -4145,8 +4189,8 @@ namespace Mono.CSharp {
                        //
                        // false is normal form, true is expanded form
                        //
-                       Hashtable candidate_to_form = null;
-                       Hashtable candidates_expanded = null;
+                       Dictionary<MethodSpec, MethodSpec> candidate_to_form = null;
+                       Dictionary<MethodSpec, Arguments> candidates_expanded = null;
                        Arguments candidate_args = Arguments;
 
                        int arg_count = Arguments != null ? Arguments.Count : 0;
@@ -4171,16 +4215,20 @@ namespace Mono.CSharp {
                                // with non-C# libraries which change the visibility of overrides (#75636)
                                //
                                int j = 0;
+                               MethodBase mb = null;
                                for (int i = 0; i < Methods.Length; ++i) {
-                                       MethodBase m = Methods [i];
+                                       var m = Methods [i];
+                                       mb = m.MetaInfo;
                                        if (TypeManager.IsOverride (m)) {
                                                if (candidate_overrides == null)
-                                                       candidate_overrides = new ArrayList ();
+                                                       candidate_overrides = new List<MethodSpec> ();
                                                candidate_overrides.Add (m);
-                                               m = TypeManager.TryGetBaseDefinition (m);
+                                               mb = TypeManager.TryGetBaseDefinition (mb);
+                                               if (mb != null && Array.Exists (Methods, l => l.MetaInfo == mb))
+                                                       continue;
                                        }
-                                       if (m != null)
-                                               Methods [j++] = m;
+                                       if (mb != null)
+                                               Methods [j++] = Import.CreateMethod (mb);
                                }
                                nmethods = j;
                        }
@@ -4219,14 +4267,14 @@ namespace Mono.CSharp {
                                
                                if (params_expanded_form) {
                                        if (candidate_to_form == null)
-                                               candidate_to_form = new PtrHashtable ();
-                                       MethodBase candidate = Methods [i];
+                                               candidate_to_form = new Dictionary<MethodSpec, MethodSpec> (4, ReferenceEquality<MethodSpec>.Default);
+                                       var candidate = Methods [i];
                                        candidate_to_form [candidate] = candidate;
                                }
                                
                                if (candidate_args != Arguments) {
                                        if (candidates_expanded == null)
-                                               candidates_expanded = new Hashtable (2);
+                                               candidates_expanded = new Dictionary<MethodSpec, Arguments> (4, ReferenceEquality<MethodSpec>.Default);
 
                                        candidates_expanded.Add (Methods [i], candidate_args);
                                        candidate_args = Arguments;
@@ -4285,55 +4333,8 @@ namespace Mono.CSharp {
                                        if (CustomErrorHandler != null && !has_inaccessible_candidates_only && CustomErrorHandler.NoExactMatch (ec, best_candidate))
                                                return null;
 
-                                       AParametersCollection pd = TypeManager.GetParameterData (best_candidate);
-                                       bool cand_params = candidate_to_form != null && candidate_to_form.Contains (best_candidate);
-                                       if (arg_count == pd.Count || pd.HasParams) {
-                                               if (TypeManager.IsGenericMethodDefinition (best_candidate)) {
-                                                       if (type_arguments == null) {
-                                                               ec.Report.Error (411, loc,
-                                                                       "The type arguments for method `{0}' cannot be inferred from " +
-                                                                       "the usage. Try specifying the type arguments explicitly",
-                                                                       TypeManager.CSharpSignature (best_candidate));
-                                                               return null;
-                                                       }
-
-                                                       Type[] g_args = TypeManager.GetGenericArguments (best_candidate);
-                                                       if (type_arguments.Count != g_args.Length) {
-                                                               ec.Report.SymbolRelatedToPreviousError (best_candidate);
-                                                               ec.Report.Error (305, loc, "Using the generic method `{0}' requires `{1}' type argument(s)",
-                                                                       TypeManager.CSharpSignature (best_candidate),
-                                                                       g_args.Length.ToString ());
-                                                               return null;
-                                                       }
-                                               } else {
-                                                       if (type_arguments != null && !TypeManager.IsGenericMethod (best_candidate)) {
-                                                               Error_TypeArgumentsCannotBeUsed (ec.Report, loc);
-                                                               return null;
-                                                       }
-                                               }
-
-                                               if (has_inaccessible_candidates_only) {
-                                                       if (InstanceExpression != null && type != ec.CurrentType && TypeManager.IsNestedFamilyAccessible (ec.CurrentType, best_candidate.DeclaringType)) {
-                                                               // Although a derived class can access protected members of
-                                                               // its base class it cannot do so through an instance of the
-                                                               // base class (CS1540).  If the qualifier_type is a base of the
-                                                               // ec.CurrentType and the lookup succeeds with the latter one,
-                                                               // then we are in this situation.
-                                                               Error_CannotAccessProtected (ec, loc, best_candidate, queried_type, ec.CurrentType);
-                                                       } else {
-                                                               ec.Report.SymbolRelatedToPreviousError (best_candidate);
-                                                               ErrorIsInaccesible (loc, GetSignatureForError (), ec.Report);
-                                                       }
-                                               }
-
-                                               if (!VerifyArgumentsCompat (ec, ref Arguments, arg_count, best_candidate, cand_params, may_fail, loc))
-                                                       return null;
-
-                                               if (has_inaccessible_candidates_only)
-                                                       return null;
-
-                                               throw new InternalErrorException ("VerifyArgumentsCompat didn't find any problem with rejected candidate " + best_candidate);
-                                       }
+                                       if (NoExactMatch (ec, ref Arguments, candidate_to_form))
+                                               return null;
                                }
 
                                //
@@ -4351,6 +4352,11 @@ namespace Mono.CSharp {
                                return null;
                        }
 
+                       if (arg_count != 0 && Arguments.HasDynamic) {
+                               best_candidate = null;
+                               return this;
+                       }
+
                        if (!is_sorted) {
                                //
                                // At this point, applicable_type is _one_ of the most derived types
@@ -4373,7 +4379,7 @@ namespace Mono.CSharp {
                                        int j = finalized; // where to put the next finalized candidate
                                        int k = finalized; // where to put the next undiscarded candidate
                                        for (int i = finalized; i < candidate_top; ++i) {
-                                               MethodBase candidate = (MethodBase) candidates [i];
+                                               var candidate = candidates [i];
                                                Type decl_type = candidate.DeclaringType;
 
                                                if (decl_type == applicable_type) {
@@ -4406,25 +4412,25 @@ namespace Mono.CSharp {
                        // Now we actually find the best method
                        //
 
-                       best_candidate = (MethodBase) candidates [0];
-                       method_params = candidate_to_form != null && candidate_to_form.Contains (best_candidate);
+                       best_candidate = candidates [0];
+                       method_params = candidate_to_form != null && candidate_to_form.ContainsKey (best_candidate);
 
                        //
                        // TODO: Broken inverse order of candidates logic does not work with optional
                        // parameters used for method overrides and I am not going to fix it for SRE
                        //
-                       if (candidates_expanded != null && candidates_expanded.Contains (best_candidate)) {
-                               candidate_args = (Arguments) candidates_expanded [best_candidate];
+                       if (candidates_expanded != null && candidates_expanded.ContainsKey (best_candidate)) {
+                               candidate_args = candidates_expanded [best_candidate];
                                arg_count = candidate_args.Count;
                        }
 
                        for (int ix = 1; ix < candidate_top; ix++) {
-                               MethodBase candidate = (MethodBase) candidates [ix];
+                               var candidate = candidates [ix];
 
-                               if (candidate == best_candidate)
+                               if (candidate.MetaInfo == best_candidate.MetaInfo)
                                        continue;
 
-                               bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
+                               bool cand_params = candidate_to_form != null && candidate_to_form.ContainsKey (candidate);
 
                                if (BetterFunction (ec, candidate_args, arg_count, 
                                        candidate, cand_params,
@@ -4437,20 +4443,20 @@ namespace Mono.CSharp {
                        // Now check that there are no ambiguities i.e the selected method
                        // should be better than all the others
                        //
-                       MethodBase ambiguous = null;
+                       MethodSpec ambiguous = null;
                        for (int ix = 1; ix < candidate_top; ix++) {
-                               MethodBase candidate = (MethodBase) candidates [ix];
+                               var candidate = candidates [ix];
 
-                               if (candidate == best_candidate)
+                               if (candidate.MetaInfo == best_candidate.MetaInfo)
                                        continue;
 
-                               bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
+                               bool cand_params = candidate_to_form != null && candidate_to_form.ContainsKey (candidate);
                                if (!BetterFunction (ec, candidate_args, arg_count,
                                        best_candidate, method_params,
                                        candidate, cand_params)) 
                                {
                                        if (!may_fail)
-                                               ec.Report.SymbolRelatedToPreviousError (candidate);
+                                               ec.Report.SymbolRelatedToPreviousError (candidate.MetaInfo);
                                        ambiguous = candidate;
                                }
                        }
@@ -4471,29 +4477,29 @@ namespace Mono.CSharp {
                                if (candidate_overrides != null) {
                                        Type[] gen_args = null;
                                        bool gen_override = false;
-                                       if (TypeManager.IsGenericMethod (best_candidate))
-                                               gen_args = TypeManager.GetGenericArguments (best_candidate);
+                                       if (best_candidate.IsGenericMethod)
+                                               gen_args = TypeManager.GetGenericArguments (best_candidate.MetaInfo);
 
-                                       foreach (MethodBase candidate in candidate_overrides) {
-                                               if (TypeManager.IsGenericMethod (candidate)) {
+                                       foreach (var candidate in candidate_overrides) {
+                                               if (candidate.IsGenericMethod) {
                                                        if (gen_args == null)
                                                                continue;
 
-                                                       if (gen_args.Length != TypeManager.GetGenericArguments (candidate).Length)
+                                                       if (gen_args.Length != TypeManager.GetGenericArguments (candidate.MetaInfo).Length)
                                                                continue;
                                                } else {
                                                        if (gen_args != null)
                                                                continue;
                                                }
                                                
-                                               if (IsOverride (candidate, best_candidate)) {
+                                               if (IsOverride (candidate.MetaInfo, best_candidate.MetaInfo)) {
                                                        gen_override = true;
                                                        best_candidate = candidate;
                                                }
                                        }
 
                                        if (gen_override && gen_args != null) {
-                                               best_candidate = ((MethodInfo) best_candidate).MakeGenericMethod (gen_args);
+                                               best_candidate = best_candidate.Inflate (gen_args);
                                        }
                                }
                        }
@@ -4513,7 +4519,7 @@ namespace Mono.CSharp {
 
                        MethodBase the_method = TypeManager.DropGenericMethodArguments (best_candidate);
                        if (TypeManager.IsGenericMethodDefinition (the_method) &&
-                           !ConstraintChecker.CheckConstraints (ec, the_method, best_candidate, loc))
+                           !ConstraintChecker.CheckConstraints (ec, the_method, best_candidate.MetaInfo, loc))
                                return null;
 
                        //
@@ -4525,11 +4531,66 @@ namespace Mono.CSharp {
 
                        IMethodData data = TypeManager.GetMethod (the_method);
                        if (data != null)
-                               data.SetMemberIsUsed ();
+                               data.SetIsUsed ();
 
                        Arguments = candidate_args;
                        return this;
                }
+
+               bool NoExactMatch (ResolveContext ec, ref Arguments Arguments, IDictionary<MethodSpec, MethodSpec> candidate_to_form)
+               {
+                       AParametersCollection pd = best_candidate.Parameters;
+                       int arg_count = Arguments == null ? 0 : Arguments.Count;
+
+                       if (arg_count == pd.Count || pd.HasParams) {
+                               if (TypeManager.IsGenericMethodDefinition (best_candidate.MetaInfo)) {
+                                       if (type_arguments == null) {
+                                               ec.Report.Error (411, loc,
+                                                       "The type arguments for method `{0}' cannot be inferred from " +
+                                                       "the usage. Try specifying the type arguments explicitly",
+                                                       TypeManager.CSharpSignature (best_candidate.MetaInfo));
+                                               return true;
+                                       }
+
+                                       Type[] g_args = TypeManager.GetGenericArguments (best_candidate.MetaInfo);
+                                       if (type_arguments.Count != g_args.Length) {
+                                               ec.Report.SymbolRelatedToPreviousError (best_candidate.MetaInfo);
+                                               ec.Report.Error (305, loc, "Using the generic method `{0}' requires `{1}' type argument(s)",
+                                                       TypeManager.CSharpSignature (best_candidate.MetaInfo),
+                                                       g_args.Length.ToString ());
+                                               return true;
+                                       }
+                               } else {
+                                       if (type_arguments != null && !best_candidate.IsGenericMethod) {
+                                               Error_TypeArgumentsCannotBeUsed (ec.Report, loc);
+                                               return true;
+                                       }
+                               }
+
+                               if (has_inaccessible_candidates_only) {
+                                       if (InstanceExpression != null && type != ec.CurrentType && TypeManager.IsNestedFamilyAccessible (ec.CurrentType, best_candidate.DeclaringType)) {
+                                               // Although a derived class can access protected members of
+                                               // its base class it cannot do so through an instance of the
+                                               // base class (CS1540).  If the qualifier_type is a base of the
+                                               // ec.CurrentType and the lookup succeeds with the latter one,
+                                               // then we are in this situation.
+                                               Error_CannotAccessProtected (ec, loc, best_candidate.MetaInfo, queried_type, ec.CurrentType);
+                                       } else {
+                                               ec.Report.SymbolRelatedToPreviousError (best_candidate.MetaInfo);
+                                               ErrorIsInaccesible (loc, GetSignatureForError (), ec.Report);
+                                       }
+                               }
+
+                               bool cand_params = candidate_to_form != null && candidate_to_form.ContainsKey (best_candidate);
+                               if (!VerifyArgumentsCompat (ec, ref Arguments, arg_count, best_candidate, cand_params, false, loc))
+                                       return true;
+
+                               if (has_inaccessible_candidates_only)
+                                       return true;
+                       }
+
+                       return false;
+               }
                
                public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
                {
@@ -4537,11 +4598,11 @@ namespace Mono.CSharp {
                }
 
                public bool VerifyArgumentsCompat (ResolveContext ec, ref Arguments arguments,
-                                                         int arg_count, MethodBase method,
+                                                         int arg_count, MethodSpec method,
                                                          bool chose_params_expanded,
                                                          bool may_fail, Location loc)
                {
-                       AParametersCollection pd = TypeManager.GetParameterData (method);
+                       AParametersCollection pd = method.Parameters;
                        int param_count = GetApplicableParametersCount (method, pd);
 
                        int errors = ec.Report.Errors;
@@ -4549,8 +4610,8 @@ namespace Mono.CSharp {
                        Type pt = null;
                        int a_idx = 0, a_pos = 0;
                        Argument a = null;
-                       ArrayList params_initializers = null;
-                       bool has_unsafe_arg = method is MethodInfo ? ((MethodInfo) method).ReturnType.IsPointer : false;
+                       ArrayInitializer params_initializers = null;
+                       bool has_unsafe_arg = method.ReturnType.IsPointer;
 
                        for (; a_idx < arg_count; a_idx++, ++a_pos) {
                                a = arguments [a_idx];
@@ -4561,7 +4622,7 @@ namespace Mono.CSharp {
 
                                        if (p_mod == Parameter.Modifier.PARAMS) {
                                                if (chose_params_expanded) {
-                                                       params_initializers = new ArrayList (arg_count - a_idx);
+                                                       params_initializers = new ArrayInitializer (arg_count - a_idx, a.Expr.Location);
                                                        pt = TypeManager.GetElementType (pt);
                                                }
                                        }
@@ -4581,32 +4642,35 @@ namespace Mono.CSharp {
                                } else {
                                        NamedArgument na = a as NamedArgument;
                                        if (na != null) {
-                                               int name_index = pd.GetParameterIndexByName (na.Name.Value);
+                                               int name_index = pd.GetParameterIndexByName (na.Name);
                                                if (name_index < 0 || name_index >= param_count) {
                                                        if (DeclaringType != null && TypeManager.IsDelegateType (DeclaringType)) {
                                                                ec.Report.SymbolRelatedToPreviousError (DeclaringType);
-                                                               ec.Report.Error (1746, na.Name.Location,
+                                                               ec.Report.Error (1746, na.Location,
                                                                        "The delegate `{0}' does not contain a parameter named `{1}'",
-                                                                       TypeManager.CSharpName (DeclaringType), na.Name.Value);
+                                                                       TypeManager.CSharpName (DeclaringType), na.Name);
                                                        } else {
-                                                               ec.Report.SymbolRelatedToPreviousError (best_candidate);
-                                                               ec.Report.Error (1739, na.Name.Location,
+                                                               ec.Report.SymbolRelatedToPreviousError (best_candidate.MetaInfo);
+                                                               ec.Report.Error (1739, na.Location,
                                                                        "The best overloaded method match for `{0}' does not contain a parameter named `{1}'",
-                                                                       TypeManager.CSharpSignature (method), na.Name.Value);
+                                                                       TypeManager.CSharpSignature (method.MetaInfo), na.Name);
                                                        }
                                                } else if (arguments[name_index] != a) {
                                                        if (DeclaringType != null && TypeManager.IsDelegateType (DeclaringType))
                                                                ec.Report.SymbolRelatedToPreviousError (DeclaringType);
                                                        else
-                                                               ec.Report.SymbolRelatedToPreviousError (best_candidate);
+                                                               ec.Report.SymbolRelatedToPreviousError (best_candidate.MetaInfo);
 
-                                                       ec.Report.Error (1744, na.Name.Location,
+                                                       ec.Report.Error (1744, na.Location,
                                                                "Named argument `{0}' cannot be used for a parameter which has positional argument specified",
-                                                               na.Name.Value);
+                                                               na.Name);
                                                }
                                        }
                                }
 
+                               if (TypeManager.IsDynamicType (a.Expr.Type))
+                                       continue;
+
                                if (delegate_type != null && !Delegate.IsTypeCovariant (a.Expr, pt))
                                        break;
 
@@ -4650,7 +4714,7 @@ namespace Mono.CSharp {
                                pt = pd.Types [param_count - 1];
                                pt = TypeManager.GetElementType (pt);
                                has_unsafe_arg |= pt.IsPointer;
-                               params_initializers = new ArrayList (0);
+                               params_initializers = new ArrayInitializer (0, loc);
                        }
 
                        //
@@ -4658,8 +4722,7 @@ namespace Mono.CSharp {
                        //
                        if (params_initializers != null) {
                                arguments.Add (new Argument (
-                                                      new ArrayCreation (new TypeExpression (pt, loc), "[]",
-                                                                         params_initializers, loc).Resolve (ec)));
+                                       new ArrayCreation (new TypeExpression (pt, loc), "[]", params_initializers, loc).Resolve (ec)));
                                arg_count++;
                        }
 
@@ -4681,9 +4744,9 @@ namespace Mono.CSharp {
 
        public class ConstantExpr : MemberExpr
        {
-               FieldInfo constant;
+               ConstSpec constant;
 
-               public ConstantExpr (FieldInfo constant, Location loc)
+               public ConstantExpr (ConstSpec constant, Location loc)
                {
                        this.constant = constant;
                        this.loc = loc;
@@ -4698,47 +4761,33 @@ namespace Mono.CSharp {
                }
 
                public override bool IsStatic {
-                       get { return constant.IsStatic; }
+                       get { return true; }
                }
 
                public override Type DeclaringType {
                        get { return constant.DeclaringType; }
                }
 
-               public override MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, Location loc, SimpleName original)
-               {
-                       constant = TypeManager.GetGenericFieldDefinition (constant);
-
-                       IConstant ic = TypeManager.GetConstant (constant);
-                       if (ic == null) {
-                               if (constant.IsLiteral) {
-                                       ic = new ExternalConstant (constant);
-                               } else {
-                                       ic = ExternalConstant.CreateDecimal (constant);
-                                       // HACK: decimal field was not resolved as constant
-                                       if (ic == null)
-                                               return new FieldExpr (constant, loc).ResolveMemberAccess (ec, left, loc, original);
-                               }
-                               TypeManager.RegisterConstant (constant, ic);
-                       }
-
-                       return base.ResolveMemberAccess (ec, left, loc, original);
-               }
-
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        throw new NotSupportedException ("ET");
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext rc)
                {
-                       IConstant ic = TypeManager.GetConstant (constant);
-                       if (ic.ResolveValue ()) {
-                               if (!ec.IsObsolete)
-                                       ic.CheckObsoleteness (loc);
+                       constant.MemberDefinition.SetIsUsed ();
+
+                       if (!rc.IsObsolete) {
+                               var oa = constant.GetObsoleteAttribute ();
+                               if (oa != null)
+                                       AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (constant.MetaInfo), loc, rc.Report);
                        }
 
-                       return ic.CreateConstantReference (loc);
+                       // Constants are resolved on-demand
+                       var c = constant.Value.Resolve (rc) as Constant;
+
+                       // Creates reference expression to the constant value
+                       return Constant.CreateConstant (rc, c.Type, c.GetValue (), loc);
                }
 
                public override void Emit (EmitContext ec)
@@ -4748,7 +4797,7 @@ namespace Mono.CSharp {
 
                public override string GetSignatureForError ()
                {
-                       return TypeManager.GetFullNameSignature (constant);
+                       return TypeManager.GetFullNameSignature (constant.MetaInfo);
                }
        }
 
@@ -4756,7 +4805,7 @@ namespace Mono.CSharp {
        ///   Fully resolved expression that evaluates to a Field
        /// </summary>
        public class FieldExpr : MemberExpr, IDynamicAssign, IMemoryLocation, IVariableReference {
-               public FieldInfo FieldInfo;
+               protected FieldSpec spec;
                readonly Type constructed_generic_type;
                VariableInfo variable_info;
                
@@ -4767,15 +4816,22 @@ namespace Mono.CSharp {
                {
                        loc = l;
                }
+
+               public FieldExpr (FieldSpec spec, Location loc)
+               {
+                       this.spec = spec;
+                       this.loc = loc;
+
+                       type = TypeManager.TypeToCoreType (spec.FieldType);
+               }
                
-               public FieldExpr (FieldInfo fi, Location l)
+               public FieldExpr (FieldBase fi, Location l)
+                       : this (fi.Spec, l)
                {
-                       FieldInfo = fi;
-                       type = TypeManager.TypeToCoreType (fi.FieldType);
                        loc = l;
                }
 
-               public FieldExpr (FieldInfo fi, Type genericType, Location l)
+               public FieldExpr (Field fi, Type genericType, Location l)
                        : this (fi, l)
                {
                        if (TypeManager.IsGenericTypeDefinition (genericType))
@@ -4785,31 +4841,37 @@ namespace Mono.CSharp {
 
                public override string Name {
                        get {
-                               return FieldInfo.Name;
+                               return spec.Name;
                        }
                }
 
                public override bool IsInstance {
                        get {
-                               return !FieldInfo.IsStatic;
+                               return !spec.IsStatic;
                        }
                }
 
                public override bool IsStatic {
                        get {
-                               return FieldInfo.IsStatic;
+                               return spec.IsStatic;
+                       }
+               }
+
+               public FieldSpec Spec {
+                       get {
+                               return spec;
                        }
                }
 
                public override Type DeclaringType {
                        get {
-                               return FieldInfo.DeclaringType;
+                               return spec.MetaInfo.DeclaringType;
                        }
                }
 
                public override string GetSignatureForError ()
                {
-                       return TypeManager.GetFullNameSignature (FieldInfo);
+                       return TypeManager.GetFullNameSignature (spec.MetaInfo);
                }
 
                public VariableInfo VariableInfo {
@@ -4821,7 +4883,7 @@ namespace Mono.CSharp {
                public override MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, Location loc,
                                                                SimpleName original)
                {
-                       FieldInfo fi = TypeManager.GetGenericFieldDefinition (FieldInfo);
+                       FieldInfo fi = TypeManager.GetGenericFieldDefinition (spec.MetaInfo);
                        Type t = fi.FieldType;
 
                        if (t.IsPointer && !ec.IsUnsafe) {
@@ -4856,17 +4918,17 @@ namespace Mono.CSharp {
 
                public Expression CreateTypeOfExpression ()
                {
-                       return new TypeOfField (GetConstructedFieldInfo (), loc);
+                       return new TypeOfField (Import.CreateField (GetConstructedFieldInfo ()), loc);
                }
 
-               override public Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return DoResolve (ec, false, false);
                }
 
                Expression DoResolve (ResolveContext ec, bool lvalue_instance, bool out_access)
                {
-                       if (!FieldInfo.IsStatic){
+                       if (!IsStatic){
                                if (InstanceExpression == null){
                                        //
                                        // This can happen when referencing an instance field using
@@ -4889,10 +4951,11 @@ namespace Mono.CSharp {
                                                        InstanceExpression = InstanceExpression.ResolveLValue (ec, right_side);
                                        }
                                } else {
-                                       ResolveFlags rf = ResolveFlags.VariableOrValue | ResolveFlags.DisableFlowAnalysis;
-
-                                       if (InstanceExpression != EmptyExpression.Null)
-                                               InstanceExpression = InstanceExpression.Resolve (ec, rf);
+                                       if (InstanceExpression != EmptyExpression.Null) {
+                                               using (ec.With (ResolveContext.Options.DoFlowAnalysis, false)) {
+                                                       InstanceExpression = InstanceExpression.Resolve (ec, ResolveFlags.VariableOrValue);
+                                               }
+                                       }
                                }
 
                                if (InstanceExpression == null)
@@ -4903,22 +4966,18 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       // TODO: the code above uses some non-standard multi-resolve rules
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        if (!ec.IsObsolete) {
-                               FieldBase f = TypeManager.GetField (FieldInfo);
+                               FieldBase f = TypeManager.GetField (spec.MetaInfo);
                                if (f != null) {
                                        f.CheckObsoleteness (loc);
                                } else {
-                                       ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (FieldInfo);
+                                       ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (spec.MetaInfo);
                                        if (oa != null)
-                                               AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (FieldInfo), loc, ec.Report);
+                                               AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (spec.MetaInfo), loc, ec.Report);
                                }
                        }
 
-                       IFixedBuffer fb = AttributeTester.GetFixedBuffer (FieldInfo);
+                       var fb = spec as FixedFieldSpec;
                        IVariableReference var = InstanceExpression as IVariableReference;
                        
                        if (fb != null) {
@@ -4928,9 +4987,9 @@ namespace Mono.CSharp {
                                }
 
                                if (InstanceExpression.eclass != ExprClass.Variable) {
-                                       ec.Report.SymbolRelatedToPreviousError (FieldInfo);
+                                       ec.Report.SymbolRelatedToPreviousError (spec.MetaInfo);
                                        ec.Report.Error (1708, loc, "`{0}': Fixed size buffers can only be accessed through locals or fields",
-                                               TypeManager.GetFullNameSignature (FieldInfo));
+                                               TypeManager.GetFullNameSignature (spec.MetaInfo));
                                } else if (var != null && var.IsHoisted) {
                                        AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, var, loc);
                                }
@@ -4945,11 +5004,10 @@ namespace Mono.CSharp {
                                return this;
 
                        VariableInfo vi = var.VariableInfo;
-                       if (!vi.IsFieldAssigned (ec, FieldInfo.Name, loc))
+                       if (!vi.IsFieldAssigned (ec, Name, loc))
                                return null;
 
-                       variable_info = vi.GetSubStruct (FieldInfo.Name);
-                       eclass = ExprClass.Variable;
+                       variable_info = vi.GetSubStruct (Name);
                        return this;
                }
 
@@ -4979,7 +5037,7 @@ namespace Mono.CSharp {
                Expression Report_AssignToReadonly (ResolveContext ec, Expression right_side)
                {
                        int i = 0;
-                       if (right_side == EmptyExpression.OutAccess || right_side == EmptyExpression.LValueMemberOutAccess)
+                       if (right_side == EmptyExpression.OutAccess.Instance || right_side == EmptyExpression.LValueMemberOutAccess)
                                i += 1;
                        if (IsStatic)
                                i += 2;
@@ -4994,21 +5052,21 @@ namespace Mono.CSharp {
                {
                        IVariableReference var = InstanceExpression as IVariableReference;
                        if (var != null && var.VariableInfo != null)
-                               var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
+                               var.VariableInfo.SetFieldAssigned (ec, Name);
 
-                       bool lvalue_instance = !FieldInfo.IsStatic && TypeManager.IsValueType (FieldInfo.DeclaringType);
-                       bool out_access = right_side == EmptyExpression.OutAccess || right_side == EmptyExpression.LValueMemberOutAccess;
+                       bool lvalue_instance = !spec.IsStatic && TypeManager.IsValueType (spec.MetaInfo.DeclaringType);
+                       bool out_access = right_side == EmptyExpression.OutAccess.Instance || right_side == EmptyExpression.LValueMemberOutAccess;
 
                        Expression e = DoResolve (ec, lvalue_instance, out_access);
 
                        if (e == null)
                                return null;
 
-                       FieldBase fb = TypeManager.GetField (FieldInfo);
+                       FieldBase fb = TypeManager.GetField (spec.MetaInfo);
                        if (fb != null) {
                                fb.SetAssigned ();
 
-                               if ((right_side == EmptyExpression.UnaryAddress || right_side == EmptyExpression.OutAccess) &&
+                               if ((right_side == EmptyExpression.UnaryAddress || right_side == EmptyExpression.OutAccess.Instance) &&
                                        (fb.ModFlags & Modifiers.VOLATILE) != 0) {
                                        ec.Report.Warning (420, 1, loc,
                                                "`{0}': A volatile field references will not be treated as volatile",
@@ -5016,7 +5074,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (FieldInfo.IsInitOnly) {
+                       if (spec.IsReadOnly) {
                                // InitOnly fields can only be assigned in constructors or initializers
                                if (!ec.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.ConstructorScope))
                                        return Report_AssignToReadonly (ec, right_side);
@@ -5025,7 +5083,7 @@ namespace Mono.CSharp {
                                        Type ctype = ec.CurrentType;
 
                                        // InitOnly fields cannot be assigned-to in a different constructor from their declaring type
-                                       if (!TypeManager.IsEqual (ctype, FieldInfo.DeclaringType))
+                                       if (!TypeManager.IsEqual (ctype, DeclaringType))
                                                return Report_AssignToReadonly (ec, right_side);
                                        // static InitOnly fields cannot be assigned-to in an instance constructor
                                        if (IsStatic && !ec.IsStatic)
@@ -5036,7 +5094,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (right_side == EmptyExpression.OutAccess &&
+                       if (right_side == EmptyExpression.OutAccess.Instance &&
                            !IsStatic && !(InstanceExpression is This) && TypeManager.mbr_type != null && TypeManager.IsSubclassOf (DeclaringType, TypeManager.mbr_type)) {
                                ec.Report.SymbolRelatedToPreviousError (DeclaringType);
                                ec.Report.Warning (197, 1, loc,
@@ -5064,7 +5122,7 @@ namespace Mono.CSharp {
 
                public override int GetHashCode ()
                {
-                       return FieldInfo.GetHashCode ();
+                       return spec.GetHashCode ();
                }
                
                public bool IsFixed {
@@ -5094,7 +5152,7 @@ namespace Mono.CSharp {
                        if (fe == null)
                                return false;
 
-                       if (FieldInfo != fe.FieldInfo)
+                       if (spec.MetaInfo != fe.spec.MetaInfo)
                                return false;
 
                        if (InstanceExpression == null || fe.InstanceExpression == null)
@@ -5108,15 +5166,15 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
                        bool is_volatile = false;
 
-                       FieldBase f = TypeManager.GetField (FieldInfo);
+                       var f = TypeManager.GetField (spec.MetaInfo);
                        if (f != null){
                                if ((f.ModFlags & Modifiers.VOLATILE) != 0)
                                        is_volatile = true;
 
-                               f.SetMemberIsUsed ();
+                               f.SetIsUsed ();
                        }
                        
-                       if (FieldInfo.IsStatic){
+                       if (IsStatic){
                                if (is_volatile)
                                        ig.Emit (OpCodes.Volatile);
 
@@ -5129,7 +5187,7 @@ namespace Mono.CSharp {
                                if (TypeManager.IsStruct (type) && TypeManager.IsEqual (type, ec.MemberContext.CurrentType)) {
                                        LoadFromPtr (ig, type);
                                } else {
-                                       IFixedBuffer ff = AttributeTester.GetFixedBuffer (FieldInfo);
+                                       var ff = spec as FixedFieldSpec;
                                        if (ff != null) {
                                                ig.Emit (OpCodes.Ldflda, GetConstructedFieldInfo ());
                                                ig.Emit (OpCodes.Ldflda, ff.Element);
@@ -5144,7 +5202,7 @@ namespace Mono.CSharp {
 
                        if (leave_copy) {
                                ec.ig.Emit (OpCodes.Dup);
-                               if (!FieldInfo.IsStatic) {
+                               if (!IsStatic) {
                                        temp = new LocalTemporary (this.Type);
                                        temp.Store (ec);
                                }
@@ -5153,8 +5211,8 @@ namespace Mono.CSharp {
 
                public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
-                       FieldAttributes fa = FieldInfo.Attributes;
-                       bool is_static = (fa & FieldAttributes.Static) != 0;
+                       //FieldAttributes fa = FieldInfo.Attributes;
+                       //bool is_static = (fa & FieldAttributes.Static) != 0;
                        ILGenerator ig = ec.ig;
 
                        prepared = prepare_for_load;
@@ -5163,13 +5221,13 @@ namespace Mono.CSharp {
                        source.Emit (ec);
                        if (leave_copy) {
                                ec.ig.Emit (OpCodes.Dup);
-                               if (!FieldInfo.IsStatic) {
+                               if (!IsStatic) {
                                        temp = new LocalTemporary (this.Type);
                                        temp.Store (ec);
                                }
                        }
 
-                       FieldBase f = TypeManager.GetField (FieldInfo);
+                       FieldBase f = TypeManager.GetField (spec.MetaInfo);
                        if (f != null){
                                if ((f.ModFlags & Modifiers.VOLATILE) != 0)
                                        ig.Emit (OpCodes.Volatile);
@@ -5177,7 +5235,7 @@ namespace Mono.CSharp {
                                f.SetAssigned ();
                        }
 
-                       if (is_static)
+                       if (IsStatic)
                                ig.Emit (OpCodes.Stsfld, GetConstructedFieldInfo ());
                        else
                                ig.Emit (OpCodes.Stfld, GetConstructedFieldInfo ());
@@ -5196,7 +5254,7 @@ namespace Mono.CSharp {
 
                public override void EmitSideEffect (EmitContext ec)
                {
-                       FieldBase f = TypeManager.GetField (FieldInfo);
+                       FieldBase f = TypeManager.GetField (spec.MetaInfo);
                        bool is_volatile = f != null && (f.ModFlags & Modifiers.VOLATILE) != 0;
 
                        if (is_volatile || is_marshal_by_ref ())
@@ -5214,12 +5272,12 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
 
-                       FieldBase f = TypeManager.GetField (FieldInfo);
+                       FieldBase f = TypeManager.GetField (spec.MetaInfo);
                        if (f != null){                         
                                if ((mode & AddressOp.Store) != 0)
                                        f.SetAssigned ();
                                if ((mode & AddressOp.Load) != 0)
-                                       f.SetMemberIsUsed ();
+                                       f.SetIsUsed ();
                        }
 
                        //
@@ -5227,10 +5285,10 @@ namespace Mono.CSharp {
                        // get the address of the copy.
                        //
                        bool need_copy;
-                       if (FieldInfo.IsInitOnly){
+                       if (spec.IsReadOnly){
                                need_copy = true;
                                if (ec.HasSet (EmitContext.Options.ConstructorScope)){
-                                       if (FieldInfo.IsStatic){
+                                       if (IsStatic){
                                                if (ec.IsStatic)
                                                        need_copy = false;
                                        } else
@@ -5249,7 +5307,7 @@ namespace Mono.CSharp {
                        }
 
 
-                       if (FieldInfo.IsStatic){
+                       if (IsStatic){
                                ig.Emit (OpCodes.Ldsflda, GetConstructedFieldInfo ());
                        } else {
                                if (!prepared)
@@ -5261,12 +5319,11 @@ namespace Mono.CSharp {
                FieldInfo GetConstructedFieldInfo ()
                {
                        if (constructed_generic_type == null)
-                               return FieldInfo;
+                               return spec.MetaInfo;
 
-                       return TypeBuilder.GetField (constructed_generic_type, FieldInfo);
+                       return TypeBuilder.GetField (constructed_generic_type, spec.MetaInfo);
                }
 
-#if NET_4_0
                public SLE.Expression MakeAssignExpression (BuilderContext ctx)
                {
                        return MakeExpression (ctx);
@@ -5274,13 +5331,12 @@ namespace Mono.CSharp {
 
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       return SLE.Expression.Field (InstanceExpression.MakeExpression (ctx), FieldInfo);
+                       return SLE.Expression.Field (InstanceExpression.MakeExpression (ctx), spec.MetaInfo);
                }
-#endif
                
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
-                       FieldInfo = storey.MutateField (FieldInfo);
+                       storey.MutateField (spec);
                        base.MutateHoistedGenericType (storey);
                }               
        }
@@ -5295,31 +5351,28 @@ namespace Mono.CSharp {
        /// </summary>
        public class PropertyExpr : MemberExpr, IDynamicAssign
        {
-               public readonly PropertyInfo PropertyInfo;
-               MethodInfo getter, setter;
+               PropertySpec spec;
+               MethodSpec getter, setter;
                bool is_static;
 
-               bool resolved;
                TypeArguments targs;
                
                LocalTemporary temp;
                bool prepared;
 
-               public PropertyExpr (Type container_type, PropertyInfo pi, Location l)
+               public PropertyExpr (Type container_type, PropertySpec spec, Location l)
                {
-                       PropertyInfo = pi;
-                       eclass = ExprClass.PropertyAccess;
-                       is_static = false;
+                       this.spec = spec;
                        loc = l;
 
-                       type = TypeManager.TypeToCoreType (pi.PropertyType);
+                       type = TypeManager.TypeToCoreType (spec.PropertyType);
 
                        ResolveAccessors (container_type);
                }
 
                public override string Name {
                        get {
-                               return PropertyInfo.Name;
+                               return spec.Name;
                        }
                }
 
@@ -5365,13 +5418,13 @@ namespace Mono.CSharp {
 
                public override Type DeclaringType {
                        get {
-                               return PropertyInfo.DeclaringType;
+                               return spec.DeclaringType;
                        }
                }
 
                public override string GetSignatureForError ()
                {
-                       return TypeManager.GetFullNameSignature (PropertyInfo);
+                       return TypeManager.GetFullNameSignature (spec.MetaInfo);
                }
 
                void FindAccessors (Type invocation_type)
@@ -5380,11 +5433,11 @@ namespace Mono.CSharp {
                                BindingFlags.Static | BindingFlags.Instance |
                                BindingFlags.DeclaredOnly;
 
-                       Type current = PropertyInfo.DeclaringType;
+                       Type current = spec.DeclaringType;
                        for (; current != null; current = current.BaseType) {
                                MemberInfo[] group = TypeManager.MemberLookup (
                                        invocation_type, invocation_type, current,
-                                       MemberTypes.Property, flags, PropertyInfo.Name, null);
+                                       MemberTypes.Property, flags, spec.Name, null);
 
                                if (group == null)
                                        continue;
@@ -5395,13 +5448,19 @@ namespace Mono.CSharp {
 
                                PropertyInfo pi = (PropertyInfo) group [0];
 
-                               if (getter == null)
-                                       getter = pi.GetGetMethod (true);
+                               if (getter == null) {
+                                       var m = pi.GetGetMethod (true);
+                                       if (m != null)
+                                               getter = Import.CreateMethod (m);
+                               }
 
-                               if (setter == null)
-                                       setter = pi.GetSetMethod (true);
+                               if (setter == null) {
+                                       var m = pi.GetSetMethod (true);
+                                       if (m != null)
+                                               setter = Import.CreateMethod (m);
+                               }
 
-                               MethodInfo accessor = getter != null ? getter : setter;
+                               var accessor = getter != null ? getter : setter;
 
                                if (!accessor.IsVirtual)
                                        return;
@@ -5421,7 +5480,7 @@ namespace Mono.CSharp {
                                MethodBase the_getter = TypeManager.DropGenericMethodArguments (getter);
                                IMethodData md = TypeManager.GetMethod (the_getter);
                                if (md != null)
-                                       md.SetMemberIsUsed ();
+                                       md.SetIsUsed ();
 
                                is_static = getter.IsStatic;
                        }
@@ -5430,23 +5489,21 @@ namespace Mono.CSharp {
                                MethodBase the_setter = TypeManager.DropGenericMethodArguments (setter);
                                IMethodData md = TypeManager.GetMethod (the_setter);
                                if (md != null)
-                                       md.SetMemberIsUsed ();
+                                       md.SetIsUsed ();
 
                                is_static = setter.IsStatic;
                        }
                }
 
-#if NET_4_0
                public SLE.Expression MakeAssignExpression (BuilderContext ctx)
                {
-                       return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), setter);
+                       return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), (MethodInfo) setter.MetaInfo);
                }
 
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), getter);
+                       return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), (MethodInfo) getter.MetaInfo);
                }
-#endif
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
@@ -5455,9 +5512,15 @@ namespace Mono.CSharp {
 
                        type = storey.MutateType (type);
                        if (getter != null)
-                               getter = storey.MutateGenericMethod (getter);
+                               storey.MutateGenericMethod (getter);
                        if (setter != null)
-                               setter = storey.MutateGenericMethod (setter);
+                               storey.MutateGenericMethod (setter);
+               }
+
+               public PropertyInfo PropertyInfo {
+                       get {
+                               return spec.MetaInfo;
+                       }
                }
 
                bool InstanceResolve (ResolveContext ec, bool lvalue_instance, bool must_do_cs1540_check)
@@ -5472,7 +5535,7 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       InstanceExpression = InstanceExpression.DoResolve (ec);
+                       InstanceExpression = InstanceExpression.Resolve (ec);
                        if (lvalue_instance && InstanceExpression != null)
                                InstanceExpression = InstanceExpression.ResolveLValue (ec, EmptyExpression.LValueMemberAccess);
 
@@ -5485,30 +5548,30 @@ namespace Mono.CSharp {
                            !TypeManager.IsInstantiationOfSameGenericType (InstanceExpression.Type, ec.CurrentType) &&
                            !TypeManager.IsNestedChildOf (ec.CurrentType, InstanceExpression.Type) &&
                            !TypeManager.IsSubclassOf (InstanceExpression.Type, ec.CurrentType)) {
-                               ec.Report.SymbolRelatedToPreviousError (PropertyInfo);
-                               Error_CannotAccessProtected (ec, loc, PropertyInfo, InstanceExpression.Type, ec.CurrentType);
+                               ec.Report.SymbolRelatedToPreviousError (spec.MetaInfo);
+                               Error_CannotAccessProtected (ec, loc, spec.MetaInfo, InstanceExpression.Type, ec.CurrentType);
                                return false;
                        }
 
                        return true;
                }
 
-               void Error_PropertyNotFound (ResolveContext ec, MethodInfo mi, bool getter)
+               void Error_PropertyNotFound (ResolveContext ec, MethodSpec mi, bool getter)
                {
                        // TODO: correctly we should compare arguments but it will lead to bigger changes
-                       if (mi is MethodBuilder) {
-                               Error_TypeDoesNotContainDefinition (ec, loc, PropertyInfo.DeclaringType, Name);
+                       if (mi.MetaInfo is MethodBuilder) {
+                               Error_TypeDoesNotContainDefinition (ec, loc, spec.DeclaringType, Name);
                                return;
                        }
                        
                        StringBuilder sig = new StringBuilder (TypeManager.CSharpName (mi.DeclaringType));
                        sig.Append ('.');
-                       AParametersCollection iparams = TypeManager.GetParameterData (mi);
+                       AParametersCollection iparams = mi.Parameters;
                        sig.Append (getter ? "get_" : "set_");
                        sig.Append (Name);
                        sig.Append (iparams.GetSignatureForError ());
 
-                       ec.Report.SymbolRelatedToPreviousError (mi);
+                       ec.Report.SymbolRelatedToPreviousError (mi.MetaInfo);
                        ec.Report.Error (1546, loc, "Property `{0}' is not supported by the C# language. Try to call the accessor method `{1}' directly",
                                Name, sig.ToString ());
                }
@@ -5516,7 +5579,7 @@ namespace Mono.CSharp {
                public bool IsAccessibleFrom (Type invocation_type, bool lvalue)
                {
                        bool dummy;
-                       MethodInfo accessor = lvalue ? setter : getter;
+                       var accessor = lvalue ? setter : getter;
                        if (accessor == null && lvalue)
                                accessor = getter;
                        return accessor != null && IsAccessorAccessible (invocation_type, accessor, out dummy);
@@ -5532,10 +5595,9 @@ namespace Mono.CSharp {
                        return t_name_len > 2 && t_name [t_name_len - 2] == '[';
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (resolved)
-                               return this;
+                       eclass = ExprClass.PropertyAccess;
 
                        bool must_do_cs1540_check = false;
                        ec.Report.DisableReporting ();
@@ -5549,7 +5611,7 @@ namespace Mono.CSharp {
                                        if (ex_method_lookup != null) {
                                                ex_method_lookup.ExtensionExpression = InstanceExpression;
                                                ex_method_lookup.SetTypeArguments (ec, targs);
-                                               return ex_method_lookup.DoResolve (ec);
+                                               return ex_method_lookup.Resolve (ec);
                                        }
                                }
 
@@ -5564,38 +5626,37 @@ namespace Mono.CSharp {
                        // Only base will allow this invocation to happen.
                        //
                        if (IsBase && getter.IsAbstract) {
-                               Error_CannotCallAbstractBase (ec, TypeManager.GetFullNameSignature (PropertyInfo));
+                               Error_CannotCallAbstractBase (ec, TypeManager.GetFullNameSignature (spec.MetaInfo));
                        }
 
-                       if (PropertyInfo.PropertyType.IsPointer && !ec.IsUnsafe){
+                       if (spec.PropertyType.IsPointer && !ec.IsUnsafe){
                                UnsafeError (ec, loc);
                        }
 
                        if (!ec.IsObsolete) {
-                               PropertyBase pb = TypeManager.GetProperty (PropertyInfo);
+                               PropertyBase pb = TypeManager.GetProperty (spec.MetaInfo);
                                if (pb != null) {
                                        pb.CheckObsoleteness (loc);
                                } else {
-                                       ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (PropertyInfo);
+                                       ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (spec.MetaInfo);
                                        if (oa != null)
                                                AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, ec.Report);
                                }
                        }
 
-                       resolved = true;
-
                        return this;
                }
 
                override public Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
-                       if (right_side == EmptyExpression.OutAccess) {
-                               if (ec.CurrentBlock.Toplevel.GetParameterReference (PropertyInfo.Name, loc) is MemberAccess) {
+                       eclass = ExprClass.PropertyAccess;
+
+                       if (right_side == EmptyExpression.OutAccess.Instance) {
+                               if (ec.CurrentBlock.Toplevel.GetParameterReference (spec.Name, loc) is MemberAccess) {
                                        ec.Report.Error (1939, loc, "A range variable `{0}' may not be passes as `ref' or `out' parameter",
-                                           PropertyInfo.Name);
+                                           spec.Name);
                                } else {
-                                       ec.Report.Error (206, loc, "A property or indexer `{0}' may not be passed as `ref' or `out' parameter",
-                                             GetSignatureForError ());
+                                       right_side.DoResolveLValue (ec, this);
                                }
                                return null;
                        }
@@ -5614,9 +5675,9 @@ namespace Mono.CSharp {
                                if (getter == null)
                                        return null;
 
-                               if (ec.CurrentBlock.Toplevel.GetParameterReference (PropertyInfo.Name, loc) is MemberAccess) {
+                               if (ec.CurrentBlock.Toplevel.GetParameterReference (spec.Name, loc) is MemberAccess) {
                                        ec.Report.Error (1947, loc, "A range variable `{0}' cannot be assigned to. Consider using `let' clause to store the value",
-                                               PropertyInfo.Name);
+                                               spec.Name);
                                } else {
                                        ec.Report.Error (200, loc, "Property or indexer `{0}' cannot be assigned to (it is read only)",
                                                GetSignatureForError ());
@@ -5629,46 +5690,46 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       if (TypeManager.GetParameterData (setter).Count != 1){
+                       if (setter.Parameters.Count != 1){
                                Error_PropertyNotFound (ec, setter, false);
                                return null;
                        }
 
                        bool must_do_cs1540_check;
                        if (!IsAccessorAccessible (ec.CurrentType, setter, out must_do_cs1540_check)) {
-                               PropertyBase.PropertyMethod pm = TypeManager.GetMethod (setter) as PropertyBase.PropertyMethod;
+                               PropertyBase.PropertyMethod pm = TypeManager.GetMethod (setter.MetaInfo) as PropertyBase.PropertyMethod;
                                if (pm != null && pm.HasCustomAccessModifier) {
                                        ec.Report.SymbolRelatedToPreviousError (pm);
                                        ec.Report.Error (272, loc, "The property or indexer `{0}' cannot be used in this context because the set accessor is inaccessible",
                                                TypeManager.CSharpSignature (setter));
                                }
                                else {
-                                       ec.Report.SymbolRelatedToPreviousError (setter);
+                                       ec.Report.SymbolRelatedToPreviousError (setter.MetaInfo);
                                        ErrorIsInaccesible (loc, TypeManager.CSharpSignature (setter), ec.Report);
                                }
                                return null;
                        }
                        
-                       if (!InstanceResolve (ec, TypeManager.IsStruct (PropertyInfo.DeclaringType), must_do_cs1540_check))
+                       if (!InstanceResolve (ec, TypeManager.IsStruct (spec.DeclaringType), must_do_cs1540_check))
                                return null;
                        
                        //
                        // Only base will allow this invocation to happen.
                        //
                        if (IsBase && setter.IsAbstract){
-                               Error_CannotCallAbstractBase (ec, TypeManager.GetFullNameSignature (PropertyInfo));
+                               Error_CannotCallAbstractBase (ec, TypeManager.GetFullNameSignature (spec.MetaInfo));
                        }
 
-                       if (PropertyInfo.PropertyType.IsPointer && !ec.IsUnsafe) {
+                       if (spec.PropertyType.IsPointer && !ec.IsUnsafe) {
                                UnsafeError (ec, loc);
                        }
 
                        if (!ec.IsObsolete) {
-                               PropertyBase pb = TypeManager.GetProperty (PropertyInfo);
+                               PropertyBase pb = TypeManager.GetProperty (spec.MetaInfo);
                                if (pb != null) {
                                        pb.CheckObsoleteness (loc);
                                } else {
-                                       ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (PropertyInfo);
+                                       ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (spec.MetaInfo);
                                        if (oa != null)
                                                AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, ec.Report);
                                }
@@ -5750,7 +5811,7 @@ namespace Mono.CSharp {
                        }
 
                        if (getter != null) {
-                               if (TypeManager.GetParameterData (getter).Count != 0) {
+                               if (!getter.Parameters.IsEmpty) {
                                        Error_PropertyNotFound (ec, getter, true);
                                        return false;
                                }
@@ -5768,21 +5829,21 @@ namespace Mono.CSharp {
 
                                if (InstanceExpression != EmptyExpression.Null) {
                                        ec.Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks the `get' accessor",
-                                               TypeManager.GetFullNameSignature (PropertyInfo));
+                                               TypeManager.GetFullNameSignature (spec.MetaInfo));
                                        return false;
                                }
                        }
 
                        if (getter != null &&
                                !IsAccessorAccessible (ec.CurrentType, getter, out must_do_cs1540_check)) {
-                               PropertyBase.PropertyMethod pm = TypeManager.GetMethod (getter) as PropertyBase.PropertyMethod;
+                               PropertyBase.PropertyMethod pm = TypeManager.GetMethod (getter.MetaInfo) as PropertyBase.PropertyMethod;
                                if (pm != null && pm.HasCustomAccessModifier) {
                                        ec.Report.SymbolRelatedToPreviousError (pm);
                                        ec.Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because the get accessor is inaccessible",
-                                               TypeManager.CSharpSignature (getter));
+                                               TypeManager.CSharpSignature (getter.MetaInfo));
                                } else {
-                                       ec.Report.SymbolRelatedToPreviousError (getter);
-                                       ErrorIsInaccesible (loc, TypeManager.CSharpSignature (getter), ec.Report);
+                                       ec.Report.SymbolRelatedToPreviousError (getter.MetaInfo);
+                                       ErrorIsInaccesible (loc, TypeManager.CSharpSignature (getter.MetaInfo), ec.Report);
                                }
 
                                return false;
@@ -5800,52 +5861,37 @@ namespace Mono.CSharp {
        /// <summary>
        ///   Fully resolved expression that evaluates to an Event
        /// </summary>
-       public class EventExpr : MemberExpr {
-               public readonly EventInfo EventInfo;
-
-               bool is_static;
-               MethodInfo add_accessor, remove_accessor;
+       public class EventExpr : MemberExpr
+       {
+               readonly EventSpec spec;
 
-               public EventExpr (EventInfo ei, Location loc)
+               public EventExpr (EventSpec spec, Location loc)
                {
-                       EventInfo = ei;
+                       this.spec = spec;
                        this.loc = loc;
-                       eclass = ExprClass.EventAccess;
-
-                       add_accessor = TypeManager.GetAddMethod (ei);
-                       remove_accessor = TypeManager.GetRemoveMethod (ei);
-                       if (add_accessor.IsStatic || remove_accessor.IsStatic)
-                               is_static = true;
-
-                       if (EventInfo is MyEventBuilder){
-                               MyEventBuilder eb = (MyEventBuilder) EventInfo;
-                               type = eb.EventType;
-                               eb.SetUsed ();
-                       } else
-                               type = EventInfo.EventHandlerType;
                }
 
                public override string Name {
                        get {
-                               return EventInfo.Name;
+                               return spec.Name;
                        }
                }
 
                public override bool IsInstance {
                        get {
-                               return !is_static;
+                               return !spec.IsStatic;
                        }
                }
 
                public override bool IsStatic {
                        get {
-                               return is_static;
+                               return spec.IsStatic;
                        }
                }
 
                public override Type DeclaringType {
                        get {
-                               return EventInfo.DeclaringType;
+                               return spec.DeclaringType;
                        }
                }
                
@@ -5862,18 +5908,21 @@ namespace Mono.CSharp {
                        // If the event is local to this class, we transform ourselves into a FieldExpr
                        //
 
-                       if (EventInfo.DeclaringType == ec.CurrentType ||
-                           TypeManager.IsNestedChildOf(ec.CurrentType, EventInfo.DeclaringType)) {
-                               EventField mi = TypeManager.GetEventField (EventInfo);
+                       if (spec.DeclaringType == ec.CurrentType ||
+                           TypeManager.IsNestedChildOf(ec.CurrentType, spec.DeclaringType)) {
+                                       
+                               // TODO: Breaks dynamic binder as currect context fields are imported and not compiled
+                               EventField mi = TypeManager.GetEventField (spec.MetaInfo).MemberDefinition as EventField;
 
-                               if (mi != null) {
+                               if (mi != null && mi.HasBackingField) {
+                                       mi.SetIsUsed ();
                                        if (!ec.IsObsolete)
                                                mi.CheckObsoleteness (loc);
 
                                        if ((mi.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0 && !ec.HasSet (ResolveContext.Options.CompoundAssignmentScope))
                                                Error_AssignmentEventOnly (ec);
                                        
-                                       FieldExpr ml = new FieldExpr (mi.BackingField.FieldBuilder, loc);
+                                       FieldExpr ml = new FieldExpr (mi.BackingField, loc);
 
                                        InstanceExpression = null;
                                
@@ -5889,7 +5938,7 @@ namespace Mono.CSharp {
 
                bool InstanceResolve (ResolveContext ec, bool must_do_cs1540_check)
                {
-                       if (is_static) {
+                       if (IsStatic) {
                                InstanceExpression = null;
                                return true;
                        }
@@ -5899,12 +5948,12 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       InstanceExpression = InstanceExpression.DoResolve (ec);
+                       InstanceExpression = InstanceExpression.Resolve (ec);
                        if (InstanceExpression == null)
                                return false;
 
-                       if (IsBase && add_accessor.IsAbstract) {
-                               Error_CannotCallAbstractBase (ec, TypeManager.CSharpSignature(add_accessor));
+                       if (IsBase && spec.IsAbstract) {
+                               Error_CannotCallAbstractBase (ec, TypeManager.CSharpSignature(spec.MetaInfo));
                                return false;
                        }
 
@@ -5918,8 +5967,8 @@ namespace Mono.CSharp {
                            !TypeManager.IsInstantiationOfSameGenericType (InstanceExpression.Type, ec.CurrentType) &&
                            !TypeManager.IsNestedChildOf (ec.CurrentType, InstanceExpression.Type) &&
                            !TypeManager.IsSubclassOf (InstanceExpression.Type, ec.CurrentType)) {
-                               ec.Report.SymbolRelatedToPreviousError (EventInfo);
-                               ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo), ec.Report);
+                               ec.Report.SymbolRelatedToPreviousError (spec.MetaInfo);
+                               ErrorIsInaccesible (loc, TypeManager.CSharpSignature (spec.MetaInfo), ec.Report);
                                return false;
                        }
 
@@ -5929,8 +5978,8 @@ namespace Mono.CSharp {
                public bool IsAccessibleFrom (Type invocation_type)
                {
                        bool dummy;
-                       return IsAccessorAccessible (invocation_type, add_accessor, out dummy) &&
-                               IsAccessorAccessible (invocation_type, remove_accessor, out dummy);
+                       return IsAccessorAccessible (invocation_type, spec.AccessorAdd, out dummy) &&
+                               IsAccessorAccessible (invocation_type, spec.AccessorRemove, out dummy);
                }
 
                public override Expression CreateExpressionTree (ResolveContext ec)
@@ -5945,13 +5994,15 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
+                       eclass = ExprClass.EventAccess;
+
                        bool must_do_cs1540_check;
-                       if (!(IsAccessorAccessible (ec.CurrentType, add_accessor, out must_do_cs1540_check) &&
-                             IsAccessorAccessible (ec.CurrentType, remove_accessor, out must_do_cs1540_check))) {
-                               ec.Report.SymbolRelatedToPreviousError (EventInfo);
-                               ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo), ec.Report);
+                       if (!(IsAccessorAccessible (ec.CurrentType, spec.AccessorAdd, out must_do_cs1540_check) &&
+                             IsAccessorAccessible (ec.CurrentType, spec.AccessorRemove, out must_do_cs1540_check))) {
+                               ec.Report.SymbolRelatedToPreviousError (spec.MetaInfo);
+                               ErrorIsInaccesible (loc, TypeManager.CSharpSignature (spec.MetaInfo), ec.Report);
                                return null;
                        }
 
@@ -5964,15 +6015,13 @@ namespace Mono.CSharp {
                        }
 
                        if (!ec.IsObsolete) {
-                               EventField ev = TypeManager.GetEventField (EventInfo);
-                               if (ev != null) {
-                                       ev.CheckObsoleteness (loc);
-                               } else {
-                                       ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (EventInfo);
-                                       if (oa != null)
-                                               AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, ec.Report);
-                               }
+                               var oa = spec.GetObsoleteAttribute ();
+                               if (oa != null)
+                                       AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, ec.Report);
                        }
+
+                       spec.MemberDefinition.SetIsUsed ();
+                       type = spec.EventType;
                        
                        return this;
                }               
@@ -5987,19 +6036,21 @@ namespace Mono.CSharp {
                {
                        ec.Report.Error (70, loc,
                                "The event `{0}' can only appear on the left hand side of += or -= when used outside of the type `{1}'",
-                               GetSignatureForError (), TypeManager.CSharpName (EventInfo.DeclaringType));
+                               GetSignatureForError (), TypeManager.CSharpName (spec.DeclaringType));
                }
 
                public override string GetSignatureForError ()
                {
-                       return TypeManager.CSharpSignature (EventInfo);
+                       return TypeManager.CSharpSignature (spec.MetaInfo);
                }
 
                public void EmitAddOrRemove (EmitContext ec, bool is_add, Expression source)
                {
                        Arguments args = new Arguments (1);
                        args.Add (new Argument (source));
-                       Invocation.EmitCall (ec, IsBase, InstanceExpression, is_add ? add_accessor : remove_accessor, args, loc);
+                       Invocation.EmitCall (ec, IsBase, InstanceExpression,
+                               is_add ? spec.AccessorAdd : spec.AccessorRemove,
+                               args, loc);
                }
        }
 
@@ -6011,7 +6062,6 @@ namespace Mono.CSharp {
                {
                        this.type = type;
                        this.loc = loc;
-                       eclass = ExprClass.Variable;
                }
 
                public override Expression CreateExpressionTree (ResolveContext ec)
@@ -6019,10 +6069,9 @@ namespace Mono.CSharp {
                        throw new NotSupportedException ("ET");
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (li != null)
-                               return this;
+                       eclass = ExprClass.Variable;
 
                        TypeExpr te = new TypeExpression (type, loc);
                        li = ec.CurrentBlock.AddTemporaryVariable (te, loc);
@@ -6043,7 +6092,7 @@ namespace Mono.CSharp {
 
                public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
-                       return DoResolve (ec);
+                       return Resolve (ec);
                }
                
                public override void Emit (EmitContext ec)
@@ -6058,7 +6107,7 @@ namespace Mono.CSharp {
 
                public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
                {
-                       return li.HoistedVariableReference;
+                       return li.HoistedVariant;
                }
 
                public override bool IsFixed {
@@ -6094,16 +6143,17 @@ namespace Mono.CSharp {
        class VarExpr : SimpleName
        {
                // Used for error reporting only
-               ArrayList initializer;
+               int initializers_count;
 
                public VarExpr (Location loc)
                        : base ("var", loc)
                {
+                       initializers_count = 1;
                }
 
-               public ArrayList VariableInitializer {
+               public int VariableInitializersCount {
                        set {
-                               this.initializer = value;
+                               this.initializers_count = value;
                        }
                }
 
@@ -6140,18 +6190,17 @@ namespace Mono.CSharp {
                        if (RootContext.Version < LanguageVersion.V_3)
                                rc.Compiler.Report.FeatureIsNotAvailable (loc, "implicitly typed local variable");
 
-                       if (initializer == null)
+                       if (initializers_count == 1)
                                return null;
 
-                       if (initializer.Count > 1) {
-                               Location loc_init = ((CSharpParser.VariableDeclaration) initializer[1]).Location;
-                               rc.Compiler.Report.Error (819, loc_init, "An implicitly typed local variable declaration cannot include multiple declarators");
-                               initializer = null;
+                       if (initializers_count > 1) {
+                               rc.Compiler.Report.Error (819, loc, "An implicitly typed local variable declaration cannot include multiple declarators");
+                               initializers_count = 1;
                                return null;
                        }
 
-                       Expression variable_initializer = ((CSharpParser.VariableDeclaration) initializer[0]).expression_or_array_initializer;
-                       if (variable_initializer == null) {
+                       if (initializers_count == 0) {
+                               initializers_count = 1;
                                rc.Compiler.Report.Error (818, loc, "An implicitly typed local variable declarator must include an initializer");
                                return null;
                        }