2 // delegate.cs: Delegate Handler
5 // Ravi Pratap (ravi@ximian.com)
6 // Miguel de Icaza (miguel@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // Licensed under the terms of the GNU GPL
11 // (C) 2001 Ximian, Inc (http://www.ximian.com)
16 using System.Collections;
17 using System.Reflection;
18 using System.Reflection.Emit;
21 namespace Mono.CSharp {
26 public class Delegate : DeclSpace, IMemberContainer
28 public Expression ReturnType;
29 public Parameters Parameters;
31 public ConstructorBuilder ConstructorBuilder;
32 public MethodBuilder InvokeBuilder;
33 public MethodBuilder BeginInvokeBuilder;
34 public MethodBuilder EndInvokeBuilder;
38 static string[] attribute_targets = new string [] { "type", "return" };
40 Expression instance_expr;
41 MethodBase delegate_method;
42 ReturnParameter return_attributes;
44 MemberCache member_cache;
46 const int AllowedModifiers =
54 public Delegate (NamespaceEntry ns, DeclSpace parent, Expression type,
55 int mod_flags, MemberName name, Parameters param_list,
57 : base (ns, parent, name, attrs)
60 this.ReturnType = type;
61 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags,
62 IsTopLevel ? Modifiers.INTERNAL :
63 Modifiers.PRIVATE, name.Location);
64 Parameters = param_list;
67 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
69 if (a.Target == AttributeTargets.ReturnValue) {
70 if (return_attributes == null)
71 return_attributes = new ReturnParameter (InvokeBuilder, Location);
73 return_attributes.ApplyAttributeBuilder (a, cb);
77 base.ApplyAttributeBuilder (a, cb);
80 public override TypeBuilder DefineType ()
82 if (TypeBuilder != null)
85 if (TypeManager.multicast_delegate_type == null && !RootContext.StdLib) {
86 Namespace system = RootNamespace.Global.GetNamespace ("System", true);
87 TypeExpr expr = system.Lookup (this, "MulticastDelegate", Location) as TypeExpr;
88 TypeManager.multicast_delegate_type = expr.Type;
91 if (TypeManager.multicast_delegate_type == null)
92 Report.Error (-100, Location, "Internal error: delegate used before " +
93 "System.MulticastDelegate is resolved. This can only " +
94 "happen during corlib compilation, when using a delegate " +
95 "in any of the `core' classes. See bug #72015 for details.");
98 if (TypeManager.NamespaceClash (Name, Location))
101 ModuleBuilder builder = CodeGen.Module.Builder;
103 TypeBuilder = builder.DefineType (
104 Name, TypeAttr, TypeManager.multicast_delegate_type);
106 TypeBuilder builder = Parent.TypeBuilder;
108 string name = Name.Substring (1 + Name.LastIndexOf ('.'));
109 TypeBuilder = builder.DefineNestedType (
110 name, TypeAttr, TypeManager.multicast_delegate_type);
113 TypeManager.AddUserType (this);
117 string[] param_names = new string [TypeParameters.Length];
118 for (int i = 0; i < TypeParameters.Length; i++)
119 param_names [i] = TypeParameters [i].Name;
121 GenericTypeParameterBuilder[] gen_params;
122 gen_params = TypeBuilder.DefineGenericParameters (param_names);
124 int offset = CountTypeParameters - CurrentTypeParameters.Length;
125 for (int i = offset; i < gen_params.Length; i++)
126 CurrentTypeParameters [i - offset].Define (gen_params [i]);
128 foreach (TypeParameter type_param in CurrentTypeParameters) {
129 if (!type_param.Resolve (this))
133 Expression current = new SimpleName (
134 MemberName.Basename, TypeParameters, Location);
135 current = current.ResolveAsTypeTerminal (this, false);
139 CurrentType = current.Type;
146 public override bool Define ()
150 foreach (TypeParameter type_param in TypeParameters) {
151 if (!type_param.Resolve (this))
155 foreach (TypeParameter type_param in TypeParameters) {
156 if (!type_param.DefineType (this))
160 foreach (TypeParameter type_param in TypeParameters) {
161 if (!type_param.CheckDependencies ())
166 member_cache = new MemberCache (TypeManager.multicast_delegate_type, this);
168 // FIXME: POSSIBLY make this static, as it is always constant
170 Type [] const_arg_types = new Type [2];
171 const_arg_types [0] = TypeManager.object_type;
172 const_arg_types [1] = TypeManager.intptr_type;
174 const MethodAttributes ctor_mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
175 MethodAttributes.HideBySig | MethodAttributes.Public;
177 ConstructorBuilder = TypeBuilder.DefineConstructor (ctor_mattr,
178 CallingConventions.Standard,
181 ConstructorBuilder.DefineParameter (1, ParameterAttributes.None, "object");
182 ConstructorBuilder.DefineParameter (2, ParameterAttributes.None, "method");
184 // HACK because System.Reflection.Emit is lame
186 Parameter [] fixed_pars = new Parameter [2];
187 fixed_pars [0] = new Parameter (TypeManager.object_type, "object",
188 Parameter.Modifier.NONE, null, Location);
189 fixed_pars [1] = new Parameter (TypeManager.intptr_type, "method",
190 Parameter.Modifier.NONE, null, Location);
191 Parameters const_parameters = new Parameters (fixed_pars);
192 const_parameters.Resolve (null);
194 TypeManager.RegisterMethod (ConstructorBuilder, const_parameters);
195 member_cache.AddMember (ConstructorBuilder, this);
197 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
200 // Here the various methods like Invoke, BeginInvoke etc are defined
202 // First, call the `out of band' special method for
203 // defining recursively any types we need:
205 if (!Parameters.Resolve (this))
212 // Check accessibility
213 foreach (Type partype in Parameters.Types){
214 if (!Parent.AsAccessible (partype, ModFlags)) {
215 Report.Error (59, Location,
216 "Inconsistent accessibility: parameter type `" +
217 TypeManager.CSharpName (partype) + "' is less " +
218 "accessible than delegate `" + Name + "'");
223 ReturnType = ReturnType.ResolveAsTypeTerminal (this, false);
224 if (ReturnType == null)
227 ret_type = ReturnType.Type;
229 if (!Parent.AsAccessible (ret_type, ModFlags)) {
230 Report.Error (58, Location,
231 "Inconsistent accessibility: return type `" +
232 TypeManager.CSharpName (ret_type) + "' is less " +
233 "accessible than delegate `" + Name + "'");
237 if (RootContext.StdLib && (ret_type == TypeManager.arg_iterator_type || ret_type == TypeManager.typed_reference_type)) {
238 Method.Error1599 (Location, ret_type);
243 // We don't have to check any others because they are all
244 // guaranteed to be accessible - they are standard types.
247 CallingConventions cc = Parameters.CallingConvention;
249 const MethodAttributes mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.NewSlot;
251 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
257 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
259 TypeManager.RegisterMethod (InvokeBuilder, Parameters);
260 member_cache.AddMember (InvokeBuilder, this);
266 Parameters async_parameters = Parameters.MergeGenerated (Parameters,
267 new Parameter (TypeManager.asynccallback_type, "callback", Parameter.Modifier.NONE, null, Location),
268 new Parameter (TypeManager.object_type, "object", Parameter.Modifier.NONE, null, Location));
270 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
271 mattr, cc, TypeManager.iasyncresult_type, async_parameters.Types);
273 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
274 TypeManager.RegisterMethod (BeginInvokeBuilder, async_parameters);
275 member_cache.AddMember (BeginInvokeBuilder, this);
278 // EndInvoke is a bit more interesting, all the parameters labeled as
279 // out or ref have to be duplicated here.
283 // Define parameters, and count out/ref parameters
285 Parameters end_parameters;
288 foreach (Parameter p in Parameters.FixedParameters) {
289 if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
293 if (out_params > 0) {
294 Type [] end_param_types = new Type [out_params];
295 Parameter [] end_params = new Parameter [out_params ];
298 for (int i = 0; i < Parameters.FixedParameters.Length; ++i) {
299 Parameter p = Parameters.FixedParameters [i];
300 if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
303 end_param_types [param] = p.ExternalType();
304 end_params [param] = p;
307 end_parameters = Parameters.CreateFullyResolved (end_params, end_param_types);
310 end_parameters = Parameters.EmptyReadOnlyParameters;
313 end_parameters = Parameters.MergeGenerated (end_parameters,
314 new Parameter (TypeManager.iasyncresult_type, "result", Parameter.Modifier.NONE, null, Location));
317 // Create method, define parameters, register parameters with type system
319 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_parameters.Types);
320 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
322 end_parameters.ApplyAttributes (EndInvokeBuilder);
323 TypeManager.RegisterMethod (EndInvokeBuilder, end_parameters);
324 member_cache.AddMember (EndInvokeBuilder, this);
329 public override void Emit ()
331 Parameters.ApplyAttributes (InvokeBuilder);
333 Parameters p = (Parameters)TypeManager.GetParameterData (BeginInvokeBuilder);
334 p.ApplyAttributes (BeginInvokeBuilder);
336 if (OptAttributes != null) {
337 OptAttributes.Emit ();
343 protected override TypeAttributes TypeAttr {
345 return Modifiers.TypeAttr (ModFlags, IsTopLevel) |
346 TypeAttributes.Class | TypeAttributes.Sealed |
351 public override string[] ValidAttributeTargets {
353 return attribute_targets;
358 protected override bool VerifyClsCompliance ()
360 if (!base.VerifyClsCompliance ()) {
364 Parameters.VerifyClsCompliance ();
366 if (!AttributeTester.IsClsCompliant (ReturnType.Type)) {
367 Report.Error (3002, Location, "Return type of `{0}' is not CLS-compliant", GetSignatureForError ());
373 public static ConstructorInfo GetConstructor (Type container_type, Type delegate_type)
375 Type dt = delegate_type;
377 Type[] g_args = null;
378 if (delegate_type.IsGenericType) {
379 g_args = delegate_type.GetGenericArguments ();
380 delegate_type = delegate_type.GetGenericTypeDefinition ();
384 Delegate d = TypeManager.LookupDelegate (delegate_type);
388 return TypeBuilder.GetConstructor (dt, d.ConstructorBuilder);
390 return d.ConstructorBuilder;
393 Expression ml = Expression.MemberLookup (container_type,
394 null, dt, ConstructorInfo.ConstructorName, MemberTypes.Constructor,
395 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, Location.Null);
397 MethodGroupExpr mg = ml as MethodGroupExpr;
399 Report.Error (-100, Location.Null, "Internal error: could not find delegate constructor!");
400 // FIXME: null will cause a crash later
404 return (ConstructorInfo) mg.Methods[0];
408 // Returns the MethodBase for "Invoke" from a delegate type, this is used
409 // to extract the signature of a delegate.
411 public static MethodInfo GetInvokeMethod (Type container_type, Type delegate_type)
413 Type dt = delegate_type;
415 Type[] g_args = null;
416 if (delegate_type.IsGenericType) {
417 g_args = delegate_type.GetGenericArguments ();
418 delegate_type = delegate_type.GetGenericTypeDefinition ();
421 Delegate d = TypeManager.LookupDelegate (delegate_type);
424 if (g_args != null) {
425 MethodInfo invoke = TypeBuilder.GetMethod (dt, d.InvokeBuilder);
427 Parameters p = (Parameters) d.Parameters.InflateTypes (g_args, g_args);
428 TypeManager.RegisterMethod (invoke, p);
433 return d.InvokeBuilder;
436 Expression ml = Expression.MemberLookup (container_type, null, dt,
437 "Invoke", Location.Null);
439 MethodGroupExpr mg = ml as MethodGroupExpr;
441 Report.Error (-100, Location.Null, "Internal error: could not find Invoke method!");
442 // FIXME: null will cause a crash later
446 return (MethodInfo) mg.Methods[0];
449 public static bool IsTypeCovariant (Expression a, Type b)
451 Type a_type = a.Type;
455 if (RootContext.Version == LanguageVersion.ISO_1)
458 if (a.Type.IsValueType)
461 return Convert.ImplicitReferenceConversionCore (
466 /// Verifies whether the method in question is compatible with the delegate
467 /// Returns the method itself if okay and null if not.
469 public static MethodBase VerifyMethod (Type container_type, Type delegate_type,
470 MethodGroupExpr old_mg, MethodBase mb,
473 MethodInfo invoke_mb = GetInvokeMethod (container_type, delegate_type);
474 ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
477 if (!old_mg.HasTypeArguments &&
478 !TypeManager.InferTypeArguments (invoke_pd, ref mb))
482 ParameterData pd = TypeManager.GetParameterData (mb);
484 if (invoke_pd.Count != pd.Count)
487 for (int i = pd.Count; i > 0; ) {
490 Type invoke_pd_type = invoke_pd.ParameterType (i);
491 Type pd_type = pd.ParameterType (i);
492 Parameter.Modifier invoke_pd_type_mod = invoke_pd.ParameterModifier (i);
493 Parameter.Modifier pd_type_mod = pd.ParameterModifier (i);
495 invoke_pd_type_mod &= ~Parameter.Modifier.PARAMS;
496 pd_type_mod &= ~Parameter.Modifier.PARAMS;
498 if (invoke_pd_type_mod != pd_type_mod)
501 if (invoke_pd_type == pd_type)
504 //if (!IsTypeCovariant (invoke_pd_type, pd_type))
507 if (RootContext.Version == LanguageVersion.ISO_1)
511 Type invoke_mb_retval = ((MethodInfo) invoke_mb).ReturnType;
512 Type mb_retval = ((MethodInfo) mb).ReturnType;
513 if (invoke_mb_retval == mb_retval)
516 //if (!IsTypeCovariant (mb_retval, invoke_mb_retval))
519 if (RootContext.Version == LanguageVersion.ISO_1)
526 // Verifies whether the invocation arguments are compatible with the
527 // delegate's target method
529 public static bool VerifyApplicability (EmitContext ec, Type delegate_type,
530 ArrayList args, Location loc)
537 arg_count = args.Count;
539 Expression ml = Expression.MemberLookup (
540 ec.ContainerType, delegate_type, "Invoke", loc);
542 MethodGroupExpr me = ml as MethodGroupExpr;
544 Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
548 MethodBase mb = GetInvokeMethod (ec.ContainerType, delegate_type);
549 ParameterData pd = TypeManager.GetParameterData (mb);
551 int pd_count = pd.Count;
553 bool params_method = pd.HasParams;
554 bool is_params_applicable = false;
555 me.DelegateType = delegate_type;
556 bool is_applicable = me.IsApplicable (ec, args, arg_count, ref mb) == 0;
558 if (!is_applicable && params_method &&
559 me.IsParamsMethodApplicable (ec, args, arg_count, ref mb))
560 is_applicable = is_params_applicable = true;
562 if (!is_applicable && !params_method && arg_count != pd_count) {
563 Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
564 TypeManager.CSharpName (delegate_type), arg_count.ToString ());
568 return me.VerifyArgumentsCompat (
569 ec, args, arg_count, mb,
570 is_params_applicable || (!is_applicable && params_method),
574 public static string FullDelegateDesc (MethodBase invoke_method)
576 return TypeManager.GetFullNameSignature (invoke_method).Replace (".Invoke", "");
579 public override MemberCache MemberCache {
585 public Expression InstanceExpression {
587 return instance_expr;
590 instance_expr = value;
594 public MethodBase TargetMethod {
596 return delegate_method;
599 delegate_method = value;
603 public Type TargetReturnType {
609 public override AttributeTargets AttributeTargets {
611 return AttributeTargets.Delegate;
616 // Represents header string for documentation comment.
618 public override string DocCommentHeader {
622 #region IMemberContainer Members
624 string IMemberContainer.Name
626 get { throw new NotImplementedException (); }
629 Type IMemberContainer.Type
631 get { throw new NotImplementedException (); }
634 MemberCache IMemberContainer.BaseCache
636 get { throw new NotImplementedException (); }
639 bool IMemberContainer.IsInterface {
645 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
647 throw new NotImplementedException ();
654 // Base class for `NewDelegate' and `ImplicitDelegateCreation'
656 public abstract class DelegateCreation : Expression, MethodGroupExpr.IErrorHandler
658 protected ConstructorInfo constructor_method;
659 protected MethodBase delegate_method;
660 // We keep this to handle IsBase only
661 protected MethodGroupExpr method_group;
662 protected Expression delegate_instance_expression;
664 ArrayList CreateDelegateMethodArguments (MethodInfo invoke_method)
666 ParameterData pd = TypeManager.GetParameterData (invoke_method);
667 ArrayList delegate_arguments = new ArrayList (pd.Count);
668 for (int i = 0; i < pd.Count; ++i) {
669 Argument.AType atype_modifier;
670 Type atype = pd.Types [i];
671 switch (pd.ParameterModifier (i)) {
672 case Parameter.Modifier.REF:
673 atype_modifier = Argument.AType.Ref;
674 atype = atype.GetElementType ();
676 case Parameter.Modifier.OUT:
677 atype_modifier = Argument.AType.Out;
678 atype = atype.GetElementType ();
680 case Parameter.Modifier.ARGLIST:
681 // TODO: investigate and test
682 throw new NotImplementedException ();
684 atype_modifier = Argument.AType.Expression;
687 delegate_arguments.Add (new Argument (new TypeExpression (atype, loc), atype_modifier));
689 return delegate_arguments;
692 public override Expression DoResolve (EmitContext ec)
694 constructor_method = Delegate.GetConstructor (ec.ContainerType, type);
696 MethodInfo invoke_method = Delegate.GetInvokeMethod (ec.ContainerType, type);
697 method_group.DelegateType = type;
698 method_group.CustomErrorHandler = this;
699 method_group = method_group.OverloadResolve (ec, CreateDelegateMethodArguments (invoke_method), false, loc);
700 if (method_group == null)
703 delegate_method = (MethodInfo) method_group;
704 if (method_group is ExtensionMethodGroupExpr) {
705 ParameterData p = TypeManager.GetParameterData (delegate_method);
706 if (p.ExtensionMethodType.IsValueType) {
707 Report.Error (1113, loc, "Extension method `{0}' of value type `{1}' cannot be used to create delegates",
708 TypeManager.CSharpSignature (delegate_method), TypeManager.CSharpName (p.ExtensionMethodType));
712 Expression ret_expr = new TypeExpression (((MethodInfo) delegate_method).ReturnType, loc);
713 if (!Delegate.IsTypeCovariant (ret_expr, (invoke_method.ReturnType))) {
714 Error_ConversionFailed (ec, delegate_method, ret_expr);
717 if (method_group.InstanceExpression != null)
718 delegate_instance_expression = method_group.InstanceExpression;
719 else if (!ec.IsStatic)
720 delegate_instance_expression = ec.GetThis (loc);
722 if (delegate_instance_expression != null && delegate_instance_expression.Type.IsValueType)
723 delegate_instance_expression = new BoxedCast (
724 delegate_instance_expression, TypeManager.object_type);
726 eclass = ExprClass.Value;
730 public override void Emit (EmitContext ec)
732 if (delegate_instance_expression == null || delegate_method.IsStatic)
733 ec.ig.Emit (OpCodes.Ldnull);
735 delegate_instance_expression.Emit (ec);
737 if (delegate_method.IsVirtual && !method_group.IsBase) {
738 ec.ig.Emit (OpCodes.Dup);
739 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
741 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
742 ec.ig.Emit (OpCodes.Newobj, constructor_method);
745 void Error_ConversionFailed (EmitContext ec, MethodBase method, Expression return_type)
747 MethodInfo invoke_method = Delegate.GetInvokeMethod (ec.ContainerType, type);
748 string member_name = delegate_instance_expression != null ?
749 Delegate.FullDelegateDesc (method) :
750 TypeManager.GetFullNameSignature (method);
752 Report.SymbolRelatedToPreviousError (type);
753 Report.SymbolRelatedToPreviousError (method);
754 if (RootContext.Version == LanguageVersion.ISO_1) {
755 Report.Error (410, loc, "A method or delegate `{0} {1}' parameters and return type must be same as delegate `{2} {3}' parameters and return type",
756 TypeManager.CSharpName (((MethodInfo) method).ReturnType), member_name,
757 TypeManager.CSharpName (invoke_method.ReturnType), Delegate.FullDelegateDesc (invoke_method));
760 if (return_type == null) {
761 Report.Error (123, loc, "A method or delegate `{0}' parameters do not match delegate `{1}' parameters",
762 member_name, Delegate.FullDelegateDesc (invoke_method));
766 Report.Error (407, loc, "A method or delegate `{0} {1}' return type does not match delegate `{2} {3}' return type",
767 return_type.GetSignatureForError (), member_name,
768 TypeManager.CSharpName (invoke_method.ReturnType), Delegate.FullDelegateDesc (invoke_method));
771 public static MethodBase ImplicitStandardConversionExists (MethodGroupExpr mg, Type target_type)
773 foreach (MethodInfo mi in mg.Methods){
774 MethodBase mb = Delegate.VerifyMethod (mg.DeclaringType, target_type, mg, mi, Location.Null);
781 #region IErrorHandler Members
783 public bool NoExactMatch (EmitContext ec, MethodBase method)
785 if (TypeManager.IsGenericMethod (method))
788 Error_ConversionFailed (ec, method, null);
796 // Created from the conversion code
798 public class ImplicitDelegateCreation : DelegateCreation
800 ImplicitDelegateCreation (Type t, MethodGroupExpr mg, Location l)
803 this.method_group = mg;
807 static public Expression Create (EmitContext ec, MethodGroupExpr mge,
808 Type target_type, Location loc)
810 ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, mge, loc);
811 return d.DoResolve (ec);
816 // A delegate-creation-expression, invoked from the `New' class
818 public class NewDelegate : DelegateCreation
820 public ArrayList Arguments;
823 // This constructor is invoked from the `New' expression
825 public NewDelegate (Type type, ArrayList Arguments, Location loc)
828 this.Arguments = Arguments;
832 public override Expression DoResolve (EmitContext ec)
834 if (Arguments == null) {
835 Error_InvalidDelegateArgument ();
839 Argument a = (Argument) Arguments [0];
840 if (!a.ResolveMethodGroup (ec))
843 Expression e = a.Expr;
844 if (e is AnonymousMethodExpression && RootContext.Version != LanguageVersion.ISO_1) {
845 AnonymousMethod am = ((AnonymousMethodExpression) e).Compatible (ec, type);
848 return am.Resolve (ec);
851 method_group = e as MethodGroupExpr;
852 if (method_group == null) {
853 if (!TypeManager.IsDelegateType (e.Type)) {
854 Report.Error (149, loc, "Method name expected");
859 // An argument is not a method but another delegate
861 delegate_instance_expression = e;
862 method_group = new MethodGroupExpr (new MemberInfo [] {
863 Delegate.GetInvokeMethod (ec.ContainerType, e.Type) }, e.Type, loc);
866 if (base.DoResolve (ec) == null)
869 if (TypeManager.IsNullableType (method_group.DeclaringType)) {
870 Report.Error (1728, loc, "Cannot use method `{0}' as delegate creation expression because it is member of Nullable type",
871 TypeManager.GetFullNameSignature (delegate_method));
874 if (Invocation.IsMethodExcluded (delegate_method)) {
875 Report.SymbolRelatedToPreviousError (delegate_method);
876 Report.Error (1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute",
877 TypeManager.CSharpSignature (delegate_method));
883 void Error_InvalidDelegateArgument ()
885 Report.Error (149, loc, "Method name expected");
889 public class DelegateInvocation : ExpressionStatement {
891 readonly Expression InstanceExpr;
892 readonly ArrayList Arguments;
896 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
898 this.InstanceExpr = instance_expr;
899 this.Arguments = args;
903 public override Expression DoResolve (EmitContext ec)
905 if (InstanceExpr is EventExpr) {
907 EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
909 Expression ml = MemberLookup (
910 ec.ContainerType, ec.ContainerType, ei.Name,
911 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
915 // If this is the case, then the Event does not belong
916 // to this Type and so, according to the spec
917 // cannot be accessed directly
919 // Note that target will not appear as an EventExpr
920 // in the case it is being referenced within the same type container;
921 // it will appear as a FieldExpr in that case.
924 Assign.error70 (ei, loc);
930 Type del_type = InstanceExpr.Type;
931 if (del_type == null)
934 if (Arguments != null){
935 foreach (Argument a in Arguments){
936 if (!a.Resolve (ec, loc))
941 if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
944 method = Delegate.GetInvokeMethod (ec.ContainerType, del_type);
945 type = ((MethodInfo) method).ReturnType;
946 eclass = ExprClass.Value;
951 public override void Emit (EmitContext ec)
954 // Invocation on delegates call the virtual Invoke member
955 // so we are always `instance' calls
957 Invocation.EmitCall (ec, false, InstanceExpr, method, Arguments, loc);
960 public override void EmitStatement (EmitContext ec)
964 // Pop the return value if there is one
966 if (method is MethodInfo){
967 Type ret = ((MethodInfo)method).ReturnType;
968 if (TypeManager.TypeToCoreType (ret) != TypeManager.void_type)
969 ec.ig.Emit (OpCodes.Pop);