691fcbe6b1291424181587d730f6e8bcda8301a1
[mono.git] / mcs / mcs / enum.cs
1 //
2 // enum.cs: Enum handling.
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //         Ravi Pratap     (ravi@ximian.com)
6 //
7 // Licensed under the terms of the GNU GPL
8 //
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
10 //
11
12 using System;
13 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
16
17 namespace Mono.CSharp {
18
19         /// <summary>
20         ///   Enumeration container
21         /// </summary>
22         public class Enum : DeclSpace {
23                 ArrayList ordered_enums;
24                 
25                 public Expression BaseType;
26                 public Attributes  OptAttributes;
27                 
28                 public Type UnderlyingType;
29
30                 Hashtable member_to_location;
31                 Hashtable member_to_attributes;
32
33                 //
34                 // This is for members that have been defined
35                 //
36                 Hashtable member_to_value;
37
38                 //
39                 // This is used to mark members we're currently defining
40                 //
41                 Hashtable in_transit;
42                 
43                 ArrayList field_builders;
44                 
45                 public const int AllowedModifiers =
46                         Modifiers.NEW |
47                         Modifiers.PUBLIC |
48                         Modifiers.PROTECTED |
49                         Modifiers.INTERNAL |
50                         Modifiers.PRIVATE;
51
52                 public Enum (TypeContainer parent, Expression type, int mod_flags, string name, Attributes attrs, Location l)
53                         : base (parent, name, l)
54                 {
55                         this.BaseType = type;
56                         ModFlags = Modifiers.Check (AllowedModifiers, mod_flags,
57                                                     IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE, l);
58                         OptAttributes = attrs;
59
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 ();
65                 }
66
67                 /// <summary>
68                 ///   Adds @name to the enumeration space, with @expr
69                 ///   being its definition.  
70                 /// </summary>
71                 public AdditionResult AddEnumMember (string name, Expression expr, Location loc,
72                                                      Attributes opt_attrs)
73                 {
74                         if (defined_names.Contains (name))
75                                 return AdditionResult.NameExists;
76
77                         DefineName (name, expr);
78
79                         ordered_enums.Add (name);
80                         member_to_location.Add (name, loc);
81
82                         if (member_to_attributes == null)
83                                 member_to_attributes = new Hashtable ();
84
85                         member_to_attributes.Add (name, opt_attrs);
86                         
87                         return AdditionResult.Success;
88                 }
89
90                 //
91                 // This is used by corlib compilation: we map from our
92                 // type to a type that is consumable by the DefineField
93                 //
94                 Type MapToInternalType (Type t)
95                 {
96                         if (t == TypeManager.int32_type)
97                                 return typeof (int);
98                         if (t == TypeManager.int64_type)
99                                 return typeof (long);
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);
118
119                         throw new Exception ();
120                 }
121                 
122                 public override TypeBuilder DefineType ()
123                 {
124                         if (TypeBuilder != null)
125                                 return TypeBuilder;
126
127                         TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel);
128
129                         attr |= TypeAttributes.Class | TypeAttributes.Sealed;
130
131                         UnderlyingType = ResolveType (BaseType, false, Location);
132
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.sbyte_type) {
141                                 Report.Error (1008, Location,
142                                               "Type byte, sbyte, short, ushort, int, uint, " +
143                                               "long, or ulong expected (got: " +
144                                               TypeManager.CSharpName (UnderlyingType) + ")");
145                                 return null;
146                         }
147
148                         if (IsTopLevel) {
149                                 ModuleBuilder builder = CodeGen.ModuleBuilder;
150
151                                 TypeBuilder = builder.DefineType (Name, attr, TypeManager.enum_type);
152                         } else {
153                                 TypeBuilder builder = Parent.TypeBuilder;
154
155                                 TypeBuilder = builder.DefineNestedType (
156                                         Basename, attr, TypeManager.enum_type);
157                         }
158
159                         //
160                         // Call MapToInternalType for corlib
161                         //
162                         TypeBuilder.DefineField ("value__", UnderlyingType,
163                                                  FieldAttributes.Public | FieldAttributes.SpecialName
164                                                  | FieldAttributes.RTSpecialName);
165
166                         TypeManager.AddEnumType (Name, TypeBuilder, this);
167
168                         return TypeBuilder;
169                 }
170
171                 bool IsValidEnumConstant (Expression e)
172                 {
173                         if (!(e is Constant))
174                                 return false;
175
176                         if (e is IntConstant || e is UIntConstant || e is LongConstant ||
177                             e is ByteConstant || e is SByteConstant || e is ShortConstant ||
178                             e is UShortConstant || e is ULongConstant || e is EnumConstant)
179                                 return true;
180                         else
181                                 return false;
182                 }
183
184                 object GetNextDefaultValue (object default_value)
185                 {
186                         if (UnderlyingType == TypeManager.int32_type) {
187                                 int i = (int) default_value;
188                                 
189                                 if (i < System.Int32.MaxValue)
190                                         return ++i;
191                                 else
192                                         return null;
193                         } else if (UnderlyingType == TypeManager.uint32_type) {
194                                 uint i = (uint) default_value;
195
196                                 if (i < System.UInt32.MaxValue)
197                                         return ++i;
198                                 else
199                                         return null;
200                         } else if (UnderlyingType == TypeManager.int64_type) {
201                                 long i = (long) default_value;
202
203                                 if (i < System.Int64.MaxValue)
204                                         return ++i;
205                                 else
206                                         return null;
207                         } else if (UnderlyingType == TypeManager.uint64_type) {
208                                 ulong i = (ulong) default_value;
209
210                                 if (i < System.UInt64.MaxValue)
211                                         return ++i;
212                                 else
213                                         return null;
214                         } else if (UnderlyingType == TypeManager.short_type) {
215                                 short i = (short) default_value;
216
217                                 if (i < System.Int16.MaxValue)
218                                         return ++i;
219                                 else
220                                         return null;
221                         } else if (UnderlyingType == TypeManager.ushort_type) {
222                                 ushort i = (ushort) default_value;
223
224                                 if (i < System.UInt16.MaxValue)
225                                         return ++i;
226                                 else
227                                         return null;
228                         } else if (UnderlyingType == TypeManager.byte_type) {
229                                 byte i = (byte) default_value;
230
231                                 if (i < System.Byte.MaxValue)
232                                         return ++i;
233                                 else
234                                         return null;
235                         } else if (UnderlyingType == TypeManager.sbyte_type) {
236                                 sbyte i = (sbyte) default_value;
237
238                                 if (i < System.SByte.MaxValue)
239                                         return ++i;
240                                 else
241                                         return null;
242                         }
243
244                         return null;
245                 }
246
247                 void Error_ConstantValueCannotBeConverted (object val, Location loc)
248                 {
249                         if (val is Constant)
250                                 Report.Error (31, loc, "Constant value '" + ((Constant) val).AsString () +
251                                               "' cannot be converted" +
252                                               " to a " + TypeManager.CSharpName (UnderlyingType));
253                         else 
254                                 Report.Error (31, loc, "Constant value '" + val +
255                                               "' cannot be converted" +
256                                               " to a " + TypeManager.CSharpName (UnderlyingType));
257                         return;
258                 }
259
260                 /// <summary>
261                 ///  Determines if a standard implicit conversion exists from
262                 ///  expr_type to target_type
263                 /// </summary>
264                 public static bool ImplicitConversionExists (Type expr_type, Type target_type)
265                 {
266                         expr_type = TypeManager.TypeToCoreType (expr_type);
267
268                         if (expr_type == TypeManager.void_type)
269                                 return false;
270                         
271                         if (expr_type == target_type)
272                                 return true;
273
274                         // First numeric conversions 
275
276                         if (expr_type == TypeManager.sbyte_type){
277                                 //
278                                 // From sbyte to short, int, long, float, double.
279                                 //
280                                 if ((target_type == TypeManager.int32_type) || 
281                                     (target_type == TypeManager.int64_type) ||
282                                     (target_type == TypeManager.double_type) ||
283                                     (target_type == TypeManager.float_type)  ||
284                                     (target_type == TypeManager.short_type) ||
285                                     (target_type == TypeManager.decimal_type))
286                                         return true;
287                                 
288                         } else if (expr_type == TypeManager.byte_type){
289                                 //
290                                 // From byte to short, ushort, int, uint, long, ulong, float, double
291                                 // 
292                                 if ((target_type == TypeManager.short_type) ||
293                                     (target_type == TypeManager.ushort_type) ||
294                                     (target_type == TypeManager.int32_type) ||
295                                     (target_type == TypeManager.uint32_type) ||
296                                     (target_type == TypeManager.uint64_type) ||
297                                     (target_type == TypeManager.int64_type) ||
298                                     (target_type == TypeManager.float_type) ||
299                                     (target_type == TypeManager.double_type) ||
300                                     (target_type == TypeManager.decimal_type))
301                                         return true;
302         
303                         } else if (expr_type == TypeManager.short_type){
304                                 //
305                                 // From short to int, long, float, double
306                                 // 
307                                 if ((target_type == TypeManager.int32_type) ||
308                                     (target_type == TypeManager.int64_type) ||
309                                     (target_type == TypeManager.double_type) ||
310                                     (target_type == TypeManager.float_type) ||
311                                     (target_type == TypeManager.decimal_type))
312                                         return true;
313                                         
314                         } else if (expr_type == TypeManager.ushort_type){
315                                 //
316                                 // From ushort to int, uint, long, ulong, float, double
317                                 //
318                                 if ((target_type == TypeManager.uint32_type) ||
319                                     (target_type == TypeManager.uint64_type) ||
320                                     (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))
325                                         return true;
326                                     
327                         } else if (expr_type == TypeManager.int32_type){
328                                 //
329                                 // From int to long, float, double
330                                 //
331                                 if ((target_type == TypeManager.int64_type) ||
332                                     (target_type == TypeManager.double_type) ||
333                                     (target_type == TypeManager.float_type) ||
334                                     (target_type == TypeManager.decimal_type))
335                                         return true;
336                                         
337                         } else if (expr_type == TypeManager.uint32_type){
338                                 //
339                                 // From uint to long, ulong, float, double
340                                 //
341                                 if ((target_type == TypeManager.int64_type) ||
342                                     (target_type == TypeManager.uint64_type) ||
343                                     (target_type == TypeManager.double_type) ||
344                                     (target_type == TypeManager.float_type) ||
345                                     (target_type == TypeManager.decimal_type))
346                                         return true;
347                                         
348                         } else if ((expr_type == TypeManager.uint64_type) ||
349                                    (expr_type == TypeManager.int64_type)) {
350                                 //
351                                 // From long/ulong to float, double
352                                 //
353                                 if ((target_type == TypeManager.double_type) ||
354                                     (target_type == TypeManager.float_type) ||
355                                     (target_type == TypeManager.decimal_type))
356                                         return true;
357                                     
358                         } else if (expr_type == TypeManager.char_type){
359                                 //
360                                 // From char to ushort, int, uint, long, ulong, float, double
361                                 // 
362                                 if ((target_type == TypeManager.ushort_type) ||
363                                     (target_type == TypeManager.int32_type) ||
364                                     (target_type == TypeManager.uint32_type) ||
365                                     (target_type == TypeManager.uint64_type) ||
366                                     (target_type == TypeManager.int64_type) ||
367                                     (target_type == TypeManager.float_type) ||
368                                     (target_type == TypeManager.double_type) ||
369                                     (target_type == TypeManager.decimal_type))
370                                         return true;
371
372                         } else if (expr_type == TypeManager.float_type){
373                                 //
374                                 // float to double
375                                 //
376                                 if (target_type == TypeManager.double_type)
377                                         return true;
378                         }       
379                         
380                         return false;
381                 }
382
383                 /// <summary>
384                 ///  This is used to lookup the value of an enum member. If the member is undefined,
385                 ///  it attempts to define it and return its value
386                 /// </summary>
387                 public object LookupEnumValue (EmitContext ec, string name, Location loc)
388                 {
389                         object default_value = null;
390                         Constant c = null;
391
392                         default_value = member_to_value [name];
393
394                         if (default_value != null)
395                                 return default_value;
396
397                         //
398                         // This may happen if we're calling a method in System.Enum, for instance
399                         // Enum.IsDefined().
400                         //
401                         if (!defined_names.Contains (name))
402                                 return null;
403
404                         if (in_transit.Contains (name)) {
405                                 Report.Error (110, loc, "The evaluation of the constant value for `" +
406                                               Name + "." + name + "' involves a circular definition.");
407                                 return null;
408                         }
409
410                         //
411                         // So if the above doesn't happen, we have a member that is undefined
412                         // We now proceed to define it 
413                         //
414                         Expression val = this [name];
415
416                         if (val == null) {
417                                 
418                                 int idx = ordered_enums.IndexOf (name);
419
420                                 if (idx == 0)
421                                         default_value = 0;
422                                 else {
423                                         for (int i = 0; i < idx; ++i) {
424                                                 string n = (string) ordered_enums [i];
425                                                 Location m_loc = (Mono.CSharp.Location)
426                                                         member_to_location [n];
427                                                 in_transit.Add (name, true);
428                                                 default_value = LookupEnumValue (ec, n, m_loc);
429                                                 in_transit.Remove (name);
430                                                 if (default_value == null)
431                                                         return null;
432                                         }
433                                         
434                                         default_value = GetNextDefaultValue (default_value);
435                                 }
436                                 
437                         } else {
438                                 bool old = ec.InEnumContext;
439                                 ec.InEnumContext = true;
440                                 in_transit.Add (name, true);
441                                 val = val.Resolve (ec);
442                                 in_transit.Remove (name);
443                                 ec.InEnumContext = old;
444
445                                 if (val == null)
446                                         return null;
447
448                                 if (!IsValidEnumConstant (val)) {
449                                         Report.Error (
450                                                 1008, loc,
451                                                 "Type byte, sbyte, short, ushort, int, uint, long, or " +
452                                                 "ulong expected (have: " + val + ")");
453                                         return null;
454                                 }
455
456                                 c = (Constant) val;
457                                 default_value = c.GetValue ();
458
459                                 if (default_value == null) {
460                                         Error_ConstantValueCannotBeConverted (c, loc);
461                                         return null;
462                                 }
463
464                                 if (val is EnumConstant){
465                                         Type etype = TypeManager.EnumToUnderlying (c.Type);
466                                         
467                                         if (!ImplicitConversionExists (etype, UnderlyingType)){
468                                                 Expression.Error_CannotConvertImplicit (
469                                                         loc, c.Type, UnderlyingType);
470                                                 return null;
471                                         }
472                                 }
473                         }
474
475                         FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
476                                         | FieldAttributes.Literal;
477                         
478                         FieldBuilder fb = TypeBuilder.DefineField (name, UnderlyingType, attr);
479
480                         try {
481                                 default_value = TypeManager.ChangeType (default_value, UnderlyingType);
482                         } catch {
483                                 Error_ConstantValueCannotBeConverted (c, loc);
484                                 return null;
485                         }
486
487                         fb.SetConstant (default_value);
488                         field_builders.Add (fb);
489                         member_to_value [name] = default_value;
490
491                         if (!TypeManager.RegisterFieldValue (fb, default_value))
492                                 return null;
493
494                         //
495                         // Now apply attributes
496                         //
497                         Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name], loc); 
498                         
499                         return default_value;
500                 }
501
502                 public override bool DefineMembers (TypeContainer parent)
503                 {
504                         return true;
505                 }
506                 
507                 public override bool Define (TypeContainer parent)
508                 {
509                         //
510                         // If there was an error during DefineEnum, return
511                         //
512                         if (TypeBuilder == null)
513                                 return false;
514                         
515                         EmitContext ec = new EmitContext (parent, this, Location, null,
516                                                           UnderlyingType, ModFlags, false);
517                         
518                         object default_value = 0;
519                         
520                         FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static
521                                              | FieldAttributes.Literal;
522
523                         
524                         foreach (string name in ordered_enums) {
525                                 //
526                                 // Have we already been defined, thanks to some cross-referencing ?
527                                 // 
528                                 if (member_to_value.Contains (name))
529                                         continue;
530                                 
531                                 Location loc = (Mono.CSharp.Location) member_to_location [name];
532
533                                 if (this [name] != null) {
534                                         default_value = LookupEnumValue (ec, name, loc);
535
536                                         if (default_value == null)
537                                                 return true;
538
539                                 } else {
540                                         FieldBuilder fb = TypeBuilder.DefineField (
541                                                 name, UnderlyingType, attr);
542                                         
543                                         if (default_value == null) {
544                                            Report.Error (543, loc, "Enumerator value for '" + name + "' is too large to " +
545                                                               "fit in its type");
546                                                 return false;
547                                         }
548                                         
549                                         try {
550                                                 default_value = TypeManager.ChangeType (default_value, UnderlyingType);
551                                         } catch {
552                                                 Error_ConstantValueCannotBeConverted (default_value, loc);
553                                                 return false;
554                                         }
555
556                                         fb.SetConstant (default_value);
557                                         field_builders.Add (fb);
558                                         member_to_value [name] = default_value;
559                                         
560                                         if (!TypeManager.RegisterFieldValue (fb, default_value))
561                                                 return false;
562
563                                         //
564                                         // Apply attributes on the enum member
565                                         //
566                                         Attribute.ApplyAttributes (ec, fb, fb, (Attributes) member_to_attributes [name], loc);
567                                 }
568
569                                 default_value = GetNextDefaultValue (default_value);
570                         }
571                         
572                         Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
573
574                         return true;
575                 }
576                 
577                 //
578                 // IMemberFinder
579                 //
580                 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
581                                                         MemberFilter filter, object criteria)
582                 {
583                         ArrayList members = new ArrayList ();
584
585                         if ((mt & MemberTypes.Field) != 0) {
586                                 foreach (FieldBuilder fb in field_builders)
587                                         if (filter (fb, criteria) == true)
588                                                 members.Add (fb);
589                         }
590
591                         return new MemberList (members);
592                 }
593
594                 public override MemberCache MemberCache {
595                         get {
596                                 return null;
597                         }
598                 }
599
600                 public ArrayList ValueNames {
601                         get {
602                                 return ordered_enums;
603                         }
604                 }
605
606                 // indexer
607                 public Expression this [string name] {
608                         get {
609                                 return (Expression) defined_names [name];
610                         }
611                 }
612         }
613 }