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 MemberName MemberName;
\r
40 Expression instance_expr;
\r
41 MethodBase delegate_method;
\r
43 const int AllowedModifiers =
\r
46 Modifiers.PROTECTED |
\r
47 Modifiers.INTERNAL |
\r
51 public Delegate (NamespaceEntry ns, TypeContainer parent, Expression type,
\r
52 int mod_flags, MemberName name, Parameters param_list,
\r
53 Attributes attrs, Location l)
\r
54 : base (ns, parent, name.GetMemberName (), attrs, l)
\r
57 this.ReturnType = type;
\r
58 this.MemberName = name;
\r
59 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags,
\r
60 IsTopLevel ? Modifiers.INTERNAL :
\r
61 Modifiers.PRIVATE, l);
\r
62 Parameters = param_list;
\r
65 public override TypeBuilder DefineType ()
\r
67 if (TypeBuilder != null)
\r
71 foreach (TypeParameter type_param in TypeParameters)
\r
72 if (!type_param.Resolve (this))
\r
76 TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel) |
\r
77 TypeAttributes.Class | TypeAttributes.Sealed;
\r
80 if (TypeManager.NamespaceClash (Name, Location))
\r
83 ModuleBuilder builder = CodeGen.Module.Builder;
\r
85 TypeBuilder = builder.DefineType (
\r
86 Name, attr, TypeManager.multicast_delegate_type);
\r
88 TypeBuilder builder = Parent.TypeBuilder;
\r
90 string name = Name.Substring (1 + Name.LastIndexOf ('.'));
\r
91 TypeBuilder = builder.DefineNestedType (
\r
92 name, attr, TypeManager.multicast_delegate_type);
\r
95 TypeManager.AddDelegateType (Name, TypeBuilder, this);
\r
98 CurrentType = new ConstructedType (
\r
99 Name, TypeParameters, Location);
\r
101 foreach (TypeParameter type_param in TypeParameters)
\r
102 type_param.Define (TypeBuilder);
\r
105 return TypeBuilder;
\r
108 public override bool DefineMembers (TypeContainer container)
\r
113 public override bool Define (TypeContainer container)
\r
115 MethodAttributes mattr;
\r
117 ParameterBuilder pb;
\r
119 EmitContext ec = new EmitContext (this, this, Location, null,
\r
120 null, ModFlags, false);
\r
123 foreach (TypeParameter type_param in TypeParameters)
\r
124 type_param.DefineType (ec, TypeBuilder);
\r
127 // FIXME: POSSIBLY make this static, as it is always constant
\r
129 Type [] const_arg_types = new Type [2];
\r
130 const_arg_types [0] = TypeManager.object_type;
\r
131 const_arg_types [1] = TypeManager.intptr_type;
\r
133 mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
\r
134 MethodAttributes.HideBySig | MethodAttributes.Public;
\r
136 ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
\r
137 CallingConventions.Standard,
\r
140 ConstructorBuilder.DefineParameter (1, ParameterAttributes.None, "object");
\r
141 ConstructorBuilder.DefineParameter (2, ParameterAttributes.None, "method");
\r
143 // HACK because System.Reflection.Emit is lame
\r
146 // FIXME: POSSIBLY make these static, as they are always the same
\r
147 Parameter [] fixed_pars = new Parameter [2];
\r
148 fixed_pars [0] = new Parameter (null, null, Parameter.Modifier.NONE, null);
\r
149 fixed_pars [1] = new Parameter (null, null, Parameter.Modifier.NONE, null);
\r
150 Parameters const_parameters = new Parameters (fixed_pars, null, Location);
\r
152 TypeManager.RegisterMethod (
\r
153 ConstructorBuilder,
\r
154 new InternalParameters (const_arg_types, const_parameters),
\r
158 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
161 // Here the various methods like Invoke, BeginInvoke etc are defined
\r
163 // First, call the `out of band' special method for
\r
164 // defining recursively any types we need:
\r
166 if (!Parameters.ComputeAndDefineParameterTypes (this))
\r
169 param_types = Parameters.GetParameterInfo (this);
\r
170 if (param_types == null)
\r
177 // Check accessibility
\r
178 foreach (Type partype in param_types){
\r
179 if (!container.AsAccessible (partype, ModFlags)) {
\r
180 Report.Error (59, Location,
\r
181 "Inconsistent accessibility: parameter type `" +
\r
182 TypeManager.CSharpName (partype) + "` is less " +
\r
183 "accessible than delegate `" + Name + "'");
\r
186 if (partype.IsPointer && !UnsafeOK (container))
\r
190 ReturnType = ResolveTypeExpr (ReturnType, false, Location);
\r
191 if (ReturnType == null)
\r
194 ret_type = ReturnType.Type;
\r
195 if (ret_type == null)
\r
198 if (!container.AsAccessible (ret_type, ModFlags)) {
\r
199 Report.Error (58, Location,
\r
200 "Inconsistent accessibility: return type `" +
\r
201 TypeManager.CSharpName (ret_type) + "` is less " +
\r
202 "accessible than delegate `" + Name + "'");
\r
206 if (ret_type.IsPointer && !UnsafeOK (container))
\r
210 // We don't have to check any others because they are all
\r
211 // guaranteed to be accessible - they are standard types.
\r
214 CallingConventions cc = Parameters.GetCallingConvention ();
\r
216 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
\r
218 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
\r
225 // Define parameters, and count out/ref parameters
\r
227 int out_params = 0;
\r
229 if (Parameters.FixedParameters != null){
\r
230 int top = Parameters.FixedParameters.Length;
\r
233 for (; i < top; i++) {
\r
234 p = Parameters.FixedParameters [i];
\r
235 pb = InvokeBuilder.DefineParameter (i+1, p.Attributes, p.Name);
\r
236 cattr = p.OptAttributes;
\r
238 Attribute.ApplyAttributes (ec, pb, pb, cattr);
\r
240 if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
\r
244 if (Parameters.ArrayParameter != null){
\r
245 Parameter p = Parameters.ArrayParameter;
\r
247 pb = InvokeBuilder.DefineParameter (
\r
248 i+1, p.Attributes, p.Name);
\r
249 cattr = p.OptAttributes;
\r
251 Attribute.ApplyAttributes (ec, pb, pb, cattr);
\r
254 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
256 TypeManager.RegisterMethod (InvokeBuilder,
\r
257 new InternalParameters (container, Parameters),
\r
263 int params_num = param_types.Length;
\r
264 Type [] async_param_types = new Type [params_num + 2];
\r
266 param_types.CopyTo (async_param_types, 0);
\r
268 async_param_types [params_num] = TypeManager.asynccallback_type;
\r
269 async_param_types [params_num + 1] = TypeManager.object_type;
\r
271 mattr = MethodAttributes.Public | MethodAttributes.HideBySig |
\r
272 MethodAttributes.Virtual | MethodAttributes.NewSlot;
\r
274 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
\r
277 TypeManager.iasyncresult_type,
\r
278 async_param_types);
\r
281 if (Parameters.FixedParameters != null){
\r
282 int top = Parameters.FixedParameters.Length;
\r
285 for (i = 0 ; i < top; i++) {
\r
286 p = Parameters.FixedParameters [i];
\r
288 pb = BeginInvokeBuilder.DefineParameter (i+1, p.Attributes, p.Name);
\r
289 cattr = p.OptAttributes;
\r
291 Attribute.ApplyAttributes (ec, pb, pb, cattr);
\r
294 if (Parameters.ArrayParameter != null){
\r
295 Parameter p = Parameters.ArrayParameter;
\r
297 pb = BeginInvokeBuilder.DefineParameter (i+1, p.Attributes, p.Name);
\r
298 cattr = p.OptAttributes;
\r
300 Attribute.ApplyAttributes (ec, pb, pb, cattr);
\r
304 BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
\r
305 BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
\r
307 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
309 Parameter [] async_params = new Parameter [params_num + 2];
\r
311 if (Parameters.FixedParameters != null){
\r
312 Parameters.FixedParameters.CopyTo (async_params, 0);
\r
313 n = Parameters.FixedParameters.Length;
\r
315 if (Parameters.ArrayParameter != null)
\r
316 async_params [n] = Parameters.ArrayParameter;
\r
318 async_params [params_num] = new Parameter (
\r
319 TypeManager.system_asynccallback_expr, "callback",
\r
320 Parameter.Modifier.NONE, null);
\r
321 async_params [params_num + 1] = new Parameter (
\r
322 TypeManager.system_object_expr, "object",
\r
323 Parameter.Modifier.NONE, null);
\r
325 Parameters async_parameters = new Parameters (async_params, null, Location);
\r
326 async_parameters.ComputeAndDefineParameterTypes (this);
\r
328 async_parameters.ComputeAndDefineParameterTypes (this);
\r
329 TypeManager.RegisterMethod (BeginInvokeBuilder,
\r
330 new InternalParameters (container, async_parameters),
\r
331 async_param_types);
\r
334 // EndInvoke is a bit more interesting, all the parameters labeled as
\r
335 // out or ref have to be duplicated here.
\r
338 Type [] end_param_types = new Type [out_params + 1];
\r
339 Parameter [] end_params = new Parameter [out_params + 1];
\r
341 if (out_params > 0){
\r
342 int top = Parameters.FixedParameters.Length;
\r
343 for (i = 0; i < top; i++){
\r
344 Parameter p = Parameters.FixedParameters [i];
\r
345 if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
\r
348 end_param_types [param] = param_types [i];
\r
349 end_params [param] = p;
\r
353 end_param_types [out_params] = TypeManager.iasyncresult_type;
\r
354 end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null);
\r
357 // Create method, define parameters, register parameters with type system
\r
359 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_param_types);
\r
360 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
363 // EndInvoke: Label the parameters
\r
365 EndInvokeBuilder.DefineParameter (out_params + 1, ParameterAttributes.None, "result");
\r
366 for (i = 0; i < end_params.Length-1; i++){
\r
367 EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
\r
370 Parameters end_parameters = new Parameters (end_params, null, Location);
\r
371 end_parameters.ComputeAndDefineParameterTypes (this);
\r
373 TypeManager.RegisterMethod (
\r
375 new InternalParameters (container, end_parameters),
\r
378 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
\r
383 /// Verifies whether the method in question is compatible with the delegate
\r
384 /// Returns the method itself if okay and null if not.
\r
386 public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb,
\r
389 ParameterData pd = Invocation.GetParameterData (mb);
\r
391 int pd_count = pd.Count;
\r
393 Expression ml = Expression.MemberLookup (
\r
394 ec, delegate_type, "Invoke", loc);
\r
396 if (!(ml is MethodGroupExpr)) {
\r
397 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
\r
401 MethodBase invoke_mb = ((MethodGroupExpr) ml).Methods [0];
\r
403 ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
\r
405 if (invoke_pd.Count != pd_count)
\r
408 for (int i = pd_count; i > 0; ) {
\r
411 if (invoke_pd.ParameterType (i) == pd.ParameterType (i) &&
\r
412 invoke_pd.ParameterModifier (i) == pd.ParameterModifier (i))
\r
419 if (((MethodInfo) invoke_mb).ReturnType == ((MethodInfo) mb).ReturnType)
\r
426 // Verifies whether the invocation arguments are compatible with the
\r
427 // delegate's target method
\r
429 public static bool VerifyApplicability (EmitContext ec,
\r
430 Type delegate_type,
\r
439 arg_count = args.Count;
\r
441 Expression ml = Expression.MemberLookup (
\r
442 ec, delegate_type, "Invoke", loc);
\r
444 if (!(ml is MethodGroupExpr)) {
\r
445 Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
\r
449 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
\r
450 ParameterData pd = Invocation.GetParameterData (mb);
\r
452 int pd_count = pd.Count;
\r
454 bool not_params_method = (pd_count == 0) ||
\r
455 (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS);
\r
457 if (not_params_method && pd_count != arg_count) {
\r
458 Report.Error (1593, loc,
\r
459 "Delegate '" + delegate_type.ToString ()
\r
460 + "' does not take '" + arg_count + "' arguments");
\r
464 return Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, !not_params_method,
\r
465 delegate_type, loc);
\r
469 /// Verifies whether the delegate in question is compatible with this one in
\r
470 /// order to determine if instantiation from the same is possible.
\r
472 public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Type probe_type, Location loc)
\r
474 Expression ml = Expression.MemberLookup (
\r
475 ec, delegate_type, "Invoke", loc);
\r
477 if (!(ml is MethodGroupExpr)) {
\r
478 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
\r
482 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
\r
483 ParameterData pd = Invocation.GetParameterData (mb);
\r
485 Expression probe_ml = Expression.MemberLookup (
\r
486 ec, delegate_type, "Invoke", loc);
\r
488 if (!(probe_ml is MethodGroupExpr)) {
\r
489 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
\r
493 MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];
\r
494 ParameterData probe_pd = Invocation.GetParameterData (probe_mb);
\r
496 if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)
\r
499 if (pd.Count != probe_pd.Count)
\r
502 for (int i = pd.Count; i > 0; ) {
\r
505 if (pd.ParameterType (i) != probe_pd.ParameterType (i) ||
\r
506 pd.ParameterModifier (i) != probe_pd.ParameterModifier (i))
\r
513 public static string FullDelegateDesc (Type del_type, MethodBase mb, ParameterData pd)
\r
515 StringBuilder sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
\r
517 sb.Append (" " + del_type.ToString ());
\r
520 int length = pd.Count;
\r
522 for (int i = length; i > 0; ) {
\r
525 sb.Append (pd.ParameterDesc (length - i - 1));
\r
531 return sb.ToString ();
\r
535 // Hack around System.Reflection as found everywhere else
\r
536 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
\r
537 MemberFilter filter, object criteria)
\r
539 ArrayList members = new ArrayList ();
\r
541 if ((mt & MemberTypes.Method) != 0) {
\r
542 if (ConstructorBuilder != null)
\r
543 if (filter (ConstructorBuilder, criteria))
\r
544 members.Add (ConstructorBuilder);
\r
546 if (InvokeBuilder != null)
\r
547 if (filter (InvokeBuilder, criteria))
\r
548 members.Add (InvokeBuilder);
\r
550 if (BeginInvokeBuilder != null)
\r
551 if (filter (BeginInvokeBuilder, criteria))
\r
552 members.Add (BeginInvokeBuilder);
\r
554 if (EndInvokeBuilder != null)
\r
555 if (filter (EndInvokeBuilder, criteria))
\r
556 members.Add (EndInvokeBuilder);
\r
559 return new MemberList (members);
\r
562 public override MemberCache MemberCache {
\r
568 public Expression InstanceExpression {
\r
570 return instance_expr;
\r
573 instance_expr = value;
\r
577 public MethodBase TargetMethod {
\r
579 return delegate_method;
\r
582 delegate_method = value;
\r
586 public Type TargetReturnType {
\r
592 public Type [] ParameterTypes {
\r
594 return param_types;
\r
601 // Base class for `NewDelegate' and `ImplicitDelegateCreation'
\r
603 public abstract class DelegateCreation : Expression {
\r
604 protected MethodBase constructor_method;
\r
605 protected MethodBase delegate_method;
\r
606 protected Expression delegate_instance_expr;
\r
608 public DelegateCreation () {}
\r
610 public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
\r
612 string method_desc;
\r
614 if (mg.Methods.Length > 1)
\r
615 method_desc = mg.Methods [0].Name;
\r
617 method_desc = Invocation.FullMethodDesc (mg.Methods [0]);
\r
619 Expression invoke_method = Expression.MemberLookup (
\r
620 ec, type, "Invoke", MemberTypes.Method,
\r
621 Expression.AllBindingFlags, loc);
\r
622 MethodBase method = ((MethodGroupExpr) invoke_method).Methods [0];
\r
623 ParameterData param = Invocation.GetParameterData (method);
\r
624 string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
\r
626 Report.Error (123, loc, "Method '" + method_desc + "' does not " +
\r
627 "match delegate '" + delegate_desc + "'");
\r
630 public override void Emit (EmitContext ec)
\r
632 if (delegate_instance_expr == null ||
\r
633 delegate_method.IsStatic)
\r
634 ec.ig.Emit (OpCodes.Ldnull);
\r
636 delegate_instance_expr.Emit (ec);
\r
638 if (delegate_method.IsVirtual) {
\r
639 ec.ig.Emit (OpCodes.Dup);
\r
640 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
\r
642 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
\r
643 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
\r
646 protected bool ResolveConstructorMethod (EmitContext ec)
\r
648 Expression ml = Expression.MemberLookup (
\r
649 ec, type, ".ctor", loc);
\r
651 if (!(ml is MethodGroupExpr)) {
\r
652 Report.Error (-100, loc, "Internal error: Could not find delegate constructor!");
\r
656 constructor_method = ((MethodGroupExpr) ml).Methods [0];
\r
660 protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
\r
662 foreach (MethodInfo mi in mg.Methods){
\r
663 delegate_method = Delegate.VerifyMethod (ec, type, mi, loc);
\r
665 if (delegate_method != null)
\r
669 if (delegate_method == null) {
\r
670 Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
\r
675 // Check safe/unsafe of the delegate
\r
678 ParameterData param = Invocation.GetParameterData (delegate_method);
\r
679 int count = param.Count;
\r
681 for (int i = 0; i < count; i++){
\r
682 if (param.ParameterType (i).IsPointer){
\r
683 Expression.UnsafeError (loc);
\r
689 if (mg.InstanceExpression != null)
\r
690 delegate_instance_expr = mg.InstanceExpression.Resolve (ec);
\r
693 if (!delegate_method.IsStatic){
\r
694 Report.Error (120, loc,
\r
695 "An object reference is required for the non-static method " +
\r
696 delegate_method.Name);
\r
699 delegate_instance_expr = null;
\r
701 delegate_instance_expr = ec.GetThis (loc);
\r
704 if (delegate_instance_expr != null)
\r
705 if (delegate_instance_expr.Type.IsValueType)
\r
706 delegate_instance_expr = new BoxedCast (delegate_instance_expr);
\r
708 eclass = ExprClass.Value;
\r
714 // Created from the conversion code
\r
716 public class ImplicitDelegateCreation : DelegateCreation {
\r
718 ImplicitDelegateCreation (Type t, Location l)
\r
724 public override Expression DoResolve (EmitContext ec)
\r
729 static public Expression Create (EmitContext ec, MethodGroupExpr mge, Type target_type, Location loc)
\r
731 ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
\r
732 if (d.ResolveConstructorMethod (ec))
\r
733 return d.ResolveMethodGroupExpr (ec, mge);
\r
740 // A delegate-creation-expression, invoked from the `New' class
\r
742 public class NewDelegate : DelegateCreation {
\r
743 public ArrayList Arguments;
\r
746 // This constructor is invoked from the `New' expression
\r
748 public NewDelegate (Type type, ArrayList Arguments, Location loc)
\r
751 this.Arguments = Arguments;
\r
755 public override Expression DoResolve (EmitContext ec)
\r
757 if (Arguments == null || Arguments.Count != 1) {
\r
758 Report.Error (149, loc,
\r
759 "Method name expected");
\r
763 if (!ResolveConstructorMethod (ec))
\r
766 Argument a = (Argument) Arguments [0];
\r
768 Expression invoke_method = Expression.MemberLookup (
\r
769 ec, type, "Invoke", MemberTypes.Method,
\r
770 Expression.AllBindingFlags, loc);
\r
772 if (invoke_method == null) {
\r
773 Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
\r
777 if (!a.ResolveMethodGroup (ec, loc))
\r
780 Expression e = a.Expr;
\r
782 MethodGroupExpr mg = e as MethodGroupExpr;
\r
784 return ResolveMethodGroupExpr (ec, mg);
\r
786 Type e_type = e.Type;
\r
788 if (!TypeManager.IsDelegateType (e_type)) {
\r
789 e.Error_UnexpectedKind ("method");
\r
793 // This is what MS' compiler reports. We could always choose
\r
794 // to be more verbose and actually give delegate-level specifics
\r
796 if (!Delegate.VerifyDelegate (ec, type, e_type, loc)) {
\r
797 Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " +
\r
798 "to type '" + type + "'");
\r
802 delegate_instance_expr = e;
\r
803 delegate_method = ((MethodGroupExpr) invoke_method).Methods [0];
\r
805 eclass = ExprClass.Value;
\r
809 public override void Emit (EmitContext ec)
\r
811 if (delegate_instance_expr == null || delegate_method.IsStatic)
\r
812 ec.ig.Emit (OpCodes.Ldnull);
\r
814 delegate_instance_expr.Emit (ec);
\r
816 if (delegate_method.IsVirtual) {
\r
817 ec.ig.Emit (OpCodes.Dup);
\r
818 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
\r
820 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
\r
821 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
\r
825 public class DelegateInvocation : ExpressionStatement {
\r
827 public Expression InstanceExpr;
\r
828 public ArrayList Arguments;
\r
832 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
\r
834 this.InstanceExpr = instance_expr;
\r
835 this.Arguments = args;
\r
839 public override Expression DoResolve (EmitContext ec)
\r
841 if (InstanceExpr is EventExpr) {
\r
843 EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
\r
845 Expression ml = MemberLookup (
\r
846 ec, ec.ContainerType, ei.Name,
\r
847 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
\r
851 // If this is the case, then the Event does not belong
\r
852 // to this Type and so, according to the spec
\r
853 // cannot be accessed directly
\r
855 // Note that target will not appear as an EventExpr
\r
856 // in the case it is being referenced within the same type container;
\r
857 // it will appear as a FieldExpr in that case.
\r
860 Assign.error70 (ei, loc);
\r
866 Type del_type = InstanceExpr.Type;
\r
867 if (del_type == null)
\r
870 if (Arguments != null){
\r
871 foreach (Argument a in Arguments){
\r
872 if (!a.Resolve (ec, loc))
\r
877 if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
\r
880 Expression lookup = Expression.MemberLookup (ec, del_type, "Invoke", loc);
\r
881 if (!(lookup is MethodGroupExpr)) {
\r
882 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
\r
886 method = ((MethodGroupExpr) lookup).Methods [0];
\r
887 type = ((MethodInfo) method).ReturnType;
\r
888 eclass = ExprClass.Value;
\r
893 public override void Emit (EmitContext ec)
\r
896 // Invocation on delegates call the virtual Invoke member
\r
897 // so we are always `instance' calls
\r
899 Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, loc);
\r
902 public override void EmitStatement (EmitContext ec)
\r
906 // Pop the return value if there is one
\r
908 if (method is MethodInfo){
\r
909 if (((MethodInfo) method).ReturnType != TypeManager.void_type)
\r
910 ec.ig.Emit (OpCodes.Pop);
\r