Fixed format of VS2005 project file. Please revert if it breaks MSBuild compilation...
[mono.git] / mcs / gmcs / generic.cs
index 608f6aae7182036ac434ddffd9dc3795a92f7190..6e7cff235998a47bff3d2b214758c70da7a866e6 100644 (file)
@@ -368,7 +368,7 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Resolve the constraints into actual types.
                /// </summary>
-               public bool ResolveTypes (EmitContext ec)
+               public bool ResolveTypes (IResolveContext ec)
                {
                        if (resolved_types)
                                return true;
@@ -390,13 +390,17 @@ namespace Mono.CSharp {
                                        return false;
                        }
 
-                       foreach (TypeExpr iface_constraint in iface_constraints) {
-                               if (iface_constraint.ResolveType (ec) == null)
+                       for (int i = 0; i < iface_constraints.Count; ++i) {
+                               TypeExpr iface_constraint = (TypeExpr) iface_constraints [i];
+                               iface_constraint = iface_constraint.ResolveAsTypeTerminal (ec, false);
+                               if (iface_constraint == null)
                                        return false;
+                               iface_constraints [i] = iface_constraint;
                        }
 
                        if (class_constraint != null) {
-                               if (class_constraint.ResolveType (ec) == null)
+                               class_constraint = class_constraint.ResolveAsTypeTerminal (ec, false);
+                               if (class_constraint == null)
                                        return false;
                        }
 
@@ -412,17 +416,17 @@ namespace Mono.CSharp {
                ///      where T : class
                ///      where U : T, struct
                /// </summary>
-               public bool CheckDependencies (EmitContext ec)
+               public bool CheckDependencies ()
                {
                        foreach (TypeParameterExpr expr in type_param_constraints) {
-                               if (!CheckDependencies (expr.TypeParameter, ec))
+                               if (!CheckDependencies (expr.TypeParameter))
                                        return false;
                        }
 
                        return true;
                }
 
-               bool CheckDependencies (TypeParameter tparam, EmitContext ec)
+               bool CheckDependencies (TypeParameter tparam)
                {
                        Constraints constraints = tparam.Constraints;
                        if (constraints == null)
@@ -456,7 +460,7 @@ namespace Mono.CSharp {
                                return true;
 
                        foreach (TypeParameterExpr expr in constraints.type_param_constraints) {
-                               if (!CheckDependencies (expr.TypeParameter, ec))
+                               if (!CheckDependencies (expr.TypeParameter))
                                        return false;
                        }
 
@@ -507,7 +511,7 @@ namespace Mono.CSharp {
                ///   method.  To do that, we're called on each of the implementing method's
                ///   type parameters.
                /// </summary>
-               public bool CheckInterfaceMethod (EmitContext ec, GenericConstraints gc)
+               public bool CheckInterfaceMethod (GenericConstraints gc)
                {
                        if (gc.Attributes != attrs)
                                return false;
@@ -596,6 +600,11 @@ namespace Mono.CSharp {
                        }
                }
 
+               // FIXME: This should be removed once we fix the handling of RootContext.Tree.Types
+               public override DeclSpace DeclContainer {
+                       get { return DeclSpace; }
+               }
+
                /// <summary>
                ///   This is the first method which is called during the resolving
                ///   process; we're called immediately after creating the type parameters
@@ -649,7 +658,7 @@ namespace Mono.CSharp {
                ///   Note that we may have circular dependencies on type parameters - this
                ///   is why Resolve() and ResolveType() are separate.
                /// </summary>
-               public bool ResolveType (EmitContext ec)
+               public bool ResolveType (IResolveContext ec)
                {
                        if (constraints != null) {
                                if (!constraints.ResolveTypes (ec)) {
@@ -666,7 +675,7 @@ namespace Mono.CSharp {
                ///   process.  We're called after everything is fully resolved and actually
                ///   register the constraints with SRE and the TypeManager.
                /// </summary>
-               public bool DefineType (EmitContext ec)
+               public bool DefineType (IResolveContext ec)
                {
                        return DefineType (ec, null, null, false);
                }
@@ -679,7 +688,7 @@ namespace Mono.CSharp {
                ///   The `builder', `implementing' and `is_override' arguments are only
                ///   applicable to method type parameters.
                /// </summary>
-               public bool DefineType (EmitContext ec, MethodBuilder builder,
+               public bool DefineType (IResolveContext ec, MethodBuilder builder,
                                        MethodInfo implementing, bool is_override)
                {
                        if (!ResolveType (ec))
@@ -710,7 +719,7 @@ namespace Mono.CSharp {
                                if (constraints != null) {
                                        if (temp_gc == null)
                                                ok = false;
-                                       else if (!constraints.CheckInterfaceMethod (ec, gc))
+                                       else if (!constraints.CheckInterfaceMethod (gc))
                                                ok = false;
                                } else {
                                        if (!is_override && (temp_gc != null))
@@ -764,10 +773,10 @@ namespace Mono.CSharp {
                ///      where T : class
                ///      where U : T, struct
                /// </summary>
-               public bool CheckDependencies (EmitContext ec)
+               public bool CheckDependencies ()
                {
                        if (constraints != null)
-                               return constraints.CheckDependencies (ec);
+                               return constraints.CheckDependencies ();
 
                        return true;
                }
@@ -780,15 +789,12 @@ namespace Mono.CSharp {
                ///   check that they're the same.
                ///   con
                /// </summary>
-               public bool UpdateConstraints (EmitContext ec, Constraints new_constraints)
+               public bool UpdateConstraints (IResolveContext ec, Constraints new_constraints)
                {
                        if (type == null)
                                throw new InvalidOperationException ();
 
-                       if (constraints == null) {
-                               new_constraints = constraints;
-                               return true;
-                       } else if (new_constraints == null)
+                       if (new_constraints == null)
                                return true;
 
                        if (!new_constraints.Resolve (ec))
@@ -796,7 +802,11 @@ namespace Mono.CSharp {
                        if (!new_constraints.ResolveTypes (ec))
                                return false;
 
-                       return constraints.CheckInterfaceMethod (ec, new_constraints);
+                       if (constraints != null) 
+                               return constraints.CheckInterfaceMethod (new_constraints);
+
+                       constraints = new_constraints;
+                       return true;
                }
 
                public void EmitAttributes ()
@@ -980,8 +990,14 @@ namespace Mono.CSharp {
                                if (t.IsGenericParameter)
                                        return dargs [t.GenericParameterPosition];
                                if (t.IsGenericType) {
+                                       Type[] args = t.GetGenericArguments ();
+                                       Type[] inflated = new Type [args.Length];
+
+                                       for (int i = 0; i < args.Length; i++)
+                                               inflated [i] = inflate (args [i]);
+
                                        t = t.GetGenericTypeDefinition ();
-                                       t = t.MakeGenericType (dargs);
+                                       t = t.MakeGenericType (inflated);
                                }
 
                                return t;
@@ -1057,7 +1073,7 @@ namespace Mono.CSharp {
 
                public void Error_CannotUseAsUnmanagedType (Location loc)
                {
-                       Report.Error (-203, loc, "Can not use type parameter as unamanged type");
+                       Report.Error (-203, loc, "Can not use type parameter as unmanaged type");
                }
        }
 
@@ -1632,18 +1648,22 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       MethodGroupExpr mg = Expression.MemberLookup (
-                               containerType, atype, ".ctor", MemberTypes.Constructor,
+                       TypeParameter tparam = TypeManager.LookupTypeParameter (atype);
+                       if (tparam != null)
+                               return tparam.HasConstructorConstraint;
+
+                       MemberList list = TypeManager.FindMembers (
+                               atype, MemberTypes.Constructor,
                                BindingFlags.Public | BindingFlags.Instance |
-                               BindingFlags.DeclaredOnly, loc)
-                               as MethodGroupExpr;
+                               BindingFlags.DeclaredOnly, null, null);
 
-                       if (!atype.IsAbstract && (mg != null) && mg.IsInstance) {
-                               foreach (MethodBase mb in mg.Methods) {
-                                       ParameterData pd = TypeManager.GetParameterData (mb);
-                                       if (pd.Count == 0)
-                                               return true;
-                               }
+                       if (atype.IsAbstract || (list == null))
+                               return false;
+
+                       foreach (MethodBase mb in list) {
+                               ParameterData pd = TypeManager.GetParameterData (mb);
+                               if ((pd.Count == 0) && mb.IsPublic && !mb.IsStatic)
+                                       return true;
                        }
 
                        return false;
@@ -1735,7 +1755,7 @@ namespace Mono.CSharp {
                Expression return_type;
                Parameters parameters;
 
-               public GenericMethod (NamespaceEntry ns, TypeContainer parent, MemberName name,
+               public GenericMethod (NamespaceEntry ns, DeclSpace parent, MemberName name,
                                      Expression return_type, Parameters parameters)
                        : base (ns, parent, name, null)
                {
@@ -1750,8 +1770,6 @@ namespace Mono.CSharp {
 
                public override bool Define ()
                {
-                       ec = new EmitContext (this, this, this, Location, null, null, ModFlags, false);
-
                        for (int i = 0; i < TypeParameters.Length; i++)
                                if (!TypeParameters [i].Resolve (this))
                                        return false;
@@ -1778,7 +1796,7 @@ namespace Mono.CSharp {
                                return false;
 
                        for (int i = 0; i < TypeParameters.Length; i++) {
-                               if (!TypeParameters [i].ResolveType (ec))
+                               if (!TypeParameters [i].ResolveType (this))
                                        return false;
                        }
 
@@ -1874,7 +1892,7 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        if (type.IsGenericParameter || TypeManager.IsValueType (type)) {
-                               LocalTemporary temp_storage = new LocalTemporary (ec, type);
+                               LocalTemporary temp_storage = new LocalTemporary (type);
 
                                temp_storage.AddressOf (ec, AddressOp.LoadStore);
                                ec.ig.Emit (OpCodes.Initobj, type);
@@ -2583,8 +2601,10 @@ namespace Mono.CSharp {
                                        break;
                                }
                        }
+
+                       // If none of the method parameters mention a generic parameter, we can't infer the generic parameters
                        if (!is_open)
-                               return true;
+                               return !TypeManager.IsGenericMethodDefinition (method);
 
                        Type[] infered_types = new Type [method_args.Length];
 
@@ -2645,6 +2665,14 @@ namespace Mono.CSharp {
                        return generic_nullable_type == DropGenericTypeArguments (t);
                }
 
+               public static bool IsNullableTypeOf (Type t, Type nullable)
+               {
+                       if (!IsNullableType (t))
+                               return false;
+
+                       return GetTypeArguments (t) [0] == nullable;
+               }
+
                public static bool IsNullableValueType (Type t)
                {
                        if (!IsNullableType (t))
@@ -2656,7 +2684,7 @@ namespace Mono.CSharp {
 
        public abstract class Nullable
        {
-               protected sealed class NullableInfo
+               public sealed class NullableInfo
                {
                        public readonly Type Type;
                        public readonly Type UnderlyingType;
@@ -2678,7 +2706,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               protected class Unwrap : Expression, IMemoryLocation, IAssignMethod
+               public class Unwrap : Expression, IMemoryLocation, IAssignMethod
                {
                        Expression expr;
                        NullableInfo info;
@@ -2686,10 +2714,15 @@ namespace Mono.CSharp {
                        LocalTemporary temp;
                        bool has_temp;
 
-                       public Unwrap (Expression expr, Location loc)
+                       protected Unwrap (Expression expr)
                        {
                                this.expr = expr;
-                               this.loc = loc;
+                               this.loc = expr.Location;
+                       }
+
+                       public static Unwrap Create (Expression expr, EmitContext ec)
+                       {
+                               return new Unwrap (expr).Resolve (ec) as Unwrap;
                        }
 
                        public override Expression DoResolve (EmitContext ec)
@@ -2698,7 +2731,7 @@ namespace Mono.CSharp {
                                if (expr == null)
                                        return null;
 
-                               temp = new LocalTemporary (ec, expr.Type);
+                               temp = new LocalTemporary (expr.Type);
 
                                info = new NullableInfo (expr.Type);
                                type = info.UnderlyingType;
@@ -2789,15 +2822,20 @@ namespace Mono.CSharp {
                        }
                }
 
-               protected class Wrap : Expression
+               public class Wrap : Expression
                {
                        Expression expr;
                        NullableInfo info;
 
-                       public Wrap (Expression expr, Location loc)
+                       protected Wrap (Expression expr)
                        {
                                this.expr = expr;
-                               this.loc = loc;
+                               this.loc = expr.Location;
+                       }
+
+                       public static Wrap Create (Expression expr, EmitContext ec)
+                       {
+                               return new Wrap (expr).Resolve (ec) as Wrap;
                        }
 
                        public override Expression DoResolve (EmitContext ec)
@@ -2824,11 +2862,11 @@ namespace Mono.CSharp {
                        }
                }
 
-               public class NullableLiteral : Expression, IMemoryLocation {
+               public class NullableLiteral : NullLiteral, IMemoryLocation {
                        public NullableLiteral (Type target_type, Location loc)
+                               : base (loc)
                        {
                                this.type = target_type;
-                               this.loc = loc;
 
                                eclass = ExprClass.Value;
                        }
@@ -2840,7 +2878,7 @@ namespace Mono.CSharp {
 
                        public override void Emit (EmitContext ec)
                        {
-                               LocalTemporary value_target = new LocalTemporary (ec, type);
+                               LocalTemporary value_target = new LocalTemporary (type);
 
                                value_target.AddressOf (ec, AddressOp.Store);
                                ec.ig.Emit (OpCodes.Initobj, type);
@@ -2849,7 +2887,7 @@ namespace Mono.CSharp {
 
                        public void AddressOf (EmitContext ec, AddressOp Mode)
                        {
-                               LocalTemporary value_target = new LocalTemporary (ec, type);
+                               LocalTemporary value_target = new LocalTemporary (type);
                                        
                                value_target.AddressOf (ec, AddressOp.Store);
                                ec.ig.Emit (OpCodes.Initobj, type);
@@ -2874,7 +2912,7 @@ namespace Mono.CSharp {
                                if (expr == null)
                                        return null;
 
-                               unwrap = (Unwrap) new Unwrap (expr, loc).Resolve (ec);
+                               unwrap = Unwrap.Create (expr, ec);
                                if (unwrap == null)
                                        return null;
 
@@ -2882,7 +2920,7 @@ namespace Mono.CSharp {
                                if (underlying == null)
                                        return null;
 
-                               wrap = new Wrap (underlying, loc).Resolve (ec);
+                               wrap = Wrap.Create (underlying, ec);
                                if (wrap == null)
                                        return null;
 
@@ -3006,15 +3044,13 @@ namespace Mono.CSharp {
                        public override Expression DoResolve (EmitContext ec)
                        {
                                if (TypeManager.IsNullableType (left.Type)) {
-                                       left_unwrap = new Unwrap (left, loc);
-                                       left = left_unwrap.Resolve (ec);
+                                       left = left_unwrap = Unwrap.Create (left, ec);
                                        if (left == null)
                                                return null;
                                }
 
                                if (TypeManager.IsNullableType (right.Type)) {
-                                       right_unwrap = new Unwrap (right, loc);
-                                       right = right_unwrap.Resolve (ec);
+                                       right = right_unwrap = Unwrap.Create (right, ec);
                                        if (right == null)
                                                return null;
                                }
@@ -3031,7 +3067,7 @@ namespace Mono.CSharp {
                                if (((Oper == Binary.Operator.BitwiseAnd) || (Oper == Binary.Operator.BitwiseOr)) &&
                                    ((left.Type == TypeManager.bool_type) && (right.Type == TypeManager.bool_type))) {
                                        Expression empty = new EmptyExpression (TypeManager.bool_type);
-                                       bool_wrap = new Wrap (empty, loc).Resolve (ec);
+                                       bool_wrap = Wrap.Create (empty, ec);
                                        null_value = new NullableLiteral (bool_wrap.Type, loc).Resolve (ec);
 
                                        type = bool_wrap.Type;
@@ -3060,7 +3096,7 @@ namespace Mono.CSharp {
                                        if (underlying == null)
                                                return null;
 
-                                       underlying = new Wrap (underlying, loc).Resolve (ec);
+                                       underlying = Wrap.Create (underlying, ec);
                                        if (underlying == null)
                                                return null;
 
@@ -3140,95 +3176,70 @@ namespace Mono.CSharp {
                        {
                                ILGenerator ig = ec.ig;
 
-                               Label left_not_null_label = ig.DefineLabel ();
-                               Label false_label = ig.DefineLabel ();
-                               Label true_label = ig.DefineLabel ();
-                               Label end_label = ig.DefineLabel ();
-
-                               bool false_label_used = false;
-                               bool true_label_used = false;
+                               // Given 'X? x;' for any value type X: 'x != null' is the same as 'x.HasValue'
+                               if (left is NullLiteral) {
+                                       if (right_unwrap == null)
+                                               throw new InternalErrorException ();
+                                       right_unwrap.EmitCheck (ec);
+                                       if (Oper == Binary.Operator.Equality) {
+                                               ig.Emit (OpCodes.Ldc_I4_0);
+                                               ig.Emit (OpCodes.Ceq);
+                                       }
+                                       return;
+                               }
 
-                               if (left_unwrap != null) {
+                               if (right is NullLiteral) {
+                                       if (left_unwrap == null)
+                                               throw new InternalErrorException ();
                                        left_unwrap.EmitCheck (ec);
-                                       if (right is NullLiteral) {
-                                               if (Oper == Binary.Operator.Equality) {
-                                                       true_label_used = true;
-                                                       ig.Emit (OpCodes.Brfalse, true_label);
-                                               } else {
-                                                       false_label_used = true;
-                                                       ig.Emit (OpCodes.Brfalse, false_label);
-                                               }
-                                       } else if (right_unwrap != null) {
-                                               ig.Emit (OpCodes.Dup);
-                                               ig.Emit (OpCodes.Brtrue, left_not_null_label);
-                                               right_unwrap.EmitCheck (ec);
+                                       if (Oper == Binary.Operator.Equality) {
+                                               ig.Emit (OpCodes.Ldc_I4_0);
                                                ig.Emit (OpCodes.Ceq);
-                                               if (Oper == Binary.Operator.Inequality) {
-                                                       ig.Emit (OpCodes.Ldc_I4_0);
-                                                       ig.Emit (OpCodes.Ceq);
-                                               }
-                                               ig.Emit (OpCodes.Br, end_label);
-
-                                               ig.MarkLabel (left_not_null_label);
-                                               ig.Emit (OpCodes.Pop);
-                                       } else {
-                                               if (Oper == Binary.Operator.Equality) {
-                                                       false_label_used = true;
-                                                       ig.Emit (OpCodes.Brfalse, false_label);
-                                               } else {
-                                                       true_label_used = true;
-                                                       ig.Emit (OpCodes.Brfalse, true_label);
-                                               }
                                        }
+                                       return;
                                }
 
-                               if (right_unwrap != null) {
+                               Label both_have_value_label = ig.DefineLabel ();
+                               Label end_label = ig.DefineLabel ();
+
+                               if (left_unwrap != null && right_unwrap != null) {
+                                       Label dissimilar_label = ig.DefineLabel ();
+
+                                       left_unwrap.EmitCheck (ec);
+                                       ig.Emit (OpCodes.Dup);
                                        right_unwrap.EmitCheck (ec);
-                                       if (left is NullLiteral) {
-                                               if (Oper == Binary.Operator.Equality) {
-                                                       true_label_used = true;
-                                                       ig.Emit (OpCodes.Brfalse, true_label);
-                                               } else {
-                                                       false_label_used = true;
-                                                       ig.Emit (OpCodes.Brfalse, false_label);
-                                               }
-                                       } else {
-                                               if (Oper == Binary.Operator.Equality) {
-                                                       false_label_used = true;
-                                                       ig.Emit (OpCodes.Brfalse, false_label);
-                                               } else {
-                                                       true_label_used = true;
-                                                       ig.Emit (OpCodes.Brfalse, true_label);
-                                               }
-                                       }
-                               }
+                                       ig.Emit (OpCodes.Bne_Un, dissimilar_label);
 
-                               bool left_is_null = left is NullLiteral;
-                               bool right_is_null = right is NullLiteral;
-                               if (left_is_null || right_is_null) {
-                                       if (((Oper == Binary.Operator.Equality) && (left_is_null == right_is_null)) ||
-                                           ((Oper == Binary.Operator.Inequality) && (left_is_null != right_is_null))) {
-                                               true_label_used = true;
-                                               ig.Emit (OpCodes.Br, true_label);
-                                       } else {
-                                               false_label_used = true;
-                                               ig.Emit (OpCodes.Br, false_label);
-                                       }
-                               } else {
-                                       underlying.Emit (ec);
+                                       ig.Emit (OpCodes.Brtrue, both_have_value_label);
+
+                                       // both are null
+                                       if (Oper == Binary.Operator.Equality)
+                                               ig.Emit (OpCodes.Ldc_I4_1);
+                                       else
+                                               ig.Emit (OpCodes.Ldc_I4_0);
                                        ig.Emit (OpCodes.Br, end_label);
-                               }
 
-                               ig.MarkLabel (false_label);
-                               if (false_label_used) {
-                                       ig.Emit (OpCodes.Ldc_I4_0);
-                                       if (true_label_used)
-                                               ig.Emit (OpCodes.Br, end_label);
+                                       ig.MarkLabel (dissimilar_label);
+                                       ig.Emit (OpCodes.Pop);
+                               } else if (left_unwrap != null) {
+                                       left_unwrap.EmitCheck (ec);
+                                       ig.Emit (OpCodes.Brtrue, both_have_value_label);
+                               } else if (right_unwrap != null) {
+                                       right_unwrap.EmitCheck (ec);
+                                       ig.Emit (OpCodes.Brtrue, both_have_value_label);
+                               } else {
+                                       throw new InternalErrorException ("shouldn't get here");
                                }
 
-                               ig.MarkLabel (true_label);
-                               if (true_label_used)
+                               // one is null while the other isn't
+                               if (Oper == Binary.Operator.Equality)
+                                       ig.Emit (OpCodes.Ldc_I4_0);
+                               else
                                        ig.Emit (OpCodes.Ldc_I4_1);
+                               ig.Emit (OpCodes.Br, end_label);
+
+                               ig.MarkLabel (both_have_value_label);
+                               underlying.Emit (ec);
 
                                ig.MarkLabel (end_label);
                        }
@@ -3318,9 +3329,8 @@ namespace Mono.CSharp {
 
                        public override Expression DoResolve (EmitContext ec)
                        {
-                               unwrap = new Unwrap (expr, loc);
-                               expr = unwrap.Resolve (ec);
-                               if (expr == null)
+                               unwrap = Unwrap.Create (expr, ec);
+                               if (unwrap == null)
                                        return null;
 
                                if (unwrap.Type != TypeManager.bool_type)
@@ -3393,7 +3403,7 @@ namespace Mono.CSharp {
                                if (TypeManager.IsNullableType (ltype)) {
                                        NullableInfo info = new NullableInfo (ltype);
 
-                                       unwrap = (Unwrap) new Unwrap (left, loc).Resolve (ec);
+                                       unwrap = Unwrap.Create (left, ec);
                                        if (unwrap == null)
                                                return null;
 
@@ -3480,7 +3490,7 @@ namespace Mono.CSharp {
                                if (expr == null)
                                        return null;
 
-                               unwrap = (Unwrap) new Unwrap (expr, loc).Resolve (ec);
+                               unwrap = Unwrap.Create (expr, ec);
                                if (unwrap == null)
                                        return null;