Fix the build
[mono.git] / mcs / mcs / delegate.cs
index 9c4cacdb403c494f09396052ddce8336f6eb0a11..fa6a8e8ba46b0a1b0b6ca617912ccfe0e2164d71 100644 (file)
@@ -47,7 +47,7 @@ namespace Mono.CSharp {
                        Modifiers.UNSAFE |
                        Modifiers.PRIVATE;
 
-               public Delegate (NamespaceEntry ns, TypeContainer parent, Expression type,
+               public Delegate (NamespaceEntry ns, DeclSpace parent, Expression type,
                                 int mod_flags, MemberName name, Parameters param_list,
                                 Attributes attrs)
                        : base (ns, parent, name, attrs)
@@ -81,11 +81,14 @@ namespace Mono.CSharp {
                        if (TypeManager.multicast_delegate_type == null && !RootContext.StdLib) {
                                Namespace system = RootNamespace.Global.GetNamespace ("System", true);
                                TypeExpr expr = system.Lookup (this, "MulticastDelegate", Location) as TypeExpr;
-                               TypeManager.multicast_delegate_type = expr.ResolveType (this);
+                               TypeManager.multicast_delegate_type = expr.Type;
                        }
 
                        if (TypeManager.multicast_delegate_type == null)
-                               throw new InternalErrorException ("System.MulticastDelegate unresolved");
+                               Report.Error (-100, Location, "Internal error: delegate used before " +
+                                             "System.MulticastDelegate is resolved.  This can only " +
+                                             "happen during corlib compilation, when using a delegate " +
+                                             "in any of the `core' classes.  See bug #72015 for details.");
 
                        if (IsTopLevel) {
                                if (TypeManager.NamespaceClash (Name, Location))
@@ -105,13 +108,57 @@ namespace Mono.CSharp {
 
                        TypeManager.AddUserType (this);
 
+#if GMCS_SOURCE
+                       if (IsGeneric) {
+                               string[] param_names = new string [TypeParameters.Length];
+                               for (int i = 0; i < TypeParameters.Length; i++)
+                                       param_names [i] = TypeParameters [i].Name;
+
+                               GenericTypeParameterBuilder[] gen_params;
+                               gen_params = TypeBuilder.DefineGenericParameters (param_names);
+
+                               int offset = CountTypeParameters - CurrentTypeParameters.Length;
+                               for (int i = offset; i < gen_params.Length; i++)
+                                       CurrentTypeParameters [i - offset].Define (gen_params [i]);
+
+                               foreach (TypeParameter type_param in CurrentTypeParameters) {
+                                       if (!type_param.Resolve (this))
+                                               return null;
+                               }
+
+                               Expression current = new SimpleName (
+                                       MemberName.Basename, TypeParameters, Location);
+                               current = current.ResolveAsTypeTerminal (this, false);
+                               if (current == null)
+                                       return null;
+
+                               CurrentType = current.Type;
+                       }
+#endif
+
                        return TypeBuilder;
                }
 
                public override bool Define ()
                {
-                       MethodAttributes mattr;
-                       int i;
+#if GMCS_SOURCE
+                       if (IsGeneric) {
+                               foreach (TypeParameter type_param in TypeParameters) {
+                                       if (!type_param.Resolve (this))
+                                               return false;
+                               }
+
+                               foreach (TypeParameter type_param in TypeParameters) {
+                                       if (!type_param.DefineType (this))
+                                               return false;
+                               }
+
+                               foreach (TypeParameter type_param in TypeParameters) {
+                                       if (!type_param.CheckDependencies ())
+                                               return false;
+                               }
+                       }
+#endif
 
                        // FIXME: POSSIBLY make this static, as it is always constant
                        //
@@ -119,10 +166,10 @@ namespace Mono.CSharp {
                        const_arg_types [0] = TypeManager.object_type;
                        const_arg_types [1] = TypeManager.intptr_type;
 
-                       mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
+                       const MethodAttributes ctor_mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
                                MethodAttributes.HideBySig | MethodAttributes.Public;
 
-                       ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
+                       ConstructorBuilder = TypeBuilder.DefineConstructor (ctor_mattr,
                                                                            CallingConventions.Standard,
                                                                            const_arg_types);
 
@@ -171,13 +218,9 @@ namespace Mono.CSharp {
                        ReturnType = ReturnType.ResolveAsTypeTerminal (this, false);
                        if (ReturnType == null)
                                return false;
-                        
-                       ret_type = ReturnType.Type;
-                       if (ret_type == null)
-                               return false;
-
-                       CheckObsoleteType (ReturnType);
 
+                       ret_type = ReturnType.Type;
+            
                        if (!Parent.AsAccessible (ret_type, ModFlags)) {
                                Report.Error (58, Location,
                                              "Inconsistent accessibility: return type `" +
@@ -198,22 +241,13 @@ namespace Mono.CSharp {
                        
                        CallingConventions cc = Parameters.CallingConvention;
 
-                       mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
+                       const MethodAttributes mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.NewSlot;
 
                        InvokeBuilder = TypeBuilder.DefineMethod ("Invoke", 
                                                                  mattr,                     
                                                                  cc,
                                                                  ret_type,                  
                                                                  Parameters.Types);
-
-                       //
-                       // Define parameters, and count out/ref parameters
-                       //
-                       int out_params = 0;
-                       foreach (Parameter p in Parameters.FixedParameters) {
-                               if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
-                                       out_params++;
-                       }
                        
                        InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
 
@@ -222,86 +256,64 @@ namespace Mono.CSharp {
                        //
                        // BeginInvoke
                        //
-                       int params_num = Parameters.Count;
-                       Type [] async_param_types = new Type [params_num + 2];
-
-                       Parameters.Types.CopyTo (async_param_types, 0);
-
-                       async_param_types [params_num] = TypeManager.asynccallback_type;
-                       async_param_types [params_num + 1] = TypeManager.object_type;
-
-                       mattr = MethodAttributes.Public | MethodAttributes.HideBySig |
-                               MethodAttributes.Virtual | MethodAttributes.NewSlot;
                        
-                       BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
-                                                                      mattr,
-                                                                      cc,
-                                                                      TypeManager.iasyncresult_type,
-                                                                      async_param_types);
-
-                       i = Parameters.Count;
-                       Parameters.ApplyAttributes (BeginInvokeBuilder);
-                       BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
-                       BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
+                       Parameters async_parameters = Parameters.MergeGenerated (Parameters, 
+                               new Parameter (TypeManager.asynccallback_type, "callback", Parameter.Modifier.NONE, null, Location),
+                               new Parameter (TypeManager.object_type, "object", Parameter.Modifier.NONE, null, Location));
                        
-                       BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
+                       BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
+                               mattr, cc, TypeManager.iasyncresult_type, async_parameters.Types);
 
-                       Parameter [] async_params = new Parameter [params_num + 2];
-                       Parameters.FixedParameters.CopyTo (async_params, 0);
-                       
-                       async_params [params_num] = new Parameter (
-                               TypeManager.asynccallback_type, "callback",
-                                                                  Parameter.Modifier.NONE, null, Location);
-                       async_params [params_num + 1] = new Parameter (
-                               TypeManager.object_type, "object",
-                                                                  Parameter.Modifier.NONE, null, Location);
-
-                       Parameters async_parameters = new Parameters (async_params);
-                       async_parameters.Resolve (this);
+                       BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
                        async_parameters.ApplyAttributes (BeginInvokeBuilder);
-
                        TypeManager.RegisterMethod (BeginInvokeBuilder, async_parameters);
 
                        //
                        // EndInvoke is a bit more interesting, all the parameters labeled as
                        // out or ref have to be duplicated here.
                        //
-                       
-                       Type [] end_param_types = new Type [out_params + 1];
-                       Parameter [] end_params = new Parameter [out_params + 1];
-                       int param = 0; 
-                       if (out_params > 0){
-                               int top = Parameters.FixedParameters.Length;
-                               for (i = 0; i < top; i++){
+
+                       //
+                       // Define parameters, and count out/ref parameters
+                       //
+                       Parameters end_parameters;
+                       int out_params = 0;
+
+                       foreach (Parameter p in Parameters.FixedParameters) {
+                               if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
+                                       ++out_params;
+                       }
+
+                       if (out_params > 0) {
+                               Type [] end_param_types = new Type [out_params];
+                               Parameter [] end_params = new Parameter [out_params ];
+
+                               int param = 0; 
+                               for (int i = 0; i < Parameters.FixedParameters.Length; ++i) {
                                        Parameter p = Parameters.FixedParameters [i];
                                        if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
                                                continue;
 
-                                       end_param_types [param] = Parameters.Types [i];
+                                       end_param_types [param] = p.ExternalType();
                                        end_params [param] = p;
-                                       param++;
+                                       ++param;
                                }
+                               end_parameters = new Parameters (end_params, end_param_types);
+                       }
+                       else {
+                               end_parameters = Parameters.EmptyReadOnlyParameters;
                        }
-                       end_param_types [out_params] = TypeManager.iasyncresult_type;
-                       end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null, Location);
 
+                       end_parameters = Parameters.MergeGenerated (end_parameters,
+                               new Parameter (TypeManager.iasyncresult_type, "result", Parameter.Modifier.NONE, null, Location));
+                       
                        //
                        // Create method, define parameters, register parameters with type system
                        //
-                       EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_param_types);
+                       EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_parameters.Types);
                        EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
 
-                       //
-                       // EndInvoke: Label the parameters
-                       //
-                       EndInvokeBuilder.DefineParameter (out_params + 1, ParameterAttributes.None, "result");
-                       for (i = 0; i < end_params.Length-1; i++){
-                               EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
-                       }
-
-                       Parameters end_parameters = new Parameters (end_params);
-                       end_parameters.Resolve (this);
-
+                       end_parameters.ApplyAttributes (EndInvokeBuilder);
                        TypeManager.RegisterMethod (EndInvokeBuilder, end_parameters);
 
                        return true;
@@ -351,40 +363,50 @@ namespace Mono.CSharp {
                // Returns the MethodBase for "Invoke" from a delegate type, this is used
                // to extract the signature of a delegate.
                //
-               public static MethodInfo GetInvokeMethod (EmitContext ec, Type delegate_type, Location loc)
+               public static MethodGroupExpr GetInvokeMethod (Type container_type, Type delegate_type, Location loc)
                {
-                       Expression ml = Expression.MemberLookup (
-                               ec, delegate_type, "Invoke", loc);
+                       Expression ml = Expression.MemberLookup (container_type, null, delegate_type,
+                               "Invoke", loc);
 
-                       if (!(ml is MethodGroupExpr)) {
+                       MethodGroupExpr mg = ml as MethodGroupExpr;
+                       if (mg == null) {
                                Report.Error (-100, loc, "Internal error: could not find Invoke method!");
                                return null;
                        }
 
-                       return (MethodInfo) (((MethodGroupExpr) ml).Methods [0]);
+                       return mg;
                }
                
                /// <summary>
                ///  Verifies whether the method in question is compatible with the delegate
                ///  Returns the method itself if okay and null if not.
                /// </summary>
-               public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb,
+               public static MethodBase VerifyMethod (Type container_type, Type delegate_type,
+                                                      MethodGroupExpr old_mg, MethodBase mb,
                                                       Location loc)
                {
-                       ParameterData pd = TypeManager.GetParameterData (mb);
+                       MethodGroupExpr mg = GetInvokeMethod (container_type, delegate_type, loc);
+                       if (mg == null)
+                               return null;
 
-                       int pd_count = pd.Count;
+                       if (old_mg.HasTypeArguments)
+                               mg.HasTypeArguments = true;
+
+                       MethodBase invoke_mb = mg.Methods [0];
+                       ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
 
-                       MethodBase invoke_mb = GetInvokeMethod (ec, delegate_type, loc);
-                       if (invoke_mb == null)
+#if GMCS_SOURCE
+                       if (!mg.HasTypeArguments &&
+                           !TypeManager.InferTypeArguments (invoke_pd, ref mb))
                                return null;
+#endif
 
-                       ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
+                       ParameterData pd = TypeManager.GetParameterData (mb);
 
-                       if (invoke_pd.Count != pd_count)
+                       if (invoke_pd.Count != pd.Count)
                                return null;
 
-                       for (int i = pd_count; i > 0; ) {
+                       for (int i = pd.Count; i > 0; ) {
                                i--;
 
                                Type invoke_pd_type = invoke_pd.ParameterType (i);
@@ -392,35 +414,34 @@ namespace Mono.CSharp {
                                Parameter.Modifier invoke_pd_type_mod = invoke_pd.ParameterModifier (i);
                                Parameter.Modifier pd_type_mod = pd.ParameterModifier (i);
 
-                               if (invoke_pd_type == pd_type &&
-                                   invoke_pd_type_mod == pd_type_mod)
+                               invoke_pd_type_mod &= ~Parameter.Modifier.PARAMS;
+                               pd_type_mod &= ~Parameter.Modifier.PARAMS;
+
+                               if (invoke_pd_type_mod != pd_type_mod)
+                                       return null;
+
+                               if (invoke_pd_type == pd_type)
                                        continue;
-                               
-                               if (invoke_pd_type.IsSubclassOf (pd_type) && 
-                                               invoke_pd_type_mod == pd_type_mod)
-                                       if (RootContext.Version == LanguageVersion.ISO_1) {
-                                               Report.FeatureIsNotStandardized (loc, "contravariance");
-                                               return null;
-                                       } else
-                                               continue;
-                                       
-                               return null;
+
+                               if (!Convert.ImplicitReferenceConversionExists (new EmptyExpression (invoke_pd_type), pd_type))
+                                       return null;
+
+                               if (RootContext.Version == LanguageVersion.ISO_1)
+                                       return null;
                        }
 
                        Type invoke_mb_retval = ((MethodInfo) invoke_mb).ReturnType;
                        Type mb_retval = ((MethodInfo) mb).ReturnType;
                        if (invoke_mb_retval == mb_retval)
                                return mb;
-                       
-                       if (mb_retval.IsSubclassOf (invoke_mb_retval))
-                               if (RootContext.Version == LanguageVersion.ISO_1) {
-                                       Report.FeatureIsNotStandardized (loc, "covariance");
-                                       return null;
-                               }
-                               else
-                                       return mb;
-                       
-                       return null;
+
+                       if (!Convert.ImplicitReferenceConversionExists (new EmptyExpression (mb_retval), invoke_mb_retval))
+                               return null;
+
+                       if (RootContext.Version == LanguageVersion.ISO_1) 
+                               return null;
+
+                       return mb;
                }
 
                // <summary>
@@ -438,7 +459,7 @@ namespace Mono.CSharp {
                                arg_count = args.Count;
 
                        Expression ml = Expression.MemberLookup (
-                               ec, delegate_type, "Invoke", loc);
+                               ec.ContainerType, delegate_type, "Invoke", loc);
 
                        MethodGroupExpr me = ml as MethodGroupExpr;
                        if (me == null) {
@@ -453,10 +474,10 @@ namespace Mono.CSharp {
 
                        bool params_method = pd.HasParams;
                        bool is_params_applicable = false;
-                       bool is_applicable = Invocation.IsApplicable (ec, args, arg_count, mb);
+                       bool is_applicable = Invocation.IsApplicable (ec, me, args, arg_count, ref mb);
 
                        if (!is_applicable && params_method &&
-                           Invocation.IsParamsMethodApplicable (ec, args, arg_count, mb))
+                           Invocation.IsParamsMethodApplicable (ec, me, args, arg_count, ref mb))
                                is_applicable = is_params_applicable = true;
 
                        if (!is_applicable && !params_method && arg_count != pd_count) {
@@ -475,10 +496,10 @@ namespace Mono.CSharp {
                ///  Verifies whether the delegate in question is compatible with this one in
                ///  order to determine if instantiation from the same is possible.
                /// </summary>
-               public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Type probe_type, Location loc)
+               public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Location loc)
                {
                        Expression ml = Expression.MemberLookup (
-                               ec, delegate_type, "Invoke", loc);
+                               ec.ContainerType, delegate_type, "Invoke", loc);
                        
                        if (!(ml is MethodGroupExpr)) {
                                Report.Error (-100, loc, "Internal error: could not find Invoke method!");
@@ -489,7 +510,7 @@ namespace Mono.CSharp {
                        ParameterData pd = TypeManager.GetParameterData (mb);
 
                        Expression probe_ml = Expression.MemberLookup (
-                               ec, delegate_type, "Invoke", loc);
+                               ec.ContainerType, delegate_type, "Invoke", loc);
                        
                        if (!(probe_ml is MethodGroupExpr)) {
                                Report.Error (-100, loc, "Internal error: could not find Invoke method!");
@@ -530,13 +551,14 @@ namespace Mono.CSharp {
                public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
                                                        MemberFilter filter, object criteria)
                {
-                       ArrayList members = new ArrayList ();
+                       ArrayList members = new ArrayList (2);
 
-                       if ((mt & MemberTypes.Method) != 0) {
-                               if (ConstructorBuilder != null)
-                               if (filter (ConstructorBuilder, criteria))
+                       if ((mt & MemberTypes.Constructor) != 0) {
+                               if (ConstructorBuilder != null && filter (ConstructorBuilder, criteria))
                                        members.Add (ConstructorBuilder);
+                       }
 
+                       if ((mt & MemberTypes.Method) != 0) {
                                if (InvokeBuilder != null)
                                if (filter (InvokeBuilder, criteria))
                                        members.Add (InvokeBuilder);
@@ -595,7 +617,6 @@ namespace Mono.CSharp {
                public override string DocCommentHeader {
                        get { return "T:"; }
                }
-
        }
 
        //
@@ -607,13 +628,13 @@ namespace Mono.CSharp {
                protected MethodGroupExpr method_group;
                protected Expression delegate_instance_expression;
 
-               public DelegateCreation () {}
+               protected DelegateCreation () {}
 
                public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
                {
                        string method_desc;
-                       MethodInfo found_method = (MethodInfo)mg.Methods [0];
-                       
+                       MethodBase found_method = mg.Methods [0];
+
                        if (mg.Methods.Length > 1)
                                method_desc = found_method.Name;
                        else
@@ -627,10 +648,32 @@ namespace Mono.CSharp {
                        ParameterData param = TypeManager.GetParameterData (method);
                        string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
 
-                       if (method.ReturnType != found_method.ReturnType) {
+#if GMCS_SOURCE
+                       if (!mg.HasTypeArguments &&
+                           !TypeManager.InferTypeArguments (param, ref found_method)) {
+                               Report.Error (411, loc, "The type arguments for " +
+                                             "method `{0}' cannot be inferred from " +
+                                             "the usage. Try specifying the type " +
+                                             "arguments explicitly.", method_desc);
+                               return;
+                       }
+#endif
+                       Report.SymbolRelatedToPreviousError (found_method);
+
+                       if (RootContext.Version == LanguageVersion.ISO_1) {
+                               Report.Error (410, loc, "The method `{0}' parameters and return type must be same as delegate `{1}' parameters and return type",
+                                       method_desc, delegate_desc);
+                               return;
+                       }
+
+                       Type delegateType = method.ReturnType;
+                       Type methodType = ((MethodInfo) found_method).ReturnType;
+                       if (delegateType != methodType &&
+                               !Convert.ImplicitReferenceConversionExists (new EmptyExpression (methodType), delegateType)) {
                                Report.Error (407, loc, "`{0}' has the wrong return type to match the delegate `{1}'", method_desc, delegate_desc);
                        } else {
-                               Report.Error (123, loc, "Method `{0}' does not match delegate `{1}'", method_desc, delegate_desc);
+                               Report.Error (123, loc, "The method `{0}' parameters do not match delegate `{1}' parameters",
+                                       TypeManager.CSharpSignature (found_method), delegate_desc);
                        }
                }
                
@@ -651,8 +694,8 @@ namespace Mono.CSharp {
 
                protected bool ResolveConstructorMethod (EmitContext ec)
                {
-                       Expression ml = Expression.MemberLookup (
-                               ec, type, ".ctor", loc);
+                       Expression ml = Expression.MemberLookupFinal(ec, 
+                               null, type, ".ctor", MemberTypes.Constructor, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
 
                        if (!(ml is MethodGroupExpr)) {
                                Report.Error (-100, loc, "Internal error: Could not find delegate constructor!");
@@ -663,19 +706,22 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg,
-                                                            bool check_only)
+               public static MethodBase ImplicitStandardConversionExists (MethodGroupExpr mg, Type targetType)
                {
                        foreach (MethodInfo mi in mg.Methods){
-                               delegate_method  = Delegate.VerifyMethod (ec, type, mi, loc);
-                               
-                               if (delegate_method != null)
-                                       break;
+                               MethodBase mb = Delegate.VerifyMethod (mg.DeclaringType, targetType, mg, mi, Location.Null);
+                               if (mb != null)
+                                       return mb;
                        }
-                       
+                       return null;
+               }
+
+               protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
+               {
+                       delegate_method = ImplicitStandardConversionExists (mg, type);
+
                        if (delegate_method == null) {
-                               if (!check_only)
-                                       Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
+                               Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
                                return null;
                        }
                        
@@ -693,7 +739,7 @@ namespace Mono.CSharp {
                                        }
                                }
                        }
-                       
+                                               
                        //TODO: implement caching when performance will be low
                        IMethodData md = TypeManager.GetMethod (delegate_method);
                        if (md == null) {
@@ -722,11 +768,11 @@ namespace Mono.CSharp {
                                delegate_instance_expression = null;
                        } else
                                delegate_instance_expression = ec.GetThis (loc);
-                       
+
                        if (delegate_instance_expression != null && delegate_instance_expression.Type.IsValueType)
                                delegate_instance_expression = new BoxedCast (
                                        delegate_instance_expression, TypeManager.object_type);
-                       
+
                        method_group = mg;
                        eclass = ExprClass.Value;
                        return this;
@@ -750,16 +796,16 @@ namespace Mono.CSharp {
                }
 
                static public Expression Create (EmitContext ec, MethodGroupExpr mge,
-                                                Type target_type, bool check_only, Location loc)
+                                                Type target_type, Location loc)
                {
                        ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
-                       if (d.ResolveConstructorMethod (ec))
-                               return d.ResolveMethodGroupExpr (ec, mge, check_only);
-                       else
+                       if (!d.ResolveConstructorMethod (ec))
                                return null;
+
+                       return d.ResolveMethodGroupExpr (ec, mge);
                }
        }
-
+       
        //
        // A delegate-creation-expression, invoked from the `New' class 
        //
@@ -778,9 +824,8 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       if (Arguments == null || Arguments.Count != 1) {
-                               Report.Error (149, loc,
-                                             "Method name expected");
+                       if (Arguments == null) {
+                               Invocation.Error_WrongNumArguments (loc, GetSignatureForError (), 0);
                                return null;
                        }
 
@@ -788,22 +833,27 @@ namespace Mono.CSharp {
                                return null;
 
                        Argument a = (Argument) Arguments [0];
-
-                       if (!a.ResolveMethodGroup (ec, loc))
+                       
+                       if (!a.ResolveMethodGroup (ec))
                                return null;
                        
                        Expression e = a.Expr;
 
-                       if (e is AnonymousMethod && RootContext.Version != LanguageVersion.ISO_1)
-                               return ((AnonymousMethod) e).Compatible (ec, type, false);
+                       if (e is AnonymousMethodExpression && RootContext.Version != LanguageVersion.ISO_1)
+                               return ((AnonymousMethodExpression) e).Compatible (ec, type);
 
                        MethodGroupExpr mg = e as MethodGroupExpr;
-                       if (mg != null)
-                               return ResolveMethodGroupExpr (ec, mg, false);
+                       if (mg != null) {
+                               if (TypeManager.IsNullableType (mg.DeclaringType)) {
+                                       Report.Error (1728, loc, "Cannot use method `{0}' as delegate creation expression because it is member of Nullable type",
+                                               mg.GetSignatureForError ());
+                                       return null;
+                               }
 
-                       Type e_type = e.Type;
+                               return ResolveMethodGroupExpr (ec, mg);
+                       }
 
-                       if (!TypeManager.IsDelegateType (e_type)) {
+                       if (!TypeManager.IsDelegateType (e.Type)) {
                                Report.Error (149, loc, "Method name expected");
                                return null;
                        }
@@ -818,16 +868,16 @@ namespace Mono.CSharp {
                        }
 
                        // This is what MS' compiler reports. We could always choose
-                       // to be more verbose and actually give delegate-level specifics                        
-                       if (!Delegate.VerifyDelegate (ec, type, e_type, loc)) {
-                               Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " +
+                       // to be more verbose and actually give delegate-level specifics
+                       if (!Delegate.VerifyDelegate (ec, type, loc)) {
+                               Report.Error (29, loc, "Cannot implicitly convert type '" + e.Type + "' " +
                                              "to type '" + type + "'");
                                return null;
                        }
                                
                        delegate_instance_expression = e;
                        delegate_method = method_group.Methods [0];
-                       
+
                        eclass = ExprClass.Value;
                        return this;
                }
@@ -888,7 +938,7 @@ namespace Mono.CSharp {
                        if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
                                return null;
 
-                       Expression lookup = Expression.MemberLookup (ec, del_type, "Invoke", loc);
+                       Expression lookup = Expression.MemberLookup (ec.ContainerType, del_type, "Invoke", loc);
                        if (!(lookup is MethodGroupExpr)) {
                                Report.Error (-100, loc, "Internal error: could not find Invoke method!");
                                return null;
@@ -917,7 +967,8 @@ namespace Mono.CSharp {
                        // Pop the return value if there is one
                        //
                        if (method is MethodInfo){
-                               if (((MethodInfo) method).ReturnType != TypeManager.void_type)
+                               Type ret = ((MethodInfo)method).ReturnType;
+                               if (TypeManager.TypeToCoreType (ret) != TypeManager.void_type)
                                        ec.ig.Emit (OpCodes.Pop);
                        }
                }