2004-06-13 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / mcs / typemanager.cs
index ad1da8698985610c8411d14c0558ded07b1c42da..53f2a4036bafdfe49d045860be24d1a7470cb3d3 100755 (executable)
@@ -70,6 +70,7 @@ public class TypeManager {
        static public Type intptr_type;
        static public Type monitor_type;
        static public Type runtime_field_handle_type;
+       static public Type runtime_argument_handle_type;
        static public Type attribute_type;
        static public Type attribute_usage_type;
        static public Type dllimport_type;
@@ -83,7 +84,7 @@ public class TypeManager {
        static public Type exception_type;
        static public Type invalid_operation_exception_type;
        static public Type obsolete_attribute_type;
-       static public object conditional_attribute_type;
+       static public Type conditional_attribute_type;
        static public Type in_attribute_type;
        static public Type cls_compliant_attribute_type;
        static public Type typed_reference_type;
@@ -535,6 +536,11 @@ public class TypeManager {
                assemblies = n;
        }
 
+        public static Assembly [] GetAssemblies ()
+        {
+                return assemblies;
+        }
+
        /// <summary>
        ///  Registers a module builder to lookup types from
        /// </summary>
@@ -844,11 +850,25 @@ public class TypeManager {
        /// </summary>
        static public string GetFullNameSignature (MemberInfo mi)
        {
-               string n = mi.Name;
-               if (n == ".ctor")
-                       n = mi.DeclaringType.Name;
-               
-               return mi.DeclaringType.FullName.Replace ('+', '.') + '.' + n;
+               return mi.DeclaringType.FullName.Replace ('+', '.') + '.' + mi.Name;
+       }
+
+       static public string GetFullNameSignature (MethodBase mb)
+       {
+               string name = mb.Name;
+               if (name == ".ctor")
+                       name = mb.DeclaringType.Name;
+
+               if (mb.IsSpecialName) {
+                       if (name.StartsWith ("get_") || name.StartsWith ("set_")) {
+                               name = name.Remove (0, 4);
+                       }
+
+                       if (name == "Item")
+                               name = "this";
+               }
+
+               return mb.DeclaringType.FullName.Replace ('+', '.') + '.' + name;
        }
 
        /// <summary>
@@ -866,34 +886,42 @@ public class TypeManager {
                return String.Format ("{0}.this[{1}]", signature.Substring (0, signature.LastIndexOf ('.')), arg);
        }
 
-        /// <summary>
-        ///   Returns the signature of the method
-        /// </summary>
-        static public string CSharpSignature (MethodBase mb)
-        {
-                string sig = "(";
+       /// <summary>
+       ///   Returns the signature of the method
+       /// </summary>
+       static public string CSharpSignature (MethodBase mb)
+       {
+               StringBuilder sig = new StringBuilder ("(");
 
                //
                // FIXME: We should really have a single function to do
                // everything instead of the following 5 line pattern
                //
-                ParameterData iparams = LookupParametersByBuilder (mb);
+               ParameterData iparams = LookupParametersByBuilder (mb);
 
-               if (iparams == null){
-                       ParameterInfo [] pi = mb.GetParameters ();
-                       iparams = new ReflectionParameters (pi);
-               }
+               if (iparams == null)
+                       iparams = new ReflectionParameters (mb);
+
+               // Is property
+               if (mb.IsSpecialName && iparams.Count == 0)
+                       return GetFullNameSignature (mb);
                
-                for (int i = 0; i < iparams.Count; i++) {
-                        if (i > 0) {
-                                sig += ", ";
-                        }
-                        sig += iparams.ParameterDesc(i);
-                }
-                sig += ")";
-
-                return GetFullNameSignature (mb) + sig;
-        }
+               for (int i = 0; i < iparams.Count; i++) {
+                       if (i > 0) {
+                               sig.Append (", ");
+                       }
+                       sig.Append (iparams.ParameterDesc (i));
+               }
+               sig.Append (")");
+
+               // Is indexer
+               if (mb.IsSpecialName && iparams.Count == 1) {
+                       sig.Replace ('(', '[');
+                       sig.Replace (')', ']');
+               }
+
+               return GetFullNameSignature (mb) + sig.ToString ();
+       }
 
        /// <summary>
        ///   Looks up a type, and aborts if it is not found.  This is used
@@ -1025,6 +1053,7 @@ public class TypeManager {
                type_type     = CoreLookupType ("System.Type");
 
                runtime_field_handle_type = CoreLookupType ("System.RuntimeFieldHandle");
+               runtime_argument_handle_type = CoreLookupType ("System.RuntimeArgumentHandle");
                runtime_helpers_type = CoreLookupType ("System.Runtime.CompilerServices.RuntimeHelpers");
                default_member_type  = CoreLookupType ("System.Reflection.DefaultMemberAttribute");
                runtime_handle_type  = CoreLookupType ("System.RuntimeTypeHandle");
@@ -2338,210 +2367,145 @@ public class TypeManager {
                return target_list;
        }
 
-       [Flags]
-       public enum MethodFlags {
-               ShouldIgnore = 1 << 2
-       }
-       
-       //
-       // 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)
-       {
-               MethodFlags flags = 0;
-               
-               if (mb.DeclaringType is TypeBuilder){
-                       IMethodData method = (IMethodData) builder_to_method [mb];
-                       if (method == null) {
-                               // FIXME: implement Obsolete attribute on Property,
-                               //        Indexer and Event.
-                               return 0;
-                       }
-
-                       if (method.ShouldIgnore ())
-                               flags |= MethodFlags.ShouldIgnore;
 
-                       return flags;
-               }
-
-               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;
-                       
-                       //
-                       // 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
        
        //
        // 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_qualifier_type;
-
-       //
-       // The assembly that defines the type is that is calling us
-       //
-       static Assembly closure_invocation_assembly;
 
        static internal bool FilterNone (MemberInfo m, object filter_criteria)
        {
                return true;
        }
-       
-       //
-       // 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_qualifier_type == null) || (closure_qualifier_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) ||
-                                       IsNestedChildOf (closure_invocation_type, m.DeclaringType);
+       internal class Closure {
+               internal bool     private_ok;
 
-                       //
-                       // 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;
-                       }
+               // Who is invoking us and which type is being queried currently.
+               internal Type     invocation_type;
+               internal Type     qualifier_type;
 
-                       // 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;
-                       }
+               // The assembly that defines the type is that is calling us
+               internal Assembly invocation_assembly;
+               internal IList almost_match;
 
-                       // We already know that we aren't in the same assembly.
-                       if (ma == MethodAttributes.Assembly)
+               private bool CheckValidFamilyAccess (bool is_static, MemberInfo m)
+               {
+                       if (invocation_type == null)
                                return false;
 
-                       // Family and FamANDAssem require that we derive.
-                       if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
-                               if (closure_invocation_type == null)
-                                       return false;
+                       Debug.Assert (IsSubclassOrNestedChildOf (invocation_type, m.DeclaringType));
 
-                               if (!IsSubclassOrNestedChildOf (closure_invocation_type, mb.DeclaringType))
-                                       return false;
+                       if (is_static)
+                               return true;
+                       
+                       // A nested class has access to all the protected members visible to its parent.
+                       if (qualifier_type != null
+                           && TypeManager.IsNestedChildOf (invocation_type, qualifier_type))
+                               return true;
 
+                       if (invocation_type == m.DeclaringType
+                           || invocation_type.IsSubclassOf (m.DeclaringType)) {
                                // 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_qualifier_type) &&
-                                   (closure_qualifier_type != null) &&
-                                   closure_invocation_type.IsSubclassOf (closure_qualifier_type) &&
-                                   !TypeManager.IsNestedChildOf (closure_invocation_type, closure_qualifier_type))
-                                       return false;
-
-                               return true;
+                               // => Ancestry should be: declaring_type ->* invocation_type ->*  qualified_type
+                               if (qualifier_type == null
+                                   || qualifier_type == invocation_type
+                                   || qualifier_type.IsSubclassOf (invocation_type))
+                                       return true;
                        }
 
-                       // Public.
-                       return true;
+                       if (almost_match != null)
+                               almost_match.Add (m);
+                       return false;
                }
-
-               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) ||
-                                       IsNestedChildOf (closure_invocation_type, m.DeclaringType);
-
+               
+               //
+               // This filter filters by name + whether it is ok to include private
+               // members in the search
+               //
+               internal bool Filter (MemberInfo m, object filter_criteria)
+               {
                        //
-                       // FamAndAssem requires that we not only derivate, but we are on the
-                       // same assembly.  
+                       // Hack: we know that the filter criteria will always be in the `closure'
+                       // fields. 
                        //
-                       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)
+                       if ((filter_criteria != null) && (m.Name != (string) filter_criteria))
                                return false;
+                       
+                       if (((qualifier_type == null) || (qualifier_type == invocation_type)) &&
+                           (m.DeclaringType == 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;
 
-                       // 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_qualifier_type) &&
-                                   (closure_qualifier_type != null) &&
-                                   closure_invocation_type.IsSubclassOf (closure_qualifier_type) &&
-                                   !TypeManager.IsNestedChildOf (closure_invocation_type, closure_qualifier_type))
-                                       return false;
-
+                               if (ma == MethodAttributes.Private)
+                                       return private_ok || (invocation_type == m.DeclaringType) ||
+                                               IsNestedChildOf (invocation_type, m.DeclaringType);
+                               
+                               // Assembly succeeds if we're in the same assembly.
+                               if (ma == MethodAttributes.Assembly)
+                                       return (invocation_assembly == mb.DeclaringType.Assembly);
+                               
+                               // FamAndAssem requires that we not only derive, but we are on the same assembly.  
+                               if (ma == MethodAttributes.FamANDAssem){
+                                       if (invocation_assembly != mb.DeclaringType.Assembly)
+                                               return false;
+                               }
+                               
+                               // Family and FamANDAssem require that we derive.
+                               if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem))
+                                       return CheckValidFamilyAccess (mb.IsStatic, m);
+                               
+                               // Public.
                                return true;
                        }
-
-                       // Public.
+                       
+                       if (m is FieldInfo){
+                               FieldInfo fi = (FieldInfo) m;
+                               FieldAttributes fa = fi.Attributes & FieldAttributes.FieldAccessMask;
+                               
+                               if (fa == FieldAttributes.Private)
+                                       return private_ok || (invocation_type == m.DeclaringType) ||
+                                               IsNestedChildOf (invocation_type, m.DeclaringType);
+                               
+                               // Assembly succeeds if we're in the same assembly.
+                               if (fa == FieldAttributes.Assembly)
+                                       return (invocation_assembly == fi.DeclaringType.Assembly);
+                                               
+                               // FamAndAssem requires that we not only derive, but we are on the same assembly.  
+                               if (fa == FieldAttributes.FamANDAssem){
+                                       if (invocation_assembly != fi.DeclaringType.Assembly)
+                                               return false;
+                               }
+                               
+                               // Family and FamANDAssem require that we derive.
+                               if ((fa == FieldAttributes.Family) || (fa == FieldAttributes.FamANDAssem))
+                                       return CheckValidFamilyAccess (fi.IsStatic, m);
+                               
+                               // Public.
+                               return true;
+                       }
+                       
+                       //
+                       // EventInfos and PropertyInfos, return true because they lack permission
+                       // information, so we need to check later on the methods.
+                       //
                        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);
+       static Closure closure = new Closure ();
+       static MemberFilter FilterWithClosure_delegate = new MemberFilter (closure.Filter);
        static MemberFilter FilterNone_delegate = new MemberFilter (FilterNone);
 
        //
@@ -2569,17 +2533,19 @@ public class TypeManager {
        // is allowed to access (using the specified `qualifier_type' if given); only use
        // BindingFlags.NonPublic to bypass the permission check.
        //
+       // The 'almost_match' argument is used for reporting error CS1540.
+       //
        // 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 qualifier_type,
                                                  Type queried_type,  MemberTypes mt,
-                                                 BindingFlags original_bf, string name)
+                                                 BindingFlags original_bf, string name, IList almost_match)
        {
                Timer.StartTimer (TimerType.MemberLookup);
 
                MemberInfo[] retval = RealMemberLookup (invocation_type, qualifier_type,
-                                                       queried_type, mt, original_bf, name);
+                                                       queried_type, mt, original_bf, name, almost_match);
 
                Timer.StopTimer (TimerType.MemberLookup);
 
@@ -2588,7 +2554,7 @@ public class TypeManager {
 
        static MemberInfo [] RealMemberLookup (Type invocation_type, Type qualifier_type,
                                               Type queried_type, MemberTypes mt,
-                                              BindingFlags original_bf, string name)
+                                              BindingFlags original_bf, string name, IList almost_match)
        {
                BindingFlags bf = original_bf;
                
@@ -2598,9 +2564,10 @@ public class TypeManager {
                bool skip_iface_check = true, used_cache = false;
                bool always_ok_flag = false;
 
-               closure_invocation_type = invocation_type;
-               closure_invocation_assembly = invocation_type != null ? invocation_type.Assembly : null;
-               closure_qualifier_type = qualifier_type;
+               closure.invocation_type = invocation_type;
+               closure.invocation_assembly = invocation_type != null ? invocation_type.Assembly : null;
+               closure.qualifier_type = qualifier_type;
+               closure.almost_match = almost_match;
 
                //
                // If we are a nested class, we always have access to our container
@@ -2647,7 +2614,7 @@ public class TypeManager {
                        else
                                bf = original_bf;
 
-                       closure_private_ok = (original_bf & BindingFlags.NonPublic) != 0;
+                       closure.private_ok = (original_bf & BindingFlags.NonPublic) != 0;
 
                        Timer.StopTimer (TimerType.MemberLookup);
 
@@ -2769,7 +2736,7 @@ public class TypeManager {
                foreach (TypeExpr itype in ifaces){
                        MemberInfo [] x;
 
-                       x = MemberLookup (null, null, itype.Type, mt, bf, name);
+                       x = MemberLookup (null, null, itype.Type, mt, bf, name, null);
                        if (x != null)
                                return x;
                }