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.char_type &&
141 UnderlyingType != TypeManager.sbyte_type) {
142 Report.Error (1008, Location,
143 "Type byte, sbyte, short, char, ushort, int, uint, " +
144 "long, or ulong expected (got: " +
145 TypeManager.CSharpName (UnderlyingType) + ")");
150 if (TypeManager.NamespaceClash (Name))
153 ModuleBuilder builder = CodeGen.ModuleBuilder;
155 TypeBuilder = builder.DefineType (Name, attr, TypeManager.enum_type);
157 TypeBuilder builder = Parent.TypeBuilder;
159 TypeBuilder = builder.DefineNestedType (
160 Basename, attr, TypeManager.enum_type);
164 // Call MapToInternalType for corlib
166 TypeBuilder.DefineField ("value__", UnderlyingType,
167 FieldAttributes.Public | FieldAttributes.SpecialName
168 | FieldAttributes.RTSpecialName);
170 TypeManager.AddEnumType (Name, TypeBuilder, this);
175 bool IsValidEnumConstant (Expression e)
177 if (!(e is Constant))
180 if (e is IntConstant || e is UIntConstant || e is LongConstant ||
181 e is ByteConstant || e is SByteConstant || e is ShortConstant ||
182 e is UShortConstant || e is ULongConstant || e is EnumConstant ||
189 object GetNextDefaultValue (object default_value)
191 if (UnderlyingType == TypeManager.int32_type) {
192 int i = (int) default_value;
194 if (i < System.Int32.MaxValue)
198 } else if (UnderlyingType == TypeManager.uint32_type) {
199 uint i = (uint) default_value;
201 if (i < System.UInt32.MaxValue)
205 } else if (UnderlyingType == TypeManager.int64_type) {
206 long i = (long) default_value;
208 if (i < System.Int64.MaxValue)
212 } else if (UnderlyingType == TypeManager.uint64_type) {
213 ulong i = (ulong) default_value;
215 if (i < System.UInt64.MaxValue)
219 } else if (UnderlyingType == TypeManager.short_type) {
220 short i = (short) default_value;
222 if (i < System.Int16.MaxValue)
226 } else if (UnderlyingType == TypeManager.ushort_type) {
227 ushort i = (ushort) default_value;
229 if (i < System.UInt16.MaxValue)
233 } else if (UnderlyingType == TypeManager.byte_type) {
234 byte i = (byte) default_value;
236 if (i < System.Byte.MaxValue)
240 } else if (UnderlyingType == TypeManager.sbyte_type) {
241 sbyte i = (sbyte) default_value;
243 if (i < System.SByte.MaxValue)
252 void Error_ConstantValueCannotBeConverted (object val, Location loc)
255 Report.Error (31, loc, "Constant value '" + ((Constant) val).AsString () +
256 "' cannot be converted" +
257 " to a " + TypeManager.CSharpName (UnderlyingType));
259 Report.Error (31, loc, "Constant value '" + val +
260 "' cannot be converted" +
261 " to a " + TypeManager.CSharpName (UnderlyingType));
266 /// Determines if a standard implicit conversion exists from
267 /// expr_type to target_type
269 public static bool ImplicitConversionExists (Type expr_type, Type target_type)
271 expr_type = TypeManager.TypeToCoreType (expr_type);
273 if (expr_type == TypeManager.void_type)
276 if (expr_type == target_type)
279 // First numeric conversions
281 if (expr_type == TypeManager.sbyte_type){
283 // From sbyte to short, int, long, float, double.
285 if ((target_type == TypeManager.int32_type) ||
286 (target_type == TypeManager.int64_type) ||
287 (target_type == TypeManager.double_type) ||
288 (target_type == TypeManager.float_type) ||
289 (target_type == TypeManager.short_type) ||
290 (target_type == TypeManager.decimal_type))
293 } else if (expr_type == TypeManager.byte_type){
295 // From byte to short, ushort, int, uint, long, ulong, float, double
297 if ((target_type == TypeManager.short_type) ||
298 (target_type == TypeManager.ushort_type) ||
299 (target_type == TypeManager.int32_type) ||
300 (target_type == TypeManager.uint32_type) ||
301 (target_type == TypeManager.uint64_type) ||
302 (target_type == TypeManager.int64_type) ||
303 (target_type == TypeManager.float_type) ||
304 (target_type == TypeManager.double_type) ||
305 (target_type == TypeManager.decimal_type))
308 } else if (expr_type == TypeManager.short_type){
310 // From short to int, long, float, double
312 if ((target_type == TypeManager.int32_type) ||
313 (target_type == TypeManager.int64_type) ||
314 (target_type == TypeManager.double_type) ||
315 (target_type == TypeManager.float_type) ||
316 (target_type == TypeManager.decimal_type))
319 } else if (expr_type == TypeManager.ushort_type){
321 // From ushort to int, uint, long, ulong, float, double
323 if ((target_type == TypeManager.uint32_type) ||
324 (target_type == TypeManager.uint64_type) ||
325 (target_type == TypeManager.int32_type) ||
326 (target_type == TypeManager.int64_type) ||
327 (target_type == TypeManager.double_type) ||
328 (target_type == TypeManager.float_type) ||
329 (target_type == TypeManager.decimal_type))
332 } else if (expr_type == TypeManager.int32_type){
334 // From int to long, float, double
336 if ((target_type == TypeManager.int64_type) ||
337 (target_type == TypeManager.double_type) ||
338 (target_type == TypeManager.float_type) ||
339 (target_type == TypeManager.decimal_type))
342 } else if (expr_type == TypeManager.uint32_type){
344 // From uint to long, ulong, float, double
346 if ((target_type == TypeManager.int64_type) ||
347 (target_type == TypeManager.uint64_type) ||
348 (target_type == TypeManager.double_type) ||
349 (target_type == TypeManager.float_type) ||
350 (target_type == TypeManager.decimal_type))
353 } else if ((expr_type == TypeManager.uint64_type) ||
354 (expr_type == TypeManager.int64_type)) {
356 // From long/ulong to float, double
358 if ((target_type == TypeManager.double_type) ||
359 (target_type == TypeManager.float_type) ||
360 (target_type == TypeManager.decimal_type))
363 } else if (expr_type == TypeManager.char_type){
365 // From char to ushort, int, uint, long, ulong, float, double
367 if ((target_type == TypeManager.ushort_type) ||
368 (target_type == TypeManager.int32_type) ||
369 (target_type == TypeManager.uint32_type) ||
370 (target_type == TypeManager.uint64_type) ||
371 (target_type == TypeManager.int64_type) ||
372 (target_type == TypeManager.float_type) ||
373 (target_type == TypeManager.double_type) ||
374 (target_type == TypeManager.decimal_type))
377 } else if (expr_type == TypeManager.float_type){
381 if (target_type == TypeManager.double_type)
389 // Horrible, horrible. But there is no other way we can pass the EmitContext
390 // to the recursive definition triggered by the evaluation of a forward
393 static EmitContext current_ec = null;
396 /// This is used to lookup the value of an enum member. If the member is undefined,
397 /// it attempts to define it and return its value
399 public object LookupEnumValue (EmitContext ec, string name, Location loc)
402 object default_value = null;
405 default_value = member_to_value [name];
407 if (default_value != null)
408 return default_value;
411 // This may happen if we're calling a method in System.Enum, for instance
414 if (!defined_names.Contains (name))
417 if (in_transit.Contains (name)) {
418 Report.Error (110, loc, "The evaluation of the constant value for `" +
419 Name + "." + name + "' involves a circular definition.");
424 // So if the above doesn't happen, we have a member that is undefined
425 // We now proceed to define it
427 Expression val = this [name];
431 int idx = ordered_enums.IndexOf (name);
436 for (int i = 0; i < idx; ++i) {
437 string n = (string) ordered_enums [i];
438 Location m_loc = (Mono.CSharp.Location)
439 member_to_location [n];
440 in_transit.Add (name, true);
442 EmitContext old_ec = current_ec;
445 default_value = LookupEnumValue (ec, n, m_loc);
449 in_transit.Remove (name);
450 if (default_value == null)
454 default_value = GetNextDefaultValue (default_value);
458 bool old = ec.InEnumContext;
459 ec.InEnumContext = true;
460 in_transit.Add (name, true);
462 EmitContext old_ec = current_ec;
464 val = val.Resolve (ec);
467 in_transit.Remove (name);
468 ec.InEnumContext = old;
473 if (!IsValidEnumConstant (val)) {
476 "Type byte, sbyte, short, ushort, int, uint, long, or " +
477 "ulong expected (have: " + val + ")");
482 default_value = c.GetValue ();
484 if (default_value == null) {
485 Error_ConstantValueCannotBeConverted (c, loc);
489 if (val is EnumConstant){
490 Type etype = TypeManager.EnumToUnderlying (c.Type);
492 if (!ImplicitConversionExists (etype, UnderlyingType)){
493 Expression.Error_CannotConvertImplicit (
494 loc, c.Type, UnderlyingType);
500 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
501 | FieldAttributes.Literal;
503 FieldBuilder fb = TypeBuilder.DefineField (name, TypeBuilder, attr);
506 default_value = TypeManager.ChangeType (default_value, UnderlyingType, out fail);
508 Error_ConstantValueCannotBeConverted (c, loc);
512 fb.SetConstant (default_value);
513 field_builders.Add (fb);
514 member_to_value [name] = default_value;
516 if (!TypeManager.RegisterFieldValue (fb, default_value))
520 // Now apply attributes
522 Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name]);
524 return default_value;
527 public override bool DefineMembers (TypeContainer parent)
532 public override bool Define (TypeContainer parent)
535 // If there was an error during DefineEnum, return
537 if (TypeBuilder == null)
540 EmitContext ec = new EmitContext (this, this, Location, null,
541 UnderlyingType, ModFlags, false);
544 object default_value = 0;
546 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
547 | FieldAttributes.Literal;
550 foreach (string name in ordered_enums) {
552 // Have we already been defined, thanks to some cross-referencing ?
554 if (member_to_value.Contains (name))
557 Location loc = (Mono.CSharp.Location) member_to_location [name];
559 if (this [name] != null) {
560 default_value = LookupEnumValue (ec, name, loc);
562 if (default_value == null)
565 if (name == "value__"){
566 Report.Error (76, loc, "The name `value__' is reserved for enumerations");
570 FieldBuilder fb = TypeBuilder.DefineField (
571 name, TypeBuilder, attr);
573 if (default_value == null) {
574 Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
580 default_value = TypeManager.ChangeType (default_value, UnderlyingType, out fail);
582 Error_ConstantValueCannotBeConverted (default_value, loc);
586 fb.SetConstant (default_value);
587 field_builders.Add (fb);
588 member_to_value [name] = default_value;
590 if (!TypeManager.RegisterFieldValue (fb, default_value))
594 // Apply attributes on the enum member
596 Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name]);
599 default_value = GetNextDefaultValue (default_value);
602 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
610 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
611 MemberFilter filter, object criteria)
613 ArrayList members = new ArrayList ();
615 if ((mt & MemberTypes.Field) != 0) {
616 if (criteria is string){
617 if (member_to_value [criteria] == null && current_ec != null){
618 LookupEnumValue (current_ec, (string) criteria, Location.Null);
622 foreach (FieldBuilder fb in field_builders)
623 if (filter (fb, criteria) == true)
627 return new MemberList (members);
630 public override MemberCache MemberCache {
636 public ArrayList ValueNames {
638 return ordered_enums;
643 public Expression this [string name] {
645 return (Expression) defined_names [name];