4 using System.Reflection;
5 using System.Reflection.Emit;
7 public class Constant : Expression {
13 public const int AllowedModifiers =
20 public Constant (string constant_type, string name, Expression expr, int mod_flags)
22 this.constant_type = constant_type;
25 this.mod_flags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE);
34 public string ConstantType {
40 public Expression Expr {
46 public FieldAttributes FieldAttr {
48 return FieldAttributes.Literal | Modifiers.FieldAttr (mod_flags) ;
52 public override void Resolve (TypeContainer tc)
56 public override void Emit (EmitContext ec)
61 // Defines the constant in the @parent
63 public void EmitConstant (RootContext rc, TypeContainer parent)
69 t = rc.LookupType (parent, constant_type);
73 tc = System.Type.GetTypeCode (t);
75 if ((tc == TypeCode.SByte) || (tc == TypeCode.Byte) ||
76 (tc == TypeCode.Int16) || (tc == TypeCode.UInt16) ||
77 (tc == TypeCode.Int32) || (tc == TypeCode.Int64) ||
78 (tc == TypeCode.UInt32) || (tc == TypeCode.UInt64)) {
80 } else if ((tc == TypeCode.Double) || (tc == TypeCode.Single)) {
82 } else if (tc == TypeCode.Char) {
83 } else if (tc == TypeCode.Decimal) {
85 } else if (t.IsSubclassOf (typeof (System.String))) {
87 } else if (t.IsSubclassOf (typeof (System.Enum))) {
90 rc.Report.Error (-3, "Constant type is not valid (only system types are allowed");
94 fb = parent.TypeBuilder.DefineField (name, t, FieldAttr);