2010-07-09 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / mcs / delegate.cs
1 //
2 // delegate.cs: Delegate Handler
3 //
4 // Authors:
5 //     Ravi Pratap (ravi@ximian.com)
6 //     Miguel de Icaza (miguel@ximian.com)
7 //     Marek Safar (marek.safar@gmail.com)
8 //
9 // Dual licensed under the terms of the MIT X11 or GNU GPL
10 //
11 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
12 // Copyright 2003-2009 Novell, Inc (http://www.novell.com)
13 //
14
15 using System;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Collections.Generic;
19
20 namespace Mono.CSharp {
21
22         //
23         // Delegate container implementation
24         //
25         public class Delegate : TypeContainer
26         {
27                 FullNamedExpression ReturnType;
28                 public readonly ParametersCompiled Parameters;
29
30                 Constructor Constructor;
31                 Method InvokeBuilder;
32                 Method BeginInvokeBuilder;
33                 Method EndInvokeBuilder;
34
35                 static readonly string[] attribute_targets = new string [] { "type", "return" };
36
37                 public static readonly string InvokeMethodName = "Invoke";
38                 
39                 Expression instance_expr;
40                 ReturnParameter return_attributes;
41
42                 const Modifiers MethodModifiers = Modifiers.PUBLIC | Modifiers.VIRTUAL;
43
44                 const Modifiers AllowedModifiers =
45                         Modifiers.NEW |
46                         Modifiers.PUBLIC |
47                         Modifiers.PROTECTED |
48                         Modifiers.INTERNAL |
49                         Modifiers.UNSAFE |
50                         Modifiers.PRIVATE;
51
52                 public Delegate (NamespaceEntry ns, DeclSpace parent, FullNamedExpression type,
53                                  Modifiers mod_flags, MemberName name, ParametersCompiled param_list,
54                                  Attributes attrs)
55                         : base (ns, parent, name, attrs, MemberKind.Delegate)
56
57                 {
58                         this.ReturnType = type;
59                         ModFlags        = ModifiersExtensions.Check (AllowedModifiers, mod_flags,
60                                                            IsTopLevel ? Modifiers.INTERNAL :
61                                                            Modifiers.PRIVATE, name.Location, Report);
62                         Parameters      = param_list;
63                         spec = new TypeSpec (Kind, null, this, null, ModFlags | Modifiers.SEALED);
64                 }
65
66                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
67                 {
68                         if (a.Target == AttributeTargets.ReturnValue) {
69                                 if (return_attributes == null)
70                                         return_attributes = new ReturnParameter (this, InvokeBuilder.MethodBuilder, Location);
71
72                                 return_attributes.ApplyAttributeBuilder (a, ctor, cdata, pa);
73                                 return;
74                         }
75
76                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
77                 }
78
79                 public override AttributeTargets AttributeTargets {
80                         get {
81                                 return AttributeTargets.Delegate;
82                         }
83                 }
84
85                 protected override bool DoDefineMembers ()
86                 {
87                         var ctor_parameters = ParametersCompiled.CreateFullyResolved (
88                                 new [] {
89                                         new Parameter (new TypeExpression (TypeManager.object_type, Location), "object", Parameter.Modifier.NONE, null, Location),
90                                         new Parameter (new TypeExpression (TypeManager.intptr_type, Location), "method", Parameter.Modifier.NONE, null, Location)
91                                 },
92                                 new [] {
93                                         TypeManager.object_type,
94                                         TypeManager.intptr_type
95                                 }
96                         );
97
98                         Constructor = new Constructor (this, System.Reflection.ConstructorInfo.ConstructorName,
99                                 Modifiers.PUBLIC, null, ctor_parameters, null, Location);
100                         Constructor.Define ();
101
102                         //
103                         // Here the various methods like Invoke, BeginInvoke etc are defined
104                         //
105                         // First, call the `out of band' special method for
106                         // defining recursively any types we need:
107                         //
108                         var p = Parameters;
109
110                         if (!p.Resolve (this))
111                                 return false;
112
113                         //
114                         // Invoke method
115                         //
116
117                         // Check accessibility
118                         foreach (var partype in p.Types) {
119                                 if (!IsAccessibleAs (partype)) {
120                                         Report.SymbolRelatedToPreviousError (partype);
121                                         Report.Error (59, Location,
122                                                 "Inconsistent accessibility: parameter type `{0}' is less accessible than delegate `{1}'",
123                                                 TypeManager.CSharpName (partype), GetSignatureForError ());
124                                 }
125                         }
126
127                         ReturnType = ReturnType.ResolveAsTypeTerminal (this, false);
128                         if (ReturnType == null)
129                                 return false;
130
131                         var ret_type = ReturnType.Type;
132
133                         //
134                         // We don't have to check any others because they are all
135                         // guaranteed to be accessible - they are standard types.
136                         //
137                         if (!IsAccessibleAs (ret_type)) {
138                                 Report.SymbolRelatedToPreviousError (ret_type);
139                                 Report.Error (58, Location,
140                                                   "Inconsistent accessibility: return type `" +
141                                                   TypeManager.CSharpName (ret_type) + "' is less " +
142                                                   "accessible than delegate `" + GetSignatureForError () + "'");
143                                 return false;
144                         }
145
146                         CheckProtectedModifier ();
147
148                         if (RootContext.StdLib && TypeManager.IsSpecialType (ret_type)) {
149                                 Method.Error1599 (Location, ret_type, Report);
150                                 return false;
151                         }
152
153                         TypeManager.CheckTypeVariance (ret_type, Variance.Covariant, this);
154
155                         InvokeBuilder = new Method (this, null, ReturnType, MethodModifiers, new MemberName (InvokeMethodName), p, null);
156                         InvokeBuilder.Define ();
157
158                         //
159                         // Don't emit async method for compiler generated delegates (e.g. dynamic site containers)
160                         //
161                         if (TypeManager.iasyncresult_type != null && TypeManager.asynccallback_type != null && !IsCompilerGenerated) {
162                                 DefineAsyncMethods (Parameters.CallingConvention);
163                         }
164
165                         return true;
166                 }
167
168                 void DefineAsyncMethods (CallingConventions cc)
169                 {
170                         //
171                         // BeginInvoke
172                         //
173                         Parameter[] compiled = new Parameter[Parameters.Count];
174                         for (int i = 0; i < compiled.Length; ++i)
175                                 compiled[i] = new Parameter (new TypeExpression (Parameters.Types[i], Location),
176                                         Parameters.FixedParameters[i].Name,
177                                         Parameters.FixedParameters[i].ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT),
178                                         null, Location);
179
180                         ParametersCompiled async_parameters = new ParametersCompiled (Compiler, compiled);
181
182                         async_parameters = ParametersCompiled.MergeGenerated (Compiler, async_parameters, false,
183                                 new Parameter[] {
184                                         new Parameter (new TypeExpression (TypeManager.asynccallback_type, Location), "callback", Parameter.Modifier.NONE, null, Location),
185                                         new Parameter (new TypeExpression (TypeManager.object_type, Location), "object", Parameter.Modifier.NONE, null, Location)
186                                 },
187                                 new [] {
188                                         TypeManager.asynccallback_type,
189                                         TypeManager.object_type
190                                 }
191                         );
192
193                         BeginInvokeBuilder = new Method (this, null,
194                                 new TypeExpression (TypeManager.iasyncresult_type, Location), MethodModifiers,
195                                 new MemberName ("BeginInvoke"), async_parameters, null);
196                         BeginInvokeBuilder.Define ();
197
198                         //
199                         // EndInvoke is a bit more interesting, all the parameters labeled as
200                         // out or ref have to be duplicated here.
201                         //
202
203                         //
204                         // Define parameters, and count out/ref parameters
205                         //
206                         ParametersCompiled end_parameters;
207                         int out_params = 0;
208
209                         foreach (Parameter p in Parameters.FixedParameters) {
210                                 if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
211                                         ++out_params;
212                         }
213
214                         if (out_params > 0) {
215                                 var end_param_types = new TypeSpec [out_params];
216                                 Parameter[] end_params = new Parameter[out_params];
217
218                                 int param = 0;
219                                 for (int i = 0; i < Parameters.FixedParameters.Length; ++i) {
220                                         Parameter p = Parameters [i];
221                                         if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
222                                                 continue;
223
224                                         end_param_types[param] = Parameters.Types[i];
225                                         end_params[param] = p;
226                                         ++param;
227                                 }
228                                 end_parameters = ParametersCompiled.CreateFullyResolved (end_params, end_param_types);
229                         } else {
230                                 end_parameters = ParametersCompiled.EmptyReadOnlyParameters;
231                         }
232
233                         end_parameters = ParametersCompiled.MergeGenerated (Compiler, end_parameters, false,
234                                 new Parameter (
235                                         new TypeExpression (TypeManager.iasyncresult_type, Location),
236                                         "result", Parameter.Modifier.NONE, null, Location),
237                                 TypeManager.iasyncresult_type);
238
239                         //
240                         // Create method, define parameters, register parameters with type system
241                         //
242                         EndInvokeBuilder = new Method (this, null, ReturnType, MethodModifiers, new MemberName ("EndInvoke"), end_parameters, null);
243                         EndInvokeBuilder.Define ();
244                 }
245
246                 public override void DefineConstants ()
247                 {
248                         if (!Parameters.IsEmpty && Parameters[Parameters.Count - 1].HasDefaultValue) {
249                                 var rc = new ResolveContext (this);
250                                 Parameters.ResolveDefaultValues (rc);
251                         }
252                 }
253
254                 public override void EmitType ()
255                 {
256                         if (ReturnType.Type == InternalType.Dynamic) {
257                                 return_attributes = new ReturnParameter (this, InvokeBuilder.MethodBuilder, Location);
258                                 PredefinedAttributes.Get.Dynamic.EmitAttribute (return_attributes.Builder);
259                         } else {
260                                 var trans_flags = TypeManager.HasDynamicTypeUsed (ReturnType.Type);
261                                 if (trans_flags != null) {
262                                         var pa = PredefinedAttributes.Get.DynamicTransform;
263                                         if (pa.Constructor != null || pa.ResolveConstructor (Location, ArrayContainer.MakeType (TypeManager.bool_type))) {
264                                                 return_attributes = new ReturnParameter (this, InvokeBuilder.MethodBuilder, Location);
265                                                 return_attributes.Builder.SetCustomAttribute (
266                                                         new CustomAttributeBuilder (pa.Constructor, new object [] { trans_flags }));
267                                         }
268                                 }
269                         }
270
271                         Parameters.ApplyAttributes (InvokeBuilder.MethodBuilder);
272                         
273                         Constructor.ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
274                         InvokeBuilder.MethodBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
275
276                         if (BeginInvokeBuilder != null) {
277                                 BeginInvokeBuilder.ParameterInfo.ApplyAttributes (BeginInvokeBuilder.MethodBuilder);
278
279                                 BeginInvokeBuilder.MethodBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
280                                 EndInvokeBuilder.MethodBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
281                         }
282
283                         if (OptAttributes != null) {
284                                 OptAttributes.Emit ();
285                         }
286
287                         base.Emit ();
288                 }
289
290                 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
291                 {
292                         base_type = TypeManager.multicast_delegate_type;
293                         base_class = null;
294                         return null;
295                 }
296
297                 protected override TypeAttributes TypeAttr {
298                         get {
299                                 return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel) |
300                                         TypeAttributes.Class | TypeAttributes.Sealed |
301                                         base.TypeAttr;
302                         }
303                 }
304
305                 public override string[] ValidAttributeTargets {
306                         get {
307                                 return attribute_targets;
308                         }
309                 }
310
311                 //TODO: duplicate
312                 protected override bool VerifyClsCompliance ()
313                 {
314                         if (!base.VerifyClsCompliance ()) {
315                                 return false;
316                         }
317
318                         Parameters.VerifyClsCompliance (this);
319
320                         if (!ReturnType.Type.IsCLSCompliant ()) {
321                                 Report.Warning (3002, 1, Location, "Return type of `{0}' is not CLS-compliant",
322                                         GetSignatureForError ());
323                         }
324                         return true;
325                 }
326
327
328                 public static MethodSpec GetConstructor (CompilerContext ctx, TypeSpec container_type, TypeSpec delType)
329                 {
330                         var ctor = MemberCache.FindMember (delType, MemberFilter.Constructor (null), BindingRestriction.DeclaredOnly);
331                         return (MethodSpec) ctor;
332                 }
333
334                 //
335                 // Returns the "Invoke" from a delegate type
336                 //
337                 public static MethodSpec GetInvokeMethod (CompilerContext ctx, TypeSpec delType)
338                 {
339                         var invoke = MemberCache.FindMember (delType,
340                                 MemberFilter.Method (InvokeMethodName, 0, null, null),
341                                 BindingRestriction.DeclaredOnly);
342
343                         return (MethodSpec) invoke;
344                 }
345
346                 public static AParametersCollection GetParameters (CompilerContext ctx, TypeSpec delType)
347                 {
348                         var invoke_mb = GetInvokeMethod (ctx, delType);
349                         return invoke_mb.Parameters;
350                 }
351
352                 //
353                 // 15.2 Delegate compatibility
354                 //
355                 public static bool IsTypeCovariant (Expression a, TypeSpec b)
356                 {
357                         //
358                         // For each value parameter (a parameter with no ref or out modifier), an 
359                         // identity conversion or implicit reference conversion exists from the
360                         // parameter type in D to the corresponding parameter type in M
361                         //
362                         if (a.Type == b)
363                                 return true;
364
365                         if (RootContext.Version == LanguageVersion.ISO_1)
366                                 return false;
367
368                         return Convert.ImplicitReferenceConversionExists (a, b);
369                 }
370
371                 public static string FullDelegateDesc (MethodSpec invoke_method)
372                 {
373                         return TypeManager.GetFullNameSignature (invoke_method).Replace (".Invoke", "");
374                 }
375                 
376                 public Expression InstanceExpression {
377                         get {
378                                 return instance_expr;
379                         }
380                         set {
381                                 instance_expr = value;
382                         }
383                 }
384         }
385
386         //
387         // Base class for `NewDelegate' and `ImplicitDelegateCreation'
388         //
389         public abstract class DelegateCreation : Expression, MethodGroupExpr.IErrorHandler
390         {
391                 protected MethodSpec constructor_method;
392                 protected MethodGroupExpr method_group;
393
394                 public static Arguments CreateDelegateMethodArguments (AParametersCollection pd, TypeSpec[] types, Location loc)
395                 {
396                         Arguments delegate_arguments = new Arguments (pd.Count);
397                         for (int i = 0; i < pd.Count; ++i) {
398                                 Argument.AType atype_modifier;
399                                 switch (pd.FixedParameters [i].ModFlags) {
400                                 case Parameter.Modifier.REF:
401                                         atype_modifier = Argument.AType.Ref;
402                                         break;
403                                 case Parameter.Modifier.OUT:
404                                         atype_modifier = Argument.AType.Out;
405                                         break;
406                                 default:
407                                         atype_modifier = 0;
408                                         break;
409                                 }
410
411                                 delegate_arguments.Add (new Argument (new TypeExpression (types [i], loc), atype_modifier));
412                         }
413
414                         return delegate_arguments;
415                 }
416
417                 public override Expression CreateExpressionTree (ResolveContext ec)
418                 {
419                         MemberAccess ma = new MemberAccess (new MemberAccess (new QualifiedAliasMember ("global", "System", loc), "Delegate", loc), "CreateDelegate", loc);
420
421                         Arguments args = new Arguments (3);
422                         args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
423                         args.Add (new Argument (new NullLiteral (loc)));
424                         args.Add (new Argument (method_group.CreateExpressionTree (ec)));
425                         Expression e = new Invocation (ma, args).Resolve (ec);
426                         if (e == null)
427                                 return null;
428
429                         e = Convert.ExplicitConversion (ec, e, type, loc);
430                         if (e == null)
431                                 return null;
432
433                         return e.CreateExpressionTree (ec);
434                 }
435
436                 protected override Expression DoResolve (ResolveContext ec)
437                 {
438                         constructor_method = Delegate.GetConstructor (ec.Compiler, ec.CurrentType, type);
439
440                         var invoke_method = Delegate.GetInvokeMethod (ec.Compiler, type);
441                         method_group.DelegateType = type;
442                         method_group.CustomErrorHandler = this;
443
444                         Arguments arguments = CreateDelegateMethodArguments (invoke_method.Parameters, invoke_method.Parameters.Types, loc);
445                         method_group = method_group.OverloadResolve (ec, ref arguments, false, loc);
446                         if (method_group == null)
447                                 return null;
448
449                         var delegate_method = method_group.BestCandidate;
450                         
451                         if (TypeManager.IsNullableType (delegate_method.DeclaringType)) {
452                                 ec.Report.Error (1728, loc, "Cannot create delegate from method `{0}' because it is a member of System.Nullable<T> type",
453                                         delegate_method.GetSignatureForError ());
454                                 return null;
455                         }               
456                         
457                         Invocation.IsSpecialMethodInvocation (ec, delegate_method, loc);
458
459                         ExtensionMethodGroupExpr emg = method_group as ExtensionMethodGroupExpr;
460                         if (emg != null) {
461                                 method_group.InstanceExpression = emg.ExtensionExpression;
462                                 TypeSpec e_type = emg.ExtensionExpression.Type;
463                                 if (TypeManager.IsValueType (e_type)) {
464                                         ec.Report.Error (1113, loc, "Extension method `{0}' of value type `{1}' cannot be used to create delegates",
465                                                 delegate_method.GetSignatureForError (), TypeManager.CSharpName (e_type));
466                                 }
467                         }
468
469                         TypeSpec rt = delegate_method.ReturnType;
470                         Expression ret_expr = new TypeExpression (rt, loc);
471                         if (!Delegate.IsTypeCovariant (ret_expr, invoke_method.ReturnType)) {
472                                 Error_ConversionFailed (ec, delegate_method, ret_expr);
473                         }
474
475                         if (delegate_method.IsConditionallyExcluded (loc)) {
476                                 ec.Report.SymbolRelatedToPreviousError (delegate_method);
477                                 MethodOrOperator m = delegate_method.MemberDefinition as MethodOrOperator;
478                                 if (m != null && m.IsPartialDefinition) {
479                                         ec.Report.Error (762, loc, "Cannot create delegate from partial method declaration `{0}'",
480                                                 delegate_method.GetSignatureForError ());
481                                 } else {
482                                         ec.Report.Error (1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute",
483                                                 TypeManager.CSharpSignature (delegate_method));
484                                 }
485                         }
486
487                         var expr = method_group.InstanceExpression;
488                         if (expr != null && (expr.Type.IsGenericParameter || !TypeManager.IsReferenceType (expr.Type)))
489                                 method_group.InstanceExpression = new BoxedCast (expr, TypeManager.object_type);
490
491                         eclass = ExprClass.Value;
492                         return this;
493                 }
494                 
495                 public override void Emit (EmitContext ec)
496                 {
497                         if (method_group.InstanceExpression == null)
498                                 ec.Emit (OpCodes.Ldnull);
499                         else
500                                 method_group.InstanceExpression.Emit (ec);
501
502                         var delegate_method = method_group.BestCandidate;
503
504                         // Any delegate must be sealed
505                         if (!delegate_method.DeclaringType.IsDelegate && delegate_method.IsVirtual && !method_group.IsBase) {
506                                 ec.Emit (OpCodes.Dup);
507                                 ec.Emit (OpCodes.Ldvirtftn, delegate_method);
508                         } else {
509                                 ec.Emit (OpCodes.Ldftn, delegate_method);
510                         }
511
512                         ec.Emit (OpCodes.Newobj, constructor_method);
513                 }
514
515                 void Error_ConversionFailed (ResolveContext ec, MethodSpec method, Expression return_type)
516                 {
517                         var invoke_method = Delegate.GetInvokeMethod (ec.Compiler, type);
518                         string member_name = method_group.InstanceExpression != null ?
519                                 Delegate.FullDelegateDesc (method) :
520                                 TypeManager.GetFullNameSignature (method);
521
522                         ec.Report.SymbolRelatedToPreviousError (type);
523                         ec.Report.SymbolRelatedToPreviousError (method);
524                         if (RootContext.Version == LanguageVersion.ISO_1) {
525                                 ec.Report.Error (410, loc, "A method or delegate `{0} {1}' parameters and return type must be same as delegate `{2} {3}' parameters and return type",
526                                         TypeManager.CSharpName (method.ReturnType), member_name,
527                                         TypeManager.CSharpName (invoke_method.ReturnType), Delegate.FullDelegateDesc (invoke_method));
528                                 return;
529                         }
530                         if (return_type == null) {
531                                 ec.Report.Error (123, loc, "A method or delegate `{0}' parameters do not match delegate `{1}' parameters",
532                                         member_name, Delegate.FullDelegateDesc (invoke_method));
533                                 return;
534                         }
535
536                         ec.Report.Error (407, loc, "A method or delegate `{0} {1}' return type does not match delegate `{2} {3}' return type",
537                                 return_type.GetSignatureForError (), member_name,
538                                 TypeManager.CSharpName (invoke_method.ReturnType), Delegate.FullDelegateDesc (invoke_method));
539                 }
540
541                 public static bool ImplicitStandardConversionExists (ResolveContext ec, MethodGroupExpr mg, TypeSpec target_type)
542                 {
543                         if (target_type == TypeManager.delegate_type || target_type == TypeManager.multicast_delegate_type)
544                                 return false;
545
546                         mg.DelegateType = target_type;
547                         var invoke = Delegate.GetInvokeMethod (ec.Compiler, target_type);
548
549                         Arguments arguments = CreateDelegateMethodArguments (invoke.Parameters, invoke.Parameters.Types, mg.Location);
550                         return mg.OverloadResolve (ec, ref arguments, true, mg.Location) != null;
551                 }
552
553                 #region IErrorHandler Members
554
555                 public bool NoExactMatch (ResolveContext ec, MethodSpec method)
556                 {
557                         if (method.IsGeneric)
558                                 return false;
559
560                         Error_ConversionFailed (ec, method, null);
561                         return true;
562                 }
563
564                 public bool AmbiguousCall (ResolveContext ec, MethodGroupExpr mg, MethodSpec ambiguous)
565                 {
566                         return false;
567                 }
568
569                 #endregion
570         }
571
572         //
573         // Created from the conversion code
574         //
575         public class ImplicitDelegateCreation : DelegateCreation
576         {
577                 ImplicitDelegateCreation (TypeSpec t, MethodGroupExpr mg, Location l)
578                 {
579                         type = t;
580                         this.method_group = mg;
581                         loc = l;
582                 }
583
584                 static public Expression Create (ResolveContext ec, MethodGroupExpr mge,
585                                                  TypeSpec target_type, Location loc)
586                 {
587                         ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, mge, loc);
588                         return d.DoResolve (ec);
589                 }
590         }
591         
592         //
593         // A delegate-creation-expression, invoked from the `New' class 
594         //
595         public class NewDelegate : DelegateCreation
596         {
597                 public Arguments Arguments;
598
599                 //
600                 // This constructor is invoked from the `New' expression
601                 //
602                 public NewDelegate (TypeSpec type, Arguments Arguments, Location loc)
603                 {
604                         this.type = type;
605                         this.Arguments = Arguments;
606                         this.loc  = loc; 
607                 }
608
609                 protected override Expression DoResolve (ResolveContext ec)
610                 {
611                         if (Arguments == null || Arguments.Count != 1) {
612                                 ec.Report.Error (149, loc, "Method name expected");
613                                 return null;
614                         }
615
616                         Argument a = Arguments [0];
617                         if (!a.ResolveMethodGroup (ec))
618                                 return null;
619
620                         Expression e = a.Expr;
621
622                         AnonymousMethodExpression ame = e as AnonymousMethodExpression;
623                         if (ame != null && RootContext.Version != LanguageVersion.ISO_1) {
624                                 e = ame.Compatible (ec, type);
625                                 if (e == null)
626                                         return null;
627
628                                 return e.Resolve (ec);
629                         }
630
631                         method_group = e as MethodGroupExpr;
632                         if (method_group == null) {
633                                 if (e.Type == InternalType.Dynamic) {
634                                         e = Convert.ImplicitConversionRequired (ec, e, type, loc);
635                                 } else if (!e.Type.IsDelegate) {
636                                         e.Error_UnexpectedKind (ec, ResolveFlags.MethodGroup | ResolveFlags.Type, loc);
637                                         return null;
638                                 }
639
640                                 //
641                                 // An argument is not a method but another delegate
642                                 //
643                                 method_group = new MethodGroupExpr (Delegate.GetInvokeMethod (ec.Compiler, e.Type), e.Type, loc);
644                                 method_group.InstanceExpression = e;
645                         }
646
647                         return base.DoResolve (ec);
648                 }
649         }
650
651         //
652         // Invocation converted to delegate Invoke call
653         //
654         class DelegateInvocation : ExpressionStatement
655         {
656                 readonly Expression InstanceExpr;
657                 Arguments arguments;
658                 MethodSpec method;
659                 
660                 public DelegateInvocation (Expression instance_expr, Arguments args, Location loc)
661                 {
662                         this.InstanceExpr = instance_expr;
663                         this.arguments = args;
664                         this.loc = loc;
665                 }
666                 
667                 public override Expression CreateExpressionTree (ResolveContext ec)
668                 {
669                         Arguments args = Arguments.CreateForExpressionTree (ec, this.arguments,
670                                 InstanceExpr.CreateExpressionTree (ec));
671
672                         return CreateExpressionFactoryCall (ec, "Invoke", args);
673                 }
674
675                 protected override Expression DoResolve (ResolveContext ec)
676                 {
677                         if (InstanceExpr is EventExpr) {
678                                 ((EventExpr) InstanceExpr).Error_CannotAssign (ec);
679                                 return null;
680                         }
681                         
682                         TypeSpec del_type = InstanceExpr.Type;
683                         if (del_type == null)
684                                 return null;
685                         
686                         method = Delegate.GetInvokeMethod (ec.Compiler, del_type);
687                         var mb = method;
688                         var me = new MethodGroupExpr (mb, del_type, loc);
689                         me.InstanceExpression = InstanceExpr;
690
691                         AParametersCollection pd = mb.Parameters;
692                         int pd_count = pd.Count;
693
694                         int arg_count = arguments == null ? 0 : arguments.Count;
695
696                         bool params_method = pd.HasParams;
697                         bool is_params_applicable = false;
698                         bool is_applicable = me.IsApplicable (ec, ref arguments, arg_count, ref mb, ref is_params_applicable) == 0;
699                         if (arguments != null)
700                                 arg_count = arguments.Count;
701
702                         if (!is_applicable && !params_method && arg_count != pd_count) {
703                                 ec.Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
704                                         TypeManager.CSharpName (del_type), arg_count.ToString ());
705                         } else if (arguments == null || !arguments.HasDynamic) {
706                                 me.VerifyArgumentsCompat (ec, ref arguments, arg_count, mb,
707                                         is_params_applicable || (!is_applicable && params_method), false, loc);
708                         }
709
710                         type = method.ReturnType;
711                         eclass = ExprClass.Value;
712                         return this;
713                 }
714
715                 public override void Emit (EmitContext ec)
716                 {
717                         //
718                         // Invocation on delegates call the virtual Invoke member
719                         // so we are always `instance' calls
720                         //
721                         Invocation.EmitCall (ec, InstanceExpr, method, arguments, loc);
722                 }
723
724                 public override void EmitStatement (EmitContext ec)
725                 {
726                         Emit (ec);
727                         // 
728                         // Pop the return value if there is one
729                         //
730                         if (type != TypeManager.void_type)
731                                 ec.Emit (OpCodes.Pop);
732                 }
733
734                 public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
735                 {
736                         return Invocation.MakeExpression (ctx, InstanceExpr, method, arguments);
737                 }
738         }
739 }