2 // delegate.cs: Delegate Handler
4 // Author: Ravi Pratap (ravi@ximian.com)
6 // Licensed under the terms of the GNU GPL
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
13 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
18 namespace Mono.MonoBASIC {
23 public class Delegate : DeclSpace {
24 public Expression ReturnType;
25 public Parameters Parameters;
26 public Attributes OptAttributes;
28 public ConstructorBuilder ConstructorBuilder;
29 public MethodBuilder InvokeBuilder;
30 public MethodBuilder BeginInvokeBuilder;
31 public MethodBuilder EndInvokeBuilder;
36 Expression instance_expr;
37 MethodBase delegate_method;
39 const int AllowedModifiers =
47 public Delegate (TypeContainer parent, Expression type, int mod_flags,
48 string name, Parameters param_list,
49 Attributes attrs, Location l)
50 : base (parent, name, l)
52 this.ReturnType = type;
54 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags,
55 IsTopLevel ? Modifiers.INTERNAL :
57 Parameters = param_list;
58 OptAttributes = attrs;
61 public override TypeBuilder DefineType ()
65 if (TypeBuilder != null)
69 ModuleBuilder builder = CodeGen.ModuleBuilder;
70 attr = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed;
72 TypeBuilder = builder.DefineType (
73 Name, attr, TypeManager.multicast_delegate_type);
75 TypeBuilder builder = Parent.TypeBuilder;
76 attr = TypeAttributes.NestedPublic | TypeAttributes.Class |
77 TypeAttributes.Sealed;
79 string name = Name.Substring (1 + Name.LastIndexOf ('.'));
80 TypeBuilder = builder.DefineNestedType (
81 name, attr, TypeManager.multicast_delegate_type);
84 TypeManager.AddDelegateType (Name, TypeBuilder, this);
89 public override bool DefineMembers (TypeContainer container)
94 public override bool Define (TypeContainer container)
96 MethodAttributes mattr;
99 // FIXME: POSSIBLY make this static, as it is always constant
101 Type [] const_arg_types = new Type [2];
102 const_arg_types [0] = TypeManager.object_type;
103 const_arg_types [1] = TypeManager.intptr_type;
105 mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
106 MethodAttributes.HideBySig | MethodAttributes.Public;
108 ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
109 CallingConventions.Standard,
112 ConstructorBuilder.DefineParameter (1, ParameterAttributes.None, "object");
113 ConstructorBuilder.DefineParameter (2, ParameterAttributes.None, "method");
115 // HACK because System.Reflection.Emit is lame
118 // FIXME: POSSIBLY make these static, as they are always the same
119 Parameter [] fixed_pars = new Parameter [2];
120 fixed_pars [0] = new Parameter (null, null, Parameter.Modifier.NONE, null);
121 fixed_pars [1] = new Parameter (null, null, Parameter.Modifier.NONE, null);
122 Parameters const_parameters = new Parameters (fixed_pars, null, Location);
124 TypeManager.RegisterMethod (
126 new InternalParameters (const_arg_types, const_parameters),
130 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
133 // Here the various methods like Invoke, BeginInvoke etc are defined
135 // First, call the `out of band' special method for
136 // defining recursively any types we need:
138 if (!Parameters.ComputeAndDefineParameterTypes (this))
141 param_types = Parameters.GetParameterInfo (this);
142 if (param_types == null)
149 // Check accessibility
150 foreach (Type partype in param_types)
151 if (!container.AsAccessible (partype, ModFlags)) {
152 Report.Error (59, Location,
153 "Inconsistent accessibility: parameter type `" +
154 TypeManager.MonoBASIC_Name (partype) + "` is less " +
155 "accessible than delegate `" + Name + "'");
159 ReturnType = ResolveTypeExpr (ReturnType, false, Location);
160 ret_type = ReturnType.Type;
161 if (ret_type == null)
164 if (!container.AsAccessible (ret_type, ModFlags)) {
165 Report.Error (58, Location,
166 "Inconsistent accessibility: return type `" +
167 TypeManager.MonoBASIC_Name (ret_type) + "` is less " +
168 "accessible than delegate `" + Name + "'");
173 // We don't have to check any others because they are all
174 // guaranteed to be accessible - they are standard types.
177 CallingConventions cc = Parameters.GetCallingConvention ();
179 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
181 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
188 if (Parameters.FixedParameters != null){
189 int top = Parameters.FixedParameters.Length;
192 for (; i < top; i++) {
193 p = Parameters.FixedParameters [i];
195 InvokeBuilder.DefineParameter (
196 i+1, p.Attributes, p.Name);
199 if (Parameters.ArrayParameter != null){
200 Parameter p = Parameters.ArrayParameter;
202 InvokeBuilder.DefineParameter (
203 i+1, p.Attributes, p.Name);
206 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
208 TypeManager.RegisterMethod (InvokeBuilder,
209 new InternalParameters (container, Parameters),
215 int params_num = param_types.Length;
216 Type [] async_param_types = new Type [params_num + 2];
218 param_types.CopyTo (async_param_types, 0);
220 async_param_types [params_num] = TypeManager.asynccallback_type;
221 async_param_types [params_num + 1] = TypeManager.object_type;
223 mattr = MethodAttributes.Public | MethodAttributes.HideBySig |
224 MethodAttributes.Virtual | MethodAttributes.NewSlot;
226 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
229 TypeManager.iasyncresult_type,
233 if (Parameters.FixedParameters != null){
234 int top = Parameters.FixedParameters.Length;
237 for (i = 0 ; i < top; i++) {
238 p = Parameters.FixedParameters [i];
240 BeginInvokeBuilder.DefineParameter (
241 i+1, p.Attributes, p.Name);
244 if (Parameters.ArrayParameter != null){
245 Parameter p = Parameters.ArrayParameter;
247 BeginInvokeBuilder.DefineParameter (
248 i+1, p.Attributes, p.Name);
252 BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
253 BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
255 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
257 Parameter [] async_params = new Parameter [params_num + 2];
259 if (Parameters.FixedParameters != null){
260 Parameters.FixedParameters.CopyTo (async_params, 0);
261 n = Parameters.FixedParameters.Length;
263 if (Parameters.ArrayParameter != null)
264 async_params [n] = Parameters.ArrayParameter;
266 async_params [params_num] = new Parameter (
267 TypeManager.system_asynccallback_expr, "callback",
268 Parameter.Modifier.NONE, null);
269 async_params [params_num + 1] = new Parameter (
270 TypeManager.system_object_expr, "object",
271 Parameter.Modifier.NONE, null);
273 Parameters async_parameters = new Parameters (async_params, null, Location);
275 async_parameters.ComputeAndDefineParameterTypes (this);
276 TypeManager.RegisterMethod (BeginInvokeBuilder,
277 new InternalParameters (container, async_parameters),
283 Type [] end_param_types = new Type [1];
284 end_param_types [0] = TypeManager.iasyncresult_type;
286 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke",
291 EndInvokeBuilder.DefineParameter (1, ParameterAttributes.None, "result");
293 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
295 Parameter [] end_params = new Parameter [1];
296 end_params [0] = new Parameter (
297 TypeManager.system_iasyncresult_expr, "result",
298 Parameter.Modifier.NONE, null);
300 TypeManager.RegisterMethod (
301 EndInvokeBuilder, new InternalParameters (
304 end_params, null, Location)),
311 /// Verifies whether the method in question is compatible with the delegate
312 /// Returns the method itself if okay and null if not.
314 public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb,
317 ParameterData pd = Invocation.GetParameterData (mb);
319 int pd_count = pd.Count;
321 Expression ml = Expression.MemberLookup (
322 ec, delegate_type, "Invoke", loc);
324 if (!(ml is MethodGroupExpr)) {
325 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
329 MethodBase invoke_mb = ((MethodGroupExpr) ml).Methods [0];
331 ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
333 if (invoke_pd.Count != pd_count)
336 for (int i = pd_count; i > 0; ) {
339 if (invoke_pd.ParameterType (i) == pd.ParameterType (i))
345 if (((MethodInfo) invoke_mb).ReturnType == ((MethodInfo) mb).ReturnType)
352 // Verifies whether the invocation arguments are compatible with the
353 // delegate's target method
355 public static bool VerifyApplicability (EmitContext ec,
365 arg_count = args.Count;
367 Expression ml = Expression.MemberLookup (
368 ec, delegate_type, "Invoke", loc);
370 if (!(ml is MethodGroupExpr)) {
371 Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
375 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
376 ParameterData pd = Invocation.GetParameterData (mb);
378 int pd_count = pd.Count;
380 bool not_params_method = (pd_count == 0) ||
381 (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS);
383 if (not_params_method && pd_count != arg_count) {
384 Report.Error (1593, loc,
385 "Delegate '" + delegate_type.ToString ()
386 + "' does not take '" + arg_count + "' arguments");
390 return Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, !not_params_method,
395 /// Verifies whether the delegate in question is compatible with this one in
396 /// order to determine if instantiation from the same is possible.
398 public static bool VerifyDelegate (EmitContext ec, Type delegate_type, Type probe_type, Location loc)
400 Expression ml = Expression.MemberLookup (
401 ec, delegate_type, "Invoke", loc);
403 if (!(ml is MethodGroupExpr)) {
404 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
408 MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
409 ParameterData pd = Invocation.GetParameterData (mb);
411 Expression probe_ml = Expression.MemberLookup (
412 ec, delegate_type, "Invoke", loc);
414 if (!(probe_ml is MethodGroupExpr)) {
415 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
419 MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];
420 ParameterData probe_pd = Invocation.GetParameterData (probe_mb);
422 if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)
425 if (pd.Count != probe_pd.Count)
428 for (int i = pd.Count; i > 0; ) {
431 if (pd.ParameterType (i) != probe_pd.ParameterType (i) ||
432 pd.ParameterModifier (i) != probe_pd.ParameterModifier (i))
439 public static string FullDelegateDesc (Type del_type, MethodBase mb, ParameterData pd)
441 StringBuilder sb = new StringBuilder (TypeManager.MonoBASIC_Name (((MethodInfo) mb).ReturnType));
443 sb.Append (" " + del_type.ToString ());
446 int length = pd.Count;
448 for (int i = length; i > 0; ) {
451 sb.Append (TypeManager.MonoBASIC_Name (pd.ParameterType (length - i - 1)));
457 return sb.ToString ();
461 // Hack around System.Reflection as found everywhere else
462 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
463 MemberFilter filter, object criteria)
465 ArrayList members = new ArrayList ();
467 if ((mt & MemberTypes.Method) != 0) {
468 if (ConstructorBuilder != null)
469 if (filter (ConstructorBuilder, criteria))
470 members.Add (ConstructorBuilder);
472 if (InvokeBuilder != null)
473 if (filter (InvokeBuilder, criteria))
474 members.Add (InvokeBuilder);
476 if (BeginInvokeBuilder != null)
477 if (filter (BeginInvokeBuilder, criteria))
478 members.Add (BeginInvokeBuilder);
480 if (EndInvokeBuilder != null)
481 if (filter (EndInvokeBuilder, criteria))
482 members.Add (EndInvokeBuilder);
485 return new MemberList (members);
488 public override MemberCache MemberCache {
494 public Expression InstanceExpression {
496 return instance_expr;
499 instance_expr = value;
503 public MethodBase TargetMethod {
505 return delegate_method;
508 delegate_method = value;
512 public Type TargetReturnType {
518 public Type [] ParameterTypes {
526 public class NewDelegate : Expression {
528 public ArrayList Arguments;
530 MethodBase constructor_method;
531 MethodBase delegate_method;
532 Expression delegate_instance_expr;
534 public NewDelegate (Type type, ArrayList Arguments, Location loc)
537 this.Arguments = Arguments;
541 public override Expression DoResolve (EmitContext ec)
543 if (Arguments == null) {
544 Report.Error (-11, loc,
545 "Delegate creation expression takes only one argument");
549 if (Arguments.Count != 1) {
550 Report.Error (-11, loc,
551 "Delegate creation expression takes only one argument");
555 Expression ml = Expression.MemberLookup (
556 ec, type, ".ctor", loc);
558 if (!(ml is MethodGroupExpr)) {
559 Report.Error (-100, loc, "Internal error: Could not find delegate constructor!");
563 constructor_method = ((MethodGroupExpr) ml).Methods [0];
564 Argument a = (Argument) Arguments [0];
566 if (!a.ResolveMethodGroup (ec, Location))
569 Expression e = a.Expr;
571 Expression invoke_method = Expression.MemberLookup (
572 ec, type, "Invoke", MemberTypes.Method,
573 Expression.AllBindingFlags, loc);
575 if (invoke_method == null) {
576 Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
580 if (e is MethodGroupExpr) {
581 MethodGroupExpr mg = (MethodGroupExpr) e;
583 foreach (MethodInfo mi in mg.Methods){
584 delegate_method = Delegate.VerifyMethod (ec, type, mi, loc);
586 if (delegate_method != null)
590 if (delegate_method == null) {
592 if (mg.Methods.Length > 1)
593 method_desc = mg.Methods [0].Name;
595 method_desc = Invocation.FullMethodDesc (mg.Methods [0]);
597 MethodBase dm = ((MethodGroupExpr) invoke_method).Methods [0];
598 ParameterData param = Invocation.GetParameterData (dm);
599 string delegate_desc = Delegate.FullDelegateDesc (type, dm, param);
601 Report.Error (30408, loc, "Method '" + method_desc + "' does not " +
602 "match delegate '" + delegate_desc + "'");
608 // Check safe/unsafe of the delegate
611 ParameterData param = Invocation.GetParameterData (delegate_method);
612 int count = param.Count;
614 for (int i = 0; i < count; i++){
615 if (param.ParameterType (i).IsPointer){
616 Expression.UnsafeError (loc);
622 if (mg.InstanceExpression != null)
623 delegate_instance_expr = mg.InstanceExpression.Resolve (ec);
626 delegate_instance_expr = ec.This;
628 delegate_instance_expr = null;
631 if (delegate_instance_expr != null)
632 if (delegate_instance_expr.Type.IsValueType)
633 delegate_instance_expr = new BoxedCast (delegate_instance_expr);
635 eclass = ExprClass.Value;
639 Type e_type = e.Type;
641 if (!TypeManager.IsDelegateType (e_type)) {
642 Report.Error (-12, loc, "Cannot create a delegate from something " +
643 "not a delegate or a method.");
647 // This is what MS' compiler reports. We could always choose
648 // to be more verbose and actually give delegate-level specifics
650 if (!Delegate.VerifyDelegate (ec, type, e_type, loc)) {
651 Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " +
652 "to type '" + type + "'");
656 delegate_instance_expr = e;
657 delegate_method = ((MethodGroupExpr) invoke_method).Methods [0];
659 eclass = ExprClass.Value;
663 public override void Emit (EmitContext ec)
665 if (delegate_instance_expr == null ||
666 delegate_method.IsStatic)
667 ec.ig.Emit (OpCodes.Ldnull);
669 delegate_instance_expr.Emit (ec);
671 if (delegate_method.IsVirtual) {
672 ec.ig.Emit (OpCodes.Dup);
673 ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
675 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
676 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
680 public class DelegateInvocation : ExpressionStatement {
682 public Expression InstanceExpr;
683 public ArrayList Arguments;
687 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
689 this.InstanceExpr = instance_expr;
690 this.Arguments = args;
694 public override Expression DoResolve (EmitContext ec)
696 if (InstanceExpr is EventExpr) {
698 EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
700 Expression ml = MemberLookup (
701 ec, ec.ContainerType, ei.Name,
702 MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
706 // If this is the case, then the Event does not belong
707 // to this Type and so, according to the spec
708 // cannot be accessed directly
710 // Note that target will not appear as an EventExpr
711 // in the case it is being referenced within the same type container;
712 // it will appear as a FieldExpr in that case.
715 Assign.error70 (ei, loc);
721 Type del_type = InstanceExpr.Type;
722 if (del_type == null)
725 if (Arguments != null){
726 foreach (Argument a in Arguments){
727 if (!a.Resolve (ec, loc))
732 if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
735 Expression lookup = Expression.MemberLookup (ec, del_type, "Invoke", loc);
736 if (!(lookup is MethodGroupExpr)) {
737 Report.Error (-100, loc, "Internal error: could not find Invoke method!");
741 method = ((MethodGroupExpr) lookup).Methods [0];
742 type = ((MethodInfo) method).ReturnType;
743 eclass = ExprClass.Value;
748 public override void Emit (EmitContext ec)
750 Delegate del = TypeManager.LookupDelegate (InstanceExpr.Type);
753 // Invocation on delegates call the virtual Invoke member
754 // so we are always `instance' calls
756 Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, loc);
759 public override void EmitStatement (EmitContext ec)
763 // Pop the return value if there is one
765 if (method is MethodInfo){
766 if (((MethodInfo) method).ReturnType != TypeManager.void_type)
767 ec.ig.Emit (OpCodes.Pop);