X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fmcs%2Fenum.cs;h=4b0f4a020432e8691595e31a69658726ba7def7b;hb=12d16ae4269e9a008863e6718075d6c168d6d16c;hp=110162f3b32b210599001194cbaea6a9a375f1f0;hpb=af43d56056c8fa82c9870744d4819499246e2713;p=mono.git diff --git a/mcs/mcs/enum.cs b/mcs/mcs/enum.cs index 110162f3b32..4b0f4a02043 100644 --- a/mcs/mcs/enum.cs +++ b/mcs/mcs/enum.cs @@ -12,134 +12,193 @@ // using System; -using System.Collections; -using System.Collections.Specialized; + +#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 (IResolveContext 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 (IResolveContext 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 || - t.IsEnum); - } + if (expr is EnumConstant) + expr = ((EnumConstant) expr).Child; + + var underlying = ((Enum) Parent).UnderlyingType; + if (expr != null) { + expr = expr.ImplicitConversionRequired (rc, underlying, Location); + 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 () { + 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); - 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 (EmitContext ec) - { - if (ValueExpr != null) { - Constant c = ValueExpr.ResolveAsConstant (ec, this); - if (c == null) - return null; + } - if (c is EnumConstant) - c = ((EnumConstant)c).Child; + /// + /// Enumeration container + /// + public class Enum : TypeContainer + { + // + // Implicit enum member initializer, used when no constant value is provided + // + sealed class ImplicitInitializer : Expression + { + readonly EnumMember prev; + readonly EnumMember current; - c = c.ImplicitConversionRequired (ec, ParentEnum.UnderlyingType, Location); - if (c == null) - return null; + public ImplicitInitializer (EnumMember current, EnumMember prev) + { + this.current = current; + this.prev = prev; + } - if (!IsValidEnumType (c.Type)) { - Enum.Error_1008 (Location); - return null; - } + public override bool ContainsEmitWithAwait () + { + return false; + } - return new EnumConstant (c, MemberType); + public override Expression CreateExpressionTree (ResolveContext ec) + { + throw new NotSupportedException ("Missing Resolve call"); } - if (prev_member == null) - return new EnumConstant ( - New.Constantify (ParentEnum.UnderlyingType), MemberType); + protected override Expression DoResolve (ResolveContext rc) + { + // We are the first member + if (prev == null) { + return New.Constantify (current.Parent.Definition, Location); + } - if (!prev_member.ResolveValue ()) - return null; + 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 ()); - 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; + return New.Constantify (current.Parent.Definition, current.Location); + } + } + + public override void Emit (EmitContext ec) + { + throw new NotSupportedException ("Missing Resolve call"); } } - } - /// - /// Enumeration container - /// - public class Enum : TypeContainer - { public static readonly string UnderlyingValueField = "value__"; - TypeExpr base_type; - - const int AllowedModifiers = + const Modifiers AllowedModifiers = Modifiers.NEW | Modifiers.PUBLIC | Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE; - public Enum (NamespaceEntry ns, DeclSpace parent, TypeExpr type, - int mod_flags, MemberName name, Attributes attrs) - : base (ns, parent, name, attrs, Kind.Enum) + 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) + { + 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) { - this.base_type = type; - int accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE; - ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, accmods, Location); + visitor.Visit (this); } public void AddEnumMember (EnumMember em) @@ -153,51 +212,48 @@ namespace Mono.CSharp { AddConstant (em); } - public static void Error_1008 (Location loc) + 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 = 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; } protected override bool DoDefineMembers () { - member_cache = new MemberCache (TypeManager.enum_type, this); - 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; } - // - // Used for error reporting only - // - public EnumMember GetDefinition (object value) + public override bool IsUnmanagedType () { - foreach (EnumMember e in defined_names.Values) { - if (value.Equals (e.Value)) - return e; - } - - throw new ArgumentOutOfRangeException (value.ToString ()); + 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 () @@ -205,26 +261,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 (), TypeManager.CSharpName (UnderlyingType)); + 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 Modifiers.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; } } }