2 // constant.cs: Constant expressions and constant folding.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Collections;
18 public class Constant : Expression {
23 public Attributes OptAttributes;
25 public const int AllowedModifiers =
32 public Constant (string constant_type, string name, Expression expr, int mod_flags, Attributes attrs)
34 this.constant_type = constant_type;
37 this.mod_flags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE);
38 OptAttributes = attrs;
47 public string ConstantType {
53 public Expression Expr {
59 public FieldAttributes FieldAttr {
61 return FieldAttributes.Literal | Modifiers.FieldAttr (mod_flags) ;
65 public override Expression DoResolve (EmitContext ec)
71 public override void Emit (EmitContext ec)
73 throw new Exception ("Unimplemented");
77 // Defines the constant in the @parent
79 public void EmitConstant (RootContext rc, TypeContainer parent)
85 t = rc.LookupType (parent, constant_type);
89 tc = System.Type.GetTypeCode (t);
91 if ((tc == TypeCode.SByte) || (tc == TypeCode.Byte) ||
92 (tc == TypeCode.Int16) || (tc == TypeCode.UInt16) ||
93 (tc == TypeCode.Int32) || (tc == TypeCode.Int64) ||
94 (tc == TypeCode.UInt32) || (tc == TypeCode.UInt64)) {
96 } else if ((tc == TypeCode.Double) || (tc == TypeCode.Single)) {
98 } else if (tc == TypeCode.Char) {
99 } else if (tc == TypeCode.Decimal) {
101 } else if (t.IsSubclassOf (typeof (System.String))) {
103 } else if (t.IsSubclassOf (typeof (System.Enum))) {
106 Report.Error (-3, "Constant type is not valid (only system types are allowed");
110 fb = parent.TypeBuilder.DefineField (name, t, FieldAttr);