Add some new classes/enums/delegates for 2.0 and some new CAS unit tests
[mono.git] / mcs / mcs / decl.cs
index c3be418d40738cf4cce5f4b3478fb2e6f726e14c..4f16835d2dad207999d2bd100439289c27ffabd3 100644 (file)
@@ -29,24 +29,56 @@ namespace Mono.CSharp {
        public class MemberName {
                public readonly string Name;
                public readonly MemberName Left;
+               public readonly Location Location;
+
+               bool is_double_colon;
 
                public static readonly MemberName Null = new MemberName ("");
 
-               public MemberName (string name)
+               private MemberName (MemberName left, string name, bool is_double_colon, Location loc)
                {
                        this.Name = name;
+                       this.Location = loc;
+                       this.is_double_colon = is_double_colon;
+                       this.Left = left;
+               }
+
+               public MemberName (string name)
+                       : this (null, name, false, Location.Null)
+               {
                }
 
                public MemberName (MemberName left, string name)
-                       : this (name)
+                       : this (left, name, false, left != null ? left.Location : Location.Null)
+               {
+               }
+
+               public MemberName (string name, Location loc)
+                       : this (null, name, false, loc)
+               {
+               }
+
+               public MemberName (MemberName left, string name, Location loc)
+                       : this (left, name, false, loc)
+               {
+               }
+
+               public MemberName (string alias, string name, Location loc)
+                       : this (new MemberName (alias), name, true, loc)
                {
-                       this.Left = left;
                }
 
                public MemberName (MemberName left, MemberName right)
+                       : this (left, right, right.Location)
                {
-                       Name = right.Name;
-                       Left = (right.Left == null) ? left : new MemberName (left, right.Left);
+               }
+
+               public MemberName (MemberName left, MemberName right, Location loc)
+                       : this (null, right.Name, false, loc)
+               {
+                       if (right.is_double_colon)
+                               throw new InternalErrorException ("Cannot append double_colon member name");
+                       this.Left = (right.Left == null) ? left : new MemberName (left, right.Left);
                }
 
                static readonly char [] dot_array = { '.' };
@@ -70,8 +102,9 @@ namespace Mono.CSharp {
                public string GetName (bool is_generic)
                {
                        string name = is_generic ? Basename : Name;
+                       string connect = is_double_colon ? "::" : ".";
                        if (Left != null)
-                               return Left.GetName (is_generic) + "." + name;
+                               return Left.GetName (is_generic) + connect + name;
                        else
                                return name;
                }
@@ -82,49 +115,51 @@ namespace Mono.CSharp {
                ///
                public string GetPartialName ()
                {
+                       string connect = is_double_colon ? "::" : ".";
                        if (Left != null)
-                               return Left.GetPartialName () + "." + Name;
+                               return Left.GetPartialName () + connect + Name;
                        else
                                return Name;
                }
 
                public string GetTypeName ()
                {
+                       string connect = is_double_colon ? "::" : ".";
                        if (Left != null)
-                               return Left.GetTypeName () + "." + Name;
+                               return Left.GetTypeName () + connect + Name;
                        else
                                return Name;
                }
 
-               public Expression GetTypeExpression (Location loc)
+               public Expression GetTypeExpression ()
                {
-                       if (Left != null) {
-                               Expression lexpr = Left.GetTypeExpression (loc);
-
-                               return new MemberAccess (lexpr, Name, loc);
-                       } else {
-                               return new SimpleName (Name, loc);
+                       if (Left == null)
+                               return new SimpleName (Name, Location);
+                       if (is_double_colon) {
+                               if (Left.Left != null)
+                                       throw new InternalErrorException ("The left side of a :: should be an identifier");
+                               return new QualifiedAliasMember (Left.Name, Name, Location);
                        }
+
+                       Expression lexpr = Left.GetTypeExpression ();
+                       return new MemberAccess (lexpr, Name, Location);
                }
 
                public MemberName Clone ()
                {
-                       if (Left != null)
-                               return new MemberName (Left.Clone (), Name);
-                       else
-                               return new MemberName (Name);
+                       MemberName left_clone = Left == null ? null : Left.Clone ();
+                       return new MemberName (left_clone, Name, is_double_colon, Location);
                }
 
                public string Basename {
-                       get {
-                               return Name;
-                       }
+                       get { return Name; }
                }
 
                public override string ToString ()
                {
+                       string connect = is_double_colon ? "::" : ".";
                        if (Left != null)
-                               return Left + "." + Name;
+                               return Left + connect + Name;
                        else
                                return Name;
                }
@@ -140,6 +175,8 @@ namespace Mono.CSharp {
                                return true;
                        if (other == null || Name != other.Name)
                                return false;
+                       if (is_double_colon != other.is_double_colon)
+                               return false;
 #if NET_2_0
                        if (TypeArguments == null)
                                return other.TypeArguments == null;
@@ -158,7 +195,8 @@ namespace Mono.CSharp {
                        int hash = Name.GetHashCode ();
                        for (MemberName n = Left; n != null; n = n.Left)
                                hash ^= n.Name.GetHashCode ();
-
+                       if (is_double_colon)
+                               hash ^= 0xbadc01d;
 #if NET_2_0
                        if (TypeArguments != null)
                                hash ^= TypeArguments.Count << 5;
@@ -202,7 +240,9 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Location where this declaration happens
                /// </summary>
-               public readonly Location Location;
+               public Location Location {
+                       get { return member_name.Location; }
+               }
 
                /// <summary>
                ///   XML documentation comment
@@ -237,8 +277,7 @@ namespace Mono.CSharp {
                /// </summary>
                internal Flags caching_flags;
 
-               public MemberCore (TypeContainer parent, MemberName name, Attributes attrs,
-                                  Location loc)
+               public MemberCore (TypeContainer parent, MemberName name, Attributes attrs)
                        : base (attrs)
                {
                        if (parent is PartialContainer && !(this is PartialContainer))
@@ -246,7 +285,6 @@ namespace Mono.CSharp {
 
                        Parent = parent;
                        member_name = name;
-                       Location = loc;
                        caching_flags = Flags.Obsolete_Undetected | Flags.ClsCompliance_Undetected | Flags.HasCompliantAttribute_Undetected | Flags.Excluded_Undetected;
                }
 
@@ -256,21 +294,6 @@ namespace Mono.CSharp {
                        cached_name = null;
                }
 
-               /// <summary>
-               /// Tests presence of ObsoleteAttribute and report proper error
-               /// </summary>
-               protected void CheckUsageOfObsoleteAttribute (Type type)
-               {
-                       if (type == null)
-                               return;
-
-                       ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (type);
-                       if (obsolete_attr == null)
-                               return;
-
-                       AttributeTester.Report_ObsoleteMessage (obsolete_attr, type.FullName, Location);
-               }
-
                public abstract bool Define ();
 
                // 
@@ -289,16 +312,19 @@ namespace Mono.CSharp {
                /// </summary>
                public virtual void Emit ()
                {
-                       // Hack with Parent == null is for EnumMember
-                       if (Parent == null || (GetObsoleteAttribute (Parent) == null && Parent.GetObsoleteAttribute (Parent) == null))
-                               VerifyObsoleteAttribute ();
-
                        if (!RootContext.VerifyClsCompliance)
                                return;
 
                        VerifyClsCompliance (Parent);
                }
 
+               public virtual EmitContext EmitContext
+               {
+                       get {
+                               return Parent.EmitContext;
+                       }
+               }
+
                public bool InUnsafe {
                        get {
                                return ((ModFlags & Modifiers.UNSAFE) != 0) || Parent.UnsafeContext;
@@ -337,7 +363,7 @@ namespace Mono.CSharp {
                /// <summary>
                /// Returns instance of ObsoleteAttribute for this MemberCore
                /// </summary>
-               public ObsoleteAttribute GetObsoleteAttribute (DeclSpace ds)
+               public virtual ObsoleteAttribute GetObsoleteAttribute ()
                {
                        // ((flags & (Flags.Obsolete_Undetected | Flags.Obsolete)) == 0) is slower, but why ?
                        if ((caching_flags & Flags.Obsolete_Undetected) == 0 && (caching_flags & Flags.Obsolete) == 0) {
@@ -350,11 +376,11 @@ namespace Mono.CSharp {
                                return null;
 
                        Attribute obsolete_attr = OptAttributes.Search (
-                               TypeManager.obsolete_attribute_type, ds.EmitContext);
+                               TypeManager.obsolete_attribute_type, EmitContext);
                        if (obsolete_attr == null)
                                return null;
 
-                       ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute (ds.EmitContext);
+                       ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute (EmitContext);
                        if (obsolete == null)
                                return null;
 
@@ -362,6 +388,34 @@ namespace Mono.CSharp {
                        return obsolete;
                }
 
+               /// <summary>
+               /// Checks for ObsoleteAttribute presence. It's used for testing of all non-types elements
+               /// </summary>
+               public virtual void CheckObsoleteness (Location loc)
+               {
+                       if (Parent != null)
+                               Parent.CheckObsoleteness (loc);
+
+                       ObsoleteAttribute oa = GetObsoleteAttribute ();
+                       if (oa == null) {
+                               return;
+                       }
+
+                       AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc);
+               }
+
+               protected void CheckObsoleteType (Expression type)
+               {
+                       ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (type.Type);
+                       if (obsolete_attr == null)
+                               return;
+
+                       if (GetObsoleteAttribute () != null || Parent.GetObsoleteAttribute () != null)
+                               return;
+
+                       AttributeTester.Report_ObsoleteMessage (obsolete_attr, TypeManager.CSharpName (type.Type), type.Location);
+               }
+
                /// <summary>
                /// Analyze whether CLS-Compliant verification must be execute for this MemberCore.
                /// </summary>
@@ -463,8 +517,6 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               protected abstract void VerifyObsoleteAttribute ();
-
                //
                // Raised (and passed an XmlElement that contains the comment)
                // when GenerateDocComment is writing documentation expectedly.
@@ -503,7 +555,7 @@ namespace Mono.CSharp {
        ///   provides the common foundation for managing those name
        ///   spaces.
        /// </remarks>
-       public abstract class DeclSpace : MemberCore, IAlias {
+       public abstract class DeclSpace : MemberCore {
                /// <summary>
                ///   This points to the actual definition that is being
                ///   created with System.Reflection.Emit
@@ -525,15 +577,17 @@ namespace Mono.CSharp {
                // The emit context for toplevel objects.
                protected EmitContext ec;
                
-               public EmitContext EmitContext {
-                       get { return ec; }
+               public override EmitContext EmitContext {
+                       get {
+                               return ec;
+                       }
                }
 
                static string[] attribute_targets = new string [] { "type" };
 
                public DeclSpace (NamespaceEntry ns, TypeContainer parent, MemberName name,
-                                 Attributes attrs, Location l)
-                       : base (parent, name, attrs, l)
+                                 Attributes attrs)
+                       : base (parent, name, attrs)
                {
                        NamespaceEntry = ns;
                        Basename = name.Name;
@@ -561,14 +615,22 @@ namespace Mono.CSharp {
                        if (symbol.MarkForDuplicationCheck () && mc.MarkForDuplicationCheck ())
                                return true;
 
-                       if (this is RootTypes) {
-                               // TODO: It should not reach this once we merge RecordDecl to AddTo
-                               return true;
+                       Report.SymbolRelatedToPreviousError (mc);
+                       if (symbol is PartialContainer || mc is PartialContainer) {
+                               Report.Error (260, symbol.Location,
+                                       "Missing partial modifier on declaration of type `{0}'. Another partial declaration of this type exists",
+                                       name);
+                               return false;
                        }
 
-                       Report.SymbolRelatedToPreviousError (mc);
-                       Report.Error (102, symbol.Location, "The type `{0}' already contains a definition for `{1}'",
-                               GetSignatureForError (), symbol.MemberName.Name);
+                       if (this is RootTypes) {
+                               Report.Error (101, symbol.Location, 
+                                       "The namespace `{0}' already contains a definition for `{1}'",
+                                       ((DeclSpace)symbol).NamespaceEntry.GetSignatureForError (), symbol.MemberName.Name);
+                       } else {
+                               Report.Error (102, symbol.Location, "The type `{0}' already contains a definition for `{1}'",
+                                       GetSignatureForError (), symbol.MemberName.Name);
+                       }
                        return false;
                }
 
@@ -588,9 +650,7 @@ namespace Mono.CSharp {
                // why there is a non-obvious test down here.
                //
                public bool IsTopLevel {
-                       get {
-                               return (Parent != null && Parent.Parent == null);
-                       }
+                       get { return (Parent != null && Parent.Parent == null); }
                }
 
                public virtual void CloseType ()
@@ -615,9 +675,7 @@ namespace Mono.CSharp {
                }
 
                protected virtual TypeAttributes TypeAttr {
-                       get {
-                               return CodeGen.Module.DefaultCharSetType;
-                       }
+                       get { return CodeGen.Module.DefaultCharSetType; }
                }
 
                /// <remarks>
@@ -857,12 +915,6 @@ namespace Mono.CSharp {
                        return ~ (~ mAccess | pAccess) == 0;
                }
 
-               public static void Error_AmbiguousTypeReference (Location loc, string name, string t1, string t2)
-               {
-                       Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
-                                     name, t1, t2);
-               }
-
                //
                // Return the nested type with name @name.  Ensures that the nested type
                // is defined if necessary.  Do _not_ use this when you have a MemberCache handy.
@@ -901,8 +953,7 @@ namespace Mono.CSharp {
                }
 
                //
-               // Public function used to locate types, this can only
-               // be used after the ResolveTree function has been invoked.
+               // Public function used to locate types.
                //
                // Set 'ignore_cs0104' to true if you want to ignore cs0104 errors.
                //
@@ -993,25 +1044,37 @@ namespace Mono.CSharp {
                }
 
                public override string[] ValidAttributeTargets {
-                       get {
-                               return attribute_targets;
-                       }
+                       get { return attribute_targets; }
                }
 
-               bool IAlias.IsType {
-                       get { return true; }
-               }
+               protected override bool VerifyClsCompliance (DeclSpace ds)
+               {
+                       if (!base.VerifyClsCompliance (ds)) {
+                               return false;
+                       }
 
-               string IAlias.Name {
-                       get { return Name; }
-               }
+                       IDictionary cache = TypeManager.AllClsTopLevelTypes;
+                       string lcase = Name.ToLower (System.Globalization.CultureInfo.InvariantCulture);
+                       if (!cache.Contains (lcase)) {
+                               cache.Add (lcase, this);
+                               return true;
+                       }
 
-               TypeExpr IAlias.ResolveAsType (EmitContext ec)
-               {
-                       if (TypeBuilder == null)
-                               throw new InvalidOperationException ();
+                       object val = cache [lcase];
+                       if (val == null) {
+                               Type t = AttributeTester.GetImportedIgnoreCaseClsType (lcase);
+                               if (t == null)
+                                       return true;
+                               Report.SymbolRelatedToPreviousError (t);
+                       }
+                       else {
+                               if (val is PartialContainer)
+                                       return true;
 
-                       return new TypeExpression (TypeBuilder, Location);
+                               Report.SymbolRelatedToPreviousError ((DeclSpace)val);
+                       }
+                       Report.Error (3005, Location, "Identifier `{0}' differing only in case is not CLS-compliant", GetSignatureForError ());
+                       return true;
                }
        }