* MenuAPI.cs: On instance of MenuTracker check if source control is
[mono.git] / mcs / mcs / assign.cs
index 3bc9617f927403eae788d6555007a41a5b0ffbc9..6a5c34d2872608c9e0fd04f861a618fff99ed553 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,10 +178,10 @@ namespace Mono.CSharp {
        public class LocalTemporary : Expression, IMemoryLocation {
                LocalBuilder builder;
                bool is_address;
-               
+
                public LocalTemporary (Type t) : this (t, false) {}
-                       
-               public LocalTemporary (Type t, bool is_address) 
+
+               public LocalTemporary (Type t, bool is_address)
                {
                        type = t;
                        eclass = ExprClass.Value;
@@ -201,7 +201,7 @@ namespace Mono.CSharp {
                        ec.FreeTemporaryLocal (builder, type);
                        builder = null;
                }
-               
+
                public override Expression DoResolve (EmitContext ec)
                {
                        return this;
@@ -210,7 +210,10 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
-                       
+
+                       if (builder == null)
+                               throw new InternalErrorException ("Emit without Store, or after Release");
+
                        ig.Emit (OpCodes.Ldloc, builder);
                        // we need to copy from the pointer
                        if (is_address)
@@ -237,7 +240,7 @@ namespace Mono.CSharp {
                        // 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
@@ -253,7 +256,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;
@@ -279,6 +282,12 @@ namespace Mono.CSharp {
                {
                        this.is_embedded = true;
                }
+               
+               public override Expression CreateExpressionTree (EmitContext ec)
+               {
+                       Report.Error (832, loc, "An expression tree cannot contain an assignment operator");
+                       return null;
+               }
 
                protected virtual Assign GetEmbeddedAssign (Location loc)
                {
@@ -322,6 +331,7 @@ namespace Mono.CSharp {
                                source = embedded = ((Assign) source).GetEmbeddedAssign (loc);
 
                        real_source = source = source.Resolve (ec);
+                                               
                        if (source == null) {
                                // Ensure that we don't propagate the error as spurious "uninitialized variable" errors.
                                target = target.ResolveLValue (ec, EmptyExpression.Null, Location);
@@ -356,12 +366,12 @@ namespace Mono.CSharp {
                        // local variable as source.
                        if (embedded != null)
                                source = (embedded.temp != null) ? embedded.temp : embedded.source;
-
+                       
                        target = target.ResolveLValue (ec, source, Location);
 
                        if (target == null)
                                return null;
-
+                       
                        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?");
@@ -386,8 +396,8 @@ namespace Mono.CSharp {
                                        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
@@ -396,18 +406,16 @@ 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;
-                                       } 
+                                       }
                                }
                        }
-                       
+
                        if (!(target is IAssignMethod) && (target.eclass != ExprClass.EventAccess)) {
-                               Report.Error (131, loc,
-                                             "Left hand of an assignment must be a variable, " +
-                                             "a property or an indexer");
+                               Error_ValueAssignment (loc);
                                return null;
                        }
 
@@ -421,47 +429,50 @@ namespace Mono.CSharp {
 
                        }
 
-                       if (target_type == source_type){
-                               if (source is New && target_type.IsValueType &&
-                                   (target.eclass != ExprClass.IndexerAccess) && (target.eclass != ExprClass.PropertyAccess)){
-                                       New n = (New) source;
-                                       
-                                       if (n.SetValueTypeVariable (target))
+                       if (target_type == source_type) {
+                               if (target.eclass == ExprClass.Variable) {
+                                       New n = source as New;
+                                       if (n == null)
+                                               return this;
+
+                                       if (n.HasInitializer) {
+                                               n.SetTargetVariable (target);
+                                       } else if (target_type.IsValueType) {
+                                               n.SetTargetVariable (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){
-                                               a.original_source.Error_ValueCannotBeConverted (loc, target_type, true);
+                                               a.original_source.Error_ValueCannotBeConverted (ec, loc, target_type, true);
                                                return null;
                                        }
-                               
+
                                        //
                                        // 2. and the original right side is implicitly convertible to
                                        // the type of target
                                        //
-                                       if (Convert.ImplicitStandardConversionExists (a.original_source, target_type))
+                                       if (Convert.ImplicitConversionExists (ec, a.original_source, target_type))
                                                return this;
 
                                        //
@@ -472,7 +483,7 @@ namespace Mono.CSharp {
                                            (b.Oper == Binary.Operator.LeftShift || b.Oper == Binary.Operator.RightShift))
                                                return this;
 
-                                       a.original_source.Error_ValueCannotBeConverted (loc, target_type, false);
+                                       a.original_source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
                                        return null;
                                }
                        }
@@ -497,7 +508,7 @@ namespace Mono.CSharp {
                                temp = new LocalTemporary (type);
                                must_free_temp = true;
                        }
-                       
+
                        return this;
                }
 
@@ -548,7 +559,7 @@ namespace Mono.CSharp {
                                ((EventExpr) target).EmitAddOrRemove (ec, source);
                                return;
                        }
-                       
+
                        IAssignMethod am = (IAssignMethod) target;
 
                        Expression temp_source;
@@ -564,14 +575,14 @@ namespace Mono.CSharp {
                                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);
@@ -581,16 +592,84 @@ namespace Mono.CSharp {
                {
                        Emit (ec, true);
                }
+
+               protected override void CloneTo (CloneContext clonectx, Expression t)
+               {
+                       Assign _target = (Assign) t;
+
+                       _target.target = target.Clone (clonectx);
+                       _target.source = source.Clone (clonectx);
+               }
        }
 
-       
+
+       // This class implements fields and events class initializers
+       public class FieldInitializer : Assign
+       {
+               //
+               // Keep resolved value because field initializers have their own rules
+               //
+               ExpressionStatement resolved;
+
+               public FieldInitializer (FieldBuilder field, Expression expression)
+                       : base (new FieldExpr (field, expression.Location, true), expression)
+               {
+                       if (!field.IsStatic)
+                               ((FieldExpr)target).InstanceExpression = CompilerGeneratedThis.Instance;
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       // Field initializer can be resolved (fail) many times
+                       if (Source == null)
+                               return null;
+
+                       if (resolved == null)
+                               resolved = base.DoResolve (ec) as ExpressionStatement;
+
+                       return resolved;
+               }
+
+               public override void EmitStatement (EmitContext ec)
+               {
+                       if (resolved == null)
+                               return;
+                       
+                       if (resolved != this)
+                               resolved.EmitStatement (ec);
+                       else
+                               base.EmitStatement (ec);
+               }
+               
+               public bool IsComplexInitializer {
+                       get {
+                               if (embedded != null)
+                                       return true;
+
+                               return !(source is Constant);
+                       }
+               }
+
+               public bool IsDefaultInitializer {
+                       get {
+                               Constant c = source as Constant;
+                               if (c == null)
+                                       return false;
+                               
+                               FieldExpr fe = (FieldExpr)target;
+                               return c.IsDefaultInitializer (fe.Type);
+                       }
+               }
+       }
+
+
        //
-       // 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)
                        : base (target, source, target.Location)
                {
@@ -615,21 +694,31 @@ namespace Mono.CSharp {
                        if (original_source == null)
                                return null;
 
-                       target = target.Resolve (ec);
+                       using (ec.Set (EmitContext.Flags.InCompoundAssignment)) {
+                               target = target.Resolve (ec);
+                       }
+                       
                        if (target == null)
                                return null;
-                       
+
+                       if (target is MethodGroupExpr){
+                               Error_CannotAssign (((MethodGroupExpr)target).Name, target.ExprClassName);
+                               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);
+                       source = new Binary (op, target, original_source, true);
                        return base.DoResolve (ec);
                }
-       }
-}
-
-
 
+               protected override void CloneTo (CloneContext clonectx, Expression t)
+               {
+                       CompoundAssign target = (CompoundAssign) t;
 
+                       target.original_source = original_source.Clone (clonectx);
+               }
+       }
+}