**** Merged from MCS ****
[mono.git] / mcs / gmcs / ecore.cs
index 0b2d62e9e29584f1f1fd3640d5af1e0506924926..743b255c62697aaa352b5ec0018ebccd130bb2b8 100755 (executable)
@@ -61,7 +61,10 @@ namespace Mono.CSharp {
 
                // Disable control flow analysis while resolving the expression.
                // This is used when resolving the instance expression of a field expression.
-               DisableFlowAnalysis     = 16
+               DisableFlowAnalysis     = 16,
+
+               // Set if this is resolving the first part of a MemberAccess.
+               Intermediate            = 32
        }
 
        //
@@ -203,22 +206,21 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Utility wrapper routine for Warning, just to beautify the code
                /// </summary>
-               public void Warning (int warning, string s)
+               public void Warning (int code, string format, params object[] args)
                {
-                       if (!Location.IsNull (loc))
-                               Report.Warning (warning, loc, s);
-                       else
-                               Report.Warning (warning, s);
+                       Report.Warning (code, loc, format, args);
                }
 
                /// <summary>
-               ///   Utility wrapper routine for Warning, only prints the warning if
-               ///   warnings of level `level' are enabled.
+               /// Tests presence of ObsoleteAttribute and report proper error
                /// </summary>
-               public void Warning (int warning, int level, string s)
+               protected void CheckObsoleteAttribute (Type type)
                {
-                       if (level <= RootContext.WarningLevel)
-                               Warning (warning, s);
+                       ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (type);
+                       if (obsolete_attr == null)
+                               return;
+
+                       AttributeTester.Report_ObsoleteMessage (obsolete_attr, type.FullName, loc);
                }
 
                /// <summary>
@@ -293,8 +295,10 @@ namespace Mono.CSharp {
                                ec.DoFlowAnalysis = false;
 
                        Expression e;
+                       bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate;
                        if (this is SimpleName)
-                               e = ((SimpleName) this).DoResolveAllowStatic (ec);
+                               e = ((SimpleName) this).DoResolveAllowStatic (ec, intermediate);
+
                        else 
                                e = DoResolve (ec);
 
@@ -515,6 +519,9 @@ namespace Mono.CSharp {
                        return null;
                }
 
+
+               private static ArrayList almostMatchedMembers = new ArrayList (4);
+
                //
                // FIXME: Probably implement a cache for (t,name,current_access_set)?
                //
@@ -559,8 +566,11 @@ namespace Mono.CSharp {
                                                       string name, MemberTypes mt,
                                                       BindingFlags bf, Location loc)
                {
+                       almostMatchedMembers.Clear ();
+
                        MemberInfo [] mi = TypeManager.MemberLookup (
-                               container_type, qualifier_type,queried_type, mt, bf, name);
+                               container_type, qualifier_type,queried_type, mt, bf, name,
+                               almostMatchedMembers);
 
                        if (mi == null)
                                return null;
@@ -636,25 +646,50 @@ namespace Mono.CSharp {
                        e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
                                          name, mt, bf, loc);
 
-                       if (e != null)
-                               return e;
-
-                       // Error has already been reported.
-                       if (errors < Report.Errors)
-                               return null;
+                       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, loc);
 
-                       MemberLookupFailed (ec, qualifier_type, queried_type, name,
-                                           null, loc);
-                       return null;
+                       return e;
                }
 
                public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
                                                       Type queried_type, string name,
                                                       string class_name, Location loc)
                {
+                       if (almostMatchedMembers.Count != 0) {
+                               if (qualifier_type == null) {
+                                       foreach (MemberInfo m in almostMatchedMembers)
+                                               Report.Error (38, loc, 
+                                                             "Cannot access non-static member `{0}' via nested type `{1}'", 
+                                                             TypeManager.GetFullNameSignature (m),
+                                                             TypeManager.CSharpName (ec.ContainerType));
+                                       return;
+                               }
+
+
+                               if (qualifier_type != ec.ContainerType) {
+                                       // 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 parent of the
+                                       // ec.ContainerType and the lookup succeeds with the latter one,
+                                       // then we are in this situation.
+                                       foreach (MemberInfo m in almostMatchedMembers)
+                                               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));
+                                       return;
+                               }
+                               almostMatchedMembers.Clear ();
+                       }
+
                        MemberInfo[] mi = TypeManager.MemberLookup (queried_type, null, queried_type,
                                                                    AllMemberTypes, AllBindingFlags |
-                                                                   BindingFlags.NonPublic, name);
+                                                                   BindingFlags.NonPublic, name, null);
 
                        if (mi == null) {
                                if (class_name != null)
@@ -669,7 +704,7 @@ namespace Mono.CSharp {
 
                        if (TypeManager.MemberLookup (queried_type, null, queried_type,
                                                      AllMemberTypes, AllBindingFlags |
-                                                     BindingFlags.NonPublic, name) == null) {
+                                                     BindingFlags.NonPublic, name, null) == null) {
                                if ((mi.Length == 1) && (mi [0] is Type)) {
                                        Type t = (Type) mi [0];
 
@@ -682,42 +717,15 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if ((qualifier_type != null) && (qualifier_type != ec.ContainerType) &&
-                           ec.ContainerType.IsSubclassOf (qualifier_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 parent of the
-                               // ec.ContainerType and the lookup succeeds with the latter one,
-                               // then we are in this situation.
 
-                               mi = TypeManager.MemberLookup (
-                                       ec.ContainerType, ec.ContainerType, ec.ContainerType,
-                                       AllMemberTypes, AllBindingFlags, name);
-
-                               if (mi != null) {
-                                       Report.Error (
-                                               1540, loc, "Cannot access protected member `" +
-                                               TypeManager.CSharpName (qualifier_type) + "." +
-                                               name + "' " + "via a qualifier of type `" +
-                                               TypeManager.CSharpName (qualifier_type) + "'; the " +
-                                               "qualifier must be of type `" +
-                                               TypeManager.CSharpName (ec.ContainerType) + "' " +
-                                               "(or derived from it)");
-                                       return;
-                               }
-                       }
 
                        if (qualifier_type != null)
-                               Report.Error (
-                                       122, loc, "`" + TypeManager.CSharpName (qualifier_type) + "." +
-                                       name + "' is inaccessible due to its protection level");
+                               Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", TypeManager.CSharpName (qualifier_type) + "." + name);
                        else if (name == ".ctor") {
                                Report.Error (143, loc, String.Format ("The type {0} has no constructors defined",
                                                                       TypeManager.CSharpName (queried_type)));
                        } else {
-                               Report.Error (
-                                       122, loc, "`" + name + "' is inaccessible due to its " +
-                                       "protection level");
+                               Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", name);
                }
                }
 
@@ -1259,13 +1267,6 @@ namespace Mono.CSharp {
                                return 0;
                }
 
-               //
-               // Default implementation of IAssignMethod.CacheTemporaries
-               //
-               public void CacheTemporaries (EmitContext ec)
-               {
-               }
-
                static void Error_NegativeArrayIndex (Location loc)
                {
                        Report.Error (284, loc, "Can not create array with a negative size");
@@ -2010,18 +2011,18 @@ namespace Mono.CSharp {
                
                public override Expression DoResolve (EmitContext ec)
                {
-                       return SimpleNameResolve (ec, null, false);
+                       return SimpleNameResolve (ec, null, false, false);
                }
 
                public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
-                       return SimpleNameResolve (ec, right_side, false);
+                       return SimpleNameResolve (ec, right_side, false, false);
                }
                
 
-               public Expression DoResolveAllowStatic (EmitContext ec)
+               public Expression DoResolveAllowStatic (EmitContext ec, bool intermediate)
                {
-                       return SimpleNameResolve (ec, null, true);
+                       return SimpleNameResolve (ec, null, true, intermediate);
                }
 
                public override Expression ResolveAsTypeStep (EmitContext ec)
@@ -2088,6 +2089,33 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               Expression SimpleNameResolve (EmitContext ec, Expression right_side,
+                                             bool allow_static, bool intermediate)
+               {
+                       Expression e = DoSimpleNameResolve (ec, right_side, allow_static, intermediate);
+                       if (e == null)
+                               return null;
+
+                       Block current_block = ec.CurrentBlock;
+                       if (current_block != null){
+                               LocalInfo vi = current_block.GetLocalInfo (Name);
+                               if (is_base &&
+                                   current_block.IsVariableNameUsedInChildBlock(Name)) {
+                                       Report.Error (135, Location,
+                                                     "'{0}' has a different meaning in a " +
+                                                     "child block", Name);
+                                       return null;
+                               }
+                       }
+
+                       if (e.Type != null && e.Type.IsPointer && !ec.InUnsafe) {
+                               UnsafeError (loc);
+                               return null;
+                       }
+
+                       return e;
+               }
+
                /// <remarks>
                ///   7.5.2: Simple Names. 
                ///
@@ -2105,7 +2133,7 @@ namespace Mono.CSharp {
                ///   Type is both an instance variable and a Type;  Type.GetType
                ///   is the static method not an instance method of type.
                /// </remarks>
-               Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
+               Expression DoSimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static, bool intermediate)
                {
                        Expression e = null;
 
@@ -2114,11 +2142,6 @@ namespace Mono.CSharp {
                        //
                        Block current_block = ec.CurrentBlock;
                        if (current_block != null){
-                               if (is_base && current_block.IsVariableNameUsedInChildBlock(Name)) {
-                                       Report.Error (135, Location, "'" + Name + "' has a different meaning in a child block");
-                                       return null;
-                               }
-
                                LocalInfo vi = current_block.GetLocalInfo (Name);
                                if (vi != null){
                                        Expression var;
@@ -2219,24 +2242,24 @@ namespace Mono.CSharp {
 
                                // This fails if ResolveMemberAccess() was unable to decide whether
                                // it's a field or a type of the same name.
+                               
                                if (!me.IsStatic && (me.InstanceExpression == null))
                                        return e;
 
                                if (!me.IsStatic &&
-                                   TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType) &&
-                                   !me.InstanceExpression.Type.IsSubclassOf (me.DeclaringType)) {
+                                   TypeManager.IsSubclassOrNestedChildOf (me.InstanceExpression.Type, me.DeclaringType) &&
+                                   me.InstanceExpression.Type != me.DeclaringType &&
+                                   !TypeManager.IsSubclassOf (me.InstanceExpression.Type, me.DeclaringType) &&
+                                   (!intermediate || !MemberAccess.IdenticalNameAndTypeName (ec, this, e, loc))) {
                                        Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
                                               "outer type `" + me.DeclaringType + "' via nested type `" +
                                               me.InstanceExpression.Type + "'");
                                        return null;
                                }
 
-                               if (right_side != null)
-                                       e = e.DoResolveLValue (ec, right_side);
-                               else
-                                       e = e.DoResolve (ec);
-
-                               return e;                               
+                               return (right_side != null)
+                                       ? e.DoResolveLValue (ec, right_side)
+                                       : e.DoResolve (ec);
                        }
 
                        if (ec.IsStatic || ec.IsFieldInitializer){
@@ -2335,11 +2358,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               public virtual TypeExpr[] GetInterfaces ()
-               {
-                       return TypeManager.GetInterfaces (Type);
-               }
-
                public abstract TypeExpr DoResolveAsTypeStep (EmitContext ec);
 
                public virtual Type ResolveType (EmitContext ec)
@@ -2447,6 +2465,16 @@ namespace Mono.CSharp {
                }
        }
 
+       /// <summary>
+       ///   Represents an "unbound generic type", ie. typeof (Foo<>).
+       ///   See 14.5.11.
+       /// </summary>
+       public class UnboundTypeExpression : TypeLookupExpression {
+               public UnboundTypeExpression (string name)
+                       : base (name)
+               { }
+       }
+
        public class TypeAliasExpression : TypeExpr, IAlias {
                TypeExpr texpr;
                TypeArguments args;
@@ -2550,6 +2578,8 @@ namespace Mono.CSharp {
                Expression instance_expression = null;
                bool is_explicit_impl = false;
                bool has_type_arguments = false;
+               bool identical_type_name = false;
+               bool is_base;
                
                public MethodGroupExpr (MemberInfo [] mi, Location l)
                {
@@ -2624,10 +2654,29 @@ namespace Mono.CSharp {
                        }
                }
 
+               public bool IdenticalTypeName {
+                       get {
+                               return identical_type_name;
+                       }
+
+                       set {
+                               identical_type_name = value;
+                       }
+               }
+
+               public bool IsBase {
+                       get {
+                               return is_base;
+                       }
+                       set {
+                               is_base = value;
+                       }
+               }
+
                public string Name {
                        get {
-                                return TypeManager.CSharpSignature (
-                                       Methods [Methods.Length - 1]);
+                               //return Methods [0].Name;
+                                return Methods [Methods.Length - 1].Name;
                        }
                }
 
@@ -2770,6 +2819,10 @@ namespace Mono.CSharp {
                Expression instance_expr;
                VariableInfo variable_info;
                
+               LocalTemporary temp;
+               bool prepared;
+               bool is_field_initializer;
+               
                public FieldExpr (FieldInfo fi, Location l)
                {
                        FieldInfo = fi;
@@ -2812,6 +2865,16 @@ namespace Mono.CSharp {
                        }
                }
 
+               public bool IsFieldInitializer {
+                       get {
+                               return is_field_initializer;
+                       }
+
+                       set {
+                               is_field_initializer = value;
+                       }
+               }
+
                public VariableInfo VariableInfo {
                        get {
                                return variable_info;
@@ -2839,6 +2902,19 @@ namespace Mono.CSharp {
                                        return null;
                        }
 
+                       ObsoleteAttribute oa;
+                       FieldBase f = TypeManager.GetField (FieldInfo);
+                       if (f != null) {
+                               oa = f.GetObsoleteAttribute (ec.DeclSpace);
+                               if (oa != null)
+                                       AttributeTester.Report_ObsoleteMessage (oa, f.GetSignatureForError (), loc);
+                               // To be sure that type is external because we do not register generated fields
+                        } else if (!(FieldInfo.DeclaringType is TypeBuilder)) {                                
+                               oa = AttributeTester.GetMemberObsoleteAttribute (FieldInfo);
+                               if (oa != null)
+                                       AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (FieldInfo), loc);
+                       }
+
                        // If the instance expression is a local variable or parameter.
                        IVariable var = instance_expr as IVariable;
                        if ((var == null) || (var.VariableInfo == null))
@@ -2899,7 +2975,8 @@ namespace Mono.CSharp {
                                        Report_AssignToReadonly (false);
 
                                Type ctype;
-                               if (ec.TypeContainer.CurrentType != null)
+                               if (!is_field_initializer &&
+                                   (ec.TypeContainer.CurrentType != null))
                                        ctype = ec.TypeContainer.CurrentType.ResolveType (ec);
                                else
                                        ctype = ec.ContainerType;
@@ -2921,8 +2998,8 @@ namespace Mono.CSharp {
 
                        return true;
                }
-
-               override public void Emit (EmitContext ec)
+               
+               public void Emit (EmitContext ec, bool leave_copy)
                {
                        ILGenerator ig = ec.ig;
                        bool is_volatile = false;
@@ -2942,57 +3019,52 @@ namespace Mono.CSharp {
                                        ig.Emit (OpCodes.Volatile);
                                
                                ig.Emit (OpCodes.Ldsfld, FieldInfo);
-                               return;
-                       }
-                       
-                       if (instance_expr.Type.IsValueType){
-                               IMemoryLocation ml;
-                               LocalTemporary tempo = null;
-                               
-                               if (!(instance_expr is IMemoryLocation)){
-                                       tempo = new LocalTemporary (ec, instance_expr.Type);
-                                       
-                                       InstanceExpression.Emit (ec);
-                                       tempo.Store (ec);
-                                       ml = tempo;
-                               } else
-                                       ml = (IMemoryLocation) instance_expr;
-                               
-                               ml.AddressOf (ec, AddressOp.Load);
                        } else {
-                               instance_expr.Emit (ec);
+                               if (!prepared)
+                                       EmitInstance (ec);
+
+                               if (is_volatile)
+                                       ig.Emit (OpCodes.Volatile);
+
+                               ig.Emit (OpCodes.Ldfld, FieldInfo);
+                       }
+
+                       if (leave_copy) {
+                               ec.ig.Emit (OpCodes.Dup);
+                               if (!FieldInfo.IsStatic) {
+                                       temp = new LocalTemporary (ec, this.Type);
+                                       temp.Store (ec);
+                               }
                        }
-                       if (is_volatile)
-                               ig.Emit (OpCodes.Volatile);
-                       
-                       ig.Emit (OpCodes.Ldfld, FieldInfo);
                }
 
-               public void EmitAssign (EmitContext ec, Expression source)
+               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
                        FieldAttributes fa = FieldInfo.Attributes;
                        bool is_static = (fa & FieldAttributes.Static) != 0;
                        bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
                        ILGenerator ig = ec.ig;
+                       prepared = prepare_for_load;
 
                        if (is_readonly && !ec.IsConstructor){
                                Report_AssignToReadonly (!is_static);
                                return;
                        }
 
-                       if (!is_static){
-                               Expression instance = instance_expr;
-
-                               if (instance.Type.IsValueType){
-                                       IMemoryLocation ml = (IMemoryLocation) instance;
-
-                                       ml.AddressOf (ec, AddressOp.Store);
-                               } else {
-                                       instance.Emit (ec);
-                               }
+                       if (!is_static) {
+                               EmitInstance (ec);
+                               if (prepare_for_load)
+                                       ig.Emit (OpCodes.Dup);
                        }
 
                        source.Emit (ec);
+                       if (leave_copy) {
+                               ec.ig.Emit (OpCodes.Dup);
+                               if (!FieldInfo.IsStatic) {
+                                       temp = new LocalTemporary (ec, this.Type);
+                                       temp.Store (ec);
+                               }
+                       }
 
                        if (FieldInfo is FieldBuilder){
                                FieldBase f = TypeManager.GetField (FieldInfo);
@@ -3008,6 +3080,29 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Stsfld, FieldInfo);
                        else 
                                ig.Emit (OpCodes.Stfld, FieldInfo);
+                       
+                       if (temp != null)
+                               temp.Emit (ec);
+               }
+
+               void EmitInstance (EmitContext ec)
+               {
+                       if (instance_expr.Type.IsValueType) {
+                               if (instance_expr is IMemoryLocation) {
+                                       ((IMemoryLocation) instance_expr).AddressOf (ec, AddressOp.LoadStore);
+                               } else {
+                                       LocalTemporary t = new LocalTemporary (ec, instance_expr.Type);
+                                       instance_expr.Emit (ec);
+                                       t.Store (ec);
+                                       t.AddressOf (ec, AddressOp.Store);
+                               }
+                       } else
+                               instance_expr.Emit (ec);
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       Emit (ec, false);
                }
                
                public void AddressOf (EmitContext ec, AddressOp mode)
@@ -3059,31 +3154,7 @@ namespace Mono.CSharp {
                        if (FieldInfo.IsStatic){
                                ig.Emit (OpCodes.Ldsflda, FieldInfo);
                        } else {
-                               //
-                               // In the case of `This', we call the AddressOf method, which will
-                               // only load the pointer, and not perform an Ldobj immediately after
-                               // the value has been loaded into the stack.
-                               //
-                               if (instance_expr is This)
-                                       ((This)instance_expr).AddressOf (ec, AddressOp.LoadStore);
-                               else if (instance_expr.Type.IsValueType && instance_expr is IMemoryLocation){
-                                       IMemoryLocation ml = (IMemoryLocation) instance_expr;
-
-                                       ml.AddressOf (ec, AddressOp.LoadStore);
-                               } else {
-                                       instance_expr.Emit (ec);
-
-                                       if (instance_expr.Type.IsValueType) {
-                                               LocalBuilder local = ig.DeclareLocal (instance_expr.Type);
-                                               ig.Emit(OpCodes.Stloc, local);
-                                               ig.Emit(OpCodes.Ldloca, local);
-                                               ig.Emit(OpCodes.Ldfld, FieldInfo);
-                                               LocalBuilder local2 = ig.DeclareLocal(type);
-                                               ig.Emit(OpCodes.Stloc, local2);
-                                               ig.Emit(OpCodes.Ldloca, local2);
-                                               return;
-                                       }
-                               }
+                               EmitInstance (ec);
                                ig.Emit (OpCodes.Ldflda, FieldInfo);
                        }
                }
@@ -3122,6 +3193,8 @@ namespace Mono.CSharp {
                bool must_do_cs1540_check;
                
                Expression instance_expr;
+               LocalTemporary temp;
+               bool prepared;
 
                public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
                {
@@ -3194,7 +3267,7 @@ namespace Mono.CSharp {
                        for (; current != null; current = current.BaseType) {
                                MemberInfo[] group = TypeManager.MemberLookup (
                                        invocation_type, invocation_type, current,
-                                       MemberTypes.Property, flags, PropertyInfo.Name);
+                                       MemberTypes.Property, flags, PropertyInfo.Name, null);
 
                                if (group == null)
                                        continue;
@@ -3299,9 +3372,7 @@ namespace Mono.CSharp {
                                is_static = true;
 
                        if (setter == null && getter == null){
-                               Error (122, "`" + PropertyInfo.Name + "' " +
-                                      "is inaccessible because of its protection level");
-                               
+                               Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", PropertyInfo.Name);
                        }
                }
 
@@ -3417,11 +3488,53 @@ namespace Mono.CSharp {
                                              PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
                                return null;
                        }
+
+                       //
+                       // Check that we are not making changes to a temporary memory location
+                       //
+                       if (instance_expr != null && instance_expr.Type.IsValueType && !(instance_expr is IMemoryLocation)) {
+                               // FIXME: Provide better error reporting.
+                               Error (1612, "Cannot modify expression because it is not a variable.");
+                               return null;
+                       }
+
                        return this;
                }
 
-               override public void Emit (EmitContext ec)
+
+               
+               public override void Emit (EmitContext ec)
+               {
+                       Emit (ec, false);
+               }
+               
+               void EmitInstance (EmitContext ec)
+               {
+                       if (is_static)
+                               return;
+
+                       if (instance_expr.Type.IsValueType) {
+                               if (instance_expr is IMemoryLocation) {
+                                       ((IMemoryLocation) instance_expr).AddressOf (ec, AddressOp.LoadStore);
+                               } else {
+                                       LocalTemporary t = new LocalTemporary (ec, instance_expr.Type);
+                                       instance_expr.Emit (ec);
+                                       t.Store (ec);
+                                       t.AddressOf (ec, AddressOp.Store);
+                               }
+                       } else
+                               instance_expr.Emit (ec);
+                       
+                       if (prepared)
+                               ec.ig.Emit (OpCodes.Dup);
+               }
+
+               
+               public void Emit (EmitContext ec, bool leave_copy)
                {
+                       if (!prepared)
+                               EmitInstance (ec);
+                       
                        //
                        // Special case: length of single dimension array property is turned into ldlen
                        //
@@ -3433,28 +3546,50 @@ namespace Mono.CSharp {
                                // System.Array.Length can be called, but the Type does not
                                // support invoking GetArrayRank, so test for that case first
                                //
-                               if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
-                                       instance_expr.Emit (ec);
+                               if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)) {
                                        ec.ig.Emit (OpCodes.Ldlen);
                                        ec.ig.Emit (OpCodes.Conv_I4);
                                        return;
                                }
                        }
 
-                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
+                       Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), getter, null, loc);
+                       
+                       if (!leave_copy)
+                               return;
                        
+                       ec.ig.Emit (OpCodes.Dup);
+                       if (!is_static) {
+                               temp = new LocalTemporary (ec, this.Type);
+                               temp.Store (ec);
+                       }
                }
 
                //
                // Implements the IAssignMethod interface for assignments
                //
-               public void EmitAssign (EmitContext ec, Expression source)
+               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
-                       Argument arg = new Argument (source, Argument.AType.Expression);
-                       ArrayList args = new ArrayList ();
+                       prepared = prepare_for_load;
+                       
+                       EmitInstance (ec);
 
-                       args.Add (arg);
-                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
+                       source.Emit (ec);
+                       if (leave_copy) {
+                               ec.ig.Emit (OpCodes.Dup);
+                               if (!is_static) {
+                                       temp = new LocalTemporary (ec, this.Type);
+                                       temp.Store (ec);
+                               }
+                       }
+                       
+                       ArrayList args = new ArrayList (1);
+                       args.Add (new Argument (new EmptyAddressOf (), Argument.AType.Expression));
+                       
+                       Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), setter, args, loc);
+                       
+                       if (temp != null)
+                               temp.Emit (ec);
                }
 
                override public void EmitStatement (EmitContext ec)
@@ -3469,7 +3604,7 @@ namespace Mono.CSharp {
        /// </summary>
        public class EventExpr : Expression, IMemberExpr {
                public readonly EventInfo EventInfo;
-               public Expression instance_expr;
+               Expression instance_expr;
 
                bool is_static;
                MethodInfo add_accessor, remove_accessor;