*** merged revision 57555 from mcs
[mono.git] / mcs / gmcs / expression.cs
index fc32bfb5e02a3dbb8b9d92443ffc24e8fc846db1..32dce23aa9767212dfac6dd09c71341471cf1ff0 100644 (file)
@@ -592,7 +592,7 @@ namespace Mono.CSharp {
                        if (Expr == null)
                                return null;
 
-                       if (TypeManager.IsNullableType (Expr.Type))
+                       if (TypeManager.IsNullableValueType (Expr.Type))
                                return new Nullable.LiftedUnaryOperator (Oper, Expr, loc).Resolve (ec);
 
                        eclass = ExprClass.Value;
@@ -899,7 +899,7 @@ namespace Mono.CSharp {
 
                        eclass = ExprClass.Value;
 
-                       if (TypeManager.IsNullableType (expr.Type))
+                       if (TypeManager.IsNullableValueType (expr.Type))
                                return new Nullable.LiftedUnaryMutator (mode, expr, loc).Resolve (ec);
 
                        return ResolveOperator (ec);
@@ -1055,8 +1055,6 @@ namespace Mono.CSharp {
                        probe_type_expr = ProbeType.ResolveAsTypeTerminal (ec, false);
                        if (probe_type_expr == null)
                                return null;
-                       if (probe_type_expr.ResolveType (ec) == null)
-                               return null;
 
                        expr = expr.Resolve (ec);
                        if (expr == null)
@@ -1184,6 +1182,9 @@ namespace Mono.CSharp {
                                        action = Action.AlwaysFalse;
                                else
                                        action = Action.Probe;
+                       } else if (etype.ContainsGenericParameters || probe_type.ContainsGenericParameters) {
+                               expr = new BoxedCast (expr, etype);
+                               action = Action.Probe;
                        } else {
                                action = Action.AlwaysFalse;
                                warning_never_matches = true;
@@ -1288,9 +1289,20 @@ namespace Mono.CSharp {
                                return this;
                        }
 
+                       if (etype.ContainsGenericParameters || type.ContainsGenericParameters) {
+                               expr = new BoxedCast (expr, etype);
+                               do_isinst = true;
+                               return this;
+                       }
+
                        Error_CannotConvertType (etype, type, loc);
                        return null;
-               }                               
+               }
+       
+               public override bool GetAttributableValue (out object value)
+               {
+                       return expr.GetAttributableValue (out value);
+               }
        }
        
        /// <summary>
@@ -1313,6 +1325,10 @@ namespace Mono.CSharp {
                        this.target_type = cast_type;
                        this.expr = expr;
                        this.loc = loc;
+
+                       if (target_type == TypeManager.system_void_expr) {
+                               Report.Error (1547, loc, "Keyword `void' cannot be used in this context");
+                       }
                }
 
                public Expression TargetType {
@@ -1354,7 +1370,7 @@ namespace Mono.CSharp {
                        if (target == null)
                                return null;
                        
-                       type = target.ResolveType (ec);
+                       type = target.Type;
 
                        if (type.IsAbstract && type.IsSealed) {
                                Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
@@ -1365,9 +1381,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) {
@@ -1612,7 +1633,7 @@ namespace Mono.CSharp {
                                                        if (ll >= 0)
                                                                right = new ULongConstant ((ulong) ll, right.Location);
                                                } else {
-                                                       e = Convert.ImplicitNumericConversion (ec, right, l);
+                                                       e = Convert.ImplicitNumericConversion (right, l);
                                                        if (e != null)
                                                                right = e;
                                                }
@@ -1629,7 +1650,7 @@ namespace Mono.CSharp {
                                                if (ll > 0)
                                                        left = new ULongConstant ((ulong) ll, right.Location);
                                        } else {
-                                               e = Convert.ImplicitNumericConversion (ec, left, r);
+                                               e = Convert.ImplicitNumericConversion (left, r);
                                                if (e != null)
                                                        left = e;
                                        }
@@ -2030,9 +2051,9 @@ namespace Mono.CSharp {
                                        // Also, a standard conversion must exist from either one
                                        //
                                        bool left_to_right =
-                                               Convert.ImplicitStandardConversionExists (ec, left, r);
+                                               Convert.ImplicitStandardConversionExists (left, r);
                                        bool right_to_left = !left_to_right &&
-                                               Convert.ImplicitStandardConversionExists (ec, right, l);
+                                               Convert.ImplicitStandardConversionExists (right, l);
 
                                        if (!left_to_right && !right_to_left) {
                                                Error_OperatorCannotBeApplied ();
@@ -2466,7 +2487,9 @@ namespace Mono.CSharp {
                                        return e;
                        }
 
-                       if (TypeManager.IsNullableType (left.Type) || TypeManager.IsNullableType (right.Type))
+                       Type ltype = left.Type, rtype = right.Type;
+                       if (ltype.IsValueType && rtype.IsValueType &&
+                           (TypeManager.IsNullableType (ltype) || TypeManager.IsNullableType (rtype)))
                                return new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
 
                        // Comparison warnings
@@ -2483,6 +2506,11 @@ namespace Mono.CSharp {
                        return ResolveOperator (ec);
                }
 
+               public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
+               {
+                       return null;
+               }
+
                private void CheckUselessComparison (Constant c, Type type)
                {
                        if (c == null || !IsTypeIntegral (type)
@@ -3349,7 +3377,7 @@ namespace Mono.CSharp {
                        if (expr == null)
                                return null;
 
-                       if (TypeManager.IsNullableType (expr.Type))
+                       if (TypeManager.IsNullableValueType (expr.Type))
                                return new Nullable.LiftedConditional (expr, trueExpr, falseExpr, loc).Resolve (ec);
                        
                        if (expr.Type != TypeManager.bool_type){
@@ -3419,6 +3447,11 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
+               {
+                       return null;
+               }
+
                public override void Emit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
@@ -4653,7 +4686,7 @@ namespace Mono.CSharp {
                                                 ref MethodBase candidate)
                {
                        if (!me.HasTypeArguments &&
-                           !TypeManager.InferTypeArguments (ec, arguments, ref candidate))
+                           !TypeManager.InferTypeArguments (arguments, ref candidate))
                                return false;
 
                        return IsApplicable (ec, arguments, arg_count, candidate);
@@ -4846,7 +4879,7 @@ namespace Mono.CSharp {
                                        if (pd.Count != arg_count)
                                                continue;
 
-                                       if (!TypeManager.InferTypeArguments (ec, Arguments, ref c))
+                                       if (!TypeManager.InferTypeArguments (Arguments, ref c))
                                                continue;
 
                                        VerifyArgumentsCompat (ec, Arguments, arg_count,
@@ -4872,7 +4905,7 @@ namespace Mono.CSharp {
                                                if (pd.Count != arg_count)
                                                        continue;
 
-                                               if (TypeManager.InferTypeArguments (ec, Arguments, ref c))
+                                               if (TypeManager.InferTypeArguments (Arguments, ref c))
                                                        continue;
 
                                                Report.Error (
@@ -5479,7 +5512,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
                                //
@@ -5878,7 +5911,7 @@ namespace Mono.CSharp {
                        if (texpr == null)
                                return null;
 
-                       type = texpr.ResolveType (ec);
+                       type = texpr.Type;
 
                        if (Arguments == null) {
                                Expression c = Constantify (type);
@@ -6307,10 +6340,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)
@@ -6323,17 +6353,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);
@@ -6343,7 +6368,7 @@ namespace Mono.CSharp {
                                        return false;
                                }
 
-                               return ret;
+                               return true;
                        }
                }
 
@@ -6374,7 +6399,7 @@ namespace Mono.CSharp {
                        if (array_type_expr == null)
                                return false;
 
-                       type = array_type_expr.ResolveType (ec);                
+                       type = array_type_expr.Type;
                        underlying_type = TypeManager.GetElementType (type);
                        dimensions = type.GetArrayRank ();
 
@@ -6804,32 +6829,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;
                }
        }
        
@@ -7094,7 +7120,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)
@@ -7109,7 +7135,7 @@ namespace Mono.CSharp {
                        if (texpr == null)
                                return null;
 
-                       typearg = texpr.ResolveType (ec);
+                       typearg = texpr.Type;
 
                        if (typearg == TypeManager.void_type) {
                                Error (673, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
@@ -7134,8 +7160,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;
                }
        }
 
@@ -7182,7 +7210,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       type_queried = texpr.ResolveType (ec);
+                       type_queried = texpr.Type;
 
                        int size_of = GetTypeSize (type_queried);
                        if (size_of > 0) {
@@ -7229,7 +7257,7 @@ 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);
@@ -7453,14 +7481,14 @@ 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;
@@ -7469,20 +7497,20 @@ namespace Mono.CSharp {
 
                        if (new_expr is Namespace) {
                                Namespace ns = (Namespace) new_expr;
-                               FullNamedExpression retval = ns.Lookup (ec.DeclContainer, lookup_id, loc);
+                               FullNamedExpression retval = ns.Lookup (rc.DeclContainer, lookup_id, loc);
                                if ((retval != null) && (args != null))
-                                       retval = new ConstructedType (retval, args, loc).ResolveAsTypeStep (ec, false);
+                                       retval = new ConstructedType (retval, args, loc).ResolveAsTypeStep (rc, false);
                                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);
                                return retval;
                        }
 
-                       TypeExpr tnew_expr = new_expr.ResolveAsTypeTerminal (ec, false);
+                       TypeExpr tnew_expr = new_expr.ResolveAsTypeTerminal (rc, false);
                        if (tnew_expr == null)
                                return null;
 
-                       Type expr_type = tnew_expr.ResolveType (ec);
+                       Type expr_type = tnew_expr.Type;
 
                        if (expr_type.IsPointer){
                                Error (23, "The `.' operator can not be applied to pointer operands (" +
@@ -7491,11 +7519,11 @@ namespace Mono.CSharp {
                        }
 
                        Expression member_lookup = MemberLookup (
-                               ec.ContainerType, expr_type, expr_type, lookup_id,
+                               rc.DeclContainer.TypeBuilder, expr_type, expr_type, lookup_id,
                                MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
                        if (member_lookup == null) {
                                int errors = Report.Errors;
-                               MemberLookupFailed (ec.ContainerType, expr_type, expr_type, lookup_id, null, false, loc);
+                               MemberLookupFailed (rc.DeclContainer.TypeBuilder, expr_type, expr_type, lookup_id, null, false, loc);
 
                                if (!silent && errors == Report.Errors) {
                                        Report.Error (426, loc, "The nested type `{0}' does not exist in the type `{1}'",
@@ -7505,11 +7533,11 @@ namespace Mono.CSharp {
                        }
 
                        if (!(member_lookup is TypeExpr)) {
-                               new_expr.Error_UnexpectedKind (ec.DeclContainer, "type", loc);
+                               new_expr.Error_UnexpectedKind (rc.DeclContainer, "type", loc);
                                return null;
                        }
 
-                       TypeExpr texpr = member_lookup.ResolveAsTypeTerminal (ec, false);
+                       TypeExpr texpr = member_lookup.ResolveAsTypeTerminal (rc, false);
                        if (texpr == null)
                                return null;
 
@@ -7529,7 +7557,7 @@ namespace Mono.CSharp {
 
                        if (the_args != null) {
                                ConstructedType ctype = new ConstructedType (texpr.Type, the_args, loc);
-                               return ctype.ResolveAsTypeStep (ec, false);
+                               return ctype.ResolveAsTypeStep (rc, false);
                        }
 
                        return texpr;
@@ -8784,17 +8812,13 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               protected 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;
-
+                       Type ltype = lexpr.Type;
                        if ((ltype == TypeManager.void_type) && (dim != "*")) {
                                Report.Error (1547, Location,
                                              "Keyword 'void' cannot be used in this context");
@@ -8821,7 +8845,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;
                        }
@@ -8976,7 +9000,7 @@ namespace Mono.CSharp {
                        if (texpr == null)
                                return null;
 
-                       otype = texpr.ResolveType (ec);
+                       otype = texpr.Type;
 
                        if (!TypeManager.VerifyUnManaged (otype, loc))
                                return null;