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
91 Type [] const_arg_types = new Type [2];
\r
93 const_arg_types [0] = TypeManager.object_type;
\r
94 const_arg_types [1] = TypeManager.intptr_type;
\r
96 mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
\r
97 MethodAttributes.HideBySig | MethodAttributes.Public;
\r
99 ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
\r
100 CallingConventions.Standard,
\r
103 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
105 // Here the various methods like Invoke, BeginInvoke etc are defined
\r
107 param_types = Parameters.GetParameterInfo (parent);
\r
108 ret_type = parent.LookupType (ReturnType, false);
\r
109 CallingConventions cc = Parameters.GetCallingConvention ();
\r
111 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
\r
113 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
\r
119 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
121 int params_num = param_types.Length;
\r
122 Type [] async_param_types = new Type [params_num + 2];
\r
124 param_types.CopyTo (async_param_types, 0);
\r
126 async_param_types [params_num] = TypeManager.asynccallback_type;
\r
127 async_param_types [params_num + 1] = TypeManager.object_type;
\r
129 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual |
\r
130 MethodAttributes.NewSlot;
\r
132 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
\r
135 TypeManager.iasyncresult_type,
\r
136 async_param_types);
\r
138 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
140 Type [] end_param_types = new Type [1];
\r
142 end_param_types [0] = TypeManager.iasyncresult_type;
\r
144 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke",
\r
150 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
155 // Verifies whether the method in question is compatible with the delegate
\r
156 // Returns the method itself if okay and null if not.
\r
158 public MethodBase VerifyMethod (MethodBase mb, Location loc)
\r
160 ParameterData pd = Invocation.GetParameterData (mb);
\r
162 bool mismatch = false;
\r
163 for (int i = param_types.Length; i > 0; ) {
\r
166 if (param_types [i] == pd.ParameterType (i))
\r
175 Report.Error (123, loc, "Method '" + Invocation.FullMethodDesc (mb) + "' does not match " +
\r
176 "delegate '" + FullDelegateDesc () + "'");
\r
180 if (ret_type == ((MethodInfo) mb).ReturnType)
\r
186 Report.Error (123, loc, "Method '" + Invocation.FullMethodDesc (mb) + "' does not match " +
\r
187 "delegate '" + FullDelegateDesc () + "'");
\r
195 // Verifies whether the invocation arguments are compatible with the
\r
196 // delegate's target method
\r
198 public bool VerifyApplicability (EmitContext ec, ArrayList args, Location loc)
\r
205 arg_count = args.Count;
\r
207 if (param_types.Length != arg_count) {
\r
208 Report.Error (1593, loc,
\r
209 "Delegate '" + Name + "' does not take '" + arg_count + "' arguments");
\r
213 for (int i = arg_count; i > 0;) {
\r
216 Argument a = (Argument) args [i];
\r
217 Expression a_expr = a.Expr;
\r
219 if (param_types [i] != a_expr.Type) {
\r
221 conv = Expression.ConvertImplicitStandard (ec, a_expr, param_types [i], loc);
\r
223 if (conv == null) {
\r
224 Report.Error (1594, loc,
\r
225 "Delegate '" + Name + "' has some invalid arguments.");
\r
227 Report.Error (1503, loc,
\r
228 "Argument " + (i+1) +
\r
229 ": Cannot convert from '" +
\r
230 TypeManager.CSharpName (a_expr.Type)
\r
231 + "' to '" + TypeManager.CSharpName (param_types [i]) + "'");
\r
235 if (a_expr != conv)
\r
244 // Verifies whether the delegate in question is compatible with this one in
\r
245 // order to determine if instantiation from the same is possible.
\r
247 public bool VerifyDelegate (Delegate del)
\r
249 if (ret_type != del.TargetReturnType)
\r
252 Type [] other_param_types = del.ParameterTypes;
\r
254 if (param_types.Length != other_param_types.Length)
\r
257 for (int i = param_types.Length; i > 0; ) {
\r
260 if (param_types [i] != other_param_types [i])
\r
264 // FIXME : Hey, what about parameter modifiers ?
\r
270 public string FullDelegateDesc ()
\r
272 StringBuilder sb = new StringBuilder (TypeManager.CSharpName (System.Type.GetType (ReturnType)));
\r
274 sb.Append (" " + Name);
\r
277 int length = param_types.Length;
\r
279 for (int i = length; i > 0; ) {
\r
282 sb.Append (TypeManager.CSharpName (param_types [length - i - 1]));
\r
288 return sb.ToString ();
\r
292 public void CloseDelegate ()
\r
294 TypeBuilder.CreateType ();
\r
297 public int ModFlags {
\r
303 public Expression InstanceExpression {
\r
305 return instance_expr;
\r
308 instance_expr = value;
\r
312 public MethodBase TargetMethod {
\r
314 return delegate_method;
\r
317 delegate_method = value;
\r
321 public Type TargetReturnType {
\r
327 public Type [] ParameterTypes {
\r
329 return param_types;
\r
335 public class NewDelegate : Expression {
\r
337 public ArrayList Arguments;
\r
339 MethodBase constructor_method;
\r
340 MethodBase delegate_method;
\r
341 Expression delegate_instance_expr;
\r
345 public NewDelegate (Type type, ArrayList Arguments, Location loc)
\r
348 this.Arguments = Arguments;
\r
349 this.Location = loc;
\r
352 public override Expression DoResolve (EmitContext ec)
\r
354 Delegate del = TypeManager.LookupDelegate (type);
\r
355 constructor_method = del.ConstructorBuilder;
\r
357 if (Arguments == null) {
\r
358 Report.Error (-11, Location,
\r
359 "Delegate creation expression takes only one argument");
\r
363 if (Arguments.Count != 1) {
\r
364 Report.Error (-11, Location,
\r
365 "Delegate creation expression takes only one argument");
\r
369 Argument a = (Argument) Arguments [0];
\r
371 if (!a.Resolve (ec))
\r
374 Expression e = a.Expr;
\r
376 if (e is MethodGroupExpr) {
\r
377 MethodGroupExpr mg = (MethodGroupExpr) e;
\r
379 delegate_method = del.VerifyMethod (mg.Methods [0], Location);
\r
381 if (delegate_method == null)
\r
384 if (mg.InstanceExpression != null)
\r
385 delegate_instance_expr = mg.InstanceExpression.Resolve (ec);
\r
387 delegate_instance_expr = null;
\r
389 if (delegate_instance_expr != null)
\r
390 if (delegate_instance_expr.Type.IsValueType)
\r
391 delegate_instance_expr = new BoxedCast (delegate_instance_expr);
\r
394 del.InstanceExpression = delegate_instance_expr;
\r
395 del.TargetMethod = delegate_method;
\r
397 eclass = ExprClass.Value;
\r
401 Type e_type = e.Type;
\r
403 Delegate d = TypeManager.LookupDelegate (e_type);
\r
406 Report.Error (-12, Location, "Cannot create a delegate from something " +
\r
407 "not a delegate or a method.");
\r
411 // This is what MS's compiler reports. We could always choose
\r
412 // to be more verbose and actually give delegate-level specifics
\r
414 if (!d.VerifyDelegate (del)) {
\r
415 Report.Error (29, Location, "Cannot implicitly convert type '" + d.Name + "' " +
\r
416 "to type '" + del.Name + "'");
\r
420 delegate_instance_expr = d.InstanceExpression;
\r
421 delegate_method = d.TargetMethod;
\r
423 del.InstanceExpression = d.InstanceExpression;
\r
424 del.TargetMethod = d.TargetMethod;
\r
426 eclass = ExprClass.Value;
\r
430 public override void Emit (EmitContext ec)
\r
432 if (delegate_instance_expr == null)
\r
433 ec.ig.Emit (OpCodes.Ldnull);
\r
435 delegate_instance_expr.Emit (ec);
\r
437 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
\r
438 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
\r
442 public class DelegateInvocation : Expression {
\r
444 public Expression InstanceExpr;
\r
445 public ArrayList Arguments;
\r
446 public Location Location;
\r
450 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
\r
452 this.InstanceExpr = instance_expr;
\r
453 this.Arguments = args;
\r
454 this.Location = loc;
\r
457 public override Expression DoResolve (EmitContext ec)
\r
459 Delegate del = TypeManager.LookupDelegate (InstanceExpr.Type);
\r
464 if (del.TargetMethod == null)
\r
467 if (Arguments != null){
\r
468 for (int i = Arguments.Count; i > 0;){
\r
470 Argument a = (Argument) Arguments [i];
\r
472 if (!a.Resolve (ec))
\r
477 if (!del.VerifyApplicability (ec, Arguments, Location))
\r
480 method = del.InvokeBuilder;
\r
481 type = ((MethodInfo) method).ReturnType;
\r
483 eclass = ExprClass.Value;
\r
488 public override void Emit (EmitContext ec)
\r
490 Delegate del = TypeManager.LookupDelegate (InstanceExpr.Type);
\r
491 Invocation.EmitCall (ec, del.TargetMethod.IsStatic, InstanceExpr, method, Arguments);
\r