2002-07-22 Tim Coleman <tim@timcoleman.com>
[mono.git] / mcs / mcs / const.cs
1 //
2 // const.cs: Constant declarations.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
9 //
10
11 //
12 // This is needed because the following situation arises:
13 //
14 //     The FieldBuilder is declared with the real type for an enumeration
15 //
16 //     When we attempt to set the value for the constant, the FieldBuilder.SetConstant
17 //     function aborts because it requires its argument to be of the same type
18 //
19
20 namespace Mono.CSharp {
21
22         using System;
23         using System.Reflection;
24         using System.Reflection.Emit;
25         using System.Collections;
26
27         public class Const : MemberCore {
28                 public Expression ConstantType;
29                 public Expression Expr;
30                 public Attributes  OptAttributes;
31                 public FieldBuilder FieldBuilder;
32
33                 object ConstantValue = null;
34                 Type type;
35
36                 public const int AllowedModifiers =
37                         Modifiers.NEW |
38                         Modifiers.PUBLIC |
39                         Modifiers.PROTECTED |
40                         Modifiers.INTERNAL |
41                         Modifiers.PRIVATE;
42
43                 public Const (Expression constant_type, string name, Expression expr, int mod_flags,
44                               Attributes attrs, Location loc)
45                         : base (name, loc)
46                 {
47                         ConstantType = constant_type;
48                         Name = name;
49                         Expr = expr;
50                         ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE, loc);
51                         OptAttributes = attrs;
52                 }
53
54                 public FieldAttributes FieldAttr {
55                         get {
56                                 return FieldAttributes.Literal | FieldAttributes.Static |
57                                         Modifiers.FieldAttr (ModFlags) ;
58                         }
59                 }
60
61 #if DEBUG
62                 void dump_tree (Type t)
63                 {
64                         Console.WriteLine ("Dumping hierarchy");
65                         while (t != null){
66                                 Console.WriteLine ("   " + t.FullName + " " +
67                                         (t.GetType ().IsEnum ? "yes" : "no"));
68                                 t = t.BaseType;
69                         }
70                 }
71 #endif
72
73                 /// <summary>
74                 ///   Defines the constant in the @parent
75                 /// </summary>
76                 public override bool Define (TypeContainer parent)
77                 {
78                         type = parent.ResolveType (ConstantType, false, Location);
79
80                         if (type == null)
81                                 return false;
82
83                         if (!TypeManager.IsBuiltinType (type) &&
84                             (!type.IsSubclassOf (TypeManager.enum_type))) {
85                                 Report.Error (
86                                         -3, Location,
87                                         "Constant type is not valid (only system types are allowed)");
88                                 return false;
89                         }
90
91                         Type ptype = parent.TypeBuilder.BaseType;
92
93                         if (ptype != null) {
94                                 MemberInfo [] mi = TypeContainer.FindMembers (
95                                         ptype, MemberTypes.Field, BindingFlags.Public,
96                                         Type.FilterName, Name);
97                                 
98                                 if (mi == null || mi.Length == 0)
99                                         if ((ModFlags & Modifiers.NEW) != 0)
100                                                 WarningNotHiding (parent);
101
102                         } else if ((ModFlags & Modifiers.NEW) != 0)
103                                 WarningNotHiding (parent);
104
105                         FieldBuilder = parent.TypeBuilder.DefineField (Name, type, FieldAttr);
106
107                         TypeManager.RegisterConstant (FieldBuilder, this);
108
109                         return true;
110                 }
111
112                 /// <summary>
113                 ///  Looks up the value of a constant field. Defines it if it hasn't
114                 ///  already been. Similar to LookupEnumValue in spirit.
115                 /// </summary>
116                 public object LookupConstantValue (EmitContext ec)
117                 {
118                         if (ConstantValue != null)
119                                 return ConstantValue;
120
121                         Expr = Expr.Resolve (ec);
122
123                         if (Expr == null) {
124                                 Report.Error (150, Location, "A constant value is expected");
125                                 return null;
126                         }
127
128                         if (!(Expr is Constant)) {
129                                 UnCheckedExpr un_expr = Expr as UnCheckedExpr;
130                                 CheckedExpr ch_expr = Expr as CheckedExpr;
131
132                                 if ((un_expr != null) && (un_expr.Expr is Constant))
133                                         Expr = un_expr.Expr;
134                                 else if ((ch_expr != null) && (ch_expr.Expr is Constant))
135                                         Expr = ch_expr.Expr;
136                                 else {
137                                         Report.Error (150, Location, "A constant value is expected");
138                                         return null;
139                                 }
140                         }
141
142                         ConstantValue = ((Constant) Expr).GetValue ();
143
144                         if (type != Expr.Type) {
145                                 try {
146                                         ConstantValue = TypeManager.ChangeType (ConstantValue, type);
147                                 } catch {
148                                         Expression.Error_CannotConvertImplicit (Location, Expr.Type, type);
149                                         return null;
150                                 }
151
152                                 if (type == TypeManager.int32_type)
153                                         Expr = new IntConstant ((int) ConstantValue);
154                                 else if (type == TypeManager.uint32_type)
155                                         Expr = new UIntConstant ((uint) ConstantValue);
156                                 else if (type == TypeManager.int64_type)
157                                         Expr = new LongConstant ((long) ConstantValue);
158                                 else if (type == TypeManager.uint64_type)
159                                         Expr = new ULongConstant ((ulong) ConstantValue);
160                                 else if (type == TypeManager.float_type)
161                                         Expr = new FloatConstant ((float) ConstantValue);
162                                 else if (type == TypeManager.double_type)
163                                         Expr = new DoubleConstant ((double) ConstantValue);
164                                 else if (type == TypeManager.string_type)
165                                         Expr = new StringConstant ((string) ConstantValue);
166                                 else if (type == TypeManager.short_type)
167                                         Expr = new ShortConstant ((short) ConstantValue);
168                                 else if (type == TypeManager.ushort_type)
169                                         Expr = new UShortConstant ((ushort) ConstantValue);
170                                 else if (type == TypeManager.sbyte_type)
171                                         Expr = new SByteConstant ((sbyte) ConstantValue);
172                                 else if (type == TypeManager.byte_type)
173                                         Expr = new ByteConstant ((byte) ConstantValue);
174                                 else if (type == TypeManager.char_type)
175                                         Expr = new CharConstant ((char) ConstantValue);
176                                 else if (type == TypeManager.bool_type)
177                                         Expr = new BoolConstant ((bool) ConstantValue);
178                         }
179
180                         if (type.IsEnum){
181                                 //
182                                 // This sadly does not work for our user-defined enumerations types ;-(
183                                 //
184                                 try {
185                                         ConstantValue = System.Enum.ToObject (
186                                                 type, ConstantValue);
187                                 } catch (ArgumentException){
188                                         Report.Error (
189                                                 -16, Location,
190                                                 ".NET SDK 1.0 does not permit to create the constant "+
191                                                 " field from a user-defined enumeration");
192                                 }
193                         }
194
195                         FieldBuilder.SetConstant (ConstantValue);
196
197                         if (!TypeManager.RegisterFieldValue (FieldBuilder, ConstantValue))
198                                 return null;
199
200                         return ConstantValue;
201                 }
202                 
203                 
204                 /// <summary>
205                 ///  Emits the field value by evaluating the expression
206                 /// </summary>
207                 public void EmitConstant (TypeContainer parent)
208                 {
209                         EmitContext ec = new EmitContext (parent, Location, null, type, ModFlags);
210                         LookupConstantValue (ec);
211                         
212                         return;
213                 }
214         }
215 }
216
217