In mcs:
[mono.git] / mcs / gmcs / namespace.cs
index 7e019c181b0528962c2d818b6564fedd2566cfb6..be3ccbb59c03bd3e0274ff2b98e361d7fd519a60 100644 (file)
@@ -3,6 +3,7 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
+//   Marek Safar (marek.safar@seznam.cz)
 //
 // (C) 2001 Ximian, Inc.
 //
@@ -13,35 +14,59 @@ using System.Reflection;
 
 namespace Mono.CSharp {
 
-       public class RootNamespace : Namespace
-       {
+       public class RootNamespace : Namespace {
                static MethodInfo get_namespaces_method;
 
+               string alias_name;
                Assembly referenced_assembly;
-               Hashtable cached_namespaces;
+
+               Hashtable all_namespaces;
+
+               static Hashtable root_namespaces;
+               public static GlobalRootNamespace Global;
                
                static RootNamespace ()
                {
                        get_namespaces_method = typeof (Assembly).GetMethod ("GetNamespaces", BindingFlags.Instance | BindingFlags.NonPublic);
+
+                       Reset ();
                }
 
-               //
-               // We access GlobalRoot here to beautify the code
-               //
-               public static GlobalRootNamespace Global {
-                       get {
-                               return GlobalRootNamespace.GlobalRoot;
-                       }
+               public static void Reset ()
+               {
+                       root_namespaces = new Hashtable ();
+                       Global = new GlobalRootNamespace ();
+                       root_namespaces ["global"] = Global;
                }
 
-               public RootNamespace (Assembly assembly) : base (null, String.Empty)
+               protected RootNamespace (string alias_name, Assembly assembly)
+                       : base (null, String.Empty)
                {
-                       this.referenced_assembly = assembly;
-                       this.cached_namespaces = new Hashtable ();
-                       this.cached_namespaces.Add ("", null);
+                       this.alias_name = alias_name;
+                       referenced_assembly = assembly;
+
+                       all_namespaces = new Hashtable ();
+                       all_namespaces.Add ("", this);
 
-                       if (this.referenced_assembly != null)
-                               ComputeNamespacesForAssembly (this.referenced_assembly);
+                       if (referenced_assembly != null)
+                               ComputeNamespaces (this.referenced_assembly);
+               }
+
+               public static void DefineRootNamespace (string name, Assembly assembly)
+               {
+                       if (name == "global") {
+                               // FIXME: Add proper error number
+                               Report.Error (-42, "Cannot define an external alias named `global'");
+                               return;
+                       }
+                       RootNamespace retval = GetRootNamespace (name);
+                       if (retval == null || retval.referenced_assembly != assembly)
+                               root_namespaces [name] = new RootNamespace (name, assembly);
+               }
+
+               public static RootNamespace GetRootNamespace (string name)
+               {
+                       return (RootNamespace) root_namespaces [name];
                }
 
                public virtual Type LookupTypeReflection (string name, Location loc)
@@ -49,37 +74,37 @@ namespace Mono.CSharp {
                        return GetTypeInAssembly (referenced_assembly, name);
                }
 
-               public virtual void RegisterNamespace (Namespace ns)
+               public void RegisterNamespace (Namespace child)
                {
-                       // Do nothing.
+                       if (child != this)
+                               all_namespaces.Add (child.Name, child);
                }
-               
-               protected void ComputeNamespacesForAssembly (Assembly assembly)
+
+               public bool IsNamespace (string name)
                {
-                       if (get_namespaces_method != null) {
-                               string [] namespaces = (string []) get_namespaces_method.Invoke (assembly, null);
-                               foreach (string ns in namespaces) {
-                                       if (ns.Length == 0)
-                                               continue;
+                       return all_namespaces.Contains (name);
+               }
 
-                                       // Method from parent class Namespace
-                                       GetNamespace (ns, true);
-                               }
-                       } else {
-                               //cached_namespaces.Add ("", null);
-                               foreach (Type t in assembly.GetExportedTypes ()) {
-                                       string ns = t.Namespace;
-                                       if (ns == null || cached_namespaces.Contains (ns))
-                                               continue;
+               protected void EnsureNamespace (string dotted_name)
+               {
+                       if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
+                               GetNamespace (dotted_name, true);
+               }
 
-                                       // Method from parent class Namespace
-                                       GetNamespace (ns, true);
-                                       cached_namespaces.Add (ns, null);
-                               }
+               protected void ComputeNamespaces (Assembly assembly)
+               {
+                       if (get_namespaces_method != null) {
+                               string [] namespaces = (string []) get_namespaces_method.Invoke (assembly, null);
+                               foreach (string ns in namespaces)
+                                       EnsureNamespace (ns);
+                               return;
                        }
+
+                       foreach (Type t in assembly.GetExportedTypes ())
+                               EnsureNamespace (t.Namespace);
                }
                
-               protected Type GetTypeInAssembly (Assembly assembly, string name)
+               protected static Type GetTypeInAssembly (Assembly assembly, string name)
                {
                        Type t = assembly.GetType (name);
                        if (t == null)
@@ -88,83 +113,85 @@ namespace Mono.CSharp {
                        if (t.IsPointer)
                                throw new InternalErrorException ("Use GetPointerType() to get a pointer");
                        
+                       
                        TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
-                       if (ta != TypeAttributes.NotPublic && ta != TypeAttributes.NestedPrivate &&
-                                       ta != TypeAttributes.NestedAssembly && ta != TypeAttributes.NestedFamANDAssem)
-                               return t;
+                       if (ta == TypeAttributes.NestedPrivate)
+                               return null;
+                       
+                       if (ta == TypeAttributes.NotPublic ||
+                                       ta == TypeAttributes.NestedAssembly ||
+                                       ta == TypeAttributes.NestedFamANDAssem)
+                               if (!TypeManager.IsFriendAssembly (t.Assembly))
+                                       return null;
 
-                       return null;
+                       return t;
                }
 
-               protected Hashtable CachedNamespaces {
-                       get {
-                               return cached_namespaces;
-                       }
+               public override string ToString ()
+               {
+                       return String.Format ("RootNamespace ({0}::)", alias_name);
                }
 
+               public override string GetSignatureForError ()
+               {
+                       return alias_name + "::";
+               }
        }
 
-       public class GlobalRootNamespace : RootNamespace
-       {
-               static ArrayList all_namespaces;
-               static Hashtable namespaces_map;
-               static Hashtable root_namespaces;
-               internal static GlobalRootNamespace GlobalRoot;
-               
+       public class GlobalRootNamespace : RootNamespace {
                Assembly [] assemblies;
                Module [] modules;
-               
-               public static void Reset ()
+
+               public GlobalRootNamespace ()
+                       : base ("global", null)
                {
-                       all_namespaces = new ArrayList ();
-                       namespaces_map = new Hashtable ();
-                       root_namespaces = new Hashtable ();
-                       GlobalRoot = new GlobalRootNamespace ();
+                       assemblies = new Assembly [0];
                }
-               
-               static GlobalRootNamespace ()
-               {
-                       Reset ();
+
+               public Assembly [] Assemblies {
+                       get { return assemblies; }
                }
 
-               public GlobalRootNamespace () : base (null)
-               {
-                       assemblies = new Assembly [0];
-                       modules = new Module [0];
+               public Module [] Modules {
+                       get { return modules; }
                }
 
-               public void AddAssemblyReference (Assembly assembly)
+               public void AddAssemblyReference (Assembly a)
                {
-                       Assembly [] tmp = new Assembly [assemblies.Length + 1];
-                       Array.Copy (assemblies, 0, tmp, 0, assemblies.Length);
-                       tmp [assemblies.Length] = assembly;
+                       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;
 
-                       assemblies = tmp;
-                       ComputeNamespacesForAssembly (assembly);
+                       ComputeNamespaces (a);
                }
 
-               public void AddModuleReference (Module module)
+               public void AddModuleReference (Module m)
                {
-                       Module [] tmp = new Module [modules.Length + 1];
-                       Array.Copy (modules, 0, tmp, 0, modules.Length);
-                       tmp [modules.Length] = module;
+                       int top = modules != null ? modules.Length : 0;
+                       Module [] n = new Module [top + 1];
+                       if (modules != null)
+                               modules.CopyTo (n, 0);
+                       n [top] = m;
+                       modules = n;
 
-                       modules = tmp;
-                       
-                       if (module != CodeGen.Module.Builder)
-                               ComputeNamespacesForModule (module);
+                       if (m == CodeGen.Module.Builder)
+                               return;
+
+                       foreach (Type t in m.GetTypes ())
+                               EnsureNamespace (t.Namespace);
                }
 
-               void ComputeNamespacesForModule (Module module)
+               public override void Error_NamespaceDoesNotExist(Location loc, string name)
                {
-                       foreach (Type t in module.GetTypes ()) {
-                               string ns = t.Namespace;
-                               if (ns == null || CachedNamespaces.Contains (ns))
-                                       continue;
-
-                               GetNamespace (ns, true);
-                               CachedNamespaces.Add (ns, null);
-                       }
+                       Report.Error (400, loc, "The type or namespace name `{0}' could not be found in the global namespace (are you missing an assembly reference?)",
+                               name);
                }
 
                public override Type LookupTypeReflection (string name, Location loc)
@@ -188,75 +215,27 @@ namespace Mono.CSharp {
                                return found_type;
                        }
 
-                       foreach (Module module in modules) {
-                               Type t = module.GetType (name);
-                               if (t == null)
-                                       continue;
+                       if (modules != null) {
+                               foreach (Module module in modules) {
+                                       Type t = module.GetType (name);
+                                       if (t == null)
+                                               continue;
 
-                               if (found_type == null) {
-                                       found_type = t;
-                                       continue;
-                               }
+                                       if (found_type == null) {
+                                               found_type = t;
+                                               continue;
+                                       }
                                        
-                               Report.SymbolRelatedToPreviousError (t);
-                               Report.SymbolRelatedToPreviousError (found_type);
-                               Report.Warning (436, 2, loc, "Ignoring imported type `{0}' since the current assembly already has a declaration with the same name",
-                                                       TypeManager.CSharpName (t));
-                               return t;
+                                       Report.SymbolRelatedToPreviousError (t);
+                                       Report.SymbolRelatedToPreviousError (found_type);
+                                       Report.Warning (436, 2, loc, "Ignoring imported type `{0}' since the current assembly already has a declaration with the same name",
+                                               TypeManager.CSharpName (t));
+                                       return t;
+                               }
                        }
 
                        return found_type;
                }
-
-               public override void RegisterNamespace (Namespace child)
-               {
-                       all_namespaces.Add (child);
-                       if (namespaces_map.Contains (child.Name))
-                               return;
-                       namespaces_map [child.Name] = true;
-               }
-               
-               public static RootNamespace DefineRootNamespace (string name, Assembly assembly)
-               {
-                       RootNamespace retval = (RootNamespace) root_namespaces [name];
-                       if (retval != null)
-                               return retval;
-
-                       retval = new RootNamespace (assembly);
-                       return retval;
-               }
-               
-               public static bool IsNamespace (string name)
-               {
-                       return namespaces_map [name] != null;
-               }
-
-               public static ArrayList UserDefinedNamespaces {
-                       get { return all_namespaces; }
-               }
-
-               public static void VerifyUsingForAll ()
-               {
-                       foreach (Namespace ns in all_namespaces)
-                               ns.VerifyUsing ();
-               }
-               
-               public static void DefineNamespacesForAll (SymbolWriter symwriter)
-               {
-                       foreach (Namespace ns in all_namespaces)
-                               ns.DefineNamespaces (symwriter);
-               }
-
-               public override string ToString ()
-               {
-                       return "Namespace (<root>)";
-               }
-
-               public override string GetSignatureForError ()
-               {
-                       return "global::";
-               }
-
        }
 
        /// <summary>
@@ -269,7 +248,6 @@ namespace Mono.CSharp {
                
                Namespace parent;
                string fullname;
-               ArrayList entries;
                Hashtable namespaces;
                IDictionary declspaces;
                Hashtable cached_types;
@@ -310,14 +288,12 @@ namespace Mono.CSharp {
                                throw new InternalErrorException ("Namespace has a null fullname");
 
                        if (parent != null && parent.MemberName != MemberName.Null)
-                               MemberName = new MemberName (
-                                       parent.MemberName, name, parent.MemberName.Location);
-                       else if (name == "")
+                               MemberName = new MemberName (parent.MemberName, name, parent.MemberName.Location);
+                       else if (name.Length == 0)
                                MemberName = MemberName.Null;
                        else
                                MemberName = new MemberName (name, Location.Null);
 
-                       entries = new ArrayList ();
                        namespaces = new Hashtable ();
                        cached_types = new Hashtable ();
 
@@ -329,6 +305,12 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               public virtual void Error_NamespaceDoesNotExist (Location loc, string name)
+               {
+                       Report.Error (234, loc, "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
+                               name, FullName);
+               }
+
                public override void Emit (EmitContext ec)
                {
                        throw new InternalErrorException ("Expression tree referenced namespace " + fullname + " during Emit ()");
@@ -387,7 +369,7 @@ namespace Mono.CSharp {
                                        t = tdecl.TypeBuilder;
                                }
                        }
-                       string lookup = t != null ? t.FullName : (fullname == "" ? name : fullname + "." + name);
+                       string lookup = t != null ? t.FullName : (fullname.Length == 0 ? name : fullname + "." + name);
                        Type rt = root.LookupTypeReflection (lookup, loc);
                        if (t == null)
                                t = rt;
@@ -399,9 +381,8 @@ namespace Mono.CSharp {
 
                public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
                {
-                       Namespace ns = GetNamespace (name, false);
-                       if (ns != null)
-                               return ns;
+                       if (namespaces.Contains (name))
+                               return (Namespace) namespaces [name];
 
                        TypeExpr te = LookupType (name, loc);
                        if (te == null || !ds.CheckAccessLevel (te.Type))
@@ -410,11 +391,6 @@ namespace Mono.CSharp {
                        return te;
                }
 
-               public void AddNamespaceEntry (NamespaceEntry entry)
-               {
-                       entries.Add (entry);
-               }
-
                public void AddDeclSpace (string name, DeclSpace ds)
                {
                        if (declspaces == null)
@@ -422,22 +398,6 @@ namespace Mono.CSharp {
                        declspaces.Add (name, ds);
                }
 
-               /// <summary>
-               ///   Used to validate that all the using clauses are correct
-               ///   after we are finished parsing all the files.  
-               /// </summary>
-               public void VerifyUsing ()
-               {
-                       foreach (NamespaceEntry entry in entries)
-                               entry.VerifyUsing ();
-               }
-
-               public void DefineNamespaces (SymbolWriter symwriter)
-               {
-                       foreach (NamespaceEntry entry in entries)
-                               entry.DefineNamespace (symwriter);
-               }
-
                /// <summary>
                ///   The qualified name of the current namespace
                /// </summary>
@@ -463,8 +423,7 @@ namespace Mono.CSharp {
                }
        }
 
-       public class NamespaceEntry
-       {
+       public class NamespaceEntry {
                Namespace ns;
                NamespaceEntry parent, implicit_parent;
                SourceFile file;
@@ -472,7 +431,16 @@ namespace Mono.CSharp {
                Hashtable aliases;
                ArrayList using_clauses;
                public bool DeclarationFound = false;
-               public bool UsingFound = false;
+               bool UsingFound;
+
+               ListDictionary extern_aliases;
+
+               static ArrayList entries = new ArrayList ();
+
+               public static void Reset ()
+               {
+                       entries = new ArrayList ();
+               }
 
                //
                // This class holds the location where a using definition is
@@ -504,7 +472,7 @@ namespace Mono.CSharp {
 
                                DeclSpace root = RootContext.Tree.Types;
                                root.NamespaceEntry = NamespaceEntry;
-                               FullNamedExpression fne = Expr.ResolveAsTypeStep (root.EmitContext, false);
+                               FullNamedExpression fne = Expr.ResolveAsTypeStep (root, false);
                                root.NamespaceEntry = null;
 
                                if (fne == null) {
@@ -534,8 +502,19 @@ namespace Mono.CSharp {
                        }
                        
                        protected FullNamedExpression resolved;
+                       bool error;
 
-                       public abstract FullNamedExpression Resolve ();
+                       public FullNamedExpression Resolve ()
+                       {
+                               if (resolved != null || error)
+                                       return resolved;
+                               resolved = DoResolve ();
+                               if (resolved == null)
+                                       error = true;
+                               return resolved;
+                       }
+
+                       protected abstract FullNamedExpression DoResolve ();
                }
 
                public class LocalAliasEntry : AliasEntry
@@ -548,16 +527,26 @@ namespace Mono.CSharp {
                                Alias = alias.GetTypeExpression ();
                        }
 
-                       public override FullNamedExpression Resolve ()
+                       protected override FullNamedExpression DoResolve ()
                        {
-                               if (resolved != null)
-                                       return resolved;
-
                                DeclSpace root = RootContext.Tree.Types;
                                root.NamespaceEntry = NamespaceEntry;
-                               resolved = Alias.ResolveAsTypeStep (root.EmitContext, false);
+                               resolved = Alias.ResolveAsTypeStep (root, false);
                                root.NamespaceEntry = null;
 
+                               if (resolved == null)
+                                       return null;
+
+                               if (resolved.Type != null) {
+                                       TypeAttributes attr = resolved.Type.Attributes & TypeAttributes.VisibilityMask;
+                                       if (attr == TypeAttributes.NestedPrivate || attr == TypeAttributes.NestedFamily ||
+                                               ((attr == TypeAttributes.NestedFamORAssem || attr == TypeAttributes.NestedAssembly) && 
+                                               TypeManager.LookupDeclSpace (resolved.Type) == null)) {
+                                               Expression.ErrorIsInaccesible (Alias.Location, Alias.ToString ());
+                                               return null;
+                                       }
+                               }
+
                                return resolved;
                        }
                }
@@ -569,27 +558,23 @@ namespace Mono.CSharp {
                        {
                        }
 
-                       public override FullNamedExpression Resolve ()
+                       protected override FullNamedExpression DoResolve ()
                        {
-                               if (resolved != null)
-                                       return resolved;
-
-                               resolved = TypeManager.ComputeNamespacesForAlias (Name);
-                               if (resolved == null) {
+                               resolved = RootNamespace.GetRootNamespace (Name);
+                               if (resolved == null)
                                        Report.Error (430, Location, "The extern alias '" + Name +
                                                                        "' was not specified in a /reference option");
-                               }
-                               
+
                                return resolved;
                        }
                }
 
-               public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
+               public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name)
                {
                        this.parent = parent;
                        this.file = file;
-                       this.IsImplicit = false;
-                       this.ID = ++next_id;
+                       entries.Add (this);
+                       this.ID = entries.Count;
 
                        if (parent != null)
                                ns = parent.NS.GetNamespace (name, true);
@@ -597,17 +582,15 @@ namespace Mono.CSharp {
                                ns = RootNamespace.Global.GetNamespace (name, true);
                        else
                                ns = RootNamespace.Global;
-                       
-                       ns.AddNamespaceEntry (this);
                }
 
-
                private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
                {
                        this.parent = parent;
                        this.file = file;
+                       // no need to add self to 'entries', since we don't have any aliases or using entries.
+                       this.ID = -1;
                        this.IsImplicit = true;
-                       this.ID = ++next_id;
                        this.ns = ns;
                }
 
@@ -630,7 +613,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               static int next_id = 0;
                public readonly int ID;
                public readonly bool IsImplicit;
 
@@ -665,6 +647,8 @@ namespace Mono.CSharp {
                                return;
                        }
 
+                       UsingFound = true;
+
                        if (name.Equals (ns.MemberName))
                                return;
                        
@@ -673,13 +657,11 @@ namespace Mono.CSharp {
 
                        foreach (UsingEntry old_entry in using_clauses) {
                                if (name.Equals (old_entry.Name)) {
-                                       if (RootContext.WarningLevel >= 3)
-                                               Report.Warning (105, loc, "The using directive for `{0}' appeared previously in this namespace", name);
+                                       Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetName ());
                                                return;
                                        }
                                }
 
-
                        UsingEntry ue = new UsingEntry (Doppelganger, name, loc);
                        using_clauses.Add (ue);
                }
@@ -691,22 +673,31 @@ namespace Mono.CSharp {
                                return;
                        }
 
+                       UsingFound = true;
+
                        if (aliases == null)
                                aliases = new Hashtable ();
 
                        if (aliases.Contains (name)) {
-                               AliasEntry ae = (AliasEntry)aliases [name];
+                               AliasEntry ae = (AliasEntry) aliases [name];
                                Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
-                               Report.Error (1537, loc, "The using alias `" + name +
-                                             "' appeared previously in this namespace");
+                               Report.Error (1537, loc, "The using alias `{0}' appeared previously in this namespace", name);
                                return;
                        }
 
                        if (RootContext.Version == LanguageVersion.Default &&
                            name == "global" && RootContext.WarningLevel >= 2)
-                               Report.Warning (440, loc, "An alias named `global' will not be used when resolving 'global::';" +
+                               Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
                                        " the global namespace will be used instead");
 
+                       // FIXME: get correct error number.  See if the above check can be merged
+                       if (extern_aliases != null && extern_aliases.Contains (name)) {
+                               AliasEntry ae = (AliasEntry) extern_aliases [name];
+                               Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
+                               Report.Error (1537, loc, "The using alias `{0}' appeared previously in this namespace", name);
+                               return;
+                       }
+
                        aliases [name] = new LocalAliasEntry (Doppelganger, name, alias, loc);
                }
 
@@ -717,14 +708,16 @@ namespace Mono.CSharp {
                                return;
                        }
                        
-                       if (aliases == null)
-                               aliases = new Hashtable ();
-                       
-                       if (aliases.Contains (name)) {
-                               AliasEntry ae = (AliasEntry) aliases [name];
+                       // Share the extern_aliases field with the Doppelganger
+                       if (extern_aliases == null) {
+                               extern_aliases = new ListDictionary ();
+                               Doppelganger.extern_aliases = extern_aliases;
+                       }
+
+                       if (extern_aliases.Contains (name)) {
+                               AliasEntry ae = (AliasEntry) extern_aliases [name];
                                Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
-                               Report.Error (1537, loc, "The using alias `" + name +
-                                             "' appeared previously in this namespace");
+                               Report.Error (1537, loc, "The using alias `{0}' appeared previously in this namespace", name);
                                return;
                        }
 
@@ -733,7 +726,10 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       aliases [name] = new ExternAliasEntry (Doppelganger, name, loc);
+                       // Register the alias in aliases and extern_aliases, since we need both of them
+                       // to keep things simple (different resolution scenarios)
+                       ExternAliasEntry alias = new ExternAliasEntry (Doppelganger, name, loc);
+                       extern_aliases [name] = alias;
                }
 
                public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
@@ -757,16 +753,13 @@ namespace Mono.CSharp {
                public FullNamedExpression LookupAlias (string name)
                {
                        AliasEntry entry = null;
-                       // We use Parent rather than ImplicitParent since we know implicit namespace declarations
-                       // cannot have using entries.
-                       for (NamespaceEntry n = this; n != null; n = n.Parent) {
-                               if (n.aliases == null)
-                                       continue;
-                               entry = n.aliases [name] as AliasEntry;
-                               if (entry != null)
-                                       return entry.Resolve ();
+                       for (NamespaceEntry n = this; n != null; n = n.ImplicitParent) {
+                               if (n.extern_aliases != null && (entry = n.extern_aliases [name] as AliasEntry) != null)
+                                       break;
+                               if (n.aliases != null && (entry = n.aliases [name] as AliasEntry) != null)
+                                       break;
                        }
-                       return null;
+                       return entry == null ? null : entry.Resolve ();
                }
 
                private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
@@ -778,11 +771,17 @@ namespace Mono.CSharp {
                        if (fne != null)
                                return fne;
 
+                       if (extern_aliases != null) {
+                               AliasEntry entry = extern_aliases [name] as AliasEntry;
+                               if (entry != null)
+                                       return entry.Resolve ();
+                       }
+                       
                        if (IsImplicit)
                                return null;
-
+                       
                        //
-                       // Check aliases.
+                       // Check aliases. 
                        //
                        if (aliases != null) {
                                AliasEntry entry = aliases [name] as AliasEntry;
@@ -839,30 +838,22 @@ namespace Mono.CSharp {
 
                readonly string [] empty_using_list = new string [0];
 
-               public void DefineNamespace (SymbolWriter symwriter)
-               {
-                       if (symfile_id != 0)
-                               return;
-                       if (parent != null)
-                               parent.DefineNamespace (symwriter);
-
-                       string [] using_list = empty_using_list;
-                       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]).Name.ToString ();
-                       } 
-
-                       int parent_id = parent != null ? parent.symfile_id : 0;
-                       if (file.SourceFileEntry == null)
-                               return;
-
-                       symfile_id = symwriter.DefineNamespace (
-                               ns.Name, file.SourceFileEntry, using_list, parent_id);
-               }
-
                public int SymbolFileID {
-                       get { return symfile_id; }
+                       get {
+                               if (symfile_id == 0 && file.SourceFileEntry != null) {
+                                       int parent_id = parent == null ? 0 : parent.SymbolFileID;
+
+                                       string [] using_list = empty_using_list;
+                                       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]).Name.ToString ();
+                                       }
+
+                                       symfile_id = CodeGen.SymbolWriter.DefineNamespace (ns.Name, file.SourceFileEntry, using_list, parent_id);
+                               }
+                               return symfile_id;
+                       }
                }
 
                static void MsgtryRef (string s)
@@ -907,25 +898,34 @@ namespace Mono.CSharp {
                ///   Used to validate that all the using clauses are correct
                ///   after we are finished parsing all the files.  
                /// </summary>
-               public void VerifyUsing ()
+               void VerifyUsing ()
                {
+                       if (extern_aliases != null) {
+                               foreach (DictionaryEntry de in extern_aliases)
+                                       ((AliasEntry) de.Value).Resolve ();
+                       }               
+
                        if (using_clauses != null) {
                                foreach (UsingEntry ue in using_clauses)
                                        ue.Resolve ();
                        }
 
                        if (aliases != null) {
-                               foreach (DictionaryEntry de in aliases) {
-                                       AliasEntry alias = (AliasEntry) de.Value;
-                                       if (alias.Resolve () == null)
-                                               if (alias is LocalAliasEntry) {
-                                                       LocalAliasEntry local = alias as LocalAliasEntry;
-                                                       Error_NamespaceNotFound (local.Location, local.Alias.ToString ());
-                                               }
-                               }
+                               foreach (DictionaryEntry de in aliases)
+                                       ((AliasEntry) de.Value).Resolve ();
                        }
                }
 
+               /// <summary>
+               ///   Used to validate that all the using clauses are correct
+               ///   after we are finished parsing all the files.  
+               /// </summary>
+               static public void VerifyAllUsing ()
+               {
+                       foreach (NamespaceEntry entry in entries)
+                               entry.VerifyUsing ();
+               }
+
                public string GetSignatureForError ()
                {
                        return ns.GetSignatureForError ();