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;
25 // Whether it is optional, this is used to allow the explicit/implicit
26 // implementation when a parent class already implements an interface.
30 // class X : IA { } class Y : X, IA { IA.Explicit (); }
34 // Far from ideal, but we want to avoid creating a copy
36 public Type [][] args;
39 // This flag on the method says `We found a match, but
40 // because it was private, we could not use the match
44 // If a method is defined here, then we always need to
45 // create a proxy for it. This is used when implementing
46 // an interface's indexer with a different IndexerName.
47 public MethodInfo [] need_proxy;
50 public class PendingImplementation {
52 /// The container for this PendingImplementation
54 TypeContainer container;
57 /// This filter is used by FindMembers, and it is used to
58 /// extract only virtual/abstract fields
60 static MemberFilter virtual_method_filter;
63 /// This is the array of TypeAndMethods that describes the pending implementations
64 /// (both interfaces and abstract methods in parent class)
66 TypeAndMethods [] pending_implementations;
68 static bool IsVirtualFilter (MemberInfo m, object filterCriteria)
70 if (!(m is MethodInfo))
73 return ((MethodInfo) m).IsVirtual;
77 /// Inits the virtual_method_filter
79 static PendingImplementation ()
81 virtual_method_filter = new MemberFilter (IsVirtualFilter);
85 // Returns a list of the abstract methods that are exposed by all of our
86 // parents that we must implement. Notice that this `flattens' the
87 // method search space, and takes into account overrides.
89 static ArrayList GetAbstractMethods (Type t)
91 ArrayList list = null;
92 bool searching = true;
93 Type current_type = t;
98 mi = TypeContainer.FindMembers (
99 current_type, MemberTypes.Method,
100 BindingFlags.Public | BindingFlags.Instance |
101 BindingFlags.DeclaredOnly,
102 virtual_method_filter, null);
104 if (current_type == TypeManager.object_type)
107 current_type = current_type.BaseType;
108 if (!current_type.IsAbstract)
115 if (mi.Count == 1 && !(mi [0] is MethodBase))
118 list = TypeManager.CopyNewMethods (list, mi);
124 for (int i = 0; i < list.Count; i++){
125 while (list.Count > i && !((MethodInfo) list [i]).IsAbstract)
135 PendingImplementation (TypeContainer container, MissingInterfacesInfo [] missing_ifaces, ArrayList abstract_methods, int total)
137 TypeBuilder type_builder = container.TypeBuilder;
139 this.container = container;
140 pending_implementations = new TypeAndMethods [total];
143 foreach (MissingInterfacesInfo missing in missing_ifaces){
145 Type t = missing.Type;
147 if (t is TypeBuilder){
150 iface = TypeManager.LookupInterface (t);
152 mi = iface.GetMethods (container);
154 mi = t.GetMethods ();
156 int count = mi.Length;
157 pending_implementations [i].type = missing.Type;
158 pending_implementations [i].optional = missing.Optional;
159 pending_implementations [i].methods = mi;
160 pending_implementations [i].args = new Type [count][];
161 pending_implementations [i].found = new bool [count];
162 pending_implementations [i].need_proxy = new MethodInfo [count];
165 foreach (MethodInfo m in mi){
166 Type [] types = TypeManager.GetArgumentTypes (m);
168 pending_implementations [i].args [j] = types;
174 if (abstract_methods != null){
175 int count = abstract_methods.Count;
176 pending_implementations [i].methods = new MethodInfo [count];
177 pending_implementations [i].need_proxy = new MethodInfo [count];
179 abstract_methods.CopyTo (pending_implementations [i].methods, 0);
180 pending_implementations [i].found = new bool [count];
181 pending_implementations [i].args = new Type [count][];
182 pending_implementations [i].type = type_builder;
185 foreach (MemberInfo m in abstract_methods){
186 MethodInfo mi = (MethodInfo) m;
188 Type [] types = TypeManager.GetArgumentTypes (mi);
190 pending_implementations [i].args [j] = types;
196 struct MissingInterfacesInfo {
198 public bool Optional;
200 public MissingInterfacesInfo (Type t)
207 static MissingInterfacesInfo [] EmptyMissingInterfacesInfo = new MissingInterfacesInfo [0];
209 static MissingInterfacesInfo [] GetMissingInterfaces (TypeBuilder type_builder)
212 // Notice that TypeBuilders will only return the interfaces that the Type
213 // is supposed to implement, not all the interfaces that the type implements.
215 // Even better -- on MS it returns an empty array, no matter what.
217 // Completely broken. So we do it ourselves!
219 TypeExpr [] impl = TypeManager.GetExplicitInterfaces (type_builder);
221 if (impl == null || impl.Length == 0)
222 return EmptyMissingInterfacesInfo;
224 MissingInterfacesInfo [] ret = new MissingInterfacesInfo [impl.Length];
226 for (int i = 0; i < impl.Length; i++)
227 ret [i] = new MissingInterfacesInfo (impl [i].Type);
229 // we really should not get here because Object doesnt implement any
230 // interfaces. But it could implement something internal, so we have
231 // to handle that case.
232 if (type_builder.BaseType == null)
235 TypeExpr [] parent_impls = TypeManager.GetInterfaces (type_builder.BaseType);
237 foreach (TypeExpr te in parent_impls) {
240 for (int i = 0; i < ret.Length; i ++) {
241 if (t == ret [i].Type) {
242 ret [i].Optional = true;
251 // Factory method: if there are pending implementation methods, we return a PendingImplementation
252 // object, otherwise we return null.
254 // Register method implementations are either abstract methods
255 // flagged as such on the base class or interface methods
257 static public PendingImplementation GetPendingImplementations (TypeContainer container)
259 TypeBuilder type_builder = container.TypeBuilder;
260 MissingInterfacesInfo [] missing_interfaces;
261 Type b = type_builder.BaseType;
263 missing_interfaces = GetMissingInterfaces (type_builder);
266 // If we are implementing an abstract class, and we are not
267 // ourselves abstract, and there are abstract methods (C# allows
268 // abstract classes that have no abstract methods), then allocate
271 // We also pre-compute the methods.
273 bool implementing_abstract = ((b != null) && b.IsAbstract && !type_builder.IsAbstract);
274 ArrayList abstract_methods = null;
276 if (implementing_abstract){
277 abstract_methods = GetAbstractMethods (b);
279 if (abstract_methods == null)
280 implementing_abstract = false;
283 int total = missing_interfaces.Length + (implementing_abstract ? 1 : 0);
287 return new PendingImplementation (container, missing_interfaces, abstract_methods, total);
290 public enum Operation {
292 // If you change this, review the whole InterfaceMethod routine as there
293 // are a couple of assumptions on these three states
295 Lookup, ClearOne, ClearAll
299 /// Whether the specified method is an interface method implementation
301 public MethodInfo IsInterfaceMethod (Type t, string name, Type ret_type, Type [] args)
303 return InterfaceMethod (t, name, ret_type, args, Operation.Lookup, null);
306 public MethodInfo IsInterfaceIndexer (Type t, Type ret_type, Type [] args)
308 return InterfaceMethod (t, null, ret_type, args, Operation.Lookup, null);
311 public void ImplementMethod (Type t, string name, Type ret_type, Type [] args, bool clear_one)
313 InterfaceMethod (t, name, ret_type, args,
314 clear_one ? Operation.ClearOne : Operation.ClearAll, null);
317 public void ImplementIndexer (Type t, MethodInfo mi, Type ret_type, Type [] args, bool clear_one)
319 InterfaceMethod (t, mi.Name, ret_type, args,
320 clear_one ? Operation.ClearOne : Operation.ClearAll, mi);
324 /// If a method in Type `t' (or null to look in all interfaces
325 /// and the base abstract class) with name `Name', return type `ret_type' and
326 /// arguments `args' implements an interface, this method will
327 /// return the MethodInfo that this method implements.
329 /// If `name' is null, we operate solely on the method's signature. This is for
330 /// instance used when implementing indexers.
332 /// The `Operation op' controls whether to lookup, clear the pending bit, or clear
333 /// all the methods with the given signature.
335 /// The `MethodInfo need_proxy' is used when we're implementing an interface's
336 /// indexer in a class. If the new indexer's IndexerName does not match the one
337 /// that was used in the interface, then we always need to create a proxy for it.
340 public MethodInfo InterfaceMethod (Type t, string name, Type ret_type, Type [] args,
341 Operation op, MethodInfo need_proxy)
343 int arg_len = args.Length;
345 if (pending_implementations == null)
348 foreach (TypeAndMethods tm in pending_implementations){
349 if (!(t == null || tm.type == t))
352 int method_count = tm.methods.Length;
354 for (int i = 0; i < method_count; i++){
360 // `need_proxy' is not null when we're implementing an
361 // interface indexer and this is Clear(One/All) operation.
362 // If `name' is null, then we do a match solely based on the
363 // signature and not on the name (this is done in the Lookup
364 // for an interface indexer).
365 if ((name != null) && (need_proxy == null) && (name != m.Name))
368 if (ret_type != m.ReturnType){
369 if (!((ret_type == null && m.ReturnType == TypeManager.void_type) ||
370 (m.ReturnType == null && ret_type == TypeManager.void_type)))
375 // Check if we have the same parameters
377 if (tm.args [i].Length != arg_len)
380 int j, top = args.Length;
383 for (j = 0; j < top; j++){
384 if (tm.args [i][j] != args[j]){
392 if (op != Operation.Lookup){
393 // If `t != null', then this is an explicitly interface
394 // implementation and we can always clear the method.
395 // `need_proxy' is not null if we're implementing an
396 // interface indexer. In this case, we need to create
397 // a proxy if the implementation's IndexerName doesn't
398 // match the IndexerName in the interface.
399 if ((t == null) && (need_proxy != null) && (name != m.Name))
400 tm.need_proxy [i] = need_proxy;
402 tm.methods [i] = null;
407 // Lookups and ClearOne return
409 if (op != Operation.ClearAll)
413 // If a specific type was requested, we can stop now.
421 /// C# allows this kind of scenarios:
422 /// interface I { void M (); }
423 /// class X { public void M (); }
424 /// class Y : X, I { }
426 /// For that case, we create an explicit implementation function
429 void DefineProxy (Type iface, MethodInfo parent_method, MethodInfo iface_method,
434 string proxy_name = iface.Name + "." + iface_method.Name;
436 proxy = container.TypeBuilder.DefineMethod (
438 MethodAttributes.HideBySig |
439 MethodAttributes.NewSlot |
440 MethodAttributes.Virtual,
441 CallingConventions.Standard | CallingConventions.HasThis,
442 parent_method.ReturnType, args);
444 int top = args.Length;
445 ILGenerator ig = proxy.GetILGenerator ();
447 ig.Emit (OpCodes.Ldarg_0);
448 for (int i = 0; i < top; i++){
451 ig.Emit (OpCodes.Ldarg_1); break;
453 ig.Emit (OpCodes.Ldarg_2); break;
455 ig.Emit (OpCodes.Ldarg_3); break;
457 ig.Emit (OpCodes.Ldarg, i - 1); break;
460 ig.Emit (OpCodes.Call, parent_method);
461 ig.Emit (OpCodes.Ret);
463 container.TypeBuilder.DefineMethodOverride (proxy, iface_method);
467 /// This function tells whether one of our parent classes implements
468 /// the given method (which turns out, it is valid to have an interface
469 /// implementation in a parent
471 bool ParentImplements (Type iface_type, MethodInfo mi)
475 Type [] args = TypeManager.GetArgumentTypes (mi);
476 ms = new MethodSignature (mi.Name, mi.ReturnType, args);
477 MemberList list = TypeContainer.FindMembers (
478 container.TypeBuilder.BaseType, MemberTypes.Method | MemberTypes.Property,
479 BindingFlags.Public | BindingFlags.Instance,
480 MethodSignature.method_signature_filter, ms);
485 MethodInfo parent = (MethodInfo) list [0];
486 if (!parent.IsAbstract)
487 DefineProxy (iface_type, parent, mi, args);
492 /// Verifies that any pending abstract methods or interface methods
493 /// were implemented.
495 public bool VerifyPendingMethods ()
497 int top = pending_implementations.Length;
501 for (i = 0; i < top; i++){
502 Type type = pending_implementations [i].type;
505 foreach (MethodInfo mi in pending_implementations [i].methods){
509 if (type.IsInterface){
510 MethodInfo need_proxy =
511 pending_implementations [i].need_proxy [j];
513 if (need_proxy != null) {
514 Type [] args = TypeManager.GetArgumentTypes (mi);
515 DefineProxy (type, need_proxy, mi, args);
519 if (ParentImplements (type, mi))
522 if (pending_implementations [i].optional)
527 if (pending_implementations [i].found [j])
528 extra = ". (method might be non-public or static)";
530 536, container.Location,
531 "`" + container.Name + "' does not implement " +
532 "interface member `" +
533 type.FullName + "." + mi.Name + "'" + extra);
536 534, container.Location,
537 "`" + container.Name + "' does not implement " +
538 "inherited abstract member `" +
539 type.FullName + "." + mi.Name + "'");