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;
36 static string[] attribute_targets = new string [] { "type", "return" };
38 Expression instance_expr;
39 MethodBase delegate_method;
40 ReturnParameter return_attributes;
42 const int AllowedModifiers =
50 public Delegate (NamespaceEntry ns, DeclSpace parent, Expression type,
51 int mod_flags, MemberName name, Parameters param_list,
53 : base (ns, parent, name, attrs)
56 this.ReturnType = type;
57 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags,
58 IsTopLevel ? Modifiers.INTERNAL :
59 Modifiers.PRIVATE, name.Location);
60 Parameters = param_list;
63 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
65 if (a.Target == AttributeTargets.ReturnValue) {
66 if (return_attributes == null)
67 return_attributes = new ReturnParameter (InvokeBuilder, Location);
69 return_attributes.ApplyAttributeBuilder (a, cb);
73 base.ApplyAttributeBuilder (a, cb);
76 public override TypeBuilder DefineType ()
78 if (TypeBuilder != null)
82 foreach (TypeParameter type_param in TypeParameters)
83 if (!type_param.Resolve (this))
87 if (TypeManager.multicast_delegate_type == null && !RootContext.StdLib) {
88 Namespace system = RootNamespace.Global.GetNamespace ("System", true);
89 TypeExpr expr = system.Lookup (this, "MulticastDelegate", Location) as TypeExpr;
90 TypeManager.multicast_delegate_type = expr.Type;
93 if (TypeManager.multicast_delegate_type == null)
94 Report.Error (-100, Location, "Internal error: delegate used before " +
95 "System.MulticastDelegate is resolved. This can only " +
96 "happen during corlib compilation, when using a delegate " +
97 "in any of the `core' classes. See bug #72015 for details.");
100 if (TypeManager.NamespaceClash (Name, Location))
103 ModuleBuilder builder = CodeGen.Module.Builder;
105 TypeBuilder = builder.DefineType (
106 Name, TypeAttr, TypeManager.multicast_delegate_type);
108 TypeBuilder builder = Parent.TypeBuilder;
110 string name = Name.Substring (1 + Name.LastIndexOf ('.'));
111 TypeBuilder = builder.DefineNestedType (
112 name, TypeAttr, TypeManager.multicast_delegate_type);
115 TypeManager.AddUserType (this);
118 string[] param_names = new string [TypeParameters.Length];
119 for (int i = 0; i < TypeParameters.Length; i++)
120 param_names [i] = TypeParameters [i].Name;
122 GenericTypeParameterBuilder[] gen_params;
123 gen_params = TypeBuilder.DefineGenericParameters (param_names);
125 int offset = CountTypeParameters - CurrentTypeParameters.Length;
126 for (int i = offset; i < gen_params.Length; i++)
127 CurrentTypeParameters [i - offset].Define (gen_params [i]);
129 foreach (TypeParameter type_param in CurrentTypeParameters) {
130 if (!type_param.Resolve (this))
134 Expression current = new SimpleName (
135 MemberName.Basename, TypeParameters, Location);
136 current = current.ResolveAsTypeTerminal (this, false);
140 CurrentType = current.Type;
146 public override bool Define ()
149 foreach (TypeParameter type_param in TypeParameters)
150 type_param.DefineType (this);
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 const MethodAttributes ctor_mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
160 MethodAttributes.HideBySig | MethodAttributes.Public;
162 ConstructorBuilder = TypeBuilder.DefineConstructor (ctor_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
171 Parameter [] fixed_pars = new Parameter [2];
172 fixed_pars [0] = new Parameter (TypeManager.object_type, "object",
173 Parameter.Modifier.NONE, null, Location);
174 fixed_pars [1] = new Parameter (TypeManager.intptr_type, "method",
175 Parameter.Modifier.NONE, null, Location);
176 Parameters const_parameters = new Parameters (fixed_pars);
177 const_parameters.Resolve (null);
179 TypeManager.RegisterMethod (ConstructorBuilder, const_parameters);
182 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
185 // Here the various methods like Invoke, BeginInvoke etc are defined
187 // First, call the `out of band' special method for
188 // defining recursively any types we need:
190 if (!Parameters.Resolve (this))
197 // Check accessibility
198 foreach (Type partype in Parameters.Types){
199 if (!Parent.AsAccessible (partype, ModFlags)) {
200 Report.Error (59, Location,
201 "Inconsistent accessibility: parameter type `" +
202 TypeManager.CSharpName (partype) + "' is less " +
203 "accessible than delegate `" + Name + "'");
208 ReturnType = ReturnType.ResolveAsTypeTerminal (this, false);
209 if (ReturnType == null)
212 ret_type = ReturnType.Type;
214 if (!Parent.AsAccessible (ret_type, ModFlags)) {
215 Report.Error (58, Location,
216 "Inconsistent accessibility: return type `" +
217 TypeManager.CSharpName (ret_type) + "' is less " +
218 "accessible than delegate `" + Name + "'");
222 if (RootContext.StdLib && (ret_type == TypeManager.arg_iterator_type || ret_type == TypeManager.typed_reference_type)) {
223 Method.Error1599 (Location, ret_type);
228 // We don't have to check any others because they are all
229 // guaranteed to be accessible - they are standard types.
232 CallingConventions cc = Parameters.CallingConvention;
234 const MethodAttributes mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.NewSlot;
236 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
242 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
244 TypeManager.RegisterMethod (InvokeBuilder, Parameters);
250 Parameters async_parameters = Parameters.MergeGenerated (Parameters,
251 new Parameter (TypeManager.asynccallback_type, "callback", Parameter.Modifier.NONE, null, Location),
252 new Parameter (TypeManager.object_type, "object", Parameter.Modifier.NONE, null, Location));
254 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
255 mattr, cc, TypeManager.iasyncresult_type, async_parameters.Types);
257 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
258 async_parameters.ApplyAttributes (BeginInvokeBuilder);
259 TypeManager.RegisterMethod (BeginInvokeBuilder, async_parameters);
262 // EndInvoke is a bit more interesting, all the parameters labeled as
263 // out or ref have to be duplicated here.
267 // Define parameters, and count out/ref parameters
269 Parameters end_parameters;
272 foreach (Parameter p in Parameters.FixedParameters) {
273 if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
277 if (out_params > 0) {
278 Type [] end_param_types = new Type [out_params];
279 Parameter [] end_params = new Parameter [out_params ];
282 for (int i = 0; i < Parameters.FixedParameters.Length; ++i) {
283 Parameter p = Parameters.FixedParameters [i];
284 if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
287 end_param_types [param] = p.ExternalType();
288 end_params [param] = p;
291 end_parameters = new Parameters (end_params, end_param_types);
294 end_parameters = Parameters.EmptyReadOnlyParameters;
297 end_parameters = Parameters.MergeGenerated (end_parameters,
298 new Parameter (TypeManager.iasyncresult_type, "result", Parameter.Modifier.NONE, null, Location));
301 // Create method, define parameters, register parameters with type system
303 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_parameters.Types);
304 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
306 end_parameters.ApplyAttributes (EndInvokeBuilder);
307 TypeManager.RegisterMethod (EndInvokeBuilder, end_parameters);
312 public override void Emit ()
314 Parameters.ApplyAttributes (InvokeBuilder);
316 if (OptAttributes != null) {
317 OptAttributes.Emit ();
323 protected override TypeAttributes TypeAttr {
325 return Modifiers.TypeAttr (ModFlags, IsTopLevel) |
326 TypeAttributes.Class | TypeAttributes.Sealed |
331 public override string[] ValidAttributeTargets {
333 return attribute_targets;
338 protected override bool VerifyClsCompliance ()
340 if (!base.VerifyClsCompliance ()) {
344 Parameters.VerifyClsCompliance ();
346 if (!AttributeTester.IsClsCompliant (ReturnType.Type)) {
347 Report.Error (3002, Location, "Return type of `{0}' is not CLS-compliant", GetSignatureForError ());
353 // Returns the MethodBase for "Invoke" from a delegate type, this is used
354 // to extract the signature of a delegate.
356 public static MethodGroupExpr GetInvokeMethod (Type container_type, Type delegate_type, Location loc)
358 Expression ml = Expression.MemberLookup (container_type, null, delegate_type,
361 MethodGroupExpr mg = ml as MethodGroupExpr;
363 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
371 /// Verifies whether the method in question is compatible with the delegate
372 /// Returns the method itself if okay and null if not.
374 public static MethodBase VerifyMethod (Type container_type, Type delegate_type,
375 MethodGroupExpr old_mg, MethodBase mb,
378 MethodGroupExpr mg = GetInvokeMethod (container_type, delegate_type, loc);
382 if (old_mg.HasTypeArguments)
383 mg.HasTypeArguments = true;
385 MethodBase invoke_mb = mg.Methods [0];
386 ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
388 if (!mg.HasTypeArguments &&
389 !TypeManager.InferTypeArguments (invoke_pd, ref mb))
392 ParameterData pd = TypeManager.GetParameterData (mb);
394 if (invoke_pd.Count != pd.Count)
397 for (int i = pd.Count; i > 0; ) {
400 Type invoke_pd_type = invoke_pd.ParameterType (i);
401 Type pd_type = pd.ParameterType (i);
402 Parameter.Modifier invoke_pd_type_mod = invoke_pd.ParameterModifier (i);
403 Parameter.Modifier pd_type_mod = pd.ParameterModifier (i);
405 invoke_pd_type_mod &= ~Parameter.Modifier.PARAMS;
406 pd_type_mod &= ~Parameter.Modifier.PARAMS;
408 if (invoke_pd_type == pd_type &&
409 invoke_pd_type_mod == pd_type_mod)
412 if (invoke_pd_type.IsSubclassOf (pd_type) &&
413 invoke_pd_type_mod == pd_type_mod)
414 if (RootContext.Version == LanguageVersion.ISO_1) {
415 Report.FeatureIsNotStandardized (loc, "contravariance");
423 Type invoke_mb_retval = ((MethodInfo) invoke_mb).ReturnType;
424 Type mb_retval = ((MethodInfo) mb).ReturnType;
425 if (invoke_mb_retval == mb_retval)
428 if (mb_retval.IsSubclassOf (invoke_mb_retval))
429 if (RootContext.Version == LanguageVersion.ISO_1) {
430 Report.FeatureIsNotStandardized (loc, "covariance");
440 // Verifies whether the invocation arguments are compatible with the
441 // delegate's target method
443 public static bool VerifyApplicability (EmitContext ec, Type delegate_type,
444 ArrayList args, Location loc)
451 arg_count = args.Count;
453 Expression ml = Expression.MemberLookup (
454 ec.ContainerType, delegate_type, "Invoke", loc);
456 MethodGroupExpr me = ml as MethodGroupExpr;
458 Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
462 MethodBase mb = me.Methods [0];
463 ParameterData pd = TypeManager.GetParameterData (mb);
465 int pd_count = pd.Count;
467 bool params_method = pd.HasParams;
468 bool is_params_applicable = false;
469 bool is_applicable = Invocation.IsApplicable (ec, me, args, arg_count, ref mb);
471 if (!is_applicable && params_method &&
472 Invocation.IsParamsMethodApplicable (ec, me, args, arg_count, ref mb))
473 is_applicable = is_params_applicable = true;
475 if (!is_applicable && !params_method && arg_count != pd_count) {
476 Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
477 TypeManager.CSharpName (delegate_type), arg_count.ToString ());
481 return Invocation.VerifyArgumentsCompat (
482 ec, args, arg_count, mb,
483 is_params_applicable || (!is_applicable && params_method),
484 delegate_type, false, loc);
488 /// Verifies whether the delegate in question is compatible with this one in
489 /// order to determine if instantiation from the same is possible.
491 public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Location loc)
493 Expression ml = Expression.MemberLookup (
494 ec.ContainerType, delegate_type, "Invoke", loc);
496 if (!(ml is MethodGroupExpr)) {
497 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
501 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
502 ParameterData pd = TypeManager.GetParameterData (mb);
504 Expression probe_ml = Expression.MemberLookup (
505 ec.ContainerType, delegate_type, "Invoke", loc);
507 if (!(probe_ml is MethodGroupExpr)) {
508 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
512 MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];
513 ParameterData probe_pd = TypeManager.GetParameterData (probe_mb);
515 if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)
518 if (pd.Count != probe_pd.Count)
521 for (int i = pd.Count; i > 0; ) {
524 if (pd.ParameterType (i) != probe_pd.ParameterType (i) ||
525 pd.ParameterModifier (i) != probe_pd.ParameterModifier (i))
532 public static string FullDelegateDesc (Type del_type, MethodBase mb, ParameterData pd)
534 StringBuilder sb = new StringBuilder ();
535 sb.Append (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
537 sb.Append (TypeManager.CSharpName (del_type));
538 sb.Append (pd.GetSignatureForError ());
539 return sb.ToString ();
542 // Hack around System.Reflection as found everywhere else
543 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
544 MemberFilter filter, object criteria)
546 ArrayList members = new ArrayList (2);
548 if ((mt & MemberTypes.Constructor) != 0) {
549 if (ConstructorBuilder != null && filter (ConstructorBuilder, criteria))
550 members.Add (ConstructorBuilder);
553 if ((mt & MemberTypes.Method) != 0) {
554 if (InvokeBuilder != null)
555 if (filter (InvokeBuilder, criteria))
556 members.Add (InvokeBuilder);
558 if (BeginInvokeBuilder != null)
559 if (filter (BeginInvokeBuilder, criteria))
560 members.Add (BeginInvokeBuilder);
562 if (EndInvokeBuilder != null)
563 if (filter (EndInvokeBuilder, criteria))
564 members.Add (EndInvokeBuilder);
567 return new MemberList (members);
570 public override MemberCache MemberCache {
576 public Expression InstanceExpression {
578 return instance_expr;
581 instance_expr = value;
585 public MethodBase TargetMethod {
587 return delegate_method;
590 delegate_method = value;
594 public Type TargetReturnType {
600 public override AttributeTargets AttributeTargets {
602 return AttributeTargets.Delegate;
607 // Represents header string for documentation comment.
609 public override string DocCommentHeader {
616 // Base class for `NewDelegate' and `ImplicitDelegateCreation'
618 public abstract class DelegateCreation : Expression {
619 protected MethodBase constructor_method;
620 protected MethodBase delegate_method;
621 protected MethodGroupExpr method_group;
622 protected Expression delegate_instance_expression;
624 protected DelegateCreation () {}
626 public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
629 MethodBase found_method = mg.Methods [0];
631 if (mg.Methods.Length > 1)
632 method_desc = found_method.Name;
634 method_desc = Invocation.FullMethodDesc (found_method);
636 Expression invoke_method = Expression.MemberLookup (
637 ec.ContainerType, type, "Invoke", MemberTypes.Method,
638 Expression.AllBindingFlags, loc);
639 MethodInfo method = ((MethodGroupExpr) invoke_method).Methods [0] as MethodInfo;
641 ParameterData param = TypeManager.GetParameterData (method);
642 string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
644 if (!mg.HasTypeArguments &&
645 !TypeManager.InferTypeArguments (param, ref found_method))
646 Report.Error (411, loc, "The type arguments for " +
647 "method `{0}' cannot be infered from " +
648 "the usage. Try specifying the type " +
649 "arguments explicitly.", method_desc);
650 else if (method.ReturnType != ((MethodInfo) found_method).ReturnType) {
651 Report.Error (407, loc, "`{0}' has the wrong return type to match the delegate `{1}'", method_desc, delegate_desc);
653 Report.Error (123, loc, "Method `{0}' does not match delegate `{1}'", method_desc, delegate_desc);
657 public override void Emit (EmitContext ec)
659 if (delegate_instance_expression == null || delegate_method.IsStatic)
660 ec.ig.Emit (OpCodes.Ldnull);
662 delegate_instance_expression.Emit (ec);
664 if (delegate_method.IsVirtual && !method_group.IsBase) {
665 ec.ig.Emit (OpCodes.Dup);
666 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
668 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
669 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
672 protected bool ResolveConstructorMethod (EmitContext ec)
674 Expression ml = Expression.MemberLookupFinal(ec,
675 null, type, ".ctor", MemberTypes.Constructor, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
677 if (!(ml is MethodGroupExpr)) {
678 Report.Error (-100, loc, "Internal error: Could not find delegate constructor!");
682 constructor_method = ((MethodGroupExpr) ml).Methods [0];
686 public static MethodBase ImplicitStandardConversionExists (MethodGroupExpr mg, Type targetType)
688 foreach (MethodInfo mi in mg.Methods){
689 MethodBase mb = Delegate.VerifyMethod (mg.DeclaringType, targetType, mg, mi, Location.Null);
696 protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
698 delegate_method = ImplicitStandardConversionExists (mg, type);
700 if (delegate_method == null) {
701 Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
706 // Check safe/unsafe of the delegate
709 ParameterData param = TypeManager.GetParameterData (delegate_method);
710 int count = param.Count;
712 for (int i = 0; i < count; i++){
713 if (param.ParameterType (i).IsPointer){
714 Expression.UnsafeError (loc);
720 //TODO: implement caching when performance will be low
721 IMethodData md = TypeManager.GetMethod (delegate_method);
723 if (System.Attribute.GetCustomAttribute (delegate_method, TypeManager.conditional_attribute_type) != null) {
724 Report.SymbolRelatedToPreviousError (delegate_method);
725 Report.Error (1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
729 md.SetMemberIsUsed ();
730 if (md.OptAttributes != null && md.OptAttributes.Search (TypeManager.conditional_attribute_type) != null) {
731 Report.SymbolRelatedToPreviousError (delegate_method);
732 Report.Error (1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
737 if (mg.InstanceExpression != null)
738 delegate_instance_expression = mg.InstanceExpression.Resolve (ec);
739 else if (ec.IsStatic) {
740 if (!delegate_method.IsStatic) {
741 Report.Error (120, loc, "`{0}': An object reference is required for the nonstatic field, method or property",
742 TypeManager.CSharpSignature (delegate_method));
745 delegate_instance_expression = null;
747 delegate_instance_expression = ec.GetThis (loc);
749 if (delegate_instance_expression != null && delegate_instance_expression.Type.IsValueType)
750 delegate_instance_expression = new BoxedCast (
751 delegate_instance_expression, TypeManager.object_type);
754 eclass = ExprClass.Value;
760 // Created from the conversion code
762 public class ImplicitDelegateCreation : DelegateCreation {
764 ImplicitDelegateCreation (Type t, Location l)
770 public override Expression DoResolve (EmitContext ec)
775 static public Expression Create (EmitContext ec, MethodGroupExpr mge,
776 Type target_type, Location loc)
778 ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
779 if (!d.ResolveConstructorMethod (ec))
782 return d.ResolveMethodGroupExpr (ec, mge);
787 // A delegate-creation-expression, invoked from the `New' class
789 public class NewDelegate : DelegateCreation {
790 public ArrayList Arguments;
793 // This constructor is invoked from the `New' expression
795 public NewDelegate (Type type, ArrayList Arguments, Location loc)
798 this.Arguments = Arguments;
802 public override Expression DoResolve (EmitContext ec)
804 if (Arguments == null || Arguments.Count != 1) {
805 Report.Error (149, loc,
806 "Method name expected");
810 if (!ResolveConstructorMethod (ec))
813 Argument a = (Argument) Arguments [0];
815 if (!a.ResolveMethodGroup (ec))
818 Expression e = a.Expr;
820 if (e is AnonymousMethod && RootContext.Version != LanguageVersion.ISO_1)
821 return ((AnonymousMethod) e).Compatible (ec, type);
823 MethodGroupExpr mg = e as MethodGroupExpr;
825 return ResolveMethodGroupExpr (ec, mg);
827 if (!TypeManager.IsDelegateType (e.Type)) {
828 Report.Error (149, loc, "Method name expected");
832 method_group = Expression.MemberLookup (
833 ec.ContainerType, type, "Invoke", MemberTypes.Method,
834 Expression.AllBindingFlags, loc) as MethodGroupExpr;
836 if (method_group == null) {
837 Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
841 // This is what MS' compiler reports. We could always choose
842 // to be more verbose and actually give delegate-level specifics
843 if (!Delegate.VerifyDelegate (ec, type, loc)) {
844 Report.Error (29, loc, "Cannot implicitly convert type '" + e.Type + "' " +
845 "to type '" + type + "'");
849 delegate_instance_expression = e;
850 delegate_method = method_group.Methods [0];
852 eclass = ExprClass.Value;
857 public class DelegateInvocation : ExpressionStatement {
859 public Expression InstanceExpr;
860 public ArrayList Arguments;
864 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
866 this.InstanceExpr = instance_expr;
867 this.Arguments = args;
871 public override Expression DoResolve (EmitContext ec)
873 if (InstanceExpr is EventExpr) {
875 EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
877 Expression ml = MemberLookup (
878 ec.ContainerType, ec.ContainerType, ei.Name,
879 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
883 // If this is the case, then the Event does not belong
884 // to this Type and so, according to the spec
885 // cannot be accessed directly
887 // Note that target will not appear as an EventExpr
888 // in the case it is being referenced within the same type container;
889 // it will appear as a FieldExpr in that case.
892 Assign.error70 (ei, loc);
898 Type del_type = InstanceExpr.Type;
899 if (del_type == null)
902 if (Arguments != null){
903 foreach (Argument a in Arguments){
904 if (!a.Resolve (ec, loc))
909 if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
912 Expression lookup = Expression.MemberLookup (ec.ContainerType, del_type, "Invoke", loc);
913 if (!(lookup is MethodGroupExpr)) {
914 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
918 method = ((MethodGroupExpr) lookup).Methods [0];
919 type = ((MethodInfo) method).ReturnType;
920 eclass = ExprClass.Value;
925 public override void Emit (EmitContext ec)
928 // Invocation on delegates call the virtual Invoke member
929 // so we are always `instance' calls
931 Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, loc);
934 public override void EmitStatement (EmitContext ec)
938 // Pop the return value if there is one
940 if (method is MethodInfo){
941 Type ret = ((MethodInfo)method).ReturnType;
942 if (TypeManager.TypeToCoreType (ret) != TypeManager.void_type)
943 ec.ig.Emit (OpCodes.Pop);