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);
85 foreach (TypeParameter type_param in TypeParameters)
86 if (!type_param.Resolve (this))
90 TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel) |
91 TypeAttributes.Class | TypeAttributes.Sealed;
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);
112 string[] param_names = new string [TypeParameters.Length];
113 for (int i = 0; i < TypeParameters.Length; i++)
114 param_names [i] = TypeParameters [i].Name;
116 GenericTypeParameterBuilder[] gen_params;
118 gen_params = TypeBuilder.DefineGenericParameters (param_names);
120 for (int i = 0; i < gen_params.Length; i++)
121 TypeParameters [i].Define (gen_params [i]);
123 foreach (TypeParameter type_param in TypeParameters) {
124 if (!type_param.DefineType (ec))
128 TypeExpr current = new ConstructedType (Name, TypeParameters, Location);
129 current = current.ResolveAsTypeTerminal (ec);
133 CurrentType = current.Type;
139 public override bool DefineMembers (TypeContainer container)
144 public override bool Define ()
146 MethodAttributes mattr;
148 ec = new EmitContext (this, this, Location, null, null, ModFlags, false);
151 foreach (TypeParameter type_param in TypeParameters)
152 type_param.DefineType (ec);
155 // FIXME: POSSIBLY make this static, as it is always constant
157 Type [] const_arg_types = new Type [2];
158 const_arg_types [0] = TypeManager.object_type;
159 const_arg_types [1] = TypeManager.intptr_type;
161 mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
162 MethodAttributes.HideBySig | MethodAttributes.Public;
164 ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
165 CallingConventions.Standard,
168 ConstructorBuilder.DefineParameter (1, ParameterAttributes.None, "object");
169 ConstructorBuilder.DefineParameter (2, ParameterAttributes.None, "method");
171 // HACK because System.Reflection.Emit is lame
174 // FIXME: POSSIBLY make these static, as they are always the same
175 Parameter [] fixed_pars = new Parameter [2];
176 fixed_pars [0] = new Parameter (TypeManager.system_object_expr, "object",
177 Parameter.Modifier.NONE, null);
178 fixed_pars [1] = new Parameter (TypeManager.system_intptr_expr, "method",
179 Parameter.Modifier.NONE, null);
180 Parameters const_parameters = new Parameters (fixed_pars, null, Location);
182 TypeManager.RegisterMethod (
184 new InternalParameters (const_arg_types, const_parameters),
188 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
191 // Here the various methods like Invoke, BeginInvoke etc are defined
193 // First, call the `out of band' special method for
194 // defining recursively any types we need:
196 if (!Parameters.ComputeAndDefineParameterTypes (ec))
199 param_types = Parameters.GetParameterInfo (ec);
200 if (param_types == null)
207 // Check accessibility
208 foreach (Type partype in param_types){
209 if (!Parent.AsAccessible (partype, ModFlags)) {
210 Report.Error (59, Location,
211 "Inconsistent accessibility: parameter type `" +
212 TypeManager.CSharpName (partype) + "` is less " +
213 "accessible than delegate `" + Name + "'");
216 if (partype.IsPointer && !UnsafeOK (Parent))
220 ReturnType = ReturnType.ResolveAsTypeTerminal (ec);
221 if (ReturnType == null)
224 ret_type = ReturnType.Type;
225 if (ret_type == null)
228 if (!Parent.AsAccessible (ret_type, ModFlags)) {
229 Report.Error (58, Location,
230 "Inconsistent accessibility: return type `" +
231 TypeManager.CSharpName (ret_type) + "` is less " +
232 "accessible than delegate `" + Name + "'");
236 if (ret_type.IsPointer && !UnsafeOK (Parent))
240 // We don't have to check any others because they are all
241 // guaranteed to be accessible - they are standard types.
244 CallingConventions cc = Parameters.GetCallingConvention ();
246 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
248 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
255 // Define parameters, and count out/ref parameters
259 if (Parameters.FixedParameters != null){
260 int top = Parameters.FixedParameters.Length;
263 for (; i < top; i++) {
264 p = Parameters.FixedParameters [i];
265 p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
267 if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
271 if (Parameters.ArrayParameter != null){
272 Parameter p = Parameters.ArrayParameter;
273 p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
276 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
278 TypeManager.RegisterMethod (InvokeBuilder,
279 new InternalParameters (param_types, Parameters),
285 int params_num = param_types.Length;
286 Type [] async_param_types = new Type [params_num + 2];
288 param_types.CopyTo (async_param_types, 0);
290 async_param_types [params_num] = TypeManager.asynccallback_type;
291 async_param_types [params_num + 1] = TypeManager.object_type;
293 mattr = MethodAttributes.Public | MethodAttributes.HideBySig |
294 MethodAttributes.Virtual | MethodAttributes.NewSlot;
296 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
299 TypeManager.iasyncresult_type,
303 if (Parameters.FixedParameters != null){
304 int top = Parameters.FixedParameters.Length;
307 for (i = 0 ; i < top; i++) {
308 p = Parameters.FixedParameters [i];
310 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
313 if (Parameters.ArrayParameter != null){
314 Parameter p = Parameters.ArrayParameter;
315 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
320 BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
321 BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
323 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
325 Parameter [] async_params = new Parameter [params_num + 2];
327 if (Parameters.FixedParameters != null){
328 Parameters.FixedParameters.CopyTo (async_params, 0);
329 n = Parameters.FixedParameters.Length;
331 if (Parameters.ArrayParameter != null)
332 async_params [n] = Parameters.ArrayParameter;
334 async_params [params_num] = new Parameter (
335 TypeManager.system_asynccallback_expr, "callback",
336 Parameter.Modifier.NONE, null);
337 async_params [params_num + 1] = new Parameter (
338 TypeManager.system_object_expr, "object",
339 Parameter.Modifier.NONE, null);
341 Parameters async_parameters = new Parameters (async_params, null, Location);
342 async_parameters.ComputeAndDefineParameterTypes (ec);
344 TypeManager.RegisterMethod (BeginInvokeBuilder,
345 new InternalParameters (async_param_types, async_parameters),
349 // EndInvoke is a bit more interesting, all the parameters labeled as
350 // out or ref have to be duplicated here.
353 Type [] end_param_types = new Type [out_params + 1];
354 Parameter [] end_params = new Parameter [out_params + 1];
357 int top = Parameters.FixedParameters.Length;
358 for (i = 0; i < top; i++){
359 Parameter p = Parameters.FixedParameters [i];
360 if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
363 end_param_types [param] = param_types [i];
364 end_params [param] = p;
368 end_param_types [out_params] = TypeManager.iasyncresult_type;
369 end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null);
372 // Create method, define parameters, register parameters with type system
374 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_param_types);
375 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
378 // EndInvoke: Label the parameters
380 EndInvokeBuilder.DefineParameter (out_params + 1, ParameterAttributes.None, "result");
381 for (i = 0; i < end_params.Length-1; i++){
382 EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
385 Parameters end_parameters = new Parameters (end_params, null, Location);
386 end_parameters.ComputeAndDefineParameterTypes (ec);
388 TypeManager.RegisterMethod (
390 new InternalParameters (end_param_types, end_parameters),
396 public override void Emit ()
398 if (OptAttributes != null) {
399 Parameters.LabelParameters (ec, InvokeBuilder, Location);
400 OptAttributes.Emit (ec, this);
406 public override string[] ValidAttributeTargets {
408 return attribute_targets;
413 protected override bool VerifyClsCompliance (DeclSpace ds)
415 if (!base.VerifyClsCompliance (ds)) {
419 AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
421 if (!AttributeTester.IsClsCompliant (ReturnType.Type)) {
422 Report.Error (3002, Location, "Return type of '{0}' is not CLS-compliant", GetSignatureForError ());
428 // Returns the MethodBase for "Invoke" from a delegate type, this is used
429 // to extract the signature of a delegate.
431 public static MethodGroupExpr GetInvokeMethod (EmitContext ec, Type delegate_type,
434 Expression ml = Expression.MemberLookup (
435 ec, delegate_type, "Invoke", loc);
437 MethodGroupExpr mg = ml as MethodGroupExpr;
439 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
447 /// Verifies whether the method in question is compatible with the delegate
448 /// Returns the method itself if okay and null if not.
450 public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb,
453 MethodGroupExpr mg = GetInvokeMethod (ec, delegate_type, loc);
457 MethodBase invoke_mb = mg.Methods [0];
458 ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
460 if (!mg.HasTypeArguments &&
461 !Invocation.InferTypeArguments (ec, invoke_pd, ref mb))
464 ParameterData pd = Invocation.GetParameterData (mb);
465 int pd_count = pd.Count;
467 if (invoke_pd.Count != pd_count)
470 for (int i = pd_count; i > 0; ) {
473 if (invoke_pd.ParameterType (i) == pd.ParameterType (i) &&
474 invoke_pd.ParameterModifier (i) == pd.ParameterModifier (i))
481 if (((MethodInfo) invoke_mb).ReturnType == ((MethodInfo) mb).ReturnType)
488 // Verifies whether the invocation arguments are compatible with the
489 // delegate's target method
491 public static bool VerifyApplicability (EmitContext ec, Type delegate_type,
492 ArrayList args, Location loc)
499 arg_count = args.Count;
501 Expression ml = Expression.MemberLookup (
502 ec, delegate_type, "Invoke", loc);
504 if (!(ml is MethodGroupExpr)) {
505 Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
509 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
510 ParameterData pd = Invocation.GetParameterData (mb);
512 int pd_count = pd.Count;
514 bool params_method = (pd_count != 0) &&
515 (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS);
517 if (!params_method && pd_count != arg_count) {
518 Report.Error (1593, loc,
519 "Delegate '{0}' does not take {1} arguments",
520 delegate_type.ToString (), arg_count);
525 // Consider the case:
526 // delegate void FOO(param object[] args);
527 // FOO f = new FOO(...);
528 // f(new object[] {1, 2, 3});
530 // This should be treated like f(1,2,3). This is done by ignoring the
531 // 'param' modifier for that invocation. If that fails, then the
532 // 'param' modifier is considered.
534 // One issue is that 'VerifyArgumentsCompat' modifies the elements of
535 // the 'args' array. However, the modifications appear idempotent.
536 // Normal 'Invocation's also have the same behaviour, implicitly.
540 if (arg_count == pd_count)
541 ans = Invocation.VerifyArgumentsCompat (
542 ec, args, arg_count, mb, false,
543 delegate_type, false, loc);
544 if (!ans && params_method)
545 ans = Invocation.VerifyArgumentsCompat (
546 ec, args, arg_count, mb, true,
547 delegate_type, false, loc);
552 /// Verifies whether the delegate in question is compatible with this one in
553 /// order to determine if instantiation from the same is possible.
555 public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Type probe_type, Location loc)
557 Expression ml = Expression.MemberLookup (
558 ec, delegate_type, "Invoke", loc);
560 if (!(ml is MethodGroupExpr)) {
561 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
565 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
566 ParameterData pd = Invocation.GetParameterData (mb);
568 Expression probe_ml = Expression.MemberLookup (
569 ec, delegate_type, "Invoke", loc);
571 if (!(probe_ml is MethodGroupExpr)) {
572 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
576 MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];
577 ParameterData probe_pd = Invocation.GetParameterData (probe_mb);
579 if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)
582 if (pd.Count != probe_pd.Count)
585 for (int i = pd.Count; i > 0; ) {
588 if (pd.ParameterType (i) != probe_pd.ParameterType (i) ||
589 pd.ParameterModifier (i) != probe_pd.ParameterModifier (i))
596 public static string FullDelegateDesc (Type del_type, MethodBase mb, ParameterData pd)
598 StringBuilder sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
600 sb.Append (" " + del_type.ToString ());
603 int length = pd.Count;
605 for (int i = length; i > 0; ) {
608 sb.Append (pd.ParameterDesc (length - i - 1));
614 return sb.ToString ();
618 // Hack around System.Reflection as found everywhere else
619 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
620 MemberFilter filter, object criteria)
622 ArrayList members = new ArrayList ();
624 if ((mt & MemberTypes.Method) != 0) {
625 if (ConstructorBuilder != null)
626 if (filter (ConstructorBuilder, criteria))
627 members.Add (ConstructorBuilder);
629 if (InvokeBuilder != null)
630 if (filter (InvokeBuilder, criteria))
631 members.Add (InvokeBuilder);
633 if (BeginInvokeBuilder != null)
634 if (filter (BeginInvokeBuilder, criteria))
635 members.Add (BeginInvokeBuilder);
637 if (EndInvokeBuilder != null)
638 if (filter (EndInvokeBuilder, criteria))
639 members.Add (EndInvokeBuilder);
642 return new MemberList (members);
645 public override MemberCache MemberCache {
651 public Expression InstanceExpression {
653 return instance_expr;
656 instance_expr = value;
660 public MethodBase TargetMethod {
662 return delegate_method;
665 delegate_method = value;
669 public Type TargetReturnType {
675 public Type [] ParameterTypes {
681 public override AttributeTargets AttributeTargets {
683 return AttributeTargets.Delegate;
687 protected override void VerifyObsoleteAttribute()
689 CheckUsageOfObsoleteAttribute (ret_type);
691 foreach (Type type in param_types) {
692 CheckUsageOfObsoleteAttribute (type);
698 // Base class for `NewDelegate' and `ImplicitDelegateCreation'
700 public abstract class DelegateCreation : Expression {
701 protected MethodBase constructor_method;
702 protected MethodBase delegate_method;
703 protected MethodGroupExpr method_group;
704 protected Expression delegate_instance_expression;
706 public DelegateCreation () {}
708 public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
712 MethodBase candidate = mg.Methods [0];
713 if (mg.Methods.Length > 1)
714 method_desc = candidate.Name;
716 method_desc = Invocation.FullMethodDesc (candidate);
718 Expression invoke_method = Expression.MemberLookup (
719 ec, type, "Invoke", MemberTypes.Method,
720 Expression.AllBindingFlags, loc);
721 MethodBase method = ((MethodGroupExpr) invoke_method).Methods [0];
722 ParameterData param = Invocation.GetParameterData (method);
723 string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
725 if (!mg.HasTypeArguments &&
726 !Invocation.InferTypeArguments (ec, param, ref candidate))
727 Report.Error (411, loc, "The type arguments for " +
728 "method `{0}' cannot be infered from " +
729 "the usage. Try specifying the type " +
730 "arguments explicitly.", method_desc);
732 Report.Error (123, loc, "Method '{0}' does not " +
733 "match delegate '{1}'", method_desc,
737 public override void Emit (EmitContext ec)
739 if (delegate_instance_expression == null || delegate_method.IsStatic)
740 ec.ig.Emit (OpCodes.Ldnull);
742 delegate_instance_expression.Emit (ec);
744 if (delegate_method.IsVirtual && !method_group.IsBase) {
745 ec.ig.Emit (OpCodes.Dup);
746 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
748 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
749 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
752 protected bool ResolveConstructorMethod (EmitContext ec)
754 Expression ml = Expression.MemberLookup (
755 ec, type, ".ctor", loc);
757 if (!(ml is MethodGroupExpr)) {
758 Report.Error (-100, loc, "Internal error: Could not find delegate constructor!");
762 constructor_method = ((MethodGroupExpr) ml).Methods [0];
766 protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
768 foreach (MethodInfo mi in mg.Methods){
769 delegate_method = Delegate.VerifyMethod (
772 if (delegate_method != null)
776 if (delegate_method == null) {
777 Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
782 // Check safe/unsafe of the delegate
785 ParameterData param = Invocation.GetParameterData (delegate_method);
786 int count = param.Count;
788 for (int i = 0; i < count; i++){
789 if (param.ParameterType (i).IsPointer){
790 Expression.UnsafeError (loc);
796 //TODO: implement caching when performance will be low
797 IMethodData md = TypeManager.GetMethod (delegate_method);
799 if (System.Attribute.GetCustomAttribute (delegate_method, TypeManager.conditional_attribute_type) != null) {
800 Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
803 if (md.OptAttributes != null && md.OptAttributes.Search (TypeManager.conditional_attribute_type, ec) != null) {
804 Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
808 if (mg.InstanceExpression != null)
809 delegate_instance_expression = mg.InstanceExpression.Resolve (ec);
810 else if (ec.IsStatic) {
811 if (!delegate_method.IsStatic) {
812 Report.Error (120, loc,
813 "An object reference is required for the non-static method " +
814 delegate_method.Name);
817 delegate_instance_expression = null;
819 delegate_instance_expression = ec.GetThis (loc);
821 if (delegate_instance_expression != null && delegate_instance_expression.Type.IsValueType)
822 delegate_instance_expression = new BoxedCast (mg.InstanceExpression);
825 eclass = ExprClass.Value;
831 // Created from the conversion code
833 public class ImplicitDelegateCreation : DelegateCreation {
835 ImplicitDelegateCreation (Type t, Location l)
841 public override Expression DoResolve (EmitContext ec)
846 static public Expression Create (EmitContext ec, MethodGroupExpr mge, Type target_type, Location loc)
848 ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
849 if (d.ResolveConstructorMethod (ec))
850 return d.ResolveMethodGroupExpr (ec, mge);
857 // A delegate-creation-expression, invoked from the `New' class
859 public class NewDelegate : DelegateCreation {
860 public ArrayList Arguments;
863 // This constructor is invoked from the `New' expression
865 public NewDelegate (Type type, ArrayList Arguments, Location loc)
868 this.Arguments = Arguments;
872 public override Expression DoResolve (EmitContext ec)
874 if (Arguments == null || Arguments.Count != 1) {
875 Report.Error (149, loc,
876 "Method name expected");
880 if (!ResolveConstructorMethod (ec))
883 Argument a = (Argument) Arguments [0];
885 if (!a.ResolveMethodGroup (ec, loc))
888 Expression e = a.Expr;
890 if (e is AnonymousMethod)
891 return ((AnonymousMethod) e).Compatible (ec, type, false);
893 MethodGroupExpr mg = e as MethodGroupExpr;
895 return ResolveMethodGroupExpr (ec, mg);
897 Type e_type = e.Type;
899 if (!TypeManager.IsDelegateType (e_type)) {
900 e.Error_UnexpectedKind ("method", loc);
904 method_group = Expression.MemberLookup (
905 ec, type, "Invoke", MemberTypes.Method,
906 Expression.AllBindingFlags, loc) as MethodGroupExpr;
908 if (method_group == null) {
909 Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
913 // This is what MS' compiler reports. We could always choose
914 // to be more verbose and actually give delegate-level specifics
915 if (!Delegate.VerifyDelegate (ec, type, e_type, loc)) {
916 Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " +
917 "to type '" + type + "'");
921 delegate_instance_expression = e;
922 delegate_method = method_group.Methods [0];
924 eclass = ExprClass.Value;
929 public class DelegateInvocation : ExpressionStatement {
931 public Expression InstanceExpr;
932 public ArrayList Arguments;
936 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
938 this.InstanceExpr = instance_expr;
939 this.Arguments = args;
943 public override Expression DoResolve (EmitContext ec)
945 if (InstanceExpr is EventExpr) {
947 EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
949 Expression ml = MemberLookup (
950 ec, ec.ContainerType, ei.Name,
951 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
955 // If this is the case, then the Event does not belong
956 // to this Type and so, according to the spec
957 // cannot be accessed directly
959 // Note that target will not appear as an EventExpr
960 // in the case it is being referenced within the same type container;
961 // it will appear as a FieldExpr in that case.
964 Assign.error70 (ei, loc);
970 Type del_type = InstanceExpr.Type;
971 if (del_type == null)
974 if (Arguments != null){
975 foreach (Argument a in Arguments){
976 if (!a.Resolve (ec, loc))
981 if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
984 Expression lookup = Expression.MemberLookup (ec, del_type, "Invoke", loc);
985 if (!(lookup is MethodGroupExpr)) {
986 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
990 method = ((MethodGroupExpr) lookup).Methods [0];
991 type = ((MethodInfo) method).ReturnType;
992 eclass = ExprClass.Value;
997 public override void Emit (EmitContext ec)
1000 // Invocation on delegates call the virtual Invoke member
1001 // so we are always `instance' calls
1003 Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, loc);
1006 public override void EmitStatement (EmitContext ec)
1010 // Pop the return value if there is one
1012 if (method is MethodInfo){
1013 if (((MethodInfo) method).ReturnType != TypeManager.void_type)
1014 ec.ig.Emit (OpCodes.Pop);