Merge pull request #5272 from lambdageek/bug-58421
[mono.git] / mcs / mcs / enum.cs
index 7a025bb6c347e4a2574a788aa94744c8650aa9ec..f124338ba558812c20521b9db4c026baf61b9a0a 100644 (file)
@@ -22,13 +22,38 @@ using MetaType = System.Type;
 using System.Reflection;
 #endif
 
-namespace Mono.CSharp {
+namespace Mono.CSharp
+{
 
        public class EnumMember : Const
        {
+#if !STATIC
+               class MemberTypeDelegator : TypeDelegator
+               {
+                       Type underlyingType;
+
+                       public MemberTypeDelegator (Type delegatingType, Type underlyingType)
+                               : base (delegatingType)
+                       {
+                               this.underlyingType = underlyingType;
+                       }
+
+                       public override Type GetEnumUnderlyingType ()
+                       {
+                               return underlyingType;
+                       }
+
+                       public override Type UnderlyingSystemType {
+                               get {
+                                       return underlyingType;
+                               }
+                       }
+               }
+#endif
+
                class EnumTypeExpr : TypeExpr
                {
-                       public override TypeSpec ResolveAsType (IMemberContext ec)
+                       public override TypeSpec ResolveAsType (IMemberContext ec, bool allowUnboundTypeArguments)
                        {
                                type = ec.CurrentType;
                                eclass = ExprClass.Type;
@@ -64,11 +89,12 @@ namespace Mono.CSharp {
                        if (expr is EnumConstant)
                                expr = ((EnumConstant) expr).Child;
 
-                       var underlying = ((Enum) Parent).UnderlyingType;
+                       var en = (Enum)Parent;
+                       var underlying = en.UnderlyingType;
                        if (expr != null) {
-                               expr = expr.ImplicitConversionRequired (rc, underlying, Location);
+                               expr = expr.ImplicitConversionRequired (rc, underlying);
                                if (expr != null && !IsValidEnumType (expr.Type)) {
-                                       Enum.Error_1008 (Location, Report);
+                                       en.Error_UnderlyingType (Location);
                                        expr = null;
                                }
                        }
@@ -84,8 +110,17 @@ namespace Mono.CSharp {
                        if (!ResolveMemberType ())
                                return false;
 
+                       MetaType ftype = MemberType.GetMetaInfo ();
+#if !STATIC
+                       //
+                       // Workaround for .net SRE limitation which cannot define field of unbaked enum type
+                       // which is how all enums are declared
+                       //
+                       ftype = new MemberTypeDelegator (ftype, ((Enum)Parent).UnderlyingType.GetMetaInfo ());
+#endif
+
                        const FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal;
-                       FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType.GetMetaInfo (), attr);
+                       FieldBuilder = Parent.TypeBuilder.DefineField (Name, ftype, attr);
                        spec = new ConstSpec (Parent.Definition, this, MemberType, FieldBuilder, ModFlags, initializer);
 
                        Parent.MemberCache.AddMember (spec);
@@ -217,7 +252,7 @@ namespace Mono.CSharp {
                        AddMember (em);
                }
 
-               public static void Error_1008 (Location loc, Report Report)
+               public void Error_UnderlyingType (Location loc)
                {
                        Report.Error (1008, loc,
                                "Type byte, sbyte, short, ushort, int, uint, long or ulong expected");
@@ -225,7 +260,21 @@ namespace Mono.CSharp {
 
                protected override void DoDefineContainer ()
                {
-                       ((EnumSpec) spec).UnderlyingType = underlying_type_expr == null ? Compiler.BuiltinTypes.Int : underlying_type_expr.Type;
+                       TypeSpec ut;
+                       if (underlying_type_expr != null) {
+                               ut = underlying_type_expr.ResolveAsType (this);
+                               if (!EnumSpec.IsValidUnderlyingType (ut)) {
+                                       Error_UnderlyingType (underlying_type_expr.Location);
+                                       ut = null;
+                               }
+                       } else {
+                               ut = null;
+                       }
+
+                       if (ut == null)
+                               ut = Compiler.BuiltinTypes.Int;
+
+                       ((EnumSpec) spec).UnderlyingType = ut;
 
                        TypeBuilder.DefineField (UnderlyingValueField, UnderlyingType.GetMetaInfo (),
                                FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);
@@ -269,7 +318,7 @@ namespace Mono.CSharp {
                        case BuiltinTypeSpec.Type.ULong:
                        case BuiltinTypeSpec.Type.UShort:
                                Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant",
-                                       GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
+                                       GetSignatureForError (), UnderlyingType.GetSignatureForError ());
                                break;
                        }