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