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 // Dual licensed under the terms of the MIT X11 or GNU GPL
10 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2003-2003 Novell, Inc (http://www.novell.com)
15 using System.Collections;
16 using System.Collections.Specialized;
17 using System.Reflection;
18 using System.Reflection.Emit;
19 using System.Globalization;
21 namespace Mono.CSharp {
23 public class EnumMember : Const {
24 protected readonly Enum ParentEnum;
25 protected readonly Expression ValueExpr;
26 readonly EnumMember prev_member;
28 public EnumMember (Enum parent, EnumMember prev_member, string name, Expression expr,
29 Attributes attrs, Location loc)
30 : base (parent, new EnumTypeExpr (parent), name, expr, Modifiers.PUBLIC,
33 this.ParentEnum = parent;
34 this.ValueExpr = expr;
35 this.prev_member = prev_member;
38 protected class EnumTypeExpr : TypeExpr
40 public readonly Enum Enum;
42 public EnumTypeExpr (Enum e)
47 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
49 type = Enum.CurrentType != null ? Enum.CurrentType : Enum.TypeBuilder;
53 public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
55 return DoResolveAsTypeStep (ec);
59 static bool IsValidEnumType (Type t)
61 return (t == TypeManager.int32_type || t == TypeManager.uint32_type || t == TypeManager.int64_type ||
62 t == TypeManager.byte_type || t == TypeManager.sbyte_type || t == TypeManager.short_type ||
63 t == TypeManager.ushort_type || t == TypeManager.uint64_type || t == TypeManager.char_type ||
68 get { return ResolveValue () ? value.GetValue () : null; }
71 public override bool Define ()
73 const FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal;
74 FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType, attr);
75 Parent.MemberCache.AddMember (FieldBuilder, this);
76 TypeManager.RegisterConstant (FieldBuilder, this);
80 protected override Constant DoResolveValue (EmitContext ec)
82 if (ValueExpr != null) {
83 Constant c = ValueExpr.ResolveAsConstant (ec, this);
87 if (c is EnumConstant)
88 c = ((EnumConstant)c).Child;
90 c = c.ImplicitConversionRequired (ParentEnum.UnderlyingType, Location);
94 if (!IsValidEnumType (c.Type)) {
95 Enum.Error_1008 (Location);
99 return new EnumConstant (c, MemberType);
102 if (prev_member == null)
103 return new EnumConstant (
104 New.Constantify (ParentEnum.UnderlyingType), MemberType);
106 if (!prev_member.ResolveValue ())
110 return (EnumConstant) prev_member.value.Increment ();
111 } catch (OverflowException) {
112 Report.Error (543, Location, "The enumerator value `{0}' is too " +
113 "large to fit in its type `{1}'", GetSignatureForError (),
114 TypeManager.CSharpName (ParentEnum.UnderlyingType));
121 /// Enumeration container
123 public class Enum : TypeContainer
125 public static readonly string UnderlyingValueField = "value__";
127 FullNamedExpression base_type;
129 public Type UnderlyingType;
131 public const int AllowedModifiers =
134 Modifiers.PROTECTED |
138 public Enum (NamespaceEntry ns, DeclSpace parent, FullNamedExpression type,
139 int mod_flags, MemberName name, Attributes attrs)
140 : base (ns, parent, name, attrs, Kind.Enum)
142 this.base_type = type;
143 int accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE;
144 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, accmods, Location);
147 public void AddEnumMember (EnumMember em)
149 if (em.Name == UnderlyingValueField) {
150 Report.Error (76, em.Location, "An item in an enumeration cannot have an identifier `{0}'",
151 UnderlyingValueField);
158 public static void Error_1008 (Location loc)
160 Report.Error (1008, loc, "Type byte, sbyte, short, ushort, " +
161 "int, uint, long or ulong expected");
164 protected override bool DefineNestedTypes ()
166 if (!base.DefineNestedTypes ())
169 if (!(base_type is TypeLookupExpression)) {
170 Error_1008 (Location);
174 TypeExpr ute = base_type.ResolveAsTypeTerminal (this, false);
175 UnderlyingType = ute.Type;
177 if (UnderlyingType != TypeManager.int32_type &&
178 UnderlyingType != TypeManager.uint32_type &&
179 UnderlyingType != TypeManager.int64_type &&
180 UnderlyingType != TypeManager.uint64_type &&
181 UnderlyingType != TypeManager.short_type &&
182 UnderlyingType != TypeManager.ushort_type &&
183 UnderlyingType != TypeManager.byte_type &&
184 UnderlyingType != TypeManager.sbyte_type) {
185 Error_1008 (Location);
190 // Call MapToInternalType for corlib
192 TypeBuilder.DefineField (UnderlyingValueField, UnderlyingType,
193 FieldAttributes.Public | FieldAttributes.SpecialName
194 | FieldAttributes.RTSpecialName);
199 protected override bool DoDefineMembers ()
201 member_cache = new MemberCache (TypeManager.enum_type, this);
202 DefineContainerMembers (constants);
207 // Used for error reporting only
209 public EnumMember GetDefinition (object value)
211 foreach (EnumMember e in defined_names.Values) {
212 if (value.Equals (e.Value))
216 throw new ArgumentOutOfRangeException (value.ToString ());
219 protected override bool VerifyClsCompliance ()
221 if (!base.VerifyClsCompliance ())
224 if (UnderlyingType == TypeManager.uint32_type ||
225 UnderlyingType == TypeManager.uint64_type ||
226 UnderlyingType == TypeManager.ushort_type) {
227 Report.Error (3009, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
233 public override AttributeTargets AttributeTargets {
235 return AttributeTargets.Enum;
239 protected override TypeAttributes TypeAttr {
241 return Modifiers.TypeAttr (ModFlags, IsTopLevel) |
242 TypeAttributes.Class | TypeAttributes.Sealed | base.TypeAttr;