In corlib/System.Runtime.InteropServices:
[mono.git] / mcs / gmcs / generic.cs
index 98ecf371f2c438ace8971474de41a5fc90be010f..689988a63fb9d94302df9942d0673ca03fe5b3be 100644 (file)
@@ -156,6 +156,11 @@ namespace Mono.CSharp {
                        }
                }
 
+               public Constraints Clone ()
+               {
+                       return new Constraints (name, constraints, loc);
+               }
+
                GenericParameterAttributes attrs;
                TypeExpr class_constraint;
                ArrayList iface_constraints;
@@ -562,6 +567,27 @@ namespace Mono.CSharp {
 
                        return true;
                }
+
+               public void VerifyClsCompliance ()
+               {
+                       if (class_constraint_type != null && !AttributeTester.IsClsCompliant (class_constraint_type))
+                               Warning_ConstrainIsNotClsCompliant (class_constraint_type, class_constraint.Location);
+
+                       if (iface_constraint_types != null) {
+                               for (int i = 0; i < iface_constraint_types.Length; ++i) {
+                                       if (!AttributeTester.IsClsCompliant (iface_constraint_types [i]))
+                                               Warning_ConstrainIsNotClsCompliant (iface_constraint_types [i],
+                                                       ((TypeExpr)iface_constraints [i]).Location);
+                               }
+                       }
+               }
+
+               void Warning_ConstrainIsNotClsCompliant (Type t, Location loc)
+               {
+                       Report.SymbolRelatedToPreviousError (t);
+                       Report.Warning (3024, 1, loc, "Constraint type `{0}' is not CLS-compliant",
+                               TypeManager.CSharpName (t));
+               }
        }
 
        /// <summary>
@@ -737,7 +763,7 @@ namespace Mono.CSharp {
                                                TypeManager.CSharpName (mparam), TypeManager.CSharpSignature (mb));
                                        return false;
                                }
-                       } else if (DeclSpace is Iterator) {
+                       } else if (DeclSpace is CompilerGeneratedClass) {
                                TypeParameter[] tparams = DeclSpace.TypeParameters;
                                Type[] types = new Type [tparams.Length];
                                for (int i = 0; i < tparams.Length; i++)
@@ -875,7 +901,7 @@ namespace Mono.CSharp {
                public MemberList FindMembers (MemberTypes mt, BindingFlags bf,
                                               MemberFilter filter, object criteria)
                {
-                       if (constraints == null)
+                       if (gc == null)
                                return MemberList.Empty;
 
                        ArrayList members = new ArrayList ();
@@ -1220,14 +1246,20 @@ namespace Mono.CSharp {
                                        ok = false;
                                        continue;
                                }
-                               if (te is TypeParameterExpr)
+
+                               atypes[i] = te.Type;
+
+                               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",
                                                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));
@@ -1238,11 +1270,18 @@ namespace Mono.CSharp {
                                        Expression.Error_VoidInvalidInTheContext (Location);
                                        return false;
                                }
-
-                               atypes [i] = te.Type;
                        }
                        return ok;
                }
+
+               public TypeArguments Clone ()
+               {
+                       TypeArguments copy = new TypeArguments (Location);
+                       foreach (Expression ta in args)
+                               copy.args.Add (ta);
+
+                       return copy;
+               }
        }
 
        public class TypeParameterName : SimpleName
@@ -1427,6 +1466,11 @@ namespace Mono.CSharp {
 
                public override bool AsAccessible (DeclSpace ds, int flags)
                {
+                       foreach (Type t in atypes) {
+                               if (!ds.AsAccessible (t, flags))
+                                       return false;
+                       }
+
                        return ds.AsAccessible (gt, flags);
                }
 
@@ -1523,8 +1567,8 @@ namespace Mono.CSharp {
                                if (agc != null) {
                                        if (agc is Constraints)
                                                ((Constraints) agc).Resolve (ec);
-                                       is_class = agc.HasReferenceTypeConstraint;
-                                       is_struct = agc.HasValueTypeConstraint;
+                                       is_class = agc.IsReferenceType;
+                                       is_struct = agc.IsValueType;
                                } else {
                                        is_class = is_struct = false;
                                }
@@ -1896,6 +1940,16 @@ namespace Mono.CSharp {
                public override string DocCommentHeader {
                        get { return "M:"; }
                }
+
+               public new void VerifyClsCompliance ()
+               {
+                       foreach (TypeParameter tp in TypeParameters) {
+                               if (tp.Constraints == null)
+                                       continue;
+
+                               tp.Constraints.VerifyClsCompliance ();
+                       }
+               }
        }
 
        public class DefaultValueExpression : Expression
@@ -1916,20 +1970,25 @@ namespace Mono.CSharp {
 
                        type = texpr.Type;
 
+                       if (type == TypeManager.void_type) {
+                               Error_VoidInvalidInTheContext (loc);
+                               return null;
+                       }
+
                        if (type.IsGenericParameter)
                        {
                                GenericConstraints constraints = TypeManager.GetTypeParameterConstraints(type);
                                if (constraints != null && constraints.IsReferenceType)
-                                       return new NullDefault(loc, this);
+                                       return new NullDefault (new NullLiteral (Location), type);
                        }
                        else
                        {
                                Constant c = New.Constantify(type);
                                if (c != null)
-                                       return c;
+                                       return new NullDefault (c, type);
 
-                               if (!TypeManager.IsValueType(type))
-                                       return new NullDefault(loc, this);
+                               if (!TypeManager.IsValueType (type))
+                                       return new NullDefault (new NullLiteral (Location), type);
                        }
                        eclass = ExprClass.Variable;
                        return this;
@@ -2047,8 +2106,8 @@ namespace Mono.CSharp {
                ///   Check whether `a' and `b' may become equal generic types.
                ///   The algorithm to do that is a little bit complicated.
                /// </summary>
-               public static bool MayBecomeEqualGenericTypes (Type a, Type b, Type[] class_infered,
-                                                              Type[] method_infered)
+               public static bool MayBecomeEqualGenericTypes (Type a, Type b, Type[] class_inferred,
+                                                              Type[] method_inferred)
                {
                        if (a.IsGenericParameter) {
                                //
@@ -2070,7 +2129,7 @@ namespace Mono.CSharp {
                                // 
                                if (b.IsGenericParameter || !b.IsGenericType) {
                                        int pos = a.GenericParameterPosition;
-                                       Type[] args = a.DeclaringMethod != null ? method_infered : class_infered;
+                                       Type[] args = a.DeclaringMethod != null ? method_inferred : class_inferred;
                                        if (args [pos] == null) {
                                                args [pos] = b;
                                                return true;
@@ -2102,7 +2161,7 @@ namespace Mono.CSharp {
                        }
 
                        if (b.IsGenericParameter)
-                               return MayBecomeEqualGenericTypes (b, a, class_infered, method_infered);
+                               return MayBecomeEqualGenericTypes (b, a, class_inferred, method_inferred);
 
                        //
                        // At this point, neither a nor b are a type parameter.
@@ -2114,7 +2173,7 @@ namespace Mono.CSharp {
                        //
 
                        if (a.IsGenericType || b.IsGenericType)
-                               return MayBecomeEqualGenericInstances (a, b, class_infered, method_infered);
+                               return MayBecomeEqualGenericInstances (a, b, class_inferred, method_inferred);
 
                        //
                        // If both of them are arrays.
@@ -2127,7 +2186,7 @@ namespace Mono.CSharp {
                                a = a.GetElementType ();
                                b = b.GetElementType ();
 
-                               return MayBecomeEqualGenericTypes (a, b, class_infered, method_infered);
+                               return MayBecomeEqualGenericTypes (a, b, class_inferred, method_inferred);
                        }
 
                        //
@@ -2142,8 +2201,8 @@ namespace Mono.CSharp {
                // particular instantiation (26.3.1).
                //
                public static bool MayBecomeEqualGenericInstances (Type a, Type b,
-                                                                  Type[] class_infered,
-                                                                  Type[] method_infered)
+                                                                  Type[] class_inferred,
+                                                                  Type[] method_inferred)
                {
                        if (!a.IsGenericType || !b.IsGenericType)
                                return false;
@@ -2151,29 +2210,25 @@ namespace Mono.CSharp {
                                return false;
 
                        return MayBecomeEqualGenericInstances (
-                               GetTypeArguments (a), GetTypeArguments (b), class_infered, method_infered);
+                               GetTypeArguments (a), GetTypeArguments (b), class_inferred, method_inferred);
                }
 
                public static bool MayBecomeEqualGenericInstances (Type[] aargs, Type[] bargs,
-                                                                  Type[] class_infered,
-                                                                  Type[] method_infered)
+                                                                  Type[] class_inferred,
+                                                                  Type[] method_inferred)
                {
                        if (aargs.Length != bargs.Length)
                                return false;
 
                        for (int i = 0; i < aargs.Length; i++) {
-                               if (!MayBecomeEqualGenericTypes (aargs [i], bargs [i], class_infered, method_infered))
+                               if (!MayBecomeEqualGenericTypes (aargs [i], bargs [i], class_inferred, method_inferred))
                                        return false;
                        }
 
                        return true;
                }
 
-               //
-               // Type inference.
-               //
-
-               static bool InferType (Type pt, Type at, Type[] infered)
+               static bool UnifyType (Type pt, Type at, Type[] inferred)
                {
                        if (pt.IsGenericParameter) {
                                if (pt.DeclaringMethod == null)
@@ -2181,20 +2236,15 @@ namespace Mono.CSharp {
 
                                int pos = pt.GenericParameterPosition;
 
-                               if (infered [pos] == null) {
-                                       infered [pos] = at;
-                                       return true;
-                               }
-
-                               if (infered [pos] != at)
-                                       return false;
+                               if (inferred [pos] == null)
+                                       inferred [pos] = at;
 
-                               return true;
+                               return inferred [pos] == at;
                        }
 
                        if (!pt.ContainsGenericParameters) {
                                if (at.ContainsGenericParameters)
-                                       return InferType (at, pt, infered);
+                                       return UnifyType (at, pt, inferred);
                                else
                                        return true;
                        }
@@ -2204,7 +2254,7 @@ namespace Mono.CSharp {
                                        if (at.GetArrayRank () != pt.GetArrayRank ())
                                                return false;
 
-                                       return InferType (pt.GetElementType (), at.GetElementType (), infered);
+                                       return UnifyType (pt.GetElementType (), at.GetElementType (), inferred);
                                }
 
                                if (!pt.IsGenericType)
@@ -2216,7 +2266,7 @@ namespace Mono.CSharp {
                                        return false;
 
                                Type[] args = GetTypeArguments (pt);
-                               return InferType (args [0], at.GetElementType (), infered);
+                               return UnifyType (args [0], at.GetElementType (), inferred);
                        }
 
                        if (pt.IsArray) {
@@ -2224,11 +2274,11 @@ namespace Mono.CSharp {
                                    (pt.GetArrayRank () != at.GetArrayRank ()))
                                        return false;
 
-                               return InferType (pt.GetElementType (), at.GetElementType (), infered);
+                               return UnifyType (pt.GetElementType (), at.GetElementType (), inferred);
                        }
 
                        if (pt.IsByRef && at.IsByRef)
-                               return InferType (pt.GetElementType (), at.GetElementType (), infered);
+                               return UnifyType (pt.GetElementType (), at.GetElementType (), inferred);
                        ArrayList list = new ArrayList ();
                        if (at.IsGenericType)
                                list.Add (at);
@@ -2237,51 +2287,26 @@ namespace Mono.CSharp {
 
                        list.AddRange (TypeManager.GetInterfaces (at));
 
-                       bool found_one = false;
-
                        foreach (Type type in list) {
                                if (!type.IsGenericType)
                                        continue;
 
-                               Type[] infered_types = new Type [infered.Length];
-
-                               if (!InferGenericInstance (pt, type, infered_types))
+                               if (DropGenericTypeArguments (pt) != DropGenericTypeArguments (type))
                                        continue;
 
-                               for (int i = 0; i < infered_types.Length; i++) {
-                                       if (infered [i] == null) {
-                                               infered [i] = infered_types [i];
-                                               continue;
-                                       }
-
-                                       if (infered [i] != infered_types [i])
-                                               return false;
-                               }
-
-                               found_one = true;
+                               if (!UnifyTypes (pt.GetGenericArguments (), type.GetGenericArguments (), inferred))
+                                       return false;
                        }
 
-                       return found_one;
+                       return true;
                }
 
-               static bool InferGenericInstance (Type pt, Type at, Type[] infered_types)
+               static bool UnifyTypes (Type[] pts, Type [] ats, Type [] inferred)
                {
-                       Type[] at_args = at.GetGenericArguments ();
-                       Type[] pt_args = pt.GetGenericArguments ();
-
-                       if (at_args.Length != pt_args.Length)
-                               return false;
-
-                       for (int i = 0; i < at_args.Length; i++) {
-                               if (!InferType (pt_args [i], at_args [i], infered_types))
+                       for (int i = 0; i < ats.Length; i++) {
+                               if (!UnifyType (pts [i], ats [i], inferred))
                                        return false;
                        }
-
-                       for (int i = 0; i < infered_types.Length; i++) {
-                               if (infered_types [i] == null)
-                                       return false;
-                       }
-
                        return true;
                }
 
@@ -2315,7 +2340,7 @@ namespace Mono.CSharp {
                                return false;
 
                        Type[] method_args = method.GetGenericArguments ();
-                       Type[] infered_types = new Type [method_args.Length];
+                       Type[] inferred_types = new Type [method_args.Length];
 
                        //
                        // If we have come this far, the case which
@@ -2331,7 +2356,7 @@ namespace Mono.CSharp {
                                Type pt = pd.ParameterType (i);
                                Type at = a.Type;
 
-                               if (!InferType (pt, at, infered_types))
+                               if (!UnifyType (pt, at, inferred_types))
                                        return false;
                        }
 
@@ -2343,46 +2368,154 @@ namespace Mono.CSharp {
                                if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
                                        continue;
 
-                               if (!InferType (element_type, a.Type, infered_types))
+                               if (!UnifyType (element_type, a.Type, inferred_types))
                                        return false;
                        }
 
-                       for (int i = 0; i < infered_types.Length; i++)
-                               if (infered_types [i] == null)
+                       for (int i = 0; i < inferred_types.Length; i++)
+                               if (inferred_types [i] == null)
                                        return false;
 
-                       method = ((MethodInfo)method).MakeGenericMethod (infered_types);
+                       method = ((MethodInfo)method).MakeGenericMethod (inferred_types);
                        return true;
                }
 
                static bool InferTypeArguments (Type[] param_types, Type[] arg_types,
-                                               Type[] infered_types)
+                                               Type[] inferred_types)
                {
-                       if (infered_types == null)
-                               return false;
-
                        for (int i = 0; i < arg_types.Length; i++) {
                                if (arg_types [i] == null)
                                        continue;
 
-                               if (!InferType (param_types [i], arg_types [i], infered_types))
+                               if (!UnifyType (param_types [i], arg_types [i], inferred_types))
                                        return false;
                        }
 
-                       for (int i = 0; i < infered_types.Length; i++)
-                               if (infered_types [i] == null)
+                       for (int i = 0; i < inferred_types.Length; i++)
+                               if (inferred_types [i] == null)
                                        return false;
 
                        return true;
                }
 
+               //
+               // Infers the remaining inferred_types from lambda expressions contained in the 
+               // invocation call.
+               //
+               static bool LambdaInfer (EmitContext ec, ArrayList arguments,
+                                        Type[] param_types, Type[] arg_types, Type[] inferred_types)
+               {
+                       int arg_count = arg_types.Length;
+                       
+                       for (int i = 0; i < arg_count; i++){
+                               Argument a = (Argument) arguments [i];
+                               
+                               LambdaExpression le = a.Expr as LambdaExpression;
+                               
+                               if (a == null)
+                                       continue;
+                               
+                               //
+                               // TODO: "The argument is a lambda expression, in
+                               // the following called L, from which no inferences
+                               // have yet been made."
+                               //
+                               
+                               //
+                               // "The corresponding parameter’s type, in the
+                               // following called P, is a delegate type with a
+                               // return type that involves one or more method type
+                               // parameters."
+                               //
+                               // 
+                               if (!TypeManager.IsDelegateType (param_types [i]))
+                                       continue;
+                               
+                               Type p_type = param_types [i];
+                               MethodGroupExpr method_group = Expression.MemberLookup (
+                                       ec.ContainerType, p_type, "Invoke", MemberTypes.Method,
+                                       Expression.AllBindingFlags, Location.Null) as MethodGroupExpr;
+                               
+                               if (method_group == null){
+                                       // This we report elsewhere as -200, but here we can ignore
+                                       continue;
+                               }
+                               MethodInfo delegate_method = method_group.Methods [0] as MethodInfo;
+                               if (delegate_method == null){
+                                       // This should not happen.
+                                       continue;
+                               }
+                               
+                               Type return_type = delegate_method.ReturnType;
+                               if (!return_type.IsGenericParameter)
+                                       continue;
+                               
+                               //
+                               // P and L have the same number of parameters, and
+                               // each parameter in P has the same modifiers as the
+                               // corresponding parameter in L, or no modifiers if
+                               // L has an implicitly typed parameter list.
+                               //
+                               ParameterData delegate_pd = TypeManager.GetParameterData (delegate_method);
+                               int delegate_pc = delegate_pd.Count;
+                               if (delegate_pc != le.Parameters.Count)
+                                       continue;
+
+#if false
+                               //FIXME
+                               if (le.HasExplicitParameters){
+                                       for (int j = 0; j < delegate_pc; j++){
+                                               if (delegate_pd.ParameterModifier [j] !=
+                                                   le.Parameters.ParameterModifier[j])
+                                                       goto do_continue;
+                                       }
+                               } else {
+                                       for (int j = 0; j < delegate_pc; j++)
+                                               if (le.Parameters.ParameterModifier [j] != Parameter.Modifier.NONE)
+                                                       goto do_continue;
+                               }
+#endif
+                               
+                               //
+                               // TODO: P’s parameter types involve no method type
+                               // parameters or involve only method type parameters
+                               // for which a consistent set of inferences have
+                               // already been made.
+                               //
+
+                               if (le.HasExplicitParameters){
+                                       //
+                                       // TODO: If L has an explicitly typed parameter
+                                       // list, when inferred types are substituted for
+                                       // method type parameters in P, each parameter in P
+                                       // has the same type as the corresponding parameter
+                                       // in L.
+                                       //
+                               } else {
+                                       //
+                                       // TODO: If L has an implicitly typed parameter
+                                       // list, when inferred types are substituted for
+                                       // method type parameters in P and the resulting
+                                       // parameter types are given to the parameters of L,
+                                       // the body of L is a valid expression or statement
+                                       // block.
+                               }
+                               
+                       do_continue:
+                               ;
+                       }
+
+                       return true;
+               }
+       
                /// <summary>
                ///   Type inference.  Try to infer the type arguments from `method',
                ///   which is invoked with the arguments `arguments'.  This is used
                ///   when resolving an Invocation or a DelegateInvocation and the user
                ///   did not explicitly specify type arguments.
                /// </summary>
-               public static bool InferTypeArguments (ArrayList arguments,
+               public static bool InferTypeArguments (EmitContext ec,
+                                                      ArrayList arguments,
                                                       ref MethodBase method)
                {
                        if (!TypeManager.IsGenericMethod (method))
@@ -2412,26 +2545,36 @@ namespace Mono.CSharp {
                        if (!is_open)
                                return !TypeManager.IsGenericMethodDefinition (method);
 
-                       Type[] infered_types = new Type [method_args.Length];
+                       Type[] inferred_types = new Type [method_args.Length];
 
                        Type[] param_types = new Type [pd.Count];
                        Type[] arg_types = new Type [pd.Count];
 
+                       int lambdas = 0;
                        for (int i = 0; i < arg_count; i++) {
                                param_types [i] = pd.ParameterType (i);
 
                                Argument a = (Argument) arguments [i];
+                               if (a.Expr is LambdaExpression)
+                                       lambdas++;
+                               
                                if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr) ||
-                                   (a.Expr is AnonymousMethod))
+                                   (a.Expr is AnonymousMethodExpression))
                                        continue;
 
                                arg_types [i] = a.Type;
                        }
 
-                       if (!InferTypeArguments (param_types, arg_types, infered_types))
-                               return false;
+                       if (!InferTypeArguments (param_types, arg_types, inferred_types)){
+                               Type it;
+                               if (lambdas == 0)
+                                       return false;
+                               
+                               if (!LambdaInfer (ec, arguments, param_types, arg_types, inferred_types))
+                                       return false;
+                       }
 
-                       method = ((MethodInfo)method).MakeGenericMethod (infered_types);
+                       method = ((MethodInfo)method).MakeGenericMethod (inferred_types);
                        return true;
                }
 
@@ -2449,7 +2592,7 @@ namespace Mono.CSharp {
                                return false;
 
                        Type[] method_args = method.GetGenericArguments ();
-                       Type[] infered_types = new Type [method_args.Length];
+                       Type[] inferred_types = new Type [method_args.Length];
 
                        Type[] param_types = new Type [pd.Count];
                        Type[] arg_types = new Type [pd.Count];
@@ -2459,33 +2602,12 @@ namespace Mono.CSharp {
                                arg_types [i] = apd.ParameterType (i);
                        }
 
-                       if (!InferTypeArguments (param_types, arg_types, infered_types))
+                       if (!InferTypeArguments (param_types, arg_types, inferred_types))
                                return false;
 
-                       method = ((MethodInfo)method).MakeGenericMethod (infered_types);
+                       method = ((MethodInfo)method).MakeGenericMethod (inferred_types);
                        return true;
                }
-
-               public static bool IsNullableType (Type t)
-               {
-                       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))
-                               return false;
-
-                       return GetTypeArguments (t) [0].IsValueType;
-               }
        }
 
        public abstract class Nullable
@@ -3227,14 +3349,13 @@ namespace Mono.CSharp {
                                        return this;
                                }
 
-                               if (unwrap != null) {
-                                       expr = Convert.ImplicitConversion (ec, unwrap, rtype, loc);
-                                       if (expr != null) {
-                                               left = expr;
-                                               expr = right;
-                                               type = expr.Type;
-                                               return this;
-                                       }
+                               Expression left_null = unwrap != null ? unwrap : left;
+                               expr = Convert.ImplicitConversion (ec, left_null, rtype, loc);
+                               if (expr != null) {
+                                       left = expr;
+                                       expr = right;
+                                       type = rtype;
+                                       return this;
                                }
 
                                Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);