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 if (TypeManager.multicast_delegate_type == null && !RootContext.StdLib) {
85 Namespace system = Namespace.LookupNamespace ("System", true);
86 TypeExpr expr = system.Lookup (this, "MulticastDelegate", Location) as TypeExpr;
87 TypeManager.multicast_delegate_type = expr.ResolveType (ec);
90 if (TypeManager.multicast_delegate_type == null)
91 throw new InternalErrorException ("System.MulticastDelegate unresolved");
94 if (TypeManager.NamespaceClash (Name, Location))
97 ModuleBuilder builder = CodeGen.Module.Builder;
99 TypeBuilder = builder.DefineType (
100 Name, TypeAttr, TypeManager.multicast_delegate_type);
102 TypeBuilder builder = Parent.TypeBuilder;
104 string name = Name.Substring (1 + Name.LastIndexOf ('.'));
105 TypeBuilder = builder.DefineNestedType (
106 name, TypeAttr, TypeManager.multicast_delegate_type);
109 TypeManager.AddUserType (Name, this);
114 public override bool Define ()
116 MethodAttributes mattr;
120 throw new InternalErrorException ("Define called before DefineType?");
122 // FIXME: POSSIBLY make this static, as it is always constant
124 Type [] const_arg_types = new Type [2];
125 const_arg_types [0] = TypeManager.object_type;
126 const_arg_types [1] = TypeManager.intptr_type;
128 mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
129 MethodAttributes.HideBySig | MethodAttributes.Public;
131 ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
132 CallingConventions.Standard,
135 ConstructorBuilder.DefineParameter (1, ParameterAttributes.None, "object");
136 ConstructorBuilder.DefineParameter (2, ParameterAttributes.None, "method");
138 // HACK because System.Reflection.Emit is lame
140 Parameter [] fixed_pars = new Parameter [2];
141 fixed_pars [0] = new Parameter (TypeManager.system_object_expr, "object",
142 Parameter.Modifier.NONE, null, Location);
143 fixed_pars [1] = new Parameter (TypeManager.system_intptr_expr, "method",
144 Parameter.Modifier.NONE, null, Location);
145 Parameters const_parameters = new Parameters (fixed_pars, null);
147 TypeManager.RegisterMethod (
149 new InternalParameters (const_arg_types, const_parameters),
153 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
156 // Here the various methods like Invoke, BeginInvoke etc are defined
158 // First, call the `out of band' special method for
159 // defining recursively any types we need:
161 param_types = Parameters.GetParameterInfo (ec);
162 if (param_types == null)
169 // Check accessibility
170 foreach (Type partype in param_types){
171 if (!Parent.AsAccessible (partype, ModFlags)) {
172 Report.Error (59, Location,
173 "Inconsistent accessibility: parameter type `" +
174 TypeManager.CSharpName (partype) + "' is less " +
175 "accessible than delegate `" + Name + "'");
178 if (partype.IsPointer && !UnsafeOK (Parent))
182 ReturnType = ReturnType.ResolveAsTypeTerminal (ec, false);
183 if (ReturnType == null)
186 ret_type = ReturnType.Type;
187 if (ret_type == null)
190 if (!Parent.AsAccessible (ret_type, ModFlags)) {
191 Report.Error (58, Location,
192 "Inconsistent accessibility: return type `" +
193 TypeManager.CSharpName (ret_type) + "' is less " +
194 "accessible than delegate `" + Name + "'");
198 if (ret_type.IsPointer && !UnsafeOK (Parent))
201 if (RootContext.StdLib && (ret_type == TypeManager.arg_iterator_type || ret_type == TypeManager.typed_reference_type)) {
202 Method.Error1599 (Location, ret_type);
207 // We don't have to check any others because they are all
208 // guaranteed to be accessible - they are standard types.
211 CallingConventions cc = Parameters.GetCallingConvention ();
213 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
215 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
222 // Define parameters, and count out/ref parameters
226 if (Parameters.FixedParameters != null){
227 int top = Parameters.FixedParameters.Length;
230 for (; i < top; i++) {
231 p = Parameters.FixedParameters [i];
232 p.DefineParameter (ec, InvokeBuilder, null, i + 1);
234 if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
238 if (Parameters.ArrayParameter != null){
239 if (TypeManager.param_array_type == null && !RootContext.StdLib) {
240 Namespace system = Namespace.LookupNamespace ("System", true);
241 TypeExpr expr = system.Lookup (this, "ParamArrayAttribute", Location) as TypeExpr;
242 TypeManager.param_array_type = expr.ResolveType (ec);
245 if (TypeManager.cons_param_array_attribute == null) {
246 Type [] void_arg = { };
247 TypeManager.cons_param_array_attribute = TypeManager.GetConstructor (
248 TypeManager.param_array_type, void_arg);
251 ParameterBuilder pb = InvokeBuilder.DefineParameter (
252 i + 1, Parameters.ArrayParameter.Attributes,Parameters.ArrayParameter.Name);
254 pb.SetCustomAttribute (
255 new CustomAttributeBuilder (TypeManager.cons_param_array_attribute, new object [0]));
258 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
260 TypeManager.RegisterMethod (InvokeBuilder,
261 new InternalParameters (param_types, Parameters),
267 int params_num = param_types.Length;
268 Type [] async_param_types = new Type [params_num + 2];
270 param_types.CopyTo (async_param_types, 0);
272 async_param_types [params_num] = TypeManager.asynccallback_type;
273 async_param_types [params_num + 1] = TypeManager.object_type;
275 mattr = MethodAttributes.Public | MethodAttributes.HideBySig |
276 MethodAttributes.Virtual | MethodAttributes.NewSlot;
278 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
281 TypeManager.iasyncresult_type,
285 if (Parameters.FixedParameters != null){
286 int top = Parameters.FixedParameters.Length;
289 for (i = 0 ; i < top; i++) {
290 p = Parameters.FixedParameters [i];
292 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1);
295 if (Parameters.ArrayParameter != null){
296 Parameter p = Parameters.ArrayParameter;
297 p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1);
302 BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
303 BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
305 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
307 Parameter [] async_params = new Parameter [params_num + 2];
309 if (Parameters.FixedParameters != null){
310 Parameters.FixedParameters.CopyTo (async_params, 0);
311 n = Parameters.FixedParameters.Length;
313 if (Parameters.ArrayParameter != null)
314 async_params [n] = Parameters.ArrayParameter;
316 async_params [params_num] = new Parameter (
317 TypeManager.system_asynccallback_expr, "callback",
318 Parameter.Modifier.NONE, null, Location);
319 async_params [params_num + 1] = new Parameter (
320 TypeManager.system_object_expr, "object",
321 Parameter.Modifier.NONE, null, Location);
323 Parameters async_parameters = new Parameters (async_params, null);
325 TypeManager.RegisterMethod (BeginInvokeBuilder,
326 new InternalParameters (async_parameters.GetParameterInfo (ec), async_parameters),
330 // EndInvoke is a bit more interesting, all the parameters labeled as
331 // out or ref have to be duplicated here.
334 Type [] end_param_types = new Type [out_params + 1];
335 Parameter [] end_params = new Parameter [out_params + 1];
338 int top = Parameters.FixedParameters.Length;
339 for (i = 0; i < top; i++){
340 Parameter p = Parameters.FixedParameters [i];
341 if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
344 end_param_types [param] = param_types [i];
345 end_params [param] = p;
349 end_param_types [out_params] = TypeManager.iasyncresult_type;
350 end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null, Location);
353 // Create method, define parameters, register parameters with type system
355 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_param_types);
356 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
359 // EndInvoke: Label the parameters
361 EndInvokeBuilder.DefineParameter (out_params + 1, ParameterAttributes.None, "result");
362 for (i = 0; i < end_params.Length-1; i++){
363 EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
366 Parameters end_parameters = new Parameters (end_params, null);
368 TypeManager.RegisterMethod (
370 new InternalParameters (end_parameters.GetParameterInfo (ec), end_parameters),
376 public override void Emit ()
378 if (OptAttributes != null) {
379 Parameters.LabelParameters (ec, InvokeBuilder);
380 OptAttributes.Emit (ec, this);
386 protected override TypeAttributes TypeAttr {
388 return Modifiers.TypeAttr (ModFlags, IsTopLevel) |
389 TypeAttributes.Class | TypeAttributes.Sealed |
394 public override string[] ValidAttributeTargets {
396 return attribute_targets;
401 protected override bool VerifyClsCompliance (DeclSpace ds)
403 if (!base.VerifyClsCompliance (ds)) {
407 AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
409 if (!AttributeTester.IsClsCompliant (ReturnType.Type)) {
410 Report.Error (3002, Location, "Return type of `{0}' is not CLS-compliant", GetSignatureForError ());
416 // Returns the MethodBase for "Invoke" from a delegate type, this is used
417 // to extract the signature of a delegate.
419 public static MethodInfo GetInvokeMethod (EmitContext ec, Type delegate_type, Location loc)
421 Expression ml = Expression.MemberLookup (
422 ec, delegate_type, "Invoke", loc);
424 if (!(ml is MethodGroupExpr)) {
425 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
429 return (MethodInfo) (((MethodGroupExpr) ml).Methods [0]);
433 /// Verifies whether the method in question is compatible with the delegate
434 /// Returns the method itself if okay and null if not.
436 public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb,
439 ParameterData pd = TypeManager.GetParameterData (mb);
441 int pd_count = pd.Count;
443 MethodBase invoke_mb = GetInvokeMethod (ec, delegate_type, loc);
444 if (invoke_mb == null)
447 ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
449 if (invoke_pd.Count != pd_count)
452 for (int i = pd_count; i > 0; ) {
455 Type invoke_pd_type = invoke_pd.ParameterType (i);
456 Type pd_type = pd.ParameterType (i);
457 Parameter.Modifier invoke_pd_type_mod = invoke_pd.ParameterModifier (i);
458 Parameter.Modifier pd_type_mod = pd.ParameterModifier (i);
460 if (invoke_pd_type == pd_type &&
461 invoke_pd_type_mod == pd_type_mod)
464 if (invoke_pd_type.IsSubclassOf (pd_type) &&
465 invoke_pd_type_mod == pd_type_mod)
466 if (RootContext.Version == LanguageVersion.ISO_1) {
467 Report.FeatureIsNotStandardized (loc, "contravariance");
475 Type invoke_mb_retval = ((MethodInfo) invoke_mb).ReturnType;
476 Type mb_retval = ((MethodInfo) mb).ReturnType;
477 if (invoke_mb_retval == mb_retval)
480 if (mb_retval.IsSubclassOf (invoke_mb_retval))
481 if (RootContext.Version == LanguageVersion.ISO_1) {
482 Report.FeatureIsNotStandardized (loc, "covariance");
492 // Verifies whether the invocation arguments are compatible with the
493 // delegate's target method
495 public static bool VerifyApplicability (EmitContext ec, Type delegate_type,
496 ArrayList args, Location loc)
503 arg_count = args.Count;
505 Expression ml = Expression.MemberLookup (
506 ec, delegate_type, "Invoke", loc);
508 MethodGroupExpr me = ml as MethodGroupExpr;
510 Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
514 MethodBase mb = me.Methods [0];
515 ParameterData pd = TypeManager.GetParameterData (mb);
517 int pd_count = pd.Count;
519 bool params_method = (pd_count != 0) &&
520 (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS);
522 bool is_params_applicable = false;
523 bool is_applicable = Invocation.IsApplicable (ec, me, args, arg_count, ref mb);
525 if (!is_applicable && params_method &&
526 Invocation.IsParamsMethodApplicable (ec, me, args, arg_count, ref mb))
527 is_applicable = is_params_applicable = true;
529 if (!is_applicable && !params_method && arg_count != pd_count) {
530 Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
531 TypeManager.CSharpName (delegate_type), arg_count);
535 return Invocation.VerifyArgumentsCompat (
536 ec, args, arg_count, mb,
537 is_params_applicable || (!is_applicable && params_method),
538 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 = TypeManager.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 = TypeManager.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 ();
589 sb.Append (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
591 sb.Append (TypeManager.CSharpName (del_type));
592 sb.Append (pd.GetSignatureForError ());
593 return sb.ToString ();
596 // Hack around System.Reflection as found everywhere else
597 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
598 MemberFilter filter, object criteria)
600 ArrayList members = new ArrayList ();
602 if ((mt & MemberTypes.Method) != 0) {
603 if (ConstructorBuilder != null)
604 if (filter (ConstructorBuilder, criteria))
605 members.Add (ConstructorBuilder);
607 if (InvokeBuilder != null)
608 if (filter (InvokeBuilder, criteria))
609 members.Add (InvokeBuilder);
611 if (BeginInvokeBuilder != null)
612 if (filter (BeginInvokeBuilder, criteria))
613 members.Add (BeginInvokeBuilder);
615 if (EndInvokeBuilder != null)
616 if (filter (EndInvokeBuilder, criteria))
617 members.Add (EndInvokeBuilder);
620 return new MemberList (members);
623 public override MemberCache MemberCache {
629 public Expression InstanceExpression {
631 return instance_expr;
634 instance_expr = value;
638 public MethodBase TargetMethod {
640 return delegate_method;
643 delegate_method = value;
647 public Type TargetReturnType {
653 public Type [] ParameterTypes {
659 public override AttributeTargets AttributeTargets {
661 return AttributeTargets.Delegate;
666 // Represents header string for documentation comment.
668 public override string DocCommentHeader {
672 protected override void VerifyObsoleteAttribute()
674 CheckUsageOfObsoleteAttribute (ret_type);
676 foreach (Type type in param_types) {
677 CheckUsageOfObsoleteAttribute (type);
683 // Base class for `NewDelegate' and `ImplicitDelegateCreation'
685 public abstract class DelegateCreation : Expression {
686 protected MethodBase constructor_method;
687 protected MethodBase delegate_method;
688 protected MethodGroupExpr method_group;
689 protected Expression delegate_instance_expression;
691 public DelegateCreation () {}
693 public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
696 MethodInfo found_method = (MethodInfo)mg.Methods [0];
698 if (mg.Methods.Length > 1)
699 method_desc = found_method.Name;
701 method_desc = Invocation.FullMethodDesc (found_method);
703 Expression invoke_method = Expression.MemberLookup (
704 ec, type, "Invoke", MemberTypes.Method,
705 Expression.AllBindingFlags, loc);
706 MethodInfo method = ((MethodGroupExpr) invoke_method).Methods [0] as MethodInfo;
708 ParameterData param = TypeManager.GetParameterData (method);
709 string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
711 if (method.ReturnType != found_method.ReturnType) {
712 Report.Error (407, loc, "`{0}' has the wrong return type to match the delegate `{1}'", method_desc, delegate_desc);
714 Report.Error (123, loc, "Method `{0}' does not match delegate `{1}'", method_desc, 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,
750 foreach (MethodInfo mi in mg.Methods){
751 delegate_method = Delegate.VerifyMethod (ec, type, mi, loc);
753 if (delegate_method != null)
757 if (delegate_method == null) {
759 Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
764 // Check safe/unsafe of the delegate
767 ParameterData param = TypeManager.GetParameterData (delegate_method);
768 int count = param.Count;
770 for (int i = 0; i < count; i++){
771 if (param.ParameterType (i).IsPointer){
772 Expression.UnsafeError (loc);
778 //TODO: implement caching when performance will be low
779 IMethodData md = TypeManager.GetMethod (delegate_method);
781 if (System.Attribute.GetCustomAttribute (delegate_method, TypeManager.conditional_attribute_type) != null) {
782 Report.Error (1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
785 md.SetMemberIsUsed ();
786 if (md.OptAttributes != null && md.OptAttributes.Search (TypeManager.conditional_attribute_type, ec) != null) {
787 Report.Error (1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
791 if (mg.InstanceExpression != null)
792 delegate_instance_expression = mg.InstanceExpression.Resolve (ec);
793 else if (ec.IsStatic) {
794 if (!delegate_method.IsStatic) {
795 Report.Error (120, loc, "`{0}': An object reference is required for the nonstatic field, method or property",
796 TypeManager.CSharpSignature (delegate_method));
799 delegate_instance_expression = null;
801 delegate_instance_expression = ec.GetThis (loc);
803 if (delegate_instance_expression != null && delegate_instance_expression.Type.IsValueType)
804 delegate_instance_expression = new BoxedCast (delegate_instance_expression);
807 eclass = ExprClass.Value;
813 // Created from the conversion code
815 public class ImplicitDelegateCreation : DelegateCreation {
817 ImplicitDelegateCreation (Type t, Location l)
823 public override Expression DoResolve (EmitContext ec)
828 static public Expression Create (EmitContext ec, MethodGroupExpr mge,
829 Type target_type, bool check_only, Location loc)
831 ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
832 if (d.ResolveConstructorMethod (ec))
833 return d.ResolveMethodGroupExpr (ec, mge, check_only);
840 // A delegate-creation-expression, invoked from the `New' class
842 public class NewDelegate : DelegateCreation {
843 public ArrayList Arguments;
846 // This constructor is invoked from the `New' expression
848 public NewDelegate (Type type, ArrayList Arguments, Location loc)
851 this.Arguments = Arguments;
855 public override Expression DoResolve (EmitContext ec)
857 if (Arguments == null || Arguments.Count != 1) {
858 Report.Error (149, loc,
859 "Method name expected");
863 if (!ResolveConstructorMethod (ec))
866 Argument a = (Argument) Arguments [0];
868 if (!a.ResolveMethodGroup (ec, loc))
871 Expression e = a.Expr;
873 if (e is AnonymousMethod && RootContext.Version != LanguageVersion.ISO_1)
874 return ((AnonymousMethod) e).Compatible (ec, type, false);
876 MethodGroupExpr mg = e as MethodGroupExpr;
878 return ResolveMethodGroupExpr (ec, mg, false);
880 Type e_type = e.Type;
882 if (!TypeManager.IsDelegateType (e_type)) {
883 Report.Error (149, loc, "Method name expected");
887 method_group = Expression.MemberLookup (
888 ec, type, "Invoke", MemberTypes.Method,
889 Expression.AllBindingFlags, loc) as MethodGroupExpr;
891 if (method_group == null) {
892 Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
896 // This is what MS' compiler reports. We could always choose
897 // to be more verbose and actually give delegate-level specifics
898 if (!Delegate.VerifyDelegate (ec, type, e_type, loc)) {
899 Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " +
900 "to type '" + type + "'");
904 delegate_instance_expression = e;
905 delegate_method = method_group.Methods [0];
907 eclass = ExprClass.Value;
912 public class DelegateInvocation : ExpressionStatement {
914 public Expression InstanceExpr;
915 public ArrayList Arguments;
919 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
921 this.InstanceExpr = instance_expr;
922 this.Arguments = args;
926 public override Expression DoResolve (EmitContext ec)
928 if (InstanceExpr is EventExpr) {
930 EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
932 Expression ml = MemberLookup (
933 ec, ec.ContainerType, ei.Name,
934 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
938 // If this is the case, then the Event does not belong
939 // to this Type and so, according to the spec
940 // cannot be accessed directly
942 // Note that target will not appear as an EventExpr
943 // in the case it is being referenced within the same type container;
944 // it will appear as a FieldExpr in that case.
947 Assign.error70 (ei, loc);
953 Type del_type = InstanceExpr.Type;
954 if (del_type == null)
957 if (Arguments != null){
958 foreach (Argument a in Arguments){
959 if (!a.Resolve (ec, loc))
964 if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
967 Expression lookup = Expression.MemberLookup (ec, del_type, "Invoke", loc);
968 if (!(lookup is MethodGroupExpr)) {
969 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
973 method = ((MethodGroupExpr) lookup).Methods [0];
974 type = ((MethodInfo) method).ReturnType;
975 eclass = ExprClass.Value;
980 public override void Emit (EmitContext ec)
983 // Invocation on delegates call the virtual Invoke member
984 // so we are always `instance' calls
986 Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, loc);
989 public override void EmitStatement (EmitContext ec)
993 // Pop the return value if there is one
995 if (method is MethodInfo){
996 if (((MethodInfo) method).ReturnType != TypeManager.void_type)
997 ec.ig.Emit (OpCodes.Pop);