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)
12 // Copyright 2011 Xamarin Inc
18 using MetaType = IKVM.Reflection.Type;
19 using IKVM.Reflection;
21 using MetaType = System.Type;
22 using System.Reflection;
25 namespace Mono.CSharp {
27 public class EnumMember : Const
29 class EnumTypeExpr : TypeExpr
31 public override TypeSpec ResolveAsType (IMemberContext ec)
33 type = ec.CurrentType;
34 eclass = ExprClass.Type;
39 public EnumMember (Enum parent, MemberName name, Attributes attrs)
40 : base (parent, new EnumTypeExpr (), Modifiers.PUBLIC, name, attrs)
44 static bool IsValidEnumType (TypeSpec t)
46 switch (t.BuiltinType) {
47 case BuiltinTypeSpec.Type.Int:
48 case BuiltinTypeSpec.Type.UInt:
49 case BuiltinTypeSpec.Type.Long:
50 case BuiltinTypeSpec.Type.Byte:
51 case BuiltinTypeSpec.Type.SByte:
52 case BuiltinTypeSpec.Type.Short:
53 case BuiltinTypeSpec.Type.UShort:
54 case BuiltinTypeSpec.Type.ULong:
55 case BuiltinTypeSpec.Type.Char:
62 public override Constant ConvertInitializer (ResolveContext rc, Constant expr)
64 if (expr is EnumConstant)
65 expr = ((EnumConstant) expr).Child;
67 var underlying = ((Enum) Parent).UnderlyingType;
69 expr = expr.ImplicitConversionRequired (rc, underlying, Location);
70 if (expr != null && !IsValidEnumType (expr.Type)) {
71 Enum.Error_1008 (Location, Report);
77 expr = New.Constantify (underlying, Location);
79 return new EnumConstant (expr, MemberType);
82 public override bool Define ()
84 if (!ResolveMemberType ())
87 const FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal;
88 FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType.GetMetaInfo (), attr);
89 spec = new ConstSpec (Parent.Definition, this, MemberType, FieldBuilder, ModFlags, initializer);
91 Parent.MemberCache.AddMember (spec);
97 /// Enumeration container
99 public class Enum : TypeContainer
102 // Implicit enum member initializer, used when no constant value is provided
104 sealed class ImplicitInitializer : Expression
106 readonly EnumMember prev;
107 readonly EnumMember current;
109 public ImplicitInitializer (EnumMember current, EnumMember prev)
111 this.current = current;
115 public override bool ContainsEmitWithAwait ()
120 public override Expression CreateExpressionTree (ResolveContext ec)
122 throw new NotSupportedException ("Missing Resolve call");
125 protected override Expression DoResolve (ResolveContext rc)
127 // We are the first member
129 return New.Constantify (current.Parent.Definition, Location);
132 var c = ((ConstSpec) prev.Spec).GetConstant (rc) as EnumConstant;
134 return c.Increment ();
135 } catch (OverflowException) {
136 rc.Report.Error (543, current.Location,
137 "The enumerator value `{0}' is outside the range of enumerator underlying type `{1}'",
138 current.GetSignatureForError (), ((Enum) current.Parent).UnderlyingType.GetSignatureForError ());
140 return New.Constantify (current.Parent.Definition, current.Location);
144 public override void Emit (EmitContext ec)
146 throw new NotSupportedException ("Missing Resolve call");
150 public static readonly string UnderlyingValueField = "value__";
152 const Modifiers AllowedModifiers =
155 Modifiers.PROTECTED |
159 readonly TypeExpr underlying_type_expr;
161 public Enum (NamespaceContainer ns, DeclSpace parent, TypeExpression type,
162 Modifiers mod_flags, MemberName name, Attributes attrs)
163 : base (ns, parent, name, attrs, MemberKind.Enum)
165 underlying_type_expr = type;
166 var accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE;
167 ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod_flags, accmods, Location, Report);
168 spec = new EnumSpec (null, this, null, null, ModFlags);
173 public override AttributeTargets AttributeTargets {
175 return AttributeTargets.Enum;
179 public TypeExpr BaseTypeExpression {
181 return underlying_type_expr;
185 protected override TypeAttributes TypeAttr {
187 return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel) |
188 TypeAttributes.Class | TypeAttributes.Sealed | base.TypeAttr;
192 public TypeSpec UnderlyingType {
194 return ((EnumSpec) spec).UnderlyingType;
200 public override void Accept (StructuralVisitor visitor)
202 visitor.Visit (this);
205 public void AddEnumMember (EnumMember em)
207 if (em.Name == UnderlyingValueField) {
208 Report.Error (76, em.Location, "An item in an enumeration cannot have an identifier `{0}'",
209 UnderlyingValueField);
216 public static void Error_1008 (Location loc, Report Report)
218 Report.Error (1008, loc,
219 "Type byte, sbyte, short, ushort, int, uint, long or ulong expected");
222 protected override bool DefineNestedTypes ()
224 ((EnumSpec) spec).UnderlyingType = underlying_type_expr == null ? Compiler.BuiltinTypes.Int : underlying_type_expr.Type;
226 TypeBuilder.DefineField (UnderlyingValueField, UnderlyingType.GetMetaInfo (),
227 FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);
232 protected override bool DoDefineMembers ()
234 if (constants != null) {
235 for (int i = 0; i < constants.Count; ++i) {
236 EnumMember em = (EnumMember) constants [i];
237 if (em.Initializer == null) {
238 em.Initializer = new ImplicitInitializer (em, i == 0 ? null : (EnumMember) constants[i - 1]);
248 public override bool IsUnmanagedType ()
253 protected override TypeSpec[] ResolveBaseTypes (out FullNamedExpression base_class)
255 base_type = Compiler.BuiltinTypes.Enum;
260 protected override bool VerifyClsCompliance ()
262 if (!base.VerifyClsCompliance ())
265 switch (UnderlyingType.BuiltinType) {
266 case BuiltinTypeSpec.Type.UInt:
267 case BuiltinTypeSpec.Type.ULong:
268 case BuiltinTypeSpec.Type.UShort:
269 Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant",
270 GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
278 class EnumSpec : TypeSpec
282 public EnumSpec (TypeSpec declaringType, ITypeDefinition definition, TypeSpec underlyingType, MetaType info, Modifiers modifiers)
283 : base (MemberKind.Enum, declaringType, definition, info, modifiers | Modifiers.SEALED)
285 this.underlying = underlyingType;
288 public TypeSpec UnderlyingType {
293 if (underlying != null)
294 throw new InternalErrorException ("UnderlyingType reset");
300 public static TypeSpec GetUnderlyingType (TypeSpec t)
302 return ((EnumSpec) t.GetDefinition ()).UnderlyingType;
305 public static bool IsValidUnderlyingType (TypeSpec type)
307 switch (type.BuiltinType) {
308 case BuiltinTypeSpec.Type.Int:
309 case BuiltinTypeSpec.Type.UInt:
310 case BuiltinTypeSpec.Type.Long:
311 case BuiltinTypeSpec.Type.Byte:
312 case BuiltinTypeSpec.Type.SByte:
313 case BuiltinTypeSpec.Type.Short:
314 case BuiltinTypeSpec.Type.UShort:
315 case BuiltinTypeSpec.Type.ULong: