Fix unmanaged type check for nested generic types
[mono.git] / mcs / mcs / enum.cs
index d323e062d02ce2db1676b70944d5eeffe79a3510..4b0f4a020432e8691595e31a69658726ba7def7b 100644 (file)
 //
 
 using System;
-using System.Collections.Generic;
+
+#if STATIC
+using MetaType = IKVM.Reflection.Type;
+using IKVM.Reflection;
+#else
+using MetaType = System.Type;
 using System.Reflection;
-using System.Reflection.Emit;
-using System.Globalization;
+#endif
 
 namespace Mono.CSharp {
 
@@ -23,32 +27,35 @@ namespace Mono.CSharp {
        {
                class EnumTypeExpr : TypeExpr
                {
-                       protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
+                       public override TypeSpec ResolveAsType (IMemberContext ec)
                        {
                                type = ec.CurrentType;
-                               return this;
-                       }
-
-                       public override TypeExpr ResolveAsTypeTerminal (IMemberContext ec, bool silent)
-                       {
-                               return DoResolveAsTypeStep (ec);
+                               eclass = ExprClass.Type;
+                               return type;
                        }
                }
 
-               public EnumMember (Enum parent, EnumMember prev_member, string name, Expression expr,
-                                  Attributes attrs, Location loc)
-                       : base (parent, new EnumTypeExpr (), name, null, Modifiers.PUBLIC,
-                               attrs, loc)
+               public EnumMember (Enum parent, MemberName name, Attributes attrs)
+                       : base (parent, new EnumTypeExpr (), Modifiers.PUBLIC, name, attrs)
                {
-                       initializer = new EnumInitializer (this, expr, prev_member);
                }
 
                static bool IsValidEnumType (TypeSpec t)
                {
-                       return (t == TypeManager.int32_type || t == TypeManager.uint32_type || t == TypeManager.int64_type ||
-                               t == TypeManager.byte_type || t == TypeManager.sbyte_type || t == TypeManager.short_type ||
-                               t == TypeManager.ushort_type || t == TypeManager.uint64_type || t == TypeManager.char_type ||
-                               TypeManager.IsEnumType (t));
+                       switch (t.BuiltinType) {
+                       case BuiltinTypeSpec.Type.Int:
+                       case BuiltinTypeSpec.Type.UInt:
+                       case BuiltinTypeSpec.Type.Long:
+                       case BuiltinTypeSpec.Type.Byte:
+                       case BuiltinTypeSpec.Type.SByte:
+                       case BuiltinTypeSpec.Type.Short:
+                       case BuiltinTypeSpec.Type.UShort:
+                       case BuiltinTypeSpec.Type.ULong:
+                       case BuiltinTypeSpec.Type.Char:
+                               return true;
+                       default:
+                               return t.IsEnum;
+                       }
                }
 
                public override Constant ConvertInitializer (ResolveContext rc, Constant expr)
@@ -66,9 +73,9 @@ namespace Mono.CSharp {
                        }
 
                        if (expr == null)
-                               expr = New.Constantify (underlying);
+                               expr = New.Constantify (underlying, Location);
 
-                       return new EnumConstant (expr, MemberType).Resolve (rc);
+                       return new EnumConstant (expr, MemberType);
                }
 
                public override bool Define ()
@@ -85,49 +92,62 @@ namespace Mono.CSharp {
                }
        }
 
-       class EnumInitializer : ConstInitializer
+       /// <summary>
+       ///   Enumeration container
+       /// </summary>
+       public class Enum : TypeContainer
        {
-               EnumMember prev;
-
-               public EnumInitializer (Const field, Expression init, EnumMember prev)
-                       : base (field, init)
+               //
+               // Implicit enum member initializer, used when no constant value is provided
+               //
+               sealed class ImplicitInitializer : Expression
                {
-                       this.prev = prev;
-               }
+                       readonly EnumMember prev;
+                       readonly EnumMember current;
 
-               protected override Expression DoResolveInitializer (ResolveContext rc)
-               {
-                       if (expr != null)
-                               return base.DoResolveInitializer (rc);
+                       public ImplicitInitializer (EnumMember current, EnumMember prev)
+                       {
+                               this.current = current;
+                               this.prev = prev;
+                       }
 
-                       if (prev == null)
-                               return field.ConvertInitializer (rc, null);
+                       public override bool ContainsEmitWithAwait ()
+                       {
+                               return false;
+                       }
 
-                       try {
-                               var ec = prev.DefineValue () as EnumConstant;
-                               expr = ec.Increment ().Resolve (rc);
-                       } catch (OverflowException) {
-                               rc.Report.Error (543, field.Location,
-                                       "The enumerator value `{0}' is outside the range of enumerator underlying type `{1}'",
-                                       field.GetSignatureForError (),
-                                       TypeManager.CSharpName (((Enum) field.Parent).UnderlyingType));
+                       public override Expression CreateExpressionTree (ResolveContext ec)
+                       {
+                               throw new NotSupportedException ("Missing Resolve call");
+                       }
 
-                               expr = field.ConvertInitializer (rc, null);
-                       }               
+                       protected override Expression DoResolve (ResolveContext rc)
+                       {
+                               // We are the first member
+                               if (prev == null) {
+                                       return New.Constantify (current.Parent.Definition, Location);
+                               }
 
-                       return expr;
+                               var c = ((ConstSpec) prev.Spec).GetConstant (rc) as EnumConstant;
+                               try {
+                                       return c.Increment ();
+                               } catch (OverflowException) {
+                                       rc.Report.Error (543, current.Location,
+                                               "The enumerator value `{0}' is outside the range of enumerator underlying type `{1}'",
+                                               current.GetSignatureForError (), ((Enum) current.Parent).UnderlyingType.GetSignatureForError ());
+
+                                       return New.Constantify (current.Parent.Definition, current.Location);
+                               }
+                       }
+
+                       public override void Emit (EmitContext ec)
+                       {
+                               throw new NotSupportedException ("Missing Resolve call");
+                       }
                }
-       }
 
-       /// <summary>
-       ///   Enumeration container
-       /// </summary>
-       public class Enum : TypeContainer
-       {
                public static readonly string UnderlyingValueField = "value__";
 
-               TypeExpr base_type;
-
                const Modifiers AllowedModifiers =
                        Modifiers.NEW |
                        Modifiers.PUBLIC |
@@ -135,16 +155,52 @@ namespace Mono.CSharp {
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE;
 
-               public Enum (NamespaceEntry ns, DeclSpace parent, TypeExpr type,
+               readonly TypeExpr underlying_type_expr;
+
+               public Enum (NamespaceContainer ns, DeclSpace parent, TypeExpression type,
                             Modifiers mod_flags, MemberName name, Attributes attrs)
                        : base (ns, parent, name, attrs, MemberKind.Enum)
                {
-                       this.base_type = type;
+                       underlying_type_expr = type;
                        var accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE;
                        ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod_flags, accmods, Location, Report);
                        spec = new EnumSpec (null, this, null, null, ModFlags);
                }
 
+               #region Properties
+
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Enum;
+                       }
+               }
+
+               public TypeExpr BaseTypeExpression {
+                       get {
+                               return underlying_type_expr;
+                       }
+               }
+
+               protected override TypeAttributes TypeAttr {
+                       get {
+                               return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel) |
+                                       TypeAttributes.Class | TypeAttributes.Sealed | base.TypeAttr;
+                       }
+               }
+
+               public TypeSpec UnderlyingType {
+                       get {
+                               return ((EnumSpec) spec).UnderlyingType;
+                       }
+               }
+
+               #endregion
+
+               public override void Accept (StructuralVisitor visitor)
+               {
+                       visitor.Visit (this);
+               }
+
                public void AddEnumMember (EnumMember em)
                {
                        if (em.Name == UnderlyingValueField) {
@@ -158,29 +214,33 @@ namespace Mono.CSharp {
 
                public static void Error_1008 (Location loc, Report Report)
                {
-                       Report.Error (1008, loc, "Type byte, sbyte, short, ushort, " +
-                                     "int, uint, long or ulong expected");
+                       Report.Error (1008, loc,
+                               "Type byte, sbyte, short, ushort, int, uint, long or ulong expected");
                }
 
                protected override bool DefineNestedTypes ()
                {
-                       if (!base.DefineNestedTypes ())
-                               return false;
-
-                       ((EnumSpec) spec).UnderlyingType = UnderlyingType;
+                       ((EnumSpec) spec).UnderlyingType = underlying_type_expr == null ? Compiler.BuiltinTypes.Int : underlying_type_expr.Type;
 
                        TypeBuilder.DefineField (UnderlyingValueField, UnderlyingType.GetMetaInfo (),
                                FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);
 
-                       if (!RootContext.StdLib)
-                               RootContext.hack_corlib_enums.Add (this);
-
                        return true;
                }
 
                protected override bool DoDefineMembers ()
                {
-                       DefineContainerMembers (constants);
+                       if (constants != null) {
+                               for (int i = 0; i < constants.Count; ++i) {
+                                       EnumMember em = (EnumMember) constants [i];
+                                       if (em.Initializer == null) {
+                                               em.Initializer = new ImplicitInitializer (em, i == 0 ? null : (EnumMember) constants[i - 1]);
+                                       }
+
+                                       em.Define ();
+                               }
+                       }
+
                        return true;
                }
 
@@ -189,10 +249,11 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public TypeSpec UnderlyingType {
-                       get {
-                               return base_type.Type;
-                       }
+               protected override TypeSpec[] ResolveBaseTypes (out FullNamedExpression base_class)
+               {
+                       base_type = Compiler.BuiltinTypes.Enum;
+                       base_class = null;
+                       return null;
                }
 
                protected override bool VerifyClsCompliance ()
@@ -200,34 +261,24 @@ namespace Mono.CSharp {
                        if (!base.VerifyClsCompliance ())
                                return false;
 
-                       if (UnderlyingType == TypeManager.uint32_type ||
-                               UnderlyingType == TypeManager.uint64_type ||
-                               UnderlyingType == TypeManager.ushort_type) {
-                               Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
+                       switch (UnderlyingType.BuiltinType) {
+                       case BuiltinTypeSpec.Type.UInt:
+                       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));
+                               break;
                        }
 
                        return true;
                }       
-
-               public override AttributeTargets AttributeTargets {
-                       get {
-                               return AttributeTargets.Enum;
-                       }
-               }
-
-               protected override TypeAttributes TypeAttr {
-                       get {
-                               return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel) |
-                                       TypeAttributes.Class | TypeAttributes.Sealed | base.TypeAttr;
-                       }
-               }
        }
 
        class EnumSpec : TypeSpec
        {
                TypeSpec underlying;
 
-               public EnumSpec (TypeSpec declaringType, ITypeDefinition definition, TypeSpec underlyingType, Type info, Modifiers modifiers)
+               public EnumSpec (TypeSpec declaringType, ITypeDefinition definition, TypeSpec underlyingType, MetaType info, Modifiers modifiers)
                        : base (MemberKind.Enum, declaringType, definition, info, modifiers | Modifiers.SEALED)
                {
                        this.underlying = underlyingType;
@@ -249,5 +300,22 @@ namespace Mono.CSharp {
                {
                        return ((EnumSpec) t.GetDefinition ()).UnderlyingType;
                }
+
+               public static bool IsValidUnderlyingType (TypeSpec type)
+               {
+                       switch (type.BuiltinType) {
+                       case BuiltinTypeSpec.Type.Int:
+                       case BuiltinTypeSpec.Type.UInt:
+                       case BuiltinTypeSpec.Type.Long:
+                       case BuiltinTypeSpec.Type.Byte:
+                       case BuiltinTypeSpec.Type.SByte:
+                       case BuiltinTypeSpec.Type.Short:
+                       case BuiltinTypeSpec.Type.UShort:
+                       case BuiltinTypeSpec.Type.ULong:
+                               return true;
+                       }
+
+                       return false;
+               }
        }
 }