2007-03-15 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / mcs / const.cs
index 5da0b004f32c309b2eaff2478a3be1b2405b3bed..2588ab92aac252b9fc079d82917a265f45d29729 100644 (file)
@@ -24,8 +24,9 @@ namespace Mono.CSharp {
        }
 
        public class Const : FieldBase, IConstant {
-               Constant value;
+               protected Constant value;
                bool in_transit;
+               bool resolved;
                bool define_called;
 
                public const int AllowedModifiers =
@@ -82,7 +83,7 @@ namespace Mono.CSharp {
                        //      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'.
@@ -167,40 +168,47 @@ 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;
+                       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;
+                               return null;
 
-                       Constant c  = value.ConvertImplicitly (MemberType);
+                       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;
+                       return c;
                }
 
-               public Constant CreateConstantReference (Location loc)
+               public virtual Constant CreateConstantReference (Location loc)
                {
                        if (value == null)
                                return null;