2002-01-31 Duncan Mak <duncan@ximian.com>
[mono.git] / mcs / mcs / const.cs
index b253e54afbd576bf0f9a286e78835ecdb91ab34b..60dce6d89e862f819da4f3a1402679722d775557 100755 (executable)
@@ -8,6 +8,15 @@
 //
 //
 
+//
+// This is needed because the following situation arises:
+//
+//     The FieldBuilder is declared with the real type for an enumeration
+//
+//     When we attempt to set the value for the constant, the FieldBuilder.SetConstant
+//     function aborts because it requires its argument to be of the same type
+//
+
 namespace Mono.CSharp {
 
        using System;
@@ -15,12 +24,12 @@ namespace Mono.CSharp {
        using System.Reflection.Emit;
        using System.Collections;
 
-
        public class Const : MemberCore {
                public readonly string ConstantType;
                public Expression Expr;
                public Attributes  OptAttributes;
                public FieldBuilder FieldBuilder;
+
                object ConstantValue = null;
                Type type;
 
@@ -38,7 +47,7 @@ namespace Mono.CSharp {
                        ConstantType = constant_type;
                        Name = name;
                        Expr = expr;
-                       ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE);
+                       ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE, loc);
                        OptAttributes = attrs;
                }
 
@@ -49,6 +58,18 @@ namespace Mono.CSharp {
                        }
                }
 
+#if DEBUG
+               void dump_tree (Type t)
+               {
+                       Console.WriteLine ("Dumping hierarchy");
+                       while (t != null){
+                               Console.WriteLine ("   " + t.FullName + " " +
+                                       (t.GetType ().IsEnum ? "yes" : "no"));
+                               t = t.BaseType;
+                       }
+               }
+#endif
+
                /// <summary>
                ///   Defines the constant in the @parent
                /// </summary>
@@ -58,7 +79,7 @@ namespace Mono.CSharp {
 
                        if (type == null)
                                return false;
-                       
+
                        if (!TypeManager.IsBuiltinType (type) &&
                            (!type.IsSubclassOf (TypeManager.enum_type))) {
                                Report.Error (
@@ -103,19 +124,25 @@ namespace Mono.CSharp {
                                Report.Error (150, Location, "A constant value is expected");
                                return null;
                        }
-                       
-                       Expr = Expression.Reduce (ec, Expr);
 
-                       if (!(Expr is Literal)) {
+                       if (!(Expr is Constant)) {
                                Report.Error (150, Location, "A constant value is expected");
                                return null;
                        }
 
-                       ConstantValue = ((Literal) Expr).GetValue ();
+                       ConstantValue = ((Constant) Expr).GetValue ();
+
+                       if (type.IsEnum){
+                               //
+                               // This sadly does not work for our user-defined enumerations types ;-(
+                               //
+                               ConstantValue = System.Enum.ToObject (
+                                       type, ConstantValue);
+                       }
 
                        FieldBuilder.SetConstant (ConstantValue);
 
-                       if (!TypeManager.RegisterField (FieldBuilder, ConstantValue))
+                       if (!TypeManager.RegisterFieldValue (FieldBuilder, ConstantValue))
                                return null;
 
                        return ConstantValue;
@@ -127,9 +154,6 @@ namespace Mono.CSharp {
                /// </summary>
                public void EmitConstant (TypeContainer parent)
                {
-                       if (FieldBuilder == null)
-                               return;
-                       
                        EmitContext ec = new EmitContext (parent, Location, null, type, ModFlags);
                        LookupConstantValue (ec);