2007-01-10 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / mcs / const.cs
index 398a290229dd50de0208994447d87a4518411cbe..b947e4d68f5485ee6552e1e4abb33d32d71d8459 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;
                bool in_transit;
+               bool define_called;
 
                public const int AllowedModifiers =
                        Modifiers.NEW |
@@ -58,9 +59,13 @@ namespace Mono.CSharp {
                /// </summary>
                public override bool Define ()
                {
-                       // Make Define () idempotent, but ensure that the error check happens.
-                       if (FieldBuilder != null)
-                               return base.CheckBase ();
+                       // Because constant define can be called from other class
+                       if (define_called) {
+                               CheckBase ();
+                               return FieldBuilder != null;
+                       }
+
+                       define_called = true;
 
                        if (!base.Define ())
                                return false;
@@ -184,10 +189,12 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public Constant Value {
-                       get {
-                               return value;
-                       }
+               public Constant CreateConstantReference (Location loc)
+               {
+                       if (value == null)
+                               return null;
+
+                       return Constant.CreateConstant (value.Type, value.GetValue(), loc);
                }
 
                #endregion
@@ -196,14 +203,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;
@@ -224,7 +231,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;
                }
@@ -246,20 +253,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