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