2 // delegate.cs: Delegate Handler
5 // Ravi Pratap (ravi@ximian.com)
6 // Miguel de Icaza (miguel@ximian.com)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
15 using System.Collections;
16 using System.Reflection;
17 using System.Reflection.Emit;
20 namespace Mono.CSharp {
25 public class Delegate : DeclSpace {
26 public Expression ReturnType;
27 public Parameters Parameters;
29 public ConstructorBuilder ConstructorBuilder;
30 public MethodBuilder InvokeBuilder;
31 public MethodBuilder BeginInvokeBuilder;
32 public MethodBuilder EndInvokeBuilder;
37 static string[] attribute_targets = new string [] { "type", "return" };
39 Expression instance_expr;
40 MethodBase delegate_method;
41 ReturnParameter return_attributes;
43 const int AllowedModifiers =
51 public Delegate (NamespaceEntry ns, TypeContainer parent, Expression type,
52 int mod_flags, MemberName name, Parameters param_list,
53 Attributes attrs, Location l)
54 : base (ns, parent, name, attrs, l)
57 this.ReturnType = type;
58 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags,
59 IsTopLevel ? Modifiers.INTERNAL :
60 Modifiers.PRIVATE, l);
61 Parameters = param_list;
64 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
66 if (a.Target == "return") {
67 if (return_attributes == null)
68 return_attributes = new ReturnParameter (InvokeBuilder, Location);
70 return_attributes.ApplyAttributeBuilder (a, cb);
74 base.ApplyAttributeBuilder (a, cb);
77 public override TypeBuilder DefineType ()
79 if (TypeBuilder != null)
83 foreach (TypeParameter type_param in TypeParameters)
84 if (!type_param.Resolve (this))
88 TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel) |
89 TypeAttributes.Class | TypeAttributes.Sealed;
92 if (TypeManager.NamespaceClash (Name, Location))
95 ModuleBuilder builder = CodeGen.Module.Builder;
97 TypeBuilder = builder.DefineType (
98 Name, attr, TypeManager.multicast_delegate_type);
100 TypeBuilder builder = Parent.TypeBuilder;
102 string name = Name.Substring (1 + Name.LastIndexOf ('.'));
103 TypeBuilder = builder.DefineNestedType (
104 name, attr, TypeManager.multicast_delegate_type);
107 TypeManager.AddDelegateType (Name, TypeBuilder, this);
110 CurrentType = new ConstructedType (
111 Name, TypeParameters, Location);
113 EmitContext ec = new EmitContext (
114 this, this, Location, null, null, ModFlags, false);
116 string[] param_names = new string [TypeParameters.Length];
117 for (int i = 0; i < TypeParameters.Length; i++)
118 param_names [i] = TypeParameters [i].Name;
120 GenericTypeParameterBuilder[] gen_params;
122 gen_params = TypeBuilder.DefineGenericParameters (param_names);
124 for (int i = 0; i < gen_params.Length; i++)
125 TypeParameters [i].Define (gen_params [i]);
127 foreach (TypeParameter type_param in TypeParameters) {
128 if (!type_param.DefineType (ec))
136 public override bool DefineMembers (TypeContainer container)
141 public override bool Define (TypeContainer container)
143 MethodAttributes mattr;
145 EmitContext ec = new EmitContext (this, this, Location, null,
146 null, ModFlags, false);
149 foreach (TypeParameter type_param in TypeParameters)
150 type_param.DefineType (ec);
153 // FIXME: POSSIBLY make this static, as it is always constant
155 Type [] const_arg_types = new Type [2];
156 const_arg_types [0] = TypeManager.object_type;
157 const_arg_types [1] = TypeManager.intptr_type;
159 mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
160 MethodAttributes.HideBySig | MethodAttributes.Public;
162 ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
163 CallingConventions.Standard,
166 ConstructorBuilder.DefineParameter (1, ParameterAttributes.None, "object");
167 ConstructorBuilder.DefineParameter (2, ParameterAttributes.None, "method");
169 // HACK because System.Reflection.Emit is lame
172 // FIXME: POSSIBLY make these static, as they are always the same
173 Parameter [] fixed_pars = new Parameter [2];
174 fixed_pars [0] = new Parameter (null, null, Parameter.Modifier.NONE, null);
175 fixed_pars [1] = new Parameter (null, null, Parameter.Modifier.NONE, null);
176 Parameters const_parameters = new Parameters (fixed_pars, null, Location);
178 TypeManager.RegisterMethod (
180 new InternalParameters (const_arg_types, const_parameters),
184 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
187 // Here the various methods like Invoke, BeginInvoke etc are defined
189 // First, call the `out of band' special method for
190 // defining recursively any types we need:
192 if (!Parameters.ComputeAndDefineParameterTypes (this))
195 param_types = Parameters.GetParameterInfo (this);
196 if (param_types == null)
203 // Check accessibility
204 foreach (Type partype in param_types){
205 if (!container.AsAccessible (partype, ModFlags)) {
206 Report.Error (59, Location,
207 "Inconsistent accessibility: parameter type `" +
208 TypeManager.CSharpName (partype) + "` is less " +
209 "accessible than delegate `" + Name + "'");
212 if (partype.IsPointer && !UnsafeOK (container))
216 ReturnType = ResolveTypeExpr (ReturnType, false, Location);
217 if (ReturnType == null)
220 ret_type = ReturnType.Type;
221 if (ret_type == null)
224 if (!container.AsAccessible (ret_type, ModFlags)) {
225 Report.Error (58, Location,
226 "Inconsistent accessibility: return type `" +
227 TypeManager.CSharpName (ret_type) + "` is less " +
228 "accessible than delegate `" + Name + "'");
232 if (ret_type.IsPointer && !UnsafeOK (container))
236 // We don't have to check any others because they are all
237 // guaranteed to be accessible - they are standard types.
240 CallingConventions cc = Parameters.GetCallingConvention ();
242 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
244 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
251 // Define parameters, and count out/ref parameters
255 if (Parameters.FixedParameters != null){
256 int top = Parameters.FixedParameters.Length;
259 for (; i < top; i++) {
260 p = Parameters.FixedParameters [i];
261 p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
263 if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
267 if (Parameters.ArrayParameter != null){
268 Parameter p = Parameters.ArrayParameter;
269 p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
272 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
274 TypeManager.RegisterMethod (InvokeBuilder,
275 new InternalParameters (container, Parameters),
281 int params_num = param_types.Length;
282 Type [] async_param_types = new Type [params_num + 2];
284 param_types.CopyTo (async_param_types, 0);
286 async_param_types [params_num] = TypeManager.asynccallback_type;
287 async_param_types [params_num + 1] = TypeManager.object_type;
289 mattr = MethodAttributes.Public | MethodAttributes.HideBySig |
290 MethodAttributes.Virtual | MethodAttributes.NewSlot;
292 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
295 TypeManager.iasyncresult_type,
299 if (Parameters.FixedParameters != null){
300 int top = Parameters.FixedParameters.Length;
303 for (i = 0 ; i < top; i++) {
304 p = Parameters.FixedParameters [i];
306 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
309 if (Parameters.ArrayParameter != null){
310 Parameter p = Parameters.ArrayParameter;
311 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
316 BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
317 BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
319 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
321 Parameter [] async_params = new Parameter [params_num + 2];
323 if (Parameters.FixedParameters != null){
324 Parameters.FixedParameters.CopyTo (async_params, 0);
325 n = Parameters.FixedParameters.Length;
327 if (Parameters.ArrayParameter != null)
328 async_params [n] = Parameters.ArrayParameter;
330 async_params [params_num] = new Parameter (
331 TypeManager.system_asynccallback_expr, "callback",
332 Parameter.Modifier.NONE, null);
333 async_params [params_num + 1] = new Parameter (
334 TypeManager.system_object_expr, "object",
335 Parameter.Modifier.NONE, null);
337 Parameters async_parameters = new Parameters (async_params, null, Location);
338 async_parameters.ComputeAndDefineParameterTypes (this);
340 async_parameters.ComputeAndDefineParameterTypes (this);
341 TypeManager.RegisterMethod (BeginInvokeBuilder,
342 new InternalParameters (container, async_parameters),
346 // EndInvoke is a bit more interesting, all the parameters labeled as
347 // out or ref have to be duplicated here.
350 Type [] end_param_types = new Type [out_params + 1];
351 Parameter [] end_params = new Parameter [out_params + 1];
354 int top = Parameters.FixedParameters.Length;
355 for (i = 0; i < top; i++){
356 Parameter p = Parameters.FixedParameters [i];
357 if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
360 end_param_types [param] = param_types [i];
361 end_params [param] = p;
365 end_param_types [out_params] = TypeManager.iasyncresult_type;
366 end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null);
369 // Create method, define parameters, register parameters with type system
371 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_param_types);
372 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
375 // EndInvoke: Label the parameters
377 EndInvokeBuilder.DefineParameter (out_params + 1, ParameterAttributes.None, "result");
378 for (i = 0; i < end_params.Length-1; i++){
379 EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
382 Parameters end_parameters = new Parameters (end_params, null, Location);
383 end_parameters.ComputeAndDefineParameterTypes (this);
385 TypeManager.RegisterMethod (
387 new InternalParameters (container, end_parameters),
393 public override void Emit (TypeContainer tc)
395 if (OptAttributes != null) {
396 EmitContext ec = new EmitContext (tc, this, Location, null, null, ModFlags, false);
397 Parameters.LabelParameters (ec, InvokeBuilder, Location);
398 OptAttributes.Emit (ec, this);
404 protected override string[] ValidAttributeTargets {
406 return attribute_targets;
411 protected override bool VerifyClsCompliance (DeclSpace ds)
413 if (!base.VerifyClsCompliance (ds)) {
417 AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
419 if (!AttributeTester.IsClsCompliant (ReturnType.Type)) {
420 Report.Error_T (3002, Location, GetSignatureForError ());
426 /// Verifies whether the method in question is compatible with the delegate
427 /// Returns the method itself if okay and null if not.
429 public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb,
432 Expression ml = Expression.MemberLookup (
433 ec, delegate_type, "Invoke", loc);
435 if (!(ml is MethodGroupExpr)) {
436 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
440 MethodBase invoke_mb = ((MethodGroupExpr) ml).Methods [0];
442 ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
444 if (!((MethodGroupExpr) ml).HasTypeArguments &&
445 !Invocation.InferTypeArguments (ec, invoke_pd, ref mb))
448 ParameterData pd = Invocation.GetParameterData (mb);
449 int pd_count = pd.Count;
451 if (invoke_pd.Count != pd_count)
454 for (int i = pd_count; i > 0; ) {
457 if (invoke_pd.ParameterType (i) == pd.ParameterType (i) &&
458 invoke_pd.ParameterModifier (i) == pd.ParameterModifier (i))
465 if (((MethodInfo) invoke_mb).ReturnType == ((MethodInfo) mb).ReturnType)
472 // Verifies whether the invocation arguments are compatible with the
473 // delegate's target method
475 public static bool VerifyApplicability (EmitContext ec,
485 arg_count = args.Count;
487 Expression ml = Expression.MemberLookup (
488 ec, delegate_type, "Invoke", loc);
490 if (!(ml is MethodGroupExpr)) {
491 Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
495 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
496 ParameterData pd = Invocation.GetParameterData (mb);
498 int pd_count = pd.Count;
500 bool params_method = (pd_count != 0) &&
501 (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS);
503 if (!params_method && pd_count != arg_count) {
504 Report.Error (1593, loc,
505 "Delegate '" + delegate_type.ToString ()
506 + "' does not take '" + arg_count + "' arguments");
511 // Consider the case:
512 // delegate void FOO(param object[] args);
513 // FOO f = new FOO(...);
514 // f(new object[] {1, 2, 3});
516 // This should be treated like f(1,2,3). This is done by ignoring the
517 // 'param' modifier for that invocation. If that fails, then the
518 // 'param' modifier is considered.
520 // One issue is that 'VerifyArgumentsCompat' modifies the elements of
521 // the 'args' array. However, the modifications appear idempotent.
522 // Normal 'Invocation's also have the same behaviour, implicitly.
526 if (arg_count == pd_count)
527 ans = Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, false, delegate_type, loc);
528 if (!ans && params_method)
529 ans = Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, true, delegate_type, loc);
534 /// Verifies whether the delegate in question is compatible with this one in
535 /// order to determine if instantiation from the same is possible.
537 public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Type probe_type, Location loc)
539 Expression ml = Expression.MemberLookup (
540 ec, delegate_type, "Invoke", loc);
542 if (!(ml is MethodGroupExpr)) {
543 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
547 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
548 ParameterData pd = Invocation.GetParameterData (mb);
550 Expression probe_ml = Expression.MemberLookup (
551 ec, delegate_type, "Invoke", loc);
553 if (!(probe_ml is MethodGroupExpr)) {
554 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
558 MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];
559 ParameterData probe_pd = Invocation.GetParameterData (probe_mb);
561 if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)
564 if (pd.Count != probe_pd.Count)
567 for (int i = pd.Count; i > 0; ) {
570 if (pd.ParameterType (i) != probe_pd.ParameterType (i) ||
571 pd.ParameterModifier (i) != probe_pd.ParameterModifier (i))
578 public static string FullDelegateDesc (Type del_type, MethodBase mb, ParameterData pd)
580 StringBuilder sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
582 sb.Append (" " + del_type.ToString ());
585 int length = pd.Count;
587 for (int i = length; i > 0; ) {
590 sb.Append (pd.ParameterDesc (length - i - 1));
596 return sb.ToString ();
600 // Hack around System.Reflection as found everywhere else
601 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
602 MemberFilter filter, object criteria)
604 ArrayList members = new ArrayList ();
606 if ((mt & MemberTypes.Method) != 0) {
607 if (ConstructorBuilder != null)
608 if (filter (ConstructorBuilder, criteria))
609 members.Add (ConstructorBuilder);
611 if (InvokeBuilder != null)
612 if (filter (InvokeBuilder, criteria))
613 members.Add (InvokeBuilder);
615 if (BeginInvokeBuilder != null)
616 if (filter (BeginInvokeBuilder, criteria))
617 members.Add (BeginInvokeBuilder);
619 if (EndInvokeBuilder != null)
620 if (filter (EndInvokeBuilder, criteria))
621 members.Add (EndInvokeBuilder);
624 return new MemberList (members);
627 public override MemberCache MemberCache {
633 public Expression InstanceExpression {
635 return instance_expr;
638 instance_expr = value;
642 public MethodBase TargetMethod {
644 return delegate_method;
647 delegate_method = value;
651 public Type TargetReturnType {
657 public Type [] ParameterTypes {
663 public override AttributeTargets AttributeTargets {
665 return AttributeTargets.Delegate | AttributeTargets.ReturnValue;
669 protected override void VerifyObsoleteAttribute()
671 CheckUsageOfObsoleteAttribute (ret_type);
673 foreach (Type type in param_types) {
674 CheckUsageOfObsoleteAttribute (type);
680 // Base class for `NewDelegate' and `ImplicitDelegateCreation'
682 public abstract class DelegateCreation : Expression {
683 protected MethodBase constructor_method;
684 protected MethodBase delegate_method;
685 protected Expression delegate_instance_expr;
687 public DelegateCreation () {}
689 public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
693 MethodBase candidate = mg.Methods [0];
694 if (mg.Methods.Length > 1)
695 method_desc = candidate.Name;
697 method_desc = Invocation.FullMethodDesc (candidate);
699 Expression invoke_method = Expression.MemberLookup (
700 ec, type, "Invoke", MemberTypes.Method,
701 Expression.AllBindingFlags, loc);
702 MethodBase method = ((MethodGroupExpr) invoke_method).Methods [0];
703 ParameterData param = Invocation.GetParameterData (method);
704 string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
706 if (!mg.HasTypeArguments &&
707 !Invocation.InferTypeArguments (ec, param, ref candidate))
708 Report.Error (411, loc, "The type arguments for " +
709 "method `{0}' cannot be infered from " +
710 "the usage. Try specifying the type " +
711 "arguments explicitly.", method_desc);
713 Report.Error (123, loc, "Method '{0}' does not " +
714 "match delegate '{1}'", method_desc,
718 public override void Emit (EmitContext ec)
720 if (delegate_instance_expr == null ||
721 delegate_method.IsStatic)
722 ec.ig.Emit (OpCodes.Ldnull);
724 delegate_instance_expr.Emit (ec);
726 if (delegate_method.IsVirtual) {
727 ec.ig.Emit (OpCodes.Dup);
728 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
730 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
731 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
734 protected bool ResolveConstructorMethod (EmitContext ec)
736 Expression ml = Expression.MemberLookup (
737 ec, type, ".ctor", loc);
739 if (!(ml is MethodGroupExpr)) {
740 Report.Error (-100, loc, "Internal error: Could not find delegate constructor!");
744 constructor_method = ((MethodGroupExpr) ml).Methods [0];
748 protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
750 foreach (MethodInfo mi in mg.Methods){
751 delegate_method = Delegate.VerifyMethod (
754 if (delegate_method != null)
758 if (delegate_method == null) {
759 Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
764 // Check safe/unsafe of the delegate
767 ParameterData param = Invocation.GetParameterData (delegate_method);
768 int count = param.Count;
770 for (int i = 0; i < count; i++){
771 if (param.ParameterType (i).IsPointer){
772 Expression.UnsafeError (loc);
778 if (mg.InstanceExpression != null)
779 delegate_instance_expr = mg.InstanceExpression.Resolve (ec);
782 if (!delegate_method.IsStatic){
783 Report.Error (120, loc,
784 "An object reference is required for the non-static method " +
785 delegate_method.Name);
788 delegate_instance_expr = null;
790 delegate_instance_expr = ec.GetThis (loc);
793 if (delegate_instance_expr != null)
794 if (delegate_instance_expr.Type.IsValueType)
795 delegate_instance_expr = new BoxedCast (delegate_instance_expr);
797 eclass = ExprClass.Value;
803 // Created from the conversion code
805 public class ImplicitDelegateCreation : DelegateCreation {
807 ImplicitDelegateCreation (Type t, Location l)
813 public override Expression DoResolve (EmitContext ec)
818 static public Expression Create (EmitContext ec, MethodGroupExpr mge, Type target_type, Location loc)
820 ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
821 if (d.ResolveConstructorMethod (ec))
822 return d.ResolveMethodGroupExpr (ec, mge);
829 // A delegate-creation-expression, invoked from the `New' class
831 public class NewDelegate : DelegateCreation {
832 public ArrayList Arguments;
835 // This constructor is invoked from the `New' expression
837 public NewDelegate (Type type, ArrayList Arguments, Location loc)
840 this.Arguments = Arguments;
844 public override Expression DoResolve (EmitContext ec)
846 if (Arguments == null || Arguments.Count != 1) {
847 Report.Error (149, loc,
848 "Method name expected");
852 if (!ResolveConstructorMethod (ec))
855 Argument a = (Argument) Arguments [0];
857 Expression invoke_method = Expression.MemberLookup (
858 ec, type, "Invoke", MemberTypes.Method,
859 Expression.AllBindingFlags, loc);
861 if (invoke_method == null) {
862 Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
866 if (!a.ResolveMethodGroup (ec, loc))
869 Expression e = a.Expr;
871 MethodGroupExpr mg = e as MethodGroupExpr;
873 return ResolveMethodGroupExpr (ec, mg);
875 Type e_type = e.Type;
877 if (!TypeManager.IsDelegateType (e_type)) {
878 e.Error_UnexpectedKind ("method");
882 // This is what MS' compiler reports. We could always choose
883 // to be more verbose and actually give delegate-level specifics
885 if (!Delegate.VerifyDelegate (ec, type, e_type, loc)) {
886 Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " +
887 "to type '" + type + "'");
891 delegate_instance_expr = e;
892 delegate_method = ((MethodGroupExpr) invoke_method).Methods [0];
894 eclass = ExprClass.Value;
898 public override void Emit (EmitContext ec)
900 if (delegate_instance_expr == null || delegate_method.IsStatic)
901 ec.ig.Emit (OpCodes.Ldnull);
903 delegate_instance_expr.Emit (ec);
905 if (delegate_method.IsVirtual) {
906 ec.ig.Emit (OpCodes.Dup);
907 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
909 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
910 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
914 public class DelegateInvocation : ExpressionStatement {
916 public Expression InstanceExpr;
917 public ArrayList Arguments;
921 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
923 this.InstanceExpr = instance_expr;
924 this.Arguments = args;
928 public override Expression DoResolve (EmitContext ec)
930 if (InstanceExpr is EventExpr) {
932 EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
934 Expression ml = MemberLookup (
935 ec, ec.ContainerType, ei.Name,
936 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
940 // If this is the case, then the Event does not belong
941 // to this Type and so, according to the spec
942 // cannot be accessed directly
944 // Note that target will not appear as an EventExpr
945 // in the case it is being referenced within the same type container;
946 // it will appear as a FieldExpr in that case.
949 Assign.error70 (ei, loc);
955 Type del_type = InstanceExpr.Type;
956 if (del_type == null)
959 if (Arguments != null){
960 foreach (Argument a in Arguments){
961 if (!a.Resolve (ec, loc))
966 if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
969 Expression lookup = Expression.MemberLookup (ec, del_type, "Invoke", loc);
970 if (!(lookup is MethodGroupExpr)) {
971 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
975 method = ((MethodGroupExpr) lookup).Methods [0];
976 type = ((MethodInfo) method).ReturnType;
977 eclass = ExprClass.Value;
982 public override void Emit (EmitContext ec)
985 // Invocation on delegates call the virtual Invoke member
986 // so we are always `instance' calls
988 Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, loc);
991 public override void EmitStatement (EmitContext ec)
995 // Pop the return value if there is one
997 if (method is MethodInfo){
998 if (((MethodInfo) method).ReturnType != TypeManager.void_type)
999 ec.ig.Emit (OpCodes.Pop);