2006-05-06 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / mcs / assign.cs
index 6b62f6d8988bf644c24e7b2a520e83762fed1f3d..dabe1c75d39e01ae4589ed17ebf5baefcec49d9c 100644 (file)
@@ -35,7 +35,7 @@ namespace Mono.CSharp {
                // end of the code generated for EmitAssign
                //
                void Emit (EmitContext ec, bool leave_copy);
-               
+
                //
                // This method does the assignment
                // `source' will be stored into the location specified by `this'
@@ -45,17 +45,17 @@ namespace Mono.CSharp {
                // for expressions like a [f ()] ++, where you can't call `f ()' twice.
                //
                void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load);
-               
+
                /*
                For simple assignments, this interface is very simple, EmitAssign is called with source
                as the source expression and leave_copy and prepare_for_load false.
-               
+
                For compound assignments it gets complicated.
-               
+
                EmitAssign will be called as before, however, prepare_for_load will be
                true. The @source expression will contain an expression
                which calls Emit. So, the calls look like:
-               
+
                this.EmitAssign (ec, source, false, true) ->
                        source.Emit (ec); ->
                                [...] ->
@@ -64,89 +64,89 @@ namespace Mono.CSharp {
                                end [...]
                        end source.Emit (ec);
                end this.EmitAssign (ec, source, false, true)
-               
-               
+
+
                When prepare_for_load is true, EmitAssign emits a `token' on the stack that
                Emit will use for its state.
-               
+
                Let's take FieldExpr as an example. assume we are emitting f ().y += 1;
-               
+
                Here is the call tree again. This time, each call is annotated with the IL
                it produces:
-               
+
                this.EmitAssign (ec, source, false, true)
                        call f
                        dup
-                       
+
                        Binary.Emit ()
                                this.Emit (ec, false);
                                ldfld y
                                end this.Emit (ec, false);
-                               
+
                                IntConstant.Emit ()
                                ldc.i4.1
                                end IntConstant.Emit
-                               
+
                                add
                        end Binary.Emit ()
-                       
+
                        stfld
                end this.EmitAssign (ec, source, false, true)
-               
+
                Observe two things:
                        1) EmitAssign left a token on the stack. It was the result of f ().
                        2) This token was used by Emit
-               
+
                leave_copy (in both EmitAssign and Emit) tells the compiler to leave a copy
                of the expression at that point in evaluation. This is used for pre/post inc/dec
                and for a = x += y. Let's do the above example with leave_copy true in EmitAssign
-               
+
                this.EmitAssign (ec, source, true, true)
                        call f
                        dup
-                       
+
                        Binary.Emit ()
                                this.Emit (ec, false);
                                ldfld y
                                end this.Emit (ec, false);
-                               
+
                                IntConstant.Emit ()
                                ldc.i4.1
                                end IntConstant.Emit
-                               
+
                                add
                        end Binary.Emit ()
-                       
+
                        dup
                        stloc temp
                        stfld
                        ldloc temp
                end this.EmitAssign (ec, source, true, true)
-               
+
                And with it true in Emit
-               
+
                this.EmitAssign (ec, source, false, true)
                        call f
                        dup
-                       
+
                        Binary.Emit ()
                                this.Emit (ec, true);
                                ldfld y
                                dup
                                stloc temp
                                end this.Emit (ec, true);
-                               
+
                                IntConstant.Emit ()
                                ldc.i4.1
                                end IntConstant.Emit
-                               
+
                                add
                        end Binary.Emit ()
-                       
+
                        stfld
                        ldloc temp
                end this.EmitAssign (ec, source, false, true)
-               
+
                Note that these two examples are what happens for ++x and x++, respectively.
                */
        }
@@ -164,7 +164,7 @@ namespace Mono.CSharp {
        ///   code to access this value, return its address or save its value.
        ///
        ///   If `is_address' is true, then the value that we store is the address to the
-       ///   real value, and not the value itself. 
+       ///   real value, and not the value itself.
        ///
        ///   This is needed for a value type, because otherwise you just end up making a
        ///   copy of the value on the stack and modifying it. You really need a pointer
@@ -178,15 +178,13 @@ namespace Mono.CSharp {
        public class LocalTemporary : Expression, IMemoryLocation {
                LocalBuilder builder;
                bool is_address;
-               
-               public LocalTemporary (EmitContext ec, Type t) : this (ec, t, false) {}
-                       
-               public LocalTemporary (EmitContext ec, Type t, bool is_address) 
+
+               public LocalTemporary (Type t) : this (t, false) {}
+
+               public LocalTemporary (Type t, bool is_address)
                {
                        type = t;
                        eclass = ExprClass.Value;
-                       loc = Location.Null;
-                       builder = ec.GetTemporaryLocal (is_address ? TypeManager.GetReferenceType (t): t);
                        this.is_address = is_address;
                }
 
@@ -203,7 +201,7 @@ namespace Mono.CSharp {
                        ec.FreeTemporaryLocal (builder, type);
                        builder = null;
                }
-               
+
                public override Expression DoResolve (EmitContext ec)
                {
                        return this;
@@ -212,7 +210,7 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
-                       
+
                        ig.Emit (OpCodes.Ldloc, builder);
                        // we need to copy from the pointer
                        if (is_address)
@@ -225,15 +223,21 @@ namespace Mono.CSharp {
                public void Store (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
+                       if (builder == null)
+                               builder = ec.GetTemporaryLocal (is_address ? TypeManager.GetReferenceType (type): type);
+
                        ig.Emit (OpCodes.Stloc, builder);
                }
 
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
+                       if (builder == null)
+                               builder = ec.GetTemporaryLocal (is_address ? TypeManager.GetReferenceType (type): type);
+
                        // if is_address, than this is just the address anyways,
                        // so we just return this.
                        ILGenerator ig = ec.ig;
-                               
+
                        if (is_address)
                                ig.Emit (OpCodes.Ldloc, builder);
                        else
@@ -249,7 +253,7 @@ namespace Mono.CSharp {
 
        /// <summary>
        ///   The Assign node takes care of assigning the value of source into
-       ///   the expression represented by target. 
+       ///   the expression represented by target.
        /// </summary>
        public class Assign : ExpressionStatement {
                protected Expression target, source, real_source;
@@ -258,6 +262,11 @@ namespace Mono.CSharp {
                protected bool is_embedded = false;
                protected bool must_free_temp = false;
 
+               public Assign (Expression target, Expression source)
+                       : this (target, source, target.Location)
+               {
+               }
+
                public Assign (Expression target, Expression source, Location l)
                {
                        this.target = target;
@@ -298,9 +307,9 @@ namespace Mono.CSharp {
 
                public static void error70 (EventInfo ei, Location l)
                {
-                       Report.Error (70, l, "The event '" + ei.Name +
-                                     "' can only appear on the left-side of a += or -= (except when" +
-                                     " used from within the type '" + ei.DeclaringType + "')");
+                       Report.Error (70, l, "The event `" + TypeManager.CSharpSignature (ei) +
+                                     "' can only appear on the left hand side of += or -= (except when" +
+                                     " used from within the type `" + ei.DeclaringType + "')");
                }
 
                //
@@ -331,9 +340,9 @@ namespace Mono.CSharp {
 
                                if (embedded == null) {
                                        if (this is CompoundAssign)
-                                               real_temp = temp = new LocalTemporary (ec, target.Type);
+                                               real_temp = temp = new LocalTemporary (target.Type);
                                        else
-                                               real_temp = temp = new LocalTemporary (ec, source.Type);
+                                               real_temp = temp = new LocalTemporary (source.Type);
                                } else
                                        temp = embedded.temp;
 
@@ -353,7 +362,8 @@ namespace Mono.CSharp {
                        if (target == null)
                                return null;
 
-                       if (source.Equals (target)) {
+                       bool same_assignment = (embedded != null) ? embedded.Target.Equals(target) : source.Equals (target);
+                       if (same_assignment) {
                                Report.Warning (1717, 3, loc, "Assignment made to same variable; did you mean to assign something else?");
                        }
 
@@ -368,17 +378,16 @@ namespace Mono.CSharp {
                                type = target_type;
                        eclass = ExprClass.Value;
 
-
                        if (target is EventExpr) {
                                EventInfo ei = ((EventExpr) target).EventInfo;
 
                                Expression ml = MemberLookup (
-                                       ec, ec.ContainerType, ei.Name,
+                                       ec.ContainerType, ec.ContainerType, ei.Name,
                                        MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
 
                                if (ml == null) {
-                                       //
-                                       // If this is the case, then the Event does not belong 
+                                       //
+                                       // If this is the case, then the Event does not belong
                                        // to this Type and so, according to the spec
                                        // is allowed to only appear on the left hand of
                                        // the += and -= operators
@@ -387,26 +396,11 @@ namespace Mono.CSharp {
                                        // in the case it is being referenced within the same type container;
                                        // it will appear as a FieldExpr in that case.
                                        //
-                                       
+
                                        if (!(source is BinaryDelegate)) {
                                                error70 (ei, loc);
                                                return null;
-                                       } 
-                               }
-                       }
-                       
-                       FieldExpr field_exp = target as FieldExpr;
-                       if (field_exp != null && field_exp.DeclaringType.IsValueType && !ec.IsConstructor && !ec.IsFieldInitializer) {
-                               field_exp = field_exp.InstanceExpression as FieldExpr;
-                               if (field_exp != null && field_exp.FieldInfo.IsInitOnly) {
-                                       if (field_exp.IsStatic) {
-                                               Report.Error (1650, loc, "Members of static readonly field '{0}' cannot be assigned to " +
-                                                       "(except in a static constructor or a variable initializer)", TypeManager.GetFullNameSignature (field_exp.FieldInfo));
-                                       } else {
-                                               Report.Error (1648, loc, "Members of readonly field '{0}' cannot be assigned to " +
-                                                       "(except in a constructor or a variable initializer)", TypeManager.GetFullNameSignature (field_exp.FieldInfo));
                                        }
-                                       return null;
                                }
                        }
 
@@ -418,7 +412,7 @@ namespace Mono.CSharp {
                        }
 
                        if ((source.eclass == ExprClass.Type) && (source is TypeExpr)) {
-                               source.Error_UnexpectedKind ("variable or value", loc);
+                               source.Error_UnexpectedKind (ec.DeclContainer, "variable or value", loc);
                                return null;
                        } else if ((RootContext.Version == LanguageVersion.ISO_1) &&
                                   (source is MethodGroupExpr)){
@@ -431,43 +425,43 @@ namespace Mono.CSharp {
                                if (source is New && target_type.IsValueType &&
                                    (target.eclass != ExprClass.IndexerAccess) && (target.eclass != ExprClass.PropertyAccess)){
                                        New n = (New) source;
-                                       
+
                                        if (n.SetValueTypeVariable (target))
                                                return n;
                                        else
                                                return null;
                                }
-                               
+
                                return this;
                        }
-                       
+
                        //
                        // If this assignment/operator was part of a compound binary
                        // operator, then we allow an explicit conversion, as detailed
-                       // in the spec. 
+                       // in the spec.
                        //
 
                        if (this is CompoundAssign){
                                CompoundAssign a = (CompoundAssign) this;
-                               
+
                                Binary b = source as Binary;
                                if (b != null){
                                        //
                                        // 1. if the source is explicitly convertible to the
                                        //    target_type
                                        //
-                                       
+
                                        source = Convert.ExplicitConversion (ec, source, target_type, loc);
                                        if (source == null){
-                                               Convert.Error_CannotImplicitConversion (loc, source_type, target_type);
+                                               a.original_source.Error_ValueCannotBeConverted (loc, target_type, true);
                                                return null;
                                        }
-                               
+
                                        //
                                        // 2. and the original right side is implicitly convertible to
                                        // the type of target
                                        //
-                                       if (Convert.ImplicitStandardConversionExists (ec, a.original_source, target_type))
+                                       if (Convert.ImplicitStandardConversionExists (a.original_source, target_type))
                                                return this;
 
                                        //
@@ -478,12 +472,18 @@ namespace Mono.CSharp {
                                            (b.Oper == Binary.Operator.LeftShift || b.Oper == Binary.Operator.RightShift))
                                                return this;
 
-                                       Convert.Error_CannotImplicitConversion (loc, a.original_source.Type, target_type);
+                                       a.original_source.Error_ValueCannotBeConverted (loc, target_type, false);
                                        return null;
                                }
                        }
+
+                       if (source.eclass == ExprClass.MethodGroup && !TypeManager.IsDelegateType (target_type)) {
+                               Report.Error (428, source.Location, "Cannot convert method group `{0}' to non-delegate type `{1}'. Did you intend to invoke the method?",
+                                       ((MethodGroupExpr)source).Name, target.GetSignatureForError ());
+                               return null;
+                       }
+
                        source = Convert.ImplicitConversionRequired (ec, source, target_type, loc);
-                       
                        if (source == null)
                                return null;
 
@@ -494,10 +494,10 @@ namespace Mono.CSharp {
                        // type and store it in a new temporary local.
                        if (is_embedded || embedded != null) {
                                type = target_type;
-                               temp = new LocalTemporary (ec, type);
+                               temp = new LocalTemporary (type);
                                must_free_temp = true;
                        }
-                       
+
                        return this;
                }
 
@@ -548,7 +548,7 @@ namespace Mono.CSharp {
                                ((EventExpr) target).EmitAddOrRemove (ec, source);
                                return;
                        }
-                       
+
                        IAssignMethod am = (IAssignMethod) target;
 
                        Expression temp_source;
@@ -562,16 +562,16 @@ namespace Mono.CSharp {
                                }
                        } else
                                temp_source = source;
-               
+
                        am.EmitAssign (ec, temp_source, !is_statement, this is CompoundAssign);
-                               
+
                        if (embedded != null) {
                                if (temp != null)
                                        temp.Release (ec);
                                embedded.ReleaseEmbedded (ec);
                        }
                }
-               
+
                public override void Emit (EmitContext ec)
                {
                        Emit (ec, false);
@@ -583,23 +583,23 @@ namespace Mono.CSharp {
                }
        }
 
-       
+
        //
-       // This class is used for compound assignments.  
+       // This class is used for compound assignments.
        //
        class CompoundAssign : Assign {
                Binary.Operator op;
                public Expression original_source;
-               
-               public CompoundAssign (Binary.Operator op, Expression target, Expression source, Location l)
-                       : base (target, source, l)
+
+               public CompoundAssign (Binary.Operator op, Expression target, Expression source)
+                       : base (target, source, target.Location)
                {
                        original_source = source;
                        this.op = op;
                }
 
                protected CompoundAssign (CompoundAssign embedded, Location l)
-                       : this (embedded.op, embedded.target, embedded.source, l)
+                       : this (embedded.op, embedded.target, embedded.source)
                {
                        this.is_embedded = true;
                }
@@ -609,11 +609,6 @@ namespace Mono.CSharp {
                        return new CompoundAssign (this, loc);
                }
 
-               public Expression ResolveSource (EmitContext ec)
-               {
-                       return original_source.Resolve (ec);
-               }
-
                public override Expression DoResolve (EmitContext ec)
                {
                        original_source = original_source.Resolve (ec);
@@ -623,18 +618,14 @@ namespace Mono.CSharp {
                        target = target.Resolve (ec);
                        if (target == null)
                                return null;
-                       
+
                        //
                        // Only now we can decouple the original source/target
                        // into a tree, to guarantee that we do not have side
                        // effects.
                        //
-                       source = new Binary (op, target, original_source, loc);
+                       source = new Binary (op, target, original_source);
                        return base.DoResolve (ec);
                }
        }
 }
-
-
-
-