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