Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / mcs / assign.cs
index 95ca88d4a8ca62c2c5ac129a73ce5f91bc7be7d0..9481aef96edae4b6d66b28b161a9eeb43e0b5b44 100644 (file)
@@ -10,6 +10,7 @@
 //
 // Copyright 2001, 2002, 2003 Ximian, Inc.
 // Copyright 2004-2008 Novell, Inc
+// Copyright 2011 Xamarin Inc
 //
 using System;
 
@@ -309,6 +310,12 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override Location StartLocation {
+                       get {
+                               return target.StartLocation;
+                       }
+               }
+
                public override bool ContainsEmitWithAwait ()
                {
                        return target.ContainsEmitWithAwait () || source.ContainsEmitWithAwait ();
@@ -342,7 +349,7 @@ namespace Mono.CSharp {
                        type = target_type;
 
                        if (!(target is IAssignMethod)) {
-                               Error_ValueAssignment (ec, loc);
+                               target.Error_ValueAssignment (ec, source);
                                return null;
                        }
 
@@ -356,7 +363,7 @@ namespace Mono.CSharp {
                        return this;
                }
 
-#if NET_4_0
+#if NET_4_0 || MOBILE_DYNAMIC
                public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
                {
                        var tassign = target as IDynamicAssign;
@@ -410,6 +417,14 @@ namespace Mono.CSharp {
                        Emit (ec, true);
                }
 
+               public override void FlowAnalysis (FlowAnalysisContext fc)
+               {
+                       source.FlowAnalysis (fc);
+
+                       if (target is ArrayAccess || target is IndexerExpr || target is PropertyExpr)
+                               target.FlowAnalysis (fc);
+               }
+
                protected override void CloneTo (CloneContext clonectx, Expression t)
                {
                        Assign _target = (Assign) t;
@@ -417,6 +432,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
@@ -453,6 +473,32 @@ 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;
+                       }
+               }
+
+               public override void MarkReachable (Reachability rc)
+               {
+                       var es = source as ExpressionStatement;
+                       if (es != null)
+                               es.MarkReachable (rc);
+               }
        }
 
        public class RuntimeExplicitAssign : Assign
@@ -477,6 +523,20 @@ 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)
+               {
+                       var expr = base.DoResolve (ec);
+                       var vr = target as VariableReference;
+                       if (vr != null && vr.VariableInfo != null)
+                               vr.VariableInfo.IsEverAssigned = false;
+
+                       return expr;
                }
 
                public void UpdateSource (Expression source)
@@ -495,20 +555,21 @@ 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;
                        }
 
                        public override ExplicitBlock ConstructorBlock {
-                               get {
-                                       return ctor_block;
-                               }
+                           get {
+                               return ctor_block;
+                           }
                        }
                }
 
@@ -516,25 +577,35 @@ 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 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;
 
+                       var bc = (BlockContext) rc;
                        if (resolved == null) {
-                               var ctx = new FieldInitializerContext (mc, ec);
+                               var ctx = new FieldInitializerContext (mc, bc);
                                resolved = base.DoResolve (ctx) as ExpressionStatement;
+                               AssignmentOffset = ctx.AssignmentInfoOffset - bc.AssignmentInfoOffset;
                        }
 
                        return resolved;
@@ -544,12 +615,28 @@ 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);
+               }
                
                public bool IsDefaultInitializer {
                        get {
@@ -618,19 +705,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);
@@ -683,7 +776,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;
@@ -731,6 +824,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)
                {
                        //
@@ -755,8 +854,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) {
                                //
@@ -778,7 +888,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;
                }
 
@@ -789,5 +899,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);
+               }
        }
 }