Merge branch 'master' of github.com:mono/mono
[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 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 //
10 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2003-2003 Novell, Inc (http://www.novell.com)
12 //
13
14 using System;
15
16 #if STATIC
17 using MetaType = IKVM.Reflection.Type;
18 using IKVM.Reflection;
19 #else
20 using MetaType = System.Type;
21 using System.Reflection;
22 #endif
23
24 namespace Mono.CSharp {
25
26         public class EnumMember : Const
27         {
28                 class EnumTypeExpr : TypeExpr
29                 {
30                         public override TypeSpec ResolveAsType (IMemberContext ec)
31                         {
32                                 type = ec.CurrentType;
33                                 eclass = ExprClass.Type;
34                                 return type;
35                         }
36                 }
37
38                 public EnumMember (Enum parent, MemberName name, Attributes attrs)
39                         : base (parent, new EnumTypeExpr (), Modifiers.PUBLIC, name, attrs)
40                 {
41                 }
42
43                 static bool IsValidEnumType (TypeSpec t)
44                 {
45                         switch (t.BuiltinType) {
46                         case BuiltinTypeSpec.Type.Int:
47                         case BuiltinTypeSpec.Type.UInt:
48                         case BuiltinTypeSpec.Type.Long:
49                         case BuiltinTypeSpec.Type.Byte:
50                         case BuiltinTypeSpec.Type.SByte:
51                         case BuiltinTypeSpec.Type.Short:
52                         case BuiltinTypeSpec.Type.UShort:
53                         case BuiltinTypeSpec.Type.ULong:
54                         case BuiltinTypeSpec.Type.Char:
55                                 return true;
56                         default:
57                                 return t.IsEnum;
58                         }
59                 }
60
61                 public override Constant ConvertInitializer (ResolveContext rc, Constant expr)
62                 {
63                         if (expr is EnumConstant)
64                                 expr = ((EnumConstant) expr).Child;
65
66                         var underlying = ((Enum) Parent).UnderlyingType;
67                         if (expr != null) {
68                                 expr = expr.ImplicitConversionRequired (rc, underlying, Location);
69                                 if (expr != null && !IsValidEnumType (expr.Type)) {
70                                         Enum.Error_1008 (Location, Report);
71                                         expr = null;
72                                 }
73                         }
74
75                         if (expr == null)
76                                 expr = New.Constantify (underlying, Location);
77
78                         return new EnumConstant (expr, MemberType);
79                 }
80
81                 public override bool Define ()
82                 {
83                         if (!ResolveMemberType ())
84                                 return false;
85
86                         const FieldAttributes attr = FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal;
87                         FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType.GetMetaInfo (), attr);
88                         spec = new ConstSpec (Parent.Definition, this, MemberType, FieldBuilder, ModFlags, initializer);
89
90                         Parent.MemberCache.AddMember (spec);
91                         return true;
92                 }
93         }
94
95         /// <summary>
96         ///   Enumeration container
97         /// </summary>
98         public class Enum : TypeContainer
99         {
100                 //
101                 // Implicit enum member initializer, used when no constant value is provided
102                 //
103                 sealed class ImplicitInitializer : Expression
104                 {
105                         readonly EnumMember prev;
106                         readonly EnumMember current;
107
108                         public ImplicitInitializer (EnumMember current, EnumMember prev)
109                         {
110                                 this.current = current;
111                                 this.prev = prev;
112                         }
113
114                         public override Expression CreateExpressionTree (ResolveContext ec)
115                         {
116                                 throw new NotSupportedException ("Missing Resolve call");
117                         }
118
119                         protected override Expression DoResolve (ResolveContext rc)
120                         {
121                                 // We are the first member
122                                 if (prev == null) {
123                                         return New.Constantify (current.Parent.Definition, Location);
124                                 }
125
126                                 var c = ((ConstSpec) prev.Spec).GetConstant (rc) as EnumConstant;
127                                 try {
128                                         return c.Increment ();
129                                 } catch (OverflowException) {
130                                         rc.Report.Error (543, current.Location,
131                                                 "The enumerator value `{0}' is outside the range of enumerator underlying type `{1}'",
132                                                 current.GetSignatureForError (), ((Enum) current.Parent).UnderlyingType.GetSignatureForError ());
133
134                                         return New.Constantify (current.Parent.Definition, current.Location);
135                                 }
136                         }
137
138                         public override void Emit (EmitContext ec)
139                         {
140                                 throw new NotSupportedException ("Missing Resolve call");
141                         }
142                 }
143
144                 public static readonly string UnderlyingValueField = "value__";
145
146                 const Modifiers AllowedModifiers =
147                         Modifiers.NEW |
148                         Modifiers.PUBLIC |
149                         Modifiers.PROTECTED |
150                         Modifiers.INTERNAL |
151                         Modifiers.PRIVATE;
152
153                 readonly TypeExpr underlying_type_expr;
154
155                 public Enum (NamespaceContainer ns, DeclSpace parent, TypeExpression type,
156                              Modifiers mod_flags, MemberName name, Attributes attrs)
157                         : base (ns, parent, name, attrs, MemberKind.Enum)
158                 {
159                         underlying_type_expr = type;
160                         var accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE;
161                         ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod_flags, accmods, Location, Report);
162                         spec = new EnumSpec (null, this, null, null, ModFlags);
163                 }
164
165                 #region Properties
166
167                 public override AttributeTargets AttributeTargets {
168                         get {
169                                 return AttributeTargets.Enum;
170                         }
171                 }
172
173                 public TypeExpr BaseTypeExpression {
174                         get {
175                                 return underlying_type_expr;
176                         }
177                 }
178
179                 protected override TypeAttributes TypeAttr {
180                         get {
181                                 return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel) |
182                                         TypeAttributes.Class | TypeAttributes.Sealed | base.TypeAttr;
183                         }
184                 }
185
186                 public TypeSpec UnderlyingType {
187                         get {
188                                 return ((EnumSpec) spec).UnderlyingType;
189                         }
190                 }
191
192                 #endregion
193
194                 public override void Accept (StructuralVisitor visitor)
195                 {
196                         visitor.Visit (this);
197                 }
198
199                 public void AddEnumMember (EnumMember em)
200                 {
201                         if (em.Name == UnderlyingValueField) {
202                                 Report.Error (76, em.Location, "An item in an enumeration cannot have an identifier `{0}'",
203                                         UnderlyingValueField);
204                                 return;
205                         }
206
207                         AddConstant (em);
208                 }
209
210                 public static void Error_1008 (Location loc, Report Report)
211                 {
212                         Report.Error (1008, loc,
213                                 "Type byte, sbyte, short, ushort, int, uint, long or ulong expected");
214                 }
215
216                 protected override bool DefineNestedTypes ()
217                 {
218                         ((EnumSpec) spec).UnderlyingType = underlying_type_expr == null ? Compiler.BuiltinTypes.Int : underlying_type_expr.Type;
219
220                         TypeBuilder.DefineField (UnderlyingValueField, UnderlyingType.GetMetaInfo (),
221                                 FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);
222
223                         return true;
224                 }
225
226                 protected override bool DoDefineMembers ()
227                 {
228                         if (constants != null) {
229                                 for (int i = 0; i < constants.Count; ++i) {
230                                         EnumMember em = (EnumMember) constants [i];
231                                         if (em.Initializer == null) {
232                                                 em.Initializer = new ImplicitInitializer (em, i == 0 ? null : (EnumMember) constants[i - 1]);
233                                         }
234
235                                         em.Define ();
236                                 }
237                         }
238
239                         return true;
240                 }
241
242                 public override bool IsUnmanagedType ()
243                 {
244                         return true;
245                 }
246
247                 protected override TypeSpec[] ResolveBaseTypes (out FullNamedExpression base_class)
248                 {
249                         base_type = Compiler.BuiltinTypes.Enum;
250                         base_class = null;
251                         return null;
252                 }
253
254                 protected override bool VerifyClsCompliance ()
255                 {
256                         if (!base.VerifyClsCompliance ())
257                                 return false;
258
259                         switch (UnderlyingType.BuiltinType) {
260                         case BuiltinTypeSpec.Type.UInt:
261                         case BuiltinTypeSpec.Type.ULong:
262                         case BuiltinTypeSpec.Type.UShort:
263                                 Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant",
264                                         GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
265                                 break;
266                         }
267
268                         return true;
269                 }       
270         }
271
272         class EnumSpec : TypeSpec
273         {
274                 TypeSpec underlying;
275
276                 public EnumSpec (TypeSpec declaringType, ITypeDefinition definition, TypeSpec underlyingType, MetaType info, Modifiers modifiers)
277                         : base (MemberKind.Enum, declaringType, definition, info, modifiers | Modifiers.SEALED)
278                 {
279                         this.underlying = underlyingType;
280                 }
281
282                 public TypeSpec UnderlyingType {
283                         get {
284                                 return underlying;
285                         }
286                         set {
287                                 if (underlying != null)
288                                         throw new InternalErrorException ("UnderlyingType reset");
289
290                                 underlying = value;
291                         }
292                 }
293
294                 public static TypeSpec GetUnderlyingType (TypeSpec t)
295                 {
296                         return ((EnumSpec) t.GetDefinition ()).UnderlyingType;
297                 }
298
299                 public static bool IsValidUnderlyingType (TypeSpec type)
300                 {
301                         switch (type.BuiltinType) {
302                         case BuiltinTypeSpec.Type.Int:
303                         case BuiltinTypeSpec.Type.UInt:
304                         case BuiltinTypeSpec.Type.Long:
305                         case BuiltinTypeSpec.Type.Byte:
306                         case BuiltinTypeSpec.Type.SByte:
307                         case BuiltinTypeSpec.Type.Short:
308                         case BuiltinTypeSpec.Type.UShort:
309                         case BuiltinTypeSpec.Type.ULong:
310                                 return true;
311                         }
312
313                         return false;
314                 }
315         }
316 }