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 param_types = Parameters.GetParameterInfo (ec);
171 if (param_types == null)
178 // Check accessibility
179 foreach (Type partype in param_types){
180 if (!Parent.AsAccessible (partype, ModFlags)) {
181 Report.Error (59, Location,
182 "Inconsistent accessibility: parameter type `" +
183 TypeManager.CSharpName (partype) + "` is less " +
184 "accessible than delegate `" + Name + "'");
187 if (partype.IsPointer && !UnsafeOK (Parent))
191 ReturnType = ReturnType.ResolveAsTypeTerminal (ec, false);
192 if (ReturnType == null)
195 ret_type = ReturnType.Type;
196 if (ret_type == null)
199 if (!Parent.AsAccessible (ret_type, ModFlags)) {
200 Report.Error (58, Location,
201 "Inconsistent accessibility: return type `" +
202 TypeManager.CSharpName (ret_type) + "` is less " +
203 "accessible than delegate `" + Name + "'");
207 if (ret_type.IsPointer && !UnsafeOK (Parent))
210 if (RootContext.StdLib && (ret_type == TypeManager.arg_iterator_type || ret_type == TypeManager.typed_reference_type)) {
211 Method.Error1599 (Location, ret_type);
216 // We don't have to check any others because they are all
217 // guaranteed to be accessible - they are standard types.
220 CallingConventions cc = Parameters.GetCallingConvention ();
222 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
224 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
231 // Define parameters, and count out/ref parameters
235 if (Parameters.FixedParameters != null){
236 int top = Parameters.FixedParameters.Length;
239 for (; i < top; i++) {
240 p = Parameters.FixedParameters [i];
241 p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
243 if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
247 if (Parameters.ArrayParameter != null){
248 Parameter p = Parameters.ArrayParameter;
249 p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
252 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
254 TypeManager.RegisterMethod (InvokeBuilder,
255 new InternalParameters (param_types, Parameters),
261 int params_num = param_types.Length;
262 Type [] async_param_types = new Type [params_num + 2];
264 param_types.CopyTo (async_param_types, 0);
266 async_param_types [params_num] = TypeManager.asynccallback_type;
267 async_param_types [params_num + 1] = TypeManager.object_type;
269 mattr = MethodAttributes.Public | MethodAttributes.HideBySig |
270 MethodAttributes.Virtual | MethodAttributes.NewSlot;
272 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
275 TypeManager.iasyncresult_type,
279 if (Parameters.FixedParameters != null){
280 int top = Parameters.FixedParameters.Length;
283 for (i = 0 ; i < top; i++) {
284 p = Parameters.FixedParameters [i];
286 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
289 if (Parameters.ArrayParameter != null){
290 Parameter p = Parameters.ArrayParameter;
291 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
296 BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
297 BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
299 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
301 Parameter [] async_params = new Parameter [params_num + 2];
303 if (Parameters.FixedParameters != null){
304 Parameters.FixedParameters.CopyTo (async_params, 0);
305 n = Parameters.FixedParameters.Length;
307 if (Parameters.ArrayParameter != null)
308 async_params [n] = Parameters.ArrayParameter;
310 async_params [params_num] = new Parameter (
311 TypeManager.system_asynccallback_expr, "callback",
312 Parameter.Modifier.NONE, null);
313 async_params [params_num + 1] = new Parameter (
314 TypeManager.system_object_expr, "object",
315 Parameter.Modifier.NONE, null);
317 Parameters async_parameters = new Parameters (async_params, null, Location);
319 TypeManager.RegisterMethod (BeginInvokeBuilder,
320 new InternalParameters (async_parameters.GetParameterInfo (ec), async_parameters),
324 // EndInvoke is a bit more interesting, all the parameters labeled as
325 // out or ref have to be duplicated here.
328 Type [] end_param_types = new Type [out_params + 1];
329 Parameter [] end_params = new Parameter [out_params + 1];
332 int top = Parameters.FixedParameters.Length;
333 for (i = 0; i < top; i++){
334 Parameter p = Parameters.FixedParameters [i];
335 if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
338 end_param_types [param] = param_types [i];
339 end_params [param] = p;
343 end_param_types [out_params] = TypeManager.iasyncresult_type;
344 end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null);
347 // Create method, define parameters, register parameters with type system
349 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_param_types);
350 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
353 // EndInvoke: Label the parameters
355 EndInvokeBuilder.DefineParameter (out_params + 1, ParameterAttributes.None, "result");
356 for (i = 0; i < end_params.Length-1; i++){
357 EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
360 Parameters end_parameters = new Parameters (end_params, null, Location);
362 TypeManager.RegisterMethod (
364 new InternalParameters (end_parameters.GetParameterInfo (ec), 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 = TypeManager.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 = TypeManager.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 MethodGroupExpr me = ml as MethodGroupExpr;
496 Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
500 MethodBase mb = me.Methods [0];
501 ParameterData pd = TypeManager.GetParameterData (mb);
503 int pd_count = pd.Count;
505 bool params_method = (pd_count != 0) &&
506 (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS);
508 bool is_params_applicable = false;
509 bool is_applicable = Invocation.IsApplicable (ec, me, args, arg_count, ref mb);
511 if (!is_applicable && params_method &&
512 Invocation.IsParamsMethodApplicable (ec, me, args, arg_count, ref mb))
513 is_applicable = is_params_applicable = true;
515 if (!is_applicable && !params_method && arg_count != pd_count) {
516 Report.Error (1593, loc,
517 "Delegate '{0}' does not take {1} arguments",
518 delegate_type.ToString (), arg_count);
522 return Invocation.VerifyArgumentsCompat (
523 ec, args, arg_count, mb,
524 is_params_applicable || (!is_applicable && params_method),
525 delegate_type, false, loc);
529 /// Verifies whether the delegate in question is compatible with this one in
530 /// order to determine if instantiation from the same is possible.
532 public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Type probe_type, Location loc)
534 Expression ml = Expression.MemberLookup (
535 ec, delegate_type, "Invoke", loc);
537 if (!(ml is MethodGroupExpr)) {
538 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
542 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
543 ParameterData pd = TypeManager.GetParameterData (mb);
545 Expression probe_ml = Expression.MemberLookup (
546 ec, delegate_type, "Invoke", loc);
548 if (!(probe_ml is MethodGroupExpr)) {
549 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
553 MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];
554 ParameterData probe_pd = TypeManager.GetParameterData (probe_mb);
556 if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)
559 if (pd.Count != probe_pd.Count)
562 for (int i = pd.Count; i > 0; ) {
565 if (pd.ParameterType (i) != probe_pd.ParameterType (i) ||
566 pd.ParameterModifier (i) != probe_pd.ParameterModifier (i))
573 public static string FullDelegateDesc (Type del_type, MethodBase mb, ParameterData pd)
575 StringBuilder sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
577 sb.Append (" " + del_type.ToString ());
580 int length = pd.Count;
582 for (int i = length; i > 0; ) {
585 sb.Append (pd.ParameterDesc (length - i - 1));
591 return sb.ToString ();
595 // Hack around System.Reflection as found everywhere else
596 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
597 MemberFilter filter, object criteria)
599 ArrayList members = new ArrayList ();
601 if ((mt & MemberTypes.Method) != 0) {
602 if (ConstructorBuilder != null)
603 if (filter (ConstructorBuilder, criteria))
604 members.Add (ConstructorBuilder);
606 if (InvokeBuilder != null)
607 if (filter (InvokeBuilder, criteria))
608 members.Add (InvokeBuilder);
610 if (BeginInvokeBuilder != null)
611 if (filter (BeginInvokeBuilder, criteria))
612 members.Add (BeginInvokeBuilder);
614 if (EndInvokeBuilder != null)
615 if (filter (EndInvokeBuilder, criteria))
616 members.Add (EndInvokeBuilder);
619 return new MemberList (members);
622 public override MemberCache MemberCache {
628 public Expression InstanceExpression {
630 return instance_expr;
633 instance_expr = value;
637 public MethodBase TargetMethod {
639 return delegate_method;
642 delegate_method = value;
646 public Type TargetReturnType {
652 public Type [] ParameterTypes {
658 public override AttributeTargets AttributeTargets {
660 return AttributeTargets.Delegate;
665 // Represents header string for documentation comment.
667 public override string DocCommentHeader {
671 protected override void VerifyObsoleteAttribute()
673 CheckUsageOfObsoleteAttribute (ret_type);
675 foreach (Type type in param_types) {
676 CheckUsageOfObsoleteAttribute (type);
682 // Base class for `NewDelegate' and `ImplicitDelegateCreation'
684 public abstract class DelegateCreation : Expression {
685 protected MethodBase constructor_method;
686 protected MethodBase delegate_method;
687 protected MethodGroupExpr method_group;
688 protected Expression delegate_instance_expression;
690 public DelegateCreation () {}
692 public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
695 MethodInfo found_method = (MethodInfo)mg.Methods [0];
697 if (mg.Methods.Length > 1)
698 method_desc = found_method.Name;
700 method_desc = Invocation.FullMethodDesc (found_method);
702 Expression invoke_method = Expression.MemberLookup (
703 ec, type, "Invoke", MemberTypes.Method,
704 Expression.AllBindingFlags, loc);
705 MethodInfo method = ((MethodGroupExpr) invoke_method).Methods [0] as MethodInfo;
707 ParameterData param = TypeManager.GetParameterData (method);
708 string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
710 if (method.ReturnType != found_method.ReturnType) {
711 Report.Error (407, loc, "'{0}' has the wrong return type to match delegate '{1}'", method_desc, delegate_desc);
713 Report.Error (123, loc, "Method '" + method_desc + "' does not " +
714 "match delegate '" + delegate_desc + "'");
718 public override void Emit (EmitContext ec)
720 if (delegate_instance_expression == null || delegate_method.IsStatic)
721 ec.ig.Emit (OpCodes.Ldnull);
723 delegate_instance_expression.Emit (ec);
725 if (delegate_method.IsVirtual && !method_group.IsBase) {
726 ec.ig.Emit (OpCodes.Dup);
727 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
729 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
730 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
733 protected bool ResolveConstructorMethod (EmitContext ec)
735 Expression ml = Expression.MemberLookup (
736 ec, type, ".ctor", loc);
738 if (!(ml is MethodGroupExpr)) {
739 Report.Error (-100, loc, "Internal error: Could not find delegate constructor!");
743 constructor_method = ((MethodGroupExpr) ml).Methods [0];
747 protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
749 foreach (MethodInfo mi in mg.Methods){
750 delegate_method = Delegate.VerifyMethod (ec, type, mi, loc);
752 if (delegate_method != null)
756 if (delegate_method == null) {
757 Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
762 // Check safe/unsafe of the delegate
765 ParameterData param = TypeManager.GetParameterData (delegate_method);
766 int count = param.Count;
768 for (int i = 0; i < count; i++){
769 if (param.ParameterType (i).IsPointer){
770 Expression.UnsafeError (loc);
776 //TODO: implement caching when performance will be low
777 IMethodData md = TypeManager.GetMethod (delegate_method);
779 if (System.Attribute.GetCustomAttribute (delegate_method, TypeManager.conditional_attribute_type) != null) {
780 Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
783 if (md.OptAttributes != null && md.OptAttributes.Search (TypeManager.conditional_attribute_type, ec) != null) {
784 Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
788 if (mg.InstanceExpression != null)
789 delegate_instance_expression = mg.InstanceExpression.Resolve (ec);
790 else if (ec.IsStatic) {
791 if (!delegate_method.IsStatic) {
792 Report.Error (120, loc,
793 "An object reference is required for the non-static method " +
794 delegate_method.Name);
797 delegate_instance_expression = null;
799 delegate_instance_expression = ec.GetThis (loc);
801 if (delegate_instance_expression != null && delegate_instance_expression.Type.IsValueType)
802 delegate_instance_expression = new BoxedCast (delegate_instance_expression);
805 eclass = ExprClass.Value;
811 // Created from the conversion code
813 public class ImplicitDelegateCreation : DelegateCreation {
815 ImplicitDelegateCreation (Type t, Location l)
821 public override Expression DoResolve (EmitContext ec)
826 static public Expression Create (EmitContext ec, MethodGroupExpr mge, Type target_type, Location loc)
828 ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
829 if (d.ResolveConstructorMethod (ec))
830 return d.ResolveMethodGroupExpr (ec, mge);
837 // A delegate-creation-expression, invoked from the `New' class
839 public class NewDelegate : DelegateCreation {
840 public ArrayList Arguments;
843 // This constructor is invoked from the `New' expression
845 public NewDelegate (Type type, ArrayList Arguments, Location loc)
848 this.Arguments = Arguments;
852 public override Expression DoResolve (EmitContext ec)
854 if (Arguments == null || Arguments.Count != 1) {
855 Report.Error (149, loc,
856 "Method name expected");
860 if (!ResolveConstructorMethod (ec))
863 Argument a = (Argument) Arguments [0];
865 if (!a.ResolveMethodGroup (ec, loc))
868 Expression e = a.Expr;
870 if (e is AnonymousMethod && RootContext.Version != LanguageVersion.ISO_1)
871 return ((AnonymousMethod) e).Compatible (ec, type, false);
873 MethodGroupExpr mg = e as MethodGroupExpr;
875 return ResolveMethodGroupExpr (ec, mg);
877 Type e_type = e.Type;
879 if (!TypeManager.IsDelegateType (e_type)) {
880 Report.Error (149, loc, "Method name expected");
884 method_group = Expression.MemberLookup (
885 ec, type, "Invoke", MemberTypes.Method,
886 Expression.AllBindingFlags, loc) as MethodGroupExpr;
888 if (method_group == null) {
889 Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
893 // This is what MS' compiler reports. We could always choose
894 // to be more verbose and actually give delegate-level specifics
895 if (!Delegate.VerifyDelegate (ec, type, e_type, loc)) {
896 Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " +
897 "to type '" + type + "'");
901 delegate_instance_expression = e;
902 delegate_method = method_group.Methods [0];
904 eclass = ExprClass.Value;
909 public class DelegateInvocation : ExpressionStatement {
911 public Expression InstanceExpr;
912 public ArrayList Arguments;
916 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
918 this.InstanceExpr = instance_expr;
919 this.Arguments = args;
923 public override Expression DoResolve (EmitContext ec)
925 if (InstanceExpr is EventExpr) {
927 EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
929 Expression ml = MemberLookup (
930 ec, ec.ContainerType, ei.Name,
931 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
935 // If this is the case, then the Event does not belong
936 // to this Type and so, according to the spec
937 // cannot be accessed directly
939 // Note that target will not appear as an EventExpr
940 // in the case it is being referenced within the same type container;
941 // it will appear as a FieldExpr in that case.
944 Assign.error70 (ei, loc);
950 Type del_type = InstanceExpr.Type;
951 if (del_type == null)
954 if (Arguments != null){
955 foreach (Argument a in Arguments){
956 if (!a.Resolve (ec, loc))
961 if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
964 Expression lookup = Expression.MemberLookup (ec, del_type, "Invoke", loc);
965 if (!(lookup is MethodGroupExpr)) {
966 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
970 method = ((MethodGroupExpr) lookup).Methods [0];
971 type = ((MethodInfo) method).ReturnType;
972 eclass = ExprClass.Value;
977 public override void Emit (EmitContext ec)
980 // Invocation on delegates call the virtual Invoke member
981 // so we are always `instance' calls
983 Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, loc);
986 public override void EmitStatement (EmitContext ec)
990 // Pop the return value if there is one
992 if (method is MethodInfo){
993 if (((MethodInfo) method).ReturnType != TypeManager.void_type)
994 ec.ig.Emit (OpCodes.Pop);