2006-04-27 Jonathan Chambers <jonathan.chambers@ansys.com>
[mcs.git] / gmcs / const.cs
blob1e980404b27de7511fd506e57e431fa0fac349d0
1 //
2 // const.cs: Constant declarations.
3 //
4 // Author:
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
7 //
8 // (C) 2001 Ximian, Inc.
9 //
12 namespace Mono.CSharp {
14 using System;
15 using System.Reflection;
16 using System.Reflection.Emit;
17 using System.Collections;
19 public interface IConstant
21 void CheckObsoleteness (Location loc);
22 bool ResolveValue ();
23 Constant Value { get; }
26 public class Const : FieldMember, IConstant {
27 Constant value;
28 bool in_transit;
30 public const int AllowedModifiers =
31 Modifiers.NEW |
32 Modifiers.PUBLIC |
33 Modifiers.PROTECTED |
34 Modifiers.INTERNAL |
35 Modifiers.PRIVATE;
37 public Const (DeclSpace parent, Expression constant_type, string name,
38 Expression expr, int mod_flags, Attributes attrs, Location loc)
39 : base (parent, constant_type, mod_flags, AllowedModifiers,
40 new MemberName (name, loc), attrs)
42 initializer = expr;
43 ModFlags |= Modifiers.STATIC;
46 protected override bool CheckBase ()
48 // Constant.Define can be called when the parent type hasn't yet been populated
49 // and it's base types need not have been populated. So, we defer this check
50 // to the second time Define () is called on this member.
51 if (ParentContainer.BaseCache == null)
52 return true;
53 return base.CheckBase ();
56 /// <summary>
57 /// Defines the constant in the @parent
58 /// </summary>
59 public override bool Define ()
61 // Make Define () idempotent, but ensure that the error check happens.
62 if (FieldBuilder != null)
63 return base.CheckBase ();
65 if (!base.Define ())
66 return false;
68 Type ttype = MemberType;
69 while (ttype.IsArray)
70 ttype = TypeManager.GetElementType (ttype);
72 FieldAttributes field_attr = FieldAttributes.Static | Modifiers.FieldAttr (ModFlags);
73 // Decimals cannot be emitted into the constant blob. So, convert to 'readonly'.
74 if (ttype == TypeManager.decimal_type) {
75 field_attr |= FieldAttributes.InitOnly;
76 ParentContainer.RegisterFieldForInitialization (this);
78 else {
79 field_attr |= FieldAttributes.Literal;
82 FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType, field_attr);
84 TypeManager.RegisterConstant (FieldBuilder, this);
86 return true;
89 /// <summary>
90 /// Emits the field value by evaluating the expression
91 /// </summary>
92 public override void Emit ()
94 if (!ResolveValue ())
95 return;
97 if (value.Type == TypeManager.decimal_type) {
98 Decimal d = ((DecimalConstant)value).Value;
99 int[] bits = Decimal.GetBits (d);
100 object[] args = new object[] { (byte)(bits [3] >> 16), (byte)(bits [3] >> 31), (uint)bits [2], (uint)bits [1], (uint)bits [0] };
101 CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.decimal_constant_attribute_ctor, args);
102 FieldBuilder.SetCustomAttribute (cab);
104 else{
105 FieldBuilder.SetConstant (value.GetTypedValue ());
108 base.Emit ();
111 public static void Error_ExpressionMustBeConstant (Location loc, string e_name)
113 Report.Error (133, loc, "The expression being assigned to `{0}' must be constant", e_name);
116 public static void Error_CyclicDeclaration (MemberCore mc)
118 Report.Error (110, mc.Location, "The evaluation of the constant value for `{0}' involves a circular definition",
119 mc.GetSignatureForError ());
122 public static void Error_ConstantCanBeInitializedWithNullOnly (Location loc, string name)
124 Report.Error (134, loc, "`{0}': the constant of reference type other than string can only be initialized with null",
125 name);
128 #region IConstant Members
130 public bool ResolveValue ()
132 if (value != null)
133 return true;
135 SetMemberIsUsed ();
136 if (in_transit) {
137 Error_CyclicDeclaration (this);
138 // Suppress cyclic errors
139 value = New.Constantify (MemberType);
140 return false;
143 in_transit = true;
144 // TODO: IResolveContext here
145 EmitContext ec = new EmitContext (this, Parent, Location, null, MemberType, ModFlags);
146 value = initializer.ResolveAsConstant (ec, this);
147 in_transit = false;
149 if (value == null)
150 return false;
152 value = value.ToType (MemberType, Location);
153 if (value == null)
154 return false;
156 if (!MemberType.IsValueType && MemberType != TypeManager.string_type && !value.IsDefaultValue) {
157 Error_ConstantCanBeInitializedWithNullOnly (Location, GetSignatureForError ());
158 return false;
161 return true;
164 public Constant Value {
165 get {
166 return value;
170 #endregion
173 public class ExternalConstant : IConstant
175 FieldInfo fi;
176 Constant value;
178 public ExternalConstant (FieldInfo fi)
180 this.fi = fi;
183 private ExternalConstant (FieldInfo fi, Constant value):
184 this (fi)
186 this.value = value;
190 // Decimal constants cannot be encoded in the constant blob, and thus are marked
191 // as IsInitOnly ('readonly' in C# parlance). We get its value from the
192 // DecimalConstantAttribute metadata.
194 public static IConstant CreateDecimal (FieldInfo fi)
196 if (fi is FieldBuilder)
197 return null;
199 object[] attrs = fi.GetCustomAttributes (TypeManager.decimal_constant_attribute_type, false);
200 if (attrs.Length != 1)
201 return null;
203 IConstant ic = new ExternalConstant (fi,
204 new DecimalConstant (((System.Runtime.CompilerServices.DecimalConstantAttribute) attrs [0]).Value, Location.Null));
206 return ic;
209 #region IConstant Members
211 public void CheckObsoleteness (Location loc)
213 ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (fi);
214 if (oa == null) {
215 return;
218 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (fi), loc);
221 public bool ResolveValue ()
223 if (value != null)
224 return true;
226 if (fi.DeclaringType.IsEnum) {
227 value = Expression.Constantify (fi.GetValue (fi), TypeManager.EnumToUnderlying (fi.FieldType));
228 value = new EnumConstant (value, fi.DeclaringType);
229 return true;
232 value = Expression.Constantify (fi.GetValue (fi), fi.FieldType);
233 return true;
236 public Constant Value {
237 get {
238 return value;
242 #endregion