condition tests passes
[mono.git] / mcs / mcs / namespace.cs
index 17af66b211eef57908aa178f2de7cd52be5e8b1c..0861c0c0421cd5e82654a6636ad95f8394e29c8d 100644 (file)
@@ -55,8 +55,7 @@ namespace Mono.CSharp {
                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'");
+                               NamespaceEntry.Error_GlobalNamespaceRedefined (Location.Null);
                                return;
                        }
                        RootNamespace retval = GetRootNamespace (name);
@@ -85,25 +84,54 @@ namespace Mono.CSharp {
                        return all_namespaces.Contains (name);
                }
 
-               protected void EnsureNamespace (string dotted_name)
+               protected void RegisterNamespace (string dotted_name)
                {
                        if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
                                GetNamespace (dotted_name, true);
                }
 
-               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;
-                       }
+               void RegisterExtensionMethodClass (Type t)
+               {
+                       string n = t.Namespace;
+                       Namespace ns = n == null ? Global : (Namespace)all_namespaces [n];
+                       if (ns == null)
+                               ns = GetNamespace (n, true);
+                       ns.RegisterExternalExtensionMethodClass (t);
+               }
+
+               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.1.0.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);
+                       if (get_namespaces_method != null && !contains_extension_methods) {
+                               string [] namespaces = (string []) get_namespaces_method.Invoke (assembly, null);
+                               foreach (string ns in namespaces)
+                                       RegisterNamespace (ns);
+                               return;
+                       }
+  
+                       foreach (Type t in assembly.GetExportedTypes ()) {
+                               if ((t.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute &&
+                                       contains_extension_methods && t.IsDefined (TypeManager.extension_attribute_type, false))
+                                       RegisterExtensionMethodClass (t);
+                               else
+                                       RegisterNamespace (t.Namespace);
+                       }
+               }
 
-                       foreach (Type t in assembly.GetExportedTypes ())
-                               EnsureNamespace (t.Namespace);
-               }
-               
                protected static Type GetTypeInAssembly (Assembly assembly, string name)
                {
                        Type t = assembly.GetType (name);
@@ -112,12 +140,15 @@ 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)
+                       if (ta == TypeAttributes.NestedPrivate)
+                               return null;
+
+                       if ((ta == TypeAttributes.NotPublic ||
+                            ta == TypeAttributes.NestedAssembly ||
+                            ta == TypeAttributes.NestedFamANDAssem) &&
+                           !TypeManager.IsFriendAssembly (t.Assembly))
                                return null;
 
                        return t;
@@ -181,10 +212,10 @@ namespace Mono.CSharp {
                                return;
 
                        foreach (Type t in m.GetTypes ())
-                               EnsureNamespace (t.Namespace);
+                               RegisterNamespace (t.Namespace);
                }
 
-               public override void Error_NamespaceDoesNotExist(Location loc, string name)
+               public override void Error_NamespaceDoesNotExist(DeclSpace ds, Location loc, string name)
                {
                        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);
@@ -221,11 +252,17 @@ namespace Mono.CSharp {
                                                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));
+                                       if (loc.IsNull) {
+                                               DeclSpace ds = TypeManager.LookupDeclSpace (t);
+                                               Report.Warning (1685, 1, ds.Location, "The type `{0}' conflicts with the predefined type `{1}' and will be ignored",
+                                                       ds.GetSignatureForError (), TypeManager.CSharpName (found_type));
+                                               return found_type;
+                                       }
+                                       Report.SymbolRelatedToPreviousError (t);
+                                       Report.Warning (436, 2, loc, "The type `{0}' conflicts with the imported type `{1}'. Ignoring the imported type definition",
+                                               TypeManager.CSharpName (t), TypeManager.CSharpName (found_type));
                                        return t;
                                }
                        }
@@ -244,10 +281,11 @@ namespace Mono.CSharp {
                
                Namespace parent;
                string fullname;
-               Hashtable namespaces;
+               IDictionary namespaces;
                IDictionary declspaces;
                Hashtable cached_types;
                RootNamespace root;
+               ArrayList external_exmethod_classes;
 
                public readonly MemberName MemberName;
 
@@ -260,7 +298,7 @@ namespace Mono.CSharp {
                {
                        // Expression members.
                        this.eclass = ExprClass.Namespace;
-                       this.Type = null;
+                       this.Type = typeof (Namespace);
                        this.loc = Location.Null;
 
                        this.parent = parent;
@@ -290,7 +328,7 @@ namespace Mono.CSharp {
                        else
                                MemberName = new MemberName (name);
 
-                       namespaces = new Hashtable ();
+                       namespaces = new HybridDictionary ();
                        cached_types = new Hashtable ();
 
                        root.RegisterNamespace (this);
@@ -301,12 +339,51 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               public virtual void Error_NamespaceDoesNotExist (Location loc, string name)
+               public virtual void Error_NamespaceDoesNotExist (DeclSpace ds, Location loc, string name)
                {
+                       if (name.IndexOf ('`') > 0) {
+                               FullNamedExpression retval = Lookup (ds, SimpleName.RemoveGenericArity (name), loc);
+                               if (retval != null) {
+                                       Error_TypeArgumentsCannotBeUsed (retval.Type, loc);
+                                       return;
+                               }
+                       } else {
+                               Type t = LookForAnyGenericType (name);
+                               if (t != null) {
+                                       Error_InvalidNumberOfTypeArguments (t, loc);
+                                       return;
+                               }
+                       }
+
                        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 static void Error_InvalidNumberOfTypeArguments (Type t, Location loc)
+               {
+                       Report.SymbolRelatedToPreviousError (t);
+                       Report.Error (305, loc, "Using the generic type `{0}' requires `{1}' type argument(s)",
+                               TypeManager.CSharpName(t), TypeManager.GetNumberOfTypeArguments(t).ToString());
+               }
+
+               public static void Error_TypeArgumentsCannotBeUsed (Type t, Location loc)
+               {
+                       Report.SymbolRelatedToPreviousError (t);
+                       Error_TypeArgumentsCannotBeUsed (loc, "type", TypeManager.CSharpName (t));
+               }
+
+               public static void Error_TypeArgumentsCannotBeUsed (MethodBase mi, Location loc)
+               {
+                       Report.SymbolRelatedToPreviousError (mi);
+                       Error_TypeArgumentsCannotBeUsed (loc, "method", TypeManager.CSharpSignature (mi));
+               }
+
+               static void Error_TypeArgumentsCannotBeUsed (Location loc, string type, string name)
+               {
+                       Report.Error(308, loc, "The non-generic {0} `{1}' cannot be used with the type arguments",
+                               type, name);
+               }
+
                public override void Emit (EmitContext ec)
                {
                        throw new InternalErrorException ("Expression tree referenced namespace " + fullname + " during Emit ()");
@@ -367,7 +444,9 @@ namespace Mono.CSharp {
                        }
                        string lookup = t != null ? t.FullName : (fullname.Length == 0 ? name : fullname + "." + name);
                        Type rt = root.LookupTypeReflection (lookup, loc);
-                       if (t == null)
+
+                       // HACK: loc.IsNull when the type is core type
+                       if (t == null || (rt != null && loc.IsNull))
                                t = rt;
 
                        TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
@@ -375,16 +454,86 @@ namespace Mono.CSharp {
                        return te;
                }
 
+               ///
+               /// Used for better error reporting only
+               /// 
+               public Type LookForAnyGenericType (string typeName)
+               {
+                       if (declspaces == null)
+                               return null;
+
+                       typeName = SimpleName.RemoveGenericArity (typeName);
+
+                       foreach (DictionaryEntry de in declspaces) {
+                               string type_item = (string) de.Key;
+                               int pos = type_item.LastIndexOf ('`');
+                               if (pos == typeName.Length && String.Compare (typeName, 0, type_item, 0, pos) == 0)
+                                       return ((DeclSpace) de.Value).TypeBuilder;
+                       }
+                       return null;
+               }
+
                public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
                {
                        if (namespaces.Contains (name))
                                return (Namespace) namespaces [name];
 
-                       TypeExpr te = LookupType (name, loc);
-                       if (te == null || !ds.CheckAccessLevel (te.Type))
-                               return null;
+                       return LookupType (name, loc);
+               }
 
-                       return te;
+               public void RegisterExternalExtensionMethodClass (Type type)
+               {
+                       if (external_exmethod_classes == null)
+                               external_exmethod_classes = new ArrayList ();
+
+                       external_exmethod_classes.Add (type);
+               }
+
+               /// 
+               /// Looks for extension method in this namespace
+               /// 
+               public ArrayList LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name, NamespaceEntry ns)
+               {
+                       ArrayList found = null;
+
+                       if (declspaces != null) {
+                               IEnumerator e = declspaces.Values.GetEnumerator ();
+                               e.Reset ();
+                               while (e.MoveNext ()) {
+                                       Class c = e.Current as Class;
+                                       if (c == null)
+                                               continue;
+
+                                       if (!c.IsStaticClass)
+                                               continue;
+
+                                       ArrayList res = c.MemberCache.FindExtensionMethods (extensionType, name, c != currentClass);
+                                       if (res == null)
+                                               continue;
+
+                                       if (found == null)
+                                               found = res;
+                                       else
+                                               found.AddRange (res);
+                               }
+                       }
+
+                       if (external_exmethod_classes == null)
+                               return found;
+
+                       foreach (Type t in external_exmethod_classes) {
+                               MemberCache m = TypeHandle.GetMemberCache (t);
+                               ArrayList res = m.FindExtensionMethods (extensionType, name, true);
+                               if (res == null)
+                                       continue;
+
+                               if (found == null)
+                                       found = res;
+                               else
+                                       found.AddRange (res);
+                       }
+
+                       return found;
                }
 
                public void AddDeclSpace (string name, DeclSpace ds)
@@ -429,6 +578,8 @@ namespace Mono.CSharp {
                public bool DeclarationFound = false;
                bool UsingFound;
 
+               public readonly DeclSpace SlaveDeclSpace;
+
                ListDictionary extern_aliases;
 
                static ArrayList entries = new ArrayList ();
@@ -445,16 +596,14 @@ namespace Mono.CSharp {
                // We use this to flag using clauses for namespaces that do not
                // exist.
                //
-               public class UsingEntry {
+               public class UsingEntry : IResolveContext {
                        public readonly MemberName Name;
-                       readonly Expression Expr;
                        readonly NamespaceEntry NamespaceEntry;
                        readonly Location Location;
                        
                        public UsingEntry (NamespaceEntry entry, MemberName name, Location loc)
                        {
                                Name = name;
-                               Expr = name.GetTypeExpression ();
                                NamespaceEntry = entry;
                                Location = loc;
                        }
@@ -466,11 +615,7 @@ namespace Mono.CSharp {
                                if (resolved != null)
                                        return resolved;
 
-                               DeclSpace root = RootContext.Tree.Types;
-                               root.NamespaceEntry = NamespaceEntry;
-                               FullNamedExpression fne = Expr.ResolveAsTypeStep (root, false);
-                               root.NamespaceEntry = null;
-
+                               FullNamedExpression fne = Name.GetTypeExpression ().ResolveAsTypeStep (this, false);
                                if (fne == null) {
                                        Error_NamespaceNotFound (Location, Name.ToString ());
                                        return null;
@@ -478,11 +623,27 @@ namespace Mono.CSharp {
 
                                resolved = fne as Namespace;
                                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.ToString ());
+                                               "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.GetSignatureForError ());
                                }
                                return resolved;
                        }
+
+                       DeclSpace IResolveContext.DeclContainer {
+                               get { return NamespaceEntry.SlaveDeclSpace; }
+                       }
+
+                       DeclSpace IResolveContext.GenericDeclContainer {
+                               get { return NamespaceEntry.SlaveDeclSpace; }
+                       }
+
+                       bool IResolveContext.IsInObsoleteScope {
+                               get { return false; }
+                       }
+                       bool IResolveContext.IsInUnsafeScope {
+                               get { return false; }
+                       }
                }
 
                public abstract class AliasEntry {
@@ -513,8 +674,7 @@ namespace Mono.CSharp {
                        protected abstract FullNamedExpression DoResolve ();
                }
 
-               public class LocalAliasEntry : AliasEntry
-               {
+               public class LocalAliasEntry : AliasEntry, IResolveContext {
                        public readonly Expression Alias;
                        
                        public LocalAliasEntry (NamespaceEntry entry, string name, MemberName alias, Location loc) :
@@ -525,11 +685,7 @@ namespace Mono.CSharp {
 
                        protected override FullNamedExpression DoResolve ()
                        {
-                               DeclSpace root = RootContext.Tree.Types;
-                               root.NamespaceEntry = NamespaceEntry;
-                               resolved = Alias.ResolveAsTypeStep (root, false);
-                               root.NamespaceEntry = null;
-
+                               resolved = Alias.ResolveAsTypeStep (this, false);
                                if (resolved == null)
                                        return null;
 
@@ -545,10 +701,24 @@ namespace Mono.CSharp {
 
                                return resolved;
                        }
+
+                       DeclSpace IResolveContext.DeclContainer {
+                               get { return NamespaceEntry.SlaveDeclSpace; }
+                       }
+
+                       DeclSpace IResolveContext.GenericDeclContainer {
+                               get { return NamespaceEntry.SlaveDeclSpace; }
+                       }
+
+                       bool IResolveContext.IsInObsoleteScope {
+                               get { return false; }
+                       }
+                       bool IResolveContext.IsInUnsafeScope {
+                               get { return false; }
+                       }
                }
 
-               public class ExternAliasEntry : AliasEntry 
-               {
+               public class ExternAliasEntry : AliasEntry {
                        public ExternAliasEntry (NamespaceEntry entry, string name, Location loc) :
                                base (entry, name, loc)
                        {
@@ -578,9 +748,10 @@ namespace Mono.CSharp {
                                ns = RootNamespace.Global.GetNamespace (name, true);
                        else
                                ns = RootNamespace.Global;
+                       SlaveDeclSpace = new RootDeclSpace (this);
                }
 
-               private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns)
+               private NamespaceEntry (NamespaceEntry parent, SourceFile file, Namespace ns, bool slave)
                {
                        this.parent = parent;
                        this.file = file;
@@ -588,6 +759,7 @@ namespace Mono.CSharp {
                        this.ID = -1;
                        this.IsImplicit = true;
                        this.ns = ns;
+                       this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
                }
 
                //
@@ -604,7 +776,7 @@ namespace Mono.CSharp {
                NamespaceEntry Doppelganger {
                        get {
                                if (!IsImplicit && doppelganger == null)
-                                       doppelganger = new NamespaceEntry (ImplicitParent, file, ns);
+                                       doppelganger = new NamespaceEntry (ImplicitParent, file, ns, true);
                                return doppelganger;
                        }
                }
@@ -627,7 +799,7 @@ namespace Mono.CSharp {
                                if (implicit_parent == null) {
                                        implicit_parent = (parent.NS == ns.Parent)
                                                ? parent
-                                               : new NamespaceEntry (parent, file, ns.Parent);
+                                               : new NamespaceEntry (parent, file, ns.Parent, false);
                                }
                                return implicit_parent;
                        }
@@ -681,8 +853,7 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       if (RootContext.Version == LanguageVersion.Default &&
-                           name == "global" && RootContext.WarningLevel >= 2)
+                       if (RootContext.Version != LanguageVersion.ISO_1 && name == "global")
                                Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
                                        " the global namespace will be used instead");
 
@@ -718,7 +889,7 @@ namespace Mono.CSharp {
                        }
 
                        if (name == "global") {
-                               Report.Error (1681, loc, "You cannot redefine the global extern alias");
+                               Error_GlobalNamespaceRedefined (loc);
                                return;
                        }
 
@@ -728,6 +899,39 @@ namespace Mono.CSharp {
                        extern_aliases [name] = alias;
                }
 
+               ///
+               /// 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)
+               {
+                       ArrayList candidates = null;
+                       if (currentClass != null) {
+                               candidates = ns.LookupExtensionMethod (extensionType, currentClass, name, this);
+                               if (candidates != null)
+                                       return new ExtensionMethodGroupExpr (candidates, this, extensionType, Location.Null);
+                       }
+
+                       foreach (Namespace n in GetUsingTable ()) {
+                               ArrayList a = n.LookupExtensionMethod (extensionType, null, name, this);
+                               if (a == null)
+                                       continue;
+
+                               if (candidates == null)
+                                       candidates = a;
+                               else
+                                       candidates.AddRange (a);
+                       }
+
+                       if (candidates != null)
+                               return new ExtensionMethodGroupExpr (candidates, parent, extensionType, Location.Null);
+
+                       if (parent == null)
+                               return null;
+
+                       return parent.LookupExtensionMethod (extensionType, currentClass, name);
+               }
+
                public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
                {
                        // Precondition: Only simple names (no dots) will be looked up with this function.
@@ -741,6 +945,8 @@ namespace Mono.CSharp {
 
                static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
                {
+                       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);
                }
@@ -763,7 +969,7 @@ namespace Mono.CSharp {
                        //
                        // Check whether it's in the namespace.
                        //
-                       FullNamedExpression fne = NS.Lookup (ds, name, loc);
+                       FullNamedExpression fne = ns.Lookup (ds, name, loc);
                        if (fne != null)
                                return fne;
 
@@ -805,7 +1011,7 @@ namespace Mono.CSharp {
                }
 
                // Our cached computation.
-               readonly Namespace [] empty_namespaces = new Namespace [0];
+               static readonly Namespace [] empty_namespaces = new Namespace [0];
                Namespace [] namespace_using_table;
                Namespace [] GetUsingTable ()
                {
@@ -832,7 +1038,7 @@ namespace Mono.CSharp {
                        return namespace_using_table;
                }
 
-               readonly string [] empty_using_list = new string [0];
+               static readonly string [] empty_using_list = new string [0];
 
                public int SymbolFileID {
                        get {
@@ -862,6 +1068,11 @@ namespace Mono.CSharp {
                        Console.WriteLine ("    Try using -pkg:" + s);
                }
 
+               public static void Error_GlobalNamespaceRedefined (Location loc)
+               {
+                       Report.Error (1681, loc, "You cannot redefine the global extern alias");
+               }
+
                public static void Error_NamespaceNotFound (Location loc, string name)
                {
                        Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",