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 {
23 ArrayList ordered_enums;
25 public Expression BaseType;
26 public Attributes OptAttributes;
28 public Type UnderlyingType;
30 Hashtable member_to_location;
31 Hashtable member_to_attributes;
34 // This is for members that have been defined
36 Hashtable member_to_value;
39 // This is used to mark members we're currently defining
43 ArrayList field_builders;
45 public const int AllowedModifiers =
52 public Enum (TypeContainer parent, Expression type, int mod_flags, string name, Attributes attrs, Location l)
53 : base (parent, name, l)
56 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags,
57 IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE, l);
58 OptAttributes = attrs;
60 ordered_enums = new ArrayList ();
61 member_to_location = new Hashtable ();
62 member_to_value = new Hashtable ();
63 in_transit = new Hashtable ();
64 field_builders = new ArrayList ();
68 /// Adds @name to the enumeration space, with @expr
69 /// being its definition.
71 public AdditionResult AddEnumMember (string name, Expression expr, Location loc,
74 if (defined_names.Contains (name))
75 return AdditionResult.NameExists;
77 DefineName (name, expr);
79 ordered_enums.Add (name);
80 member_to_location.Add (name, loc);
82 if (member_to_attributes == null)
83 member_to_attributes = new Hashtable ();
85 member_to_attributes.Add (name, opt_attrs);
87 return AdditionResult.Success;
91 // This is used by corlib compilation: we map from our
92 // type to a type that is consumable by the DefineField
94 Type MapToInternalType (Type t)
96 if (t == TypeManager.int32_type)
98 if (t == TypeManager.int64_type)
100 if (t == TypeManager.uint32_type)
101 return typeof (uint);
102 if (t == TypeManager.uint64_type)
103 return typeof (ulong);
104 if (t == TypeManager.float_type)
105 return typeof (float);
106 if (t == TypeManager.double_type)
107 return typeof (double);
108 if (t == TypeManager.byte_type)
109 return typeof (byte);
110 if (t == TypeManager.sbyte_type)
111 return typeof (sbyte);
112 if (t == TypeManager.char_type)
113 return typeof (char);
114 if (t == TypeManager.short_type)
115 return typeof (short);
116 if (t == TypeManager.ushort_type)
117 return typeof (ushort);
119 throw new Exception ();
122 public override TypeBuilder DefineType ()
124 if (TypeBuilder != null)
127 TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel);
129 attr |= TypeAttributes.Class | TypeAttributes.Sealed;
131 UnderlyingType = ResolveType (BaseType, false, Location);
133 if (UnderlyingType != TypeManager.int32_type &&
134 UnderlyingType != TypeManager.uint32_type &&
135 UnderlyingType != TypeManager.int64_type &&
136 UnderlyingType != TypeManager.uint64_type &&
137 UnderlyingType != TypeManager.short_type &&
138 UnderlyingType != TypeManager.ushort_type &&
139 UnderlyingType != TypeManager.byte_type &&
140 UnderlyingType != TypeManager.sbyte_type) {
141 Report.Error (1008, Location,
142 "Type byte, sbyte, short, ushort, int, uint, " +
143 "long, or ulong expected (got: " +
144 TypeManager.CSharpName (UnderlyingType) + ")");
149 ModuleBuilder builder = CodeGen.ModuleBuilder;
151 TypeBuilder = builder.DefineType (Name, attr, TypeManager.enum_type);
153 TypeBuilder builder = Parent.TypeBuilder;
155 TypeBuilder = builder.DefineNestedType (
156 Basename, attr, TypeManager.enum_type);
160 // Call MapToInternalType for corlib
162 TypeBuilder.DefineField ("value__", UnderlyingType,
163 FieldAttributes.Public | FieldAttributes.SpecialName
164 | FieldAttributes.RTSpecialName);
166 TypeManager.AddEnumType (Name, TypeBuilder, this);
171 bool IsValidEnumConstant (Expression e)
173 if (!(e is Constant))
176 if (e is IntConstant || e is UIntConstant || e is LongConstant ||
177 e is ByteConstant || e is SByteConstant || e is ShortConstant ||
178 e is UShortConstant || e is ULongConstant || e is EnumConstant)
184 object GetNextDefaultValue (object default_value)
186 if (UnderlyingType == TypeManager.int32_type) {
187 int i = (int) default_value;
189 if (i < System.Int32.MaxValue)
193 } else if (UnderlyingType == TypeManager.uint32_type) {
194 uint i = (uint) default_value;
196 if (i < System.UInt32.MaxValue)
200 } else if (UnderlyingType == TypeManager.int64_type) {
201 long i = (long) default_value;
203 if (i < System.Int64.MaxValue)
207 } else if (UnderlyingType == TypeManager.uint64_type) {
208 ulong i = (ulong) default_value;
210 if (i < System.UInt64.MaxValue)
214 } else if (UnderlyingType == TypeManager.short_type) {
215 short i = (short) default_value;
217 if (i < System.Int16.MaxValue)
221 } else if (UnderlyingType == TypeManager.ushort_type) {
222 ushort i = (ushort) default_value;
224 if (i < System.UInt16.MaxValue)
228 } else if (UnderlyingType == TypeManager.byte_type) {
229 byte i = (byte) default_value;
231 if (i < System.Byte.MaxValue)
235 } else if (UnderlyingType == TypeManager.sbyte_type) {
236 sbyte i = (sbyte) default_value;
238 if (i < System.SByte.MaxValue)
247 void Error_ConstantValueCannotBeConverted (object val, Location loc)
250 Report.Error (31, loc, "Constant value '" + ((Constant) val).AsString () +
251 "' cannot be converted" +
252 " to a " + TypeManager.CSharpName (UnderlyingType));
254 Report.Error (31, loc, "Constant value '" + val +
255 "' cannot be converted" +
256 " to a " + TypeManager.CSharpName (UnderlyingType));
261 /// This is used to lookup the value of an enum member. If the member is undefined,
262 /// it attempts to define it and return its value
264 public object LookupEnumValue (EmitContext ec, string name, Location loc)
266 object default_value = null;
269 default_value = member_to_value [name];
271 if (default_value != null)
272 return default_value;
275 // This may happen if we're calling a method in System.Enum, for instance
278 if (!defined_names.Contains (name))
281 if (in_transit.Contains (name)) {
282 Report.Error (110, loc, "The evaluation of the constant value for `" +
283 Name + "." + name + "' involves a circular definition.");
288 // So if the above doesn't happen, we have a member that is undefined
289 // We now proceed to define it
291 Expression val = this [name];
295 int idx = ordered_enums.IndexOf (name);
300 for (int i = 0; i < idx; ++i) {
301 string n = (string) ordered_enums [i];
302 Location m_loc = (Mono.CSharp.Location)
303 member_to_location [n];
304 in_transit.Add (name, true);
305 default_value = LookupEnumValue (ec, n, m_loc);
306 in_transit.Remove (name);
307 if (default_value == null)
311 default_value = GetNextDefaultValue (default_value);
315 bool old = ec.InEnumContext;
316 ec.InEnumContext = true;
317 in_transit.Add (name, true);
318 val = val.Resolve (ec);
319 in_transit.Remove (name);
320 ec.InEnumContext = old;
325 if (IsValidEnumConstant (val)) {
327 default_value = c.GetValue ();
329 if (default_value == null) {
330 Error_ConstantValueCannotBeConverted (c, loc);
337 "Type byte, sbyte, short, ushort, int, uint, long, or " +
338 "ulong expected (have: " + val + ")");
343 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
344 | FieldAttributes.Literal;
346 FieldBuilder fb = TypeBuilder.DefineField (name, UnderlyingType, attr);
349 default_value = TypeManager.ChangeType (default_value, UnderlyingType);
351 Error_ConstantValueCannotBeConverted (c, loc);
355 fb.SetConstant (default_value);
356 field_builders.Add (fb);
357 member_to_value [name] = default_value;
359 if (!TypeManager.RegisterFieldValue (fb, default_value))
363 // Now apply attributes
365 Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name], loc);
367 return default_value;
370 public override bool DefineMembers (TypeContainer parent)
375 public override bool Define (TypeContainer parent)
378 // If there was an error during DefineEnum, return
380 if (TypeBuilder == null)
383 EmitContext ec = new EmitContext (parent, this, Location, null,
384 UnderlyingType, ModFlags, false);
386 object default_value = 0;
388 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
389 | FieldAttributes.Literal;
392 foreach (string name in ordered_enums) {
394 // Have we already been defined, thanks to some cross-referencing ?
396 if (member_to_value.Contains (name))
399 Location loc = (Mono.CSharp.Location) member_to_location [name];
401 if (this [name] != null) {
402 default_value = LookupEnumValue (ec, name, loc);
404 if (default_value == null)
408 FieldBuilder fb = TypeBuilder.DefineField (
409 name, UnderlyingType, attr);
411 if (default_value == null) {
412 Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
418 default_value = TypeManager.ChangeType (default_value, UnderlyingType);
420 Error_ConstantValueCannotBeConverted (default_value, loc);
424 fb.SetConstant (default_value);
425 field_builders.Add (fb);
426 member_to_value [name] = default_value;
428 if (!TypeManager.RegisterFieldValue (fb, default_value))
432 // Apply attributes on the enum member
434 Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name], loc);
437 default_value = GetNextDefaultValue (default_value);
440 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
448 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
449 MemberFilter filter, object criteria)
451 ArrayList members = new ArrayList ();
453 if ((mt & MemberTypes.Field) != 0) {
454 foreach (FieldBuilder fb in field_builders)
455 if (filter (fb, criteria) == true)
459 return new MemberList (members);
462 public override MemberCache MemberCache {
468 public ArrayList ValueNames {
470 return ordered_enums;
475 public Expression this [string name] {
477 return (Expression) defined_names [name];