Merge pull request #5002 from BrzVlad/feature-sgen-modes
[mono.git] / mcs / mcs / expression.cs
index 76bcdbfa0ee7bdf8109573a1e6db43c9b9797deb..da37657867ed0e59f43a1d9eefdd010e977e9ae8 100644 (file)
@@ -718,8 +718,11 @@ namespace Mono.CSharp
 
                Expression ResolveAddressOf (ResolveContext ec)
                {
-                       if (!ec.IsUnsafe)
+                       if (ec.CurrentIterator != null) {
+                               UnsafeInsideIteratorError (ec, loc);
+                       } else if (!ec.IsUnsafe) {
                                UnsafeError (ec, loc);
+                       }
 
                        Expr = Expr.DoResolveLValue (ec, EmptyExpression.UnaryAddress);
                        if (Expr == null || Expr.eclass != ExprClass.Variable) {
@@ -737,7 +740,7 @@ namespace Mono.CSharp
                                is_fixed = vr.IsFixed;
                                vr.SetHasAddressTaken ();
 
-                               if (vr.IsHoisted) {
+                               if (vr.IsHoisted && ec.CurrentIterator == null) {
                                        AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, vr, loc);
                                }
                        } else {
@@ -984,8 +987,11 @@ namespace Mono.CSharp
                        if (expr == null)
                                return null;
 
-                       if (!ec.IsUnsafe)
+                       if (ec.CurrentIterator != null) {
+                               UnsafeInsideIteratorError (ec, loc);
+                       } else if (!ec.IsUnsafe) {
                                UnsafeError (ec, loc);
+                       }
 
                        var pc = expr.Type as PointerContainer;
 
@@ -1484,6 +1490,11 @@ namespace Mono.CSharp
                        expr.EmitSideEffect (ec);
                }
 
+               public override void EmitPrepare (EmitContext ec)
+               {
+                       expr.EmitPrepare (ec);
+               }
+
                public override void FlowAnalysis (FlowAnalysisContext fc)
                {
                        expr.FlowAnalysis (fc);
@@ -1529,7 +1540,7 @@ namespace Mono.CSharp
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        if (Variable != null)
-                               throw new NotSupportedException ();
+                               ec.Report.Error (8122, loc, "An expression tree cannot contain a pattern matching operator");
 
                        Arguments args = Arguments.CreateForExpressionTree (ec, null,
                                expr.CreateExpressionTree (ec),
@@ -1584,6 +1595,14 @@ namespace Mono.CSharp
                        ec.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
                }
 
+               public override void EmitPrepare (EmitContext ec)
+               {
+                       base.EmitPrepare (ec);
+
+                       if (Variable != null)
+                               Variable.CreateBuilder (ec);
+               }
+
                void EmitPatternMatch (EmitContext ec)
                {
                        var no_match = ec.DefineLabel ();
@@ -1689,7 +1708,13 @@ namespace Mono.CSharp
                                        value_on_stack = false;
                                }
 
-                               Variable.CreateBuilder (ec);
+                               //
+                               // 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 (expr_unwrap != null) {
@@ -1733,6 +1758,21 @@ namespace Mono.CSharp
                                fc.SetVariableAssigned (Variable.VariableInfo, true);
                }
 
+               public override void FlowAnalysisConditional (FlowAnalysisContext fc)
+               {
+                       if (Variable == null) {
+                               base.FlowAnalysisConditional (fc);
+                               return;
+                       }
+
+                       expr.FlowAnalysis (fc);
+
+                       fc.DefiniteAssignmentOnTrue = fc.BranchDefiniteAssignment ();
+                       fc.DefiniteAssignmentOnFalse = fc.DefiniteAssignment;
+
+                       fc.SetVariableAssigned (Variable.VariableInfo, fc.DefiniteAssignmentOnTrue);
+               }
+
                protected override void ResolveProbeType (ResolveContext rc)
                {
                        if (!(ProbeType is TypeExpr) && rc.Module.Compiler.Settings.Version == LanguageVersion.Experimental) {
@@ -1843,6 +1883,15 @@ namespace Mono.CSharp
 
                Expression ResolveResultExpression (ResolveContext ec)
                {
+                       if (Variable != null) {
+                               if (expr is NullLiteral) {
+                                       ec.Report.Error (8117, loc, "Cannot use null as pattern matching operand");
+                                       return this;
+                               }
+
+                               CheckExpressionVariable (ec);
+                       }
+
                        TypeSpec d = expr.Type;
                        bool d_is_nullable = false;
 
@@ -1850,7 +1899,7 @@ namespace Mono.CSharp
                        // If E is a method group or the null literal, or if the type of E is a reference
                        // type or a nullable type and the value of E is null, the result is false
                        //
-                       if (expr.IsNull || expr.eclass == ExprClass.MethodGroup)
+                       if (expr.IsNull)
                                return CreateConstantResult (ec, false);
 
                        if (d.IsNullableType) {
@@ -1864,6 +1913,11 @@ namespace Mono.CSharp
                        TypeSpec t = probe_type_expr;
                        bool t_is_nullable = false;
                        if (t.IsNullableType) {
+                               if (Variable != null) {
+                                       ec.Report.Error (8116, loc, "The nullable type `{0}' pattern matching is not allowed. Consider using underlying type `{1}'",
+                                                                        t.GetSignatureForError (), Nullable.NullableInfo.GetUnderlyingType (t).GetSignatureForError ());
+                               }
+
                                var ut = Nullable.NullableInfo.GetUnderlyingType (t);
                                if (!ut.IsGenericParameter) {
                                        t = ut;
@@ -1908,9 +1962,13 @@ namespace Mono.CSharp
                                        return ResolveGenericParameter (ec, d, tps);
 
                                if (t.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
-                                       ec.Report.Warning (1981, 3, loc,
-                                               "Using `{0}' to test compatibility with `{1}' is identical to testing compatibility with `object'",
-                                               OperatorName, t.GetSignatureForError ());
+                                       if (Variable != null) {
+                                               ec.Report.Error (8208, loc, "The type `{0}' pattern matching is not allowed", t.GetSignatureForError ());
+                                       } else {
+                                               ec.Report.Warning (1981, 3, loc,
+                                                       "Using `{0}' to test compatibility with `{1}' is identical to testing compatibility with `object'",
+                                                       OperatorName, t.GetSignatureForError ());
+                                       }
                                }
 
                                if (TypeManager.IsGenericParameter (d))
@@ -2362,6 +2420,11 @@ namespace Mono.CSharp
                        eclass = ExprClass.Value;
                        TypeSpec etype = expr.Type;
 
+                       if (type == null) {
+                               type = InternalType.ErrorType;
+                               return this;
+                       }
+
                        if (!TypeSpec.IsReferenceType (type) && !type.IsNullableType) {
                                if (TypeManager.IsGenericParameter (type)) {
                                        ec.Report.Error (413, loc,
@@ -2448,8 +2511,12 @@ namespace Mono.CSharp
                                return null;
                        }
 
-                       if (type.IsPointer && !ec.IsUnsafe) {
-                               UnsafeError (ec, loc);
+                       if (type.IsPointer) {
+                               if (ec.CurrentIterator != null) {
+                                       UnsafeInsideIteratorError (ec, loc);
+                               } else if (!ec.IsUnsafe) {
+                                       UnsafeError (ec, loc);
+                               }
                        }
 
                        eclass = ExprClass.Value;
@@ -2547,12 +2614,14 @@ namespace Mono.CSharp
 
                public override Expression CreateExpressionTree (ResolveContext rc)
                {
-                       rc.Report.Error (8046, loc, "An expression tree cannot contain a declaration expression");
+                       rc.Report.Error (8198, loc, "An expression tree cannot contain out variable declaration");
                        return null;
                }
 
                bool DoResolveCommon (ResolveContext rc)
                {
+                       CheckExpressionVariable (rc);
+
                        var var_expr = VariableType as VarExpr;
                        if (var_expr != null) {
                                type = InternalType.VarOutType;
@@ -3105,9 +3174,9 @@ namespace Mono.CSharp
                        public override Expression ConvertResult (ResolveContext ec, Binary b)
                        {
                                if (left != null) {
-                                       b.left = EmptyCast.Create (b.left, left);
+                                       b.left = Convert.UserDefinedConversion (ec, b.left, left, Convert.UserConversionRestriction.ImplicitOnly, b.loc) ?? EmptyCast.Create (b.left, left);
                                } else if (right != null) {
-                                       b.right = EmptyCast.Create (b.right, right);
+                                       b.right = Convert.UserDefinedConversion (ec, b.right, right, Convert.UserConversionRestriction.ImplicitOnly, b.loc) ?? EmptyCast.Create (b.right, right);
                                }
 
                                TypeSpec r_type = ReturnType;
@@ -3443,7 +3512,7 @@ namespace Mono.CSharp
                        }
                }
 
-               static CSharp.Operator.OpType ConvertBinaryToUserOperator (Operator op)
+               public static CSharp.Operator.OpType ConvertBinaryToUserOperator (Operator op)
                {
                        switch (op) {
                        case Operator.Addition:
@@ -7023,6 +7092,16 @@ namespace Mono.CSharp
                        }
                }
 
+               bool statement_resolve;
+               public override ExpressionStatement ResolveStatement (BlockContext bc)
+               {
+                       statement_resolve = true;
+                       var es = base.ResolveStatement (bc);
+                       statement_resolve = false;
+
+                       return es;
+               }
+
                protected override Expression DoResolve (ResolveContext rc)
                {
                        ResolveConditionalAccessReceiver (rc);
@@ -7111,7 +7190,7 @@ namespace Mono.CSharp
 
                        var method = mg.BestCandidate;
                        type = mg.BestCandidateReturnType;
-                       if (conditional_access_receiver)
+                       if (conditional_access_receiver && !statement_resolve)
                                type = LiftMemberType (ec, type);
 
                        if (arguments == null && method.DeclaringType.BuiltinType == BuiltinTypeSpec.Type.Object && method.Name == Destructor.MetadataName) {
@@ -7545,14 +7624,21 @@ namespace Mono.CSharp
                        bool is_value_type = type.IsStructOrEnum;
                        VariableReference vr = target as VariableReference;
 
+                       bool prepare_await = ec.HasSet (BuilderContext.Options.AsyncBody) && arguments?.ContainsEmitWithAwait () == true;
+
                        if (target != null && is_value_type && (vr != null || method == null)) {
+                               if (prepare_await) {
+                                       arguments = arguments.Emit (ec, false, true);
+                                       prepare_await = false;
+                               }
+                               
                                target.AddressOf (ec, AddressOp.Store);
                        } else if (vr != null && vr.IsRef) {
                                vr.EmitLoad (ec);
                        }
 
                        if (arguments != null) {
-                               if (ec.HasSet (BuilderContext.Options.AsyncBody) && (arguments.Count > (this is NewInitialize ? 0 : 1)) && arguments.ContainsEmitWithAwait ())
+                               if (prepare_await)
                                        arguments = arguments.Emit (ec, false, true);
 
                                arguments.Emit (ec);
@@ -7862,6 +7948,8 @@ namespace Mono.CSharp
                {
                }
 
+               public bool NoEmptyInterpolation { get; set; }
+
                public ComposedTypeSpecifier Rank {
                        get {
                                return this.rank;
@@ -8072,11 +8160,21 @@ namespace Mono.CSharp
                        if (element == null)
                                return null;
 
-                       if (element is CompoundAssign.TargetExpression) {
-                               if (first_emit != null)
-                                       throw new InternalErrorException ("Can only handle one mutator at a time");
-                               first_emit = element;
-                               element = first_emit_temp = new LocalTemporary (element.Type);
+                       var te = element as CompoundAssign.TargetExpression;
+                       if (te != null) {
+                               for (int i = 1; i < initializers.Count; ++i) {
+                                       if (initializers [i].ContainsEmitWithAwait ()) {
+                                               te.RequiresEmitWithAwait = true;
+                                               break;
+                                       }
+                               }
+
+                               if (!te.RequiresEmitWithAwait) {
+                                       if (first_emit != null)
+                                               throw new InternalErrorException ("Can only handle one mutator at a time");
+                                       first_emit = element;
+                                       element = first_emit_temp = new LocalTemporary (element.Type);
+                               }
                        }
 
                        return Convert.ImplicitConversionRequired (
@@ -8398,7 +8496,7 @@ namespace Mono.CSharp
 
                public override void Emit (EmitContext ec)
                {
-                       if (EmitOptimizedEmpty (ec))
+                       if (!NoEmptyInterpolation && EmitOptimizedEmpty (ec))
                                return;
 
                        var await_field = EmitToFieldSource (ec);
@@ -9937,7 +10035,7 @@ namespace Mono.CSharp
 
                        TypeSpec nested = null;
                        while (expr_type != null) {
-                               nested = MemberCache.FindNestedType (expr_type, Name, Arity);
+                               nested = MemberCache.FindNestedType (expr_type, Name, Arity, false);
                                if (nested == null) {
                                        if (expr_type == tnew_expr) {
                                                Error_IdentifierNotFound (rc, expr_type);
@@ -9945,7 +10043,7 @@ namespace Mono.CSharp
                                        }
 
                                        expr_type = tnew_expr;
-                                       nested = MemberCache.FindNestedType (expr_type, Name, Arity);
+                                       nested = MemberCache.FindNestedType (expr_type, Name, Arity, false);
                                        ErrorIsInaccesible (rc, nested.GetSignatureForError (), loc);
                                        break;
                                }
@@ -9986,7 +10084,7 @@ namespace Mono.CSharp
 
                public void Error_IdentifierNotFound (IMemberContext rc, TypeSpec expr_type)
                {
-                       var nested = MemberCache.FindNestedType (expr_type, Name, -System.Math.Max (1, Arity));
+                       var nested = MemberCache.FindNestedType (expr_type, Name, -System.Math.Max (1, Arity), false);
 
                        if (nested != null) {
                                Error_TypeArgumentsCannotBeUsed (rc, nested, expr.Location);
@@ -10458,8 +10556,12 @@ namespace Mono.CSharp
                        }
 
                        type = ac.Element;
-                       if (type.IsPointer && !ec.IsUnsafe) {
-                               UnsafeError (ec, ea.Location);
+                       if (type.IsPointer) {
+                               if (ec.CurrentIterator != null) {
+                                       UnsafeInsideIteratorError (ec, ea.Location);
+                               } else if (!ec.IsUnsafe) {
+                                       UnsafeError (ec, ea.Location);
+                               }
                        }
 
                        if (conditional_access_receiver)
@@ -10925,9 +11027,18 @@ namespace Mono.CSharp
 
                #region IBaseMembersProvider Members
 
-               IList<MemberSpec> OverloadResolver.IBaseMembersProvider.GetBaseMembers (TypeSpec baseType)
+               IList<MemberSpec> OverloadResolver.IBaseMembersProvider.GetBaseMembers (TypeSpec type)
                {
-                       return baseType == null ? null : MemberCache.FindMembers (baseType, MemberCache.IndexerNameAlias, false);
+                       var baseType = type.BaseType;
+                       var members = baseType == null ? null : MemberCache.FindMembers (baseType, MemberCache.IndexerNameAlias, false);
+
+                       if (members == null && !type.IsInterface) {
+                               var tps = queried_type as TypeParameterSpec;
+                               if (tps != null)
+                                       members = MemberCache.FindInterfaceMembers (tps, MemberCache.IndexerNameAlias);
+                       }
+
+                       return members;
                }
 
                IParametersMember OverloadResolver.IBaseMembersProvider.GetOverrideMemberParameters (MemberSpec member)
@@ -11368,7 +11479,10 @@ namespace Mono.CSharp
                                if (!(ec.CurrentMemberDefinition is Field) && !TypeManager.VerifyUnmanaged (ec.Module, type, loc))
                                        return null;
 
-                               if (!ec.IsUnsafe) {
+                               var rc = ec as ResolveContext;
+                               if (rc?.CurrentIterator != null) {
+                                       UnsafeInsideIteratorError (ec.Module.Compiler.Report, loc);
+                               } else if (!ec.IsUnsafe) {
                                        UnsafeError (ec.Module.Compiler.Report, loc);
                                }
 
@@ -12138,21 +12252,23 @@ namespace Mono.CSharp
                                args);
                }
 
-               protected override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext rc)
                {
-                       Expression e = base.DoResolve (ec);
+                       Expression e = base.DoResolve (rc);
                        if (type == null)
                                return null;
 
                        if (type.IsDelegate) {
-                               ec.Report.Error (1958, Initializers.Location,
+                               rc.Report.Error (1958, Initializers.Location,
                                        "Object and collection initializers cannot be used to instantiate a delegate");
                        }
 
-                       Expression previous = ec.CurrentInitializerVariable;
-                       ec.CurrentInitializerVariable = new InitializerTargetExpression (this);
-                       initializers.Resolve (ec);
-                       ec.CurrentInitializerVariable = previous;
+                       Expression previous = rc.CurrentInitializerVariable;
+                       rc.CurrentInitializerVariable = new InitializerTargetExpression (this);
+                       using (rc.With (ResolveContext.Options.DontSetConditionalAccessReceiver, false)) {
+                               initializers.Resolve (rc);
+                       }
+                       rc.CurrentInitializerVariable = previous;
 
                        dynamic = e as DynamicExpressionStatement;
                        if (dynamic != null)
@@ -12229,6 +12345,7 @@ namespace Mono.CSharp
                                                throw new NotImplementedException ();
 
                                        sf = ec.GetTemporaryField (type);
+                                       sf.AutomaticallyReuse = false;
                                        sf.EmitAssign (ec, temp, false, false);
                                        temp_target = sf;
                                        temp.Release (ec);
@@ -12246,8 +12363,8 @@ namespace Mono.CSharp
                                temp.Release (ec);
 
                        if (sf != null)
-                               sf.IsAvailableForReuse = true;
-
+                               sf.PrepareCleanup (ec);
+                       
                        return true;
                }
 
@@ -12325,6 +12442,7 @@ namespace Mono.CSharp
                        int errors = ec.Report.Errors;
                        type.CreateContainer ();
                        type.DefineContainer ();
+                       type.ExpandBaseInterfaces ();
                        type.Define ();
                        if ((ec.Report.Errors - errors) == 0) {
                                parent.Module.AddAnonymousType (type);
@@ -12671,4 +12789,68 @@ namespace Mono.CSharp
                        return value;
                }
        }
+
+       class ThrowExpression : ExpressionStatement
+       {
+               Expression expr;
+
+               public ThrowExpression (Expression expr, Location loc)
+               {
+                       this.expr = expr;
+                       this.loc = loc;
+               }
+
+               protected override void CloneTo (CloneContext clonectx, Expression t)
+               {
+                       var target = (ThrowExpression)t;
+                       target.expr = expr.Clone (clonectx);
+               }
+
+               public override bool ContainsEmitWithAwait ()
+               {
+                       return expr.ContainsEmitWithAwait ();
+               }
+
+               public override Expression CreateExpressionTree (ResolveContext rc)
+               {
+                       rc.Report.Error (8188, loc, "An expression tree cannot not contain a throw expression");
+                       return expr;
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
+               {
+                       expr = expr.Resolve (rc, ResolveFlags.Type | ResolveFlags.VariableOrValue);
+
+                       if (expr == null)
+                               return null;
+
+                       expr = Throw.ConvertType (rc, expr);
+
+                       eclass = ExprClass.Value;
+                       type = InternalType.ThrowExpr;
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       EmitStatement (ec);
+               }
+
+               public override void EmitStatement (EmitContext ec)
+               {
+                       expr.Emit (ec);
+
+                       ec.Emit (OpCodes.Throw);
+               }
+
+               public override void FlowAnalysis (FlowAnalysisContext fc)
+               {
+                       expr.FlowAnalysis (fc);
+               }
+
+               public override Reachability MarkReachable (Reachability rc)
+               {
+                       return Reachability.CreateUnreachable ();
+               }
+       }
 }