[mono-api-html] Print string fields with no (or null) value without an NRE.
[mono.git] / mcs / mcs / enum.cs
index afea3f6913f7427cca204c2a03b12de1fa09e3fe..d2a031bd01960a220b28583df64f92fbae01991d 100644 (file)
@@ -9,62 +9,74 @@
 //
 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
 // Copyright 2003-2003 Novell, Inc (http://www.novell.com)
+// Copyright 2011 Xamarin Inc
 //
 
 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 {
 
-       public class EnumMember : Const {
-               protected readonly Enum ParentEnum;
-               protected readonly Expression ValueExpr;
-               readonly EnumMember prev_member;
-
-               public EnumMember (Enum parent, EnumMember prev_member, string name, Expression expr,
-                                  Attributes attrs, Location loc)
-                       : base (parent, new EnumTypeExpr (parent), name, expr, Modifiers.PUBLIC,
-                               attrs, loc)
-               {
-                       this.ParentEnum = parent;
-                       this.ValueExpr = expr;
-                       this.prev_member = prev_member;
-               }
-
-               protected class EnumTypeExpr : TypeExpr
+       public class EnumMember : Const
+       {
+               class EnumTypeExpr : TypeExpr
                {
-                       public readonly Enum Enum;
-
-                       public EnumTypeExpr (Enum e)
+                       public override TypeSpec ResolveAsType (IMemberContext ec)
                        {
-                               this.Enum = e;
+                               type = ec.CurrentType;
+                               eclass = ExprClass.Type;
+                               return type;
                        }
+               }
 
-                       protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
-                       {
-                               type = Enum.CurrentType != null ? Enum.CurrentType : Enum.TypeBuilder;
-                               return this;
-                       }
+               public EnumMember (Enum parent, MemberName name, Attributes attrs)
+                       : base (parent, new EnumTypeExpr (), Modifiers.PUBLIC, name, attrs)
+               {
+               }
 
-                       public override TypeExpr ResolveAsTypeTerminal (IMemberContext ec, bool silent)
-                       {
-                               return DoResolveAsTypeStep (ec);
+               static bool IsValidEnumType (TypeSpec 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;
                        }
                }
 
-               static bool IsValidEnumType (Type t)
+               public override Constant ConvertInitializer (ResolveContext rc, Constant expr)
                {
-                       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));
-               }
+                       if (expr is EnumConstant)
+                               expr = ((EnumConstant) expr).Child;
+
+                       var underlying = ((Enum) Parent).UnderlyingType;
+                       if (expr != null) {
+                               expr = expr.ImplicitConversionRequired (rc, underlying);
+                               if (expr != null && !IsValidEnumType (expr.Type)) {
+                                       Enum.Error_1008 (Location, Report);
+                                       expr = null;
+                               }
+                       }
+
+                       if (expr == null)
+                               expr = New.Constantify (underlying, Location);
 
-               public object Value {
-                       get { return ResolveValue () ? value.GetValue () : null; }
+                       return new EnumConstant (expr, MemberType);
                }
 
                public override bool Define ()
@@ -73,60 +85,76 @@ namespace Mono.CSharp {
                                return false;
 
                        const FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal;
-                       FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType, attr);
-                       Parent.MemberCache.AddMember (FieldBuilder, this);
-                       TypeManager.RegisterConstant (FieldBuilder, this);
+                       FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType.GetMetaInfo (), attr);
+                       spec = new ConstSpec (Parent.Definition, this, MemberType, FieldBuilder, ModFlags, initializer);
+
+                       Parent.MemberCache.AddMember (spec);
                        return true;
                }
-       
-               protected override Constant DoResolveValue (ResolveContext ec)
+               
+               public override void Accept (StructuralVisitor visitor)
                {
-                       if (ValueExpr != null) {
-                               Constant c = ValueExpr.ResolveAsConstant (ec, this);
-                               if (c == null)
-                                       return null;
+                       visitor.Visit (this);
+               }
 
-                               if (c is EnumConstant)
-                                       c = ((EnumConstant)c).Child;
+       }
 
-                               c = c.ImplicitConversionRequired (ec, ParentEnum.UnderlyingType, Location);
-                               if (c == null)
-                                       return null;
+       /// <summary>
+       ///   Enumeration container
+       /// </summary>
+       public class Enum : TypeDefinition
+       {
+               //
+               // Implicit enum member initializer, used when no constant value is provided
+               //
+               sealed class ImplicitInitializer : Expression
+               {
+                       readonly EnumMember prev;
+                       readonly EnumMember current;
 
-                               if (!IsValidEnumType (c.Type)) {
-                                       Enum.Error_1008 (Location, ec.Report);
-                                       return null;
-                               }
+                       public ImplicitInitializer (EnumMember current, EnumMember prev)
+                       {
+                               this.current = current;
+                               this.prev = prev;
+                       }
+
+                       public override bool ContainsEmitWithAwait ()
+                       {
+                               return false;
+                       }
 
-                               return new EnumConstant (c, MemberType).Resolve (ec);
+                       public override Expression CreateExpressionTree (ResolveContext ec)
+                       {
+                               throw new NotSupportedException ("Missing Resolve call");
                        }
 
-                       if (prev_member == null)
-                               return new EnumConstant (New.Constantify (ParentEnum.UnderlyingType), MemberType).Resolve (ec);
+                       protected override Expression DoResolve (ResolveContext rc)
+                       {
+                               // We are the first member
+                               if (prev == null) {
+                                       return New.Constantify (current.Parent.Definition, Location);
+                               }
+
+                               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 ());
 
-                       if (!prev_member.ResolveValue ())
-                               return null;
+                                       return New.Constantify (current.Parent.Definition, current.Location);
+                               }
+                       }
 
-                       try {
-                               return ((EnumConstant) prev_member.value).Increment ();
-                       } catch (OverflowException) {
-                               Report.Error (543, Location, "The enumerator value `{0}' is too " +
-                                             "large to fit in its type `{1}'", GetSignatureForError (),
-                                             TypeManager.CSharpName (ParentEnum.UnderlyingType));
-                               return null;
+                       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 |
@@ -134,13 +162,48 @@ namespace Mono.CSharp {
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE;
 
-               public Enum (NamespaceEntry ns, DeclSpace parent, TypeExpr type,
-                            Modifiers mod_flags, MemberName name, Attributes attrs)
-                       : base (ns, parent, name, attrs, Kind.Enum)
+               readonly FullNamedExpression underlying_type_expr;
+
+               public Enum (TypeContainer parent, FullNamedExpression type, Modifiers mod_flags, MemberName name, Attributes attrs)
+                       : base (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 FullNamedExpression BaseTypeExpression {
+                       get {
+                               return underlying_type_expr;
+                       }
+               }
+
+               protected override TypeAttributes TypeAttr {
+                       get {
+                               return base.TypeAttr | TypeAttributes.Class | TypeAttributes.Sealed;
+                       }
+               }
+
+               public TypeSpec UnderlyingType {
+                       get {
+                               return ((EnumSpec) spec).UnderlyingType;
+                       }
+               }
+
+               #endregion
+
+               public override void Accept (StructuralVisitor visitor)
+               {
+                       visitor.Visit (this);
                }
 
                public void AddEnumMember (EnumMember em)
@@ -151,34 +214,36 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       AddConstant (em);
+                       AddMember (em);
                }
 
                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 ()
+               protected override void DoDefineContainer ()
                {
-                       if (!base.DefineNestedTypes ())
-                               return false;
+                       ((EnumSpec) spec).UnderlyingType = underlying_type_expr == null ? Compiler.BuiltinTypes.Int : underlying_type_expr.Type;
 
-                       //
-                       // Call MapToInternalType for corlib
-                       //
-                       TypeBuilder.DefineField (UnderlyingValueField, UnderlyingType,
-                                                FieldAttributes.Public | FieldAttributes.SpecialName
-                                                | FieldAttributes.RTSpecialName);
+                       TypeBuilder.DefineField (UnderlyingValueField, UnderlyingType.GetMetaInfo (),
+                               FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);
 
-                       return true;
+                       DefineBaseTypes ();
                }
 
                protected override bool DoDefineMembers ()
                {
-                       member_cache = new MemberCache (TypeManager.enum_type, this);
-                       DefineContainerMembers (constants);
+                       for (int i = 0; i < Members.Count; ++i) {
+                               EnumMember em = (EnumMember) Members[i];
+                               if (em.Initializer == null) {
+                                       em.Initializer = new ImplicitInitializer (em, i == 0 ? null : (EnumMember) Members[i - 1]);
+                               }
+
+                               em.Define ();
+                       }
+
                        return true;
                }
 
@@ -187,10 +252,11 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public Type 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 ()
@@ -198,26 +264,61 @@ 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 (), UnderlyingType.GetSignatureForError ());
+                               break;
                        }
 
                        return true;
                }       
+       }
 
-               public override AttributeTargets AttributeTargets {
+       class EnumSpec : TypeSpec
+       {
+               TypeSpec underlying;
+
+               public EnumSpec (TypeSpec declaringType, ITypeDefinition definition, TypeSpec underlyingType, MetaType info, Modifiers modifiers)
+                       : base (MemberKind.Enum, declaringType, definition, info, modifiers | Modifiers.SEALED)
+               {
+                       this.underlying = underlyingType;
+               }
+
+               public TypeSpec UnderlyingType {
                        get {
-                               return AttributeTargets.Enum;
+                               return underlying;
+                       }
+                       set {
+                               if (underlying != null)
+                                       throw new InternalErrorException ("UnderlyingType reset");
+
+                               underlying = value;
                        }
                }
 
-               protected override TypeAttributes TypeAttr {
-                       get {
-                               return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel) |
-                                       TypeAttributes.Class | TypeAttributes.Sealed | base.TypeAttr;
+               public static TypeSpec GetUnderlyingType (TypeSpec t)
+               {
+                       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;
                }
        }
 }