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
35 public class PendingImplementation {
37 /// The container for this PendingImplementation
39 TypeContainer container;
42 /// This filter is used by FindMembers, and it is used to
43 /// extract only virtual/abstract fields
45 static MemberFilter virtual_method_filter;
48 /// This is the array of TypeAndMethods that describes the pending implementations
49 /// (both interfaces and abstract methods in parent class)
51 TypeAndMethods [] pending_implementations;
53 static bool IsVirtualFilter (MemberInfo m, object filterCriteria)
55 if (!(m is MethodInfo))
58 return ((MethodInfo) m).IsVirtual;
62 /// Inits the virtual_method_filter
64 static PendingImplementation ()
66 virtual_method_filter = new MemberFilter (IsVirtualFilter);
70 // Returns a list of the abstract methods that are exposed by all of our
71 // parents that we must implement. Notice that this `flattens' the
72 // method search space, and takes into account overrides.
74 static ArrayList GetAbstractMethods (Type t)
76 ArrayList list = null;
77 bool searching = true;
78 Type current_type = t;
83 mi = TypeContainer.FindMembers (
84 current_type, MemberTypes.Method,
85 BindingFlags.Public | BindingFlags.Instance |
86 BindingFlags.DeclaredOnly,
87 virtual_method_filter, null);
89 if (current_type == TypeManager.object_type)
92 current_type = current_type.BaseType;
93 if (!current_type.IsAbstract)
100 int count = mi.Length;
104 if (count == 1 && !(mi [0] is MethodBase))
107 list = TypeManager.CopyNewMethods (list, mi);
113 for (int i = 0; i < list.Count; i++){
114 while (list.Count > i && !((MethodInfo) list [i]).IsAbstract)
124 PendingImplementation (TypeContainer container, Type [] ifaces, ArrayList abstract_methods, int total)
126 TypeBuilder type_builder = container.TypeBuilder;
128 this.container = container;
129 pending_implementations = new TypeAndMethods [total];
133 foreach (Type t in ifaces){
136 if (t is TypeBuilder){
139 iface = TypeManager.LookupInterface (t);
141 mi = iface.GetMethods ();
143 mi = t.GetMethods ();
145 int count = mi.Length;
146 pending_implementations [i].type = t;
147 pending_implementations [i].methods = mi;
148 pending_implementations [i].args = new Type [count][];
149 pending_implementations [i].found = new bool [count];
152 foreach (MethodInfo m in mi){
153 Type [] types = TypeManager.GetArgumentTypes (m);
155 pending_implementations [i].args [j] = types;
162 if (abstract_methods != null){
163 int count = abstract_methods.Count;
164 pending_implementations [i].methods = new MethodInfo [count];
166 abstract_methods.CopyTo (pending_implementations [i].methods, 0);
167 pending_implementations [i].found = new bool [count];
168 pending_implementations [i].args = new Type [count][];
169 pending_implementations [i].type = type_builder;
172 foreach (MemberInfo m in abstract_methods){
173 MethodInfo mi = (MethodInfo) m;
175 Type [] types = TypeManager.GetArgumentTypes (mi);
177 pending_implementations [i].args [j] = types;
184 // Factory method: if there are pending implementation methods, we return a PendingImplementation
185 // object, otherwise we return null.
187 // Register method implementations are either abstract methods
188 // flagged as such on the base class or interface methods
190 static public PendingImplementation GetPendingImplementations (TypeContainer container)
192 TypeBuilder type_builder = container.TypeBuilder;
194 Type b = type_builder.BaseType;
198 // Notice that TypeBuilders will only return the interfaces that the Type
199 // is supposed to implement, not all the interfaces that the type implements.
201 // Completely broken. Anyways, we take advantage of this, so we only register
202 // the implementations that we need, as they are those that are listed by the
205 ifaces = type_builder.GetInterfaces ();
208 Type x = type_builder;
211 Type [] iff = x.GetInterfaces ();
212 Console.WriteLine ("Type: " + x.Name);
214 foreach (Type tt in iff){
215 Console.WriteLine (" Iface: " + tt.Name);
222 icount = ifaces.Length;
225 // If we are implementing an abstract class, and we are not
226 // ourselves abstract, and there are abstract methods (C# allows
227 // abstract classes that have no abstract methods), then allocate
230 // We also pre-compute the methods.
232 bool implementing_abstract = ((b != null) && b.IsAbstract && !type_builder.IsAbstract);
233 ArrayList abstract_methods = null;
235 if (implementing_abstract){
236 abstract_methods = GetAbstractMethods (b);
238 if (abstract_methods == null)
239 implementing_abstract = false;
242 int total = icount + (implementing_abstract ? 1 : 0);
246 return new PendingImplementation (container, ifaces, abstract_methods, total);
249 public enum Operation {
251 // If you change this, review the whole InterfaceMethod routine as there
252 // are a couple of assumptions on these three states
254 Lookup, ClearOne, ClearAll
258 /// Whether the specified method is an interface method implementation
260 public MethodInfo IsInterfaceMethod (Type t, string name, Type ret_type, Type [] args)
262 return InterfaceMethod (t, name, ret_type, args, Operation.Lookup);
265 public void ImplementMethod (Type t, string name, Type ret_type, Type [] args, bool clear_one)
267 InterfaceMethod (t, name, ret_type, args, clear_one ? Operation.ClearOne : Operation.ClearAll);
271 /// If a method in Type `t' (or null to look in all interfaces
272 /// and the base abstract class) with name `Name', return type `ret_type' and
273 /// arguments `args' implements an interface, this method will
274 /// return the MethodInfo that this method implements.
276 /// The `Operation op' controls whether to lookup, clear the pending bit, or clear
277 /// all the methods with the given signature.
279 public MethodInfo InterfaceMethod (Type t, string name, Type ret_type, Type [] args, Operation op)
281 int arg_len = args.Length;
283 if (pending_implementations == null)
286 foreach (TypeAndMethods tm in pending_implementations){
287 if (!(t == null || tm.type == t))
291 foreach (MethodInfo m in tm.methods){
302 if (ret_type != m.ReturnType){
303 if (!((ret_type == null && m.ReturnType == TypeManager.void_type) ||
304 (m.ReturnType == null && ret_type == TypeManager.void_type)))
312 // Check if we have the same parameters
314 if (tm.args [i].Length != arg_len){
319 int j, top = args.Length;
322 for (j = 0; j < top; j++){
323 if (tm.args [i][j] != args[j]){
333 if (op != Operation.Lookup)
334 tm.methods [i] = null;
338 // Lookups and ClearOne return
340 if (op != Operation.ClearAll)
344 // If a specific type was requested, we can stop now.
352 /// C# allows this kind of scenarios:
353 /// interface I { void M (); }
354 /// class X { public void M (); }
355 /// class Y : X, I { }
357 /// For that case, we create an explicit implementation function
360 void DefineProxy (Type iface, MethodInfo parent_method, MethodInfo iface_method,
365 string proxy_name = iface.Name + "." + iface_method.Name;
367 proxy = container.TypeBuilder.DefineMethod (
369 MethodAttributes.HideBySig |
370 MethodAttributes.NewSlot |
371 MethodAttributes.Virtual,
372 CallingConventions.Standard | CallingConventions.HasThis,
373 parent_method.ReturnType, args);
375 int top = args.Length;
376 ILGenerator ig = proxy.GetILGenerator ();
378 ig.Emit (OpCodes.Ldarg_0);
379 for (int i = 0; i < top; i++){
382 ig.Emit (OpCodes.Ldarg_1); break;
384 ig.Emit (OpCodes.Ldarg_2); break;
386 ig.Emit (OpCodes.Ldarg_3); break;
388 ig.Emit (OpCodes.Ldarg, i - 1); break;
391 ig.Emit (OpCodes.Call, parent_method);
392 ig.Emit (OpCodes.Ret);
394 container.TypeBuilder.DefineMethodOverride (proxy, iface_method);
398 /// This function tells whether one of our parent classes implements
399 /// the given method (which turns out, it is valid to have an interface
400 /// implementation in a parent
402 bool ParentImplements (Type iface_type, MethodInfo mi)
406 Type [] args = TypeManager.GetArgumentTypes (mi);
407 ms = new MethodSignature (mi.Name, mi.ReturnType, args);
408 MemberInfo [] list = TypeContainer.FindMembers (
409 container.TypeBuilder.BaseType, MemberTypes.Method | MemberTypes.Property,
410 BindingFlags.Public | BindingFlags.Instance,
411 MethodSignature.method_signature_filter, ms);
413 if (list == null || list.Length == 0)
416 DefineProxy (iface_type, (MethodInfo) list [0], mi, args);
421 /// Verifies that any pending abstract methods or interface methods
422 /// were implemented.
424 public bool VerifyPendingMethods ()
426 int top = pending_implementations.Length;
430 for (i = 0; i < top; i++){
431 Type type = pending_implementations [i].type;
434 foreach (MethodInfo mi in pending_implementations [i].methods){
438 if (type.IsInterface){
439 if (ParentImplements (type, mi))
444 if (pending_implementations [i].found [j])
445 extra = ". (method might be private or static)";
447 536, container.Location,
448 "`" + container.Name + "' does not implement " +
449 "interface member `" +
450 type.FullName + "." + mi.Name + "'" + extra);
453 534, container.Location,
454 "`" + container.Name + "' does not implement " +
455 "inherited abstract member `" +
456 type.FullName + "." + mi.Name + "'");