ecore.cs : Added support for Implicit numeric conversions
[mono.git] / mcs / mbas / typemanager.cs
index eb4352f1317ad84fffd74e1d7b03e4191af5569d..608ee89dfee433cef07a380b7c1808b59821cd9c 100644 (file)
@@ -148,6 +148,7 @@ public class TypeManager {
        static public ConstructorInfo void_decimal_ctor_five_args;
        static public ConstructorInfo void_datetime_ctor_ticks_arg;
        static public ConstructorInfo unverifiable_code_ctor;
+       static public ConstructorInfo default_member_ctor;
        
        // <remarks>
        //   Holds the Array of Assemblies that have been loaded
@@ -160,7 +161,13 @@ public class TypeManager {
        //  Keeps a list of module builders. We used this to do lookups
        //  on the modulebuilder using GetType -- needed for arrays
        // </remarks>
-       static ModuleBuilder [] modules;
+       
+       // This is changed from list of ModuleBuilders 
+       // to list of Modules for getting addmodule 
+       // compiler option working
+       // Anirban - 13.07.2004
+
+       static System.Reflection.Module [] modules;
 
        // <remarks>
        //   This is the type_cache from the assemblies to avoid
@@ -206,12 +213,6 @@ public class TypeManager {
        // <remarks>
        static Hashtable method_internal_params;
 
-       // <remarks>
-       //  Keeps track of attribute types
-       // </remarks>
-
-       static Hashtable builder_to_attr;
-
        // <remarks>
        //  Keeps track of methods
        // </remarks>
@@ -300,10 +301,10 @@ public class TypeManager {
                user_types = new ArrayList ();
                
                types = new CaseInsensitiveHashtable ();
+               negative_hits = new CaseInsensitiveHashtable ();
                typecontainers = new CaseInsensitiveHashtable ();
                
                builder_to_declspace = new PtrHashtable ();
-               builder_to_attr = new PtrHashtable ();
                builder_to_method = new PtrHashtable ();
                method_arguments = new PtrHashtable ();
                method_internal_params = new PtrHashtable ();
@@ -387,11 +388,6 @@ public class TypeManager {
                builder_to_method.Add (builder, method);
        }
 
-       public static void RegisterAttrType (Type t, TypeContainer tc)
-       {
-               builder_to_attr.Add (t, tc);
-       }
-
        /// <summary>
        ///   Returns the TypeContainer whose Type is 't' or null if there is no
        ///   TypeContainer for 't' (ie, the Type comes from a library)
@@ -426,10 +422,10 @@ public class TypeManager {
        {
                return builder_to_declspace [t] as Enum;
        }
-       
-       public static TypeContainer LookupAttr (Type t)
+
+       public static TypeContainer LookupClass (Type t)
        {
-               return (TypeContainer) builder_to_attr [t];
+               return builder_to_declspace [t] as TypeContainer;
        }
        
        /// <summary>
@@ -454,10 +450,10 @@ public class TypeManager {
        /// <summary>
        ///  Registers a module builder to lookup types from
        /// </summary>
-       public static void AddModule (ModuleBuilder mb)
+       public static void AddModule (System.Reflection.Module mb)
        {
                int top = modules != null ? modules.Length : 0;
-               ModuleBuilder [] n = new ModuleBuilder [top + 1];
+               System.Reflection.Module [] n = new System.Reflection.Module [top + 1];
 
                if (modules != null)
                        modules.CopyTo (n, 0);
@@ -465,12 +461,16 @@ public class TypeManager {
                modules = n;
        }
 
-
        private class StandardModule {
                public readonly string Namespace;
                public readonly string Name;
-               public StandardModule(string _namespace, string name) { Namespace = _namespace; Name = name; }
-               public override string ToString() { return ((Namespace != null && Namespace.Length > 0)?(Namespace + "."):"") + Name; }
+               private readonly string asString;
+               public StandardModule(string _namespace, string name) { 
+                       Namespace = _namespace; 
+                       Name = name; 
+                       asString = ((Namespace != null && Namespace.Length > 0)?(Namespace + "."):"") + Name;
+               }
+               public override string ToString() { return asString; }
        }
 
        private static StandardModule[] standardModules;
@@ -552,7 +552,7 @@ public class TypeManager {
                                        return t;
                        }
 
-                       foreach (ModuleBuilder mb in modules) {
+                       foreach (System.Reflection.Module mb in modules) {
                                t = mb.GetType (name, false, true);
                                if (t != null){
                                        return t;
@@ -564,6 +564,7 @@ public class TypeManager {
                return null;
        }
 
+       static CaseInsensitiveHashtable negative_hits;
        //
        // This function is used when you want to avoid the lookups, and want to go
        // directly to the source.  This will use the cache.
@@ -578,11 +579,13 @@ public class TypeManager {
                if (t != null)
                        return t;
 
+               if (negative_hits.Contains (name))
+                       return null;
                t = LookupTypeReflection (name);
                if (t == null)
-                       return null;
-
-               types [name] = t;
+                       negative_hits [name] = null;
+               else
+                       types [name] = t;
                return t;
        }
        
@@ -603,6 +606,9 @@ public class TypeManager {
                if (t != null)
                        return t;
 
+               if (negative_hits.Contains (name))
+                       return null;
+
                //
                // Optimization: ComposedCast will work with an existing type, and might already have the
                // full name of the type, so the full system lookup can probably be avoided.
@@ -614,11 +620,16 @@ public class TypeManager {
                for (int n = 1; n <= count; n++){
                        string top_level_type = String.Join (".", elements, 0, n);
 
+                       if (negative_hits.Contains (top_level_type))
+                               continue;
+
                        t = (Type) types [top_level_type];
                        if (t == null){
                                t = LookupTypeReflection (top_level_type);
-                               if (t == null)
+                               if (t == null){
+                                       negative_hits [top_level_type] = null;
                                        continue;
+                               }
                        }
                        
                        if (count == n){
@@ -627,11 +638,14 @@ public class TypeManager {
                        } 
                        
                        string newt = top_level_type + "+" + String.Join ("+", elements, n, count - n);
-                       t = LookupTypeDirect (newt);
-                       if (t != null)
-                               types [newt] = t;
+                       t = LookupTypeReflection (newt);
+                       if (t == null)
+                               negative_hits [name] = null;
+                       else
+                               types [name] = t;
                        return t;
                }
+               negative_hits [name] = null;
                return null;
        }
 
@@ -652,7 +666,7 @@ public class TypeManager {
                        }
                }
 
-               foreach (ModuleBuilder mb in modules) {
+               foreach (System.Reflection.Module mb in modules) {
                        foreach (Type t in mb.GetTypes ()) {
                                string ns = t.Namespace;
 
@@ -1020,6 +1034,8 @@ public class TypeManager {
                unverifiable_code_ctor = GetConstructor (
                        unverifiable_code_type, void_arg);
                
+               default_member_ctor = GetConstructor (
+                       default_member_type, string_);
        }
 
        const BindingFlags instance_and_static = BindingFlags.Static | BindingFlags.Instance;
@@ -1367,12 +1383,33 @@ public class TypeManager {
        {
                if (indexer_arguments.Contains (indexer))
                        return (Type []) indexer_arguments [indexer];
-               else if (indexer is PropertyBuilder)
+               else {
                        // If we're a PropertyBuilder and not in the
                        // 'indexer_arguments' hash, then we're a property and
                        // not an indexer.
-                       return NoTypes;
-               else {
+
+                       MethodInfo mi = indexer.GetSetMethod (true);
+                       if (mi == null) {
+                               mi = indexer.GetGetMethod (true);
+                               if (mi == null)
+                                       return NoTypes;
+                       }
+
+                       ParameterInfo [] pi = mi.GetParameters ();
+                       if (pi == null)
+                               return NoTypes;
+
+                       int c = pi.Length;
+                       Type [] types = new Type [c];
+                       
+                       for (int i = 0; i < c; i++)
+                               types [i] = pi [i].ParameterType;
+
+                       indexer_arguments.Add (indexer, types);
+                       return types;
+               }
+               /*else 
+               {
                        ParameterInfo [] pi = indexer.GetIndexParameters ();
                        // Property, not an indexer.
                        if (pi == null)
@@ -1385,7 +1422,7 @@ public class TypeManager {
 
                        indexer_arguments.Add (indexer, types);
                        return types;
-               }
+               }*/
        }
        
        // <remarks>
@@ -1532,24 +1569,35 @@ public class TypeManager {
        ///   This expands in context like: IA; IB : IA; IC : IA, IB; the interface "IC" to
        ///   be IA, IB, IC.
        /// </remarks>
+
        public static Type [] ExpandInterfaces (Type [] base_interfaces)
-       {
-               ArrayList new_ifaces = new ArrayList ();
+       {       
+               ArrayList new_ifaces = new ArrayList();
+               ExpandAllInterfaces (base_interfaces, ref new_ifaces);
+               Type [] ret = new Type [new_ifaces.Count];
+               new_ifaces.CopyTo (ret, 0);
                
-               foreach (Type iface in base_interfaces){
+               return ret;
+       }
+
+       /// <summary>
+       ///   Recursively finds out each base interface in case  
+       ///   of multiple inheritance
+       /// </summary>
+       public static void ExpandAllInterfaces 
+                       (Type [] base_interfaces, ref ArrayList new_ifaces)
+       {
+               foreach (Type iface in base_interfaces) {
                        if (!new_ifaces.Contains (iface))
                                new_ifaces.Add (iface);
                        
                        Type [] implementing = TypeManager.GetInterfaces (iface);
-                       
-                       foreach (Type imp in implementing){
-                               if (!new_ifaces.Contains (imp))
-                                       new_ifaces.Add (imp);
-                       }
+                       //
+                       // Incase any base interface is present call this function again
+                       //
+                       if (implementing.Length != 0)
+                               ExpandAllInterfaces (implementing, ref new_ifaces);
                }
-               Type [] ret = new Type [new_ifaces.Count];
-               new_ifaces.CopyTo (ret, 0);
-               return ret;
        }
                
        /// <summary>
@@ -1821,17 +1869,17 @@ public class TypeManager {
                        if (t.IsInterface) {
                                Interface i = LookupInterface (t);
 
-                               if ((i == null) || (i.IndexerName == null))
+                               if ((i == null) || (i.DefaultPropName == null))
                                        return "Item";
 
-                               return i.IndexerName;
+                               return i.DefaultPropName;
                        } else {
                                TypeContainer tc = LookupTypeContainer (t);
 
-                               if ((tc == null) || (tc.IndexerName == null))
+                               if ((tc == null) || (tc.DefaultPropName == null))
                                        return "Item";
 
-                               return tc.IndexerName;
+                               return tc.DefaultPropName;
                        }
                }