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)
17 using MetaType = IKVM.Reflection.Type;
18 using IKVM.Reflection;
20 using MetaType = System.Type;
21 using System.Reflection;
24 namespace Mono.CSharp {
26 public class EnumMember : Const
28 class EnumTypeExpr : TypeExpr
30 public override TypeSpec ResolveAsType (IMemberContext ec)
32 type = ec.CurrentType;
33 eclass = ExprClass.Type;
38 public EnumMember (Enum parent, MemberName name, Attributes attrs)
39 : base (parent, new EnumTypeExpr (), Modifiers.PUBLIC, name, attrs)
43 static bool IsValidEnumType (TypeSpec t)
45 switch (t.BuiltinType) {
46 case BuiltinTypeSpec.Type.Int:
47 case BuiltinTypeSpec.Type.UInt:
48 case BuiltinTypeSpec.Type.Long:
49 case BuiltinTypeSpec.Type.Byte:
50 case BuiltinTypeSpec.Type.SByte:
51 case BuiltinTypeSpec.Type.Short:
52 case BuiltinTypeSpec.Type.UShort:
53 case BuiltinTypeSpec.Type.ULong:
54 case BuiltinTypeSpec.Type.Char:
61 public override Constant ConvertInitializer (ResolveContext rc, Constant expr)
63 if (expr is EnumConstant)
64 expr = ((EnumConstant) expr).Child;
66 var underlying = ((Enum) Parent).UnderlyingType;
68 expr = expr.ImplicitConversionRequired (rc, underlying, Location);
69 if (expr != null && !IsValidEnumType (expr.Type)) {
70 Enum.Error_1008 (Location, Report);
76 expr = New.Constantify (underlying, Location);
78 return new EnumConstant (expr, MemberType);
81 public override bool Define ()
83 if (!ResolveMemberType ())
86 const FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal;
87 FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType.GetMetaInfo (), attr);
88 spec = new ConstSpec (Parent.Definition, this, MemberType, FieldBuilder, ModFlags, initializer);
90 Parent.MemberCache.AddMember (spec);
96 /// Enumeration container
98 public class Enum : TypeContainer
101 // Implicit enum member initializer, used when no constant value is provided
103 sealed class ImplicitInitializer : Expression
105 readonly EnumMember prev;
106 readonly EnumMember current;
108 public ImplicitInitializer (EnumMember current, EnumMember prev)
110 this.current = current;
114 public override Expression CreateExpressionTree (ResolveContext ec)
116 throw new NotSupportedException ("Missing Resolve call");
119 protected override Expression DoResolve (ResolveContext rc)
121 // We are the first member
123 return New.Constantify (current.Parent.Definition, Location);
126 var c = ((ConstSpec) prev.Spec).GetConstant (rc) as EnumConstant;
128 return c.Increment ();
129 } catch (OverflowException) {
130 rc.Report.Error (543, current.Location,
131 "The enumerator value `{0}' is outside the range of enumerator underlying type `{1}'",
132 current.GetSignatureForError (), ((Enum) current.Parent).UnderlyingType.GetSignatureForError ());
134 return New.Constantify (current.Parent.Definition, current.Location);
138 public override void Emit (EmitContext ec)
140 throw new NotSupportedException ("Missing Resolve call");
144 public static readonly string UnderlyingValueField = "value__";
146 const Modifiers AllowedModifiers =
149 Modifiers.PROTECTED |
153 readonly TypeExpr underlying_type_expr;
155 public Enum (NamespaceContainer ns, DeclSpace parent, TypeExpression type,
156 Modifiers mod_flags, MemberName name, Attributes attrs)
157 : base (ns, parent, name, attrs, MemberKind.Enum)
159 underlying_type_expr = type;
160 var accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE;
161 ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod_flags, accmods, Location, Report);
162 spec = new EnumSpec (null, this, null, null, ModFlags);
167 public override AttributeTargets AttributeTargets {
169 return AttributeTargets.Enum;
173 public TypeExpr BaseTypeExpression {
175 return underlying_type_expr;
179 protected override TypeAttributes TypeAttr {
181 return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel) |
182 TypeAttributes.Class | TypeAttributes.Sealed | base.TypeAttr;
186 public TypeSpec UnderlyingType {
188 return ((EnumSpec) spec).UnderlyingType;
194 public override void Accept (StructuralVisitor visitor)
196 visitor.Visit (this);
199 public void AddEnumMember (EnumMember em)
201 if (em.Name == UnderlyingValueField) {
202 Report.Error (76, em.Location, "An item in an enumeration cannot have an identifier `{0}'",
203 UnderlyingValueField);
210 public static void Error_1008 (Location loc, Report Report)
212 Report.Error (1008, loc,
213 "Type byte, sbyte, short, ushort, int, uint, long or ulong expected");
216 protected override bool DefineNestedTypes ()
218 ((EnumSpec) spec).UnderlyingType = underlying_type_expr == null ? Compiler.BuiltinTypes.Int : underlying_type_expr.Type;
220 TypeBuilder.DefineField (UnderlyingValueField, UnderlyingType.GetMetaInfo (),
221 FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);
226 protected override bool DoDefineMembers ()
228 if (constants != null) {
229 for (int i = 0; i < constants.Count; ++i) {
230 EnumMember em = (EnumMember) constants [i];
231 if (em.Initializer == null) {
232 em.Initializer = new ImplicitInitializer (em, i == 0 ? null : (EnumMember) constants[i - 1]);
242 public override bool IsUnmanagedType ()
247 protected override TypeSpec[] ResolveBaseTypes (out FullNamedExpression base_class)
249 base_type = Compiler.BuiltinTypes.Enum;
254 protected override bool VerifyClsCompliance ()
256 if (!base.VerifyClsCompliance ())
259 switch (UnderlyingType.BuiltinType) {
260 case BuiltinTypeSpec.Type.UInt:
261 case BuiltinTypeSpec.Type.ULong:
262 case BuiltinTypeSpec.Type.UShort:
263 Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant",
264 GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
272 class EnumSpec : TypeSpec
276 public EnumSpec (TypeSpec declaringType, ITypeDefinition definition, TypeSpec underlyingType, MetaType info, Modifiers modifiers)
277 : base (MemberKind.Enum, declaringType, definition, info, modifiers | Modifiers.SEALED)
279 this.underlying = underlyingType;
282 public TypeSpec UnderlyingType {
287 if (underlying != null)
288 throw new InternalErrorException ("UnderlyingType reset");
294 public static TypeSpec GetUnderlyingType (TypeSpec t)
296 return ((EnumSpec) t.GetDefinition ()).UnderlyingType;
299 public static bool IsValidUnderlyingType (TypeSpec type)
301 switch (type.BuiltinType) {
302 case BuiltinTypeSpec.Type.Int:
303 case BuiltinTypeSpec.Type.UInt:
304 case BuiltinTypeSpec.Type.Long:
305 case BuiltinTypeSpec.Type.Byte:
306 case BuiltinTypeSpec.Type.SByte:
307 case BuiltinTypeSpec.Type.Short:
308 case BuiltinTypeSpec.Type.UShort:
309 case BuiltinTypeSpec.Type.ULong: