2007-02-06 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / const.cs
index b947e4d68f5485ee6552e1e4abb33d32d71d8459..3e1e9c37dc62966de5c1d3ec78719cbf6bfbb1c0 100644 (file)
@@ -23,7 +23,7 @@ namespace Mono.CSharp {
                Constant CreateConstantReference (Location loc);
        }
 
-       public class Const : FieldMember, IConstant {
+       public class Const : FieldBase, IConstant {
                Constant value;
                bool in_transit;
                bool define_called;
@@ -83,15 +83,16 @@ namespace Mono.CSharp {
                        // Decimals cannot be emitted into the constant blob.  So, convert to 'readonly'.
                        if (ttype == TypeManager.decimal_type) {
                                field_attr |= FieldAttributes.InitOnly;
-                               Parent.PartialContainer.RegisterFieldForInitialization (this);
                        } else {
                                field_attr |= FieldAttributes.Literal;
                        }
 
                        FieldBuilder = Parent.TypeBuilder.DefineField (Name, MemberType, field_attr);
-
                        TypeManager.RegisterConstant (FieldBuilder, this);
 
+                       if (ttype == TypeManager.decimal_type)
+                               Parent.PartialContainer.RegisterFieldForInitialization (this, new FieldInitializer (FieldBuilder, initializer));
+
                        return true;
                }
 
@@ -177,15 +178,16 @@ namespace Mono.CSharp {
                        if (value == null)
                                return false;
 
-                       value = value.ImplicitConversionRequired (MemberType, Location);
-                       if (value == null)
-                               return false;
-
-                       if (!MemberType.IsValueType && MemberType != TypeManager.string_type && !value.IsDefaultValue) {
-                               Error_ConstantCanBeInitializedWithNullOnly (Location, GetSignatureForError ());
+                       Constant c  = value.ConvertImplicitly (MemberType);
+                       if (c == null) {
+                               if (!MemberType.IsValueType && MemberType != TypeManager.string_type && !value.IsDefaultValue)
+                                       Error_ConstantCanBeInitializedWithNullOnly (Location, GetSignatureForError ());
+                               else
+                                       value.Error_ValueCannotBeConverted (null, Location, MemberType, false);
                                return false;
                        }
 
+                       value = c;
                        return true;
                }