+
+
+ //
+ // Returns whether the array of memberinfos contains the given method
+ //
+ static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
+ {
+ Type [] new_args = TypeManager.GetArgumentTypes (new_method);
+
+ foreach (MethodBase method in array){
+ if (method.Name != new_method.Name)
+ continue;
+
+ Type [] old_args = TypeManager.GetArgumentTypes (method);
+ int old_count = old_args.Length;
+ int i;
+
+ if (new_args.Length != old_count)
+ continue;
+
+ for (i = 0; i < old_count; i++){
+ if (old_args [i] != new_args [i])
+ break;
+ }
+ if (i != old_count)
+ continue;
+
+ return true;
+ }
+ return false;
+ }
+
+ //
+ // We copy methods from `new_members' into `target_list' if the signature
+ // for the method from in the new list does not exist in the target_list
+ //
+ // The name is assumed to be the same.
+ //
+ public static ArrayList CopyNewMethods (ArrayList target_list, MemberList new_members)
+ {
+ if (target_list == null){
+ target_list = new ArrayList ();
+
+ foreach (MemberInfo mi in new_members){
+ if (mi is MethodBase)
+ target_list.Add (mi);
+ }
+ return target_list;
+ }
+
+ MemberInfo [] target_array = new MemberInfo [target_list.Count];
+ target_list.CopyTo (target_array, 0);
+
+ foreach (MemberInfo mi in new_members){
+ MethodBase new_method = (MethodBase) mi;
+
+ if (!ArrayContainsMethod (target_array, new_method))
+ target_list.Add (new_method);
+ }
+ return target_list;
+ }
+
+ [Flags]
+ public enum MethodFlags {
+ IsObsolete = 1,
+ IsObsoleteError = 2,
+ ShouldIgnore = 3
+ }
+
+ //
+ // Returns the TypeManager.MethodFlags for this method.
+ // This emits an error 619 / warning 618 if the method is obsolete.
+ // In the former case, TypeManager.MethodFlags.IsObsoleteError is returned.
+ //
+ static public MethodFlags GetMethodFlags (MethodBase mb, Location loc)
+ {
+ MethodFlags flags = 0;
+
+ if (mb.DeclaringType is TypeBuilder){
+ MethodData method = (MethodData) builder_to_method [mb];
+ if (method == null) {
+ // FIXME: implement Obsolete attribute on Property,
+ // Indexer and Event.
+ return 0;
+ }
+
+ return method.GetMethodFlags (loc);
+ }
+
+ object [] attrs = mb.GetCustomAttributes (true);
+ foreach (object ta in attrs){
+ if (!(ta is System.Attribute)){
+ Console.WriteLine ("Unknown type in GetMethodFlags: " + ta);
+ continue;
+ }
+ System.Attribute a = (System.Attribute) ta;
+ if (a.TypeId == TypeManager.obsolete_attribute_type){
+ ObsoleteAttribute oa = (ObsoleteAttribute) a;
+
+ string method_desc = TypeManager.CSharpSignature (mb);
+
+ if (oa.IsError) {
+ Report.Error (619, loc, "Method `" + method_desc +
+ "' is obsolete: `" + oa.Message + "'");
+ return MethodFlags.IsObsoleteError;
+ } else
+ Report.Warning (618, loc, "Method `" + method_desc +
+ "' is obsolete: `" + oa.Message + "'");
+
+ flags |= MethodFlags.IsObsolete;
+
+ continue;
+ }
+
+ //
+ // Skip over conditional code.
+ //
+ if (a.TypeId == TypeManager.conditional_attribute_type){
+ ConditionalAttribute ca = (ConditionalAttribute) a;
+
+ if (RootContext.AllDefines [ca.ConditionString] == null)
+ flags |= MethodFlags.ShouldIgnore;
+ }
+ }
+
+ return flags;
+ }
+
+#region MemberLookup implementation
+
+ //
+ // Name of the member
+ //
+ static string closure_name;
+
+ //
+ // Whether we allow private members in the result (since FindMembers
+ // uses NonPublic for both protected and private), we need to distinguish.
+ //
+ static bool closure_private_ok;
+
+ //
+ // Who is invoking us and which type is being queried currently.
+ //
+ static Type closure_invocation_type;
+ static Type closure_queried_type;
+ static Type closure_start_type;
+
+ //
+ // The assembly that defines the type is that is calling us
+ //
+ static Assembly closure_invocation_assembly;
+
+ //
+ // This filter filters by name + whether it is ok to include private
+ // members in the search
+ //
+ static internal bool FilterWithClosure (MemberInfo m, object filter_criteria)
+ {
+ //
+ // Hack: we know that the filter criteria will always be in the `closure'
+ // fields.
+ //
+
+ if ((filter_criteria != null) && (m.Name != (string) filter_criteria))
+ return false;
+
+ if ((closure_start_type == closure_invocation_type) &&
+ (m.DeclaringType == closure_invocation_type))
+ return true;
+
+ //
+ // Ugly: we need to find out the type of `m', and depending
+ // on this, tell whether we accept or not
+ //
+ if (m is MethodBase){
+ MethodBase mb = (MethodBase) m;
+ MethodAttributes ma = mb.Attributes & MethodAttributes.MemberAccessMask;
+
+ if (ma == MethodAttributes.Private)
+ return closure_private_ok || (closure_invocation_type == m.DeclaringType);
+
+ //
+ // FamAndAssem requires that we not only derivate, but we are on the
+ // same assembly.
+ //
+ if (ma == MethodAttributes.FamANDAssem){
+ if (closure_invocation_assembly != mb.DeclaringType.Assembly)
+ return false;
+ }
+
+ // Assembly and FamORAssem succeed if we're in the same assembly.
+ if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
+ if (closure_invocation_assembly == mb.DeclaringType.Assembly)
+ return true;
+ }
+
+ // We already know that we aren't in the same assembly.
+ if (ma == MethodAttributes.Assembly)
+ return false;
+
+ // Family and FamANDAssem require that we derive.
+ if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
+ if (closure_invocation_type == null)
+ return false;
+
+ if (!IsSubclassOrNestedChildOf (closure_invocation_type, mb.DeclaringType))
+ return false;
+
+ // Although a derived class can access protected members of its base class
+ // it cannot do so through an instance of the base class (CS1540).
+ if (!mb.IsStatic && (closure_invocation_type != closure_start_type) &&
+ closure_invocation_type.IsSubclassOf (closure_start_type))
+ return false;
+
+ return true;
+ }
+
+ // Public.
+ return true;
+ }
+
+ if (m is FieldInfo){
+ FieldInfo fi = (FieldInfo) m;
+ FieldAttributes fa = fi.Attributes & FieldAttributes.FieldAccessMask;
+
+ if (fa == FieldAttributes.Private)
+ return closure_private_ok || (closure_invocation_type == m.DeclaringType);
+
+ //
+ // FamAndAssem requires that we not only derivate, but we are on the
+ // same assembly.
+ //
+ if (fa == FieldAttributes.FamANDAssem){
+ if (closure_invocation_assembly != fi.DeclaringType.Assembly)
+ return false;
+ }
+
+ // Assembly and FamORAssem succeed if we're in the same assembly.
+ if ((fa == FieldAttributes.Assembly) || (fa == FieldAttributes.FamORAssem)){
+ if (closure_invocation_assembly == fi.DeclaringType.Assembly)
+ return true;
+ }
+
+ // We already know that we aren't in the same assembly.
+ if (fa == FieldAttributes.Assembly)
+ return false;
+
+ // Family and FamANDAssem require that we derive.
+ if ((fa == FieldAttributes.Family) || (fa == FieldAttributes.FamANDAssem)){
+ if (closure_invocation_type == null)
+ return false;
+
+ if (!IsSubclassOrNestedChildOf (closure_invocation_type, fi.DeclaringType))
+ return false;
+
+ // Although a derived class can access protected members of its base class
+ // it cannot do so through an instance of the base class (CS1540).
+ if (!fi.IsStatic && (closure_invocation_type != closure_start_type) &&
+ closure_invocation_type.IsSubclassOf (closure_start_type))
+ return false;
+
+ return true;
+ }
+
+ // Public.
+ return true;
+ }
+
+ //
+ // EventInfos and PropertyInfos, return true because they lack permission
+ // informaiton, so we need to check later on the methods.
+ //
+ return true;
+ }
+
+ static MemberFilter FilterWithClosure_delegate = new MemberFilter (FilterWithClosure);
+
+ //
+ // Looks up a member called `name' in the `queried_type'. This lookup
+ // is done by code that is contained in the definition for `invocation_type'.
+ //
+ // The binding flags are `bf' and the kind of members being looked up are `mt'
+ //
+ // Returns an array of a single element for everything but Methods/Constructors
+ // that might return multiple matches.
+ //
+ public static MemberInfo [] MemberLookup (Type invocation_type, Type queried_type,
+ MemberTypes mt, BindingFlags original_bf, string name)
+ {
+ Timer.StartTimer (TimerType.MemberLookup);
+
+ MemberInfo[] retval = RealMemberLookup (invocation_type, queried_type,
+ mt, original_bf, name);
+
+ Timer.StopTimer (TimerType.MemberLookup);
+
+ return retval;
+ }
+
+ static MemberInfo [] RealMemberLookup (Type invocation_type, Type queried_type,
+ MemberTypes mt, BindingFlags original_bf, string name)
+ {
+ BindingFlags bf = original_bf;
+
+ ArrayList method_list = null;
+ Type current_type = queried_type;
+ bool searching = (original_bf & BindingFlags.DeclaredOnly) == 0;
+ bool private_ok;
+ bool always_ok_flag = false;
+ bool skip_iface_check = true, used_cache = false;
+
+ closure_name = name;
+ closure_invocation_type = invocation_type;
+ closure_invocation_assembly = invocation_type != null ? invocation_type.Assembly : null;
+ closure_start_type = queried_type;
+
+ //
+ // If we are a nested class, we always have access to our container
+ // type names
+ //
+ if (invocation_type != null){
+ string invocation_name = invocation_type.FullName;
+ if (invocation_name.IndexOf ('+') != -1){
+ string container = queried_type.FullName + "+";
+ int container_length = container.Length;
+
+ if (invocation_name.Length > container_length){
+ string shared = invocation_name.Substring (0, container_length);
+
+ if (shared == container)
+ always_ok_flag = true;
+ }
+ }
+ }
+
+ do {
+ MemberList list;
+
+ //
+ // `NonPublic' is lame, because it includes both protected and
+ // private methods, so we need to control this behavior by
+ // explicitly tracking if a private method is ok or not.
+ //
+ // The possible cases are:
+ // public, private and protected (internal does not come into the
+ // equation)
+ //
+ if (invocation_type != null){
+ if (invocation_type == current_type){
+ private_ok = true;
+ } else
+ private_ok = always_ok_flag;
+
+ if (private_ok || invocation_type.IsSubclassOf (current_type))
+ bf = original_bf | BindingFlags.NonPublic;
+ } else {
+ private_ok = false;
+ bf = original_bf & ~BindingFlags.NonPublic;
+ }
+
+ closure_private_ok = private_ok;
+ closure_queried_type = current_type;
+
+ Timer.StopTimer (TimerType.MemberLookup);
+
+ list = MemberLookup_FindMembers (current_type, mt, bf, name, out used_cache);
+
+ Timer.StartTimer (TimerType.MemberLookup);
+
+ //
+ // When queried for an interface type, the cache will automatically check all
+ // inherited members, so we don't need to do this here. However, this only
+ // works if we already used the cache in the first iteration of this loop.
+ //
+ // If we used the cache in any further iteration, we can still terminate the
+ // loop since the cache always looks in all parent classes.
+ //
+
+ if (used_cache)
+ searching = false;
+ else
+ skip_iface_check = false;
+
+ if (current_type == TypeManager.object_type)
+ searching = false;
+ else {
+ current_type = current_type.BaseType;
+
+ //
+ // This happens with interfaces, they have a null
+ // basetype. Look members up in the Object class.
+ //
+ if (current_type == null)
+ current_type = TypeManager.object_type;
+ }
+
+ if (list.Count == 0)
+ continue;
+
+ //
+ // Events and types are returned by both `static' and `instance'
+ // searches, which means that our above FindMembers will
+ // return two copies of the same.
+ //
+ if (list.Count == 1 && !(list [0] is MethodBase)){
+ return (MemberInfo []) list;
+ }
+
+ //
+ // Multiple properties: we query those just to find out the indexer
+ // name
+ //
+ if (list [0] is PropertyInfo)
+ return (MemberInfo []) list;
+
+ //
+ // We found methods, turn the search into "method scan"
+ // mode.
+ //
+
+ method_list = CopyNewMethods (method_list, list);
+ mt &= (MemberTypes.Method | MemberTypes.Constructor);
+ } while (searching);
+
+ if (method_list != null && method_list.Count > 0)
+ return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
+
+ //
+ // This happens if we already used the cache in the first iteration, in this case
+ // the cache already looked in all interfaces.
+ //
+ if (skip_iface_check)
+ return null;
+
+ //
+ // Interfaces do not list members they inherit, so we have to
+ // scan those.
+ //
+ if (!queried_type.IsInterface)
+ return null;
+
+ if (queried_type.IsArray)
+ queried_type = TypeManager.array_type;
+
+ Type [] ifaces = GetInterfaces (queried_type);
+ if (ifaces == null)
+ return null;
+
+ foreach (Type itype in ifaces){
+ MemberInfo [] x;
+
+ x = MemberLookup (null, itype, mt, bf, name);
+ if (x != null)
+ return x;
+ }
+
+ return null;
+ }
+#endregion
+
+}
+
+/// <summary>
+/// There is exactly one instance of this class per type.
+/// </summary>
+public sealed class TypeHandle : IMemberContainer {
+ public readonly TypeHandle BaseType;
+
+ readonly int id = ++next_id;
+ static int next_id = 0;
+
+ /// <summary>
+ /// Lookup a TypeHandle instance for the given type. If the type doesn't have
+ /// a TypeHandle yet, a new instance of it is created. This static method
+ /// ensures that we'll only have one TypeHandle instance per type.
+ /// </summary>
+ public static TypeHandle GetTypeHandle (Type t)
+ {
+ TypeHandle handle = (TypeHandle) type_hash [t];
+ if (handle != null)
+ return handle;
+
+ handle = new TypeHandle (t);
+ type_hash.Add (t, handle);
+ return handle;
+ }
+
+ /// <summary>
+ /// Returns the TypeHandle for TypeManager.object_type.
+ /// </summary>
+ public static IMemberContainer ObjectType {
+ get {
+ if (object_type != null)
+ return object_type;
+
+ object_type = GetTypeHandle (TypeManager.object_type);
+
+ return object_type;
+ }
+ }
+
+ /// <summary>
+ /// Returns the TypeHandle for TypeManager.array_type.
+ /// </summary>
+ public static IMemberContainer ArrayType {
+ get {
+ if (array_type != null)
+ return array_type;
+
+ array_type = GetTypeHandle (TypeManager.array_type);
+
+ return array_type;
+ }
+ }
+
+ private static PtrHashtable type_hash = new PtrHashtable ();
+
+ private static TypeHandle object_type = null;
+ private static TypeHandle array_type = null;
+
+ private Type type;
+ private bool is_interface;
+ private MemberCache member_cache;
+
+ private TypeHandle (Type type)
+ {
+ this.type = type;
+ if (type.BaseType != null)
+ BaseType = GetTypeHandle (type.BaseType);
+ else if ((type != TypeManager.object_type) && (type != typeof (object)))
+ is_interface = true;
+ this.member_cache = new MemberCache (this);
+ }
+
+ // IMemberContainer methods
+
+ public string Name {
+ get {
+ return type.FullName;
+ }
+ }
+
+ public Type Type {
+ get {
+ return type;
+ }
+ }
+
+ public IMemberContainer Parent {
+ get {
+ return BaseType;
+ }
+ }
+
+ public bool IsInterface {
+ get {
+ return is_interface;
+ }
+ }
+
+ public MemberList GetMembers (MemberTypes mt, BindingFlags bf)
+ {
+ if (mt == MemberTypes.Event)
+ return new MemberList (type.GetEvents (bf | BindingFlags.DeclaredOnly));
+ else
+ return new MemberList (type.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
+ null, null));
+ }
+
+ // IMemberFinder methods
+
+ public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
+ MemberFilter filter, object criteria)
+ {
+ return member_cache.FindMembers (mt, bf, name, filter, criteria);
+ }
+
+ public MemberCache MemberCache {
+ get {
+ return member_cache;
+ }
+ }
+
+ public override string ToString ()
+ {
+ if (BaseType != null)
+ return "TypeHandle (" + id + "," + Name + " : " + BaseType + ")";
+ else
+ return "TypeHandle (" + id + "," + Name + ")";
+ }