2 // pending.cs: Pending method implementation
5 // Miguel de Icaza (miguel@gnu.org)
7 // Licensed under the terms of the GNU GPL
9 // (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
14 using System.Collections;
15 using System.Reflection;
16 using System.Reflection.Emit;
18 namespace Mono.CSharp {
20 struct TypeAndMethods {
22 public MethodInfo [] methods;
24 // Far from ideal, but we want to avoid creating a copy
26 public Type [][] args;
29 // This flag on the method says `We found a match, but
30 // because it was private, we could not use the match
34 // If a method is defined here, then we always need to
35 // create a proxy for it. This is used when implementing
36 // an interface's indexer with a different IndexerName.
37 public MethodInfo [] need_proxy;
40 public class PendingImplementation {
42 /// The container for this PendingImplementation
44 TypeContainer container;
47 /// This filter is used by FindMembers, and it is used to
48 /// extract only virtual/abstract fields
50 static MemberFilter virtual_method_filter;
53 /// This is the array of TypeAndMethods that describes the pending implementations
54 /// (both interfaces and abstract methods in parent class)
56 TypeAndMethods [] pending_implementations;
58 static bool IsVirtualFilter (MemberInfo m, object filterCriteria)
60 if (!(m is MethodInfo))
63 return ((MethodInfo) m).IsVirtual;
67 /// Inits the virtual_method_filter
69 static PendingImplementation ()
71 virtual_method_filter = new MemberFilter (IsVirtualFilter);
75 // Returns a list of the abstract methods that are exposed by all of our
76 // parents that we must implement. Notice that this `flattens' the
77 // method search space, and takes into account overrides.
79 static ArrayList GetAbstractMethods (Type t)
81 ArrayList list = null;
82 bool searching = true;
83 Type current_type = t;
88 mi = TypeContainer.FindMembers (
89 current_type, MemberTypes.Method,
90 BindingFlags.Public | BindingFlags.Instance |
91 BindingFlags.DeclaredOnly,
92 virtual_method_filter, null);
94 if (current_type == TypeManager.object_type)
97 current_type = current_type.BaseType;
98 if (!current_type.IsAbstract)
105 if (mi.Count == 1 && !(mi [0] is MethodBase))
108 list = TypeManager.CopyNewMethods (list, mi);
114 for (int i = 0; i < list.Count; i++){
115 while (list.Count > i && !((MethodInfo) list [i]).IsAbstract)
125 PendingImplementation (TypeContainer container, Type [] ifaces, ArrayList abstract_methods, int total)
127 TypeBuilder type_builder = container.TypeBuilder;
129 this.container = container;
130 pending_implementations = new TypeAndMethods [total];
134 foreach (Type t in ifaces){
137 if (t is TypeBuilder){
140 iface = TypeManager.LookupInterface (t);
142 mi = iface.GetMethods ();
144 mi = t.GetMethods ();
146 int count = mi.Length;
147 pending_implementations [i].type = t;
148 pending_implementations [i].methods = mi;
149 pending_implementations [i].args = new Type [count][];
150 pending_implementations [i].found = new bool [count];
151 pending_implementations [i].need_proxy = new MethodInfo [count];
154 foreach (MethodInfo m in mi){
155 Type [] types = TypeManager.GetArgumentTypes (m);
157 pending_implementations [i].args [j] = types;
164 if (abstract_methods != null){
165 int count = abstract_methods.Count;
166 pending_implementations [i].methods = new MethodInfo [count];
167 pending_implementations [i].need_proxy = new MethodInfo [count];
169 abstract_methods.CopyTo (pending_implementations [i].methods, 0);
170 pending_implementations [i].found = new bool [count];
171 pending_implementations [i].args = new Type [count][];
172 pending_implementations [i].type = type_builder;
175 foreach (MemberInfo m in abstract_methods){
176 MethodInfo mi = (MethodInfo) m;
178 Type [] types = TypeManager.GetArgumentTypes (mi);
180 pending_implementations [i].args [j] = types;
187 // Factory method: if there are pending implementation methods, we return a PendingImplementation
188 // object, otherwise we return null.
190 // Register method implementations are either abstract methods
191 // flagged as such on the base class or interface methods
193 static public PendingImplementation GetPendingImplementations (TypeContainer container)
195 TypeBuilder type_builder = container.TypeBuilder;
197 Type b = type_builder.BaseType;
201 // Notice that TypeBuilders will only return the interfaces that the Type
202 // is supposed to implement, not all the interfaces that the type implements.
204 // Completely broken. Anyways, we take advantage of this, so we only register
205 // the implementations that we need, as they are those that are listed by the
208 ifaces = type_builder.GetInterfaces ();
211 Type x = type_builder;
214 Type [] iff = x.GetInterfaces ();
215 Console.WriteLine ("Type: " + x.Name);
217 foreach (Type tt in iff){
218 Console.WriteLine (" Iface: " + tt.Name);
225 icount = ifaces.Length;
228 // If we are implementing an abstract class, and we are not
229 // ourselves abstract, and there are abstract methods (C# allows
230 // abstract classes that have no abstract methods), then allocate
233 // We also pre-compute the methods.
235 bool implementing_abstract = ((b != null) && b.IsAbstract && !type_builder.IsAbstract);
236 ArrayList abstract_methods = null;
238 if (implementing_abstract){
239 abstract_methods = GetAbstractMethods (b);
241 if (abstract_methods == null)
242 implementing_abstract = false;
245 int total = icount + (implementing_abstract ? 1 : 0);
249 return new PendingImplementation (container, ifaces, abstract_methods, total);
252 public enum Operation {
254 // If you change this, review the whole InterfaceMethod routine as there
255 // are a couple of assumptions on these three states
257 Lookup, ClearOne, ClearAll
261 /// Whether the specified method is an interface method implementation
263 public MethodInfo IsInterfaceMethod (Type t, string name, Type ret_type, Type [] args)
265 return InterfaceMethod (t, name, ret_type, args, Operation.Lookup, null);
268 public MethodInfo IsInterfaceIndexer (Type t, Type ret_type, Type [] args)
270 return InterfaceMethod (t, null, ret_type, args, Operation.Lookup, null);
273 public void ImplementMethod (Type t, string name, Type ret_type, Type [] args, bool clear_one)
275 InterfaceMethod (t, name, ret_type, args,
276 clear_one ? Operation.ClearOne : Operation.ClearAll, null);
279 public void ImplementIndexer (Type t, MethodInfo mi, Type ret_type, Type [] args, bool clear_one)
281 InterfaceMethod (t, mi.Name, ret_type, args,
282 clear_one ? Operation.ClearOne : Operation.ClearAll, mi);
286 /// If a method in Type `t' (or null to look in all interfaces
287 /// and the base abstract class) with name `Name', return type `ret_type' and
288 /// arguments `args' implements an interface, this method will
289 /// return the MethodInfo that this method implements.
291 /// If `name' is null, we operate solely on the method's signature. This is for
292 /// instance used when implementing indexers.
294 /// The `Operation op' controls whether to lookup, clear the pending bit, or clear
295 /// all the methods with the given signature.
297 /// The `MethodInfo need_proxy' is used when we're implementing an interface's
298 /// indexer in a class. If the new indexer's IndexerName does not match the one
299 /// that was used in the interface, then we always need to create a proxy for it.
302 public MethodInfo InterfaceMethod (Type t, string name, Type ret_type, Type [] args,
303 Operation op, MethodInfo need_proxy)
305 int arg_len = args.Length;
307 if (pending_implementations == null)
310 foreach (TypeAndMethods tm in pending_implementations){
311 if (!(t == null || tm.type == t))
315 foreach (MethodInfo m in tm.methods){
321 // `need_proxy' is not null when we're implementing an
322 // interface indexer and this is Clear(One/All) operation.
323 // If `name' is null, then we do a match solely based on the
324 // signature and not on the name (this is done in the Lookup
325 // for an interface indexer).
326 if ((name != null) && (need_proxy == null) && (name != m.Name)){
331 if (ret_type != m.ReturnType){
332 if (!((ret_type == null && m.ReturnType == TypeManager.void_type) ||
333 (m.ReturnType == null && ret_type == TypeManager.void_type)))
341 // Check if we have the same parameters
343 if (tm.args [i].Length != arg_len){
348 int j, top = args.Length;
351 for (j = 0; j < top; j++){
352 if (tm.args [i][j] != args[j]){
362 if (op != Operation.Lookup){
363 // If `t != null', then this is an explicitly interface
364 // implementation and we can always clear the method.
365 // `need_proxy' is not null if we're implementing an
366 // interface indexer. In this case, we need to create
367 // a proxy if the implementation's IndexerName doesn't
368 // match the IndexerName in the interface.
369 if ((t == null) && (need_proxy != null) && (name != m.Name))
370 tm.need_proxy [i] = need_proxy;
372 tm.methods [i] = null;
377 // Lookups and ClearOne return
379 if (op != Operation.ClearAll)
383 // If a specific type was requested, we can stop now.
391 /// C# allows this kind of scenarios:
392 /// interface I { void M (); }
393 /// class X { public void M (); }
394 /// class Y : X, I { }
396 /// For that case, we create an explicit implementation function
399 void DefineProxy (Type iface, MethodInfo parent_method, MethodInfo iface_method,
404 string proxy_name = iface.Name + "." + iface_method.Name;
406 proxy = container.TypeBuilder.DefineMethod (
408 MethodAttributes.HideBySig |
409 MethodAttributes.NewSlot |
410 MethodAttributes.Virtual,
411 CallingConventions.Standard | CallingConventions.HasThis,
412 parent_method.ReturnType, args);
414 int top = args.Length;
415 ILGenerator ig = proxy.GetILGenerator ();
417 ig.Emit (OpCodes.Ldarg_0);
418 for (int i = 0; i < top; i++){
421 ig.Emit (OpCodes.Ldarg_1); break;
423 ig.Emit (OpCodes.Ldarg_2); break;
425 ig.Emit (OpCodes.Ldarg_3); break;
427 ig.Emit (OpCodes.Ldarg, i - 1); break;
430 ig.Emit (OpCodes.Call, parent_method);
431 ig.Emit (OpCodes.Ret);
433 container.TypeBuilder.DefineMethodOverride (proxy, iface_method);
437 /// This function tells whether one of our parent classes implements
438 /// the given method (which turns out, it is valid to have an interface
439 /// implementation in a parent
441 bool ParentImplements (Type iface_type, MethodInfo mi)
445 Type [] args = TypeManager.GetArgumentTypes (mi);
446 ms = new MethodSignature (mi.Name, mi.ReturnType, args);
447 MemberList list = TypeContainer.FindMembers (
448 container.TypeBuilder.BaseType, MemberTypes.Method | MemberTypes.Property,
449 BindingFlags.Public | BindingFlags.Instance,
450 MethodSignature.method_signature_filter, ms);
455 DefineProxy (iface_type, (MethodInfo) list [0], mi, args);
460 /// Verifies that any pending abstract methods or interface methods
461 /// were implemented.
463 public bool VerifyPendingMethods ()
465 int top = pending_implementations.Length;
469 for (i = 0; i < top; i++){
470 Type type = pending_implementations [i].type;
473 foreach (MethodInfo mi in pending_implementations [i].methods){
477 if (type.IsInterface){
478 MethodInfo need_proxy =
479 pending_implementations [i].need_proxy [j];
481 if (need_proxy != null) {
482 Type [] args = TypeManager.GetArgumentTypes (mi);
483 DefineProxy (type, need_proxy, mi, args);
487 if (ParentImplements (type, mi))
492 if (pending_implementations [i].found [j])
493 extra = ". (method might be private or static)";
495 536, container.Location,
496 "`" + container.Name + "' does not implement " +
497 "interface member `" +
498 type.FullName + "." + mi.Name + "'" + extra);
501 534, container.Location,
502 "`" + container.Name + "' does not implement " +
503 "inherited abstract member `" +
504 type.FullName + "." + mi.Name + "'");