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