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