Clearly split namespace and type resolve paths
[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 TypeExpr ResolveAsType (IMemberContext ec, bool silent)
31                         {
32                                 type = ec.CurrentType;
33                                 eclass = ExprClass.Type;
34                                 return this;
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                 public Enum (NamespaceContainer ns, DeclSpace parent, TypeExpression type,
154                              Modifiers mod_flags, MemberName name, Attributes attrs)
155                         : base (ns, parent, name, attrs, MemberKind.Enum)
156                 {
157                         base_type_expr = type;
158                         var accmods = IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE;
159                         ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod_flags, accmods, Location, Report);
160                         spec = new EnumSpec (null, this, null, null, ModFlags);
161                 }
162
163                 #region Properties
164
165                 public override AttributeTargets AttributeTargets {
166                         get {
167                                 return AttributeTargets.Enum;
168                         }
169                 }
170
171                 public TypeExpr BaseTypeExpression {
172                         get {
173                                 return base_type_expr;
174                         }
175                 }
176
177                 protected override TypeAttributes TypeAttr {
178                         get {
179                                 return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel) |
180                                         TypeAttributes.Class | TypeAttributes.Sealed | base.TypeAttr;
181                         }
182                 }
183
184                 public TypeSpec UnderlyingType {
185                         get {
186                                 return ((EnumSpec) spec).UnderlyingType;
187                         }
188                 }
189
190                 #endregion
191
192                 public override void Accept (StructuralVisitor visitor)
193                 {
194                         visitor.Visit (this);
195                 }
196
197                 public void AddEnumMember (EnumMember em)
198                 {
199                         if (em.Name == UnderlyingValueField) {
200                                 Report.Error (76, em.Location, "An item in an enumeration cannot have an identifier `{0}'",
201                                         UnderlyingValueField);
202                                 return;
203                         }
204
205                         AddConstant (em);
206                 }
207
208                 public static void Error_1008 (Location loc, Report Report)
209                 {
210                         Report.Error (1008, loc,
211                                 "Type byte, sbyte, short, ushort, int, uint, long or ulong expected");
212                 }
213
214                 protected override bool DefineNestedTypes ()
215                 {
216                         ((EnumSpec) spec).UnderlyingType = base_type_expr == null ? Compiler.BuiltinTypes.Int : base_type_expr.Type;
217
218                         TypeBuilder.DefineField (UnderlyingValueField, UnderlyingType.GetMetaInfo (),
219                                 FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);
220
221                         return true;
222                 }
223
224                 protected override bool DoDefineMembers ()
225                 {
226                         if (constants != null) {
227                                 for (int i = 0; i < constants.Count; ++i) {
228                                         EnumMember em = (EnumMember) constants [i];
229                                         if (em.Initializer == null) {
230                                                 em.Initializer = new ImplicitInitializer (em, i == 0 ? null : (EnumMember) constants[i - 1]);
231                                         }
232
233                                         em.Define ();
234                                 }
235                         }
236
237                         return true;
238                 }
239
240                 public override bool IsUnmanagedType ()
241                 {
242                         return true;
243                 }
244
245                 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
246                 {
247                         base_type = Compiler.BuiltinTypes.Enum;
248                         base_class = base_type_expr;
249                         return null;
250                 }
251
252                 protected override bool VerifyClsCompliance ()
253                 {
254                         if (!base.VerifyClsCompliance ())
255                                 return false;
256
257                         switch (UnderlyingType.BuiltinType) {
258                         case BuiltinTypeSpec.Type.UInt:
259                         case BuiltinTypeSpec.Type.ULong:
260                         case BuiltinTypeSpec.Type.UShort:
261                                 Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant",
262                                         GetSignatureForError (), TypeManager.CSharpName (UnderlyingType));
263                                 break;
264                         }
265
266                         return true;
267                 }       
268         }
269
270         class EnumSpec : TypeSpec
271         {
272                 TypeSpec underlying;
273
274                 public EnumSpec (TypeSpec declaringType, ITypeDefinition definition, TypeSpec underlyingType, MetaType info, Modifiers modifiers)
275                         : base (MemberKind.Enum, declaringType, definition, info, modifiers | Modifiers.SEALED)
276                 {
277                         this.underlying = underlyingType;
278                 }
279
280                 public TypeSpec UnderlyingType {
281                         get {
282                                 return underlying;
283                         }
284                         set {
285                                 if (underlying != null)
286                                         throw new InternalErrorException ("UnderlyingType reset");
287
288                                 underlying = value;
289                         }
290                 }
291
292                 public static TypeSpec GetUnderlyingType (TypeSpec t)
293                 {
294                         return ((EnumSpec) t.GetDefinition ()).UnderlyingType;
295                 }
296
297                 public static bool IsValidUnderlyingType (TypeSpec type)
298                 {
299                         switch (type.BuiltinType) {
300                         case BuiltinTypeSpec.Type.Int:
301                         case BuiltinTypeSpec.Type.UInt:
302                         case BuiltinTypeSpec.Type.Long:
303                         case BuiltinTypeSpec.Type.Byte:
304                         case BuiltinTypeSpec.Type.SByte:
305                         case BuiltinTypeSpec.Type.Short:
306                         case BuiltinTypeSpec.Type.UShort:
307                         case BuiltinTypeSpec.Type.ULong:
308                                 return true;
309                         }
310
311                         return false;
312                 }
313         }
314 }