condition tests passes
[mono.git] / mcs / mcs / namespace.cs
index ce548ce3ad02394c6c7ecd6fc282ee05c353de20..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);
@@ -91,19 +90,48 @@ namespace Mono.CSharp {
                                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)
-                                       RegisterNamespace (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 ())
-                               RegisterNamespace (t.Namespace);
-               }
-               
                protected static Type GetTypeInAssembly (Assembly assembly, string name)
                {
                        Type t = assembly.GetType (name);
@@ -257,6 +285,7 @@ namespace Mono.CSharp {
                IDictionary declspaces;
                Hashtable cached_types;
                RootNamespace root;
+               ArrayList external_exmethod_classes;
 
                public readonly MemberName MemberName;
 
@@ -315,7 +344,7 @@ namespace Mono.CSharp {
                        if (name.IndexOf ('`') > 0) {
                                FullNamedExpression retval = Lookup (ds, SimpleName.RemoveGenericArity (name), loc);
                                if (retval != null) {
-                                       Error_TypeArgumentsCannotBeUsed (retval.Type, loc, "type");
+                                       Error_TypeArgumentsCannotBeUsed (retval.Type, loc);
                                        return;
                                }
                        } else {
@@ -337,11 +366,22 @@ namespace Mono.CSharp {
                                TypeManager.CSharpName(t), TypeManager.GetNumberOfTypeArguments(t).ToString());
                }
 
-               public static void Error_TypeArgumentsCannotBeUsed(Type t, Location loc, string symbol)
+               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(t);
-                       Report.Error(308, loc, "The non-generic {0} `{1}' cannot be used with the type argument(s)",
-                               symbol, TypeManager.CSharpName(t));
+                       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)
@@ -438,11 +478,62 @@ namespace Mono.CSharp {
                        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)
@@ -534,7 +625,7 @@ 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.ToString ());
+                                               "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces", Name.GetSignatureForError ());
                                }
                                return resolved;
                        }
@@ -762,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");
 
@@ -799,7 +889,7 @@ namespace Mono.CSharp {
                        }
 
                        if (name == "global") {
-                               Report.Error (1681, loc, "You cannot redefine the global extern alias");
+                               Error_GlobalNamespaceRedefined (loc);
                                return;
                        }
 
@@ -809,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.
@@ -822,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);
                }
@@ -943,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?",