Anirban Bhattacharjee <banirban@novell.com>
[mono.git] / mcs / mbas / typemanager.cs
index 18e1c04180510018f3bd4e2ef759a578b965f914..9d34c5882953cdecaaed91f2c7abbb95975d71fd 100644 (file)
@@ -160,7 +160,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
@@ -201,7 +207,7 @@ public class TypeManager {
        static Hashtable indexer_arguments;
 
        // <remarks>
-       //   Maybe `method_arguments' should be replaced and only
+       //   Maybe 'method_arguments' should be replaced and only
        //   method_internal_params should be kept?
        // <remarks>
        static Hashtable method_internal_params;
@@ -328,14 +334,14 @@ public class TypeManager {
                                //
                                // This probably never happens, as we catch this before
                                //
-                               Report.Error (-17, "The type `" + name + "' has already been defined.");
+                               Report.Error (-17, "The type '" + name + "' has already been defined.");
                                return;
                        }
 
                        tc = builder_to_declspace [t] as TypeContainer;
                        
                        Report.Warning (
-                               1595, "The type `" + name + "' is defined in an existing assembly;"+
+                               1595, "The type '" + name + "' is defined in an existing assembly;"+
                                " Using the new definition from: " + tc.Location);
                        Report.Warning (1595, "Previously defined in: " + prev.Assembly.FullName);
                        
@@ -393,8 +399,8 @@ public class TypeManager {
        }
 
        /// <summary>
-       ///   Returns the TypeContainer whose Type is `t' or null if there is no
-       ///   TypeContainer for `t' (ie, the Type comes from a library)
+       ///   Returns the TypeContainer whose Type is 't' or null if there is no
+       ///   TypeContainer for 't' (ie, the Type comes from a library)
        /// </summary>
        public static TypeContainer LookupTypeContainer (Type t)
        {
@@ -444,15 +450,20 @@ public class TypeManager {
                
                n [top] = a;
                assemblies = n;
+
+               foreach(Type type in a.GetTypes()) {
+                       if (type.IsPublic ) // && type. attributed as standard module
+                               AddStandardModule(type);
+               }
        }
 
        /// <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);
@@ -460,6 +471,78 @@ 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 static StandardModule[] standardModules;
+
+       /// <summary>
+       ///  Registers a new 'standard module' to lookup short-qualified or unqualified members
+       /// </summary>
+       internal static void AddStandardModule(Module module)
+       {
+               int top = standardModules != null ? standardModules.Length : 0;
+               StandardModule [] n = new StandardModule [top + 1];
+
+               if (standardModules != null)
+                       standardModules.CopyTo (n, 0);
+               string name = module.Name;
+               int split = name.LastIndexOf('.'); 
+               if (split > 0)
+                       name = name.Substring(split+1);
+               n [top] = new StandardModule(module.Namespace.Name, name); 
+               // Console.WriteLine("Standard Module added: " + n [top]);
+               standardModules = n;
+       }
+
+       /// 
+       ///  Registers a existing 'standard module' to lookup short-qualified or unqualified members
+       /// 
+       private static void AddStandardModule(Type type)
+       {
+           object[] attributes = type.GetCustomAttributes(false);
+           for (int i = 0; i < attributes.Length; i ++) {
+                       if (attributes[i].ToString() == "Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute") {
+                               int top = standardModules != null ? standardModules.Length : 0;
+                               StandardModule [] n = new StandardModule [top + 1];
+
+                               if (standardModules != null)
+                                       standardModules.CopyTo (n, 0);
+                               n [top] = new StandardModule(type.Namespace, type.Name) ;
+                               standardModules = n;
+                               return;
+                       }
+        }
+       }
+
+       //
+       // 
+       //
+       public static Type[] GetPertinentStandardModules(params string[] namespaces)
+       {
+               ArrayList list = new ArrayList();
+               foreach(string Namespace in namespaces)
+               { 
+                       for(int i = 0; i < standardModules.Length; i++) {
+                               if (standardModules[i].Namespace == Namespace) {
+                                       string name = standardModules[i].ToString();
+                                       Type t = LookupType(name);
+                                       if (t == null) {
+                                               System.Console.WriteLine("Could not find standard module '" + name + "'"); 
+                                       }
+                                       else {
+                                               list.Add(t);
+                                       }
+                               }
+                       }
+               }
+               return (Type[])list.ToArray(typeof(Type));
+       }
+
        //
        // Low-level lookup, cache-less
        //
@@ -467,17 +550,21 @@ public class TypeManager {
        {
                Type t;
 
-               foreach (Assembly a in assemblies){
-                       t = a.GetType (name, false, true);
-                       if (t != null)
-                               return t;
-               }
+               try {
+                       foreach (Assembly a in assemblies){
+                               t = a.GetType (name, false, true);
+                               if (t != null)
+                                       return t;
+                       }
 
-               foreach (ModuleBuilder mb in modules) {
-                       t = mb.GetType (name, false, true);
-                       if (t != null){
-                               return t;
+                       foreach (System.Reflection.Module mb in modules) {
+                               t = mb.GetType (name, false, true);
+                               if (t != null){
+                                       return t;
+                               }
                        }
+               } catch (Exception e) {
+                       System.Console.WriteLine("\nERROR: " + e.ToString() + "WHILE EXECUTING LookupTypeReflection(\"" + name + "\")\n");
                }
                return null;
        }
@@ -560,8 +647,8 @@ public class TypeManager {
        {
                CaseInsensitiveHashtable namespaces = new CaseInsensitiveHashtable ();
 
-               foreach (Assembly a in assemblies){
-                       foreach (Type t in a.GetTypes ()){
+               foreach (Assembly a in assemblies) {
+                       foreach (Type t in a.GetTypes ()) {
                                string ns = t.Namespace;
 
                                if (namespaces.Contains (ns))
@@ -570,8 +657,8 @@ public class TypeManager {
                        }
                }
 
-               foreach (ModuleBuilder mb in modules){
-                       foreach (Type t in mb.GetTypes ()){
+               foreach (System.Reflection.Module mb in modules) {
+                       foreach (Type t in mb.GetTypes ()) {
                                string ns = t.Namespace;
 
                                if (namespaces.Contains (ns))
@@ -579,6 +666,7 @@ public class TypeManager {
                                namespaces [ns] = ns;
                        }
                }
+
                return namespaces;
        }
        
@@ -589,9 +677,7 @@ public class TypeManager {
        {
                return Regex.Replace (t.FullName, 
                        @"^System\." +
-                       @"(Int32|UInt32|Int16|Uint16|Int64|UInt64|" +
-                       @"Single|Double|Char|Decimal|Byte|SByte|Object|" +
-                       @"Boolean|String|Void|DateTime)" +
+                       @"(Int32|Int16|Int64|Single|Double|Char|Decimal|Byte|Object|Boolean|String|DateTime)" +
                        @"(\W+|\b)", 
                        new MatchEvaluator (MonoBASIC_NameMatch));
        }       
@@ -600,14 +686,9 @@ public class TypeManager {
        {
                string s = match.Groups [1].Captures [0].Value;
                return s.ToLower ().
-               Replace ("int32", "int").
-               Replace ("uint32", "uint").
+               Replace ("int32", "integer").
                Replace ("int16", "short").
-               Replace ("uint16", "ushort").
                Replace ("int64", "long").
-               Replace ("uint64", "ulong").
-               Replace ("single", "float").
-               Replace ("boolean", "bool").
                Replace ("datetime", "date")
                + match.Groups [2].Captures [0].Value;
        }
@@ -650,7 +731,7 @@ public class TypeManager {
                Type t = LookupType (name);
 
                if (t == null){
-                       Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
+                       Report.Error (518, "The predefined type '" + name + "' is not defined or imported");
                        Environment.Exit (0);
                }
 
@@ -658,8 +739,8 @@ public class TypeManager {
        }
 
        /// <summary>
-       ///   Returns the MethodInfo for a method named `name' defined
-       ///   in type `t' which takes arguments of types `args'
+       ///   Returns the MethodInfo for a method named 'name' defined
+       ///   in type 't' which takes arguments of types 'args'
        /// </summary>
        static MethodInfo GetMethod (Type t, string name, Type [] args)
        {
@@ -672,13 +753,13 @@ public class TypeManager {
                list = FindMembers (t, MemberTypes.Method, instance_and_static | BindingFlags.Public| BindingFlags.IgnoreCase,
                                    signature_filter, sig);
                if (list.Count == 0) {
-                       Report.Error (-19, "Can not find the core function `" + name + "'");
+                       Report.Error (-19, "Can not find the core function '" + name + "'");
                        return null;
                }
 
                MethodInfo mi = list [0] as MethodInfo;
                if (mi == null) {
-                       Report.Error (-19, "Can not find the core function `" + name + "'");
+                       Report.Error (-19, "Can not find the core function '" + name + "'");
                        return null;
                }
 
@@ -700,13 +781,13 @@ public class TypeManager {
                                    instance_and_static | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.IgnoreCase,
                                    signature_filter, sig);
                if (list.Count == 0){
-                       Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
+                       Report.Error (-19, "Can not find the core constructor for type '" + t.Name + "'");
                        return null;
                }
 
                ConstructorInfo ci = list [0] as ConstructorInfo;
                if (ci == null){
-                       Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
+                       Report.Error (-19, "Can not find the core constructor for type '" + t.Name + "'");
                        return null;
                }
 
@@ -960,7 +1041,7 @@ public class TypeManager {
                DeclSpace decl = (DeclSpace) builder_to_declspace [t];
                bf |= BindingFlags.IgnoreCase;
                //
-               // `builder_to_declspace' contains all dynamic types.
+               // 'builder_to_declspace' contains all dynamic types.
                //
                if (decl != null) {
                        MemberList list;
@@ -1017,7 +1098,7 @@ public class TypeManager {
 
        /// <summary>
        ///   This method is only called from within MemberLookup.  It tries to use the member
-       ///   cache if possible and falls back to the normal FindMembers if not.  The `used_cache'
+       ///   cache if possible and falls back to the normal FindMembers if not.  The 'used_cache'
        ///   flag tells the caller whether we used the cache or not.  If we used the cache, then
        ///   our return value will already contain all inherited members and the caller don't need
        ///   to check base classes and interfaces anymore.
@@ -1037,7 +1118,7 @@ public class TypeManager {
                }
 
                //
-               // If this is a dynamic type, it's always in the `builder_to_declspace' hash table
+               // If this is a dynamic type, it's always in the 'builder_to_declspace' hash table
                // and we can ask the DeclSpace for the MemberCache.
                //
                if (t is TypeBuilder) {
@@ -1163,7 +1244,7 @@ public class TypeManager {
        }
 
        //
-       // Checks whether `type' is a subclass or nested child of `parent'.
+       // Checks whether 'type' is a subclass or nested child of 'parent'.
        //
        public static bool IsSubclassOrNestedChildOf (Type type, Type parent)
        {
@@ -1179,7 +1260,7 @@ public class TypeManager {
        }
 
        //
-       // Checks whether `type' is a nested child of `parent'.
+       // Checks whether 'type' is a nested child of 'parent'.
        //
        public static bool IsNestedChildOf (Type type, Type parent)
        {
@@ -1293,7 +1374,7 @@ public class TypeManager {
                        return (Type []) indexer_arguments [indexer];
                else if (indexer is PropertyBuilder)
                        // If we're a PropertyBuilder and not in the
-                       // `indexer_arguments' hash, then we're a property and
+                       // 'indexer_arguments' hash, then we're a property and
                        // not an indexer.
                        return NoTypes;
                else {
@@ -1434,7 +1515,7 @@ public class TypeManager {
 
                        return (MethodInfo) de.Second;
                } else
-                       return pi.GetSetMethod ();
+                       return pi.GetSetMethod (true);
        }
 
        static public MethodInfo GetPropertySetter (PropertyInfo pi)
@@ -1444,7 +1525,7 @@ public class TypeManager {
 
                        return (MethodInfo) de.First;
                } else
-                       return pi.GetGetMethod ();
+                       return pi.GetGetMethod (true);
        }
 
        /// <summary>
@@ -1477,7 +1558,7 @@ public class TypeManager {
        }
                
        /// <summary>
-       ///   This function returns the interfaces in the type `t'.  Works with
+       ///   This function returns the interfaces in the type 't'.  Works with
        ///   both types and TypeBuilders.
        /// </summary>
        public static Type [] GetInterfaces (Type t)
@@ -1527,7 +1608,7 @@ public class TypeManager {
                //
                // FIXME OPTIMIZATION:
                // as soon as we hit a non-TypeBuiler in the interface
-               // chain, we could return, as the `Type.GetInterfaces'
+               // chain, we could return, as the 'Type.GetInterfaces'
                // will return all the interfaces implement by the type
                // or its parents.
                //
@@ -1559,7 +1640,7 @@ public class TypeManager {
                NumberFormatInfo provider = ci.NumberFormat;
 
                //
-               // We must use Type.Equals() here since `conversionType' is
+               // We must use Type.Equals() here since 'conversionType' is
                // the TypeBuilder created version of a system type and not
                // the system type itself.  You cannot use Type.GetTypeCode()
                // on such a type - it'd always return TypeCode.Object.
@@ -1734,10 +1815,10 @@ public class TypeManager {
        ///   Returns the name of the indexer in a given type.
        /// </summary>
        /// <remarks>
-       ///   The default is not always `Item'.  The user can change this behaviour by
+       ///   The default is not always 'Item'.  The user can change this behaviour by
        ///   using the DefaultMemberAttribute in the class.
        ///
-       ///   For example, the String class indexer is named `Chars' not `Item' 
+       ///   For example, the String class indexer is named 'Chars' not 'Item' 
        /// </remarks>
        public static string IndexerPropertyName (Type t)
        {
@@ -1809,7 +1890,7 @@ public class TypeManager {
        }
        
        //
-       // We copy methods from `new_members' into `target_list' if the signature
+       // 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.
@@ -1878,12 +1959,12 @@ public class TypeManager {
                                string method_desc = TypeManager.MonoBASIC_Signature (mb);
 
                                if (oa.IsError) {
-                                       Report.Error (619, loc, "Method `" + method_desc +
-                                                     "' is obsolete: `" + oa.Message + "'");
+                                       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 + "'");
+                                       Report.Warning (618, loc, "Method '" + method_desc +
+                                                       "' is obsolete: '" + oa.Message + "'");
 
                                flags |= MethodFlags.IsObsolete;
 
@@ -1936,7 +2017,7 @@ public class TypeManager {
        static internal bool FilterWithClosure (MemberInfo m, object filter_criteria)
        {
                //
-               // Hack: we know that the filter criteria will always be in the `closure'
+               // Hack: we know that the filter criteria will always be in the 'closure'
                // fields. 
                //
 
@@ -1948,7 +2029,7 @@ public class TypeManager {
                        return true;
 
                //
-               // Ugly: we need to find out the type of `m', and depending
+               // Ugly: we need to find out the type of 'm', and depending
                // on this, tell whether we accept or not
                //
                if (m is MethodBase){
@@ -2048,10 +2129,10 @@ public class TypeManager {
        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'.
+       // 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'
+       // 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.
@@ -2076,6 +2157,8 @@ public class TypeManager {
                
                ArrayList method_list = null;
                Type current_type = queried_type;
+               if (queried_type == null)
+                       throw new ArgumentNullException("queried_type");
                bool searching = (original_bf & BindingFlags.DeclaredOnly) == 0;
                bool private_ok;
                bool always_ok_flag = false;
@@ -2109,7 +2192,7 @@ public class TypeManager {
                        MemberList list;
 
                        //
-                       // `NonPublic' is lame, because it includes both protected and
+                       // '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.
                        //
@@ -2137,7 +2220,7 @@ public class TypeManager {
                        closure_queried_type = current_type;
 
                        Timer.StopTimer (TimerType.MemberLookup);
-bf |= BindingFlags.IgnoreCase;
+                       bf |= BindingFlags.IgnoreCase;
                        list = MemberLookup_FindMembers (current_type, mt, bf, name, out used_cache);
 
                        Timer.StartTimer (TimerType.MemberLookup);
@@ -2173,7 +2256,7 @@ bf |= BindingFlags.IgnoreCase;
                                continue;
                                
                        //
-                       // Events and types are returned by both `static' and `instance'
+                       // Events and types are returned by both 'static' and 'instance'
                        // searches, which means that our above FindMembers will
                        // return two copies of the same.
                        //