2006-03-09 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / mcs / ecore.cs
index 06a7b05dbd43a40d640032a691c67ac9abaa1419..5966502089a5587ead97aedc7d87ecb578a0336e 100644 (file)
@@ -59,7 +59,12 @@ namespace Mono.CSharp {
                DisableFlowAnalysis     = 8,
 
                // Set if this is resolving the first part of a MemberAccess.
-               Intermediate            = 16
+               Intermediate            = 16,
+
+               // Disable control flow analysis _of struct_ while resolving the expression.
+               // This is used when resolving the instance expression of a field expression.
+               DisableStructFlowAnalysis       = 32,
+
        }
 
        //
@@ -90,23 +95,6 @@ namespace Mono.CSharp {
                void AddressOf (EmitContext ec, AddressOp mode);
        }
 
-       /// <summary>
-       ///   We are either a namespace or a type.
-       ///   If we're a type, `IsType' is true and we may use `Type' to get
-       ///   a TypeExpr representing that type.
-       /// </summary>
-       public interface IAlias {
-               bool IsType {
-                       get;
-               }
-
-               string Name {
-                       get;
-               }
-
-               TypeExpr ResolveAsType (EmitContext ec);
-       }
-
        /// <summary>
        ///   This interface is implemented by variables
        /// </summary>
@@ -115,7 +103,7 @@ namespace Mono.CSharp {
                        get;
                }
 
-               bool VerifyFixed (bool is_expression);
+               bool VerifyFixed ();
        }
 
        /// <remarks>
@@ -127,19 +115,12 @@ namespace Mono.CSharp {
                protected Location loc;
                
                public Type Type {
-                       get {
-                               return type;
-                       }
-
-                       set {
-                               type = value;
-                       }
+                       get { return type; }
+                       set { type = value; }
                }
 
-               public Location Location {
-                       get {
-                               return loc;
-                       }
+               public virtual Location Location {
+                       get { return loc; }
                }
 
                /// <summary>
@@ -147,33 +128,25 @@ namespace Mono.CSharp {
                /// </summary>
                public void Error (int error, string s)
                {
-                       if (!Location.IsNull (loc))
-                               Report.Error (error, loc, s);
-                       else
+                       if (loc.IsNull)
                                Report.Error (error, s);
-               }
-
-               /// <summary>
-               ///   Utility wrapper routine for Warning, just to beautify the code
-               /// </summary>
-               public void Warning (int code, string format, params object[] args)
-               {
-                       Report.Warning (code, loc, format, args);
+                       else
+                               Report.Error (error, loc, s);
                }
 
                // Not nice but we have broken hierarchy
                public virtual void CheckMarshallByRefAccess (Type container) {}
 
-               /// <summary>
-               /// Tests presence of ObsoleteAttribute and report proper error
-               /// </summary>
-               protected void CheckObsoleteAttribute (Type type)
+               public virtual bool GetAttributableValue (out object value)
                {
-                       ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (type);
-                       if (obsolete_attr == null)
-                               return;
+                       Attribute.Error_AttributeArgumentNotValid (loc);
+                       value = null;
+                       return false;
+               }
 
-                       AttributeTester.Report_ObsoleteMessage (obsolete_attr, type.FullName, loc);
+               public virtual string GetSignatureForError ()
+               {
+                       return TypeManager.CSharpName (type);
                }
 
                public static bool IsAccessorAccessible (Type invocation_type, MethodInfo mi, out bool must_do_cs1540_check)
@@ -252,7 +225,7 @@ namespace Mono.CSharp {
                // This is used if the expression should be resolved as a type or namespace name.
                // the default implementation fails.   
                //
-               public virtual FullNamedExpression ResolveAsTypeStep (EmitContext ec)
+               public virtual FullNamedExpression ResolveAsTypeStep (IResolveContext ec,  bool silent)
                {
                        return null;
                }
@@ -262,57 +235,129 @@ namespace Mono.CSharp {
                // value will be returned if the expression is not a type
                // reference
                //
-               public TypeExpr ResolveAsTypeTerminal (EmitContext ec, bool silent)
+               public virtual TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
+               {
+                       TypeExpr te = ResolveAsBaseTerminal (ec, silent);
+                       if (te == null)
+                               return null;
+
+                       ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (te.Type);
+                       if (obsolete_attr != null && !ec.IsInObsoleteScope) {
+                               AttributeTester.Report_ObsoleteMessage (obsolete_attr, GetSignatureForError (), Location);
+                       }
+                       return te;
+               }
+
+               public TypeExpr ResolveAsBaseTerminal (IResolveContext ec, bool silent)
                {
                        int errors = Report.Errors;
 
-                       FullNamedExpression fne = ResolveAsTypeStep (ec);
+                       FullNamedExpression fne = ResolveAsTypeStep (ec, silent);
 
-                       if (fne == null) {
+                       if (fne == null){
                                if (!silent && errors == Report.Errors)
-                                       Report.Error (246, Location, "Cannot find type '{0}'", ToString ());
+                                       Report.Error (118, loc, "Expecting a type.");
                                return null;
                        }
 
                        if (fne.eclass != ExprClass.Type) {
                                if (!silent && errors == Report.Errors)
-                                       Report.Error (118, Location, "'{0}' denotes a '{1}', where a type was expected",
-                                                     fne.FullName, fne.ExprClassName ());
+                                       fne.Error_UnexpectedKind (null, "type", loc);
                                return null;
                        }
 
                        TypeExpr te = fne as TypeExpr;
 
-                       if (!te.CheckAccessLevel (ec.DeclSpace)) {
-                               Report.Error (122, Location, "'{0}' is inaccessible due to its protection level", te.Name);
+                       if (!te.CheckAccessLevel (ec.DeclContainer)) {
+                               ErrorIsInaccesible (loc, TypeManager.CSharpName (te.Type));
                                return null;
                        }
 
+                       te.loc = loc;
                        return te;
                }
 
-               ResolveFlags ExprClassToResolveFlags ()
+               public static void ErrorIsInaccesible (Location loc, string name)
+               {
+                       Report.Error (122, loc, "`{0}' is inaccessible due to its protection level", name);
+               }
+
+               protected static void Error_CannotAccessProtected (Location loc, MemberInfo m, Type qualifier, Type container)
                {
-                       switch (eclass) {
-                       case ExprClass.Type:
-                       case ExprClass.Namespace:
-                               return ResolveFlags.Type;
+                       Report.Error (1540, loc, "Cannot access protected member `{0}' via a qualifier of type `{1}';"
+                               + " the qualifier must be of type `{2}' (or derived from it)", 
+                               TypeManager.GetFullNameSignature (m),
+                               TypeManager.CSharpName (qualifier),
+                               TypeManager.CSharpName (container));
+
+               }
+
+               public virtual void Error_ValueCannotBeConverted (Location loc, Type target, bool expl)
+               {
+                       if (Type.Name == target.Name){
+                               Report.ExtraInformation (loc,
+                                       String.Format (
+                                       "The type {0} has two conflicting definitions, one comes from {1} and the other from {2}",
+                                       Type.Name, Type.Assembly.FullName, target.Assembly.FullName));
+                                                        
+                       }
 
-                       case ExprClass.MethodGroup:
-                               return ResolveFlags.MethodGroup;
+                       if (expl) {
+                               Report.Error (30, loc, "Cannot convert type `{0}' to `{1}'",
+                                       GetSignatureForError (), TypeManager.CSharpName (target));
+                               return;
+                       }
+                       
+                       Expression e = (this is EnumConstant) ? ((EnumConstant)this).Child : this;
+                       bool b = Convert.ExplicitNumericConversion (e, target) != null;
 
-                       case ExprClass.Value:
-                       case ExprClass.Variable:
-                       case ExprClass.PropertyAccess:
-                       case ExprClass.EventAccess:
-                       case ExprClass.IndexerAccess:
-                               return ResolveFlags.VariableOrValue;
+                       if (b || Convert.ExplicitReferenceConversionExists (Type, target) || Convert.ExplicitUnsafe (e, target) != null) {
+                               Report.Error (266, loc, "Cannot implicitly convert type `{0}' to `{1}'. An explicit conversion exists (are you missing a cast?)",
+                                       TypeManager.CSharpName (Type), TypeManager.CSharpName (target));
+                               return;
+                       }
 
-                       default:
-                               throw new Exception ("Expression " + GetType () +
-                                                    " ExprClass is Invalid after resolve");
+                       if (Type != TypeManager.string_type && this is Constant && !(this is NullCast)) {
+                               Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
+                                       GetSignatureForError (), TypeManager.CSharpName (target));
+                               return;
                        }
 
+                       Report.Error (29, loc, "Cannot implicitly convert type {0} to `{1}'",
+                               Type == TypeManager.anonymous_method_type ?
+                               "anonymous method" : "`" + GetSignatureForError () + "'",
+                               TypeManager.CSharpName (target));
+               }
+
+               protected static void Error_TypeDoesNotContainDefinition (Location loc, Type type, string name)
+               {
+                       Report.Error (117, loc, "`{0}' does not contain a definition for `{1}'",
+                               TypeManager.CSharpName (type), name);
+               }
+
+               ResolveFlags ExprClassToResolveFlags
+               {
+                       get {
+                               switch (eclass) {
+                                       case ExprClass.Type:
+                                       case ExprClass.Namespace:
+                                               return ResolveFlags.Type;
+
+                                       case ExprClass.MethodGroup:
+                                               return ResolveFlags.MethodGroup;
+
+                                       case ExprClass.Value:
+                                       case ExprClass.Variable:
+                                       case ExprClass.PropertyAccess:
+                                       case ExprClass.EventAccess:
+                                       case ExprClass.IndexerAccess:
+                                               return ResolveFlags.VariableOrValue;
+
+                                       default:
+                                               throw new Exception ("Expression " + GetType () +
+                                                       " ExprClass is Invalid after resolve");
+                               }
+                       }
                }
               
                /// <summary>
@@ -326,26 +371,30 @@ namespace Mono.CSharp {
                public Expression Resolve (EmitContext ec, ResolveFlags flags)
                {
                        if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) 
-                               return ResolveAsTypeStep (ec);
+                               return ResolveAsTypeStep (ec, false);
 
                        bool old_do_flow_analysis = ec.DoFlowAnalysis;
+                       bool old_omit_struct_analysis = ec.OmitStructFlowAnalysis;
                        if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
                                ec.DoFlowAnalysis = false;
+                       if ((flags & ResolveFlags.DisableStructFlowAnalysis) != 0)
+                               ec.OmitStructFlowAnalysis = true;
 
                        Expression e;
-                       bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate;
-                       if (this is SimpleName)
+                       if (this is SimpleName) {
+                               bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate;
                                e = ((SimpleName) this).DoResolve (ec, intermediate);
-
+                       }
                        else 
                                e = DoResolve (ec);
 
                        ec.DoFlowAnalysis = old_do_flow_analysis;
+                       ec.OmitStructFlowAnalysis = old_omit_struct_analysis;
 
                        if (e == null)
                                return null;
 
-                       if ((flags & e.ExprClassToResolveFlags ()) == 0) {
+                       if ((flags & e.ExprClassToResolveFlags) == 0) {
                                e.Error_UnexpectedKind (flags, loc);
                                return null;
                        }
@@ -374,6 +423,31 @@ namespace Mono.CSharp {
                        return e;
                }
 
+               public Constant ResolveAsConstant (EmitContext ec, MemberCore mc)
+               {
+                       Expression e = Resolve (ec);
+                       if (e == null)
+                               return null;
+
+                       Constant c = e as Constant;
+                       if (c != null)
+                               return c;
+
+                       EmptyCast empty = e as EmptyCast;
+                       if (empty != null) {
+                               c = empty.Child as Constant;
+                               if (c != null) {
+                                       // TODO: not sure about this maybe there is easier way how to use EmptyCast
+                                       if (e.Type.IsEnum)
+                                               c.Type = e.Type;
+
+                                       return c;
+                               }
+                       }
+                       Const.Error_ExpressionMustBeConstant (loc, mc.GetSignatureForError ());
+                       return null;
+               }
+
                /// <summary>
                ///   Resolves an expression for LValue assignment
                /// </summary>
@@ -454,33 +528,33 @@ namespace Mono.CSharp {
                public static Constant Constantify (object v, Type t)
                {
                        if (t == TypeManager.int32_type)
-                               return new IntConstant ((int) v);
+                               return new IntConstant ((int) v, Location.Null);
                        else if (t == TypeManager.uint32_type)
-                               return new UIntConstant ((uint) v);
+                               return new UIntConstant ((uint) v, Location.Null);
                        else if (t == TypeManager.int64_type)
-                               return new LongConstant ((long) v);
+                               return new LongConstant ((long) v, Location.Null);
                        else if (t == TypeManager.uint64_type)
-                               return new ULongConstant ((ulong) v);
+                               return new ULongConstant ((ulong) v, Location.Null);
                        else if (t == TypeManager.float_type)
-                               return new FloatConstant ((float) v);
+                               return new FloatConstant ((float) v, Location.Null);
                        else if (t == TypeManager.double_type)
-                               return new DoubleConstant ((double) v);
+                               return new DoubleConstant ((double) v, Location.Null);
                        else if (t == TypeManager.string_type)
-                               return new StringConstant ((string) v);
+                               return new StringConstant ((string) v, Location.Null);
                        else if (t == TypeManager.short_type)
-                               return new ShortConstant ((short)v);
+                               return new ShortConstant ((short)v, Location.Null);
                        else if (t == TypeManager.ushort_type)
-                               return new UShortConstant ((ushort)v);
+                               return new UShortConstant ((ushort)v, Location.Null);
                        else if (t == TypeManager.sbyte_type)
-                               return new SByteConstant (((sbyte)v));
+                               return new SByteConstant ((sbyte)v, Location.Null);
                        else if (t == TypeManager.byte_type)
-                               return new ByteConstant ((byte)v);
+                               return new ByteConstant ((byte)v, Location.Null);
                        else if (t == TypeManager.char_type)
-                               return new CharConstant ((char)v);
+                               return new CharConstant ((char)v, Location.Null);
                        else if (t == TypeManager.bool_type)
-                               return new BoolConstant ((bool) v);
+                               return new BoolConstant ((bool) v, Location.Null);
                        else if (t == TypeManager.decimal_type)
-                               return new DecimalConstant ((decimal) v);
+                               return new DecimalConstant ((decimal) v, Location.Null);
                        else if (TypeManager.IsEnumType (t)){
                                Type real_type = TypeManager.TypeToCoreType (v.GetType ());
                                if (real_type == t)
@@ -490,7 +564,7 @@ namespace Mono.CSharp {
 
                                return new EnumConstant (e, t);
                        } else if (v == null && !TypeManager.IsValueType (t))
-                               return NullLiteral.Null;
+                               return new NullLiteral (Location.Null);
                        else
                                throw new Exception ("Unknown type for constant (" + t +
                                                     "), details: " + v);
@@ -499,14 +573,15 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Returns a fully formed expression after a MemberLookup
                /// </summary>
-               public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
+               /// 
+               public static Expression ExprClassFromMemberInfo (Type containerType, MemberInfo mi, Location loc)
                {
                        if (mi is EventInfo)
                                return new EventExpr ((EventInfo) mi, loc);
                        else if (mi is FieldInfo)
                                return new FieldExpr ((FieldInfo) mi, loc);
                        else if (mi is PropertyInfo)
-                               return new PropertyExpr (ec, (PropertyInfo) mi, loc);
+                               return new PropertyExpr (containerType, (PropertyInfo) mi, loc);
                        else if (mi is Type){
                                return new TypeExpression ((System.Type) mi, loc);
                        }
@@ -544,10 +619,10 @@ namespace Mono.CSharp {
                // FIXME: Potential optimization, have a static ArrayList
                //
 
-               public static Expression MemberLookup (EmitContext ec, Type queried_type, string name,
+               public static Expression MemberLookup (Type container_type, Type queried_type, string name,
                                                       MemberTypes mt, BindingFlags bf, Location loc)
                {
-                       return MemberLookup (ec, ec.ContainerType, null, queried_type, name, mt, bf, loc);
+                       return MemberLookup (container_type, null, queried_type, name, mt, bf, loc);
                }
 
                //
@@ -555,7 +630,7 @@ namespace Mono.CSharp {
                // `qualifier_type' or null to lookup members in the current class.
                //
 
-               public static Expression MemberLookup (EmitContext ec, Type container_type,
+               public static Expression MemberLookup (Type container_type,
                                                       Type qualifier_type, Type queried_type,
                                                       string name, MemberTypes mt,
                                                       BindingFlags bf, Location loc)
@@ -576,7 +651,7 @@ namespace Mono.CSharp {
                        if (count > 1)
                                return null;
 
-                       return ExprClassFromMemberInfo (ec, mi [0], loc);
+                       return ExprClassFromMemberInfo (container_type, mi [0], loc);
                }
 
                public const MemberTypes AllMemberTypes =
@@ -592,24 +667,24 @@ namespace Mono.CSharp {
                        BindingFlags.Static |
                        BindingFlags.Instance;
 
-               public static Expression MemberLookup (EmitContext ec, Type queried_type,
+               public static Expression MemberLookup (Type container_type, Type queried_type,
                                                       string name, Location loc)
                {
-                       return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
+                       return MemberLookup (container_type, null, queried_type, name,
                                             AllMemberTypes, AllBindingFlags, loc);
                }
 
-               public static Expression MemberLookup (EmitContext ec, Type qualifier_type,
+               public static Expression MemberLookup (Type container_type, Type qualifier_type,
                                                       Type queried_type, string name, Location loc)
                {
-                       return MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
+                       return MemberLookup (container_type, qualifier_type, queried_type,
                                             name, AllMemberTypes, AllBindingFlags, loc);
                }
 
                public static Expression MethodLookup (EmitContext ec, Type queried_type,
                                                       string name, Location loc)
                {
-                       return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
+                       return MemberLookup (ec.ContainerType, null, queried_type, name,
                                             MemberTypes.Method, AllBindingFlags, loc);
                }
 
@@ -635,16 +710,16 @@ namespace Mono.CSharp {
 
                        int errors = Report.Errors;
 
-                       e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type, name, mt, bf, loc);
+                       e = MemberLookup (ec.ContainerType, qualifier_type, queried_type, name, mt, bf, loc);
 
                        if (e == null && errors == Report.Errors)
                                // No errors were reported by MemberLookup, but there was an error.
-                               MemberLookupFailed (ec, qualifier_type, queried_type, name, null, true, loc);
+                               MemberLookupFailed (ec.ContainerType, qualifier_type, queried_type, name, null, true, loc);
 
                        return e;
                }
 
-               public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
+               public static void MemberLookupFailed (Type container_type, Type qualifier_type,
                                                       Type queried_type, string name,
                                                       string class_name, bool complain_if_none_found, 
                                                       Location loc)
@@ -665,35 +740,27 @@ namespace Mono.CSharp {
                                        
                                        Report.SymbolRelatedToPreviousError (m);
                                        if (qualifier_type == null) {
-                                               Report.Error (38, loc, 
-                                                             "Cannot access non-static member `{0}' via nested type `{1}'", 
-                                                             TypeManager.GetFullNameSignature (m),
-                                                             TypeManager.CSharpName (ec.ContainerType));
+                                               Report.Error (38, loc, "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
+                                                             TypeManager.CSharpName (m.DeclaringType),
+                                                             TypeManager.CSharpName (container_type));
                                                
-                                       } else if (qualifier_type != ec.ContainerType &&
-                                                  TypeManager.IsNestedFamilyAccessible (ec.ContainerType, declaring_type)) {
+                                       } else if (qualifier_type != container_type &&
+                                                  TypeManager.IsNestedFamilyAccessible (container_type, declaring_type)) {
                                                // 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.ContainerType and the lookup succeeds with the latter one,
                                                // then we are in this situation.
-                                               Report.Error (1540, loc, 
-                                                             "Cannot access protected member `{0}' via a qualifier of type `{1}';"
-                                                             + " the qualifier must be of type `{2}' (or derived from it)", 
-                                                             TypeManager.GetFullNameSignature (m),
-                                                             TypeManager.CSharpName (qualifier_type),
-                                                             TypeManager.CSharpName (ec.ContainerType));
+                                               Error_CannotAccessProtected (loc, m, qualifier_type, container_type);
                                        } else {
-                                               Report.Error (122, loc, 
-                                                             "'{0}' is inaccessible due to its protection level", 
-                                                             TypeManager.GetFullNameSignature (m));
+                                               ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (m));
                                        }
                                }
                                almostMatchedMembers.Clear ();
                                return;
                        }
 
-                       object lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
+                       MemberInfo[] lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
                                                                  AllMemberTypes, AllBindingFlags |
                                                                  BindingFlags.NonPublic, name, null);
 
@@ -702,27 +769,22 @@ namespace Mono.CSharp {
                                        return;
 
                                if (class_name != null)
-                                       Report.Error (103, loc, "The name `" + name + "' could not be " +
-                                                     "found in `" + class_name + "'");
+                                       Report.Error (103, loc, "The name `{0}' does not exist in the context of `{1}'",
+                                               name, class_name);
                                else
-                                       Report.Error (
-                                               117, loc, "`" + queried_type + "' does not contain a " +
-                                               "definition for `" + name + "'");
+                                       Error_TypeDoesNotContainDefinition (loc, queried_type, name);
                                return;
                        }
 
-                       if (name == ".ctor" && TypeManager.FindMembers (qualifier_type, MemberTypes.Constructor,
-                               BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, null).Count == 0)
+                       MemberList ml = TypeManager.FindMembers (queried_type, MemberTypes.Constructor,
+                               BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, null);
+                       if (name == ".ctor" && ml.Count == 0)
                        {
-                               Report.Error (143, loc, String.Format ("The type '{0}' has no constructors defined", TypeManager.CSharpName (queried_type)));
+                               Report.Error (143, loc, "The type `{0}' has no constructors defined", TypeManager.CSharpName (queried_type));
                                return;
                        }
 
-                       if (qualifier_type != null) {
-                               Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", TypeManager.CSharpName (qualifier_type) + "." + name);
-                       } else {
-                               Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", name);
-                       }
+                       ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (lookup [0]));
                }
 
                /// <summary>
@@ -784,48 +846,59 @@ namespace Mono.CSharp {
                        //
                        // If no implicit conversion to bool exists, try using `operator true'
                        //
-                       Expression operator_true = Expression.GetOperatorTrue (ec, e, loc);
-                       if (operator_true == null){
-                               Report.Error (31, loc, "Can not convert the expression to a boolean");
+                       converted = Expression.GetOperatorTrue (ec, e, loc);
+                       if (converted == null){
+                               e.Error_ValueCannotBeConverted (loc, TypeManager.bool_type, false);
                                return null;
                        }
-                       return operator_true;
+                       return converted;
                }
                
-               public string ExprClassName ()
-               {
-                       switch (eclass){
-                       case ExprClass.Invalid:
-                               return "Invalid";
-                       case ExprClass.Value:
-                               return "value";
-                       case ExprClass.Variable:
-                               return "variable";
-                       case ExprClass.Namespace:
-                               return "namespace";
-                       case ExprClass.Type:
-                               return "type";
-                       case ExprClass.MethodGroup:
-                               return "method group";
-                       case ExprClass.PropertyAccess:
-                               return "property access";
-                       case ExprClass.EventAccess:
-                               return "event access";
-                       case ExprClass.IndexerAccess:
-                               return "indexer access";
-                       case ExprClass.Nothing:
-                               return "null";
-                       }
-                       throw new Exception ("Should not happen");
+               public virtual string ExprClassName
+               {
+                       get {
+                               switch (eclass){
+                                       case ExprClass.Invalid:
+                                               return "Invalid";
+                                       case ExprClass.Value:
+                                               return "value";
+                                       case ExprClass.Variable:
+                                               return "variable";
+                                       case ExprClass.Namespace:
+                                               return "namespace";
+                                       case ExprClass.Type:
+                                               return "type";
+                                       case ExprClass.MethodGroup:
+                                               return "method group";
+                                       case ExprClass.PropertyAccess:
+                                               return "property access";
+                                       case ExprClass.EventAccess:
+                                               return "event access";
+                                       case ExprClass.IndexerAccess:
+                                               return "indexer access";
+                                       case ExprClass.Nothing:
+                                               return "null";
+                               }
+                               throw new Exception ("Should not happen");
+                       }
                }
                
                /// <summary>
                ///   Reports that we were expecting `expr' to be of class `expected'
                /// </summary>
-               public void Error_UnexpectedKind (string expected, Location loc)
+               public void Error_UnexpectedKind (DeclSpace ds, string expected, Location loc)
                {
-                       Report.Error (118, loc,
-                               "Expression denotes a '{0}', where a '{1}' was expected", ExprClassName (), expected);
+                       Error_UnexpectedKind (ds, expected, ExprClassName, loc);
+               }
+
+               public void Error_UnexpectedKind (DeclSpace ds, string expected, string was, Location loc)
+               {
+                       string name = GetSignatureForError ();
+                       if (ds != null)
+                               name = ds.GetSignatureForError () + '.' + name;
+
+                       Report.Error (118, loc, "`{0}' is a `{1}' but a `{2}' was expected",
+                             name, was, expected);
                }
 
                public void Error_UnexpectedKind (ResolveFlags flags, Location loc)
@@ -849,301 +922,21 @@ namespace Mono.CSharp {
 
                        StringBuilder sb = new StringBuilder (valid [0]);
                        for (int i = 1; i < count - 1; i++) {
-                               sb.Append ("', '");
+                               sb.Append ("', `");
                                sb.Append (valid [i]);
                        }
                        if (count > 1) {
-                               sb.Append ("' or '");
+                               sb.Append ("' or `");
                                sb.Append (valid [count - 1]);
                        }
 
                        Report.Error (119, loc, 
-                               "Expression denotes a '{0}', where a '{1}' was expected", ExprClassName (), sb);
+                               "Expression denotes a `{0}', where a `{1}' was expected", ExprClassName, sb.ToString ());
                }
                
-               static public void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
-               {
-                       Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
-                                     TypeManager.CSharpName (t));
-               }
-
                public static void UnsafeError (Location loc)
                {
-                       Report.Error (214, loc, "Pointers may only be used in an unsafe context");
-               }
-               
-               /// <summary>
-               ///   Converts the IntConstant, UIntConstant, LongConstant or
-               ///   ULongConstant into the integral target_type.   Notice
-               ///   that we do not return an `Expression' we do return
-               ///   a boxed integral type.
-               ///
-               ///   FIXME: Since I added the new constants, we need to
-               ///   also support conversions from CharConstant, ByteConstant,
-               ///   SByteConstant, UShortConstant, ShortConstant
-               ///
-               ///   This is used by the switch statement, so the domain
-               ///   of work is restricted to the literals above, and the
-               ///   targets are int32, uint32, char, byte, sbyte, ushort,
-               ///   short, uint64 and int64
-               /// </summary>
-               public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
-               {
-                       if (!Convert.ImplicitStandardConversionExists (Convert.ConstantEC, c, target_type)){
-                               Convert.Error_CannotImplicitConversion (loc, c.Type, target_type);
-                               return null;
-                       }
-                       
-                       string s = "";
-
-                       if (c.Type == target_type)
-                               return ((Constant) c).GetValue ();
-
-                       //
-                       // Make into one of the literals we handle, we dont really care
-                       // about this value as we will just return a few limited types
-                       // 
-                       if (c is EnumConstant)
-                               c = ((EnumConstant)c).WidenToCompilerConstant ();
-
-                       if (c is IntConstant){
-                               int v = ((IntConstant) c).Value;
-                               
-                               if (target_type == TypeManager.uint32_type){
-                                       if (v >= 0)
-                                               return (uint) v;
-                               } else if (target_type == TypeManager.char_type){
-                                       if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return (char) v;
-                               } else if (target_type == TypeManager.byte_type){
-                                       if (v >= Byte.MinValue && v <= Byte.MaxValue)
-                                               return (byte) v;
-                               } else if (target_type == TypeManager.sbyte_type){
-                                       if (v >= SByte.MinValue && v <= SByte.MaxValue)
-                                               return (sbyte) v;
-                               } else if (target_type == TypeManager.short_type){
-                                       if (v >= Int16.MinValue && v <= UInt16.MaxValue)
-                                               return (short) v;
-                               } else if (target_type == TypeManager.ushort_type){
-                                       if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
-                                               return (ushort) v;
-                               } else if (target_type == TypeManager.int64_type)
-                                       return (long) v;
-                               else if (target_type == TypeManager.uint64_type){
-                                       if (v > 0)
-                                               return (ulong) v;
-                               }
-
-                               s = v.ToString ();
-                       } else if (c is UIntConstant){
-                               uint v = ((UIntConstant) c).Value;
-
-                               if (target_type == TypeManager.int32_type){
-                                       if (v <= Int32.MaxValue)
-                                               return (int) v;
-                               } else if (target_type == TypeManager.char_type){
-                                       if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return (char) v;
-                               } else if (target_type == TypeManager.byte_type){
-                                       if (v <= Byte.MaxValue)
-                                               return (byte) v;
-                               } else if (target_type == TypeManager.sbyte_type){
-                                       if (v <= SByte.MaxValue)
-                                               return (sbyte) v;
-                               } else if (target_type == TypeManager.short_type){
-                                       if (v <= UInt16.MaxValue)
-                                               return (short) v;
-                               } else if (target_type == TypeManager.ushort_type){
-                                       if (v <= UInt16.MaxValue)
-                                               return (ushort) v;
-                               } else if (target_type == TypeManager.int64_type)
-                                       return (long) v;
-                               else if (target_type == TypeManager.uint64_type)
-                                       return (ulong) v;
-                               s = v.ToString ();
-                       } else if (c is LongConstant){ 
-                               long v = ((LongConstant) c).Value;
-
-                               if (target_type == TypeManager.int32_type){
-                                       if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
-                                               return (int) v;
-                               } else if (target_type == TypeManager.uint32_type){
-                                       if (v >= 0 && v <= UInt32.MaxValue)
-                                               return (uint) v;
-                               } else if (target_type == TypeManager.char_type){
-                                       if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return (char) v;
-                               } else if (target_type == TypeManager.byte_type){
-                                       if (v >= Byte.MinValue && v <= Byte.MaxValue)
-                                               return (byte) v;
-                               } else if (target_type == TypeManager.sbyte_type){
-                                       if (v >= SByte.MinValue && v <= SByte.MaxValue)
-                                               return (sbyte) v;
-                               } else if (target_type == TypeManager.short_type){
-                                       if (v >= Int16.MinValue && v <= UInt16.MaxValue)
-                                               return (short) v;
-                               } else if (target_type == TypeManager.ushort_type){
-                                       if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
-                                               return (ushort) v;
-                               } else if (target_type == TypeManager.uint64_type){
-                                       if (v > 0)
-                                               return (ulong) v;
-                               }
-                               s = v.ToString ();
-                       } else if (c is ULongConstant){
-                               ulong v = ((ULongConstant) c).Value;
-
-                               if (target_type == TypeManager.int32_type){
-                                       if (v <= Int32.MaxValue)
-                                               return (int) v;
-                               } else if (target_type == TypeManager.uint32_type){
-                                       if (v <= UInt32.MaxValue)
-                                               return (uint) v;
-                               } else if (target_type == TypeManager.char_type){
-                                       if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return (char) v;
-                               } else if (target_type == TypeManager.byte_type){
-                                       if (v >= Byte.MinValue && v <= Byte.MaxValue)
-                                               return (byte) v;
-                               } else if (target_type == TypeManager.sbyte_type){
-                                       if (v <= (int) SByte.MaxValue)
-                                               return (sbyte) v;
-                               } else if (target_type == TypeManager.short_type){
-                                       if (v <= UInt16.MaxValue)
-                                               return (short) v;
-                               } else if (target_type == TypeManager.ushort_type){
-                                       if (v <= UInt16.MaxValue)
-                                               return (ushort) v;
-                               } else if (target_type == TypeManager.int64_type){
-                                       if (v <= Int64.MaxValue)
-                                               return (long) v;
-                               }
-                               s = v.ToString ();
-                       } else if (c is ByteConstant){
-                               byte v = ((ByteConstant) c).Value;
-                               
-                               if (target_type == TypeManager.int32_type)
-                                       return (int) v;
-                               else if (target_type == TypeManager.uint32_type)
-                                       return (uint) v;
-                               else if (target_type == TypeManager.char_type)
-                                       return (char) v;
-                               else if (target_type == TypeManager.sbyte_type){
-                                       if (v <= SByte.MaxValue)
-                                               return (sbyte) v;
-                               } else if (target_type == TypeManager.short_type)
-                                       return (short) v;
-                               else if (target_type == TypeManager.ushort_type)
-                                       return (ushort) v;
-                               else if (target_type == TypeManager.int64_type)
-                                       return (long) v;
-                               else if (target_type == TypeManager.uint64_type)
-                                       return (ulong) v;
-                               s = v.ToString ();
-                       } else if (c is SByteConstant){
-                               sbyte v = ((SByteConstant) c).Value;
-                               
-                               if (target_type == TypeManager.int32_type)
-                                       return (int) v;
-                               else if (target_type == TypeManager.uint32_type){
-                                       if (v >= 0)
-                                               return (uint) v;
-                               } else if (target_type == TypeManager.char_type){
-                                       if (v >= 0)
-                                               return (char) v;
-                               } else if (target_type == TypeManager.byte_type){
-                                       if (v >= 0)
-                                               return (byte) v;
-                               } else if (target_type == TypeManager.short_type)
-                                       return (short) v;
-                               else if (target_type == TypeManager.ushort_type){
-                                       if (v >= 0)
-                                               return (ushort) v;
-                               } else if (target_type == TypeManager.int64_type)
-                                       return (long) v;
-                               else if (target_type == TypeManager.uint64_type){
-                                       if (v >= 0)
-                                               return (ulong) v;
-                               }
-                               s = v.ToString ();
-                       } else if (c is ShortConstant){
-                               short v = ((ShortConstant) c).Value;
-                               
-                               if (target_type == TypeManager.int32_type){
-                                       return (int) v;
-                               } else if (target_type == TypeManager.uint32_type){
-                                       if (v >= 0)
-                                               return (uint) v;
-                               } else if (target_type == TypeManager.char_type){
-                                       if (v >= 0)
-                                               return (char) v;
-                               } else if (target_type == TypeManager.byte_type){
-                                       if (v >= Byte.MinValue && v <= Byte.MaxValue)
-                                               return (byte) v;
-                               } else if (target_type == TypeManager.sbyte_type){
-                                       if (v >= SByte.MinValue && v <= SByte.MaxValue)
-                                               return (sbyte) v;
-                               } else if (target_type == TypeManager.ushort_type){
-                                       if (v >= 0)
-                                               return (ushort) v;
-                               } else if (target_type == TypeManager.int64_type)
-                                       return (long) v;
-                               else if (target_type == TypeManager.uint64_type)
-                                       return (ulong) v;
-
-                               s = v.ToString ();
-                       } else if (c is UShortConstant){
-                               ushort v = ((UShortConstant) c).Value;
-                               
-                               if (target_type == TypeManager.int32_type)
-                                       return (int) v;
-                               else if (target_type == TypeManager.uint32_type)
-                                       return (uint) v;
-                               else if (target_type == TypeManager.char_type){
-                                       if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return (char) v;
-                               } else if (target_type == TypeManager.byte_type){
-                                       if (v >= Byte.MinValue && v <= Byte.MaxValue)
-                                               return (byte) v;
-                               } else if (target_type == TypeManager.sbyte_type){
-                                       if (v <= SByte.MaxValue)
-                                               return (byte) v;
-                               } else if (target_type == TypeManager.short_type){
-                                       if (v <= Int16.MaxValue)
-                                               return (short) v;
-                               } else if (target_type == TypeManager.int64_type)
-                                       return (long) v;
-                               else if (target_type == TypeManager.uint64_type)
-                                       return (ulong) v;
-
-                               s = v.ToString ();
-                       } else if (c is CharConstant){
-                               char v = ((CharConstant) c).Value;
-                               
-                               if (target_type == TypeManager.int32_type)
-                                       return (int) v;
-                               else if (target_type == TypeManager.uint32_type)
-                                       return (uint) v;
-                               else if (target_type == TypeManager.byte_type){
-                                       if (v >= Byte.MinValue && v <= Byte.MaxValue)
-                                               return (byte) v;
-                               } else if (target_type == TypeManager.sbyte_type){
-                                       if (v <= SByte.MaxValue)
-                                               return (sbyte) v;
-                               } else if (target_type == TypeManager.short_type){
-                                       if (v <= Int16.MaxValue)
-                                               return (short) v;
-                               } else if (target_type == TypeManager.ushort_type)
-                                       return (short) v;
-                               else if (target_type == TypeManager.int64_type)
-                                       return (long) v;
-                               else if (target_type == TypeManager.uint64_type)
-                                       return (ulong) v;
-
-                               s = v.ToString ();
-                       }
-                       Error_ConstantValueCannotBeConverted (loc, s, target_type);
-                       return null;
+                       Report.Error (214, loc, "Pointers and fixed size buffers may only be used in an unsafe context");
                }
 
                //
@@ -1251,6 +1044,11 @@ namespace Mono.CSharp {
                {
                        Report.Error (248, loc, "Cannot create an array with a negative size");
                }
+
+               protected void Error_CannotCallAbstractBase (string name)
+               {
+                       Report.Error (205, loc, "Cannot call an abstract base member `{0}'", name);
+               }
                
                //
                // Converts `source' to an int, uint, long or ulong.
@@ -1270,7 +1068,7 @@ namespace Mono.CSharp {
                                        if (target == null){
                                                target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
                                                if (target == null)
-                                                       Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
+                                                       source.Error_ValueCannotBeConverted (loc, TypeManager.int32_type, false);
                                        }
                                }
                        } 
@@ -1346,7 +1144,7 @@ namespace Mono.CSharp {
        ///
        /// </summary>
        public class EmptyCast : Expression {
-               protected Expression child;
+               protected readonly Expression child;
 
                public Expression Child {
                        get {
@@ -1374,6 +1172,12 @@ namespace Mono.CSharp {
                {
                        child.Emit (ec);
                }
+
+               public override bool GetAttributableValue (out object value)
+               {
+                       return child.GetAttributableValue (out value);
+               }
+
        }
        /// <summary>
        ///     This is a numeric cast to a Decimal
@@ -1382,36 +1186,30 @@ namespace Mono.CSharp {
 
                MethodInfo conversion_operator;
 
-               public CastToDecimal (EmitContext ec, Expression child)
-                       : this (ec, child, false)
+               public CastToDecimal (Expression child)
+                       : this (child, false)
                {
                }
 
-               public CastToDecimal (EmitContext ec, Expression child, bool find_explicit)
+               public CastToDecimal (Expression child, bool find_explicit)
                        : base (child, TypeManager.decimal_type)
                {
-                       conversion_operator = GetConversionOperator (ec, find_explicit);
+                       conversion_operator = GetConversionOperator (find_explicit);
 
                        if (conversion_operator == null)
-                               Convert.Error_CannotImplicitConversion (loc, child.Type, type);
+                               throw new InternalErrorException ("Outer conversion routine is out of sync");
                }
 
                // Returns the implicit operator that converts from
                // 'child.Type' to System.Decimal.
-               MethodInfo GetConversionOperator (EmitContext ec, bool find_explicit)
+               MethodInfo GetConversionOperator (bool find_explicit)
                {
-                       string operator_name = "op_Implicit";
-
-                       if (find_explicit)
-                               operator_name = "op_Explicit";
+                       string operator_name = find_explicit ? "op_Explicit" : "op_Implicit";
                        
-                       MethodGroupExpr opers = Expression.MethodLookup (
-                               ec, type, operator_name, loc) as MethodGroupExpr;
+                       MemberInfo [] mi = TypeManager.MemberLookup (type, type, type, MemberTypes.Method,
+                               BindingFlags.Static | BindingFlags.Public, operator_name, null);
 
-                       if (opers == null)
-                               Convert.Error_CannotImplicitConversion (loc, child.Type, type);
-                       
-                       foreach (MethodInfo oper in opers.Methods) {
+                       foreach (MethodInfo oper in mi) {
                                ParameterData pd = TypeManager.GetParameterData (oper);
 
                                if (pd.ParameterType (0) == child.Type && oper.ReturnType == type)
@@ -1428,49 +1226,48 @@ namespace Mono.CSharp {
                        ig.Emit (OpCodes.Call, conversion_operator);
                }
        }
+
        /// <summary>
        ///     This is an explicit numeric cast from a Decimal
        /// </summary>
        public class CastFromDecimal : EmptyCast
        {
-               MethodInfo conversion_operator;
-               public CastFromDecimal (EmitContext ec, Expression child, Type return_type)
+               static IDictionary operators;
+
+               public CastFromDecimal (Expression child, Type return_type)
                        : base (child, return_type)
                {
                        if (child.Type != TypeManager.decimal_type)
                                throw new InternalErrorException (
                                        "The expected type is Decimal, instead it is " + child.Type.FullName);
-
-                       conversion_operator = GetConversionOperator (ec);
-                       if (conversion_operator == null)
-                               Convert.Error_CannotImplicitConversion (loc, child.Type, type);
                }
 
                // Returns the explicit operator that converts from an
                // express of type System.Decimal to 'type'.
-               MethodInfo GetConversionOperator (EmitContext ec)
-               {                               
-                       MethodGroupExpr opers = Expression.MethodLookup (
-                               ec, child.Type, "op_Explicit", loc) as MethodGroupExpr;
-
-                       if (opers == null)
-                               Convert.Error_CannotImplicitConversion (loc, child.Type, type);
-                       
-                       foreach (MethodInfo oper in opers.Methods) {
-                               ParameterData pd = TypeManager.GetParameterData (oper);
-
-                               if (pd.ParameterType (0) == child.Type && oper.ReturnType == type)
-                                       return oper;
+               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);
+
+                               operators = new System.Collections.Specialized.HybridDictionary ();
+                               foreach (MethodInfo oper in all_oper) {
+                                       ParameterData pd = TypeManager.GetParameterData (oper);
+                                       if (pd.ParameterType (0) == TypeManager.decimal_type)
+                                               operators.Add (oper.ReturnType, oper);
+                               }
                        }
 
-                       return null;
+                       return operators.Contains (type) ? this : null;
                }
+
                public override void Emit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
                        child.Emit (ec);
 
-                       ig.Emit (OpCodes.Call, conversion_operator);
+                       ig.Emit (OpCodes.Call, (MethodInfo)operators [type]);
                }
        }
 
@@ -1479,9 +1276,10 @@ namespace Mono.CSharp {
        // a NullLiteral is still a Constant
        //
        public class NullCast : Constant {
-               protected Expression child;
+               public Constant child;
                                
-               public NullCast (Expression child, Type return_type)
+               public NullCast (Constant child, Type return_type):
+                       base (Location.Null)
                {
                        eclass = child.eclass;
                        type = return_type;
@@ -1511,9 +1309,14 @@ namespace Mono.CSharp {
                        child.Emit (ec);
                }
 
+               public override Constant Increment ()
+               {
+                       throw new NotSupportedException ();
+               }
+
                public override bool IsDefaultValue {
                        get {
-                               throw new NotImplementedException ();
+                               return true;
                        }
                }
 
@@ -1522,6 +1325,15 @@ namespace Mono.CSharp {
                                return false;
                        }
                }
+
+               public override Constant Reduce (EmitContext ec, Type target_type)
+               {
+                       if (type == target_type)
+                               return child.Reduce (ec, target_type);
+
+                       return null;
+               }
+
        }
 
 
@@ -1531,7 +1343,8 @@ namespace Mono.CSharp {
        public class EnumConstant : Constant {
                public Constant Child;
 
-               public EnumConstant (Constant child, Type enum_type)
+               public EnumConstant (Constant child, Type enum_type):
+                       base (child.Location)
                {
                        eclass = child.eclass;
                        this.Child = child;
@@ -1551,72 +1364,24 @@ namespace Mono.CSharp {
                        Child.Emit (ec);
                }
 
-               public override object GetValue ()
-               {
-                       return Child.GetValue ();
-               }
-
-               public object GetValueAsEnumType ()
+               public override string GetSignatureForError()
                {
-                       return System.Enum.ToObject (type, Child.GetValue ());
+                       return TypeManager.CSharpName (Type);
                }
 
-               //
-               // Converts from one of the valid underlying types for an enumeration
-               // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
-               // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
-               //
-               public Constant WidenToCompilerConstant ()
+               public override object GetValue ()
                {
-                       Type t = TypeManager.EnumToUnderlying (Child.Type);
-                       object v = ((Constant) Child).GetValue ();;
-                       
-                       if (t == TypeManager.int32_type)
-                               return new IntConstant ((int) v);
-                       if (t == TypeManager.uint32_type)
-                               return new UIntConstant ((uint) v);
-                       if (t == TypeManager.int64_type)
-                               return new LongConstant ((long) v);
-                       if (t == TypeManager.uint64_type)
-                               return new ULongConstant ((ulong) v);
-                       if (t == TypeManager.short_type)
-                               return new ShortConstant ((short) v);
-                       if (t == TypeManager.ushort_type)
-                               return new UShortConstant ((ushort) v);
-                       if (t == TypeManager.byte_type)
-                               return new ByteConstant ((byte) v);
-                       if (t == TypeManager.sbyte_type)
-                               return new SByteConstant ((sbyte) v);
-
-                       throw new Exception ("Invalid enumeration underlying type: " + t);
+                       return Child.GetValue ();
                }
 
-               //
-               // Extracts the value in the enumeration on its native representation
-               //
-               public object GetPlainValue ()
+               public override object GetTypedValue ()
                {
-                       Type t = TypeManager.EnumToUnderlying (Child.Type);
-                       object v = ((Constant) Child).GetValue ();;
-                       
-                       if (t == TypeManager.int32_type)
-                               return (int) v;
-                       if (t == TypeManager.uint32_type)
-                               return (uint) v;
-                       if (t == TypeManager.int64_type)
-                               return (long) v;
-                       if (t == TypeManager.uint64_type)
-                               return (ulong) v;
-                       if (t == TypeManager.short_type)
-                               return (short) v;
-                       if (t == TypeManager.ushort_type)
-                               return (ushort) v;
-                       if (t == TypeManager.byte_type)
-                               return (byte) v;
-                       if (t == TypeManager.sbyte_type)
-                               return (sbyte) v;
+                       // FIXME: runtime is not ready to work with just emited enums
+                       if (!RootContext.StdLib) {
+                               return Child.GetValue ();
+                       }
 
-                       return null;
+                       return System.Enum.ToObject (type, Child.GetValue ());
                }
                
                public override string AsString ()
@@ -1654,6 +1419,11 @@ namespace Mono.CSharp {
                        return Child.ConvertToInt ();
                }
 
+               public override Constant Increment()
+               {
+                       return new EnumConstant (Child.Increment (), type);
+               }
+
                public override bool IsDefaultValue {
                        get {
                                return Child.IsDefaultValue;
@@ -1669,6 +1439,35 @@ namespace Mono.CSharp {
                                return Child.IsNegative;
                        }
                }
+
+               public override Constant Reduce(EmitContext ec, Type target_type)
+               {
+                       if (Child.Type == target_type)
+                               return Child;
+
+                       return Child.Reduce (ec, target_type);
+               }
+
+               public override Constant ToType (Type type, Location loc)
+               {
+                       if (Type == type) {
+                               // This is workaround of mono bug. It can be removed when the latest corlib spreads enough
+                               if (TypeManager.IsEnumType (type.UnderlyingSystemType))
+                                       return this;
+
+                               if (type.UnderlyingSystemType != Child.Type)
+                                       Child = Child.ToType (type.UnderlyingSystemType, loc);
+                               return this;
+                       }
+
+                       if (!Convert.ImplicitStandardConversionExists (this, type)){
+                               Error_ValueCannotBeConverted (loc, type, false);
+                               return null;
+                       }
+
+                       return Child.ToType (type, loc);
+               }
+
        }
 
        /// <summary>
@@ -1679,12 +1478,6 @@ namespace Mono.CSharp {
        /// </summary>
        public class BoxedCast : EmptyCast {
 
-               public BoxedCast (Expression expr)
-                       : base (expr, TypeManager.object_type) 
-               {
-                       eclass = ExprClass.Value;
-               }
-
                public BoxedCast (Expression expr, Type target_type)
                        : base (expr, target_type)
                {
@@ -1721,6 +1514,13 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               {
+                       if (right_side == EmptyExpression.LValueMemberAccess)
+                               Report.Error (445, loc, "Cannot modify the result of an unboxing conversion");
+                       return base.DoResolveLValue (ec, right_side);
+               }
+
                public override void Emit (EmitContext ec)
                {
                        Type t = type;
@@ -1756,12 +1556,10 @@ namespace Mono.CSharp {
                }
 
                Mode mode;
-               bool checked_state;
                
-               public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
+               public ConvCast (Expression child, Type return_type, Mode m)
                        : base (child, return_type)
                {
-                       checked_state = ec.CheckState;
                        mode = m;
                }
 
@@ -1784,7 +1582,7 @@ namespace Mono.CSharp {
                        
                        base.Emit (ec);
 
-                       if (checked_state){
+                       if (ec.CheckState){
                                switch (mode){
                                case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
                                case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
@@ -2034,22 +1832,24 @@ namespace Mono.CSharp {
                public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
                {
                        if (ec.IsFieldInitializer)
+                               Report.Error (236, l,
+                                       "A field initializer cannot reference the nonstatic field, method, or property `{0}'",
+                                       name);
+                       else {
+                               if (name.LastIndexOf ('.') > 0)
+                                       name = name.Substring (name.LastIndexOf ('.') + 1);
+
                                Report.Error (
-                                       236, l,
-                                       "A field initializer cannot reference the non-static field, " +
-                                       "method or property `"+name+"'");
-                       else
-                               Report.Error (
-                                       120, l,
-                                       "An object reference is required " +
-                                       "for the non-static field `"+name+"'");
+                                       120, l, "`{0}': An object reference is required for the nonstatic field, method or property",
+                                       name);
+                       }
                }
 
                public bool IdenticalNameAndTypeName (EmitContext ec, Expression resolved_to, Location loc)
                {
                        return resolved_to != null && resolved_to.Type != null && 
                                resolved_to.Type.Name == Name &&
-                               (ec.DeclSpace.LookupType (Name, loc, /* ignore_cs0104 = */ true) != null);
+                               (ec.DeclContainer.LookupType (Name, loc, /* ignore_cs0104 = */ true) != null);
                }
 
                public override Expression DoResolve (EmitContext ec)
@@ -2068,14 +1868,42 @@ namespace Mono.CSharp {
                        return SimpleNameResolve (ec, null, intermediate);
                }
 
-               public override FullNamedExpression ResolveAsTypeStep (EmitContext ec)
+               public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
                        int errors = Report.Errors;
-                       FullNamedExpression dt = ec.DeclSpace.LookupType (Name, loc, /*ignore_cs0104=*/ false);
-                       if (Report.Errors != errors)
+                       FullNamedExpression fne = ec.DeclContainer.LookupType (Name, loc, /*ignore_cs0104=*/ false);
+                       if (fne != null)
+                               return fne;
+
+                       if (silent || errors != Report.Errors)
                                return null;
 
-                       return dt;
+                       MemberCore mc = ec.DeclContainer.GetDefinition (Name);
+                       if (mc != null) {
+                               Error_UnexpectedKind (ec.DeclContainer, "type", GetMemberType (mc), loc);
+                       } else {
+                               NamespaceEntry.Error_NamespaceNotFound (loc, Name);
+                       }
+
+                       return null;
+               }
+
+               // TODO: I am still not convinced about this. If someone else will need it
+               // implement this as virtual property in MemberCore hierarchy
+               string GetMemberType (MemberCore mc)
+               {
+                       if (mc is PropertyBase)
+                               return "property";
+                       if (mc is Indexer)
+                               return "indexer";
+                       if (mc is FieldBase)
+                               return "field";
+                       if (mc is MethodCore)
+                               return "method";
+                       if (mc is EnumMember)
+                               return "enum";
+
+                       return "type";
                }
 
                Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool intermediate)
@@ -2122,14 +1950,15 @@ namespace Mono.CSharp {
                        if (current_block != null){
                                LocalInfo vi = current_block.GetLocalInfo (Name);
                                if (vi != null){
-                                       Expression var;
-                                       
-                                       var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
-                                       
-                                       if (right_side != null)
+                                       LocalVariableReference var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
+                                       if (right_side != null) {
                                                return var.ResolveLValue (ec, right_side, loc);
-                                       else
-                                               return var.Resolve (ec);
+                                       } else {
+                                               ResolveFlags rf = ResolveFlags.VariableOrValue;
+                                               if (intermediate)
+                                                       rf |= ResolveFlags.DisableFlowAnalysis;
+                                               return var.Resolve (ec, rf);
+                                       }
                                }
 
                                ParameterReference pref = current_block.Toplevel.GetParameterReference (Name, loc);
@@ -2145,14 +1974,14 @@ namespace Mono.CSharp {
                        // Stage 2: Lookup members 
                        //
 
-                       DeclSpace lookup_ds = ec.DeclSpace;
+                       DeclSpace lookup_ds = ec.DeclContainer;
                        Type almost_matched_type = null;
                        ArrayList almost_matched = null;
                        do {
                                if (lookup_ds.TypeBuilder == null)
                                        break;
 
-                               e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
+                               e = MemberLookup (ec.ContainerType, lookup_ds.TypeBuilder, Name, loc);
                                if (e != null)
                                        break;
 
@@ -2165,14 +1994,14 @@ namespace Mono.CSharp {
                        } while (lookup_ds != null);
                                
                        if (e == null && ec.ContainerType != null)
-                               e = MemberLookup (ec, ec.ContainerType, Name, loc);
+                               e = MemberLookup (ec.ContainerType, ec.ContainerType, Name, loc);
 
                        if (e == null) {
                                if (almost_matched == null && almostMatchedMembers.Count > 0) {
                                        almost_matched_type = ec.ContainerType;
                                        almost_matched = (ArrayList) almostMatchedMembers.Clone ();
                                }
-                               e = ResolveAsTypeStep (ec);
+                               e = ResolveAsTypeStep (ec, true);
                        }
 
                        if (e == null) {
@@ -2180,7 +2009,7 @@ namespace Mono.CSharp {
                                        almostMatchedMembers = almost_matched;
                                if (almost_matched_type == null)
                                        almost_matched_type = ec.ContainerType;
-                               MemberLookupFailed (ec, null, almost_matched_type, ((SimpleName) this).Name, ec.DeclSpace.Name, true, loc);
+                               MemberLookupFailed (ec.ContainerType, null, almost_matched_type, ((SimpleName) this).Name, ec.DeclContainer.Name, true, loc);
                                return null;
                        }
 
@@ -2202,8 +2031,8 @@ namespace Mono.CSharp {
 
                                                if (!me.IsStatic &&
                                                    (!intermediate || !IdenticalNameAndTypeName (ec, me, loc))) {
-                                                       Error_ObjectRefRequired (ec, loc, Name);
-                                                       return null;
+                                                       Error_ObjectRefRequired (ec, loc, me.GetSignatureForError ());
+                                                       return EmptyExpression.Null;
                                                }
 
                                                //
@@ -2230,9 +2059,8 @@ namespace Mono.CSharp {
                                    me.InstanceExpression.Type != me.DeclaringType &&
                                    !me.InstanceExpression.Type.IsSubclassOf (me.DeclaringType) &&
                                    (!intermediate || !IdenticalNameAndTypeName (ec, e, loc))) {
-                                       Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
-                                              "outer type `" + me.DeclaringType + "' via nested type `" +
-                                              me.InstanceExpression.Type + "'");
+                                       Report.Error (38, loc, "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
+                                               TypeManager.CSharpName (me.DeclaringType), TypeManager.CSharpName (me.InstanceExpression.Type));
                                        return null;
                                }
 
@@ -2253,13 +2081,18 @@ namespace Mono.CSharp {
 
                        Error (103, "The name `" + Name +
                               "' does not exist in the class `" +
-                              ec.DeclSpace.Name + "'");
+                              ec.DeclContainer.Name + "'");
                }
 
                public override string ToString ()
                {
                        return Name;
                }
+
+               public override string GetSignatureForError ()
+               {
+                       return Name;
+               }
        }
 
        /// <summary>
@@ -2267,7 +2100,7 @@ namespace Mono.CSharp {
        ///   section 10.8.1 (Fully Qualified Names).
        /// </summary>
        public abstract class FullNamedExpression : Expression {
-               public override FullNamedExpression ResolveAsTypeStep (EmitContext ec)
+               public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
                        return this;
                }
@@ -2278,10 +2111,10 @@ namespace Mono.CSharp {
        }
        
        /// <summary>
-       ///   Fully resolved expression that evaluates to a type
+       ///   Expression that evaluates to a type
        /// </summary>
        public abstract class TypeExpr : FullNamedExpression {
-               override public FullNamedExpression ResolveAsTypeStep (EmitContext ec)
+               override public FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
                        TypeExpr t = DoResolveAsTypeStep (ec);
                        if (t == null)
@@ -2339,16 +2172,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public abstract TypeExpr DoResolveAsTypeStep (EmitContext ec);
-
-               public virtual Type ResolveType (EmitContext ec)
-               {
-                       TypeExpr t = ResolveAsTypeTerminal (ec, false);
-                       if (t == null)
-                               return null;
-
-                       return t.Type;
-               }
+               public abstract TypeExpr DoResolveAsTypeStep (IResolveContext ec);
 
                public abstract string Name {
                        get;
@@ -2374,6 +2198,9 @@ namespace Mono.CSharp {
                }
        }
 
+       /// <summary>
+       ///   Fully resolved Expression that already evaluated to a type
+       /// </summary>
        public class TypeExpression : TypeExpr {
                public TypeExpression (Type t, Location l)
                {
@@ -2382,21 +2209,22 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               public override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
+               {
+                       return this;
+               }
+
+               public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
                {
                        return this;
                }
 
                public override string Name {
-                       get {
-                               return Type.ToString ();
-                       }
+                       get { return Type.ToString (); }
                }
 
                public override string FullName {
-                       get {
-                               return Type.FullName;
-                       }
+                       get { return Type.FullName; }
                }
        }
 
@@ -2405,44 +2233,88 @@ namespace Mono.CSharp {
        ///   by the parser to setup the core types.  A TypeLookupExpression is always
        ///   classified as a type.
        /// </summary>
-       public class TypeLookupExpression : TypeExpr {
-               string name;
+       public sealed class TypeLookupExpression : TypeExpr {
+               readonly string name;
                
                public TypeLookupExpression (string name)
                {
                        this.name = name;
+                       eclass = ExprClass.Type;
                }
 
-               public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
                {
-                       if (type == null) {
-                               FullNamedExpression t = ec.DeclSpace.LookupType (name, Location.Null, /*ignore_cs0104=*/ false);
-                               if (t == null) {
-                                       Report.Error (246, loc, "Cannot find type `" + name + "'");
-                                       return null;
+                       // It's null for corlib compilation only
+                       if (type == null)
+                               return DoResolveAsTypeStep (ec);
+
+                       return this;
+               }
+
+               static readonly char [] dot_array = { '.' };
+               public override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
+               {
+                       // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
+                       string rest = null;
+                       string lookup_name = name;
+                       int pos = name.IndexOf ('.');
+                       if (pos >= 0) {
+                               rest = name.Substring (pos + 1);
+                               lookup_name = name.Substring (0, pos);
+                       }
+
+                       FullNamedExpression resolved = RootNamespace.Global.Lookup (ec.DeclContainer, lookup_name, Location.Null);
+
+                       if (resolved != null && rest != null) {
+                               // Now handle the rest of the the name.
+                               string [] elements = rest.Split (dot_array);
+                               string element;
+                               int count = elements.Length;
+                               int i = 0;
+                               while (i < count && resolved != null && resolved is Namespace) {
+                                       Namespace ns = resolved as Namespace;
+                                       element = elements [i++];
+                                       lookup_name += "." + element;
+                                       resolved = ns.Lookup (ec.DeclContainer, element, Location.Null);
                                }
-                               if (!(t is TypeExpr)) {
-                                       Report.Error (118, Location, "'{0}' denotes a '{1}', where a type was expected",
-                                                     t.FullName, t.ExprClassName ());
 
-                                       return null;
+                               if (resolved != null && resolved is TypeExpr) {
+                                       Type t = ((TypeExpr) resolved).Type;
+                                       while (t != null) {
+                                               if (!ec.DeclContainer.CheckAccessLevel (t)) {
+                                                       resolved = null;
+                                                       lookup_name = t.FullName;
+                                                       break;
+                                               }
+                                               if (i == count) {
+                                                       type = t;
+                                                       return this;
+                                               }
+                                               t = TypeManager.GetNestedType (t, elements [i++]);
+                                       }
                                }
-                               type = ((TypeExpr) t).ResolveType (ec);
                        }
 
+                       if (resolved == null) {
+                               NamespaceEntry.Error_NamespaceNotFound (loc, lookup_name);
+                               return null;
+                       }
+
+                       if (!(resolved is TypeExpr)) {
+                               resolved.Error_UnexpectedKind (ec.DeclContainer, "type", loc);
+                               return null;
+                       }
+
+                       type = resolved.Type;
                        return this;
                }
 
                public override string Name {
-                       get {
-                               return name;
-                       }
+                       get { return name; }
                }
 
                public override string FullName {
-                       get {
-                               return name;
-                       }
+                       get { return name; }
                }
        }
 
@@ -2465,13 +2337,9 @@ namespace Mono.CSharp {
                        get { return texpr.FullName; }
                }
 
-               public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               public override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
                {
-                       Type type = texpr.ResolveType (ec);
-                       if (type == null)
-                               return null;
-
-                       return new TypeExpression (type, loc);
+                       return texpr;
                }
 
                public override bool CheckAccessLevel (DeclSpace ds)
@@ -2543,11 +2411,10 @@ namespace Mono.CSharp {
 
                public static void error176 (Location loc, string name)
                {
-                       Report.Error (176, loc, "Static member `" + name + "' cannot be accessed " +
-                                     "with an instance reference, qualify with a type name instead");
+                       Report.Error (176, loc, "Static member `{0}' cannot be accessed " +
+                                     "with an instance reference, qualify it with a type name instead", name);
                }
 
-
                // TODO: possible optimalization
                // Cache resolved constant result in FieldBuilder <-> expression map
                public virtual Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
@@ -2571,7 +2438,7 @@ namespace Mono.CSharp {
                                if (original != null && original.IdenticalNameAndTypeName (ec, left, loc))
                                        return this;
 
-                               error176 (loc, Name);
+                               error176 (loc, GetSignatureForError ());
                                return null;
                        }
 
@@ -2676,9 +2543,14 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override string GetSignatureForError ()
+               {
+                       return TypeManager.CSharpSignature (Methods [0]);
+               }
+
                public override string Name {
                        get {
-                                return Methods [0].Name;
+                               return Methods [0].Name;
                        }
                }
 
@@ -2823,6 +2695,11 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override string GetSignatureForError ()
+               {
+                       return TypeManager.GetFullNameSignature (FieldInfo);
+               }
+
                public VariableInfo VariableInfo {
                        get {
                                return variable_info;
@@ -2832,86 +2709,35 @@ namespace Mono.CSharp {
                public override Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
                                                                SimpleName original)
                {
-                       bool left_is_type = left is TypeExpr;
-
-                       Type decl_type = FieldInfo.DeclaringType;
-                       
-                       bool is_emitted = FieldInfo is FieldBuilder;
                        Type t = FieldInfo.FieldType;
-                       
-                       if (is_emitted) {
-                               Const c = TypeManager.LookupConstant ((FieldBuilder) FieldInfo);
-                               
-                               if (c != null) {
-                                       object o;
-                                       if (!c.LookupConstantValue (out o))
-                                               return null;
 
-                                       c.SetMemberIsUsed ();
-                                       object real_value = ((Constant) c.Expr).GetValue ();
-
-                                       Expression exp = Constantify (real_value, t);
-                                       
-                                       if (!left_is_type && 
-                                           (original == null || !original.IdenticalNameAndTypeName (ec, left, loc))) {
-                                               Report.SymbolRelatedToPreviousError (c);
-                                               error176 (loc, c.GetSignatureForError ());
-                                               return null;
+                       if (FieldInfo.IsLiteral || (FieldInfo.IsInitOnly && t == TypeManager.decimal_type)) {
+                               IConstant ic = TypeManager.GetConstant (FieldInfo);
+                               if (ic == null) {
+                                       if (FieldInfo.IsLiteral) {
+                                               ic = new ExternalConstant (FieldInfo);
+                                       } else {
+                                               ic = ExternalConstant.CreateDecimal (FieldInfo);
+                                               if (ic == null) {
+                                                       return base.ResolveMemberAccess (ec, left, loc, original);
+                                               }
                                        }
-                                       
-                                       return exp;
+                                       TypeManager.RegisterConstant (FieldInfo, ic);
                                }
-                       }
 
-                       //
-                       // Decimal constants cannot be encoded in the constant blob, and thus are marked
-                       // as IsInitOnly ('readonly' in C# parlance).  We get its value from the 
-                       // DecimalConstantAttribute metadata.
-                       //
-                       if (FieldInfo.IsInitOnly && !is_emitted && t == TypeManager.decimal_type) {
-                               object[] attrs = FieldInfo.GetCustomAttributes (TypeManager.decimal_constant_attribute_type, false);
-                               if (attrs.Length == 1)
-                                       return new DecimalConstant (((System.Runtime.CompilerServices.DecimalConstantAttribute) attrs [0]).Value);
-                       }
-                       
-                       if (FieldInfo.IsLiteral) {
-                               object o;
-                               
-                               if (is_emitted)
-                                       o = TypeManager.GetValue ((FieldBuilder) FieldInfo);
-                               else
-                                       o = FieldInfo.GetValue (FieldInfo);
-                               
-                               if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
-                                       if (!left_is_type &&
-                                           (original == null || !original.IdenticalNameAndTypeName (ec, left, loc))) {
-                                               error176 (loc, FieldInfo.Name);
-                                               return null;
-                                       }                                       
-                                       
-                                       Expression enum_member = MemberLookup (
-                                              ec, decl_type, "value__", MemberTypes.Field,
-                                              AllBindingFlags | BindingFlags.NonPublic, loc); 
-                                       
-                                       Enum en = TypeManager.LookupEnum (decl_type);
-                                       
-                                       Constant c;
-                                       if (en != null)
-                                               c = Constantify (o, en.UnderlyingType);
-                                       else 
-                                               c = Constantify (o, enum_member.Type);
-                                       
-                                       return new EnumConstant (c, decl_type);
-                               }
-                               
-                               Expression exp = Constantify (o, t);
-                               
-                               if (!left_is_type) {
-                                       error176 (loc, FieldInfo.Name);
+                               bool left_is_type = left is TypeExpr;
+                               if (!left_is_type && (original == null || !original.IdenticalNameAndTypeName (ec, left, loc))) {
+                                       Report.SymbolRelatedToPreviousError (FieldInfo);
+                                       error176 (loc, TypeManager.GetFullNameSignature (FieldInfo));
                                        return null;
                                }
-                               
-                               return exp;
+
+                               if (ic.ResolveValue ()) {
+                                       if (!ec.IsInObsoleteScope)
+                                               ic.CheckObsoleteness (loc);
+                               }
+
+                               return ic.Value;
                        }
                        
                        if (t.IsPointer && !ec.InUnsafe) {
@@ -2923,21 +2749,26 @@ namespace Mono.CSharp {
                }
 
                override public Expression DoResolve (EmitContext ec)
+               {
+                       return DoResolve (ec, false);
+               }
+
+               Expression DoResolve (EmitContext ec, bool lvalue_instance)
                {
                        if (ec.InRefOutArgumentResolving && FieldInfo.IsInitOnly && !ec.IsConstructor && FieldInfo.FieldType.IsValueType) {
                                if (FieldInfo.FieldType is TypeBuilder) {
                                        if (FieldInfo.IsStatic)
-                                               Report.Error (1651, loc, "Members of readonly static field '{0}.{1}' cannot be passed ref or out (except in a constructor)",
-                                                       TypeManager.CSharpName (DeclaringType), Name);
+                                               Report.Error (1651, loc, "Fields of static readonly field `{0}' cannot be passed ref or out (except in a static constructor)",
+                                                       GetSignatureForError ());
                                        else
-                                               Report.Error (1649, loc, "Members of readonly field '{0}.{1}' cannot be passed ref or out (except in a constructor)",
+                                               Report.Error (1649, loc, "Members of readonly field `{0}.{1}' cannot be passed ref or out (except in a constructor)",
                                                        TypeManager.CSharpName (DeclaringType), Name);
                                } else {
                                        if (FieldInfo.IsStatic)
-                                               Report.Error (199, loc, "A static readonly field '{0}' cannot be passed ref or out (except in a static constructor)",
+                                               Report.Error (199, loc, "A static readonly field `{0}' cannot be passed ref or out (except in a static constructor)",
                                                        Name);
                                        else
-                                               Report.Error (192, loc, "A readonly field '{0}' cannot be passed ref or out (except in a constructor)",
+                                               Report.Error (192, loc, "A readonly field `{0}' cannot be passed ref or out (except in a constructor)",
                                                        Name);
                                }
                                return null;
@@ -2956,19 +2787,27 @@ namespace Mono.CSharp {
                                // Resolve the field's instance expression while flow analysis is turned
                                // off: when accessing a field "a.b", we must check whether the field
                                // "a.b" is initialized, not whether the whole struct "a" is initialized.
-                               InstanceExpression = InstanceExpression.Resolve (
-                                       ec, ResolveFlags.VariableOrValue | ResolveFlags.DisableFlowAnalysis);
+
+                               if (lvalue_instance) {
+                                       bool old_do_flow_analysis = ec.DoFlowAnalysis;
+                                       ec.DoFlowAnalysis = false;
+                                       InstanceExpression = InstanceExpression.ResolveLValue (ec, EmptyExpression.LValueMemberAccess, loc);
+                                       ec.DoFlowAnalysis = old_do_flow_analysis;
+                               } else {
+                                       ResolveFlags rf = ResolveFlags.VariableOrValue | ResolveFlags.DisableFlowAnalysis;
+                                       InstanceExpression = InstanceExpression.Resolve (ec, rf);
+                               }
+
                                if (InstanceExpression == null)
                                        return null;
                        }
 
-                       if (!in_initializer) {
+                       if (!in_initializer && !ec.IsFieldInitializer) {
                                ObsoleteAttribute oa;
                                FieldBase f = TypeManager.GetField (FieldInfo);
                                if (f != null) {
-                                       oa = f.GetObsoleteAttribute (f.Parent);
-                                       if (oa != null)
-                                               AttributeTester.Report_ObsoleteMessage (oa, f.GetSignatureForError (), loc);
+                                       if (!ec.IsInObsoleteScope)
+                                               f.CheckObsoleteness (loc);
                                 
                                        // To be sure that type is external because we do not register generated fields
                                } else if (!(FieldInfo.DeclaringType is TypeBuilder)) {                                
@@ -2978,14 +2817,18 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (ec.CurrentAnonymousMethod != null){
+                       AnonymousContainer am = ec.CurrentAnonymousMethod;
+                       if (am != null){
                                if (!FieldInfo.IsStatic){
-                                       if (!ec.CurrentAnonymousMethod.IsIterator && (ec.TypeContainer is Struct)){
-                                               Report.Error (1673, loc, "Can not reference instance variables in anonymous methods hosted in structs");
+                                       if (!am.IsIterator && (ec.TypeContainer is Struct)){
+                                               Report.Error (1673, loc,
+                                               "Anonymous methods inside structs cannot access instance members of `{0}'. Consider copying `{0}' to a local variable outside the anonymous method and using the local instead",
+                                                       "this");
                                                return null;
                                        }
-                                       ec.CaptureField (this);
-                               } 
+                                       if ((am.ContainerAnonymousMethod == null) && (InstanceExpression is This))
+                                               ec.CaptureField (this);
+                               }
                        }
                        
                        // If the instance expression is a local variable or parameter.
@@ -3001,18 +2844,32 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               void Report_AssignToReadonly (bool is_instance)
+               void Report_AssignToReadonly (Expression right_side)
                {
+                       int code;
                        string msg;
-                       
-                       if (is_instance)
-                               msg = "Readonly field can not be assigned outside " +
-                               "of constructor or variable initializer";
-                       else
-                               msg = "A static readonly field can only be assigned in " +
-                               "a static constructor";
+                       bool need_error_sig = false;
+                       if (right_side == EmptyExpression.LValueMemberAccess) {
+                               if (IsStatic) {
+                                       code = 1650;
+                                       msg = "Fields of static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)";
+                               } else {
+                                       code = 1648;
+                                       msg = "Members of readonly field `{0}' cannot be modified (except in a constructor or a variable initializer)";
+                               }
+                               need_error_sig = true;
+                       } else if (IsStatic) {
+                               code = 198;
+                               msg = "A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)";
+                       } else {
+                               code = 191;
+                               msg = "A readonly field cannot be assigned to (except in a constructor or a variable initializer)";
+                       }
 
-                       Report.Error (is_instance ? 191 : 198, loc, msg);
+                       if (need_error_sig)
+                               Report.Error (code, loc, msg, GetSignatureForError ());
+                       else
+                               Report.Error (code, loc, msg);
                }
                
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
@@ -3021,17 +2878,13 @@ namespace Mono.CSharp {
                        if ((var != null) && (var.VariableInfo != null))
                                var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
 
-                       Expression e = DoResolve (ec);
+                       bool lvalue_instance = !FieldInfo.IsStatic && FieldInfo.DeclaringType.IsValueType;
+
+                       Expression e = DoResolve (ec, lvalue_instance);
 
                        if (e == null)
                                return null;
 
-                       if (!FieldInfo.IsStatic && (InstanceExpression.Type.IsValueType && !(InstanceExpression is IMemoryLocation))) {
-                               // FIXME: Provide better error reporting.
-                               Error (1612, "Cannot modify expression because it is not a variable.");
-                               return null;
-                       }
-
                        FieldBase fb = TypeManager.GetField (FieldInfo);
                        if (fb != null)
                                fb.SetAssigned ();
@@ -3045,13 +2898,13 @@ namespace Mono.CSharp {
 
                        if (ec.IsConstructor){
                                if (IsStatic && !ec.IsStatic)
-                                       Report_AssignToReadonly (false);
+                                       Report_AssignToReadonly (right_side);
 
                                if (ec.ContainerType == FieldInfo.DeclaringType)
                                        return this;
                        }
 
-                       Report_AssignToReadonly (!IsStatic);
+                       Report_AssignToReadonly (right_side);
                        
                        return null;
                }
@@ -3060,17 +2913,18 @@ namespace Mono.CSharp {
                {
                        if (!IsStatic && Type.IsValueType && !container.IsSubclassOf (TypeManager.mbr_type) && DeclaringType.IsSubclassOf (TypeManager.mbr_type)) {
                                Report.SymbolRelatedToPreviousError (DeclaringType);
-                               Report.Error (1690, loc, "Cannot call '{0}' method, property, or indexer because it is a value type member of a marshal-by-reference class", Name);
+                               Report.Error (1690, loc, "Cannot call methods, properties, or indexers on `{0}' because it is a value type member of a marshal-by-reference class",
+                                       GetSignatureForError ());
                        }
                }
 
-               public bool VerifyFixed (bool is_expression)
+               public bool VerifyFixed ()
                {
                        IVariable variable = InstanceExpression as IVariable;
-                       if ((variable == null) || !variable.VerifyFixed (true))
-                               return false;
-
-                       return true;
+                       // A variable of the form V.I is fixed when V is a fixed variable of a struct type.
+                       // We defer the InstanceExpression check after the variable check to avoid a 
+                       // separate null check on InstanceExpression.
+                       return variable != null && InstanceExpression.Type.IsValueType && variable.VerifyFixed ();
                }
 
                public override int GetHashCode()
@@ -3149,7 +3003,7 @@ namespace Mono.CSharp {
                        prepared = prepare_for_load;
 
                        if (is_readonly && !ec.IsConstructor){
-                               Report_AssignToReadonly (!is_static);
+                               Report_AssignToReadonly (source);
                                return;
                        }
 
@@ -3170,7 +3024,7 @@ namespace Mono.CSharp {
                                        if ((f.ModFlags & Modifiers.VOLATILE) != 0)
                                                ig.Emit (OpCodes.Volatile);
                                        
-                                       f.status |= Field.Status.ASSIGNED;
+                                       f.SetAssigned ();
                                }
                        } 
 
@@ -3196,12 +3050,13 @@ namespace Mono.CSharp {
                                FieldBase f = TypeManager.GetField (FieldInfo);
                                if (f != null){
                                        if ((f.ModFlags & Modifiers.VOLATILE) != 0){
-                                               Error (676, "volatile variable: can not take its address, or pass as ref/out parameter");
+                                               Report.Warning (420, 1, loc, "`{0}': A volatile fields cannot be passed using a ref or out parameter",
+                                                       f.GetSignatureForError ());
                                                return;
                                        }
                                        
                                        if ((mode & AddressOp.Store) != 0)
-                                               f.status |= Field.Status.ASSIGNED;
+                                               f.SetAssigned ();
                                        if ((mode & AddressOp.Load) != 0)
                                                f.SetMemberIsUsed ();
                                }
@@ -3281,7 +3136,7 @@ namespace Mono.CSharp {
 
                internal static PtrHashtable AccessorTable = new PtrHashtable (); 
 
-               public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
+               public PropertyExpr (Type containerType, PropertyInfo pi, Location l)
                {
                        PropertyInfo = pi;
                        eclass = ExprClass.PropertyAccess;
@@ -3290,7 +3145,7 @@ namespace Mono.CSharp {
 
                        type = TypeManager.TypeToCoreType (pi.PropertyType);
 
-                       ResolveAccessors (ec);
+                       ResolveAccessors (containerType);
                }
 
                public override string Name {
@@ -3317,21 +3172,14 @@ namespace Mono.CSharp {
                        }
                }
 
-               public bool VerifyAssignable ()
+               public override string GetSignatureForError ()
                {
-                       if (setter == null) {
-                               Report.Error (200, loc, 
-                                             "The property `" + PropertyInfo.Name +
-                                             "' can not be assigned to, as it has not set accessor");
-                               return false;
-                       }
-
-                       return true;
+                       return TypeManager.GetFullNameSignature (PropertyInfo);
                }
 
                void FindAccessors (Type invocation_type)
                {
-                       BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
+                       const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
                                BindingFlags.Static | BindingFlags.Instance |
                                BindingFlags.DeclaredOnly;
 
@@ -3367,9 +3215,10 @@ namespace Mono.CSharp {
                // We also perform the permission checking here, as the PropertyInfo does not
                // hold the information for the accessibility of its setter/getter
                //
-               void ResolveAccessors (EmitContext ec)
+               // TODO: can use TypeManager.GetProperty to boost performance
+               void ResolveAccessors (Type containerType)
                {
-                       FindAccessors (ec.ContainerType);
+                       FindAccessors (containerType);
 
                        if (getter != null) {
                                IMethodData md = TypeManager.GetMethod (getter);
@@ -3390,7 +3239,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               bool InstanceResolve (EmitContext ec, bool must_do_cs1540_check)
+               bool InstanceResolve (EmitContext ec, bool lvalue_instance, bool must_do_cs1540_check)
                {
                        if (is_static) {
                                InstanceExpression = null;
@@ -3402,28 +3251,45 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       InstanceExpression = InstanceExpression.DoResolve (ec);
+                       if (lvalue_instance)
+                               InstanceExpression = InstanceExpression.ResolveLValue (ec, EmptyExpression.LValueMemberAccess, loc);
+                       else
+                               InstanceExpression = InstanceExpression.DoResolve (ec);
                        if (InstanceExpression == null)
                                return false;
                        
                        InstanceExpression.CheckMarshallByRefAccess (ec.ContainerType);
 
-                       if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null) {
-                               if ((InstanceExpression.Type != ec.ContainerType) &&
-                                   ec.ContainerType.IsSubclassOf (InstanceExpression.Type)) {
-                                       Report.Error (1540, loc, "Cannot access protected member `" +
-                                                     PropertyInfo.DeclaringType + "." + PropertyInfo.Name + 
-                                                     "' via a qualifier of type `" +
-                                                     TypeManager.CSharpName (InstanceExpression.Type) +
-                                                     "'; the qualifier must be of type `" +
-                                                     TypeManager.CSharpName (ec.ContainerType) +
-                                                     "' (or derived from it)");
-                                       return false;
-                               }
+                       if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null &&
+                           InstanceExpression.Type != ec.ContainerType &&
+                           ec.ContainerType.IsSubclassOf (PropertyInfo.DeclaringType) &&
+                           !InstanceExpression.Type.IsSubclassOf (ec.ContainerType)) {
+                               Error_CannotAccessProtected (loc, PropertyInfo, InstanceExpression.Type, ec.ContainerType);
+                               return false;
                        }
 
                        return true;
                }
+
+               void Error_PropertyNotFound (MethodInfo mi, bool getter)
+               {
+                       // TODO: correctly we should compare arguments but it will lead to bigger changes
+                       if (mi is MethodBuilder) {
+                               Error_TypeDoesNotContainDefinition (loc, PropertyInfo.DeclaringType, Name);
+                               return;
+                       }
+
+                       StringBuilder sig = new StringBuilder (TypeManager.CSharpName (mi.DeclaringType));
+                       sig.Append ('.');
+                       ParameterData iparams = TypeManager.GetParameterData (mi);
+                       sig.Append (getter ? "get_" : "set_");
+                       sig.Append (Name);
+                       sig.Append (iparams.GetSignatureForError ());
+
+                       Report.SymbolRelatedToPreviousError (mi);
+                       Report.Error (1546, loc, "Property `{0}' is not supported by the C# language. Try to call the accessor method `{1}' directly",
+                               Name, sig.ToString ());
+               }
                
                override public Expression DoResolve (EmitContext ec)
                {
@@ -3431,11 +3297,8 @@ namespace Mono.CSharp {
                                return this;
 
                        if (getter != null){
-                               if (TypeManager.GetArgumentTypes (getter).Length != 0){
-                                       Report.Error (
-                                               117, loc, "`{0}' does not contain a " +
-                                               "definition for `{1}'.", getter.DeclaringType,
-                                               Name);
+                               if (TypeManager.GetParameterData (getter).Count != 0){
+                                       Error_PropertyNotFound (getter, true);
                                        return null;
                                }
                        }
@@ -3451,10 +3314,8 @@ namespace Mono.CSharp {
                                        return null;
 
                                if (InstanceExpression != EmptyExpression.Null) {
-                                       Report.Error (154, loc, 
-                                               "The property `" + PropertyInfo.Name +
-                                               "' can not be used in " +
-                                               "this context because it lacks a get accessor");
+                                       Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks the `get' accessor",
+                                               TypeManager.GetFullNameSignature (PropertyInfo));
                                        return null;
                                }
                        } 
@@ -3465,24 +3326,22 @@ namespace Mono.CSharp {
                                PropertyBase.PropertyMethod pm = TypeManager.GetMethod (getter) as PropertyBase.PropertyMethod;
                                if (pm != null && pm.HasCustomAccessModifier) {
                                        Report.SymbolRelatedToPreviousError (pm);
-                                       Report.Error (271, loc, "The property or indexer '{0}' cannot be used in this context because the get accessor is inaccessible",
+                                       Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because the get accessor is inaccessible",
                                                TypeManager.CSharpSignature (getter));
                                }
                                else
-                                       Report.Error (122, loc, "'{0}' is inaccessible due to its protection level",
-                                               TypeManager.CSharpSignature (getter));
+                                       ErrorIsInaccesible (loc, TypeManager.CSharpSignature (getter));
                                return null;
                        }
                        
-                       if (!InstanceResolve (ec, must_do_cs1540_check))
+                       if (!InstanceResolve (ec, false, must_do_cs1540_check))
                                return null;
 
                        //
                        // Only base will allow this invocation to happen.
                        //
-                       if (IsBase && getter.IsAbstract){
-                               Report.Error (205, loc, "Cannot call an abstract base property: " +
-                                             PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
+                       if (IsBase && getter.IsAbstract) {
+                               Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (PropertyInfo));
                                return null;
                        }
 
@@ -3498,6 +3357,18 @@ namespace Mono.CSharp {
 
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
+                       if (right_side == EmptyExpression.OutAccess) {
+                               Report.Error (206, loc, "A property or indexer `{0}' may not be passed as an out or ref parameter",
+                                             GetSignatureForError ());
+                               return null;
+                       }
+
+                       if (right_side == EmptyExpression.LValueMemberAccess) {
+                               Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable",
+                                             GetSignatureForError ());
+                               return null;
+                       }
+
                        if (setter == null){
                                //
                                // The following condition happens if the PropertyExpr was
@@ -3507,18 +3378,13 @@ namespace Mono.CSharp {
                                //
                                if (getter == null)
                                        return null;
-                               
-                               // TODO: Print better property name
-                               Report.Error (200, loc, "Property or indexer '{0}' cannot be assigned to -- it is read only",
-                                             PropertyInfo.Name);
+                               Report.Error (200, loc, "Property or indexer `{0}' cannot be assigned to (it is read only)",
+                                             GetSignatureForError ());
                                return null;
                        }
 
-                       if (TypeManager.GetArgumentTypes (setter).Length != 1){
-                               Report.Error (
-                                       117, loc, "`{0}' does not contain a " +
-                                       "definition for `{1}'.", getter.DeclaringType,
-                                       Name);
+                       if (TypeManager.GetParameterData (setter).Count != 1){
+                               Error_PropertyNotFound (setter, false);
                                return null;
                        }
 
@@ -3527,33 +3393,22 @@ namespace Mono.CSharp {
                                PropertyBase.PropertyMethod pm = TypeManager.GetMethod (setter) as PropertyBase.PropertyMethod;
                                if (pm != null && pm.HasCustomAccessModifier) {
                                        Report.SymbolRelatedToPreviousError (pm);
-                                       Report.Error (272, loc, "The property or indexer '{0}' cannot be used in this context because the set accessor is inaccessible",
+                                       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
-                                       Report.Error (122, loc, "'{0}' is inaccessible due to its protection level",
-                                               TypeManager.CSharpSignature (setter));
+                                       ErrorIsInaccesible (loc, TypeManager.CSharpSignature (setter));
                                return null;
                        }
                        
-                       if (!InstanceResolve (ec, must_do_cs1540_check))
+                       if (!InstanceResolve (ec, PropertyInfo.DeclaringType.IsValueType, must_do_cs1540_check))
                                return null;
                        
                        //
                        // Only base will allow this invocation to happen.
                        //
                        if (IsBase && setter.IsAbstract){
-                               Report.Error (205, loc, "Cannot call an abstract base property: " +
-                                             PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
-                               return null;
-                       }
-
-                       //
-                       // Check that we are not making changes to a temporary memory location
-                       //
-                       if (InstanceExpression != null && InstanceExpression.Type.IsValueType && !(InstanceExpression is IMemoryLocation)) {
-                               // FIXME: Provide better error reporting.
-                               Error (1612, "Cannot modify expression because it is not a variable.");
+                               Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (PropertyInfo));
                                return null;
                        }
 
@@ -3567,9 +3422,6 @@ namespace Mono.CSharp {
                
                public void Emit (EmitContext ec, bool leave_copy)
                {
-                       if (!prepared)
-                               EmitInstance (ec, false);
-                       
                        //
                        // Special case: length of single dimension array property is turned into ldlen
                        //
@@ -3582,21 +3434,22 @@ namespace Mono.CSharp {
                                // support invoking GetArrayRank, so test for that case first
                                //
                                if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)) {
+                                       if (!prepared)
+                                               EmitInstance (ec, false);
                                        ec.ig.Emit (OpCodes.Ldlen);
                                        ec.ig.Emit (OpCodes.Conv_I4);
                                        return;
                                }
                        }
 
-                       Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), getter, null, loc);
+                       Invocation.EmitCall (ec, IsBase, IsStatic, InstanceExpression, getter, null, loc, prepared, false);
                        
-                       if (!leave_copy)
-                               return;
-                       
-                       ec.ig.Emit (OpCodes.Dup);
-                       if (!is_static) {
-                               temp = new LocalTemporary (ec, this.Type);
-                               temp.Store (ec);
+                       if (leave_copy) {
+                               ec.ig.Emit (OpCodes.Dup);
+                               if (!is_static) {
+                                       temp = new LocalTemporary (ec, this.Type);
+                                       temp.Store (ec);
+                               }
                        }
                }
 
@@ -3605,23 +3458,32 @@ namespace Mono.CSharp {
                //
                public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
+                       Expression my_source = source;
+
                        prepared = prepare_for_load;
                        
-                       EmitInstance (ec, prepare_for_load);
-
-                       source.Emit (ec);
-                       if (leave_copy) {
-                               ec.ig.Emit (OpCodes.Dup);
+                       if (prepared) {
+                               source.Emit (ec);
+                               if (leave_copy) {
+                                       ec.ig.Emit (OpCodes.Dup);
+                                       if (!is_static) {
+                                               temp = new LocalTemporary (ec, this.Type);
+                                               temp.Store (ec);
+                                       }
+                               }
+                       } else if (leave_copy) {
+                               source.Emit (ec);
                                if (!is_static) {
                                        temp = new LocalTemporary (ec, this.Type);
                                        temp.Store (ec);
                                }
+                               my_source = temp;
                        }
                        
                        ArrayList args = new ArrayList (1);
-                       args.Add (new Argument (new EmptyAddressOf (), Argument.AType.Expression));
+                       args.Add (new Argument (my_source, Argument.AType.Expression));
                        
-                       Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), setter, args, loc);
+                       Invocation.EmitCall (ec, IsBase, IsStatic, InstanceExpression, setter, args, loc, false, prepared);
                        
                        if (temp != null)
                                temp.Emit (ec);
@@ -3693,7 +3555,7 @@ namespace Mono.CSharp {
                                MemberInfo mi = TypeManager.GetPrivateFieldOfEvent (EventInfo);
 
                                if (mi != null) {
-                                       MemberExpr ml = (MemberExpr) ExprClassFromMemberInfo (ec, mi, loc);
+                                       MemberExpr ml = (MemberExpr) ExprClassFromMemberInfo (ec.ContainerType, mi, loc);
 
                                        if (ml == null) {
                                                Report.Error (-200, loc, "Internal error!!");
@@ -3730,14 +3592,11 @@ namespace Mono.CSharp {
                        // This is using the same mechanism as the CS1540 check in PropertyExpr.
                        // However, in the Event case, we reported a CS0122 instead.
                        //
-                       if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null) {
-                               if ((InstanceExpression.Type != ec.ContainerType) &&
-                                       ec.ContainerType.IsSubclassOf (InstanceExpression.Type)) {
-                                       Report.Error (122, loc, "'{0}' is inaccessible due to its protection level",
-                                               DeclaringType.Name + "." + EventInfo.Name);
-
-                                       return false;
-                               }
+                       if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null &&
+                           InstanceExpression.Type != ec.ContainerType &&
+                           ec.ContainerType.IsSubclassOf (InstanceExpression.Type)) {
+                               ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo));
+                               return false;
                        }
 
                        return true;
@@ -3753,10 +3612,8 @@ namespace Mono.CSharp {
                        bool must_do_cs1540_check;
                        if (!(IsAccessorAccessible (ec.ContainerType, add_accessor, out must_do_cs1540_check) &&
                              IsAccessorAccessible (ec.ContainerType, remove_accessor, out must_do_cs1540_check))) {
-                               
-                               Report.Error (122, loc, "'{0}' is inaccessible due to its protection level",
-                                               DeclaringType.Name + "." + EventInfo.Name);
-                               return null;
+                               ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo));
+                               return null;
                        }
 
                        if (!InstanceResolve (ec, must_do_cs1540_check))
@@ -3768,12 +3625,17 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        if (InstanceExpression is This)
-                               Report.Error (79, loc, "The event `{0}' can only appear on the left hand side of += or -=, try calling the actual delegate", Name);
+                               Report.Error (79, loc, "The event `{0}' can only appear on the left hand side of += or -=", GetSignatureForError ());
                        else
                                Report.Error (70, loc, "The event `{0}' can only appear on the left hand side of += or -= "+
                                              "(except on the defining type)", Name);
                }
 
+               public override string GetSignatureForError ()
+               {
+                       return TypeManager.CSharpSignature (EventInfo);
+               }
+
                public void EmitAddOrRemove (EmitContext ec, Expression source)
                {
                        BinaryDelegate source_del = (BinaryDelegate) source;
@@ -3792,4 +3654,92 @@ namespace Mono.CSharp {
                                        ec, false, IsStatic, InstanceExpression, remove_accessor, args, loc);
                }
        }
+
+       
+       public class TemporaryVariable : Expression, IMemoryLocation
+       {
+               LocalInfo li;
+               
+               public TemporaryVariable (Type type, Location loc)
+               {
+                       this.type = type;
+                       this.loc = loc;
+                       eclass = ExprClass.Value;
+               }
+               
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       if (li != null)
+                               return this;
+                       
+                       TypeExpr te = new TypeExpression (type, loc);
+                       li = ec.CurrentBlock.AddTemporaryVariable (te, loc);
+                       if (!li.Resolve (ec))
+                               return null;
+                       
+                       AnonymousContainer am = ec.CurrentAnonymousMethod;
+                       if ((am != null) && am.IsIterator)
+                               ec.CaptureVariable (li);
+                       
+                       return this;
+               }
+               
+               public override void Emit (EmitContext ec)
+               {
+                       ILGenerator ig = ec.ig;
+                       
+                       if (li.FieldBuilder != null) {
+                               ig.Emit (OpCodes.Ldarg_0);
+                               ig.Emit (OpCodes.Ldfld, li.FieldBuilder);
+                       } else {
+                               ig.Emit (OpCodes.Ldloc, li.LocalBuilder);
+                       }
+               }
+               
+               public void EmitLoadAddress (EmitContext ec)
+               {
+                       ILGenerator ig = ec.ig;
+                       
+                       if (li.FieldBuilder != null) {
+                               ig.Emit (OpCodes.Ldarg_0);
+                               ig.Emit (OpCodes.Ldflda, li.FieldBuilder);
+                       } else {
+                               ig.Emit (OpCodes.Ldloca, li.LocalBuilder);
+                       }
+               }
+               
+               public void Store (EmitContext ec, Expression right_side)
+               {
+                       if (li.FieldBuilder != null)
+                               ec.ig.Emit (OpCodes.Ldarg_0);
+                       
+                       right_side.Emit (ec);
+                       if (li.FieldBuilder != null) {
+                               ec.ig.Emit (OpCodes.Stfld, li.FieldBuilder);
+                       } else {
+                               ec.ig.Emit (OpCodes.Stloc, li.LocalBuilder);
+                       }
+               }
+               
+               public void EmitThis (EmitContext ec)
+               {
+                       if (li.FieldBuilder != null) {
+                               ec.ig.Emit (OpCodes.Ldarg_0);
+                       }
+               }
+               
+               public void EmitStore (ILGenerator ig)
+               {
+                       if (li.FieldBuilder != null)
+                               ig.Emit (OpCodes.Stfld, li.FieldBuilder);
+                       else
+                               ig.Emit (OpCodes.Stloc, li.LocalBuilder);
+               }
+               
+               public void AddressOf (EmitContext ec, AddressOp mode)
+               {
+                       EmitLoadAddress (ec);
+               }
+       }
+       
 }