2 // const.cs: Constant declarations.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // (C) 2001 Ximian, Inc.
13 // This is needed because the following situation arises:
15 // The FieldBuilder is declared with the real type for an enumeration
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
21 namespace Mono.CSharp {
24 using System.Reflection;
25 using System.Reflection.Emit;
26 using System.Collections;
28 public class Const : FieldMember {
29 public Expression Expr;
32 bool resolved = false;
33 object ConstantValue = null;
35 bool in_transit = false;
37 public const int AllowedModifiers =
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)
50 ModFlags |= Modifiers.STATIC;
54 void dump_tree (Type t)
56 Console.WriteLine ("Dumping hierarchy");
58 Console.WriteLine (" " + t.FullName + " " +
59 (t.GetType ().IsEnum ? "yes" : "no"));
66 /// Defines the constant in the @parent
68 public override bool Define ()
73 const_ec = new EmitContext (Parent, Location, null, MemberType, ModFlags);
75 Type ttype = MemberType;
77 ttype = TypeManager.GetElementType (ttype);
79 if (!TypeManager.IsBuiltinType(ttype) && (!ttype.IsSubclassOf(TypeManager.enum_type)) && !(Expr is NullLiteral)){
82 "Constant type is not valid (only system types are allowed)");
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;
92 field_attr |= FieldAttributes.Literal;
95 FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType, field_attr);
97 TypeManager.RegisterConstant (FieldBuilder, this);
103 // Changes the type of the constant expression `expr' to the Type `type'
104 // Returns null on failure.
106 public static Constant ChangeType (Location loc, Constant expr, Type type)
108 if (type == TypeManager.object_type)
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;
117 if (!Convert.ImplicitStandardConversionExists (Convert.ConstantEC, expr, type)){
118 Convert.Error_CannotImplicitConversion (loc, expr.Type, type);
122 object constant_value = TypeManager.ChangeType (expr.GetValue (), type, out fail);
124 Convert.Error_CannotImplicitConversion (loc, expr.Type, type);
127 // We should always catch the error before this is ever
128 // reached, by calling Convert.ImplicitStandardConversionExists
130 throw new Exception (
131 String.Format ("LookupConstantValue: This should never be reached {0} {1}", expr.Type, type));
135 if (type == TypeManager.int32_type)
136 retval = new IntConstant ((int) constant_value);
137 else if (type == TypeManager.uint32_type)
138 retval = new UIntConstant ((uint) constant_value);
139 else if (type == TypeManager.int64_type)
140 retval = new LongConstant ((long) constant_value);
141 else if (type == TypeManager.uint64_type)
142 retval = new ULongConstant ((ulong) constant_value);
143 else if (type == TypeManager.float_type)
144 retval = new FloatConstant ((float) constant_value);
145 else if (type == TypeManager.double_type)
146 retval = new DoubleConstant ((double) constant_value);
147 else if (type == TypeManager.string_type)
148 retval = new StringConstant ((string) constant_value);
149 else if (type == TypeManager.short_type)
150 retval = new ShortConstant ((short) constant_value);
151 else if (type == TypeManager.ushort_type)
152 retval = new UShortConstant ((ushort) constant_value);
153 else if (type == TypeManager.sbyte_type)
154 retval = new SByteConstant ((sbyte) constant_value);
155 else if (type == TypeManager.byte_type)
156 retval = new ByteConstant ((byte) constant_value);
157 else if (type == TypeManager.char_type)
158 retval = new CharConstant ((char) constant_value);
159 else if (type == TypeManager.bool_type)
160 retval = new BoolConstant ((bool) constant_value);
161 else if (type == TypeManager.decimal_type)
162 retval = new DecimalConstant ((decimal) constant_value);
164 throw new Exception ("LookupConstantValue: Unhandled constant type: " + type);
170 /// Looks up the value of a constant field. Defines it if it hasn't
171 /// already been. Similar to LookupEnumValue in spirit.
173 public bool LookupConstantValue (out object value)
176 value = ConstantValue;
181 Report.Error (110, Location,
182 "The evaluation of the constant value for `" +
183 Name + "' involves a circular definition.");
189 int errors = Report.Errors;
192 // We might have cleared Expr ourselves in a recursive definition
199 Expr = Expr.Resolve (const_ec);
204 if (errors == Report.Errors)
205 Report.Error (150, Location, "A constant value is expected");
210 Expression real_expr = Expr;
212 Constant ce = Expr as Constant;
214 UnCheckedExpr un_expr = Expr as UnCheckedExpr;
215 CheckedExpr ch_expr = Expr as CheckedExpr;
216 EmptyCast ec_expr = Expr as EmptyCast;
218 if ((un_expr != null) && (un_expr.Expr is Constant))
220 else if ((ch_expr != null) && (ch_expr.Expr is Constant))
222 else if ((ec_expr != null) && (ec_expr.Child is Constant))
223 Expr = ec_expr.Child;
224 else if (Expr is ArrayCreation){
225 Report.Error (133, Location, "Arrays can not be constant");
227 if (errors == Report.Errors)
228 Report.Error (150, Location, "A constant value is expected");
233 ce = Expr as Constant;
236 if (MemberType != real_expr.Type) {
237 ce = ChangeType (Location, ce, MemberType);
244 ConstantValue = ce.GetValue ();
246 if (MemberType.IsEnum){
248 // This sadly does not work for our user-defined enumerations types ;-(
251 ConstantValue = System.Enum.ToObject (
252 MemberType, ConstantValue);
253 } catch (ArgumentException){
256 ".NET SDK 1.0 does not permit to create the constant "+
257 " field from a user-defined enumeration");
261 if (ce is DecimalConstant) {
262 Decimal d = ((DecimalConstant)ce).Value;
263 int[] bits = Decimal.GetBits (d);
264 object[] args = new object[] { (byte)(bits [3] >> 16), (byte)(bits [3] >> 31), (uint)bits [2], (uint)bits [1], (uint)bits [0] };
265 CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.decimal_constant_attribute_ctor, args);
266 FieldBuilder.SetCustomAttribute (cab);
269 FieldBuilder.SetConstant (ConstantValue);
272 if (!TypeManager.RegisterFieldValue (FieldBuilder, ConstantValue))
273 throw new Exception ("Cannot register const value");
275 value = ConstantValue;
282 /// Emits the field value by evaluating the expression
284 public override void Emit ()
287 LookupConstantValue (out value);
289 if (OptAttributes != null) {
290 OptAttributes.Emit (const_ec, this);