[mcs] Implements C# 7.2 readonly structs
[mono.git] / mcs / mcs / expression.cs
index 74f0aadd45b33f0f86b7ae297cb653f7a00cc1a3..89207a3b54a26868eeadd5b13ed7110a4e9f768e 100644 (file)
@@ -1687,8 +1687,15 @@ namespace Mono.CSharp
                                ec.Emit (OpCodes.Dup);
                                no_value_label = ec.DefineLabel ();
                                ec.Emit (OpCodes.Brfalse_S, no_value_label);
+
+                               if (Variable.HoistedVariant != null)
+                                       ec.EmitThis ();
+
                                expr_unwrap.Emit (ec);
                        } else {
+                               if (Variable?.HoistedVariant != null)
+                                       ec.EmitThis ();
+
                                expr.Emit (ec);
 
                                // Only to make verifier happy
@@ -1708,19 +1715,29 @@ namespace Mono.CSharp
                                        value_on_stack = false;
                                }
 
-                               //
-                               // It's ok to have variable builder create out of order. It simplified emit
-                               // of statements like while (condition) { }
-                               //
-                               if (!Variable.Created)
-                                       Variable.CreateBuilder (ec);
-                               
-                               Variable.EmitAssign (ec);
+                               if (Variable.HoistedVariant != null) {
+                                       Variable.HoistedVariant.EmitAssignFromStack (ec);
+
+                                       if (expr_unwrap != null) {
+                                               ec.MarkLabel (no_value_label);
+                                       } else if (!value_on_stack) {
+                                               Variable.HoistedVariant.Emit (ec);
+                                       }
+                               } else {
+                                       //
+                                       // It's ok to have variable builder created out of order. It simplifies emit
+                                       // of statements like while (condition) { }
+                                       //
+                                       if (!Variable.Created)
+                                               Variable.CreateBuilder (ec);
 
-                               if (expr_unwrap != null) {
-                                       ec.MarkLabel (no_value_label);
-                               } else if (!value_on_stack) {
-                                       Variable.Emit (ec);
+                                       Variable.EmitAssign (ec);
+
+                                       if (expr_unwrap != null) {
+                                               ec.MarkLabel (no_value_label);
+                                       } else if (!value_on_stack) {
+                                               Variable.Emit (ec);
+                                       }
                                }
                        }
                }
@@ -2599,7 +2616,8 @@ namespace Mono.CSharp
 
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
-                       Variable.CreateBuilder (ec);
+                       if (!Variable.Created)
+                               Variable.CreateBuilder (ec);
 
                        if (Initializer != null) {
                                lvr.EmitAssign (ec, Initializer, false, false);
@@ -2672,6 +2690,11 @@ namespace Mono.CSharp
                {
                        throw new NotImplementedException ();
                }
+
+               public override void EmitPrepare (EmitContext ec)
+               {
+                       Variable.CreateBuilder (ec);
+               }
        }
        
        //
@@ -6539,18 +6562,19 @@ namespace Mono.CSharp
                                return;
                        }
 
+                       bool dereference = IsRef && !(source is ReferenceExpression);
                        New n_source = source as New;
                        if (n_source != null && n_source.CanEmitOptimizedLocalTarget (ec)) {
                                if (!n_source.Emit (ec, this)) {
                                        if (leave_copy) {
                                                EmitLoad (ec);
-                                               if (IsRef)
+                                               if (dereference)
                                                        ec.EmitLoadFromPtr (type);
                                        }
                                        return;
                                }
                        } else {
-                               if (IsRef)
+                               if (dereference)
                                        EmitLoad (ec);
 
                                source.Emit (ec);
@@ -6558,13 +6582,13 @@ namespace Mono.CSharp
 
                        if (leave_copy) {
                                ec.Emit (OpCodes.Dup);
-                               if (IsRef) {
+                               if (dereference) {
                                        temp = new LocalTemporary (Type);
                                        temp.Store (ec);
                                }
                        }
 
-                       if (IsRef)
+                       if (dereference)
                                ec.EmitStoreFromPtr (type);
                        else
                                Variable.EmitAssign (ec);
@@ -6648,7 +6672,7 @@ namespace Mono.CSharp
                }
 
                public override bool IsRef {
-                       get { return false; }
+                       get { return local_info.IsByRef; }
                }
 
                public override string Name {
@@ -6689,7 +6713,11 @@ namespace Mono.CSharp
                                        AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, this, loc);
                                } else if (local_info.IsFixed) {
                                        ec.Report.Error (1764, loc,
-                                               "Cannot use fixed local `{0}' inside an anonymous method, lambda expression or query expression",
+                                               "Cannot use fixed variable `{0}' inside an anonymous method, lambda expression or query expression",
+                                               GetSignatureForError ());
+                               } else if (local_info.IsByRef) {
+                                       ec.Report.Error (8175, loc,
+                                               "Cannot use by-reference variable `{0}' inside an anonymous method, lambda expression, or query expression",
                                                GetSignatureForError ());
                                }
 
@@ -7111,20 +7139,48 @@ namespace Mono.CSharp
                protected override Expression DoResolve (ResolveContext rc)
                {
                        ResolveConditionalAccessReceiver (rc);
-                       return DoResolveInvocation (rc);
+                       return DoResolveInvocation (rc, null);
                }
 
                public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
                {
                        var sn = expr as SimpleName;
                        if (sn != null && sn.Name == "var" && sn.Arity == 0 && arguments?.Count > 1) {
-                               throw new NotImplementedException ("var deconstruct");
+                               var targets = new List<Expression> (arguments.Count);
+                               var variables = new List<LocalVariable> (arguments.Count);
+                               foreach (var arg in arguments) {
+                                       var arg_sn = arg.Expr as SimpleName;
+                                       if (arg_sn == null || arg_sn.Arity != 0) {
+                                               rc.Report.Error (8199, loc, "The syntax `var (...)' as an lvalue is reserved");
+                                               return ErrorExpression.Instance;
+                                       }
+
+                                       var lv = new LocalVariable (rc.CurrentBlock, arg_sn.Name, arg.Expr.Location);
+                                       rc.CurrentBlock.AddLocalName (lv);
+                                       variables.Add (lv);
+
+                                       targets.Add (new LocalVariableReference (lv, arg_sn.Location));
+                               }
+
+                               var res = new TupleDeconstruct (targets, variables, right_side, loc);
+                               return res.Resolve (rc);
+                       }
+
+                       if (right_side != null) {
+                               if (eclass != ExprClass.Unresolved)
+                                       return this;
+
+                               var res = DoResolveInvocation (rc, right_side);
+                               if (res == null)
+                                       return null;
+
+                               return res;
                        }
 
                        return base.DoResolveLValue (rc, right_side);
                }
 
-               Expression DoResolveInvocation (ResolveContext ec)
+               Expression DoResolveInvocation (ResolveContext ec, Expression rhs)
                {
                        Expression member_expr;
                        var atn = expr as ATypeNameExpression;
@@ -7220,6 +7276,17 @@ namespace Mono.CSharp
                        IsSpecialMethodInvocation (ec, method, loc);
                        
                        eclass = ExprClass.Value;
+
+                       if (type.Kind == MemberKind.ByRef) {
+                               if (rhs == null && arguments?.ContainsEmitWithAwait () == true) {
+                                       ec.Report.Error (8178, loc, "`await' cannot be used in an expression containing a call to `{0}' because it returns by reference",
+                                               GetSignatureForError ());
+                               }
+
+                               if (rhs != EmptyExpression.OutAccess)
+                                       return ByRefDereference.Create (this).Resolve (ec);
+                       }
+
                        return this;
                }
 
@@ -7353,7 +7420,14 @@ namespace Mono.CSharp
                        else
                                mg.EmitCall (ec, arguments, false);
                }
-               
+
+               public override void EmitPrepare (EmitContext ec)
+               {
+                       mg.EmitPrepare (ec);
+
+                       arguments?.EmitPrepare (ec);
+               }
+
                public override void EmitStatement (EmitContext ec)
                {
                        if (mg.IsConditionallyExcluded)
@@ -8931,7 +9005,7 @@ namespace Mono.CSharp
                        if (eclass == ExprClass.Unresolved)
                                ResolveBase (ec);
 
-                       if (type.IsClass){
+                       if (type.IsClass || type.IsReadOnly) {
                                if (right_side == EmptyExpression.UnaryAddress)
                                        ec.Report.Error (459, loc, "Cannot take the address of `this' because it is read-only");
                                else if (right_side == EmptyExpression.OutAccess)
@@ -11544,6 +11618,39 @@ namespace Mono.CSharp
                }
        }
 
+       class ReferenceTypeExpr : TypeExpr
+       {
+               FullNamedExpression element;
+
+               public ReferenceTypeExpr (FullNamedExpression element, Location loc)
+               {
+                       this.element = element;
+                       this.loc = loc;
+               }
+
+               public override TypeSpec ResolveAsType (IMemberContext mc, bool allowUnboundTypeArguments = false)
+               {
+                       type = element.ResolveAsType (mc);
+                       if (type == null)
+                               return null;
+
+                       eclass = ExprClass.Type;
+                       type = ReferenceContainer.MakeType (mc.Module, type);
+
+                       return type;
+               }
+
+               public override string GetSignatureForError ()
+               {
+                       return "ref " + element.GetSignatureForError ();
+               }
+
+               public override object Accept (StructuralVisitor visitor)
+               {
+                       return visitor.Visit (this);
+               }
+       }
+
        class FixedBufferPtr : Expression
        {
                readonly Expression array;
@@ -12871,4 +12978,146 @@ namespace Mono.CSharp
                        return Reachability.CreateUnreachable ();
                }
        }
+
+       class ReferenceExpression : CompositeExpression
+       {
+               public ReferenceExpression (Expression expr, Location loc)
+                       : base (expr)
+               {
+                       this.loc = loc;
+               }
+
+               static bool CanBeByRef (Expression expr)
+               {
+                       if (expr is IAssignMethod)
+                               return true;
+
+                       var invocation = expr as Invocation;
+                       if (invocation?.Type.Kind == MemberKind.ByRef)
+                               return true;
+
+                       return false;
+               }
+
+               public override Expression CreateExpressionTree (ResolveContext rc)
+               {
+                       throw new NotSupportedException ("ET");
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
+               {
+                       var res = expr.DoResolveLValue (rc, EmptyExpression.OutAccess);
+                       if (res == null || !CanBeByRef (res)) {
+                               if (res?.Type != InternalType.ErrorType)
+                                       rc.Report.Error (8156, expr.Location, "An expression cannot be used in this context because it may not be returned by reference");
+                               return ErrorExpression.Instance;
+                       }
+
+                       type = res.Type;
+                       var type_container = type as ReferenceContainer;
+                       if (type_container != null)
+                               type = type_container.Element;
+
+                       expr = res;
+                       eclass = ExprClass.Value;
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       var ml = expr as IMemoryLocation;
+                       if (ml != null)
+                               ml.AddressOf (ec, AddressOp.LoadStore);
+                       else
+                               expr.Emit (ec);
+               }
+
+               public override void Error_ValueCannotBeConverted (ResolveContext rc, TypeSpec target, bool expl)
+               {
+                       rc.Report.Error (8173, loc, "The expression must be of type `{0}' because it is being assigned by reference", target.GetSignatureForError ());
+               }
+       }
+
+       class ByRefDereference : CompositeExpression, IMemoryLocation, IAssignMethod
+       {
+               bool prepared;
+               LocalTemporary temporary;
+
+               private ByRefDereference (Expression expr)
+                       : base (expr)
+               {
+               }
+
+               public static Expression Create (Expression expr)
+               {
+                       var rc = expr.Type as ReferenceContainer;
+                       if (rc == null)
+                               return expr;
+
+                       return new ByRefDereference (expr) {
+                               type = rc.Element
+                       };
+               }
+
+               public void AddressOf (EmitContext ec, AddressOp mode)
+               {
+                       expr.Emit (ec);
+               }
+
+               public void Emit (EmitContext ec, bool leave_copy)
+               {
+                       Emit (ec);
+                       if (leave_copy) {
+                               ec.Emit (OpCodes.Dup);
+                               temporary = new LocalTemporary (type);
+                               temporary.Store (ec);
+                       }
+               }
+
+               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
+               {
+                       prepared = isCompound;
+
+                       expr.Emit (ec);
+
+                       if (isCompound)
+                               ec.Emit (OpCodes.Dup);
+                       
+                       source.Emit (ec);
+                       if (leave_copy) {
+                               throw new NotImplementedException ("leave_copy");
+                       }
+                       
+                       ec.EmitStoreFromPtr (type);
+                       
+                       if (temporary != null) {
+                               temporary.Emit (ec);
+                               temporary.Release (ec);
+                       }
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
+               {
+                       eclass = ExprClass.Variable;
+                       return this;
+               }
+
+               public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
+               {
+                       return DoResolve (rc);
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       if (!prepared)
+                               base.Emit(ec);
+                       
+                       ec.EmitLoadFromPtr (type);
+               }
+
+               public override object Accept (StructuralVisitor visitor)
+               {
+                       return visitor.Visit (this);
+               }
+       }
 }