2009-02-16 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / generic.cs
index f528c6a414922c5f81f42f6c5b6d9fa173f7217c..9bdcc1d77fc33725dde6203eae64c7e32a2ca3c8 100644 (file)
@@ -124,6 +124,13 @@ namespace Mono.CSharp {
                }
        }
 
+       public enum Variance
+       {
+               None,
+               Covariant,
+               Contravariant
+       }
+
        public enum SpecialConstraint
        {
                Constructor,
@@ -182,7 +189,7 @@ namespace Mono.CSharp {
                        if (resolved)
                                return true;
 
-                       iface_constraints = new ArrayList ();
+                       iface_constraints = new ArrayList (2);  // TODO: Too expensive allocation
                        type_param_constraints = new ArrayList ();
 
                        foreach (object obj in constraints) {
@@ -231,12 +238,9 @@ namespace Mono.CSharp {
                                }
 
                                TypeExpr expr;
-                               ConstructedType cexpr = fn as ConstructedType;
+                               GenericTypeExpr cexpr = fn as GenericTypeExpr;
                                if (cexpr != null) {
-                                       if (!cexpr.ResolveConstructedType (ec))
-                                               return false;
-
-                                       expr = cexpr;
+                                       expr = cexpr.ResolveAsBaseTerminal (ec, false);
                                } else
                                        expr = ((Expression) obj).ResolveAsTypeTerminal (ec, false);
 
@@ -256,11 +260,10 @@ namespace Mono.CSharp {
                                        type_param_constraints.Add (expr);
                                else if (expr.IsInterface)
                                        iface_constraints.Add (expr);
-                               else if (class_constraint != null) {
+                               else if (class_constraint != null || iface_constraints.Count != 0) {
                                        Report.Error (406, loc,
-                                                     "`{0}': the class constraint for `{1}' " +
-                                                     "must come before any other constraints.",
-                                                     expr.GetSignatureForError (), name);
+                                               "The class type constraint `{0}' must be listed before any other constraints. Consider moving type constraint to the beginning of the constraint list",
+                                               expr.GetSignatureForError ());
                                        return false;
                                } else if (HasReferenceTypeConstraint || HasValueTypeConstraint) {
                                        Report.Error (450, loc, "`{0}': cannot specify both " +
@@ -345,26 +348,52 @@ namespace Mono.CSharp {
                        else
                                effective_base_type = TypeManager.object_type;
 
+                       if ((attrs & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0)
+                               attrs |= GenericParameterAttributes.DefaultConstructorConstraint;
+
                        resolved = true;
                        return true;
                }
 
-               bool CheckTypeParameterConstraints (TypeParameter tparam, Hashtable seen)
+               bool CheckTypeParameterConstraints (TypeParameter tparam, ref TypeExpr prevConstraint, ArrayList seen)
                {
-                       seen.Add (tparam, true);
+                       seen.Add (tparam);
 
                        Constraints constraints = tparam.Constraints;
                        if (constraints == null)
                                return true;
 
                        if (constraints.HasValueTypeConstraint) {
-                               Report.Error (456, loc, "Type parameter `{0}' has " +
-                                             "the `struct' constraint, so it cannot " +
-                                             "be used as a constraint for `{1}'",
-                                             tparam.Name, name);
+                               Report.Error (456, loc,
+                                       "Type parameter `{0}' has the `struct' constraint, so it cannot be used as a constraint for `{1}'",
+                                       tparam.Name, name);
                                return false;
                        }
 
+                       //
+                       //  Checks whether there are no conflicts between type parameter constraints
+                       //
+                       //   class Foo<T, U>
+                       //      where T : A
+                       //      where U : A, B  // A and B are not convertible
+                       //
+                       if (constraints.HasClassConstraint) {
+                               if (prevConstraint != null) {
+                                       Type t2 = constraints.ClassConstraint;
+                                       TypeExpr e2 = constraints.class_constraint;
+
+                                       if (!Convert.ImplicitReferenceConversionExists (prevConstraint, t2) &&
+                                               !Convert.ImplicitReferenceConversionExists (e2, prevConstraint.Type)) {
+                                               Report.Error (455, loc,
+                                                       "Type parameter `{0}' inherits conflicting constraints `{1}' and `{2}'",
+                                                       name, TypeManager.CSharpName (prevConstraint.Type), TypeManager.CSharpName (t2));
+                                               return false;
+                                       }
+                               }
+
+                               prevConstraint = constraints.class_constraint;
+                       }
+
                        if (constraints.type_param_constraints == null)
                                return true;
 
@@ -376,7 +405,7 @@ namespace Mono.CSharp {
                                        return false;
                                }
 
-                               if (!CheckTypeParameterConstraints (expr.TypeParameter, seen))
+                               if (!CheckTypeParameterConstraints (expr.TypeParameter, ref prevConstraint, seen))
                                        return false;
                        }
 
@@ -394,7 +423,7 @@ namespace Mono.CSharp {
                        resolved_types = true;
 
                        foreach (object obj in constraints) {
-                               ConstructedType cexpr = obj as ConstructedType;
+                               GenericTypeExpr cexpr = obj as GenericTypeExpr;
                                if (cexpr == null)
                                        continue;
 
@@ -402,10 +431,14 @@ namespace Mono.CSharp {
                                        return false;
                        }
 
-                       foreach (TypeParameterExpr expr in type_param_constraints) {
-                               Hashtable seen = new Hashtable ();
-                               if (!CheckTypeParameterConstraints (expr.TypeParameter, seen))
-                                       return false;
+                       if (type_param_constraints.Count != 0) {
+                               ArrayList seen = new ArrayList ();
+                               TypeExpr prev_constraint = class_constraint;
+                               foreach (TypeParameterExpr expr in type_param_constraints) {
+                                       if (!CheckTypeParameterConstraints (expr.TypeParameter, ref prev_constraint, seen))
+                                               return false;
+                                       seen.Clear ();
+                               }
                        }
 
                        for (int i = 0; i < iface_constraints.Count; ++i) {
@@ -425,66 +458,6 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               /// <summary>
-               ///   Check whether there are no conflicts in our type parameter constraints.
-               ///
-               ///   This is an example:
-               ///
-               ///   class Foo<T,U>
-               ///      where T : class
-               ///      where U : T, struct
-               /// </summary>
-               public bool CheckDependencies ()
-               {
-                       foreach (TypeParameterExpr expr in type_param_constraints) {
-                               if (!CheckDependencies (expr.TypeParameter))
-                                       return false;
-                       }
-
-                       return true;
-               }
-
-               bool CheckDependencies (TypeParameter tparam)
-               {
-                       Constraints constraints = tparam.Constraints;
-                       if (constraints == null)
-                               return true;
-
-                       if (HasValueTypeConstraint && constraints.HasClassConstraint) {
-                               Report.Error (455, loc, "Type parameter `{0}' inherits " +
-                                             "conflicting constraints `{1}' and `{2}'",
-                                             name, TypeManager.CSharpName (constraints.ClassConstraint),
-                                             "System.ValueType");
-                               return false;
-                       }
-
-                       if (HasClassConstraint && constraints.HasClassConstraint) {
-                               Type t1 = ClassConstraint;
-                               TypeExpr e1 = class_constraint;
-                               Type t2 = constraints.ClassConstraint;
-                               TypeExpr e2 = constraints.class_constraint;
-
-                               if (!Convert.ImplicitReferenceConversionExists (e1, t2) &&
-                                   !Convert.ImplicitReferenceConversionExists (e2, t1)) {
-                                       Report.Error (455, loc,
-                                                     "Type parameter `{0}' inherits " +
-                                                     "conflicting constraints `{1}' and `{2}'",
-                                                     name, TypeManager.CSharpName (t1), TypeManager.CSharpName (t2));
-                                       return false;
-                               }
-                       }
-
-                       if (constraints.type_param_constraints == null)
-                               return true;
-
-                       foreach (TypeParameterExpr expr in constraints.type_param_constraints) {
-                               if (!CheckDependencies (expr.TypeParameter))
-                                       return false;
-                       }
-
-                       return true;
-               }
-
                public override GenericParameterAttributes Attributes {
                        get { return attrs; }
                }
@@ -606,20 +579,23 @@ namespace Mono.CSharp {
        {
                static readonly string[] attribute_target = new string [] { "type parameter" };
                
-               string name;
                DeclSpace decl;
                GenericConstraints gc;
                Constraints constraints;
                GenericTypeParameterBuilder type;
                MemberCache member_cache;
+               Variance variance;
 
                public TypeParameter (DeclSpace parent, DeclSpace decl, string name,
-                                     Constraints constraints, Attributes attrs, Location loc)
+                                     Constraints constraints, Attributes attrs, Variance variance, Location loc)
                        : base (parent, new MemberName (name, loc), attrs)
                {
-                       this.name = name;
                        this.decl = decl;
                        this.constraints = constraints;
+                       this.variance = variance;
+                       if (variance != Variance.None && !(decl is Interface) && !(decl is Delegate)) {
+                               Report.Error (-36, loc, "Generic variance can only be used with interfaces and delegates");
+                       }
                }
 
                public GenericConstraints GenericConstraints {
@@ -634,6 +610,10 @@ namespace Mono.CSharp {
                        get { return decl; }
                }
 
+               public Variance Variance {
+                       get { return variance; }
+               }
+
                public Type Type {
                        get { return type; }
                }
@@ -782,34 +762,28 @@ namespace Mono.CSharp {
                                gc = (GenericConstraints) constraints;
                        }
 
-                       if (gc == null)
-                               return true;
-
-                       if (gc.HasClassConstraint)
-                               type.SetBaseTypeConstraint (gc.ClassConstraint);
-
-                       type.SetInterfaceConstraints (gc.InterfaceConstraints);
-                       type.SetGenericParameterAttributes (gc.Attributes);
-                       TypeManager.RegisterBuilder (type, gc.InterfaceConstraints);
-
+                       SetConstraints (type);
                        return true;
                }
 
-               /// <summary>
-               ///   Check whether there are no conflicts in our type parameter constraints.
-               ///
-               ///   This is an example:
-               ///
-               ///   class Foo<T,U>
-               ///      where T : class
-               ///      where U : T, struct
-               /// </summary>
-               public bool CheckDependencies ()
+               public void SetConstraints (GenericTypeParameterBuilder type)
                {
-                       if (constraints != null)
-                               return constraints.CheckDependencies ();
+                       GenericParameterAttributes attr = GenericParameterAttributes.None;
+                       if (variance == Variance.Contravariant)
+                               attr |= GenericParameterAttributes.Contravariant;
+                       else if (variance == Variance.Covariant)
+                               attr |= GenericParameterAttributes.Covariant;
 
-                       return true;
+                       if (gc != null) {
+                               if (gc.HasClassConstraint || gc.HasValueTypeConstraint)
+                                       type.SetBaseTypeConstraint (gc.EffectiveBaseClass);
+
+                               attr |= gc.Attributes;
+                               type.SetInterfaceConstraints (gc.InterfaceConstraints);
+                               TypeManager.RegisterBuilder (type, gc.InterfaceConstraints);
+                       }
+                       
+                       type.SetGenericParameterAttributes (attr);
                }
 
                /// <summary>
@@ -963,26 +937,6 @@ namespace Mono.CSharp {
                        return false;
                }
 
-               public override string ToString ()
-               {
-                       return "TypeParameter[" + name + "]";
-               }
-
-               public static string GetSignatureForError (TypeParameter[] tp)
-               {
-                       if (tp == null || tp.Length == 0)
-                               return "";
-
-                       StringBuilder sb = new StringBuilder ("<");
-                       for (int i = 0; i < tp.Length; ++i) {
-                               if (i > 0)
-                                       sb.Append (",");
-                               sb.Append (tp[i].GetSignatureForError ());
-                       }
-                       sb.Append ('>');
-                       return sb.ToString ();
-               }
-
                public void InflateConstraints (Type declaring)
                {
                        if (constraints != null)
@@ -1045,7 +999,7 @@ namespace Mono.CSharp {
                                if (t == null)
                                        return null;
                                if (t.IsGenericParameter)
-                                       return dargs [t.GenericParameterPosition];
+                                       return t.GenericParameterPosition < dargs.Length ? dargs [t.GenericParameterPosition] : t;
                                if (t.IsGenericType) {
                                        Type[] args = t.GetGenericArguments ();
                                        Type[] inflated = new Type [args.Length];
@@ -1122,36 +1076,25 @@ namespace Mono.CSharp {
                }
        }
 
-       /// <summary>
-       ///   Tracks the type arguments when instantiating a generic type.  We're used in
-       ///   ConstructedType.
-       /// </summary>
+       //
+       // Tracks the type arguments when instantiating a generic type. It's used
+       // by both type arguments and type parameters
+       //
        public class TypeArguments {
-               public readonly Location Location;
                ArrayList args;
                Type[] atypes;
-               int dimension;
-               bool has_type_args;
                
-               public TypeArguments (Location loc)
+               public TypeArguments ()
                {
                        args = new ArrayList ();
-                       this.Location = loc;
                }
 
-               public TypeArguments (Location loc, params Expression[] types)
+               public TypeArguments (params FullNamedExpression[] types)
                {
-                       this.Location = loc;
                        this.args = new ArrayList (types);
                }
-               
-               public TypeArguments (int dimension, Location loc)
-               {
-                       this.dimension = dimension;
-                       this.Location = loc;
-               }
 
-               public void Add (Expression type)
+               public void Add (FullNamedExpression type)
                {
                        args.Add (type);
                }
@@ -1161,33 +1104,10 @@ namespace Mono.CSharp {
                        args.AddRange (new_args.args);
                }
 
-               /// <summary>
-               ///   We're used during the parsing process: the parser can't distinguish
-               ///   between type parameters and type arguments.  Because of that, the
-               ///   parser creates a `MemberName' with `TypeArguments' for both cases and
-               ///   in case of a generic type definition, we call GetDeclarations().
-               /// </summary>
+               // TODO: Should be deleted
                public TypeParameterName[] GetDeclarations ()
                {
-                       TypeParameterName[] ret = new TypeParameterName [args.Count];
-                       for (int i = 0; i < args.Count; i++) {
-                               TypeParameterName name = args [i] as TypeParameterName;
-                               if (name != null) {
-                                       ret [i] = name;
-                                       continue;
-                               }
-                               SimpleName sn = args [i] as SimpleName;
-                               if (sn != null && !sn.HasTypeArguments) {
-                                       ret [i] = new TypeParameterName (sn.Name, null, sn.Location);
-                                       continue;
-                               }
-
-                               Expression expr = (Expression) args [i];
-                               // TODO: Wrong location
-                               Report.Error (81, Location, "Type parameter declaration must be an identifier not a type");
-                               ret [i] = new TypeParameterName (expr.GetSignatureForError (), null, expr.Location);
-                       }
-                       return ret;
+                       return (TypeParameterName[]) args.ToArray (typeof (TypeParameterName));
                }
 
                /// <summary>
@@ -1200,44 +1120,12 @@ namespace Mono.CSharp {
                        }
                }
 
-               public bool HasTypeArguments {
-                       get {
-                               return has_type_args;
-                       }
-               }
-
                public int Count {
                        get {
-                               if (dimension > 0)
-                                       return dimension;
-                               else
-                                       return args.Count;
-                       }
-               }
-
-               public bool IsUnbound {
-                       get {
-                               return dimension > 0;
+                               return args.Count;
                        }
                }
 
-               public override string ToString ()
-               {
-                       StringBuilder s = new StringBuilder ();
-
-                       int count = Count;
-                       for (int i = 0; i < count; i++){
-                               //
-                               // FIXME: Use TypeManager.CSharpname once we have the type
-                               //
-                               if (args != null)
-                                       s.Append (args [i].ToString ());
-                               if (i+1 < count)
-                                       s.Append (",");
-                       }
-                       return s.ToString ();
-               }
-
                public string GetSignatureForError()
                {
                        StringBuilder sb = new StringBuilder();
@@ -1256,48 +1144,46 @@ namespace Mono.CSharp {
                /// </summary>
                public bool Resolve (IResolveContext ec)
                {
+                       if (atypes != null)
+                               return atypes.Length != 0;
+
                        int count = args.Count;
                        bool ok = true;
 
                        atypes = new Type [count];
 
                        for (int i = 0; i < count; i++){
-                               TypeExpr te = ((Expression) args [i]).ResolveAsTypeTerminal (ec, false);
+                               TypeExpr te = ((FullNamedExpression) args[i]).ResolveAsTypeTerminal (ec, false);
                                if (te == null) {
                                        ok = false;
                                        continue;
                                }
 
                                atypes[i] = te.Type;
-                               if (te.Type.IsGenericParameter) {
-                                       if (te is TypeParameterExpr)
-                                               has_type_args = true;
-                                       continue;
-                               }
 
                                if (te.Type.IsSealed && te.Type.IsAbstract) {
-                                       Report.Error (718, Location, "`{0}': static classes cannot be used as generic arguments",
+                                       Report.Error (718, te.Location, "`{0}': static classes cannot be used as generic arguments",
                                                te.GetSignatureForError ());
-                                       return false;
-                               }
-
-                               if (te.Type.IsPointer) {
-                                       Report.Error (306, Location, "The type `{0}' may not be used " +
-                                                         "as a type argument", TypeManager.CSharpName (te.Type));
-                                       return false;
+                                       ok = false;
                                }
 
-                               if (te.Type == TypeManager.void_type) {
-                                       Expression.Error_VoidInvalidInTheContext (Location);
-                                       return false;
+                               if (te.Type.IsPointer || TypeManager.IsSpecialType (te.Type)) {
+                                       Report.Error (306, te.Location,
+                                               "The type `{0}' may not be used as a type argument",
+                                               te.GetSignatureForError ());
+                                       ok = false;
                                }
                        }
+
+                       if (!ok)
+                               atypes = Type.EmptyTypes;
+
                        return ok;
                }
 
                public TypeArguments Clone ()
                {
-                       TypeArguments copy = new TypeArguments (Location);
+                       TypeArguments copy = new TypeArguments ();
                        foreach (Expression ta in args)
                                copy.args.Add (ta);
 
@@ -1308,11 +1194,18 @@ namespace Mono.CSharp {
        public class TypeParameterName : SimpleName
        {
                Attributes attributes;
+               Variance variance;
 
                public TypeParameterName (string name, Attributes attrs, Location loc)
+                       : this (name, attrs, Variance.None, loc)
+               {
+               }
+
+               public TypeParameterName (string name, Attributes attrs, Variance variance, Location loc)
                        : base (name, loc)
                {
                        attributes = attrs;
+                       this.variance = variance;
                }
 
                public Attributes OptAttributes {
@@ -1320,43 +1213,38 @@ namespace Mono.CSharp {
                                return attributes;
                        }
                }
+
+               public Variance Variance {
+                       get {
+                               return variance;
+                       }
+               }
        }
 
        /// <summary>
-       ///   An instantiation of a generic type.
+       ///   A reference expression to generic type
        /// </summary>  
-       public class ConstructedType : TypeExpr {
-               FullNamedExpression name;
+       class GenericTypeExpr : TypeExpr
+       {
                TypeArguments args;
-               Type[] gen_params, atypes;
-               Type gt;
+               Type[] gen_params;      // TODO: Waiting for constrains check cleanup
+               Type open_type;
 
-               /// <summary>
-               ///   Instantiate the generic type `fname' with the type arguments `args'.
-               /// </summary>          
-               public ConstructedType (FullNamedExpression fname, TypeArguments args, Location l)
-               {
-                       loc = l;
-                       this.name = fname;
-                       this.args = args;
-
-                       eclass = ExprClass.Type;
-               }
-
-               /// <summary>
-               ///   This is used to construct the `this' type inside a generic type definition.
-               /// </summary>
-               public ConstructedType (Type t, TypeParameter[] type_params, Location l)
+               //
+               // Should be carefully used only with defined generic containers. Type parameters
+               // can be used as type arguments in this case.
+               //
+               // TODO: This could be GenericTypeExpr specialization
+               //
+               public GenericTypeExpr (DeclSpace gType, Location l)
                {
-                       gt = t.GetGenericTypeDefinition ();
+                       open_type = gType.TypeBuilder.GetGenericTypeDefinition ();
 
-                       args = new TypeArguments (l);
-                       foreach (TypeParameter type_param in type_params)
+                       args = new TypeArguments ();
+                       foreach (TypeParameter type_param in gType.TypeParameters)
                                args.Add (new TypeParameterExpr (type_param, l));
 
                        this.loc = l;
-                       this.name = new TypeExpression (gt, l);
-                       eclass = ExprClass.Type;
                }
 
                /// <summary>
@@ -1364,11 +1252,12 @@ namespace Mono.CSharp {
                ///   Use this constructor if you already know the fully resolved
                ///   generic type.
                /// </summary>          
-               public ConstructedType (Type t, TypeArguments args, Location l)
-                       : this ((FullNamedExpression)null, args, l)
+               public GenericTypeExpr (Type t, TypeArguments args, Location l)
                {
-                       gt = t.GetGenericTypeDefinition ();
-                       this.name = new TypeExpression (gt, l);
+                       open_type = t.GetGenericTypeDefinition ();
+
+                       loc = l;
+                       this.args = args;
                }
 
                public TypeArguments TypeArguments {
@@ -1377,14 +1266,31 @@ namespace Mono.CSharp {
 
                public override string GetSignatureForError ()
                {
-                       return TypeManager.RemoveGenericArity (gt.FullName) + "<" + args.GetSignatureForError () + ">";
+                       return TypeManager.CSharpName (type);
                }
 
                protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
                {
-                       if (!ResolveConstructedType (ec))
+                       if (eclass != ExprClass.Invalid)
+                               return this;
+
+                       eclass = ExprClass.Type;
+
+                       if (!args.Resolve (ec))
                                return null;
 
+                       gen_params = open_type.GetGenericArguments ();
+                       Type[] atypes = args.Arguments;
+                       
+                       if (atypes.Length != gen_params.Length) {
+                               Namespace.Error_InvalidNumberOfTypeArguments (open_type, loc);
+                               return null;
+                       }
+
+                       //
+                       // Now bind the parameters
+                       //
+                       type = open_type.MakeGenericType (atypes);
                        return this;
                }
 
@@ -1394,107 +1300,79 @@ namespace Mono.CSharp {
                /// </summary>
                public bool CheckConstraints (IResolveContext ec)
                {
-                       return ConstraintChecker.CheckConstraints (ec, gt, gen_params, atypes, loc);
+                       return ConstraintChecker.CheckConstraints (ec, open_type, gen_params, args.Arguments, loc);
                }
 
-               /// <summary>
-               ///   Resolve the constructed type, but don't check the constraints.
-               /// </summary>
-               public bool ResolveConstructedType (IResolveContext ec)
+               static bool IsVariant (Type type)
                {
-                       if (type != null)
-                               return true;
-                       // If we already know the fully resolved generic type.
-                       if (gt != null)
-                               return DoResolveType (ec);
-
-                       int num_args;
-                       Type t = name.Type;
-
-                       if (t == null) {
-                               Report.Error (246, loc, "Cannot find type `{0}'<...>", GetSignatureForError ());
-                               return false;
-                       }
-
-                       num_args = TypeManager.GetNumberOfTypeArguments (t);
-                       if (num_args == 0) {
-                               Report.Error (308, loc,
-                                             "The non-generic type `{0}' cannot " +
-                                             "be used with type arguments.",
-                                             TypeManager.CSharpName (t));
-                               return false;
-                       }
-
-                       gt = t.GetGenericTypeDefinition ();
-                       return DoResolveType (ec);
+                       return (type.GenericParameterAttributes & GenericParameterAttributes.VarianceMask) != 0;
                }
-
-               bool DoResolveType (IResolveContext ec)
+       
+               static bool IsCovariant (Type type)
                {
-                       //
-                       // Resolve the arguments.
-                       //
-                       if (args.Resolve (ec) == false)
-                               return false;
-
-                       gen_params = gt.GetGenericArguments ();
-                       atypes = args.Arguments;
-
-                       if (atypes.Length != gen_params.Length) {
-                               Report.Error (305, loc,
-                                             "Using the generic type `{0}' " +
-                                             "requires {1} type arguments",
-                                             TypeManager.CSharpName (gt),
-                                             gen_params.Length.ToString ());
-                               return false;
-                       }
-
-                       //
-                       // Now bind the parameters.
-                       //
-                       type = gt.MakeGenericType (atypes);
-                       return true;
+                       return (type.GenericParameterAttributes & GenericParameterAttributes.Covariant) != 0;
                }
-
-               public Expression GetSimpleName (EmitContext ec)
+       
+               static bool IsContravariant (Type type)
                {
-                       return this;
+                       return (type.GenericParameterAttributes & GenericParameterAttributes.Contravariant) != 0;
+               }
+       
+               public bool VerifyVariantTypeParameters ()
+               {
+                       for (int i = 0; i < args.Count; i++) {
+                               Type argument = args.Arguments[i];
+                               if (argument.IsGenericParameter && IsVariant (argument)) {
+                                       if (IsContravariant (argument) && !IsContravariant (gen_params[i])) {
+                                               Report.Error (-34, loc, "Contravariant type parameters can only be used " +
+                                             "as type arguments in contravariant positions");
+                                               return false;
+                                       }
+                                       else if (IsCovariant (argument) && !IsCovariant (gen_params[i])) {
+                                               Report.Error (-35, loc, "Covariant type parameters can only be used " +
+                                             "as type arguments in covariant positions");
+                                               return false;
+                                       }
+                               }
+                       }
+                       return true;
                }
 
+               
                public override bool CheckAccessLevel (DeclSpace ds)
                {
-                       return ds.CheckAccessLevel (gt);
+                       return ds.CheckAccessLevel (open_type);
                }
 
                public override bool AsAccessible (DeclSpace ds)
                {
-                       foreach (Type t in atypes) {
+                       foreach (Type t in args.Arguments) {
                                if (!ds.IsAccessibleAs (t))
                                        return false;
                        }
 
-                       return ds.IsAccessibleAs (gt);
+                       return ds.IsAccessibleAs (open_type);
                }
 
                public override bool IsClass {
-                       get { return gt.IsClass; }
+                       get { return open_type.IsClass; }
                }
 
                public override bool IsValueType {
-                       get { return gt.IsValueType; }
+                       get { return TypeManager.IsStruct (open_type); }
                }
 
                public override bool IsInterface {
-                       get { return gt.IsInterface; }
+                       get { return open_type.IsInterface; }
                }
 
                public override bool IsSealed {
-                       get { return gt.IsSealed; }
+                       get { return open_type.IsSealed; }
                }
 
                public override bool Equals (object obj)
                {
-                       ConstructedType cobj = obj as ConstructedType;
+                       GenericTypeExpr cobj = obj as GenericTypeExpr;
                        if (cobj == null)
                                return false;
 
@@ -1568,7 +1446,7 @@ namespace Mono.CSharp {
                                if (!atype.IsGenericType)
 #endif
                                is_class = atype.IsClass || atype.IsInterface;
-                               is_struct = atype.IsValueType && !TypeManager.IsNullableType (atype);
+                               is_struct = TypeManager.IsValueType (atype) && !TypeManager.IsNullableType (atype);
                        }
 
                        //
@@ -1619,7 +1497,7 @@ namespace Mono.CSharp {
                        if (!gc.HasConstructorConstraint)
                                return true;
 
-                       if (TypeManager.IsBuiltinType (atype) || atype.IsValueType)
+                       if (TypeManager.IsBuiltinType (atype) || TypeManager.IsValueType (atype))
                                return true;
 
                        if (HasDefaultConstructor (atype))
@@ -1643,7 +1521,7 @@ namespace Mono.CSharp {
                        if (TypeManager.HasGenericArguments (ctype)) {
                                Type[] types = TypeManager.GetTypeArguments (ctype);
 
-                               TypeArguments new_args = new TypeArguments (loc);
+                               TypeArguments new_args = new TypeArguments ();
 
                                for (int i = 0; i < types.Length; i++) {
                                        Type t = types [i];
@@ -1655,19 +1533,39 @@ namespace Mono.CSharp {
                                        new_args.Add (new TypeExpression (t, loc));
                                }
 
-                               TypeExpr ct = new ConstructedType (ctype, new_args, loc);
+                               TypeExpr ct = new GenericTypeExpr (ctype, new_args, loc);
                                if (ct.ResolveAsTypeStep (ec, false) == null)
                                        return false;
                                ctype = ct.Type;
                        } else if (ctype.IsGenericParameter) {
                                int pos = ctype.GenericParameterPosition;
-                               ctype = atypes [pos];
+                               if (ctype.DeclaringMethod == null) {
+                                       // FIXME: Implement
+                                       return true;
+                               } else {                                
+                                       ctype = atypes [pos];
+                               }
                        }
 
                        if (Convert.ImplicitStandardConversionExists (expr, ctype))
                                return true;
 
-                       Error_TypeMustBeConvertible (expr.Type, ctype, ptype);
+                       Report_SymbolRelatedToPreviousError ();
+                       Report.SymbolRelatedToPreviousError (expr.Type);
+
+                       if (TypeManager.IsNullableType (expr.Type) && ctype.IsInterface) {
+                               Report.Error (313, loc,
+                                       "The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. " +
+                                       "The nullable type `{0}' never satisfies interface constraint of type `{3}'",
+                                       TypeManager.CSharpName (expr.Type), TypeManager.CSharpName (ptype),
+                                       GetSignatureForError (), TypeManager.CSharpName (ctype));
+                       } else {
+                               Report.Error (309, loc,
+                                       "The type `{0}' must be convertible to `{1}' in order to " +
+                                       "use it as parameter `{2}' in the generic type or method `{3}'",
+                                       TypeManager.CSharpName (expr.Type), TypeManager.CSharpName (ctype),
+                                       TypeManager.CSharpName (ptype), GetSignatureForError ());
+                       }
                        return false;
                }
 
@@ -1726,17 +1624,6 @@ namespace Mono.CSharp {
                protected abstract string GetSignatureForError ();
                protected abstract void Report_SymbolRelatedToPreviousError ();
 
-               void Error_TypeMustBeConvertible (Type atype, Type gc, Type ptype)
-               {
-                       Report_SymbolRelatedToPreviousError ();
-                       Report.SymbolRelatedToPreviousError (atype);
-                       Report.Error (309, loc, 
-                                     "The type `{0}' must be convertible to `{1}' in order to " +
-                                     "use it as parameter `{2}' in the generic type or method `{3}'",
-                                     TypeManager.CSharpName (atype), TypeManager.CSharpName (gc),
-                                     TypeManager.CSharpName (ptype), GetSignatureForError ());
-               }
-
                public static bool CheckConstraints (EmitContext ec, MethodBase definition,
                                                     MethodBase instantiated, Location loc)
                {
@@ -1807,10 +1694,10 @@ namespace Mono.CSharp {
        public class GenericMethod : DeclSpace
        {
                FullNamedExpression return_type;
-               Parameters parameters;
+               ParametersCompiled parameters;
 
                public GenericMethod (NamespaceEntry ns, DeclSpace parent, MemberName name,
-                                     FullNamedExpression return_type, Parameters parameters)
+                                     FullNamedExpression return_type, ParametersCompiled parameters)
                        : base (ns, parent, name, null)
                {
                        this.return_type = return_type;
@@ -1835,22 +1722,26 @@ namespace Mono.CSharp {
                ///   Define and resolve the type parameters.
                ///   We're called from Method.Define().
                /// </summary>
-               public bool Define (MethodBuilder mb)
+               public bool Define (MethodOrOperator m)
                {
                        TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
                        string[] snames = new string [names.Length];
                        for (int i = 0; i < names.Length; i++) {
                                string type_argument_name = names[i].Name;
-                               Parameter p = parameters.GetParameterByName (type_argument_name);
-                               if (p != null) {
-                                       Error_ParameterNameCollision (p.Location, type_argument_name, "method parameter");
-                                       return false;
+                               int idx = parameters.GetParameterIndexByName (type_argument_name);
+                               if (idx >= 0) {
+                                       Block b = m.Block;
+                                       if (b == null)
+                                               b = new Block (null);
+
+                                       b.Error_AlreadyDeclaredTypeParameter (parameters [i].Location,
+                                               type_argument_name, "method parameter");
                                }
                                
                                snames[i] = type_argument_name;
                        }
 
-                       GenericTypeParameterBuilder[] gen_params = mb.DefineGenericParameters (snames);
+                       GenericTypeParameterBuilder[] gen_params = m.MethodBuilder.DefineGenericParameters (snames);
                        for (int i = 0; i < TypeParameters.Length; i++)
                                TypeParameters [i].Define (gen_params [i]);
 
@@ -1865,12 +1756,6 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               internal static void Error_ParameterNameCollision (Location loc, string name, string collisionWith)
-               {
-                       Report.Error (412, loc, "The type parameter name `{0}' is the same as `{1}'",
-                               name, collisionWith);
-               }
-
                /// <summary>
                ///   We're called from MethodData.Define() after creating the MethodBuilder.
                /// </summary>
@@ -1882,11 +1767,8 @@ namespace Mono.CSharp {
                                            ec, mb, implementing, is_override))
                                        return false;
 
-                       bool ok = true;
-                       foreach (Parameter p in parameters.FixedParameters){
-                               if (p.Resolve (ec) == null)
-                                       ok = false;
-                       }
+                       bool ok = parameters.Resolve (ec);
+
                        if ((return_type != null) && (return_type.ResolveAsTypeTerminal (ec, false) == null))
                                ok = false;
 
@@ -1902,11 +1784,6 @@ namespace Mono.CSharp {
                                OptAttributes.Emit ();
                }
 
-               public override bool DefineMembers ()
-               {
-                       return true;
-               }
-
                public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
                                                        MemberFilter filter, object criteria)
                {
@@ -1963,7 +1840,7 @@ namespace Mono.CSharp {
                                // become equal.
                                //
                                while (b.IsArray) {
-                                       b = b.GetElementType ();
+                                       b = GetElementType (b);
                                        if (a.Equals (b))
                                                return false;
                                }
@@ -2031,8 +1908,8 @@ namespace Mono.CSharp {
                                if (a.GetArrayRank () != b.GetArrayRank ())
                                        return false;
                        
-                               a = a.GetElementType ();
-                               b = b.GetElementType ();
+                               a = GetElementType (a);
+                               b = GetElementType (b);
 
                                return MayBecomeEqualGenericTypes (a, b, class_inferred, method_inferred);
                        }
@@ -2262,7 +2139,7 @@ namespace Mono.CSharp {
                                        if (at.GetArrayRank () != pt.GetArrayRank ())
                                                return false;
 
-                                       return UnifyType (pt.GetElementType (), at.GetElementType (), inferred);
+                                       return UnifyType (TypeManager.GetElementType (pt), TypeManager.GetElementType (at), inferred);
                                }
 
                                if (!pt.IsGenericType)
@@ -2390,6 +2267,8 @@ namespace Mono.CSharp {
                        } else {
                                params_arguments_start = arg_count;
                        }
+
+                       Type [] ptypes = methodParameters.Types;
                        
                        //
                        // The first inference phase
@@ -2405,6 +2284,9 @@ namespace Mono.CSharp {
                                                method_parameter = methodParameters.Types [params_arguments_start];
                                        else
                                                method_parameter = TypeManager.GetElementType (methodParameters.Types [params_arguments_start]);
+
+                                       ptypes = (Type[]) ptypes.Clone ();
+                                       ptypes [i] = method_parameter;
                                }
 
                                //
@@ -2432,16 +2314,16 @@ namespace Mono.CSharp {
                        // we don't need to call it in cycle
                        //
                        bool fixed_any = false;
-                       if (!tic.FixIndependentTypeArguments (methodParameters, ref fixed_any))
+                       if (!tic.FixIndependentTypeArguments (ptypes, ref fixed_any))
                                return false;
 
-                       return DoSecondPhase (ec, tic, methodParameters, !fixed_any);
+                       return DoSecondPhase (ec, tic, ptypes, !fixed_any);
                }
 
-               bool DoSecondPhase (EmitContext ec, TypeInferenceContext tic, AParametersCollection methodParameters, bool fixDependent)
+               bool DoSecondPhase (EmitContext ec, TypeInferenceContext tic, Type[] methodParameters, bool fixDependent)
                {
                        bool fixed_any = false;
-                       if (fixDependent && !tic.FixDependentTypes (methodParameters, ref fixed_any))
+                       if (fixDependent && !tic.FixDependentTypes (ref fixed_any))
                                return false;
 
                        // If no further unfixed type variables exist, type inference succeeds
@@ -2455,7 +2337,10 @@ namespace Mono.CSharp {
                        // contain unfixed type variables but the input types do not,
                        // an output type inference is made
                        for (int i = 0; i < arg_count; i++) {
-                               Type t_i = methodParameters.Types [i];
+                               
+                               // Align params arguments
+                               Type t_i = methodParameters [i >= methodParameters.Length ? methodParameters.Length - 1: i];
+                               
                                if (!TypeManager.IsDelegateType (t_i)) {
                                        if (TypeManager.DropGenericTypeArguments (t_i) != TypeManager.expression_type)
                                                continue;
@@ -2486,6 +2371,7 @@ namespace Mono.CSharp {
                readonly Type[] unfixed_types;
                readonly Type[] fixed_types;
                readonly ArrayList[] bounds;
+               bool failed;
                
                public TypeInferenceContext (Type[] typeArguments)
                {
@@ -2614,7 +2500,7 @@ namespace Mono.CSharp {
                // a, There is at least one type variable Xj that depends on Xi
                // b, Xi has a non-empty set of bounds
                // 
-               public bool FixDependentTypes (AParametersCollection methodParameters, ref bool fixed_any)
+               public bool FixDependentTypes (ref bool fixed_any)
                {
                        for (int i = 0; i < unfixed_types.Length; ++i) {
                                if (unfixed_types[i] == null)
@@ -2635,11 +2521,11 @@ namespace Mono.CSharp {
                //
                // All unfixed type variables Xi which depend on no Xj are fixed
                //
-               public bool FixIndependentTypeArguments (AParametersCollection methodParameters, ref bool fixed_any)
+               public bool FixIndependentTypeArguments (Type[] methodParameters, ref bool fixed_any)
                {
                        ArrayList types_to_fix = new ArrayList (unfixed_types);
-                       for (int i = 0; i < methodParameters.Types.Length; ++i) {
-                               Type t = methodParameters.Types [i];
+                       for (int i = 0; i < methodParameters.Length; ++i) {
+                               Type t = methodParameters[i];
                                if (t.IsGenericParameter)
                                        continue;
 
@@ -2689,6 +2575,9 @@ namespace Mono.CSharp {
                        if (unfixed_types[i] == null)
                                throw new InternalErrorException ("Type argument has been already fixed");
 
+                       if (failed)
+                               return false;
+
                        ArrayList candidates = (ArrayList)bounds [i];
                        if (candidates == null)
                                return false;
@@ -2878,7 +2767,8 @@ namespace Mono.CSharp {
                                u_candidates.AddRange (TypeManager.GetInterfaces (u));
 
                                Type open_v = v.GetGenericTypeDefinition ();
-                               int score = 0;
+                               Type [] unique_candidate_targs = null;
+                               Type [] ga_v = v.GetGenericArguments ();                        
                                foreach (Type u_candidate in u_candidates) {
                                        if (!u_candidate.IsGenericType || u_candidate.IsGenericTypeDefinition)
                                                continue;
@@ -2886,17 +2776,35 @@ namespace Mono.CSharp {
                                        if (TypeManager.DropGenericTypeArguments (u_candidate) != open_v)
                                                continue;
 
-                                       Type [] ga_u = u_candidate.GetGenericArguments ();
-                                       Type [] ga_v = v.GetGenericArguments ();
-                                       bool all_exact = true;
-                                       for (int i = 0; i < ga_u.Length; ++i)
-                                               if (ExactInference (ga_u [i], ga_v [i]) == 0)
-                                                       all_exact = false;
+                                       //
+                                       // The unique set of types U1..Uk means that if we have an interface C<T>,
+                                       // class U: C<int>, C<long> then no type inference is made when inferring
+                                       // from U to C<T> because T could be int or long
+                                       //
+                                       if (unique_candidate_targs != null) {
+                                               Type[] second_unique_candidate_targs = u_candidate.GetGenericArguments ();
+                                               if (TypeManager.IsEqual (unique_candidate_targs, second_unique_candidate_targs)) {
+                                                       unique_candidate_targs = second_unique_candidate_targs;
+                                                       continue;
+                                               }
+                                               
+                                               //
+                                               // This should always cause type inference failure
+                                               //
+                                               failed = true;
+                                               return 1;
+                                       }
 
-                                       if (all_exact && score == 0)
-                                               ++score;
+                                       unique_candidate_targs = u_candidate.GetGenericArguments ();
+                               }
+
+                               if (unique_candidate_targs != null) {
+                                       int score = 0;
+                                       for (int i = 0; i < unique_candidate_targs.Length; ++i)
+                                               if (ExactInference (unique_candidate_targs [i], ga_v [i]) == 0)
+                                                       ++score;
+                                       return score;
                                }
-                               return score;
                        }
 
                        return 0;
@@ -2967,10 +2875,11 @@ namespace Mono.CSharp {
                        return LowerBoundInference (e.Type, t) * 2;
                }
 
-               static void RemoveDependentTypes (ArrayList types, Type returnType)
+               void RemoveDependentTypes (ArrayList types, Type returnType)
                {
-                       if (returnType.IsGenericParameter) {
-                               types [returnType.GenericParameterPosition] = null;
+                       int idx = IsUnfixed (returnType);
+                       if (idx >= 0) {
+                               types [idx] = null;
                                return;
                        }