2007-01-25 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / namespace.cs
index c30b4cab73ef50b2cd5071339912eedb284193f0..af9c93b0d6b3744526fb375606039b4e942eecf1 100644 (file)
@@ -3,6 +3,7 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
+//   Marek Safar (marek.safar@seznam.cz)
 //
 // (C) 2001 Ximian, Inc.
 //
@@ -86,7 +87,7 @@ namespace Mono.CSharp {
 
                protected void EnsureNamespace (string dotted_name)
                {
-                       if (dotted_name != null && dotted_name != "" && ! IsNamespace (dotted_name))
+                       if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
                                GetNamespace (dotted_name, true);
                }
 
@@ -111,12 +112,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;
@@ -183,6 +187,12 @@ namespace Mono.CSharp {
                                EnsureNamespace (t.Namespace);
                }
 
+               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);
+               }
+
                public override Type LookupTypeReflection (string name, Location loc)
                {
                        Type found_type = null;
@@ -214,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;
                                }
                        }
@@ -253,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;
@@ -278,7 +294,7 @@ namespace Mono.CSharp {
 
                        if (parent != null && parent.MemberName != MemberName.Null)
                                MemberName = new MemberName (parent.MemberName, name);
-                       else if (name == "")
+                       else if (name.Length == 0)
                                MemberName = MemberName.Null;
                        else
                                MemberName = new MemberName (name);
@@ -294,6 +310,40 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               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, "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?",
+                               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, 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 ()");
@@ -352,9 +402,11 @@ namespace Mono.CSharp {
                                        t = tdecl.TypeBuilder;
                                }
                        }
-                       string lookup = t != null ? t.FullName : (fullname == "" ? name : fullname + "." + name);
+                       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);
@@ -362,6 +414,25 @@ 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))
@@ -416,6 +487,8 @@ namespace Mono.CSharp {
                public bool DeclarationFound = false;
                bool UsingFound;
 
+               public readonly DeclSpace SlaveDeclSpace;
+
                ListDictionary extern_aliases;
 
                static ArrayList entries = new ArrayList ();
@@ -432,7 +505,7 @@ 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;
@@ -453,11 +526,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 = Expr.ResolveAsTypeStep (this, false);
                                if (fne == null) {
                                        Error_NamespaceNotFound (Location, Name.ToString ());
                                        return null;
@@ -466,10 +535,25 @@ 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;
                        }
+
+                       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 {
@@ -500,8 +584,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) :
@@ -512,19 +595,40 @@ 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)
-                                       Error_NamespaceNotFound (Location, Alias.ToString ());
+                                       return null;
+
+                               if (resolved.Type != null) {
+                                       TypeAttributes attr = resolved.Type.Attributes & TypeAttributes.VisibilityMask;
+                                       if (attr == TypeAttributes.NestedPrivate || attr == TypeAttributes.NestedFamily ||
+                                               ((attr == TypeAttributes.NestedFamORAssem || attr == TypeAttributes.NestedAssembly) && 
+                                               TypeManager.LookupDeclSpace (resolved.Type) == null)) {
+                                               Expression.ErrorIsInaccesible (Alias.Location, Alias.ToString ());
+                                               return null;
+                                       }
+                               }
+
                                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)
                        {
@@ -541,7 +645,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name, Location loc)
+               public NamespaceEntry (NamespaceEntry parent, SourceFile file, string name)
                {
                        this.parent = parent;
                        this.file = file;
@@ -554,9 +658,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;
@@ -564,6 +669,7 @@ namespace Mono.CSharp {
                        this.ID = -1;
                        this.IsImplicit = true;
                        this.ns = ns;
+                       this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
                }
 
                //
@@ -580,7 +686,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;
                        }
                }
@@ -603,7 +709,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;
                        }