2009-03-17 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / assign.cs
index c50c278145b535605db2587691d906f7d104d9e0..19a5e7e6dac9d5137a87f225cf383d69ef9d4768 100644 (file)
@@ -253,13 +253,6 @@ namespace Mono.CSharp {
 
                        source.Emit (ec);
 
-                       // HACK: variable is already emitted when source is an initializer 
-                       if (source is NewInitialize) {
-                               if (leave_copy)
-                                       Emit (ec);
-                               return;
-                       }
-
                        Store (ec);
 
                        if (leave_copy)
@@ -299,6 +292,11 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Ldloca, builder);
                }
 
+               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+               {
+                       type = storey.MutateType (type);
+               }
+
                public bool PointsToAddress {
                        get {
                                return is_address;
@@ -375,20 +373,7 @@ namespace Mono.CSharp {
                                if (resolved != this)
                                        return resolved;
                        }
-                       
-                       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;
-                               }
-                       }
-                       
+
                        return this;
                }
 
@@ -410,7 +395,8 @@ namespace Mono.CSharp {
 
                void Emit (EmitContext ec, bool is_statement)
                {
-                       ((IAssignMethod) target).EmitAssign (ec, source, !is_statement, this is CompoundAssign);
+                       IAssignMethod t = (IAssignMethod) target;
+                       t.EmitAssign (ec, source, !is_statement, this is CompoundAssign);
                }
 
                public override void Emit (EmitContext ec)
@@ -477,7 +463,7 @@ namespace Mono.CSharp {
                IResolveContext rc;
 
                public FieldInitializer (FieldBuilder field, Expression expression, IResolveContext rc)
-                       : base (new FieldExpr (field, expression.Location, true), expression, expression.Location)
+                       : base (new FieldExpr (field, expression.Location), expression, expression.Location)
                {
                        this.rc = rc;
                        if (!field.IsStatic)
@@ -601,10 +587,9 @@ namespace Mono.CSharp {
                        this.op = op;
                }
 
-               // !!! What a stupid name
-               public class Helper : Expression {
+               public sealed class TargetExpression : Expression {
                        Expression child;
-                       public Helper (Expression child)
+                       public TargetExpression (Expression child)
                        {
                                this.child = child;
                                this.loc = child.Location;
@@ -657,44 +642,39 @@ namespace Mono.CSharp {
                        // into a tree, to guarantee that we do not have side
                        // effects.
                        //
-                       source = new Binary (op, new Helper (target), original_source, true);
+                       source = new Binary (op, new TargetExpression (target), original_source, true);
                        return base.DoResolve (ec);
                }
 
                protected override Expression ResolveConversions (EmitContext ec)
                {
-                       // source might have changed to BinaryDelegate
                        Type target_type = target.Type;
-                       Type source_type = source.Type;
-                       Binary b = source as Binary;
-                       if (b == null)
-                               return base.ResolveConversions (ec);
-
-                       // FIXME: Restrict only to predefined operators
-
-                       //
-                       // 1. if the source is explicitly convertible to the
-                       //    target_type
-                       //
-                       source = Convert.ExplicitConversion (ec, source, target_type, loc);
-                       if (source == null){
-                               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
+                       // 1. the return type is implicitly convertible to the type of target
                        //
-                       if (Convert.ImplicitConversionExists (ec, original_source, target_type))
+                       if (Convert.ImplicitConversionExists (ec, source, target_type)) {
+                               source = Convert.ImplicitConversion (ec, source, target_type, loc);
                                return this;
+                       }
 
                        //
-                       // In the spec 2.4 they added: or if type of the target is int
-                       // and the operator is a shift operator...
+                       // Otherwise, if the selected operator is a predefined operator
                        //
-                       if (source_type == TypeManager.int32_type && (b.Oper & Binary.Operator.ShiftMask) != 0)
-                               return this;
+                       Binary b = source as Binary;
+                       if (b != null) {
+                               //
+                               // 2a. the operator is a shift operator
+                               //
+                               // 2b. the return type is explicitly convertible to the type of x, and
+                               // y is implicitly convertible to the type of x
+                               //
+                               if ((b.Oper & Binary.Operator.ShiftMask) != 0 ||
+                                       Convert.ImplicitConversionExists (ec, original_source, target_type)) {
+                                       source = Convert.ExplicitConversion (ec, source, target_type, loc);
+                                       return this;
+                               }
+                       }
 
                        original_source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
                        return null;