2009-01-02 Ivan N. Zlatev <contact@i-nz.net>
[mono.git] / mcs / mcs / const.cs
index 73c7deabd83a07ee7fed1587d0d348edacf0375e..1f082adb94bee259b43431e0a123ac1446d66985 100644 (file)
@@ -5,8 +5,8 @@
 //   Miguel de Icaza (miguel@ximian.com)
 //   Marek Safar (marek.safar@seznam.cz)
 //
-// (C) 2001 Ximian, Inc.
-//
+// Copyright 2001-2003 Ximian, Inc.
+// Copyright 2003-2008 Novell, Inc.
 //
 
 namespace Mono.CSharp {
@@ -36,9 +36,9 @@ namespace Mono.CSharp {
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE;
 
-               public Const (DeclSpace parent, Expression constant_type, string name,
+               public Const (DeclSpace parent, FullNamedExpression type, string name,
                              Expression expr, int mod_flags, Attributes attrs, Location loc)
-                       : base (parent, constant_type, mod_flags, AllowedModifiers,
+                       : base (parent, type, mod_flags, AllowedModifiers,
                                new MemberName (name, loc), attrs)
                {
                        initializer = expr;
@@ -74,7 +74,6 @@ namespace Mono.CSharp {
                        Type ttype = MemberType;
                        if (!IsConstantTypeValid (ttype)) {
                                Error_InvalidConstantType (ttype, Location);
-                               return false;
                        }
 
                        // If the constant is private then we don't need any field the
@@ -82,9 +81,6 @@ namespace Mono.CSharp {
                        //if ((ModFlags & Modifiers.PRIVATE) != 0 && RootContext.Optimize)
                        //      return true;
 
-                       while (ttype.IsArray)
-                               ttype = TypeManager.GetElementType (ttype);
-
                        FieldAttributes field_attr = FieldAttributes.Static | Modifiers.FieldAttr (ModFlags);
                        // Decimals cannot be emitted into the constant blob.  So, convert to 'readonly'.
                        if (ttype == TypeManager.decimal_type) {
@@ -97,9 +93,9 @@ namespace Mono.CSharp {
                        TypeManager.RegisterConstant (FieldBuilder, this);
                        Parent.MemberCache.AddMember (FieldBuilder, this);
 
-                       if (ttype == TypeManager.decimal_type)
+                       if ((field_attr & FieldAttributes.InitOnly) != 0)
                                Parent.PartialContainer.RegisterFieldForInitialization (this,
-                                       new FieldInitializer (FieldBuilder, initializer));
+                                       new FieldInitializer (FieldBuilder, initializer, this));
 
                        return true;
                }
@@ -183,10 +179,10 @@ namespace Mono.CSharp {
                                mc.GetSignatureForError ());
                }
 
-               public static void Error_ConstantCanBeInitializedWithNullOnly (Location loc, string name)
+               public static void Error_ConstantCanBeInitializedWithNullOnly (Type type, Location loc, string name)
                {
-                       Report.Error (134, loc, "`{0}': the constant of reference type other than string can only be initialized with null",
-                               name);
+                       Report.Error (134, loc, "A constant `{0}' of reference type `{1}' can only be initialized with null",
+                               name, TypeManager.CSharpName (type));
                }
 
                public static void Error_InvalidConstantType (Type t, Location loc)
@@ -230,10 +226,10 @@ namespace Mono.CSharp {
 
                        Constant c = value.ConvertImplicitly (MemberType);
                        if (c == null) {
-                               if (!MemberType.IsValueType && MemberType != TypeManager.string_type && !value.IsDefaultValue)
-                                       Error_ConstantCanBeInitializedWithNullOnly (Location, GetSignatureForError ());
+                               if (TypeManager.IsReferenceType (MemberType))
+                                       Error_ConstantCanBeInitializedWithNullOnly (MemberType, Location, GetSignatureForError ());
                                else
-                                       value.Error_ValueCannotBeConverted (null, Location, MemberType, false);
+                                       value.Error_ValueCannotBeConverted (ec, Location, MemberType, false);
                        }
 
                        return c;