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 == AttributeTargets.ReturnValue) {
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)
82 ec = new EmitContext (this, this, Location, null, null, ModFlags, false);
84 TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel) |
85 TypeAttributes.Class | TypeAttributes.Sealed;
87 if (TypeManager.multicast_delegate_type == null)
88 Report.Error (-100, loc, "Internal error: delegate used before " +
89 "System.MulticastDelegate is resolved. This can only " +
90 "happen during corlib compilation, when using a delegate " +
91 "in any of the `core' classes. See bug #72015 for details.");
94 if (TypeManager.NamespaceClash (Name, Location))
97 ModuleBuilder builder = CodeGen.Module.Builder;
99 TypeBuilder = builder.DefineType (
100 Name, attr, TypeManager.multicast_delegate_type);
102 TypeBuilder builder = Parent.TypeBuilder;
104 string name = Name.Substring (1 + Name.LastIndexOf ('.'));
105 TypeBuilder = builder.DefineNestedType (
106 name, attr, TypeManager.multicast_delegate_type);
109 TypeManager.AddDelegateType (Name, TypeBuilder, this);
114 public override bool DefineMembers (TypeContainer container)
119 public override bool Define ()
121 MethodAttributes mattr;
123 ec = new EmitContext (this, this, Location, null, null, ModFlags, false);
125 // FIXME: POSSIBLY make this static, as it is always constant
127 Type [] const_arg_types = new Type [2];
128 const_arg_types [0] = TypeManager.object_type;
129 const_arg_types [1] = TypeManager.intptr_type;
131 mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
132 MethodAttributes.HideBySig | MethodAttributes.Public;
134 ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
135 CallingConventions.Standard,
138 ConstructorBuilder.DefineParameter (1, ParameterAttributes.None, "object");
139 ConstructorBuilder.DefineParameter (2, ParameterAttributes.None, "method");
141 // HACK because System.Reflection.Emit is lame
144 // FIXME: POSSIBLY make these static, as they are always the same
145 Parameter [] fixed_pars = new Parameter [2];
146 fixed_pars [0] = new Parameter (TypeManager.system_object_expr, "object",
147 Parameter.Modifier.NONE, null);
148 fixed_pars [1] = new Parameter (TypeManager.system_intptr_expr, "method",
149 Parameter.Modifier.NONE, null);
150 Parameters const_parameters = new Parameters (fixed_pars, null, Location);
152 TypeManager.RegisterMethod (
154 new InternalParameters (const_arg_types, const_parameters),
158 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
161 // Here the various methods like Invoke, BeginInvoke etc are defined
163 // First, call the `out of band' special method for
164 // defining recursively any types we need:
166 if (!Parameters.ComputeAndDefineParameterTypes (ec))
169 param_types = Parameters.GetParameterInfo (ec);
170 if (param_types == null)
177 // Check accessibility
178 foreach (Type partype in param_types){
179 if (!Parent.AsAccessible (partype, ModFlags)) {
180 Report.Error (59, Location,
181 "Inconsistent accessibility: parameter type `" +
182 TypeManager.CSharpName (partype) + "` is less " +
183 "accessible than delegate `" + Name + "'");
186 if (partype.IsPointer && !UnsafeOK (Parent))
190 ReturnType = ReturnType.ResolveAsTypeTerminal (ec, false);
191 if (ReturnType == null)
194 ret_type = ReturnType.Type;
195 if (ret_type == null)
198 if (!Parent.AsAccessible (ret_type, ModFlags)) {
199 Report.Error (58, Location,
200 "Inconsistent accessibility: return type `" +
201 TypeManager.CSharpName (ret_type) + "` is less " +
202 "accessible than delegate `" + Name + "'");
206 if (ret_type.IsPointer && !UnsafeOK (Parent))
210 // We don't have to check any others because they are all
211 // guaranteed to be accessible - they are standard types.
214 CallingConventions cc = Parameters.GetCallingConvention ();
216 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
218 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
225 // Define parameters, and count out/ref parameters
229 if (Parameters.FixedParameters != null){
230 int top = Parameters.FixedParameters.Length;
233 for (; i < top; i++) {
234 p = Parameters.FixedParameters [i];
235 p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
237 if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
241 if (Parameters.ArrayParameter != null){
242 Parameter p = Parameters.ArrayParameter;
243 p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
246 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
248 TypeManager.RegisterMethod (InvokeBuilder,
249 new InternalParameters (param_types, Parameters),
255 int params_num = param_types.Length;
256 Type [] async_param_types = new Type [params_num + 2];
258 param_types.CopyTo (async_param_types, 0);
260 async_param_types [params_num] = TypeManager.asynccallback_type;
261 async_param_types [params_num + 1] = TypeManager.object_type;
263 mattr = MethodAttributes.Public | MethodAttributes.HideBySig |
264 MethodAttributes.Virtual | MethodAttributes.NewSlot;
266 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
269 TypeManager.iasyncresult_type,
273 if (Parameters.FixedParameters != null){
274 int top = Parameters.FixedParameters.Length;
277 for (i = 0 ; i < top; i++) {
278 p = Parameters.FixedParameters [i];
280 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
283 if (Parameters.ArrayParameter != null){
284 Parameter p = Parameters.ArrayParameter;
285 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
290 BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
291 BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
293 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
295 Parameter [] async_params = new Parameter [params_num + 2];
297 if (Parameters.FixedParameters != null){
298 Parameters.FixedParameters.CopyTo (async_params, 0);
299 n = Parameters.FixedParameters.Length;
301 if (Parameters.ArrayParameter != null)
302 async_params [n] = Parameters.ArrayParameter;
304 async_params [params_num] = new Parameter (
305 TypeManager.system_asynccallback_expr, "callback",
306 Parameter.Modifier.NONE, null);
307 async_params [params_num + 1] = new Parameter (
308 TypeManager.system_object_expr, "object",
309 Parameter.Modifier.NONE, null);
311 Parameters async_parameters = new Parameters (async_params, null, Location);
312 async_parameters.ComputeAndDefineParameterTypes (ec);
314 TypeManager.RegisterMethod (BeginInvokeBuilder,
315 new InternalParameters (async_param_types, async_parameters),
319 // EndInvoke is a bit more interesting, all the parameters labeled as
320 // out or ref have to be duplicated here.
323 Type [] end_param_types = new Type [out_params + 1];
324 Parameter [] end_params = new Parameter [out_params + 1];
327 int top = Parameters.FixedParameters.Length;
328 for (i = 0; i < top; i++){
329 Parameter p = Parameters.FixedParameters [i];
330 if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
333 end_param_types [param] = param_types [i];
334 end_params [param] = p;
338 end_param_types [out_params] = TypeManager.iasyncresult_type;
339 end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null);
342 // Create method, define parameters, register parameters with type system
344 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_param_types);
345 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
348 // EndInvoke: Label the parameters
350 EndInvokeBuilder.DefineParameter (out_params + 1, ParameterAttributes.None, "result");
351 for (i = 0; i < end_params.Length-1; i++){
352 EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
355 Parameters end_parameters = new Parameters (end_params, null, Location);
356 end_parameters.ComputeAndDefineParameterTypes (ec);
358 TypeManager.RegisterMethod (
360 new InternalParameters (end_param_types, end_parameters),
366 public override void Emit ()
368 if (OptAttributes != null) {
369 Parameters.LabelParameters (ec, InvokeBuilder, Location);
370 OptAttributes.Emit (ec, this);
376 public override string[] ValidAttributeTargets {
378 return attribute_targets;
383 protected override bool VerifyClsCompliance (DeclSpace ds)
385 if (!base.VerifyClsCompliance (ds)) {
389 AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
391 if (!AttributeTester.IsClsCompliant (ReturnType.Type)) {
392 Report.Error (3002, Location, "Return type of '{0}' is not CLS-compliant", GetSignatureForError ());
398 // Returns the MethodBase for "Invoke" from a delegate type, this is used
399 // to extract the signature of a delegate.
401 public static MethodInfo GetInvokeMethod (EmitContext ec, Type delegate_type, Location loc)
403 Expression ml = Expression.MemberLookup (
404 ec, delegate_type, "Invoke", loc);
406 if (!(ml is MethodGroupExpr)) {
407 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
411 return (MethodInfo) (((MethodGroupExpr) ml).Methods [0]);
415 /// Verifies whether the method in question is compatible with the delegate
416 /// Returns the method itself if okay and null if not.
418 public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb,
421 ParameterData pd = Invocation.GetParameterData (mb);
423 int pd_count = pd.Count;
425 MethodBase invoke_mb = GetInvokeMethod (ec, delegate_type, loc);
426 if (invoke_mb == null)
429 ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
431 if (invoke_pd.Count != pd_count)
434 for (int i = pd_count; i > 0; ) {
437 Type invoke_pd_type = invoke_pd.ParameterType (i);
438 Type pd_type = pd.ParameterType (i);
439 Parameter.Modifier invoke_pd_type_mod = invoke_pd.ParameterModifier (i);
440 Parameter.Modifier pd_type_mod = pd.ParameterModifier (i);
442 if (invoke_pd_type == pd_type &&
443 invoke_pd_type_mod == pd_type_mod)
446 if (invoke_pd_type.IsSubclassOf (pd_type) &&
447 invoke_pd_type_mod == pd_type_mod)
448 if (RootContext.Version == LanguageVersion.ISO_1) {
449 Report.FeatureIsNotStandardized (loc, "contravariance");
457 Type invoke_mb_retval = ((MethodInfo) invoke_mb).ReturnType;
458 Type mb_retval = ((MethodInfo) mb).ReturnType;
459 if (invoke_mb_retval == mb_retval)
462 if (mb_retval.IsSubclassOf (invoke_mb_retval))
463 if (RootContext.Version == LanguageVersion.ISO_1) {
464 Report.FeatureIsNotStandardized (loc, "covariance");
474 // Verifies whether the invocation arguments are compatible with the
475 // delegate's target method
477 public static bool VerifyApplicability (EmitContext ec, Type delegate_type,
478 ArrayList args, Location loc)
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 '{0}' does not take {1} arguments",
506 delegate_type.ToString (), arg_count);
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 (
528 ec, args, arg_count, mb, false,
529 delegate_type, false, loc);
530 if (!ans && params_method)
531 ans = Invocation.VerifyArgumentsCompat (
532 ec, args, arg_count, mb, true,
533 delegate_type, false, loc);
538 /// Verifies whether the delegate in question is compatible with this one in
539 /// order to determine if instantiation from the same is possible.
541 public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Type probe_type, Location loc)
543 Expression ml = Expression.MemberLookup (
544 ec, delegate_type, "Invoke", loc);
546 if (!(ml is MethodGroupExpr)) {
547 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
551 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
552 ParameterData pd = Invocation.GetParameterData (mb);
554 Expression probe_ml = Expression.MemberLookup (
555 ec, delegate_type, "Invoke", loc);
557 if (!(probe_ml is MethodGroupExpr)) {
558 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
562 MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];
563 ParameterData probe_pd = Invocation.GetParameterData (probe_mb);
565 if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)
568 if (pd.Count != probe_pd.Count)
571 for (int i = pd.Count; i > 0; ) {
574 if (pd.ParameterType (i) != probe_pd.ParameterType (i) ||
575 pd.ParameterModifier (i) != probe_pd.ParameterModifier (i))
582 public static string FullDelegateDesc (Type del_type, MethodBase mb, ParameterData pd)
584 StringBuilder sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
586 sb.Append (" " + del_type.ToString ());
589 int length = pd.Count;
591 for (int i = length; i > 0; ) {
594 sb.Append (pd.ParameterDesc (length - i - 1));
600 return sb.ToString ();
604 // Hack around System.Reflection as found everywhere else
605 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
606 MemberFilter filter, object criteria)
608 ArrayList members = new ArrayList ();
610 if ((mt & MemberTypes.Method) != 0) {
611 if (ConstructorBuilder != null)
612 if (filter (ConstructorBuilder, criteria))
613 members.Add (ConstructorBuilder);
615 if (InvokeBuilder != null)
616 if (filter (InvokeBuilder, criteria))
617 members.Add (InvokeBuilder);
619 if (BeginInvokeBuilder != null)
620 if (filter (BeginInvokeBuilder, criteria))
621 members.Add (BeginInvokeBuilder);
623 if (EndInvokeBuilder != null)
624 if (filter (EndInvokeBuilder, criteria))
625 members.Add (EndInvokeBuilder);
628 return new MemberList (members);
631 public override MemberCache MemberCache {
637 public Expression InstanceExpression {
639 return instance_expr;
642 instance_expr = value;
646 public MethodBase TargetMethod {
648 return delegate_method;
651 delegate_method = value;
655 public Type TargetReturnType {
661 public Type [] ParameterTypes {
667 public override AttributeTargets AttributeTargets {
669 return AttributeTargets.Delegate;
674 // Represents header string for documentation comment.
676 public override string DocCommentHeader {
680 protected override void VerifyObsoleteAttribute()
682 CheckUsageOfObsoleteAttribute (ret_type);
684 foreach (Type type in param_types) {
685 CheckUsageOfObsoleteAttribute (type);
691 // Base class for `NewDelegate' and `ImplicitDelegateCreation'
693 public abstract class DelegateCreation : Expression {
694 protected MethodBase constructor_method;
695 protected MethodBase delegate_method;
696 protected MethodGroupExpr method_group;
697 protected Expression delegate_instance_expression;
699 public DelegateCreation () {}
701 public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
705 if (mg.Methods.Length > 1)
706 method_desc = mg.Methods [0].Name;
708 method_desc = Invocation.FullMethodDesc (mg.Methods [0]);
710 Expression invoke_method = Expression.MemberLookup (
711 ec, type, "Invoke", MemberTypes.Method,
712 Expression.AllBindingFlags, loc);
713 MethodBase method = ((MethodGroupExpr) invoke_method).Methods [0];
714 ParameterData param = Invocation.GetParameterData (method);
715 string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
717 Report.Error (123, loc, "Method '" + method_desc + "' does not " +
718 "match delegate '" + delegate_desc + "'");
721 public override void Emit (EmitContext ec)
723 if (delegate_instance_expression == null || delegate_method.IsStatic)
724 ec.ig.Emit (OpCodes.Ldnull);
726 delegate_instance_expression.Emit (ec);
728 if (delegate_method.IsVirtual && !method_group.IsBase) {
729 ec.ig.Emit (OpCodes.Dup);
730 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
732 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
733 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
736 protected bool ResolveConstructorMethod (EmitContext ec)
738 Expression ml = Expression.MemberLookup (
739 ec, type, ".ctor", loc);
741 if (!(ml is MethodGroupExpr)) {
742 Report.Error (-100, loc, "Internal error: Could not find delegate constructor!");
746 constructor_method = ((MethodGroupExpr) ml).Methods [0];
750 protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
752 foreach (MethodInfo mi in mg.Methods){
753 delegate_method = Delegate.VerifyMethod (ec, type, mi, loc);
755 if (delegate_method != null)
759 if (delegate_method == null) {
760 Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
765 // Check safe/unsafe of the delegate
768 ParameterData param = Invocation.GetParameterData (delegate_method);
769 int count = param.Count;
771 for (int i = 0; i < count; i++){
772 if (param.ParameterType (i).IsPointer){
773 Expression.UnsafeError (loc);
779 //TODO: implement caching when performance will be low
780 IMethodData md = TypeManager.GetMethod (delegate_method);
782 if (System.Attribute.GetCustomAttribute (delegate_method, TypeManager.conditional_attribute_type) != null) {
783 Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
786 if (md.OptAttributes != null && md.OptAttributes.Search (TypeManager.conditional_attribute_type, ec) != null) {
787 Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
791 if (mg.InstanceExpression != null)
792 delegate_instance_expression = mg.InstanceExpression.Resolve (ec);
793 else if (ec.IsStatic) {
794 if (!delegate_method.IsStatic) {
795 Report.Error (120, loc,
796 "An object reference is required for the non-static method " +
797 delegate_method.Name);
800 delegate_instance_expression = null;
802 delegate_instance_expression = ec.GetThis (loc);
804 if (delegate_instance_expression != null && delegate_instance_expression.Type.IsValueType)
805 delegate_instance_expression = new BoxedCast (delegate_instance_expression);
808 eclass = ExprClass.Value;
814 // Created from the conversion code
816 public class ImplicitDelegateCreation : DelegateCreation {
818 ImplicitDelegateCreation (Type t, Location l)
824 public override Expression DoResolve (EmitContext ec)
829 static public Expression Create (EmitContext ec, MethodGroupExpr mge, Type target_type, Location loc)
831 ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
832 if (d.ResolveConstructorMethod (ec))
833 return d.ResolveMethodGroupExpr (ec, mge);
840 // A delegate-creation-expression, invoked from the `New' class
842 public class NewDelegate : DelegateCreation {
843 public ArrayList Arguments;
846 // This constructor is invoked from the `New' expression
848 public NewDelegate (Type type, ArrayList Arguments, Location loc)
851 this.Arguments = Arguments;
855 public override Expression DoResolve (EmitContext ec)
857 if (Arguments == null || Arguments.Count != 1) {
858 Report.Error (149, loc,
859 "Method name expected");
863 if (!ResolveConstructorMethod (ec))
866 Argument a = (Argument) Arguments [0];
868 if (!a.ResolveMethodGroup (ec, loc))
871 Expression e = a.Expr;
873 if (e is AnonymousMethod && RootContext.Version != LanguageVersion.ISO_1)
874 return ((AnonymousMethod) e).Compatible (ec, type, false);
876 MethodGroupExpr mg = e as MethodGroupExpr;
878 return ResolveMethodGroupExpr (ec, mg);
880 Type e_type = e.Type;
882 if (!TypeManager.IsDelegateType (e_type)) {
883 Report.Error (149, loc, "Method name expected");
887 method_group = Expression.MemberLookup (
888 ec, type, "Invoke", MemberTypes.Method,
889 Expression.AllBindingFlags, loc) as MethodGroupExpr;
891 if (method_group == null) {
892 Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
896 // This is what MS' compiler reports. We could always choose
897 // to be more verbose and actually give delegate-level specifics
898 if (!Delegate.VerifyDelegate (ec, type, e_type, loc)) {
899 Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " +
900 "to type '" + type + "'");
904 delegate_instance_expression = e;
905 delegate_method = method_group.Methods [0];
907 eclass = ExprClass.Value;
912 public class DelegateInvocation : ExpressionStatement {
914 public Expression InstanceExpr;
915 public ArrayList Arguments;
919 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
921 this.InstanceExpr = instance_expr;
922 this.Arguments = args;
926 public override Expression DoResolve (EmitContext ec)
928 if (InstanceExpr is EventExpr) {
930 EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
932 Expression ml = MemberLookup (
933 ec, ec.ContainerType, ei.Name,
934 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
938 // If this is the case, then the Event does not belong
939 // to this Type and so, according to the spec
940 // cannot be accessed directly
942 // Note that target will not appear as an EventExpr
943 // in the case it is being referenced within the same type container;
944 // it will appear as a FieldExpr in that case.
947 Assign.error70 (ei, loc);
953 Type del_type = InstanceExpr.Type;
954 if (del_type == null)
957 if (Arguments != null){
958 foreach (Argument a in Arguments){
959 if (!a.Resolve (ec, loc))
964 if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
967 Expression lookup = Expression.MemberLookup (ec, del_type, "Invoke", loc);
968 if (!(lookup is MethodGroupExpr)) {
969 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
973 method = ((MethodGroupExpr) lookup).Methods [0];
974 type = ((MethodInfo) method).ReturnType;
975 eclass = ExprClass.Value;
980 public override void Emit (EmitContext ec)
983 // Invocation on delegates call the virtual Invoke member
984 // so we are always `instance' calls
986 Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, loc);
989 public override void EmitStatement (EmitContext ec)
993 // Pop the return value if there is one
995 if (method is MethodInfo){
996 if (((MethodInfo) method).ReturnType != TypeManager.void_type)
997 ec.ig.Emit (OpCodes.Pop);