Merge pull request #5567 from kumpera/fix_59334
[mono.git] / mcs / mcs / assign.cs
index b9aa689fc7a967ce988f321ce873965ca93bedac..a07c8c0ef397d1e2d597680f035cf078aa732489 100644 (file)
@@ -310,6 +310,12 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override Location StartLocation {
+                       get {
+                               return target.StartLocation;
+                       }
+               }
+
                public override bool ContainsEmitWithAwait ()
                {
                        return target.ContainsEmitWithAwait () || source.ContainsEmitWithAwait ();
@@ -328,7 +334,7 @@ namespace Mono.CSharp {
                                                
                        if (source == null) {
                                ok = false;
-                               source = EmptyExpression.Null;
+                               source = ErrorExpression.Instance;
                        }
 
                        target = target.ResolveLValue (ec, source);
@@ -343,7 +349,7 @@ namespace Mono.CSharp {
                        type = target_type;
 
                        if (!(target is IAssignMethod)) {
-                               Error_ValueAssignment (ec, source);
+                               target.Error_ValueAssignment (ec, source);
                                return null;
                        }
 
@@ -357,7 +363,6 @@ namespace Mono.CSharp {
                        return this;
                }
 
-#if NET_4_0
                public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
                {
                        var tassign = target as IDynamicAssign;
@@ -385,7 +390,7 @@ namespace Mono.CSharp {
 
                        return System.Linq.Expressions.Expression.Assign (target_object, source_object);
                }
-#endif
+
                protected virtual Expression ResolveConversions (ResolveContext ec)
                {
                        source = Convert.ImplicitConversionRequired (ec, source, target.Type, source.Location);
@@ -411,6 +416,20 @@ namespace Mono.CSharp {
                        Emit (ec, true);
                }
 
+               public override void FlowAnalysis (FlowAnalysisContext fc)
+               {
+                       source.FlowAnalysis (fc);
+
+                       if (target is ArrayAccess || target is IndexerExpr) {
+                               target.FlowAnalysis (fc);
+                               return;
+                       }
+
+                       var pe = target as PropertyExpr;
+                       if (pe != null && !pe.IsAutoPropertyAccess)
+                               target.FlowAnalysis (fc);
+               }
+
                protected override void CloneTo (CloneContext clonectx, Expression t)
                {
                        Assign _target = (Assign) t;
@@ -418,6 +437,11 @@ namespace Mono.CSharp {
                        _target.target = target.Clone (clonectx);
                        _target.source = source.Clone (clonectx);
                }
+
+               public override object Accept (StructuralVisitor visitor)
+               {
+                       return visitor.Visit (this);
+               }
        }
 
        public class SimpleAssign : Assign
@@ -454,6 +478,42 @@ namespace Mono.CSharp {
 
                        return this;
                }
+
+               public override void FlowAnalysis (FlowAnalysisContext fc)
+               {
+                       base.FlowAnalysis (fc);
+
+                       var vr = target as VariableReference;
+                       if (vr != null) {
+                               if (vr.VariableInfo != null)
+                                       fc.SetVariableAssigned (vr.VariableInfo);
+
+                               return;
+                       }
+
+                       var fe = target as FieldExpr;
+                       if (fe != null) {
+                               fe.SetFieldAssigned (fc);
+                               return;
+                       }
+
+                       var pe = target as PropertyExpr;
+                       if (pe != null) {
+                               pe.SetBackingFieldAssigned (fc);
+                               return;
+                       }
+
+                       var td = target as TupleDeconstruct;
+                       if (td != null) {
+                               td.SetGeneratedFieldAssigned (fc);
+                               return;
+                       }
+               }
+
+               public override Reachability MarkReachable (Reachability rc)
+               {
+                       return source.MarkReachable (rc);
+               }
        }
 
        public class RuntimeExplicitAssign : Assign
@@ -478,6 +538,10 @@ namespace Mono.CSharp {
                public CompilerAssign (Expression target, Expression source, Location loc)
                        : base (target, source, loc)
                {
+                       if (target.Type != null) {
+                               type = target.Type;
+                               eclass = ExprClass.Value;
+                       }
                }
 
                protected override Expression DoResolve (ResolveContext ec)
@@ -506,20 +570,24 @@ namespace Mono.CSharp {
                // share same constructor (block) for expression trees resolve but
                // they have they own resolve scope
                //
-               sealed class FieldInitializerContext : ResolveContext
+               sealed class FieldInitializerContext : BlockContext
                {
-                       ExplicitBlock ctor_block;
+                       readonly ExplicitBlock ctor_block;
 
-                       public FieldInitializerContext (IMemberContext mc, ResolveContext constructorContext)
-                               : base (mc, Options.FieldInitializerScope | Options.ConstructorScope)
+                       public FieldInitializerContext (IMemberContext mc, BlockContext constructorContext)
+                               : base (mc, null, constructorContext.ReturnType)
                        {
+                               flags |= Options.FieldInitializerScope | Options.ConstructorScope;
                                this.ctor_block = constructorContext.CurrentBlock.Explicit;
+
+                               if (ctor_block.IsCompilerGenerated)
+                                       CurrentBlock = ctor_block;
                        }
 
                        public override ExplicitBlock ConstructorBlock {
-                               get {
-                                       return ctor_block;
-                               }
+                           get {
+                               return ctor_block;
+                           }
                        }
                }
 
@@ -527,25 +595,41 @@ namespace Mono.CSharp {
                // Keep resolved value because field initializers have their own rules
                //
                ExpressionStatement resolved;
-               IMemberContext mc;
+               FieldBase mc;
 
-               public FieldInitializer (FieldSpec spec, Expression expression, IMemberContext mc)
-                       : base (new FieldExpr (spec, expression.Location), expression, expression.Location)
+               public FieldInitializer (FieldBase mc, Expression expression, Location loc)
+                       : base (new FieldExpr (mc.Spec, expression.Location), expression, loc)
                {
                        this.mc = mc;
-                       if (!spec.IsStatic)
+                       if (!mc.IsStatic)
                                ((FieldExpr)target).InstanceExpression = new CompilerGeneratedThis (mc.CurrentType, expression.Location);
                }
 
-               protected override Expression DoResolve (ResolveContext ec)
+               public int AssignmentOffset { get; private set; }
+
+               public FieldBase Field {
+                       get {
+                               return mc;
+                       }
+               }
+
+               public override Location StartLocation {
+                       get {
+                               return loc;
+                       }
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
                {
                        // Field initializer can be resolved (fail) many times
                        if (source == null)
                                return null;
 
                        if (resolved == null) {
-                               var ctx = new FieldInitializerContext (mc, ec);
+                               var bc = (BlockContext) rc;
+                               var ctx = new FieldInitializerContext (mc, bc);
                                resolved = base.DoResolve (ctx) as ExpressionStatement;
+                               AssignmentOffset = ctx.AssignmentInfoOffset - bc.AssignmentInfoOffset;
                        }
 
                        return resolved;
@@ -555,12 +639,29 @@ namespace Mono.CSharp {
                {
                        if (resolved == null)
                                return;
-                       
+
+                       //
+                       // Emit sequence symbol info even if we are in compiler generated
+                       // block to allow debugging field initializers when constructor is
+                       // compiler generated
+                       //
+                       if (ec.HasSet (BuilderContext.Options.OmitDebugInfo) && ec.HasMethodSymbolBuilder) {
+                               using (ec.With (BuilderContext.Options.OmitDebugInfo, false)) {
+                                       ec.Mark (loc);
+                               }
+                       }
+
                        if (resolved != this)
                                resolved.EmitStatement (ec);
                        else
                                base.EmitStatement (ec);
                }
+
+               public override void FlowAnalysis (FlowAnalysisContext fc)
+               {
+                       source.FlowAnalysis (fc);
+                       ((FieldExpr) target).SetFieldAssigned (fc);
+               }
                
                public bool IsDefaultInitializer {
                        get {
@@ -580,6 +681,33 @@ namespace Mono.CSharp {
                }
        }
 
+       class PrimaryConstructorAssign : SimpleAssign
+       {
+               readonly Field field;
+               readonly Parameter parameter;
+
+               public PrimaryConstructorAssign (Field field, Parameter parameter)
+                       : base (null, null, parameter.Location)
+               {
+                       this.field = field;
+                       this.parameter = parameter;
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
+               {
+                       target = new FieldExpr (field, loc);
+                       source = rc.CurrentBlock.ParametersBlock.GetParameterInfo (parameter).CreateReferenceExpression (rc, loc);
+                       return base.DoResolve (rc);
+               }
+
+               public override void EmitStatement (EmitContext ec)
+               {
+                       using (ec.With (BuilderContext.Options.OmitDebugInfo, true)) {
+                               base.EmitStatement (ec);
+                       }
+               }
+       }
+
        //
        // This class is used for compound assignments.
        //
@@ -596,9 +724,11 @@ namespace Mono.CSharp {
                                this.loc = child.Location;
                        }
 
+                       public bool RequiresEmitWithAwait { get; set; }
+
                        public override bool ContainsEmitWithAwait ()
                        {
-                               return child.ContainsEmitWithAwait ();
+                               return RequiresEmitWithAwait || child.ContainsEmitWithAwait ();
                        }
 
                        public override Expression CreateExpressionTree (ResolveContext ec)
@@ -629,19 +759,25 @@ namespace Mono.CSharp {
                Expression right;
                Expression left;
 
-               public CompoundAssign (Binary.Operator op, Expression target, Expression source, Location loc)
-                       : base (target, source, loc)
+               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, Location loc)
-                       : this (op, target, source, loc)
+               public CompoundAssign (Binary.Operator op, Expression target, Expression source, Expression left)
+                       : this (op, target, source)
                {
                        this.left = left;
                }
 
+               public Binary.Operator Operator {
+                       get {
+                               return op;
+                       }
+               }
+
                protected override Expression DoResolve (ResolveContext ec)
                {
                        right = right.Resolve (ec);
@@ -694,7 +830,7 @@ namespace Mono.CSharp {
                        if (left == null)
                                left = new TargetExpression (target);
 
-                       source = new Binary (op, left, right, true, loc);
+                       source = new Binary (op, left, right, true);
 
                        if (target is DynamicMemberAssignable) {
                                Arguments targs = ((DynamicMemberAssignable) target).Arguments;
@@ -742,6 +878,12 @@ namespace Mono.CSharp {
                        return base.DoResolve (ec);
                }
 
+               public override void FlowAnalysis (FlowAnalysisContext fc)
+               {
+                       target.FlowAnalysis (fc);
+                       source.FlowAnalysis (fc);
+               }
+
                protected override Expression ResolveConversions (ResolveContext ec)
                {
                        //
@@ -766,8 +908,19 @@ namespace Mono.CSharp {
                        // Otherwise, if the selected operator is a predefined operator
                        //
                        Binary b = source as Binary;
-                       if (b == null && source is ReducedExpression)
-                               b = ((ReducedExpression) source).OriginalExpression as Binary;
+                       if (b == null) {
+                               if (source is ReducedExpression)
+                                       b = ((ReducedExpression) source).OriginalExpression as Binary;
+                               else if (source is ReducedExpression.ReducedConstantExpression) {
+                                       b = ((ReducedExpression.ReducedConstantExpression) source).OriginalExpression as Binary;
+                               } else if (source is Nullable.LiftedBinaryOperator) {
+                                       var po = ((Nullable.LiftedBinaryOperator) source);
+                                       if (po.UserOperator == null)
+                                               b = po.Binary;
+                               } else if (source is TypeCast) {
+                                       b = ((TypeCast) source).Child as Binary;
+                               }
+                       }
 
                        if (b != null) {
                                //
@@ -789,7 +942,7 @@ namespace Mono.CSharp {
                                return new SimpleAssign (target, new DynamicConversion (target_type, CSharpBinderFlags.ConvertExplicit, arg, loc), loc).Resolve (ec);
                        }
 
-                       right.Error_ValueCannotBeConverted (ec, loc, target_type, false);
+                       right.Error_ValueCannotBeConverted (ec, target_type, false);
                        return null;
                }
 
@@ -800,5 +953,10 @@ namespace Mono.CSharp {
                        ctarget.right = ctarget.source = source.Clone (clonectx);
                        ctarget.target = target.Clone (clonectx);
                }
+
+               public override object Accept (StructuralVisitor visitor)
+               {
+                       return visitor.Visit (this);
+               }
        }
 }