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