1 // -*- coding: dos -*-
\r
3 // delegate.cs: Delegate Handler
\r
6 // Ravi Pratap (ravi@ximian.com)
\r
7 // Miguel de Icaza (miguel@ximian.com)
\r
9 // Licensed under the terms of the GNU GPL
\r
11 // (C) 2001 Ximian, Inc (http://www.ximian.com)
\r
16 using System.Collections;
\r
17 using System.Reflection;
\r
18 using System.Reflection.Emit;
\r
21 namespace Mono.CSharp {
\r
26 public class Delegate : DeclSpace {
\r
27 public Expression ReturnType;
\r
28 public Parameters Parameters;
\r
30 public ConstructorBuilder ConstructorBuilder;
\r
31 public MethodBuilder InvokeBuilder;
\r
32 public MethodBuilder BeginInvokeBuilder;
\r
33 public MethodBuilder EndInvokeBuilder;
\r
35 Type [] param_types;
\r
38 Expression instance_expr;
\r
39 MethodBase delegate_method;
\r
41 const int AllowedModifiers =
\r
44 Modifiers.PROTECTED |
\r
45 Modifiers.INTERNAL |
\r
49 public Delegate (NamespaceEntry ns, TypeContainer parent, Expression type,
\r
50 int mod_flags, string name, Parameters param_list,
\r
51 Attributes attrs, Location l)
\r
52 : base (ns, parent, name, attrs, l)
\r
55 this.ReturnType = type;
\r
56 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags,
\r
57 IsTopLevel ? Modifiers.INTERNAL :
\r
58 Modifiers.PRIVATE, l);
\r
59 Parameters = param_list;
\r
62 public override TypeBuilder DefineType ()
\r
64 if (TypeBuilder != null)
\r
68 foreach (TypeParameter type_param in TypeParameters)
\r
69 if (!type_param.Resolve (this))
\r
73 TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel) |
\r
74 TypeAttributes.Class | TypeAttributes.Sealed;
\r
77 if (TypeManager.NamespaceClash (Name, Location))
\r
80 ModuleBuilder builder = CodeGen.Module.Builder;
\r
82 TypeBuilder = builder.DefineType (
\r
83 Name, attr, TypeManager.multicast_delegate_type);
\r
85 TypeBuilder builder = Parent.TypeBuilder;
\r
87 string name = Name.Substring (1 + Name.LastIndexOf ('.'));
\r
88 TypeBuilder = builder.DefineNestedType (
\r
89 name, attr, TypeManager.multicast_delegate_type);
\r
92 TypeManager.AddDelegateType (Name, TypeBuilder, this);
\r
95 CurrentType = new ConstructedType (
\r
96 Name, TypeParameters, Location);
\r
98 foreach (TypeParameter type_param in TypeParameters)
\r
99 type_param.Define (TypeBuilder);
\r
102 return TypeBuilder;
\r
105 public override bool DefineMembers (TypeContainer container)
\r
110 public override bool Define (TypeContainer container)
\r
112 MethodAttributes mattr;
\r
114 ParameterBuilder pb;
\r
116 EmitContext ec = new EmitContext (this, this, Location, null,
\r
117 null, ModFlags, false);
\r
120 foreach (TypeParameter type_param in TypeParameters)
\r
121 type_param.DefineType (ec, TypeBuilder);
\r
124 // FIXME: POSSIBLY make this static, as it is always constant
\r
126 Type [] const_arg_types = new Type [2];
\r
127 const_arg_types [0] = TypeManager.object_type;
\r
128 const_arg_types [1] = TypeManager.intptr_type;
\r
130 mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
\r
131 MethodAttributes.HideBySig | MethodAttributes.Public;
\r
133 ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
\r
134 CallingConventions.Standard,
\r
137 ConstructorBuilder.DefineParameter (1, ParameterAttributes.None, "object");
\r
138 ConstructorBuilder.DefineParameter (2, ParameterAttributes.None, "method");
\r
140 // HACK because System.Reflection.Emit is lame
\r
143 // FIXME: POSSIBLY make these static, as they are always the same
\r
144 Parameter [] fixed_pars = new Parameter [2];
\r
145 fixed_pars [0] = new Parameter (null, null, Parameter.Modifier.NONE, null);
\r
146 fixed_pars [1] = new Parameter (null, null, Parameter.Modifier.NONE, null);
\r
147 Parameters const_parameters = new Parameters (fixed_pars, null, Location);
\r
149 TypeManager.RegisterMethod (
\r
150 ConstructorBuilder,
\r
151 new InternalParameters (const_arg_types, const_parameters),
\r
155 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
158 // Here the various methods like Invoke, BeginInvoke etc are defined
\r
160 // First, call the `out of band' special method for
\r
161 // defining recursively any types we need:
\r
163 if (!Parameters.ComputeAndDefineParameterTypes (this))
\r
166 param_types = Parameters.GetParameterInfo (this);
\r
167 if (param_types == null)
\r
174 // Check accessibility
\r
175 foreach (Type partype in param_types){
\r
176 if (!container.AsAccessible (partype, ModFlags)) {
\r
177 Report.Error (59, Location,
\r
178 "Inconsistent accessibility: parameter type `" +
\r
179 TypeManager.CSharpName (partype) + "` is less " +
\r
180 "accessible than delegate `" + Name + "'");
\r
183 if (partype.IsPointer && !UnsafeOK (container))
\r
187 ReturnType = ResolveTypeExpr (ReturnType, false, Location);
\r
188 if (ReturnType == null)
\r
191 ret_type = ReturnType.Type;
\r
192 if (ret_type == null)
\r
195 if (!container.AsAccessible (ret_type, ModFlags)) {
\r
196 Report.Error (58, Location,
\r
197 "Inconsistent accessibility: return type `" +
\r
198 TypeManager.CSharpName (ret_type) + "` is less " +
\r
199 "accessible than delegate `" + Name + "'");
\r
203 if (ret_type.IsPointer && !UnsafeOK (container))
\r
207 // We don't have to check any others because they are all
\r
208 // guaranteed to be accessible - they are standard types.
\r
211 CallingConventions cc = Parameters.GetCallingConvention ();
\r
213 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
\r
215 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
\r
222 // Define parameters, and count out/ref parameters
\r
224 int out_params = 0;
\r
226 if (Parameters.FixedParameters != null){
\r
227 int top = Parameters.FixedParameters.Length;
\r
230 for (; i < top; i++) {
\r
231 p = Parameters.FixedParameters [i];
\r
232 pb = InvokeBuilder.DefineParameter (i+1, p.Attributes, p.Name);
\r
233 cattr = p.OptAttributes;
\r
235 Attribute.ApplyAttributes (ec, pb, pb, cattr);
\r
237 if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
\r
241 if (Parameters.ArrayParameter != null){
\r
242 Parameter p = Parameters.ArrayParameter;
\r
244 pb = InvokeBuilder.DefineParameter (
\r
245 i+1, p.Attributes, p.Name);
\r
246 cattr = p.OptAttributes;
\r
248 Attribute.ApplyAttributes (ec, pb, pb, cattr);
\r
251 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
253 TypeManager.RegisterMethod (InvokeBuilder,
\r
254 new InternalParameters (container, Parameters),
\r
260 int params_num = param_types.Length;
\r
261 Type [] async_param_types = new Type [params_num + 2];
\r
263 param_types.CopyTo (async_param_types, 0);
\r
265 async_param_types [params_num] = TypeManager.asynccallback_type;
\r
266 async_param_types [params_num + 1] = TypeManager.object_type;
\r
268 mattr = MethodAttributes.Public | MethodAttributes.HideBySig |
\r
269 MethodAttributes.Virtual | MethodAttributes.NewSlot;
\r
271 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
\r
274 TypeManager.iasyncresult_type,
\r
275 async_param_types);
\r
278 if (Parameters.FixedParameters != null){
\r
279 int top = Parameters.FixedParameters.Length;
\r
282 for (i = 0 ; i < top; i++) {
\r
283 p = Parameters.FixedParameters [i];
\r
285 pb = BeginInvokeBuilder.DefineParameter (i+1, p.Attributes, p.Name);
\r
286 cattr = p.OptAttributes;
\r
288 Attribute.ApplyAttributes (ec, pb, pb, cattr);
\r
291 if (Parameters.ArrayParameter != null){
\r
292 Parameter p = Parameters.ArrayParameter;
\r
294 pb = BeginInvokeBuilder.DefineParameter (i+1, p.Attributes, p.Name);
\r
295 cattr = p.OptAttributes;
\r
297 Attribute.ApplyAttributes (ec, pb, pb, cattr);
\r
301 BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
\r
302 BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
\r
304 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
306 Parameter [] async_params = new Parameter [params_num + 2];
\r
308 if (Parameters.FixedParameters != null){
\r
309 Parameters.FixedParameters.CopyTo (async_params, 0);
\r
310 n = Parameters.FixedParameters.Length;
\r
312 if (Parameters.ArrayParameter != null)
\r
313 async_params [n] = Parameters.ArrayParameter;
\r
315 async_params [params_num] = new Parameter (
\r
316 TypeManager.system_asynccallback_expr, "callback",
\r
317 Parameter.Modifier.NONE, null);
\r
318 async_params [params_num + 1] = new Parameter (
\r
319 TypeManager.system_object_expr, "object",
\r
320 Parameter.Modifier.NONE, null);
\r
322 Parameters async_parameters = new Parameters (async_params, null, Location);
\r
323 async_parameters.ComputeAndDefineParameterTypes (this);
\r
325 async_parameters.ComputeAndDefineParameterTypes (this);
\r
326 TypeManager.RegisterMethod (BeginInvokeBuilder,
\r
327 new InternalParameters (container, async_parameters),
\r
328 async_param_types);
\r
331 // EndInvoke is a bit more interesting, all the parameters labeled as
\r
332 // out or ref have to be duplicated here.
\r
335 Type [] end_param_types = new Type [out_params + 1];
\r
336 Parameter [] end_params = new Parameter [out_params + 1];
\r
338 if (out_params > 0){
\r
339 int top = Parameters.FixedParameters.Length;
\r
340 for (i = 0; i < top; i++){
\r
341 Parameter p = Parameters.FixedParameters [i];
\r
342 if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
\r
345 end_param_types [param] = param_types [i];
\r
346 end_params [param] = p;
\r
350 end_param_types [out_params] = TypeManager.iasyncresult_type;
\r
351 end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null);
\r
354 // Create method, define parameters, register parameters with type system
\r
356 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_param_types);
\r
357 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
360 // EndInvoke: Label the parameters
\r
362 EndInvokeBuilder.DefineParameter (out_params + 1, ParameterAttributes.None, "result");
\r
363 for (i = 0; i < end_params.Length-1; i++){
\r
364 EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
\r
367 Parameters end_parameters = new Parameters (end_params, null, Location);
\r
368 end_parameters.ComputeAndDefineParameterTypes (this);
\r
370 TypeManager.RegisterMethod (
\r
372 new InternalParameters (container, end_parameters),
\r
375 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
\r
380 /// Verifies whether the method in question is compatible with the delegate
\r
381 /// Returns the method itself if okay and null if not.
\r
383 public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb,
\r
386 ParameterData pd = Invocation.GetParameterData (mb);
\r
388 int pd_count = pd.Count;
\r
390 Expression ml = Expression.MemberLookup (
\r
391 ec, delegate_type, "Invoke", loc);
\r
393 if (!(ml is MethodGroupExpr)) {
\r
394 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
\r
398 MethodBase invoke_mb = ((MethodGroupExpr) ml).Methods [0];
\r
400 ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
\r
402 if (invoke_pd.Count != pd_count)
\r
405 for (int i = pd_count; i > 0; ) {
\r
408 if (invoke_pd.ParameterType (i) == pd.ParameterType (i) &&
\r
409 invoke_pd.ParameterModifier (i) == pd.ParameterModifier (i))
\r
416 if (((MethodInfo) invoke_mb).ReturnType == ((MethodInfo) mb).ReturnType)
\r
423 // Verifies whether the invocation arguments are compatible with the
\r
424 // delegate's target method
\r
426 public static bool VerifyApplicability (EmitContext ec,
\r
427 Type delegate_type,
\r
436 arg_count = args.Count;
\r
438 Expression ml = Expression.MemberLookup (
\r
439 ec, delegate_type, "Invoke", loc);
\r
441 if (!(ml is MethodGroupExpr)) {
\r
442 Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
\r
446 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
\r
447 ParameterData pd = Invocation.GetParameterData (mb);
\r
449 int pd_count = pd.Count;
\r
451 bool not_params_method = (pd_count == 0) ||
\r
452 (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS);
\r
454 if (not_params_method && pd_count != arg_count) {
\r
455 Report.Error (1593, loc,
\r
456 "Delegate '" + delegate_type.ToString ()
\r
457 + "' does not take '" + arg_count + "' arguments");
\r
461 return Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, !not_params_method,
\r
462 delegate_type, loc);
\r
466 /// Verifies whether the delegate in question is compatible with this one in
\r
467 /// order to determine if instantiation from the same is possible.
\r
469 public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Type probe_type, Location loc)
\r
471 Expression ml = Expression.MemberLookup (
\r
472 ec, delegate_type, "Invoke", loc);
\r
474 if (!(ml is MethodGroupExpr)) {
\r
475 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
\r
479 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
\r
480 ParameterData pd = Invocation.GetParameterData (mb);
\r
482 Expression probe_ml = Expression.MemberLookup (
\r
483 ec, delegate_type, "Invoke", loc);
\r
485 if (!(probe_ml is MethodGroupExpr)) {
\r
486 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
\r
490 MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];
\r
491 ParameterData probe_pd = Invocation.GetParameterData (probe_mb);
\r
493 if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)
\r
496 if (pd.Count != probe_pd.Count)
\r
499 for (int i = pd.Count; i > 0; ) {
\r
502 if (pd.ParameterType (i) != probe_pd.ParameterType (i) ||
\r
503 pd.ParameterModifier (i) != probe_pd.ParameterModifier (i))
\r
510 public static string FullDelegateDesc (Type del_type, MethodBase mb, ParameterData pd)
\r
512 StringBuilder sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
\r
514 sb.Append (" " + del_type.ToString ());
\r
517 int length = pd.Count;
\r
519 for (int i = length; i > 0; ) {
\r
522 sb.Append (pd.ParameterDesc (length - i - 1));
\r
528 return sb.ToString ();
\r
532 // Hack around System.Reflection as found everywhere else
\r
533 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
\r
534 MemberFilter filter, object criteria)
\r
536 ArrayList members = new ArrayList ();
\r
538 if ((mt & MemberTypes.Method) != 0) {
\r
539 if (ConstructorBuilder != null)
\r
540 if (filter (ConstructorBuilder, criteria))
\r
541 members.Add (ConstructorBuilder);
\r
543 if (InvokeBuilder != null)
\r
544 if (filter (InvokeBuilder, criteria))
\r
545 members.Add (InvokeBuilder);
\r
547 if (BeginInvokeBuilder != null)
\r
548 if (filter (BeginInvokeBuilder, criteria))
\r
549 members.Add (BeginInvokeBuilder);
\r
551 if (EndInvokeBuilder != null)
\r
552 if (filter (EndInvokeBuilder, criteria))
\r
553 members.Add (EndInvokeBuilder);
\r
556 return new MemberList (members);
\r
559 public override MemberCache MemberCache {
\r
565 public Expression InstanceExpression {
\r
567 return instance_expr;
\r
570 instance_expr = value;
\r
574 public MethodBase TargetMethod {
\r
576 return delegate_method;
\r
579 delegate_method = value;
\r
583 public Type TargetReturnType {
\r
589 public Type [] ParameterTypes {
\r
591 return param_types;
\r
598 // Base class for `NewDelegate' and `ImplicitDelegateCreation'
\r
600 public abstract class DelegateCreation : Expression {
\r
601 protected MethodBase constructor_method;
\r
602 protected MethodBase delegate_method;
\r
603 protected Expression delegate_instance_expr;
\r
605 public DelegateCreation () {}
\r
607 public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
\r
609 string method_desc;
\r
611 if (mg.Methods.Length > 1)
\r
612 method_desc = mg.Methods [0].Name;
\r
614 method_desc = Invocation.FullMethodDesc (mg.Methods [0]);
\r
616 Expression invoke_method = Expression.MemberLookup (
\r
617 ec, type, "Invoke", MemberTypes.Method,
\r
618 Expression.AllBindingFlags, loc);
\r
619 MethodBase method = ((MethodGroupExpr) invoke_method).Methods [0];
\r
620 ParameterData param = Invocation.GetParameterData (method);
\r
621 string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
\r
623 Report.Error (123, loc, "Method '" + method_desc + "' does not " +
\r
624 "match delegate '" + delegate_desc + "'");
\r
627 public override void Emit (EmitContext ec)
\r
629 if (delegate_instance_expr == null ||
\r
630 delegate_method.IsStatic)
\r
631 ec.ig.Emit (OpCodes.Ldnull);
\r
633 delegate_instance_expr.Emit (ec);
\r
635 if (delegate_method.IsVirtual) {
\r
636 ec.ig.Emit (OpCodes.Dup);
\r
637 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
\r
639 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
\r
640 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
\r
643 protected bool ResolveConstructorMethod (EmitContext ec)
\r
645 Expression ml = Expression.MemberLookup (
\r
646 ec, type, ".ctor", loc);
\r
648 if (!(ml is MethodGroupExpr)) {
\r
649 Report.Error (-100, loc, "Internal error: Could not find delegate constructor!");
\r
653 constructor_method = ((MethodGroupExpr) ml).Methods [0];
\r
657 protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
\r
659 foreach (MethodInfo mi in mg.Methods){
\r
660 delegate_method = Delegate.VerifyMethod (ec, type, mi, loc);
\r
662 if (delegate_method != null)
\r
666 if (delegate_method == null) {
\r
667 Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
\r
672 // Check safe/unsafe of the delegate
\r
675 ParameterData param = Invocation.GetParameterData (delegate_method);
\r
676 int count = param.Count;
\r
678 for (int i = 0; i < count; i++){
\r
679 if (param.ParameterType (i).IsPointer){
\r
680 Expression.UnsafeError (loc);
\r
686 if (mg.InstanceExpression != null)
\r
687 delegate_instance_expr = mg.InstanceExpression.Resolve (ec);
\r
690 if (!delegate_method.IsStatic){
\r
691 Report.Error (120, loc,
\r
692 "An object reference is required for the non-static method " +
\r
693 delegate_method.Name);
\r
696 delegate_instance_expr = null;
\r
698 delegate_instance_expr = ec.GetThis (loc);
\r
701 if (delegate_instance_expr != null)
\r
702 if (delegate_instance_expr.Type.IsValueType)
\r
703 delegate_instance_expr = new BoxedCast (delegate_instance_expr);
\r
705 eclass = ExprClass.Value;
\r
711 // Created from the conversion code
\r
713 public class ImplicitDelegateCreation : DelegateCreation {
\r
715 ImplicitDelegateCreation (Type t, Location l)
\r
721 public override Expression DoResolve (EmitContext ec)
\r
726 static public Expression Create (EmitContext ec, MethodGroupExpr mge, Type target_type, Location loc)
\r
728 ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
\r
729 if (d.ResolveConstructorMethod (ec))
\r
730 return d.ResolveMethodGroupExpr (ec, mge);
\r
737 // A delegate-creation-expression, invoked from the `New' class
\r
739 public class NewDelegate : DelegateCreation {
\r
740 public ArrayList Arguments;
\r
743 // This constructor is invoked from the `New' expression
\r
745 public NewDelegate (Type type, ArrayList Arguments, Location loc)
\r
748 this.Arguments = Arguments;
\r
752 public override Expression DoResolve (EmitContext ec)
\r
754 if (Arguments == null || Arguments.Count != 1) {
\r
755 Report.Error (149, loc,
\r
756 "Method name expected");
\r
760 if (!ResolveConstructorMethod (ec))
\r
763 Argument a = (Argument) Arguments [0];
\r
765 Expression invoke_method = Expression.MemberLookup (
\r
766 ec, type, "Invoke", MemberTypes.Method,
\r
767 Expression.AllBindingFlags, loc);
\r
769 if (invoke_method == null) {
\r
770 Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
\r
774 if (!a.ResolveMethodGroup (ec, loc))
\r
777 Expression e = a.Expr;
\r
779 MethodGroupExpr mg = e as MethodGroupExpr;
\r
781 return ResolveMethodGroupExpr (ec, mg);
\r
783 Type e_type = e.Type;
\r
785 if (!TypeManager.IsDelegateType (e_type)) {
\r
786 e.Error_UnexpectedKind ("method");
\r
790 // This is what MS' compiler reports. We could always choose
\r
791 // to be more verbose and actually give delegate-level specifics
\r
793 if (!Delegate.VerifyDelegate (ec, type, e_type, loc)) {
\r
794 Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " +
\r
795 "to type '" + type + "'");
\r
799 delegate_instance_expr = e;
\r
800 delegate_method = ((MethodGroupExpr) invoke_method).Methods [0];
\r
802 eclass = ExprClass.Value;
\r
806 public override void Emit (EmitContext ec)
\r
808 if (delegate_instance_expr == null || delegate_method.IsStatic)
\r
809 ec.ig.Emit (OpCodes.Ldnull);
\r
811 delegate_instance_expr.Emit (ec);
\r
813 if (delegate_method.IsVirtual) {
\r
814 ec.ig.Emit (OpCodes.Dup);
\r
815 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
\r
817 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
\r
818 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
\r
822 public class DelegateInvocation : ExpressionStatement {
\r
824 public Expression InstanceExpr;
\r
825 public ArrayList Arguments;
\r
829 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
\r
831 this.InstanceExpr = instance_expr;
\r
832 this.Arguments = args;
\r
836 public override Expression DoResolve (EmitContext ec)
\r
838 if (InstanceExpr is EventExpr) {
\r
840 EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
\r
842 Expression ml = MemberLookup (
\r
843 ec, ec.ContainerType, ei.Name,
\r
844 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
\r
848 // If this is the case, then the Event does not belong
\r
849 // to this Type and so, according to the spec
\r
850 // cannot be accessed directly
\r
852 // Note that target will not appear as an EventExpr
\r
853 // in the case it is being referenced within the same type container;
\r
854 // it will appear as a FieldExpr in that case.
\r
857 Assign.error70 (ei, loc);
\r
863 Type del_type = InstanceExpr.Type;
\r
864 if (del_type == null)
\r
867 if (Arguments != null){
\r
868 foreach (Argument a in Arguments){
\r
869 if (!a.Resolve (ec, loc))
\r
874 if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
\r
877 Expression lookup = Expression.MemberLookup (ec, del_type, "Invoke", loc);
\r
878 if (!(lookup is MethodGroupExpr)) {
\r
879 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
\r
883 method = ((MethodGroupExpr) lookup).Methods [0];
\r
884 type = ((MethodInfo) method).ReturnType;
\r
885 eclass = ExprClass.Value;
\r
890 public override void Emit (EmitContext ec)
\r
893 // Invocation on delegates call the virtual Invoke member
\r
894 // so we are always `instance' calls
\r
896 Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, loc);
\r
899 public override void EmitStatement (EmitContext ec)
\r
903 // Pop the return value if there is one
\r
905 if (method is MethodInfo){
\r
906 if (((MethodInfo) method).ReturnType != TypeManager.void_type)
\r
907 ec.ig.Emit (OpCodes.Pop);
\r