2001-10-12 Ravi Pratap <ravi@ximian.com>
[mono.git] / mcs / mcs / constant.cs
1 namespace CIR {
2
3         using System;
4         using System.Reflection;
5         using System.Reflection.Emit;
6         using System.Collections;
7         
8         public class Constant : Expression {
9                 string     name;
10                 Expression expr;
11                 string     constant_type;
12                 int        mod_flags;
13                 public Attributes  OptAttributes;
14
15                 public const int AllowedModifiers =
16                         Modifiers.NEW |
17                         Modifiers.PUBLIC |
18                         Modifiers.PROTECTED |
19                         Modifiers.INTERNAL |
20                         Modifiers.PRIVATE;
21
22                 public Constant (string constant_type, string name, Expression expr, int mod_flags, Attributes attrs)
23                 {
24                         this.constant_type = constant_type;
25                         this.name = name;
26                         this.expr = expr;
27                         this.mod_flags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE);
28                         OptAttributes = attrs;
29                 }
30
31                 public string Name {
32                         get {
33                                 return name;
34                         }
35                 }
36
37                 public string ConstantType {
38                         get {
39                                 return constant_type;
40                         }
41                 }
42
43                 public Expression Expr {
44                         get {
45                                 return expr;
46                         }
47                 }
48
49                 public FieldAttributes FieldAttr {
50                         get {
51                                 return FieldAttributes.Literal | Modifiers.FieldAttr (mod_flags) ;
52                         }
53                 }
54
55                 public override Expression DoResolve (EmitContext ec)
56                 {
57                         // FIXME: implement
58                         return this;
59                 }
60
61                 public override void Emit (EmitContext ec)
62                 {
63                         throw new Exception ("Unimplemented");
64                 }
65                        
66                 // <summary>
67                 //   Defines the constant in the @parent
68                 // </summary>
69                 public void EmitConstant (RootContext rc, TypeContainer parent)
70                 {
71                         FieldBuilder fb;
72                         TypeCode tc;
73                         Type t;
74                         
75                         t = rc.LookupType (parent, constant_type);
76                         if (t == null)
77                                 return;
78
79                         tc = System.Type.GetTypeCode (t);
80                         
81                         if ((tc == TypeCode.SByte)  || (tc == TypeCode.Byte)   ||
82                             (tc == TypeCode.Int16)  || (tc == TypeCode.UInt16) ||
83                             (tc == TypeCode.Int32)  || (tc == TypeCode.Int64)  ||
84                             (tc == TypeCode.UInt32) || (tc == TypeCode.UInt64)) {
85                                 
86                         } else if ((tc == TypeCode.Double) || (tc == TypeCode.Single)) {
87
88                         } else if (tc == TypeCode.Char) {
89                         } else if (tc == TypeCode.Decimal) {
90
91                         } else if (t.IsSubclassOf (typeof (System.String))) {
92
93                         } else if (t.IsSubclassOf (typeof (System.Enum))) {
94
95                         } else {
96                                 Report.Error (-3, "Constant type is not valid (only system types are allowed");
97                                 return;
98                         }
99
100                         fb = parent.TypeBuilder.DefineField (name, t, FieldAttr);
101                         
102                 }
103         }
104 }
105
106