2007-06-20 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / gmcs / generic.cs
index b75b603d295d3d632b991773039f878eec285206..89010834903d4f0605be0924f31431dfea63a69f 100644 (file)
@@ -600,6 +600,7 @@ namespace Mono.CSharp {
                Constraints constraints;
                Location loc;
                GenericTypeParameterBuilder type;
+               MemberCache member_cache;
 
                public TypeParameter (DeclSpace parent, DeclSpace decl, string name,
                                      Constraints constraints, Attributes attrs, Location loc)
@@ -619,10 +620,6 @@ namespace Mono.CSharp {
                        get { return constraints; }
                }
 
-               public bool HasConstructorConstraint {
-                       get { return constraints != null && constraints.HasConstructorConstraint; }
-               }
-
                public DeclSpace DeclSpace {
                        get { return decl; }
                }
@@ -882,11 +879,19 @@ namespace Mono.CSharp {
                }
 
                MemberCache IMemberContainer.BaseCache {
-                       get { return null; }
+                       get {
+                               if (gc == null)
+                                       return null;
+
+                               if (gc.EffectiveBaseClass.BaseType == null)
+                                       return null;
+
+                               return TypeManager.LookupMemberCache (gc.EffectiveBaseClass.BaseType);
+                       }
                }
 
                bool IMemberContainer.IsInterface {
-                       get { return true; }
+                       get { return false; }
                }
 
                MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
@@ -894,14 +899,25 @@ namespace Mono.CSharp {
                        return FindMembers (mt, bf, null, null);
                }
 
-               MemberCache IMemberContainer.MemberCache {
-                       get { return null; }
+               public MemberCache MemberCache {
+                       get {
+                               if (member_cache != null)
+                                       return member_cache;
+
+                               if (gc == null)
+                                       return null;
+
+                               Type[] ifaces = TypeManager.ExpandInterfaces (gc.InterfaceConstraints);
+                               member_cache = new MemberCache (this, gc.EffectiveBaseClass, ifaces);
+
+                               return member_cache;
+                       }
                }
 
                public MemberList FindMembers (MemberTypes mt, BindingFlags bf,
                                               MemberFilter filter, object criteria)
                {
-                       if (constraints == null)
+                       if (gc == null)
                                return MemberList.Empty;
 
                        ArrayList members = new ArrayList ();
@@ -987,7 +1003,7 @@ namespace Mono.CSharp {
                                bool has_class_constr = false;
                                if (list.Count > 0) {
                                        Type first = (Type) list [0];
-                                       has_class_constr = !first.IsInterface && !first.IsGenericParameter;
+                                       has_class_constr = !first.IsGenericParameter && !first.IsInterface;
                                }
 
                                if ((list.Count > 0) && has_class_constr) {
@@ -1119,6 +1135,12 @@ namespace Mono.CSharp {
                        this.Location = loc;
                }
 
+               public TypeArguments (Location loc, params Expression[] types)
+               {
+                       this.Location = loc;
+                       this.args = new ArrayList (types);
+               }
+               
                public TypeArguments (int dimension, Location loc)
                {
                        this.dimension = dimension;
@@ -1246,16 +1268,20 @@ namespace Mono.CSharp {
                                        ok = false;
                                        continue;
                                }
-                               if (te is TypeParameterExpr)
-                                       has_type_args = true;
 
-#if !MS_COMPATIBLE
+                               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",
                                                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 +1292,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
@@ -1621,7 +1654,7 @@ namespace Mono.CSharp {
                        if (TypeManager.IsBuiltinType (atype) || atype.IsValueType)
                                return true;
 
-                       if (HasDefaultConstructor (ec.DeclContainer.TypeBuilder, atype))
+                       if (HasDefaultConstructor (atype))
                                return true;
 
                        Report_SymbolRelatedToPreviousError ();
@@ -1670,7 +1703,7 @@ namespace Mono.CSharp {
                        return false;
                }
 
-               bool HasDefaultConstructor (Type containerType, Type atype)
+               bool HasDefaultConstructor (Type atype)
                {
                        if (atype.IsAbstract)
                                return false;
@@ -1698,8 +1731,12 @@ namespace Mono.CSharp {
                        }
 
                        TypeParameter tparam = TypeManager.LookupTypeParameter (atype);
-                       if (tparam != null)
-                               return tparam.HasConstructorConstraint;
+                       if (tparam != null) {
+                               if (tparam.GenericConstraints == null)
+                                       return false;
+                               else
+                                       return tparam.GenericConstraints.HasConstructorConstraint;
+                       }
 
                        MemberList list = TypeManager.FindMembers (
                                atype, MemberTypes.Constructor,
@@ -1841,8 +1878,10 @@ namespace Mono.CSharp {
                                        Error_ParameterNameCollision (p.Location, type_argument_name, "method parameter");
                                        return false;
                                }
+
+                               // FIXME: This is wrong, since it only looks at the outermost set of variables
                                if (block != null) {
-                                       LocalInfo li = (LocalInfo)block.Variables[type_argument_name];
+                                       LocalInfo li = (LocalInfo)block.Variables [type_argument_name];
                                        if (li != null) {
                                                Error_ParameterNameCollision (li.Location, type_argument_name, "local variable");
                                                return false;
@@ -2063,9 +2102,8 @@ namespace Mono.CSharp {
                static void InitGenericCodeHelpers ()
                {
                        // Activator
-                       Type [] type_arg = { type_type };
                        activator_create_instance = GetMethod (
-                               activator_type, "CreateInstance", type_arg);
+                               activator_type, "CreateInstance", Type.EmptyTypes);
                }
 
                static Type CoreLookupType (string ns, string name, int arity)
@@ -2095,8 +2133,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) {
                                //
@@ -2118,7 +2156,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;
@@ -2150,7 +2188,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.
@@ -2162,7 +2200,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.
@@ -2175,7 +2213,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);
                        }
 
                        //
@@ -2190,8 +2228,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;
@@ -2199,29 +2237,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)
@@ -2229,20 +2263,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;
                        }
@@ -2252,7 +2281,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)
@@ -2264,7 +2293,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) {
@@ -2272,11 +2301,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);
@@ -2285,51 +2314,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))
-                                       return false;
-                       }
-
-                       for (int i = 0; i < infered_types.Length; i++) {
-                               if (infered_types [i] == null)
+                       for (int i = 0; i < ats.Length; i++) {
+                               if (!UnifyType (pts [i], ats [i], inferred))
                                        return false;
                        }
-
                        return true;
                }
 
@@ -2363,7 +2367,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
@@ -2379,7 +2383,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;
                        }
 
@@ -2391,46 +2395,236 @@ 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 type of a single LambdaExpression in the invocation call and
+               // stores the infered type in the inferred_types array.
+               //
+               // The index of the arguments that contain lambdas is passed in
+               //
+               // @lambdas.  Merely to avoid rescanning the list.
+               //
+               // The method being called:
+               //   @method_generic_args: The generic type arguments for the method being called
+               //   @method_pd: The ParameterData for the method being called.
+               //
+               // The call site:
+               //   @arguments: Arraylist of Argument()s.  The arguments being passed.
+               //
+               // Returns:
+               //   @inferred_types: the array that is populated with our results.
+               //
+               // true if the code was able to do one inference.
+               //
+               static bool LambdaInfer (EmitContext ec,
+                                        Type [] method_generic_args,
+                                        ParameterData method_pd,
+                                        ArrayList arguments,
+                                        Type[] inferred_types,
+                                        ArrayList lambdas)
+               {
+                       int last_count = lambdas.Count;
+
+                       for (int i = 0; i < last_count; i++){
+                               int argn = (int) lambdas [i];
+
+                               Argument a = (Argument) arguments [argn];
+
+                               LambdaExpression le = a.Expr as LambdaExpression;
+
+                               if (le == null)
+                                       throw new Exception (
+                                            String.Format ("Internal Compiler error: argument {0} should be a Lambda Expression",
+                                                           argn));
+                                                            
+                               //
+                               // "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 (method_pd.ParameterType (argn)))
+                                       goto useless_lambda;
+                               
+                               Type p_type = method_pd.ParameterType (argn);
+                               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
+                                       goto useless_lambda;
+                               }
+                               MethodInfo p_delegate_method = method_group.Methods [0] as MethodInfo;
+                               if (p_delegate_method == null){
+                                       // This should not happen.
+                                       goto useless_lambda;
+                               }
+                               
+                               Type p_return_type = p_delegate_method.ReturnType;
+                               if (!p_return_type.IsGenericParameter)
+                                       goto useless_lambda;
+                               
+                               //
+                               // 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 p_delegate_parameters = TypeManager.GetParameterData (p_delegate_method);
+                               int p_delegate_parameter_count = p_delegate_parameters.Count;
+                               if (p_delegate_parameter_count != le.Parameters.Count)
+                                       goto useless_lambda;
+
+                               if (le.HasExplicitParameters){
+                                       for (int j = 0; j < p_delegate_parameter_count; j++){
+                                               if (p_delegate_parameters.ParameterModifier (j) != 
+                                                   le.Parameters.ParameterModifier (j))
+                                                       goto useless_lambda;
+                                       }
+                               } else { 
+                                       for (int j = 0; j < p_delegate_parameter_count; j++)
+                                               if (le.Parameters.ParameterModifier (j) != Parameter.Modifier.NONE)
+                                                       goto useless_lambda;
+                               }
+                               
+                               //
+                               // 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.
+                               //
+                               //Console.WriteLine ("Method: {0}", p_delegate_method);
+                               //for (int j = 0; j < p_delegate_parameter_count; j++){
+                               //Console.WriteLine ("PType [{2}, {0}] = {1}", j, p_delegate_parameters.ParameterType (j), argn);
+                               //}
+                               
+                               //
+                               // At this point we know that P has method type parameters
+                               // that involve only type parameters that have a consistent
+                               // set of inferences 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.
+
+                                       Type [] types = new Type [p_delegate_parameter_count];
+
+                                       //bool failure = false;
+                                       for (int j = 0; j < p_delegate_parameter_count; j++){
+                                               Type p_pt = p_delegate_parameters.ParameterType (j);
+
+                                               if (!p_pt.IsGenericParameter){
+                                                       types [j] = p_pt;
+                                                       continue;
+                                               }
+
+                                               //bool found = false;
+                                               for (int k = 0; k < method_generic_args.Length; k++){
+                                                       if (method_generic_args [k] == p_pt){
+                                                               types [j] = inferred_types [k];
+                                                               break;
+                                                       }
+                                               }
+                                               //
+                                               // If we could not infer just yet, continue
+                                               //
+                                               if (types [j] == null)
+                                                       goto do_continue;
+                                       }
+
+                                       //
+                                       // If it results in a valid expression or statement block
+                                       //
+                                       Type lambda_inferred_type = le.TryBuild (ec, types);
+
+                                       if (lambda_inferred_type != null){
+                                               //
+                                               // Success, set the proper inferred_type value to the new type.
+                                               // return true
+                                               //
+                                               for (int k = 0; k < method_generic_args.Length; k++){
+                                                       if (method_generic_args [k] == p_return_type){
+                                                               inferred_types [k] = lambda_inferred_type;
+
+                                                               lambdas.RemoveAt (i);
+                                                               return true;
+                                                       }
+                                               }
+                                       }
+                               }
+
+                       useless_lambda:
+                               lambdas.RemoveAt (i);
+                               
+                       do_continue:
+                               ;
+                       }
+
+#if false
+                       Console.WriteLine ("Inferred types");
+                       foreach (Type it in inferred_types){
+                               Console.WriteLine ("  IT: {0}", it);
+                               if (it == null)
+                                       return false;
+                       }
+#endif
+
+                       // No inference was made in any of the elements.
+                       return false;
+               }
+       
                /// <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))
@@ -2446,11 +2640,12 @@ namespace Mono.CSharp {
                        if (arg_count != pd.Count)
                                return false;
 
-                       Type[] method_args = method.GetGenericArguments ();
+                       Type[] method_generic_args = method.GetGenericArguments ();
 
                        bool is_open = false;
-                       for (int i = 0; i < method_args.Length; i++) {
-                               if (method_args [i].IsGenericParameter) {
+                       
+                       for (int i = 0; i < method_generic_args.Length; i++) {
+                               if (method_generic_args [i].IsGenericParameter) {
                                        is_open = true;
                                        break;
                                }
@@ -2460,26 +2655,69 @@ namespace Mono.CSharp {
                        if (!is_open)
                                return !TypeManager.IsGenericMethodDefinition (method);
 
-                       Type[] infered_types = new Type [method_args.Length];
+                       Type[] inferred_types = new Type [method_generic_args.Length];
 
                        Type[] param_types = new Type [pd.Count];
                        Type[] arg_types = new Type [pd.Count];
-
+                       ArrayList lambdas = null;
+                       
                        for (int i = 0; i < arg_count; i++) {
                                param_types [i] = pd.ParameterType (i);
 
                                Argument a = (Argument) arguments [i];
-                               if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr) ||
-                                   (a.Expr is AnonymousMethodExpression))
+                               if (a.Expr is NullLiteral || a.Expr is MethodGroupExpr)
+                                       continue;
+                                                               
+                               if (a.Expr is LambdaExpression){
+                                       if (lambdas == null)
+                                               lambdas = new ArrayList ();
+                                       lambdas.Add (i);
+                               }
+                               else if (a.Expr is AnonymousMethodExpression) {
+                                       if (RootContext.Version != LanguageVersion.LINQ)
+                                               continue;
+
+                                       Type dtype = param_types[i];
+                                       if (!TypeManager.IsDelegateType (dtype))
+                                               continue;
+
+                                       AnonymousMethodExpression am = (AnonymousMethodExpression)a.Expr;
+                                       Expression e = am.InferTypeArguments (ec, dtype);
+                                       if (e == null)
+                                               return false;
+
+                                       arg_types[i] = e.Type;
                                        continue;
+                               }
 
                                arg_types [i] = a.Type;
                        }
 
-                       if (!InferTypeArguments (param_types, arg_types, infered_types))
-                               return false;
+                       if (!InferTypeArguments (param_types, arg_types, inferred_types)){
+                               //Console.WriteLine ("InferTypeArgument found {0} lambdas ", lambdas);
+                               if (lambdas == null)
+                                       return false;
+
+                               //
+                               // While the lambda expressions lead to a valid inference
+                               // 
+                               int lambda_count;
+                               do {
+                                       lambda_count = lambdas.Count;
+                                       if (!LambdaInfer (ec, method_generic_args, pd, arguments, inferred_types, lambdas))
+                                               return false;
+                               } while (lambdas.Count != 0 && lambdas.Count != lambda_count);
+                       } 
+
+                       method = ((MethodInfo)method).MakeGenericMethod (inferred_types);
+
+#if MS_COMPATIBLE
+                       // MS implementation throws NotSupportedException for GetParameters
+                       // on unbaked generic method
+                       ParameterData p = TypeManager.GetParameterData (method);
+                       p.InflateTypes (param_types, inferred_types);
+#endif
 
-                       method = ((MethodInfo)method).MakeGenericMethod (infered_types);
                        return true;
                }
 
@@ -2497,7 +2735,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];
@@ -2507,10 +2745,10 @@ 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;
                }
        }