2007-03-15 Igor Zelmanovich <igorz@mainsoft.com>
[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 //         Marek Safar     (marek.safar@seznam.cz)
7 //
8 // Licensed under the terms of the GNU GPL
9 //
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 //
12
13 using System;
14 using System.Collections;
15 using System.Collections.Specialized;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Globalization;
19
20 namespace Mono.CSharp {
21
22         public class EnumMember : Const {
23                 protected readonly Enum ParentEnum;
24                 protected readonly Expression ValueExpr;
25                 readonly EnumMember prev_member;
26
27                 public EnumMember (Enum parent, EnumMember prev_member, string name, Expression expr,
28                                    Attributes attrs, Location loc)
29                         : base (parent, new EnumTypeExpr (parent), name, expr, Modifiers.PUBLIC,
30                                 attrs, loc)
31                 {
32                         this.ParentEnum = parent;
33                         this.ValueExpr = expr;
34                         this.prev_member = prev_member;
35                 }
36
37                 protected class EnumTypeExpr : TypeExpr
38                 {
39                         public readonly Enum Enum;
40
41                         public EnumTypeExpr (Enum e)
42                         {
43                                 this.Enum = e;
44                         }
45
46                         protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
47                         {
48                                 type = Enum.CurrentType != null ? Enum.CurrentType : Enum.TypeBuilder;
49                                 return this;
50                         }
51
52                         public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
53                         {
54                                 return DoResolveAsTypeStep (ec);
55                         }
56
57                         public override string Name {
58                                 get { return Enum.Name; }
59                         }
60
61                         public override string FullName {
62                                 get { return Enum.Name; }
63                         }
64                 }
65
66                 static bool IsValidEnumType (Type t)
67                 {
68                         return (t == TypeManager.int32_type || t == TypeManager.uint32_type || t == TypeManager.int64_type ||
69                                 t == TypeManager.byte_type || t == TypeManager.sbyte_type || t == TypeManager.short_type ||
70                                 t == TypeManager.ushort_type || t == TypeManager.uint64_type || t == TypeManager.char_type ||
71                                 t.IsEnum);
72                 }
73
74                 public object Value {
75                         get { return value.GetValue (); }
76                 }
77         
78                 protected override Constant DoResolveValue (EmitContext ec)
79                 {
80                         if (ValueExpr != null) {
81                                 Constant c = ValueExpr.ResolveAsConstant (ec, this);
82                                 if (c == null)
83                                         return null;
84
85                                 if (c is EnumConstant)
86                                         c = ((EnumConstant)c).Child;
87
88                                 c = c.ImplicitConversionRequired (ParentEnum.UnderlyingType, Location);
89                                 if (c == null)
90                                         return null;
91
92                                 if (!IsValidEnumType (c.Type)) {
93                                         Enum.Error_1008 (Location);
94                                         return null;
95                                 }
96
97                                 return new EnumConstant (c, MemberType);
98                         }
99
100                         if (prev_member == null) {
101                                 return new EnumConstant (
102                                         New.Constantify (ParentEnum.UnderlyingType), MemberType);
103                         }
104
105                         if (!prev_member.ResolveValue ()) {
106                                 prev_member.value = new EnumConstant (
107                                         New.Constantify (ParentEnum.UnderlyingType), MemberType);
108                                 return null;
109                         }
110
111                         try {
112                                 return (EnumConstant) prev_member.value.Increment ();
113                         } catch (OverflowException) {
114                                 Report.Error (543, Location, "The enumerator value `{0}' is too " +
115                                               "large to fit in its type `{1}'", GetSignatureForError (),
116                                               TypeManager.CSharpName (ParentEnum.UnderlyingType));
117                                 return null;
118                         }
119                 }
120         }
121
122         /// <summary>
123         ///   Enumeration container
124         /// </summary>
125         public class Enum : TypeContainer
126         {
127                 Expression base_type;
128
129                 public Type UnderlyingType;
130
131                 public const int AllowedModifiers =
132                         Modifiers.NEW |
133                         Modifiers.PUBLIC |
134                         Modifiers.PROTECTED |
135                         Modifiers.INTERNAL |
136                         Modifiers.PRIVATE;
137
138                 public Enum (NamespaceEntry ns, DeclSpace parent, Expression type,
139                              int mod_flags, MemberName name, Attributes attrs)
140                         : base (ns, parent, name, attrs, Kind.Enum)
141                 {
142                         this.base_type = type;
143                         int accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE;
144                         ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, accmods, Location);
145                 }
146
147                 public void AddEnumMember (EnumMember em)
148                 {
149                         if (em.Name == "value__") {
150                                 Report.Error (76, em.Location, "An item in an enumeration cannot " +
151                                               "have an identifier `value__'");
152                                 return;
153                         }
154
155                         AddConstant (em);
156                 }
157
158                 public static void Error_1008 (Location loc)
159                 {
160                         Report.Error (1008, loc, "Type byte, sbyte, short, ushort, " +
161                                       "int, uint, long or ulong expected");
162                 }
163
164                 protected override bool DefineNestedTypes ()
165                 {
166                         if (!base.DefineNestedTypes ())
167                                 return false;
168
169                         if (!(base_type is TypeLookupExpression)) {
170                                 Error_1008 (Location);
171                                 return false;
172                         }
173
174                         TypeExpr ute = base_type.ResolveAsTypeTerminal (this, false);
175                         UnderlyingType = ute.Type;
176
177                         if (UnderlyingType != TypeManager.int32_type &&
178                             UnderlyingType != TypeManager.uint32_type &&
179                             UnderlyingType != TypeManager.int64_type &&
180                             UnderlyingType != TypeManager.uint64_type &&
181                             UnderlyingType != TypeManager.short_type &&
182                             UnderlyingType != TypeManager.ushort_type &&
183                             UnderlyingType != TypeManager.byte_type  &&
184                             UnderlyingType != TypeManager.sbyte_type) {
185                                 Error_1008 (Location);
186                                 return false;
187                         }
188
189                         //
190                         // Call MapToInternalType for corlib
191                         //
192                         TypeBuilder.DefineField ("value__", UnderlyingType,
193                                                  FieldAttributes.Public | FieldAttributes.SpecialName
194                                                  | FieldAttributes.RTSpecialName);
195
196                         return true;
197                 }
198
199                 //
200                 // Used for error reporting only
201                 //
202                 public EnumMember GetDefinition (object value)
203                 {
204                         foreach (EnumMember e in defined_names.Values) {
205                                 if (e.Value.Equals (value))
206                                         return e;
207                         }
208
209                         throw new ArgumentOutOfRangeException (value.ToString ());
210                 }
211
212                 protected override bool VerifyClsCompliance ()
213                 {
214                         if (!base.VerifyClsCompliance ())
215                                 return false;
216
217                         if (UnderlyingType == TypeManager.uint32_type ||
218                                 UnderlyingType == TypeManager.uint64_type ||
219                                 UnderlyingType == TypeManager.ushort_type) {
220                                 Report.Error (3009, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
221                         }
222
223                         return true;
224                 }       
225
226                 public override AttributeTargets AttributeTargets {
227                         get {
228                                 return AttributeTargets.Enum;
229                         }
230                 }
231
232                 protected override TypeAttributes TypeAttr {
233                         get {
234                                 return Modifiers.TypeAttr (ModFlags, IsTopLevel) |
235                                         TypeAttributes.Class | TypeAttributes.Sealed | base.TypeAttr;
236                         }
237                 }
238         }
239 }