Merge pull request #498 from Unroll-Me/master
[mono.git] / mcs / mcs / namespace.cs
index 8d70e793b7fa4fe3ab444f4f7eca212d5bf10a8d..7dd44807f6810e358d40b258aed1bbce65ddb369 100644 (file)
@@ -41,6 +41,46 @@ namespace Mono.CSharp {
                        report.Error (1681, loc, "The global extern alias cannot be redefined");
                }
 
+               //
+               // For better error reporting where we try to guess missing using directive
+               //
+               public List<string> FindTypeNamespaces (IMemberContext ctx, string name, int arity)
+               {
+                       List<string> res = null;
+
+                       foreach (var ns in all_namespaces) {
+                               var type = ns.Value.LookupType (ctx, name, arity, LookupMode.Normal, Location.Null);
+                               if (type != null) {
+                                       if (res == null)
+                                               res = new List<string> ();
+
+                                       res.Add (ns.Key);
+                               }
+                       }
+
+                       return res;
+               }
+
+               //
+               // For better error reporting where compiler tries to guess missing using directive
+               //
+               public List<string> FindExtensionMethodNamespaces (IMemberContext ctx, TypeSpec extensionType, string name, int arity)
+               {
+                       List<string> res = null;
+
+                       foreach (var ns in all_namespaces) {
+                               var methods = ns.Value.LookupExtensionMethod (ctx, extensionType, name, arity);
+                               if (methods != null) {
+                                       if (res == null)
+                                               res = new List<string> ();
+
+                                       res.Add (ns.Key);
+                               }
+                       }
+
+                       return res;
+               }
+
                public void RegisterNamespace (Namespace child)
                {
                        if (child != this)
@@ -181,14 +221,51 @@ namespace Mono.CSharp {
                                return;
                        }
 
+                       string assembly = null;
+                       string possible_name = fullname + "." + name;
+
+                       // Only assembly unique name should be added
+                       switch (possible_name) {
+                       case "System.Drawing":
+                       case "System.Web.Services":
+                       case "System.Web":
+                       case "System.Data":
+                       case "System.Configuration":
+                       case "System.Data.Services":
+                       case "System.DirectoryServices":
+                       case "System.Json":
+                       case "System.Net.Http":
+                       case "System.Numerics":
+                       case "System.Runtime.Caching":
+                       case "System.ServiceModel":
+                       case "System.Transactions":
+                       case "System.Web.Routing":
+                       case "System.Xml.Linq":
+                       case "System.Xml":
+                               assembly = possible_name;
+                               break;
+
+                       case "System.Linq":
+                       case "System.Linq.Expressions":
+                               assembly = "System.Core";
+                               break;
+
+                       case "System.Windows.Forms":
+                       case "System.Windows.Forms.Layout":
+                               assembly = "System.Windows.Name";
+                               break;
+                       }
+
+                       assembly = assembly == null ? "an" : "`" + assembly + "'";
+
                        if (this is GlobalRootNamespace) {
                                ctx.Module.Compiler.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);
+                                       "The type or namespace name `{0}' could not be found in the global namespace. Are you missing {1} assembly reference?",
+                                       name, assembly);
                        } else {
                                ctx.Module.Compiler.Report.Error (234, loc,
-                                       "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
-                                       name, GetSignatureForError ());
+                                       "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing {2} assembly reference?",
+                                       name, GetSignatureForError (), assembly);
                        }
                }
 
@@ -209,10 +286,16 @@ namespace Mono.CSharp {
                                ns_parent = this;
                        }
 
+                       return ns_parent.TryAddNamespace (name.Basename);
+               }
+
+               Namespace TryAddNamespace (string name)
+               {
                        Namespace ns;
-                       if (!ns_parent.namespaces.TryGetValue (name.Basename, out ns)) {
-                               ns = new Namespace (ns_parent, name.Basename);
-                               ns_parent.namespaces.Add (name.Basename, ns);
+
+                       if (!namespaces.TryGetValue (name, out ns)) {
+                               ns = new Namespace (this, name);
+                               namespaces.Add (name, ns);
                        }
 
                        return ns;
@@ -415,12 +498,14 @@ namespace Mono.CSharp {
                                types = new Dictionary<string, IList<TypeSpec>> (64);
                        }
 
-                       if (ts.IsStatic && ts.Arity == 0 &&
-                               (ts.MemberDefinition.DeclaringAssembly == null || ts.MemberDefinition.DeclaringAssembly.HasExtensionMethod)) {
-                               if (extension_method_types == null)
-                                       extension_method_types = new List<TypeSpec> ();
+                       if (ts.IsClass && ts.Arity == 0) {
+                               var extension_method_allowed = ts.MemberDefinition.IsImported ? (ts.Modifiers & Modifiers.METHOD_EXTENSION) != 0 : (ts.IsStatic || ts.MemberDefinition.IsPartial);
+                               if (extension_method_allowed) {
+                                       if (extension_method_types == null)
+                                               extension_method_types = new List<TypeSpec> ();
 
-                               extension_method_types.Add (ts);
+                                       extension_method_types.Add (ts);
+                               }
                        }
 
                        var name = ts.Name;
@@ -565,6 +650,11 @@ namespace Mono.CSharp {
                                        "Identifier `{0}' differing only in case is not CLS-compliant", compiled.GetSignatureForError ());
                        }
                }
+
+               public override string ToString ()
+               {
+                       return Name;
+               }
        }
 
        public class CompilationSourceFile : NamespaceContainer
@@ -799,8 +889,10 @@ namespace Mono.CSharp {
                                name = mn.Name;
                        }
 
+                       var names_container = Parent == null ? Module : (TypeContainer) this;
+
                        MemberCore mc;
-                       if (defined_names.TryGetValue (name, out mc)) {
+                       if (names_container.DefinedNames.TryGetValue (name, out mc)) {
                                if (tc is NamespaceContainer && mc is NamespaceContainer) {
                                        containers.Add (tc);
                                        return;
@@ -814,14 +906,34 @@ namespace Mono.CSharp {
                                                GetSignatureForError (), mn.GetSignatureForError ());
                                }
                        } else {
-                               defined_names.Add (name, tc);
+                               names_container.DefinedNames.Add (name, tc);
+
+                               var tdef = tc.PartialContainer;
+                               if (tdef != null) {
+                                       //
+                                       // Same name conflict in different namespace containers
+                                       //
+                                       var conflict = ns.GetAllTypes (name);
+                                       if (conflict != null) {
+                                               foreach (var e in conflict) {
+                                                       if (e.Arity == mn.Arity) {
+                                                               mc = (MemberCore) e.MemberDefinition;
+                                                               break;
+                                                       }
+                                               }
+                                       }
+
+                                       if (mc != null) {
+                                               Report.SymbolRelatedToPreviousError (mc);
+                                               Report.Error (101, tc.Location, "The namespace `{0}' already contains a definition for `{1}'",
+                                                       GetSignatureForError (), mn.GetSignatureForError ());
+                                       } else {
+                                               ns.AddType (Module, tdef.Definition);
+                                       }
+                               }
                        }
 
                        base.AddTypeContainer (tc);
-
-                       var tdef = tc.PartialContainer;
-                       if (tdef != null)
-                               ns.AddType (Module, tdef.Definition);
                }
 
                public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
@@ -836,7 +948,7 @@ namespace Mono.CSharp {
                        base.EmitContainer ();
                }
 
-               public static ExtensionMethodCandidates LookupExtensionMethod (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, NamespaceContainer container, int position)
+               public ExtensionMethodCandidates LookupExtensionMethod (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, int position)
                {
                        //
                        // Here we try to resume the search for extension method at the point
@@ -857,7 +969,8 @@ namespace Mono.CSharp {
                        // checked before we hit A.N1 using
                        //
                        ExtensionMethodCandidates candidates;
-                       for (; container != null; container = container.Parent) {
+                       var container = this;
+                       do {
                                candidates = container.LookupExtensionMethodCandidates (invocationContext, extensionType, name, arity, ref position);
                                if (candidates != null || container.MemberName == null)
                                        return candidates;
@@ -883,7 +996,8 @@ namespace Mono.CSharp {
                                }
 
                                position = 0;
-                       }
+                               container = container.Parent;
+                       } while (container != null);
 
                        return null;
                }
@@ -1096,44 +1210,6 @@ namespace Mono.CSharp {
                        return match;
                }
 
-               static void MsgtryRef (string s)
-               {
-                       Console.WriteLine ("    Try using -r:" + s);
-               }
-
-               static void MsgtryPkg (string s)
-               {
-                       Console.WriteLine ("    Try using -pkg:" + s);
-               }
-
-               public static void Error_NamespaceNotFound (Location loc, string name, Report Report)
-               {
-                       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);
-
-                       switch (name) {
-                       case "Gtk": case "GtkSharp":
-                               MsgtryPkg ("gtk-sharp-2.0");
-                               break;
-
-                       case "Gdk": case "GdkSharp":
-                               MsgtryPkg ("gdk-sharp-2.0");
-                               break;
-
-                       case "Glade": case "GladeSharp":
-                               MsgtryPkg ("glade-sharp-2.0");
-                               break;
-
-                       case "System.Drawing":
-                       case "System.Web.Services":
-                       case "System.Web":
-                       case "System.Data":
-                       case "System.Windows.Forms":
-                               MsgtryRef (name);
-                               break;
-                       }
-               }
-
                protected override void DefineNamespace ()
                {
                        if (namespace_using_table == null)
@@ -1176,11 +1252,23 @@ namespace Mono.CSharp {
 
                                        entry.Define (this);
 
+                                       //
+                                       // It's needed for repl only, when using clause cannot be resolved don't hold it in
+                                       // global list which is resolved for each evaluation
+                                       //
+                                       if (entry.ResolvedExpression == null) {
+                                               clauses.RemoveAt (i--);
+                                               continue;
+                                       }
+
                                        Namespace using_ns = entry.ResolvedExpression as Namespace;
                                        if (using_ns == null)
                                                continue;
 
                                        if (list.Contains (using_ns)) {
+                                               // Ensure we don't report the warning multiple times in repl
+                                               clauses.RemoveAt (i--);
+
                                                Compiler.Report.Warning (105, 3, entry.Location,
                                                        "The using directive for `{0}' appeared previously in this namespace", using_ns.GetSignatureForError ());
                                        } else {
@@ -1206,8 +1294,9 @@ namespace Mono.CSharp {
                        }
                }
 
-               public void EnableUsingClausesRedefinition ()
+               public void EnableRedefinition ()
                {
+                       is_defined = false;
                        namespace_using_table = null;
                }
 
@@ -1303,6 +1392,11 @@ namespace Mono.CSharp {
                                }
                        }
                }
+
+               public override string ToString()
+               {
+                       return resolved.ToString();
+               }
        }
 
        public class UsingExternAlias : UsingAliasNamespace