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;
77 // This is used by corlib compilation: we map from our
78 // type to a type that is consumable by the DefineField
80 Type MapToInternalType (Type t)
82 if (t == TypeManager.int32_type)
84 if (t == TypeManager.int64_type)
86 if (t == TypeManager.uint32_type)
88 if (t == TypeManager.uint64_type)
89 return typeof (ulong);
90 if (t == TypeManager.float_type)
91 return typeof (float);
92 if (t == TypeManager.double_type)
93 return typeof (double);
94 if (t == TypeManager.byte_type)
96 if (t == TypeManager.sbyte_type)
97 return typeof (sbyte);
98 if (t == TypeManager.char_type)
100 if (t == TypeManager.short_type)
101 return typeof (short);
102 if (t == TypeManager.ushort_type)
103 return typeof (ushort);
105 throw new Exception ();
108 public override TypeBuilder DefineType ()
110 if (TypeBuilder != null)
113 TypeAttributes attr = TypeAttributes.Class | TypeAttributes.Sealed;
115 UnderlyingType = TypeManager.LookupType (BaseType);
117 if (UnderlyingType != TypeManager.int32_type &&
118 UnderlyingType != TypeManager.uint32_type &&
119 UnderlyingType != TypeManager.int64_type &&
120 UnderlyingType != TypeManager.uint64_type &&
121 UnderlyingType != TypeManager.short_type &&
122 UnderlyingType != TypeManager.ushort_type &&
123 UnderlyingType != TypeManager.byte_type &&
124 UnderlyingType != TypeManager.sbyte_type) {
125 Report.Error (1008, Location,
126 "Type byte, sbyte, short, ushort, int, uint, " +
127 "long, or ulong expected (got: " +
128 TypeManager.CSharpName (UnderlyingType) + ")");
133 ModuleBuilder builder = CodeGen.ModuleBuilder;
135 if ((ModFlags & Modifiers.PUBLIC) != 0)
136 attr |= TypeAttributes.Public;
138 attr |= TypeAttributes.NotPublic;
140 TypeBuilder = builder.DefineType (Name, attr, TypeManager.enum_type);
142 TypeBuilder builder = Parent.TypeBuilder;
144 if ((ModFlags & Modifiers.PUBLIC) != 0)
145 attr |= TypeAttributes.NestedPublic;
147 attr |= TypeAttributes.NestedPrivate;
150 TypeBuilder = builder.DefineNestedType (
151 Basename, attr, TypeManager.enum_type);
155 // Call MapToInternalType for corlib
157 TypeBuilder.DefineField ("value__", UnderlyingType,
158 FieldAttributes.Public | FieldAttributes.SpecialName
159 | FieldAttributes.RTSpecialName);
161 TypeManager.AddEnumType (Name, TypeBuilder, this);
166 bool IsValidEnumConstant (Expression e)
168 if (!(e is Constant))
171 if (e is IntConstant || e is UIntConstant || e is LongConstant ||
172 e is ByteConstant || e is SByteConstant || e is ShortConstant ||
173 e is UShortConstant || e is ULongConstant || e is EnumConstant)
179 object GetNextDefaultValue (object default_value)
181 if (UnderlyingType == TypeManager.int32_type) {
182 int i = (int) default_value;
184 if (i < System.Int32.MaxValue)
188 } else if (UnderlyingType == TypeManager.uint32_type) {
189 uint i = (uint) default_value;
191 if (i < System.UInt32.MaxValue)
195 } else if (UnderlyingType == TypeManager.int64_type) {
196 long i = (long) default_value;
198 if (i < System.Int64.MaxValue)
202 } else if (UnderlyingType == TypeManager.uint64_type) {
203 ulong i = (ulong) default_value;
205 if (i < System.UInt64.MaxValue)
209 } else if (UnderlyingType == TypeManager.short_type) {
210 short i = (short) default_value;
212 if (i < System.Int16.MaxValue)
216 } else if (UnderlyingType == TypeManager.ushort_type) {
217 ushort i = (ushort) default_value;
219 if (i < System.UInt16.MaxValue)
223 } else if (UnderlyingType == TypeManager.byte_type) {
224 byte i = (byte) default_value;
226 if (i < System.Byte.MaxValue)
230 } else if (UnderlyingType == TypeManager.sbyte_type) {
231 sbyte i = (sbyte) default_value;
233 if (i < System.SByte.MaxValue)
242 void Error_ConstantValueCannotBeConverted (object val, Location loc)
245 Report.Error (31, loc, "Constant value '" + ((Constant) val).AsString () +
246 "' cannot be converted" +
247 " to a " + TypeManager.CSharpName (UnderlyingType));
249 Report.Error (31, loc, "Constant value '" + val +
250 "' cannot be converted" +
251 " to a " + TypeManager.CSharpName (UnderlyingType));
256 /// This is used to lookup the value of an enum member. If the member is undefined,
257 /// it attempts to define it and return its value
259 public object LookupEnumValue (EmitContext ec, string name, Location loc)
261 object default_value = null;
264 default_value = member_to_value [name];
266 if (default_value != null)
267 return default_value;
269 if (!defined_names.Contains (name)) {
270 Report.Error (117, loc, "'"+ Name + "' does not contain a definition for '"
276 // So if the above doesn't happen, we have a member that is undefined
277 // We now proceed to define it
279 Expression val = this [name];
283 int idx = ordered_enums.IndexOf (name);
288 for (int i = 0; i < idx; ++i) {
289 string n = (string) ordered_enums [i];
290 Location m_loc = (Mono.CSharp.Location)
291 member_to_location [n];
292 default_value = LookupEnumValue (ec, n, m_loc);
295 default_value = GetNextDefaultValue (default_value);
299 bool old = ec.InEnumContext;
300 ec.InEnumContext = true;
301 val = val.Resolve (ec);
302 ec.InEnumContext = old;
305 Report.Error (-12, loc, "Definition is circular.");
309 if (IsValidEnumConstant (val)) {
311 default_value = c.GetValue ();
313 if (default_value == null) {
314 Error_ConstantValueCannotBeConverted (c, loc);
321 "Type byte, sbyte, short, ushort, int, uint, long, or " +
322 "ulong expected (have: " + val + ")");
327 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
328 | FieldAttributes.Literal;
330 FieldBuilder fb = TypeBuilder.DefineField (name, UnderlyingType, attr);
333 default_value = Convert.ChangeType (default_value, UnderlyingType);
335 Error_ConstantValueCannotBeConverted (c, loc);
339 fb.SetConstant (default_value);
340 field_builders.Add (fb);
341 member_to_value [name] = default_value;
343 if (!TypeManager.RegisterFieldValue (fb, default_value))
346 return default_value;
349 public override bool Define (TypeContainer parent)
352 // If there was an error during DefineEnum, return
354 if (TypeBuilder == null)
357 EmitContext ec = new EmitContext (parent, this, Location, null,
358 UnderlyingType, ModFlags, false);
360 object default_value = 0;
362 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
363 | FieldAttributes.Literal;
366 foreach (string name in ordered_enums) {
368 // Have we already been defined, thanks to some cross-referencing ?
370 if (member_to_value.Contains (name))
373 Location loc = (Mono.CSharp.Location) member_to_location [name];
375 if (this [name] != null) {
376 default_value = LookupEnumValue (ec, name, loc);
378 if (default_value == null)
382 FieldBuilder fb = TypeBuilder.DefineField (
383 name, UnderlyingType, attr);
385 if (default_value == null) {
386 Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
392 default_value = Convert.ChangeType (default_value, UnderlyingType);
394 Error_ConstantValueCannotBeConverted (default_value, loc);
398 fb.SetConstant (default_value);
399 field_builders.Add (fb);
400 member_to_value [name] = default_value;
402 if (!TypeManager.RegisterFieldValue (fb, default_value))
406 default_value = GetNextDefaultValue (default_value);
409 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
415 // Hack around System.Reflection as found everywhere else
417 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf,
418 MemberFilter filter, object criteria)
420 ArrayList members = new ArrayList ();
422 if ((mt & MemberTypes.Field) != 0) {
423 foreach (FieldBuilder fb in field_builders)
424 if (filter (fb, criteria) == true)
428 int count = members.Count;
431 MemberInfo [] mi = new MemberInfo [count];
432 members.CopyTo (mi, 0);
439 public ArrayList ValueNames {
441 return ordered_enums;
446 public Expression this [string name] {
448 return (Expression) defined_names [name];