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;
270 // This may happen if we're calling a method in System.Enum, for instance
273 if (!defined_names.Contains (name))
277 // So if the above doesn't happen, we have a member that is undefined
278 // We now proceed to define it
280 Expression val = this [name];
284 int idx = ordered_enums.IndexOf (name);
289 for (int i = 0; i < idx; ++i) {
290 string n = (string) ordered_enums [i];
291 Location m_loc = (Mono.CSharp.Location)
292 member_to_location [n];
293 default_value = LookupEnumValue (ec, n, m_loc);
296 default_value = GetNextDefaultValue (default_value);
300 bool old = ec.InEnumContext;
301 ec.InEnumContext = true;
302 val = val.Resolve (ec);
303 ec.InEnumContext = old;
306 Report.Error (-12, loc, "Definition is circular.");
310 if (IsValidEnumConstant (val)) {
312 default_value = c.GetValue ();
314 if (default_value == null) {
315 Error_ConstantValueCannotBeConverted (c, loc);
322 "Type byte, sbyte, short, ushort, int, uint, long, or " +
323 "ulong expected (have: " + val + ")");
328 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
329 | FieldAttributes.Literal;
331 FieldBuilder fb = TypeBuilder.DefineField (name, UnderlyingType, attr);
334 default_value = TypeManager.ChangeType (default_value, UnderlyingType);
336 Error_ConstantValueCannotBeConverted (c, loc);
340 fb.SetConstant (default_value);
341 field_builders.Add (fb);
342 member_to_value [name] = default_value;
344 if (!TypeManager.RegisterFieldValue (fb, default_value))
347 return default_value;
350 public override bool Define (TypeContainer parent)
353 // If there was an error during DefineEnum, return
355 if (TypeBuilder == null)
358 EmitContext ec = new EmitContext (parent, this, Location, null,
359 UnderlyingType, ModFlags, false);
361 object default_value = 0;
363 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
364 | FieldAttributes.Literal;
367 foreach (string name in ordered_enums) {
369 // Have we already been defined, thanks to some cross-referencing ?
371 if (member_to_value.Contains (name))
374 Location loc = (Mono.CSharp.Location) member_to_location [name];
376 if (this [name] != null) {
377 default_value = LookupEnumValue (ec, name, loc);
379 if (default_value == null)
383 FieldBuilder fb = TypeBuilder.DefineField (
384 name, UnderlyingType, attr);
386 if (default_value == null) {
387 Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
393 default_value = TypeManager.ChangeType (default_value, UnderlyingType);
395 Error_ConstantValueCannotBeConverted (default_value, loc);
399 fb.SetConstant (default_value);
400 field_builders.Add (fb);
401 member_to_value [name] = default_value;
403 if (!TypeManager.RegisterFieldValue (fb, default_value))
407 default_value = GetNextDefaultValue (default_value);
410 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
416 // Hack around System.Reflection as found everywhere else
418 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf,
419 MemberFilter filter, object criteria)
421 ArrayList members = new ArrayList ();
423 if ((mt & MemberTypes.Field) != 0) {
424 foreach (FieldBuilder fb in field_builders)
425 if (filter (fb, criteria) == true)
429 int count = members.Count;
432 MemberInfo [] mi = new MemberInfo [count];
433 members.CopyTo (mi, 0);
440 public ArrayList ValueNames {
442 return ordered_enums;
447 public Expression this [string name] {
449 return (Expression) defined_names [name];