2 // enum.cs: Enum handling.
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 // Ravi Pratap (ravi@ximian.com)
7 // Licensed under the terms of the GNU GPL
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
13 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
19 public class Enum : DeclSpace {
21 ArrayList ordered_enums;
22 public readonly string BaseType;
23 public readonly string EnumName;
25 public TypeBuilder EnumBuilder;
26 public Attributes OptAttributes;
30 public readonly RootContext RootContext;
32 Hashtable member_to_location;
33 ArrayList field_builders;
35 public const int AllowedModifiers =
42 public Enum (RootContext rc, string type, int mod_flags, string name, Attributes attrs, Location l)
48 this.mod_flags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PUBLIC);
49 OptAttributes = attrs;
50 ordered_enums = new ArrayList ();
51 member_to_location = new Hashtable ();
52 field_builders = new ArrayList ();
56 // Adds @name to the enumeration space, with @expr
57 // being its definition.
59 public AdditionResult AddEnumMember (string name, Expression expr, Location loc)
61 if (defined_names.Contains (name))
62 return AdditionResult.NameExists;
64 DefineName (name, expr);
66 ordered_enums.Add (name);
67 member_to_location.Add (name, loc);
69 return AdditionResult.Success;
72 public void DefineEnum (object parent_builder)
74 TypeAttributes attr = TypeAttributes.Class | TypeAttributes.Sealed;
76 UnderlyingType = RootContext.TypeManager.LookupType (BaseType);
78 if (UnderlyingType != TypeManager.int32_type && UnderlyingType != TypeManager.uint32_type &&
79 UnderlyingType != TypeManager.int64_type && UnderlyingType != TypeManager.uint64_type &&
80 UnderlyingType != TypeManager.short_type && UnderlyingType != TypeManager.ushort_type &&
81 UnderlyingType != TypeManager.byte_type && UnderlyingType != TypeManager.sbyte_type) {
82 Report.Error (1008, Location,
83 "Type byte, sbyte, short, ushort, int, uint, long, or ulong expected");
87 if (parent_builder is ModuleBuilder) {
88 ModuleBuilder builder = (ModuleBuilder) parent_builder;
90 if ((ModFlags & Modifiers.PUBLIC) != 0)
91 attr |= TypeAttributes.Public;
93 attr |= TypeAttributes.NotPublic;
95 EnumBuilder = builder.DefineType (EnumName, attr, TypeManager.enum_type);
98 TypeBuilder builder = (TypeBuilder) parent_builder;
100 if ((ModFlags & Modifiers.PUBLIC) != 0)
101 attr |= TypeAttributes.NestedPublic;
103 attr |= TypeAttributes.NestedPrivate;
105 EnumBuilder = builder.DefineNestedType (EnumName, attr, TypeManager.enum_type);
108 EnumBuilder.DefineField ("value__", UnderlyingType,
109 FieldAttributes.Public | FieldAttributes.SpecialName);
111 RootContext.TypeManager.AddEnumType (EnumName, EnumBuilder, this);
116 bool IsValidEnumLiteral (Expression e)
121 if (e is IntLiteral || e is UIntLiteral || e is LongLiteral || e is ULongLiteral)
127 object GetNextDefaultValue (object default_value)
129 if (UnderlyingType == TypeManager.int32_type) {
130 int i = (int) default_value;
132 if (i < System.Int32.MaxValue)
136 } else if (UnderlyingType == TypeManager.uint32_type) {
137 uint i = (uint) default_value;
139 if (i < System.UInt32.MaxValue)
143 } else if (UnderlyingType == TypeManager.int64_type) {
144 long i = (long) default_value;
146 if (i < System.Int64.MaxValue)
150 } else if (UnderlyingType == TypeManager.uint64_type) {
151 ulong i = (ulong) default_value;
153 if (i < System.UInt64.MaxValue)
157 } else if (UnderlyingType == TypeManager.short_type) {
158 short i = (short) default_value;
160 if (i < System.Int16.MaxValue)
164 } else if (UnderlyingType == TypeManager.ushort_type) {
165 ushort i = (ushort) default_value;
167 if (i < System.UInt16.MaxValue)
171 } else if (UnderlyingType == TypeManager.byte_type) {
172 byte i = (byte) default_value;
174 if (i < System.Byte.MaxValue)
178 } else if (UnderlyingType == TypeManager.sbyte_type) {
179 sbyte i = (sbyte) default_value;
181 if (i < System.SByte.MaxValue)
190 void error31 (Literal l, Location loc)
192 Report.Error (31, loc, "Constant value '" + l.AsString () +
193 "' cannot be converted" +
194 " to a " + TypeManager.CSharpName (UnderlyingType));
198 public void Populate (TypeContainer tc)
201 // If there was an error during DefineEnum, return
203 if (EnumBuilder == null){
207 EmitContext ec = new EmitContext (tc, null, UnderlyingType, ModFlags);
209 object default_value = 0;
211 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
212 | FieldAttributes.Literal;
215 foreach (string name in ordered_enums) {
216 Expression e = this [name];
217 Location loc = (Location) member_to_location [name];
220 e = Expression.Reduce (ec, e);
222 if (IsValidEnumLiteral (e)) {
223 Literal l = (Literal) e;
224 default_value = l.GetValue ();
226 if (default_value == null) {
232 Report.Error (1008, loc,
233 "Type byte, sbyte, short, ushort, int, uint, long, or ulong expected");
238 FieldBuilder fb = EnumBuilder.DefineField (name, UnderlyingType, attr);
240 if (default_value == null) {
241 Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
247 default_value = Convert.ChangeType (default_value, UnderlyingType);
249 error31 ((Literal) e, loc);
253 fb.SetConstant (default_value);
254 field_builders.Add (fb);
256 if (!TypeManager.RegisterField (fb, default_value))
259 default_value = GetNextDefaultValue (default_value);
262 if (OptAttributes == null)
265 if (OptAttributes.AttributeSections == null)
268 foreach (AttributeSection asec in OptAttributes.AttributeSections) {
269 if (asec.Attributes == null)
272 foreach (Attribute a in asec.Attributes) {
273 CustomAttributeBuilder cb = a.Resolve (ec);
278 EnumBuilder.SetCustomAttribute (cb);
284 // Hack around System.Reflection as found everywhere else
286 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria)
288 ArrayList members = new ArrayList ();
290 if ((mt & MemberTypes.Field) != 0) {
291 foreach (FieldBuilder fb in field_builders)
292 if (filter (fb, criteria) == true)
296 int count = members.Count;
299 MemberInfo [] mi = new MemberInfo [count];
300 members.CopyTo (mi, 0);
307 public void CloseEnum ()
309 EnumBuilder.CreateType ();
312 public ArrayList ValueNames {
314 return ordered_enums;
318 public int ModFlags {
325 public Expression this [string name] {
327 return (Expression) defined_names [name];