In corlib/System.Runtime.InteropServices:
[mono.git] / mcs / gmcs / generic.cs
index 5a81309b45a9fd777247a18ea7b79a1f077db5b8..689988a63fb9d94302df9942d0673ca03fe5b3be 100644 (file)
@@ -1246,16 +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 !MS_COMPATIBLE
                                if (te.Type.IsSealed && te.Type.IsAbstract) {
                                        Report.Error (718, Location, "`{0}': static classes cannot be used as generic arguments",
                                                te.GetSignatureForError ());
                                        return false;
                                }
-#endif
+
                                if (te.Type.IsPointer) {
                                        Report.Error (306, Location, "The type `{0}' may not be used " +
                                                          "as a type argument", TypeManager.CSharpName (te.Type));
@@ -1266,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
@@ -2217,11 +2228,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               //
-               // Type inference.
-               //
-
-               static bool InferType (Type pt, Type at, Type[] inferred)
+               static bool UnifyType (Type pt, Type at, Type[] inferred)
                {
                        if (pt.IsGenericParameter) {
                                if (pt.DeclaringMethod == null)
@@ -2229,20 +2236,15 @@ namespace Mono.CSharp {
 
                                int pos = pt.GenericParameterPosition;
 
-                               if (inferred [pos] == null) {
+                               if (inferred [pos] == null)
                                        inferred [pos] = at;
-                                       return true;
-                               }
-
-                               if (inferred [pos] != at)
-                                       return false;
 
-                               return true;
+                               return inferred [pos] == at;
                        }
 
                        if (!pt.ContainsGenericParameters) {
                                if (at.ContainsGenericParameters)
-                                       return InferType (at, pt, inferred);
+                                       return UnifyType (at, pt, inferred);
                                else
                                        return true;
                        }
@@ -2252,7 +2254,7 @@ namespace Mono.CSharp {
                                        if (at.GetArrayRank () != pt.GetArrayRank ())
                                                return false;
 
-                                       return InferType (pt.GetElementType (), at.GetElementType (), inferred);
+                                       return UnifyType (pt.GetElementType (), at.GetElementType (), inferred);
                                }
 
                                if (!pt.IsGenericType)
@@ -2264,7 +2266,7 @@ namespace Mono.CSharp {
                                        return false;
 
                                Type[] args = GetTypeArguments (pt);
-                               return InferType (args [0], at.GetElementType (), inferred);
+                               return UnifyType (args [0], at.GetElementType (), inferred);
                        }
 
                        if (pt.IsArray) {
@@ -2272,11 +2274,11 @@ namespace Mono.CSharp {
                                    (pt.GetArrayRank () != at.GetArrayRank ()))
                                        return false;
 
-                               return InferType (pt.GetElementType (), at.GetElementType (), inferred);
+                               return UnifyType (pt.GetElementType (), at.GetElementType (), inferred);
                        }
 
                        if (pt.IsByRef && at.IsByRef)
-                               return InferType (pt.GetElementType (), at.GetElementType (), inferred);
+                               return UnifyType (pt.GetElementType (), at.GetElementType (), inferred);
                        ArrayList list = new ArrayList ();
                        if (at.IsGenericType)
                                list.Add (at);
@@ -2285,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[] inferred_types = new Type [inferred.Length];
-
-                               if (!InferGenericInstance (pt, type, inferred_types))
+                               if (DropGenericTypeArguments (pt) != DropGenericTypeArguments (type))
                                        continue;
 
-                               for (int i = 0; i < inferred_types.Length; i++) {
-                                       if (inferred [i] == null) {
-                                               inferred [i] = inferred_types [i];
-                                               continue;
-                                       }
-
-                                       if (inferred [i] != inferred_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[] inferred_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], inferred_types))
-                                       return false;
-                       }
-
-                       for (int i = 0; i < inferred_types.Length; i++) {
-                               if (inferred_types [i] == null)
+                       for (int i = 0; i < ats.Length; i++) {
+                               if (!UnifyType (pts [i], ats [i], inferred))
                                        return false;
                        }
-
                        return true;
                }
 
@@ -2379,7 +2356,7 @@ namespace Mono.CSharp {
                                Type pt = pd.ParameterType (i);
                                Type at = a.Type;
 
-                               if (!InferType (pt, at, inferred_types))
+                               if (!UnifyType (pt, at, inferred_types))
                                        return false;
                        }
 
@@ -2391,7 +2368,7 @@ namespace Mono.CSharp {
                                if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
                                        continue;
 
-                               if (!InferType (element_type, a.Type, inferred_types))
+                               if (!UnifyType (element_type, a.Type, inferred_types))
                                        return false;
                        }
 
@@ -2406,14 +2383,11 @@ namespace Mono.CSharp {
                static bool InferTypeArguments (Type[] param_types, Type[] arg_types,
                                                Type[] inferred_types)
                {
-                       if (inferred_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], inferred_types))
+                               if (!UnifyType (param_types [i], arg_types [i], inferred_types))
                                        return false;
                        }
 
@@ -2424,13 +2398,124 @@ namespace Mono.CSharp {
                        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))
@@ -2465,10 +2550,14 @@ namespace Mono.CSharp {
                        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 AnonymousMethodExpression))
                                        continue;
@@ -2476,8 +2565,14 @@ namespace Mono.CSharp {
                                arg_types [i] = a.Type;
                        }
 
-                       if (!InferTypeArguments (param_types, arg_types, inferred_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 (inferred_types);
                        return true;