2004-12-22 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / mcs / decl.cs
old mode 100755 (executable)
new mode 100644 (file)
index bdee3c3..52d6326
@@ -7,6 +7,7 @@
 // Licensed under the terms of the GNU GPL
 //
 // (C) 2001 Ximian, Inc (http://www.ximian.com)
+// (C) 2004 Novell, Inc
 //
 // TODO: Move the method verification stuff from the class.cs and interface.cs here
 //
@@ -17,6 +18,12 @@ using System.Globalization;
 using System.Reflection.Emit;
 using System.Reflection;
 
+#if BOOTSTRAP_WITH_OLDLIB
+using XmlElement = System.Object;
+#else
+using System.Xml;
+#endif
+
 namespace Mono.CSharp {
 
        public class MemberName {
@@ -55,10 +62,14 @@ namespace Mono.CSharp {
                                return name;
                }
 
-               public string GetFullName ()
+               ///
+               /// This returns exclusively the name as seen on the source code
+               /// it is not the fully qualifed type after resolution
+               ///
+               public string GetPartialName ()
                {
                        if (Left != null)
-                               return Left.GetFullName () + "." + Name;
+                               return Left.GetPartialName () + "." + Name;
                        else
                                return Name;
                }
@@ -98,7 +109,10 @@ namespace Mono.CSharp {
 
                public override string ToString ()
                {
-                       return GetFullName ();
+                       if (Left != null)
+                               return Left + "." + Name;
+                       else
+                               return Name;
                }
        }
 
@@ -117,7 +131,8 @@ namespace Mono.CSharp {
                        }
                }
 
-               public readonly MemberName MemberName;
+                // Is not readonly because of IndexerName attribute
+               public MemberName MemberName;
 
                /// <summary>
                ///   Modifier flags that the user specified in the source code
@@ -131,6 +146,17 @@ namespace Mono.CSharp {
                /// </summary>
                public readonly Location Location;
 
+               /// <summary>
+               ///   XML documentation comment
+               /// </summary>
+               public string DocComment;
+
+               /// <summary>
+               ///   Represents header string for documentation comment 
+               ///   for each member types.
+               /// </summary>
+               public abstract string DocCommentHeader { get; }
+
                [Flags]
                public enum Flags {
                        Obsolete_Undetected = 1,                // Obsolete attribute has not been detected yet
@@ -199,7 +225,7 @@ namespace Mono.CSharp {
                /// </summary>
                public virtual void Emit ()
                {
-                       // Hack with Parent == null is for EnumMember 
+                       // Hack with Parent == null is for EnumMember
                        if (Parent == null || (GetObsoleteAttribute (Parent) == null && Parent.GetObsoleteAttribute (Parent) == null))
                                VerifyObsoleteAttribute ();
 
@@ -248,15 +274,12 @@ namespace Mono.CSharp {
                        if (OptAttributes == null)
                                return null;
 
-                       // TODO: remove this allocation
-                       EmitContext ec = new EmitContext (ds.Parent, ds, ds.Location,
-                               null, null, ds.ModFlags, false);
-
-                       Attribute obsolete_attr = OptAttributes.Search (TypeManager.obsolete_attribute_type, ec);
+                       Attribute obsolete_attr = OptAttributes.Search (
+                               TypeManager.obsolete_attribute_type, ds.EmitContext);
                        if (obsolete_attr == null)
                                return null;
 
-                       ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute (ds);
+                       ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute (ds.EmitContext);
                        if (obsolete == null)
                                return null;
 
@@ -285,7 +308,7 @@ namespace Mono.CSharp {
                /// <summary>
                /// Returns true when MemberCore is exposed from assembly.
                /// </summary>
-               protected bool IsExposedFromAssembly (DeclSpace ds)
+               public bool IsExposedFromAssembly (DeclSpace ds)
                {
                        if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
                                return false;
@@ -305,12 +328,11 @@ namespace Mono.CSharp {
                bool GetClsCompliantAttributeValue (DeclSpace ds)
                {
                        if (OptAttributes != null) {
-                               EmitContext ec = new EmitContext (ds.Parent, ds, ds.Location,
-                                                                 null, null, ds.ModFlags, false);
-                               Attribute cls_attribute = OptAttributes.GetClsCompliantAttribute (ec);
+                               Attribute cls_attribute = OptAttributes.Search (
+                                       TypeManager.cls_compliant_attribute_type, ds.EmitContext);
                                if (cls_attribute != null) {
                                        caching_flags |= Flags.HasClsCompliantAttribute;
-                                       return cls_attribute.GetClsCompliantAttributeValue (ds);
+                                       return cls_attribute.GetClsCompliantAttributeValue (ds.EmitContext);
                                }
                        }
                        return ds.GetClsCompliantAttributeValue ();
@@ -325,7 +347,6 @@ namespace Mono.CSharp {
                        }
                }
 
-
                /// <summary>
                /// The main virtual method for CLS-Compliant verifications.
                /// The method returns true if member is CLS-Compliant and false if member is not
@@ -335,8 +356,11 @@ namespace Mono.CSharp {
                protected virtual bool VerifyClsCompliance (DeclSpace ds)
                {
                        if (!IsClsCompliaceRequired (ds)) {
-                               if ((RootContext.WarningLevel >= 2) && HasClsCompliantAttribute && !IsExposedFromAssembly (ds)) {
-                                       Report.Warning (3019, Location, "CLS compliance checking will not be performed on '{0}' because it is private or internal", GetSignatureForError ());
+                               if (HasClsCompliantAttribute && RootContext.WarningLevel >= 2) {
+                                       if (!IsExposedFromAssembly (ds))
+                                               Report.Warning (3019, Location, "CLS compliance checking will not be performed on '{0}' because it is private or internal", GetSignatureForError ());
+                                       if (!CodeGen.Assembly.IsClsCompliant)
+                                               Report.Warning (3021, Location, "'{0}' does not need a CLSCompliant attribute because the assembly does not have a CLSCompliant attribute", GetSignatureForError ());
                                }
                                return false;
                        }
@@ -357,6 +381,34 @@ namespace Mono.CSharp {
 
                protected abstract void VerifyObsoleteAttribute ();
 
+               //
+               // Raised (and passed an XmlElement that contains the comment)
+               // when GenerateDocComment is writing documentation expectedly.
+               //
+               internal virtual void OnGenerateDocComment (DeclSpace ds, XmlElement intermediateNode)
+               {
+               }
+
+               //
+               // Returns a string that represents the signature for this 
+               // member which should be used in XML documentation.
+               //
+               public virtual string GetDocCommentName (DeclSpace ds)
+               {
+                       if (ds == null || this is DeclSpace)
+                               return DocCommentHeader + Name;
+                       else
+                               return String.Concat (DocCommentHeader, ds.Name, ".", Name);
+               }
+
+               //
+               // Generates xml doc comments (if any), and if required,
+               // handle warning report.
+               //
+               internal virtual void GenerateDocComment (DeclSpace ds)
+               {
+                       DocUtil.GenerateDocComment (this, ds);
+               }
        }
 
        /// <summary>
@@ -367,9 +419,9 @@ namespace Mono.CSharp {
        ///   provides the common foundation for managing those name
        ///   spaces.
        /// </remarks>
-       public abstract class DeclSpace : MemberCore {
+       public abstract class DeclSpace : MemberCore, IAlias {
                /// <summary>
-               ///   this points to the actual definition that is being
+               ///   This points to the actual definition that is being
                ///   created with System.Reflection.Emit
                /// </summary>
                public TypeBuilder TypeBuilder;
@@ -386,6 +438,13 @@ namespace Mono.CSharp {
                
                protected Hashtable defined_names;
 
+               // The emit context for toplevel objects.
+               protected EmitContext ec;
+               
+               public EmitContext EmitContext {
+                       get { return ec; }
+               }
+
                static string[] attribute_targets = new string [] { "type" };
 
                public DeclSpace (NamespaceEntry ns, TypeContainer parent, MemberName name,
@@ -404,7 +463,7 @@ namespace Mono.CSharp {
                {
                        if (basename == Basename && !(this is Interface)) {
                                Report.SymbolRelatedToPreviousError (this);
-                               Report.Error (542, "'{0}': member names cannot be the same as their enclosing type", symbol.Location, symbol.GetSignatureForError ());
+                               Report.Error (542,  symbol.Location, "'{0}': member names cannot be the same as their enclosing type", symbol.GetSignatureForError ());
                                return false;
                        }
 
@@ -461,7 +520,7 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Looks up the alias for the name
                /// </summary>
-               public string LookupAlias (string name)
+               public IAlias LookupAlias (string name)
                {
                        if (NamespaceEntry != null)
                                return NamespaceEntry.LookupAlias (name);
@@ -827,11 +886,11 @@ namespace Mono.CSharp {
                        return t;
                }
 
-               public static void Error_AmbiguousTypeReference (Location loc, string name, Type t1, Type t2)
+               public static void Error_AmbiguousTypeReference (Location loc, string name, string t1, string t2)
                {
                        Report.Error (104, loc,
-                                     String.Format ("`{0}' is an ambiguous reference ({1} or {2}) ", name,
-                                                    t1.FullName, t2.FullName));
+                                     "`{0}' is an ambiguous reference ({1} or {2})",
+                                     name, t1, t2);
                }
 
                /// <summary>
@@ -915,9 +974,9 @@ namespace Mono.CSharp {
                                if (name.IndexOf ('.') > 0)
                                        continue;
 
-                               string alias_value = ns.LookupAlias (name);
+                               IAlias alias_value = ns.LookupAlias (name);
                                if (alias_value != null) {
-                                       t = LookupInterfaceOrClass ("", alias_value, out error);
+                                       t = LookupInterfaceOrClass ("", alias_value.Name, out error);
                                        if (error)
                                                return null;
 
@@ -937,7 +996,7 @@ namespace Mono.CSharp {
                                        if (match != null){
                                                if (t != null){
                                                        if (CheckAccessLevel (match)) {
-                                                               Error_AmbiguousTypeReference (loc, name, t, match);
+                                                               Error_AmbiguousTypeReference (loc, name, t.FullName, match.FullName);
                                                                return null;
                                                        }
                                                        continue;
@@ -997,12 +1056,10 @@ namespace Mono.CSharp {
                        caching_flags &= ~Flags.HasCompliantAttribute_Undetected;
 
                        if (OptAttributes != null) {
-                               EmitContext ec = new EmitContext (Parent, this, Location,
-                                                                 null, null, ModFlags, false);
-                               Attribute cls_attribute = OptAttributes.GetClsCompliantAttribute (ec);
+                               Attribute cls_attribute = OptAttributes.Search (TypeManager.cls_compliant_attribute_type, ec);
                                if (cls_attribute != null) {
                                        caching_flags |= Flags.HasClsCompliantAttribute;
-                                       if (cls_attribute.GetClsCompliantAttributeValue (this)) {
+                                       if (cls_attribute.GetClsCompliantAttributeValue (ec)) {
                                                caching_flags |= Flags.ClsCompliantAttributeTrue;
                                                return true;
                                        }
@@ -1030,6 +1087,22 @@ namespace Mono.CSharp {
                                return attribute_targets;
                        }
                }
+
+               bool IAlias.IsType {
+                       get { return true; }
+               }
+
+               string IAlias.Name {
+                       get { return Name; }
+               }
+
+               TypeExpr IAlias.ResolveAsType (EmitContext ec)
+               {
+                       if (TypeBuilder == null)
+                               throw new InvalidOperationException ();
+
+                       return new TypeExpression (TypeBuilder, Location);
+               }
        }
 
        /// <summary>
@@ -1202,12 +1275,12 @@ namespace Mono.CSharp {
                }
 
                /// <summary>
-               ///   Returns the IMemberContainer of the parent class or null if this
+               ///   Returns the IMemberContainer of the base class or null if this
                ///   is an interface or TypeManger.object_type.
                ///   This is used when creating the member cache for a class to get all
-               ///   members from the parent class.
+               ///   members from the base class.
                /// </summary>
-               IMemberContainer ParentContainer {
+               MemberCache BaseCache {
                        get;
                }
 
@@ -1258,31 +1331,21 @@ namespace Mono.CSharp {
                public readonly IMemberContainer Container;
                protected Hashtable member_hash;
                protected Hashtable method_hash;
-               
+
                /// <summary>
                ///   Create a new MemberCache for the given IMemberContainer `container'.
                /// </summary>
-               public MemberCache (IMemberContainer container, bool setup_inherited_interfaces)
+               public MemberCache (IMemberContainer container)
                {
                        this.Container = container;
 
                        Timer.IncrementCounter (CounterType.MemberCache);
                        Timer.StartTimer (TimerType.CacheInit);
 
-                       
-
-                       // If we have a parent class (we have a parent class unless we're
+                       // If we have a base class (we have a base class unless we're
                        // TypeManager.object_type), we deep-copy its MemberCache here.
-                       if (Container.IsInterface) {
-                               MemberCache parent;
-                               
-                               if (Container.ParentContainer != null)
-                                       parent = Container.ParentContainer.MemberCache;
-                               else
-                                       parent = null;
-                               member_hash = SetupCacheForInterface (parent, setup_inherited_interfaces);
-                       } else if (Container.ParentContainer != null)
-                               member_hash = SetupCache (Container.ParentContainer.MemberCache);
+                       if (Container.BaseCache != null)
+                               member_hash = SetupCache (Container.BaseCache);
                        else
                                member_hash = new Hashtable ();
 
@@ -1300,37 +1363,55 @@ namespace Mono.CSharp {
                        Timer.StopTimer (TimerType.CacheInit);
                }
 
+               public MemberCache (Type[] ifaces)
+               {
+                       //
+                       // The members of this cache all belong to other caches.  
+                       // So, 'Container' will not be used.
+                       //
+                       this.Container = null;
+
+                       member_hash = new Hashtable ();
+                       if (ifaces == null)
+                               return;
+
+                       foreach (Type itype in ifaces)
+                               AddCacheContents (TypeManager.LookupMemberCache (itype));
+               }
+
                /// <summary>
-               ///   Bootstrap this member cache by doing a deep-copy of our parent.
+               ///   Bootstrap this member cache by doing a deep-copy of our base.
                /// </summary>
-               Hashtable SetupCache (MemberCache parent)
+               Hashtable SetupCache (MemberCache base_class)
                {
                        Hashtable hash = new Hashtable ();
-                       if (parent == null)
+
+                       if (base_class == null)
                                return hash;
 
-                       IDictionaryEnumerator it = parent.member_hash.GetEnumerator ();
+                       IDictionaryEnumerator it = base_class.member_hash.GetEnumerator ();
                        while (it.MoveNext ()) {
                                hash [it.Key] = ((ArrayList) it.Value).Clone ();
-                       }
+                        }
                                 
                        return hash;
                }
 
-
                /// <summary>
-               ///   Add the contents of `new_hash' to `hash'.
+               ///   Add the contents of `cache' to the member_hash.
                /// </summary>
-               void AddHashtable (Hashtable hash, MemberCache cache)
+               void AddCacheContents (MemberCache cache)
                {
-                       Hashtable new_hash = cache.member_hash;
-                       IDictionaryEnumerator it = new_hash.GetEnumerator ();
+                       IDictionaryEnumerator it = cache.member_hash.GetEnumerator ();
                        while (it.MoveNext ()) {
-                               ArrayList list = (ArrayList) hash [it.Key];
+                               ArrayList list = (ArrayList) member_hash [it.Key];
                                if (list == null)
-                                       hash [it.Key] = list = new ArrayList ();
+                                       member_hash [it.Key] = list = new ArrayList ();
+
+                               ArrayList entries = (ArrayList) it.Value;
+                               for (int i = entries.Count-1; i >= 0; i--) {
+                                       CacheEntry entry = (CacheEntry) entries [i];
 
-                               foreach (CacheEntry entry in (ArrayList) it.Value) {
                                        if (entry.Container != cache.Container)
                                                break;
                                        list.Add (entry);
@@ -1338,34 +1419,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               /// <summary>
-               ///   Bootstrap the member cache for an interface type.
-               ///   Type.GetMembers() won't return any inherited members for interface types,
-               ///   so we need to do this manually.  Interfaces also inherit from System.Object.
-               /// </summary>
-               Hashtable SetupCacheForInterface (MemberCache parent, bool deep_setup)
-               {
-                       Hashtable hash = SetupCache (parent);
-
-                       if (!deep_setup)
-                               return hash;
-
-                       TypeExpr [] ifaces = TypeManager.GetInterfaces (Container.Type);
-
-                       foreach (TypeExpr iface in ifaces) {
-                               Type itype = iface.Type;
-
-                               IMemberContainer iface_container =
-                                       TypeManager.LookupMemberContainer (itype);
-
-                               MemberCache iface_cache = iface_container.MemberCache;
-
-                               AddHashtable (hash, iface_cache);
-                       }
-
-                       return hash;
-               }
-
                /// <summary>
                ///   Add all members from class `container' to the cache.
                /// </summary>
@@ -1415,7 +1468,7 @@ namespace Mono.CSharp {
                                }
 
                                // When this method is called for the current class, the list will
-                               // already contain all inherited members from our parent classes.
+                               // already contain all inherited members from our base classes.
                                // We cannot add new members in front of the list since this'd be an
                                // expensive operation, that's why the list is sorted in reverse order
                                // (ie. members from the current class are coming last).
@@ -1526,7 +1579,7 @@ namespace Mono.CSharp {
                ///   number to speed up the searching process.
                /// </summary>
                [Flags]
-               protected internal enum EntryType {
+               protected enum EntryType {
                        None            = 0x000,
 
                        Instance        = 0x001,
@@ -1549,7 +1602,7 @@ namespace Mono.CSharp {
                        MaskType        = Constructor|Event|Field|Method|Property|NestedType
                }
 
-               protected internal struct CacheEntry {
+               protected struct CacheEntry {
                        public readonly IMemberContainer Container;
                        public readonly EntryType EntryType;
                        public readonly MemberInfo Member;
@@ -1561,6 +1614,12 @@ namespace Mono.CSharp {
                                this.Member = member;
                                this.EntryType = GetEntryType (mt, bf);
                        }
+
+                       public override string ToString ()
+                       {
+                               return String.Format ("CacheEntry ({0}:{1}:{2})", Container.Name,
+                                                     EntryType, Member);
+                       }
                }
 
                /// <summary>
@@ -1654,9 +1713,9 @@ namespace Mono.CSharp {
 
 
                        // `applicable' is a list of all members with the given member name `name'
-                       // in the current class and all its parent classes.  The list is sorted in
+                       // in the current class and all its base classes.  The list is sorted in
                        // reverse order due to the way how the cache is initialy created (to speed
-                       // things up, we're doing a deep-copy of our parent).
+                       // things up, we're doing a deep-copy of our base).
 
                        for (int i = applicable.Count-1; i >= 0; i--) {
                                CacheEntry entry = (CacheEntry) applicable [i];