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 bool ContainsEmitWithAwait ()
119 public override Expression CreateExpressionTree (ResolveContext ec)
121 throw new NotSupportedException ("Missing Resolve call");
124 protected override Expression DoResolve (ResolveContext rc)
126 // We are the first member
128 return New.Constantify (current.Parent.Definition, Location);
131 var c = ((ConstSpec) prev.Spec).GetConstant (rc) as EnumConstant;
133 return c.Increment ();
134 } catch (OverflowException) {
135 rc.Report.Error (543, current.Location,
136 "The enumerator value `{0}' is outside the range of enumerator underlying type `{1}'",
137 current.GetSignatureForError (), ((Enum) current.Parent).UnderlyingType.GetSignatureForError ());
139 return New.Constantify (current.Parent.Definition, current.Location);
143 public override void Emit (EmitContext ec)
145 throw new NotSupportedException ("Missing Resolve call");
149 public static readonly string UnderlyingValueField = "value__";
151 const Modifiers AllowedModifiers =
154 Modifiers.PROTECTED |
158 readonly TypeExpr underlying_type_expr;
160 public Enum (NamespaceContainer ns, DeclSpace parent, TypeExpression type,
161 Modifiers mod_flags, MemberName name, Attributes attrs)
162 : base (ns, parent, name, attrs, MemberKind.Enum)
164 underlying_type_expr = type;
165 var accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE;
166 ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod_flags, accmods, Location, Report);
167 spec = new EnumSpec (null, this, null, null, ModFlags);
172 public override AttributeTargets AttributeTargets {
174 return AttributeTargets.Enum;
178 public TypeExpr BaseTypeExpression {
180 return underlying_type_expr;
184 protected override TypeAttributes TypeAttr {
186 return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel) |
187 TypeAttributes.Class | TypeAttributes.Sealed | base.TypeAttr;
191 public TypeSpec UnderlyingType {
193 return ((EnumSpec) spec).UnderlyingType;
199 public override void Accept (StructuralVisitor visitor)
201 visitor.Visit (this);
204 public void AddEnumMember (EnumMember em)
206 if (em.Name == UnderlyingValueField) {
207 Report.Error (76, em.Location, "An item in an enumeration cannot have an identifier `{0}'",
208 UnderlyingValueField);
215 public static void Error_1008 (Location loc, Report Report)
217 Report.Error (1008, loc,
218 "Type byte, sbyte, short, ushort, int, uint, long or ulong expected");
221 protected override bool DefineNestedTypes ()
223 ((EnumSpec) spec).UnderlyingType = underlying_type_expr == null ? Compiler.BuiltinTypes.Int : underlying_type_expr.Type;
225 TypeBuilder.DefineField (UnderlyingValueField, UnderlyingType.GetMetaInfo (),
226 FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);
231 protected override bool DoDefineMembers ()
233 if (constants != null) {
234 for (int i = 0; i < constants.Count; ++i) {
235 EnumMember em = (EnumMember) constants [i];
236 if (em.Initializer == null) {
237 em.Initializer = new ImplicitInitializer (em, i == 0 ? null : (EnumMember) constants[i - 1]);
247 public override bool IsUnmanagedType ()
252 protected override TypeSpec[] ResolveBaseTypes (out FullNamedExpression base_class)
254 base_type = Compiler.BuiltinTypes.Enum;
259 protected override bool VerifyClsCompliance ()
261 if (!base.VerifyClsCompliance ())
264 switch (UnderlyingType.BuiltinType) {
265 case BuiltinTypeSpec.Type.UInt:
266 case BuiltinTypeSpec.Type.ULong:
267 case BuiltinTypeSpec.Type.UShort:
268 Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant",
269 GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
277 class EnumSpec : TypeSpec
281 public EnumSpec (TypeSpec declaringType, ITypeDefinition definition, TypeSpec underlyingType, MetaType info, Modifiers modifiers)
282 : base (MemberKind.Enum, declaringType, definition, info, modifiers | Modifiers.SEALED)
284 this.underlying = underlyingType;
287 public TypeSpec UnderlyingType {
292 if (underlying != null)
293 throw new InternalErrorException ("UnderlyingType reset");
299 public static TypeSpec GetUnderlyingType (TypeSpec t)
301 return ((EnumSpec) t.GetDefinition ()).UnderlyingType;
304 public static bool IsValidUnderlyingType (TypeSpec type)
306 switch (type.BuiltinType) {
307 case BuiltinTypeSpec.Type.Int:
308 case BuiltinTypeSpec.Type.UInt:
309 case BuiltinTypeSpec.Type.Long:
310 case BuiltinTypeSpec.Type.Byte:
311 case BuiltinTypeSpec.Type.SByte:
312 case BuiltinTypeSpec.Type.Short:
313 case BuiltinTypeSpec.Type.UShort:
314 case BuiltinTypeSpec.Type.ULong: