2 // const.cs: Constant declarations.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
12 // This is needed because the following situation arises:
14 // The FieldBuilder is declared with the real type for an enumeration
16 // When we attempt to set the value for the constant, the FieldBuilder.SetConstant
17 // function aborts because it requires its argument to be of the same type
20 namespace Mono.CSharp {
23 using System.Reflection;
24 using System.Reflection.Emit;
25 using System.Collections;
27 public class Const : FieldBase {
28 public Expression Expr;
31 bool resolved = false;
32 object ConstantValue = null;
35 bool in_transit = false;
37 public const int AllowedModifiers =
44 public Const (Expression constant_type, string name, Expression expr,
45 int mod_flags, Attributes attrs, Location loc)
46 : base (constant_type, mod_flags, AllowedModifiers,
47 new MemberName (name), null, attrs, loc)
52 public FieldAttributes FieldAttr {
54 return FieldAttributes.Literal | FieldAttributes.Static |
55 Modifiers.FieldAttr (ModFlags) ;
60 void dump_tree (Type t)
62 Console.WriteLine ("Dumping hierarchy");
64 Console.WriteLine (" " + t.FullName + " " +
65 (t.GetType ().IsEnum ? "yes" : "no"));
72 /// Defines the constant in the @parent
74 public override bool Define (TypeContainer parent)
76 type = parent.ResolveType (Type, false, Location);
81 const_ec = new EmitContext (parent, Location, null, type, ModFlags);
85 ttype = TypeManager.GetElementType (ttype);
87 if (!TypeManager.IsBuiltinType (ttype) &&
88 (!ttype.IsSubclassOf (TypeManager.enum_type))) {
91 "Constant type is not valid (only system types are allowed)");
95 Type ptype = parent.TypeBuilder.BaseType;
98 MemberList list = TypeContainer.FindMembers (
99 ptype, MemberTypes.Field, BindingFlags.Public,
100 System.Type.FilterName, Name);
103 if ((ModFlags & Modifiers.NEW) != 0)
104 WarningNotHiding (parent);
106 } else if ((ModFlags & Modifiers.NEW) != 0)
107 WarningNotHiding (parent);
109 FieldBuilder = parent.TypeBuilder.DefineField (Name, type, FieldAttr);
111 TypeManager.RegisterConstant (FieldBuilder, this);
117 // Changes the type of the constant expression `expr' to the Type `type'
118 // Returns null on failure.
120 public static Constant ChangeType (Location loc, Constant expr, Type type)
122 if (type == TypeManager.object_type)
127 // from the null type to any reference-type.
128 if (expr is NullLiteral && !type.IsValueType && !TypeManager.IsEnumType (type))
129 return NullLiteral.Null;
131 if (!Convert.ImplicitStandardConversionExists (expr, type)){
132 Convert.Error_CannotImplicitConversion (loc, expr.Type, type);
136 object constant_value = TypeManager.ChangeType (expr.GetValue (), type, out fail);
138 Convert.Error_CannotImplicitConversion (loc, expr.Type, type);
141 // We should always catch the error before this is ever
142 // reached, by calling Convert.ImplicitStandardConversionExists
144 throw new Exception (
145 String.Format ("LookupConstantValue: This should never be reached {0} {1}", expr.Type, type));
149 if (type == TypeManager.int32_type)
150 retval = new IntConstant ((int) constant_value);
151 else if (type == TypeManager.uint32_type)
152 retval = new UIntConstant ((uint) constant_value);
153 else if (type == TypeManager.int64_type)
154 retval = new LongConstant ((long) constant_value);
155 else if (type == TypeManager.uint64_type)
156 retval = new ULongConstant ((ulong) constant_value);
157 else if (type == TypeManager.float_type)
158 retval = new FloatConstant ((float) constant_value);
159 else if (type == TypeManager.double_type)
160 retval = new DoubleConstant ((double) constant_value);
161 else if (type == TypeManager.string_type)
162 retval = new StringConstant ((string) constant_value);
163 else if (type == TypeManager.short_type)
164 retval = new ShortConstant ((short) constant_value);
165 else if (type == TypeManager.ushort_type)
166 retval = new UShortConstant ((ushort) constant_value);
167 else if (type == TypeManager.sbyte_type)
168 retval = new SByteConstant ((sbyte) constant_value);
169 else if (type == TypeManager.byte_type)
170 retval = new ByteConstant ((byte) constant_value);
171 else if (type == TypeManager.char_type)
172 retval = new CharConstant ((char) constant_value);
173 else if (type == TypeManager.bool_type)
174 retval = new BoolConstant ((bool) constant_value);
176 throw new Exception ("LookupConstantValue: Unhandled constant type: " + type);
182 /// Looks up the value of a constant field. Defines it if it hasn't
183 /// already been. Similar to LookupEnumValue in spirit.
185 public bool LookupConstantValue (out object value)
188 value = ConstantValue;
193 Report.Error (110, Location,
194 "The evaluation of the constant value for `" +
195 Name + "' involves a circular definition.");
201 int errors = Report.Errors;
204 // We might have cleared Expr ourselves in a recursive definition
211 Expr = Expr.Resolve (const_ec);
216 if (errors == Report.Errors)
217 Report.Error (150, Location, "A constant value is expected");
222 Expression real_expr = Expr;
224 Constant ce = Expr as Constant;
226 UnCheckedExpr un_expr = Expr as UnCheckedExpr;
227 CheckedExpr ch_expr = Expr as CheckedExpr;
228 EmptyCast ec_expr = Expr as EmptyCast;
230 if ((un_expr != null) && (un_expr.Expr is Constant))
232 else if ((ch_expr != null) && (ch_expr.Expr is Constant))
234 else if ((ec_expr != null) && (ec_expr.Child is Constant))
235 Expr = ec_expr.Child;
236 else if (Expr is ArrayCreation) {
237 ArrayCreation ac = (ArrayCreation) Expr;
239 Expr = ac.TurnIntoConstant ();
241 Report.Error (150, Location, "A constant value is expected");
246 if (errors == Report.Errors)
247 Report.Error (150, Location, "A constant value is expected");
252 ce = Expr as Constant;
255 if (type != real_expr.Type) {
256 ce = ChangeType (Location, ce, type);
263 ConstantValue = ce.GetValue ();
267 // This sadly does not work for our user-defined enumerations types ;-(
270 ConstantValue = System.Enum.ToObject (
271 type, ConstantValue);
272 } catch (ArgumentException){
275 ".NET SDK 1.0 does not permit to create the constant "+
276 " field from a user-defined enumeration");
280 FieldBuilder.SetConstant (ConstantValue);
282 if (!TypeManager.RegisterFieldValue (FieldBuilder, ConstantValue))
283 throw new Exception ("Cannot register const value");
285 value = ConstantValue;
292 /// Emits the field value by evaluating the expression
294 public override void Emit (TypeContainer parent)
297 LookupConstantValue (out value);
299 if (OptAttributes != null) {
300 OptAttributes.Emit (const_ec, this);