New tests.
[mono.git] / mcs / mcs / assign.cs
index ba126d64e20b447a67a2cac1993a4321e51f7a92..befc203eda07436a95ca558573f261297f1a9c38 100644 (file)
@@ -14,7 +14,6 @@
 using System;
 using System.Reflection;
 using System.Reflection.Emit;
-using System.Collections;
 
 namespace Mono.CSharp {
 
@@ -176,27 +175,19 @@ namespace Mono.CSharp {
        ///   Does not happen with a class because a class is a pointer -- so you always
        ///   get the indirection.
        ///
-       ///   The `is_address' stuff is really just a hack. We need to come up with a better
-       ///   way to handle it.
        /// </remarks>
        public class LocalTemporary : Expression, IMemoryLocation, IAssignMethod {
                LocalBuilder builder;
-               bool is_address;
 
-               public LocalTemporary (Type t) : this (t, false) {}
-
-               public LocalTemporary (Type t, bool is_address)
+               public LocalTemporary (TypeSpec t)
                {
                        type = t;
                        eclass = ExprClass.Value;
-                       this.is_address = is_address;
                }
 
-               public LocalTemporary (LocalBuilder b, Type t)
+               public LocalTemporary (LocalBuilder b, TypeSpec t)
+                       : this (t)
                {
-                       type = t;
-                       eclass = ExprClass.Value;
-                       loc = Location.Null;
                        builder = b;
                }
 
@@ -210,10 +201,10 @@ namespace Mono.CSharp {
                {
                        Arguments args = new Arguments (1);
                        args.Add (new Argument (this));
-                       return CreateExpressionFactoryCall ("Constant", args);
+                       return CreateExpressionFactoryCall (ec, "Constant", args);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        return this;
                }
@@ -225,15 +216,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)
-                               LoadFromPtr (ig, type);
+                       ec.Emit (OpCodes.Ldloc, builder);
                }
 
                #region IAssignMethod Members
@@ -265,41 +251,27 @@ namespace Mono.CSharp {
                        get { return builder; }
                }
 
-               // NB: if you have `is_address' on the stack there must
-               // be a managed pointer. Otherwise, it is the type from
-               // the ctor.
                public void Store (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
                        if (builder == null)
-                               builder = ec.GetTemporaryLocal (is_address ? TypeManager.GetReferenceType (type): type);
+                               builder = ec.GetTemporaryLocal (type);
 
-                       ig.Emit (OpCodes.Stloc, builder);
+                       ec.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
-                               ig.Emit (OpCodes.Ldloca, builder);
-               }
-
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       type = storey.MutateType (type);
-               }
+                               builder = ec.GetTemporaryLocal (type);
 
-               public bool PointsToAddress {
-                       get {
-                               return is_address;
+                       if (builder.LocalType.IsByRef) {
+                               //
+                               // if is_address, than this is just the address anyways,
+                               // so we just return this.
+                               //
+                               ec.Emit (OpCodes.Ldloc, builder);
+                       } else {
+                               ec.Emit (OpCodes.Ldloca, builder);
                        }
                }
        }
@@ -308,7 +280,7 @@ namespace Mono.CSharp {
        ///   The Assign node takes care of assigning the value of source into
        ///   the expression represented by target.
        /// </summary>
-       public class Assign : ExpressionStatement {
+       public abstract class Assign : ExpressionStatement {
                protected Expression target, source;
 
                protected Assign (Expression target, Expression source, Location loc)
@@ -320,7 +292,7 @@ namespace Mono.CSharp {
                
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       Report.Error (832, loc, "An expression tree cannot contain an assignment operator");
+                       ec.Report.Error (832, loc, "An expression tree cannot contain an assignment operator");
                        return null;
                }
 
@@ -332,7 +304,7 @@ namespace Mono.CSharp {
                        get { return source; }
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        bool ok = true;
                        source = source.Resolve (ec);
@@ -347,30 +319,23 @@ namespace Mono.CSharp {
                        if (target == null || !ok)
                                return null;
 
-                       Type target_type = target.Type;
-                       Type source_type = source.Type;
+                       TypeSpec target_type = target.Type;
+                       TypeSpec source_type = source.Type;
 
                        eclass = ExprClass.Value;
                        type = target_type;
 
                        if (!(target is IAssignMethod)) {
-                               Error_ValueAssignment (loc);
+                               Error_ValueAssignment (ec, loc);
                                return null;
                        }
 
-                       if ((RootContext.Version == LanguageVersion.ISO_1) &&
-                                  (source is MethodGroupExpr)){
-                               ((MethodGroupExpr) source).ReportUsageError ();
+                       if ((RootContext.Version == LanguageVersion.ISO_1) && (source is MethodGroupExpr)){
+                               ((MethodGroupExpr) source).ReportUsageError (ec);
                                return null;
                        }
 
                        if (!TypeManager.IsEqual (target_type, source_type)) {
-                               if (TypeManager.IsDynamicType (source_type)) {
-                                       Arguments args = new Arguments (1);
-                                       args.Add (new Argument (source));
-                                       return new DynamicConversion (target_type, false, args, loc).Resolve (ec);
-                               }
-
                                Expression resolved = ResolveConversions (ec);
 
                                if (resolved != this)
@@ -380,13 +345,29 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+#if NET_4_0
+               public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
                {
-                       source.MutateHoistedGenericType (storey);
-                       target.MutateHoistedGenericType (storey);
-                       type = storey.MutateType (type);                        
-               }
+                       var tassign = target as IDynamicAssign;
+                       if (tassign == null)
+                               throw new InternalErrorException (target.GetType () + " does not support dynamic assignment");
+
+                       var target_object = tassign.MakeAssignExpression (ctx);
+
+                       //
+                       // Some hacking is needed as DLR does not support void type and requires
+                       // always have object convertible return type to support caching and chaining
+                       //
+                       // We do this by introducing an explicit block which returns RHS value when
+                       // available or null
+                       //
+                       if (target_object.NodeType == System.Linq.Expressions.ExpressionType.Block)
+                               return target_object;
 
+                       var source_object = System.Linq.Expressions.Expression.Convert (source.MakeExpression (ctx), target_object.Type);
+                       return System.Linq.Expressions.Expression.Assign (target_object, source_object);
+               }
+#endif
                protected virtual Expression ResolveConversions (ResolveContext ec)
                {
                        source = Convert.ImplicitConversionRequired (ec, source, target.Type, loc);
@@ -421,7 +402,7 @@ namespace Mono.CSharp {
                }
        }
 
-       class SimpleAssign : Assign {
+       public class SimpleAssign : Assign {
                public SimpleAssign (Expression target, Expression source)
                        : this (target, source, target.Location)
                {
@@ -443,14 +424,14 @@ namespace Mono.CSharp {
                        return t.Equals (source);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        Expression e = base.DoResolve (ec);
                        if (e == null || e != this)
                                return e;
 
                        if (CheckEqualAssign (target))
-                               Report.Warning (1717, 3, loc, "Assignment made to same variable; did you mean to assign something else?");
+                               ec.Report.Warning (1717, 3, loc, "Assignment made to same variable; did you mean to assign something else?");
 
                        return this;
                }
@@ -465,7 +446,7 @@ namespace Mono.CSharp {
                ExpressionStatement resolved;
                IMemberContext rc;
 
-               public FieldInitializer (FieldBuilder field, Expression expression, IMemberContext rc)
+               public FieldInitializer (FieldBase field, Expression expression, IMemberContext rc)
                        : base (new FieldExpr (field, expression.Location), expression, expression.Location)
                {
                        this.rc = rc;
@@ -473,7 +454,7 @@ namespace Mono.CSharp {
                                ((FieldExpr)target).InstanceExpression = CompilerGeneratedThis.Instance;
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        // Field initializer can be resolved (fail) many times
                        if (source == null)
@@ -482,7 +463,7 @@ namespace Mono.CSharp {
                        if (resolved == null) {
                                //
                                // Field initializers are tricky for partial classes. They have to
-                               // share same costructor (block) but they have they own resolve scope.
+                               // share same constructor (block) but they have they own resolve scope.
                                //
 
                                IMemberContext old = ec.MemberContext;
@@ -543,10 +524,10 @@ namespace Mono.CSharp {
                        return new SimpleAssign (target, source).CreateExpressionTree (ec);
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
                        if (op != Binary.Operator.Addition && op != Binary.Operator.Subtraction)
-                               target.Error_AssignmentEventOnly ();
+                               target.Error_AssignmentEventOnly (ec);
 
                        source = source.Resolve (ec);
                        if (source == null)
@@ -578,18 +559,11 @@ namespace Mono.CSharp {
        //
        // This class is used for compound assignments.
        //
-       class CompoundAssign : Assign {
-               Binary.Operator op;
-               Expression original_source;
-
-               public CompoundAssign (Binary.Operator op, Expression target, Expression source)
-                       : base (target, source, target.Location)
+       public class CompoundAssign : Assign
+       {
+               // This is just a hack implemented for arrays only
+               public sealed class TargetExpression : Expression
                {
-                       original_source = source;
-                       this.op = op;
-               }
-
-               public sealed class TargetExpression : Expression {
                        Expression child;
                        public TargetExpression (Expression child)
                        {
@@ -602,11 +576,8 @@ namespace Mono.CSharp {
                                throw new NotSupportedException ("ET");
                        }
 
-                       public override Expression DoResolve (ResolveContext ec)
+                       protected override Expression DoResolve (ResolveContext ec)
                        {
-                               child = child.Resolve (ec);
-                               if (child == null)
-                                       return null;
                                type = child.Type;
                                eclass = ExprClass.Value;
                                return this;
@@ -618,10 +589,28 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Expression DoResolve (ResolveContext ec)
+               // Used for underlying binary operator
+               readonly Binary.Operator op;
+               Expression right;
+               Expression left;
+
+               public CompoundAssign (Binary.Operator op, Expression target, Expression source)
+                       : base (target, source, target.Location)
+               {
+                       right = source;
+                       this.op = op;
+               }
+
+               public CompoundAssign (Binary.Operator op, Expression target, Expression source, Expression left)
+                       : this (op, target, source)
                {
-                       original_source = original_source.Resolve (ec);
-                       if (original_source == null)
+                       this.left = left;
+               }
+
+               protected override Expression DoResolve (ResolveContext ec)
+               {
+                       right = right.Resolve (ec);
+                       if (right == null)
                                return null;
 
                        MemberAccess ma = target as MemberAccess;
@@ -633,21 +622,25 @@ namespace Mono.CSharp {
                                return null;
 
                        if (target is MethodGroupExpr){
-                               Error_CannotAssign (((MethodGroupExpr)target).Name, target.ExprClassName);
+                               ec.Report.Error (1656, loc,
+                                       "Cannot assign to `{0}' because it is a `{1}'",
+                                       ((MethodGroupExpr)target).Name, target.ExprClassName);
                                return null;
                        }
 
                        if (target is EventExpr)
-                               return new EventAddOrRemove (target, op, original_source, loc).DoResolve (ec);
+                               return new EventAddOrRemove (target, op, right, loc).Resolve (ec);
 
                        //
                        // 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, new TargetExpression (target), original_source, true);
+                       if (left == null)
+                               left = new TargetExpression (target);
+
+                       source = new Binary (op, left, right, true, loc);
 
-                       // TODO: TargetExpression breaks MemberAccess composition
                        if (target is DynamicMemberBinder) {
                                Arguments targs = ((DynamicMemberBinder) target).Arguments;
                                source = source.Resolve (ec);
@@ -655,18 +648,18 @@ namespace Mono.CSharp {
                                Arguments args = new Arguments (2);
                                args.AddRange (targs);
                                args.Add (new Argument (source));
-                               source = new DynamicMemberBinder (true, ma.Name, args, loc).Resolve (ec);
+                               source = new DynamicMemberBinder (ma.Name, args, loc).ResolveLValue (ec, right);
 
                                // Handles possible event addition/subtraction
                                if (op == Binary.Operator.Addition || op == Binary.Operator.Subtraction) {
                                        args = new Arguments (2);
                                        args.AddRange (targs);
-                                       args.Add (new Argument (original_source));
+                                       args.Add (new Argument (right));
                                        string method_prefix = op == Binary.Operator.Addition ?
                                                Event.AEventAccessor.AddPrefix : Event.AEventAccessor.RemovePrefix;
 
-                                       Expression invoke = new DynamicInvocation (
-                                               new MemberAccess (original_source, method_prefix + ma.Name, loc), args, loc).Resolve (ec);
+                                       var invoke = DynamicInvocation.CreateSpecialNameInvoke (
+                                               new MemberAccess (right, method_prefix + ma.Name, loc), args, loc).Resolve (ec);
 
                                        args = new Arguments (1);
                                        args.AddRange (targs);
@@ -682,7 +675,7 @@ namespace Mono.CSharp {
 
                protected override Expression ResolveConversions (ResolveContext ec)
                {
-                       Type target_type = target.Type;
+                       TypeSpec target_type = target.Type;
 
                        //
                        // 1. the return type is implicitly convertible to the type of target
@@ -704,13 +697,19 @@ namespace Mono.CSharp {
                                // y is implicitly convertible to the type of x
                                //
                                if ((b.Oper & Binary.Operator.ShiftMask) != 0 ||
-                                       Convert.ImplicitConversionExists (ec, original_source, target_type)) {
+                                       Convert.ImplicitConversionExists (ec, right, target_type)) {
                                        source = Convert.ExplicitConversion (ec, source, target_type, loc);
                                        return this;
                                }
                        }
 
-                       original_source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
+                       if (source.Type == InternalType.Dynamic) {
+                               Arguments arg = new Arguments (1);
+                               arg.Add (new Argument (source));
+                               return new SimpleAssign (target, new DynamicConversion (target_type, CSharpBinderFlags.ConvertExplicit, arg, loc), loc).Resolve (ec);
+                       }
+
+                       right.Error_ValueCannotBeConverted (ec, loc, target_type, false);
                        return null;
                }
 
@@ -718,7 +717,7 @@ namespace Mono.CSharp {
                {
                        CompoundAssign ctarget = (CompoundAssign) t;
 
-                       ctarget.original_source = ctarget.source = source.Clone (clonectx);
+                       ctarget.right = ctarget.source = source.Clone (clonectx);
                        ctarget.target = target.Clone (clonectx);
                }
        }