2009-06-22 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / delegate.cs
index afdfe73ae65ebcfa9c674f4f0a0bbdf9481104e8..622244d1b5214bfd19f9636464c6713e74fe8d86 100644 (file)
@@ -26,7 +26,7 @@ namespace Mono.CSharp {
        public class Delegate : DeclSpace, IMemberContainer
        {
                FullNamedExpression ReturnType;
-               public Parameters      Parameters;
+               public ParametersCompiled      Parameters;
 
                public ConstructorBuilder ConstructorBuilder;
                public MethodBuilder      InvokeBuilder;
@@ -55,7 +55,7 @@ namespace Mono.CSharp {
                        Modifiers.PRIVATE;
 
                public Delegate (NamespaceEntry ns, DeclSpace parent, FullNamedExpression type,
-                                int mod_flags, MemberName name, Parameters param_list,
+                                int mod_flags, MemberName name, ParametersCompiled param_list,
                                 Attributes attrs)
                        : base (ns, parent, name, attrs)
 
@@ -67,17 +67,17 @@ namespace Mono.CSharp {
                        Parameters      = param_list;
                }
 
-               public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        if (a.Target == AttributeTargets.ReturnValue) {
                                if (return_attributes == null)
                                        return_attributes = new ReturnParameter (InvokeBuilder, Location);
 
-                               return_attributes.ApplyAttributeBuilder (a, cb);
+                               return_attributes.ApplyAttributeBuilder (a, cb, pa);
                                return;
                        }
 
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
 
                public override TypeBuilder DefineType ()
@@ -89,7 +89,7 @@ namespace Mono.CSharp {
                                if (TypeManager.NamespaceClash (Name, Location))
                                        return null;
                                
-                               ModuleBuilder builder = CodeGen.Module.Builder;
+                               ModuleBuilder builder = Module.Builder;
 
                                TypeBuilder = builder.DefineType (
                                        Name, TypeAttr, TypeManager.multicast_delegate_type);
@@ -136,7 +136,6 @@ namespace Mono.CSharp {
 
                public override bool Define ()
                {
-#if GMCS_SOURCE
                        if (IsGeneric) {
                                foreach (TypeParameter type_param in TypeParameters) {
                                        if (!type_param.Resolve (this))
@@ -147,13 +146,8 @@ namespace Mono.CSharp {
                                        if (!type_param.DefineType (this))
                                                return false;
                                }
-
-                               foreach (TypeParameter type_param in TypeParameters) {
-                                       if (!type_param.CheckDependencies ())
-                                               return false;
-                               }
                        }
-#endif
+
                        member_cache = new MemberCache (TypeManager.multicast_delegate_type, this);
 
                        // FIXME: POSSIBLY make this static, as it is always constant
@@ -174,13 +168,14 @@ namespace Mono.CSharp {
                        //
                        // HACK because System.Reflection.Emit is lame
                        //
-                       Parameter [] fixed_pars = new Parameter [2];
-                       fixed_pars [0] = new Parameter (TypeManager.object_type, "object",
-                                                       Parameter.Modifier.NONE, null, Location);
-                       fixed_pars [1] = new Parameter (TypeManager.intptr_type, "method", 
-                                                       Parameter.Modifier.NONE, null, Location);
-                       Parameters const_parameters = new Parameters (fixed_pars);
-                       const_parameters.Resolve (null);
+                       IParameterData [] fixed_pars = new IParameterData [] {
+                               new ParameterData ("object", Parameter.Modifier.NONE),
+                               new ParameterData ("method", Parameter.Modifier.NONE)
+                       };
+
+                       AParametersCollection const_parameters = new ParametersImported (
+                               fixed_pars,
+                               new Type[] { TypeManager.object_type, TypeManager.intptr_type });
                        
                        TypeManager.RegisterMethod (ConstructorBuilder, const_parameters);
                        member_cache.AddMember (ConstructorBuilder, this);
@@ -205,9 +200,9 @@ namespace Mono.CSharp {
                                if (!IsAccessibleAs (partype)) {
                                        Report.SymbolRelatedToPreviousError (partype);
                                        Report.Error (59, Location,
-                                                     "Inconsistent accessibility: parameter type `" +
-                                                     TypeManager.CSharpName (partype) + "' is less " +
-                                                         "accessible than delegate `" + GetSignatureForError () + "'");
+                                                     "Inconsistent accessibility: parameter type `{0}' is less accessible than delegate `{1}'",
+                                                     TypeManager.CSharpName (partype),
+                                                     GetSignatureForError ());
                                        return false;
                                }
                        }
@@ -229,11 +224,13 @@ namespace Mono.CSharp {
 
                        CheckProtectedModifier ();
 
-                       if (RootContext.StdLib && (ret_type == TypeManager.arg_iterator_type || ret_type == TypeManager.typed_reference_type)) {
+                       if (RootContext.StdLib && TypeManager.IsSpecialType (ret_type)) {
                                Method.Error1599 (Location, ret_type);
                                return false;
                        }
 
+                       TypeManager.CheckTypeVariance (ret_type, Variance.Covariant, this);
+
                        //
                        // We don't have to check any others because they are all
                        // guaranteed to be accessible - they are standard types.
@@ -245,7 +242,7 @@ namespace Mono.CSharp {
                                                                  mattr,                     
                                                                  cc,
                                                                  ret_type,                  
-                                                                 Parameters.Types);
+                                                                 Parameters.GetEmitTypes ());
                        
                        InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
 
@@ -264,12 +261,19 @@ namespace Mono.CSharp {
                        //
                        // BeginInvoke
                        //
-                       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));
+                       ParametersCompiled async_parameters = ParametersCompiled.MergeGenerated (Parameters, false,
+                               new Parameter [] {
+                                       new Parameter (null, "callback", Parameter.Modifier.NONE, null, Location),
+                                       new Parameter (null, "object", Parameter.Modifier.NONE, null, Location)
+                               },
+                               new Type [] {
+                                       TypeManager.asynccallback_type,
+                                       TypeManager.object_type
+                               }
+                       );
 
                        BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
-                               mattr, cc, TypeManager.iasyncresult_type, async_parameters.Types);
+                               mattr, cc, TypeManager.iasyncresult_type, async_parameters.GetEmitTypes ());
 
                        BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
                        TypeManager.RegisterMethod (BeginInvokeBuilder, async_parameters);
@@ -283,7 +287,7 @@ namespace Mono.CSharp {
                        //
                        // Define parameters, and count out/ref parameters
                        //
-                       Parameters end_parameters;
+                       ParametersCompiled end_parameters;
                        int out_params = 0;
 
                        foreach (Parameter p in Parameters.FixedParameters) {
@@ -293,30 +297,30 @@ namespace Mono.CSharp {
 
                        if (out_params > 0) {
                                Type [] end_param_types = new Type [out_params];
-                               Parameter [] end_params = new Parameter [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];
+                                       Parameter p = Parameters [i];
                                        if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
                                                continue;
 
-                                       end_param_types [param] = p.ExternalType ();
+                                       end_param_types [param] = Parameters.Types [i];
                                        end_params [param] = p;
                                        ++param;
                                }
-                               end_parameters = Parameters.CreateFullyResolved (end_params, end_param_types);
+                               end_parameters = ParametersCompiled.CreateFullyResolved (end_params, end_param_types);
                        } else {
-                               end_parameters = Parameters.EmptyReadOnlyParameters;
+                               end_parameters = ParametersCompiled.EmptyReadOnlyParameters;
                        }
 
-                       end_parameters = Parameters.MergeGenerated (end_parameters,
-                               new Parameter (TypeManager.iasyncresult_type, "result", Parameter.Modifier.NONE, null, Location));
+                       end_parameters = ParametersCompiled.MergeGenerated (end_parameters, false,
+                               new Parameter (null, "result", Parameter.Modifier.NONE, null, Location), TypeManager.iasyncresult_type);
 
                        //
                        // Create method, define parameters, register parameters with type system
                        //
-                       EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_parameters.Types);
+                       EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_parameters.GetEmitTypes ());
                        EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
 
                        end_parameters.ApplyAttributes (EndInvokeBuilder);
@@ -329,7 +333,7 @@ namespace Mono.CSharp {
                        Parameters.ApplyAttributes (InvokeBuilder);
 
                        if (BeginInvokeBuilder != null) {
-                               Parameters p = (Parameters) TypeManager.GetParameterData (BeginInvokeBuilder);
+                               ParametersCompiled p = (ParametersCompiled) TypeManager.GetParameterData (BeginInvokeBuilder);
                                p.ApplyAttributes (BeginInvokeBuilder);
                        }
 
@@ -364,7 +368,8 @@ namespace Mono.CSharp {
                        Parameters.VerifyClsCompliance ();
 
                        if (!AttributeTester.IsClsCompliant (ReturnType.Type)) {
-                               Report.Error (3002, Location, "Return type of `{0}' is not CLS-compliant", GetSignatureForError ());
+                               Report.Warning (3002, 1, Location, "Return type of `{0}' is not CLS-compliant",
+                                       GetSignatureForError ());
                        }
                        return true;
                }
@@ -373,13 +378,11 @@ namespace Mono.CSharp {
                public static ConstructorInfo GetConstructor (Type container_type, Type delegate_type)
                {
                        Type dt = delegate_type;
-#if GMCS_SOURCE
                        Type[] g_args = null;
-                       if (delegate_type.IsGenericType) {
-                               g_args = delegate_type.GetGenericArguments ();
-                               delegate_type = delegate_type.GetGenericTypeDefinition ();
+                       if (TypeManager.IsGenericType (delegate_type)) {
+                               g_args = TypeManager.GetTypeArguments (delegate_type);
+                               delegate_type = TypeManager.DropGenericTypeArguments (delegate_type);
                        }
-#endif
 
                        Delegate d = TypeManager.LookupDelegate (delegate_type);
                        if (d != null) {
@@ -411,20 +414,21 @@ namespace Mono.CSharp {
                public static MethodInfo GetInvokeMethod (Type container_type, Type delegate_type)
                {
                        Type dt = delegate_type;
-#if GMCS_SOURCE
+
                        Type[] g_args = null;
-                       if (delegate_type.IsGenericType) {
-                               g_args = delegate_type.GetGenericArguments ();
-                               delegate_type = delegate_type.GetGenericTypeDefinition ();
+                       if (TypeManager.IsGenericType (delegate_type)) {
+                               g_args = TypeManager.GetTypeArguments (delegate_type);
+                               delegate_type = TypeManager.DropGenericTypeArguments (delegate_type);
                        }
-#endif
+
                        Delegate d = TypeManager.LookupDelegate (delegate_type);
+                       MethodInfo invoke;
                        if (d != null) {
 #if GMCS_SOURCE
                                if (g_args != null) {
-                                       MethodInfo invoke = TypeBuilder.GetMethod (dt, d.InvokeBuilder);
+                                       invoke = TypeBuilder.GetMethod (dt, d.InvokeBuilder);
 #if MS_COMPATIBLE
-                                       Parameters p = (Parameters) d.Parameters.InflateTypes (g_args, g_args);
+                                       ParametersCompiled p = (ParametersCompiled) d.Parameters.InflateTypes (g_args, g_args);
                                        TypeManager.RegisterMethod (invoke, p);
 #endif
                                        return invoke;
@@ -443,23 +447,36 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       return (MethodInfo) mg.Methods[0];
+                       invoke = (MethodInfo) mg.Methods[0];
+#if MS_COMPATIBLE
+                       if (g_args != null) {
+                               AParametersCollection p = TypeManager.GetParameterData (invoke);
+                               p = p.InflateTypes (g_args, g_args);
+                               TypeManager.RegisterMethod (invoke, p);
+                               return invoke;
+                       }
+#endif
+
+                       return invoke;
                }
 
+               //
+               // 15.2 Delegate compatibility
+               //
                public static bool IsTypeCovariant (Expression a, Type b)
                {
-                       Type a_type = a.Type;
-                       if (a_type == b)
+                       //
+                       // For each value parameter (a parameter with no ref or out modifier), an 
+                       // identity conversion or implicit reference conversion exists from the
+                       // parameter type in D to the corresponding parameter type in M
+                       //
+                       if (a.Type == b)
                                return true;
 
                        if (RootContext.Version == LanguageVersion.ISO_1)
                                return false;
 
-                       if (a.Type.IsValueType)
-                               return false;
-
-                       return Convert.ImplicitReferenceConversionCore (
-                               a, b);
+                       return Convert.ImplicitReferenceConversionExists (a, b);
                }
                
                /// <summary>
@@ -467,22 +484,25 @@ namespace Mono.CSharp {
                ///  Returns the method itself if okay and null if not.
                /// </summary>
                public static MethodBase VerifyMethod (Type container_type, Type delegate_type,
-                                                      MethodGroupExpr old_mg, MethodBase mb,
-                                                      Location loc)
+                                                      MethodGroupExpr old_mg, MethodBase mb)
                {
+                       bool is_method_definition = TypeManager.IsGenericMethodDefinition (mb);
+                       
                        MethodInfo invoke_mb = GetInvokeMethod (container_type, delegate_type);
                        if (invoke_mb == null)
                                return null;
+                               
+                       if (is_method_definition)
+                               invoke_mb = (MethodInfo) TypeManager.DropGenericMethodArguments (invoke_mb);
 
-                       ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
+                       AParametersCollection invoke_pd = TypeManager.GetParameterData (invoke_mb);
 
 #if GMCS_SOURCE
-                       if (old_mg.type_arguments == null &&
+                       if (!is_method_definition && old_mg.type_arguments == null &&
                            !TypeManager.InferTypeArguments (invoke_pd, ref mb))
                                return null;
 #endif
-
-                       ParameterData pd = TypeManager.GetParameterData (mb);
+                       AParametersCollection pd = TypeManager.GetParameterData (mb);
 
                        if (invoke_pd.Count != pd.Count)
                                return null;
@@ -490,10 +510,10 @@ namespace Mono.CSharp {
                        for (int i = pd.Count; i > 0; ) {
                                i--;
 
-                               Type invoke_pd_type = invoke_pd.ParameterType (i);
-                               Type pd_type = pd.ParameterType (i);
-                               Parameter.Modifier invoke_pd_type_mod = invoke_pd.ParameterModifier (i);
-                               Parameter.Modifier pd_type_mod = pd.ParameterModifier (i);
+                               Type invoke_pd_type = invoke_pd.Types [i];
+                               Type pd_type = pd.Types [i];
+                               Parameter.Modifier invoke_pd_type_mod = invoke_pd.FixedParameters [i].ModFlags;
+                               Parameter.Modifier pd_type_mod = pd.FixedParameters [i].ModFlags;
 
                                invoke_pd_type_mod &= ~Parameter.Modifier.PARAMS;
                                pd_type_mod &= ~Parameter.Modifier.PARAMS;
@@ -501,14 +521,13 @@ namespace Mono.CSharp {
                                if (invoke_pd_type_mod != pd_type_mod)
                                        return null;
 
-                               if (invoke_pd_type == pd_type)
+                               if (TypeManager.IsEqual (invoke_pd_type, pd_type))
                                        continue;
 
-                               //if (!IsTypeCovariant (invoke_pd_type, pd_type))
-                               //      return null;
+                               if (IsTypeCovariant (new EmptyExpression (invoke_pd_type), pd_type))
+                                       continue;
 
-                               if (RootContext.Version == LanguageVersion.ISO_1)
-                                       return null;
+                               return null;
                        }
 
                        Type invoke_mb_retval = ((MethodInfo) invoke_mb).ReturnType;
@@ -530,7 +549,7 @@ namespace Mono.CSharp {
                //  delegate's target method
                // </summary>
                public static bool VerifyApplicability (EmitContext ec, Type delegate_type,
-                                                       ArrayList args, Location loc)
+                                                       ref ArrayList args, Location loc)
                {
                        int arg_count;
 
@@ -539,24 +558,18 @@ namespace Mono.CSharp {
                        else
                                arg_count = args.Count;
 
-                       Expression ml = Expression.MemberLookup (
-                               ec.ContainerType, delegate_type, "Invoke", loc);
-
-                       MethodGroupExpr me = ml as MethodGroupExpr;
-                       if (me == null) {
-                               Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
-                               return false;
-                       }
-                       
                        MethodBase mb = GetInvokeMethod (ec.ContainerType, delegate_type);
-                       ParameterData pd = TypeManager.GetParameterData (mb);
+                       MethodGroupExpr me = new MethodGroupExpr (new MemberInfo [] { mb }, delegate_type, loc);
+                       
+                       AParametersCollection pd = TypeManager.GetParameterData (mb);
 
                        int pd_count = pd.Count;
 
                        bool params_method = pd.HasParams;
                        bool is_params_applicable = false;
-                       me.DelegateType = delegate_type;
-                       bool is_applicable = me.IsApplicable (ec, args, arg_count, ref mb, ref is_params_applicable) == 0;
+                       bool is_applicable = me.IsApplicable (ec, ref args, arg_count, ref mb, ref is_params_applicable) == 0;
+                       if (args != null)
+                               arg_count = args.Count;
 
                        if (!is_applicable && !params_method && arg_count != pd_count) {
                                Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
@@ -660,28 +673,28 @@ namespace Mono.CSharp {
                protected MethodGroupExpr method_group;
                protected Expression delegate_instance_expression;
 
-               ArrayList CreateDelegateMethodArguments (MethodInfo invoke_method)
+               public static ArrayList CreateDelegateMethodArguments (MethodInfo invoke_method, Location loc)
                {
-                       ParameterData pd = TypeManager.GetParameterData (invoke_method);
+                       AParametersCollection pd = TypeManager.GetParameterData (invoke_method);
                        ArrayList delegate_arguments = new ArrayList (pd.Count);
                        for (int i = 0; i < pd.Count; ++i) {
                                Argument.AType atype_modifier;
                                Type atype = pd.Types [i];
-                               switch (pd.ParameterModifier (i)) {
-                                       case Parameter.Modifier.REF:
-                                               atype_modifier = Argument.AType.Ref;
-                                               atype = atype.GetElementType ();
-                                               break;
-                                       case Parameter.Modifier.OUT:
-                                               atype_modifier = Argument.AType.Out;
-                                               atype = atype.GetElementType ();
-                                               break;
-                                       case Parameter.Modifier.ARGLIST:
-                                               // __arglist is not valid
-                                               throw new InternalErrorException ("__arglist modifier");
-                                       default:
-                                               atype_modifier = Argument.AType.Expression;
-                                               break;
+                               switch (pd.FixedParameters [i].ModFlags) {
+                               case Parameter.Modifier.REF:
+                                       atype_modifier = Argument.AType.Ref;
+                                       //atype = atype.GetElementType ();
+                                       break;
+                               case Parameter.Modifier.OUT:
+                                       atype_modifier = Argument.AType.Out;
+                                       //atype = atype.GetElementType ();
+                                       break;
+                               case Parameter.Modifier.ARGLIST:
+                                       // __arglist is not valid
+                                       throw new InternalErrorException ("__arglist modifier");
+                               default:
+                                       atype_modifier = Argument.AType.Expression;
+                                       break;
                                }
                                delegate_arguments.Add (new Argument (new TypeExpression (atype, loc), atype_modifier));
                        }
@@ -715,12 +728,21 @@ namespace Mono.CSharp {
                        method_group.DelegateType = type;
                        method_group.CustomErrorHandler = this;
 
-                       ArrayList arguments = CreateDelegateMethodArguments (invoke_method);
+                       ArrayList arguments = CreateDelegateMethodArguments (invoke_method, loc);
                        method_group = method_group.OverloadResolve (ec, ref arguments, false, loc);
                        if (method_group == null)
                                return null;
 
                        delegate_method = (MethodInfo) method_group;
+                       
+                       if (TypeManager.IsNullableType (delegate_method.DeclaringType)) {
+                               Report.Error (1728, loc, "Cannot create delegate from method `{0}' because it is a member of System.Nullable<T> type",
+                                       TypeManager.GetFullNameSignature (delegate_method));
+                               return null;
+                       }               
+                       
+                       Invocation.IsSpecialMethodInvocation (delegate_method, loc);
+
                        ExtensionMethodGroupExpr emg = method_group as ExtensionMethodGroupExpr;
                        if (emg != null) {
                                delegate_instance_expression = emg.ExtensionExpression;
@@ -737,6 +759,18 @@ namespace Mono.CSharp {
                                Error_ConversionFailed (ec, delegate_method, ret_expr);
                        }
 
+                       if (Invocation.IsMethodExcluded (delegate_method, loc)) {
+                               Report.SymbolRelatedToPreviousError (delegate_method);
+                               MethodOrOperator m = TypeManager.GetMethod (delegate_method) as MethodOrOperator;
+                               if (m != null && m.IsPartialDefinition) {
+                                       Report.Error (762, loc, "Cannot create delegate from partial method declaration `{0}'",
+                                               TypeManager.CSharpSignature (delegate_method));
+                               } else {
+                                       Report.Error (1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute",
+                                               TypeManager.CSharpSignature (delegate_method));
+                               }
+                       }
+
                        DoResolveInstanceExpression (ec);
                        eclass = ExprClass.Value;
                        return this;
@@ -749,15 +783,18 @@ namespace Mono.CSharp {
                        //
                        if (delegate_instance_expression != null)
                                return;
-                       
-                       if (method_group.InstanceExpression != null)
-                               delegate_instance_expression = method_group.InstanceExpression;
-                       else if (!delegate_method.IsStatic && !ec.IsStatic)
-                               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);
+                       Expression instance = method_group.InstanceExpression;
+                       if (instance != null && instance != EmptyExpression.Null) {
+                               delegate_instance_expression = instance;
+                               Type instance_type = delegate_instance_expression.Type;
+                               if (TypeManager.IsValueType (instance_type) || TypeManager.IsGenericParameter (instance_type)) {
+                                       delegate_instance_expression = new BoxedCast (
+                                               delegate_instance_expression, TypeManager.object_type);
+                               }
+                       } else if (!delegate_method.IsStatic && !ec.IsStatic) {
+                               delegate_instance_expression = ec.GetThis (loc);
+                       }
                }
                
                public override void Emit (EmitContext ec)
@@ -769,9 +806,11 @@ namespace Mono.CSharp {
 
                        if (!delegate_method.DeclaringType.IsSealed && delegate_method.IsVirtual && !method_group.IsBase) {
                                ec.ig.Emit (OpCodes.Dup);
-                               ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
-                       } else
-                               ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
+                               ec.ig.Emit (OpCodes.Ldvirtftn, delegate_method);
+                       } else {
+                               ec.ig.Emit (OpCodes.Ldftn, delegate_method);
+                       }
+
                        ec.ig.Emit (OpCodes.Newobj, constructor_method);
                }
 
@@ -807,13 +846,22 @@ namespace Mono.CSharp {
                                return null;
 
                        foreach (MethodInfo mi in mg.Methods){
-                               MethodBase mb = Delegate.VerifyMethod (mg.DeclaringType, target_type, mg, mi, Location.Null);
+                               MethodBase mb = Delegate.VerifyMethod (mg.DeclaringType, target_type, mg, mi);
                                if (mb != null)
                                        return mb;
                        }
                        return null;
                }
 
+               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+               {
+                       if (delegate_instance_expression != null)
+                               delegate_instance_expression.MutateHoistedGenericType (storey);
+
+                       delegate_method = storey.MutateGenericMethod (delegate_method);
+                       constructor_method = storey.MutateConstructor (constructor_method);
+               }
+
                #region IErrorHandler Members
 
                public bool NoExactMatch (EmitContext ec, MethodBase method)
@@ -825,6 +873,11 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               public bool AmbiguousCall (MethodBase ambiguous)
+               {
+                       return false;
+               }
+
                #endregion
        }
 
@@ -867,7 +920,7 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       if (Arguments == null) {
+                       if (Arguments == null || Arguments.Count != 1) {
                                Error_InvalidDelegateArgument ();
                                return null;
                        }
@@ -875,19 +928,22 @@ namespace Mono.CSharp {
                        Argument a = (Argument) Arguments [0];
                        if (!a.ResolveMethodGroup (ec))
                                return null;
-                       
+
                        Expression e = a.Expr;
-                       if (e is AnonymousMethodExpression && RootContext.Version != LanguageVersion.ISO_1) {
-                               e = ((AnonymousMethodExpression) e).Compatible (ec, type);
+
+                       AnonymousMethodExpression ame = e as AnonymousMethodExpression;
+                       if (ame != null && RootContext.Version != LanguageVersion.ISO_1) {
+                               e = ame.Compatible (ec, type);
                                if (e == null)
                                        return null;
+
                                return e.Resolve (ec);
                        }
 
                        method_group = e as MethodGroupExpr;
                        if (method_group == null) {
                                if (!TypeManager.IsDelegateType (e.Type)) {
-                                       Report.Error (149, loc, "Method name expected");
+                                       e.Error_UnexpectedKind (ResolveFlags.MethodGroup | ResolveFlags.Type, loc);
                                        return null;
                                }
 
@@ -899,21 +955,7 @@ namespace Mono.CSharp {
                                        Delegate.GetInvokeMethod (ec.ContainerType, e.Type) }, e.Type, loc);
                        }
 
-                       if (base.DoResolve (ec) == null)
-                               return null;
-
-                       if (TypeManager.IsNullableType (method_group.DeclaringType)) {
-                               Report.Error (1728, loc, "Cannot use method `{0}' as delegate creation expression because it is member of Nullable type",
-                                       TypeManager.GetFullNameSignature (delegate_method));
-                       }
-
-                       if (Invocation.IsMethodExcluded (delegate_method)) {
-                               Report.SymbolRelatedToPreviousError (delegate_method);
-                               Report.Error (1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute",
-                                       TypeManager.CSharpSignature (delegate_method));
-                       }
-
-                       return this;
+                       return base.DoResolve (ec);
                }
 
                void Error_InvalidDelegateArgument ()
@@ -925,9 +967,8 @@ namespace Mono.CSharp {
        public class DelegateInvocation : ExpressionStatement {
 
                readonly Expression InstanceExpr;
-               readonly ArrayList  Arguments;
-
-               MethodBase method;
+               ArrayList  Arguments;
+               MethodInfo method;
                
                public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
                {
@@ -935,6 +976,23 @@ namespace Mono.CSharp {
                        this.Arguments = args;
                        this.loc = loc;
                }
+               
+               public override Expression CreateExpressionTree (EmitContext ec)
+               {
+                       ArrayList args;
+                       if (Arguments == null)
+                               args = new ArrayList (1);
+                       else
+                               args = new ArrayList (Arguments.Count + 1);
+
+                       args.Add (new Argument (InstanceExpr.CreateExpressionTree (ec)));
+                       if (Arguments != null) {
+                               foreach (Argument a in Arguments)
+                                       args.Add (new Argument (a.Expr.CreateExpressionTree (ec)));
+                       }
+
+                       return CreateExpressionFactoryCall ("Invoke", args);
+               }
 
                public override Expression DoResolve (EmitContext ec)
                {
@@ -954,12 +1012,11 @@ namespace Mono.CSharp {
                                }
                        }
                        
-                       if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
+                       if (!Delegate.VerifyApplicability (ec, del_type, ref Arguments, loc))
                                return null;
 
                        method = Delegate.GetInvokeMethod (ec.ContainerType, del_type);
-                       type = ((MethodInfo) method).ReturnType;
-                       type = TypeManager.TypeToCoreType (type);
+                       type = TypeManager.TypeToCoreType (method.ReturnType);
                        eclass = ExprClass.Value;
                        
                        return this;
@@ -980,12 +1037,22 @@ namespace Mono.CSharp {
                        // 
                        // Pop the return value if there is one
                        //
-                       if (method is MethodInfo){
-                               Type ret = ((MethodInfo)method).ReturnType;
-                               if (TypeManager.TypeToCoreType (ret) != TypeManager.void_type)
-                                       ec.ig.Emit (OpCodes.Pop);
-                       }
+                       if (type != TypeManager.void_type)
+                               ec.ig.Emit (OpCodes.Pop);
                }
 
+               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+               {
+                       method = storey.MutateGenericMethod (method);
+                       type = storey.MutateType (type);
+
+                       if (Arguments != null) {
+                               foreach (Argument a in Arguments) {
+                                       a.Expr.MutateHoistedGenericType (storey);
+                               }
+                       }
+
+                       InstanceExpr.MutateHoistedGenericType (storey);
+               }
        }
 }