2007-11-16 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / const.cs
index 7422b492b3f23be40c2771edca882855dc315718..de881fbc2c102a34c30ca7c382605c243e649050 100644 (file)
@@ -20,12 +20,13 @@ namespace Mono.CSharp {
        {
                void CheckObsoleteness (Location loc);
                bool ResolveValue ();
-               Constant Value { get; }
+               Constant CreateConstantReference (Location loc);
        }
 
-       public class Const : FieldMember, IConstant {
-               Constant value;
+       public class Const : FieldBase, IConstant {
+               protected Constant value;
                bool in_transit;
+               bool resolved;
                bool define_called;
 
                public const int AllowedModifiers =
@@ -76,21 +77,29 @@ namespace Mono.CSharp {
                                return false;
                        }
 
+                       // If the constant is private then we don't need any field the
+                       // value is already inlined and cannot be referenced
+                       //if ((ModFlags & Modifiers.PRIVATE) != 0 && RootContext.Optimize)
+                       //      return true;
+
                        while (ttype.IsArray)
-                           ttype = TypeManager.GetElementType (ttype);
+                               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) {
                                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);
+                       Parent.MemberCache.AddMember (FieldBuilder, this);
+
+                       if (ttype == TypeManager.decimal_type)
+                               Parent.PartialContainer.RegisterFieldForInitialization (this,
+                                       new FieldInitializer (FieldBuilder, initializer));
 
                        return true;
                }
@@ -117,6 +126,9 @@ namespace Mono.CSharp {
                        if (!ResolveValue ())
                                return;
 
+                       if (FieldBuilder == null)
+                               return;
+
                        if (value.Type == TypeManager.decimal_type) {
                                Decimal d = ((DecimalConstant)value).Value;
                                int[] bits = Decimal.GetBits (d);
@@ -157,42 +169,53 @@ namespace Mono.CSharp {
 
                public bool ResolveValue ()
                {
-                       if (value != null)
-                               return true;
+                       if (resolved)
+                               return value != null;
 
                        SetMemberIsUsed ();
                        if (in_transit) {
                                Error_CyclicDeclaration (this);
                                // Suppress cyclic errors
                                value = New.Constantify (MemberType);
+                               resolved = true;
                                return false;
                        }
 
                        in_transit = true;
                        // TODO: IResolveContext here
-                       EmitContext ec = new EmitContext (this, Parent, Location, null, MemberType, ModFlags);
-                       value = initializer.ResolveAsConstant (ec, this);
+                       EmitContext ec = new EmitContext (
+                               this, Parent, Location, null, MemberType, ModFlags);
+                       ec.InEnumContext = this is EnumMember;
+                       ec.IsAnonymousMethodAllowed = false;
+                       value = DoResolveValue (ec);
                        in_transit = false;
+                       resolved = true;
+                       return value != null;
+               }
 
+               protected virtual Constant DoResolveValue (EmitContext ec)
+               {
+                       Constant value = initializer.ResolveAsConstant (ec, this);
                        if (value == null)
-                               return false;
-
-                       value = value.ImplicitConversionRequired (MemberType, Location);
-                       if (value == null)
-                               return false;
+                               return null;
 
-                       if (!MemberType.IsValueType && MemberType != TypeManager.string_type && !value.IsDefaultValue) {
-                               Error_ConstantCanBeInitializedWithNullOnly (Location, GetSignatureForError ());
-                               return false;
+                       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 true;
+                       return c;
                }
 
-               public Constant Value {
-                       get {
-                               return value;
-                       }
+               public virtual Constant CreateConstantReference (Location loc)
+               {
+                       if (value == null)
+                               return null;
+
+                       return Constant.CreateConstant (value.Type, value.GetValue(), loc);
                }
 
                #endregion
@@ -201,14 +224,14 @@ namespace Mono.CSharp {
        public class ExternalConstant : IConstant
        {
                FieldInfo fi;
-               Constant value;
+               object value;
 
                public ExternalConstant (FieldInfo fi)
                {
                        this.fi = fi;
                }
 
-               private ExternalConstant (FieldInfo fi, Constant value):
+               private ExternalConstant (FieldInfo fi, object value):
                        this (fi)
                {
                        this.value = value;
@@ -229,7 +252,7 @@ namespace Mono.CSharp {
                                return null;
 
                        IConstant ic = new ExternalConstant (fi,
-                               new DecimalConstant (((System.Runtime.CompilerServices.DecimalConstantAttribute) attrs [0]).Value, Location.Null));
+                               ((System.Runtime.CompilerServices.DecimalConstantAttribute) attrs [0]).Value);
 
                        return ic;
                }
@@ -251,20 +274,13 @@ namespace Mono.CSharp {
                        if (value != null)
                                return true;
 
-                       if (fi.DeclaringType.IsEnum) {
-                               value = Expression.Constantify (fi.GetValue (fi), TypeManager.EnumToUnderlying (fi.FieldType));
-                               value = new EnumConstant (value, fi.DeclaringType);
-                               return true;
-                       }
-
-                       value = Expression.Constantify (fi.GetValue (fi), fi.FieldType);
+                       value = fi.GetValue (fi);
                        return true;
                }
 
-               public Constant Value {
-                       get {
-                               return value;
-                       }
+               public Constant CreateConstantReference (Location loc)
+               {
+                       return Constant.CreateConstant (fi.FieldType, value, loc);
                }
 
                #endregion