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;
27 public Type UnderlyingType;
29 Hashtable member_to_location;
30 Hashtable member_to_attributes;
33 // This is for members that have been defined
35 Hashtable member_to_value;
38 // This is used to mark members we're currently defining
42 ArrayList field_builders;
44 public const int AllowedModifiers =
51 public Enum (NamespaceEntry ns, TypeContainer parent, Expression type,
52 int mod_flags, MemberName name, Attributes attrs, Location l)
53 : base (ns, parent, name, attrs, l)
56 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags,
57 IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE, l);
59 ordered_enums = new ArrayList ();
60 member_to_location = new Hashtable ();
61 member_to_value = new Hashtable ();
62 in_transit = new Hashtable ();
63 field_builders = new ArrayList ();
67 /// Adds @name to the enumeration space, with @expr
68 /// being its definition.
70 public AdditionResult AddEnumMember (string name, Expression expr, Location loc,
73 if (defined_names.Contains (name))
74 return AdditionResult.NameExists;
76 if (name == "value__") {
77 Report.Error (76, loc, "An item in an enumeration can't have an identifier `value__'");
78 return AdditionResult.Error;
81 DefineName (name, expr);
83 ordered_enums.Add (name);
84 member_to_location.Add (name, loc);
86 if (member_to_attributes == null)
87 member_to_attributes = new Hashtable ();
89 member_to_attributes.Add (name, opt_attrs);
91 return AdditionResult.Success;
94 public override void ApplyAttributeBuilder (object builder, Attribute a, CustomAttributeBuilder cb)
96 ((TypeBuilder) builder).SetCustomAttribute (cb);
100 // This is used by corlib compilation: we map from our
101 // type to a type that is consumable by the DefineField
103 Type MapToInternalType (Type t)
105 if (t == TypeManager.int32_type)
107 if (t == TypeManager.int64_type)
108 return typeof (long);
109 if (t == TypeManager.uint32_type)
110 return typeof (uint);
111 if (t == TypeManager.uint64_type)
112 return typeof (ulong);
113 if (t == TypeManager.float_type)
114 return typeof (float);
115 if (t == TypeManager.double_type)
116 return typeof (double);
117 if (t == TypeManager.byte_type)
118 return typeof (byte);
119 if (t == TypeManager.sbyte_type)
120 return typeof (sbyte);
121 if (t == TypeManager.char_type)
122 return typeof (char);
123 if (t == TypeManager.short_type)
124 return typeof (short);
125 if (t == TypeManager.ushort_type)
126 return typeof (ushort);
128 throw new Exception ();
131 public override TypeBuilder DefineType ()
133 if (TypeBuilder != null)
136 TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel);
138 attr |= TypeAttributes.Class | TypeAttributes.Sealed;
140 UnderlyingType = ResolveType (BaseType, false, Location);
142 if (UnderlyingType != TypeManager.int32_type &&
143 UnderlyingType != TypeManager.uint32_type &&
144 UnderlyingType != TypeManager.int64_type &&
145 UnderlyingType != TypeManager.uint64_type &&
146 UnderlyingType != TypeManager.short_type &&
147 UnderlyingType != TypeManager.ushort_type &&
148 UnderlyingType != TypeManager.byte_type &&
149 UnderlyingType != TypeManager.sbyte_type) {
150 Report.Error (1008, Location,
151 "Type byte, sbyte, short, ushort, int, uint, " +
152 "long, or ulong expected (got: " +
153 TypeManager.CSharpName (UnderlyingType) + ")");
158 if (TypeManager.NamespaceClash (Name, Location))
161 ModuleBuilder builder = CodeGen.Module.Builder;
163 TypeBuilder = builder.DefineType (Name, attr, TypeManager.enum_type);
165 TypeBuilder builder = Parent.TypeBuilder;
167 TypeBuilder = builder.DefineNestedType (
168 Basename, attr, TypeManager.enum_type);
172 // Call MapToInternalType for corlib
174 TypeBuilder.DefineField ("value__", UnderlyingType,
175 FieldAttributes.Public | FieldAttributes.SpecialName
176 | FieldAttributes.RTSpecialName);
178 TypeManager.AddEnumType (Name, TypeBuilder, this);
183 bool IsValidEnumConstant (Expression e)
185 if (!(e is Constant))
188 if (e is IntConstant || e is UIntConstant || e is LongConstant ||
189 e is ByteConstant || e is SByteConstant || e is ShortConstant ||
190 e is UShortConstant || e is ULongConstant || e is EnumConstant ||
197 object GetNextDefaultValue (object default_value)
199 if (UnderlyingType == TypeManager.int32_type) {
200 int i = (int) default_value;
202 if (i < System.Int32.MaxValue)
206 } else if (UnderlyingType == TypeManager.uint32_type) {
207 uint i = (uint) default_value;
209 if (i < System.UInt32.MaxValue)
213 } else if (UnderlyingType == TypeManager.int64_type) {
214 long i = (long) default_value;
216 if (i < System.Int64.MaxValue)
220 } else if (UnderlyingType == TypeManager.uint64_type) {
221 ulong i = (ulong) default_value;
223 if (i < System.UInt64.MaxValue)
227 } else if (UnderlyingType == TypeManager.short_type) {
228 short i = (short) default_value;
230 if (i < System.Int16.MaxValue)
234 } else if (UnderlyingType == TypeManager.ushort_type) {
235 ushort i = (ushort) default_value;
237 if (i < System.UInt16.MaxValue)
241 } else if (UnderlyingType == TypeManager.byte_type) {
242 byte i = (byte) default_value;
244 if (i < System.Byte.MaxValue)
248 } else if (UnderlyingType == TypeManager.sbyte_type) {
249 sbyte i = (sbyte) default_value;
251 if (i < System.SByte.MaxValue)
260 void Error_ConstantValueCannotBeConverted (object val, Location loc)
263 Report.Error (31, loc, "Constant value '" + ((Constant) val).AsString () +
264 "' cannot be converted" +
265 " to a " + TypeManager.CSharpName (UnderlyingType));
267 Report.Error (31, loc, "Constant value '" + val +
268 "' cannot be converted" +
269 " to a " + TypeManager.CSharpName (UnderlyingType));
274 /// Determines if a standard implicit conversion exists from
275 /// expr_type to target_type
277 public static bool ImplicitConversionExists (Type expr_type, Type target_type)
279 expr_type = TypeManager.TypeToCoreType (expr_type);
281 if (expr_type == TypeManager.void_type)
284 if (expr_type == target_type)
287 // First numeric conversions
289 if (expr_type == TypeManager.sbyte_type){
291 // From sbyte to short, int, long, float, double.
293 if ((target_type == TypeManager.int32_type) ||
294 (target_type == TypeManager.int64_type) ||
295 (target_type == TypeManager.double_type) ||
296 (target_type == TypeManager.float_type) ||
297 (target_type == TypeManager.short_type) ||
298 (target_type == TypeManager.decimal_type))
301 } else if (expr_type == TypeManager.byte_type){
303 // From byte to short, ushort, int, uint, long, ulong, float, double
305 if ((target_type == TypeManager.short_type) ||
306 (target_type == TypeManager.ushort_type) ||
307 (target_type == TypeManager.int32_type) ||
308 (target_type == TypeManager.uint32_type) ||
309 (target_type == TypeManager.uint64_type) ||
310 (target_type == TypeManager.int64_type) ||
311 (target_type == TypeManager.float_type) ||
312 (target_type == TypeManager.double_type) ||
313 (target_type == TypeManager.decimal_type))
316 } else if (expr_type == TypeManager.short_type){
318 // From short to int, long, float, double
320 if ((target_type == TypeManager.int32_type) ||
321 (target_type == TypeManager.int64_type) ||
322 (target_type == TypeManager.double_type) ||
323 (target_type == TypeManager.float_type) ||
324 (target_type == TypeManager.decimal_type))
327 } else if (expr_type == TypeManager.ushort_type){
329 // From ushort to int, uint, long, ulong, float, double
331 if ((target_type == TypeManager.uint32_type) ||
332 (target_type == TypeManager.uint64_type) ||
333 (target_type == TypeManager.int32_type) ||
334 (target_type == TypeManager.int64_type) ||
335 (target_type == TypeManager.double_type) ||
336 (target_type == TypeManager.float_type) ||
337 (target_type == TypeManager.decimal_type))
340 } else if (expr_type == TypeManager.int32_type){
342 // From int to long, float, double
344 if ((target_type == TypeManager.int64_type) ||
345 (target_type == TypeManager.double_type) ||
346 (target_type == TypeManager.float_type) ||
347 (target_type == TypeManager.decimal_type))
350 } else if (expr_type == TypeManager.uint32_type){
352 // From uint to long, ulong, float, double
354 if ((target_type == TypeManager.int64_type) ||
355 (target_type == TypeManager.uint64_type) ||
356 (target_type == TypeManager.double_type) ||
357 (target_type == TypeManager.float_type) ||
358 (target_type == TypeManager.decimal_type))
361 } else if ((expr_type == TypeManager.uint64_type) ||
362 (expr_type == TypeManager.int64_type)) {
364 // From long/ulong to float, double
366 if ((target_type == TypeManager.double_type) ||
367 (target_type == TypeManager.float_type) ||
368 (target_type == TypeManager.decimal_type))
371 } else if (expr_type == TypeManager.char_type){
373 // From char to ushort, int, uint, long, ulong, float, double
375 if ((target_type == TypeManager.ushort_type) ||
376 (target_type == TypeManager.int32_type) ||
377 (target_type == TypeManager.uint32_type) ||
378 (target_type == TypeManager.uint64_type) ||
379 (target_type == TypeManager.int64_type) ||
380 (target_type == TypeManager.float_type) ||
381 (target_type == TypeManager.double_type) ||
382 (target_type == TypeManager.decimal_type))
385 } else if (expr_type == TypeManager.float_type){
389 if (target_type == TypeManager.double_type)
397 // Horrible, horrible. But there is no other way we can pass the EmitContext
398 // to the recursive definition triggered by the evaluation of a forward
401 static EmitContext current_ec = null;
404 /// This is used to lookup the value of an enum member. If the member is undefined,
405 /// it attempts to define it and return its value
407 public object LookupEnumValue (EmitContext ec, string name, Location loc)
410 object default_value = null;
413 default_value = member_to_value [name];
415 if (default_value != null)
416 return default_value;
419 // This may happen if we're calling a method in System.Enum, for instance
422 if (!defined_names.Contains (name))
425 if (in_transit.Contains (name)) {
426 Report.Error (110, loc, "The evaluation of the constant value for `" +
427 Name + "." + name + "' involves a circular definition.");
432 // So if the above doesn't happen, we have a member that is undefined
433 // We now proceed to define it
435 Expression val = this [name];
439 int idx = ordered_enums.IndexOf (name);
444 for (int i = 0; i < idx; ++i) {
445 string n = (string) ordered_enums [i];
446 Location m_loc = (Mono.CSharp.Location)
447 member_to_location [n];
448 in_transit.Add (name, true);
450 EmitContext old_ec = current_ec;
453 default_value = LookupEnumValue (ec, n, m_loc);
457 in_transit.Remove (name);
458 if (default_value == null)
462 default_value = GetNextDefaultValue (default_value);
466 bool old = ec.InEnumContext;
467 ec.InEnumContext = true;
468 in_transit.Add (name, true);
470 EmitContext old_ec = current_ec;
472 val = val.Resolve (ec);
475 in_transit.Remove (name);
476 ec.InEnumContext = old;
481 if (!IsValidEnumConstant (val)) {
484 "Type byte, sbyte, short, ushort, int, uint, long, or " +
485 "ulong expected (have: " + val + ")");
490 default_value = c.GetValue ();
492 if (default_value == null) {
493 Error_ConstantValueCannotBeConverted (c, loc);
497 if (val is EnumConstant){
498 Type etype = TypeManager.EnumToUnderlying (c.Type);
500 if (!ImplicitConversionExists (etype, UnderlyingType)){
501 Convert.Error_CannotImplicitConversion (
502 loc, c.Type, UnderlyingType);
508 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
509 | FieldAttributes.Literal;
511 FieldBuilder fb = TypeBuilder.DefineField (name, TypeBuilder, attr);
514 default_value = TypeManager.ChangeType (default_value, UnderlyingType, out fail);
516 Error_ConstantValueCannotBeConverted (c, loc);
520 fb.SetConstant (default_value);
521 field_builders.Add (fb);
522 member_to_value [name] = default_value;
524 if (!TypeManager.RegisterFieldValue (fb, default_value))
528 // Now apply attributes
530 Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name]);
532 return default_value;
535 public override bool DefineMembers (TypeContainer parent)
540 public override bool Define (TypeContainer parent)
543 // If there was an error during DefineEnum, return
545 if (TypeBuilder == null)
548 EmitContext ec = new EmitContext (this, this, Location, null,
549 UnderlyingType, ModFlags, false);
552 object default_value = 0;
554 FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
555 | FieldAttributes.Literal;
558 foreach (string name in ordered_enums) {
560 // Have we already been defined, thanks to some cross-referencing ?
562 if (member_to_value.Contains (name))
565 Location loc = (Mono.CSharp.Location) member_to_location [name];
567 if (this [name] != null) {
568 default_value = LookupEnumValue (ec, name, loc);
570 if (default_value == null)
573 if (name == "value__"){
574 Report.Error (76, loc, "The name `value__' is reserved for enumerations");
578 FieldBuilder fb = TypeBuilder.DefineField (
579 name, TypeBuilder, attr);
581 if (default_value == null) {
582 Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
588 default_value = TypeManager.ChangeType (default_value, UnderlyingType, out fail);
590 Error_ConstantValueCannotBeConverted (default_value, loc);
594 fb.SetConstant (default_value);
595 field_builders.Add (fb);
596 member_to_value [name] = default_value;
598 if (!TypeManager.RegisterFieldValue (fb, default_value))
602 // Apply attributes on the enum member
604 Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name]);
607 default_value = GetNextDefaultValue (default_value);
612 public override void Emit (TypeContainer tc)
614 if (OptAttributes != null) {
615 EmitContext ec = new EmitContext (tc, this, Location, null, null, ModFlags, false);
616 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
622 protected override bool IsIdentifierClsCompliant (DeclSpace ds)
624 if (!base.IsIdentifierClsCompliant (ds))
627 for (int i = 1; i < ordered_enums.Count; ++i) {
628 string checked_name = ordered_enums [i] as string;
629 for (int ii = 0; ii < ordered_enums.Count; ++ii) {
633 string enumerator_name = ordered_enums [ii] as string;
634 if (String.Compare (checked_name, enumerator_name, true) == 0) {
635 Report.SymbolRelatedToPreviousError ((Location)member_to_location [enumerator_name], enumerator_name);
636 Report.Error_T (3005, (Location)member_to_location [checked_name], GetEnumeratorName (checked_name));
644 protected override bool VerifyClsCompliance (DeclSpace ds)
646 if (!base.VerifyClsCompliance (ds))
649 if (!AttributeTester.IsClsCompliant (UnderlyingType)) {
650 Report.Error_T (3009, Location, GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
657 /// Returns full enum name.
659 string GetEnumeratorName (string valueName)
661 return String.Concat (Name, ".", valueName);
667 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
668 MemberFilter filter, object criteria)
670 ArrayList members = new ArrayList ();
672 if ((mt & MemberTypes.Field) != 0) {
673 if (criteria is string){
674 if (member_to_value [criteria] == null && current_ec != null){
675 LookupEnumValue (current_ec, (string) criteria, Location.Null);
679 foreach (FieldBuilder fb in field_builders)
680 if (filter (fb, criteria) == true)
684 return new MemberList (members);
687 public override MemberCache MemberCache {
693 public ArrayList ValueNames {
695 return ordered_enums;
700 public Expression this [string name] {
702 return (Expression) defined_names [name];