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 void DefineEnum (object parent_builder)
78 TypeAttributes attr = TypeAttributes.Class | TypeAttributes.Sealed;
80 UnderlyingType = RootContext.TypeManager.LookupType (BaseType);
82 if (UnderlyingType != TypeManager.int32_type &&
83 UnderlyingType != TypeManager.uint32_type &&
84 UnderlyingType != TypeManager.int64_type &&
85 UnderlyingType != TypeManager.uint64_type &&
86 UnderlyingType != TypeManager.short_type &&
87 UnderlyingType != TypeManager.ushort_type &&
88 UnderlyingType != TypeManager.byte_type &&
89 UnderlyingType != TypeManager.sbyte_type) {
90 Report.Error (1008, Location,
91 "Type byte, sbyte, short, ushort, int, uint, " +
92 "long, or ulong expected");
96 if (parent_builder is ModuleBuilder) {
97 ModuleBuilder builder = (ModuleBuilder) parent_builder;
99 if ((ModFlags & Modifiers.PUBLIC) != 0)
100 attr |= TypeAttributes.Public;
102 attr |= TypeAttributes.NotPublic;
104 TypeBuilder = builder.DefineType (Name, attr, TypeManager.enum_type);
106 TypeBuilder builder = (System.Reflection.Emit.TypeBuilder) parent_builder;
108 if ((ModFlags & Modifiers.PUBLIC) != 0)
109 attr |= TypeAttributes.NestedPublic;
111 attr |= TypeAttributes.NestedPrivate;
114 TypeBuilder = builder.DefineNestedType (
115 Basename, attr, TypeManager.enum_type);
118 TypeBuilder.DefineField ("value__", UnderlyingType,
119 FieldAttributes.Public | FieldAttributes.SpecialName
120 | FieldAttributes.RTSpecialName);
122 RootContext.TypeManager.AddEnumType (Name, TypeBuilder, this);
127 bool IsValidEnumConstant (Expression e)
129 if (!(e is Constant))
132 if (e is IntConstant || e is UIntConstant || e is LongConstant ||
133 e is ByteConstant || e is SByteConstant || e is ShortConstant ||
134 e is UShortConstant || e is ULongConstant || e is EnumConstant)
140 object GetNextDefaultValue (object default_value)
142 if (UnderlyingType == TypeManager.int32_type) {
143 int i = (int) default_value;
145 if (i < System.Int32.MaxValue)
149 } else if (UnderlyingType == TypeManager.uint32_type) {
150 uint i = (uint) default_value;
152 if (i < System.UInt32.MaxValue)
156 } else if (UnderlyingType == TypeManager.int64_type) {
157 long i = (long) default_value;
159 if (i < System.Int64.MaxValue)
163 } else if (UnderlyingType == TypeManager.uint64_type) {
164 ulong i = (ulong) default_value;
166 if (i < System.UInt64.MaxValue)
170 } else if (UnderlyingType == TypeManager.short_type) {
171 short i = (short) default_value;
173 if (i < System.Int16.MaxValue)
177 } else if (UnderlyingType == TypeManager.ushort_type) {
178 ushort i = (ushort) default_value;
180 if (i < System.UInt16.MaxValue)
184 } else if (UnderlyingType == TypeManager.byte_type) {
185 byte i = (byte) default_value;
187 if (i < System.Byte.MaxValue)
191 } else if (UnderlyingType == TypeManager.sbyte_type) {
192 sbyte i = (sbyte) default_value;
194 if (i < System.SByte.MaxValue)
203 void Error_ConstantValueCannotBeConverted (object val, Location loc)
206 Report.Error (31, loc, "Constant value '" + ((Constant) val).AsString () +
207 "' cannot be converted" +
208 " to a " + TypeManager.CSharpName (UnderlyingType));
210 Report.Error (31, loc, "Constant value '" + val +
211 "' cannot be converted" +
212 " to a " + TypeManager.CSharpName (UnderlyingType));
217 /// This is used to lookup the value of an enum member. If the member is undefined,
218 /// it attempts to define it and return its value
220 public object LookupEnumValue (EmitContext ec, string name, Location loc)
222 object default_value = null;
225 default_value = member_to_value [name];
227 if (default_value != null)
228 return default_value;
230 if (!defined_names.Contains (name)) {
231 Report.Error (117, loc, "'"+ Name + "' does not contain a definition for '"
237 // So if the above doesn't happen, we have a member that is undefined
238 // We now proceed to define it
240 Expression val = this [name];
244 int idx = ordered_enums.IndexOf (name);
249 for (int i = 0; i < idx; ++i) {
250 string n = (string) ordered_enums [i];
251 Location m_loc = (Mono.CSharp.Location)
252 member_to_location [n];
253 default_value = LookupEnumValue (ec, n, m_loc);
256 default_value = GetNextDefaultValue (default_value);
260 val = val.Resolve (ec);
263 Report.Error (-12, loc, "Definition is circular.");
267 if (IsValidEnumConstant (val)) {
269 default_value = c.GetValue ();
271 if (default_value == null) {
272 Error_ConstantValueCannotBeConverted (c, loc);
279 "Type byte, sbyte, short, ushort, int, uint, long, or " +
285 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
286 | FieldAttributes.Literal;
288 FieldBuilder fb = TypeBuilder.DefineField (name, UnderlyingType, attr);
291 default_value = Convert.ChangeType (default_value, UnderlyingType);
293 Error_ConstantValueCannotBeConverted (c, loc);
297 fb.SetConstant (default_value);
298 field_builders.Add (fb);
299 member_to_value [name] = default_value;
301 if (!TypeManager.RegisterFieldValue (fb, default_value))
304 return default_value;
307 public override bool Define (TypeContainer parent)
310 // If there was an error during DefineEnum, return
312 if (TypeBuilder == null)
315 EmitContext ec = new EmitContext (parent, this, Location, null,
316 UnderlyingType, ModFlags, false);
318 object default_value = 0;
320 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
321 | FieldAttributes.Literal;
324 foreach (string name in ordered_enums) {
326 // Have we already been defined, thanks to some cross-referencing ?
328 if (member_to_value.Contains (name))
331 Location loc = (Mono.CSharp.Location) member_to_location [name];
333 if (this [name] != null) {
334 default_value = LookupEnumValue (ec, name, loc);
336 if (default_value == null)
340 FieldBuilder fb = TypeBuilder.DefineField (
341 name, UnderlyingType, attr);
343 if (default_value == null) {
344 Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
350 default_value = Convert.ChangeType (default_value, UnderlyingType);
352 Error_ConstantValueCannotBeConverted (default_value, loc);
356 fb.SetConstant (default_value);
357 field_builders.Add (fb);
358 member_to_value [name] = default_value;
360 if (!TypeManager.RegisterFieldValue (fb, default_value))
364 default_value = GetNextDefaultValue (default_value);
367 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
373 // Hack around System.Reflection as found everywhere else
375 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf,
376 MemberFilter filter, object criteria)
378 ArrayList members = new ArrayList ();
380 if ((mt & MemberTypes.Field) != 0) {
381 foreach (FieldBuilder fb in field_builders)
382 if (filter (fb, criteria) == true)
386 int count = members.Count;
389 MemberInfo [] mi = new MemberInfo [count];
390 members.CopyTo (mi, 0);
397 public ArrayList ValueNames {
399 return ordered_enums;
404 public Expression this [string name] {
406 return (Expression) defined_names [name];