2004-02-14 Francisco Figueiredo Jr. <fxjrlists@yahoo.com.br>
[mono.git] / mcs / gmcs / decl.cs
1 //
2 // decl.cs: Declaration base class for structs, classes, enums and interfaces.
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //
6 // Licensed under the terms of the GNU GPL
7 //
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
9 //
10 // TODO: Move the method verification stuff from the class.cs and interface.cs here
11 //
12
13 using System;
14 using System.Collections;
15 using System.Reflection.Emit;
16 using System.Reflection;
17
18 namespace Mono.CSharp {
19
20         /// <summary>
21         ///   Base representation for members.  This is only used to keep track
22         ///   of Name, Location and Modifier flags.
23         /// </summary>
24         public abstract class MemberCore {
25                 /// <summary>
26                 ///   Public name
27                 /// </summary>
28                 public string Name;
29
30                 /// <summary>
31                 ///   Modifier flags that the user specified in the source code
32                 /// </summary>
33                 public int ModFlags;
34
35                 /// <summary>
36                 ///   Location where this declaration happens
37                 /// </summary>
38                 public readonly Location Location;
39
40                 /// <summary>
41                 ///   Attributes for this type
42                 /// </summary>
43                 Attributes attributes;
44
45                 public MemberCore (string name, Attributes attrs, Location loc)
46                 {
47                         Name = name;
48                         Location = loc;
49                         attributes = attrs;
50                 }
51
52                 public abstract bool Define (TypeContainer parent);
53
54                 public Attributes OptAttributes 
55                 {
56                         get {
57                                 return attributes;
58                         }
59                         set {
60                                 attributes = value;
61                         }
62                 }
63
64                 // 
65                 // Whehter is it ok to use an unsafe pointer in this type container
66                 //
67                 public bool UnsafeOK (DeclSpace parent)
68                 {
69                         //
70                         // First check if this MemberCore modifier flags has unsafe set
71                         //
72                         if ((ModFlags & Modifiers.UNSAFE) != 0)
73                                 return true;
74
75                         if (parent.UnsafeContext)
76                                 return true;
77
78                         Expression.UnsafeError (Location);
79                         return false;
80                 }
81         }
82
83         /// <summary>
84         ///   Base class for structs, classes, enumerations and interfaces.  
85         /// </summary>
86         /// <remarks>
87         ///   They all create new declaration spaces.  This
88         ///   provides the common foundation for managing those name
89         ///   spaces.
90         /// </remarks>
91         public abstract class DeclSpace : MemberCore {
92                 /// <summary>
93                 ///   This points to the actual definition that is being
94                 ///   created with System.Reflection.Emit
95                 /// </summary>
96                 public TypeBuilder TypeBuilder;
97
98                 /// <summary>
99                 ///   If we are a generic type, this is the type we are
100                 ///   currently defining.  We need to lookup members on this
101                 ///   instead of the TypeBuilder.
102                 /// </summary>
103                 public TypeExpr CurrentType;
104
105                 /// <summary>
106                 ///   This variable tracks whether we have Closed the type
107                 /// </summary>
108                 public bool Created = false;
109                 
110                 //
111                 // This is the namespace in which this typecontainer
112                 // was declared.  We use this to resolve names.
113                 //
114                 public NamespaceEntry NamespaceEntry;
115
116                 public Hashtable Cache = new Hashtable ();
117                 
118                 public string Basename;
119                 
120                 /// <summary>
121                 ///   defined_names is used for toplevel objects
122                 /// </summary>
123                 protected Hashtable defined_names;
124
125                 //
126                 // Whether we are Generic
127                 //
128                 public bool IsGeneric;
129
130                 TypeContainer parent;
131
132                 public DeclSpace (NamespaceEntry ns, TypeContainer parent, string name, Attributes attrs, Location l)
133                         : base (name, attrs, l)
134                 {
135                         NamespaceEntry = ns;
136                         Basename = name.Substring (1 + name.LastIndexOf ('.'));
137                         defined_names = new Hashtable ();
138                         this.parent = parent;
139                 }
140
141                 public void RecordDecl ()
142                 {
143                         if ((NamespaceEntry != null) && (parent == RootContext.Tree.Types))
144                                 NamespaceEntry.DefineName (Basename, this);
145                 }
146
147                 /// <summary>
148                 ///   The result value from adding an declaration into
149                 ///   a struct or a class
150                 /// </summary>
151                 public enum AdditionResult {
152                         /// <summary>
153                         /// The declaration has been successfully
154                         /// added to the declation space.
155                         /// </summary>
156                         Success,
157
158                         /// <summary>
159                         ///   The symbol has already been defined.
160                         /// </summary>
161                         NameExists,
162
163                         /// <summary>
164                         ///   Returned if the declation being added to the
165                         ///   name space clashes with its container name.
166                         ///
167                         ///   The only exceptions for this are constructors
168                         ///   and static constructors
169                         /// </summary>
170                         EnclosingClash,
171
172                         /// <summary>
173                         ///   Returned if a constructor was created (because syntactically
174                         ///   it looked like a constructor) but was not (because the name
175                         ///   of the method is not the same as the container class
176                         /// </summary>
177                         NotAConstructor,
178
179                         /// <summary>
180                         ///   This is only used by static constructors to emit the
181                         ///   error 111, but this error for other things really
182                         ///   happens at another level for other functions.
183                         /// </summary>
184                         MethodExists,
185
186                         /// <summary>
187                         ///   Some other error.
188                         /// </summary>
189                         Error
190                 }
191
192                 /// <summary>
193                 ///   Returns a status code based purely on the name
194                 ///   of the member being added
195                 /// </summary>
196                 protected AdditionResult IsValid (string basename, string name)
197                 {
198                         if (basename == Basename)
199                                 return AdditionResult.EnclosingClash;
200
201                         if (defined_names.Contains (name))
202                                 return AdditionResult.NameExists;
203
204                         return AdditionResult.Success;
205                 }
206
207                 public static int length;
208                 public static int small;
209                 
210                 /// <summary>
211                 ///   Introduce @name into this declaration space and
212                 ///   associates it with the object @o.  Note that for
213                 ///   methods this will just point to the first method. o
214                 /// </summary>
215                 protected void DefineName (string name, object o)
216                 {
217                         defined_names.Add (name, o);
218
219 #if DEBUGME
220                         int p = name.LastIndexOf ('.');
221                         int l = name.Length;
222                         length += l;
223                         small += l -p;
224 #endif
225                 }
226
227                 /// <summary>
228                 ///   Returns the object associated with a given name in the declaration
229                 ///   space.  This is the inverse operation of `DefineName'
230                 /// </summary>
231                 public object GetDefinition (string name)
232                 {
233                         return defined_names [name];
234                 }
235                 
236                 bool in_transit = false;
237                 
238                 /// <summary>
239                 ///   This function is used to catch recursive definitions
240                 ///   in declarations.
241                 /// </summary>
242                 public bool InTransit {
243                         get {
244                                 return in_transit;
245                         }
246
247                         set {
248                                 in_transit = value;
249                         }
250                 }
251
252                 public TypeContainer Parent {
253                         get {
254                                 return parent;
255                         }
256                 }
257
258                 /// <summary>
259                 ///   Looks up the alias for the name
260                 /// </summary>
261                 public string LookupAlias (string name)
262                 {
263                         if (NamespaceEntry != null)
264                                 return NamespaceEntry.LookupAlias (name);
265                         else
266                                 return null;
267                 }
268                 
269                 // 
270                 // root_types contains all the types.  All TopLevel types
271                 // hence have a parent that points to `root_types', that is
272                 // why there is a non-obvious test down here.
273                 //
274                 public bool IsTopLevel {
275                         get {
276                                 if (parent != null){
277                                         if (parent.parent == null)
278                                                 return true;
279                                 }
280                                 return false;
281                         }
282                 }
283
284                 public virtual void CloseType ()
285                 {
286                         if (!Created){
287                                 try {
288                                         TypeBuilder.CreateType ();
289                                 } catch {
290                                         //
291                                         // The try/catch is needed because
292                                         // nested enumerations fail to load when they
293                                         // are defined.
294                                         //
295                                         // Even if this is the right order (enumerations
296                                         // declared after types).
297                                         //
298                                         // Note that this still creates the type and
299                                         // it is possible to save it
300                                 }
301                                 Created = true;
302                         }
303                 }
304
305                 /// <remarks>
306                 ///  Should be overriten by the appropriate declaration space
307                 /// </remarks>
308                 public abstract TypeBuilder DefineType ();
309                 
310                 /// <summary>
311                 ///   Define all members, but don't apply any attributes or do anything which may
312                 ///   access not-yet-defined classes.  This method also creates the MemberCache.
313                 /// </summary>
314                 public abstract bool DefineMembers (TypeContainer parent);
315
316                 //
317                 // Whether this is an `unsafe context'
318                 //
319                 public bool UnsafeContext {
320                         get {
321                                 if ((ModFlags & Modifiers.UNSAFE) != 0)
322                                         return true;
323                                 if (parent != null)
324                                         return parent.UnsafeContext;
325                                 return false;
326                         }
327                 }
328
329                 public static string MakeFQN (string nsn, string name)
330                 {
331                         if (nsn == "")
332                                 return name;
333                         return String.Concat (nsn, ".", name);
334                 }
335
336                 EmitContext type_resolve_ec;
337                 EmitContext GetTypeResolveEmitContext (TypeContainer parent, Location loc)
338                 {
339                         type_resolve_ec = new EmitContext (parent, this, loc, null, null, ModFlags, false);
340                         type_resolve_ec.ResolvingTypeTree = true;
341
342                         return type_resolve_ec;
343                 }
344
345                 // <summary>
346                 //    Looks up the type, as parsed into the expression `e' 
347                 // </summary>
348                 public Type ResolveType (Expression e, bool silent, Location loc)
349                 {
350                         if (type_resolve_ec == null)
351                                 type_resolve_ec = GetTypeResolveEmitContext (parent, loc);
352                         type_resolve_ec.loc = loc;
353                         type_resolve_ec.ContainerType = TypeBuilder;
354
355                         int errors = Report.Errors;
356                         TypeExpr d = e.ResolveAsTypeTerminal (type_resolve_ec);
357
358                         if (d == null || d.eclass != ExprClass.Type){
359                                 if (!silent && errors == Report.Errors){
360                                         Console.WriteLine ("Type is: " + e.GetType().ToString ());
361                                         Report.Error (246, loc, "Cannot find type `"+ e.ToString () +"'");
362                                 }
363                                 return null;
364                         }
365
366                         if (!d.CheckAccessLevel (this)) {
367                                 Report. Error (122, loc,  "`" + d.Name + "' " +
368                                        "is inaccessible because of its protection level");
369                                 return null;
370                         }
371
372                         return d.ResolveType (type_resolve_ec);
373                 }
374
375                 // <summary>
376                 //    Resolves the expression `e' for a type, and will recursively define
377                 //    types. 
378                 // </summary>
379                 public TypeExpr ResolveTypeExpr (Expression e, bool silent, Location loc)
380                 {
381                         if (type_resolve_ec == null)
382                                 type_resolve_ec = GetTypeResolveEmitContext (parent, loc);
383                         type_resolve_ec.loc = loc;
384                         type_resolve_ec.ContainerType = TypeBuilder;
385
386                         TypeExpr d = e.ResolveAsTypeTerminal (type_resolve_ec);
387                          
388                         if (d == null || d.eclass != ExprClass.Type){
389                                 if (!silent){
390                                         Report.Error (246, loc, "Cannot find type `"+ e +"'");
391                                 }
392                                 return null;
393                         }
394
395                         return d;
396                 }
397                 
398                 public bool CheckAccessLevel (Type check_type) 
399                 {
400                         if (check_type == TypeBuilder)
401                                 return true;
402
403                         if (check_type.IsGenericParameter)
404                                 return true; // FIXME
405                         
406                         TypeAttributes check_attr = check_type.Attributes & TypeAttributes.VisibilityMask;
407                         
408                         //
409                         // Broken Microsoft runtime, return public for arrays, no matter what 
410                         // the accessibility is for their underlying class, and they return 
411                         // NonPublic visibility for pointers
412                         //
413                         if (check_type.IsArray || check_type.IsPointer)
414                                 return CheckAccessLevel (TypeManager.GetElementType (check_type));
415
416                         switch (check_attr){
417                         case TypeAttributes.Public:
418                                 return true;
419
420                         case TypeAttributes.NotPublic:
421                                 //
422                                 // This test should probably use the declaringtype.
423                                 //
424                                 if (check_type.Assembly == TypeBuilder.Assembly){
425                                         return true;
426                                 }
427                                 return false;
428                                 
429                         case TypeAttributes.NestedPublic:
430                                 return true;
431
432                         case TypeAttributes.NestedPrivate:
433                                 string check_type_name = check_type.FullName;
434                                 string type_name = TypeBuilder.FullName;
435                                 
436                                 int cio = check_type_name.LastIndexOf ('+');
437                                 string container = check_type_name.Substring (0, cio);
438
439                                 //
440                                 // Check if the check_type is a nested class of the current type
441                                 //
442                                 if (check_type_name.StartsWith (type_name + "+")){
443                                         return true;
444                                 }
445                                 
446                                 if (type_name.StartsWith (container)){
447                                         return true;
448                                 }
449
450                                 return false;
451
452                         case TypeAttributes.NestedFamily:
453                                 //
454                                 // Only accessible to methods in current type or any subtypes
455                                 //
456                                 return FamilyAccessible (check_type);
457
458                         case TypeAttributes.NestedFamANDAssem:
459                                 return (check_type.Assembly == TypeBuilder.Assembly) &&
460                                         FamilyAccessible (check_type);
461
462                         case TypeAttributes.NestedFamORAssem:
463                                 return (check_type.Assembly == TypeBuilder.Assembly) ||
464                                         FamilyAccessible (check_type);
465
466                         case TypeAttributes.NestedAssembly:
467                                 return check_type.Assembly == TypeBuilder.Assembly;
468                         }
469
470                         Console.WriteLine ("HERE: " + check_attr);
471                         return false;
472
473                 }
474
475                 protected bool FamilyAccessible (Type check_type)
476                 {
477                         Type declaring = check_type.DeclaringType;
478                         if (TypeBuilder.IsSubclassOf (declaring))
479                                 return true;
480
481                         string check_type_name = check_type.FullName;
482                         
483                         int cio = check_type_name.LastIndexOf ('+');
484                         string container = check_type_name.Substring (0, cio);
485                         
486                         //
487                         // Check if the check_type is a nested class of the current type
488                         //
489                         if (check_type_name.StartsWith (container + "+"))
490                                 return true;
491
492                         return false;
493                 }
494
495                 // Access level of a type.
496                 const int X = 1;
497                 enum AccessLevel { // Each column represents `is this scope larger or equal to Blah scope'
498                         // Public    Assembly   Protected
499                         Protected           = (0 << 0) | (0 << 1) | (X << 2),
500                         Public              = (X << 0) | (X << 1) | (X << 2),
501                         Private             = (0 << 0) | (0 << 1) | (0 << 2),
502                         Internal            = (0 << 0) | (X << 1) | (0 << 2),
503                         ProtectedOrInternal = (0 << 0) | (X << 1) | (X << 2),
504                 }
505
506                 static AccessLevel GetAccessLevelFromModifiers (int flags)
507                 {
508                         if ((flags & Modifiers.INTERNAL) != 0) {
509
510                                 if ((flags & Modifiers.PROTECTED) != 0)
511                                         return AccessLevel.ProtectedOrInternal;
512                                 else
513                                         return AccessLevel.Internal;
514
515                         } else if ((flags & Modifiers.PROTECTED) != 0)
516                                 return AccessLevel.Protected;
517                         else if ((flags & Modifiers.PRIVATE) != 0)
518                                 return AccessLevel.Private;
519                         else
520                                 return AccessLevel.Public;
521                 }
522
523                 // What is the effective access level of this?
524                 // TODO: Cache this?
525                 AccessLevel EffectiveAccessLevel {
526                         get {
527                                 AccessLevel myAccess = GetAccessLevelFromModifiers (ModFlags);
528                                 if (!IsTopLevel && (Parent != null))
529                                         return myAccess & Parent.EffectiveAccessLevel;
530                                 return myAccess;
531                         }
532                 }
533
534                 // Return the access level for type `t'
535                 static AccessLevel TypeEffectiveAccessLevel (Type t)
536                 {
537                         if (t.IsPublic)
538                                 return AccessLevel.Public;
539                         if (t.IsNestedPrivate)
540                                 return AccessLevel.Private;
541                         if (t.IsNotPublic)
542                                 return AccessLevel.Internal;
543
544                         // By now, it must be nested
545                         AccessLevel parentLevel = TypeEffectiveAccessLevel (t.DeclaringType);
546
547                         if (t.IsNestedPublic)
548                                 return parentLevel;
549                         if (t.IsNestedAssembly)
550                                 return parentLevel & AccessLevel.Internal;
551                         if (t.IsNestedFamily)
552                                 return parentLevel & AccessLevel.Protected;
553                         if (t.IsNestedFamORAssem)
554                                 return parentLevel & AccessLevel.ProtectedOrInternal;
555                         if (t.IsNestedFamANDAssem)
556                                 throw new NotImplementedException ("NestedFamANDAssem not implemented, cant make this kind of type from c# anyways");
557
558                         // nested private is taken care of
559
560                         throw new Exception ("I give up, what are you?");
561                 }
562
563                 //
564                 // This answers `is the type P, as accessible as a member M which has the
565                 // accessability @flags which is declared as a nested member of the type T, this declspace'
566                 //
567                 public bool AsAccessible (Type p, int flags)
568                 {
569                         if (p.IsGenericParameter)
570                                 return true; // FIXME
571
572                         //
573                         // 1) if M is private, its accessability is the same as this declspace.
574                         // we already know that P is accessible to T before this method, so we
575                         // may return true.
576                         //
577
578                         if ((flags & Modifiers.PRIVATE) != 0)
579                                 return true;
580
581                         while (p.IsArray || p.IsPointer || p.IsByRef)
582                                 p = TypeManager.GetElementType (p);
583
584                         AccessLevel pAccess = TypeEffectiveAccessLevel (p);
585                         AccessLevel mAccess = this.EffectiveAccessLevel &
586                                 GetAccessLevelFromModifiers (flags);
587
588                         // for every place from which we can access M, we must
589                         // be able to access P as well. So, we want
590                         // For every bit in M and P, M_i -> P_1 == true
591                         // or, ~ (M -> P) == 0 <-> ~ ( ~M | P) == 0
592
593                         return ~ (~ mAccess | pAccess) == 0;
594                 }
595                 
596                 static DoubleHash dh = new DoubleHash (1000);
597
598                 Type LookupInterfaceOrClass (string ns, string name, out bool error)
599                 {
600                         DeclSpace parent;
601                         Type t;
602                         object r;
603                         
604                         error = false;
605
606                         if (dh.Lookup (ns, name, out r))
607                                 return (Type) r;
608                         else {
609                                 if (ns != ""){
610                                         if (Namespace.IsNamespace (ns)){
611                                                 string fullname = (ns != "") ? ns + "." + name : name;
612                                                 t = TypeManager.LookupType (fullname);
613                                         } else
614                                                 t = null;
615                                 } else
616                                         t = TypeManager.LookupType (name);
617                         }
618                         
619                         if (t != null) {
620                                 dh.Insert (ns, name, t);
621                                 return t;
622                         }
623
624                         //
625                         // In case we are fed a composite name, normalize it.
626                         //
627                         int p = name.LastIndexOf ('.');
628                         if (p != -1){
629                                 ns = MakeFQN (ns, name.Substring (0, p));
630                                 name = name.Substring (p+1);
631                         }
632                         
633                         parent = RootContext.Tree.LookupByNamespace (ns, name);
634                         if (parent == null) {
635                                 dh.Insert (ns, name, null);
636                                 return null;
637                         }
638
639                         t = parent.DefineType ();
640                         if (t == null){
641                                 error = true;
642                                 return null;
643                         }
644                         
645                         dh.Insert (ns, name, t);
646                         return t;
647                 }
648
649                 public static void Error_AmbiguousTypeReference (Location loc, string name, Type t1, Type t2)
650                 {
651                         Report.Error (104, loc,
652                                       String.Format ("`{0}' is an ambiguous reference ({1} or {2}) ", name,
653                                                      t1.FullName, t2.FullName));
654                 }
655
656                 /// <summary>
657                 ///   GetType is used to resolve type names at the DeclSpace level.
658                 ///   Use this to lookup class/struct bases, interface bases or 
659                 ///   delegate type references
660                 /// </summary>
661                 ///
662                 /// <remarks>
663                 ///   Contrast this to LookupType which is used inside method bodies to 
664                 ///   lookup types that have already been defined.  GetType is used
665                 ///   during the tree resolution process and potentially define
666                 ///   recursively the type
667                 /// </remarks>
668                 public Type FindType (Location loc, string name)
669                 {
670                         Type t;
671                         bool error;
672
673                         //
674                         // For the case the type we are looking for is nested within this one
675                         // or is in any base class
676                         //
677                         DeclSpace containing_ds = this;
678
679                         while (containing_ds != null){
680                                 Type container_type = containing_ds.TypeBuilder;
681                                 Type current_type = container_type;
682
683                                 while (current_type != null && current_type != TypeManager.object_type) {
684                                         string pre = current_type.FullName;
685
686                                         t = LookupInterfaceOrClass (pre, name, out error);
687                                         if (error)
688                                                 return null;
689                                 
690                                         if ((t != null) && containing_ds.CheckAccessLevel (t))
691                                                 return t;
692
693                                         current_type = current_type.BaseType;
694                                 }
695                                 containing_ds = containing_ds.Parent;
696                         }
697
698                         //
699                         // Attempt to lookup the class on our namespace and all it's implicit parents
700                         //
701                         for (NamespaceEntry ns = NamespaceEntry; ns != null; ns = ns.ImplicitParent) {
702                                 t = LookupInterfaceOrClass (ns.FullName, name, out error);
703                                 if (error)
704                                         return null;
705                                 
706                                 if (t != null) 
707                                         return t;
708                         }
709                         
710                         //
711                         // Attempt to do a direct unqualified lookup
712                         //
713                         t = LookupInterfaceOrClass ("", name, out error);
714                         if (error)
715                                 return null;
716                         
717                         if (t != null)
718                                 return t;
719                         
720                         //
721                         // Attempt to lookup the class on any of the `using'
722                         // namespaces
723                         //
724
725                         for (NamespaceEntry ns = NamespaceEntry; ns != null; ns = ns.Parent){
726
727                                 t = LookupInterfaceOrClass (ns.FullName, name, out error);
728                                 if (error)
729                                         return null;
730
731                                 if (t != null)
732                                         return t;
733
734                                 //
735                                 // Now check the using clause list
736                                 //
737                                 Type match = null;
738                                 foreach (Namespace using_ns in ns.GetUsingTable ()) {
739                                         match = LookupInterfaceOrClass (using_ns.Name, name, out error);
740                                         if (error)
741                                                 return null;
742
743                                         if (match != null){
744                                                 if (t != null){
745                                                         if (CheckAccessLevel (match)) {
746                                                                 Error_AmbiguousTypeReference (loc, name, t, match);
747                                                                 return null;
748                                                         }
749                                                         continue;
750                                                 }
751                                                 
752                                                 t = match;
753                                         }
754                                 }
755                                 if (t != null)
756                                         return t;
757                         }
758
759                         //Report.Error (246, Location, "Can not find type `"+name+"'");
760                         return null;
761                 }
762
763                 /// <remarks>
764                 ///   This function is broken and not what you're looking for.  It should only
765                 ///   be used while the type is still being created since it doesn't use the cache
766                 ///   and relies on the filter doing the member name check.
767                 /// </remarks>
768                 public abstract MemberList FindMembers (MemberTypes mt, BindingFlags bf,
769                                                         MemberFilter filter, object criteria);
770
771                 /// <remarks>
772                 ///   If we have a MemberCache, return it.  This property may return null if the
773                 ///   class doesn't have a member cache or while it's still being created.
774                 /// </remarks>
775                 public abstract MemberCache MemberCache {
776                         get;
777                 }
778
779                 //
780                 // Extensions for generics
781                 //
782                 TypeParameter[] type_params;
783
784                 ///
785                 /// Called by the parser to configure the type_parameter_list for this
786                 /// declaration space
787                 ///
788                 public AdditionResult SetParameterInfo (ArrayList type_parameter_list, ArrayList constraints_list)
789                 {
790                         type_params = new TypeParameter [type_parameter_list.Count];
791
792                         //
793                         // Mark this type as Generic
794                         //
795                         IsGeneric = true;
796                         
797                         //
798                         // Register all the names
799                         //
800                         for (int i = 0; i < type_parameter_list.Count; i++) {
801                                 string name = (string) type_parameter_list [i];
802
803                                 AdditionResult res = IsValid (name, name);
804
805                                 if (res != AdditionResult.Success)
806                                         return res;
807
808                                 Constraints constraints = null;
809                                 if (constraints_list != null) {
810                                         foreach (Constraints constraint in constraints_list) {
811                                                 if (constraint.TypeParameter == name) {
812                                                         constraints = constraint;
813                                                         break;
814                                                 }
815                                         }
816                                 }
817
818                                 type_params [i] = new TypeParameter (name, constraints, Location);
819
820                                 DefineName (name, type_params [i]);
821                         }
822
823                         return AdditionResult.Success;
824                 }
825
826                 public TypeParameter[] TypeParameters {
827                         get {
828                                 return type_params;
829                         }
830                 }
831
832                 public TypeParameterExpr LookupGeneric (string name, Location loc)
833                 {
834                         if (TypeParameters != null) {
835                                 foreach (TypeParameter type_param in TypeParameters) {
836                                         if (type_param.Name != name)
837                                                 continue;
838
839                                         return new TypeParameterExpr (type_param, loc);
840                                 }
841                         }
842
843                         if (parent != null)
844                                 return parent.LookupGeneric (name, loc);
845
846                         return null;
847                 }
848         }
849
850         /// <summary>
851         ///   This is a readonly list of MemberInfo's.      
852         /// </summary>
853         public class MemberList : IList {
854                 public readonly IList List;
855                 int count;
856
857                 /// <summary>
858                 ///   Create a new MemberList from the given IList.
859                 /// </summary>
860                 public MemberList (IList list)
861                 {
862                         if (list != null)
863                                 this.List = list;
864                         else
865                                 this.List = new ArrayList ();
866                         count = List.Count;
867                 }
868
869                 /// <summary>
870                 ///   Concatenate the ILists `first' and `second' to a new MemberList.
871                 /// </summary>
872                 public MemberList (IList first, IList second)
873                 {
874                         ArrayList list = new ArrayList ();
875                         list.AddRange (first);
876                         list.AddRange (second);
877                         count = list.Count;
878                         List = list;
879                 }
880
881                 public static readonly MemberList Empty = new MemberList (new ArrayList ());
882
883                 /// <summary>
884                 ///   Cast the MemberList into a MemberInfo[] array.
885                 /// </summary>
886                 /// <remarks>
887                 ///   This is an expensive operation, only use it if it's really necessary.
888                 /// </remarks>
889                 public static explicit operator MemberInfo [] (MemberList list)
890                 {
891                         Timer.StartTimer (TimerType.MiscTimer);
892                         MemberInfo [] result = new MemberInfo [list.Count];
893                         list.CopyTo (result, 0);
894                         Timer.StopTimer (TimerType.MiscTimer);
895                         return result;
896                 }
897
898                 // ICollection
899
900                 public int Count {
901                         get {
902                                 return count;
903                         }
904                 }
905
906                 public bool IsSynchronized {
907                         get {
908                                 return List.IsSynchronized;
909                         }
910                 }
911
912                 public object SyncRoot {
913                         get {
914                                 return List.SyncRoot;
915                         }
916                 }
917
918                 public void CopyTo (Array array, int index)
919                 {
920                         List.CopyTo (array, index);
921                 }
922
923                 // IEnumerable
924
925                 public IEnumerator GetEnumerator ()
926                 {
927                         return List.GetEnumerator ();
928                 }
929
930                 // IList
931
932                 public bool IsFixedSize {
933                         get {
934                                 return true;
935                         }
936                 }
937
938                 public bool IsReadOnly {
939                         get {
940                                 return true;
941                         }
942                 }
943
944                 object IList.this [int index] {
945                         get {
946                                 return List [index];
947                         }
948
949                         set {
950                                 throw new NotSupportedException ();
951                         }
952                 }
953
954                 // FIXME: try to find out whether we can avoid the cast in this indexer.
955                 public MemberInfo this [int index] {
956                         get {
957                                 return (MemberInfo) List [index];
958                         }
959                 }
960
961                 public int Add (object value)
962                 {
963                         throw new NotSupportedException ();
964                 }
965
966                 public void Clear ()
967                 {
968                         throw new NotSupportedException ();
969                 }
970
971                 public bool Contains (object value)
972                 {
973                         return List.Contains (value);
974                 }
975
976                 public int IndexOf (object value)
977                 {
978                         return List.IndexOf (value);
979                 }
980
981                 public void Insert (int index, object value)
982                 {
983                         throw new NotSupportedException ();
984                 }
985
986                 public void Remove (object value)
987                 {
988                         throw new NotSupportedException ();
989                 }
990
991                 public void RemoveAt (int index)
992                 {
993                         throw new NotSupportedException ();
994                 }
995         }
996
997         /// <summary>
998         ///   This interface is used to get all members of a class when creating the
999         ///   member cache.  It must be implemented by all DeclSpace derivatives which
1000         ///   want to support the member cache and by TypeHandle to get caching of
1001         ///   non-dynamic types.
1002         /// </summary>
1003         public interface IMemberContainer {
1004                 /// <summary>
1005                 ///   The name of the IMemberContainer.  This is only used for
1006                 ///   debugging purposes.
1007                 /// </summary>
1008                 string Name {
1009                         get;
1010                 }
1011
1012                 /// <summary>
1013                 ///   The type of this IMemberContainer.
1014                 /// </summary>
1015                 Type Type {
1016                         get;
1017                 }
1018
1019                 /// <summary>
1020                 ///   Returns the IMemberContainer of the parent class or null if this
1021                 ///   is an interface or TypeManger.object_type.
1022                 ///   This is used when creating the member cache for a class to get all
1023                 ///   members from the parent class.
1024                 /// </summary>
1025                 IMemberContainer Parent {
1026                         get;
1027                 }
1028
1029                 /// <summary>
1030                 ///   Whether this is an interface.
1031                 /// </summary>
1032                 bool IsInterface {
1033                         get;
1034                 }
1035
1036                 /// <summary>
1037                 ///   Returns all members of this class with the corresponding MemberTypes
1038                 ///   and BindingFlags.
1039                 /// </summary>
1040                 /// <remarks>
1041                 ///   When implementing this method, make sure not to return any inherited
1042                 ///   members and check the MemberTypes and BindingFlags properly.
1043                 ///   Unfortunately, System.Reflection is lame and doesn't provide a way to
1044                 ///   get the BindingFlags (static/non-static,public/non-public) in the
1045                 ///   MemberInfo class, but the cache needs this information.  That's why
1046                 ///   this method is called multiple times with different BindingFlags.
1047                 /// </remarks>
1048                 MemberList GetMembers (MemberTypes mt, BindingFlags bf);
1049
1050                 /// <summary>
1051                 ///   Return the container's member cache.
1052                 /// </summary>
1053                 MemberCache MemberCache {
1054                         get;
1055                 }
1056         }
1057
1058         /// <summary>
1059         ///   The MemberCache is used by dynamic and non-dynamic types to speed up
1060         ///   member lookups.  It has a member name based hash table; it maps each member
1061         ///   name to a list of CacheEntry objects.  Each CacheEntry contains a MemberInfo
1062         ///   and the BindingFlags that were initially used to get it.  The cache contains
1063         ///   all members of the current class and all inherited members.  If this cache is
1064         ///   for an interface types, it also contains all inherited members.
1065         ///
1066         ///   There are two ways to get a MemberCache:
1067         ///   * if this is a dynamic type, lookup the corresponding DeclSpace and then
1068         ///     use the DeclSpace.MemberCache property.
1069         ///   * if this not a dynamic type, call TypeHandle.GetTypeHandle() to get a
1070         ///     TypeHandle instance for the type and then use TypeHandle.MemberCache.
1071         /// </summary>
1072         public class MemberCache {
1073                 public readonly IMemberContainer Container;
1074                 protected Hashtable member_hash;
1075                 protected Hashtable method_hash;
1076                 
1077                 Hashtable interface_hash;
1078
1079                 /// <summary>
1080                 ///   Create a new MemberCache for the given IMemberContainer `container'.
1081                 /// </summary>
1082                 public MemberCache (IMemberContainer container)
1083                 {
1084                         this.Container = container;
1085
1086                         Timer.IncrementCounter (CounterType.MemberCache);
1087                         Timer.StartTimer (TimerType.CacheInit);
1088
1089                         
1090
1091                         // If we have a parent class (we have a parent class unless we're
1092                         // TypeManager.object_type), we deep-copy its MemberCache here.
1093                         if (Container.IsInterface) {
1094                                 MemberCache parent;
1095                                 interface_hash = new Hashtable ();
1096                                 
1097                                 if (Container.Parent != null)
1098                                         parent = Container.Parent.MemberCache;
1099                                 else
1100                                         parent = TypeHandle.ObjectType.MemberCache;
1101                                 member_hash = SetupCacheForInterface (parent);
1102                         } else if (Container.Parent != null)
1103                                 member_hash = SetupCache (Container.Parent.MemberCache);
1104                         else
1105                                 member_hash = new Hashtable ();
1106
1107                         // If this is neither a dynamic type nor an interface, create a special
1108                         // method cache with all declared and inherited methods.
1109                         Type type = container.Type;
1110                         if (!(type is TypeBuilder) && !type.IsInterface) {
1111                                 method_hash = new Hashtable ();
1112                                 AddMethods (type);
1113                         }
1114
1115                         // Add all members from the current class.
1116                         AddMembers (Container);
1117
1118                         Timer.StopTimer (TimerType.CacheInit);
1119                 }
1120
1121                 /// <summary>
1122                 ///   Bootstrap this member cache by doing a deep-copy of our parent.
1123                 /// </summary>
1124                 Hashtable SetupCache (MemberCache parent)
1125                 {
1126                         Hashtable hash = new Hashtable ();
1127
1128                         IDictionaryEnumerator it = parent.member_hash.GetEnumerator ();
1129                         while (it.MoveNext ()) {
1130                                 hash [it.Key] = ((ArrayList) it.Value).Clone ();
1131                         }
1132                                 
1133                         return hash;
1134                 }
1135
1136
1137                 /// <summary>
1138                 ///   Add the contents of `new_hash' to `hash'.
1139                 /// </summary>
1140                 void AddHashtable (Hashtable hash, Hashtable new_hash)
1141                 {
1142                         IDictionaryEnumerator it = new_hash.GetEnumerator ();
1143                         while (it.MoveNext ()) {
1144                                 ArrayList list = (ArrayList) hash [it.Key];
1145                                 if (list != null)
1146                                         list.AddRange ((ArrayList) it.Value);
1147                                 else
1148                                         hash [it.Key] = ((ArrayList) it.Value).Clone ();
1149                         }
1150                 }
1151
1152                 /// <summary>
1153                 ///   Bootstrap the member cache for an interface type.
1154                 ///   Type.GetMembers() won't return any inherited members for interface types,
1155                 ///   so we need to do this manually.  Interfaces also inherit from System.Object.
1156                 /// </summary>
1157                 Hashtable SetupCacheForInterface (MemberCache parent)
1158                 {
1159                         Hashtable hash = SetupCache (parent);
1160                         TypeExpr [] ifaces = TypeManager.GetInterfaces (Container.Type);
1161
1162                         foreach (TypeExpr iface in ifaces) {
1163                                 Type itype = iface.Type;
1164
1165                                 if (interface_hash.Contains (itype))
1166                                         continue;
1167                                 
1168                                 interface_hash [itype] = null;
1169
1170                                 IMemberContainer iface_container =
1171                                         TypeManager.LookupMemberContainer (itype);
1172
1173                                 MemberCache iface_cache = iface_container.MemberCache;
1174
1175                                 AddHashtable (hash, iface_cache.member_hash);
1176                                 
1177                                 if (iface_cache.interface_hash == null)
1178                                         continue;
1179                                 
1180                                 foreach (Type parent_contains in iface_cache.interface_hash.Keys)
1181                                         interface_hash [parent_contains] = null;
1182                         }
1183
1184                         return hash;
1185                 }
1186
1187                 /// <summary>
1188                 ///   Add all members from class `container' to the cache.
1189                 /// </summary>
1190                 void AddMembers (IMemberContainer container)
1191                 {
1192                         // We need to call AddMembers() with a single member type at a time
1193                         // to get the member type part of CacheEntry.EntryType right.
1194                         AddMembers (MemberTypes.Constructor, container);
1195                         AddMembers (MemberTypes.Field, container);
1196                         AddMembers (MemberTypes.Method, container);
1197                         AddMembers (MemberTypes.Property, container);
1198                         AddMembers (MemberTypes.Event, container);
1199                         // Nested types are returned by both Static and Instance searches.
1200                         AddMembers (MemberTypes.NestedType,
1201                                     BindingFlags.Static | BindingFlags.Public, container);
1202                         AddMembers (MemberTypes.NestedType,
1203                                     BindingFlags.Static | BindingFlags.NonPublic, container);
1204                 }
1205
1206                 void AddMembers (MemberTypes mt, IMemberContainer container)
1207                 {
1208                         AddMembers (mt, BindingFlags.Static | BindingFlags.Public, container);
1209                         AddMembers (mt, BindingFlags.Static | BindingFlags.NonPublic, container);
1210                         AddMembers (mt, BindingFlags.Instance | BindingFlags.Public, container);
1211                         AddMembers (mt, BindingFlags.Instance | BindingFlags.NonPublic, container);
1212                 }
1213
1214                 /// <summary>
1215                 ///   Add all members from class `container' with the requested MemberTypes and
1216                 ///   BindingFlags to the cache.  This method is called multiple times with different
1217                 ///   MemberTypes and BindingFlags.
1218                 /// </summary>
1219                 void AddMembers (MemberTypes mt, BindingFlags bf, IMemberContainer container)
1220                 {
1221                         MemberList members = container.GetMembers (mt, bf);
1222
1223                         foreach (MemberInfo member in members) {
1224                                 string name = member.Name;
1225
1226                                 // We use a name-based hash table of ArrayList's.
1227                                 ArrayList list = (ArrayList) member_hash [name];
1228                                 if (list == null) {
1229                                         list = new ArrayList ();
1230                                         member_hash.Add (name, list);
1231                                 }
1232
1233                                 // When this method is called for the current class, the list will
1234                                 // already contain all inherited members from our parent classes.
1235                                 // We cannot add new members in front of the list since this'd be an
1236                                 // expensive operation, that's why the list is sorted in reverse order
1237                                 // (ie. members from the current class are coming last).
1238                                 list.Add (new CacheEntry (container, member, mt, bf));
1239                         }
1240                 }
1241
1242                 /// <summary>
1243                 ///   Add all declared and inherited methods from class `type' to the method cache.
1244                 /// </summary>
1245                 void AddMethods (Type type)
1246                 {
1247                         AddMethods (BindingFlags.Static | BindingFlags.Public |
1248                                     BindingFlags.FlattenHierarchy, type);
1249                         AddMethods (BindingFlags.Static | BindingFlags.NonPublic |
1250                                     BindingFlags.FlattenHierarchy, type);
1251                         AddMethods (BindingFlags.Instance | BindingFlags.Public, type);
1252                         AddMethods (BindingFlags.Instance | BindingFlags.NonPublic, type);
1253                 }
1254
1255                 void AddMethods (BindingFlags bf, Type type)
1256                 {
1257                         MemberInfo [] members = type.GetMethods (bf);
1258
1259                         foreach (MethodBase member in members) {
1260                                 string name = member.Name;
1261
1262                                 // Varargs methods aren't allowed in C# code.
1263                                 if ((member.CallingConvention & CallingConventions.VarArgs) != 0)
1264                                         continue;
1265
1266                                 // We use a name-based hash table of ArrayList's.
1267                                 ArrayList list = (ArrayList) method_hash [name];
1268                                 if (list == null) {
1269                                         list = new ArrayList ();
1270                                         method_hash.Add (name, list);
1271                                 }
1272
1273                                 // Unfortunately, the elements returned by Type.GetMethods() aren't
1274                                 // sorted so we need to do this check for every member.
1275                                 BindingFlags new_bf = bf;
1276                                 if (member.DeclaringType == type)
1277                                         new_bf |= BindingFlags.DeclaredOnly;
1278
1279                                 list.Add (new CacheEntry (Container, member, MemberTypes.Method, new_bf));
1280                         }
1281                 }
1282
1283                 /// <summary>
1284                 ///   Compute and return a appropriate `EntryType' magic number for the given
1285                 ///   MemberTypes and BindingFlags.
1286                 /// </summary>
1287                 protected static EntryType GetEntryType (MemberTypes mt, BindingFlags bf)
1288                 {
1289                         EntryType type = EntryType.None;
1290
1291                         if ((mt & MemberTypes.Constructor) != 0)
1292                                 type |= EntryType.Constructor;
1293                         if ((mt & MemberTypes.Event) != 0)
1294                                 type |= EntryType.Event;
1295                         if ((mt & MemberTypes.Field) != 0)
1296                                 type |= EntryType.Field;
1297                         if ((mt & MemberTypes.Method) != 0)
1298                                 type |= EntryType.Method;
1299                         if ((mt & MemberTypes.Property) != 0)
1300                                 type |= EntryType.Property;
1301                         // Nested types are returned by static and instance searches.
1302                         if ((mt & MemberTypes.NestedType) != 0)
1303                                 type |= EntryType.NestedType | EntryType.Static | EntryType.Instance;
1304
1305                         if ((bf & BindingFlags.Instance) != 0)
1306                                 type |= EntryType.Instance;
1307                         if ((bf & BindingFlags.Static) != 0)
1308                                 type |= EntryType.Static;
1309                         if ((bf & BindingFlags.Public) != 0)
1310                                 type |= EntryType.Public;
1311                         if ((bf & BindingFlags.NonPublic) != 0)
1312                                 type |= EntryType.NonPublic;
1313                         if ((bf & BindingFlags.DeclaredOnly) != 0)
1314                                 type |= EntryType.Declared;
1315
1316                         return type;
1317                 }
1318
1319                 /// <summary>
1320                 ///   The `MemberTypes' enumeration type is a [Flags] type which means that it may
1321                 ///   denote multiple member types.  Returns true if the given flags value denotes a
1322                 ///   single member types.
1323                 /// </summary>
1324                 public static bool IsSingleMemberType (MemberTypes mt)
1325                 {
1326                         switch (mt) {
1327                         case MemberTypes.Constructor:
1328                         case MemberTypes.Event:
1329                         case MemberTypes.Field:
1330                         case MemberTypes.Method:
1331                         case MemberTypes.Property:
1332                         case MemberTypes.NestedType:
1333                                 return true;
1334
1335                         default:
1336                                 return false;
1337                         }
1338                 }
1339
1340                 /// <summary>
1341                 ///   We encode the MemberTypes and BindingFlags of each members in a "magic"
1342                 ///   number to speed up the searching process.
1343                 /// </summary>
1344                 [Flags]
1345                 protected enum EntryType {
1346                         None            = 0x000,
1347
1348                         Instance        = 0x001,
1349                         Static          = 0x002,
1350                         MaskStatic      = Instance|Static,
1351
1352                         Public          = 0x004,
1353                         NonPublic       = 0x008,
1354                         MaskProtection  = Public|NonPublic,
1355
1356                         Declared        = 0x010,
1357
1358                         Constructor     = 0x020,
1359                         Event           = 0x040,
1360                         Field           = 0x080,
1361                         Method          = 0x100,
1362                         Property        = 0x200,
1363                         NestedType      = 0x400,
1364
1365                         MaskType        = Constructor|Event|Field|Method|Property|NestedType
1366                 }
1367
1368                 protected struct CacheEntry {
1369                         public readonly IMemberContainer Container;
1370                         public readonly EntryType EntryType;
1371                         public readonly MemberInfo Member;
1372
1373                         public CacheEntry (IMemberContainer container, MemberInfo member,
1374                                            MemberTypes mt, BindingFlags bf)
1375                         {
1376                                 this.Container = container;
1377                                 this.Member = member;
1378                                 this.EntryType = GetEntryType (mt, bf);
1379                         }
1380                 }
1381
1382                 /// <summary>
1383                 ///   This is called each time we're walking up one level in the class hierarchy
1384                 ///   and checks whether we can abort the search since we've already found what
1385                 ///   we were looking for.
1386                 /// </summary>
1387                 protected bool DoneSearching (ArrayList list)
1388                 {
1389                         //
1390                         // We've found exactly one member in the current class and it's not
1391                         // a method or constructor.
1392                         //
1393                         if (list.Count == 1 && !(list [0] is MethodBase))
1394                                 return true;
1395
1396                         //
1397                         // Multiple properties: we query those just to find out the indexer
1398                         // name
1399                         //
1400                         if ((list.Count > 0) && (list [0] is PropertyInfo))
1401                                 return true;
1402
1403                         return false;
1404                 }
1405
1406                 /// <summary>
1407                 ///   Looks up members with name `name'.  If you provide an optional
1408                 ///   filter function, it'll only be called with members matching the
1409                 ///   requested member name.
1410                 ///
1411                 ///   This method will try to use the cache to do the lookup if possible.
1412                 ///
1413                 ///   Unlike other FindMembers implementations, this method will always
1414                 ///   check all inherited members - even when called on an interface type.
1415                 ///
1416                 ///   If you know that you're only looking for methods, you should use
1417                 ///   MemberTypes.Method alone since this speeds up the lookup a bit.
1418                 ///   When doing a method-only search, it'll try to use a special method
1419                 ///   cache (unless it's a dynamic type or an interface) and the returned
1420                 ///   MemberInfo's will have the correct ReflectedType for inherited methods.
1421                 ///   The lookup process will automatically restart itself in method-only
1422                 ///   search mode if it discovers that it's about to return methods.
1423                 /// </summary>
1424                 ArrayList global = new ArrayList ();
1425                 bool using_global = false;
1426                 
1427                 public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
1428                                                MemberFilter filter, object criteria)
1429                 {
1430                         if (using_global)
1431                                 throw new Exception ();
1432                         
1433                         bool declared_only = (bf & BindingFlags.DeclaredOnly) != 0;
1434                         bool method_search = mt == MemberTypes.Method;
1435                         // If we have a method cache and we aren't already doing a method-only search,
1436                         // then we restart a method search if the first match is a method.
1437                         bool do_method_search = !method_search && (method_hash != null);
1438
1439                         ArrayList applicable;
1440
1441                         // If this is a method-only search, we try to use the method cache if
1442                         // possible; a lookup in the method cache will return a MemberInfo with
1443                         // the correct ReflectedType for inherited methods.
1444                         
1445                         if (method_search && (method_hash != null))
1446                                 applicable = (ArrayList) method_hash [name];
1447                         else
1448                                 applicable = (ArrayList) member_hash [name];
1449                         
1450                         if (applicable == null)
1451                                 return MemberList.Empty;
1452
1453                         //
1454                         // 32  slots gives 53 rss/54 size
1455                         // 2/4 slots gives 55 rss
1456                         //
1457                         // Strange: from 25,000 calls, only 1,800
1458                         // are above 2.  Why does this impact it?
1459                         //
1460                         global.Clear ();
1461                         using_global = true;
1462
1463                         Timer.StartTimer (TimerType.CachedLookup);
1464
1465                         EntryType type = GetEntryType (mt, bf);
1466
1467                         IMemberContainer current = Container;
1468
1469                         // `applicable' is a list of all members with the given member name `name'
1470                         // in the current class and all its parent classes.  The list is sorted in
1471                         // reverse order due to the way how the cache is initialy created (to speed
1472                         // things up, we're doing a deep-copy of our parent).
1473
1474                         for (int i = applicable.Count-1; i >= 0; i--) {
1475                                 CacheEntry entry = (CacheEntry) applicable [i];
1476
1477                                 // This happens each time we're walking one level up in the class
1478                                 // hierarchy.  If we're doing a DeclaredOnly search, we must abort
1479                                 // the first time this happens (this may already happen in the first
1480                                 // iteration of this loop if there are no members with the name we're
1481                                 // looking for in the current class).
1482                                 if (entry.Container != current) {
1483                                         if (declared_only || DoneSearching (global))
1484                                                 break;
1485
1486                                         current = entry.Container;
1487                                 }
1488
1489                                 // Is the member of the correct type ?
1490                                 if ((entry.EntryType & type & EntryType.MaskType) == 0)
1491                                         continue;
1492
1493                                 // Is the member static/non-static ?
1494                                 if ((entry.EntryType & type & EntryType.MaskStatic) == 0)
1495                                         continue;
1496
1497                                 // Apply the filter to it.
1498                                 if (filter (entry.Member, criteria)) {
1499                                         if ((entry.EntryType & EntryType.MaskType) != EntryType.Method)
1500                                                 do_method_search = false;
1501                                         global.Add (entry.Member);
1502                                 }
1503                         }
1504
1505                         Timer.StopTimer (TimerType.CachedLookup);
1506
1507                         // If we have a method cache and we aren't already doing a method-only
1508                         // search, we restart in method-only search mode if the first match is
1509                         // a method.  This ensures that we return a MemberInfo with the correct
1510                         // ReflectedType for inherited methods.
1511                         if (do_method_search && (global.Count > 0)){
1512                                 using_global = false;
1513
1514                                 return FindMembers (MemberTypes.Method, bf, name, filter, criteria);
1515                         }
1516
1517                         using_global = false;
1518                         MemberInfo [] copy = new MemberInfo [global.Count];
1519                         global.CopyTo (copy);
1520                         return new MemberList (copy);
1521                 }
1522         }
1523 }