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;
17 namespace Mono.CSharp {
20 /// Enumeration container
22 public class Enum : DeclSpace {
24 ArrayList ordered_enums;
25 public readonly string BaseType;
26 public Attributes OptAttributes;
28 public Type UnderlyingType;
30 Hashtable member_to_location;
33 // This is for members that have been defined
35 Hashtable member_to_value;
37 ArrayList field_builders;
39 public const int AllowedModifiers =
46 public Enum (TypeContainer parent, string type, int mod_flags, string name, Attributes attrs, Location l)
47 : base (parent, name, l)
50 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PUBLIC, l);
51 OptAttributes = attrs;
53 ordered_enums = new ArrayList ();
54 member_to_location = new Hashtable ();
55 member_to_value = new Hashtable ();
56 field_builders = new ArrayList ();
60 /// Adds @name to the enumeration space, with @expr
61 /// being its definition.
63 public AdditionResult AddEnumMember (string name, Expression expr, Location loc)
65 if (defined_names.Contains (name))
66 return AdditionResult.NameExists;
68 DefineName (name, expr);
70 ordered_enums.Add (name);
71 member_to_location.Add (name, loc);
73 return AdditionResult.Success;
76 public override TypeBuilder DefineType ()
78 if (TypeBuilder != null)
81 TypeAttributes attr = TypeAttributes.Class | TypeAttributes.Sealed;
83 UnderlyingType = TypeManager.LookupType (BaseType);
85 if (UnderlyingType != TypeManager.int32_type &&
86 UnderlyingType != TypeManager.uint32_type &&
87 UnderlyingType != TypeManager.int64_type &&
88 UnderlyingType != TypeManager.uint64_type &&
89 UnderlyingType != TypeManager.short_type &&
90 UnderlyingType != TypeManager.ushort_type &&
91 UnderlyingType != TypeManager.byte_type &&
92 UnderlyingType != TypeManager.sbyte_type) {
93 Report.Error (1008, Location,
94 "Type byte, sbyte, short, ushort, int, uint, " +
95 "long, or ulong expected (got: " +
96 TypeManager.CSharpName (UnderlyingType) + ")");
101 ModuleBuilder builder = CodeGen.ModuleBuilder;
103 if ((ModFlags & Modifiers.PUBLIC) != 0)
104 attr |= TypeAttributes.Public;
106 attr |= TypeAttributes.NotPublic;
108 TypeBuilder = builder.DefineType (Name, attr, TypeManager.enum_type);
110 TypeBuilder builder = Parent.TypeBuilder;
112 if ((ModFlags & Modifiers.PUBLIC) != 0)
113 attr |= TypeAttributes.NestedPublic;
115 attr |= TypeAttributes.NestedPrivate;
118 TypeBuilder = builder.DefineNestedType (
119 Basename, attr, TypeManager.enum_type);
122 TypeBuilder.DefineField ("value__", UnderlyingType,
123 FieldAttributes.Public | FieldAttributes.SpecialName
124 | FieldAttributes.RTSpecialName);
126 TypeManager.AddEnumType (Name, TypeBuilder, this);
131 bool IsValidEnumConstant (Expression e)
133 if (!(e is Constant))
136 if (e is IntConstant || e is UIntConstant || e is LongConstant ||
137 e is ByteConstant || e is SByteConstant || e is ShortConstant ||
138 e is UShortConstant || e is ULongConstant || e is EnumConstant)
144 object GetNextDefaultValue (object default_value)
146 if (UnderlyingType == TypeManager.int32_type) {
147 int i = (int) default_value;
149 if (i < System.Int32.MaxValue)
153 } else if (UnderlyingType == TypeManager.uint32_type) {
154 uint i = (uint) default_value;
156 if (i < System.UInt32.MaxValue)
160 } else if (UnderlyingType == TypeManager.int64_type) {
161 long i = (long) default_value;
163 if (i < System.Int64.MaxValue)
167 } else if (UnderlyingType == TypeManager.uint64_type) {
168 ulong i = (ulong) default_value;
170 if (i < System.UInt64.MaxValue)
174 } else if (UnderlyingType == TypeManager.short_type) {
175 short i = (short) default_value;
177 if (i < System.Int16.MaxValue)
181 } else if (UnderlyingType == TypeManager.ushort_type) {
182 ushort i = (ushort) default_value;
184 if (i < System.UInt16.MaxValue)
188 } else if (UnderlyingType == TypeManager.byte_type) {
189 byte i = (byte) default_value;
191 if (i < System.Byte.MaxValue)
195 } else if (UnderlyingType == TypeManager.sbyte_type) {
196 sbyte i = (sbyte) default_value;
198 if (i < System.SByte.MaxValue)
207 void Error_ConstantValueCannotBeConverted (object val, Location loc)
210 Report.Error (31, loc, "Constant value '" + ((Constant) val).AsString () +
211 "' cannot be converted" +
212 " to a " + TypeManager.CSharpName (UnderlyingType));
214 Report.Error (31, loc, "Constant value '" + val +
215 "' cannot be converted" +
216 " to a " + TypeManager.CSharpName (UnderlyingType));
221 /// This is used to lookup the value of an enum member. If the member is undefined,
222 /// it attempts to define it and return its value
224 public object LookupEnumValue (EmitContext ec, string name, Location loc)
226 object default_value = null;
229 default_value = member_to_value [name];
231 if (default_value != null)
232 return default_value;
234 if (!defined_names.Contains (name)) {
235 Report.Error (117, loc, "'"+ Name + "' does not contain a definition for '"
241 // So if the above doesn't happen, we have a member that is undefined
242 // We now proceed to define it
244 Expression val = this [name];
248 int idx = ordered_enums.IndexOf (name);
253 for (int i = 0; i < idx; ++i) {
254 string n = (string) ordered_enums [i];
255 Location m_loc = (Mono.CSharp.Location)
256 member_to_location [n];
257 default_value = LookupEnumValue (ec, n, m_loc);
260 default_value = GetNextDefaultValue (default_value);
264 val = val.Resolve (ec);
267 Report.Error (-12, loc, "Definition is circular.");
271 if (IsValidEnumConstant (val)) {
273 default_value = c.GetValue ();
275 if (default_value == null) {
276 Error_ConstantValueCannotBeConverted (c, loc);
283 "Type byte, sbyte, short, ushort, int, uint, long, or " +
284 "ulong expected (have: " + val + ")");
289 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
290 | FieldAttributes.Literal;
292 FieldBuilder fb = TypeBuilder.DefineField (name, UnderlyingType, attr);
295 default_value = Convert.ChangeType (default_value, UnderlyingType);
297 Error_ConstantValueCannotBeConverted (c, loc);
301 fb.SetConstant (default_value);
302 field_builders.Add (fb);
303 member_to_value [name] = default_value;
305 if (!TypeManager.RegisterFieldValue (fb, default_value))
308 return default_value;
311 public override bool Define (TypeContainer parent)
314 // If there was an error during DefineEnum, return
316 if (TypeBuilder == null)
319 EmitContext ec = new EmitContext (parent, this, Location, null,
320 UnderlyingType, ModFlags, false);
322 object default_value = 0;
324 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
325 | FieldAttributes.Literal;
328 foreach (string name in ordered_enums) {
330 // Have we already been defined, thanks to some cross-referencing ?
332 if (member_to_value.Contains (name))
335 Location loc = (Mono.CSharp.Location) member_to_location [name];
337 if (this [name] != null) {
338 default_value = LookupEnumValue (ec, name, loc);
340 if (default_value == null)
344 FieldBuilder fb = TypeBuilder.DefineField (
345 name, UnderlyingType, attr);
347 if (default_value == null) {
348 Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
354 default_value = Convert.ChangeType (default_value, UnderlyingType);
356 Error_ConstantValueCannotBeConverted (default_value, loc);
360 fb.SetConstant (default_value);
361 field_builders.Add (fb);
362 member_to_value [name] = default_value;
364 if (!TypeManager.RegisterFieldValue (fb, default_value))
368 default_value = GetNextDefaultValue (default_value);
371 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
377 // Hack around System.Reflection as found everywhere else
379 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf,
380 MemberFilter filter, object criteria)
382 ArrayList members = new ArrayList ();
384 if ((mt & MemberTypes.Field) != 0) {
385 foreach (FieldBuilder fb in field_builders)
386 if (filter (fb, criteria) == true)
390 int count = members.Count;
393 MemberInfo [] mi = new MemberInfo [count];
394 members.CopyTo (mi, 0);
401 public ArrayList ValueNames {
403 return ordered_enums;
408 public Expression this [string name] {
410 return (Expression) defined_names [name];