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,
\r
384 MethodGroupExpr mg, MethodBase mb,
\r
387 Expression ml = Expression.MemberLookup (
\r
388 ec, delegate_type, "Invoke", loc);
\r
390 if (!(ml is MethodGroupExpr)) {
\r
391 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
\r
395 MethodBase invoke_mb = ((MethodGroupExpr) ml).Methods [0];
\r
396 ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
\r
398 if (!mg.HasTypeArguments &&
\r
399 !Invocation.InferTypeArguments (ec, invoke_pd, ref mb))
\r
402 ParameterData pd = Invocation.GetParameterData (mb);
\r
403 int pd_count = pd.Count;
\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 MethodBase candidate = mg.Methods [0];
\r
615 if (mg.Methods.Length > 1)
\r
616 method_desc = candidate.Name;
\r
618 method_desc = Invocation.FullMethodDesc (candidate);
\r
620 Expression invoke_method = Expression.MemberLookup (
\r
621 ec, type, "Invoke", MemberTypes.Method,
\r
622 Expression.AllBindingFlags, loc);
\r
623 MethodBase method = ((MethodGroupExpr) invoke_method).Methods [0];
\r
624 ParameterData param = Invocation.GetParameterData (method);
\r
625 string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
\r
627 if (!mg.HasTypeArguments &&
\r
628 !Invocation.InferTypeArguments (ec, param, ref candidate))
\r
629 Report.Error (411, loc, "The type arguments for " +
\r
630 "method `{0}' cannot be infered from " +
\r
631 "the usage. Try specifying the type " +
\r
632 "arguments explicitly.", method_desc);
\r
634 Report.Error (123, loc, "Method '{0}' does not " +
\r
635 "match delegate '{1}'", method_desc,
\r
639 public override void Emit (EmitContext ec)
\r
641 if (delegate_instance_expr == null ||
\r
642 delegate_method.IsStatic)
\r
643 ec.ig.Emit (OpCodes.Ldnull);
\r
645 delegate_instance_expr.Emit (ec);
\r
647 if (delegate_method.IsVirtual) {
\r
648 ec.ig.Emit (OpCodes.Dup);
\r
649 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
\r
651 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
\r
652 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
\r
655 protected bool ResolveConstructorMethod (EmitContext ec)
\r
657 Expression ml = Expression.MemberLookup (
\r
658 ec, type, ".ctor", loc);
\r
660 if (!(ml is MethodGroupExpr)) {
\r
661 Report.Error (-100, loc, "Internal error: Could not find delegate constructor!");
\r
665 constructor_method = ((MethodGroupExpr) ml).Methods [0];
\r
669 protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
\r
671 foreach (MethodInfo mi in mg.Methods){
\r
672 delegate_method = Delegate.VerifyMethod (
\r
673 ec, type, mg, mi, loc);
\r
675 if (delegate_method != null)
\r
679 if (delegate_method == null) {
\r
680 Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
\r
685 // Check safe/unsafe of the delegate
\r
688 ParameterData param = Invocation.GetParameterData (delegate_method);
\r
689 int count = param.Count;
\r
691 for (int i = 0; i < count; i++){
\r
692 if (param.ParameterType (i).IsPointer){
\r
693 Expression.UnsafeError (loc);
\r
699 if (mg.InstanceExpression != null)
\r
700 delegate_instance_expr = mg.InstanceExpression.Resolve (ec);
\r
703 if (!delegate_method.IsStatic){
\r
704 Report.Error (120, loc,
\r
705 "An object reference is required for the non-static method " +
\r
706 delegate_method.Name);
\r
709 delegate_instance_expr = null;
\r
711 delegate_instance_expr = ec.GetThis (loc);
\r
714 if (delegate_instance_expr != null)
\r
715 if (delegate_instance_expr.Type.IsValueType)
\r
716 delegate_instance_expr = new BoxedCast (delegate_instance_expr);
\r
718 eclass = ExprClass.Value;
\r
724 // Created from the conversion code
\r
726 public class ImplicitDelegateCreation : DelegateCreation {
\r
728 ImplicitDelegateCreation (Type t, Location l)
\r
734 public override Expression DoResolve (EmitContext ec)
\r
739 static public Expression Create (EmitContext ec, MethodGroupExpr mge, Type target_type, Location loc)
\r
741 ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
\r
742 if (d.ResolveConstructorMethod (ec))
\r
743 return d.ResolveMethodGroupExpr (ec, mge);
\r
750 // A delegate-creation-expression, invoked from the `New' class
\r
752 public class NewDelegate : DelegateCreation {
\r
753 public ArrayList Arguments;
\r
756 // This constructor is invoked from the `New' expression
\r
758 public NewDelegate (Type type, ArrayList Arguments, Location loc)
\r
761 this.Arguments = Arguments;
\r
765 public override Expression DoResolve (EmitContext ec)
\r
767 if (Arguments == null || Arguments.Count != 1) {
\r
768 Report.Error (149, loc,
\r
769 "Method name expected");
\r
773 if (!ResolveConstructorMethod (ec))
\r
776 Argument a = (Argument) Arguments [0];
\r
778 Expression invoke_method = Expression.MemberLookup (
\r
779 ec, type, "Invoke", MemberTypes.Method,
\r
780 Expression.AllBindingFlags, loc);
\r
782 if (invoke_method == null) {
\r
783 Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
\r
787 if (!a.ResolveMethodGroup (ec, loc))
\r
790 Expression e = a.Expr;
\r
792 MethodGroupExpr mg = e as MethodGroupExpr;
\r
794 return ResolveMethodGroupExpr (ec, mg);
\r
796 Type e_type = e.Type;
\r
798 if (!TypeManager.IsDelegateType (e_type)) {
\r
799 e.Error_UnexpectedKind ("method");
\r
803 // This is what MS' compiler reports. We could always choose
\r
804 // to be more verbose and actually give delegate-level specifics
\r
806 if (!Delegate.VerifyDelegate (ec, type, e_type, loc)) {
\r
807 Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " +
\r
808 "to type '" + type + "'");
\r
812 delegate_instance_expr = e;
\r
813 delegate_method = ((MethodGroupExpr) invoke_method).Methods [0];
\r
815 eclass = ExprClass.Value;
\r
819 public override void Emit (EmitContext ec)
\r
821 if (delegate_instance_expr == null || delegate_method.IsStatic)
\r
822 ec.ig.Emit (OpCodes.Ldnull);
\r
824 delegate_instance_expr.Emit (ec);
\r
826 if (delegate_method.IsVirtual) {
\r
827 ec.ig.Emit (OpCodes.Dup);
\r
828 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
\r
830 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
\r
831 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
\r
835 public class DelegateInvocation : ExpressionStatement {
\r
837 public Expression InstanceExpr;
\r
838 public ArrayList Arguments;
\r
842 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
\r
844 this.InstanceExpr = instance_expr;
\r
845 this.Arguments = args;
\r
849 public override Expression DoResolve (EmitContext ec)
\r
851 if (InstanceExpr is EventExpr) {
\r
853 EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
\r
855 Expression ml = MemberLookup (
\r
856 ec, ec.ContainerType, ei.Name,
\r
857 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
\r
861 // If this is the case, then the Event does not belong
\r
862 // to this Type and so, according to the spec
\r
863 // cannot be accessed directly
\r
865 // Note that target will not appear as an EventExpr
\r
866 // in the case it is being referenced within the same type container;
\r
867 // it will appear as a FieldExpr in that case.
\r
870 Assign.error70 (ei, loc);
\r
876 Type del_type = InstanceExpr.Type;
\r
877 if (del_type == null)
\r
880 if (Arguments != null){
\r
881 foreach (Argument a in Arguments){
\r
882 if (!a.Resolve (ec, loc))
\r
887 if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
\r
890 Expression lookup = Expression.MemberLookup (ec, del_type, "Invoke", loc);
\r
891 if (!(lookup is MethodGroupExpr)) {
\r
892 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
\r
896 method = ((MethodGroupExpr) lookup).Methods [0];
\r
897 type = ((MethodInfo) method).ReturnType;
\r
898 eclass = ExprClass.Value;
\r
903 public override void Emit (EmitContext ec)
\r
906 // Invocation on delegates call the virtual Invoke member
\r
907 // so we are always `instance' calls
\r
909 Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, loc);
\r
912 public override void EmitStatement (EmitContext ec)
\r
916 // Pop the return value if there is one
\r
918 if (method is MethodInfo){
\r
919 if (((MethodInfo) method).ReturnType != TypeManager.void_type)
\r
920 ec.ig.Emit (OpCodes.Pop);
\r