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