2007-01-28 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / mcs / namespace.cs
index b018750a8e21ad8cdd99ccab252da1284bdf0334..af9c93b0d6b3744526fb375606039b4e942eecf1 100644 (file)
@@ -187,7 +187,7 @@ namespace Mono.CSharp {
                                EnsureNamespace (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);
@@ -224,11 +224,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;
                                }
                        }
@@ -263,7 +269,7 @@ namespace Mono.CSharp {
                {
                        // Expression members.
                        this.eclass = ExprClass.Namespace;
-                       this.Type = null;
+                       this.Type = typeof (Namespace);
                        this.loc = Location.Null;
 
                        this.parent = parent;
@@ -304,12 +310,20 @@ 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)
                {
-                       Type t = LookForAnyGenericType (name);
-                       if (t != null) {
-                               Error_InvalidNumberOfTypeArguments(t, loc);
-                               return;
+                       if (name.IndexOf ('`') > 0) {
+                               FullNamedExpression retval = Lookup (ds, SimpleName.RemoveGenericArity (name), loc);
+                               if (retval != null) {
+                                       Error_TypeArgumentsCannotBeUsed (retval.Type, loc, "type");
+                                       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?",
@@ -323,6 +337,13 @@ namespace Mono.CSharp {
                                TypeManager.CSharpName(t), TypeManager.GetNumberOfTypeArguments(t).ToString());
                }
 
+               public static void Error_TypeArgumentsCannotBeUsed(Type t, Location loc, string symbol)
+               {
+                       Report.SymbolRelatedToPreviousError(t);
+                       Report.Error(308, loc, "The non-generic {0} `{1}' cannot be used with the type argument(s)",
+                               symbol, TypeManager.CSharpName(t));
+               }
+
                public override void Emit (EmitContext ec)
                {
                        throw new InternalErrorException ("Expression tree referenced namespace " + fullname + " during Emit ()");
@@ -383,7 +404,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);
@@ -396,17 +419,16 @@ namespace Mono.CSharp {
                /// 
                public Type LookForAnyGenericType (string typeName)
                {
-                       typeName = SimpleName.RemoveGenericArity(typeName);
+                       if (declspaces == null)
+                               return null;
 
-                       foreach (string type_item in declspaces.Keys) {
-                               string[] sep_type = type_item.Split('`');
-                               if (sep_type.Length < 2)
-                                       continue;
+                       typeName = SimpleName.RemoveGenericArity (typeName);
 
-                               if (typeName == sep_type [0]) {
-                                       DeclSpace tdecl = (DeclSpace)declspaces [type_item];
-                                       return tdecl.TypeBuilder;
-                               }
+                       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;
                }
@@ -513,7 +535,7 @@ namespace Mono.CSharp {
                                resolved = fne as Namespace;
                                if (resolved == null) {
                                        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.ToString ());
                                }
                                return resolved;
                        }