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 // The name of the indexer (if it exists), precompute set/get, because
51 // they would be recomputed many times inside a loop later on.
53 public string set_indexer_name;
54 public string get_indexer_name;
57 public class PendingImplementation {
59 /// The container for this PendingImplementation
61 TypeContainer container;
64 /// This filter is used by FindMembers, and it is used to
65 /// extract only virtual/abstract fields
67 static MemberFilter virtual_method_filter;
70 /// This is the array of TypeAndMethods that describes the pending implementations
71 /// (both interfaces and abstract methods in parent class)
73 TypeAndMethods [] pending_implementations;
75 static bool IsVirtualFilter (MemberInfo m, object filterCriteria)
77 MethodInfo mi = m as MethodInfo;
78 return (mi == null) ? false : mi.IsVirtual;
82 /// Inits the virtual_method_filter
84 static PendingImplementation ()
86 virtual_method_filter = new MemberFilter (IsVirtualFilter);
90 // Returns a list of the abstract methods that are exposed by all of our
91 // parents that we must implement. Notice that this `flattens' the
92 // method search space, and takes into account overrides.
94 static ArrayList GetAbstractMethods (Type t)
96 ArrayList list = null;
97 bool searching = true;
98 Type current_type = t;
103 mi = TypeContainer.FindMembers (
104 current_type, MemberTypes.Method,
105 BindingFlags.Public | BindingFlags.Instance |
106 BindingFlags.DeclaredOnly,
107 virtual_method_filter, null);
109 if (current_type == TypeManager.object_type)
112 current_type = current_type.BaseType;
113 if (!current_type.IsAbstract)
120 if (mi.Count == 1 && !(mi [0] is MethodBase))
123 list = TypeManager.CopyNewMethods (list, mi);
129 for (int i = 0; i < list.Count; i++){
130 while (list.Count > i && !((MethodInfo) list [i]).IsAbstract)
140 PendingImplementation (TypeContainer container, MissingInterfacesInfo [] missing_ifaces, ArrayList abstract_methods, int total)
142 TypeBuilder type_builder = container.TypeBuilder;
144 this.container = container;
145 pending_implementations = new TypeAndMethods [total];
148 foreach (MissingInterfacesInfo missing in missing_ifaces){
150 Type t = missing.Type;
152 if (t is TypeBuilder){
155 iface = TypeManager.LookupInterface (t);
157 mi = iface.GetMethods ();
159 mi = t.GetMethods ();
161 int count = mi.Length;
162 pending_implementations [i].type = t;
163 pending_implementations [i].optional = missing.Optional;
164 pending_implementations [i].methods = mi;
165 pending_implementations [i].args = new Type [count][];
166 pending_implementations [i].found = new bool [count];
167 pending_implementations [i].need_proxy = new MethodInfo [count];
168 string indexer_name = TypeManager.IndexerPropertyName (t);
170 pending_implementations [i].set_indexer_name = "set_" + indexer_name;
171 pending_implementations [i].get_indexer_name = "get_" + indexer_name;
174 foreach (MethodInfo m in mi){
175 Type [] types = TypeManager.GetArgumentTypes (m);
177 pending_implementations [i].args [j] = types;
183 if (abstract_methods != null){
184 int count = abstract_methods.Count;
185 pending_implementations [i].methods = new MethodInfo [count];
186 pending_implementations [i].need_proxy = new MethodInfo [count];
188 abstract_methods.CopyTo (pending_implementations [i].methods, 0);
189 pending_implementations [i].found = new bool [count];
190 pending_implementations [i].args = new Type [count][];
191 pending_implementations [i].type = type_builder;
193 string indexer_name = TypeManager.IndexerPropertyName (type_builder);
194 pending_implementations [i].set_indexer_name = "set_" + indexer_name;
195 pending_implementations [i].get_indexer_name = "get_" + indexer_name;
198 foreach (MemberInfo m in abstract_methods){
199 MethodInfo mi = (MethodInfo) m;
201 Type [] types = TypeManager.GetArgumentTypes (mi);
203 pending_implementations [i].args [j] = types;
209 struct MissingInterfacesInfo {
211 public bool Optional;
213 public MissingInterfacesInfo (Type t)
220 static MissingInterfacesInfo [] EmptyMissingInterfacesInfo = new MissingInterfacesInfo [0];
222 static MissingInterfacesInfo [] GetMissingInterfaces (TypeBuilder type_builder)
225 // Notice that TypeBuilders will only return the interfaces that the Type
226 // is supposed to implement, not all the interfaces that the type implements.
228 // Even better -- on MS it returns an empty array, no matter what.
230 // Completely broken. So we do it ourselves!
232 Type [] impl = TypeManager.GetExplicitInterfaces (type_builder);
234 if (impl == null || impl.Length == 0)
235 return EmptyMissingInterfacesInfo;
237 MissingInterfacesInfo [] ret = new MissingInterfacesInfo [impl.Length];
239 for (int i = 0; i < impl.Length; i++)
240 ret [i] = new MissingInterfacesInfo (impl [i]);
242 // we really should not get here because Object doesnt implement any
243 // interfaces. But it could implement something internal, so we have
244 // to handle that case.
245 if (type_builder.BaseType == null)
248 Type [] parent_impls = TypeManager.GetInterfaces (type_builder.BaseType);
250 foreach (Type t in parent_impls) {
251 for (int i = 0; i < ret.Length; i ++) {
252 if (t == ret [i].Type) {
253 ret [i].Optional = true;
262 // Factory method: if there are pending implementation methods, we return a PendingImplementation
263 // object, otherwise we return null.
265 // Register method implementations are either abstract methods
266 // flagged as such on the base class or interface methods
268 static public PendingImplementation GetPendingImplementations (TypeContainer container)
270 TypeBuilder type_builder = container.TypeBuilder;
271 MissingInterfacesInfo [] missing_interfaces;
272 Type b = type_builder.BaseType;
274 missing_interfaces = GetMissingInterfaces (type_builder);
277 // If we are implementing an abstract class, and we are not
278 // ourselves abstract, and there are abstract methods (C# allows
279 // abstract classes that have no abstract methods), then allocate
282 // We also pre-compute the methods.
284 bool implementing_abstract = ((b != null) && b.IsAbstract && !type_builder.IsAbstract);
285 ArrayList abstract_methods = null;
287 if (implementing_abstract){
288 abstract_methods = GetAbstractMethods (b);
290 if (abstract_methods == null)
291 implementing_abstract = false;
294 int total = missing_interfaces.Length + (implementing_abstract ? 1 : 0);
298 return new PendingImplementation (container, missing_interfaces, abstract_methods, total);
301 public enum Operation {
303 // If you change this, review the whole InterfaceMethod routine as there
304 // are a couple of assumptions on these three states
306 Lookup, ClearOne, ClearAll
310 /// Whether the specified method is an interface method implementation
312 public MethodInfo IsInterfaceMethod (Type t, string name, Type ret_type, Type [] args)
314 return InterfaceMethod (t, name, ret_type, args, Operation.Lookup, null);
317 public MethodInfo IsInterfaceIndexer (Type t, Type ret_type, Type [] args)
319 return InterfaceMethod (t, null, ret_type, args, Operation.Lookup, null);
322 public void ImplementMethod (Type t, string name, Type ret_type, Type [] args, bool clear_one)
324 InterfaceMethod (t, name, ret_type, args,
325 clear_one ? Operation.ClearOne : Operation.ClearAll, null);
328 public void ImplementIndexer (Type t, MethodInfo mi, Type ret_type, Type [] args, bool clear_one)
330 InterfaceMethod (t, null, ret_type, args,
331 clear_one ? Operation.ClearOne : Operation.ClearAll, mi);
335 /// If a method in Type `t' (or null to look in all interfaces
336 /// and the base abstract class) with name `Name', return type `ret_type' and
337 /// arguments `args' implements an interface, this method will
338 /// return the MethodInfo that this method implements.
340 /// If `name' is null, we operate solely on the method's signature. This is for
341 /// instance used when implementing indexers.
343 /// The `Operation op' controls whether to lookup, clear the pending bit, or clear
344 /// all the methods with the given signature.
346 /// The `MethodInfo need_proxy' is used when we're implementing an interface's
347 /// indexer in a class. If the new indexer's IndexerName does not match the one
348 /// that was used in the interface, then we always need to create a proxy for it.
351 public MethodInfo InterfaceMethod (Type t, string name, Type ret_type, Type [] args,
352 Operation op, MethodInfo need_proxy)
354 int arg_len = args.Length;
356 if (pending_implementations == null)
359 foreach (TypeAndMethods tm in pending_implementations){
360 if (!(t == null || tm.type == t))
363 int method_count = tm.methods.Length;
365 for (int i = 0; i < method_count; i++){
372 // `need_proxy' is not null when we're implementing an
373 // interface indexer and this is Clear(One/All) operation.
375 // If `name' is null, then we do a match solely based on the
376 // signature and not on the name (this is done in the Lookup
377 // for an interface indexer).
380 if (m.Name != tm.get_indexer_name && m.Name != tm.set_indexer_name)
382 } else if ((need_proxy == null) && (name != m.Name))
385 if (!TypeManager.IsEqual (ret_type, m.ReturnType)){
386 if (!((ret_type == null && m.ReturnType == TypeManager.void_type) ||
387 (m.ReturnType == null && ret_type == TypeManager.void_type)))
392 // Check if we have the same parameters
394 if (tm.args [i].Length != arg_len)
397 int j, top = args.Length;
400 for (j = 0; j < top; j++){
401 if (!TypeManager.IsEqual (tm.args [i][j], args[j])){
409 if (op != Operation.Lookup){
410 // If `t != null', then this is an explicitly interface
411 // implementation and we can always clear the method.
412 // `need_proxy' is not null if we're implementing an
413 // interface indexer. In this case, we need to create
414 // a proxy if the implementation's IndexerName doesn't
415 // match the IndexerName in the interface.
416 bool name_matches = false;
417 if (name == m.Name || m.Name == tm.get_indexer_name || m.Name == tm.set_indexer_name)
420 if ((t == null) && (need_proxy != null) && !name_matches)
421 tm.need_proxy [i] = need_proxy;
423 tm.methods [i] = null;
428 // Lookups and ClearOne return
430 if (op != Operation.ClearAll)
434 // If a specific type was requested, we can stop now.
442 /// C# allows this kind of scenarios:
443 /// interface I { void M (); }
444 /// class X { public void M (); }
445 /// class Y : X, I { }
447 /// For that case, we create an explicit implementation function
450 void DefineProxy (Type iface, MethodInfo parent_method, MethodInfo iface_method,
455 string proxy_name = iface.Name + "." + iface_method.Name;
457 proxy = container.TypeBuilder.DefineMethod (
459 MethodAttributes.HideBySig |
460 MethodAttributes.NewSlot |
461 MethodAttributes.Virtual,
462 CallingConventions.Standard | CallingConventions.HasThis,
463 parent_method.ReturnType, args);
465 int top = args.Length;
466 ILGenerator ig = proxy.GetILGenerator ();
468 ig.Emit (OpCodes.Ldarg_0);
469 for (int i = 0; i < top; i++){
472 ig.Emit (OpCodes.Ldarg_1); break;
474 ig.Emit (OpCodes.Ldarg_2); break;
476 ig.Emit (OpCodes.Ldarg_3); break;
478 ig.Emit (OpCodes.Ldarg, i - 1); break;
481 ig.Emit (OpCodes.Call, parent_method);
482 ig.Emit (OpCodes.Ret);
484 container.TypeBuilder.DefineMethodOverride (proxy, iface_method);
488 /// This function tells whether one of our parent classes implements
489 /// the given method (which turns out, it is valid to have an interface
490 /// implementation in a parent
492 bool ParentImplements (Type iface_type, MethodInfo mi)
496 Type [] args = TypeManager.GetArgumentTypes (mi);
497 ms = new MethodSignature (mi.Name, mi.ReturnType, args);
498 MemberList list = TypeContainer.FindMembers (
499 container.TypeBuilder.BaseType, MemberTypes.Method | MemberTypes.Property,
500 BindingFlags.Public | BindingFlags.Instance,
501 MethodSignature.method_signature_filter, ms);
506 MethodInfo parent = (MethodInfo) list [0];
507 if (!parent.IsAbstract)
508 DefineProxy (iface_type, parent, mi, args);
513 /// Verifies that any pending abstract methods or interface methods
514 /// were implemented.
516 public bool VerifyPendingMethods ()
518 int top = pending_implementations.Length;
522 for (i = 0; i < top; i++){
523 Type type = pending_implementations [i].type;
526 foreach (MethodInfo mi in pending_implementations [i].methods){
530 if (type.IsInterface){
531 MethodInfo need_proxy =
532 pending_implementations [i].need_proxy [j];
534 if (need_proxy != null) {
535 Type [] args = TypeManager.GetArgumentTypes (mi);
536 DefineProxy (type, need_proxy, mi, args);
540 if (ParentImplements (type, mi))
543 if (pending_implementations [i].optional)
546 if (pending_implementations [i].found [j]) {
547 string[] methodLabel = TypeManager.CSharpSignature (mi).Split ('.');
548 Report.Error (536, container.Location, "'{0}' does not implement interface member '{1}'. '{2}.{3}' is either static, not public, or has the wrong return type",
549 container.Name, TypeManager.CSharpSignature (mi), container.Name, methodLabel[methodLabel.Length - 1]);
552 Report.Error (535, container.Location, "'{0}' does not implement interface member '{1}'",
553 container.Name, TypeManager.CSharpSignature (mi));
556 Report.Error (534, container.Location, "'{0}' does not implement inherited abstract member '{1}'",
557 container.Name, TypeManager.CSharpSignature (mi));