2005-05-31 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / mcs / delegate.cs
index 7fc79d868d4fc165bac1572d6dc65dafd24180b9..1a14bd913658f378fb7e60ea954d8aed8a47ac00 100644 (file)
@@ -49,7 +49,7 @@ namespace Mono.CSharp {
                        Modifiers.PRIVATE;
 
                public Delegate (NamespaceEntry ns, TypeContainer parent, Expression type,
-                                int mod_flags, string name, Parameters param_list,
+                                int mod_flags, MemberName name, Parameters param_list,
                                 Attributes attrs, Location l)
                        : base (ns, parent, name, attrs, l)
 
@@ -79,8 +79,16 @@ namespace Mono.CSharp {
                        if (TypeBuilder != null)
                                return TypeBuilder;
 
-                       TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel) |
-                               TypeAttributes.Class | TypeAttributes.Sealed;
+                       ec = new EmitContext (this, this, Location, null, null, ModFlags, false);
+
+                       if (TypeManager.multicast_delegate_type == null && !RootContext.StdLib) {
+                               Namespace system = Namespace.LookupNamespace ("System", true);
+                               TypeExpr expr = system.Lookup (this, "MulticastDelegate", Location) as TypeExpr;
+                               TypeManager.multicast_delegate_type = expr.ResolveType (ec);
+                       }
+
+                       if (TypeManager.multicast_delegate_type == null)
+                               throw new InternalErrorException ("System.MulticastDelegate unresolved");
 
                        if (IsTopLevel) {
                                if (TypeManager.NamespaceClash (Name, Location))
@@ -89,13 +97,13 @@ namespace Mono.CSharp {
                                ModuleBuilder builder = CodeGen.Module.Builder;
 
                                TypeBuilder = builder.DefineType (
-                                       Name, attr, TypeManager.multicast_delegate_type);
+                                       Name, TypeAttr, TypeManager.multicast_delegate_type);
                        } else {
                                TypeBuilder builder = Parent.TypeBuilder;
 
                                string name = Name.Substring (1 + Name.LastIndexOf ('.'));
                                TypeBuilder = builder.DefineNestedType (
-                                       name, attr, TypeManager.multicast_delegate_type);
+                                       name, TypeAttr, TypeManager.multicast_delegate_type);
                        }
 
                        TypeManager.AddDelegateType (Name, TypeBuilder, this);
@@ -112,8 +120,9 @@ namespace Mono.CSharp {
                {
                        MethodAttributes mattr;
                        int i;
-                       EmitContext ec = new EmitContext (this, this, Location, null,
-                                                         null, ModFlags, false);
+
+                       if (ec == null)
+                               throw new InternalErrorException ("Define called before DefineType?");
 
                        // FIXME: POSSIBLY make this static, as it is always constant
                        //
@@ -133,12 +142,12 @@ namespace Mono.CSharp {
                        //
                        // HACK because System.Reflection.Emit is lame
                        //
-                       //
-                       // FIXME: POSSIBLY make these static, as they are always the same
                        Parameter [] fixed_pars = new Parameter [2];
-                       fixed_pars [0] = new Parameter (null, null, Parameter.Modifier.NONE, null);
-                       fixed_pars [1] = new Parameter (null, null, Parameter.Modifier.NONE, null);
-                       Parameters const_parameters = new Parameters (fixed_pars, null, Location);
+                       fixed_pars [0] = new Parameter (TypeManager.system_object_expr, "object",
+                                                       Parameter.Modifier.NONE, null, Location);
+                       fixed_pars [1] = new Parameter (TypeManager.system_intptr_expr, "method", 
+                                                       Parameter.Modifier.NONE, null, Location);
+                       Parameters const_parameters = new Parameters (fixed_pars, null);
                        
                        TypeManager.RegisterMethod (
                                ConstructorBuilder,
@@ -154,10 +163,7 @@ namespace Mono.CSharp {
                        // First, call the `out of band' special method for
                        // defining recursively any types we need:
                        
-                       if (!Parameters.ComputeAndDefineParameterTypes (this))
-                               return false;
-                       
-                       param_types = Parameters.GetParameterInfo (this);
+                       param_types = Parameters.GetParameterInfo (ec);
                        if (param_types == null)
                                return false;
 
@@ -178,7 +184,7 @@ namespace Mono.CSharp {
                                        return false;
                        }
                        
-                       ReturnType = ResolveTypeExpr (ReturnType, false, Location);
+                       ReturnType = ReturnType.ResolveAsTypeTerminal (ec, false);
                         if (ReturnType == null)
                             return false;
                         
@@ -197,6 +203,11 @@ namespace Mono.CSharp {
                        if (ret_type.IsPointer && !UnsafeOK (Parent))
                                return false;
 
+                       if (RootContext.StdLib && (ret_type == TypeManager.arg_iterator_type || ret_type == TypeManager.typed_reference_type)) {
+                               Method.Error1599 (Location, ret_type);
+                               return false;
+                       }
+
                        //
                        // We don't have to check any others because they are all
                        // guaranteed to be accessible - they are standard types.
@@ -223,7 +234,7 @@ namespace Mono.CSharp {
                                
                                for (; i < top; i++) {
                                        p = Parameters.FixedParameters [i];
-                                       p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
+                                       p.DefineParameter (ec, InvokeBuilder, null, i + 1);
 
                                        if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
                                                out_params++;
@@ -231,13 +242,30 @@ namespace Mono.CSharp {
                        }
                        if (Parameters.ArrayParameter != null){
                                Parameter p = Parameters.ArrayParameter;
-                               p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
+
+                               if (TypeManager.param_array_type == null && !RootContext.StdLib) {
+                                       Namespace system = Namespace.LookupNamespace ("System", true);
+                                       TypeExpr expr = system.Lookup (this, "ParamArrayAttribute", Location) as TypeExpr;
+                                       TypeManager.param_array_type = expr.ResolveType (ec);
+                               }
+
+                               if (TypeManager.cons_param_array_attribute == null) {
+                                       Type [] void_arg = { };
+                                       TypeManager.cons_param_array_attribute = TypeManager.GetConstructor (
+                                               TypeManager.param_array_type, void_arg);
+                               }
+
+                               ParameterBuilder pb = InvokeBuilder.DefineParameter (
+                                       i + 1, Parameters.ArrayParameter.Attributes,Parameters.ArrayParameter.Name);
+                               
+                               pb.SetCustomAttribute (
+                                       new CustomAttributeBuilder (TypeManager.cons_param_array_attribute, new object [0]));
                        }
                        
                        InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
 
                        TypeManager.RegisterMethod (InvokeBuilder,
-                                                   new InternalParameters (Parent, Parameters),
+                                                   new InternalParameters (param_types, Parameters),
                                                    param_types);
 
                        //
@@ -268,12 +296,12 @@ namespace Mono.CSharp {
                                for (i = 0 ; i < top; i++) {
                                        p = Parameters.FixedParameters [i];
 
-                                       p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
+                                       p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1);
                                }
                        }
                        if (Parameters.ArrayParameter != null){
                                Parameter p = Parameters.ArrayParameter;
-                               p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
+                               p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1);
 
                                i++;
                        }
@@ -294,17 +322,15 @@ namespace Mono.CSharp {
                        
                        async_params [params_num] = new Parameter (
                                TypeManager.system_asynccallback_expr, "callback",
-                                                                  Parameter.Modifier.NONE, null);
+                                                                  Parameter.Modifier.NONE, null, Location);
                        async_params [params_num + 1] = new Parameter (
                                TypeManager.system_object_expr, "object",
-                                                                  Parameter.Modifier.NONE, null);
+                                                                  Parameter.Modifier.NONE, null, Location);
+
+                       Parameters async_parameters = new Parameters (async_params, null);
 
-                       Parameters async_parameters = new Parameters (async_params, null, Location);
-                       async_parameters.ComputeAndDefineParameterTypes (this);
-                       
-                       async_parameters.ComputeAndDefineParameterTypes (this);
                        TypeManager.RegisterMethod (BeginInvokeBuilder,
-                                                   new InternalParameters (Parent, async_parameters),
+                                                   new InternalParameters (async_parameters.GetParameterInfo (ec), async_parameters),
                                                    async_param_types);
 
                        //
@@ -328,7 +354,7 @@ namespace Mono.CSharp {
                                }
                        }
                        end_param_types [out_params] = TypeManager.iasyncresult_type;
-                       end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null);
+                       end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null, Location);
 
                        //
                        // Create method, define parameters, register parameters with type system
@@ -344,12 +370,11 @@ namespace Mono.CSharp {
                                EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
                        }
 
-                       Parameters end_parameters = new Parameters (end_params, null, Location);
-                       end_parameters.ComputeAndDefineParameterTypes (this);
+                       Parameters end_parameters = new Parameters (end_params, null);
 
                        TypeManager.RegisterMethod (
                                EndInvokeBuilder,
-                               new InternalParameters (Parent, end_parameters),
+                               new InternalParameters (end_parameters.GetParameterInfo (ec), end_parameters),
                                end_param_types);
 
                        return true;
@@ -358,15 +383,21 @@ namespace Mono.CSharp {
                public override void Emit ()
                {
                        if (OptAttributes != null) {
-                               EmitContext ec = new EmitContext (
-                                       Parent, this, Location, null, null, ModFlags, false);
-                               Parameters.LabelParameters (ec, InvokeBuilder, Location);
+                               Parameters.LabelParameters (ec, InvokeBuilder);
                                OptAttributes.Emit (ec, this);
                        }
 
                        base.Emit ();
                }
 
+               protected override TypeAttributes TypeAttr {
+                       get {
+                               return Modifiers.TypeAttr (ModFlags, IsTopLevel) |
+                                       TypeAttributes.Class | TypeAttributes.Sealed |
+                                       base.TypeAttr;
+                       }
+               }
+
                public override string[] ValidAttributeTargets {
                        get {
                                return attribute_targets;
@@ -383,11 +414,28 @@ namespace Mono.CSharp {
                        AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
 
                        if (!AttributeTester.IsClsCompliant (ReturnType.Type)) {
-                               Report.Error_T (3002, Location, GetSignatureForError ());
+                               Report.Error (3002, Location, "Return type of '{0}' is not CLS-compliant", GetSignatureForError ());
                        }
                        return true;
                }
 
+               //
+               // 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)
+               {
+                       Expression ml = Expression.MemberLookup (
+                               ec, delegate_type, "Invoke", loc);
+
+                       if (!(ml is MethodGroupExpr)) {
+                               Report.Error (-100, loc, "Internal error: could not find Invoke method!");
+                               return null;
+                       }
+
+                       return (MethodInfo) (((MethodGroupExpr) ml).Methods [0]);
+               }
+               
                /// <summary>
                ///  Verifies whether the method in question is compatible with the delegate
                ///  Returns the method itself if okay and null if not.
@@ -395,21 +443,15 @@ namespace Mono.CSharp {
                public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb,
                                                       Location loc)
                {
-                       ParameterData pd = Invocation.GetParameterData (mb);
+                       ParameterData pd = TypeManager.GetParameterData (mb);
 
                        int pd_count = pd.Count;
 
-                       Expression ml = Expression.MemberLookup (
-                               ec, delegate_type, "Invoke", loc);
-
-                       if (!(ml is MethodGroupExpr)) {
-                               Report.Error (-100, loc, "Internal error: could not find Invoke method!");
+                       MethodBase invoke_mb = GetInvokeMethod (ec, delegate_type, loc);
+                       if (invoke_mb == null)
                                return null;
-                       }
-
-                       MethodBase invoke_mb = ((MethodGroupExpr) ml).Methods [0];
 
-                       ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
+                       ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
 
                        if (invoke_pd.Count != pd_count)
                                return null;
@@ -417,28 +459,48 @@ namespace Mono.CSharp {
                        for (int i = pd_count; i > 0; ) {
                                i--;
 
-                               if (invoke_pd.ParameterType (i) == pd.ParameterType (i) &&
-                                   invoke_pd.ParameterModifier (i) == pd.ParameterModifier (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);
+
+                               if (invoke_pd_type == pd_type &&
+                                   invoke_pd_type_mod == pd_type_mod)
                                        continue;
-                               else {
-                                       return null;
-                               }
+                               
+                               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 (((MethodInfo) invoke_mb).ReturnType == ((MethodInfo) mb).ReturnType)
+                       Type invoke_mb_retval = ((MethodInfo) invoke_mb).ReturnType;
+                       Type mb_retval = ((MethodInfo) mb).ReturnType;
+                       if (invoke_mb_retval == mb_retval)
                                return mb;
-                       else
-                               return null;
+                       
+                       if (mb_retval.IsSubclassOf (invoke_mb_retval))
+                               if (RootContext.Version == LanguageVersion.ISO_1) {
+                                       Report.FeatureIsNotStandardized (loc, "covariance");
+                                       return null;
+                               }
+                               else
+                                       return mb;
+                       
+                       return null;
                }
 
                // <summary>
                //  Verifies whether the invocation arguments are compatible with the
                //  delegate's target method
                // </summary>
-               public static bool VerifyApplicability (EmitContext ec,
-                                                       Type delegate_type,
-                                                       ArrayList args,
-                                                       Location loc)
+               public static bool VerifyApplicability (EmitContext ec, Type delegate_type,
+                                                       ArrayList args, Location loc)
                {
                        int arg_count;
 
@@ -450,47 +512,38 @@ namespace Mono.CSharp {
                        Expression ml = Expression.MemberLookup (
                                ec, delegate_type, "Invoke", loc);
 
-                       if (!(ml is MethodGroupExpr)) {
+                       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 = ((MethodGroupExpr) ml).Methods [0];
-                       ParameterData pd = Invocation.GetParameterData (mb);
+                       MethodBase mb = me.Methods [0];
+                       ParameterData pd = TypeManager.GetParameterData (mb);
 
                        int pd_count = pd.Count;
 
                        bool params_method = (pd_count != 0) &&
                                (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS);
 
-                       if (!params_method && pd_count != arg_count) {
+                       bool is_params_applicable = false;
+                       bool is_applicable = Invocation.IsApplicable (ec, me, args, arg_count, ref mb);
+
+                       if (!is_applicable && params_method &&
+                           Invocation.IsParamsMethodApplicable (ec, me, args, arg_count, ref mb))
+                               is_applicable = is_params_applicable = true;
+
+                       if (!is_applicable && !params_method && arg_count != pd_count) {
                                Report.Error (1593, loc,
-                                             "Delegate '" + delegate_type.ToString ()
-                                             + "' does not take '" + arg_count + "' arguments");
+                                             "Delegate '{0}' does not take {1} arguments",
+                                             delegate_type.ToString (), arg_count);
                                return false;
                        }
 
-                       //
-                       // Consider the case:
-                       //   delegate void FOO(param object[] args);
-                       //   FOO f = new FOO(...);
-                       //   f(new object[] {1, 2, 3});
-                       //
-                       // This should be treated like f(1,2,3).  This is done by ignoring the 
-                       // 'param' modifier for that invocation.  If that fails, then the
-                       // 'param' modifier is considered.
-                       //
-                       // One issue is that 'VerifyArgumentsCompat' modifies the elements of
-                       // the 'args' array.  However, the modifications appear idempotent.
-                       // Normal 'Invocation's also have the same behaviour, implicitly.
-                       //
-
-                       bool ans = false;
-                       if (arg_count == pd_count)
-                               ans = Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, false, delegate_type, loc);
-                       if (!ans && params_method)
-                               ans = Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, true,  delegate_type, loc);
-                       return ans;
+                       return Invocation.VerifyArgumentsCompat (
+                                       ec, args, arg_count, mb, 
+                                       is_params_applicable || (!is_applicable && params_method),
+                                       delegate_type, false, loc);
                }
                
                /// <summary>
@@ -508,7 +561,7 @@ namespace Mono.CSharp {
                        }
                        
                        MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
-                       ParameterData pd = Invocation.GetParameterData (mb);
+                       ParameterData pd = TypeManager.GetParameterData (mb);
 
                        Expression probe_ml = Expression.MemberLookup (
                                ec, delegate_type, "Invoke", loc);
@@ -519,7 +572,7 @@ namespace Mono.CSharp {
                        }
                        
                        MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];
-                       ParameterData probe_pd = Invocation.GetParameterData (probe_mb);
+                       ParameterData probe_pd = TypeManager.GetParameterData (probe_mb);
                        
                        if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)
                                return false;
@@ -629,6 +682,13 @@ namespace Mono.CSharp {
                        }
                }
 
+               //
+               //   Represents header string for documentation comment.
+               //
+               public override string DocCommentHeader {
+                       get { return "T:"; }
+               }
+
                protected override void VerifyObsoleteAttribute()
                {
                        CheckUsageOfObsoleteAttribute (ret_type);
@@ -646,36 +706,42 @@ namespace Mono.CSharp {
                protected MethodBase constructor_method;
                protected MethodBase delegate_method;
                protected MethodGroupExpr method_group;
+               protected Expression delegate_instance_expression;
 
                public DelegateCreation () {}
 
                public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
                {
                        string method_desc;
+                       MethodInfo found_method = (MethodInfo)mg.Methods [0];
                        
                        if (mg.Methods.Length > 1)
-                               method_desc = mg.Methods [0].Name;
+                               method_desc = found_method.Name;
                        else
-                               method_desc = Invocation.FullMethodDesc (mg.Methods [0]);
+                               method_desc = Invocation.FullMethodDesc (found_method);
 
                        Expression invoke_method = Expression.MemberLookup (
                                ec, type, "Invoke", MemberTypes.Method,
                                Expression.AllBindingFlags, loc);
-                       MethodBase method = ((MethodGroupExpr) invoke_method).Methods [0];
-                       ParameterData param = Invocation.GetParameterData (method);
+                       MethodInfo method = ((MethodGroupExpr) invoke_method).Methods [0] as MethodInfo;
+
+                       ParameterData param = TypeManager.GetParameterData (method);
                        string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
-                       
-                       Report.Error (123, loc, "Method '" + method_desc + "' does not " +
-                                     "match delegate '" + delegate_desc + "'");
+
+                       if (method.ReturnType != found_method.ReturnType) {
+                               Report.Error (407, loc, "'{0}' has the wrong return type to match delegate '{1}'", method_desc, delegate_desc);
+                       } else {
+                               Report.Error (123, loc, "Method '" + method_desc + "' does not " +
+                                       "match delegate '" + delegate_desc + "'");
+                       }
                }
                
                public override void Emit (EmitContext ec)
                {
-                       if (method_group.InstanceExpression == null ||
-                           delegate_method.IsStatic)
+                       if (delegate_instance_expression == null || delegate_method.IsStatic)
                                ec.ig.Emit (OpCodes.Ldnull);
                        else
-                               method_group.InstanceExpression.Emit (ec);
+                               delegate_instance_expression.Emit (ec);
                        
                        if (delegate_method.IsVirtual && !method_group.IsBase) {
                                ec.ig.Emit (OpCodes.Dup);
@@ -699,7 +765,8 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
+               protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg,
+                                                            bool check_only)
                {
                        foreach (MethodInfo mi in mg.Methods){
                                delegate_method  = Delegate.VerifyMethod (ec, type, mi, loc);
@@ -709,7 +776,8 @@ namespace Mono.CSharp {
                        }
                        
                        if (delegate_method == null) {
-                               Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
+                               if (!check_only)
+                                       Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
                                return null;
                        }
                        
@@ -717,7 +785,7 @@ namespace Mono.CSharp {
                        // Check safe/unsafe of the delegate
                        //
                        if (!ec.InUnsafe){
-                               ParameterData param = Invocation.GetParameterData (delegate_method);
+                               ParameterData param = TypeManager.GetParameterData (delegate_method);
                                int count = param.Count;
                                
                                for (int i = 0; i < count; i++){
@@ -732,18 +800,17 @@ namespace Mono.CSharp {
                        IMethodData md = TypeManager.GetMethod (delegate_method);
                        if (md == null) {
                                if (System.Attribute.GetCustomAttribute (delegate_method, TypeManager.conditional_attribute_type) != null) {
-                                       // Cannot create delegate with '{0}' because it has a Conditional attribute
-                                       Report.Error_T (1618, loc, TypeManager.CSharpSignature (delegate_method));
+                                       Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
                                }
                        } else {
+                               md.SetMemberIsUsed ();
                                if (md.OptAttributes != null && md.OptAttributes.Search (TypeManager.conditional_attribute_type, ec) != null) {
-                                       // Cannot create delegate with '{0}' because it has a Conditional attribute
-                                       Report.Error_T (1618, loc, TypeManager.CSharpSignature (delegate_method));
+                                       Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
                                }
                        }
                        
                        if (mg.InstanceExpression != null)
-                               mg.InstanceExpression = mg.InstanceExpression.Resolve (ec);
+                               delegate_instance_expression = mg.InstanceExpression.Resolve (ec);
                        else if (ec.IsStatic) {
                                if (!delegate_method.IsStatic) {
                                        Report.Error (120, loc,
@@ -751,12 +818,12 @@ namespace Mono.CSharp {
                                                      delegate_method.Name);
                                        return null;
                                }
-                               mg.InstanceExpression = null;
+                               delegate_instance_expression = null;
                        } else
-                               mg.InstanceExpression = ec.GetThis (loc);
+                               delegate_instance_expression = ec.GetThis (loc);
                        
-                       if (mg.InstanceExpression != null && mg.InstanceExpression.Type.IsValueType)
-                               mg.InstanceExpression = new BoxedCast (mg.InstanceExpression);
+                       if (delegate_instance_expression != null && delegate_instance_expression.Type.IsValueType)
+                               delegate_instance_expression = new BoxedCast (delegate_instance_expression);
                        
                        method_group = mg;
                        eclass = ExprClass.Value;
@@ -779,17 +846,18 @@ namespace Mono.CSharp {
                {
                        return this;
                }
-               
-               static public Expression Create (EmitContext ec, MethodGroupExpr mge, Type target_type, Location loc)
+
+               static public Expression Create (EmitContext ec, MethodGroupExpr mge,
+                                                Type target_type, bool check_only, Location loc)
                {
                        ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
                        if (d.ResolveConstructorMethod (ec))
-                               return d.ResolveMethodGroupExpr (ec, mge);
+                               return d.ResolveMethodGroupExpr (ec, mge, check_only);
                        else
                                return null;
                }
        }
-       
+
        //
        // A delegate-creation-expression, invoked from the `New' class 
        //
@@ -824,14 +892,17 @@ namespace Mono.CSharp {
                        
                        Expression e = a.Expr;
 
+                       if (e is AnonymousMethod && RootContext.Version != LanguageVersion.ISO_1)
+                               return ((AnonymousMethod) e).Compatible (ec, type, false);
+
                        MethodGroupExpr mg = e as MethodGroupExpr;
                        if (mg != null)
-                               return ResolveMethodGroupExpr (ec, mg);
+                               return ResolveMethodGroupExpr (ec, mg, false);
 
                        Type e_type = e.Type;
 
                        if (!TypeManager.IsDelegateType (e_type)) {
-                               e.Error_UnexpectedKind ("method");
+                               Report.Error (149, loc, "Method name expected");
                                return null;
                        }
 
@@ -852,7 +923,7 @@ namespace Mono.CSharp {
                                return null;
                        }
                                
-                       method_group.InstanceExpression = e;
+                       delegate_instance_expression = e;
                        delegate_method = method_group.Methods [0];
                        
                        eclass = ExprClass.Value;