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;
38 ArrayList field_builders;
40 public const int AllowedModifiers =
47 public Enum (TypeContainer parent, Expression type, int mod_flags, string name, Attributes attrs, Location l)
48 : base (parent, name, l)
51 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PUBLIC, l);
52 OptAttributes = attrs;
54 ordered_enums = new ArrayList ();
55 member_to_location = new Hashtable ();
56 member_to_value = new Hashtable ();
57 field_builders = new ArrayList ();
61 /// Adds @name to the enumeration space, with @expr
62 /// being its definition.
64 public AdditionResult AddEnumMember (string name, Expression expr, Location loc,
67 if (defined_names.Contains (name))
68 return AdditionResult.NameExists;
70 DefineName (name, expr);
72 ordered_enums.Add (name);
73 member_to_location.Add (name, loc);
75 if (member_to_attributes == null)
76 member_to_attributes = new Hashtable ();
78 member_to_attributes.Add (name, opt_attrs);
80 return AdditionResult.Success;
84 // This is used by corlib compilation: we map from our
85 // type to a type that is consumable by the DefineField
87 Type MapToInternalType (Type t)
89 if (t == TypeManager.int32_type)
91 if (t == TypeManager.int64_type)
93 if (t == TypeManager.uint32_type)
95 if (t == TypeManager.uint64_type)
96 return typeof (ulong);
97 if (t == TypeManager.float_type)
98 return typeof (float);
99 if (t == TypeManager.double_type)
100 return typeof (double);
101 if (t == TypeManager.byte_type)
102 return typeof (byte);
103 if (t == TypeManager.sbyte_type)
104 return typeof (sbyte);
105 if (t == TypeManager.char_type)
106 return typeof (char);
107 if (t == TypeManager.short_type)
108 return typeof (short);
109 if (t == TypeManager.ushort_type)
110 return typeof (ushort);
112 throw new Exception ();
115 public override TypeBuilder DefineType ()
117 if (TypeBuilder != null)
120 TypeAttributes attr = TypeAttributes.Class | TypeAttributes.Sealed;
122 UnderlyingType = ResolveType (BaseType, false, Location);
124 if (UnderlyingType != TypeManager.int32_type &&
125 UnderlyingType != TypeManager.uint32_type &&
126 UnderlyingType != TypeManager.int64_type &&
127 UnderlyingType != TypeManager.uint64_type &&
128 UnderlyingType != TypeManager.short_type &&
129 UnderlyingType != TypeManager.ushort_type &&
130 UnderlyingType != TypeManager.byte_type &&
131 UnderlyingType != TypeManager.sbyte_type) {
132 Report.Error (1008, Location,
133 "Type byte, sbyte, short, ushort, int, uint, " +
134 "long, or ulong expected (got: " +
135 TypeManager.CSharpName (UnderlyingType) + ")");
140 ModuleBuilder builder = CodeGen.ModuleBuilder;
142 if ((ModFlags & Modifiers.PUBLIC) != 0)
143 attr |= TypeAttributes.Public;
145 attr |= TypeAttributes.NotPublic;
147 TypeBuilder = builder.DefineType (Name, attr, TypeManager.enum_type);
149 TypeBuilder builder = Parent.TypeBuilder;
151 if ((ModFlags & Modifiers.PUBLIC) != 0)
152 attr |= TypeAttributes.NestedPublic;
154 attr |= TypeAttributes.NestedPrivate;
157 TypeBuilder = builder.DefineNestedType (
158 Basename, attr, TypeManager.enum_type);
162 // Call MapToInternalType for corlib
164 TypeBuilder.DefineField ("value__", UnderlyingType,
165 FieldAttributes.Public | FieldAttributes.SpecialName
166 | FieldAttributes.RTSpecialName);
168 TypeManager.AddEnumType (Name, TypeBuilder, this);
173 bool IsValidEnumConstant (Expression e)
175 if (!(e is Constant))
178 if (e is IntConstant || e is UIntConstant || e is LongConstant ||
179 e is ByteConstant || e is SByteConstant || e is ShortConstant ||
180 e is UShortConstant || e is ULongConstant || e is EnumConstant)
186 object GetNextDefaultValue (object default_value)
188 if (UnderlyingType == TypeManager.int32_type) {
189 int i = (int) default_value;
191 if (i < System.Int32.MaxValue)
195 } else if (UnderlyingType == TypeManager.uint32_type) {
196 uint i = (uint) default_value;
198 if (i < System.UInt32.MaxValue)
202 } else if (UnderlyingType == TypeManager.int64_type) {
203 long i = (long) default_value;
205 if (i < System.Int64.MaxValue)
209 } else if (UnderlyingType == TypeManager.uint64_type) {
210 ulong i = (ulong) default_value;
212 if (i < System.UInt64.MaxValue)
216 } else if (UnderlyingType == TypeManager.short_type) {
217 short i = (short) default_value;
219 if (i < System.Int16.MaxValue)
223 } else if (UnderlyingType == TypeManager.ushort_type) {
224 ushort i = (ushort) default_value;
226 if (i < System.UInt16.MaxValue)
230 } else if (UnderlyingType == TypeManager.byte_type) {
231 byte i = (byte) default_value;
233 if (i < System.Byte.MaxValue)
237 } else if (UnderlyingType == TypeManager.sbyte_type) {
238 sbyte i = (sbyte) default_value;
240 if (i < System.SByte.MaxValue)
249 void Error_ConstantValueCannotBeConverted (object val, Location loc)
252 Report.Error (31, loc, "Constant value '" + ((Constant) val).AsString () +
253 "' cannot be converted" +
254 " to a " + TypeManager.CSharpName (UnderlyingType));
256 Report.Error (31, loc, "Constant value '" + val +
257 "' cannot be converted" +
258 " to a " + TypeManager.CSharpName (UnderlyingType));
263 /// This is used to lookup the value of an enum member. If the member is undefined,
264 /// it attempts to define it and return its value
266 public object LookupEnumValue (EmitContext ec, string name, Location loc)
268 object default_value = null;
271 default_value = member_to_value [name];
273 if (default_value != null)
274 return default_value;
277 // This may happen if we're calling a method in System.Enum, for instance
280 if (!defined_names.Contains (name))
284 // So if the above doesn't happen, we have a member that is undefined
285 // We now proceed to define it
287 Expression val = this [name];
291 int idx = ordered_enums.IndexOf (name);
296 for (int i = 0; i < idx; ++i) {
297 string n = (string) ordered_enums [i];
298 Location m_loc = (Mono.CSharp.Location)
299 member_to_location [n];
300 default_value = LookupEnumValue (ec, n, m_loc);
303 default_value = GetNextDefaultValue (default_value);
307 bool old = ec.InEnumContext;
308 ec.InEnumContext = true;
309 val = val.Resolve (ec);
310 ec.InEnumContext = old;
313 Report.Error (-12, loc, "Definition is circular.");
317 if (IsValidEnumConstant (val)) {
319 default_value = c.GetValue ();
321 if (default_value == null) {
322 Error_ConstantValueCannotBeConverted (c, loc);
329 "Type byte, sbyte, short, ushort, int, uint, long, or " +
330 "ulong expected (have: " + val + ")");
335 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
336 | FieldAttributes.Literal;
338 FieldBuilder fb = TypeBuilder.DefineField (name, UnderlyingType, attr);
341 default_value = TypeManager.ChangeType (default_value, UnderlyingType);
343 Error_ConstantValueCannotBeConverted (c, loc);
347 fb.SetConstant (default_value);
348 field_builders.Add (fb);
349 member_to_value [name] = default_value;
351 if (!TypeManager.RegisterFieldValue (fb, default_value))
355 // Now apply attributes
357 Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name], loc);
359 return default_value;
362 public override bool Define (TypeContainer parent)
365 // If there was an error during DefineEnum, return
367 if (TypeBuilder == null)
370 EmitContext ec = new EmitContext (parent, this, Location, null,
371 UnderlyingType, ModFlags, false);
373 object default_value = 0;
375 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
376 | FieldAttributes.Literal;
379 foreach (string name in ordered_enums) {
381 // Have we already been defined, thanks to some cross-referencing ?
383 if (member_to_value.Contains (name))
386 Location loc = (Mono.CSharp.Location) member_to_location [name];
388 if (this [name] != null) {
389 default_value = LookupEnumValue (ec, name, loc);
391 if (default_value == null)
395 FieldBuilder fb = TypeBuilder.DefineField (
396 name, UnderlyingType, attr);
398 if (default_value == null) {
399 Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
405 default_value = TypeManager.ChangeType (default_value, UnderlyingType);
407 Error_ConstantValueCannotBeConverted (default_value, loc);
411 fb.SetConstant (default_value);
412 field_builders.Add (fb);
413 member_to_value [name] = default_value;
415 if (!TypeManager.RegisterFieldValue (fb, default_value))
419 // Apply attributes on the enum member
421 Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name], loc);
424 default_value = GetNextDefaultValue (default_value);
427 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
433 // Hack around System.Reflection as found everywhere else
435 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf,
436 MemberFilter filter, object criteria)
438 ArrayList members = new ArrayList ();
440 if ((mt & MemberTypes.Field) != 0) {
441 foreach (FieldBuilder fb in field_builders)
442 if (filter (fb, criteria) == true)
446 int count = members.Count;
449 MemberInfo [] mi = new MemberInfo [count];
450 members.CopyTo (mi, 0);
457 public ArrayList ValueNames {
459 return ordered_enums;
464 public Expression this [string name] {
466 return (Expression) defined_names [name];