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