Add setting of The WRAP flag in the Wrap property's set method.
[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 : MemberBase {
28                 public Expression ConstantType;
29                 public Expression Expr;
30                 public FieldBuilder FieldBuilder;
31                 EmitContext const_ec;
32
33                 object ConstantValue = null;
34                 Type type;
35
36                 bool in_transit = false;
37
38                 public const int AllowedModifiers =
39                         Modifiers.NEW |
40                         Modifiers.PUBLIC |
41                         Modifiers.PROTECTED |
42                         Modifiers.INTERNAL |
43                         Modifiers.PRIVATE;
44
45                 public Const (Expression constant_type, string name, Expression expr, int mod_flags,
46                               Attributes attrs, Location loc)
47                         : base (constant_type, mod_flags, AllowedModifiers, Modifiers.PRIVATE, name, attrs, loc)
48                 {
49                         ConstantType = constant_type;
50                         Name = name;
51                         Expr = expr;
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                         const_ec = new EmitContext (parent, Location, null, type, ModFlags);
84                         
85                         if (!TypeManager.IsBuiltinType (type) &&
86                             (!type.IsSubclassOf (TypeManager.enum_type))) {
87                                 Report.Error (
88                                         -3, Location,
89                                         "Constant type is not valid (only system types are allowed)");
90                                 return false;
91                         }
92
93                         Type ptype = parent.TypeBuilder.BaseType;
94
95                         if (ptype != null) {
96                                 MemberList list = TypeContainer.FindMembers (
97                                         ptype, MemberTypes.Field, BindingFlags.Public,
98                                         System.Type.FilterName, Name);
99                                 
100                                 if (list.Count == 0)
101                                         if ((ModFlags & Modifiers.NEW) != 0)
102                                                 WarningNotHiding (parent);
103
104                         } else if ((ModFlags & Modifiers.NEW) != 0)
105                                 WarningNotHiding (parent);
106
107                         FieldBuilder = parent.TypeBuilder.DefineField (Name, type, FieldAttr);
108
109                         TypeManager.RegisterConstant (FieldBuilder, this);
110
111                         return true;
112                 }
113
114                 //
115                 // Changes the type of the constant expression `expr' to the Type `type'
116                 // Returns null on failure.
117                 //
118                 public static Constant ChangeType (Location loc, Constant expr, Type type)
119                 {
120                         bool fail;
121
122                         // from the null type to any reference-type.
123                         if (expr is NullLiteral && !type.IsValueType && !TypeManager.IsEnumType (type))
124                                 return NullLiteral.Null;
125
126                         if (!Convert.ImplicitStandardConversionExists (expr, type)){
127                                 Convert.Error_CannotImplicitConversion (loc, expr.Type, type);
128                                 return null;
129                         }
130                         
131                         object constant_value = TypeManager.ChangeType (expr.GetValue (), type, out fail);
132                         if (fail){
133                                 Convert.Error_CannotImplicitConversion (loc, expr.Type, type);
134                                 
135                                 //
136                                 // We should always catch the error before this is ever
137                                 // reached, by calling Convert.ImplicitStandardConversionExists
138                                 //
139                                 throw new Exception (
140                                                      String.Format ("LookupConstantValue: This should never be reached {0} {1}", expr.Type, type));
141                         }
142
143                         Constant retval;
144                         if (type == TypeManager.int32_type)
145                                 retval = new IntConstant ((int) constant_value);
146                         else if (type == TypeManager.uint32_type)
147                                 retval = new UIntConstant ((uint) constant_value);
148                         else if (type == TypeManager.int64_type)
149                                 retval = new LongConstant ((long) constant_value);
150                         else if (type == TypeManager.uint64_type)
151                                 retval = new ULongConstant ((ulong) constant_value);
152                         else if (type == TypeManager.float_type)
153                                 retval = new FloatConstant ((float) constant_value);
154                         else if (type == TypeManager.double_type)
155                                 retval = new DoubleConstant ((double) constant_value);
156                         else if (type == TypeManager.string_type)
157                                 retval = new StringConstant ((string) constant_value);
158                         else if (type == TypeManager.short_type)
159                                 retval = new ShortConstant ((short) constant_value);
160                         else if (type == TypeManager.ushort_type)
161                                 retval = new UShortConstant ((ushort) constant_value);
162                         else if (type == TypeManager.sbyte_type)
163                                 retval = new SByteConstant ((sbyte) constant_value);
164                         else if (type == TypeManager.byte_type)
165                                 retval = new ByteConstant ((byte) constant_value);
166                         else if (type == TypeManager.char_type)
167                                 retval = new CharConstant ((char) constant_value);
168                         else if (type == TypeManager.bool_type)
169                                 retval = new BoolConstant ((bool) constant_value);
170                         else
171                                 throw new Exception ("LookupConstantValue: Unhandled constant type: " + type);
172                         
173                         return retval;
174                 }
175                 
176                 /// <summary>
177                 ///  Looks up the value of a constant field. Defines it if it hasn't
178                 ///  already been. Similar to LookupEnumValue in spirit.
179                 /// </summary>
180                 public object LookupConstantValue ()
181                 {
182                         if (ConstantValue != null)
183                                 return ConstantValue;
184
185                         if (in_transit) {
186                                 Report.Error (110, Location,
187                                               "The evaluation of the constant value for `" +
188                                               Name + "' involves a circular definition.");
189                                 return null;
190                         }
191
192                         in_transit = true;
193                         int errors = Report.Errors;
194
195                         Expr = Expr.Resolve (const_ec);
196
197                         in_transit = false;
198
199                         if (Expr == null) {
200                                 if (errors == Report.Errors)
201                                         Report.Error (150, Location, "A constant value is expected");
202                                 return null;
203                         }
204
205                         Constant ce = Expr as Constant;
206                         if (ce == null){
207                                 UnCheckedExpr un_expr = Expr as UnCheckedExpr;
208                                 CheckedExpr ch_expr = Expr as CheckedExpr;
209
210                                 if ((un_expr != null) && (un_expr.Expr is Constant))
211                                         Expr = un_expr.Expr;
212                                 else if ((ch_expr != null) && (ch_expr.Expr is Constant))
213                                         Expr = ch_expr.Expr;
214                                 else {
215                                         if (errors == Report.Errors)
216                                                 Report.Error (150, Location, "A constant value is expected");
217                                         return null;
218                                 }
219                         }
220
221                         if (type != ce.Type) {
222                                 ce = ChangeType (Location, ce, type);
223                                 if (ce == null)
224                                         return null;
225                                 Expr = ce;
226                         }
227                         ConstantValue = ce.GetValue ();
228
229                         if (type.IsEnum){
230                                 //
231                                 // This sadly does not work for our user-defined enumerations types ;-(
232                                 //
233                                 try {
234                                         ConstantValue = System.Enum.ToObject (
235                                                 type, ConstantValue);
236                                 } catch (ArgumentException){
237                                         Report.Error (
238                                                 -16, Location,
239                                                 ".NET SDK 1.0 does not permit to create the constant "+
240                                                 " field from a user-defined enumeration");
241                                 }
242                         }
243
244                         FieldBuilder.SetConstant (ConstantValue);
245
246                         if (!TypeManager.RegisterFieldValue (FieldBuilder, ConstantValue))
247                                 return null;
248
249                         return ConstantValue;
250                 }
251                 
252                 
253                 /// <summary>
254                 ///  Emits the field value by evaluating the expression
255                 /// </summary>
256                 public void EmitConstant (TypeContainer parent)
257                 {
258                         LookupConstantValue ();
259                         
260                         return;
261                 }
262         }
263 }
264
265