2 // delegate.cs: Delegate Handler
\r
4 // Author: Ravi Pratap (ravi@ximian.com)
\r
6 // Licensed under the terms of the GNU GPL
\r
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
\r
13 using System.Collections;
\r
14 using System.Reflection;
\r
15 using System.Reflection.Emit;
\r
20 public class Delegate {
\r
22 public readonly string Name;
\r
23 public readonly string ReturnType;
\r
24 public int mod_flags;
\r
25 public Parameters Parameters;
\r
26 public Attributes OptAttributes;
\r
27 public TypeBuilder TypeBuilder;
\r
29 public ConstructorBuilder ConstructorBuilder;
\r
30 public MethodBuilder InvokeBuilder;
\r
31 public MethodBuilder BeginInvokeBuilder;
\r
32 public MethodBuilder EndInvokeBuilder;
\r
34 public readonly RootContext RootContext;
\r
36 Type [] param_types;
\r
39 Expression instance_expr;
\r
40 MethodBase delegate_method;
\r
44 const int AllowedModifiers =
\r
47 Modifiers.PROTECTED |
\r
48 Modifiers.INTERNAL |
\r
51 public Delegate (RootContext rc, string type, int mod_flags, string name, Parameters param_list,
\r
52 Attributes attrs, Location loc)
\r
54 this.RootContext = rc;
\r
56 this.ReturnType = type;
\r
57 this.mod_flags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PUBLIC);
\r
58 Parameters = param_list;
\r
59 OptAttributes = attrs;
\r
63 public void DefineDelegate (object parent_builder)
\r
65 TypeAttributes attr;
\r
67 if (parent_builder is ModuleBuilder) {
\r
68 ModuleBuilder builder = (ModuleBuilder) parent_builder;
\r
70 attr = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed;
\r
72 TypeBuilder = builder.DefineType (Name, attr, TypeManager.delegate_type);
\r
75 TypeBuilder builder = (TypeBuilder) parent_builder;
\r
77 attr = TypeAttributes.NestedPublic | TypeAttributes.Class | TypeAttributes.Sealed;
\r
79 TypeBuilder = builder.DefineNestedType (Name, attr, TypeManager.delegate_type);
\r
83 RootContext.TypeManager.AddDelegateType (Name, TypeBuilder, this);
\r
86 public void Populate (TypeContainer parent)
\r
89 MethodAttributes mattr;
\r
92 Type [] const_arg_types = new Type [2];
\r
94 const_arg_types [0] = TypeManager.object_type;
\r
95 const_arg_types [1] = TypeManager.intptr_type;
\r
97 mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
\r
98 MethodAttributes.HideBySig | MethodAttributes.Public;
\r
100 ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
\r
101 CallingConventions.Standard,
\r
104 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
106 // Here the various methods like Invoke, BeginInvoke etc are defined
\r
108 param_types = Parameters.GetParameterInfo (parent);
\r
109 ret_type = parent.LookupType (ReturnType, false);
\r
110 CallingConventions cc = Parameters.GetCallingConvention ();
\r
112 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
\r
114 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
\r
120 for (i = 0 ; i < param_types.Length; i++) {
\r
121 Parameter p = Parameters.FixedParameters [i];
\r
122 string name = p.Name;
\r
123 ParameterBuilder pb = InvokeBuilder.DefineParameter (i+1, p.Attributes, name);
\r
126 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
128 TypeContainer.RegisterParameterForBuilder (InvokeBuilder,
\r
129 new InternalParameters (parent, Parameters));
\r
131 int params_num = param_types.Length;
\r
132 Type [] async_param_types = new Type [params_num + 2];
\r
134 param_types.CopyTo (async_param_types, 0);
\r
136 async_param_types [params_num] = TypeManager.asynccallback_type;
\r
137 async_param_types [params_num + 1] = TypeManager.object_type;
\r
139 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual |
\r
140 MethodAttributes.NewSlot;
\r
142 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
\r
145 TypeManager.iasyncresult_type,
\r
146 async_param_types);
\r
148 for (i = 0 ; i < param_types.Length; i++) {
\r
149 Parameter p = Parameters.FixedParameters [i];
\r
150 string name = p.Name;
\r
151 BeginInvokeBuilder.DefineParameter (i + 1, p.Attributes, name);
\r
154 BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
\r
155 BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
\r
157 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
159 Parameter [] async_params = new Parameter [params_num + 2];
\r
160 Parameters.FixedParameters.CopyTo (async_params, 0);
\r
162 async_params [params_num] = new Parameter ("System.AsyncCallback", "callback",
\r
163 Parameter.Modifier.NONE, null);
\r
164 async_params [params_num + 1] = new Parameter ("System.IAsyncResult", "object",
\r
165 Parameter.Modifier.NONE, null);
\r
167 TypeContainer.RegisterParameterForBuilder (BeginInvokeBuilder,
\r
168 new InternalParameters (parent, new Parameters (async_params, null)));
\r
170 Type [] end_param_types = new Type [1];
\r
171 end_param_types [0] = TypeManager.iasyncresult_type;
\r
173 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke",
\r
178 EndInvokeBuilder.DefineParameter (1, ParameterAttributes.None, "result");
\r
180 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
182 Parameter [] end_params = new Parameter [1];
\r
183 end_params [0] = new Parameter ("System.IAsyncResult", "result",
\r
184 Parameter.Modifier.NONE, null);
\r
186 TypeContainer.RegisterParameterForBuilder (EndInvokeBuilder,
\r
187 new InternalParameters (parent, new Parameters (end_params, null)));
\r
192 // Verifies whether the method in question is compatible with the delegate
\r
193 // Returns the method itself if okay and null if not.
\r
195 public MethodBase VerifyMethod (MethodBase mb, Location loc)
\r
197 ParameterData pd = Invocation.GetParameterData (mb);
\r
199 bool mismatch = false;
\r
200 for (int i = param_types.Length; i > 0; ) {
\r
203 if (param_types [i] == pd.ParameterType (i))
\r
212 Report.Error (123, loc, "Method '" + Invocation.FullMethodDesc (mb) + "' does not match " +
\r
213 "delegate '" + FullDelegateDesc () + "'");
\r
217 if (ret_type == ((MethodInfo) mb).ReturnType)
\r
223 Report.Error (123, loc, "Method '" + Invocation.FullMethodDesc (mb) + "' does not match " +
\r
224 "delegate '" + FullDelegateDesc () + "'");
\r
232 // Verifies whether the invocation arguments are compatible with the
\r
233 // delegate's target method
\r
235 public bool VerifyApplicability (EmitContext ec, ArrayList args, Location loc)
\r
242 arg_count = args.Count;
\r
244 if (param_types.Length != arg_count) {
\r
245 Report.Error (1593, loc,
\r
246 "Delegate '" + Name + "' does not take '" + arg_count + "' arguments");
\r
250 for (int i = arg_count; i > 0;) {
\r
253 Argument a = (Argument) args [i];
\r
254 Expression a_expr = a.Expr;
\r
256 if (param_types [i] != a_expr.Type) {
\r
258 conv = Expression.ConvertImplicitStandard (ec, a_expr, param_types [i], loc);
\r
260 if (conv == null) {
\r
261 Report.Error (1594, loc,
\r
262 "Delegate '" + Name + "' has some invalid arguments.");
\r
264 Report.Error (1503, loc,
\r
265 "Argument " + (i+1) +
\r
266 ": Cannot convert from '" +
\r
267 TypeManager.CSharpName (a_expr.Type)
\r
268 + "' to '" + TypeManager.CSharpName (param_types [i]) + "'");
\r
272 if (a_expr != conv)
\r
281 // Verifies whether the delegate in question is compatible with this one in
\r
282 // order to determine if instantiation from the same is possible.
\r
284 public bool VerifyDelegate (Delegate del)
\r
286 if (ret_type != del.TargetReturnType)
\r
289 Type [] other_param_types = del.ParameterTypes;
\r
291 if (param_types.Length != other_param_types.Length)
\r
294 for (int i = param_types.Length; i > 0; ) {
\r
297 if (param_types [i] != other_param_types [i])
\r
301 // FIXME : Hey, what about parameter modifiers ?
\r
307 public string FullDelegateDesc ()
\r
309 StringBuilder sb = new StringBuilder (TypeManager.CSharpName (System.Type.GetType (ReturnType)));
\r
311 sb.Append (" " + Name);
\r
314 int length = param_types.Length;
\r
316 for (int i = length; i > 0; ) {
\r
319 sb.Append (TypeManager.CSharpName (param_types [length - i - 1]));
\r
325 return sb.ToString ();
\r
329 // Hack around System.Reflection as found everywhere else
\r
330 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria)
\r
332 ArrayList members = new ArrayList ();
\r
334 if ((mt & MemberTypes.Method) != 0) {
\r
335 if (filter (ConstructorBuilder, criteria))
\r
336 members.Add (ConstructorBuilder);
\r
338 if (filter (InvokeBuilder, criteria))
\r
339 members.Add (InvokeBuilder);
\r
341 if (filter (BeginInvokeBuilder, criteria))
\r
342 members.Add (BeginInvokeBuilder);
\r
344 if (filter (EndInvokeBuilder, criteria))
\r
345 members.Add (EndInvokeBuilder);
\r
348 int count = members.Count;
\r
351 MemberInfo [] mi = new MemberInfo [count];
\r
352 members.CopyTo (mi, 0);
\r
359 public void CloseDelegate ()
\r
361 TypeBuilder.CreateType ();
\r
364 public int ModFlags {
\r
370 public Expression InstanceExpression {
\r
372 return instance_expr;
\r
375 instance_expr = value;
\r
379 public MethodBase TargetMethod {
\r
381 return delegate_method;
\r
384 delegate_method = value;
\r
388 public Type TargetReturnType {
\r
394 public Type [] ParameterTypes {
\r
396 return param_types;
\r
402 public class NewDelegate : Expression {
\r
404 public ArrayList Arguments;
\r
406 MethodBase constructor_method;
\r
407 MethodBase delegate_method;
\r
408 Expression delegate_instance_expr;
\r
412 public NewDelegate (Type type, ArrayList Arguments, Location loc)
\r
415 this.Arguments = Arguments;
\r
416 this.Location = loc;
\r
419 public override Expression DoResolve (EmitContext ec)
\r
421 Delegate del = TypeManager.LookupDelegate (type);
\r
422 constructor_method = del.ConstructorBuilder;
\r
424 if (Arguments == null) {
\r
425 Report.Error (-11, Location,
\r
426 "Delegate creation expression takes only one argument");
\r
430 if (Arguments.Count != 1) {
\r
431 Report.Error (-11, Location,
\r
432 "Delegate creation expression takes only one argument");
\r
436 Argument a = (Argument) Arguments [0];
\r
438 if (!a.Resolve (ec, Location))
\r
441 Expression e = a.Expr;
\r
443 if (e is MethodGroupExpr) {
\r
444 MethodGroupExpr mg = (MethodGroupExpr) e;
\r
446 delegate_method = del.VerifyMethod (mg.Methods [0], Location);
\r
448 if (delegate_method == null)
\r
451 if (mg.InstanceExpression != null)
\r
452 delegate_instance_expr = mg.InstanceExpression.Resolve (ec);
\r
454 delegate_instance_expr = null;
\r
456 if (delegate_instance_expr != null)
\r
457 if (delegate_instance_expr.Type.IsValueType)
\r
458 delegate_instance_expr = new BoxedCast (delegate_instance_expr);
\r
461 del.InstanceExpression = delegate_instance_expr;
\r
462 del.TargetMethod = delegate_method;
\r
464 eclass = ExprClass.Value;
\r
468 Type e_type = e.Type;
\r
470 Delegate d = TypeManager.LookupDelegate (e_type);
\r
473 Report.Error (-12, Location, "Cannot create a delegate from something " +
\r
474 "not a delegate or a method.");
\r
478 // This is what MS's compiler reports. We could always choose
\r
479 // to be more verbose and actually give delegate-level specifics
\r
481 if (!d.VerifyDelegate (del)) {
\r
482 Report.Error (29, Location, "Cannot implicitly convert type '" + d.Name + "' " +
\r
483 "to type '" + del.Name + "'");
\r
487 delegate_instance_expr = d.InstanceExpression;
\r
488 delegate_method = d.TargetMethod;
\r
490 del.InstanceExpression = d.InstanceExpression;
\r
491 del.TargetMethod = d.TargetMethod;
\r
493 eclass = ExprClass.Value;
\r
497 public override void Emit (EmitContext ec)
\r
499 if (delegate_instance_expr == null)
\r
500 ec.ig.Emit (OpCodes.Ldnull);
\r
502 delegate_instance_expr.Emit (ec);
\r
504 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
\r
505 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
\r
509 public class DelegateInvocation : ExpressionStatement {
\r
511 public Expression InstanceExpr;
\r
512 public ArrayList Arguments;
\r
513 public Location Location;
\r
517 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
\r
519 this.InstanceExpr = instance_expr;
\r
520 this.Arguments = args;
\r
521 this.Location = loc;
\r
524 public override Expression DoResolve (EmitContext ec)
\r
526 Delegate del = TypeManager.LookupDelegate (InstanceExpr.Type);
\r
531 if (del.TargetMethod == null)
\r
534 if (Arguments != null){
\r
535 for (int i = Arguments.Count; i > 0;){
\r
537 Argument a = (Argument) Arguments [i];
\r
539 if (!a.Resolve (ec, Location))
\r
544 if (!del.VerifyApplicability (ec, Arguments, Location))
\r
547 method = del.InvokeBuilder;
\r
548 type = ((MethodInfo) method).ReturnType;
\r
550 eclass = ExprClass.Value;
\r
555 public override void Emit (EmitContext ec)
\r
557 Delegate del = TypeManager.LookupDelegate (InstanceExpr.Type);
\r
560 // Invocation on delegates call the virtual Invoke member
\r
561 // so we are always `instance' calls
\r
563 Invocation.EmitCall (ec, false, InstanceExpr, method, Arguments);
\r
566 public override void EmitStatement (EmitContext ec)
\r
570 // Pop the return value if there is one
\r
572 if (method is MethodInfo){
\r
573 if (((MethodInfo) method).ReturnType != TypeManager.void_type)
\r
574 ec.ig.Emit (OpCodes.Pop);
\r