Fix #77628.
[mono.git] / mcs / mcs / expression.cs
index 243d16ff95211467aff6587dcc13ef255d035d59..9005f6641e63b54bd625e0bb7dea0b21d758e0bc 100644 (file)
@@ -359,7 +359,7 @@ namespace Mono.CSharp {
                        
                        op_name = oper_names [(int) Oper];
 
-                       mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
+                       mg = MemberLookup (ec.ContainerType, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
                        
                        if (mg != null) {
                                Expression e = StaticCallExpr.MakeSimpleCall (
@@ -850,7 +850,7 @@ namespace Mono.CSharp {
                        else 
                                op_name = "op_Decrement";
 
-                       mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
+                       mg = MemberLookup (ec.ContainerType, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
 
                        if (mg != null) {
                                method = StaticCallExpr.MakeSimpleCall (
@@ -880,7 +880,7 @@ namespace Mono.CSharp {
                                if (expr == null)
                                        return null;
                        } else {
-                               expr.Error_UnexpectedKind (ec, "variable, indexer or property access", loc);
+                               expr.Error_UnexpectedKind (ec.DeclContainer, "variable, indexer or property access", loc);
                                return null;
                        }
 
@@ -1252,7 +1252,12 @@ namespace Mono.CSharp {
 
                        Error_CannotConvertType (etype, probe_type, loc);
                        return null;
-               }                               
+               }
+       
+               public override bool GetAttributableValue (out object value)
+               {
+                       return expr.GetAttributableValue (out value);
+               }
        }
        
        /// <summary>
@@ -1327,9 +1332,14 @@ namespace Mono.CSharp {
 
                        Constant c = expr as Constant;
                        if (c != null) {
-                               c = c.TryReduce (ec, type, loc);
-                               if (c != null)
-                                       return c;
+                               try {
+                                       c = c.TryReduce (ec, type, loc);
+                                       if (c != null)
+                                               return c;
+                               }
+                               catch (OverflowException) {
+                                       return null;
+                               }
                        }
 
                        if (type.IsPointer && !ec.InUnsafe) {
@@ -1832,10 +1842,10 @@ namespace Mono.CSharp {
                                string op = oper_names [(int) oper];
                                
                                MethodGroupExpr union;
-                               left_operators = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc);
+                               left_operators = MemberLookup (ec.ContainerType, l, op, MemberTypes.Method, AllBindingFlags, loc);
                                if (r != l){
                                        right_operators = MemberLookup (
-                                               ec, r, op, MemberTypes.Method, AllBindingFlags, loc);
+                                               ec.ContainerType, r, op, MemberTypes.Method, AllBindingFlags, loc);
                                        union = Invocation.MakeUnionSet (left_operators, right_operators, loc);
                                } else
                                        union = (MethodGroupExpr) left_operators;
@@ -3987,15 +3997,17 @@ namespace Mono.CSharp {
                                        return false;
                                }
 
+                               int errors = Report.Errors;
                                Expr = Expr.DoResolveLValue (ec, Expr);
-                               if (Expr == null)
+                               if (Expr == null && errors == Report.Errors)
                                        Error_LValueRequired (loc);
                        } else if (ArgType == AType.Out) {
+                               int errors = Report.Errors;
                                ec.InRefOutArgumentResolving = true;
                                Expr = Expr.DoResolveLValue (ec, EmptyExpression.OutAccess);
                                ec.InRefOutArgumentResolving = false;
 
-                               if (Expr == null)
+                               if (Expr == null && errors == Report.Errors)
                                        Error_LValueRequired (loc);
                        }
                        else
@@ -4028,20 +4040,6 @@ namespace Mono.CSharp {
                                        }
                                }
                        }
-
-                       if (Expr.eclass != ExprClass.Variable){
-                               //
-                               // We just probe to match the CSC output
-                               //
-                               if (Expr.eclass == ExprClass.PropertyAccess ||
-                                   Expr.eclass == ExprClass.IndexerAccess){
-                                       Report.Error (206, loc, "A property or indexer `{0}' may not be passed as an out or ref parameter",
-                                               Expr.GetSignatureForError ());
-                               } else {
-                                       Error_LValueRequired (loc);
-                               }
-                               return false;
-                       }
                                
                        return true;
                }
@@ -4590,6 +4588,8 @@ namespace Mono.CSharp {
                                nmethods = j;
                        }
 
+                       int applicable_errors = Report.Errors;
+                       
                        //
                        // First we construct the set of applicable methods
                        //
@@ -4633,6 +4633,9 @@ namespace Mono.CSharp {
                                }
                        }
 
+                       if (applicable_errors != Report.Errors)
+                               return null;
+                       
                        int candidate_top = candidates.Count;
 
                        if (applicable_type == null) {
@@ -5172,14 +5175,14 @@ namespace Mono.CSharp {
                /// <summary>
                /// This checks the ConditionalAttribute on the method 
                /// </summary>
-               static bool IsMethodExcluded (MethodBase method, EmitContext ec)
+               static bool IsMethodExcluded (MethodBase method)
                {
                        if (method.IsConstructor)
                                return false;
 
                        IMethodData md = TypeManager.GetMethod (method);
                        if (md != null)
-                               return md.IsExcluded (ec);
+                               return md.IsExcluded ();
 
                        // For some methods (generated by delegate class) GetMethod returns null
                        // because they are not included in builder_to_method table
@@ -5248,7 +5251,7 @@ namespace Mono.CSharp {
                                        method = TypeManager.void_array_copyto_array_int;
                        }
 
-                       if (ec.TestObsoleteMethodUsage) {
+                       if (!ec.IsInObsoleteScope) {
                                //
                                // This checks ObsoleteAttribute on the method and on the declaring type
                                //
@@ -5263,7 +5266,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (IsMethodExcluded (method, ec))
+                       if (IsMethodExcluded (method))
                                return; 
                        
                        if (!is_static){
@@ -5690,7 +5693,7 @@ namespace Mono.CSharp {
                        MethodGroupExpr mg = ml as MethodGroupExpr;
 
                        if (mg == null) {
-                               ml.Error_UnexpectedKind (ec, "method group", loc);
+                               ml.Error_UnexpectedKind (ec.DeclContainer, "method group", loc);
                                return null;
                        }
 
@@ -5704,7 +5707,7 @@ namespace Mono.CSharp {
                        method = Invocation.OverloadResolve (ec, mg, Arguments, false, loc);
                        if (method == null) {
                                if (almostMatchedMembers.Count != 0)
-                                       MemberLookupFailed (ec, type, type, ".ctor", null, true, loc);
+                                       MemberLookupFailed (ec.ContainerType, type, type, ".ctor", null, true, loc);
                                return null;
                        }
 
@@ -6021,10 +6024,7 @@ namespace Mono.CSharp {
                public bool ValidateInitializers (EmitContext ec, Type array_type)
                {
                        if (initializers == null) {
-                               if (expect_initializers)
-                                       return false;
-                               else
-                                       return true;
+                               return !expect_initializers;
                        }
                        
                        if (underlying_type == null)
@@ -6037,17 +6037,12 @@ namespace Mono.CSharp {
                        array_data = new ArrayList ();
                        bounds = new Hashtable ();
                        
-                       bool ret;
-
                        if (arguments != null) {
-                               ret = CheckIndices (ec, initializers, 0, true);
-                               return ret;
+                               return CheckIndices (ec, initializers, 0, true);
                        } else {
                                arguments = new ArrayList ();
 
-                               ret = CheckIndices (ec, initializers, 0, false);
-                               
-                               if (!ret)
+                               if (!CheckIndices (ec, initializers, 0, false))
                                        return false;
                                
                                UpdateIndices (ec);
@@ -6057,7 +6052,7 @@ namespace Mono.CSharp {
                                        return false;
                                }
 
-                               return ret;
+                               return true;
                        }
                }
 
@@ -6143,11 +6138,11 @@ namespace Mono.CSharp {
                        if (is_builtin_type) {
                                Expression ml;
                                
-                               ml = MemberLookup (ec, type, ".ctor", MemberTypes.Constructor,
+                               ml = MemberLookup (ec.ContainerType, type, ".ctor", MemberTypes.Constructor,
                                                   AllBindingFlags, loc);
                                
                                if (!(ml is MethodGroupExpr)) {
-                                       ml.Error_UnexpectedKind (ec, "method group", loc);
+                                       ml.Error_UnexpectedKind (ec.DeclContainer, "method group", loc);
                                        return null;
                                }
                                
@@ -6515,32 +6510,33 @@ namespace Mono.CSharp {
                        }
                }
 
-               public object EncodeAsAttribute ()
+               public override bool GetAttributableValue (out object value)
                {
                        if (!is_one_dimensional){
-                               Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
-                               return null;
+//                             Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
+                               return base.GetAttributableValue (out value);
                        }
 
-                       if (array_data == null){
-                               Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
-                               return null;
+                       if (array_data == null) {
+                               Constant c = (Constant)((Argument)arguments [0]).Expr;
+                               if (c.IsDefaultValue) {
+                                       value = new object [0];
+                                       return true;
+                               }
+//                             Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
+                               return base.GetAttributableValue (out value);
                        }
                        
                        object [] ret = new object [array_data.Count];
-                       int i = 0;
-                       foreach (Expression e in array_data){
-                               object v;
-                               
-                               if (e is NullLiteral)
-                                       v = null;
-                               else {
-                                       if (!Attribute.GetAttributeArgumentExpression (e, Location, array_element_type, out v))
-                                               return null;
+                       for (int i = 0; i < ret.Length; ++i)
+                       {
+                               if (!((Expression)array_data [i]).GetAttributableValue (out ret [i])) {
+                                       value = null;
+                                       return false;
                                }
-                               ret [i++] = v;
                        }
-                       return ret;
+                       value = ret;
+                       return true;
                }
        }
        
@@ -6801,7 +6797,7 @@ namespace Mono.CSharp {
        ///   Implements the typeof operator
        /// </summary>
        public class TypeOf : Expression {
-               public Expression QueriedType;
+               readonly Expression QueriedType;
                protected Type typearg;
                
                public TypeOf (Expression queried_type, Location l)
@@ -6841,8 +6837,10 @@ namespace Mono.CSharp {
                        ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
                }
 
-               public Type TypeArg { 
-                       get { return typearg; }
+               public override bool GetAttributableValue (out object value)
+               {
+                       value = typearg;
+                       return true;
                }
        }
 
@@ -6931,13 +6929,13 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+               public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
                        if (alias == "global")
                                return new MemberAccess (RootNamespace.Global, identifier, loc).ResolveAsTypeStep (ec, silent);
 
                        int errors = Report.Errors;
-                       FullNamedExpression fne = ec.DeclSpace.NamespaceEntry.LookupAlias (alias);
+                       FullNamedExpression fne = ec.DeclContainer.NamespaceEntry.LookupAlias (alias);
                        if (fne == null) {
                                if (errors == Report.Errors)
                                        Report.Error (432, loc, "Alias `{0}' not found", alias);
@@ -6958,7 +6956,7 @@ namespace Mono.CSharp {
                                fne = RootNamespace.Global;
                        } else {
                                int errors = Report.Errors;
-                               fne = ec.DeclSpace.NamespaceEntry.LookupAlias (alias);
+                               fne = ec.DeclContainer.NamespaceEntry.LookupAlias (alias);
                                if (fne == null) {
                                        if (errors == Report.Errors)
                                                Report.Error (432, loc, "Alias `{0}' not found", alias);
@@ -7043,7 +7041,7 @@ namespace Mono.CSharp {
 
                        if (new_expr is Namespace) {
                                Namespace ns = (Namespace) new_expr;
-                               FullNamedExpression retval = ns.Lookup (ec.DeclSpace, Identifier, loc);
+                               FullNamedExpression retval = ns.Lookup (ec.DeclContainer, Identifier, loc);
                                if (retval == null)
                                        Report.Error (234, loc, "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
                                                Identifier, ns.FullName);
@@ -7112,21 +7110,21 @@ namespace Mono.CSharp {
                        return DoResolve (ec, right_side);
                }
 
-               public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+               public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
                        return ResolveNamespaceOrType (ec, silent);
                }
 
-               public FullNamedExpression ResolveNamespaceOrType (EmitContext ec, bool silent)
+               public FullNamedExpression ResolveNamespaceOrType (IResolveContext rc, bool silent)
                {
-                       FullNamedExpression new_expr = expr.ResolveAsTypeStep (ec, silent);
+                       FullNamedExpression new_expr = expr.ResolveAsTypeStep (rc, silent);
 
                        if (new_expr == null)
                                return null;
 
                        if (new_expr is Namespace) {
                                Namespace ns = (Namespace) new_expr;
-                               FullNamedExpression retval = ns.Lookup (ec.DeclSpace, Identifier, loc);
+                               FullNamedExpression retval = ns.Lookup (rc.DeclContainer, Identifier, loc);
                                if (!silent && retval == null)
                                        Report.Error (234, loc, "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
                                                Identifier, ns.FullName);
@@ -7141,10 +7139,10 @@ namespace Mono.CSharp {
                                return null;
                        }
                        
-                       Expression member_lookup = MemberLookup (ec, expr_type, expr_type, Identifier, loc);
+                       Expression member_lookup = MemberLookup (rc.DeclContainer.TypeBuilder, expr_type, expr_type, Identifier, loc);
                        if (member_lookup == null) {
                                int errors = Report.Errors;
-                               MemberLookupFailed (ec, expr_type, expr_type, Identifier, null, false, loc);
+                               MemberLookupFailed (rc.DeclContainer.TypeBuilder, expr_type, expr_type, Identifier, null, false, loc);
 
                                if (!silent && errors == Report.Errors) {
                                        Report.Error (426, loc, "The nested type `{0}' does not exist in the type `{1}'",
@@ -7154,11 +7152,11 @@ namespace Mono.CSharp {
                        }
 
                        if (!(member_lookup is TypeExpr)) {
-                               new_expr.Error_UnexpectedKind (ec, "type", loc);
+                               new_expr.Error_UnexpectedKind (rc.DeclContainer, "type", loc);
                                return null;
                        } 
 
-                       member_lookup = member_lookup.Resolve (ec, ResolveFlags.Type);
+                       member_lookup = member_lookup.ResolveAsTypeTerminal (rc, silent);
                        return (member_lookup as TypeExpr);
                }
 
@@ -7965,6 +7963,19 @@ namespace Mono.CSharp {
 
                public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
+                       if (right_side == EmptyExpression.OutAccess) {
+                               Report.Error (206, loc, "A property or indexer `{0}' may not be passed as an out or ref parameter",
+                                             GetSignatureForError ());
+                               return null;
+                       }
+
+                       // if the indexer returns a value type, and we try to set a field in it
+                       if (right_side == EmptyExpression.LValueMemberAccess) {
+                               Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable",
+                                             GetSignatureForError ());
+                               return null;
+                       }
+
                        ArrayList AllSetters = new ArrayList();
                        if (!CommonResolve (ec))
                                return null;
@@ -8078,6 +8089,12 @@ namespace Mono.CSharp {
                {
                        Emit (ec, false);
                }
+
+               public override string GetSignatureForError ()
+               {
+                       // FIXME: print the argument list of the indexer
+                       return instance_expr.GetSignatureForError () + ".this[...]";
+               }
        }
 
        /// <summary>
@@ -8139,10 +8156,10 @@ namespace Mono.CSharp {
                                return null;
                        }
                        
-                       member_lookup = MemberLookup (ec, ec.ContainerType, null, base_type, member,
+                       member_lookup = MemberLookup (ec.ContainerType, null, base_type, member,
                                                      AllMemberTypes, AllBindingFlags, loc);
                        if (member_lookup == null) {
-                               MemberLookupFailed (ec, base_type, base_type, member, null, true, loc);
+                               MemberLookupFailed (ec.ContainerType, base_type, base_type, member, null, true, loc);
                                return null;
                        }
 
@@ -8335,17 +8352,13 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               public override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
                {
                        TypeExpr lexpr = left.ResolveAsTypeTerminal (ec, false);
                        if (lexpr == null)
                                return null;
 
-                       bool old = ec.TestObsoleteMethodUsage;
-                       ec.TestObsoleteMethodUsage = false;
                        Type ltype = lexpr.ResolveType (ec);
-                       ec.TestObsoleteMethodUsage = old;
-
                        if ((ltype == TypeManager.void_type) && (dim != "*")) {
                                Report.Error (1547, Location,
                                              "Keyword 'void' cannot be used in this context");
@@ -8361,7 +8374,7 @@ namespace Mono.CSharp {
                                throw new InternalErrorException ("Couldn't create computed type " + ltype + dim);
                        }
 
-                       if (!ec.InUnsafe && type.IsPointer){
+                       if (type.IsPointer && !ec.IsInUnsafeScope){
                                UnsafeError (loc);
                                return null;
                        }