2006-04-08 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / mcs / ecore.cs
index 25be959853383847aa3e5492693d172d81eb6e0b..343c0c7a64b111f90a054b6b8bcf890da728c85c 100644 (file)
@@ -3,6 +3,7 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
+//   Marek Safar (marek.safar@seznam.cz)
 //
 // (C) 2001, 2002, 2003 Ximian, Inc.
 //
@@ -119,7 +120,7 @@ namespace Mono.CSharp {
                        set { type = value; }
                }
 
-               public Location Location {
+               public virtual Location Location {
                        get { return loc; }
                }
 
@@ -134,27 +135,14 @@ namespace Mono.CSharp {
                                Report.Error (error, loc, 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);
-               }
-
                // 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 (Type valueType, out object value)
                {
-                       ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (type);
-                       if (obsolete_attr == null)
-                               return;
-
-                       AttributeTester.Report_ObsoleteMessage (obsolete_attr, type.FullName, loc);
+                       Attribute.Error_AttributeArgumentNotValid (loc);
+                       value = null;
+                       return false;
                }
 
                public virtual string GetSignatureForError ()
@@ -238,7 +226,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,  bool silent)
+               public virtual FullNamedExpression ResolveAsTypeStep (IResolveContext ec,  bool silent)
                {
                        return null;
                }
@@ -248,14 +236,30 @@ 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, silent);
 
-                       if (fne == null)
+                       if (fne == null){
+                               if (!silent && errors == Report.Errors)
+                                       Report.Error (118, loc, "Expecting a type.");
                                return null;
+                       }
 
                        if (fne.eclass != ExprClass.Type) {
                                if (!silent && errors == Report.Errors)
@@ -265,11 +269,12 @@ namespace Mono.CSharp {
 
                        TypeExpr te = fne as TypeExpr;
 
-                       if (!te.CheckAccessLevel (ec.DeclSpace)) {
+                       if (!te.CheckAccessLevel (ec.DeclContainer)) {
                                ErrorIsInaccesible (loc, TypeManager.CSharpName (te.Type));
                                return null;
                        }
 
+                       te.loc = loc;
                        return te;
                }
 
@@ -278,9 +283,51 @@ namespace Mono.CSharp {
                        Report.Error (122, loc, "`{0}' is inaccessible due to its protection level", name);
                }
 
-               public virtual void Error_ValueCannotBeConverted (Location loc, Type t)
+               protected static void Error_CannotAccessProtected (Location loc, MemberInfo m, Type qualifier, Type container)
                {
-                       Convert.Error_CannotImplicitConversion (loc, Type, t);
+                       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));
+                                                        
+                       }
+
+                       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;
+
+                       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;
+                       }
+
+                       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)
@@ -289,28 +336,29 @@ namespace Mono.CSharp {
                                TypeManager.CSharpName (type), name);
                }
 
-               ResolveFlags ExprClassToResolveFlags ()
+               ResolveFlags ExprClassToResolveFlags
                {
-                       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");
+                       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>
@@ -334,10 +382,10 @@ namespace Mono.CSharp {
                                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);
 
@@ -347,7 +395,7 @@ namespace Mono.CSharp {
                        if (e == null)
                                return null;
 
-                       if ((flags & e.ExprClassToResolveFlags ()) == 0) {
+                       if ((flags & e.ExprClassToResolveFlags) == 0) {
                                e.Error_UnexpectedKind (flags, loc);
                                return null;
                        }
@@ -379,23 +427,12 @@ namespace Mono.CSharp {
                public Constant ResolveAsConstant (EmitContext ec, MemberCore mc)
                {
                        Expression e = Resolve (ec);
-                       if (e != 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;
-                                       }
-                               }
-                       }
+                       if (e == null)
+                               return null;
+
+                       Constant c = e as Constant;
+                       if (c != null)
+                               return c;
 
                        Const.Error_ExpressionMustBeConstant (loc, mc.GetSignatureForError ());
                        return null;
@@ -481,33 +518,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)
@@ -517,7 +554,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);
@@ -527,15 +564,14 @@ namespace Mono.CSharp {
                ///   Returns a fully formed expression after a MemberLookup
                /// </summary>
                /// 
-               // TODO: This can be heavily cached
-               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);
                        }
@@ -573,10 +609,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);
                }
 
                //
@@ -584,7 +620,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)
@@ -605,7 +641,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 =
@@ -621,24 +657,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);
                }
 
@@ -664,16 +700,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)
@@ -696,21 +732,16 @@ namespace Mono.CSharp {
                                        if (qualifier_type == null) {
                                                Report.Error (38, loc, "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
                                                              TypeManager.CSharpName (m.DeclaringType),
-                                                             TypeManager.CSharpName (ec.ContainerType));
+                                                             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 {
                                                ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (m));
                                        }
@@ -739,7 +770,7 @@ namespace Mono.CSharp {
                                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;
                        }
 
@@ -805,54 +836,56 @@ 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;
                }
                
-               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 (EmitContext ec, string expected, Location loc)
+               public void Error_UnexpectedKind (DeclSpace ds, string expected, Location loc)
                {
-                       Error_UnexpectedKind (ec, expected, ExprClassName (), loc);
+                       Error_UnexpectedKind (ds, expected, ExprClassName, loc);
                }
 
-               public void Error_UnexpectedKind (EmitContext ec, string expected, string was, Location loc)
+               public void Error_UnexpectedKind (DeclSpace ds, string expected, string was, Location loc)
                {
                        string name = GetSignatureForError ();
-                       if (ec != null)
-                               name = ec.DeclSpace.GetSignatureForError () + '.' + name;
+                       if (ds != null)
+                               name = ds.GetSignatureForError () + '.' + name;
 
                        Report.Error (118, loc, "`{0}' is a `{1}' but a `{2}' was expected",
                              name, was, expected);
@@ -888,7 +921,7 @@ namespace Mono.CSharp {
                        }
 
                        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 ());
                }
                
                public static void UnsafeError (Location loc)
@@ -1025,7 +1058,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);
                                        }
                                }
                        } 
@@ -1101,13 +1134,7 @@ namespace Mono.CSharp {
        ///
        /// </summary>
        public class EmptyCast : Expression {
-               protected Expression child;
-
-               public Expression Child {
-                       get {
-                               return child;
-                       }
-               }               
+               protected readonly Expression child;
 
                public EmptyCast (Expression child, Type return_type)
                {
@@ -1129,6 +1156,12 @@ namespace Mono.CSharp {
                {
                        child.Emit (ec);
                }
+
+               public override bool GetAttributableValue (Type valueType, out object value)
+               {
+                       return child.GetAttributableValue (valueType, out value);
+               }
+
        }
        /// <summary>
        ///     This is a numeric cast to a Decimal
@@ -1137,36 +1170,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)
@@ -1183,49 +1210,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]);
                }
        }
 
@@ -1234,9 +1260,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;
@@ -1273,7 +1300,7 @@ namespace Mono.CSharp {
 
                public override bool IsDefaultValue {
                        get {
-                               throw new NotImplementedException ();
+                               return true;
                        }
                }
 
@@ -1282,6 +1309,15 @@ namespace Mono.CSharp {
                                return false;
                        }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       if (type == target_type)
+                               return child.Reduce (inCheckedContext, target_type);
+
+                       return null;
+               }
+
        }
 
 
@@ -1291,7 +1327,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;
@@ -1311,6 +1348,17 @@ namespace Mono.CSharp {
                        Child.Emit (ec);
                }
 
+               public override bool GetAttributableValue (Type valueType, out object value)
+               {
+                       value = GetTypedValue ();
+                       return true;
+               }
+
+               public override string GetSignatureForError()
+               {
+                       return TypeManager.CSharpName (Type);
+               }
+
                public override object GetValue ()
                {
                        return Child.GetValue ();
@@ -1326,11 +1374,6 @@ namespace Mono.CSharp {
                        return System.Enum.ToObject (type, Child.GetValue ());
                }
                
-               public override void Error_ValueCannotBeConverted (Location loc, Type t)
-               {
-                       Convert.Error_CannotImplicitConversion (loc, Type, t);
-               }
-
                public override string AsString ()
                {
                        return Child.AsString ();
@@ -1387,6 +1430,14 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override Constant Reduce(bool inCheckedContext, Type target_type)
+               {
+                       if (Child.Type == target_type)
+                               return Child;
+
+                       return Child.Reduce (inCheckedContext, target_type);
+               }
+
                public override Constant ToType (Type type, Location loc)
                {
                        if (Type == type) {
@@ -1399,8 +1450,8 @@ namespace Mono.CSharp {
                                return this;
                        }
 
-                       if (!Convert.ImplicitStandardConversionExists (Convert.ConstantEC, this, type)){
-                               Error_ValueCannotBeConverted (loc, type);
+                       if (!Convert.ImplicitStandardConversionExists (this, type)){
+                               Error_ValueCannotBeConverted (loc, type, false);
                                return null;
                        }
 
@@ -1453,6 +1504,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;
@@ -1488,12 +1546,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;
                }
 
@@ -1516,7 +1572,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;
@@ -1783,7 +1839,7 @@ namespace Mono.CSharp {
                {
                        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)
@@ -1802,23 +1858,34 @@ namespace Mono.CSharp {
                        return SimpleNameResolve (ec, null, intermediate);
                }
 
-               public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+               public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
                        int errors = Report.Errors;
-                       FullNamedExpression fne = ec.DeclSpace.LookupType (Name, loc, /*ignore_cs0104=*/ false);
+                       FullNamedExpression fne = ec.DeclContainer.LookupType (Name, loc, /*ignore_cs0104=*/ false);
                        if (fne != null)
                                return fne;
 
                        if (silent || errors != Report.Errors)
                                return null;
 
-                       MemberCore mc = ec.DeclSpace.GetDefinition (Name);
+                       MemberCore mc = ec.DeclContainer.GetDefinition (Name);
                        if (mc != null) {
-                               Error_UnexpectedKind (ec, "type", GetMemberType (mc), loc);
-                       } else {
-                               NamespaceEntry.Error_NamespaceNotFound (loc, Name);
+                               Error_UnexpectedKind (ec.DeclContainer, "type", GetMemberType (mc), loc);
+                               return null;
                        }
 
+                       string ns = ec.DeclContainer.NamespaceEntry.NS.Name;
+                       string fullname = (ns.Length > 0) ? ns + "." + Name : Name;
+                       foreach (Assembly a in RootNamespace.Global.Assemblies) {
+                               Type type = a.GetType (fullname);
+                               if (type != null) {
+                                       Report.SymbolRelatedToPreviousError (type);
+                                       Expression.ErrorIsInaccesible (loc, fullname);
+                                       return null;
+                               }
+                       }
+
+                       NamespaceEntry.Error_NamespaceNotFound (loc, Name);
                        return null;
                }
 
@@ -1832,6 +1899,10 @@ namespace Mono.CSharp {
                                return "indexer";
                        if (mc is FieldBase)
                                return "field";
+                       if (mc is MethodCore)
+                               return "method";
+                       if (mc is EnumMember)
+                               return "enum";
 
                        return "type";
                }
@@ -1904,14 +1975,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;
 
@@ -1924,14 +1995,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, false);
+                               e = ResolveAsTypeStep (ec, true);
                        }
 
                        if (e == null) {
@@ -1939,7 +2010,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;
                        }
 
@@ -1962,7 +2033,7 @@ namespace Mono.CSharp {
                                                if (!me.IsStatic &&
                                                    (!intermediate || !IdenticalNameAndTypeName (ec, me, loc))) {
                                                        Error_ObjectRefRequired (ec, loc, me.GetSignatureForError ());
-                                                       return null;
+                                                       return EmptyExpression.Null;
                                                }
 
                                                //
@@ -2011,7 +2082,7 @@ namespace Mono.CSharp {
 
                        Error (103, "The name `" + Name +
                               "' does not exist in the class `" +
-                              ec.DeclSpace.Name + "'");
+                              ec.DeclContainer.Name + "'");
                }
 
                public override string ToString ()
@@ -2030,7 +2101,7 @@ namespace Mono.CSharp {
        ///   section 10.8.1 (Fully Qualified Names).
        /// </summary>
        public abstract class FullNamedExpression : Expression {
-               public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+               public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
                        return this;
                }
@@ -2041,10 +2112,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, bool silent)
+               override public FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
                        TypeExpr t = DoResolveAsTypeStep (ec);
                        if (t == null)
@@ -2102,16 +2173,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;
-               }
+               protected abstract TypeExpr DoResolveAsTypeStep (IResolveContext ec);
 
                public abstract string Name {
                        get;
@@ -2137,6 +2199,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)
                {
@@ -2145,7 +2210,12 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
+               {
+                       return this;
+               }
+
+               public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
                {
                        return this;
                }
@@ -2164,20 +2234,27 @@ 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;
                }
 
-               static readonly char [] dot_array = { '.' };
-               public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
                {
-                       if (type != null)
-                               return this;
+                       // It's null for corlib compilation only
+                       if (type == null)
+                               return DoResolveAsTypeStep (ec);
 
+                       return this;
+               }
+
+               static readonly char [] dot_array = { '.' };
+               protected 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;
@@ -2187,7 +2264,7 @@ namespace Mono.CSharp {
                                lookup_name = name.Substring (0, pos);
                        }
 
-                       FullNamedExpression resolved = Namespace.Root.Lookup (ec.DeclSpace, lookup_name, Location.Null);
+                       FullNamedExpression resolved = RootNamespace.Global.Lookup (ec.DeclContainer, lookup_name, Location.Null);
 
                        if (resolved != null && rest != null) {
                                // Now handle the rest of the the name.
@@ -2199,13 +2276,13 @@ namespace Mono.CSharp {
                                        Namespace ns = resolved as Namespace;
                                        element = elements [i++];
                                        lookup_name += "." + element;
-                                       resolved = ns.Lookup (ec.DeclSpace, element, Location.Null);
+                                       resolved = ns.Lookup (ec.DeclContainer, element, Location.Null);
                                }
 
                                if (resolved != null && resolved is TypeExpr) {
                                        Type t = ((TypeExpr) resolved).Type;
                                        while (t != null) {
-                                               if (!ec.DeclSpace.CheckAccessLevel (t)) {
+                                               if (!ec.DeclContainer.CheckAccessLevel (t)) {
                                                        resolved = null;
                                                        lookup_name = t.FullName;
                                                        break;
@@ -2225,11 +2302,11 @@ namespace Mono.CSharp {
                        }
 
                        if (!(resolved is TypeExpr)) {
-                               resolved.Error_UnexpectedKind (ec, "type", loc);
+                               resolved.Error_UnexpectedKind (ec.DeclContainer, "type", loc);
                                return null;
                        }
 
-                       type = ((TypeExpr) resolved).ResolveType (ec);
+                       type = resolved.Type;
                        return this;
                }
 
@@ -2261,13 +2338,9 @@ namespace Mono.CSharp {
                        get { return texpr.FullName; }
                }
 
-               public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               protected 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)
@@ -2343,7 +2416,6 @@ namespace Mono.CSharp {
                                      "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,
@@ -2356,7 +2428,7 @@ namespace Mono.CSharp {
 
                        if (left is TypeExpr) {
                                if (!IsStatic) {
-                                       SimpleName.Error_ObjectRefRequired (ec, loc, Name);
+                                       SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
                                        return null;
                                }
 
@@ -2390,7 +2462,7 @@ namespace Mono.CSharp {
                                if (InstanceExpression is IMemoryLocation) {
                                        ((IMemoryLocation) InstanceExpression).AddressOf (ec, AddressOp.LoadStore);
                                } else {
-                                       LocalTemporary t = new LocalTemporary (ec, InstanceExpression.Type);
+                                       LocalTemporary t = new LocalTemporary (InstanceExpression.Type);
                                        InstanceExpression.Emit (ec);
                                        t.Store (ec);
                                        t.AddressOf (ec, AddressOp.Store);
@@ -2638,10 +2710,19 @@ namespace Mono.CSharp {
                public override Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
                                                                SimpleName original)
                {
-                       if (FieldInfo.IsLiteral) {
+                       Type t = FieldInfo.FieldType;
+
+                       if (FieldInfo.IsLiteral || (FieldInfo.IsInitOnly && t == TypeManager.decimal_type)) {
                                IConstant ic = TypeManager.GetConstant (FieldInfo);
                                if (ic == null) {
-                                       ic = new ExternalConstant (FieldInfo);
+                                       if (FieldInfo.IsLiteral) {
+                                               ic = new ExternalConstant (FieldInfo);
+                                       } else {
+                                               ic = ExternalConstant.CreateDecimal (FieldInfo);
+                                               if (ic == null) {
+                                                       return base.ResolveMemberAccess (ec, left, loc, original);
+                                               }
+                                       }
                                        TypeManager.RegisterConstant (FieldInfo, ic);
                                }
 
@@ -2652,26 +2733,13 @@ namespace Mono.CSharp {
                                        return null;
                                }
 
-                               if (ic.ResolveValue ())
-                                       ic.CheckObsoleteness (loc);
+                               if (ic.ResolveValue ()) {
+                                       if (!ec.IsInObsoleteScope)
+                                               ic.CheckObsoleteness (loc);
+                               }
 
                                return ic.Value;
                        }
-
-                       bool is_emitted = FieldInfo is FieldBuilder;
-                       Type t = FieldInfo.FieldType;
-                       
-                       //
-                       // 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.
-                       //
-                       //TODO: incorporare in GetContant otherwise we miss all error checks + obsoleteness check
-                       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 (t.IsPointer && !ec.InUnsafe) {
                                UnsafeError (loc);
@@ -2682,6 +2750,11 @@ 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) {
@@ -2715,17 +2788,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) {
-                                       f.CheckObsoleteness (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)) {                                
@@ -2762,16 +2845,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 = "A readonly field cannot be assigned to (except in a constructor or a variable initializer)";
-                       else
+                       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)
@@ -2780,17 +2879,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))) {
-                               Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable",
-                                       InstanceExpression.GetSignatureForError ());
-                               return null;
-                       }
-
                        FieldBase fb = TypeManager.GetField (FieldInfo);
                        if (fb != null)
                                fb.SetAssigned ();
@@ -2799,18 +2894,21 @@ namespace Mono.CSharp {
                                return this;
 
                        //
-                       // InitOnly fields can only be assigned in constructors
+                       // InitOnly fields can only be assigned in constructors or initializers
                        //
 
+                       if (ec.IsFieldInitializer)
+                               return this;
+
                        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;
                }
@@ -2858,15 +2956,13 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
                        bool is_volatile = false;
 
-                       if (FieldInfo is FieldBuilder){
-                               FieldBase f = TypeManager.GetField (FieldInfo);
-                               if (f != null){
-                                       if ((f.ModFlags & Modifiers.VOLATILE) != 0)
-                                               is_volatile = true;
-                                       
-                                       f.SetMemberIsUsed ();
-                               }
-                       } 
+                       FieldBase f = TypeManager.GetField (FieldInfo);
+                       if (f != null){
+                               if ((f.ModFlags & Modifiers.VOLATILE) != 0)
+                                       is_volatile = true;
+
+                               f.SetMemberIsUsed ();
+                       }
                        
                        if (FieldInfo.IsStatic){
                                if (is_volatile)
@@ -2894,7 +2990,7 @@ namespace Mono.CSharp {
                        if (leave_copy) {       
                                ec.ig.Emit (OpCodes.Dup);
                                if (!FieldInfo.IsStatic) {
-                                       temp = new LocalTemporary (ec, this.Type);
+                                       temp = new LocalTemporary (this.Type);
                                        temp.Store (ec);
                                }
                        }
@@ -2909,7 +3005,7 @@ namespace Mono.CSharp {
                        prepared = prepare_for_load;
 
                        if (is_readonly && !ec.IsConstructor){
-                               Report_AssignToReadonly (!is_static);
+                               Report_AssignToReadonly (source);
                                return;
                        }
 
@@ -2919,7 +3015,7 @@ namespace Mono.CSharp {
                        if (leave_copy) {
                                ec.ig.Emit (OpCodes.Dup);
                                if (!FieldInfo.IsStatic) {
-                                       temp = new LocalTemporary (ec, this.Type);
+                                       temp = new LocalTemporary (this.Type);
                                        temp.Store (ec);
                                }
                        }
@@ -3042,7 +3138,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;
@@ -3051,7 +3147,7 @@ namespace Mono.CSharp {
 
                        type = TypeManager.TypeToCoreType (pi.PropertyType);
 
-                       ResolveAccessors (ec);
+                       ResolveAccessors (containerType);
                }
 
                public override string Name {
@@ -3085,7 +3181,7 @@ namespace Mono.CSharp {
 
                void FindAccessors (Type invocation_type)
                {
-                       BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
+                       const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
                                BindingFlags.Static | BindingFlags.Instance |
                                BindingFlags.DeclaredOnly;
 
@@ -3121,9 +3217,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);
@@ -3144,7 +3241,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;
@@ -3156,24 +3253,21 @@ 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;
@@ -3205,7 +3299,7 @@ namespace Mono.CSharp {
                                return this;
 
                        if (getter != null){
-                               if (TypeManager.GetArgumentTypes (getter).Length != 0){
+                               if (TypeManager.GetParameterData (getter).Count != 0){
                                        Error_PropertyNotFound (getter, true);
                                        return null;
                                }
@@ -3242,7 +3336,7 @@ namespace Mono.CSharp {
                                return null;
                        }
                        
-                       if (!InstanceResolve (ec, must_do_cs1540_check))
+                       if (!InstanceResolve (ec, false, must_do_cs1540_check))
                                return null;
 
                        //
@@ -3265,6 +3359,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
@@ -3274,13 +3380,12 @@ namespace Mono.CSharp {
                                //
                                if (getter == null)
                                        return null;
-                               
-                               Report.Error (200, loc, " Property or indexer `{0}' cannot be assigned to (it is read only)",
-                                             TypeManager.GetFullNameSignature (PropertyInfo));
+                               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){
+                       if (TypeManager.GetParameterData (setter).Count != 1){
                                Error_PropertyNotFound (setter, false);
                                return null;
                        }
@@ -3298,7 +3403,7 @@ namespace Mono.CSharp {
                                return null;
                        }
                        
-                       if (!InstanceResolve (ec, must_do_cs1540_check))
+                       if (!InstanceResolve (ec, PropertyInfo.DeclaringType.IsValueType, must_do_cs1540_check))
                                return null;
                        
                        //
@@ -3309,15 +3414,6 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       //
-                       // Check that we are not making changes to a temporary memory location
-                       //
-                       if (InstanceExpression != null && InstanceExpression.Type.IsValueType && !(InstanceExpression is IMemoryLocation)) {
-                               Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable",
-                                       InstanceExpression.GetSignatureForError ());
-                               return null;
-                       }
-
                        return this;
                }
                
@@ -3328,9 +3424,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
                        //
@@ -3343,21 +3436,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 (this.Type);
+                                       temp.Store (ec);
+                               }
                        }
                }
 
@@ -3366,23 +3460,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 (this.Type);
+                                               temp.Store (ec);
+                                       }
+                               }
+                       } else if (leave_copy) {
+                               source.Emit (ec);
                                if (!is_static) {
-                                       temp = new LocalTemporary (ec, this.Type);
+                                       temp = new LocalTemporary (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);
@@ -3454,7 +3557,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!!");
@@ -3491,12 +3594,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)) {
-                                       ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo));
-                                       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;
@@ -3554,4 +3656,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);
+               }
+       }
+       
 }