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 && !RootContext.StdLib) {
88 TypeExpr expr = new TypeLookupExpression ("System.MulticastDelegate");
89 TypeManager.multicast_delegate_type = expr.ResolveType (ec);
92 if (TypeManager.multicast_delegate_type == null)
93 throw new InternalErrorException ("System.MulticastDelegate unresolved");
96 if (TypeManager.NamespaceClash (Name, Location))
99 ModuleBuilder builder = CodeGen.Module.Builder;
101 TypeBuilder = builder.DefineType (
102 Name, attr, TypeManager.multicast_delegate_type);
104 TypeBuilder builder = Parent.TypeBuilder;
106 string name = Name.Substring (1 + Name.LastIndexOf ('.'));
107 TypeBuilder = builder.DefineNestedType (
108 name, attr, TypeManager.multicast_delegate_type);
111 TypeManager.AddDelegateType (Name, TypeBuilder, this);
116 public override bool DefineMembers (TypeContainer container)
121 public override bool Define ()
123 MethodAttributes mattr;
127 throw new InternalErrorException ("Define called before DefineType?");
129 // FIXME: POSSIBLY make this static, as it is always constant
131 Type [] const_arg_types = new Type [2];
132 const_arg_types [0] = TypeManager.object_type;
133 const_arg_types [1] = TypeManager.intptr_type;
135 mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
136 MethodAttributes.HideBySig | MethodAttributes.Public;
138 ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
139 CallingConventions.Standard,
142 ConstructorBuilder.DefineParameter (1, ParameterAttributes.None, "object");
143 ConstructorBuilder.DefineParameter (2, ParameterAttributes.None, "method");
145 // HACK because System.Reflection.Emit is lame
148 // FIXME: POSSIBLY make these static, as they are always the same
149 Parameter [] fixed_pars = new Parameter [2];
150 fixed_pars [0] = new Parameter (TypeManager.system_object_expr, "object",
151 Parameter.Modifier.NONE, null);
152 fixed_pars [1] = new Parameter (TypeManager.system_intptr_expr, "method",
153 Parameter.Modifier.NONE, null);
154 Parameters const_parameters = new Parameters (fixed_pars, null, Location);
156 TypeManager.RegisterMethod (
158 new InternalParameters (const_arg_types, const_parameters),
162 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
165 // Here the various methods like Invoke, BeginInvoke etc are defined
167 // First, call the `out of band' special method for
168 // defining recursively any types we need:
170 if (!Parameters.ComputeAndDefineParameterTypes (ec))
173 param_types = Parameters.GetParameterInfo (ec);
174 if (param_types == null)
181 // Check accessibility
182 foreach (Type partype in param_types){
183 if (!Parent.AsAccessible (partype, ModFlags)) {
184 Report.Error (59, Location,
185 "Inconsistent accessibility: parameter type `" +
186 TypeManager.CSharpName (partype) + "` is less " +
187 "accessible than delegate `" + Name + "'");
190 if (partype.IsPointer && !UnsafeOK (Parent))
194 ReturnType = ReturnType.ResolveAsTypeTerminal (ec, false);
195 if (ReturnType == null)
198 ret_type = ReturnType.Type;
199 if (ret_type == null)
202 if (!Parent.AsAccessible (ret_type, ModFlags)) {
203 Report.Error (58, Location,
204 "Inconsistent accessibility: return type `" +
205 TypeManager.CSharpName (ret_type) + "` is less " +
206 "accessible than delegate `" + Name + "'");
210 if (ret_type.IsPointer && !UnsafeOK (Parent))
214 // We don't have to check any others because they are all
215 // guaranteed to be accessible - they are standard types.
218 CallingConventions cc = Parameters.GetCallingConvention ();
220 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
222 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
229 // Define parameters, and count out/ref parameters
233 if (Parameters.FixedParameters != null){
234 int top = Parameters.FixedParameters.Length;
237 for (; i < top; i++) {
238 p = Parameters.FixedParameters [i];
239 p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
241 if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
245 if (Parameters.ArrayParameter != null){
246 Parameter p = Parameters.ArrayParameter;
247 p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
250 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
252 TypeManager.RegisterMethod (InvokeBuilder,
253 new InternalParameters (param_types, Parameters),
259 int params_num = param_types.Length;
260 Type [] async_param_types = new Type [params_num + 2];
262 param_types.CopyTo (async_param_types, 0);
264 async_param_types [params_num] = TypeManager.asynccallback_type;
265 async_param_types [params_num + 1] = TypeManager.object_type;
267 mattr = MethodAttributes.Public | MethodAttributes.HideBySig |
268 MethodAttributes.Virtual | MethodAttributes.NewSlot;
270 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
273 TypeManager.iasyncresult_type,
277 if (Parameters.FixedParameters != null){
278 int top = Parameters.FixedParameters.Length;
281 for (i = 0 ; i < top; i++) {
282 p = Parameters.FixedParameters [i];
284 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
287 if (Parameters.ArrayParameter != null){
288 Parameter p = Parameters.ArrayParameter;
289 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
294 BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
295 BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
297 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
299 Parameter [] async_params = new Parameter [params_num + 2];
301 if (Parameters.FixedParameters != null){
302 Parameters.FixedParameters.CopyTo (async_params, 0);
303 n = Parameters.FixedParameters.Length;
305 if (Parameters.ArrayParameter != null)
306 async_params [n] = Parameters.ArrayParameter;
308 async_params [params_num] = new Parameter (
309 TypeManager.system_asynccallback_expr, "callback",
310 Parameter.Modifier.NONE, null);
311 async_params [params_num + 1] = new Parameter (
312 TypeManager.system_object_expr, "object",
313 Parameter.Modifier.NONE, null);
315 Parameters async_parameters = new Parameters (async_params, null, Location);
316 async_parameters.ComputeAndDefineParameterTypes (ec);
318 TypeManager.RegisterMethod (BeginInvokeBuilder,
319 new InternalParameters (async_param_types, async_parameters),
323 // EndInvoke is a bit more interesting, all the parameters labeled as
324 // out or ref have to be duplicated here.
327 Type [] end_param_types = new Type [out_params + 1];
328 Parameter [] end_params = new Parameter [out_params + 1];
331 int top = Parameters.FixedParameters.Length;
332 for (i = 0; i < top; i++){
333 Parameter p = Parameters.FixedParameters [i];
334 if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
337 end_param_types [param] = param_types [i];
338 end_params [param] = p;
342 end_param_types [out_params] = TypeManager.iasyncresult_type;
343 end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null);
346 // Create method, define parameters, register parameters with type system
348 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_param_types);
349 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
352 // EndInvoke: Label the parameters
354 EndInvokeBuilder.DefineParameter (out_params + 1, ParameterAttributes.None, "result");
355 for (i = 0; i < end_params.Length-1; i++){
356 EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
359 Parameters end_parameters = new Parameters (end_params, null, Location);
360 end_parameters.ComputeAndDefineParameterTypes (ec);
362 TypeManager.RegisterMethod (
364 new InternalParameters (end_param_types, end_parameters),
370 public override void Emit ()
372 if (OptAttributes != null) {
373 Parameters.LabelParameters (ec, InvokeBuilder, Location);
374 OptAttributes.Emit (ec, this);
380 public override string[] ValidAttributeTargets {
382 return attribute_targets;
387 protected override bool VerifyClsCompliance (DeclSpace ds)
389 if (!base.VerifyClsCompliance (ds)) {
393 AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
395 if (!AttributeTester.IsClsCompliant (ReturnType.Type)) {
396 Report.Error (3002, Location, "Return type of '{0}' is not CLS-compliant", GetSignatureForError ());
402 // Returns the MethodBase for "Invoke" from a delegate type, this is used
403 // to extract the signature of a delegate.
405 public static MethodInfo GetInvokeMethod (EmitContext ec, Type delegate_type, Location loc)
407 Expression ml = Expression.MemberLookup (
408 ec, delegate_type, "Invoke", loc);
410 if (!(ml is MethodGroupExpr)) {
411 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
415 return (MethodInfo) (((MethodGroupExpr) ml).Methods [0]);
419 /// Verifies whether the method in question is compatible with the delegate
420 /// Returns the method itself if okay and null if not.
422 public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb,
425 ParameterData pd = Invocation.GetParameterData (mb);
427 int pd_count = pd.Count;
429 MethodBase invoke_mb = GetInvokeMethod (ec, delegate_type, loc);
430 if (invoke_mb == null)
433 ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
435 if (invoke_pd.Count != pd_count)
438 for (int i = pd_count; i > 0; ) {
441 Type invoke_pd_type = invoke_pd.ParameterType (i);
442 Type pd_type = pd.ParameterType (i);
443 Parameter.Modifier invoke_pd_type_mod = invoke_pd.ParameterModifier (i);
444 Parameter.Modifier pd_type_mod = pd.ParameterModifier (i);
446 if (invoke_pd_type == pd_type &&
447 invoke_pd_type_mod == pd_type_mod)
450 if (invoke_pd_type.IsSubclassOf (pd_type) &&
451 invoke_pd_type_mod == pd_type_mod)
452 if (RootContext.Version == LanguageVersion.ISO_1) {
453 Report.FeatureIsNotStandardized (loc, "contravariance");
461 Type invoke_mb_retval = ((MethodInfo) invoke_mb).ReturnType;
462 Type mb_retval = ((MethodInfo) mb).ReturnType;
463 if (invoke_mb_retval == mb_retval)
466 if (mb_retval.IsSubclassOf (invoke_mb_retval))
467 if (RootContext.Version == LanguageVersion.ISO_1) {
468 Report.FeatureIsNotStandardized (loc, "covariance");
478 // Verifies whether the invocation arguments are compatible with the
479 // delegate's target method
481 public static bool VerifyApplicability (EmitContext ec, Type delegate_type,
482 ArrayList args, Location loc)
489 arg_count = args.Count;
491 Expression ml = Expression.MemberLookup (
492 ec, delegate_type, "Invoke", loc);
494 if (!(ml is MethodGroupExpr)) {
495 Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
499 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
500 ParameterData pd = Invocation.GetParameterData (mb);
502 int pd_count = pd.Count;
504 bool params_method = (pd_count != 0) &&
505 (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS);
507 if (!params_method && pd_count != arg_count) {
508 Report.Error (1593, loc,
509 "Delegate '{0}' does not take {1} arguments",
510 delegate_type.ToString (), arg_count);
515 // Consider the case:
516 // delegate void FOO(param object[] args);
517 // FOO f = new FOO(...);
518 // f(new object[] {1, 2, 3});
520 // This should be treated like f(1,2,3). This is done by ignoring the
521 // 'param' modifier for that invocation. If that fails, then the
522 // 'param' modifier is considered.
524 // One issue is that 'VerifyArgumentsCompat' modifies the elements of
525 // the 'args' array. However, the modifications appear idempotent.
526 // Normal 'Invocation's also have the same behaviour, implicitly.
530 if (arg_count == pd_count)
531 ans = Invocation.VerifyArgumentsCompat (
532 ec, args, arg_count, mb, false,
533 delegate_type, false, loc);
534 if (!ans && params_method)
535 ans = Invocation.VerifyArgumentsCompat (
536 ec, args, arg_count, mb, true,
537 delegate_type, false, loc);
542 /// Verifies whether the delegate in question is compatible with this one in
543 /// order to determine if instantiation from the same is possible.
545 public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Type probe_type, Location loc)
547 Expression ml = Expression.MemberLookup (
548 ec, delegate_type, "Invoke", loc);
550 if (!(ml is MethodGroupExpr)) {
551 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
555 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
556 ParameterData pd = Invocation.GetParameterData (mb);
558 Expression probe_ml = Expression.MemberLookup (
559 ec, delegate_type, "Invoke", loc);
561 if (!(probe_ml is MethodGroupExpr)) {
562 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
566 MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];
567 ParameterData probe_pd = Invocation.GetParameterData (probe_mb);
569 if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)
572 if (pd.Count != probe_pd.Count)
575 for (int i = pd.Count; i > 0; ) {
578 if (pd.ParameterType (i) != probe_pd.ParameterType (i) ||
579 pd.ParameterModifier (i) != probe_pd.ParameterModifier (i))
586 public static string FullDelegateDesc (Type del_type, MethodBase mb, ParameterData pd)
588 StringBuilder sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
590 sb.Append (" " + del_type.ToString ());
593 int length = pd.Count;
595 for (int i = length; i > 0; ) {
598 sb.Append (pd.ParameterDesc (length - i - 1));
604 return sb.ToString ();
608 // Hack around System.Reflection as found everywhere else
609 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
610 MemberFilter filter, object criteria)
612 ArrayList members = new ArrayList ();
614 if ((mt & MemberTypes.Method) != 0) {
615 if (ConstructorBuilder != null)
616 if (filter (ConstructorBuilder, criteria))
617 members.Add (ConstructorBuilder);
619 if (InvokeBuilder != null)
620 if (filter (InvokeBuilder, criteria))
621 members.Add (InvokeBuilder);
623 if (BeginInvokeBuilder != null)
624 if (filter (BeginInvokeBuilder, criteria))
625 members.Add (BeginInvokeBuilder);
627 if (EndInvokeBuilder != null)
628 if (filter (EndInvokeBuilder, criteria))
629 members.Add (EndInvokeBuilder);
632 return new MemberList (members);
635 public override MemberCache MemberCache {
641 public Expression InstanceExpression {
643 return instance_expr;
646 instance_expr = value;
650 public MethodBase TargetMethod {
652 return delegate_method;
655 delegate_method = value;
659 public Type TargetReturnType {
665 public Type [] ParameterTypes {
671 public override AttributeTargets AttributeTargets {
673 return AttributeTargets.Delegate;
678 // Represents header string for documentation comment.
680 public override string DocCommentHeader {
684 protected override void VerifyObsoleteAttribute()
686 CheckUsageOfObsoleteAttribute (ret_type);
688 foreach (Type type in param_types) {
689 CheckUsageOfObsoleteAttribute (type);
695 // Base class for `NewDelegate' and `ImplicitDelegateCreation'
697 public abstract class DelegateCreation : Expression {
698 protected MethodBase constructor_method;
699 protected MethodBase delegate_method;
700 protected MethodGroupExpr method_group;
701 protected Expression delegate_instance_expression;
703 public DelegateCreation () {}
705 public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
709 if (mg.Methods.Length > 1)
710 method_desc = mg.Methods [0].Name;
712 method_desc = Invocation.FullMethodDesc (mg.Methods [0]);
714 Expression invoke_method = Expression.MemberLookup (
715 ec, type, "Invoke", MemberTypes.Method,
716 Expression.AllBindingFlags, loc);
717 MethodBase method = ((MethodGroupExpr) invoke_method).Methods [0];
718 ParameterData param = Invocation.GetParameterData (method);
719 string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
721 Report.Error (123, loc, "Method '" + method_desc + "' does not " +
722 "match delegate '" + delegate_desc + "'");
725 public override void Emit (EmitContext ec)
727 if (delegate_instance_expression == null || delegate_method.IsStatic)
728 ec.ig.Emit (OpCodes.Ldnull);
730 delegate_instance_expression.Emit (ec);
732 if (delegate_method.IsVirtual && !method_group.IsBase) {
733 ec.ig.Emit (OpCodes.Dup);
734 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
736 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
737 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
740 protected bool ResolveConstructorMethod (EmitContext ec)
742 Expression ml = Expression.MemberLookup (
743 ec, type, ".ctor", loc);
745 if (!(ml is MethodGroupExpr)) {
746 Report.Error (-100, loc, "Internal error: Could not find delegate constructor!");
750 constructor_method = ((MethodGroupExpr) ml).Methods [0];
754 protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
756 foreach (MethodInfo mi in mg.Methods){
757 delegate_method = Delegate.VerifyMethod (ec, type, mi, loc);
759 if (delegate_method != null)
763 if (delegate_method == null) {
764 Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
769 // Check safe/unsafe of the delegate
772 ParameterData param = Invocation.GetParameterData (delegate_method);
773 int count = param.Count;
775 for (int i = 0; i < count; i++){
776 if (param.ParameterType (i).IsPointer){
777 Expression.UnsafeError (loc);
783 //TODO: implement caching when performance will be low
784 IMethodData md = TypeManager.GetMethod (delegate_method);
786 if (System.Attribute.GetCustomAttribute (delegate_method, TypeManager.conditional_attribute_type) != null) {
787 Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
790 if (md.OptAttributes != null && md.OptAttributes.Search (TypeManager.conditional_attribute_type, ec) != null) {
791 Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
795 if (mg.InstanceExpression != null)
796 delegate_instance_expression = mg.InstanceExpression.Resolve (ec);
797 else if (ec.IsStatic) {
798 if (!delegate_method.IsStatic) {
799 Report.Error (120, loc,
800 "An object reference is required for the non-static method " +
801 delegate_method.Name);
804 delegate_instance_expression = null;
806 delegate_instance_expression = ec.GetThis (loc);
808 if (delegate_instance_expression != null && delegate_instance_expression.Type.IsValueType)
809 delegate_instance_expression = new BoxedCast (delegate_instance_expression);
812 eclass = ExprClass.Value;
818 // Created from the conversion code
820 public class ImplicitDelegateCreation : DelegateCreation {
822 ImplicitDelegateCreation (Type t, Location l)
828 public override Expression DoResolve (EmitContext ec)
833 static public Expression Create (EmitContext ec, MethodGroupExpr mge, Type target_type, Location loc)
835 ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
836 if (d.ResolveConstructorMethod (ec))
837 return d.ResolveMethodGroupExpr (ec, mge);
844 // A delegate-creation-expression, invoked from the `New' class
846 public class NewDelegate : DelegateCreation {
847 public ArrayList Arguments;
850 // This constructor is invoked from the `New' expression
852 public NewDelegate (Type type, ArrayList Arguments, Location loc)
855 this.Arguments = Arguments;
859 public override Expression DoResolve (EmitContext ec)
861 if (Arguments == null || Arguments.Count != 1) {
862 Report.Error (149, loc,
863 "Method name expected");
867 if (!ResolveConstructorMethod (ec))
870 Argument a = (Argument) Arguments [0];
872 if (!a.ResolveMethodGroup (ec, loc))
875 Expression e = a.Expr;
877 if (e is AnonymousMethod && RootContext.Version != LanguageVersion.ISO_1)
878 return ((AnonymousMethod) e).Compatible (ec, type, false);
880 MethodGroupExpr mg = e as MethodGroupExpr;
882 return ResolveMethodGroupExpr (ec, mg);
884 Type e_type = e.Type;
886 if (!TypeManager.IsDelegateType (e_type)) {
887 Report.Error (149, loc, "Method name expected");
891 method_group = Expression.MemberLookup (
892 ec, type, "Invoke", MemberTypes.Method,
893 Expression.AllBindingFlags, loc) as MethodGroupExpr;
895 if (method_group == null) {
896 Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
900 // This is what MS' compiler reports. We could always choose
901 // to be more verbose and actually give delegate-level specifics
902 if (!Delegate.VerifyDelegate (ec, type, e_type, loc)) {
903 Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " +
904 "to type '" + type + "'");
908 delegate_instance_expression = e;
909 delegate_method = method_group.Methods [0];
911 eclass = ExprClass.Value;
916 public class DelegateInvocation : ExpressionStatement {
918 public Expression InstanceExpr;
919 public ArrayList Arguments;
923 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
925 this.InstanceExpr = instance_expr;
926 this.Arguments = args;
930 public override Expression DoResolve (EmitContext ec)
932 if (InstanceExpr is EventExpr) {
934 EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
936 Expression ml = MemberLookup (
937 ec, ec.ContainerType, ei.Name,
938 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
942 // If this is the case, then the Event does not belong
943 // to this Type and so, according to the spec
944 // cannot be accessed directly
946 // Note that target will not appear as an EventExpr
947 // in the case it is being referenced within the same type container;
948 // it will appear as a FieldExpr in that case.
951 Assign.error70 (ei, loc);
957 Type del_type = InstanceExpr.Type;
958 if (del_type == null)
961 if (Arguments != null){
962 foreach (Argument a in Arguments){
963 if (!a.Resolve (ec, loc))
968 if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
971 Expression lookup = Expression.MemberLookup (ec, del_type, "Invoke", loc);
972 if (!(lookup is MethodGroupExpr)) {
973 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
977 method = ((MethodGroupExpr) lookup).Methods [0];
978 type = ((MethodInfo) method).ReturnType;
979 eclass = ExprClass.Value;
984 public override void Emit (EmitContext ec)
987 // Invocation on delegates call the virtual Invoke member
988 // so we are always `instance' calls
990 Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, loc);
993 public override void EmitStatement (EmitContext ec)
997 // Pop the return value if there is one
999 if (method is MethodInfo){
1000 if (((MethodInfo) method).ReturnType != TypeManager.void_type)
1001 ec.ig.Emit (OpCodes.Pop);