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)
83 foreach (TypeParameter type_param in TypeParameters)
84 if (!type_param.Resolve (this))
88 TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel) |
89 TypeAttributes.Class | TypeAttributes.Sealed;
92 if (TypeManager.NamespaceClash (Name, Location))
95 ModuleBuilder builder = CodeGen.Module.Builder;
97 TypeBuilder = builder.DefineType (
98 Name, attr, TypeManager.multicast_delegate_type);
100 TypeBuilder builder = Parent.TypeBuilder;
102 string name = Name.Substring (1 + Name.LastIndexOf ('.'));
103 TypeBuilder = builder.DefineNestedType (
104 name, attr, TypeManager.multicast_delegate_type);
107 TypeManager.AddDelegateType (Name, TypeBuilder, this);
110 CurrentType = new ConstructedType (
111 Name, TypeParameters, Location);
113 EmitContext ec = new EmitContext (
114 this, this, Location, null, null, ModFlags, false);
116 string[] param_names = new string [TypeParameters.Length];
117 for (int i = 0; i < TypeParameters.Length; i++)
118 param_names [i] = TypeParameters [i].Name;
120 GenericTypeParameterBuilder[] gen_params;
122 gen_params = TypeBuilder.DefineGenericParameters (param_names);
124 for (int i = 0; i < gen_params.Length; i++)
125 TypeParameters [i].Define (gen_params [i]);
127 foreach (TypeParameter type_param in TypeParameters) {
128 if (!type_param.DefineType (ec))
136 public override bool DefineMembers (TypeContainer container)
141 public override bool Define ()
143 MethodAttributes mattr;
145 EmitContext ec = new EmitContext (this, this, Location, null,
146 null, ModFlags, false);
149 foreach (TypeParameter type_param in TypeParameters)
150 type_param.DefineType (ec);
153 // FIXME: POSSIBLY make this static, as it is always constant
155 Type [] const_arg_types = new Type [2];
156 const_arg_types [0] = TypeManager.object_type;
157 const_arg_types [1] = TypeManager.intptr_type;
159 mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
160 MethodAttributes.HideBySig | MethodAttributes.Public;
162 ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
163 CallingConventions.Standard,
166 ConstructorBuilder.DefineParameter (1, ParameterAttributes.None, "object");
167 ConstructorBuilder.DefineParameter (2, ParameterAttributes.None, "method");
169 // HACK because System.Reflection.Emit is lame
172 // FIXME: POSSIBLY make these static, as they are always the same
173 Parameter [] fixed_pars = new Parameter [2];
174 fixed_pars [0] = new Parameter (null, null, Parameter.Modifier.NONE, null);
175 fixed_pars [1] = new Parameter (null, null, Parameter.Modifier.NONE, null);
176 Parameters const_parameters = new Parameters (fixed_pars, null, Location);
178 TypeManager.RegisterMethod (
180 new InternalParameters (const_arg_types, const_parameters),
184 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
187 // Here the various methods like Invoke, BeginInvoke etc are defined
189 // First, call the `out of band' special method for
190 // defining recursively any types we need:
192 if (!Parameters.ComputeAndDefineParameterTypes (this))
195 param_types = Parameters.GetParameterInfo (this);
196 if (param_types == null)
203 // Check accessibility
204 foreach (Type partype in param_types){
205 if (!Parent.AsAccessible (partype, ModFlags)) {
206 Report.Error (59, Location,
207 "Inconsistent accessibility: parameter type `" +
208 TypeManager.CSharpName (partype) + "` is less " +
209 "accessible than delegate `" + Name + "'");
212 if (partype.IsPointer && !UnsafeOK (Parent))
216 ReturnType = ResolveTypeExpr (ReturnType, false, Location);
217 if (ReturnType == null)
220 ret_type = ReturnType.Type;
221 if (ret_type == null)
224 if (!Parent.AsAccessible (ret_type, ModFlags)) {
225 Report.Error (58, Location,
226 "Inconsistent accessibility: return type `" +
227 TypeManager.CSharpName (ret_type) + "` is less " +
228 "accessible than delegate `" + Name + "'");
232 if (ret_type.IsPointer && !UnsafeOK (Parent))
236 // We don't have to check any others because they are all
237 // guaranteed to be accessible - they are standard types.
240 CallingConventions cc = Parameters.GetCallingConvention ();
242 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
244 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
251 // Define parameters, and count out/ref parameters
255 if (Parameters.FixedParameters != null){
256 int top = Parameters.FixedParameters.Length;
259 for (; i < top; i++) {
260 p = Parameters.FixedParameters [i];
261 p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
263 if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
267 if (Parameters.ArrayParameter != null){
268 Parameter p = Parameters.ArrayParameter;
269 p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
272 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
274 TypeManager.RegisterMethod (InvokeBuilder,
275 new InternalParameters (Parent, Parameters),
281 int params_num = param_types.Length;
282 Type [] async_param_types = new Type [params_num + 2];
284 param_types.CopyTo (async_param_types, 0);
286 async_param_types [params_num] = TypeManager.asynccallback_type;
287 async_param_types [params_num + 1] = TypeManager.object_type;
289 mattr = MethodAttributes.Public | MethodAttributes.HideBySig |
290 MethodAttributes.Virtual | MethodAttributes.NewSlot;
292 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
295 TypeManager.iasyncresult_type,
299 if (Parameters.FixedParameters != null){
300 int top = Parameters.FixedParameters.Length;
303 for (i = 0 ; i < top; i++) {
304 p = Parameters.FixedParameters [i];
306 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
309 if (Parameters.ArrayParameter != null){
310 Parameter p = Parameters.ArrayParameter;
311 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
316 BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
317 BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
319 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
321 Parameter [] async_params = new Parameter [params_num + 2];
323 if (Parameters.FixedParameters != null){
324 Parameters.FixedParameters.CopyTo (async_params, 0);
325 n = Parameters.FixedParameters.Length;
327 if (Parameters.ArrayParameter != null)
328 async_params [n] = Parameters.ArrayParameter;
330 async_params [params_num] = new Parameter (
331 TypeManager.system_asynccallback_expr, "callback",
332 Parameter.Modifier.NONE, null);
333 async_params [params_num + 1] = new Parameter (
334 TypeManager.system_object_expr, "object",
335 Parameter.Modifier.NONE, null);
337 Parameters async_parameters = new Parameters (async_params, null, Location);
338 async_parameters.ComputeAndDefineParameterTypes (this);
340 async_parameters.ComputeAndDefineParameterTypes (this);
341 TypeManager.RegisterMethod (BeginInvokeBuilder,
342 new InternalParameters (Parent, async_parameters),
346 // EndInvoke is a bit more interesting, all the parameters labeled as
347 // out or ref have to be duplicated here.
350 Type [] end_param_types = new Type [out_params + 1];
351 Parameter [] end_params = new Parameter [out_params + 1];
354 int top = Parameters.FixedParameters.Length;
355 for (i = 0; i < top; i++){
356 Parameter p = Parameters.FixedParameters [i];
357 if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
360 end_param_types [param] = param_types [i];
361 end_params [param] = p;
365 end_param_types [out_params] = TypeManager.iasyncresult_type;
366 end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null);
369 // Create method, define parameters, register parameters with type system
371 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_param_types);
372 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
375 // EndInvoke: Label the parameters
377 EndInvokeBuilder.DefineParameter (out_params + 1, ParameterAttributes.None, "result");
378 for (i = 0; i < end_params.Length-1; i++){
379 EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
382 Parameters end_parameters = new Parameters (end_params, null, Location);
383 end_parameters.ComputeAndDefineParameterTypes (this);
385 TypeManager.RegisterMethod (
387 new InternalParameters (Parent, end_parameters),
393 public override void Emit ()
395 if (OptAttributes != null) {
396 EmitContext ec = new EmitContext (
397 Parent, this, Location, null, null, ModFlags, false);
398 Parameters.LabelParameters (ec, InvokeBuilder, Location);
399 OptAttributes.Emit (ec, this);
405 public override string[] ValidAttributeTargets {
407 return attribute_targets;
412 protected override bool VerifyClsCompliance (DeclSpace ds)
414 if (!base.VerifyClsCompliance (ds)) {
418 AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
420 if (!AttributeTester.IsClsCompliant (ReturnType.Type)) {
421 Report.Error (3002, Location, "Return type of '{0}' is not CLS-compliant", GetSignatureForError ());
427 /// Verifies whether the method in question is compatible with the delegate
428 /// Returns the method itself if okay and null if not.
430 public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb,
433 Expression ml = Expression.MemberLookup (
434 ec, delegate_type, "Invoke", loc);
436 if (!(ml is MethodGroupExpr)) {
437 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
441 MethodBase invoke_mb = ((MethodGroupExpr) ml).Methods [0];
443 ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
445 if (!((MethodGroupExpr) ml).HasTypeArguments &&
446 !Invocation.InferTypeArguments (ec, invoke_pd, ref mb))
449 ParameterData pd = Invocation.GetParameterData (mb);
450 int pd_count = pd.Count;
452 if (invoke_pd.Count != pd_count)
455 for (int i = pd_count; i > 0; ) {
458 if (invoke_pd.ParameterType (i) == pd.ParameterType (i) &&
459 invoke_pd.ParameterModifier (i) == pd.ParameterModifier (i))
466 if (((MethodInfo) invoke_mb).ReturnType == ((MethodInfo) mb).ReturnType)
473 // Verifies whether the invocation arguments are compatible with the
474 // delegate's target method
476 public static bool VerifyApplicability (EmitContext ec,
486 arg_count = args.Count;
488 Expression ml = Expression.MemberLookup (
489 ec, delegate_type, "Invoke", loc);
491 if (!(ml is MethodGroupExpr)) {
492 Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
496 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
497 ParameterData pd = Invocation.GetParameterData (mb);
499 int pd_count = pd.Count;
501 bool params_method = (pd_count != 0) &&
502 (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS);
504 if (!params_method && pd_count != arg_count) {
505 Report.Error (1593, loc,
506 "Delegate '" + delegate_type.ToString ()
507 + "' does not take '" + arg_count + "' arguments");
512 // Consider the case:
513 // delegate void FOO(param object[] args);
514 // FOO f = new FOO(...);
515 // f(new object[] {1, 2, 3});
517 // This should be treated like f(1,2,3). This is done by ignoring the
518 // 'param' modifier for that invocation. If that fails, then the
519 // 'param' modifier is considered.
521 // One issue is that 'VerifyArgumentsCompat' modifies the elements of
522 // the 'args' array. However, the modifications appear idempotent.
523 // Normal 'Invocation's also have the same behaviour, implicitly.
527 if (arg_count == pd_count)
528 ans = Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, false, delegate_type, loc);
529 if (!ans && params_method)
530 ans = Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, true, delegate_type, loc);
535 /// Verifies whether the delegate in question is compatible with this one in
536 /// order to determine if instantiation from the same is possible.
538 public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Type probe_type, Location loc)
540 Expression ml = Expression.MemberLookup (
541 ec, delegate_type, "Invoke", loc);
543 if (!(ml is MethodGroupExpr)) {
544 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
548 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
549 ParameterData pd = Invocation.GetParameterData (mb);
551 Expression probe_ml = Expression.MemberLookup (
552 ec, delegate_type, "Invoke", loc);
554 if (!(probe_ml is MethodGroupExpr)) {
555 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
559 MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];
560 ParameterData probe_pd = Invocation.GetParameterData (probe_mb);
562 if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)
565 if (pd.Count != probe_pd.Count)
568 for (int i = pd.Count; i > 0; ) {
571 if (pd.ParameterType (i) != probe_pd.ParameterType (i) ||
572 pd.ParameterModifier (i) != probe_pd.ParameterModifier (i))
579 public static string FullDelegateDesc (Type del_type, MethodBase mb, ParameterData pd)
581 StringBuilder sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
583 sb.Append (" " + del_type.ToString ());
586 int length = pd.Count;
588 for (int i = length; i > 0; ) {
591 sb.Append (pd.ParameterDesc (length - i - 1));
597 return sb.ToString ();
601 // Hack around System.Reflection as found everywhere else
602 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
603 MemberFilter filter, object criteria)
605 ArrayList members = new ArrayList ();
607 if ((mt & MemberTypes.Method) != 0) {
608 if (ConstructorBuilder != null)
609 if (filter (ConstructorBuilder, criteria))
610 members.Add (ConstructorBuilder);
612 if (InvokeBuilder != null)
613 if (filter (InvokeBuilder, criteria))
614 members.Add (InvokeBuilder);
616 if (BeginInvokeBuilder != null)
617 if (filter (BeginInvokeBuilder, criteria))
618 members.Add (BeginInvokeBuilder);
620 if (EndInvokeBuilder != null)
621 if (filter (EndInvokeBuilder, criteria))
622 members.Add (EndInvokeBuilder);
625 return new MemberList (members);
628 public override MemberCache MemberCache {
634 public Expression InstanceExpression {
636 return instance_expr;
639 instance_expr = value;
643 public MethodBase TargetMethod {
645 return delegate_method;
648 delegate_method = value;
652 public Type TargetReturnType {
658 public Type [] ParameterTypes {
664 public override AttributeTargets AttributeTargets {
666 return AttributeTargets.Delegate;
670 protected override void VerifyObsoleteAttribute()
672 CheckUsageOfObsoleteAttribute (ret_type);
674 foreach (Type type in param_types) {
675 CheckUsageOfObsoleteAttribute (type);
681 // Base class for `NewDelegate' and `ImplicitDelegateCreation'
683 public abstract class DelegateCreation : Expression {
684 protected MethodBase constructor_method;
685 protected MethodBase delegate_method;
686 protected MethodGroupExpr method_group;
688 public DelegateCreation () {}
690 public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
694 MethodBase candidate = mg.Methods [0];
695 if (mg.Methods.Length > 1)
696 method_desc = candidate.Name;
698 method_desc = Invocation.FullMethodDesc (candidate);
700 Expression invoke_method = Expression.MemberLookup (
701 ec, type, "Invoke", MemberTypes.Method,
702 Expression.AllBindingFlags, loc);
703 MethodBase method = ((MethodGroupExpr) invoke_method).Methods [0];
704 ParameterData param = Invocation.GetParameterData (method);
705 string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
707 if (!mg.HasTypeArguments &&
708 !Invocation.InferTypeArguments (ec, param, ref candidate))
709 Report.Error (411, loc, "The type arguments for " +
710 "method `{0}' cannot be infered from " +
711 "the usage. Try specifying the type " +
712 "arguments explicitly.", method_desc);
714 Report.Error (123, loc, "Method '{0}' does not " +
715 "match delegate '{1}'", method_desc,
719 public override void Emit (EmitContext ec)
721 if (method_group.InstanceExpression == null ||
722 delegate_method.IsStatic)
723 ec.ig.Emit (OpCodes.Ldnull);
725 method_group.InstanceExpression.Emit (ec);
727 if (delegate_method.IsVirtual && !method_group.IsBase) {
728 ec.ig.Emit (OpCodes.Dup);
729 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
731 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
732 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
735 protected bool ResolveConstructorMethod (EmitContext ec)
737 Expression ml = Expression.MemberLookup (
738 ec, type, ".ctor", loc);
740 if (!(ml is MethodGroupExpr)) {
741 Report.Error (-100, loc, "Internal error: Could not find delegate constructor!");
745 constructor_method = ((MethodGroupExpr) ml).Methods [0];
749 protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
751 foreach (MethodInfo mi in mg.Methods){
752 delegate_method = Delegate.VerifyMethod (
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 mg.InstanceExpression = 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 mg.InstanceExpression = null;
802 mg.InstanceExpression = ec.GetThis (loc);
804 if (mg.InstanceExpression != null && mg.InstanceExpression.Type.IsValueType)
805 mg.InstanceExpression = new BoxedCast (mg.InstanceExpression);
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 MethodGroupExpr mg = e as MethodGroupExpr;
875 return ResolveMethodGroupExpr (ec, mg);
877 Type e_type = e.Type;
879 if (!TypeManager.IsDelegateType (e_type)) {
880 e.Error_UnexpectedKind ("method");
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 method_group.InstanceExpression = 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);