2 // decl.cs: Declaration base class for structs, classes, enums and interfaces.
4 // Author: Miguel de Icaza (miguel@gnu.org)
6 // Licensed under the terms of the GNU GPL
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
10 // TODO: Move the method verification stuff from the class.cs and interface.cs here
14 using System.Collections;
15 using System.Reflection.Emit;
16 using System.Reflection;
18 namespace Mono.MonoBASIC {
21 /// Base representation for members. This is only used to keep track
22 /// of Name, Location and Modifier flags.
24 public abstract class MemberCore : Attributable
32 /// Modifier flags that the user specified in the source code
37 /// Location where this declaration happens
39 public readonly Location Location;
41 public MemberCore (string name, Attributes attrs, Location loc)
48 protected void WarningNotHiding (TypeContainer parent)
52 "The member " + parent.MakeName (Name) + " does not hide an " +
53 "inherited member. The keyword new is not required");
57 void Error_CannotChangeAccessModifiers (TypeContainer parent, MethodInfo parent_method,
61 // FIXME: report the old/new permissions?
64 507, Location, parent.MakeName (Name) +
65 ": can't change the access modifiers when overriding inherited " +
66 "member `" + name + "'");
70 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
71 // that have been defined.
73 // `name' is the user visible name for reporting errors (this is used to
74 // provide the right name regarding method names and properties)
76 protected bool CheckMethodAgainstBase (TypeContainer parent, MethodAttributes my_attrs,
77 MethodInfo mb, string name)
81 if ((ModFlags & Modifiers.OVERRIDE) != 0){
82 // Now we check that the overriden method is not final
85 Report.Error (30267, Location, parent.MakeName (Name) + " : cannot " +
86 "override inherited member `" + name +
87 "' because it is NotOverridable.");
90 else if (!(mb.IsAbstract || mb.IsVirtual)){
92 31086, Location, parent.MakeName (Name) +
93 ": Cannot override inherited member `" +
94 name + "' because it is not " +
95 "declared as Overridable");
100 // Check that the permissions are not being changed
102 MethodAttributes thisp = my_attrs & MethodAttributes.MemberAccessMask;
103 MethodAttributes parentp = mb.Attributes & MethodAttributes.MemberAccessMask;
105 if (thisp != parentp){
106 Error_CannotChangeAccessModifiers (parent, mb, name);
111 if ((ModFlags & ( Modifiers.NEW | Modifiers.SHADOWS | Modifiers.OVERRIDE )) == 0) {
112 if ((ModFlags & Modifiers.NONVIRTUAL) != 0) {
113 Report.Error (31088, Location,
114 parent.MakeName (Name) + " cannot " +
115 "be declared NotOverridable since this method is " +
116 "not marked as Overrides");
121 if ((ModFlags & (Modifiers.OVERRIDE)) == 0) {
122 if (Name != "Finalize") {
125 name + " cannot Shadows the method " +
126 parent.MakeName (Name) + " since it is declared " +
127 "'MustOverride' in base class");
131 else if (mb.IsVirtual){
132 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SHADOWS)) == 0){
133 if (Name != "Finalize"){
135 40005, 2, Location, parent.MakeName (Name) +
136 " shadows overridable member `" + name +
137 "'. To make the current member override that " +
138 "implementation, add the overrides keyword." );
139 ModFlags |= Modifiers.SHADOWS;
144 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE |
145 Modifiers.SHADOWS)) == 0){
146 if (Name != "Finalize"){
148 40004, 1, Location, "The keyword Shadows is required on " +
149 parent.MakeName (Name) + " because it hides " +
150 "inherited member `" + name + "'");
151 ModFlags |= Modifiers.SHADOWS;
159 public abstract bool Define (TypeContainer parent);
162 // Whehter is it ok to use an unsafe pointer in this type container
164 public bool UnsafeOK (DeclSpace parent)
167 // First check if this MemberCore modifier flags has unsafe set
169 if ((ModFlags & Modifiers.UNSAFE) != 0)
172 if (parent.UnsafeContext)
175 Expression.UnsafeError (Location);
181 // FIXME: This is temporary outside DeclSpace, because I have to fix a bug
182 // in MCS that makes it fail the lookup for the enum
186 /// The result value from adding an declaration into
187 /// a struct or a class
189 public enum AdditionResult {
191 /// The declaration has been successfully
192 /// added to the declation space.
197 /// The symbol has already been defined.
202 /// Returned if the declation being added to the
203 /// name space clashes with its container name.
205 /// The only exceptions for this are constructors
206 /// and static constructors
211 /// Returned if a constructor was created (because syntactically
212 /// it looked like a constructor) but was not (because the name
213 /// of the method is not the same as the container class
218 /// This is only used by static constructors to emit the
219 /// error 111, but this error for other things really
220 /// happens at another level for other functions.
226 /// Base class for structs, classes, enumerations and interfaces.
229 /// They all create new declaration spaces. This
230 /// provides the common foundation for managing those name
233 public abstract class DeclSpace : MemberCore {
235 /// this points to the actual definition that is being
236 /// created with System.Reflection.Emit
238 public TypeBuilder TypeBuilder;
241 /// This variable tracks whether we have Closed the type
243 public bool Created = false;
246 // This is the namespace in which this typecontainer
247 // was declared. We use this to resolve names.
249 public Namespace Namespace;
251 public Hashtable Cache = new Hashtable ();
253 public string Basename;
256 /// defined_names is used for toplevel objects
258 protected Hashtable defined_names;
260 TypeContainer parent;
262 public DeclSpace (TypeContainer parent, string name, Attributes attrs, Location l)
263 : base (name, attrs, l)
265 Basename = name.Substring (1 + name.LastIndexOf ('.'));
266 defined_names = new Hashtable ();
267 this.parent = parent;
271 /// Returns a status code based purely on the name
272 /// of the member being added
274 protected AdditionResult IsValid (string name)
276 if (name == Basename)
277 return AdditionResult.EnclosingClash;
279 if (defined_names.Contains (name))
280 return AdditionResult.NameExists;
282 return AdditionResult.Success;
286 /// Introduce @name into this declaration space and
287 /// associates it with the object @o. Note that for
288 /// methods this will just point to the first method. o
290 protected void DefineName (string name, object o)
293 defined_names.Add (name, o);
295 catch (ArgumentException exp) {
296 Report.Error (30260, /*Location,*/
297 "More than one defination with same name '" + name +
298 "' is found in the container '" + Name + "'");
304 /// Returns the object associated with a given name in the declaration
305 /// space. This is the inverse operation of `DefineName'
307 public object GetDefinition (string name)
309 return defined_names [name];
312 bool in_transit = false;
315 /// This function is used to catch recursive definitions
318 public bool InTransit {
328 public TypeContainer Parent {
335 /// Looks up the alias for the name
337 public string LookupAlias (string name)
339 return RootContext.SourceBeingCompiled.LookupAlias (name);
343 // root_types contains all the types. All TopLevel types
344 // hence have a parent that points to `root_types', that is
345 // why there is a non-obvious test down here.
347 public bool IsTopLevel {
350 if (parent.parent == null)
357 public virtual void CloseType ()
361 TypeBuilder.CreateType ();
364 // The try/catch is needed because
365 // nested enumerations fail to load when they
368 // Even if this is the right order (enumerations
369 // declared after types).
371 // Note that this still creates the type and
372 // it is possible to save it
379 /// Should be overriten by the appropriate declaration space
381 public abstract TypeBuilder DefineType ();
384 /// Define all members, but don't apply any attributes or do anything which may
385 /// access not-yet-defined classes. This method also creates the MemberCache.
387 public abstract bool DefineMembers (TypeContainer parent);
390 // Whether this is an `unsafe context'
392 public bool UnsafeContext {
394 if ((ModFlags & Modifiers.UNSAFE) != 0)
397 return parent.UnsafeContext;
402 public static string MakeFQN (string nsn, string name)
404 string prefix = (nsn == "" ? "" : nsn + ".");
406 return prefix + name;
409 EmitContext type_resolve_ec;
410 EmitContext GetTypeResolveEmitContext (TypeContainer parent, Location loc)
412 type_resolve_ec = new EmitContext (parent, this, loc, null, null, ModFlags, false);
413 type_resolve_ec.ResolvingTypeTree = true;
415 return type_resolve_ec;
419 // Looks up the type, as parsed into the expression `e'
421 public Type ResolveType (Expression e, bool silent, Location loc)
423 if (type_resolve_ec == null)
424 type_resolve_ec = GetTypeResolveEmitContext (parent, loc);
425 type_resolve_ec.loc = loc;
427 int errors = Report.Errors;
428 Expression d = e.Resolve (type_resolve_ec, ResolveFlags.Type);
429 if (d == null || d.eclass != ExprClass.Type){
430 if (!silent && errors == Report.Errors){
431 Report.Error (30002, loc, "Cannot find type `"+ e.ToString () +"'");
440 // Resolves the expression `e' for a type, and will recursively define
443 public Expression ResolveTypeExpr (Expression e, bool silent, Location loc)
445 if (type_resolve_ec == null)
446 type_resolve_ec = GetTypeResolveEmitContext (parent, loc);
448 Expression d = e.Resolve (type_resolve_ec, ResolveFlags.Type);
449 if (d == null || d.eclass != ExprClass.Type){
451 Report.Error (30002, loc, "Cannot find type `"+ e +"'");
459 Type LookupInterfaceOrClass (string ns, string name, out bool error)
465 name = MakeFQN (ns, name);
467 t = TypeManager.LookupType (name);
471 parent = (DeclSpace) RootContext.Tree.Decls [name];
475 t = parent.DefineType ();
477 Report.Error (30256, Location, "Class definition is circular: `"+name+"'");
484 public static void Error_AmbiguousTypeReference (Location loc, string name, Type t1, Type t2)
486 Report.Error (104, loc,
487 String.Format ("`{0}' is an ambiguous reference ({1} or {2}) ", name,
488 t1.FullName, t2.FullName));
492 /// GetType is used to resolve type names at the DeclSpace level.
493 /// Use this to lookup class/struct bases, interface bases or
494 /// delegate type references
498 /// Contrast this to LookupType which is used inside method bodies to
499 /// lookup types that have already been defined. GetType is used
500 /// during the tree resolution process and potentially define
501 /// recursively the type
503 public Type FindType (Location loc, string name)
509 // For the case the type we are looking for is nested within this one
510 // or is in any base class
512 DeclSpace containing_ds = this;
514 while (containing_ds != null){
515 Type current_type = containing_ds.TypeBuilder;
517 while (current_type != null) {
518 string pre = current_type.FullName;
520 t = LookupInterfaceOrClass (pre, name, out error);
527 current_type = current_type.BaseType;
529 containing_ds = containing_ds.Parent;
533 // Attempt to lookup the class on our namespace and all it's implicit parents
535 for (string ns = Namespace.Name; ns != null; ns = RootContext.ImplicitParent (ns)) {
537 t = LookupInterfaceOrClass (ns, name, out error);
546 // Attempt to do a direct unqualified lookup
548 t = LookupInterfaceOrClass ("", name, out error);
556 // Attempt to lookup the class on any of the `using'
560 for (Namespace ns = Namespace; ns != null; ns = ns.Parent){
562 t = LookupInterfaceOrClass (ns.Name, name, out error);
570 // Now check the using clause list
572 ICollection imports_list = RootContext.SourceBeingCompiled.ImportsTable;
574 if (imports_list == null)
578 foreach (SourceBeingCompiled.ImportsEntry ue in imports_list){
579 match = LookupInterfaceOrClass (ue.Name, name, out error);
585 Error_AmbiguousTypeReference (loc, name, t, match);
597 //Report.Error (246, Location, "Can not find type `"+name+"'");
602 /// This function is broken and not what you're looking for. It should only
603 /// be used while the type is still being created since it doesn't use the cache
604 /// and relies on the filter doing the member name check.
606 public abstract MemberList FindMembers (MemberTypes mt, BindingFlags bf,
607 MemberFilter filter, object criteria);
610 /// If we have a MemberCache, return it. This property may return null if the
611 /// class doesn't have a member cache or while it's still being created.
613 public abstract MemberCache MemberCache {
617 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
620 TypeBuilder.SetCustomAttribute (cb);
621 } catch (System.ArgumentException e) {
622 Report.Warning (-21, a.Location,
623 "The CharSet named property on StructLayout\n"+
624 "\tdoes not work correctly on Microsoft.NET\n"+
625 "\tYou might want to remove the CharSet declaration\n"+
626 "\tor compile using the Mono runtime instead of the\n"+
627 "\tMicrosoft .NET runtime\n"+
628 "\tThe runtime gave the error: " + e);
635 /// This is a readonly list of MemberInfo's.
637 public class MemberList : IList {
638 public readonly IList List;
642 /// Create a new MemberList from the given IList.
644 public MemberList (IList list)
649 this.List = new ArrayList ();
654 /// Concatenate the ILists `first' and `second' to a new MemberList.
656 public MemberList (IList first, IList second)
658 ArrayList list = new ArrayList ();
659 list.AddRange (first);
660 list.AddRange (second);
665 public static readonly MemberList Empty = new MemberList (new ArrayList ());
668 /// Cast the MemberList into a MemberInfo[] array.
671 /// This is an expensive operation, only use it if it's really necessary.
673 public static explicit operator MemberInfo [] (MemberList list)
675 Timer.StartTimer (TimerType.MiscTimer);
676 MemberInfo [] result = new MemberInfo [list.Count];
677 list.CopyTo (result, 0);
678 Timer.StopTimer (TimerType.MiscTimer);
690 public bool IsSynchronized {
692 return List.IsSynchronized;
696 public object SyncRoot {
698 return List.SyncRoot;
702 public void CopyTo (Array array, int index)
704 List.CopyTo (array, index);
709 public IEnumerator GetEnumerator ()
711 return List.GetEnumerator ();
716 public bool IsFixedSize {
722 public bool IsReadOnly {
728 object IList.this [int index] {
734 throw new NotSupportedException ();
738 // FIXME: try to find out whether we can avoid the cast in this indexer.
739 public MemberInfo this [int index] {
741 return (MemberInfo) List [index];
745 public int Add (object value)
747 throw new NotSupportedException ();
752 throw new NotSupportedException ();
755 public bool Contains (object value)
757 return List.Contains (value);
760 public int IndexOf (object value)
762 return List.IndexOf (value);
765 public void Insert (int index, object value)
767 throw new NotSupportedException ();
770 public void Remove (object value)
772 throw new NotSupportedException ();
775 public void RemoveAt (int index)
777 throw new NotSupportedException ();
782 /// This interface is used to get all members of a class when creating the
783 /// member cache. It must be implemented by all DeclSpace derivatives which
784 /// want to support the member cache and by TypeHandle to get caching of
785 /// non-dynamic types.
787 public interface IMemberContainer {
789 /// The name of the IMemberContainer. This is only used for
790 /// debugging purposes.
797 /// The type of this IMemberContainer.
804 /// Returns the IMemberContainer of the parent class or null if this
805 /// is an interface or TypeManger.object_type.
806 /// This is used when creating the member cache for a class to get all
807 /// members from the parent class.
809 IMemberContainer Parent {
814 /// Whether this is an interface.
821 /// Returns all members of this class with the corresponding MemberTypes
822 /// and BindingFlags.
825 /// When implementing this method, make sure not to return any inherited
826 /// members and check the MemberTypes and BindingFlags properly.
827 /// Unfortunately, System.Reflection is lame and doesn't provide a way to
828 /// get the BindingFlags (static/non-static,public/non-public) in the
829 /// MemberInfo class, but the cache needs this information. That's why
830 /// this method is called multiple times with different BindingFlags.
832 MemberList GetMembers (MemberTypes mt, BindingFlags bf);
835 /// Return the container's member cache.
837 MemberCache MemberCache {
843 /// The MemberCache is used by dynamic and non-dynamic types to speed up
844 /// member lookups. It has a member name based hash table; it maps each member
845 /// name to a list of CacheEntry objects. Each CacheEntry contains a MemberInfo
846 /// and the BindingFlags that were initially used to get it. The cache contains
847 /// all members of the current class and all inherited members. If this cache is
848 /// for an interface types, it also contains all inherited members.
850 /// There are two ways to get a MemberCache:
851 /// * if this is a dynamic type, lookup the corresponding DeclSpace and then
852 /// use the DeclSpace.MemberCache property.
853 /// * if this not a dynamic type, call TypeHandle.GetTypeHandle() to get a
854 /// TypeHandle instance for the type and then use TypeHandle.MemberCache.
856 public class MemberCache {
857 public readonly IMemberContainer Container;
858 protected CaseInsensitiveHashtable member_hash;
859 protected CaseInsensitiveHashtable method_hash;
860 protected CaseInsensitiveHashtable interface_hash;
863 /// Create a new MemberCache for the given IMemberContainer `container'.
865 public MemberCache (IMemberContainer container)
867 this.Container = container;
869 Timer.IncrementCounter (CounterType.MemberCache);
870 Timer.StartTimer (TimerType.CacheInit);
872 interface_hash = new CaseInsensitiveHashtable ();
874 // If we have a parent class (we have a parent class unless we're
875 // TypeManager.object_type), we deep-copy its MemberCache here.
876 if (Container.Parent != null)
877 member_hash = SetupCache (Container.Parent.MemberCache);
878 else if (Container.IsInterface)
879 member_hash = SetupCacheForInterface ();
881 member_hash = new CaseInsensitiveHashtable ();
883 // If this is neither a dynamic type nor an interface, create a special
884 // method cache with all declared and inherited methods.
885 Type type = container.Type;
886 if (!(type is TypeBuilder) && !type.IsInterface) {
887 method_hash = new CaseInsensitiveHashtable ();
891 // Add all members from the current class.
892 AddMembers (Container);
894 Timer.StopTimer (TimerType.CacheInit);
898 /// Bootstrap this member cache by doing a deep-copy of our parent.
900 CaseInsensitiveHashtable SetupCache (MemberCache parent)
902 CaseInsensitiveHashtable hash = new CaseInsensitiveHashtable ();
904 IDictionaryEnumerator it = parent.member_hash.GetEnumerator ();
905 while (it.MoveNext ()) {
906 ArrayList al = (ArrayList) it.Value;
907 // Constructors are never inherited and all have the same key
908 if ((((CacheEntry)al [0]).EntryType & EntryType.Constructor) == 0)
909 hash [it.Key] = ((ArrayList) it.Value).Clone ();
915 void AddInterfaces (MemberCache parent)
917 foreach (Type iface in parent.interface_hash.Keys) {
918 if (!interface_hash.Contains (iface))
919 interface_hash.Add (iface, true);
924 /// Add the contents of `new_hash' to `hash'.
926 void AddHashtable (Hashtable hash, Hashtable new_hash)
928 IDictionaryEnumerator it = new_hash.GetEnumerator ();
929 while (it.MoveNext ()) {
930 ArrayList list = (ArrayList) hash [it.Key];
932 list.AddRange ((ArrayList) it.Value);
934 hash [it.Key] = ((ArrayList) it.Value).Clone ();
939 /// Bootstrap the member cache for an interface type.
940 /// Type.GetMembers() won't return any inherited members for interface types,
941 /// so we need to do this manually. Interfaces also inherit from System.Object.
943 CaseInsensitiveHashtable SetupCacheForInterface ()
945 CaseInsensitiveHashtable hash = SetupCache (TypeHandle.ObjectType.MemberCache);
946 Type [] ifaces = TypeManager.GetInterfaces (Container.Type);
948 foreach (Type iface in ifaces) {
949 if (interface_hash.Contains (iface))
951 interface_hash.Add (iface, true);
953 IMemberContainer iface_container =
954 TypeManager.LookupMemberContainer (iface);
956 MemberCache iface_cache = iface_container.MemberCache;
957 AddHashtable (hash, iface_cache.member_hash);
958 AddInterfaces (iface_cache);
965 /// Add all members from class `container' to the cache.
967 void AddMembers (IMemberContainer container)
969 // We need to call AddMembers() with a single member type at a time
970 // to get the member type part of CacheEntry.EntryType right.
971 AddMembers (MemberTypes.Constructor, container);
972 AddMembers (MemberTypes.Field, container);
973 AddMembers (MemberTypes.Method, container);
974 AddMembers (MemberTypes.Property, container);
975 AddMembers (MemberTypes.Event, container);
976 // Nested types are returned by both Static and Instance searches.
977 AddMembers (MemberTypes.NestedType,
978 BindingFlags.Static | BindingFlags.Public, container);
979 AddMembers (MemberTypes.NestedType,
980 BindingFlags.Static | BindingFlags.NonPublic, container);
983 void AddMembers (MemberTypes mt, IMemberContainer container)
985 AddMembers (mt, BindingFlags.Static | BindingFlags.Public, container);
986 AddMembers (mt, BindingFlags.Static | BindingFlags.NonPublic, container);
987 AddMembers (mt, BindingFlags.Instance | BindingFlags.Public, container);
988 AddMembers (mt, BindingFlags.Instance | BindingFlags.NonPublic, container);
992 /// Add all members from class `container' with the requested MemberTypes and
993 /// BindingFlags to the cache. This method is called multiple times with different
994 /// MemberTypes and BindingFlags.
996 void AddMembers (MemberTypes mt, BindingFlags bf, IMemberContainer container)
998 MemberList members = container.GetMembers (mt, bf);
999 /*BindingFlags new_bf = (container == Container) ?
1000 bf | BindingFlags.DeclaredOnly : bf;
1002 foreach (MemberInfo member in members) {
1003 string name = member.Name;
1005 // We use a name-based hash table of ArrayList's.
1006 ArrayList list = (ArrayList) member_hash [name];
1008 list = new ArrayList ();
1009 member_hash.Add (name, list);
1012 // When this method is called for the current class, the list will
1013 // already contain all inherited members from our parent classes.
1014 // We cannot add new members in front of the list since this'd be an
1015 // expensive operation, that's why the list is sorted in reverse order
1016 // (ie. members from the current class are coming last).
1017 list.Add (new CacheEntry (container, member, mt, bf));
1022 /// Add all declared and inherited methods from class `type' to the method cache.
1024 void AddMethods (Type type)
1026 AddMethods (BindingFlags.Static | BindingFlags.Public, type);
1027 AddMethods (BindingFlags.Static | BindingFlags.NonPublic, type);
1028 AddMethods (BindingFlags.Instance | BindingFlags.Public, type);
1029 AddMethods (BindingFlags.Instance | BindingFlags.NonPublic, type);
1032 void AddMethods (BindingFlags bf, Type type)
1034 MemberInfo [] members = type.GetMethods (bf);
1036 foreach (MethodBase member in members) {
1037 string name = member.Name;
1039 // Varargs methods aren't allowed in C# code.
1040 if ((member.CallingConvention & CallingConventions.VarArgs) != 0)
1043 // We use a name-based hash table of ArrayList's.
1044 ArrayList list = (ArrayList) method_hash [name];
1046 list = new ArrayList ();
1047 method_hash.Add (name, list);
1050 // Unfortunately, the elements returned by Type.GetMethods() aren't
1051 // sorted so we need to do this check for every member.
1052 BindingFlags new_bf = bf;
1053 if (member.DeclaringType == type)
1054 new_bf |= BindingFlags.DeclaredOnly;
1056 list.Add (new CacheEntry (Container, member, MemberTypes.Method, new_bf));
1061 /// Compute and return a appropriate `EntryType' magic number for the given
1062 /// MemberTypes and BindingFlags.
1064 protected static EntryType GetEntryType (MemberTypes mt, BindingFlags bf)
1066 EntryType type = EntryType.None;
1068 if ((mt & MemberTypes.Constructor) != 0)
1069 type |= EntryType.Constructor;
1070 if ((mt & MemberTypes.Event) != 0)
1071 type |= EntryType.Event;
1072 if ((mt & MemberTypes.Field) != 0)
1073 type |= EntryType.Field;
1074 if ((mt & MemberTypes.Method) != 0)
1075 type |= EntryType.Method;
1076 if ((mt & MemberTypes.Property) != 0)
1077 type |= EntryType.Property;
1078 // Nested types are returned by static and instance searches.
1079 if ((mt & MemberTypes.NestedType) != 0)
1080 type |= EntryType.NestedType | EntryType.Static | EntryType.Instance;
1082 if ((bf & BindingFlags.Instance) != 0)
1083 type |= EntryType.Instance;
1084 if ((bf & BindingFlags.Static) != 0)
1085 type |= EntryType.Static;
1086 if ((bf & BindingFlags.Public) != 0)
1087 type |= EntryType.Public;
1088 if ((bf & BindingFlags.NonPublic) != 0)
1089 type |= EntryType.NonPublic;
1090 if ((bf & BindingFlags.DeclaredOnly) != 0)
1091 type |= EntryType.Declared;
1097 /// The `MemberTypes' enumeration type is a [Flags] type which means that it may
1098 /// denote multiple member types. Returns true if the given flags value denotes a
1099 /// single member types.
1101 public static bool IsSingleMemberType (MemberTypes mt)
1104 case MemberTypes.Constructor:
1105 case MemberTypes.Event:
1106 case MemberTypes.Field:
1107 case MemberTypes.Method:
1108 case MemberTypes.Property:
1109 case MemberTypes.NestedType:
1118 /// We encode the MemberTypes and BindingFlags of each members in a "magic"
1119 /// number to speed up the searching process.
1122 protected enum EntryType {
1127 MaskStatic = Instance|Static,
1131 MaskProtection = Public|NonPublic,
1135 Constructor = 0x020,
1142 MaskType = Constructor|Event|Field|Method|Property|NestedType
1145 protected struct CacheEntry {
1146 public readonly IMemberContainer Container;
1147 public readonly EntryType EntryType;
1148 public readonly MemberInfo Member;
1150 public CacheEntry (IMemberContainer container, MemberInfo member,
1151 MemberTypes mt, BindingFlags bf)
1153 this.Container = container;
1154 this.Member = member;
1155 this.EntryType = GetEntryType (mt, bf);
1160 /// This is called each time we're walking up one level in the class hierarchy
1161 /// and checks whether we can abort the search since we've already found what
1162 /// we were looking for.
1164 protected bool DoneSearching (ArrayList list)
1167 // We've found exactly one member in the current class and it's not
1168 // a method or constructor.
1170 if (list.Count == 1 && !(list [0] is MethodBase))
1174 // Multiple properties: we query those just to find out the indexer
1177 if ((list.Count > 0) && (list [0] is PropertyInfo))
1184 /// Looks up members with name `name'. If you provide an optional
1185 /// filter function, it'll only be called with members matching the
1186 /// requested member name.
1188 /// This method will try to use the cache to do the lookup if possible.
1190 /// Unlike other FindMembers implementations, this method will always
1191 /// check all inherited members - even when called on an interface type.
1193 /// If you know that you're only looking for methods, you should use
1194 /// MemberTypes.Method alone since this speeds up the lookup a bit.
1195 /// When doing a method-only search, it'll try to use a special method
1196 /// cache (unless it's a dynamic type or an interface) and the returned
1197 /// MemberInfo's will have the correct ReflectedType for inherited methods.
1198 /// The lookup process will automatically restart itself in method-only
1199 /// search mode if it discovers that it's about to return methods.
1201 public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
1202 MemberFilter filter, object criteria)
1204 bool declared_only = (bf & BindingFlags.DeclaredOnly) != 0;
1205 bool method_search = mt == MemberTypes.Method;
1206 // If we have a method cache and we aren't already doing a method-only search,
1207 // then we restart a method search if the first match is a method.
1208 bool do_method_search = !method_search && (method_hash != null);
1209 bf |= BindingFlags.IgnoreCase;
1210 ArrayList applicable;
1212 // If this is a method-only search, we try to use the method cache if
1213 // possible; a lookup in the method cache will return a MemberInfo with
1214 // the correct ReflectedType for inherited methods.
1215 if (method_search && (method_hash != null))
1216 applicable = (ArrayList) method_hash [name];
1218 applicable = (ArrayList) member_hash [name];
1220 if (applicable == null)
1221 return MemberList.Empty;
1223 ArrayList list = new ArrayList ();
1225 Timer.StartTimer (TimerType.CachedLookup);
1227 EntryType type = GetEntryType (mt, bf);
1229 IMemberContainer current = Container;
1231 // `applicable' is a list of all members with the given member name `name'
1232 // in the current class and all its parent classes. The list is sorted in
1233 // reverse order due to the way how the cache is initialy created (to speed
1234 // things up, we're doing a deep-copy of our parent).
1236 for (int i = applicable.Count-1; i >= 0; i--) {
1237 CacheEntry entry = (CacheEntry) applicable [i];
1239 // This happens each time we're walking one level up in the class
1240 // hierarchy. If we're doing a DeclaredOnly search, we must abort
1241 // the first time this happens (this may already happen in the first
1242 // iteration of this loop if there are no members with the name we're
1243 // looking for in the current class).
1244 if (entry.Container != current) {
1245 if (declared_only || DoneSearching (list))
1248 current = entry.Container;
1251 // Is the member of the correct type ?
1252 if ((entry.EntryType & type & EntryType.MaskType) == 0)
1255 // Is the member static/non-static ?
1256 if ((entry.EntryType & type & EntryType.MaskStatic) == 0)
1259 // Apply the filter to it.
1260 if (filter (entry.Member, criteria)) {
1261 if ((entry.EntryType & EntryType.MaskType) != EntryType.Method)
1262 do_method_search = false;
1263 list.Add (entry.Member);
1267 Timer.StopTimer (TimerType.CachedLookup);
1269 // If we have a method cache and we aren't already doing a method-only
1270 // search, we restart in method-only search mode if the first match is
1271 // a method. This ensures that we return a MemberInfo with the correct
1272 // ReflectedType for inherited methods.
1273 if (do_method_search && (list.Count > 0))
1274 return FindMembers (MemberTypes.Method, bf, name, filter, criteria);
1276 return new MemberList (list);