2008-10-14 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / namespace.cs
index b5514618805617419adb2d3d9e0b114fd675e163..b18731ea7e2163e203c6fbd8df3f42cb949e8f6b 100644 (file)
@@ -16,14 +16,19 @@ using System.Reflection;
 namespace Mono.CSharp {
 
        public class RootNamespace : Namespace {
+               //
+               // Points to Mono's GetNamespaces method, an
+               // optimization when running on Mono to fetch all the
+               // namespaces in an assembly
+               //
                static MethodInfo get_namespaces_method;
 
                string alias_name;
-               Assembly referenced_assembly;
+               protected Assembly [] referenced_assemblies;
 
                Hashtable all_namespaces;
 
-               static Hashtable root_namespaces;
+               static ListDictionary root_namespaces;
                public static GlobalRootNamespace Global;
                
                static RootNamespace ()
@@ -35,33 +40,60 @@ namespace Mono.CSharp {
 
                public static void Reset ()
                {
-                       root_namespaces = new Hashtable ();
+                       root_namespaces = new ListDictionary ();
                        Global = new GlobalRootNamespace ();
                        root_namespaces ["global"] = Global;
                }
 
-               protected RootNamespace (string alias_name, Assembly assembly)
+               protected RootNamespace (string alias_name)
                        : base (null, String.Empty)
                {
                        this.alias_name = alias_name;
-                       referenced_assembly = assembly;
+                       referenced_assemblies = new Assembly [0];
 
                        all_namespaces = new Hashtable ();
                        all_namespaces.Add ("", this);
+               }
+
+               public void AddAssemblyReference (Assembly a)
+               {
+                       foreach (Assembly assembly in referenced_assemblies) {
+                               if (a == assembly)
+                                       return;
+                       }
+
+                       // How to test whether attribute exists without loading the assembly :-(
+#if NET_2_1
+                       const string SystemCore = "System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"; 
+#else
+                       const string SystemCore = "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; 
+#endif
+                       if (TypeManager.extension_attribute_type == null &&
+                               a.FullName == SystemCore) {
+                               TypeManager.extension_attribute_type = a.GetType("System.Runtime.CompilerServices.ExtensionAttribute");
+                       }
 
-                       if (referenced_assembly != null)
-                               ComputeNamespaces (this.referenced_assembly);
+                       int top = referenced_assemblies.Length;
+                       Assembly [] n = new Assembly [top + 1];
+                       referenced_assemblies.CopyTo (n, 0);
+                       n [top] = a;
+                       referenced_assemblies = n;
                }
 
-               public static void DefineRootNamespace (string name, Assembly assembly)
+               public static void DefineRootNamespace (string alias, Assembly assembly)
                {
-                       if (name == "global") {
+                       if (alias == "global") {
                                NamespaceEntry.Error_GlobalNamespaceRedefined (Location.Null);
                                return;
                        }
-                       RootNamespace retval = GetRootNamespace (name);
-                       if (retval == null || retval.referenced_assembly != assembly)
-                               root_namespaces [name] = new RootNamespace (name, assembly);
+
+                       RootNamespace retval = GetRootNamespace (alias);
+                       if (retval == null) {
+                               retval = new RootNamespace (alias);
+                               root_namespaces.Add (alias, retval);
+                       }
+
+                       retval.AddAssemblyReference (assembly);
                }
 
                public static RootNamespace GetRootNamespace (string name)
@@ -69,9 +101,44 @@ namespace Mono.CSharp {
                        return (RootNamespace) root_namespaces [name];
                }
 
+               public static void ComputeNamespaces ()
+               {
+                       foreach (RootNamespace rn in root_namespaces.Values) {
+                               foreach (Assembly a in rn.referenced_assemblies) {
+                                       try {
+                                               rn.ComputeNamespaces (a);
+                                       } catch (TypeLoadException e) {
+                                               Report.Error (11, Location.Null, e.Message);
+                                       } catch (System.IO.FileNotFoundException) {
+                                               Report.Error (12, Location.Null, "An assembly `{0}' is used without being referenced",
+                                                       a.FullName);
+                                       }
+                               }
+                       }
+               }
+
                public virtual Type LookupTypeReflection (string name, Location loc)
                {
-                       return GetTypeInAssembly (referenced_assembly, name);
+                       Type found_type = null;
+
+                       foreach (Assembly a in referenced_assemblies) {
+                               Type t = GetTypeInAssembly (a, name);
+                               if (t == null)
+                                       continue;
+
+                               if (found_type == null) {
+                                       found_type = t;
+                                       continue;
+                               }
+
+                               Report.SymbolRelatedToPreviousError (found_type);
+                               Report.SymbolRelatedToPreviousError (t);
+                               Report.Error (433, loc, "The imported type `{0}' is defined multiple times", name);
+
+                               return found_type;
+                       }
+
+                       return found_type;
                }
 
                public void RegisterNamespace (Namespace child)
@@ -103,17 +170,6 @@ namespace Mono.CSharp {
 
                protected void ComputeNamespaces (Assembly assembly)
                {
-                       // How to test whether attribute exists without loading the assembly :-(
-#if NET_2_1
-                       const string SystemCore = "System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"; 
-#else
-                       const string SystemCore = "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; 
-#endif
-                       if (TypeManager.extension_attribute_type == null &&
-                               assembly.FullName == SystemCore) {
-                               TypeManager.extension_attribute_type = assembly.GetType("System.Runtime.CompilerServices.ExtensionAttribute");
-                       }
                        bool contains_extension_methods = TypeManager.extension_attribute_type != null &&
                                        assembly.IsDefined(TypeManager.extension_attribute_type, false);
  
@@ -167,39 +223,21 @@ namespace Mono.CSharp {
        }
 
        public class GlobalRootNamespace : RootNamespace {
-               Assembly [] assemblies;
                Module [] modules;
 
                public GlobalRootNamespace ()
-                       : base ("global", null)
+                       : base ("global")
                {
-                       assemblies = new Assembly [0];
                }
 
                public Assembly [] Assemblies {
-                       get { return assemblies; }
+                   get { return referenced_assemblies; }
                }
 
                public Module [] Modules {
                        get { return modules; }
                }
 
-               public void AddAssemblyReference (Assembly a)
-               {
-                       foreach (Assembly assembly in assemblies) {
-                               if (a == assembly)
-                                       return;
-                       }
-
-                       int top = assemblies.Length;
-                       Assembly [] n = new Assembly [top + 1];
-                       assemblies.CopyTo (n, 0);
-                       n [top] = a;
-                       assemblies = n;
-
-                       ComputeNamespaces (a);
-               }
-
                public void AddModuleReference (Module m)
                {
                        int top = modules != null ? modules.Length : 0;
@@ -224,24 +262,7 @@ namespace Mono.CSharp {
 
                public override Type LookupTypeReflection (string name, Location loc)
                {
-                       Type found_type = null;
-               
-                       foreach (Assembly a in assemblies) {
-                               Type t = GetTypeInAssembly (a, name);
-                               if (t == null)
-                                       continue;
-                                       
-                               if (found_type == null) {
-                                       found_type = t;
-                                       continue;
-                               }
-
-                               Report.SymbolRelatedToPreviousError (found_type);
-                               Report.SymbolRelatedToPreviousError (t);
-                               Report.Error (433, loc, "The imported type `{0}' is defined multiple times", name);
-                                       
-                               return found_type;
-                       }
+                       Type found_type = base.LookupTypeReflection (name, loc);
 
                        if (modules != null) {
                                foreach (Module module in modules) {
@@ -416,6 +437,11 @@ namespace Mono.CSharp {
                        return ns;
                }
 
+               public bool HasDefinition (string name)
+               {
+                       return declspaces != null && declspaces [name] != null;
+               }
+
                TypeExpr LookupType (string name, Location loc)
                {
                        if (cached_types.Contains (name))
@@ -436,6 +462,15 @@ namespace Mono.CSharp {
                                        //
                                        tdecl.DefineType ();
                                        t = tdecl.TypeBuilder;
+
+                                       if (RootContext.EvalMode){
+                                               // Replace the TypeBuilder with a System.Type, as
+                                               // Reflection.Emit fails otherwise (we end up pretty
+                                               // much with Random type definitions later on).
+                                               Type tt = t.Assembly.GetType (t.Name);
+                                               if (tt != null)
+                                                       t = tt;
+                                       }
                                }
                        }
                        string lookup = t != null ? t.FullName : (fullname.Length == 0 ? name : fullname + "." + name);
@@ -488,7 +523,7 @@ namespace Mono.CSharp {
                /// 
                /// Looks for extension method in this namespace
                /// 
-               public ArrayList LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name, NamespaceEntry ns)
+               public ArrayList LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name)
                {
                        ArrayList found = null;
 
@@ -539,6 +574,11 @@ namespace Mono.CSharp {
                        declspaces.Add (name, ds);
                }
 
+               public void RemoveDeclSpace (string name)
+               {
+                       declspaces.Remove (name);
+               }
+               
                /// <summary>
                ///   The qualified name of the current namespace
                /// </summary>
@@ -604,6 +644,11 @@ namespace Mono.CSharp {
                                }
                                return resolved;
                        }
+
+                       public override string ToString ()
+                       {
+                               return Name;
+                       }
                }
 
                class UsingAliasEntry {
@@ -627,6 +672,12 @@ namespace Mono.CSharp {
 
                                return fne;
                        }
+
+                       public override string ToString ()
+                       {
+                               return Alias;
+                       }
+                       
                }
 
                class LocalUsingAliasEntry : UsingAliasEntry {
@@ -641,12 +692,14 @@ namespace Mono.CSharp {
 
                        public override FullNamedExpression Resolve (IResolveContext rc)
                        {
-                               if (resolved != null)
+                               if (resolved != null || value == null)
                                        return (FullNamedExpression)resolved;
 
                                resolved = value.GetTypeExpression ().ResolveAsTypeStep (rc, false);
-                               if (resolved == null)
+                               if (resolved == null) {
+                                       value = null;
                                        return null;
+                               }
 
                                // FIXME: This is quite wrong, the accessibility is not global
                                if (resolved.Type != null) {
@@ -661,11 +714,16 @@ namespace Mono.CSharp {
 
                                return (FullNamedExpression)resolved;
                        }
+
+                       public override string ToString ()
+                       {
+                               return String.Format ("{0} = {1}", Alias, value.GetSignatureForError ());
+                       }
                }
 
                Namespace ns;
                NamespaceEntry parent, implicit_parent;
-               SourceFile file;
+               CompilationUnit file;
                int symfile_id;
 
                // Namespace using import block
@@ -686,7 +744,7 @@ namespace Mono.CSharp {
                        entries = new ArrayList ();
                }
 
-               public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name)
+               public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name)
                {
                        this.parent = parent;
                        this.file = file;
@@ -701,7 +759,7 @@ namespace Mono.CSharp {
                        SlaveDeclSpace = new RootDeclSpace (this);
                }
 
-               private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns, bool slave)
+               private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
                {
                        this.parent = parent;
                        this.file = file;
@@ -710,6 +768,67 @@ namespace Mono.CSharp {
                        this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
                }
 
+               //
+               // Populates the Namespace with some using declarations, used by the
+               // eval mode. 
+               //
+               public void Populate (ArrayList source_using_aliases, ArrayList source_using_clauses)
+               {
+                       foreach (UsingAliasEntry uae in source_using_aliases){
+                               if (using_aliases == null)
+                                       using_aliases = new ArrayList ();
+                               
+                               using_aliases.Add (uae);
+                       }
+
+                       foreach (UsingEntry ue in source_using_clauses){
+                               if (using_clauses == null)
+                                       using_clauses = new ArrayList ();
+                               
+                               using_clauses.Add (ue);
+                       }
+               }
+
+               //
+               // Extracts the using alises and using clauses into a couple of
+               // arrays that might already have the same information;  Used by the
+               // C# Eval mode.
+               //
+               public void Extract (ArrayList out_using_aliases, ArrayList out_using_clauses)
+               {
+                       if (using_aliases != null){
+                               foreach (UsingAliasEntry uae in using_aliases){
+                                       bool replaced = false;
+                                       
+                                       for (int i = 0; i < out_using_aliases.Count; i++){
+                                               UsingAliasEntry out_uea = (UsingAliasEntry) out_using_aliases [i];
+                                               
+                                               if (out_uea.Alias == uae.Alias){
+                                                       out_using_aliases [i] = uae;
+                                                       replaced = true;
+                                                       break;
+                                               }
+                                       }
+                                       if (!replaced)
+                                               out_using_aliases.Add (uae);
+                               }
+                       }
+
+                       if (using_clauses != null){
+                               foreach (UsingEntry ue in using_clauses){
+                                       bool found = false;
+                                       
+                                       foreach (UsingEntry out_ue in out_using_clauses)
+                                               if (out_ue.Name == ue.Name){
+                                                       found = true;
+                                                       break;
+                                               }
+                                       if (!found)
+                                               out_using_clauses.Add (ue);
+                               }
+                       }
+               }
+               
                //
                // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
                // resolved as if the immediately containing namespace body has no using-directives.
@@ -840,13 +959,13 @@ namespace Mono.CSharp {
                {
                        ArrayList candidates = null;
                        if (currentClass != null) {
-                               candidates = ns.LookupExtensionMethod (extensionType, currentClass, name, this);
+                               candidates = ns.LookupExtensionMethod (extensionType, currentClass, name);
                                if (candidates != null)
                                        return new ExtensionMethodGroupExpr (candidates, this, extensionType, loc);
                        }
 
                        foreach (Namespace n in GetUsingTable ()) {
-                               ArrayList a = n.LookupExtensionMethod (extensionType, null, name, this);
+                               ArrayList a = n.LookupExtensionMethod (extensionType, null, name);
                                if (a == null)
                                        continue;
 
@@ -867,7 +986,7 @@ namespace Mono.CSharp {
                        //
                        Namespace parent_ns = ns.Parent;
                        do {
-                               candidates = parent_ns.LookupExtensionMethod (extensionType, null, name, parent);
+                               candidates = parent_ns.LookupExtensionMethod (extensionType, null, name);
                                if (candidates != null)
                                        return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
 
@@ -921,19 +1040,34 @@ namespace Mono.CSharp {
                        // Check whether it's in the namespace.
                        //
                        FullNamedExpression fne = ns.Lookup (ds, name, loc);
-                       if (fne != null)
-                               return fne;
 
                        //
                        // Check aliases. 
                        //
                        if (using_aliases != null) {
                                foreach (UsingAliasEntry ue in using_aliases) {
-                                       if (ue.Alias == name)
+                                       if (ue.Alias == name) {
+                                               if (fne != null) {
+                                                       if (Doppelganger != null) {
+                                                               // TODO: Namespace has broken location
+                                                               //Report.SymbolRelatedToPreviousError (fne.Location, null);
+                                                               Report.SymbolRelatedToPreviousError (ue.Location, null);
+                                                               Report.Error (576, loc,
+                                                                       "Namespace `{0}' contains a definition with same name as alias `{1}'",
+                                                                       GetSignatureForError (), name);
+                                                       } else {
+                                                               return fne;
+                                                       }
+                                               }
+
                                                return ue.Resolve (Doppelganger);
+                                       }
                                }
                        }
 
+                       if (fne != null)
+                               return fne;
+
                        if (IsImplicit)
                                return null;
 
@@ -991,10 +1125,10 @@ namespace Mono.CSharp {
                                        if (using_clauses != null) {
                                                using_list = new string [using_clauses.Count];
                                                for (int i = 0; i < using_clauses.Count; i++)
-                                                       using_list [i] = ((UsingEntry) using_clauses [i]).MemberName.GetTypeName ();
+                                                       using_list [i] = ((UsingEntry) using_clauses [i]).MemberName.GetName ();
                                        }
 
-                                       symfile_id = SymbolWriter.DefineNamespace (ns.Name, file.SourceFileEntry, using_list, parent_id);
+                                       symfile_id = SymbolWriter.DefineNamespace (ns.Name, file.CompileUnitEntry, using_list, parent_id);
                                }
                                return symfile_id;
                        }
@@ -1017,6 +1151,11 @@ namespace Mono.CSharp {
 
                public static void Error_NamespaceNotFound (Location loc, string name)
                {
+                       if (RootContext.EvalMode){
+                               // Do not report this, it might be an error on the eval side, and we are lax there.
+                               return;
+                       }
+                       
                        Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
                                name);