2001-12-18 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / delegate.cs
1 //\r
2 // delegate.cs: Delegate Handler\r
3 //\r
4 // Author: Ravi Pratap (ravi@ximian.com)\r
5 //\r
6 // Licensed under the terms of the GNU GPL\r
7 //\r
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)\r
9 //\r
10 //\r
11 \r
12 using System;\r
13 using System.Collections;\r
14 using System.Reflection;\r
15 using System.Reflection.Emit;\r
16 using System.Text;\r
17 \r
18 namespace Mono.CSharp {\r
19 \r
20         /// <summary>\r
21         ///   Holds Delegates\r
22         /// </summary>\r
23         public class Delegate {\r
24 \r
25                 public readonly string Name;\r
26                 public readonly string ReturnType;\r
27                 public int             mod_flags;\r
28                 public Parameters      Parameters;\r
29                 public Attributes      OptAttributes;\r
30                 public TypeBuilder     TypeBuilder;\r
31 \r
32                 public ConstructorBuilder ConstructorBuilder;\r
33                 public MethodBuilder      InvokeBuilder;\r
34                 public MethodBuilder      BeginInvokeBuilder;\r
35                 public MethodBuilder      EndInvokeBuilder;\r
36                 \r
37                 Type [] param_types;\r
38                 Type ret_type;\r
39                 \r
40                 Expression instance_expr;\r
41                 MethodBase delegate_method;\r
42         \r
43                 Location loc;\r
44 \r
45                 const int AllowedModifiers =\r
46                         Modifiers.NEW |\r
47                         Modifiers.PUBLIC |\r
48                         Modifiers.PROTECTED |\r
49                         Modifiers.INTERNAL |\r
50                         Modifiers.PRIVATE;\r
51 \r
52                 public Delegate (string type, int mod_flags, string name, Parameters param_list,\r
53                                  Attributes attrs, Location loc)\r
54                 {\r
55                         this.Name       = name;\r
56                         this.ReturnType = type;\r
57                         this.mod_flags  = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PUBLIC);\r
58                         Parameters      = param_list;\r
59                         OptAttributes   = attrs;\r
60                         this.loc        = loc;\r
61                 }\r
62 \r
63                 public void DefineDelegate (object parent_builder)\r
64                 {\r
65                         TypeAttributes attr;\r
66                         \r
67                         if (parent_builder is ModuleBuilder) {\r
68                                 ModuleBuilder builder = (ModuleBuilder) parent_builder;\r
69                                 attr = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed;\r
70 \r
71                                 TypeBuilder = builder.DefineType (Name, attr, TypeManager.delegate_type);\r
72                         } else {\r
73                                 TypeBuilder builder = (TypeBuilder) parent_builder;\r
74                                 attr = TypeAttributes.NestedPublic | TypeAttributes.Class |\r
75                                         TypeAttributes.Sealed;\r
76 \r
77                                 TypeBuilder = builder.DefineNestedType (\r
78                                         Name, attr, TypeManager.delegate_type);\r
79                         }\r
80 \r
81                         RootContext.TypeManager.AddDelegateType (Name, TypeBuilder, this);\r
82                 }\r
83 \r
84                 public bool Populate (TypeContainer parent)\r
85                 {\r
86 \r
87                         MethodAttributes mattr;\r
88                         int i;\r
89                         \r
90                         // FIXME: POSSIBLY make this static, as it is always constant\r
91                         // \r
92                         Type [] const_arg_types = new Type [2];\r
93                         const_arg_types [0] = TypeManager.object_type;\r
94                         const_arg_types [1] = TypeManager.intptr_type;\r
95 \r
96                         mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |\r
97                                 MethodAttributes.HideBySig | MethodAttributes.Public;\r
98 \r
99                         ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,\r
100                                                                             CallingConventions.Standard,\r
101                                                                             const_arg_types);\r
102 \r
103                         //\r
104                         // HACK because System.Reflection.Emit is lame\r
105                         //\r
106                         //\r
107                         // FIXME: POSSIBLY make these static, as they are always the same\r
108                         Parameter [] fixed_pars = new Parameter [2];\r
109                         fixed_pars [0] = new Parameter (null, null, Parameter.Modifier.NONE, null);\r
110                         fixed_pars [1] = new Parameter (null, null, Parameter.Modifier.NONE, null);\r
111                         Parameters const_parameters = new Parameters (fixed_pars, null);\r
112                         \r
113                         TypeManager.RegisterMethod (\r
114                                 ConstructorBuilder,\r
115                                 new InternalParameters (const_arg_types, const_parameters),\r
116                                 const_arg_types);\r
117                                 \r
118                         \r
119                         ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);\r
120                         \r
121                         // Here the various methods like Invoke, BeginInvoke etc are defined\r
122 \r
123                         //\r
124                         // TODO: Call AsAccessible for each type\r
125                         //\r
126 \r
127                         //\r
128                         // Invoke method\r
129                         //\r
130                         param_types = Parameters.GetParameterInfo (parent);\r
131                         if (param_types == null)\r
132                                 return false;\r
133                         \r
134                         ret_type = parent.LookupType (ReturnType, false);\r
135                         if (ret_type == null)\r
136                                 return false;\r
137                         \r
138                         CallingConventions cc = Parameters.GetCallingConvention ();\r
139 \r
140                         mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;\r
141 \r
142                         InvokeBuilder = TypeBuilder.DefineMethod ("Invoke", \r
143                                                                   mattr,                     \r
144                                                                   cc,\r
145                                                                   ret_type,                  \r
146                                                                   param_types);\r
147 \r
148                         for (i = 0 ; i < param_types.Length; i++) {\r
149                                 Parameter p = Parameters.FixedParameters [i];\r
150                                 string name = p.Name;\r
151                                 ParameterBuilder pb = InvokeBuilder.DefineParameter (i+1, p.Attributes, name); \r
152                         }\r
153                         \r
154                         InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);\r
155 \r
156                         TypeManager.RegisterMethod (InvokeBuilder,\r
157                                                     new InternalParameters (parent, Parameters),\r
158                                                     param_types);\r
159 \r
160                         //\r
161                         // BeginInvoke\r
162                         //\r
163                         int params_num = param_types.Length;\r
164                         Type [] async_param_types = new Type [params_num + 2];\r
165 \r
166                         param_types.CopyTo (async_param_types, 0);\r
167 \r
168                         async_param_types [params_num] = TypeManager.asynccallback_type;\r
169                         async_param_types [params_num + 1] = TypeManager.object_type;\r
170 \r
171                         mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual |\r
172                                 MethodAttributes.NewSlot;\r
173                         \r
174                         BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",\r
175                                                                        mattr,\r
176                                                                        cc,\r
177                                                                        TypeManager.iasyncresult_type,\r
178                                                                        async_param_types);\r
179 \r
180                         for (i = 0 ; i < param_types.Length; i++) {\r
181                                 Parameter p = Parameters.FixedParameters [i];\r
182                                 string name = p.Name;\r
183                                 BeginInvokeBuilder.DefineParameter (i + 1, p.Attributes, name); \r
184                         }\r
185                         \r
186                         BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");\r
187                         BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");\r
188                         \r
189                         BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);\r
190 \r
191                         Parameter [] async_params = new Parameter [params_num + 2];\r
192                         Parameters.FixedParameters.CopyTo (async_params, 0);\r
193 \r
194                         async_params [params_num] = new Parameter ("System.AsyncCallback", "callback",\r
195                                                                    Parameter.Modifier.NONE, null);\r
196                         async_params [params_num + 1] = new Parameter ("System.IAsyncResult", "object",\r
197                                                                    Parameter.Modifier.NONE, null);\r
198 \r
199                         TypeManager.RegisterMethod (BeginInvokeBuilder,\r
200                                                     new InternalParameters (\r
201                                                             parent,\r
202                                                             new Parameters (async_params, null)),\r
203                                                     async_param_types);\r
204 \r
205                         //\r
206                         // EndInvoke\r
207                         //\r
208                         Type [] end_param_types = new Type [1];\r
209                         end_param_types [0] = TypeManager.iasyncresult_type;\r
210                         \r
211                         EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke",\r
212                                                                      mattr,\r
213                                                                      cc,\r
214                                                                      ret_type,\r
215                                                                      end_param_types);\r
216                         EndInvokeBuilder.DefineParameter (1, ParameterAttributes.None, "result");\r
217                         \r
218                         EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);\r
219 \r
220                         Parameter [] end_params = new Parameter [1];\r
221                         end_params [0] = new Parameter ("System.IAsyncResult", "result",\r
222                                                         Parameter.Modifier.NONE, null);\r
223 \r
224                         TypeManager.RegisterMethod (EndInvokeBuilder,\r
225                                                     new InternalParameters (\r
226                                                             parent, new Parameters (end_params, null)),\r
227                                                     end_param_types);\r
228 \r
229                         return true;\r
230                 }\r
231 \r
232                 /// <summary>\r
233                 ///  Verifies whether the method in question is compatible with the delegate\r
234                 ///  Returns the method itself if okay and null if not.\r
235                 /// </summary>\r
236                 public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb, Location loc)\r
237                 {\r
238                         ParameterData pd = Invocation.GetParameterData (mb);\r
239 \r
240                         Expression ml = Expression.MemberLookup (ec, delegate_type, "Invoke", false, loc);\r
241 \r
242                         if (!(ml is MethodGroupExpr)) {\r
243                                 Report.Error (-100, loc, "Internal error : could not find Invoke method!");\r
244                                 return null;\r
245                         }\r
246 \r
247                         MethodBase invoke_mb = ((MethodGroupExpr) ml).Methods [0];\r
248 \r
249                         ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);\r
250 \r
251                         bool mismatch = false;\r
252                         for (int i = pd.Count; i > 0; ) {\r
253                                 i--;\r
254 \r
255                                 if (invoke_pd.ParameterType (i) == pd.ParameterType (i))\r
256                                         continue;\r
257                                 else {\r
258                                         mismatch = true;\r
259                                         break;\r
260                                 }\r
261                         }\r
262 \r
263                         if (mismatch) {\r
264                                 Report.Error (123, loc, "Method '" + Invocation.FullMethodDesc (mb) + "' does not match " +\r
265                                               "delegate '" + FullDelegateDesc (delegate_type, invoke_mb, invoke_pd) + "'");\r
266                                 return null;\r
267                         }\r
268 \r
269                         if (((MethodInfo) invoke_mb).ReturnType == ((MethodInfo) mb).ReturnType)\r
270                                 return mb;\r
271                         else\r
272                                 mismatch = true;\r
273 \r
274                         if (mismatch) {\r
275                                 Report.Error (123, loc, "Method '" + Invocation.FullMethodDesc (mb) + "' does not match " +\r
276                                               "delegate '" + FullDelegateDesc (delegate_type, invoke_mb, invoke_pd) + "'");\r
277                                 return null;\r
278                         }\r
279 \r
280                         return null;\r
281                 }\r
282 \r
283                 // <summary>\r
284                 //  Verifies whether the invocation arguments are compatible with the\r
285                 //  delegate's target method\r
286                 // </summary>\r
287                 public static bool VerifyApplicability (EmitContext ec, Type delegate_type, ArrayList args, Location loc)\r
288                 {\r
289                         int arg_count;\r
290 \r
291                         if (args == null)\r
292                                 arg_count = 0;\r
293                         else\r
294                                 arg_count = args.Count;\r
295 \r
296                         Expression ml = Expression.MemberLookup (ec, delegate_type, "Invoke", false, loc);\r
297                         \r
298                         if (!(ml is MethodGroupExpr)) {\r
299                                 Report.Error (-100, loc, "Internal error : could not find Invoke method!");\r
300                                 return false;\r
301                         }\r
302                         \r
303                         MethodBase mb = ((MethodGroupExpr) ml).Methods [0];\r
304 \r
305                         ParameterData pd = Invocation.GetParameterData (mb);\r
306                         \r
307                         if (pd.Count != arg_count) {\r
308                                 Report.Error (1593, loc,\r
309                                               "Delegate '" + delegate_type.ToString ()\r
310                                               + "' does not take '" + arg_count + "' arguments");\r
311                                 return false;\r
312                         }\r
313 \r
314                         for (int i = arg_count; i > 0;) {\r
315                                 i--;\r
316                                 Expression conv;\r
317                                 Argument a = (Argument) args [i];\r
318                                 Expression a_expr = a.Expr;\r
319                                 \r
320                                 if (pd.ParameterType (i) != a_expr.Type) {\r
321                                         \r
322                                         conv = Expression.ConvertImplicitStandard (ec, a_expr, pd.ParameterType (i), loc);\r
323 \r
324                                         if (conv == null) {\r
325                                                 Report.Error (1594, loc,\r
326                                                               "Delegate '" + delegate_type.ToString () +\r
327                                                               "' has some invalid arguments.");\r
328 \r
329                                                 Report.Error (1503, loc,\r
330                                                        "Argument " + (i+1) +\r
331                                                        ": Cannot convert from '" +\r
332                                                        TypeManager.CSharpName (a_expr.Type)\r
333                                                        + "' to '" + TypeManager.CSharpName (pd.ParameterType (i)) + "'");\r
334                                                 return false;\r
335                                         }\r
336 \r
337                                         if (a_expr != conv)\r
338                                                 a.Expr = conv;\r
339                                 }\r
340                         }\r
341 \r
342                         return true;\r
343                 }\r
344 \r
345                 /// <summary>\r
346                 ///  Verifies whether the delegate in question is compatible with this one in\r
347                 ///  order to determine if instantiation from the same is possible.\r
348                 /// </summary>\r
349                 public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Type probe_type, Location loc)\r
350                 {\r
351                         Expression ml = Expression.MemberLookup (ec, delegate_type, "Invoke", false, loc);\r
352                         \r
353                         if (!(ml is MethodGroupExpr)) {\r
354                                 Report.Error (-100, loc, "Internal error : could not find Invoke method!");\r
355                                 return false;\r
356                         }\r
357                         \r
358                         MethodBase mb = ((MethodGroupExpr) ml).Methods [0];\r
359                         ParameterData pd = Invocation.GetParameterData (mb);\r
360 \r
361                         Expression probe_ml = Expression.MemberLookup (ec, delegate_type, "Invoke", false, loc);\r
362                         \r
363                         if (!(probe_ml is MethodGroupExpr)) {\r
364                                 Report.Error (-100, loc, "Internal error : could not find Invoke method!");\r
365                                 return false;\r
366                         }\r
367                         \r
368                         MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];\r
369                         ParameterData probe_pd = Invocation.GetParameterData (probe_mb);\r
370                         \r
371                         if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)\r
372                                 return false;\r
373 \r
374                         if (pd.Count != probe_pd.Count)\r
375                                 return false;\r
376 \r
377                         for (int i = pd.Count; i > 0; ) {\r
378                                 i--;\r
379 \r
380                                 if (pd.ParameterType (i) != probe_pd.ParameterType (i) ||\r
381                                     pd.ParameterModifier (i) != probe_pd.ParameterModifier (i))\r
382                                         return false;\r
383                         }\r
384                         \r
385                         return true;\r
386                 }\r
387                 \r
388                 public static string FullDelegateDesc (Type del_type, MethodBase mb, ParameterData pd)\r
389                 {\r
390                         StringBuilder sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));\r
391                         \r
392                         sb.Append (" " + del_type.ToString ());\r
393                         sb.Append (" (");\r
394 \r
395                         int length = pd.Count;\r
396                         \r
397                         for (int i = length; i > 0; ) {\r
398                                 i--;\r
399                                 \r
400                                 sb.Append (TypeManager.CSharpName (pd.ParameterType (length - i - 1)));\r
401                                 if (i != 0)\r
402                                         sb.Append (", ");\r
403                         }\r
404                         \r
405                         sb.Append (")");\r
406                         return sb.ToString ();\r
407                         \r
408                 }\r
409                 \r
410                 // Hack around System.Reflection as found everywhere else\r
411                 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria)\r
412                 {\r
413                         ArrayList members = new ArrayList ();\r
414 \r
415                         if ((mt & MemberTypes.Method) != 0) {\r
416                                 if (filter (ConstructorBuilder, criteria))\r
417                                         members.Add (ConstructorBuilder);\r
418 \r
419                                 if (filter (InvokeBuilder, criteria))\r
420                                         members.Add (InvokeBuilder);\r
421 \r
422                                 if (filter (BeginInvokeBuilder, criteria))\r
423                                         members.Add (BeginInvokeBuilder);\r
424 \r
425                                 if (filter (EndInvokeBuilder, criteria))\r
426                                         members.Add (EndInvokeBuilder);\r
427                         }\r
428 \r
429                         int count = members.Count;\r
430 \r
431                         if (count > 0) {\r
432                                 MemberInfo [] mi = new MemberInfo [count];\r
433                                 members.CopyTo (mi, 0);\r
434                                 return mi;\r
435                         }\r
436 \r
437                         return null;\r
438                 }\r
439                 \r
440                 public void CloseDelegate ()\r
441                 {\r
442                         TypeBuilder.CreateType ();\r
443                 }\r
444                 \r
445                 public int ModFlags {\r
446                         get {\r
447                                 return mod_flags;\r
448                         }\r
449                 }\r
450 \r
451                 public Expression InstanceExpression {\r
452                         get {\r
453                                 return instance_expr;\r
454                         }\r
455                         set {\r
456                                 instance_expr = value;\r
457                         }\r
458                 }\r
459 \r
460                 public MethodBase TargetMethod {\r
461                         get {\r
462                                 return delegate_method;\r
463                         }\r
464                         set {\r
465                                 delegate_method = value;\r
466                         }\r
467                 }\r
468 \r
469                 public Type TargetReturnType {\r
470                         get {\r
471                                 return ret_type;\r
472                         }\r
473                 }\r
474 \r
475                 public Type [] ParameterTypes {\r
476                         get {\r
477                                 return param_types;\r
478                         }\r
479                 }\r
480                 \r
481         }\r
482 \r
483         public class NewDelegate : Expression {\r
484 \r
485                 public ArrayList Arguments;\r
486 \r
487                 MethodBase constructor_method;\r
488                 MethodBase delegate_method;\r
489                 Expression delegate_instance_expr;\r
490 \r
491                 Location Location;\r
492                 \r
493                 public NewDelegate (Type type, ArrayList Arguments, Location loc)\r
494                 {\r
495                         this.type = type;\r
496                         this.Arguments = Arguments;\r
497                         this.Location  = loc; \r
498                 }\r
499 \r
500                 public override Expression DoResolve (EmitContext ec)\r
501                 {\r
502                         if (Arguments == null) {\r
503                                 Report.Error (-11, Location,\r
504                                               "Delegate creation expression takes only one argument");\r
505                                 return null;\r
506                         }\r
507                         \r
508                         if (Arguments.Count != 1) {\r
509                                 Report.Error (-11, Location,\r
510                                               "Delegate creation expression takes only one argument");\r
511                                 return null;\r
512                         }\r
513 \r
514                         Expression ml = Expression.MemberLookup (ec, type, ".ctor", false, Location);\r
515 \r
516                         if (!(ml is MethodGroupExpr)) {\r
517                                 Report.Error (-100, Location, "Internal error : Could not find delegate constructor!");\r
518                                 return null;\r
519                         }\r
520 \r
521                         constructor_method = ((MethodGroupExpr) ml).Methods [0];\r
522                         \r
523                         Argument a = (Argument) Arguments [0];\r
524                         \r
525                         if (!a.Resolve (ec, Location))\r
526                                 return null;\r
527                         \r
528                         Expression e = a.Expr;\r
529                         \r
530                         if (e is MethodGroupExpr) {\r
531                                 MethodGroupExpr mg = (MethodGroupExpr) e;\r
532                                 \r
533                                 delegate_method  = Delegate.VerifyMethod (ec, type, mg.Methods [0], Location);\r
534                                 \r
535                                 if (delegate_method == null)\r
536                                         return null;\r
537                                 \r
538                                 if (mg.InstanceExpression != null)\r
539                                         delegate_instance_expr = mg.InstanceExpression.Resolve (ec);\r
540                                 else\r
541                                         delegate_instance_expr = null;\r
542                                 \r
543                                 if (delegate_instance_expr != null)\r
544                                         if (delegate_instance_expr.Type.IsValueType)\r
545                                                 delegate_instance_expr = new BoxedCast (delegate_instance_expr);\r
546 \r
547                                 DictionaryEntry de = new DictionaryEntry (delegate_method, delegate_instance_expr);\r
548                                 TypeManager.RegisterDelegateData (type, de);\r
549                                 \r
550                                 eclass = ExprClass.Value;\r
551                                 return this;\r
552                         }\r
553 \r
554                         Type e_type = e.Type;\r
555 \r
556                         if (!TypeManager.IsDelegateType (e_type)) {\r
557                                 Report.Error (-12, Location, "Cannot create a delegate from something " +\r
558                                               "not a delegate or a method.");\r
559                                 return null;\r
560                         }\r
561 \r
562                         // This is what MS' compiler reports. We could always choose\r
563                         // to be more verbose and actually give delegate-level specifics\r
564                         \r
565                         if (!Delegate.VerifyDelegate (ec, type, e_type, Location)) {\r
566                                 Report.Error (29, Location, "Cannot implicitly convert type '" + e_type + "' " +\r
567                                               "to type '" + type + "'");\r
568                                 return null;\r
569                         }\r
570 \r
571                         DictionaryEntry d = TypeManager.GetDelegateData (e_type);\r
572 \r
573                         delegate_instance_expr = (Expression) d.Value;\r
574                         delegate_method        = (MethodBase) d.Key;\r
575                         \r
576                         eclass = ExprClass.Value;\r
577                         return this;\r
578                 }\r
579                 \r
580                 public override void Emit (EmitContext ec)\r
581                 {\r
582                         if (delegate_instance_expr == null)\r
583                                 ec.ig.Emit (OpCodes.Ldnull);\r
584                         else\r
585                                 delegate_instance_expr.Emit (ec);\r
586                         \r
587                         ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);\r
588                         ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);\r
589                 }\r
590         }\r
591 \r
592         public class DelegateInvocation : ExpressionStatement {\r
593 \r
594                 public Expression InstanceExpr;\r
595                 public ArrayList  Arguments;\r
596                 public Location   Location;\r
597 \r
598                 MethodBase method;\r
599                 \r
600                 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)\r
601                 {\r
602                         this.InstanceExpr = instance_expr;\r
603                         this.Arguments = args;\r
604                         this.Location = loc;\r
605                 }\r
606 \r
607                 public override Expression DoResolve (EmitContext ec)\r
608                 {\r
609                         Type del_type = InstanceExpr.Type;\r
610                         \r
611                         if (Arguments != null){\r
612                                 for (int i = Arguments.Count; i > 0;){\r
613                                         --i;\r
614                                         Argument a = (Argument) Arguments [i];\r
615                                         \r
616                                         if (!a.Resolve (ec, Location))\r
617                                                 return null;\r
618                                 }\r
619                         }\r
620                         \r
621                         if (!Delegate.VerifyApplicability (ec, del_type, Arguments, Location))\r
622                                 return null;\r
623 \r
624                         Expression ml = Expression.MemberLookup (ec, del_type, "Invoke", false, Location);\r
625                         \r
626                         if (!(ml is MethodGroupExpr)) {\r
627                                 Report.Error (-100, Location, "Internal error : could not find Invoke method!");\r
628                                 return null;\r
629                         }\r
630                         \r
631                         method = ((MethodGroupExpr) ml).Methods [0];\r
632                         \r
633                         type = ((MethodInfo) method).ReturnType;\r
634                         \r
635                         eclass = ExprClass.Value;\r
636                         \r
637                         return this;\r
638                 }\r
639 \r
640                 public override void Emit (EmitContext ec)\r
641                 {\r
642                         Delegate del = TypeManager.LookupDelegate (InstanceExpr.Type);\r
643 \r
644                         //\r
645                         // Invocation on delegates call the virtual Invoke member\r
646                         // so we are always `instance' calls\r
647                         //\r
648                         Invocation.EmitCall (ec, false, InstanceExpr, method, Arguments);\r
649                 }\r
650 \r
651                 public override void EmitStatement (EmitContext ec)\r
652                 {\r
653                         Emit (ec);\r
654                         // \r
655                         // Pop the return value if there is one\r
656                         //\r
657                         if (method is MethodInfo){\r
658                                 if (((MethodInfo) method).ReturnType != TypeManager.void_type)\r
659                                         ec.ig.Emit (OpCodes.Pop);\r
660                         }\r
661                 }\r
662 \r
663         }\r
664 }\r