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 protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
32 type = ec.CurrentType;
36 public override TypeExpr ResolveAsTypeTerminal (IMemberContext ec, bool silent)
38 return DoResolveAsTypeStep (ec);
42 public EnumMember (Enum parent, MemberName name, Attributes attrs)
43 : base (parent, new EnumTypeExpr (), Modifiers.PUBLIC, name, attrs)
47 static bool IsValidEnumType (TypeSpec t)
49 return (t == TypeManager.int32_type || t == TypeManager.uint32_type || t == TypeManager.int64_type ||
50 t == TypeManager.byte_type || t == TypeManager.sbyte_type || t == TypeManager.short_type ||
51 t == TypeManager.ushort_type || t == TypeManager.uint64_type || t == TypeManager.char_type ||
52 TypeManager.IsEnumType (t));
55 public override Constant ConvertInitializer (ResolveContext rc, Constant expr)
57 if (expr is EnumConstant)
58 expr = ((EnumConstant) expr).Child;
60 var underlying = ((Enum) Parent).UnderlyingType;
62 expr = expr.ImplicitConversionRequired (rc, underlying, Location);
63 if (expr != null && !IsValidEnumType (expr.Type)) {
64 Enum.Error_1008 (Location, Report);
70 expr = New.Constantify (underlying, Location);
72 return new EnumConstant (expr, MemberType).Resolve (rc);
75 public override bool Define ()
77 if (!ResolveMemberType ())
80 const FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal;
81 FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType.GetMetaInfo (), attr);
82 spec = new ConstSpec (Parent.Definition, this, MemberType, FieldBuilder, ModFlags, initializer);
84 Parent.MemberCache.AddMember (spec);
90 /// Enumeration container
92 public class Enum : TypeContainer
95 // Implicit enum member initializer, used when no constant value is provided
97 class ImplicitInitializer : Expression
99 readonly EnumMember prev;
100 readonly EnumMember current;
102 public ImplicitInitializer (EnumMember current, EnumMember prev)
104 this.current = current;
108 public override Expression CreateExpressionTree (ResolveContext ec)
110 throw new NotSupportedException ("Missing Resolve call");
113 protected override Expression DoResolve (ResolveContext rc)
115 // We are the first member
117 return New.Constantify (current.Parent.Definition, Location).Resolve (rc);
120 var c = ((ConstSpec) prev.Spec).GetConstant (rc) as EnumConstant;
122 return c.Increment ().Resolve (rc);
123 } catch (OverflowException) {
124 rc.Report.Error (543, current.Location,
125 "The enumerator value `{0}' is outside the range of enumerator underlying type `{1}'",
126 current.GetSignatureForError (), ((Enum) current.Parent).UnderlyingType.GetSignatureForError ());
128 return New.Constantify (current.Parent.Definition, current.Location).Resolve (rc);
132 public override void Emit (EmitContext ec)
134 throw new NotSupportedException ("Missing Resolve call");
138 public static readonly string UnderlyingValueField = "value__";
140 const Modifiers AllowedModifiers =
143 Modifiers.PROTECTED |
147 public Enum (NamespaceEntry ns, DeclSpace parent, TypeExpression type,
148 Modifiers mod_flags, MemberName name, Attributes attrs)
149 : base (ns, parent, name, attrs, MemberKind.Enum)
151 base_type_expr = type;
152 var accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE;
153 ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod_flags, accmods, Location, Report);
154 spec = new EnumSpec (null, this, null, null, ModFlags);
159 public override AttributeTargets AttributeTargets {
161 return AttributeTargets.Enum;
165 public TypeExpr BaseTypeExpression {
167 return base_type_expr;
171 protected override TypeAttributes TypeAttr {
173 return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel) |
174 TypeAttributes.Class | TypeAttributes.Sealed | base.TypeAttr;
178 public TypeSpec UnderlyingType {
180 return ((EnumSpec) spec).UnderlyingType;
186 public void AddEnumMember (EnumMember em)
188 if (em.Name == UnderlyingValueField) {
189 Report.Error (76, em.Location, "An item in an enumeration cannot have an identifier `{0}'",
190 UnderlyingValueField);
197 public static void Error_1008 (Location loc, Report Report)
199 Report.Error (1008, loc,
200 "Type byte, sbyte, short, ushort, int, uint, long or ulong expected");
203 protected override bool DefineNestedTypes ()
205 ((EnumSpec) spec).UnderlyingType = base_type_expr == null ? TypeManager.int32_type : base_type_expr.Type;
207 TypeBuilder.DefineField (UnderlyingValueField, UnderlyingType.GetMetaInfo (),
208 FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);
213 protected override bool DoDefineMembers ()
215 if (constants != null) {
216 for (int i = 0; i < constants.Count; ++i) {
217 EnumMember em = (EnumMember) constants [i];
218 if (em.Initializer == null) {
219 em.Initializer = new ImplicitInitializer (em, i == 0 ? null : (EnumMember) constants[i - 1]);
229 public override bool IsUnmanagedType ()
234 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
236 base_type = TypeManager.enum_type;
237 base_class = base_type_expr;
241 protected override bool VerifyClsCompliance ()
243 if (!base.VerifyClsCompliance ())
246 if (UnderlyingType == TypeManager.uint32_type ||
247 UnderlyingType == TypeManager.uint64_type ||
248 UnderlyingType == TypeManager.ushort_type) {
249 Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
256 class EnumSpec : TypeSpec
260 public EnumSpec (TypeSpec declaringType, ITypeDefinition definition, TypeSpec underlyingType, MetaType info, Modifiers modifiers)
261 : base (MemberKind.Enum, declaringType, definition, info, modifiers | Modifiers.SEALED)
263 this.underlying = underlyingType;
266 public TypeSpec UnderlyingType {
271 if (underlying != null)
272 throw new InternalErrorException ("UnderlyingType reset");
278 public static TypeSpec GetUnderlyingType (TypeSpec t)
280 return ((EnumSpec) t.GetDefinition ()).UnderlyingType;