Fixed string null constant conversion to object constant
[mono.git] / mcs / mcs / namespace.cs
index 6b44a9f8d8e132bce1a5e2f0f3ae2464ba2d8290..f454b93498665cd1da1c66d74f2f4d03f96d42e9 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.Forms";
+                               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);
                        }
                }
 
@@ -199,16 +276,7 @@ namespace Mono.CSharp {
 
                public Namespace AddNamespace (MemberName name)
                {
-                       Namespace ns_parent;
-                       if (name.Left != null) {
-                               if (parent != null)
-                                       ns_parent = parent.AddNamespace (name.Left);
-                               else
-                                       ns_parent = AddNamespace (name.Left);
-                       } else {
-                               ns_parent = this;
-                       }
-
+                       var ns_parent = name.Left == null ? this : AddNamespace (name.Left);
                        return ns_parent.TryAddNamespace (name.Basename);
                }
 
@@ -301,8 +369,10 @@ namespace Mono.CSharp {
                                        if (mode != LookupMode.Normal)
                                                continue;
 
-                                       if (ts.MemberDefinition.IsImported)
+                                       if (ts.MemberDefinition.IsImported) {
+                                               ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (best);
                                                ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ts);
+                                       }
 
                                        ctx.Module.Compiler.Report.Warning (436, 2, loc,
                                                "The type `{0}' conflicts with the imported type of same name'. Ignoring the imported type definition",
@@ -421,12 +491,14 @@ namespace Mono.CSharp {
                                types = new Dictionary<string, IList<TypeSpec>> (64);
                        }
 
-                       if ((ts.IsStatic || ts.MemberDefinition.IsPartial) && 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;
@@ -571,6 +643,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
@@ -677,6 +754,11 @@ namespace Mono.CSharp {
 
                        return Compiler.Settings.IsConditionalSymbolDefined (value);
                }
+
+               public override void Accept (StructuralVisitor visitor)
+               {
+                       visitor.Visit (this);
+               }
        }
 
 
@@ -810,7 +892,7 @@ namespace Mono.CSharp {
                        MemberCore mc;
                        if (names_container.DefinedNames.TryGetValue (name, out mc)) {
                                if (tc is NamespaceContainer && mc is NamespaceContainer) {
-                                       containers.Add (tc);
+                                       AddTypeContainerMember (tc);
                                        return;
                                }
 
@@ -823,13 +905,33 @@ namespace Mono.CSharp {
                                }
                        } else {
                                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)
@@ -960,6 +1062,9 @@ namespace Mono.CSharp {
 
                public override void GetCompletionStartingWith (string prefix, List<string> results)
                {
+                       if (Usings == null)
+                               return;
+
                        foreach (var un in Usings) {
                                if (un.Alias != null)
                                        continue;
@@ -1035,7 +1140,7 @@ namespace Mono.CSharp {
                        if (aliases != null && arity == 0) {
                                UsingAliasNamespace uan;
                                if (aliases.TryGetValue (name, out uan)) {
-                                       if (fne != null) {
+                                       if (fne != null && mode != LookupMode.Probing) {
                                                // TODO: Namespace has broken location
                                                //Report.SymbolRelatedToPreviousError (fne.Location, null);
                                                Compiler.Report.SymbolRelatedToPreviousError (uan.Location, null);
@@ -1106,44 +1211,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)
@@ -1228,8 +1295,9 @@ namespace Mono.CSharp {
                        }
                }
 
-               public void EnableUsingClausesRedefinition ()
+               public void EnableRedefinition ()
                {
+                       is_defined = false;
                        namespace_using_table = null;
                }
 
@@ -1265,6 +1333,11 @@ namespace Mono.CSharp {
 
                        return false;
                }
+
+               public override void Accept (StructuralVisitor visitor)
+               {
+                       visitor.Visit (this);
+               }
        }
 
        public class UsingNamespace
@@ -1325,6 +1398,11 @@ namespace Mono.CSharp {
                                }
                        }
                }
+
+               public override string ToString()
+               {
+                       return resolved.ToString();
+               }
        }
 
        public class UsingExternAlias : UsingAliasNamespace