2008-11-24 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / enum.cs
index 7239063eee2e966617bee13dcecbf8dbbc9b4304..2523e0cf76c1cc701b5fb638ac8bc2d7254e300b 100644 (file)
@@ -5,9 +5,10 @@
 //         Ravi Pratap     (ravi@ximian.com)
 //         Marek Safar     (marek.safar@seznam.cz)
 //
-// Licensed under the terms of the GNU GPL
+// Dual licensed under the terms of the MIT X11 or GNU GPL
 //
-// (C) 2001 Ximian, Inc (http://www.ximian.com)
+// Copyright 2001 Ximian, Inc (http://www.ximian.com)
+// Copyright 2003-2003 Novell, Inc (http://www.novell.com)
 //
 
 using System;
@@ -53,14 +54,6 @@ namespace Mono.CSharp {
                        {
                                return DoResolveAsTypeStep (ec);
                        }
-
-                       public override string Name {
-                               get { return Enum.Name; }
-                       }
-
-                       public override string FullName {
-                               get { return Enum.Name; }
-                       }
                }
 
                static bool IsValidEnumType (Type t)
@@ -72,11 +65,14 @@ namespace Mono.CSharp {
                }
 
                public object Value {
-                       get { return value.GetValue (); }
+                       get { return ResolveValue () ? value.GetValue () : null; }
                }
 
                public override bool Define ()
                {
+                       if (!ResolveMemberType ())
+                               return false;
+
                        const FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal;
                        FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType, attr);
                        Parent.MemberCache.AddMember (FieldBuilder, this);
@@ -94,7 +90,7 @@ namespace Mono.CSharp {
                                if (c is EnumConstant)
                                        c = ((EnumConstant)c).Child;
 
-                               c = c.ImplicitConversionRequired (ParentEnum.UnderlyingType, Location);
+                               c = c.ImplicitConversionRequired (ec, ParentEnum.UnderlyingType, Location);
                                if (c == null)
                                        return null;
 
@@ -106,16 +102,12 @@ namespace Mono.CSharp {
                                return new EnumConstant (c, MemberType);
                        }
 
-                       if (prev_member == null) {
+                       if (prev_member == null)
                                return new EnumConstant (
                                        New.Constantify (ParentEnum.UnderlyingType), MemberType);
-                       }
 
-                       if (!prev_member.ResolveValue ()) {
-                               prev_member.value = new EnumConstant (
-                                       New.Constantify (ParentEnum.UnderlyingType), MemberType);
+                       if (!prev_member.ResolveValue ())
                                return null;
-                       }
 
                        try {
                                return (EnumConstant) prev_member.value.Increment ();
@@ -133,18 +125,18 @@ namespace Mono.CSharp {
        /// </summary>
        public class Enum : TypeContainer
        {
-               Expression base_type;
+               public static readonly string UnderlyingValueField = "value__";
 
-               public Type UnderlyingType;
+               TypeExpr base_type;
 
-               public const int AllowedModifiers =
+               const int AllowedModifiers =
                        Modifiers.NEW |
                        Modifiers.PUBLIC |
                        Modifiers.PROTECTED |
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE;
 
-               public Enum (NamespaceEntry ns, DeclSpace parent, Expression type,
+               public Enum (NamespaceEntry ns, DeclSpace parent, TypeExpr type,
                             int mod_flags, MemberName name, Attributes attrs)
                        : base (ns, parent, name, attrs, Kind.Enum)
                {
@@ -155,9 +147,9 @@ namespace Mono.CSharp {
 
                public void AddEnumMember (EnumMember em)
                {
-                       if (em.Name == "value__") {
-                               Report.Error (76, em.Location, "An item in an enumeration cannot " +
-                                             "have an identifier `value__'");
+                       if (em.Name == UnderlyingValueField) {
+                               Report.Error (76, em.Location, "An item in an enumeration cannot have an identifier `{0}'",
+                                       UnderlyingValueField);
                                return;
                        }
 
@@ -175,54 +167,32 @@ namespace Mono.CSharp {
                        if (!base.DefineNestedTypes ())
                                return false;
 
-                       if (!(base_type is TypeLookupExpression)) {
-                               Error_1008 (Location);
-                               return false;
-                       }
-
-                       TypeExpr ute = base_type.ResolveAsTypeTerminal (this, false);
-                       UnderlyingType = ute.Type;
-
-                       if (UnderlyingType != TypeManager.int32_type &&
-                           UnderlyingType != TypeManager.uint32_type &&
-                           UnderlyingType != TypeManager.int64_type &&
-                           UnderlyingType != TypeManager.uint64_type &&
-                           UnderlyingType != TypeManager.short_type &&
-                           UnderlyingType != TypeManager.ushort_type &&
-                           UnderlyingType != TypeManager.byte_type  &&
-                           UnderlyingType != TypeManager.sbyte_type) {
-                               Error_1008 (Location);
-                               return false;
-                       }
-
                        //
                        // Call MapToInternalType for corlib
                        //
-                       TypeBuilder.DefineField ("value__", UnderlyingType,
+                       TypeBuilder.DefineField (UnderlyingValueField, UnderlyingType,
                                                 FieldAttributes.Public | FieldAttributes.SpecialName
                                                 | FieldAttributes.RTSpecialName);
 
                        return true;
                }
 
-               protected override bool DoDefineMembers ()
+               public override bool Define ()
                {
                        member_cache = new MemberCache (TypeManager.enum_type, this);
                        DefineContainerMembers (constants);
                        return true;
                }
 
-               //
-               // Used for error reporting only
-               //
-               public EnumMember GetDefinition (object value)
+               public override bool IsUnmanagedType ()
                {
-                       foreach (EnumMember e in defined_names.Values) {
-                               if (e.Value.Equals (value))
-                                       return e;
-                       }
+                       return true;
+               }
 
-                       throw new ArgumentOutOfRangeException (value.ToString ());
+               public Type UnderlyingType {
+                       get {
+                               return base_type.Type;
+                       }
                }
 
                protected override bool VerifyClsCompliance ()
@@ -233,7 +203,7 @@ namespace Mono.CSharp {
                        if (UnderlyingType == TypeManager.uint32_type ||
                                UnderlyingType == TypeManager.uint64_type ||
                                UnderlyingType == TypeManager.ushort_type) {
-                               Report.Error (3009, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
+                               Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
                        }
 
                        return true;