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 : MemberBase {
28 public Expression ConstantType;
29 public Expression Expr;
30 public FieldBuilder FieldBuilder;
33 object ConstantValue = null;
36 bool in_transit = false;
38 public const int AllowedModifiers =
45 public Const (Expression constant_type, string name, Expression expr, int mod_flags,
46 Attributes attrs, Location loc)
47 : base (constant_type, mod_flags, AllowedModifiers, Modifiers.PRIVATE, name, attrs, loc)
49 ConstantType = constant_type;
54 public FieldAttributes FieldAttr {
56 return FieldAttributes.Literal | FieldAttributes.Static |
57 Modifiers.FieldAttr (ModFlags) ;
62 void dump_tree (Type t)
64 Console.WriteLine ("Dumping hierarchy");
66 Console.WriteLine (" " + t.FullName + " " +
67 (t.GetType ().IsEnum ? "yes" : "no"));
74 /// Defines the constant in the @parent
76 public override bool Define (TypeContainer parent)
78 type = parent.ResolveType (ConstantType, false, Location);
83 const_ec = new EmitContext (parent, Location, null, type, ModFlags);
85 if (!TypeManager.IsBuiltinType (type) &&
86 (!type.IsSubclassOf (TypeManager.enum_type))) {
89 "Constant type is not valid (only system types are allowed)");
93 Type ptype = parent.TypeBuilder.BaseType;
96 MemberList list = TypeContainer.FindMembers (
97 ptype, MemberTypes.Field, BindingFlags.Public,
98 System.Type.FilterName, Name);
101 if ((ModFlags & Modifiers.NEW) != 0)
102 WarningNotHiding (parent);
104 } else if ((ModFlags & Modifiers.NEW) != 0)
105 WarningNotHiding (parent);
107 FieldBuilder = parent.TypeBuilder.DefineField (Name, type, FieldAttr);
109 TypeManager.RegisterConstant (FieldBuilder, this);
115 /// Looks up the value of a constant field. Defines it if it hasn't
116 /// already been. Similar to LookupEnumValue in spirit.
118 public object LookupConstantValue ()
120 if (ConstantValue != null)
121 return ConstantValue;
124 Report.Error (110, Location,
125 "The evaluation of the constant value for `" +
126 Name + "' involves a circular definition.");
131 int errors = Report.Errors;
133 Expr = Expr.Resolve (const_ec);
138 if (errors == Report.Errors)
139 Report.Error (150, Location, "A constant value is expected");
143 if (!(Expr is Constant)) {
144 UnCheckedExpr un_expr = Expr as UnCheckedExpr;
145 CheckedExpr ch_expr = Expr as CheckedExpr;
147 if ((un_expr != null) && (un_expr.Expr is Constant))
149 else if ((ch_expr != null) && (ch_expr.Expr is Constant))
152 if (errors == Report.Errors)
153 Report.Error (150, Location, "A constant value is expected");
158 ConstantValue = ((Constant) Expr).GetValue ();
160 if (type != Expr.Type) {
163 // from the null type to any reference-type.
164 if (Expr is NullLiteral && !type.IsValueType &&
165 !TypeManager.IsEnumType (type)){
166 return NullLiteral.Null;
169 ConstantValue = TypeManager.ChangeType (ConstantValue, type, out fail);
171 Convert.Error_CannotImplicitConversion (Location, Expr.Type, type);
175 if (type == TypeManager.int32_type)
176 Expr = new IntConstant ((int) ConstantValue);
177 else if (type == TypeManager.uint32_type)
178 Expr = new UIntConstant ((uint) ConstantValue);
179 else if (type == TypeManager.int64_type)
180 Expr = new LongConstant ((long) ConstantValue);
181 else if (type == TypeManager.uint64_type)
182 Expr = new ULongConstant ((ulong) ConstantValue);
183 else if (type == TypeManager.float_type)
184 Expr = new FloatConstant ((float) ConstantValue);
185 else if (type == TypeManager.double_type)
186 Expr = new DoubleConstant ((double) ConstantValue);
187 else if (type == TypeManager.string_type)
188 Expr = new StringConstant ((string) ConstantValue);
189 else if (type == TypeManager.short_type)
190 Expr = new ShortConstant ((short) ConstantValue);
191 else if (type == TypeManager.ushort_type)
192 Expr = new UShortConstant ((ushort) ConstantValue);
193 else if (type == TypeManager.sbyte_type)
194 Expr = new SByteConstant ((sbyte) ConstantValue);
195 else if (type == TypeManager.byte_type)
196 Expr = new ByteConstant ((byte) ConstantValue);
197 else if (type == TypeManager.char_type)
198 Expr = new CharConstant ((char) ConstantValue);
199 else if (type == TypeManager.bool_type)
200 Expr = new BoolConstant ((bool) ConstantValue);
205 // This sadly does not work for our user-defined enumerations types ;-(
208 ConstantValue = System.Enum.ToObject (
209 type, ConstantValue);
210 } catch (ArgumentException){
213 ".NET SDK 1.0 does not permit to create the constant "+
214 " field from a user-defined enumeration");
218 FieldBuilder.SetConstant (ConstantValue);
220 if (!TypeManager.RegisterFieldValue (FieldBuilder, ConstantValue))
223 return ConstantValue;
228 /// Emits the field value by evaluating the expression
230 public void EmitConstant (TypeContainer parent)
232 LookupConstantValue ();