Update
[mono.git] / mcs / mcs / enum.cs
old mode 100755 (executable)
new mode 100644 (file)
index 1099ec5..808ee9a
@@ -17,32 +17,34 @@ using System.Globalization;
 
 namespace Mono.CSharp {
 
-       class EnumMember: Attributable {
-               string name;
-               Enum parent;
-               Location loc;
-
+       class EnumMember: MemberCore {
                static string[] attribute_targets = new string [] { "field" };
 
+               Enum parent_enum;
                public FieldBuilder builder;
+               internal readonly Expression Type;
 
-               public EnumMember (string name, Enum parent, Location loc, Attributes attrs):
-                       base (attrs)
+               public EnumMember (Enum parent_enum, Expression expr, string name,
+                                  Location loc, Attributes attrs):
+                       base (null, new MemberName (name), attrs, loc)
                {
-                       this.name = name;
-                       this.parent = parent;
-                       this.loc = loc;
+                       this.parent_enum = parent_enum;
+                       this.ModFlags = parent_enum.ModFlags;
+                       this.Type = expr;
                }
 
                public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
                {
                        if (a.Type == TypeManager.marshal_as_attr_type) {
-                               UnmanagedMarshal marshal = a.GetMarshal ();
+                               UnmanagedMarshal marshal = a.GetMarshal (this);
                                if (marshal != null) {
                                        builder.SetMarshal (marshal);
-                                       return;
                                }
-                               Report.Warning_T (-24, a.Location);
+                               return;
+                       }
+
+                       if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
+                               a.Error_InvalidSecurityParent ();
                                return;
                        }
 
@@ -55,44 +57,66 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override bool IsClsCompliaceRequired(DeclSpace ds)
-               {
-                       return parent.IsClsCompliaceRequired (ds);
-               }
-
                public void DefineMember (TypeBuilder tb)
                {
                        FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
                                | FieldAttributes.Literal;
                        
-                       builder = tb.DefineField (name, tb, attr);
+                       builder = tb.DefineField (Name, tb, attr);
+               }
+
+               public override bool Define ()
+               {
+                       throw new NotImplementedException ();
                }
 
                public void Emit (EmitContext ec)
                {
                        if (OptAttributes != null)
                                OptAttributes.Emit (ec, this); 
+
+                       Emit ();
                }
 
-               protected override string[] ValidAttributeTargets {
+               public override string GetSignatureForError()
+               {
+                       return String.Concat (parent_enum.GetSignatureForError (), '.', base.GetSignatureForError ());
+               }
+
+               public override string[] ValidAttributeTargets {
                        get {
                                return attribute_targets;
                        }
                }
+
+               protected override bool VerifyClsCompliance(DeclSpace ds)
+               {
+                       // Because parent is TypeContainer and we have only DeclSpace parent.
+                       // Parameter replacing is required
+                       return base.VerifyClsCompliance (parent_enum);
+               }
+
+               // There is no base type
+               protected override void VerifyObsoleteAttribute()
+               {
+               }
+
+               public override string DocCommentHeader {
+                       get { return "F:"; }
+               }
        }
 
        /// <summary>
        ///   Enumeration container
        /// </summary>
        public class Enum : DeclSpace {
-               ArrayList ordered_enums;
+               public ArrayList ordered_enums;
                
                public Expression BaseType;
                
                public Type UnderlyingType;
 
                Hashtable member_to_location;
-               Hashtable member_to_attributes;
 
                //
                // This is for members that have been defined
@@ -105,9 +129,6 @@ namespace Mono.CSharp {
                Hashtable in_transit;
                
                ArrayList field_builders;
-
-
-               Hashtable name_to_member;
                
                public const int AllowedModifiers =
                        Modifiers.NEW |
@@ -116,8 +137,8 @@ namespace Mono.CSharp {
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE;
 
-               public Enum (NamespaceEntry ns, TypeContainer parent, Expression type, int mod_flags,
-                            string name, Attributes attrs, Location l)
+               public Enum (NamespaceEntry ns, TypeContainer parent, Expression type,
+                            int mod_flags, MemberName name, Attributes attrs, Location l)
                        : base (ns, parent, name, attrs, l)
                {
                        this.BaseType = type;
@@ -129,38 +150,28 @@ namespace Mono.CSharp {
                        member_to_value = new Hashtable ();
                        in_transit = new Hashtable ();
                        field_builders = new ArrayList ();
-
-                       name_to_member = new Hashtable ();
                }
 
                /// <summary>
                ///   Adds @name to the enumeration space, with @expr
                ///   being its definition.  
                /// </summary>
-               public AdditionResult AddEnumMember (string name, Expression expr, Location loc,
-                                                    Attributes opt_attrs)
+               public void AddEnumMember (string name, Expression expr, Location loc, Attributes opt_attrs, string documentation)
                {
-                       if (defined_names.Contains (name))
-                               return AdditionResult.NameExists;
-
                        if (name == "value__") {
                                Report.Error (76, loc, "An item in an enumeration can't have an identifier `value__'");
-                               return AdditionResult.Error;
+                               return;
                        }
 
-                       DefineName (name, expr);
+                       EnumMember em = new EnumMember (this, expr, name, loc, opt_attrs);
+                       em.DocComment = documentation;
+                       if (!AddToContainer (em, name, ""))
+                               return;
+
 
+                       // TODO: can be almost deleted
                        ordered_enums.Add (name);
                        member_to_location.Add (name, loc);
-
-                       if (member_to_attributes == null)
-                               member_to_attributes = new Hashtable ();
-
-                       member_to_attributes.Add (name, opt_attrs);
-
-                       name_to_member.Add (name, new EnumMember (name, this, loc, opt_attrs));
-                       
-                       return AdditionResult.Success;
                }
 
                //
@@ -202,6 +213,8 @@ namespace Mono.CSharp {
 
                        TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel);
 
+                       ec = new EmitContext (this, this, Location, null, null, ModFlags, false);
+
                        attr |= TypeAttributes.Class | TypeAttributes.Sealed;
 
                        if (!(BaseType is TypeLookupExpression)) {
@@ -211,7 +224,8 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       UnderlyingType = ResolveType (BaseType, false, Location);
+                       TypeExpr ute = ResolveBaseTypeExpr (BaseType, false, Location);
+                       UnderlyingType = ute.Type;
 
                        if (UnderlyingType != TypeManager.int32_type &&
                            UnderlyingType != TypeManager.uint32_type &&
@@ -579,7 +593,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       EnumMember em = name_to_member [name] as EnumMember;
+                       EnumMember em = (EnumMember) defined_names [name];
                        em.DefineMember (TypeBuilder);
 
                        bool fail;
@@ -604,7 +618,7 @@ namespace Mono.CSharp {
                        return true;
                }
                
-               public override bool Define (TypeContainer parent)
+               public override bool Define ()
                {
                        //
                        // If there was an error during DefineEnum, return
@@ -612,9 +626,7 @@ namespace Mono.CSharp {
                        if (TypeBuilder == null)
                                return false;
 
-                       EmitContext ec = new EmitContext (this, this, Location, null,
-                                                         UnderlyingType, ModFlags, false);
-
+                       ec = new EmitContext (this, this, Location, null, UnderlyingType, ModFlags, false);
                        
                        object default_value = 0;
                        
@@ -639,7 +651,7 @@ namespace Mono.CSharp {
                                                return false;
                                        }
 
-                                       EnumMember em = name_to_member [name] as EnumMember;
+                                       EnumMember em = (EnumMember) defined_names [name];
 
                                        em.DefineMember (TypeBuilder);
                                        FieldBuilder fb = em.builder;
@@ -667,53 +679,49 @@ namespace Mono.CSharp {
 
                                default_value = GetNextDefaultValue (default_value);
                        }
+
                        return true;
                }
 
-               public override void Emit (TypeContainer tc)
+               public override void Emit ()
                {
-                       EmitContext ec = new EmitContext (tc, this, Location, null, null, ModFlags, false);
-
                        if (OptAttributes != null) {
                                OptAttributes.Emit (ec, this);
                        }
 
-                       foreach (EnumMember em in name_to_member.Values) {
+                       foreach (EnumMember em in defined_names.Values) {
                                em.Emit (ec);
                        }
 
-                       base.Emit (tc);
+                       base.Emit ();
                }
                
-               protected override bool IsIdentifierClsCompliant (DeclSpace ds)
-               {
-                       if (!base.IsIdentifierClsCompliant (ds))
-                               return false;
-
-                       for (int i = 1; i < ordered_enums.Count; ++i) {
-                               string checked_name = ordered_enums [i] as string;
-                               for (int ii = 0; ii < ordered_enums.Count; ++ii) {
-                                       if (ii == i)
-                                               continue;
-
-                                       string enumerator_name = ordered_enums [ii] as string;
-                                       if (String.Compare (checked_name, enumerator_name, true, CultureInfo.InvariantCulture) == 0) {
-                                               Report.SymbolRelatedToPreviousError ((Location)member_to_location [enumerator_name], enumerator_name);
-                                               Report.Error_T (3005, (Location)member_to_location [checked_name], GetEnumeratorName (checked_name));
-                                               break;
-                                       }
-                               }
-                       }
-                       return true;
-               }
+               void VerifyClsName ()
+               {
+                       Hashtable ht = new Hashtable ();
+                       foreach (string name in ordered_enums) {
+                               string locase = name.ToLower (System.Globalization.CultureInfo.InvariantCulture);
+                               if (!ht.Contains (locase)) {
+                                       ht.Add (locase, defined_names [name]);
+                                       continue;
+                               }
+                               MemberCore conflict = (MemberCore)ht [locase];
+                               Report.SymbolRelatedToPreviousError (conflict);
+                               conflict = GetDefinition (name);
+                               Report.Error (3005, conflict.Location, "Identifier '{0}' differing only in case is not CLS-compliant", conflict.GetSignatureForError ());
+                       }
+               }
 
                protected override bool VerifyClsCompliance (DeclSpace ds)
                {
                        if (!base.VerifyClsCompliance (ds))
                                return false;
 
+                       VerifyClsName ();
+
                        if (!AttributeTester.IsClsCompliant (UnderlyingType)) {
-                               Report.Error_T (3009, Location, GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
+                               Report.Error (3009, Location, "'{0}': base type '{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
                        }
 
                        return true;
@@ -765,7 +773,7 @@ namespace Mono.CSharp {
                // indexer
                public Expression this [string name] {
                        get {
-                               return (Expression) defined_names [name];
+                               return ((EnumMember) defined_names [name]).Type;
                        }
                }
 
@@ -775,5 +783,25 @@ namespace Mono.CSharp {
                        }
                }
 
+               protected override void VerifyObsoleteAttribute()
+               {
+                       // UnderlyingType is never obsolete
+               }
+
+               //
+               // Generates xml doc comments (if any), and if required,
+               // handle warning report.
+               //
+               internal override void GenerateDocComment (DeclSpace ds)
+               {
+                       DocUtil.GenerateEnumDocComment (this, ds);
+               }
+
+               //
+               //   Represents header string for documentation comment.
+               //
+               public override string DocCommentHeader {
+                       get { return "T:"; }
+               }
        }
 }