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 int params_num = param_types.Length;
\r
129 Type [] async_param_types = new Type [params_num + 2];
\r
131 param_types.CopyTo (async_param_types, 0);
\r
133 async_param_types [params_num] = TypeManager.asynccallback_type;
\r
134 async_param_types [params_num + 1] = TypeManager.object_type;
\r
136 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual |
\r
137 MethodAttributes.NewSlot;
\r
139 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
\r
142 TypeManager.iasyncresult_type,
\r
143 async_param_types);
\r
145 for (i = 0 ; i < param_types.Length; i++) {
\r
146 Parameter p = Parameters.FixedParameters [i];
\r
147 string name = p.Name;
\r
148 BeginInvokeBuilder.DefineParameter (i + 1, p.Attributes, name);
\r
151 BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
\r
152 BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
\r
154 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
156 Type [] end_param_types = new Type [1];
\r
158 end_param_types [0] = TypeManager.iasyncresult_type;
\r
160 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke",
\r
165 EndInvokeBuilder.DefineParameter (1, ParameterAttributes.None, "result");
\r
167 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
172 // Verifies whether the method in question is compatible with the delegate
\r
173 // Returns the method itself if okay and null if not.
\r
175 public MethodBase VerifyMethod (MethodBase mb, Location loc)
\r
177 ParameterData pd = Invocation.GetParameterData (mb);
\r
179 bool mismatch = false;
\r
180 for (int i = param_types.Length; i > 0; ) {
\r
183 if (param_types [i] == pd.ParameterType (i))
\r
192 Report.Error (123, loc, "Method '" + Invocation.FullMethodDesc (mb) + "' does not match " +
\r
193 "delegate '" + FullDelegateDesc () + "'");
\r
197 if (ret_type == ((MethodInfo) mb).ReturnType)
\r
203 Report.Error (123, loc, "Method '" + Invocation.FullMethodDesc (mb) + "' does not match " +
\r
204 "delegate '" + FullDelegateDesc () + "'");
\r
212 // Verifies whether the invocation arguments are compatible with the
\r
213 // delegate's target method
\r
215 public bool VerifyApplicability (EmitContext ec, ArrayList args, Location loc)
\r
222 arg_count = args.Count;
\r
224 if (param_types.Length != arg_count) {
\r
225 Report.Error (1593, loc,
\r
226 "Delegate '" + Name + "' does not take '" + arg_count + "' arguments");
\r
230 for (int i = arg_count; i > 0;) {
\r
233 Argument a = (Argument) args [i];
\r
234 Expression a_expr = a.Expr;
\r
236 if (param_types [i] != a_expr.Type) {
\r
238 conv = Expression.ConvertImplicitStandard (ec, a_expr, param_types [i], loc);
\r
240 if (conv == null) {
\r
241 Report.Error (1594, loc,
\r
242 "Delegate '" + Name + "' has some invalid arguments.");
\r
244 Report.Error (1503, loc,
\r
245 "Argument " + (i+1) +
\r
246 ": Cannot convert from '" +
\r
247 TypeManager.CSharpName (a_expr.Type)
\r
248 + "' to '" + TypeManager.CSharpName (param_types [i]) + "'");
\r
252 if (a_expr != conv)
\r
261 // Verifies whether the delegate in question is compatible with this one in
\r
262 // order to determine if instantiation from the same is possible.
\r
264 public bool VerifyDelegate (Delegate del)
\r
266 if (ret_type != del.TargetReturnType)
\r
269 Type [] other_param_types = del.ParameterTypes;
\r
271 if (param_types.Length != other_param_types.Length)
\r
274 for (int i = param_types.Length; i > 0; ) {
\r
277 if (param_types [i] != other_param_types [i])
\r
281 // FIXME : Hey, what about parameter modifiers ?
\r
287 public string FullDelegateDesc ()
\r
289 StringBuilder sb = new StringBuilder (TypeManager.CSharpName (System.Type.GetType (ReturnType)));
\r
291 sb.Append (" " + Name);
\r
294 int length = param_types.Length;
\r
296 for (int i = length; i > 0; ) {
\r
299 sb.Append (TypeManager.CSharpName (param_types [length - i - 1]));
\r
305 return sb.ToString ();
\r
309 public void CloseDelegate ()
\r
311 TypeBuilder.CreateType ();
\r
314 public int ModFlags {
\r
320 public Expression InstanceExpression {
\r
322 return instance_expr;
\r
325 instance_expr = value;
\r
329 public MethodBase TargetMethod {
\r
331 return delegate_method;
\r
334 delegate_method = value;
\r
338 public Type TargetReturnType {
\r
344 public Type [] ParameterTypes {
\r
346 return param_types;
\r
352 public class NewDelegate : Expression {
\r
354 public ArrayList Arguments;
\r
356 MethodBase constructor_method;
\r
357 MethodBase delegate_method;
\r
358 Expression delegate_instance_expr;
\r
362 public NewDelegate (Type type, ArrayList Arguments, Location loc)
\r
365 this.Arguments = Arguments;
\r
366 this.Location = loc;
\r
369 public override Expression DoResolve (EmitContext ec)
\r
371 Delegate del = TypeManager.LookupDelegate (type);
\r
372 constructor_method = del.ConstructorBuilder;
\r
374 if (Arguments == null) {
\r
375 Report.Error (-11, Location,
\r
376 "Delegate creation expression takes only one argument");
\r
380 if (Arguments.Count != 1) {
\r
381 Report.Error (-11, Location,
\r
382 "Delegate creation expression takes only one argument");
\r
386 Argument a = (Argument) Arguments [0];
\r
388 if (!a.Resolve (ec))
\r
391 Expression e = a.Expr;
\r
393 if (e is MethodGroupExpr) {
\r
394 MethodGroupExpr mg = (MethodGroupExpr) e;
\r
396 delegate_method = del.VerifyMethod (mg.Methods [0], Location);
\r
398 if (delegate_method == null)
\r
401 if (mg.InstanceExpression != null)
\r
402 delegate_instance_expr = mg.InstanceExpression.Resolve (ec);
\r
404 delegate_instance_expr = null;
\r
406 if (delegate_instance_expr != null)
\r
407 if (delegate_instance_expr.Type.IsValueType)
\r
408 delegate_instance_expr = new BoxedCast (delegate_instance_expr);
\r
411 del.InstanceExpression = delegate_instance_expr;
\r
412 del.TargetMethod = delegate_method;
\r
414 eclass = ExprClass.Value;
\r
418 Type e_type = e.Type;
\r
420 Delegate d = TypeManager.LookupDelegate (e_type);
\r
423 Report.Error (-12, Location, "Cannot create a delegate from something " +
\r
424 "not a delegate or a method.");
\r
428 // This is what MS's compiler reports. We could always choose
\r
429 // to be more verbose and actually give delegate-level specifics
\r
431 if (!d.VerifyDelegate (del)) {
\r
432 Report.Error (29, Location, "Cannot implicitly convert type '" + d.Name + "' " +
\r
433 "to type '" + del.Name + "'");
\r
437 delegate_instance_expr = d.InstanceExpression;
\r
438 delegate_method = d.TargetMethod;
\r
440 del.InstanceExpression = d.InstanceExpression;
\r
441 del.TargetMethod = d.TargetMethod;
\r
443 eclass = ExprClass.Value;
\r
447 public override void Emit (EmitContext ec)
\r
449 if (delegate_instance_expr == null)
\r
450 ec.ig.Emit (OpCodes.Ldnull);
\r
452 delegate_instance_expr.Emit (ec);
\r
454 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
\r
455 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
\r
459 public class DelegateInvocation : ExpressionStatement {
\r
461 public Expression InstanceExpr;
\r
462 public ArrayList Arguments;
\r
463 public Location Location;
\r
467 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
\r
469 this.InstanceExpr = instance_expr;
\r
470 this.Arguments = args;
\r
471 this.Location = loc;
\r
474 public override Expression DoResolve (EmitContext ec)
\r
476 Delegate del = TypeManager.LookupDelegate (InstanceExpr.Type);
\r
481 if (del.TargetMethod == null)
\r
484 if (Arguments != null){
\r
485 for (int i = Arguments.Count; i > 0;){
\r
487 Argument a = (Argument) Arguments [i];
\r
489 if (!a.Resolve (ec))
\r
494 if (!del.VerifyApplicability (ec, Arguments, Location))
\r
497 method = del.InvokeBuilder;
\r
498 type = ((MethodInfo) method).ReturnType;
\r
500 eclass = ExprClass.Value;
\r
505 public override void Emit (EmitContext ec)
\r
507 Delegate del = TypeManager.LookupDelegate (InstanceExpr.Type);
\r
508 Invocation.EmitCall (ec, del.TargetMethod.IsStatic, InstanceExpr, method, Arguments);
\r
511 public override void EmitStatement (EmitContext ec)
\r
515 // Pop the return value if there is one
\r
517 if (method is MethodInfo){
\r
518 if (((MethodInfo) method).ReturnType != TypeManager.void_type)
\r
519 ec.ig.Emit (OpCodes.Pop);
\r