Nothing to see here
[mono.git] / mcs / mcs / namespace.cs
index fb3924eee901e2eae4d8a9927bbeb7f44643a886..2b3a3ac5d700df9c3bb1eb92407a9f6c1f700da0 100644 (file)
@@ -5,7 +5,8 @@
 //   Miguel de Icaza (miguel@ximian.com)
 //   Marek Safar (marek.safar@seznam.cz)
 //
-// (C) 2001 Ximian, Inc.
+// Copyright 2001 Ximian, Inc.
+// Copyright 2003-2008 Novell, Inc.
 //
 using System;
 using System.Collections;
@@ -15,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 ()
@@ -34,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;
+                       }
 
-                       if (referenced_assembly != null)
-                               ComputeNamespaces (this.referenced_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 &&
+                               a.FullName == SystemCore) {
+                               TypeManager.extension_attribute_type = a.GetType("System.Runtime.CompilerServices.ExtensionAttribute");
+                       }
+
+                       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)
@@ -68,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)
@@ -102,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=b77a5c561934e089"; 
-#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);
  
@@ -166,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;
@@ -223,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) {
@@ -311,12 +333,12 @@ namespace Mono.CSharp {
                        if (this.root == null)
                                throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
                        
-                       string pname = parent != null ? parent.Name : "";
+                       string pname = parent != null ? parent.fullname : "";
                                
                        if (pname == "")
                                fullname = name;
                        else
-                               fullname = parent.Name + "." + name;
+                               fullname = parent.fullname + "." + name;
 
                        if (fullname == null)
                                throw new InternalErrorException ("Namespace has a null fullname");
@@ -356,7 +378,7 @@ namespace Mono.CSharp {
                        }
 
                        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);
+                               name, GetSignatureForError ());
                }
 
                public static void Error_InvalidNumberOfTypeArguments (Type t, Location loc)
@@ -384,14 +406,9 @@ namespace Mono.CSharp {
                                type, name);
                }
 
-               public override void Emit (EmitContext ec)
-               {
-                       throw new InternalErrorException ("Expression tree referenced namespace " + fullname + " during Emit ()");
-               }
-
                public override string GetSignatureForError ()
                {
-                       return Name;
+                       return fullname;
                }
                
                public Namespace GetNamespace (string name, bool create)
@@ -420,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))
@@ -440,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);
@@ -492,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;
 
@@ -543,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>
@@ -550,10 +586,6 @@ namespace Mono.CSharp {
                        get { return fullname; }
                }
 
-               public override string FullName {
-                       get { return fullname; }
-               }
-
                /// <summary>
                ///   The parent of this namespace, used by the parser to "Pop"
                ///   the current namespace declaration
@@ -561,61 +593,45 @@ namespace Mono.CSharp {
                public Namespace Parent {
                        get { return parent; }
                }
-
-               public override string ToString ()
-               {
-                       return String.Format ("Namespace ({0})", Name);
-               }
        }
 
-       public class NamespaceEntry {
-               Namespace ns;
-               NamespaceEntry parent, implicit_parent;
-               SourceFile file;
-               int symfile_id;
-               Hashtable aliases;
-               ArrayList using_clauses;
-               public bool DeclarationFound = false;
-               bool UsingFound;
-
-               public readonly DeclSpace SlaveDeclSpace;
+       //
+       // Namespace container as created by the parser
+       //
+       public class NamespaceEntry : IResolveContext {
 
-               ListDictionary extern_aliases;
+               class UsingEntry {
+                       readonly MemberName name;
+                       Namespace resolved;
+                       
+                       public UsingEntry (MemberName name)
+                       {
+                               this.name = name;
+                       }
 
-               static ArrayList entries = new ArrayList ();
+                       public string GetSignatureForError ()
+                       {
+                               return name.GetSignatureForError ();
+                       }
 
-               public static void Reset ()
-               {
-                       entries = new ArrayList ();
-               }
+                       public Location Location {
+                               get { return name.Location; }
+                       }
 
-               //
-               // This class holds the location where a using definition is
-               // done, and whether it has been used by the program or not.
-               //
-               // We use this to flag using clauses for namespaces that do not
-               // exist.
-               //
-               public class UsingEntry : IResolveContext {
-                       public readonly MemberName Name;
-                       readonly NamespaceEntry NamespaceEntry;
-                       readonly Location Location;
+                       public MemberName MemberName {
+                               get { return name; }
+                       }
                        
-                       public UsingEntry (NamespaceEntry entry, MemberName name, Location loc)
-                       {
-                               Name = name;
-                               NamespaceEntry = entry;
-                               Location = loc;
+                       public string Name {
+                               get { return GetSignatureForError (); }
                        }
 
-                       internal Namespace resolved;
-
-                       public Namespace Resolve ()
+                       public Namespace Resolve (IResolveContext rc)
                        {
                                if (resolved != null)
                                        return resolved;
 
-                               FullNamedExpression fne = Name.GetTypeExpression ().ResolveAsTypeStep (this, false);
+                               FullNamedExpression fne = name.GetTypeExpression ().ResolveAsTypeStep (rc, false);
                                if (fne == null)
                                        return null;
 
@@ -623,122 +639,116 @@ namespace Mono.CSharp {
                                if (resolved == null) {
                                        Report.SymbolRelatedToPreviousError (fne.Type);
                                        Report.Error (138, Location,
-                                               "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.GetSignatureForError ());
+                                               "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces",
+                                               GetSignatureForError ());
                                }
                                return resolved;
                        }
 
-                       DeclSpace IResolveContext.DeclContainer {
-                               get { return NamespaceEntry.SlaveDeclSpace; }
+                       public override string ToString ()
+                       {
+                               return Name;
                        }
+               }
 
-                       DeclSpace IResolveContext.GenericDeclContainer {
-                               get { return NamespaceEntry.SlaveDeclSpace; }
-                       }
+               class UsingAliasEntry {
+                       public readonly string Alias;
+                       public Location Location;
 
-                       bool IResolveContext.IsInObsoleteScope {
-                               get { return false; }
-                       }
-                       bool IResolveContext.IsInUnsafeScope {
-                               get { return false; }
+                       public UsingAliasEntry (string alias, Location loc)
+                       {
+                               this.Alias = alias;
+                               this.Location = loc;
                        }
-               }
 
-               public abstract class AliasEntry {
-                       public readonly string Name;
-                       public readonly NamespaceEntry NamespaceEntry;
-                       public readonly Location Location;
-                       
-                       protected AliasEntry (NamespaceEntry entry, string name, Location loc)
+                       public virtual FullNamedExpression Resolve (IResolveContext rc)
                        {
-                               Name = name;
-                               NamespaceEntry = entry;
-                               Location = loc;
+                               FullNamedExpression fne = RootNamespace.GetRootNamespace (Alias);
+                               if (fne == null) {
+                                       Report.Error (430, Location,
+                                               "The extern alias `{0}' was not specified in -reference option",
+                                               Alias);
+                               }
+
+                               return fne;
                        }
-                       
-                       protected FullNamedExpression resolved;
-                       bool error;
 
-                       public FullNamedExpression Resolve ()
+                       public override string ToString ()
                        {
-                               if (resolved != null || error)
-                                       return resolved;
-                               resolved = DoResolve ();
-                               if (resolved == null)
-                                       error = true;
-                               return resolved;
+                               return Alias;
                        }
-
-                       protected abstract FullNamedExpression DoResolve ();
+                       
                }
 
-               public class LocalAliasEntry : AliasEntry, IResolveContext {
-                       public readonly Expression Alias;
-                       
-                       public LocalAliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc) :
-                               base (entry, name, loc)
+               class LocalUsingAliasEntry : UsingAliasEntry {
+                       Expression resolved;
+                       MemberName value;
+
+                       public LocalUsingAliasEntry (string alias, MemberName name, Location loc)
+                               : base (alias, loc)
                        {
-                               Alias = alias.GetTypeExpression ();
+                               this.value = name;
                        }
 
-                       protected override FullNamedExpression DoResolve ()
+                       public override FullNamedExpression Resolve (IResolveContext rc)
                        {
-                               resolved = Alias.ResolveAsTypeStep (this, false);
-                               if (resolved == null)
+                               if (resolved != null || value == null)
+                                       return (FullNamedExpression)resolved;
+
+                               resolved = value.GetTypeExpression ().ResolveAsTypeStep (rc, false);
+                               if (resolved == null) {
+                                       value = null;
                                        return null;
+                               }
 
+                               // FIXME: This is quite wrong, the accessibility is not global
                                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 ());
+                                               Expression.ErrorIsInaccesible (resolved.Location, resolved.GetSignatureForError ());
                                                return null;
                                        }
                                }
 
-                               return resolved;
+                               return (FullNamedExpression)resolved;
                        }
 
-                       DeclSpace IResolveContext.DeclContainer {
-                               get { return NamespaceEntry.SlaveDeclSpace; }
+                       public override string ToString ()
+                       {
+                               return String.Format ("{0} = {1}", Alias, value.GetSignatureForError ());
                        }
+               }
 
-                       DeclSpace IResolveContext.GenericDeclContainer {
-                               get { return NamespaceEntry.SlaveDeclSpace; }
-                       }
+               Namespace ns;
+               NamespaceEntry parent, implicit_parent;
+               CompilationUnit file;
+               int symfile_id;
 
-                       bool IResolveContext.IsInObsoleteScope {
-                               get { return false; }
-                       }
-                       bool IResolveContext.IsInUnsafeScope {
-                               get { return false; }
-                       }
-               }
+               // Namespace using import block
+               ArrayList using_aliases;
+               ArrayList using_clauses;
+               public bool DeclarationFound = false;
+               // End
 
-               public class ExternAliasEntry : AliasEntry {
-                       public ExternAliasEntry (NamespaceEntry entry, string name, Location loc) :
-                               base (entry, name, loc)
-                       {
-                       }
+               public readonly bool IsImplicit;
+               public readonly DeclSpace SlaveDeclSpace;
+               static readonly Namespace [] empty_namespaces = new Namespace [0];
+               Namespace [] namespace_using_table;
 
-                       protected override FullNamedExpression DoResolve ()
-                       {
-                               resolved = RootNamespace.GetRootNamespace (Name);
-                               if (resolved == null)
-                                       Report.Error (430, Location, "The extern alias '" + Name +
-                                                                       "' was not specified in a /reference option");
+               static ArrayList entries = new ArrayList ();
 
-                               return resolved;
-                       }
+               public static void Reset ()
+               {
+                       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;
                        entries.Add (this);
-                       this.ID = entries.Count;
 
                        if (parent != null)
                                ns = parent.NS.GetNamespace (name, true);
@@ -749,17 +759,76 @@ 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;
-                       // no need to add self to 'entries', since we don't have any aliases or using entries.
-                       this.ID = -1;
                        this.IsImplicit = true;
                        this.ns = ns;
                        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.
@@ -773,15 +842,14 @@ namespace Mono.CSharp {
                NamespaceEntry doppelganger;
                NamespaceEntry Doppelganger {
                        get {
-                               if (!IsImplicit && doppelganger == null)
+                               if (!IsImplicit && doppelganger == null) {
                                        doppelganger = new NamespaceEntry (ImplicitParent, file, ns, true);
+                                       doppelganger.using_aliases = using_aliases;
+                               }
                                return doppelganger;
                        }
                }
 
-               public readonly int ID;
-               public readonly bool IsImplicit;
-
                public Namespace NS {
                        get { return ns; }
                }
@@ -806,112 +874,98 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Records a new namespace for resolving name references
                /// </summary>
-               public void Using (MemberName name, Location loc)
+               public void AddUsing (MemberName name, Location loc)
                {
                        if (DeclarationFound){
                                Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
-                               return;
                        }
 
-                       UsingFound = true;
-
-                       if (name.Equals (ns.MemberName))
-                               return;
-                       
-                       if (using_clauses == null)
+                       if (using_clauses == null) {
                                using_clauses = new ArrayList ();
-
-                       foreach (UsingEntry old_entry in using_clauses) {
-                               if (name.Equals (old_entry.Name)) {
-                                       Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetName ());
-                                       return;
+                       } else {
+                               foreach (UsingEntry old_entry in using_clauses) {
+                                       if (name.Equals (old_entry.MemberName)) {
+                                               Report.SymbolRelatedToPreviousError (old_entry.Location, old_entry.GetSignatureForError ());
+                                               Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetSignatureForError ());
+                                               return;
+                                       }
                                }
                        }
 
-                       UsingEntry ue = new UsingEntry (Doppelganger, name, loc);
-                       using_clauses.Add (ue);
+                       using_clauses.Add (new UsingEntry (name));
                }
 
-               public void UsingAlias (string name, MemberName alias, Location loc)
+               public void AddUsingAlias (string alias, MemberName name, Location loc)
                {
+                       // TODO: This is parser bussines
                        if (DeclarationFound){
                                Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
-                               return;
                        }
 
-                       UsingFound = true;
-
-                       if (aliases == null)
-                               aliases = new Hashtable ();
-
-                       if (aliases.Contains (name)) {
-                               AliasEntry ae = (AliasEntry) aliases [name];
-                               Report.SymbolRelatedToPreviousError (ae.Location, ae.Name);
-                               Report.Error (1537, loc, "The using alias `{0}' appeared previously in this namespace", name);
-                               return;
-                       }
-
-                       if (RootContext.Version != LanguageVersion.ISO_1 && name == "global")
+                       if (RootContext.Version != LanguageVersion.ISO_1 && alias == "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);
+                       AddUsingAlias (new LocalUsingAliasEntry (alias, name, loc));
                }
 
-               public void UsingExternalAlias (string name, Location loc)
+               public void AddUsingExternalAlias (string alias, Location loc)
                {
-                       if (UsingFound || DeclarationFound) {
-                               Report.Error (439, loc, "An extern alias declaration must precede all other elements");
-                               return;
-                       }
-                       
-                       // Share the extern_aliases field with the Doppelganger
-                       if (extern_aliases == null) {
-                               extern_aliases = new ListDictionary ();
-                               Doppelganger.extern_aliases = extern_aliases;
+                       // TODO: Do this in parser
+                       bool not_first = using_clauses != null || DeclarationFound;
+                       if (using_aliases != null && !not_first) {
+                               foreach (UsingAliasEntry uae in using_aliases) {
+                                       if (uae is LocalUsingAliasEntry) {
+                                               not_first = true;
+                                               break;
+                                       }
+                               }
                        }
 
-                       if (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;
-                       }
+                       if (not_first)
+                               Report.Error (439, loc, "An extern alias declaration must precede all other elements");
 
-                       if (name == "global") {
+                       if (alias == "global") {
                                Error_GlobalNamespaceRedefined (loc);
                                return;
                        }
 
-                       // 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;
+                       AddUsingAlias (new UsingAliasEntry (alias, loc));
+               }
+
+               void AddUsingAlias (UsingAliasEntry uae)
+               {
+                       if (using_aliases == null) {
+                               using_aliases = new ArrayList ();
+                       } else {
+                               foreach (UsingAliasEntry entry in using_aliases) {
+                                       if (uae.Alias == entry.Alias) {
+                                               Report.SymbolRelatedToPreviousError (uae.Location, uae.Alias);
+                                               Report.Error (1537, entry.Location, "The using alias `{0}' appeared previously in this namespace",
+                                                       entry.Alias);
+                                               return;
+                                       }
+                               }
+                       }
+
+                       using_aliases.Add (uae);
                }
 
                ///
                /// Does extension methods look up to find a method which matches name and extensionType.
                /// Search starts from this namespace and continues hierarchically up to top level.
                ///
-               public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name)
+               public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name, Location loc)
                {
                        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, Location.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;
 
@@ -922,12 +976,27 @@ namespace Mono.CSharp {
                        }
 
                        if (candidates != null)
-                               return new ExtensionMethodGroupExpr (candidates, parent, extensionType, Location.Null);
+                               return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
 
                        if (parent == null)
                                return null;
 
-                       return parent.LookupExtensionMethod (extensionType, currentClass, name);
+                       //
+                       // Inspect parent namespaces in namespace expression
+                       //
+                       Namespace parent_ns = ns.Parent;
+                       do {
+                               candidates = parent_ns.LookupExtensionMethod (extensionType, null, name);
+                               if (candidates != null)
+                                       return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
+
+                               parent_ns = parent_ns.Parent;
+                       } while (parent_ns != null);
+
+                       //
+                       // Continue in parent scope
+                       //
+                       return parent.LookupExtensionMethod (extensionType, currentClass, name, loc);
                }
 
                public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
@@ -946,20 +1015,23 @@ namespace Mono.CSharp {
                        Report.SymbolRelatedToPreviousError (t1.Type);
                        Report.SymbolRelatedToPreviousError (t2.Type);
                        Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
-                               name, t1.FullName, t2.FullName);
+                               name, t1.GetSignatureForError (), t2.GetSignatureForError ());
                }
 
                // Looks-up a alias named @name in this and surrounding namespace declarations
                public FullNamedExpression LookupAlias (string name)
                {
-                       AliasEntry entry = null;
                        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;
+                               if (n.using_aliases == null)
+                                       continue;
+
+                               foreach (UsingAliasEntry ue in n.using_aliases) {
+                                       if (ue.Alias == name)
+                                               return ue.Resolve (Doppelganger);
+                               }
                        }
-                       return entry == null ? null : entry.Resolve ();
+
+                       return null;
                }
 
                private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
@@ -968,27 +1040,37 @@ namespace Mono.CSharp {
                        // Check whether it's in the namespace.
                        //
                        FullNamedExpression fne = ns.Lookup (ds, name, loc);
-                       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. 
                        //
-                       if (aliases != null) {
-                               AliasEntry entry = aliases [name] as AliasEntry;
-                               if (entry != null)
-                                       return entry.Resolve ();
+                       if (using_aliases != null) {
+                               foreach (UsingAliasEntry ue in using_aliases) {
+                                       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;
+
                        //
                        // Check using entries.
                        //
@@ -1008,9 +1090,6 @@ namespace Mono.CSharp {
                        return fne;
                }
 
-               // Our cached computation.
-               static readonly Namespace [] empty_namespaces = new Namespace [0];
-               Namespace [] namespace_using_table;
                Namespace [] GetUsingTable ()
                {
                        if (namespace_using_table != null)
@@ -1024,15 +1103,14 @@ namespace Mono.CSharp {
                        ArrayList list = new ArrayList (using_clauses.Count);
 
                        foreach (UsingEntry ue in using_clauses) {
-                               Namespace using_ns = ue.Resolve ();
+                               Namespace using_ns = ue.Resolve (Doppelganger);
                                if (using_ns == null)
                                        continue;
 
                                list.Add (using_ns);
                        }
 
-                       namespace_using_table = new Namespace [list.Count];
-                       list.CopyTo (namespace_using_table, 0);
+                       namespace_using_table = (Namespace[])list.ToArray (typeof (Namespace));
                        return namespace_using_table;
                }
 
@@ -1047,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]).Name.GetTypeName ();
+                                                       using_list [i] = ((UsingEntry) using_clauses [i]).MemberName.GetTypeName ();
                                        }
 
-                                       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;
                        }
@@ -1073,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);
 
@@ -1105,19 +1188,14 @@ namespace Mono.CSharp {
                /// </summary>
                void VerifyUsing ()
                {
-                       if (extern_aliases != null) {
-                               foreach (DictionaryEntry de in extern_aliases)
-                                       ((AliasEntry) de.Value).Resolve ();
-                       }               
+                       if (using_aliases != null) {
+                               foreach (UsingAliasEntry ue in using_aliases)
+                                       ue.Resolve (Doppelganger);
+                       }
 
                        if (using_clauses != null) {
                                foreach (UsingEntry ue in using_clauses)
-                                       ue.Resolve ();
-                       }
-
-                       if (aliases != null) {
-                               foreach (DictionaryEntry de in aliases)
-                                       ((AliasEntry) de.Value).Resolve ();
+                                       ue.Resolve (Doppelganger);
                        }
                }
 
@@ -1140,5 +1218,25 @@ namespace Mono.CSharp {
                {
                        return ns.ToString ();
                }
+
+               #region IResolveContext Members
+
+               public DeclSpace DeclContainer {
+                       get { return SlaveDeclSpace; }
+               }
+
+               public bool IsInObsoleteScope {
+                       get { return SlaveDeclSpace.IsInObsoleteScope; }
+               }
+
+               public bool IsInUnsafeScope {
+                       get { return SlaveDeclSpace.IsInUnsafeScope; }
+               }
+
+               public DeclSpace GenericDeclContainer {
+                       get { return SlaveDeclSpace; }
+               }
+
+               #endregion
        }
 }