Remove TypeManager.LookupType and TypeManager.LookupTypeDirect.
[mono.git] / mcs / bmcs / const.cs
1 //
2 // const.cs: Constant declarations.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Marek Safar (marek.safar@seznam.cz)
7 //
8 // (C) 2001 Ximian, Inc.
9 //
10 //
11
12 //
13 // This is needed because the following situation arises:
14 //
15 //     The FieldBuilder is declared with the real type for an enumeration
16 //
17 //     When we attempt to set the value for the constant, the FieldBuilder.SetConstant
18 //     function aborts because it requires its argument to be of the same type
19 //
20
21 namespace Mono.CSharp {
22
23         using System;
24         using System.Reflection;
25         using System.Reflection.Emit;
26         using System.Collections;
27
28         public class Const : FieldMember {
29                 public Expression Expr;
30                 EmitContext const_ec;
31
32                 bool resolved = false;
33                 object ConstantValue = null;
34
35                 bool in_transit = false;
36
37                 public const int AllowedModifiers =
38                         Modifiers.NEW |
39                         Modifiers.PUBLIC |
40                         Modifiers.PROTECTED |
41                         Modifiers.INTERNAL |
42                         Modifiers.PRIVATE;
43
44                 public Const (TypeContainer parent, Expression constant_type, string name,
45                               Expression expr, int mod_flags, Attributes attrs, Location loc)
46                         : base (parent, constant_type, mod_flags, AllowedModifiers,
47                                 new MemberName (name), null, attrs, loc)
48                 {
49                         Expr = expr;
50                         ModFlags |= Modifiers.STATIC;
51                 }
52
53 #if DEBUG
54                 void dump_tree (Type t)
55                 {
56                         Console.WriteLine ("Dumping hierarchy");
57                         while (t != null){
58                                 Console.WriteLine ("   " + t.FullName + " " +
59                                         (t.GetType ().IsEnum ? "yes" : "no"));
60                                 t = t.BaseType;
61                         }
62                 }
63 #endif
64
65                 /// <summary>
66                 ///   Defines the constant in the @parent
67                 /// </summary>
68                 public override bool Define ()
69                 {
70                         if (!base.Define ())
71                                 return false;
72
73                         const_ec = new EmitContext (Parent, Location, null, MemberType, ModFlags);
74
75                         Type ttype = MemberType;
76                         while (ttype.IsArray)
77                             ttype = TypeManager.GetElementType (ttype);
78                         
79                         if (!TypeManager.IsBuiltinType(ttype) && (!ttype.IsSubclassOf(TypeManager.enum_type)) && !(Expr is NullLiteral)){
80                                 Report.Error (
81                                         -3, Location,
82                                         "Constant type is not valid (only system types are allowed)");
83                                 return false;
84                         }
85
86                         FieldAttributes field_attr = FieldAttributes.Static | Modifiers.FieldAttr (ModFlags);
87                         // I don't know why but they emit decimal constant as InitOnly
88                         if (ttype == TypeManager.decimal_type) {
89                                 field_attr |= FieldAttributes.InitOnly;
90                         }
91                         else {
92                                 field_attr |= FieldAttributes.Literal;
93                         }
94
95                         FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType, field_attr);
96
97                         TypeManager.RegisterConstant (FieldBuilder, this);
98
99                         return true;
100                 }
101
102                 //
103                 // Changes the type of the constant expression `expr' to the Type `type'
104                 // Returns null on failure.
105                 //
106                 public static Constant ChangeType (Location loc, Constant expr, Type type)
107                 {
108                         if (type == TypeManager.object_type)
109                                 return expr;
110
111                         bool fail;
112
113                         // from the null type to any reference-type.
114                         if (expr.Type == TypeManager.null_type && !type.IsValueType && !TypeManager.IsEnumType (type))
115                                 return NullLiteral.Null;
116
117                         if (!Convert.WideningStandardConversionExists (Convert.ConstantEC, expr, type)){
118                                 Convert.Error_CannotWideningConversion (loc, expr.Type, type);
119                                 return null;
120                         }
121
122                         // Special-case: The 0 literal can be converted to an enum value,
123                         // and WideningStandardConversionExists will return true in that case.
124                         if (expr.Type == TypeManager.int32_type && TypeManager.IsEnumType (type)){
125                                 if (expr is IntLiteral && ((IntLiteral) expr).Value == 0)
126                                         return new EnumConstant (expr, type);
127                         }
128                         
129                         object constant_value = TypeManager.ChangeType (expr.GetValue (), type, out fail);
130                         if (fail){
131                                 Convert.Error_CannotWideningConversion (loc, expr.Type, type);
132                                 
133                                 //
134                                 // We should always catch the error before this is ever
135                                 // reached, by calling Convert.WideningStandardConversionExists
136                                 //
137                                 throw new Exception (
138                                                      String.Format ("LookupConstantValue: This should never be reached {0} {1}", expr.Type, type));
139                         }
140
141                         Constant retval;
142                         if (type == TypeManager.int32_type)
143                                 retval = new IntConstant ((int) constant_value);
144                         else if (type == TypeManager.uint32_type)
145                                 retval = new UIntConstant ((uint) constant_value);
146                         else if (type == TypeManager.int64_type)
147                                 retval = new LongConstant ((long) constant_value);
148                         else if (type == TypeManager.uint64_type)
149                                 retval = new ULongConstant ((ulong) constant_value);
150                         else if (type == TypeManager.float_type)
151                                 retval = new FloatConstant ((float) constant_value);
152                         else if (type == TypeManager.double_type)
153                                 retval = new DoubleConstant ((double) constant_value);
154                         else if (type == TypeManager.string_type)
155                                 retval = new StringConstant ((string) constant_value);
156                         else if (type == TypeManager.short_type)
157                                 retval = new ShortConstant ((short) constant_value);
158                         else if (type == TypeManager.ushort_type)
159                                 retval = new UShortConstant ((ushort) constant_value);
160                         else if (type == TypeManager.sbyte_type)
161                                 retval = new SByteConstant ((sbyte) constant_value);
162                         else if (type == TypeManager.byte_type)
163                                 retval = new ByteConstant ((byte) constant_value);
164                         else if (type == TypeManager.char_type)
165                                 retval = new CharConstant ((char) constant_value);
166                         else if (type == TypeManager.bool_type)
167                                 retval = new BoolConstant ((bool) constant_value);
168                         else if (type == TypeManager.decimal_type)
169                                 retval = new DecimalConstant ((decimal) constant_value);
170                         else
171                                 throw new Exception ("LookupConstantValue: Unhandled constant type: " + type);
172                         
173                         return retval;
174                 }
175                 
176                 /// <summary>
177                 ///  Looks up the value of a constant field. Defines it if it hasn't
178                 ///  already been. Similar to LookupEnumValue in spirit.
179                 /// </summary>
180                 public bool LookupConstantValue (out object value)
181                 {
182                         if (resolved) {
183                                 value = ConstantValue;
184                                 return true;
185                         }
186
187                         if (in_transit) {
188                                 Report.Error (110, Location,
189                                               "The evaluation of the constant value for `" +
190                                               Name + "' involves a circular definition.");
191                                 value = null;
192                                 return false;
193                         }
194
195                         in_transit = true;
196                         int errors = Report.Errors;
197
198                         //
199                         // We might have cleared Expr ourselves in a recursive definition
200                         //
201                         if (Expr == null){
202                                 value = null;
203                                 return false;
204                         }
205
206                         Expr = Expr.Resolve (const_ec);
207
208                         in_transit = false;
209
210                         if (Expr == null) {
211                                 if (errors == Report.Errors)
212                                         Report.Error (150, Location, "A constant value is expected");
213                                 value = null;
214                                 return false;
215                         }
216
217                         Expression real_expr = Expr;
218
219                         Constant ce = Expr as Constant;
220                         if (ce == null){
221                                 UnCheckedExpr un_expr = Expr as UnCheckedExpr;
222                                 CheckedExpr ch_expr = Expr as CheckedExpr;
223                                 EmptyCast ec_expr = Expr as EmptyCast;
224
225                                 if ((un_expr != null) && (un_expr.Expr is Constant))
226                                         Expr = un_expr.Expr;
227                                 else if ((ch_expr != null) && (ch_expr.Expr is Constant))
228                                         Expr = ch_expr.Expr;
229                                 else if ((ec_expr != null) && (ec_expr.Child is Constant))
230                                         Expr = ec_expr.Child;
231                                 else if (Expr is ArrayCreation){
232                                         Report.Error (133, Location, "Arrays can not be constant");
233                                 } else {
234                                         if (errors == Report.Errors)
235                                                 Report.Error (150, Location, "A constant value is expected");
236                                         value = null;
237                                         return false;
238                                 }
239
240                                 ce = Expr as Constant;
241                         }
242
243                         if (MemberType != real_expr.Type) {
244                                 ce = ChangeType (Location, ce, MemberType);
245                                 if (ce == null){
246                                         value = null;
247                                         return false;
248                                 }
249                                 Expr = ce;
250                         }
251                         ConstantValue = ce.GetValue ();
252
253                         if (MemberType.IsEnum){
254                                 //
255                                 // This sadly does not work for our user-defined enumerations types ;-(
256                                 //
257                                 try {
258                                         ConstantValue = System.Enum.ToObject (
259                                                 MemberType, ConstantValue);
260                                 } catch (ArgumentException){
261                                         Report.Error (
262                                                 -16, Location,
263                                                 ".NET SDK 1.0 does not permit to create the constant "+
264                                                 " field from a user-defined enumeration");
265                                 }
266                         }
267
268                         if (ce is DecimalConstant) {
269                                 Decimal d = ((DecimalConstant)ce).Value;
270                                 int[] bits = Decimal.GetBits (d);
271                                 object[] args = new object[] { (byte)(bits [3] >> 16), (byte)(bits [3] >> 31), (uint)bits [2], (uint)bits [1], (uint)bits [0] };
272                                 CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.decimal_constant_attribute_ctor, args);
273                                 FieldBuilder.SetCustomAttribute (cab);
274                         }
275                         else{
276                                 FieldBuilder.SetConstant (ConstantValue);
277                         }
278
279                         if (!TypeManager.RegisterFieldValue (FieldBuilder, ConstantValue))
280                                 throw new Exception ("Cannot register const value");
281
282                         value = ConstantValue;
283                         resolved = true;
284                         return true;
285                 }
286                 
287                 
288                 /// <summary>
289                 ///  Emits the field value by evaluating the expression
290                 /// </summary>
291                 public override void Emit ()
292                 {
293                         object value;
294                         LookupConstantValue (out value);
295
296                         if (OptAttributes != null) {
297                                 OptAttributes.Emit (const_ec, this);
298                         }
299
300                         base.Emit ();
301                 }
302         }
303 }
304
305