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
18 namespace Mono.CSharp {
\r
23 public class Delegate {
\r
25 public readonly string Name;
\r
26 public readonly string ReturnType;
\r
27 public int mod_flags;
\r
28 public Parameters Parameters;
\r
29 public Attributes OptAttributes;
\r
30 public TypeBuilder TypeBuilder;
\r
32 public ConstructorBuilder ConstructorBuilder;
\r
33 public MethodBuilder InvokeBuilder;
\r
34 public MethodBuilder BeginInvokeBuilder;
\r
35 public MethodBuilder EndInvokeBuilder;
\r
37 public readonly RootContext RootContext;
\r
39 Type [] param_types;
\r
42 Expression instance_expr;
\r
43 MethodBase delegate_method;
\r
47 const int AllowedModifiers =
\r
50 Modifiers.PROTECTED |
\r
51 Modifiers.INTERNAL |
\r
54 public Delegate (RootContext rc, string type, int mod_flags, string name, Parameters param_list,
\r
55 Attributes attrs, Location loc)
\r
57 this.RootContext = rc;
\r
59 this.ReturnType = type;
\r
60 this.mod_flags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PUBLIC);
\r
61 Parameters = param_list;
\r
62 OptAttributes = attrs;
\r
66 public void DefineDelegate (object parent_builder)
\r
68 TypeAttributes attr;
\r
70 if (parent_builder is ModuleBuilder) {
\r
71 ModuleBuilder builder = (ModuleBuilder) parent_builder;
\r
73 attr = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed;
\r
75 TypeBuilder = builder.DefineType (Name, attr, TypeManager.delegate_type);
\r
78 TypeBuilder builder = (TypeBuilder) parent_builder;
\r
80 attr = TypeAttributes.NestedPublic | TypeAttributes.Class | TypeAttributes.Sealed;
\r
82 TypeBuilder = builder.DefineNestedType (Name, attr, TypeManager.delegate_type);
\r
86 RootContext.TypeManager.AddDelegateType (Name, TypeBuilder, this);
\r
89 public void Populate (TypeContainer parent)
\r
92 MethodAttributes mattr;
\r
95 Type [] const_arg_types = new Type [2];
\r
97 const_arg_types [0] = TypeManager.object_type;
\r
98 const_arg_types [1] = TypeManager.intptr_type;
\r
100 mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
\r
101 MethodAttributes.HideBySig | MethodAttributes.Public;
\r
103 ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
\r
104 CallingConventions.Standard,
\r
107 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
109 // Here the various methods like Invoke, BeginInvoke etc are defined
\r
111 param_types = Parameters.GetParameterInfo (parent);
\r
112 ret_type = parent.LookupType (ReturnType, false);
\r
113 CallingConventions cc = Parameters.GetCallingConvention ();
\r
115 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
\r
117 InvokeBuilder = TypeBuilder.DefineMethod ("Invoke",
\r
123 for (i = 0 ; i < param_types.Length; i++) {
\r
124 Parameter p = Parameters.FixedParameters [i];
\r
125 string name = p.Name;
\r
126 ParameterBuilder pb = InvokeBuilder.DefineParameter (i+1, p.Attributes, name);
\r
129 InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
131 TypeContainer.RegisterParameterForBuilder (InvokeBuilder,
\r
132 new InternalParameters (parent, Parameters));
\r
134 int params_num = param_types.Length;
\r
135 Type [] async_param_types = new Type [params_num + 2];
\r
137 param_types.CopyTo (async_param_types, 0);
\r
139 async_param_types [params_num] = TypeManager.asynccallback_type;
\r
140 async_param_types [params_num + 1] = TypeManager.object_type;
\r
142 mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual |
\r
143 MethodAttributes.NewSlot;
\r
145 BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
\r
148 TypeManager.iasyncresult_type,
\r
149 async_param_types);
\r
151 for (i = 0 ; i < param_types.Length; i++) {
\r
152 Parameter p = Parameters.FixedParameters [i];
\r
153 string name = p.Name;
\r
154 BeginInvokeBuilder.DefineParameter (i + 1, p.Attributes, name);
\r
157 BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
\r
158 BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
\r
160 BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
162 Parameter [] async_params = new Parameter [params_num + 2];
\r
163 Parameters.FixedParameters.CopyTo (async_params, 0);
\r
165 async_params [params_num] = new Parameter ("System.AsyncCallback", "callback",
\r
166 Parameter.Modifier.NONE, null);
\r
167 async_params [params_num + 1] = new Parameter ("System.IAsyncResult", "object",
\r
168 Parameter.Modifier.NONE, null);
\r
170 TypeContainer.RegisterParameterForBuilder (BeginInvokeBuilder,
\r
171 new InternalParameters (parent, new Parameters (async_params, null)));
\r
173 Type [] end_param_types = new Type [1];
\r
174 end_param_types [0] = TypeManager.iasyncresult_type;
\r
176 EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke",
\r
181 EndInvokeBuilder.DefineParameter (1, ParameterAttributes.None, "result");
\r
183 EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
\r
185 Parameter [] end_params = new Parameter [1];
\r
186 end_params [0] = new Parameter ("System.IAsyncResult", "result",
\r
187 Parameter.Modifier.NONE, null);
\r
189 TypeContainer.RegisterParameterForBuilder (EndInvokeBuilder,
\r
190 new InternalParameters (parent, new Parameters (end_params, null)));
\r
195 // Verifies whether the method in question is compatible with the delegate
\r
196 // Returns the method itself if okay and null if not.
\r
198 public MethodBase VerifyMethod (MethodBase mb, Location loc)
\r
200 ParameterData pd = Invocation.GetParameterData (mb);
\r
202 bool mismatch = false;
\r
203 for (int i = param_types.Length; i > 0; ) {
\r
206 if (param_types [i] == pd.ParameterType (i))
\r
215 Report.Error (123, loc, "Method '" + Invocation.FullMethodDesc (mb) + "' does not match " +
\r
216 "delegate '" + FullDelegateDesc () + "'");
\r
220 if (ret_type == ((MethodInfo) mb).ReturnType)
\r
226 Report.Error (123, loc, "Method '" + Invocation.FullMethodDesc (mb) + "' does not match " +
\r
227 "delegate '" + FullDelegateDesc () + "'");
\r
235 // Verifies whether the invocation arguments are compatible with the
\r
236 // delegate's target method
\r
238 public bool VerifyApplicability (EmitContext ec, ArrayList args, Location loc)
\r
245 arg_count = args.Count;
\r
247 if (param_types.Length != arg_count) {
\r
248 Report.Error (1593, loc,
\r
249 "Delegate '" + Name + "' does not take '" + arg_count + "' arguments");
\r
253 for (int i = arg_count; i > 0;) {
\r
256 Argument a = (Argument) args [i];
\r
257 Expression a_expr = a.Expr;
\r
259 if (param_types [i] != a_expr.Type) {
\r
261 conv = Expression.ConvertImplicitStandard (ec, a_expr, param_types [i], loc);
\r
263 if (conv == null) {
\r
264 Report.Error (1594, loc,
\r
265 "Delegate '" + Name + "' has some invalid arguments.");
\r
267 Report.Error (1503, loc,
\r
268 "Argument " + (i+1) +
\r
269 ": Cannot convert from '" +
\r
270 TypeManager.CSharpName (a_expr.Type)
\r
271 + "' to '" + TypeManager.CSharpName (param_types [i]) + "'");
\r
275 if (a_expr != conv)
\r
284 /// Verifies whether the delegate in question is compatible with this one in
\r
285 /// order to determine if instantiation from the same is possible.
\r
287 public bool VerifyDelegate (Delegate del)
\r
289 if (ret_type != del.TargetReturnType)
\r
292 Type [] other_param_types = del.ParameterTypes;
\r
294 if (param_types.Length != other_param_types.Length)
\r
297 for (int i = param_types.Length; i > 0; ) {
\r
300 if (param_types [i] != other_param_types [i])
\r
304 // FIXME : Hey, what about parameter modifiers ?
\r
310 public string FullDelegateDesc ()
\r
312 StringBuilder sb = new StringBuilder (TypeManager.CSharpName (System.Type.GetType (ReturnType)));
\r
314 sb.Append (" " + Name);
\r
317 int length = param_types.Length;
\r
319 for (int i = length; i > 0; ) {
\r
322 sb.Append (TypeManager.CSharpName (param_types [length - i - 1]));
\r
328 return sb.ToString ();
\r
332 // Hack around System.Reflection as found everywhere else
\r
333 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria)
\r
335 ArrayList members = new ArrayList ();
\r
337 if ((mt & MemberTypes.Method) != 0) {
\r
338 if (filter (ConstructorBuilder, criteria))
\r
339 members.Add (ConstructorBuilder);
\r
341 if (filter (InvokeBuilder, criteria))
\r
342 members.Add (InvokeBuilder);
\r
344 if (filter (BeginInvokeBuilder, criteria))
\r
345 members.Add (BeginInvokeBuilder);
\r
347 if (filter (EndInvokeBuilder, criteria))
\r
348 members.Add (EndInvokeBuilder);
\r
351 int count = members.Count;
\r
354 MemberInfo [] mi = new MemberInfo [count];
\r
355 members.CopyTo (mi, 0);
\r
362 public void CloseDelegate ()
\r
364 TypeBuilder.CreateType ();
\r
367 public int ModFlags {
\r
373 public Expression InstanceExpression {
\r
375 return instance_expr;
\r
378 instance_expr = value;
\r
382 public MethodBase TargetMethod {
\r
384 return delegate_method;
\r
387 delegate_method = value;
\r
391 public Type TargetReturnType {
\r
397 public Type [] ParameterTypes {
\r
399 return param_types;
\r
405 public class NewDelegate : Expression {
\r
407 public ArrayList Arguments;
\r
409 MethodBase constructor_method;
\r
410 MethodBase delegate_method;
\r
411 Expression delegate_instance_expr;
\r
415 public NewDelegate (Type type, ArrayList Arguments, Location loc)
\r
418 this.Arguments = Arguments;
\r
419 this.Location = loc;
\r
422 public override Expression DoResolve (EmitContext ec)
\r
424 Delegate del = TypeManager.LookupDelegate (type);
\r
425 constructor_method = del.ConstructorBuilder;
\r
427 if (Arguments == null) {
\r
428 Report.Error (-11, Location,
\r
429 "Delegate creation expression takes only one argument");
\r
433 if (Arguments.Count != 1) {
\r
434 Report.Error (-11, Location,
\r
435 "Delegate creation expression takes only one argument");
\r
439 Argument a = (Argument) Arguments [0];
\r
441 if (!a.Resolve (ec, Location))
\r
444 Expression e = a.Expr;
\r
446 if (e is MethodGroupExpr) {
\r
447 MethodGroupExpr mg = (MethodGroupExpr) e;
\r
449 delegate_method = del.VerifyMethod (mg.Methods [0], Location);
\r
451 if (delegate_method == null)
\r
454 if (mg.InstanceExpression != null)
\r
455 delegate_instance_expr = mg.InstanceExpression.Resolve (ec);
\r
457 delegate_instance_expr = null;
\r
459 if (delegate_instance_expr != null)
\r
460 if (delegate_instance_expr.Type.IsValueType)
\r
461 delegate_instance_expr = new BoxedCast (delegate_instance_expr);
\r
464 del.InstanceExpression = delegate_instance_expr;
\r
465 del.TargetMethod = delegate_method;
\r
467 eclass = ExprClass.Value;
\r
471 Type e_type = e.Type;
\r
473 Delegate d = TypeManager.LookupDelegate (e_type);
\r
476 Report.Error (-12, Location, "Cannot create a delegate from something " +
\r
477 "not a delegate or a method.");
\r
481 // This is what MS's compiler reports. We could always choose
\r
482 // to be more verbose and actually give delegate-level specifics
\r
484 if (!d.VerifyDelegate (del)) {
\r
485 Report.Error (29, Location, "Cannot implicitly convert type '" + d.Name + "' " +
\r
486 "to type '" + del.Name + "'");
\r
490 delegate_instance_expr = d.InstanceExpression;
\r
491 delegate_method = d.TargetMethod;
\r
493 del.InstanceExpression = d.InstanceExpression;
\r
494 del.TargetMethod = d.TargetMethod;
\r
496 eclass = ExprClass.Value;
\r
500 public override void Emit (EmitContext ec)
\r
502 if (delegate_instance_expr == null)
\r
503 ec.ig.Emit (OpCodes.Ldnull);
\r
505 delegate_instance_expr.Emit (ec);
\r
507 ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
\r
508 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
\r
512 public class DelegateInvocation : ExpressionStatement {
\r
514 public Expression InstanceExpr;
\r
515 public ArrayList Arguments;
\r
516 public Location Location;
\r
520 public DelegateInvocation (Expression instance_expr, ArrayList args, Location loc)
\r
522 this.InstanceExpr = instance_expr;
\r
523 this.Arguments = args;
\r
524 this.Location = loc;
\r
527 public override Expression DoResolve (EmitContext ec)
\r
529 Delegate del = TypeManager.LookupDelegate (InstanceExpr.Type);
\r
534 if (del.TargetMethod == null)
\r
537 if (Arguments != null){
\r
538 for (int i = Arguments.Count; i > 0;){
\r
540 Argument a = (Argument) Arguments [i];
\r
542 if (!a.Resolve (ec, Location))
\r
547 if (!del.VerifyApplicability (ec, Arguments, Location))
\r
550 method = del.InvokeBuilder;
\r
551 type = ((MethodInfo) method).ReturnType;
\r
553 eclass = ExprClass.Value;
\r
558 public override void Emit (EmitContext ec)
\r
560 Delegate del = TypeManager.LookupDelegate (InstanceExpr.Type);
\r
563 // Invocation on delegates call the virtual Invoke member
\r
564 // so we are always `instance' calls
\r
566 Invocation.EmitCall (ec, false, InstanceExpr, method, Arguments);
\r
569 public override void EmitStatement (EmitContext ec)
\r
573 // Pop the return value if there is one
\r
575 if (method is MethodInfo){
\r
576 if (((MethodInfo) method).ReturnType != TypeManager.void_type)
\r
577 ec.ig.Emit (OpCodes.Pop);
\r