2 // enum.cs: Enum handling.
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 // Ravi Pratap (ravi@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
14 using System.Collections;
15 using System.Collections.Specialized;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Globalization;
20 namespace Mono.CSharp {
22 public class EnumMember : Const {
23 protected readonly Enum ParentEnum;
24 protected readonly Expression ValueExpr;
25 readonly EnumMember prev_member;
27 public EnumMember (Enum parent, EnumMember prev_member, string name, Expression expr,
28 Attributes attrs, Location loc)
29 : base (parent, new EnumTypeExpr (parent), name, expr, Modifiers.PUBLIC,
32 this.ParentEnum = parent;
33 this.ValueExpr = expr;
34 this.prev_member = prev_member;
37 protected class EnumTypeExpr : TypeExpr
39 public readonly Enum Enum;
41 public EnumTypeExpr (Enum e)
46 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
48 type = Enum.CurrentType != null ? Enum.CurrentType : Enum.TypeBuilder;
52 public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
54 return DoResolveAsTypeStep (ec);
57 public override string Name {
58 get { return Enum.Name; }
61 public override string FullName {
62 get { return Enum.Name; }
66 static bool IsValidEnumType (Type t)
68 return (t == TypeManager.int32_type || t == TypeManager.uint32_type || t == TypeManager.int64_type ||
69 t == TypeManager.byte_type || t == TypeManager.sbyte_type || t == TypeManager.short_type ||
70 t == TypeManager.ushort_type || t == TypeManager.uint64_type || t == TypeManager.char_type ||
75 get { return value.GetValue (); }
78 protected override Constant DoResolveValue (EmitContext ec)
80 if (ValueExpr != null) {
81 Constant c = ValueExpr.ResolveAsConstant (ec, this);
85 if (c is EnumConstant)
86 c = ((EnumConstant)c).Child;
88 c = c.ImplicitConversionRequired (ParentEnum.UnderlyingType, Location);
92 if (!IsValidEnumType (c.Type)) {
93 Enum.Error_1008 (Location);
97 return new EnumConstant (c, MemberType);
100 if ((prev_member == null) || (prev_member.value == null)) {
101 return new EnumConstant (
102 New.Constantify (ParentEnum.UnderlyingType), MemberType);
106 return (EnumConstant) prev_member.value.Increment ();
107 } catch (OverflowException) {
108 Report.Error (543, Location, "The enumerator value `{0}' is too " +
109 "large to fit in its type `{1}'", GetSignatureForError (),
110 TypeManager.CSharpName (ParentEnum.UnderlyingType));
117 /// Enumeration container
119 public class Enum : TypeContainer
121 Expression base_type;
123 public Type UnderlyingType;
125 public const int AllowedModifiers =
128 Modifiers.PROTECTED |
132 public Enum (NamespaceEntry ns, DeclSpace parent, Expression type,
133 int mod_flags, MemberName name, Attributes attrs)
134 : base (ns, parent, name, attrs, Kind.Enum)
136 this.base_type = type;
137 int accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE;
138 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, accmods, Location);
141 public void AddEnumMember (EnumMember em)
143 if (em.Name == "value__") {
144 Report.Error (76, em.Location, "An item in an enumeration cannot " +
145 "have an identifier `value__'");
152 public static void Error_1008 (Location loc)
154 Report.Error (1008, loc, "Type byte, sbyte, short, ushort, " +
155 "int, uint, long or ulong expected");
158 protected override bool DefineNestedTypes ()
160 if (!base.DefineNestedTypes ())
163 if (!(base_type is TypeLookupExpression)) {
164 Error_1008 (Location);
168 TypeExpr ute = base_type.ResolveAsTypeTerminal (this, false);
169 UnderlyingType = ute.Type;
171 if (UnderlyingType != TypeManager.int32_type &&
172 UnderlyingType != TypeManager.uint32_type &&
173 UnderlyingType != TypeManager.int64_type &&
174 UnderlyingType != TypeManager.uint64_type &&
175 UnderlyingType != TypeManager.short_type &&
176 UnderlyingType != TypeManager.ushort_type &&
177 UnderlyingType != TypeManager.byte_type &&
178 UnderlyingType != TypeManager.sbyte_type) {
179 Error_1008 (Location);
184 // Call MapToInternalType for corlib
186 TypeBuilder.DefineField ("value__", UnderlyingType,
187 FieldAttributes.Public | FieldAttributes.SpecialName
188 | FieldAttributes.RTSpecialName);
194 // Used for error reporting only
196 public EnumMember GetDefinition (object value)
198 foreach (EnumMember e in defined_names.Values) {
199 if (e.Value.Equals (value))
203 throw new ArgumentOutOfRangeException (value.ToString ());
206 protected override bool VerifyClsCompliance ()
208 if (!base.VerifyClsCompliance ())
211 if (UnderlyingType == TypeManager.uint32_type ||
212 UnderlyingType == TypeManager.uint64_type ||
213 UnderlyingType == TypeManager.ushort_type) {
214 Report.Error (3009, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
220 public override AttributeTargets AttributeTargets {
222 return AttributeTargets.Enum;
226 protected override TypeAttributes TypeAttr {
228 return Modifiers.TypeAttr (ModFlags, IsTopLevel) |
229 TypeAttributes.Class | TypeAttributes.Sealed | base.TypeAttr;