2002-08-06 Rafael Teixeira <rafaelteixeirabr@hotmail.com>
[mono.git] / mcs / mbas / pending.cs
1 //
2 // pending.cs: Pending method implementation
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@gnu.org)
6 //
7 // Licensed under the terms of the GNU GPL
8 //
9 // (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
10 //
11 //
12
13 using System;
14 using System.Collections;
15 using System.Reflection;
16 using System.Reflection.Emit;
17
18 namespace Mono.CSharp {
19
20         struct TypeAndMethods {
21                 public Type          type;
22                 public MethodInfo [] methods;
23                 
24                 // Far from ideal, but we want to avoid creating a copy
25                 // of methods above.
26                 public Type [][]     args;
27                 
28                 //
29                 // This flag on the method says `We found a match, but
30                 // because it was private, we could not use the match
31                 //
32                 public bool []       found;
33
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;
38         }
39
40         public class PendingImplementation {
41                 /// <summary>
42                 ///   The container for this PendingImplementation
43                 /// </summary>
44                 TypeContainer container;
45                 
46                 /// <summary>
47                 ///   This filter is used by FindMembers, and it is used to
48                 ///   extract only virtual/abstract fields
49                 /// </summary>
50                 static MemberFilter virtual_method_filter;
51
52                 /// <summary>
53                 ///   This is the array of TypeAndMethods that describes the pending implementations
54                 ///   (both interfaces and abstract methods in parent class)
55                 /// </summary>
56                 TypeAndMethods [] pending_implementations;
57
58                 static bool IsVirtualFilter (MemberInfo m, object filterCriteria)
59                 {
60                         if (!(m is MethodInfo))
61                                 return false;
62
63                         return ((MethodInfo) m).IsVirtual;
64                 }
65
66                 /// <summary>
67                 ///   Inits the virtual_method_filter
68                 /// </summary>
69                 static PendingImplementation ()
70                 {
71                         virtual_method_filter = new MemberFilter (IsVirtualFilter);
72                 }
73
74                 // <remarks>
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.  
78                 // </remarks>
79                 static ArrayList GetAbstractMethods (Type t)
80                 {
81                         ArrayList list = null;
82                         bool searching = true;
83                         Type current_type = t;
84                         
85                         do {
86                                 MemberInfo [] mi;
87                                 
88                                 mi = TypeContainer.FindMembers (
89                                         current_type, MemberTypes.Method,
90                                         BindingFlags.Public | BindingFlags.Instance |
91                                         BindingFlags.DeclaredOnly,
92                                         virtual_method_filter, null);
93
94                                 if (current_type == TypeManager.object_type)
95                                         searching = false;
96                                 else {
97                                         current_type = current_type.BaseType;
98                                         if (!current_type.IsAbstract)
99                                                 searching = false;
100                                 }
101
102                                 if (mi == null)
103                                         continue;
104
105                                 int count = mi.Length;
106                                 if (count == 0)
107                                         continue;
108
109                                 if (count == 1 && !(mi [0] is MethodBase))
110                                         searching = false;
111                                 else 
112                                         list = TypeManager.CopyNewMethods (list, mi);
113                         } while (searching);
114
115                         if (list == null)
116                                 return null;
117                         
118                         for (int i = 0; i < list.Count; i++){
119                                 while (list.Count > i && !((MethodInfo) list [i]).IsAbstract)
120                                         list.RemoveAt (i);
121                         }
122
123                         if (list.Count == 0)
124                                 return null;
125
126                         return list;
127                 }
128
129                 PendingImplementation (TypeContainer container, Type [] ifaces, ArrayList abstract_methods, int total)
130                 {
131                         TypeBuilder type_builder = container.TypeBuilder;
132                         
133                         this.container = container;
134                         pending_implementations = new TypeAndMethods [total];
135
136                         int i = 0;
137                         if (ifaces != null){
138                                 foreach (Type t in ifaces){
139                                         MethodInfo [] mi;
140
141                                         if (t is TypeBuilder){
142                                                 Interface iface;
143
144                                                 iface = TypeManager.LookupInterface (t);
145                                                 
146                                                 mi = iface.GetMethods ();
147                                         } else
148                                                 mi = t.GetMethods ();
149
150                                         int count = mi.Length;
151                                         pending_implementations [i].type = t;
152                                         pending_implementations [i].methods = mi;
153                                         pending_implementations [i].args = new Type [count][];
154                                         pending_implementations [i].found = new bool [count];
155                                         pending_implementations [i].need_proxy = new MethodInfo [count];
156
157                                         int j = 0;
158                                         foreach (MethodInfo m in mi){
159                                                 Type [] types = TypeManager.GetArgumentTypes (m);
160
161                                                 pending_implementations [i].args [j] = types;
162                                                 j++;
163                                         }
164                                         i++;
165                                 }
166                         }
167
168                         if (abstract_methods != null){
169                                 int count = abstract_methods.Count;
170                                 pending_implementations [i].methods = new MethodInfo [count];
171                                 pending_implementations [i].need_proxy = new MethodInfo [count];
172                                 
173                                 abstract_methods.CopyTo (pending_implementations [i].methods, 0);
174                                 pending_implementations [i].found = new bool [count];
175                                 pending_implementations [i].args = new Type [count][];
176                                 pending_implementations [i].type = type_builder;
177                                 
178                                 int j = 0;
179                                 foreach (MemberInfo m in abstract_methods){
180                                         MethodInfo mi = (MethodInfo) m;
181                                         
182                                         Type [] types = TypeManager.GetArgumentTypes (mi);
183                                         
184                                         pending_implementations [i].args [j] = types;
185                                         j++;
186                                 }
187                         }
188                 }
189                 
190                 //
191                 // Factory method: if there are pending implementation methods, we return a PendingImplementation
192                 // object, otherwise we return null.
193                 //
194                 // Register method implementations are either abstract methods
195                 // flagged as such on the base class or interface methods
196                 //
197                 static public PendingImplementation GetPendingImplementations (TypeContainer container)
198                 {
199                         TypeBuilder type_builder = container.TypeBuilder;
200                         Type [] ifaces;
201                         Type b = type_builder.BaseType;
202                         int icount = 0;
203
204                         //
205                         // Notice that TypeBuilders will only return the interfaces that the Type
206                         // is supposed to implement, not all the interfaces that the type implements.
207                         //
208                         // Completely broken.  Anyways, we take advantage of this, so we only register
209                         // the implementations that we need, as they are those that are listed by the
210                         // TypeBuilder.
211                         //
212                         ifaces = type_builder.GetInterfaces ();
213 #if DEBUG
214                         {
215                                 Type x = type_builder;
216
217                                 while (x != null){
218                                         Type [] iff = x.GetInterfaces ();
219                                         Console.WriteLine ("Type: " + x.Name);
220                                         
221                                         foreach (Type tt in iff){
222                                                 Console.WriteLine ("  Iface: " + tt.Name);
223                                         }
224                                         x = x.BaseType;
225                                 }
226                         }
227 #endif
228                                         
229                         icount = ifaces.Length;
230
231                         //
232                         // If we are implementing an abstract class, and we are not
233                         // ourselves abstract, and there are abstract methods (C# allows
234                         // abstract classes that have no abstract methods), then allocate
235                         // one slot.
236                         //
237                         // We also pre-compute the methods.
238                         //
239                         bool implementing_abstract = ((b != null) && b.IsAbstract && !type_builder.IsAbstract);
240                         ArrayList abstract_methods = null;
241
242                         if (implementing_abstract){
243                                 abstract_methods = GetAbstractMethods (b);
244                                 
245                                 if (abstract_methods == null)
246                                         implementing_abstract = false;
247                         }
248                         
249                         int total = icount +  (implementing_abstract ? 1 : 0);
250                         if (total == 0)
251                                 return null;
252
253                         return new PendingImplementation (container, ifaces, abstract_methods, total);
254                 }
255
256                 public enum Operation {
257                         //
258                         // If you change this, review the whole InterfaceMethod routine as there
259                         // are a couple of assumptions on these three states
260                         //
261                         Lookup, ClearOne, ClearAll
262                 }
263
264                 /// <summary>
265                 ///   Whether the specified method is an interface method implementation
266                 /// </summary>
267                 public MethodInfo IsInterfaceMethod (Type t, string name, Type ret_type, Type [] args)
268                 {
269                         return InterfaceMethod (t, name, ret_type, args, Operation.Lookup, null);
270                 }
271
272                 public MethodInfo IsInterfaceIndexer (Type t, Type ret_type, Type [] args)
273                 {
274                         return InterfaceMethod (t, null, ret_type, args, Operation.Lookup, null);
275                 }
276
277                 public void ImplementMethod (Type t, string name, Type ret_type, Type [] args, bool clear_one) 
278                 {
279                         InterfaceMethod (t, name, ret_type, args,
280                                          clear_one ? Operation.ClearOne : Operation.ClearAll, null);
281                 }
282
283                 public void ImplementIndexer (Type t, MethodInfo mi, Type ret_type, Type [] args, bool clear_one) 
284                 {
285                         InterfaceMethod (t, mi.Name, ret_type, args,
286                                          clear_one ? Operation.ClearOne : Operation.ClearAll, mi);
287                 }
288                 
289                 /// <remarks>
290                 ///   If a method in Type `t' (or null to look in all interfaces
291                 ///   and the base abstract class) with name `Name', return type `ret_type' and
292                 ///   arguments `args' implements an interface, this method will
293                 ///   return the MethodInfo that this method implements.
294                 ///
295                 ///   If `name' is null, we operate solely on the method's signature.  This is for
296                 ///   instance used when implementing indexers.
297                 ///
298                 ///   The `Operation op' controls whether to lookup, clear the pending bit, or clear
299                 ///   all the methods with the given signature.
300                 ///
301                 ///   The `MethodInfo need_proxy' is used when we're implementing an interface's
302                 ///   indexer in a class.  If the new indexer's IndexerName does not match the one
303                 ///   that was used in the interface, then we always need to create a proxy for it.
304                 ///
305                 /// </remarks>
306                 public MethodInfo InterfaceMethod (Type t, string name, Type ret_type, Type [] args,
307                                                    Operation op, MethodInfo need_proxy)
308                 {
309                         int arg_len = args.Length;
310
311                         if (pending_implementations == null)
312                                 return null;
313
314                         foreach (TypeAndMethods tm in pending_implementations){
315                                 if (!(t == null || tm.type == t))
316                                         continue;
317
318                                 int i = 0;
319                                 foreach (MethodInfo m in tm.methods){
320                                         if (m == null){
321                                                 i++;
322                                                 continue;
323                                         }
324
325                                         // `need_proxy' is not null when we're implementing an
326                                         // interface indexer and this is Clear(One/All) operation.
327                                         // If `name' is null, then we do a match solely based on the
328                                         // signature and not on the name (this is done in the Lookup
329                                         // for an interface indexer).
330                                         if ((name != null) && (need_proxy == null) && (name != m.Name)){
331                                                 i++;
332                                                 continue;
333                                         }
334
335                                         if (ret_type != m.ReturnType){
336                                                 if (!((ret_type == null && m.ReturnType == TypeManager.void_type) ||
337                                                       (m.ReturnType == null && ret_type == TypeManager.void_type)))
338                                                 {
339                                                         i++;
340                                                         continue;
341                                                 }
342                                         }
343
344                                         //
345                                         // Check if we have the same parameters
346                                         //
347                                         if (tm.args [i].Length != arg_len){
348                                                 i++;
349                                                 continue;
350                                         }
351
352                                         int j, top = args.Length;
353                                         bool fail = false;
354                                         
355                                         for (j = 0; j < top; j++){
356                                                 if (tm.args [i][j] != args[j]){
357                                                         fail = true;
358                                                         break;
359                                                 }
360                                         }
361                                         if (fail){
362                                                 i++;
363                                                 continue;
364                                         }
365
366                                         if (op != Operation.Lookup){
367                                                 // If `t != null', then this is an explicitly interface
368                                                 // implementation and we can always clear the method.
369                                                 // `need_proxy' is not null if we're implementing an
370                                                 // interface indexer.  In this case, we need to create
371                                                 // a proxy if the implementation's IndexerName doesn't
372                                                 // match the IndexerName in the interface.
373                                                 if ((t == null) && (need_proxy != null) && (name != m.Name))
374                                                         tm.need_proxy [i] = need_proxy;
375                                                 else
376                                                         tm.methods [i] = null;
377                                         }
378                                         tm.found [i] = true;
379
380                                         //
381                                         // Lookups and ClearOne return
382                                         //
383                                         if (op != Operation.ClearAll)
384                                                 return m;
385                                 }
386
387                                 // If a specific type was requested, we can stop now.
388                                 if (tm.type == t)
389                                         return null;
390                         }
391                         return null;
392                 }
393
394                 /// <summary>
395                 ///   C# allows this kind of scenarios:
396                 ///   interface I { void M (); }
397                 ///   class X { public void M (); }
398                 ///   class Y : X, I { }
399                 ///
400                 ///   For that case, we create an explicit implementation function
401                 ///   I.M in Y.
402                 /// </summary>
403                 void DefineProxy (Type iface, MethodInfo parent_method, MethodInfo iface_method,
404                                   Type [] args)
405                 {
406                         MethodBuilder proxy;
407
408                         string proxy_name = iface.Name + "." + iface_method.Name;
409
410                         proxy = container.TypeBuilder.DefineMethod (
411                                 proxy_name,
412                                 MethodAttributes.HideBySig |
413                                 MethodAttributes.NewSlot |
414                                 MethodAttributes.Virtual,
415                                 CallingConventions.Standard | CallingConventions.HasThis,
416                                 parent_method.ReturnType, args);
417
418                         int top = args.Length;
419                         ILGenerator ig = proxy.GetILGenerator ();
420
421                         ig.Emit (OpCodes.Ldarg_0);
422                         for (int i = 0; i < top; i++){
423                                 switch (i){
424                                 case 0:
425                                         ig.Emit (OpCodes.Ldarg_1); break;
426                                 case 1:
427                                         ig.Emit (OpCodes.Ldarg_2); break;
428                                 case 2:
429                                         ig.Emit (OpCodes.Ldarg_3); break;
430                                 default:
431                                         ig.Emit (OpCodes.Ldarg, i - 1); break;
432                                 }
433                         }
434                         ig.Emit (OpCodes.Call, parent_method);
435                         ig.Emit (OpCodes.Ret);
436
437                         container.TypeBuilder.DefineMethodOverride (proxy, iface_method);
438                 }
439                 
440                 /// <summary>
441                 ///   This function tells whether one of our parent classes implements
442                 ///   the given method (which turns out, it is valid to have an interface
443                 ///   implementation in a parent
444                 /// </summary>
445                 bool ParentImplements (Type iface_type, MethodInfo mi)
446                 {
447                         MethodSignature ms;
448                         
449                         Type [] args = TypeManager.GetArgumentTypes (mi);
450                         ms = new MethodSignature (mi.Name, mi.ReturnType, args);
451                         MemberInfo [] list = TypeContainer.FindMembers (
452                                 container.TypeBuilder.BaseType, MemberTypes.Method | MemberTypes.Property,
453                                 BindingFlags.Public | BindingFlags.Instance,
454                                 MethodSignature.method_signature_filter, ms);
455
456                         if (list == null || list.Length == 0)
457                                 return false;
458
459                         DefineProxy (iface_type, (MethodInfo) list [0], mi, args);
460                         return true;
461                 }
462
463                 /// <summary>
464                 ///   Verifies that any pending abstract methods or interface methods
465                 ///   were implemented.
466                 /// </summary>
467                 public bool VerifyPendingMethods ()
468                 {
469                         int top = pending_implementations.Length;
470                         bool errors = false;
471                         int i;
472                         
473                         for (i = 0; i < top; i++){
474                                 Type type = pending_implementations [i].type;
475                                 int j = 0;
476                                 
477                                 foreach (MethodInfo mi in pending_implementations [i].methods){
478                                         if (mi == null)
479                                                 continue;
480
481                                         if (type.IsInterface){
482                                                 MethodInfo need_proxy =
483                                                         pending_implementations [i].need_proxy [j];
484
485                                                 if (need_proxy != null) {
486                                                         Type [] args = TypeManager.GetArgumentTypes (mi);
487                                                         DefineProxy (type, need_proxy, mi, args);
488                                                         continue;
489                                                 }
490
491                                                 if (ParentImplements (type, mi))
492                                                         continue;
493
494                                                 string extra = "";
495                                                 
496                                                 if (pending_implementations [i].found [j])
497                                                         extra = ".  (method might be private or static)";
498                                                 Report.Error (
499                                                         536, container.Location,
500                                                         "`" + container.Name + "' does not implement " +
501                                                         "interface member `" +
502                                                         type.FullName + "." + mi.Name + "'" + extra);
503                                         } else {
504                                                 Report.Error (
505                                                         534, container.Location,
506                                                         "`" + container.Name + "' does not implement " +
507                                                         "inherited abstract member `" +
508                                                         type.FullName + "." + mi.Name + "'");
509                                         }
510                                         errors = true;
511                                         j++;
512                                 }
513                         }
514                         return errors;
515                 }
516         } /* end of class */
517 }